Commit f022b8fb authored by Hongchan Choi's avatar Hongchan Choi Committed by Commit Bot

Tidy up redundant call chain of CallbackBufferSize

CallbackBufferSize() were scattered around many classes for no specific
reason. This CL cleans them up and contains it to
DefaultAudioDestinationNode.

Bug: 854229
Change-Id: I9116cf8ecd9398108fcdcac89cbbfb8c42de957e
Reviewed-on: https://chromium-review.googlesource.com/1112072
Commit-Queue: Hongchan Choi <hongchan@chromium.org>
Reviewed-by: default avatarRaymond Toy <rtoy@chromium.org>
Cr-Commit-Position: refs/heads/master@{#570081}
parent a212c946
......@@ -68,8 +68,6 @@ class AudioDestinationHandler : public AudioHandler, public AudioIOCallback {
// restart of the context.
virtual void RestartRendering() = 0;
// Returns the rendering callback buffer size.
size_t CallbackBufferSize() const override = 0;
virtual double SampleRate() const = 0;
protected:
......@@ -84,7 +82,6 @@ class AudioDestinationNode : public AudioNode {
AudioDestinationHandler& GetAudioDestinationHandler() const;
unsigned long maxChannelCount() const;
size_t CallbackBufferSize() const { return Handler().CallbackBufferSize(); }
protected:
AudioDestinationNode(BaseAudioContext&);
......
......@@ -238,11 +238,6 @@ class MODULES_EXPORT AudioHandler : public ThreadSafeRefCounted<AudioHandler> {
void UpdateChannelCountMode();
void UpdateChannelInterpretation();
// Default callbackBufferSize should be the render quantum size
virtual size_t CallbackBufferSize() const {
return AudioUtilities::kRenderQuantumFrames;
}
protected:
// Inputs and outputs must be created before the AudioHandler is
// initialized.
......
......@@ -147,11 +147,6 @@ class MODULES_EXPORT BaseAudioContext
: ClosedContextSampleRate();
}
size_t CallbackBufferSize() const {
return destination_node_ ? destination_node_->Handler().CallbackBufferSize()
: 0;
}
String state() const;
AudioContextState ContextState() const { return context_state_; }
void ThrowExceptionForClosedState(ExceptionState&);
......
......@@ -132,7 +132,7 @@ unsigned long DefaultAudioDestinationHandler::MaxChannelCount() const {
return AudioDestination::MaxChannelCount();
}
size_t DefaultAudioDestinationHandler::CallbackBufferSize() const {
size_t DefaultAudioDestinationHandler::GetCallbackBufferSize() const {
return destination_->CallbackBufferSize();
}
......
......@@ -55,10 +55,11 @@ class DefaultAudioDestinationHandler final : public AudioDestinationHandler {
void StopRendering() override;
void RestartRendering() override;
unsigned long MaxChannelCount() const override;
// Returns the rendering callback buffer size.
size_t CallbackBufferSize() const override;
double SampleRate() const override;
// Returns a hadrware callback buffer size from audio infra.
size_t GetCallbackBufferSize() const;
// Returns a given frames-per-buffer size from audio infra.
int GetFramesPerBuffer() const;
......@@ -89,8 +90,6 @@ class DefaultAudioDestinationNode final : public AudioDestinationNode {
static DefaultAudioDestinationNode* Create(BaseAudioContext*,
const WebAudioLatencyHint&);
size_t CallbackBufferSize() const { return Handler().CallbackBufferSize(); }
private:
explicit DefaultAudioDestinationNode(BaseAudioContext&,
const WebAudioLatencyHint&);
......
......@@ -135,12 +135,6 @@ void OfflineAudioDestinationHandler::StopRendering() {
NOTREACHED();
}
size_t OfflineAudioDestinationHandler::CallbackBufferSize() const {
// The callback buffer size has no meaning for an offline context.
NOTREACHED();
return 0;
}
void OfflineAudioDestinationHandler::InitializeOfflineRenderThread(
AudioBuffer* render_target) {
DCHECK(IsMainThread());
......
......@@ -66,10 +66,6 @@ class OfflineAudioDestinationHandler final : public AudioDestinationHandler {
void RestartRendering() override;
// Returns the rendering callback buffer size. This should never be
// called.
size_t CallbackBufferSize() const override;
double SampleRate() const override { return sample_rate_; }
size_t RenderQuantumFrames() const {
......
......@@ -35,6 +35,7 @@
#include "third_party/blink/renderer/modules/webaudio/audio_node_output.h"
#include "third_party/blink/renderer/modules/webaudio/audio_processing_event.h"
#include "third_party/blink/renderer/modules/webaudio/base_audio_context.h"
#include "third_party/blink/renderer/modules/webaudio/default_audio_destination_node.h"
#include "third_party/blink/renderer/platform/bindings/exception_state.h"
#include "third_party/blink/renderer/platform/cross_thread_functional.h"
#include "third_party/blink/renderer/platform/waitable_event.h"
......@@ -373,7 +374,7 @@ static size_t ChooseBufferSize(size_t callback_buffer_size) {
// Choose a buffer size based on the audio hardware buffer size. Arbitarily
// make it a power of two that is 4 times greater than the hardware buffer
// size.
// FIXME: What is the best way to choose this?
// TODO(crbug.com/855758): What is the best way to choose this?
size_t buffer_size =
1 << static_cast<unsigned>(log2(4 * callback_buffer_size) + 0.5);
......@@ -397,29 +398,29 @@ ScriptProcessorNode* ScriptProcessorNode::Create(
ScriptProcessorNode* ScriptProcessorNode::Create(
BaseAudioContext& context,
size_t buffer_size,
size_t requested_buffer_size,
ExceptionState& exception_state) {
DCHECK(IsMainThread());
// Default is 2 inputs and 2 outputs.
return Create(context, buffer_size, 2, 2, exception_state);
return Create(context, requested_buffer_size, 2, 2, exception_state);
}
ScriptProcessorNode* ScriptProcessorNode::Create(
BaseAudioContext& context,
size_t buffer_size,
size_t requested_buffer_size,
unsigned number_of_input_channels,
ExceptionState& exception_state) {
DCHECK(IsMainThread());
// Default is 2 outputs.
return Create(context, buffer_size, number_of_input_channels, 2,
return Create(context, requested_buffer_size, number_of_input_channels, 2,
exception_state);
}
ScriptProcessorNode* ScriptProcessorNode::Create(
BaseAudioContext& context,
size_t buffer_size,
size_t requested_buffer_size,
unsigned number_of_input_channels,
unsigned number_of_output_channels,
ExceptionState& exception_state) {
......@@ -455,17 +456,27 @@ ScriptProcessorNode* ScriptProcessorNode::Create(
return nullptr;
}
// Check for valid buffer size.
switch (buffer_size) {
// Sanitize user-supplied buffer size.
size_t buffer_size;
switch (requested_buffer_size) {
case 0:
// Choose an appropriate size. For an AudioContext, we need to
// choose an appropriate size based on the callback buffer size.
// For OfflineAudioContext, there's no callback buffer size, so
// just use the minimum valid buffer size.
buffer_size =
context.HasRealtimeConstraint()
? ChooseBufferSize(context.destination()->CallbackBufferSize())
: 256;
if (context.HasRealtimeConstraint()) {
// TODO(crbug.com/854229): Due to the incompatible constructor between
// AudioDestinationNode and DefaultAudioDestinationNode, casting
// directly from |destination()| is impossible. This is a temporary
// workaround until the refactoring is completed.
DefaultAudioDestinationHandler& destination_handler =
static_cast<DefaultAudioDestinationHandler&>(
context.destination()->GetAudioDestinationHandler());
buffer_size =
ChooseBufferSize(destination_handler.GetCallbackBufferSize());
} else {
buffer_size = 256;
}
break;
case 256:
case 512:
......@@ -474,11 +485,12 @@ ScriptProcessorNode* ScriptProcessorNode::Create(
case 4096:
case 8192:
case 16384:
buffer_size = requested_buffer_size;
break;
default:
exception_state.ThrowDOMException(
DOMExceptionCode::kIndexSizeError,
"buffer size (" + String::Number(buffer_size) +
"buffer size (" + String::Number(requested_buffer_size) +
") must be 0 or a power of two between 256 and 16384.");
return nullptr;
}
......
......@@ -126,14 +126,14 @@ class ScriptProcessorNode final
// The value chosen must carefully balance between latency and audio quality.
static ScriptProcessorNode* Create(BaseAudioContext&, ExceptionState&);
static ScriptProcessorNode* Create(BaseAudioContext&,
size_t buffer_size,
size_t requested_buffer_size,
ExceptionState&);
static ScriptProcessorNode* Create(BaseAudioContext&,
size_t buffer_size,
size_t requested_buffer_size,
unsigned number_of_input_channels,
ExceptionState&);
static ScriptProcessorNode* Create(BaseAudioContext&,
size_t buffer_size,
size_t requested_buffer_size,
unsigned number_of_input_channels,
unsigned number_of_output_channels,
ExceptionState&);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment