Commit 589d63a3 authored by Dave Tapuska's avatar Dave Tapuska Committed by Commit Bot

Fix 64 bit truncations in webaudio.

BUG=879657

Change-Id: I522fa35be26873436ed090990cbae1897acef6f7
Reviewed-on: https://chromium-review.googlesource.com/c/1355841Reviewed-by: default avatarHongchan Choi <hongchan@chromium.org>
Commit-Queue: Dave Tapuska <dtapuska@chromium.org>
Cr-Commit-Position: refs/heads/master@{#612759}
parent 5ea7e168
...@@ -25,6 +25,7 @@ ...@@ -25,6 +25,7 @@
#include <algorithm> #include <algorithm>
#include "base/numerics/safe_conversions.h"
#include "third_party/blink/renderer/core/frame/use_counter.h" #include "third_party/blink/renderer/core/frame/use_counter.h"
#include "third_party/blink/renderer/modules/webaudio/audio_buffer_source_node.h" #include "third_party/blink/renderer/modules/webaudio/audio_buffer_source_node.h"
#include "third_party/blink/renderer/modules/webaudio/audio_buffer_source_options.h" #include "third_party/blink/renderer/modules/webaudio/audio_buffer_source_options.h"
...@@ -227,9 +228,10 @@ bool AudioBufferSourceHandler::RenderFromBuffer( ...@@ -227,9 +228,10 @@ bool AudioBufferSourceHandler::RenderFromBuffer(
// Avoid converting from time to sample-frames twice by computing // Avoid converting from time to sample-frames twice by computing
// the grain end time first before computing the sample frame. // the grain end time first before computing the sample frame.
unsigned end_frame = unsigned end_frame =
is_grain_ ? audio_utilities::TimeToSampleFrame( is_grain_
grain_offset_ + grain_duration_, buffer_sample_rate) ? base::saturated_cast<uint32_t>(audio_utilities::TimeToSampleFrame(
: buffer_length; grain_offset_ + grain_duration_, buffer_sample_rate))
: buffer_length;
// Do some sanity checking. // Do some sanity checking.
if (end_frame > buffer_length) if (end_frame > buffer_length)
......
...@@ -204,7 +204,7 @@ void BaseAudioContext::ThrowExceptionForClosedState( ...@@ -204,7 +204,7 @@ void BaseAudioContext::ThrowExceptionForClosedState(
"AudioContext has been closed."); "AudioContext has been closed.");
} }
AudioBuffer* BaseAudioContext::createBuffer(unsigned number_of_channels, AudioBuffer* BaseAudioContext::createBuffer(uint32_t number_of_channels,
uint32_t number_of_frames, uint32_t number_of_frames,
float sample_rate, float sample_rate,
ExceptionState& exception_state) { ExceptionState& exception_state) {
...@@ -369,7 +369,7 @@ ScriptProcessorNode* BaseAudioContext::createScriptProcessor( ...@@ -369,7 +369,7 @@ ScriptProcessorNode* BaseAudioContext::createScriptProcessor(
} }
ScriptProcessorNode* BaseAudioContext::createScriptProcessor( ScriptProcessorNode* BaseAudioContext::createScriptProcessor(
size_t buffer_size, uint32_t buffer_size,
ExceptionState& exception_state) { ExceptionState& exception_state) {
DCHECK(IsMainThread()); DCHECK(IsMainThread());
...@@ -377,8 +377,8 @@ ScriptProcessorNode* BaseAudioContext::createScriptProcessor( ...@@ -377,8 +377,8 @@ ScriptProcessorNode* BaseAudioContext::createScriptProcessor(
} }
ScriptProcessorNode* BaseAudioContext::createScriptProcessor( ScriptProcessorNode* BaseAudioContext::createScriptProcessor(
size_t buffer_size, uint32_t buffer_size,
size_t number_of_input_channels, uint32_t number_of_input_channels,
ExceptionState& exception_state) { ExceptionState& exception_state) {
DCHECK(IsMainThread()); DCHECK(IsMainThread());
...@@ -387,9 +387,9 @@ ScriptProcessorNode* BaseAudioContext::createScriptProcessor( ...@@ -387,9 +387,9 @@ ScriptProcessorNode* BaseAudioContext::createScriptProcessor(
} }
ScriptProcessorNode* BaseAudioContext::createScriptProcessor( ScriptProcessorNode* BaseAudioContext::createScriptProcessor(
size_t buffer_size, uint32_t buffer_size,
size_t number_of_input_channels, uint32_t number_of_input_channels,
size_t number_of_output_channels, uint32_t number_of_output_channels,
ExceptionState& exception_state) { ExceptionState& exception_state) {
DCHECK(IsMainThread()); DCHECK(IsMainThread());
...@@ -473,7 +473,7 @@ ChannelSplitterNode* BaseAudioContext::createChannelSplitter( ...@@ -473,7 +473,7 @@ ChannelSplitterNode* BaseAudioContext::createChannelSplitter(
} }
ChannelSplitterNode* BaseAudioContext::createChannelSplitter( ChannelSplitterNode* BaseAudioContext::createChannelSplitter(
size_t number_of_outputs, uint32_t number_of_outputs,
ExceptionState& exception_state) { ExceptionState& exception_state) {
DCHECK(IsMainThread()); DCHECK(IsMainThread());
...@@ -488,7 +488,7 @@ ChannelMergerNode* BaseAudioContext::createChannelMerger( ...@@ -488,7 +488,7 @@ ChannelMergerNode* BaseAudioContext::createChannelMerger(
} }
ChannelMergerNode* BaseAudioContext::createChannelMerger( ChannelMergerNode* BaseAudioContext::createChannelMerger(
size_t number_of_inputs, uint32_t number_of_inputs,
ExceptionState& exception_state) { ExceptionState& exception_state) {
DCHECK(IsMainThread()); DCHECK(IsMainThread());
...@@ -697,8 +697,8 @@ static bool IsAudible(const AudioBus* rendered_data) { ...@@ -697,8 +697,8 @@ static bool IsAudible(const AudioBus* rendered_data) {
// for the total energy. // for the total energy.
float energy = 0; float energy = 0;
unsigned data_size = rendered_data->length(); uint32_t data_size = rendered_data->length();
for (unsigned k = 0; k < rendered_data->NumberOfChannels(); ++k) { for (uint32_t k = 0; k < rendered_data->NumberOfChannels(); ++k) {
const float* data = rendered_data->Channel(k)->Data(); const float* data = rendered_data->Channel(k)->Data();
float channel_energy; float channel_energy;
vector_math::Vsvesq(data, 1, &channel_energy, data_size); vector_math::Vsvesq(data, 1, &channel_energy, data_size);
...@@ -790,11 +790,11 @@ void BaseAudioContext::PerformCleanupOnMainThread() { ...@@ -790,11 +790,11 @@ void BaseAudioContext::PerformCleanupOnMainThread() {
} }
// Break the connection and release active nodes that have finished // Break the connection and release active nodes that have finished
// playing. // playing.
unsigned remove_count = 0; wtf_size_t remove_count = 0;
Vector<bool> removables; Vector<bool> removables;
removables.resize(active_source_nodes_.size()); removables.resize(active_source_nodes_.size());
for (AudioHandler* handler : finished_handlers) { for (AudioHandler* handler : finished_handlers) {
for (unsigned i = 0; i < active_source_nodes_.size(); ++i) { for (wtf_size_t i = 0; i < active_source_nodes_.size(); ++i) {
if (handler == &active_source_nodes_[i]->Handler()) { if (handler == &active_source_nodes_[i]->Handler()) {
handler->BreakConnectionWithLock(); handler->BreakConnectionWithLock();
removables[i] = true; removables[i] = true;
...@@ -808,11 +808,11 @@ void BaseAudioContext::PerformCleanupOnMainThread() { ...@@ -808,11 +808,11 @@ void BaseAudioContext::PerformCleanupOnMainThread() {
if (remove_count > 0) { if (remove_count > 0) {
HeapVector<Member<AudioNode>> actives; HeapVector<Member<AudioNode>> actives;
DCHECK_GE(active_source_nodes_.size(), remove_count); DCHECK_GE(active_source_nodes_.size(), remove_count);
size_t initial_capacity = wtf_size_t initial_capacity =
std::min(active_source_nodes_.size() - remove_count, std::min(active_source_nodes_.size() - remove_count,
active_source_nodes_.size()); active_source_nodes_.size());
actives.ReserveInitialCapacity(initial_capacity); actives.ReserveInitialCapacity(initial_capacity);
for (unsigned i = 0; i < removables.size(); ++i) { for (wtf_size_t i = 0; i < removables.size(); ++i) {
if (!removables[i]) if (!removables[i])
actives.push_back(active_source_nodes_[i]); actives.push_back(active_source_nodes_[i]);
} }
......
...@@ -137,7 +137,7 @@ class MODULES_EXPORT BaseAudioContext ...@@ -137,7 +137,7 @@ class MODULES_EXPORT BaseAudioContext
AudioContextState ContextState() const { return context_state_; } AudioContextState ContextState() const { return context_state_; }
void ThrowExceptionForClosedState(ExceptionState&); void ThrowExceptionForClosedState(ExceptionState&);
AudioBuffer* createBuffer(unsigned number_of_channels, AudioBuffer* createBuffer(uint32_t number_of_channels,
uint32_t number_of_frames, uint32_t number_of_frames,
float sample_rate, float sample_rate,
ExceptionState&); ExceptionState&);
...@@ -184,21 +184,21 @@ class MODULES_EXPORT BaseAudioContext ...@@ -184,21 +184,21 @@ class MODULES_EXPORT BaseAudioContext
DynamicsCompressorNode* createDynamicsCompressor(ExceptionState&); DynamicsCompressorNode* createDynamicsCompressor(ExceptionState&);
AnalyserNode* createAnalyser(ExceptionState&); AnalyserNode* createAnalyser(ExceptionState&);
ScriptProcessorNode* createScriptProcessor(ExceptionState&); ScriptProcessorNode* createScriptProcessor(ExceptionState&);
ScriptProcessorNode* createScriptProcessor(size_t buffer_size, ScriptProcessorNode* createScriptProcessor(uint32_t buffer_size,
ExceptionState&); ExceptionState&);
ScriptProcessorNode* createScriptProcessor(size_t buffer_size, ScriptProcessorNode* createScriptProcessor(uint32_t buffer_size,
size_t number_of_input_channels, uint32_t number_of_input_channels,
ExceptionState&); ExceptionState&);
ScriptProcessorNode* createScriptProcessor(size_t buffer_size, ScriptProcessorNode* createScriptProcessor(uint32_t buffer_size,
size_t number_of_input_channels, uint32_t number_of_input_channels,
size_t number_of_output_channels, uint32_t number_of_output_channels,
ExceptionState&); ExceptionState&);
StereoPannerNode* createStereoPanner(ExceptionState&); StereoPannerNode* createStereoPanner(ExceptionState&);
ChannelSplitterNode* createChannelSplitter(ExceptionState&); ChannelSplitterNode* createChannelSplitter(ExceptionState&);
ChannelSplitterNode* createChannelSplitter(size_t number_of_outputs, ChannelSplitterNode* createChannelSplitter(uint32_t number_of_outputs,
ExceptionState&); ExceptionState&);
ChannelMergerNode* createChannelMerger(ExceptionState&); ChannelMergerNode* createChannelMerger(ExceptionState&);
ChannelMergerNode* createChannelMerger(size_t number_of_inputs, ChannelMergerNode* createChannelMerger(uint32_t number_of_inputs,
ExceptionState&); ExceptionState&);
OscillatorNode* createOscillator(ExceptionState&); OscillatorNode* createOscillator(ExceptionState&);
PeriodicWave* createPeriodicWave(const Vector<float>& real, PeriodicWave* createPeriodicWave(const Vector<float>& real,
...@@ -255,7 +255,7 @@ class MODULES_EXPORT BaseAudioContext ...@@ -255,7 +255,7 @@ class MODULES_EXPORT BaseAudioContext
using GraphAutoLocker = DeferredTaskHandler::GraphAutoLocker; using GraphAutoLocker = DeferredTaskHandler::GraphAutoLocker;
// Returns the maximum numuber of channels we can support. // Returns the maximum numuber of channels we can support.
static unsigned MaxNumberOfChannels() { return kMaxNumberOfChannels; } static uint32_t MaxNumberOfChannels() { return kMaxNumberOfChannels; }
// EventTarget // EventTarget
const AtomicString& InterfaceName() const final; const AtomicString& InterfaceName() const final;
......
...@@ -110,7 +110,7 @@ void ConvolverHandler::SetBuffer(AudioBuffer* buffer, ...@@ -110,7 +110,7 @@ void ConvolverHandler::SetBuffer(AudioBuffer* buffer,
} }
unsigned number_of_channels = buffer->numberOfChannels(); unsigned number_of_channels = buffer->numberOfChannels();
size_t buffer_length = buffer->length(); uint32_t buffer_length = buffer->length();
// The current implementation supports only 1-, 2-, or 4-channel impulse // The current implementation supports only 1-, 2-, or 4-channel impulse
// responses, with the 4-channel response being interpreted as true-stereo // responses, with the 4-channel response being interpreted as true-stereo
......
...@@ -202,7 +202,7 @@ void DefaultAudioDestinationHandler::Render( ...@@ -202,7 +202,7 @@ void DefaultAudioDestinationHandler::Render(
Context()->UpdateWorkletGlobalScopeOnRenderingThread(); Context()->UpdateWorkletGlobalScopeOnRenderingThread();
} }
size_t DefaultAudioDestinationHandler::GetCallbackBufferSize() const { uint32_t DefaultAudioDestinationHandler::GetCallbackBufferSize() const {
DCHECK(IsMainThread()); DCHECK(IsMainThread());
DCHECK(IsInitialized()); DCHECK(IsInitialized());
......
...@@ -70,7 +70,7 @@ class DefaultAudioDestinationHandler final : public AudioDestinationHandler, ...@@ -70,7 +70,7 @@ class DefaultAudioDestinationHandler final : public AudioDestinationHandler,
const AudioIOPosition& output_position) final; const AudioIOPosition& output_position) final;
// Returns a hadrware callback buffer size from audio infra. // Returns a hadrware callback buffer size from audio infra.
size_t GetCallbackBufferSize() const; uint32_t GetCallbackBufferSize() const;
// Returns a given frames-per-buffer size from audio infra. // Returns a given frames-per-buffer size from audio infra.
int GetFramesPerBuffer() const; int GetFramesPerBuffer() const;
......
...@@ -220,7 +220,7 @@ unsigned PeriodicWave::NumberOfPartialsForRange(unsigned range_index) const { ...@@ -220,7 +220,7 @@ unsigned PeriodicWave::NumberOfPartialsForRange(unsigned range_index) const {
// Tell V8 about the memory we're using so it can properly schedule garbage // Tell V8 about the memory we're using so it can properly schedule garbage
// collects. // collects.
void PeriodicWave::AdjustV8ExternalMemory(int delta) { void PeriodicWave::AdjustV8ExternalMemory(int64_t delta) {
v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory(delta); v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory(delta);
v8_external_memory_ += delta; v8_external_memory_ += delta;
} }
......
...@@ -111,7 +111,7 @@ class PeriodicWave final : public ScriptWrappable { ...@@ -111,7 +111,7 @@ class PeriodicWave final : public ScriptWrappable {
unsigned NumberOfPartialsForRange(unsigned range_index) const; unsigned NumberOfPartialsForRange(unsigned range_index) const;
void AdjustV8ExternalMemory(int delta); void AdjustV8ExternalMemory(int64_t delta);
// Creates tables based on numberOfComponents Fourier coefficients. // Creates tables based on numberOfComponents Fourier coefficients.
void CreateBandLimitedTables(const float* real, void CreateBandLimitedTables(const float* real,
......
...@@ -46,9 +46,9 @@ namespace blink { ...@@ -46,9 +46,9 @@ namespace blink {
ScriptProcessorHandler::ScriptProcessorHandler( ScriptProcessorHandler::ScriptProcessorHandler(
AudioNode& node, AudioNode& node,
float sample_rate, float sample_rate,
size_t buffer_size, uint32_t buffer_size,
unsigned number_of_input_channels, uint32_t number_of_input_channels,
unsigned number_of_output_channels) uint32_t number_of_output_channels)
: AudioHandler(kNodeTypeScriptProcessor, node, sample_rate), : AudioHandler(kNodeTypeScriptProcessor, node, sample_rate),
double_buffer_index_(0), double_buffer_index_(0),
input_buffers_(MakeGarbageCollected<HeapVector<Member<AudioBuffer>>>()), input_buffers_(MakeGarbageCollected<HeapVector<Member<AudioBuffer>>>()),
...@@ -85,9 +85,9 @@ ScriptProcessorHandler::ScriptProcessorHandler( ...@@ -85,9 +85,9 @@ ScriptProcessorHandler::ScriptProcessorHandler(
scoped_refptr<ScriptProcessorHandler> ScriptProcessorHandler::Create( scoped_refptr<ScriptProcessorHandler> ScriptProcessorHandler::Create(
AudioNode& node, AudioNode& node,
float sample_rate, float sample_rate,
size_t buffer_size, uint32_t buffer_size,
unsigned number_of_input_channels, uint32_t number_of_input_channels,
unsigned number_of_output_channels) { uint32_t number_of_output_channels) {
return base::AdoptRef(new ScriptProcessorHandler( return base::AdoptRef(new ScriptProcessorHandler(
node, sample_rate, buffer_size, number_of_input_channels, node, sample_rate, buffer_size, number_of_input_channels,
number_of_output_channels)); number_of_output_channels));
...@@ -106,7 +106,7 @@ void ScriptProcessorHandler::Initialize() { ...@@ -106,7 +106,7 @@ void ScriptProcessorHandler::Initialize() {
// Create double buffers on both the input and output sides. // Create double buffers on both the input and output sides.
// These AudioBuffers will be directly accessed in the main thread by // These AudioBuffers will be directly accessed in the main thread by
// JavaScript. // JavaScript.
for (unsigned i = 0; i < 2; ++i) { for (uint32_t i = 0; i < 2; ++i) {
AudioBuffer* input_buffer = AudioBuffer* input_buffer =
number_of_input_channels_ number_of_input_channels_
? AudioBuffer::Create(number_of_input_channels_, BufferSize(), ? AudioBuffer::Create(number_of_input_channels_, BufferSize(),
...@@ -140,7 +140,7 @@ void ScriptProcessorHandler::Process(uint32_t frames_to_process) { ...@@ -140,7 +140,7 @@ void ScriptProcessorHandler::Process(uint32_t frames_to_process) {
// Get input and output buffers. We double-buffer both the input and output // Get input and output buffers. We double-buffer both the input and output
// sides. // sides.
unsigned double_buffer_index = this->DoubleBufferIndex(); uint32_t double_buffer_index = this->DoubleBufferIndex();
bool is_double_buffer_index_good = bool is_double_buffer_index_good =
double_buffer_index < 2 && double_buffer_index < input_buffers_->size() && double_buffer_index < 2 && double_buffer_index < input_buffers_->size() &&
double_buffer_index < output_buffers_->size(); double_buffer_index < output_buffers_->size();
...@@ -152,7 +152,7 @@ void ScriptProcessorHandler::Process(uint32_t frames_to_process) { ...@@ -152,7 +152,7 @@ void ScriptProcessorHandler::Process(uint32_t frames_to_process) {
AudioBuffer* output_buffer = output_buffers_->at(double_buffer_index).Get(); AudioBuffer* output_buffer = output_buffers_->at(double_buffer_index).Get();
// Check the consistency of input and output buffers. // Check the consistency of input and output buffers.
unsigned number_of_input_channels = internal_input_bus_->NumberOfChannels(); uint32_t number_of_input_channels = internal_input_bus_->NumberOfChannels();
bool buffers_are_good = bool buffers_are_good =
output_buffer && BufferSize() == output_buffer->length() && output_buffer && BufferSize() == output_buffer->length() &&
buffer_read_write_index_ + frames_to_process <= BufferSize(); buffer_read_write_index_ + frames_to_process <= BufferSize();
...@@ -175,7 +175,7 @@ void ScriptProcessorHandler::Process(uint32_t frames_to_process) { ...@@ -175,7 +175,7 @@ void ScriptProcessorHandler::Process(uint32_t frames_to_process) {
if (!is_frames_to_process_good) if (!is_frames_to_process_good)
return; return;
unsigned number_of_output_channels = output_bus->NumberOfChannels(); uint32_t number_of_output_channels = output_bus->NumberOfChannels();
bool channels_are_good = bool channels_are_good =
(number_of_input_channels == number_of_input_channels_) && (number_of_input_channels == number_of_input_channels_) &&
...@@ -184,7 +184,7 @@ void ScriptProcessorHandler::Process(uint32_t frames_to_process) { ...@@ -184,7 +184,7 @@ void ScriptProcessorHandler::Process(uint32_t frames_to_process) {
if (!channels_are_good) if (!channels_are_good)
return; return;
for (unsigned i = 0; i < number_of_input_channels; ++i) for (uint32_t i = 0; i < number_of_input_channels; ++i)
internal_input_bus_->SetChannelMemory( internal_input_bus_->SetChannelMemory(
i, i,
input_buffer->getChannelData(i).View()->Data() + input_buffer->getChannelData(i).View()->Data() +
...@@ -195,7 +195,7 @@ void ScriptProcessorHandler::Process(uint32_t frames_to_process) { ...@@ -195,7 +195,7 @@ void ScriptProcessorHandler::Process(uint32_t frames_to_process) {
internal_input_bus_->CopyFrom(*input_bus); internal_input_bus_->CopyFrom(*input_bus);
// Copy from the output buffer to the output. // Copy from the output buffer to the output.
for (unsigned i = 0; i < number_of_output_channels; ++i) { for (uint32_t i = 0; i < number_of_output_channels; ++i) {
memcpy(output_bus->Channel(i)->MutableData(), memcpy(output_bus->Channel(i)->MutableData(),
output_buffer->getChannelData(i).View()->Data() + output_buffer->getChannelData(i).View()->Data() +
buffer_read_write_index_, buffer_read_write_index_,
...@@ -253,7 +253,7 @@ void ScriptProcessorHandler::Process(uint32_t frames_to_process) { ...@@ -253,7 +253,7 @@ void ScriptProcessorHandler::Process(uint32_t frames_to_process) {
} }
} }
void ScriptProcessorHandler::FireProcessEvent(unsigned double_buffer_index) { void ScriptProcessorHandler::FireProcessEvent(uint32_t double_buffer_index) {
DCHECK(IsMainThread()); DCHECK(IsMainThread());
if (!Context() || !Context()->GetExecutionContext()) if (!Context() || !Context()->GetExecutionContext())
...@@ -288,7 +288,7 @@ void ScriptProcessorHandler::FireProcessEvent(unsigned double_buffer_index) { ...@@ -288,7 +288,7 @@ void ScriptProcessorHandler::FireProcessEvent(unsigned double_buffer_index) {
} }
void ScriptProcessorHandler::FireProcessEventForOfflineAudioContext( void ScriptProcessorHandler::FireProcessEventForOfflineAudioContext(
unsigned double_buffer_index, uint32_t double_buffer_index,
WaitableEvent* waitable_event) { WaitableEvent* waitable_event) {
DCHECK(IsMainThread()); DCHECK(IsMainThread());
...@@ -334,7 +334,7 @@ double ScriptProcessorHandler::LatencyTime() const { ...@@ -334,7 +334,7 @@ double ScriptProcessorHandler::LatencyTime() const {
return std::numeric_limits<double>::infinity(); return std::numeric_limits<double>::infinity();
} }
void ScriptProcessorHandler::SetChannelCount(unsigned channel_count, void ScriptProcessorHandler::SetChannelCount(uint32_t channel_count,
ExceptionState& exception_state) { ExceptionState& exception_state) {
DCHECK(IsMainThread()); DCHECK(IsMainThread());
BaseAudioContext::GraphAutoLocker locker(Context()); BaseAudioContext::GraphAutoLocker locker(Context());
...@@ -365,22 +365,22 @@ void ScriptProcessorHandler::SetChannelCountMode( ...@@ -365,22 +365,22 @@ void ScriptProcessorHandler::SetChannelCountMode(
ScriptProcessorNode::ScriptProcessorNode(BaseAudioContext& context, ScriptProcessorNode::ScriptProcessorNode(BaseAudioContext& context,
float sample_rate, float sample_rate,
size_t buffer_size, uint32_t buffer_size,
unsigned number_of_input_channels, uint32_t number_of_input_channels,
unsigned number_of_output_channels) uint32_t number_of_output_channels)
: AudioNode(context) { : AudioNode(context) {
SetHandler(ScriptProcessorHandler::Create(*this, sample_rate, buffer_size, SetHandler(ScriptProcessorHandler::Create(*this, sample_rate, buffer_size,
number_of_input_channels, number_of_input_channels,
number_of_output_channels)); number_of_output_channels));
} }
static size_t ChooseBufferSize(size_t callback_buffer_size) { static uint32_t ChooseBufferSize(uint32_t callback_buffer_size) {
// Choose a buffer size based on the audio hardware buffer size. Arbitarily // 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 // make it a power of two that is 4 times greater than the hardware buffer
// size. // size.
// TODO(crbug.com/855758): What is the best way to choose this? // TODO(crbug.com/855758): What is the best way to choose this?
size_t buffer_size = uint32_t buffer_size =
1 << static_cast<unsigned>(log2(4 * callback_buffer_size) + 0.5); 1 << static_cast<uint32_t>(log2(4 * callback_buffer_size) + 0.5);
if (buffer_size < 256) if (buffer_size < 256)
return 256; return 256;
...@@ -402,7 +402,7 @@ ScriptProcessorNode* ScriptProcessorNode::Create( ...@@ -402,7 +402,7 @@ ScriptProcessorNode* ScriptProcessorNode::Create(
ScriptProcessorNode* ScriptProcessorNode::Create( ScriptProcessorNode* ScriptProcessorNode::Create(
BaseAudioContext& context, BaseAudioContext& context,
size_t requested_buffer_size, uint32_t requested_buffer_size,
ExceptionState& exception_state) { ExceptionState& exception_state) {
DCHECK(IsMainThread()); DCHECK(IsMainThread());
...@@ -412,8 +412,8 @@ ScriptProcessorNode* ScriptProcessorNode::Create( ...@@ -412,8 +412,8 @@ ScriptProcessorNode* ScriptProcessorNode::Create(
ScriptProcessorNode* ScriptProcessorNode::Create( ScriptProcessorNode* ScriptProcessorNode::Create(
BaseAudioContext& context, BaseAudioContext& context,
size_t requested_buffer_size, uint32_t requested_buffer_size,
unsigned number_of_input_channels, uint32_t number_of_input_channels,
ExceptionState& exception_state) { ExceptionState& exception_state) {
DCHECK(IsMainThread()); DCHECK(IsMainThread());
...@@ -424,9 +424,9 @@ ScriptProcessorNode* ScriptProcessorNode::Create( ...@@ -424,9 +424,9 @@ ScriptProcessorNode* ScriptProcessorNode::Create(
ScriptProcessorNode* ScriptProcessorNode::Create( ScriptProcessorNode* ScriptProcessorNode::Create(
BaseAudioContext& context, BaseAudioContext& context,
size_t requested_buffer_size, uint32_t requested_buffer_size,
unsigned number_of_input_channels, uint32_t number_of_input_channels,
unsigned number_of_output_channels, uint32_t number_of_output_channels,
ExceptionState& exception_state) { ExceptionState& exception_state) {
DCHECK(IsMainThread()); DCHECK(IsMainThread());
...@@ -461,7 +461,7 @@ ScriptProcessorNode* ScriptProcessorNode::Create( ...@@ -461,7 +461,7 @@ ScriptProcessorNode* ScriptProcessorNode::Create(
} }
// Sanitize user-supplied buffer size. // Sanitize user-supplied buffer size.
size_t buffer_size; uint32_t buffer_size;
switch (requested_buffer_size) { switch (requested_buffer_size) {
case 0: case 0:
// Choose an appropriate size. For an AudioContext, we need to // Choose an appropriate size. For an AudioContext, we need to
......
...@@ -54,52 +54,52 @@ class ScriptProcessorHandler final : public AudioHandler { ...@@ -54,52 +54,52 @@ class ScriptProcessorHandler final : public AudioHandler {
static scoped_refptr<ScriptProcessorHandler> Create( static scoped_refptr<ScriptProcessorHandler> Create(
AudioNode&, AudioNode&,
float sample_rate, float sample_rate,
size_t buffer_size, uint32_t buffer_size,
unsigned number_of_input_channels, uint32_t number_of_input_channels,
unsigned number_of_output_channels); uint32_t number_of_output_channels);
~ScriptProcessorHandler() override; ~ScriptProcessorHandler() override;
// AudioHandler // AudioHandler
void Process(uint32_t frames_to_process) override; void Process(uint32_t frames_to_process) override;
void Initialize() override; void Initialize() override;
uint32_t BufferSize() const { return static_cast<uint32_t>(buffer_size_); } uint32_t BufferSize() const { return buffer_size_; }
void SetChannelCount(unsigned, ExceptionState&) override; void SetChannelCount(uint32_t, ExceptionState&) override;
void SetChannelCountMode(const String&, ExceptionState&) override; void SetChannelCountMode(const String&, ExceptionState&) override;
unsigned NumberOfOutputChannels() const override { uint32_t NumberOfOutputChannels() const override {
return number_of_output_channels_; return number_of_output_channels_;
} }
private: private:
ScriptProcessorHandler(AudioNode&, ScriptProcessorHandler(AudioNode&,
float sample_rate, float sample_rate,
size_t buffer_size, uint32_t buffer_size,
unsigned number_of_input_channels, uint32_t number_of_input_channels,
unsigned number_of_output_channels); uint32_t number_of_output_channels);
double TailTime() const override; double TailTime() const override;
double LatencyTime() const override; double LatencyTime() const override;
bool RequiresTailProcessing() const final; bool RequiresTailProcessing() const final;
void FireProcessEvent(unsigned); void FireProcessEvent(uint32_t);
void FireProcessEventForOfflineAudioContext(unsigned, WaitableEvent*); void FireProcessEventForOfflineAudioContext(uint32_t, WaitableEvent*);
// Double buffering // Double buffering
unsigned DoubleBufferIndex() const { return double_buffer_index_; } uint32_t DoubleBufferIndex() const { return double_buffer_index_; }
void SwapBuffers() { double_buffer_index_ = 1 - double_buffer_index_; } void SwapBuffers() { double_buffer_index_ = 1 - double_buffer_index_; }
unsigned double_buffer_index_; uint32_t double_buffer_index_;
// These Persistent don't make reference cycles including the owner // These Persistent don't make reference cycles including the owner
// ScriptProcessorNode. // ScriptProcessorNode.
CrossThreadPersistent<HeapVector<Member<AudioBuffer>>> input_buffers_; CrossThreadPersistent<HeapVector<Member<AudioBuffer>>> input_buffers_;
CrossThreadPersistent<HeapVector<Member<AudioBuffer>>> output_buffers_; CrossThreadPersistent<HeapVector<Member<AudioBuffer>>> output_buffers_;
size_t buffer_size_; uint32_t buffer_size_;
unsigned buffer_read_write_index_; uint32_t buffer_read_write_index_;
unsigned number_of_input_channels_; uint32_t number_of_input_channels_;
unsigned number_of_output_channels_; uint32_t number_of_output_channels_;
scoped_refptr<AudioBus> internal_input_bus_; scoped_refptr<AudioBus> internal_input_bus_;
// Synchronize process() with fireProcessEvent(). // Synchronize process() with fireProcessEvent().
...@@ -127,23 +127,23 @@ class ScriptProcessorNode final ...@@ -127,23 +127,23 @@ class ScriptProcessorNode final
// The value chosen must carefully balance between latency and audio quality. // The value chosen must carefully balance between latency and audio quality.
static ScriptProcessorNode* Create(BaseAudioContext&, ExceptionState&); static ScriptProcessorNode* Create(BaseAudioContext&, ExceptionState&);
static ScriptProcessorNode* Create(BaseAudioContext&, static ScriptProcessorNode* Create(BaseAudioContext&,
size_t requested_buffer_size, uint32_t requested_buffer_size,
ExceptionState&); ExceptionState&);
static ScriptProcessorNode* Create(BaseAudioContext&, static ScriptProcessorNode* Create(BaseAudioContext&,
size_t requested_buffer_size, uint32_t requested_buffer_size,
unsigned number_of_input_channels, uint32_t number_of_input_channels,
ExceptionState&); ExceptionState&);
static ScriptProcessorNode* Create(BaseAudioContext&, static ScriptProcessorNode* Create(BaseAudioContext&,
size_t requested_buffer_size, uint32_t requested_buffer_size,
unsigned number_of_input_channels, uint32_t number_of_input_channels,
unsigned number_of_output_channels, uint32_t number_of_output_channels,
ExceptionState&); ExceptionState&);
ScriptProcessorNode(BaseAudioContext&, ScriptProcessorNode(BaseAudioContext&,
float sample_rate, float sample_rate,
size_t buffer_size, uint32_t buffer_size,
unsigned number_of_input_channels, uint32_t number_of_input_channels,
unsigned number_of_output_channels); uint32_t number_of_output_channels);
DEFINE_ATTRIBUTE_EVENT_LISTENER(audioprocess, kAudioprocess); DEFINE_ATTRIBUTE_EVENT_LISTENER(audioprocess, kAudioprocess);
uint32_t bufferSize() const; uint32_t bufferSize() const;
......
...@@ -49,7 +49,7 @@ using namespace vector_math; ...@@ -49,7 +49,7 @@ using namespace vector_math;
const unsigned kMaxBusChannels = 32; const unsigned kMaxBusChannels = 32;
scoped_refptr<AudioBus> AudioBus::Create(unsigned number_of_channels, scoped_refptr<AudioBus> AudioBus::Create(unsigned number_of_channels,
size_t length, uint32_t length,
bool allocate) { bool allocate) {
DCHECK_LE(number_of_channels, kMaxBusChannels); DCHECK_LE(number_of_channels, kMaxBusChannels);
if (number_of_channels > kMaxBusChannels) if (number_of_channels > kMaxBusChannels)
...@@ -58,7 +58,7 @@ scoped_refptr<AudioBus> AudioBus::Create(unsigned number_of_channels, ...@@ -58,7 +58,7 @@ scoped_refptr<AudioBus> AudioBus::Create(unsigned number_of_channels,
return base::AdoptRef(new AudioBus(number_of_channels, length, allocate)); return base::AdoptRef(new AudioBus(number_of_channels, length, allocate));
} }
AudioBus::AudioBus(unsigned number_of_channels, size_t length, bool allocate) AudioBus::AudioBus(unsigned number_of_channels, uint32_t length, bool allocate)
: length_(length), sample_rate_(0) { : length_(length), sample_rate_(0) {
channels_.ReserveInitialCapacity(number_of_channels); channels_.ReserveInitialCapacity(number_of_channels);
...@@ -74,7 +74,7 @@ AudioBus::AudioBus(unsigned number_of_channels, size_t length, bool allocate) ...@@ -74,7 +74,7 @@ AudioBus::AudioBus(unsigned number_of_channels, size_t length, bool allocate)
void AudioBus::SetChannelMemory(unsigned channel_index, void AudioBus::SetChannelMemory(unsigned channel_index,
float* storage, float* storage,
size_t length) { uint32_t length) {
if (channel_index < channels_.size()) { if (channel_index < channels_.size()) {
Channel(channel_index)->Set(storage, length); Channel(channel_index)->Set(storage, length);
// FIXME: verify that this length matches all the other channel lengths // FIXME: verify that this length matches all the other channel lengths
...@@ -82,7 +82,7 @@ void AudioBus::SetChannelMemory(unsigned channel_index, ...@@ -82,7 +82,7 @@ void AudioBus::SetChannelMemory(unsigned channel_index,
} }
} }
void AudioBus::ResizeSmaller(size_t new_length) { void AudioBus::ResizeSmaller(uint32_t new_length) {
DCHECK_LE(new_length, length_); DCHECK_LE(new_length, length_);
if (new_length <= length_) if (new_length <= length_)
length_ = new_length; length_ = new_length;
...@@ -190,7 +190,7 @@ scoped_refptr<AudioBus> AudioBus::CreateBufferFromRange( ...@@ -190,7 +190,7 @@ scoped_refptr<AudioBus> AudioBus::CreateBufferFromRange(
const AudioBus* source_buffer, const AudioBus* source_buffer,
unsigned start_frame, unsigned start_frame,
unsigned end_frame) { unsigned end_frame) {
size_t number_of_source_frames = source_buffer->length(); uint32_t number_of_source_frames = source_buffer->length();
unsigned number_of_channels = source_buffer->NumberOfChannels(); unsigned number_of_channels = source_buffer->NumberOfChannels();
// Sanity checking // Sanity checking
...@@ -200,7 +200,7 @@ scoped_refptr<AudioBus> AudioBus::CreateBufferFromRange( ...@@ -200,7 +200,7 @@ scoped_refptr<AudioBus> AudioBus::CreateBufferFromRange(
if (!is_range_safe) if (!is_range_safe)
return nullptr; return nullptr;
size_t range_length = end_frame - start_frame; uint32_t range_length = end_frame - start_frame;
scoped_refptr<AudioBus> audio_bus = Create(number_of_channels, range_length); scoped_refptr<AudioBus> audio_bus = Create(number_of_channels, range_length);
audio_bus->SetSampleRate(source_buffer->SampleRate()); audio_bus->SetSampleRate(source_buffer->SampleRate());
......
...@@ -70,11 +70,13 @@ class PLATFORM_EXPORT AudioBus : public ThreadSafeRefCounted<AudioBus> { ...@@ -70,11 +70,13 @@ class PLATFORM_EXPORT AudioBus : public ThreadSafeRefCounted<AudioBus> {
// is false then setChannelMemory() has to be called later on for each // is false then setChannelMemory() has to be called later on for each
// channel before the AudioBus is useable... // channel before the AudioBus is useable...
static scoped_refptr<AudioBus> Create(unsigned number_of_channels, static scoped_refptr<AudioBus> Create(unsigned number_of_channels,
size_t length, uint32_t length,
bool allocate = true); bool allocate = true);
// Tells the given channel to use an externally allocated buffer. // Tells the given channel to use an externally allocated buffer.
void SetChannelMemory(unsigned channel_index, float* storage, size_t length); void SetChannelMemory(unsigned channel_index,
float* storage,
uint32_t length);
// Channels // Channels
unsigned NumberOfChannels() const { return channels_.size(); } unsigned NumberOfChannels() const { return channels_.size(); }
...@@ -87,11 +89,11 @@ class PLATFORM_EXPORT AudioBus : public ThreadSafeRefCounted<AudioBus> { ...@@ -87,11 +89,11 @@ class PLATFORM_EXPORT AudioBus : public ThreadSafeRefCounted<AudioBus> {
const AudioChannel* ChannelByType(unsigned type) const; const AudioChannel* ChannelByType(unsigned type) const;
// Number of sample-frames // Number of sample-frames
size_t length() const { return length_; } uint32_t length() const { return length_; }
// resizeSmaller() can only be called with a new length <= the current length. // resizeSmaller() can only be called with a new length <= the current length.
// The data stored in the bus will remain undisturbed. // The data stored in the bus will remain undisturbed.
void ResizeSmaller(size_t new_length); void ResizeSmaller(uint32_t new_length);
// Sample-rate : 0.0 if unknown or "don't care" // Sample-rate : 0.0 if unknown or "don't care"
float SampleRate() const { return sample_rate_; } float SampleRate() const { return sample_rate_; }
...@@ -167,7 +169,7 @@ class PLATFORM_EXPORT AudioBus : public ThreadSafeRefCounted<AudioBus> { ...@@ -167,7 +169,7 @@ class PLATFORM_EXPORT AudioBus : public ThreadSafeRefCounted<AudioBus> {
protected: protected:
AudioBus() = default; AudioBus() = default;
AudioBus(unsigned number_of_channels, size_t length, bool allocate); AudioBus(unsigned number_of_channels, uint32_t length, bool allocate);
void DiscreteSumFrom(const AudioBus&); void DiscreteSumFrom(const AudioBus&);
...@@ -176,7 +178,7 @@ class PLATFORM_EXPORT AudioBus : public ThreadSafeRefCounted<AudioBus> { ...@@ -176,7 +178,7 @@ class PLATFORM_EXPORT AudioBus : public ThreadSafeRefCounted<AudioBus> {
void SumFromByUpMixing(const AudioBus&); void SumFromByUpMixing(const AudioBus&);
void SumFromByDownMixing(const AudioBus&); void SumFromByDownMixing(const AudioBus&);
size_t length_; uint32_t length_;
Vector<std::unique_ptr<AudioChannel>> channels_; Vector<std::unique_ptr<AudioChannel>> channels_;
int layout_; int layout_;
float sample_rate_; // 0.0 if unknown or N/A float sample_rate_; // 0.0 if unknown or N/A
......
...@@ -246,7 +246,7 @@ void AudioDestination::Resume() { ...@@ -246,7 +246,7 @@ void AudioDestination::Resume() {
} }
} }
size_t AudioDestination::CallbackBufferSize() const { uint32_t AudioDestination::CallbackBufferSize() const {
DCHECK(IsMainThread()); DCHECK(IsMainThread());
return callback_buffer_size_; return callback_buffer_size_;
} }
......
...@@ -99,7 +99,7 @@ class PLATFORM_EXPORT AudioDestination ...@@ -99,7 +99,7 @@ class PLATFORM_EXPORT AudioDestination
scoped_refptr<base::SingleThreadTaskRunner> worklet_task_runner); scoped_refptr<base::SingleThreadTaskRunner> worklet_task_runner);
// Getters must be accessed from the main thread. // Getters must be accessed from the main thread.
size_t CallbackBufferSize() const; uint32_t CallbackBufferSize() const;
bool IsPlaying(); bool IsPlaying();
// TODO(hongchan): this should not be called by the rendering thread. // TODO(hongchan): this should not be called by the rendering thread.
...@@ -124,7 +124,7 @@ class PLATFORM_EXPORT AudioDestination ...@@ -124,7 +124,7 @@ class PLATFORM_EXPORT AudioDestination
// Accessed by the main thread. // Accessed by the main thread.
std::unique_ptr<WebAudioDevice> web_audio_device_; std::unique_ptr<WebAudioDevice> web_audio_device_;
const unsigned number_of_output_channels_; const unsigned number_of_output_channels_;
size_t callback_buffer_size_; uint32_t callback_buffer_size_;
PlayState play_state_; PlayState play_state_;
// The task runner for AudioWorklet operation. This is only valid when // The task runner for AudioWorklet operation. This is only valid when
......
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