Diff
Modified: trunk/Source/WebCore/ChangeLog (270946 => 270947)
--- trunk/Source/WebCore/ChangeLog 2020-12-17 22:21:07 UTC (rev 270946)
+++ trunk/Source/WebCore/ChangeLog 2020-12-17 22:23:39 UTC (rev 270947)
@@ -1,3 +1,35 @@
+2020-12-17 Chris Dumez <[email protected]>
+
+ [WebAudio] Simplify code related to dispatchToRenderThread
+ https://bugs.webkit.org/show_bug.cgi?id=219990
+
+ Reviewed by Geoffrey Garen.
+
+ Simplify code related to dispatchToRenderThread in WebAudio:
+ 1. AudioDestination::start() now always gets called with a non-null dispatchToRenderThread lambda.
+ In the case where there is no AudioWorkletThread to dispatch to, the lambda simply calls its
+ task synchronously.
+ 2. For Cocoa ports, make it so that only AudioDestinationCocoa needs to worry about the
+ dispatchToRenderThread lambda. The dispatchToRenderThread lambda is no longer exposed to
+ subclasses such as MockAudioDestinationCocoa & RemoteAudioDestinationProxy.
+
+ * Modules/webaudio/DefaultAudioDestinationNode.cpp:
+ (WebCore::Function<void):
+ * platform/audio/cocoa/AudioDestinationCocoa.cpp:
+ (WebCore::AudioDestinationCocoa::start):
+ (WebCore::AudioDestinationCocoa::startRendering):
+ (WebCore::AudioDestinationCocoa::stop):
+ (WebCore::AudioDestinationCocoa::stopRendering):
+ (WebCore::AudioDestinationCocoa::render):
+ * platform/audio/cocoa/AudioDestinationCocoa.h:
+ * platform/audio/gstreamer/AudioDestinationGStreamer.cpp:
+ (WebCore::AudioDestinationGStreamer::start):
+ * platform/mock/MockAudioDestinationCocoa.cpp:
+ (WebCore::MockAudioDestinationCocoa::startRendering):
+ (WebCore::MockAudioDestinationCocoa::stopRendering):
+ (WebCore::MockAudioDestinationCocoa::tick):
+ * platform/mock/MockAudioDestinationCocoa.h:
+
2020-12-17 Jer Noble <[email protected]>
[macOS] Use low-power audio buffer sizes for more output devices
Modified: trunk/Source/WebCore/Headers.cmake (270946 => 270947)
--- trunk/Source/WebCore/Headers.cmake 2020-12-17 22:21:07 UTC (rev 270946)
+++ trunk/Source/WebCore/Headers.cmake 2020-12-17 22:23:39 UTC (rev 270947)
@@ -1117,6 +1117,8 @@
platform/audio/PlatformMediaSessionManager.h
platform/audio/PushPullFIFO.h
+ platform/audio/gstreamer/AudioDestinationGStreamer.h
+
platform/encryptedmedia/CDMEncryptionScheme.h
platform/encryptedmedia/CDMFactory.h
platform/encryptedmedia/CDMInstance.h
Modified: trunk/Source/WebCore/Modules/webaudio/DefaultAudioDestinationNode.cpp (270946 => 270947)
--- trunk/Source/WebCore/Modules/webaudio/DefaultAudioDestinationNode.cpp 2020-12-17 22:21:07 UTC (rev 270946)
+++ trunk/Source/WebCore/Modules/webaudio/DefaultAudioDestinationNode.cpp 2020-12-17 22:23:39 UTC (rev 270947)
@@ -133,7 +133,7 @@
}, WorkerRunLoop::defaultMode());
};
}
- return { };
+ return [](Function<void()>&& function) { function(); };
}
void DefaultAudioDestinationNode::startRendering(CompletionHandler<void(Optional<Exception>&&)>&& completionHandler)
Modified: trunk/Source/WebCore/platform/audio/cocoa/AudioDestinationCocoa.cpp (270946 => 270947)
--- trunk/Source/WebCore/platform/audio/cocoa/AudioDestinationCocoa.cpp 2020-12-17 22:21:07 UTC (rev 270946)
+++ trunk/Source/WebCore/platform/audio/cocoa/AudioDestinationCocoa.cpp 2020-12-17 22:23:39 UTC (rev 270947)
@@ -104,8 +104,18 @@
void AudioDestinationCocoa::start(Function<void(Function<void()>&&)>&& dispatchToRenderThread, CompletionHandler<void(bool)>&& completionHandler)
{
+ ASSERT(isMainThread());
LOG(Media, "AudioDestinationCocoa::start");
- m_dispatchToRenderThread = WTFMove(dispatchToRenderThread);
+ {
+ auto locker = holdLock(m_dispatchToRenderThreadLock);
+ m_dispatchToRenderThread = WTFMove(dispatchToRenderThread);
+ }
+ startRendering(WTFMove(completionHandler));
+}
+
+void AudioDestinationCocoa::startRendering(CompletionHandler<void(bool)>&& completionHandler)
+{
+ ASSERT(isMainThread());
auto success = m_audioOutputUnitAdaptor.start() == noErr;
if (success)
setIsPlaying(true);
@@ -117,24 +127,25 @@
void AudioDestinationCocoa::stop(CompletionHandler<void(bool)>&& completionHandler)
{
+ ASSERT(isMainThread());
LOG(Media, "AudioDestinationCocoa::stop");
+ stopRendering(WTFMove(completionHandler));
+ {
+ auto locker = holdLock(m_dispatchToRenderThreadLock);
+ m_dispatchToRenderThread = nullptr;
+ }
+}
+
+void AudioDestinationCocoa::stopRendering(CompletionHandler<void(bool)>&& completionHandler)
+{
+ ASSERT(isMainThread());
auto success = m_audioOutputUnitAdaptor.stop() == noErr;
- auto dispatchToRenderThread = std::exchange(m_dispatchToRenderThread, nullptr);
if (success)
setIsPlaying(false);
- auto callCompletionHandlerOnMainThread = [completionHandler = WTFMove(completionHandler), success]() mutable {
- callOnMainThread([completionHandler = WTFMove(completionHandler), success]() mutable {
- completionHandler(success);
- });
- };
-
- if (dispatchToRenderThread) {
- // Do a round-trip to the worklet thread to make sure we call the completion handler after
- // the last rendering quantum has been processed by the worklet thread.
- dispatchToRenderThread(WTFMove(callCompletionHandlerOnMainThread));
- } else
- callCompletionHandlerOnMainThread();
+ callOnMainThread([completionHandler = WTFMove(completionHandler), success]() mutable {
+ completionHandler(success);
+ });
}
void AudioDestinationCocoa::setIsPlaying(bool isPlaying)
@@ -187,6 +198,8 @@
// Pulls on our provider to get rendered audio stream.
OSStatus AudioDestinationCocoa::render(double sampleTime, uint64_t hostTime, UInt32 numberOfFrames, AudioBufferList* ioData)
{
+ ASSERT(!isMainThread());
+
if (!hasEnoughFrames(numberOfFrames))
return noErr;
@@ -208,15 +221,16 @@
}
// When there is a AudioWorklet, we do rendering on the AudioWorkletThread.
- if (m_dispatchToRenderThread) {
- m_dispatchToRenderThread([this, protectedThis = makeRef(*this), framesToRender]() mutable {
- auto locker = tryHoldLock(m_isPlayingLock);
- if (locker && m_isPlaying)
- renderOnRenderingThead(framesToRender);
- });
- } else
- renderOnRenderingThead(framesToRender);
+ auto locker = tryHoldLock(m_dispatchToRenderThreadLock);
+ if (!locker || !m_dispatchToRenderThread)
+ return -1;
+ m_dispatchToRenderThread([this, protectedThis = makeRef(*this), framesToRender]() mutable {
+ auto locker = tryHoldLock(m_isPlayingLock);
+ if (locker && m_isPlaying)
+ renderOnRenderingThead(framesToRender);
+ });
+
return noErr;
}
Modified: trunk/Source/WebCore/platform/audio/cocoa/AudioDestinationCocoa.h (270946 => 270947)
--- trunk/Source/WebCore/platform/audio/cocoa/AudioDestinationCocoa.h 2020-12-17 22:21:07 UTC (rev 270946)
+++ trunk/Source/WebCore/platform/audio/cocoa/AudioDestinationCocoa.h 2020-12-17 22:23:39 UTC (rev 270947)
@@ -65,9 +65,12 @@
private:
friend Ref<AudioDestination> AudioDestination::create(AudioIOCallback&, const String&, unsigned, unsigned, float);
- void start(Function<void(Function<void()>&&)>&&, CompletionHandler<void(bool)>&&) override;
- void stop(CompletionHandler<void(bool)>&&) override;
+ WEBCORE_EXPORT void start(Function<void(Function<void()>&&)>&& dispatchToRenderThread, CompletionHandler<void(bool)>&&) final;
+ WEBCORE_EXPORT void stop(CompletionHandler<void(bool)>&&) final;
+ virtual void startRendering(CompletionHandler<void(bool)>&&);
+ virtual void stopRendering(CompletionHandler<void(bool)>&&);
+
void renderOnRenderingThead(size_t framesToRender);
AudioOutputUnitAdaptor m_audioOutputUnitAdaptor;
@@ -86,6 +89,7 @@
std::unique_ptr<MultiChannelResampler> m_resampler;
AudioIOPosition m_outputTimestamp;
+ Lock m_dispatchToRenderThreadLock;
Function<void(Function<void()>&&)> m_dispatchToRenderThread;
float m_contextSampleRate;
Modified: trunk/Source/WebCore/platform/audio/gstreamer/AudioDestinationGStreamer.cpp (270946 => 270947)
--- trunk/Source/WebCore/platform/audio/gstreamer/AudioDestinationGStreamer.cpp 2020-12-17 22:21:07 UTC (rev 270946)
+++ trunk/Source/WebCore/platform/audio/gstreamer/AudioDestinationGStreamer.cpp 2020-12-17 22:23:39 UTC (rev 270947)
@@ -219,12 +219,15 @@
void AudioDestinationGStreamer::start(Function<void(Function<void()>&&)>&& dispatchToRenderThread, CompletionHandler<void(bool)>&& completionHandler)
{
+ webkitWebAudioSourceSetDispatchToRenderThreadCallback(WEBKIT_WEB_AUDIO_SRC(m_src.get()), WTFMove(dispatchToRenderThread));
+ startRendering(WTFMove(completionHandler));
+}
+
+void AudioDestinationGStreamer::startRendering(CompletionHandler<void(bool)>&& completionHandler)
+{
ASSERT(m_audioSinkAvailable);
bool success = false;
if (m_audioSinkAvailable) {
- if (dispatchToRenderThread)
- webkitWebAudioSourceSetDispatchToRenderThreadCallback(WEBKIT_WEB_AUDIO_SRC(m_src.get()), WTFMove(dispatchToRenderThread));
-
GST_DEBUG("Starting");
if (gst_element_set_state(m_pipeline.get(), GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE) {
g_warning("Error: Failed to set pipeline to playing");
@@ -242,6 +245,11 @@
void AudioDestinationGStreamer::stop(CompletionHandler<void(bool)>&& completionHandler)
{
+ stopRendering(WTFMove(completionHandler));
+}
+
+void AudioDestinationGStreamer::stopRendering(CompletionHandler<void(bool)>&& completionHandler)
+{
ASSERT(m_audioSinkAvailable);
bool success = false;
if (m_audioSinkAvailable) {
Modified: trunk/Source/WebCore/platform/audio/gstreamer/AudioDestinationGStreamer.h (270946 => 270947)
--- trunk/Source/WebCore/platform/audio/gstreamer/AudioDestinationGStreamer.h 2020-12-17 22:21:07 UTC (rev 270946)
+++ trunk/Source/WebCore/platform/audio/gstreamer/AudioDestinationGStreamer.h 2020-12-17 22:23:39 UTC (rev 270947)
@@ -34,8 +34,8 @@
AudioDestinationGStreamer(AudioIOCallback&, unsigned long numberOfOutputChannels, float sampleRate);
virtual ~AudioDestinationGStreamer();
- void start(Function<void(Function<void()>&&)>&& dispatchToRenderThread, CompletionHandler<void(bool)>&&) override;
- void stop(CompletionHandler<void(bool)>&&) override;
+ WEBCORE_EXPORT void start(Function<void(Function<void()>&&)>&& dispatchToRenderThread, CompletionHandler<void(bool)>&&) final;
+ WEBCORE_EXPORT void stop(CompletionHandler<void(bool)>&&) final;
bool isPlaying() override { return m_isPlaying; }
float sampleRate() const override { return m_sampleRate; }
@@ -43,6 +43,10 @@
gboolean handleMessage(GstMessage*);
+protected:
+ virtual void startRendering(CompletionHandler<void(bool)>&&);
+ virtual void stopRendering(CompletionHandler<void(bool)>&&);
+
private:
RefPtr<AudioBus> m_renderBus;
Modified: trunk/Source/WebCore/platform/mock/MockAudioDestinationCocoa.cpp (270946 => 270947)
--- trunk/Source/WebCore/platform/mock/MockAudioDestinationCocoa.cpp 2020-12-17 22:21:07 UTC (rev 270946)
+++ trunk/Source/WebCore/platform/mock/MockAudioDestinationCocoa.cpp 2020-12-17 22:23:39 UTC (rev 270947)
@@ -45,15 +45,8 @@
MockAudioDestinationCocoa::~MockAudioDestinationCocoa() = default;
-void MockAudioDestinationCocoa::start(Function<void(Function<void()>&&)>&& dispatchToRenderThread, CompletionHandler<void(bool)>&& completionHandler)
+void MockAudioDestinationCocoa::startRendering(CompletionHandler<void(bool)>&& completionHandler)
{
- m_dispatchToRenderThread = WTFMove(dispatchToRenderThread);
- if (!m_dispatchToRenderThread) {
- m_dispatchToRenderThread = [this](Function<void()>&& function) {
- m_workQueue->dispatch(WTFMove(function));
- };
- }
-
m_timer.startRepeating(Seconds { m_numberOfFramesToProcess / sampleRate() });
setIsPlaying(true);
@@ -62,21 +55,19 @@
});
}
-void MockAudioDestinationCocoa::stop(CompletionHandler<void(bool)>&& completionHandler)
+void MockAudioDestinationCocoa::stopRendering(CompletionHandler<void(bool)>&& completionHandler)
{
m_timer.stop();
setIsPlaying(false);
- m_dispatchToRenderThread([completionHandler = WTFMove(completionHandler)]() mutable {
- callOnMainThread([completionHandler = WTFMove(completionHandler)]() mutable {
- completionHandler(true);
- });
+ callOnMainThread([completionHandler = WTFMove(completionHandler)]() mutable {
+ completionHandler(true);
});
}
void MockAudioDestinationCocoa::tick()
{
- m_dispatchToRenderThread([this, sampleRate = sampleRate(), numberOfFramesToProcess = m_numberOfFramesToProcess] {
+ m_workQueue->dispatch([this, protectedThis = makeRef(*this), sampleRate = sampleRate(), numberOfFramesToProcess = m_numberOfFramesToProcess] {
AudioStreamBasicDescription streamFormat;
getAudioStreamBasicDescription(streamFormat);
Modified: trunk/Source/WebCore/platform/mock/MockAudioDestinationCocoa.h (270946 => 270947)
--- trunk/Source/WebCore/platform/mock/MockAudioDestinationCocoa.h 2020-12-17 22:21:07 UTC (rev 270946)
+++ trunk/Source/WebCore/platform/mock/MockAudioDestinationCocoa.h 2020-12-17 22:23:39 UTC (rev 270947)
@@ -47,14 +47,13 @@
WEBCORE_EXPORT virtual ~MockAudioDestinationCocoa();
private:
- void start(Function<void(Function<void()>&&)>&&, CompletionHandler<void(bool)>&&) final;
- void stop(CompletionHandler<void(bool)>&&) final;
+ void startRendering(CompletionHandler<void(bool)>&&) final;
+ void stopRendering(CompletionHandler<void(bool)>&&) final;
void tick();
Ref<WorkQueue> m_workQueue;
RunLoop::Timer<MockAudioDestinationCocoa> m_timer;
- Function<void(Function<void()>&&)> m_dispatchToRenderThread;
uint32_t m_numberOfFramesToProcess { 384 };
};
Modified: trunk/Source/WebKit/ChangeLog (270946 => 270947)
--- trunk/Source/WebKit/ChangeLog 2020-12-17 22:21:07 UTC (rev 270946)
+++ trunk/Source/WebKit/ChangeLog 2020-12-17 22:23:39 UTC (rev 270947)
@@ -1,3 +1,25 @@
+2020-12-17 Chris Dumez <[email protected]>
+
+ [WebAudio] Simplify code related to dispatchToRenderThread
+ https://bugs.webkit.org/show_bug.cgi?id=219990
+
+ Reviewed by Geoffrey Garen.
+
+ Simplify code related to dispatchToRenderThread in WebAudio:
+ 1. AudioDestination::start() now always gets called with a non-null dispatchToRenderThread lambda.
+ In the case where there is no AudioWorkletThread to dispatch to, the lambda simply calls its
+ task synchronously.
+ 2. For Cocoa ports, make it so that only AudioDestinationCocoa needs to worry about the
+ dispatchToRenderThread lambda. The dispatchToRenderThread lambda is no longer exposed to
+ subclasses such as MockAudioDestinationCocoa & RemoteAudioDestinationProxy.
+
+ * WebProcess/GPU/media/RemoteAudioDestinationProxy.cpp:
+ (WebKit::RemoteAudioDestinationProxy::startRenderingThread):
+ (WebKit::RemoteAudioDestinationProxy::startRendering):
+ (WebKit::RemoteAudioDestinationProxy::stopRendering):
+ (WebKit::RemoteAudioDestinationProxy::gpuProcessConnectionDidClose):
+ * WebProcess/GPU/media/RemoteAudioDestinationProxy.h:
+
2020-12-17 Alex Christensen <[email protected]>
Fix "Open with Preview" menu item in PDF context menus on Big Sur
Modified: trunk/Source/WebKit/WebProcess/GPU/media/RemoteAudioDestinationProxy.cpp (270946 => 270947)
--- trunk/Source/WebKit/WebProcess/GPU/media/RemoteAudioDestinationProxy.cpp 2020-12-17 22:21:07 UTC (rev 270946)
+++ trunk/Source/WebKit/WebProcess/GPU/media/RemoteAudioDestinationProxy.cpp 2020-12-17 22:23:39 UTC (rev 270947)
@@ -63,7 +63,7 @@
, m_ringBuffer(makeUnique<WebCore::CARingBuffer>(makeUniqueRef<SharedRingBufferStorage>(std::bind(&RemoteAudioDestinationProxy::storageChanged, this, std::placeholders::_1))))
, m_sampleRate(hardwareSampleRate())
#else
- : WebCore::AudioDestination(callback)
+ : WebCore::AudioDestinationGStreamer(callback, numberOfOutputChannels, sampleRate)
, m_numberOfOutputChannels(numberOfOutputChannels)
#endif
, m_inputDeviceId(inputDeviceId)
@@ -83,13 +83,7 @@
if (m_shouldStopThread)
break;
- if (m_dispatchToRenderThread) {
- // If there is an AudioWorklet active, we need to render the quantum on the AudioWorkletThread.
- m_dispatchToRenderThread([this, protectedThis = makeRef(*this)] {
- renderQuantum();
- });
- } else
- renderQuantum();
+ renderQuantum();
} while (!m_shouldStopThread);
};
m_renderThread = Thread::create("RemoteAudioDestinationProxy render thread", WTFMove(offThreadRendering), ThreadType::Audio, Thread::QOS::UserInteractive);
@@ -158,31 +152,19 @@
stopRenderingThread();
}
-void RemoteAudioDestinationProxy::start(Function<void(Function<void()>&&)>&& dispatchToRenderThread, CompletionHandler<void(bool)>&& completionHandler)
+void RemoteAudioDestinationProxy::startRendering(CompletionHandler<void(bool)>&& completionHandler)
{
- WebProcess::singleton().ensureGPUProcessConnection().connection().sendWithAsyncReply(Messages::RemoteAudioDestinationManager::StartAudioDestination(m_destinationID), [this, protectedThis = makeRef(*this), completionHandler = WTFMove(completionHandler), dispatchToRenderThread = WTFMove(dispatchToRenderThread)](bool isPlaying) mutable {
- m_dispatchToRenderThread = WTFMove(dispatchToRenderThread);
+ WebProcess::singleton().ensureGPUProcessConnection().connection().sendWithAsyncReply(Messages::RemoteAudioDestinationManager::StartAudioDestination(m_destinationID), [this, protectedThis = makeRef(*this), completionHandler = WTFMove(completionHandler)](bool isPlaying) mutable {
setIsPlaying(isPlaying);
completionHandler(isPlaying);
});
}
-void RemoteAudioDestinationProxy::stop(CompletionHandler<void(bool)>&& completionHandler)
+void RemoteAudioDestinationProxy::stopRendering(CompletionHandler<void(bool)>&& completionHandler)
{
WebProcess::singleton().ensureGPUProcessConnection().connection().sendWithAsyncReply(Messages::RemoteAudioDestinationManager::StopAudioDestination(m_destinationID), [this, protectedThis = makeRef(*this), completionHandler = WTFMove(completionHandler)](bool isPlaying) mutable {
setIsPlaying(isPlaying);
- auto callCompletionHandler = [completionHandler = WTFMove(completionHandler), isPlaying]() mutable {
- completionHandler(!isPlaying);
- };
- auto dispatchToRenderThread = std::exchange(m_dispatchToRenderThread, nullptr);
- if (dispatchToRenderThread) {
- // Do a round-trip to the worklet thread to make sure we call the completion handler after
- // the last rendering quantum has been processed by the worklet thread.
- dispatchToRenderThread([callCompletionHandler = WTFMove(callCompletionHandler)]() mutable {
- callOnMainThread(WTFMove(callCompletionHandler));
- });
- } else
- callCompletionHandler();
+ completionHandler(!isPlaying);
});
}
@@ -231,7 +213,7 @@
connectToGPUProcess();
if (isPlaying())
- start(std::exchange(m_dispatchToRenderThread, nullptr), [](bool) { });
+ startRendering([](bool) { });
}
} // namespace WebKit
Modified: trunk/Source/WebKit/WebProcess/GPU/media/RemoteAudioDestinationProxy.h (270946 => 270947)
--- trunk/Source/WebKit/WebProcess/GPU/media/RemoteAudioDestinationProxy.h 2020-12-17 22:21:07 UTC (rev 270946)
+++ trunk/Source/WebKit/WebProcess/GPU/media/RemoteAudioDestinationProxy.h 2020-12-17 22:23:39 UTC (rev 270947)
@@ -39,7 +39,7 @@
#include "SharedRingBufferStorage.h"
#include <WebCore/AudioDestinationCocoa.h>
#else
-#include <WebCore/AudioDestination.h>
+#include <WebCore/AudioDestinationGStreamer.h>
#endif
namespace WebCore {
@@ -57,11 +57,11 @@
class SharedRingBufferFrameBounds;
-class RemoteAudioDestinationProxy
+class RemoteAudioDestinationProxy final
#if PLATFORM(COCOA)
: public WebCore::AudioDestinationCocoa
#else
- : public WebCore::AudioDestination
+ : public WebCore::AudioDestinationGStreamer
#endif
, public GPUProcessConnection::Client {
WTF_MAKE_NONCOPYABLE(RemoteAudioDestinationProxy);
@@ -76,8 +76,8 @@
~RemoteAudioDestinationProxy();
private:
- void start(Function<void(Function<void()>&&)>&& dispatchToRenderThread, CompletionHandler<void(bool)>&&) final;
- void stop(CompletionHandler<void(bool)>&&) final;
+ void startRendering(CompletionHandler<void(bool)>&&) final;
+ void stopRendering(CompletionHandler<void(bool)>&&) final;
void startRenderingThread();
void stopRenderingThread();
@@ -92,7 +92,6 @@
bool isPlaying() final { return false; }
void setIsPlaying(bool) { }
float sampleRate() const final { return 0; }
- unsigned framesPerBuffer() const final { return 0; }
unsigned numberOfOutputChannels() const { return m_numberOfOutputChannels; }
#endif
@@ -116,7 +115,6 @@
String m_inputDeviceId;
unsigned m_numberOfInputChannels;
- Function<void(Function<void()>&&)> m_dispatchToRenderThread;
RefPtr<Thread> m_renderThread;
std::atomic<bool> m_shouldStopThread { false };
};