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 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
|
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "third_party/blink/renderer/modules/webaudio/offline_audio_destination_handler.h"
#include <algorithm>
#include "base/compiler_specific.h"
#include "base/trace_event/typed_macros.h"
#include "media/base/audio_glitch_info.h"
#include "third_party/blink/renderer/core/execution_context/execution_context.h"
#include "third_party/blink/renderer/modules/webaudio/audio_node_input.h"
#include "third_party/blink/renderer/modules/webaudio/audio_node_output.h"
#include "third_party/blink/renderer/modules/webaudio/audio_worklet.h"
#include "third_party/blink/renderer/modules/webaudio/audio_worklet_messaging_proxy.h"
#include "third_party/blink/renderer/modules/webaudio/base_audio_context.h"
#include "third_party/blink/renderer/modules/webaudio/cross_thread_audio_worklet_processor_info.h"
#include "third_party/blink/renderer/modules/webaudio/offline_audio_context.h"
#include "third_party/blink/renderer/platform/audio/audio_bus.h"
#include "third_party/blink/renderer/platform/audio/audio_utilities.h"
#include "third_party/blink/renderer/platform/audio/denormal_disabler.h"
#include "third_party/blink/renderer/platform/audio/hrtf_database_loader.h"
#include "third_party/blink/renderer/platform/scheduler/public/non_main_thread.h"
#include "third_party/blink/renderer/platform/wtf/cross_thread_copier_base.h"
#include "third_party/blink/renderer/platform/wtf/cross_thread_functional.h"
namespace blink {
OfflineAudioDestinationHandler::OfflineAudioDestinationHandler(
AudioNode& node,
unsigned number_of_channels,
uint32_t frames_to_process,
float sample_rate)
: AudioDestinationHandler(node),
frames_to_process_(frames_to_process),
number_of_channels_(number_of_channels),
sample_rate_(sample_rate),
main_thread_task_runner_(Context()->GetExecutionContext()->GetTaskRunner(
TaskType::kInternalMedia)) {
DCHECK(main_thread_task_runner_->BelongsToCurrentThread());
channel_count_ = number_of_channels;
SetInternalChannelCountMode(V8ChannelCountMode::Enum::kExplicit);
SetInternalChannelInterpretation(AudioBus::kSpeakers);
}
scoped_refptr<OfflineAudioDestinationHandler>
OfflineAudioDestinationHandler::Create(AudioNode& node,
unsigned number_of_channels,
uint32_t frames_to_process,
float sample_rate) {
return base::AdoptRef(new OfflineAudioDestinationHandler(
node, number_of_channels, frames_to_process, sample_rate));
}
OfflineAudioDestinationHandler::~OfflineAudioDestinationHandler() {
DCHECK(!IsInitialized());
}
void OfflineAudioDestinationHandler::Dispose() {
Uninitialize();
AudioDestinationHandler::Dispose();
}
void OfflineAudioDestinationHandler::Initialize() {
if (IsInitialized()) {
return;
}
AudioHandler::Initialize();
}
void OfflineAudioDestinationHandler::Uninitialize() {
if (!IsInitialized()) {
return;
}
// See https://crbug.com/1110035 and https://crbug.com/1080821. Resetting the
// thread unique pointer multiple times or not-resetting at all causes a
// mysterious CHECK failure or a crash.
if (render_thread_) {
render_thread_.reset();
}
AudioHandler::Uninitialize();
}
OfflineAudioContext* OfflineAudioDestinationHandler::Context() const {
return static_cast<OfflineAudioContext*>(AudioDestinationHandler::Context());
}
uint32_t OfflineAudioDestinationHandler::MaxChannelCount() const {
return channel_count_;
}
void OfflineAudioDestinationHandler::StartRendering() {
DCHECK(IsMainThread());
DCHECK(shared_render_target_);
DCHECK(render_thread_task_runner_);
TRACE_EVENT(TRACE_DISABLED_BY_DEFAULT("webaudio.audionode"),
"OfflineAudioDestinationHandler::StartRendering", "this",
reinterpret_cast<void*>(this));
// Rendering was not started. Starting now.
if (!is_rendering_started_) {
is_rendering_started_ = true;
PostCrossThreadTask(
*render_thread_task_runner_, FROM_HERE,
CrossThreadBindOnce(
&OfflineAudioDestinationHandler::StartOfflineRendering,
WrapRefCounted(this)));
return;
}
// Rendering is already started, which implicitly means we resume the
// rendering by calling `DoOfflineRendering()` on the render thread.
PostCrossThreadTask(
*render_thread_task_runner_, FROM_HERE,
CrossThreadBindOnce(&OfflineAudioDestinationHandler::DoOfflineRendering,
WrapRefCounted(this)));
}
void OfflineAudioDestinationHandler::StopRendering() {
// offline audio rendering CANNOT BE stopped by JavaScript.
NOTREACHED();
}
void OfflineAudioDestinationHandler::Pause() {
NOTREACHED();
}
void OfflineAudioDestinationHandler::Resume() {
NOTREACHED();
}
void OfflineAudioDestinationHandler::InitializeOfflineRenderThread(
AudioBuffer* render_target) {
DCHECK(IsMainThread());
shared_render_target_ = render_target->CreateSharedAudioBuffer();
render_bus_ =
AudioBus::Create(render_target->numberOfChannels(),
GetDeferredTaskHandler().RenderQuantumFrames());
DCHECK(render_bus_);
PrepareTaskRunnerForRendering();
}
void OfflineAudioDestinationHandler::StartOfflineRendering() {
DCHECK(!IsMainThread());
DCHECK(render_bus_);
bool is_audio_context_initialized = Context()->IsDestinationInitialized();
DCHECK(is_audio_context_initialized);
DCHECK_EQ(render_bus_->NumberOfChannels(),
shared_render_target_->numberOfChannels());
DCHECK_GE(render_bus_->length(),
GetDeferredTaskHandler().RenderQuantumFrames());
// Start rendering.
DoOfflineRendering();
}
void OfflineAudioDestinationHandler::DoOfflineRendering() {
DCHECK(!IsMainThread());
TRACE_EVENT(TRACE_DISABLED_BY_DEFAULT("webaudio.audionode"),
"OfflineAudioDestinationHandler::DoOfflineRendering", "this",
reinterpret_cast<void*>(this));
unsigned number_of_channels = shared_render_target_->numberOfChannels();
Vector<float*> destinations;
destinations.ReserveInitialCapacity(number_of_channels);
for (unsigned i = 0; i < number_of_channels; ++i) {
destinations.push_back(
static_cast<float*>(shared_render_target_->channels()[i].Data()));
}
// If there is more to process and there is no suspension at the moment,
// do continue to render quanta. Then calling OfflineAudioContext.resume()
// will pick up the render loop again from where it was suspended.
while (frames_to_process_ > 0) {
// Suspend the rendering if a scheduled suspend found at the current
// sample frame. Otherwise render one quantum.
if (RenderIfNotSuspended(nullptr, render_bus_.get(),
GetDeferredTaskHandler().RenderQuantumFrames())) {
return;
}
uint32_t frames_available_to_copy = std::min(
frames_to_process_, GetDeferredTaskHandler().RenderQuantumFrames());
for (unsigned channel_index = 0; channel_index < number_of_channels;
++channel_index) {
const float* source = render_bus_->Channel(channel_index)->Data();
UNSAFE_TODO(memcpy(destinations[channel_index] + frames_processed_,
source, sizeof(float) * frames_available_to_copy));
}
frames_processed_ += frames_available_to_copy;
DCHECK_GE(frames_to_process_, frames_available_to_copy);
frames_to_process_ -= frames_available_to_copy;
}
DCHECK_EQ(frames_to_process_, 0u);
FinishOfflineRendering();
}
void OfflineAudioDestinationHandler::SuspendOfflineRendering() {
DCHECK(!IsMainThread());
// The actual rendering has been suspended. Notify the context.
PostCrossThreadTask(
*main_thread_task_runner_, FROM_HERE,
CrossThreadBindOnce(&OfflineAudioDestinationHandler::NotifySuspend,
GetWeakPtr(), Context()->CurrentSampleFrame()));
}
void OfflineAudioDestinationHandler::FinishOfflineRendering() {
DCHECK(!IsMainThread());
// The actual rendering has been completed. Notify the context.
PostCrossThreadTask(
*main_thread_task_runner_, FROM_HERE,
CrossThreadBindOnce(&OfflineAudioDestinationHandler::NotifyComplete,
GetWeakPtr()));
}
void OfflineAudioDestinationHandler::NotifySuspend(size_t frame) {
DCHECK(IsMainThread());
if (!IsExecutionContextDestroyed() && Context()) {
Context()->ResolveSuspendOnMainThread(frame);
}
}
void OfflineAudioDestinationHandler::NotifyComplete() {
DCHECK(IsMainThread());
render_thread_.reset();
// If the execution context has been destroyed, there's nowhere to send the
// notification, so just return.
if (IsExecutionContextDestroyed()) {
return;
}
// The OfflineAudioContext might be gone.
if (Context() && Context()->GetExecutionContext()) {
Context()->FireCompletionEvent();
}
}
bool OfflineAudioDestinationHandler::RenderIfNotSuspended(
AudioBus* source_bus,
AudioBus* destination_bus,
uint32_t number_of_frames) {
// We don't want denormals slowing down any of the audio processing
// since they can very seriously hurt performance.
// This will take care of all AudioNodes because they all process within this
// scope.
DenormalDisabler denormal_disabler;
// Need to check if the context actually alive. Otherwise the subsequent
// steps will fail. If the context is not alive somehow, return immediately
// and do nothing.
//
// TODO(hongchan): because the context can go away while rendering, so this
// check cannot guarantee the safe execution of the following steps.
DCHECK(Context());
if (!Context()) {
return false;
}
Context()->GetDeferredTaskHandler().SetAudioThreadToCurrentThread();
// If the destination node is not initialized, pass the silence to the final
// audio destination (one step before the FIFO). This check is for the case
// where the destination is in the middle of tearing down process.
if (!IsInitialized()) {
destination_bus->Zero();
return false;
}
// Take care pre-render tasks at the beginning of each render quantum. Then
// it will stop the rendering loop if the context needs to be suspended
// at the beginning of the next render quantum.
if (Context()->HandlePreRenderTasks(number_of_frames, nullptr, nullptr,
base::TimeDelta(),
media::AudioGlitchInfo())) {
SuspendOfflineRendering();
return true;
}
DCHECK_GE(NumberOfInputs(), 1u);
// This will cause the node(s) connected to us to process, which in turn will
// pull on their input(s), all the way backwards through the rendering graph.
scoped_refptr<AudioBus> rendered_bus =
Input(0).Pull(destination_bus, number_of_frames);
if (!rendered_bus) {
destination_bus->Zero();
} else if (rendered_bus != destination_bus) {
// in-place processing was not possible - so copy
destination_bus->CopyFrom(*rendered_bus);
}
// Process nodes which need a little extra help because they are not connected
// to anything, but still need to process.
Context()->GetDeferredTaskHandler().ProcessAutomaticPullNodes(
number_of_frames);
// Let the context take care of any business at the end of each render
// quantum.
Context()->HandlePostRenderTasks();
// Advance current sample-frame.
AdvanceCurrentSampleFrame(number_of_frames);
Context()->UpdateWorkletGlobalScopeOnRenderingThread();
return false;
}
void OfflineAudioDestinationHandler::PrepareTaskRunnerForRendering() {
DCHECK(IsMainThread());
AudioWorklet* audio_worklet = Context()->audioWorklet();
if (audio_worklet && audio_worklet->IsReady()) {
if (!render_thread_) {
// The context (re)started with the AudioWorklet mode. Assign the task
// runner only when it is not set yet.
if (!render_thread_task_runner_) {
render_thread_task_runner_ =
audio_worklet->GetMessagingProxy()
->GetBackingWorkerThread()
->GetTaskRunner(TaskType::kMiscPlatformAPI);
}
} else {
// The AudioWorklet is activated and the render task runner should be
// changed.
render_thread_ = nullptr;
render_thread_task_runner_ =
audio_worklet->GetMessagingProxy()
->GetBackingWorkerThread()
->GetTaskRunner(TaskType::kMiscPlatformAPI);
}
} else {
if (!render_thread_) {
// The context started from the non-AudioWorklet mode.
render_thread_ = NonMainThread::CreateThread(
ThreadCreationParams(ThreadType::kOfflineAudioRenderThread));
render_thread_task_runner_ = render_thread_->GetTaskRunner();
}
}
// The task runner MUST be valid at this point.
DCHECK(render_thread_task_runner_);
}
void OfflineAudioDestinationHandler::RestartRendering() {
DCHECK(IsMainThread());
// The rendering thread might have been changed, so we need to set up the
// task runner again.
PrepareTaskRunnerForRendering();
}
} // namespace blink
|