- Revision
- 263026
- Author
- [email protected]
- Date
- 2020-06-15 02:19:31 -0700 (Mon, 15 Jun 2020)
Log Message
Simplify MediaRecorderPrivateWriterCocoa threading logic
https://bugs.webkit.org/show_bug.cgi?id=213126
Reviewed by Eric Carlson.
Always hop to the main thread when receiving compressed samples.
This makes the threading model simpler and pushing enqueued samples in the writer is not very performance sensitive.
Add handling of the special case of video samples being pushed but no compressed sample is yet produced.
In that case, when stopping recording, the samples will be pushed and we will start the writer.
Minor refactoring to remove some unnecessary checks and make sure some of the member fields gets initialized in their declaration.
Covered by existing tests.
* platform/mediarecorder/cocoa/AudioSampleBufferCompressor.h:
* platform/mediarecorder/cocoa/AudioSampleBufferCompressor.mm:
(WebCore::AudioSampleBufferCompressor::~AudioSampleBufferCompressor):
* platform/mediarecorder/cocoa/MediaRecorderPrivateWriterCocoa.h:
* platform/mediarecorder/cocoa/MediaRecorderPrivateWriterCocoa.mm:
(WebCore::MediaRecorderPrivateWriter::compressedVideoOutputBufferCallback):
(WebCore::MediaRecorderPrivateWriter::compressedAudioOutputBufferCallback):
(WebCore::MediaRecorderPrivateWriter::processNewCompressedVideoSampleBuffers):
(WebCore::MediaRecorderPrivateWriter::processNewCompressedAudioSampleBuffers):
(WebCore::MediaRecorderPrivateWriter::stopRecording):
* platform/mediarecorder/cocoa/VideoSampleBufferCompressor.h:
* platform/mediarecorder/cocoa/VideoSampleBufferCompressor.mm:
(WebCore::VideoSampleBufferCompressor::~VideoSampleBufferCompressor):
Modified Paths
Diff
Modified: trunk/Source/WebCore/ChangeLog (263025 => 263026)
--- trunk/Source/WebCore/ChangeLog 2020-06-15 09:13:08 UTC (rev 263025)
+++ trunk/Source/WebCore/ChangeLog 2020-06-15 09:19:31 UTC (rev 263026)
@@ -1,5 +1,35 @@
2020-06-15 Youenn Fablet <[email protected]>
+ Simplify MediaRecorderPrivateWriterCocoa threading logic
+ https://bugs.webkit.org/show_bug.cgi?id=213126
+
+ Reviewed by Eric Carlson.
+
+ Always hop to the main thread when receiving compressed samples.
+ This makes the threading model simpler and pushing enqueued samples in the writer is not very performance sensitive.
+
+ Add handling of the special case of video samples being pushed but no compressed sample is yet produced.
+ In that case, when stopping recording, the samples will be pushed and we will start the writer.
+
+ Minor refactoring to remove some unnecessary checks and make sure some of the member fields gets initialized in their declaration.
+ Covered by existing tests.
+
+ * platform/mediarecorder/cocoa/AudioSampleBufferCompressor.h:
+ * platform/mediarecorder/cocoa/AudioSampleBufferCompressor.mm:
+ (WebCore::AudioSampleBufferCompressor::~AudioSampleBufferCompressor):
+ * platform/mediarecorder/cocoa/MediaRecorderPrivateWriterCocoa.h:
+ * platform/mediarecorder/cocoa/MediaRecorderPrivateWriterCocoa.mm:
+ (WebCore::MediaRecorderPrivateWriter::compressedVideoOutputBufferCallback):
+ (WebCore::MediaRecorderPrivateWriter::compressedAudioOutputBufferCallback):
+ (WebCore::MediaRecorderPrivateWriter::processNewCompressedVideoSampleBuffers):
+ (WebCore::MediaRecorderPrivateWriter::processNewCompressedAudioSampleBuffers):
+ (WebCore::MediaRecorderPrivateWriter::stopRecording):
+ * platform/mediarecorder/cocoa/VideoSampleBufferCompressor.h:
+ * platform/mediarecorder/cocoa/VideoSampleBufferCompressor.mm:
+ (WebCore::VideoSampleBufferCompressor::~VideoSampleBufferCompressor):
+
+2020-06-15 Youenn Fablet <[email protected]>
+
Add a quirk to allow starting AudioContext if document was interacted
https://bugs.webkit.org/show_bug.cgi?id=213118
Modified: trunk/Source/WebCore/platform/mediarecorder/cocoa/AudioSampleBufferCompressor.h (263025 => 263026)
--- trunk/Source/WebCore/platform/mediarecorder/cocoa/AudioSampleBufferCompressor.h 2020-06-15 09:13:08 UTC (rev 263025)
+++ trunk/Source/WebCore/platform/mediarecorder/cocoa/AudioSampleBufferCompressor.h 2020-06-15 09:19:31 UTC (rev 263026)
@@ -59,10 +59,11 @@
void processSampleBuffersUntilLowWaterTime(CMTime);
OSStatus provideSourceDataNumOutputPackets(UInt32*, AudioBufferList*, AudioStreamPacketDescription**);
+ dispatch_queue_t m_serialDispatchQueue;
+ CMTime m_lowWaterTime;
+
RetainPtr<CMBufferQueueRef> m_outputBufferQueue;
RetainPtr<CMBufferQueueRef> m_inputBufferQueue;
- dispatch_queue_t m_serialDispatchQueue;
- CMTime m_lowWaterTime;
bool m_isEncoding { false };
RetainPtr<AudioConverterRef> m_converter;
@@ -70,7 +71,7 @@
AudioStreamBasicDescription m_destinationFormat;
RetainPtr<CMFormatDescriptionRef> m_destinationFormatDescription;
RetainPtr<NSNumber> m_gdrCountNum;
- UInt32 m_maxOutputPacketSize;
+ UInt32 m_maxOutputPacketSize { 0 };
Vector<AudioStreamPacketDescription> m_destinationPacketDescriptions;
CMTime m_currentNativePresentationTimeStamp;
@@ -81,9 +82,9 @@
Vector<char> m_destinationBuffer;
RetainPtr<CMBlockBufferRef> m_sampleBlockBuffer;
- size_t m_sampleBlockBufferSize;
- size_t m_currentOffsetInSampleBlockBuffer;
- AudioFormatID m_outputCodecType;
+ size_t m_sampleBlockBufferSize { 0 };
+ size_t m_currentOffsetInSampleBlockBuffer { 0 };
+ AudioFormatID m_outputCodecType { kAudioFormatMPEG4AAC };
};
}
Modified: trunk/Source/WebCore/platform/mediarecorder/cocoa/AudioSampleBufferCompressor.mm (263025 => 263026)
--- trunk/Source/WebCore/platform/mediarecorder/cocoa/AudioSampleBufferCompressor.mm 2020-06-15 09:13:08 UTC (rev 263025)
+++ trunk/Source/WebCore/platform/mediarecorder/cocoa/AudioSampleBufferCompressor.mm 2020-06-15 09:19:31 UTC (rev 263026)
@@ -52,14 +52,12 @@
AudioSampleBufferCompressor::AudioSampleBufferCompressor()
: m_serialDispatchQueue { dispatch_queue_create("com.apple.AudioSampleBufferCompressor", DISPATCH_QUEUE_SERIAL) }
, m_lowWaterTime { CMTimeMakeWithSeconds(LOW_WATER_TIME_IN_SECONDS, 1000) }
- , m_outputCodecType { kAudioFormatMPEG4AAC }
{
}
AudioSampleBufferCompressor::~AudioSampleBufferCompressor()
{
- if (m_serialDispatchQueue)
- dispatch_release(m_serialDispatchQueue);
+ dispatch_release(m_serialDispatchQueue);
}
bool AudioSampleBufferCompressor::initialize(CMBufferQueueTriggerCallback callback, void* callbackObject)
Modified: trunk/Source/WebCore/platform/mediarecorder/cocoa/MediaRecorderPrivateWriterCocoa.h (263025 => 263026)
--- trunk/Source/WebCore/platform/mediarecorder/cocoa/MediaRecorderPrivateWriterCocoa.h 2020-06-15 09:13:08 UTC (rev 263025)
+++ trunk/Source/WebCore/platform/mediarecorder/cocoa/MediaRecorderPrivateWriterCocoa.h 2020-06-15 09:19:31 UTC (rev 263026)
@@ -95,18 +95,18 @@
void flushCompressedSampleBuffers(CompletionHandler<void()>&&);
void appendEndOfVideoSampleDurationIfNeeded(CompletionHandler<void()>&&);
+ bool m_hasAudio { false };
+ bool m_hasVideo { false };
+
bool m_hasStartedWriting { false };
bool m_isStopped { false };
+ bool m_isStopping { false };
RetainPtr<AVAssetWriter> m_writer;
- bool m_isStopping { false };
RefPtr<SharedBuffer> m_data;
CompletionHandler<void(RefPtr<SharedBuffer>&&)> m_fetchDataCompletionHandler;
- bool m_hasAudio;
- bool m_hasVideo;
-
RetainPtr<CMFormatDescriptionRef> m_audioFormatDescription;
std::unique_ptr<AudioSampleBufferCompressor> m_audioCompressor;
RetainPtr<AVAssetWriterInput> m_audioAssetWriterInput;
Modified: trunk/Source/WebCore/platform/mediarecorder/cocoa/MediaRecorderPrivateWriterCocoa.mm (263025 => 263026)
--- trunk/Source/WebCore/platform/mediarecorder/cocoa/MediaRecorderPrivateWriterCocoa.mm 2020-06-15 09:13:08 UTC (rev 263025)
+++ trunk/Source/WebCore/platform/mediarecorder/cocoa/MediaRecorderPrivateWriterCocoa.mm 2020-06-15 09:19:31 UTC (rev 263026)
@@ -135,14 +135,18 @@
void MediaRecorderPrivateWriter::compressedVideoOutputBufferCallback(void *mediaRecorderPrivateWriter, CMBufferQueueTriggerToken)
{
- auto *writer = static_cast<MediaRecorderPrivateWriter*>(mediaRecorderPrivateWriter);
- writer->processNewCompressedVideoSampleBuffers();
+ callOnMainThread([weakWriter = makeWeakPtr(static_cast<MediaRecorderPrivateWriter*>(mediaRecorderPrivateWriter))] {
+ if (weakWriter)
+ weakWriter->processNewCompressedVideoSampleBuffers();
+ });
}
void MediaRecorderPrivateWriter::compressedAudioOutputBufferCallback(void *mediaRecorderPrivateWriter, CMBufferQueueTriggerToken)
{
- auto *writer = static_cast<MediaRecorderPrivateWriter*>(mediaRecorderPrivateWriter);
- writer->processNewCompressedAudioSampleBuffers();
+ callOnMainThread([weakWriter = makeWeakPtr(static_cast<MediaRecorderPrivateWriter*>(mediaRecorderPrivateWriter))] {
+ if (weakWriter)
+ weakWriter->processNewCompressedAudioSampleBuffers();
+ });
}
MediaRecorderPrivateWriter::MediaRecorderPrivateWriter(bool hasAudio, bool hasVideo)
@@ -188,16 +192,13 @@
ASSERT(m_hasVideo);
if (!m_videoFormatDescription) {
m_videoFormatDescription = CMSampleBufferGetFormatDescription(m_videoCompressor->getOutputSampleBuffer());
- callOnMainThread([weakThis = makeWeakPtr(this), this] {
- if (!weakThis)
- return;
- if (m_hasAudio && !m_audioFormatDescription)
- return;
+ if (m_hasAudio && !m_audioFormatDescription)
+ return;
- startAssetWriter();
- });
+ startAssetWriter();
}
+
if (!m_hasStartedWriting)
return;
appendCompressedSampleBuffers();
@@ -208,16 +209,12 @@
ASSERT(m_hasAudio);
if (!m_audioFormatDescription) {
m_audioFormatDescription = CMSampleBufferGetFormatDescription(m_audioCompressor->getOutputSampleBuffer());
- callOnMainThread([weakThis = makeWeakPtr(this), this] {
- if (!weakThis)
- return;
+ if (m_hasVideo && !m_videoFormatDescription)
+ return;
- if (m_hasVideo && !m_videoFormatDescription)
- return;
+ startAssetWriter();
+ }
- startAssetWriter();
- });
- }
if (!m_hasStartedWriting)
return;
appendCompressedSampleBuffers();
@@ -435,35 +432,43 @@
if (m_audioCompressor)
m_audioCompressor->finish();
- if (!m_hasStartedWriting)
- return;
- ASSERT([m_writer status] == AVAssetWriterStatusWriting);
-
m_isStopping = true;
+ // We hop to the main thread since finishing the video compressor might trigger starting the writer asynchronously.
+ callOnMainThread([this, weakThis = makeWeakPtr(this)]() mutable {
+ auto whenFinished = [this] {
+ m_isStopping = false;
+ if (m_fetchDataCompletionHandler) {
+ auto buffer = WTFMove(m_data);
+ m_fetchDataCompletionHandler(WTFMove(buffer));
+ }
- flushCompressedSampleBuffers([this, weakThis = makeWeakPtr(this)]() mutable {
- if (!weakThis)
+ m_isStopped = false;
+ m_hasStartedWriting = false;
+ clear();
+ };
+
+ if (!m_hasStartedWriting) {
+ whenFinished();
return;
+ }
- ALLOW_DEPRECATED_DECLARATIONS_BEGIN
- [m_writer flush];
- ALLOW_DEPRECATED_DECLARATIONS_END
- [m_writer finishWritingWithCompletionHandler:[this, weakThis = WTFMove(weakThis)]() mutable {
- callOnMainThread([this, weakThis = WTFMove(weakThis)]() mutable {
- if (!weakThis)
- return;
+ ASSERT([m_writer status] == AVAssetWriterStatusWriting);
+ flushCompressedSampleBuffers([this, weakThis = WTFMove(weakThis), whenFinished = WTFMove(whenFinished)]() mutable {
+ if (!weakThis)
+ return;
- m_isStopping = false;
- if (m_fetchDataCompletionHandler) {
- auto buffer = WTFMove(m_data);
- m_fetchDataCompletionHandler(WTFMove(buffer));
- }
+ ALLOW_DEPRECATED_DECLARATIONS_BEGIN
+ [m_writer flush];
+ ALLOW_DEPRECATED_DECLARATIONS_END
- m_isStopped = false;
- m_hasStartedWriting = false;
- clear();
- });
- }];
+ [m_writer finishWritingWithCompletionHandler:[weakThis = WTFMove(weakThis), whenFinished = WTFMove(whenFinished)]() mutable {
+ callOnMainThread([weakThis = WTFMove(weakThis), whenFinished = WTFMove(whenFinished)]() mutable {
+ if (!weakThis)
+ return;
+ whenFinished();
+ });
+ }];
+ });
});
}
Modified: trunk/Source/WebCore/platform/mediarecorder/cocoa/VideoSampleBufferCompressor.h (263025 => 263026)
--- trunk/Source/WebCore/platform/mediarecorder/cocoa/VideoSampleBufferCompressor.h 2020-06-15 09:13:08 UTC (rev 263025)
+++ trunk/Source/WebCore/platform/mediarecorder/cocoa/VideoSampleBufferCompressor.h 2020-06-15 09:19:31 UTC (rev 263026)
@@ -56,12 +56,12 @@
static void videoCompressionCallback(void *refCon, void*, OSStatus, VTEncodeInfoFlags, CMSampleBufferRef);
dispatch_queue_t m_serialDispatchQueue;
+ CMVideoCodecType m_outputCodecType { kCMVideoCodecType_H264 };
+
RetainPtr<CMBufferQueueRef> m_outputBufferQueue;
RetainPtr<VTCompressionSessionRef> m_vtSession;
bool m_isEncoding { false };
-
- CMVideoCodecType m_outputCodecType;
float m_maxKeyFrameIntervalDuration { 2.0 };
unsigned m_expectedFrameRate { 30 };
};
Modified: trunk/Source/WebCore/platform/mediarecorder/cocoa/VideoSampleBufferCompressor.mm (263025 => 263026)
--- trunk/Source/WebCore/platform/mediarecorder/cocoa/VideoSampleBufferCompressor.mm 2020-06-15 09:13:08 UTC (rev 263025)
+++ trunk/Source/WebCore/platform/mediarecorder/cocoa/VideoSampleBufferCompressor.mm 2020-06-15 09:19:31 UTC (rev 263026)
@@ -55,8 +55,7 @@
VideoSampleBufferCompressor::~VideoSampleBufferCompressor()
{
- if (m_serialDispatchQueue)
- dispatch_release(m_serialDispatchQueue);
+ dispatch_release(m_serialDispatchQueue);
}
bool VideoSampleBufferCompressor::initialize(CMBufferQueueTriggerCallback callback, void* callbackObject)