1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
|
/*
* Copyright (C) 2021-2023 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 "RemoteAudioMediaStreamTrackRendererInternalUnitManager.h"
#if ENABLE(GPU_PROCESS) && ENABLE(MEDIA_STREAM) && PLATFORM(COCOA)
#include "GPUConnectionToWebProcess.h"
#include "GPUProcess.h"
#include "GPUProcessConnectionMessages.h"
#include "IPCSemaphore.h"
#include "Logging.h"
#include "RemoteAudioSessionProxy.h"
#include "SharedCARingBuffer.h"
#include <WebCore/AVAudioSessionCaptureDeviceManager.h>
#include <WebCore/AudioMediaStreamTrackRenderer.h>
#include <WebCore/AudioMediaStreamTrackRendererInternalUnit.h>
#include <WebCore/AudioSampleBufferList.h>
#include <WebCore/AudioSession.h>
#include <WebCore/AudioUtilities.h>
#include <WebCore/CAAudioStreamDescription.h>
#include <WebCore/CARingBuffer.h>
#include <WebCore/CoreAudioCaptureSource.h>
#include <WebCore/CoreAudioSharedUnit.h>
#include <WebCore/WebAudioBufferList.h>
#include <wtf/TZoneMallocInlines.h>
#include <wtf/WeakPtr.h>
namespace WebKit {
class RemoteAudioMediaStreamTrackRendererInternalUnitManagerUnit
: public WebCore::CoreAudioSpeakerSamplesProducer
, public WebCore::AudioSessionInterruptionObserver
, public WebCore::AudioMediaStreamTrackRendererInternalUnit::Client {
WTF_MAKE_TZONE_ALLOCATED(RemoteAudioMediaStreamTrackRendererInternalUnitManagerUnit);
public:
static Ref<RemoteAudioMediaStreamTrackRendererInternalUnitManagerUnit> create(
AudioMediaStreamTrackRendererInternalUnitIdentifier identifier, const String& deviceID, GPUConnectionToWebProcess& connection, CompletionHandler<void(std::optional<WebCore::CAAudioStreamDescription>, size_t)>&& callback) {
return adoptRef(*new RemoteAudioMediaStreamTrackRendererInternalUnitManagerUnit(identifier, deviceID, connection, WTFMove(callback)));
}
~RemoteAudioMediaStreamTrackRendererInternalUnitManagerUnit();
void start(ConsumerSharedCARingBuffer::Handle&&, IPC::Semaphore&&);
void stop();
void updateShouldRegisterAsSpeakerSamplesProducer();
USING_CAN_MAKE_WEAKPTR(WebCore::AudioSessionInterruptionObserver);
private:
RemoteAudioMediaStreamTrackRendererInternalUnitManagerUnit(AudioMediaStreamTrackRendererInternalUnitIdentifier, const String&, GPUConnectionToWebProcess&, CompletionHandler<void(std::optional<WebCore::CAAudioStreamDescription>, size_t)>&&);
// CoreAudioSpeakerSamplesProducer
const WebCore::CAAudioStreamDescription& format() final { return *m_description; }
void captureUnitIsStarting() final;
void captureUnitHasStopped() final;
void canRenderAudioChanged() final;
// Background thread.
OSStatus produceSpeakerSamples(size_t sampleCount, AudioBufferList&, uint64_t sampleTime, double hostTime, AudioUnitRenderActionFlags&) final;
// WebCore::AudioSessionInterruptionObserver
void beginAudioSessionInterruption() final;
void endAudioSessionInterruption(WebCore::AudioSession::MayResume) final;
// WebCore::AudioMediaStreamTrackRendererInternal::Client
OSStatus render(size_t sampleCount, AudioBufferList&, uint64_t sampleTime, double hostTime, AudioUnitRenderActionFlags&) final;
void reset() final;
Ref<WebCore::AudioMediaStreamTrackRendererInternalUnit> protectedLocalUnit() { return m_localUnit; }
void setShouldRegisterAsSpeakerSamplesProducer(bool);
bool computeShouldRegisterAsSpeakerSamplesProducer() const;
AudioMediaStreamTrackRendererInternalUnitIdentifier m_identifier;
ThreadSafeWeakPtr<GPUConnectionToWebProcess> m_connection;
Ref<WebCore::AudioMediaStreamTrackRendererInternalUnit> m_localUnit;
uint64_t m_readOffset { 0 };
uint64_t m_generateOffset { 0 };
uint64_t m_frameChunkSize { 0 };
IPC::Semaphore m_renderSemaphore;
std::unique_ptr<ConsumerSharedCARingBuffer> m_ringBuffer;
bool m_isPlaying { false };
std::optional<WebCore::CAAudioStreamDescription> m_description;
bool m_canUseCaptureUnit { false };
bool m_shouldRegisterAsSpeakerSamplesProducer { false };
bool m_canReset { true };
};
WTF_MAKE_TZONE_ALLOCATED_IMPL(RemoteAudioMediaStreamTrackRendererInternalUnitManager);
RemoteAudioMediaStreamTrackRendererInternalUnitManager::RemoteAudioMediaStreamTrackRendererInternalUnitManager(GPUConnectionToWebProcess& gpuConnectionToWebProcess)
: m_gpuConnectionToWebProcess(gpuConnectionToWebProcess)
{
}
RemoteAudioMediaStreamTrackRendererInternalUnitManager::~RemoteAudioMediaStreamTrackRendererInternalUnitManager() = default;
void RemoteAudioMediaStreamTrackRendererInternalUnitManager::ref() const
{
m_gpuConnectionToWebProcess.get()->ref();
}
void RemoteAudioMediaStreamTrackRendererInternalUnitManager::deref() const
{
m_gpuConnectionToWebProcess.get()->deref();
}
void RemoteAudioMediaStreamTrackRendererInternalUnitManager::createUnit(AudioMediaStreamTrackRendererInternalUnitIdentifier identifier, const String& deviceID, CompletionHandler<void(std::optional<WebCore::CAAudioStreamDescription>, size_t)>&& callback)
{
ASSERT(!m_units.contains(identifier));
if (auto connection = m_gpuConnectionToWebProcess.get())
m_units.add(identifier, RemoteAudioMediaStreamTrackRendererInternalUnitManagerUnit::create(identifier, deviceID, *connection, WTFMove(callback)));
}
void RemoteAudioMediaStreamTrackRendererInternalUnitManager::deleteUnit(AudioMediaStreamTrackRendererInternalUnitIdentifier identifier)
{
if (!m_units.remove(identifier))
return;
if (m_units.isEmpty()) {
if (auto connection = m_gpuConnectionToWebProcess.get())
connection->protectedGPUProcess()->tryExitIfUnusedAndUnderMemoryPressure();
}
}
void RemoteAudioMediaStreamTrackRendererInternalUnitManager::startUnit(AudioMediaStreamTrackRendererInternalUnitIdentifier identifier, ConsumerSharedCARingBuffer::Handle&& handle, IPC::Semaphore&& semaphore)
{
if (RefPtr unit = m_units.get(identifier))
unit->start(WTFMove(handle), WTFMove(semaphore));
}
void RemoteAudioMediaStreamTrackRendererInternalUnitManager::stopUnit(AudioMediaStreamTrackRendererInternalUnitIdentifier identifier)
{
if (RefPtr unit = m_units.get(identifier))
unit->stop();
}
void RemoteAudioMediaStreamTrackRendererInternalUnitManager::notifyLastToCaptureAudioChanged()
{
for (Ref unit : m_units.values())
unit->updateShouldRegisterAsSpeakerSamplesProducer();
}
void RemoteAudioMediaStreamTrackRendererInternalUnitManager::setLastDeviceUsed(const String& deviceID)
{
#if PLATFORM(IOS_FAMILY) && USE(AUDIO_SESSION)
RefPtr connection = m_gpuConnectionToWebProcess.get();
Ref audioSession = connection->audioSessionProxy();
audioSession->setPreferredSpeakerID(deviceID != AudioMediaStreamTrackRenderer::defaultDeviceID() ? deviceID : emptyString());
#endif
}
std::optional<SharedPreferencesForWebProcess> RemoteAudioMediaStreamTrackRendererInternalUnitManager::sharedPreferencesForWebProcess() const
{
if (RefPtr gpuConnectionToWebProcess = m_gpuConnectionToWebProcess.get())
return gpuConnectionToWebProcess->sharedPreferencesForWebProcess();
return std::nullopt;
}
WTF_MAKE_TZONE_ALLOCATED_IMPL(RemoteAudioMediaStreamTrackRendererInternalUnitManagerUnit);
RemoteAudioMediaStreamTrackRendererInternalUnitManagerUnit::RemoteAudioMediaStreamTrackRendererInternalUnitManagerUnit(AudioMediaStreamTrackRendererInternalUnitIdentifier identifier, const String& deviceID, GPUConnectionToWebProcess& connection, CompletionHandler<void(std::optional<WebCore::CAAudioStreamDescription>, size_t)>&& callback)
: m_identifier(identifier)
, m_connection(connection)
, m_localUnit(WebCore::AudioMediaStreamTrackRendererInternalUnit::create(deviceID, *this))
, m_canUseCaptureUnit(deviceID == AudioMediaStreamTrackRenderer::defaultDeviceID())
{
WebCore::AudioSession::protectedSharedSession()->addInterruptionObserver(*this);
protectedLocalUnit()->retrieveFormatDescription([weakThis = WeakPtr { *this }, this, callback = WTFMove(callback)](auto&& description) mutable {
if (!weakThis || !description) {
RELEASE_LOG_IF(!description, WebRTC, "RemoteAudioMediaStreamTrackRendererInternalUnitManagerUnit unable to get format description");
callback(std::nullopt, 0);
return;
}
size_t tenMsSampleSize = description->sampleRate() * 10 / 1000;
m_description = *description;
m_frameChunkSize = std::max(WebCore::AudioUtilities::renderQuantumSize, tenMsSampleSize);
callback(*description, m_frameChunkSize);
});
}
RemoteAudioMediaStreamTrackRendererInternalUnitManagerUnit::~RemoteAudioMediaStreamTrackRendererInternalUnitManagerUnit()
{
WebCore::AudioSession::protectedSharedSession()->removeInterruptionObserver(*this);
stop();
}
bool RemoteAudioMediaStreamTrackRendererInternalUnitManagerUnit::computeShouldRegisterAsSpeakerSamplesProducer() const
{
RefPtr connection = m_connection.get();
return m_canUseCaptureUnit && connection && connection->isLastToCaptureAudio() && WebCore::CoreAudioCaptureSourceFactory::singleton().shouldAudioCaptureUnitRenderAudio();
}
void RemoteAudioMediaStreamTrackRendererInternalUnitManagerUnit::reset()
{
if (!m_canReset)
return;
m_canReset = false;
if (RefPtr connection = m_connection.get())
connection->protectedConnection()->send(Messages::GPUProcessConnection::ResetAudioMediaStreamTrackRendererInternalUnit { m_identifier }, 0);
}
void RemoteAudioMediaStreamTrackRendererInternalUnitManagerUnit::setShouldRegisterAsSpeakerSamplesProducer(bool value)
{
if (m_shouldRegisterAsSpeakerSamplesProducer == value)
return;
ASSERT(m_canUseCaptureUnit);
m_shouldRegisterAsSpeakerSamplesProducer = value;
if (!m_isPlaying)
return;
if (m_shouldRegisterAsSpeakerSamplesProducer) {
protectedLocalUnit()->stop();
WebCore::CoreAudioCaptureSourceFactory::singleton().registerSpeakerSamplesProducer(*this);
} else {
WebCore::CoreAudioCaptureSourceFactory::singleton().unregisterSpeakerSamplesProducer(*this);
protectedLocalUnit()->start();
}
}
void RemoteAudioMediaStreamTrackRendererInternalUnitManagerUnit::start(ConsumerSharedCARingBuffer::Handle&& handle, IPC::Semaphore&& semaphore)
{
if (m_isPlaying)
stop();
m_ringBuffer = ConsumerSharedCARingBuffer::map(*m_description, WTFMove(handle));
if (!m_ringBuffer)
return;
m_readOffset = 0;
m_generateOffset = 0;
m_isPlaying = true;
m_canReset = true;
m_renderSemaphore = WTFMove(semaphore);
m_shouldRegisterAsSpeakerSamplesProducer = computeShouldRegisterAsSpeakerSamplesProducer();
if (m_shouldRegisterAsSpeakerSamplesProducer) {
WebCore::CoreAudioCaptureSourceFactory::singleton().registerSpeakerSamplesProducer(*this);
bool shouldNotStartLocalUnit = WebCore::CoreAudioCaptureSourceFactory::singleton().shouldAudioCaptureUnitRenderAudio() || WebCore::AudioSession::sharedSession().isInterrupted();
if (shouldNotStartLocalUnit)
return;
}
protectedLocalUnit()->start();
}
void RemoteAudioMediaStreamTrackRendererInternalUnitManagerUnit::stop()
{
m_isPlaying = false;
WebCore::CoreAudioCaptureSourceFactory::singleton().unregisterSpeakerSamplesProducer(*this);
protectedLocalUnit()->stop();
}
void RemoteAudioMediaStreamTrackRendererInternalUnitManagerUnit::updateShouldRegisterAsSpeakerSamplesProducer()
{
setShouldRegisterAsSpeakerSamplesProducer(computeShouldRegisterAsSpeakerSamplesProducer());
}
OSStatus RemoteAudioMediaStreamTrackRendererInternalUnitManagerUnit::render(size_t sampleCount, AudioBufferList& list, uint64_t, double, AudioUnitRenderActionFlags& flags)
{
ASSERT(!isMainRunLoop());
OSStatus status = -1;
if (m_ringBuffer->fetchIfHasEnoughData(&list, sampleCount, m_readOffset)) {
m_readOffset += sampleCount;
status = noErr;
} else {
WebCore::AudioSampleBufferList::zeroABL(list, static_cast<size_t>(sampleCount * m_description->bytesPerFrame()));
flags = kAudioUnitRenderAction_OutputIsSilence;
}
auto requestedSamplesCount = m_generateOffset;
for (; requestedSamplesCount < sampleCount; requestedSamplesCount += m_frameChunkSize)
m_renderSemaphore.signal();
m_generateOffset = requestedSamplesCount - sampleCount;
return status;
}
void RemoteAudioMediaStreamTrackRendererInternalUnitManagerUnit::captureUnitIsStarting()
{
// Capture unit is starting and audio will be rendered through it and not by our local unit so stop the local unit.
if (m_isPlaying && WebCore::CoreAudioCaptureSourceFactory::singleton().shouldAudioCaptureUnitRenderAudio())
protectedLocalUnit()->stop();
}
void RemoteAudioMediaStreamTrackRendererInternalUnitManagerUnit::captureUnitHasStopped()
{
// Capture unit has stopped and audio will no longer be rendered through it so start the local unit.
if (m_isPlaying && !WebCore::CoreAudioSharedUnit::singleton().isSuspended())
protectedLocalUnit()->start();
}
void RemoteAudioMediaStreamTrackRendererInternalUnitManagerUnit::canRenderAudioChanged()
{
callOnMainRunLoop([protectedThis = Ref { *this }] {
if (protectedThis->m_isPlaying)
protectedThis->updateShouldRegisterAsSpeakerSamplesProducer();
});
}
// Background thread.
OSStatus RemoteAudioMediaStreamTrackRendererInternalUnitManagerUnit::produceSpeakerSamples(size_t sampleCount, AudioBufferList& list, uint64_t sampleTime, double hostTime, AudioUnitRenderActionFlags& flags)
{
return render(sampleCount, list, sampleTime, hostTime, flags);
}
void RemoteAudioMediaStreamTrackRendererInternalUnitManagerUnit::beginAudioSessionInterruption()
{
if (m_isPlaying)
protectedLocalUnit()->stop();
}
void RemoteAudioMediaStreamTrackRendererInternalUnitManagerUnit::endAudioSessionInterruption(WebCore::AudioSession::MayResume)
{
if (!m_isPlaying)
return;
if (m_shouldRegisterAsSpeakerSamplesProducer && (WebCore::CoreAudioSharedUnit::singleton().isRunning() || WebCore::CoreAudioSharedUnit::singleton().isSuspended()))
return;
protectedLocalUnit()->start();
}
} // namespace WebKit
#endif // ENABLE(GPU_PROCESS) && ENABLE(MEDIA_STREAM) && PLATFORM(COCOA)
|