Commit f86fcc2e authored by vrk@chromium.org's avatar vrk@chromium.org

Clean up audio-related utility functions to compute buffer sizes

Renames SelectSamplesPerPacket() to GetHighLatencyOutputBufferSize() and moves
it into audio_hardware.h, where its low-latency counterpart lies.

BUG=NONE
TEST=NONE


Review URL: http://codereview.chromium.org/9442005

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@124981 0039d316-1c4b-4281-b951-d872f2087c98
parent 5f29c1d6
......@@ -12,7 +12,6 @@
#include "content/browser/renderer_host/media/audio_input_sync_writer.h"
#include "content/browser/renderer_host/media/media_stream_manager.h"
#include "content/common/media/audio_messages.h"
#include "media/audio/audio_util.h"
using content::BrowserMessageFilter;
using content::BrowserThread;
......@@ -209,11 +208,7 @@ void AudioInputRendererHost::OnCreateStream(int stream_id,
AudioParameters audio_params(params);
// Select the hardware packet size if not specified.
if (!audio_params.samples_per_packet) {
audio_params.samples_per_packet =
media::SelectSamplesPerPacket(audio_params.sample_rate);
}
DCHECK_GT(audio_params.samples_per_packet, 0);
uint32 packet_size = audio_params.GetPacketSize();
// Create a new AudioEntry structure.
......
......@@ -199,12 +199,8 @@ void AudioRendererHost::OnCreateStream(
DCHECK(LookupById(stream_id) == NULL);
AudioParameters audio_params(params);
DCHECK_GT(audio_params.samples_per_packet, 0);
// Select the hardware packet size if not specified.
if (!audio_params.samples_per_packet) {
audio_params.samples_per_packet =
media::SelectSamplesPerPacket(audio_params.sample_rate);
}
uint32 packet_size = audio_params.GetPacketSize();
scoped_ptr<AudioEntry> entry(new AudioEntry());
......
......@@ -204,7 +204,7 @@ class AudioRendererHostTest : public testing::Test {
params.channels = 2;
params.sample_rate = AudioParameters::kAudioCDSampleRate;
params.bits_per_sample = 16;
params.samples_per_packet = 0;
params.samples_per_packet = AudioParameters::kAudioCDSampleRate / 10;
// Send a create stream message to the audio output stream and wait until
// we receive the created message.
......
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
......@@ -48,6 +48,31 @@ size_t GetOutputBufferSize() {
return output_buffer_size;
}
size_t GetHighLatencyOutputBufferSize(int sample_rate) {
// The minimum number of samples in a hardware packet.
// This value is selected so that we can handle down to 5khz sample rate.
static const size_t kMinSamplesPerHardwarePacket = 1024;
// The maximum number of samples in a hardware packet.
// This value is selected so that we can handle up to 192khz sample rate.
static const size_t kMaxSamplesPerHardwarePacket = 64 * 1024;
// This constant governs the hardware audio buffer size, this value should be
// chosen carefully.
// This value is selected so that we have 8192 samples for 48khz streams.
static const size_t kMillisecondsPerHardwarePacket = 170;
// Select the number of samples that can provide at least
// |kMillisecondsPerHardwarePacket| worth of audio data.
size_t samples = kMinSamplesPerHardwarePacket;
while (samples <= kMaxSamplesPerHardwarePacket &&
samples * base::Time::kMillisecondsPerSecond <
sample_rate * kMillisecondsPerHardwarePacket) {
samples *= 2;
}
return samples;
}
uint32 GetInputChannelCount() {
DCHECK(RenderThreadImpl::current() != NULL);
......
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
......@@ -24,8 +24,13 @@ CONTENT_EXPORT double GetInputSampleRate();
// Fetch the buffer size we use for the default output device.
// Must be called from RenderThreadImpl::current().
// Must be used in conjunction with AUDIO_PCM_LOW_LATENCY.
CONTENT_EXPORT size_t GetOutputBufferSize();
// Computes a buffer size based on the given |sample_rate|. Must be used in
// conjunction with AUDIO_PCM_LINEAR.
CONTENT_EXPORT size_t GetHighLatencyOutputBufferSize(int sample_rate);
// Fetch the number of audio channels for the default input device.
// Must be called from RenderThreadImpl::current().
CONTENT_EXPORT uint32 GetInputChannelCount();
......@@ -33,6 +38,7 @@ CONTENT_EXPORT uint32 GetInputChannelCount();
// Forces the next call to any of the Get functions to query the hardware
// and repopulate the cache.
CONTENT_EXPORT void ResetCache();
} // namespace audio_hardware
#endif // CONTENT_RENDERER_MEDIA_AUDIO_HARDWARE_H_
......@@ -11,6 +11,7 @@
#include "base/bind.h"
#include "content/common/child_process.h"
#include "content/common/media/audio_messages.h"
#include "content/renderer/media/audio_hardware.h"
#include "content/renderer/render_thread_impl.h"
#include "media/audio/audio_buffers_state.h"
#include "media/audio/audio_util.h"
......@@ -71,7 +72,7 @@ bool AudioRendererImpl::OnInitialize(int bits_per_channel,
if (!is_initialized_) {
sink_->Initialize(
media::SelectSamplesPerPacket(sample_rate),
audio_hardware::GetHighLatencyOutputBufferSize(sample_rate),
audio_parameters_.channels,
audio_parameters_.sample_rate,
audio_parameters_.format,
......
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
......@@ -9,6 +9,8 @@
#include "media/base/channel_layout.h"
#include "media/base/media_export.h"
// TODO(vrk): This should probably be changed to an immutable object instead of
// a struct. See crbug.com/115902.
struct MEDIA_EXPORT AudioParameters {
// Compare is useful when AudioParameters is used as a key in std::map.
class MEDIA_EXPORT Compare {
......
......@@ -123,11 +123,6 @@ MEDIA_EXPORT bool IsWASAPISupported();
MEDIA_EXPORT void Crossfade(int bytes_to_crossfade, int number_of_channels,
int bytes_per_channel, const uint8* src,
uint8* dest);
// Calculates a safe hardware buffer size (in number of samples) given a set
// of audio parameters.
MEDIA_EXPORT uint32 SelectSamplesPerPacket(int sample_rate);
} // namespace media
#endif // MEDIA_AUDIO_AUDIO_UTIL_H_
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