Diff
Modified: trunk/Source/WTF/ChangeLog (270937 => 270938)
--- trunk/Source/WTF/ChangeLog 2020-12-17 18:26:06 UTC (rev 270937)
+++ trunk/Source/WTF/ChangeLog 2020-12-17 19:10:53 UTC (rev 270938)
@@ -1,3 +1,24 @@
+2020-12-17 Chris Dumez <[email protected]>
+
+ [GPUProcess] Replace WebAudio rendering timer with a cross-process semaphore
+ https://bugs.webkit.org/show_bug.cgi?id=219964
+
+ Reviewed by Geoff Garen.
+
+ Introduce MachSemaphore class, which is a C++ wrapper around a low-level
+ mach semaphore. This semaphore can be used cross-process and sent via
+ IPC as a MachSendRight.
+
+ * WTF.xcodeproj/project.pbxproj:
+ * wtf/PlatformMac.cmake:
+ * wtf/cocoa/MachSemaphore.cpp: Added.
+ (WTF::MachSemaphore::MachSemaphore):
+ (WTF::MachSemaphore::~MachSemaphore):
+ (WTF::MachSemaphore::signal):
+ (WTF::MachSemaphore::wait):
+ (WTF::MachSemaphore::createSendRight):
+ * wtf/cocoa/MachSemaphore.h: Added.
+
2020-12-15 Yusuke Suzuki <[email protected]>
[JSC] Accept arbitrary module namespace identifier names
Modified: trunk/Source/WTF/WTF.xcodeproj/project.pbxproj (270937 => 270938)
--- trunk/Source/WTF/WTF.xcodeproj/project.pbxproj 2020-12-17 18:26:06 UTC (rev 270937)
+++ trunk/Source/WTF/WTF.xcodeproj/project.pbxproj 2020-12-17 19:10:53 UTC (rev 270938)
@@ -68,6 +68,7 @@
3337DB9CE743410FAF076E17 /* StackTrace.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 313EDEC9778E49C9BEA91CFC /* StackTrace.cpp */; };
4427C5AA21F6D6C300A612A4 /* ASCIICType.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4427C5A921F6D6C300A612A4 /* ASCIICType.cpp */; };
46BEB6EB22FFE24900269867 /* RefCounted.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 46BEB6E922FFDDD500269867 /* RefCounted.cpp */; };
+ 46CB6B3D258AD93F006333B3 /* MachSemaphore.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 46BAA9C8258AD5A500795877 /* MachSemaphore.cpp */; };
50DE35F5215BB01500B979C7 /* ExternalStringImpl.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 50DE35F3215BB01500B979C7 /* ExternalStringImpl.cpp */; };
515F794E1CFC9F4A00CCED93 /* CrossThreadCopier.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 515F794B1CFC9F4A00CCED93 /* CrossThreadCopier.cpp */; };
517F82D71FD22F3000DA3DEA /* CrossThreadTaskHandler.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 517F82D51FD22F2F00DA3DEA /* CrossThreadTaskHandler.cpp */; };
@@ -377,6 +378,8 @@
4427C5A921F6D6C300A612A4 /* ASCIICType.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ASCIICType.cpp; sourceTree = "<group>"; };
4468567225094FE8008CCA05 /* ThreadSanitizerSupport.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ThreadSanitizerSupport.h; sourceTree = "<group>"; };
46BA9EAB1F4CD61E009A2BBC /* CompletionHandler.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CompletionHandler.h; sourceTree = "<group>"; };
+ 46BAA9C8258AD5A500795877 /* MachSemaphore.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = MachSemaphore.cpp; sourceTree = "<group>"; };
+ 46BAA9C9258AD5A500795877 /* MachSemaphore.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MachSemaphore.h; sourceTree = "<group>"; };
46BEB6E922FFDDD500269867 /* RefCounted.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = RefCounted.cpp; sourceTree = "<group>"; };
50DE35F3215BB01500B979C7 /* ExternalStringImpl.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ExternalStringImpl.cpp; sourceTree = "<group>"; };
50DE35F4215BB01500B979C7 /* ExternalStringImpl.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ExternalStringImpl.h; sourceTree = "<group>"; };
@@ -1545,6 +1548,8 @@
143DDE9520C8BC37007F76FA /* Entitlements.mm */,
A331D95E21F249F6009F02AA /* FileSystemCocoa.mm */,
1C503BE523AAE0AE0072E66B /* LanguageCocoa.mm */,
+ 46BAA9C8258AD5A500795877 /* MachSemaphore.cpp */,
+ 46BAA9C9258AD5A500795877 /* MachSemaphore.h */,
7A6EBA3320746C34004F9C44 /* MachSendRight.cpp */,
A8A472C5151A825A004123FF /* MainThreadCocoa.mm */,
ADF2CE651E39F106006889DB /* MemoryFootprintCocoa.cpp */,
@@ -1703,6 +1708,7 @@
0F60F32F1DFCBD1B00416D6C /* LockedPrintStream.cpp in Sources */,
93B5B45122171EEA004B7AA7 /* Logger.cpp in Sources */,
53534F2A1EC0E10E00141B2F /* MachExceptions.defs in Sources */,
+ 46CB6B3D258AD93F006333B3 /* MachSemaphore.cpp in Sources */,
7A6EBA3420746C34004F9C44 /* MachSendRight.cpp in Sources */,
A8A473E5151A825B004123FF /* MainThread.cpp in Sources */,
A8A473E4151A825B004123FF /* MainThreadCocoa.mm in Sources */,
Modified: trunk/Source/WTF/wtf/PlatformMac.cmake (270937 => 270938)
--- trunk/Source/WTF/wtf/PlatformMac.cmake 2020-12-17 18:26:06 UTC (rev 270937)
+++ trunk/Source/WTF/wtf/PlatformMac.cmake 2020-12-17 19:10:53 UTC (rev 270938)
@@ -60,6 +60,7 @@
cocoa/Entitlements.mm
cocoa/FileSystemCocoa.mm
cocoa/LanguageCocoa.mm
+ cocoa/MachSemaphore.cpp
cocoa/MachSendRight.cpp
cocoa/MainThreadCocoa.mm
cocoa/MemoryFootprintCocoa.cpp
Added: trunk/Source/WTF/wtf/cocoa/MachSemaphore.cpp (0 => 270938)
--- trunk/Source/WTF/wtf/cocoa/MachSemaphore.cpp (rev 0)
+++ trunk/Source/WTF/wtf/cocoa/MachSemaphore.cpp 2020-12-17 19:10:53 UTC (rev 270938)
@@ -0,0 +1,67 @@
+/*
+ * Copyright (C) 2020 Apple Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "config.h"
+#include "MachSemaphore.h"
+
+#include <mach/mach.h>
+
+namespace WTF {
+
+MachSemaphore::MachSemaphore()
+{
+ auto ret = semaphore_create(mach_task_self(), &m_semaphore, SYNC_POLICY_FIFO, 0);
+ ASSERT_UNUSED(ret, ret == KERN_SUCCESS);
+}
+
+MachSemaphore::MachSemaphore(MachSendRight&& sendRight)
+ : m_sendRight(WTFMove(sendRight))
+ , m_semaphore(m_sendRight.sendRight())
+{
+ ASSERT(m_sendRight);
+}
+
+MachSemaphore::~MachSemaphore()
+{
+ if (!m_sendRight)
+ semaphore_destroy(mach_task_self(), m_semaphore);
+}
+
+void MachSemaphore::signal()
+{
+ semaphore_signal(m_semaphore);
+}
+
+void MachSemaphore::wait()
+{
+ semaphore_wait(m_semaphore);
+}
+
+MachSendRight MachSemaphore::createSendRight()
+{
+ return MachSendRight::create(m_semaphore);
+}
+
+} // namespace WTF
Added: trunk/Source/WTF/wtf/cocoa/MachSemaphore.h (0 => 270938)
--- trunk/Source/WTF/wtf/cocoa/MachSemaphore.h (rev 0)
+++ trunk/Source/WTF/wtf/cocoa/MachSemaphore.h 2020-12-17 19:10:53 UTC (rev 270938)
@@ -0,0 +1,56 @@
+/*
+ * Copyright (C) 2020 Apple Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#pragma once
+
+#include <mach/semaphore.h>
+#include <wtf/MachSendRight.h>
+#include <wtf/Noncopyable.h>
+
+namespace WTF {
+
+class MachSendRight;
+
+class MachSemaphore {
+ WTF_MAKE_FAST_ALLOCATED;
+ WTF_MAKE_NONCOPYABLE(MachSemaphore);
+public:
+ WTF_EXPORT_PRIVATE MachSemaphore();
+ WTF_EXPORT_PRIVATE explicit MachSemaphore(MachSendRight&&);
+ WTF_EXPORT_PRIVATE ~MachSemaphore();
+
+ WTF_EXPORT_PRIVATE void signal();
+ WTF_EXPORT_PRIVATE void wait();
+
+ WTF_EXPORT_PRIVATE MachSendRight createSendRight();
+
+private:
+ MachSendRight m_sendRight;
+ semaphore_t m_semaphore { SEMAPHORE_NULL };
+};
+
+} // namespace WTF
+
+using WTF::MachSemaphore;
Modified: trunk/Source/WebCore/ChangeLog (270937 => 270938)
--- trunk/Source/WebCore/ChangeLog 2020-12-17 18:26:06 UTC (rev 270937)
+++ trunk/Source/WebCore/ChangeLog 2020-12-17 19:10:53 UTC (rev 270938)
@@ -1,3 +1,20 @@
+2020-12-17 Chris Dumez <[email protected]>
+
+ [GPUProcess] Replace WebAudio rendering timer with a cross-process semaphore
+ https://bugs.webkit.org/show_bug.cgi?id=219964
+
+ Reviewed by Geoff Garen.
+
+ Revert changes to CARingBuffer from r270907. These are no longer needed
+ with this new approach so we might as well remove the complexity from
+ our ring buffer implementation.
+
+ * platform/audio/cocoa/CARingBuffer.cpp:
+ (WebCore::CARingBufferStorageVector::flush):
+ (WebCore::CARingBuffer::fetchIfHasEnoughData):
+ (WebCore::CARingBuffer::fetch):
+ * platform/audio/cocoa/CARingBuffer.h:
+
2020-12-17 Patrick Angle <[email protected]>
Web Inspector: REGRESSION(r266467): viewing a WebSocket created before Web Inspector was opened doesn't show any message frames
Modified: trunk/Source/WebCore/platform/audio/cocoa/CARingBuffer.cpp (270937 => 270938)
--- trunk/Source/WebCore/platform/audio/cocoa/CARingBuffer.cpp 2020-12-17 18:26:06 UTC (rev 270937)
+++ trunk/Source/WebCore/platform/audio/cocoa/CARingBuffer.cpp 2020-12-17 19:10:53 UTC (rev 270938)
@@ -181,7 +181,6 @@
timeBounds.m_updateCounter = 0;
}
m_timeBoundsQueuePtr = 0;
- m_lastReadFrame = 0;
}
CARingBuffer::Error CARingBuffer::store(const AudioBufferList* list, size_t framesToWrite, uint64_t startFrame)
@@ -270,11 +269,6 @@
m_buffers->getCurrentFrameBounds(startFrame, endFrame);
}
-uint64_t CARingBuffer::lastReadFrame() const
-{
- return m_buffers->lastReadFrame();
-}
-
void CARingBufferStorageVector::getCurrentFrameBounds(uint64_t& startFrame, uint64_t& endFrame)
{
uint32_t curPtr = m_timeBoundsQueuePtr.load();
@@ -285,16 +279,6 @@
endFrame = bounds.m_endFrame;
}
-void CARingBufferStorageVector::setLastReadFrame(uint64_t lastReadFrame)
-{
- m_lastReadFrame.store(lastReadFrame, std::memory_order_release);
-}
-
-uint64_t CARingBufferStorageVector::lastReadFrame() const
-{
- return m_lastReadFrame.load(std::memory_order_acquire);
-}
-
void CARingBuffer::clipTimeBounds(uint64_t& startRead, uint64_t& endRead)
{
uint64_t startTime;
@@ -341,8 +325,6 @@
bool CARingBuffer::fetchIfHasEnoughData(AudioBufferList* list, size_t frameCount, uint64_t startFrame, FetchMode mode)
{
- m_buffers->setLastReadFrame(startFrame + frameCount);
-
// When the RingBuffer is backed by shared memory, getCurrentFrameBounds() makes sure we pull frame bounds from shared memory before fetching.
uint64_t start, end;
getCurrentFrameBounds(start, end);
@@ -355,8 +337,6 @@
void CARingBuffer::fetch(AudioBufferList* list, size_t frameCount, uint64_t startRead, FetchMode mode)
{
- m_buffers->setLastReadFrame(startRead + frameCount);
-
// When the RingBuffer is backed by shared memory, make sure we pull frame bounds from shared memory before fetching.
updateFrameBounds();
fetchInternal(list, frameCount, startRead, mode);
Modified: trunk/Source/WebCore/platform/audio/cocoa/CARingBuffer.h (270937 => 270938)
--- trunk/Source/WebCore/platform/audio/cocoa/CARingBuffer.h 2020-12-17 18:26:06 UTC (rev 270937)
+++ trunk/Source/WebCore/platform/audio/cocoa/CARingBuffer.h 2020-12-17 19:10:53 UTC (rev 270938)
@@ -51,8 +51,6 @@
virtual uint64_t currentStartFrame() const = 0;
virtual uint64_t currentEndFrame() const = 0;
virtual void flush() = 0;
- virtual void setLastReadFrame(uint64_t) = 0;
- virtual uint64_t lastReadFrame() const = 0;
};
class CARingBufferStorageVector final : public CARingBufferStorage {
@@ -69,8 +67,6 @@
uint64_t currentStartFrame() const final;
uint64_t currentEndFrame() const final;
void flush() final;
- void setLastReadFrame(uint64_t) final;
- uint64_t lastReadFrame() const final;
struct TimeBounds {
TimeBounds()
@@ -88,7 +84,6 @@
Vector<TimeBounds> m_timeBoundsQueue;
Lock m_currentFrameBoundsLock;
std::atomic<int32_t> m_timeBoundsQueuePtr { 0 };
- std::atomic<uint64_t> m_lastReadFrame { 0 };
};
class CARingBuffer {
@@ -118,9 +113,6 @@
WEBCORE_EXPORT void getCurrentFrameBounds(uint64_t& startFrame, uint64_t& endFrame);
- // Note that this will only return useful values if the reader has read-write access to the storage.
- WEBCORE_EXPORT uint64_t lastReadFrame() const;
-
uint32_t channelCount() const { return m_channelCount; }
CARingBufferStorage& storage() { return m_buffers; }
Modified: trunk/Source/WebKit/ChangeLog (270937 => 270938)
--- trunk/Source/WebKit/ChangeLog 2020-12-17 18:26:06 UTC (rev 270937)
+++ trunk/Source/WebKit/ChangeLog 2020-12-17 19:10:53 UTC (rev 270938)
@@ -1,3 +1,48 @@
+2020-12-17 Chris Dumez <[email protected]>
+
+ [GPUProcess] Replace WebAudio rendering timer with a cross-process semaphore
+ https://bugs.webkit.org/show_bug.cgi?id=219964
+
+ Reviewed by Geoff Garen.
+
+ r270907 replaced the per-rendering quantum IPC from the GPUProcess to the
+ WebProcess with a very high-frequency timer in the WebProcess to trigger
+ the rendering. While this works, this seemed a bit fragile and it also
+ adds some latency to avoid missed frames.
+
+ In this patch, I am replacing the high-frequency timer introduced in r270907
+ with a cross-process semaphore. When the WebProcess asks the GPU process
+ to create an audio device, the GPUProcess sends it back a MachSendRight
+ to the "rendering" semaphore. The WebProcess then starts a high-priority
+ AudioThread which waits this semaphore to be signaled to render a quantum.
+ Whenever render() gets called on the GPUProcess side, the GPUProcess
+ simply signals the "rendering" semaphore, causing the WebProcess to do
+ the rendering of a quantum. As was the case before, the WebProcess stores
+ rendered buffers into the RingBuffer it shares with the GPUProcess and
+ the GPUProcess fetches from the RingBuffer as needed.
+
+ * GPUProcess/media/RemoteAudioDestinationManager.cpp:
+ (WebKit::RemoteAudioDestination::createRenderSemaphoreSendRight):
+ (WebKit::RemoteAudioDestination::audioSamplesStorageChanged):
+ (WebKit::RemoteAudioDestination::render):
+ (WebKit::RemoteAudioDestinationManager::createAudioDestination):
+ * GPUProcess/media/RemoteAudioDestinationManager.h:
+ * GPUProcess/media/RemoteAudioDestinationManager.messages.in:
+ * Shared/Cocoa/SharedRingBufferStorage.cpp:
+ * Shared/Cocoa/SharedRingBufferStorage.h:
+ * WebProcess/GPU/media/RemoteAudioDestinationProxy.cpp:
+ (WebKit::RemoteAudioDestinationProxy::RemoteAudioDestinationProxy):
+ (WebKit::RemoteAudioDestinationProxy::startRenderingThread):
+ (WebKit::RemoteAudioDestinationProxy::stopRenderingThread):
+ (WebKit::RemoteAudioDestinationProxy::connectToGPUProcess):
+ (WebKit::RemoteAudioDestinationProxy::~RemoteAudioDestinationProxy):
+ (WebKit::RemoteAudioDestinationProxy::start):
+ (WebKit::RemoteAudioDestinationProxy::stop):
+ (WebKit::RemoteAudioDestinationProxy::renderQuantum):
+ (WebKit::RemoteAudioDestinationProxy::storageChanged):
+ (WebKit::RemoteAudioDestinationProxy::gpuProcessConnectionDidClose):
+ * WebProcess/GPU/media/RemoteAudioDestinationProxy.h:
+
2020-12-17 Patrick Angle <[email protected]>
Web Inspector: REGRESSION(r266467): viewing a WebSocket created before Web Inspector was opened doesn't show any message frames
Modified: trunk/Source/WebKit/GPUProcess/media/RemoteAudioDestinationManager.cpp (270937 => 270938)
--- trunk/Source/WebKit/GPUProcess/media/RemoteAudioDestinationManager.cpp 2020-12-17 18:26:06 UTC (rev 270937)
+++ trunk/Source/WebKit/GPUProcess/media/RemoteAudioDestinationManager.cpp 2020-12-17 19:10:53 UTC (rev 270938)
@@ -39,6 +39,7 @@
#include <WebCore/CAAudioStreamDescription.h>
#include <WebCore/CARingBuffer.h>
#include <WebCore/WebAudioBufferList.h>
+#include <wtf/cocoa/MachSemaphore.h>
#endif
namespace WebKit {
@@ -67,6 +68,8 @@
}
#if PLATFORM(COCOA)
+ MachSendRight createRenderSemaphoreSendRight() { return m_renderSemaphore.createSendRight(); }
+
void audioSamplesStorageChanged(const SharedMemory::IPCHandle& ipcHandle, const WebCore::CAAudioStreamDescription& description, uint64_t numberOfFrames)
{
m_description = description;
@@ -78,7 +81,7 @@
return;
}
- auto memory = SharedMemory::map(ipcHandle.handle, SharedMemory::Protection::ReadWrite);
+ auto memory = SharedMemory::map(ipcHandle.handle, SharedMemory::Protection::ReadOnly);
storage().setStorage(WTFMove(memory));
storage().setReadOnly(true);
m_ringBuffer->allocate(description, numberOfFrames);
@@ -145,6 +148,11 @@
status = noErr;
}
+ for (unsigned i = 0; i < numberOfFrames; i += WebCore::AudioUtilities::renderQuantumSize) {
+ // Ask the audio thread in the WebContent process to render a quantum.
+ m_renderSemaphore.signal();
+ }
+
return status;
}
#endif
@@ -158,6 +166,7 @@
WebCore::CAAudioStreamDescription m_description;
UniqueRef<WebCore::CARingBuffer> m_ringBuffer;
+ MachSemaphore m_renderSemaphore;
uint64_t m_startFrame { 0 };
#endif
@@ -171,13 +180,16 @@
RemoteAudioDestinationManager::~RemoteAudioDestinationManager() = default;
-void RemoteAudioDestinationManager::createAudioDestination(const String& inputDeviceId, uint32_t numberOfInputChannels, uint32_t numberOfOutputChannels, float sampleRate, float hardwareSampleRate,
- CompletionHandler<void(RemoteAudioDestinationIdentifier)>&& completionHandler)
+void RemoteAudioDestinationManager::createAudioDestination(const String& inputDeviceId, uint32_t numberOfInputChannels, uint32_t numberOfOutputChannels, float sampleRate, float hardwareSampleRate, CreationCompletionHandler&& completionHandler)
{
auto newID = RemoteAudioDestinationIdentifier::generateThreadSafe();
auto destination = RemoteAudioDestination::create(m_gpuConnectionToWebProcess, newID, inputDeviceId, numberOfInputChannels, numberOfOutputChannels, sampleRate, hardwareSampleRate);
- m_audioDestinations.add(newID, WTFMove(destination));
+ m_audioDestinations.add(newID, destination.copyRef());
+#if PLATFORM(COCOA)
+ completionHandler(newID, destination->createRenderSemaphoreSendRight());
+#else
completionHandler(newID);
+#endif
}
void RemoteAudioDestinationManager::deleteAudioDestination(RemoteAudioDestinationIdentifier identifier, CompletionHandler<void()>&& completionHandler)
Modified: trunk/Source/WebKit/GPUProcess/media/RemoteAudioDestinationManager.h (270937 => 270938)
--- trunk/Source/WebKit/GPUProcess/media/RemoteAudioDestinationManager.h 2020-12-17 18:26:06 UTC (rev 270937)
+++ trunk/Source/WebKit/GPUProcess/media/RemoteAudioDestinationManager.h 2020-12-17 19:10:53 UTC (rev 270938)
@@ -38,6 +38,10 @@
namespace WebCore {
class CAAudioStreamDescription;
}
+
+namespace WTF {
+class MachSendRight;
+}
#endif
namespace WebKit {
@@ -59,7 +63,12 @@
void didReceiveMessage(IPC::Connection&, IPC::Decoder&);
void didReceiveSyncMessage(IPC::Connection&, IPC::Decoder&, std::unique_ptr<IPC::Encoder>&);
- void createAudioDestination(const String& inputDeviceId, uint32_t numberOfInputChannels, uint32_t numberOfOutputChannels, float sampleRate, float hardwareSampleRate, CompletionHandler<void(RemoteAudioDestinationIdentifier)>&&);
+#if PLATFORM(COCOA)
+ using CreationCompletionHandler = CompletionHandler<void(RemoteAudioDestinationIdentifier, WTF::MachSendRight)>;
+#else
+ using CreationCompletionHandler = CompletionHandler<void(RemoteAudioDestinationIdentifier)>;
+#endif
+ void createAudioDestination(const String& inputDeviceId, uint32_t numberOfInputChannels, uint32_t numberOfOutputChannels, float sampleRate, float hardwareSampleRate, CreationCompletionHandler&&);
void deleteAudioDestination(RemoteAudioDestinationIdentifier, CompletionHandler<void()>&&);
void startAudioDestination(RemoteAudioDestinationIdentifier, CompletionHandler<void(bool)>&&);
void stopAudioDestination(RemoteAudioDestinationIdentifier, CompletionHandler<void(bool)>&&);
Modified: trunk/Source/WebKit/GPUProcess/media/RemoteAudioDestinationManager.messages.in (270937 => 270938)
--- trunk/Source/WebKit/GPUProcess/media/RemoteAudioDestinationManager.messages.in 2020-12-17 18:26:06 UTC (rev 270937)
+++ trunk/Source/WebKit/GPUProcess/media/RemoteAudioDestinationManager.messages.in 2020-12-17 19:10:53 UTC (rev 270938)
@@ -24,7 +24,13 @@
#if ENABLE(GPU_PROCESS) && ENABLE(WEB_AUDIO)
messages -> RemoteAudioDestinationManager NotRefCounted {
+#if PLATFORM(COCOA)
+ CreateAudioDestination(String inputDeviceId, uint32_t numberOfInputChannels, uint32_t numberOfOutputChannels, float sampleRate, float hardwareSampleRate) -> (WebKit::RemoteAudioDestinationIdentifier identifier, MachSendRight renderSemaphoreSendRight) Synchronous
+#endif
+#if !PLATFORM(COCOA)
CreateAudioDestination(String inputDeviceId, uint32_t numberOfInputChannels, uint32_t numberOfOutputChannels, float sampleRate, float hardwareSampleRate) -> (WebKit::RemoteAudioDestinationIdentifier identifier) Synchronous
+#endif
+
DeleteAudioDestination(WebKit::RemoteAudioDestinationIdentifier identifier) -> () Async
StartAudioDestination(WebKit::RemoteAudioDestinationIdentifier identifier) -> (bool isPlaying) Async
Modified: trunk/Source/WebKit/Shared/Cocoa/SharedRingBufferStorage.cpp (270937 => 270938)
--- trunk/Source/WebKit/Shared/Cocoa/SharedRingBufferStorage.cpp 2020-12-17 18:26:06 UTC (rev 270937)
+++ trunk/Source/WebKit/Shared/Cocoa/SharedRingBufferStorage.cpp 2020-12-17 19:10:53 UTC (rev 270938)
@@ -106,20 +106,6 @@
m_endFrame = pair.second;
}
-void SharedRingBufferStorage::setLastReadFrame(uint64_t lastReadFrame)
-{
- if (!m_storage || m_storage->protection() != SharedMemory::Protection::ReadWrite)
- return;
-
- sharedFrameBounds()->lastReadFrame.store(lastReadFrame, std::memory_order_release);
-}
-
-uint64_t SharedRingBufferStorage::lastReadFrame() const
-{
- auto* sharedBounds = sharedFrameBounds();
- return sharedBounds ? sharedBounds->lastReadFrame.load(std::memory_order_acquire) : 0;
-}
-
void SharedRingBufferStorage::flush()
{
m_startFrame = m_endFrame = 0;
Modified: trunk/Source/WebKit/Shared/Cocoa/SharedRingBufferStorage.h (270937 => 270938)
--- trunk/Source/WebKit/Shared/Cocoa/SharedRingBufferStorage.h 2020-12-17 18:26:06 UTC (rev 270937)
+++ trunk/Source/WebKit/Shared/Cocoa/SharedRingBufferStorage.h 2020-12-17 19:10:53 UTC (rev 270938)
@@ -59,8 +59,6 @@
uint64_t currentEndFrame() const final { return m_endFrame; }
void updateFrameBounds() final;
void flush() final;
- void setLastReadFrame(uint64_t) final;
- uint64_t lastReadFrame() const final;
private:
static constexpr unsigned boundsBufferSize { 32 };
@@ -67,7 +65,6 @@
struct FrameBounds {
std::pair<uint64_t, uint64_t> boundsBuffer[boundsBufferSize];
Atomic<unsigned> boundsBufferIndex { 0 };
- Atomic<uint64_t> lastReadFrame { 0 };
};
FrameBounds* sharedFrameBounds() const;
Modified: trunk/Source/WebKit/WebProcess/GPU/media/RemoteAudioDestinationProxy.cpp (270937 => 270938)
--- trunk/Source/WebKit/WebProcess/GPU/media/RemoteAudioDestinationProxy.cpp 2020-12-17 18:26:06 UTC (rev 270937)
+++ trunk/Source/WebKit/WebProcess/GPU/media/RemoteAudioDestinationProxy.cpp 2020-12-17 19:10:53 UTC (rev 270938)
@@ -34,13 +34,12 @@
#include "WebProcess.h"
#include <WebCore/AudioBus.h>
#include <WebCore/AudioUtilities.h>
-#include <wtf/RunLoop.h>
-#include <wtf/threads/BinarySemaphore.h>
#if PLATFORM(COCOA)
#include <WebCore/CARingBuffer.h>
#include <WebCore/WebAudioBufferList.h>
#include <mach/mach_time.h>
+#include <wtf/cocoa/MachSemaphore.h>
#endif
namespace WebKit {
@@ -47,7 +46,6 @@
// Allocate a ring buffer large enough to contain 2 seconds of audio.
constexpr size_t ringBufferSizeInSecond = 2;
-constexpr uint64_t framesAheadOfReader = 2 * WebCore::AudioUtilities::renderQuantumSize;
using AudioDestination = WebCore::AudioDestination;
using AudioIOCallback = WebCore::AudioIOCallback;
@@ -63,6 +61,7 @@
: WebCore::AudioDestinationCocoa(callback, numberOfOutputChannels, sampleRate, false)
, m_numberOfFrames(hardwareSampleRate() * ringBufferSizeInSecond)
, m_ringBuffer(makeUnique<WebCore::CARingBuffer>(makeUniqueRef<SharedRingBufferStorage>(std::bind(&RemoteAudioDestinationProxy::storageChanged, this, std::placeholders::_1))))
+ , m_sampleRate(hardwareSampleRate())
#else
: WebCore::AudioDestination(callback)
, m_numberOfOutputChannels(numberOfOutputChannels)
@@ -75,41 +74,57 @@
void RemoteAudioDestinationProxy::startRenderingThread()
{
+#if PLATFORM(COCOA)
ASSERT(!m_renderThread);
- BinarySemaphore semaphore;
- auto offThreadRendering = [this, protectedThis = makeRef(*this), &semaphore]() mutable {
- m_dispatchToRenderThread = [runLoop = makeRef(RunLoop::current())](Function<void()>&& task) {
- runLoop->dispatch(WTFMove(task));
- };
- semaphore.signal();
- RunLoop::run();
+ auto offThreadRendering = [this]() mutable {
+ do {
+ m_renderSemaphore->wait();
+ 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();
+ } while (!m_shouldStopThread);
};
m_renderThread = Thread::create("RemoteAudioDestinationProxy render thread", WTFMove(offThreadRendering), ThreadType::Audio, Thread::QOS::UserInteractive);
- m_renderThread->detach();
- semaphore.wait();
+#endif
}
-void RemoteAudioDestinationProxy::stopRenderingThreadIfNecessary()
+void RemoteAudioDestinationProxy::stopRenderingThread()
{
+#if PLATFORM(COCOA)
if (!m_renderThread)
return;
- m_dispatchToRenderThread([] {
- RunLoop::current().stop();
- });
- m_renderThread = nullptr;
+ m_shouldStopThread = true;
+ if (m_renderSemaphore)
+ m_renderSemaphore->signal();
+ m_renderThread->waitForCompletion();
+#endif
}
void RemoteAudioDestinationProxy::connectToGPUProcess()
{
RemoteAudioDestinationIdentifier destinationID;
+#if PLATFORM(COCOA)
+ MachSendRight renderSemaphoreSendRight;
+#endif
auto& connection = WebProcess::singleton().ensureGPUProcessConnection();
connection.addClient(*this);
auto didSucceed = connection.connection().sendSync(
Messages::RemoteAudioDestinationManager::CreateAudioDestination(m_inputDeviceId, m_numberOfInputChannels, numberOfOutputChannels(), sampleRate(), hardwareSampleRate()),
- Messages::RemoteAudioDestinationManager::CreateAudioDestination::Reply(destinationID), 0);
+ Messages::RemoteAudioDestinationManager::CreateAudioDestination::Reply(destinationID
+#if PLATFORM(COCOA)
+ , renderSemaphoreSendRight
+#endif
+ ), 0);
if (!didSucceed) {
// The GPUProcess likely crashed during this synchronous IPC. gpuProcessConnectionDidClose() will get called to reconnect to the GPUProcess.
@@ -125,7 +140,10 @@
getAudioStreamBasicDescription(streamFormat);
m_ringBuffer->allocate(streamFormat, m_numberOfFrames);
m_audioBufferList = makeUnique<WebCore::WebAudioBufferList>(streamFormat);
+ m_renderSemaphore = makeUnique<MachSemaphore>(WTFMove(renderSemaphoreSendRight));
#endif
+
+ startRenderingThread();
}
RemoteAudioDestinationProxy::~RemoteAudioDestinationProxy()
@@ -137,68 +155,37 @@
// Can't remove this from proxyMap() here because the object would have been already deleted.
});
- RELEASE_ASSERT(!m_renderThread);
- RELEASE_ASSERT(!m_dispatchToRenderThread);
+ stopRenderingThread();
}
void RemoteAudioDestinationProxy::start(Function<void(Function<void()>&&)>&& dispatchToRenderThread, 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 {
- if (m_dispatchToRenderThread)
- return completionHandler(true); // Already rendering. Can happen when start() gets called multiple times without stopping in between.
-
m_dispatchToRenderThread = WTFMove(dispatchToRenderThread);
setIsPlaying(isPlaying);
- if (!isPlaying)
- return completionHandler(false);
- startRendering([completionHandler = WTFMove(completionHandler)]() mutable {
- completionHandler(true);
- });
+ completionHandler(isPlaying);
});
}
-void RemoteAudioDestinationProxy::startRendering(CompletionHandler<void()>&& completionHandler)
-{
- ASSERT(isMainThread());
-
- if (!m_dispatchToRenderThread)
- startRenderingThread();
-
- m_dispatchToRenderThread([this, protectedThis = makeRef(*this), sampleRate = hardwareSampleRate(), completionHandler = WTFMove(completionHandler)]() mutable {
- m_sampleRate = sampleRate;
- renderQuantum();
- // To avoid the need for an IPC message from the GPUProcess for each rendering quantum, we use a timer to trigger the rendering of each quantum.
- m_renderingTimer = makeUnique<RunLoop::Timer<RemoteAudioDestinationProxy>>(RunLoop::current(), this, &RemoteAudioDestinationProxy::renderQuantum);
- m_renderingTimer->startRepeating(Seconds { WebCore::AudioUtilities::renderQuantumSize / static_cast<double>(sampleRate) });
-
- RunLoop::main().dispatch(WTFMove(completionHandler));
- });
-}
-
void RemoteAudioDestinationProxy::stop(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);
- stopRenderingIfNecessary([completionHandler = WTFMove(completionHandler), isPlaying]() mutable {
+ 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();
});
}
-void RemoteAudioDestinationProxy::stopRenderingIfNecessary(CompletionHandler<void()>&& completionHandler)
-{
- ASSERT(isMainThread());
- if (!m_dispatchToRenderThread)
- return completionHandler();
-
- m_dispatchToRenderThread([this, protectedThis = makeRef(*this), completionHandler = WTFMove(completionHandler)]() mutable {
- m_renderingTimer = nullptr;
- RunLoop::main().dispatch(WTFMove(completionHandler));
- });
- stopRenderingThreadIfNecessary();
- m_dispatchToRenderThread = nullptr;
-}
-
void RemoteAudioDestinationProxy::renderQuantum()
{
ASSERT(!isMainThread());
@@ -205,14 +192,9 @@
#if PLATFORM(COCOA)
m_audioBufferList->setSampleCount(WebCore::AudioUtilities::renderQuantumSize);
-
- // Try to stay a few rendering quantums ahead of the reader.
- auto lastReadFrame = m_ringBuffer->lastReadFrame();
- while (lastReadFrame >= m_currentFrame || (m_currentFrame - lastReadFrame) < framesAheadOfReader) {
- AudioDestinationCocoa::render(m_currentFrame / static_cast<double>(m_sampleRate), mach_absolute_time(), WebCore::AudioUtilities::renderQuantumSize, m_audioBufferList->list());
- m_ringBuffer->store(m_audioBufferList->list(), WebCore::AudioUtilities::renderQuantumSize, m_currentFrame);
- m_currentFrame += WebCore::AudioUtilities::renderQuantumSize;
- }
+ AudioDestinationCocoa::render(m_currentFrame / static_cast<double>(m_sampleRate), mach_absolute_time(), WebCore::AudioUtilities::renderQuantumSize, m_audioBufferList->list());
+ m_ringBuffer->store(m_audioBufferList->list(), WebCore::AudioUtilities::renderQuantumSize, m_currentFrame);
+ m_currentFrame += WebCore::AudioUtilities::renderQuantumSize;
#endif
}
@@ -221,7 +203,7 @@
{
SharedMemory::Handle handle;
if (storage)
- storage->createHandle(handle, SharedMemory::Protection::ReadWrite);
+ storage->createHandle(handle, SharedMemory::Protection::ReadOnly);
// FIXME: Send the actual data size with IPCHandle.
#if OS(DARWIN) || OS(WINDOWS)
@@ -242,6 +224,10 @@
{
oldConnection.removeClient(*this);
+ stopRenderingThread();
+#if PLATFORM(COCOA)
+ m_renderSemaphore = nullptr;
+#endif
connectToGPUProcess();
if (isPlaying())
Modified: trunk/Source/WebKit/WebProcess/GPU/media/RemoteAudioDestinationProxy.h (270937 => 270938)
--- trunk/Source/WebKit/WebProcess/GPU/media/RemoteAudioDestinationProxy.h 2020-12-17 18:26:06 UTC (rev 270937)
+++ trunk/Source/WebKit/WebProcess/GPU/media/RemoteAudioDestinationProxy.h 2020-12-17 19:10:53 UTC (rev 270938)
@@ -47,6 +47,12 @@
class WebAudioBufferList;
}
+namespace WTF {
+#if PLATFORM(COCOA)
+class MachSemaphore;
+#endif
+}
+
namespace WebKit {
class SharedRingBufferFrameBounds;
@@ -74,9 +80,7 @@
void stop(CompletionHandler<void(bool)>&&) final;
void startRenderingThread();
- void stopRenderingThreadIfNecessary();
- void startRendering(CompletionHandler<void()>&&);
- void stopRenderingIfNecessary(CompletionHandler<void()>&& = [] { });
+ void stopRenderingThread();
void renderQuantum();
void connectToGPUProcess();
@@ -101,8 +105,10 @@
#if PLATFORM(COCOA)
uint64_t m_numberOfFrames { 0 };
std::unique_ptr<WebCore::CARingBuffer> m_ringBuffer;
+ std::unique_ptr<WTF::MachSemaphore> m_renderSemaphore;
std::unique_ptr<WebCore::WebAudioBufferList> m_audioBufferList;
uint64_t m_currentFrame { 0 };
+ float m_sampleRate;
#else
unsigned m_numberOfOutputChannels;
#endif
@@ -112,9 +118,7 @@
Function<void(Function<void()>&&)> m_dispatchToRenderThread;
RefPtr<Thread> m_renderThread;
-
- std::unique_ptr<RunLoop::Timer<RemoteAudioDestinationProxy>> m_renderingTimer;
- float m_sampleRate { 0 };
+ std::atomic<bool> m_shouldStopThread { false };
};
} // namespace WebKit