|
Line 0
a/Source/WebCore/platform/mediarecorder/MediaRecorderPrivateWriter.mm_sec1
|
|
|
1 |
/* |
| 2 |
* Copyright (C) 2015 Apple Inc. All rights reserved. |
| 3 |
* |
| 4 |
* Redistribution and use in source and binary forms, with or without |
| 5 |
* modification, are permitted provided that the following conditions |
| 6 |
* are met: |
| 7 |
* 1. Redistributions of source code must retain the above copyright |
| 8 |
* notice, this list of conditions and the following disclaimer. |
| 9 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 10 |
* notice, this list of conditions and the following disclaimer in the |
| 11 |
* documentation and/or other materials provided with the distribution. |
| 12 |
* |
| 13 |
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' |
| 14 |
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 15 |
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 16 |
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS |
| 17 |
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 18 |
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 19 |
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 20 |
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 21 |
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 22 |
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| 23 |
* THE POSSIBILITY OF SUCH DAMAGE. |
| 24 |
*/ |
| 25 |
|
| 26 |
#include "config.h" |
| 27 |
#include "MediaRecorderPrivateWriter.h" |
| 28 |
|
| 29 |
#if ENABLE(MEDIA_STREAM) && USE(AVFOUNDATION) |
| 30 |
|
| 31 |
#include "AudioStreamDescription.h" |
| 32 |
#include "Blob.h" |
| 33 |
#include "FileSystem.h" |
| 34 |
#include "SharedBuffer.h" |
| 35 |
#include "WebAudioBufferList.h" |
| 36 |
#include <AVFoundation/AVAssetWriter.h> |
| 37 |
#include <AVFoundation/AVAssetWriterInput.h> |
| 38 |
#include <pal/cf/CoreMediaSoftLink.h> |
| 39 |
|
| 40 |
typedef AVAssetWriter AVAssetWriterType; |
| 41 |
typedef AVAssetWriterInput AVAssetWriterInputType; |
| 42 |
|
| 43 |
SOFT_LINK_FRAMEWORK_OPTIONAL(AVFoundation) |
| 44 |
|
| 45 |
SOFT_LINK_CLASS(AVFoundation, AVAssetWriter) |
| 46 |
SOFT_LINK_CLASS(AVFoundation, AVAssetWriterInput) |
| 47 |
|
| 48 |
SOFT_LINK_CONSTANT(AVFoundation, AVFileTypeMPEG4, NSString *) |
| 49 |
SOFT_LINK_CONSTANT(AVFoundation, AVVideoCodecKey, NSString *) |
| 50 |
SOFT_LINK_CONSTANT(AVFoundation, AVVideoCodecTypeH264, NSString *) |
| 51 |
SOFT_LINK_CONSTANT(AVFoundation, AVVideoWidthKey, NSString *) |
| 52 |
SOFT_LINK_CONSTANT(AVFoundation, AVVideoHeightKey, NSString *) |
| 53 |
SOFT_LINK_CONSTANT(AVFoundation, AVMediaTypeVideo, NSString *) |
| 54 |
SOFT_LINK_CONSTANT(AVFoundation, AVMediaTypeAudio, NSString *) |
| 55 |
SOFT_LINK_CONSTANT(AVFoundation, AVEncoderBitRatePerChannelKey, NSString *) |
| 56 |
SOFT_LINK_CONSTANT(AVFoundation, AVFormatIDKey, NSString *) |
| 57 |
SOFT_LINK_CONSTANT(AVFoundation, AVNumberOfChannelsKey, NSString *) |
| 58 |
SOFT_LINK_CONSTANT(AVFoundation, AVSampleRateKey, NSString *) |
| 59 |
|
| 60 |
#define AVFileTypeMPEG4 getAVFileTypeMPEG4() |
| 61 |
#define AVMediaTypeAudio getAVMediaTypeAudio() |
| 62 |
#define AVMediaTypeVideo getAVMediaTypeVideo() |
| 63 |
#define AVVideoCodecKey getAVVideoCodecKey() |
| 64 |
#define AVVideoCodecTypeH264 getAVVideoCodecTypeH264() |
| 65 |
#define AVVideoWidthKey getAVVideoWidthKey() |
| 66 |
#define AVVideoHeightKey getAVVideoHeightKey() |
| 67 |
#define AVEncoderBitRatePerChannelKey getAVEncoderBitRatePerChannelKey() |
| 68 |
#define AVFormatIDKey getAVFormatIDKey() |
| 69 |
#define AVNumberOfChannelsKey getAVNumberOfChannelsKey() |
| 70 |
#define AVSampleRateKey getAVSampleRateKey() |
| 71 |
|
| 72 |
using namespace WebCore; |
| 73 |
|
| 74 |
namespace WebCore { |
| 75 |
|
| 76 |
using namespace PAL; |
| 77 |
|
| 78 |
bool MediaRecorderPrivateWriter::setupWriter() |
| 79 |
{ |
| 80 |
if (m_writer) |
| 81 |
return false; |
| 82 |
|
| 83 |
NSString *directory = FileSystem::createTemporaryDirectory(@"videos"); |
| 84 |
NSString *path = [directory stringByAppendingString:@"/test.mp4"]; |
| 85 |
NSURL *outputURL = [NSURL fileURLWithPath:path]; |
| 86 |
m_path = [path UTF8String]; |
| 87 |
NSError *error = nil; |
| 88 |
m_writer = adoptNS([allocAVAssetWriterInstance() initWithURL:outputURL fileType:AVFileTypeMPEG4 error:&error]); |
| 89 |
if (error) |
| 90 |
return false; |
| 91 |
return true; |
| 92 |
} |
| 93 |
|
| 94 |
bool MediaRecorderPrivateWriter::setVideoInput(int width, int height) |
| 95 |
{ |
| 96 |
if (m_videoInput) |
| 97 |
return false; |
| 98 |
NSDictionary *videoSettings = @{ AVVideoCodecKey: AVVideoCodecTypeH264, AVVideoWidthKey: [NSNumber numberWithInt:width], AVVideoHeightKey: [NSNumber numberWithInt:height] }; |
| 99 |
m_videoInput = adoptNS([allocAVAssetWriterInputInstance() initWithMediaType:AVMediaTypeVideo outputSettings:videoSettings sourceFormatHint:NULL]); |
| 100 |
[m_videoInput setExpectsMediaDataInRealTime:true]; |
| 101 |
|
| 102 |
if (![m_writer canAddInput:m_videoInput.get()]) |
| 103 |
return false; |
| 104 |
[m_writer addInput:m_videoInput.get()]; |
| 105 |
m_videoPullQueue = dispatch_queue_create("WebCoreVideoRecordingPullBufferQueue", DISPATCH_QUEUE_SERIAL); |
| 106 |
return true; |
| 107 |
} |
| 108 |
|
| 109 |
bool MediaRecorderPrivateWriter::setAudioInput() |
| 110 |
{ |
| 111 |
if (m_audioInput) |
| 112 |
return false; |
| 113 |
NSDictionary *audioSettings = @{ AVEncoderBitRatePerChannelKey : @(28000), AVFormatIDKey : @(kAudioFormatMPEG4AAC), AVNumberOfChannelsKey : @(1), AVSampleRateKey : @(22050) }; |
| 114 |
|
| 115 |
m_audioInput = adoptNS([allocAVAssetWriterInputInstance() initWithMediaType:AVMediaTypeAudio outputSettings:audioSettings sourceFormatHint:NULL]); |
| 116 |
[m_audioInput setExpectsMediaDataInRealTime:true]; |
| 117 |
|
| 118 |
if (![m_writer canAddInput:m_audioInput.get()]) |
| 119 |
return false; |
| 120 |
[m_writer addInput:m_audioInput.get()]; |
| 121 |
m_audioPullQueue = dispatch_queue_create("WebCoreAudioRecordingPushBufferQueue", DISPATCH_QUEUE_SERIAL); |
| 122 |
return true; |
| 123 |
} |
| 124 |
|
| 125 |
void MediaRecorderPrivateWriter::appendVideoSampleBuffer(CMSampleBufferRef sampleBuffer) |
| 126 |
{ |
| 127 |
if (!canWrite) { |
| 128 |
bool success = [m_writer startWriting]; |
| 129 |
if (!success) |
| 130 |
isStopped = true; |
| 131 |
CMTime startTime = CMClockGetTime(CMClockGetHostTimeClock()); |
| 132 |
[m_writer startSessionAtSourceTime:startTime]; |
| 133 |
canWrite = true; |
| 134 |
[m_videoInput requestMediaDataWhenReadyOnQueue:m_videoPullQueue usingBlock:[this, weakThis = makeWeakPtr(*this)] { |
| 135 |
do { |
| 136 |
if (!weakThis) |
| 137 |
return; |
| 138 |
if (![m_videoInput isReadyForMoreMediaData]) |
| 139 |
break; |
| 140 |
auto locker = holdLock(m_videoLock); |
| 141 |
if (m_videoBufferPool.isEmpty()) |
| 142 |
break; |
| 143 |
auto buf = m_videoBufferPool.takeFirst(); |
| 144 |
locker.unlockEarly(); |
| 145 |
if (![m_videoInput appendSampleBuffer:buf.get()]) |
| 146 |
break; |
| 147 |
} while (true); |
| 148 |
if (isStopped && m_videoBufferPool.isEmpty()) { |
| 149 |
[m_videoInput markAsFinished]; |
| 150 |
m_FinishWritingVideoSemaphore.signal(); |
| 151 |
} |
| 152 |
}]; |
| 153 |
return; |
| 154 |
} |
| 155 |
CMTime startTime = CMClockGetTime(CMClockGetHostTimeClock()); |
| 156 |
CMItemCount count; |
| 157 |
CMSampleBufferGetSampleTimingInfoArray(sampleBuffer, 0, nil, &count); |
| 158 |
CMSampleTimingInfo* pInfo = new CMSampleTimingInfo[count]; |
| 159 |
CMSampleBufferGetSampleTimingInfoArray(sampleBuffer, count, pInfo, &count); |
| 160 |
for (CMItemCount i = 0; i < count; i++) { |
| 161 |
pInfo[i].decodeTimeStamp = kCMTimeInvalid; |
| 162 |
pInfo[i].presentationTimeStamp = startTime; |
| 163 |
} |
| 164 |
CMSampleBufferRef sout; |
| 165 |
CMSampleBufferCreateCopyWithNewTiming(kCFAllocatorDefault, sampleBuffer, count, pInfo, &sout); |
| 166 |
free(pInfo); |
| 167 |
auto locker = holdLock(m_videoLock); |
| 168 |
if (isStopped) |
| 169 |
return; |
| 170 |
m_videoBufferPool.append(retainPtr(sout)); |
| 171 |
} |
| 172 |
|
| 173 |
void MediaRecorderPrivateWriter::appendAudioSampleBuffer(const PlatformAudioData& data, const AudioStreamDescription& description, const WTF::MediaTime&, size_t sampleCount) |
| 174 |
{ |
| 175 |
if ((!canWrite && m_videoInput) || isStopped) |
| 176 |
return; |
| 177 |
CMSampleBufferRef sampleBuffer; |
| 178 |
CMFormatDescriptionRef format; |
| 179 |
OSStatus error; |
| 180 |
auto& basicDescription = *WTF::get<const AudioStreamBasicDescription*>(description.platformDescription().description); |
| 181 |
error = CMAudioFormatDescriptionCreate(kCFAllocatorDefault, &basicDescription, 0, NULL, 0, NULL, NULL, &format); |
| 182 |
if (firstAudioSample) { |
| 183 |
if (!m_videoInput) { |
| 184 |
// audio-only recording |
| 185 |
bool success = [m_writer startWriting]; |
| 186 |
if (!success) |
| 187 |
isStopped = true; |
| 188 |
CMTime startTime = CMClockGetTime(CMClockGetHostTimeClock()); |
| 189 |
[m_writer startSessionAtSourceTime:startTime]; |
| 190 |
canWrite = true; |
| 191 |
} |
| 192 |
firstAudioSample = false; |
| 193 |
[m_audioInput requestMediaDataWhenReadyOnQueue:m_audioPullQueue usingBlock:[this, weakThis = makeWeakPtr(*this)] { |
| 194 |
do { |
| 195 |
if (!weakThis) |
| 196 |
return; |
| 197 |
if (![m_audioInput isReadyForMoreMediaData]) |
| 198 |
break; |
| 199 |
auto locker = holdLock(m_audioLock); |
| 200 |
if (m_audioBufferPool.isEmpty()) |
| 201 |
break; |
| 202 |
auto buf = m_audioBufferPool.takeFirst(); |
| 203 |
locker.unlockEarly(); |
| 204 |
[m_audioInput appendSampleBuffer:buf.get()]; |
| 205 |
} while (true); |
| 206 |
if (isStopped && m_audioBufferPool.isEmpty()) { |
| 207 |
[m_audioInput markAsFinished]; |
| 208 |
m_FinishWritingAudioSemaphore.signal(); |
| 209 |
} |
| 210 |
}]; |
| 211 |
} |
| 212 |
CMTime startTime = CMClockGetTime(CMClockGetHostTimeClock()); |
| 213 |
|
| 214 |
error = CMAudioSampleBufferCreateWithPacketDescriptions(kCFAllocatorDefault, NULL, false, NULL, NULL, format, sampleCount, startTime, NULL, &sampleBuffer); |
| 215 |
if (error) |
| 216 |
return; |
| 217 |
error = CMSampleBufferSetDataBufferFromAudioBufferList(sampleBuffer, kCFAllocatorDefault, kCFAllocatorDefault, 0, downcast<WebAudioBufferList>(data).list()); |
| 218 |
if (error) |
| 219 |
return; |
| 220 |
|
| 221 |
auto locker = holdLock(m_audioLock); |
| 222 |
m_audioBufferPool.append(retainPtr(sampleBuffer)); |
| 223 |
} |
| 224 |
|
| 225 |
void MediaRecorderPrivateWriter::stopRecording() |
| 226 |
{ |
| 227 |
isStopped = true; |
| 228 |
ASSERT([m_writer status] == AVAssetWriterStatusWriting); |
| 229 |
if (m_videoInput) |
| 230 |
m_FinishWritingVideoSemaphore.wait(); |
| 231 |
|
| 232 |
if (m_audioInput) |
| 233 |
m_FinishWritingAudioSemaphore.wait(); |
| 234 |
|
| 235 |
[m_writer finishWritingWithCompletionHandler:^{ |
| 236 |
isStopped = false; |
| 237 |
canWrite = false; |
| 238 |
firstAudioSample = true; |
| 239 |
if (m_videoInput) { |
| 240 |
m_videoInput.clear(); |
| 241 |
m_videoInput = NULL; |
| 242 |
dispatch_release(m_videoPullQueue); |
| 243 |
} |
| 244 |
if (m_audioInput) { |
| 245 |
m_audioInput.clear(); |
| 246 |
m_audioInput = NULL; |
| 247 |
dispatch_release(m_audioPullQueue); |
| 248 |
} |
| 249 |
m_writer.clear(); |
| 250 |
m_writer = NULL; |
| 251 |
m_FinishWritingSemaphore.signal(); |
| 252 |
}]; |
| 253 |
m_FinishWritingSemaphore.wait(); |
| 254 |
} |
| 255 |
|
| 256 |
Ref<Blob> MediaRecorderPrivateWriter::fetchData() |
| 257 |
{ |
| 258 |
if (m_path.isEmpty()) |
| 259 |
return Blob::create(); |
| 260 |
|
| 261 |
RefPtr<SharedBuffer> data = SharedBuffer::createWithContentsOfFile(m_path); |
| 262 |
if (!data) |
| 263 |
return Blob::create(); |
| 264 |
return Blob::create(*data, "video/mp4"); // FIXME: we will need to support more MIME types |
| 265 |
} |
| 266 |
|
| 267 |
} // namespace WebCore |
| 268 |
|
| 269 |
#endif // ENABLE(MEDIA_STREAM) && USE(AVFOUNDATION) |