summaryrefslogtreecommitdiffstats
path: root/media/audio
diff options
context:
space:
mode:
authorhshi@chromium.org <hshi@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-03-07 01:09:42 +0000
committerhshi@chromium.org <hshi@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-03-07 01:09:42 +0000
commita32383a62ec66b892573fbef6c7434ad32fb2857 (patch)
tree8782790146237f2e48782c35866d58de99609733 /media/audio
parent73deb661416b1e9d72b4bf3b251deebd527e62ec (diff)
downloadchromium_src-a32383a62ec66b892573fbef6c7434ad32fb2857.zip
chromium_src-a32383a62ec66b892573fbef6c7434ad32fb2857.tar.gz
chromium_src-a32383a62ec66b892573fbef6c7434ad32fb2857.tar.bz2
Revert 186180 "Moved AudioUtil static functions:"
> Moved AudioUtil static functions: > GetAudioHardwareSampleRate > GetAudioInputHardwareSampleRate > GetAudioHardwareBufferSize > GetAudioInputHardwareChannelLayout > to AudioManager interfaces: > GetDefaultOutputStreamParameters() > GetDefaultInputStreamParameters(const std::string& device_id) > > By doing this, we remove the messy ifdef statements in AudioUtil, allow getting the native sample rate for Pulse, each AudioManager is responsible for providing the optimal audio params to achieve the best audio performance, for example, cras can raise the buffer size to reduce CPU consumption without affecting other Linux products. > > > BUG=178142, 137326, 120319 > TEST=media_unittests, content_unittests > > Review URL: https://chromiumcodereview.appspot.com/12316131 BUG=180682 TBR=xians@chromium.org TBR=dalecurtis@chromium.org Review URL: https://codereview.chromium.org/12440008 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@186566 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'media/audio')
-rw-r--r--media/audio/android/audio_manager_android.cc56
-rw-r--r--media/audio/android/audio_manager_android.h7
-rw-r--r--media/audio/audio_input_volume_unittest.cc25
-rw-r--r--media/audio/audio_low_latency_input_output_unittest.cc28
-rw-r--r--media/audio/audio_manager.h12
-rw-r--r--media/audio/audio_manager_base.cc28
-rw-r--r--media/audio/audio_manager_base.h21
-rw-r--r--media/audio/audio_output_proxy_unittest.cc2
-rw-r--r--media/audio/audio_util.cc118
-rw-r--r--media/audio/audio_util.h16
-rw-r--r--media/audio/cras/audio_manager_cras.cc45
-rw-r--r--media/audio/cras/audio_manager_cras.h7
-rw-r--r--media/audio/ios/audio_manager_ios.h5
-rw-r--r--media/audio/ios/audio_manager_ios.mm30
-rw-r--r--media/audio/linux/audio_manager_linux.cc59
-rw-r--r--media/audio/linux/audio_manager_linux.h7
-rw-r--r--media/audio/mac/audio_device_listener_mac.cc4
-rw-r--r--media/audio/mac/audio_low_latency_input_mac.cc8
-rw-r--r--media/audio/mac/audio_low_latency_output_mac.cc8
-rw-r--r--media/audio/mac/audio_manager_mac.cc47
-rw-r--r--media/audio/mac/audio_manager_mac.h7
-rw-r--r--media/audio/mock_audio_manager.cc10
-rw-r--r--media/audio/mock_audio_manager.h4
-rw-r--r--media/audio/openbsd/audio_manager_openbsd.cc38
-rw-r--r--media/audio/openbsd/audio_manager_openbsd.h5
-rw-r--r--media/audio/pulse/audio_manager_pulse.cc46
-rw-r--r--media/audio/pulse/audio_manager_pulse.h11
-rw-r--r--media/audio/win/audio_manager_win.cc102
-rw-r--r--media/audio/win/audio_manager_win.h7
-rw-r--r--media/audio/win/audio_output_win_unittest.cc3
30 files changed, 313 insertions, 453 deletions
diff --git a/media/audio/android/audio_manager_android.cc b/media/audio/android/audio_manager_android.cc
index 80e1a27..93296d7 100644
--- a/media/audio/android/audio_manager_android.cc
+++ b/media/audio/android/audio_manager_android.cc
@@ -8,9 +8,8 @@
#include "media/audio/android/opensles_input.h"
#include "media/audio/android/opensles_output.h"
#include "media/audio/audio_manager.h"
-#include "media/audio/audio_parameters.h"
+#include "media/audio/audio_util.h"
#include "media/audio/fake_audio_input_stream.h"
-#include "media/base/channel_layout.h"
namespace media {
@@ -44,16 +43,20 @@ void AudioManagerAndroid::GetAudioInputDeviceNames(
media::AudioDeviceName(kDefaultDeviceName, kDefaultDeviceId));
}
-AudioParameters AudioManagerAndroid::GetInputStreamParameters(
- const std::string& device_id) {
- // TODO(xians): figure out the right input sample rate and buffer size to
- // achieve the best audio performance for Android devices.
- // TODO(xians): query the native channel layout for the specific device.
- static const int kDefaultSampleRate = 16000;
- static const int kDefaultBufferSize = 1024;
+AudioParameters
+AudioManagerAndroid::GetPreferredLowLatencyOutputStreamParameters(
+ const AudioParameters& input_params) {
+ // TODO(leozwang): Android defines the minimal buffer size requirment
+ // we should follow it. From Android 4.1, a new audio low latency api
+ // set was introduced and is under development, we want to take advantage
+ // of it.
+ int buffer_size = GetAudioHardwareBufferSize();
+ if (input_params.frames_per_buffer() < buffer_size)
+ buffer_size = input_params.frames_per_buffer();
+
return AudioParameters(
- AudioParameters::AUDIO_PCM_LOW_LATENCY, CHANNEL_LAYOUT_STEREO,
- kDefaultSampleRate, 16, kDefaultBufferSize);
+ AudioParameters::AUDIO_PCM_LOW_LATENCY, input_params.channel_layout(),
+ input_params.sample_rate(), 16, buffer_size);
}
AudioOutputStream* AudioManagerAndroid::MakeLinearOutputStream(
@@ -80,35 +83,4 @@ AudioInputStream* AudioManagerAndroid::MakeLowLatencyInputStream(
return new OpenSLESInputStream(this, params);
}
-AudioParameters AudioManagerAndroid::GetPreferredOutputStreamParameters(
- const AudioParameters& input_params) {
- // TODO(xians): figure out the right output sample rate and sample rate to
- // achieve the best audio performance for Android devices.
- static const int kDefaultSampleRate = 16000;
- static const int kDefaultBufferSize = 1024;
-
- ChannelLayout channel_layout = CHANNEL_LAYOUT_STEREO;
- int sample_rate = kDefaultSampleRate;
- int buffer_size = kDefaultBufferSize;
- int bits_per_sample = 16;
- int input_channels = 0;
- if (input_params.IsValid()) {
- // Use the client's input parameters if they are valid.
- sample_rate = input_params.sample_rate();
- bits_per_sample = input_params.bits_per_sample();
- channel_layout = input_params.channel_layout();
- input_channels = input_params.input_channels();
-
- // TODO(leozwang): Android defines the minimal buffer size requirment
- // we should follow it. From Android 4.1, a new audio low latency api
- // set was introduced and is under development, we want to take advantage
- // of it.
- buffer_size = std::min(buffer_size, input_params.frames_per_buffer());
- }
-
- return AudioParameters(
- AudioParameters::AUDIO_PCM_LOW_LATENCY, channel_layout, input_channels,
- sample_rate, bits_per_sample, buffer_size);
-}
-
} // namespace media
diff --git a/media/audio/android/audio_manager_android.h b/media/audio/android/audio_manager_android.h
index fbc05d2..2c8f7ac 100644
--- a/media/audio/android/audio_manager_android.h
+++ b/media/audio/android/audio_manager_android.h
@@ -19,8 +19,6 @@ class MEDIA_EXPORT AudioManagerAndroid : public AudioManagerBase {
virtual bool HasAudioInputDevices() OVERRIDE;
virtual void GetAudioInputDeviceNames(media::AudioDeviceNames* device_names)
OVERRIDE;
- virtual AudioParameters GetInputStreamParameters(
- const std::string& device_id) OVERRIDE;
// Implementation of AudioManagerBase.
virtual AudioOutputStream* MakeLinearOutputStream(
@@ -31,13 +29,12 @@ class MEDIA_EXPORT AudioManagerAndroid : public AudioManagerBase {
const AudioParameters& params, const std::string& device_id) OVERRIDE;
virtual AudioInputStream* MakeLowLatencyInputStream(
const AudioParameters& params, const std::string& device_id) OVERRIDE;
+ virtual AudioParameters GetPreferredLowLatencyOutputStreamParameters(
+ const AudioParameters& input_params) OVERRIDE;
protected:
virtual ~AudioManagerAndroid();
- virtual AudioParameters GetPreferredOutputStreamParameters(
- const AudioParameters& input_params) OVERRIDE;
-
private:
DISALLOW_COPY_AND_ASSIGN(AudioManagerAndroid);
};
diff --git a/media/audio/audio_input_volume_unittest.cc b/media/audio/audio_input_volume_unittest.cc
index 2f3bda9..8f754cc 100644
--- a/media/audio/audio_input_volume_unittest.cc
+++ b/media/audio/audio_input_volume_unittest.cc
@@ -51,10 +51,29 @@ class AudioInputVolumeTest : public ::testing::Test {
}
AudioInputStream* CreateAndOpenStream(const std::string& device_id) {
- const AudioParameters& params =
- audio_manager_->GetInputStreamParameters(device_id);
+ AudioParameters::Format format = AudioParameters::AUDIO_PCM_LOW_LATENCY;
+ ChannelLayout channel_layout =
+ media::GetAudioInputHardwareChannelLayout(device_id);
+ int bits_per_sample = 16;
+ int sample_rate =
+ static_cast<int>(media::GetAudioInputHardwareSampleRate(device_id));
+ int samples_per_packet = 0;
+#if defined(OS_MACOSX)
+ samples_per_packet = (sample_rate / 100);
+#elif defined(OS_LINUX) || defined(OS_OPENBSD)
+ samples_per_packet = (sample_rate / 100);
+#elif defined(OS_WIN)
+ if (sample_rate == 44100)
+ samples_per_packet = 448;
+ else
+ samples_per_packet = (sample_rate / 100);
+#else
+#error Unsupported platform
+#endif
AudioInputStream* ais = audio_manager_->MakeAudioInputStream(
- params, device_id);
+ AudioParameters(format, channel_layout, sample_rate, bits_per_sample,
+ samples_per_packet),
+ device_id);
EXPECT_TRUE(NULL != ais);
#if defined(OS_LINUX) || defined(OS_OPENBSD)
diff --git a/media/audio/audio_low_latency_input_output_unittest.cc b/media/audio/audio_low_latency_input_output_unittest.cc
index a13d3ba..8c05b12 100644
--- a/media/audio/audio_low_latency_input_output_unittest.cc
+++ b/media/audio/audio_low_latency_input_output_unittest.cc
@@ -14,6 +14,7 @@
#include "build/build_config.h"
#include "media/audio/audio_io.h"
#include "media/audio/audio_manager_base.h"
+#include "media/audio/audio_util.h"
#include "media/base/seekable_buffer.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
@@ -285,10 +286,14 @@ class AudioInputStreamTraits {
public:
typedef AudioInputStream StreamType;
- static AudioParameters GetDefaultAudioStreamParameters(
- AudioManager* audio_manager) {
- return audio_manager->GetInputStreamParameters(
- AudioManagerBase::kDefaultDeviceId);
+ static int HardwareSampleRate() {
+ return static_cast<int>(media::GetAudioInputHardwareSampleRate(
+ AudioManagerBase::kDefaultDeviceId));
+ }
+
+ // TODO(henrika): add support for GetAudioInputHardwareBufferSize in media.
+ static int HardwareBufferSize() {
+ return static_cast<int>(media::GetAudioHardwareBufferSize());
}
static StreamType* CreateStream(AudioManager* audio_manager,
@@ -302,9 +307,12 @@ class AudioOutputStreamTraits {
public:
typedef AudioOutputStream StreamType;
- static AudioParameters GetDefaultAudioStreamParameters(
- AudioManager* audio_manager) {
- return audio_manager->GetDefaultOutputStreamParameters();
+ static int HardwareSampleRate() {
+ return static_cast<int>(media::GetAudioHardwareSampleRate());
+ }
+
+ static int HardwareBufferSize() {
+ return static_cast<int>(media::GetAudioHardwareBufferSize());
}
static StreamType* CreateStream(AudioManager* audio_manager,
@@ -331,13 +339,11 @@ class StreamWrapper {
#endif
bits_per_sample_(16) {
// Use the preferred sample rate.
- const AudioParameters& params =
- StreamTraits::GetDefaultAudioStreamParameters(audio_manager_);
- sample_rate_ = params.sample_rate();
+ sample_rate_ = StreamTraits::HardwareSampleRate();
// Use the preferred buffer size. Note that the input side uses the same
// size as the output side in this implementation.
- samples_per_packet_ = params.frames_per_buffer();
+ samples_per_packet_ = StreamTraits::HardwareBufferSize();
}
virtual ~StreamWrapper() {}
diff --git a/media/audio/audio_manager.h b/media/audio/audio_manager.h
index fbc3c7c..f169b41 100644
--- a/media/audio/audio_manager.h
+++ b/media/audio/audio_manager.h
@@ -120,18 +120,6 @@ class MEDIA_EXPORT AudioManager {
virtual void RemoveOutputDeviceChangeListener(
AudioDeviceListener* listener) = 0;
- // Returns the default output hardware audio parameters for opening output
- // streams. It is a convenience interface to
- // AudioManagerBase::GetPreferredOutputStreamParameters and each AudioManager
- // does not need their own implementation to this interface.
- virtual AudioParameters GetDefaultOutputStreamParameters() = 0;
-
- // Returns the input hardware audio parameters of the specific device
- // for opening input streams. Each AudioManager needs to implement their own
- // version of this interface.
- virtual AudioParameters GetInputStreamParameters(
- const std::string& device_id) = 0;
-
protected:
AudioManager();
diff --git a/media/audio/audio_manager_base.cc b/media/audio/audio_manager_base.cc
index 2a65f82..662d39a 100644
--- a/media/audio/audio_manager_base.cc
+++ b/media/audio/audio_manager_base.cc
@@ -182,7 +182,7 @@ AudioOutputStream* AudioManagerBase::MakeAudioOutputStreamProxy(
// as our input parameters.
AudioParameters output_params = params;
if (use_audio_output_resampler) {
- output_params = GetPreferredOutputStreamParameters(params);
+ output_params = GetPreferredLowLatencyOutputStreamParameters(params);
// Ensure we only pass on valid output parameters.
if (!output_params.IsValid()) {
@@ -317,6 +317,22 @@ void AudioManagerBase::ShutdownOnAudioThread() {
#endif // defined(OS_IOS)
}
+AudioParameters AudioManagerBase::GetPreferredLowLatencyOutputStreamParameters(
+ const AudioParameters& input_params) {
+#if defined(OS_IOS)
+ // IOS implements audio input only.
+ NOTIMPLEMENTED();
+ return AudioParameters();
+#else
+ // TODO(dalecurtis): This should include bits per channel and channel layout
+ // eventually.
+ return AudioParameters(
+ AudioParameters::AUDIO_PCM_LOW_LATENCY,
+ input_params.channel_layout(), input_params.input_channels(),
+ GetAudioHardwareSampleRate(), 16, GetAudioHardwareBufferSize());
+#endif // defined(OS_IOS)
+}
+
void AudioManagerBase::AddOutputDeviceChangeListener(
AudioDeviceListener* listener) {
DCHECK(message_loop_->BelongsToCurrentThread());
@@ -335,14 +351,4 @@ void AudioManagerBase::NotifyAllOutputDeviceChangeListeners() {
FOR_EACH_OBSERVER(AudioDeviceListener, output_listeners_, OnDeviceChange());
}
-AudioParameters AudioManagerBase::GetDefaultOutputStreamParameters() {
- return GetPreferredOutputStreamParameters(AudioParameters());
-}
-
-AudioParameters AudioManagerBase::GetInputStreamParameters(
- const std::string& device_id) {
- NOTREACHED();
- return AudioParameters();
-}
-
} // namespace media
diff --git a/media/audio/audio_manager_base.h b/media/audio/audio_manager_base.h
index 2275f9b..22140cb 100644
--- a/media/audio/audio_manager_base.h
+++ b/media/audio/audio_manager_base.h
@@ -83,16 +83,21 @@ class MEDIA_EXPORT AudioManagerBase : public AudioManager {
virtual AudioInputStream* MakeLowLatencyInputStream(
const AudioParameters& params, const std::string& device_id) = 0;
+ // Returns the preferred hardware audio output parameters for opening output
+ // streams in the |AUDIO_PCM_LOW_LATENCY| format.
+ // TODO(dalecurtis): Retrieve the |channel_layout| value from hardware instead
+ // of accepting the value.
+ // TODO(dalecurtis): Each AudioManager should implement their own version, see
+ // http://crbug.com/137326
+ virtual AudioParameters GetPreferredLowLatencyOutputStreamParameters(
+ const AudioParameters& input_params);
+
// Listeners will be notified on the AudioManager::GetMessageLoop() loop.
virtual void AddOutputDeviceChangeListener(
AudioDeviceListener* listener) OVERRIDE;
virtual void RemoveOutputDeviceChangeListener(
AudioDeviceListener* listener) OVERRIDE;
- virtual AudioParameters GetDefaultOutputStreamParameters() OVERRIDE;
- virtual AudioParameters GetInputStreamParameters(
- const std::string& device_id) OVERRIDE;
-
protected:
AudioManagerBase();
@@ -116,14 +121,6 @@ class MEDIA_EXPORT AudioManagerBase : public AudioManager {
// thread.
void NotifyAllOutputDeviceChangeListeners();
- // Returns the preferred hardware audio output parameters for opening output
- // streams. If the users inject a valid |input_params|, each AudioManager
- // will decide if they should return the values from |input_params| or the
- // default hardware values. If the |input_params| is invalid, it will return
- // the default hardware audio parameters.
- virtual AudioParameters GetPreferredOutputStreamParameters(
- const AudioParameters& input_params) = 0;
-
// Map of cached AudioOutputDispatcher instances. Must only be touched
// from the audio thread (no locking).
AudioOutputDispatchersMap output_dispatchers_;
diff --git a/media/audio/audio_output_proxy_unittest.cc b/media/audio/audio_output_proxy_unittest.cc
index 6c4870a..880e996 100644
--- a/media/audio/audio_output_proxy_unittest.cc
+++ b/media/audio/audio_output_proxy_unittest.cc
@@ -117,8 +117,6 @@ class MockAudioManager : public AudioManagerBase {
const AudioParameters& params, const std::string& device_id));
MOCK_METHOD2(MakeLowLatencyInputStream, AudioInputStream*(
const AudioParameters& params, const std::string& device_id));
- MOCK_METHOD1(GetPreferredOutputStreamParameters, AudioParameters(
- const AudioParameters& params));
};
class MockAudioSourceCallback : public AudioOutputStream::AudioSourceCallback {
diff --git a/media/audio/audio_util.cc b/media/audio/audio_util.cc
index f88478f..0c8b96c 100644
--- a/media/audio/audio_util.cc
+++ b/media/audio/audio_util.cc
@@ -108,6 +108,124 @@ bool AdjustVolume(void* buf,
return false;
}
+int GetAudioHardwareSampleRate() {
+#if defined(OS_MACOSX)
+ // Hardware sample-rate on the Mac can be configured, so we must query.
+ return AUAudioOutputStream::HardwareSampleRate();
+#elif defined(OS_WIN)
+ if (!CoreAudioUtil::IsSupported()) {
+ // Fall back to Windows Wave implementation on Windows XP or lower
+ // and use 48kHz as default input sample rate.
+ return 48000;
+ }
+
+ // TODO(crogers): tune this rate for best possible WebAudio performance.
+ // WebRTC works well at 48kHz and a buffer size of 480 samples will be used
+ // for this case. Note that exclusive mode is experimental.
+ const CommandLine* cmd_line = CommandLine::ForCurrentProcess();
+ if (cmd_line->HasSwitch(switches::kEnableExclusiveAudio)) {
+ // This sample rate will be combined with a buffer size of 256 samples
+ // (see GetAudioHardwareBufferSize()), which corresponds to an output
+ // delay of ~5.33ms.
+ return 48000;
+ }
+
+ // Hardware sample-rate on Windows can be configured, so we must query.
+ // TODO(henrika): improve possibility to specify an audio endpoint.
+ // Use the default device (same as for Wave) for now to be compatible.
+ return WASAPIAudioOutputStream::HardwareSampleRate();
+#elif defined(OS_ANDROID)
+ // TODO(leozwang): return native sampling rate on Android.
+ return 16000;
+#else
+ return 48000;
+#endif
+}
+
+int GetAudioInputHardwareSampleRate(const std::string& device_id) {
+ // TODO(henrika): add support for device selection on all platforms.
+ // Only exists on Windows today.
+#if defined(OS_MACOSX)
+ return AUAudioInputStream::HardwareSampleRate();
+#elif defined(OS_WIN)
+ if (!CoreAudioUtil::IsSupported()) {
+ return 48000;
+ }
+ return WASAPIAudioInputStream::HardwareSampleRate(device_id);
+#elif defined(OS_ANDROID)
+ return 16000;
+#else
+ // Hardware for Linux is nearly always 48KHz.
+ // TODO(crogers) : return correct value in rare non-48KHz cases.
+ return 48000;
+#endif
+}
+
+size_t GetAudioHardwareBufferSize() {
+ int user_buffer_size = GetUserBufferSize();
+ if (user_buffer_size)
+ return user_buffer_size;
+
+ // The sizes here were determined by experimentation and are roughly
+ // the lowest value (for low latency) that still allowed glitch-free
+ // audio under high loads.
+ //
+ // For Mac OS X and Windows the chromium audio backend uses a low-latency
+ // Core Audio API, so a low buffer size is possible. For Linux, further
+ // tuning may be needed.
+#if defined(OS_MACOSX)
+ return 128;
+#elif defined(OS_WIN)
+ // TODO(henrika): resolve conflict with GetUserBufferSize().
+ // If the user tries to set a buffer size using GetUserBufferSize() it will
+ // most likely fail since only the native/perfect buffer size is allowed.
+
+ // Buffer size to use when a proper size can't be determined from the system.
+ static const int kFallbackBufferSize = 2048;
+
+ if (!CoreAudioUtil::IsSupported()) {
+ // Fall back to Windows Wave implementation on Windows XP or lower
+ // and assume 48kHz as default sample rate.
+ return kFallbackBufferSize;
+ }
+
+ // TODO(crogers): tune this size to best possible WebAudio performance.
+ // WebRTC always uses 10ms for Windows and does not call this method.
+ // Note that exclusive mode is experimental.
+ const CommandLine* cmd_line = CommandLine::ForCurrentProcess();
+ if (cmd_line->HasSwitch(switches::kEnableExclusiveAudio)) {
+ return 256;
+ }
+
+ AudioParameters params;
+ HRESULT hr = CoreAudioUtil::GetPreferredAudioParameters(eRender, eConsole,
+ &params);
+ return FAILED(hr) ? kFallbackBufferSize : params.frames_per_buffer();
+#elif defined(OS_LINUX)
+ return 512;
+#else
+ return 2048;
+#endif
+}
+
+ChannelLayout GetAudioInputHardwareChannelLayout(const std::string& device_id) {
+ // TODO(henrika): add support for device selection on all platforms.
+ // Only exists on Windows today.
+#if defined(OS_MACOSX)
+ return CHANNEL_LAYOUT_MONO;
+#elif defined(OS_WIN)
+ if (!CoreAudioUtil::IsSupported()) {
+ // Fall back to Windows Wave implementation on Windows XP or lower and
+ // use stereo by default.
+ return CHANNEL_LAYOUT_STEREO;
+ }
+ return WASAPIAudioInputStream::HardwareChannelCount(device_id) == 1 ?
+ CHANNEL_LAYOUT_MONO : CHANNEL_LAYOUT_STEREO;
+#else
+ return CHANNEL_LAYOUT_STEREO;
+#endif
+}
+
// Computes a buffer size based on the given |sample_rate|. Must be used in
// conjunction with AUDIO_PCM_LINEAR.
size_t GetHighLatencyOutputBufferSize(int sample_rate) {
diff --git a/media/audio/audio_util.h b/media/audio/audio_util.h
index 0076b34..6067b21 100644
--- a/media/audio/audio_util.h
+++ b/media/audio/audio_util.h
@@ -39,6 +39,22 @@ MEDIA_EXPORT bool AdjustVolume(void* buf,
int bytes_per_sample,
float volume);
+// Returns the default audio output hardware sample-rate.
+MEDIA_EXPORT int GetAudioHardwareSampleRate();
+
+// Returns the audio input hardware sample-rate for the specified device.
+MEDIA_EXPORT int GetAudioInputHardwareSampleRate(
+ const std::string& device_id);
+
+// Returns the optimal low-latency buffer size for the audio hardware.
+// This is the smallest buffer size the system can comfortably render
+// at without glitches. The buffer size is in sample-frames.
+MEDIA_EXPORT size_t GetAudioHardwareBufferSize();
+
+// Returns the channel layout for the specified audio input device.
+MEDIA_EXPORT ChannelLayout GetAudioInputHardwareChannelLayout(
+ const std::string& device_id);
+
// Computes a buffer size based on the given |sample_rate|. Must be used in
// conjunction with AUDIO_PCM_LINEAR.
MEDIA_EXPORT size_t GetHighLatencyOutputBufferSize(int sample_rate);
diff --git a/media/audio/cras/audio_manager_cras.cc b/media/audio/cras/audio_manager_cras.cc
index 977fba9..3401cc5 100644
--- a/media/audio/cras/audio_manager_cras.cc
+++ b/media/audio/cras/audio_manager_cras.cc
@@ -10,18 +10,15 @@
#include "base/nix/xdg_util.h"
#include "base/process_util.h"
#include "base/stl_util.h"
+#include "media/audio/audio_util.h"
#include "media/audio/cras/cras_input.h"
#include "media/audio/cras/cras_output.h"
-#include "media/base/channel_layout.h"
namespace media {
// Maximum number of output streams that can be open simultaneously.
static const int kMaxOutputStreams = 50;
-// Default sample rate for input and output streams.
-static const int kDefaultSampleRate = 48000;
-
static const char kCrasAutomaticDeviceName[] = "Automatic";
static const char kCrasAutomaticDeviceId[] = "automatic";
@@ -52,15 +49,6 @@ void AudioManagerCras::GetAudioInputDeviceNames(
return;
}
-AudioParameters AudioManagerCras::GetInputStreamParameters(
- const std::string& device_id) {
- static const int kDefaultInputBufferSize = 1024;
-
- return AudioParameters(
- AudioParameters::AUDIO_PCM_LOW_LATENCY, CHANNEL_LAYOUT_STEREO,
- kDefaultSampleRate, 16, kDefaultInputBufferSize);
-}
-
void AudioManagerCras::GetCrasAudioInputDevices(
media::AudioDeviceNames* device_names) {
// Cras will route audio from a proper physical device automatically.
@@ -92,28 +80,6 @@ AudioInputStream* AudioManagerCras::MakeLowLatencyInputStream(
return MakeInputStream(params, device_id);
}
-AudioParameters AudioManagerCras::GetPreferredOutputStreamParameters(
- const AudioParameters& input_params) {
- static const int kDefaultOutputBufferSize = 512;
-
- ChannelLayout channel_layout = CHANNEL_LAYOUT_STEREO;
- int sample_rate = kDefaultSampleRate;
- int buffer_size = kDefaultOutputBufferSize;
- int bits_per_sample = 16;
- int input_channels = 0;
- if (input_params.IsValid()) {
- sample_rate = input_params.sample_rate();
- bits_per_sample = input_params.bits_per_sample();
- channel_layout = input_params.channel_layout();
- input_channels = input_params.input_channels();
- buffer_size = input_params.frames_per_buffer();
- }
-
- return AudioParameters(
- AudioParameters::AUDIO_PCM_LOW_LATENCY, channel_layout,
- sample_rate, bits_per_sample, buffer_size, input_channels);
-}
-
AudioOutputStream* AudioManagerCras::MakeOutputStream(
const AudioParameters& params) {
return new CrasOutputStream(params, this);
@@ -124,4 +90,13 @@ AudioInputStream* AudioManagerCras::MakeInputStream(
return new CrasInputStream(params, this);
}
+AudioParameters AudioManagerCras::GetPreferredLowLatencyOutputStreamParameters(
+ const AudioParameters& input_params) {
+ // TODO(dalecurtis): This should include bits per channel and channel layout
+ // eventually.
+ return AudioParameters(
+ AudioParameters::AUDIO_PCM_LOW_LATENCY, input_params.channel_layout(),
+ input_params.sample_rate(), 16, input_params.frames_per_buffer());
+}
+
} // namespace media
diff --git a/media/audio/cras/audio_manager_cras.h b/media/audio/cras/audio_manager_cras.h
index 2c38c8e..e84f47f 100644
--- a/media/audio/cras/audio_manager_cras.h
+++ b/media/audio/cras/audio_manager_cras.h
@@ -23,8 +23,6 @@ class MEDIA_EXPORT AudioManagerCras : public AudioManagerBase {
virtual void ShowAudioInputSettings() OVERRIDE;
virtual void GetAudioInputDeviceNames(media::AudioDeviceNames* device_names)
OVERRIDE;
- virtual AudioParameters GetInputStreamParameters(
- const std::string& device_id) OVERRIDE;
// AudioManagerBase implementation.
virtual AudioOutputStream* MakeLinearOutputStream(
@@ -35,13 +33,12 @@ class MEDIA_EXPORT AudioManagerCras : public AudioManagerBase {
const AudioParameters& params, const std::string& device_id) OVERRIDE;
virtual AudioInputStream* MakeLowLatencyInputStream(
const AudioParameters& params, const std::string& device_id) OVERRIDE;
+ virtual AudioParameters GetPreferredLowLatencyOutputStreamParameters(
+ const AudioParameters& input_params) OVERRIDE;
protected:
virtual ~AudioManagerCras();
- virtual AudioParameters GetPreferredOutputStreamParameters(
- const AudioParameters& input_params) OVERRIDE;
-
private:
// Gets a list of available cras input devices.
void GetCrasAudioInputDevices(media::AudioDeviceNames* device_names);
diff --git a/media/audio/ios/audio_manager_ios.h b/media/audio/ios/audio_manager_ios.h
index e1fd5de..34a85fc 100644
--- a/media/audio/ios/audio_manager_ios.h
+++ b/media/audio/ios/audio_manager_ios.h
@@ -25,8 +25,6 @@ class MEDIA_EXPORT AudioManagerIOS : public AudioManagerBase {
const AudioParameters& params) OVERRIDE;
virtual AudioInputStream* MakeAudioInputStream(
const AudioParameters& params, const std::string& device_id) OVERRIDE;
- virtual AudioParameters GetInputStreamParameters(
- const std::string& device_id) OVERRIDE;
// Implementation of AudioManagerBase.
virtual AudioOutputStream* MakeLinearOutputStream(
@@ -43,9 +41,6 @@ class MEDIA_EXPORT AudioManagerIOS : public AudioManagerBase {
protected:
virtual ~AudioManagerIOS();
- virtual AudioParameters GetPreferredOutputStreamParameters(
- const AudioParameters& input_params) OVERRIDE;
-
private:
DISALLOW_COPY_AND_ASSIGN(AudioManagerIOS);
};
diff --git a/media/audio/ios/audio_manager_ios.mm b/media/audio/ios/audio_manager_ios.mm
index 00b8d79..8949b1c 100644
--- a/media/audio/ios/audio_manager_ios.mm
+++ b/media/audio/ios/audio_manager_ios.mm
@@ -8,11 +8,9 @@
#import <AVFoundation/AVFoundation.h>
#include "base/sys_info.h"
-#include "media/audio/audio_parameters.h"
#include "media/audio/fake_audio_input_stream.h"
#include "media/audio/ios/audio_session_util_ios.h"
#include "media/audio/mac/audio_input_mac.h"
-#include "media/base/channel_layout.h"
#include "media/base/limits.h"
namespace media {
@@ -49,18 +47,6 @@ bool AudioManagerIOS::HasAudioInputDevices() {
return error == kAudioSessionNoError ? audio_input_is_available : false;
}
-AudioParameters AudioManagerIOS::GetInputStreamParameters(
- const std::string& device_id) {
- // TODO(xians): figure out the right input sample rate and buffer size to
- // achieve the best audio performance for iOS devices.
- // TODO(xians): query the native channel layout for the specific device.
- static const int kDefaultSampleRate = 48000;
- static const int kDefaultBufferSize = 2048;
- return AudioParameters(
- AudioParameters::AUDIO_PCM_LOW_LATENCY, CHANNEL_LAYOUT_STEREO,
- kDefaultSampleRate, 16, kDefaultBufferSize);
-}
-
AudioOutputStream* AudioManagerIOS::MakeAudioOutputStream(
const AudioParameters& params) {
NOTIMPLEMENTED(); // Only input is supported on iOS.
@@ -104,22 +90,6 @@ AudioInputStream* AudioManagerIOS::MakeLowLatencyInputStream(
return MakeAudioInputStream(params, device_id);
}
-
-AudioParameters AudioManagerIOS::GetPreferredOutputStreamParameters(
- const AudioParameters& input_params) {
- // TODO(xians): handle the case when input_params is valid.
- // TODO(xians): figure out the right output sample rate and sample rate to
- // achieve the best audio performance for iOS devices.
- static const int kDefaultSampleRate = 48000;
- static const int kDefaultBufferSize = 2048;
- if (input_params.IsValid()) {
- NOTREACHED();
- }
- return AudioParameters(
- AudioParameters::AUDIO_PCM_LOW_LATENCY, CHANNEL_LAYOUT_STEREO,
- kDefaultSampleRate, 16, kDefaultBufferSize);
-}
-
// Called by the stream when it has been released by calling Close().
void AudioManagerIOS::ReleaseOutputStream(AudioOutputStream* stream) {
NOTIMPLEMENTED(); // Only input is supported on iOS.
diff --git a/media/audio/linux/audio_manager_linux.cc b/media/audio/linux/audio_manager_linux.cc
index d591cb2..fa86125 100644
--- a/media/audio/linux/audio_manager_linux.cc
+++ b/media/audio/linux/audio_manager_linux.cc
@@ -11,7 +11,7 @@
#include "base/process_util.h"
#include "base/stl_util.h"
#include "media/audio/audio_output_dispatcher.h"
-#include "media/audio/audio_parameters.h"
+#include "media/audio/audio_util.h"
#if defined(USE_CRAS)
#include "media/audio/cras/audio_manager_cras.h"
#endif
@@ -21,7 +21,6 @@
#if defined(USE_PULSEAUDIO)
#include "media/audio/pulse/audio_manager_pulse.h"
#endif
-#include "media/base/channel_layout.h"
#include "media/base/limits.h"
#include "media/base/media_switches.h"
@@ -30,9 +29,6 @@ namespace media {
// Maximum number of output streams that can be open simultaneously.
static const int kMaxOutputStreams = 50;
-// Default sample rate for input and output streams.
-static const int kDefaultSampleRate = 48000;
-
// Since "default", "pulse" and "dmix" devices are virtual devices mapped to
// real devices, we remove them from the list to avoiding duplicate counting.
// In addition, note that we support no more than 2 channels for recording,
@@ -98,15 +94,6 @@ void AudioManagerLinux::GetAudioInputDeviceNames(
GetAlsaAudioInputDevices(device_names);
}
-AudioParameters AudioManagerLinux::GetInputStreamParameters(
- const std::string& device_id) {
- static const int kDefaultInputBufferSize = 1024;
-
- return AudioParameters(
- AudioParameters::AUDIO_PCM_LOW_LATENCY, CHANNEL_LAYOUT_STEREO,
- kDefaultSampleRate, 16, kDefaultInputBufferSize);
-}
-
void AudioManagerLinux::GetAlsaAudioInputDevices(
media::AudioDeviceNames* device_names) {
// Constants specified by the ALSA API for device hints.
@@ -264,32 +251,6 @@ AudioInputStream* AudioManagerLinux::MakeLowLatencyInputStream(
return MakeInputStream(params, device_id);
}
-AudioParameters AudioManagerLinux::GetPreferredOutputStreamParameters(
- const AudioParameters& input_params) {
- static const int kDefaultOutputBufferSize = 512;
- ChannelLayout channel_layout = CHANNEL_LAYOUT_STEREO;
- int sample_rate = kDefaultSampleRate;
- int buffer_size = kDefaultOutputBufferSize;
- int bits_per_sample = 16;
- int input_channels = 0;
- if (input_params.IsValid()) {
- // Some clients, such as WebRTC, have a more limited use case and work
- // acceptably with a smaller buffer size. The check below allows clients
- // which want to try a smaller buffer size on Linux to do so.
- // TODO(dalecurtis): This should include bits per channel and channel layout
- // eventually.
- sample_rate = input_params.sample_rate();
- bits_per_sample = input_params.bits_per_sample();
- channel_layout = input_params.channel_layout();
- input_channels = input_params.input_channels();
- buffer_size = std::min(input_params.frames_per_buffer(), buffer_size);
- }
-
- return AudioParameters(
- AudioParameters::AUDIO_PCM_LOW_LATENCY, channel_layout, input_channels,
- sample_rate, bits_per_sample, buffer_size);
-}
-
AudioOutputStream* AudioManagerLinux::MakeOutputStream(
const AudioParameters& params) {
std::string device_name = AlsaPcmOutputStream::kAutoSelectDevice;
@@ -331,4 +292,22 @@ AudioManager* CreateAudioManager() {
return new AudioManagerLinux();
}
+AudioParameters AudioManagerLinux::GetPreferredLowLatencyOutputStreamParameters(
+ const AudioParameters& input_params) {
+ // Since Linux doesn't actually have a low latency path the hardware buffer
+ // size is quite large in order to prevent glitches with general usage. Some
+ // clients, such as WebRTC, have a more limited use case and work acceptably
+ // with a smaller buffer size. The check below allows clients which want to
+ // try a smaller buffer size on Linux to do so.
+ int buffer_size = GetAudioHardwareBufferSize();
+ if (input_params.frames_per_buffer() < buffer_size)
+ buffer_size = input_params.frames_per_buffer();
+
+ // TODO(dalecurtis): This should include bits per channel and channel layout
+ // eventually.
+ return AudioParameters(
+ AudioParameters::AUDIO_PCM_LOW_LATENCY, input_params.channel_layout(),
+ input_params.sample_rate(), 16, buffer_size);
+}
+
} // namespace media
diff --git a/media/audio/linux/audio_manager_linux.h b/media/audio/linux/audio_manager_linux.h
index 4c89773..02da961 100644
--- a/media/audio/linux/audio_manager_linux.h
+++ b/media/audio/linux/audio_manager_linux.h
@@ -27,8 +27,6 @@ class MEDIA_EXPORT AudioManagerLinux : public AudioManagerBase {
virtual void ShowAudioInputSettings() OVERRIDE;
virtual void GetAudioInputDeviceNames(media::AudioDeviceNames* device_names)
OVERRIDE;
- virtual AudioParameters GetInputStreamParameters(
- const std::string& device_id) OVERRIDE;
// Implementation of AudioManagerBase.
virtual AudioOutputStream* MakeLinearOutputStream(
@@ -39,13 +37,12 @@ class MEDIA_EXPORT AudioManagerLinux : public AudioManagerBase {
const AudioParameters& params, const std::string& device_id) OVERRIDE;
virtual AudioInputStream* MakeLowLatencyInputStream(
const AudioParameters& params, const std::string& device_id) OVERRIDE;
+ virtual AudioParameters GetPreferredLowLatencyOutputStreamParameters(
+ const AudioParameters& input_params) OVERRIDE;
protected:
virtual ~AudioManagerLinux();
- virtual AudioParameters GetPreferredOutputStreamParameters(
- const AudioParameters& input_params) OVERRIDE;
-
private:
enum StreamType {
kStreamPlayback = 0,
diff --git a/media/audio/mac/audio_device_listener_mac.cc b/media/audio/mac/audio_device_listener_mac.cc
index 24da08a..65a4854 100644
--- a/media/audio/mac/audio_device_listener_mac.cc
+++ b/media/audio/mac/audio_device_listener_mac.cc
@@ -125,8 +125,8 @@ AudioDeviceListenerMac::AudioDeviceListenerMac(const base::Closure& listener_cb)
// by pausing and resuming the dispatch queue before and after each pumped
// task. This is not ideal and long term we should replace the audio thread
// on OSX with a dispatch queue. See http://crbug.com/158170 for discussion.
- // TODO(dalecurtis): Does not fix the cases where
- // GetDefaultOutputStreamParameters() are called by the browser process.
+ // TODO(dalecurtis): Does not fix the cases where GetAudioHardwareSampleRate()
+ // and GetAudioInputHardwareSampleRate() are called by the browser process.
// These are one time events due to renderer side cache and thus unlikely to
// occur at the same time as a device callback. Should be fixed along with
// http://crbug.com/137326 using a forced PostTask.
diff --git a/media/audio/mac/audio_low_latency_input_mac.cc b/media/audio/mac/audio_low_latency_input_mac.cc
index 972ae39..85eef1f 100644
--- a/media/audio/mac/audio_low_latency_input_mac.cc
+++ b/media/audio/mac/audio_low_latency_input_mac.cc
@@ -67,10 +67,7 @@ AUAudioInputStream::AUAudioInputStream(
// Note that we use the same native buffer size as for the output side here
// since the AUHAL implementation requires that both capture and render side
// use the same buffer size. See http://crbug.com/154352 for more details.
- // TODO(xians): Get the audio parameters from the right device.
- const AudioParameters parameters =
- manager_->GetInputStreamParameters(AudioManagerBase::kDefaultDeviceId);
- number_of_frames_ = parameters.frames_per_buffer();
+ number_of_frames_ = GetAudioHardwareBufferSize();
DVLOG(1) << "Size of data buffer in frames : " << number_of_frames_;
// Derive size (in bytes) of the buffers that we will render to.
@@ -228,8 +225,7 @@ bool AUAudioInputStream::Open() {
// Set the desired number of frames in the IO buffer (output scope).
// WARNING: Setting this value changes the frame size for all audio units in
// the current process. It's imperative that the input and output frame sizes
- // be the same as the frames_per_buffer() returned by
- // GetInputStreamParameters().
+ // be the same as audio_util::GetAudioHardwareBufferSize().
// TODO(henrika): Due to http://crrev.com/159666 this is currently not true
// and should be fixed, a CHECK() should be added at that time.
result = AudioUnitSetProperty(audio_unit_,
diff --git a/media/audio/mac/audio_low_latency_output_mac.cc b/media/audio/mac/audio_low_latency_output_mac.cc
index 4b82bef..6ab0ead 100644
--- a/media/audio/mac/audio_low_latency_output_mac.cc
+++ b/media/audio/mac/audio_low_latency_output_mac.cc
@@ -81,10 +81,7 @@ AUAudioOutputStream::AUAudioOutputStream(
// Calculate the number of sample frames per callback.
number_of_frames_ = params.GetBytesPerBuffer() / format_.mBytesPerPacket;
DVLOG(1) << "Number of frames per callback: " << number_of_frames_;
- const AudioParameters parameters =
- manager_->GetDefaultOutputStreamParameters();
- CHECK_EQ(number_of_frames_,
- static_cast<size_t>(parameters.frames_per_buffer()));
+ CHECK_EQ(number_of_frames_, GetAudioHardwareBufferSize());
}
AUAudioOutputStream::~AUAudioOutputStream() {
@@ -170,8 +167,7 @@ bool AUAudioOutputStream::Configure() {
// Set the buffer frame size.
// WARNING: Setting this value changes the frame size for all audio units in
// the current process. It's imperative that the input and output frame sizes
- // be the same as the frames_per_buffer() returned by
- // GetDefaultOutputStreamParameters.
+ // be the same as audio_util::GetAudioHardwareBufferSize().
// See http://crbug.com/154352 for details.
UInt32 buffer_size = number_of_frames_;
result = AudioUnitSetProperty(
diff --git a/media/audio/mac/audio_manager_mac.cc b/media/audio/mac/audio_manager_mac.cc
index 5a82ce0..4030a23 100644
--- a/media/audio/mac/audio_manager_mac.cc
+++ b/media/audio/mac/audio_manager_mac.cc
@@ -12,7 +12,7 @@
#include "base/mac/mac_logging.h"
#include "base/mac/scoped_cftyperef.h"
#include "base/sys_string_conversions.h"
-#include "media/audio/audio_parameters.h"
+#include "media/audio/audio_util.h"
#include "media/audio/mac/audio_input_mac.h"
#include "media/audio/mac/audio_low_latency_input_mac.h"
#include "media/audio/mac/audio_low_latency_output_mac.h"
@@ -20,7 +20,6 @@
#include "media/audio/mac/audio_synchronized_mac.h"
#include "media/audio/mac/audio_unified_mac.h"
#include "media/base/bind_to_loop.h"
-#include "media/base/channel_layout.h"
#include "media/base/limits.h"
#include "media/base/media_switches.h"
@@ -29,9 +28,6 @@ namespace media {
// Maximum number of output streams that can be open simultaneously.
static const int kMaxOutputStreams = 50;
-// Default buffer size in samples for low-latency input and output streams.
-static const int kDefaultLowLatencyBufferSize = 128;
-
static bool HasAudioHardware(AudioObjectPropertySelector selector) {
AudioDeviceID output_device_id = kAudioObjectUnknown;
const AudioObjectPropertyAddress property_address = {
@@ -277,15 +273,6 @@ void AudioManagerMac::GetAudioInputDeviceNames(
}
}
-AudioParameters AudioManagerMac::GetInputStreamParameters(
- const std::string& device_id) {
- // TODO(xians): query the native channel layout for the specific device.
- return AudioParameters(
- AudioParameters::AUDIO_PCM_LOW_LATENCY, CHANNEL_LAYOUT_STEREO,
- AUAudioInputStream::HardwareSampleRate(), 16,
- kDefaultLowLatencyBufferSize);
-}
-
AudioOutputStream* AudioManagerMac::MakeLinearOutputStream(
const AudioParameters& params) {
DCHECK_EQ(AudioParameters::AUDIO_PCM_LINEAR, params.format());
@@ -334,28 +321,22 @@ AudioInputStream* AudioManagerMac::MakeLowLatencyInputStream(
return stream;
}
-AudioParameters AudioManagerMac::GetPreferredOutputStreamParameters(
+AudioParameters AudioManagerMac::GetPreferredLowLatencyOutputStreamParameters(
const AudioParameters& input_params) {
- ChannelLayout channel_layout = CHANNEL_LAYOUT_STEREO;
- int input_channels = 0;
- if (input_params.IsValid()) {
- channel_layout = input_params.channel_layout();
- input_channels = input_params.input_channels();
-
- if (CommandLine::ForCurrentProcess()->HasSwitch(
- switches::kEnableWebAudioInput)) {
- // TODO(crogers): given the limitations of the AudioOutputStream
- // back-ends used with kEnableWebAudioInput, we hard-code to stereo.
- // Specifically, this is a limitation of AudioSynchronizedStream which
- // can be removed as part of the work to consolidate these back-ends.
- channel_layout = CHANNEL_LAYOUT_STEREO;
- }
+ if (CommandLine::ForCurrentProcess()->HasSwitch(
+ switches::kEnableWebAudioInput)) {
+ // TODO(crogers): given the limitations of the AudioOutputStream
+ // back-ends used with kEnableWebAudioInput, we hard-code to stereo.
+ // Specifically, this is a limitation of AudioSynchronizedStream which
+ // can be removed as part of the work to consolidate these back-ends.
+ return AudioParameters(
+ AudioParameters::AUDIO_PCM_LOW_LATENCY,
+ CHANNEL_LAYOUT_STEREO, input_params.input_channels(),
+ GetAudioHardwareSampleRate(), 16, GetAudioHardwareBufferSize());
}
- return AudioParameters(
- AudioParameters::AUDIO_PCM_LOW_LATENCY, channel_layout, input_channels,
- AUAudioOutputStream::HardwareSampleRate(), 16,
- kDefaultLowLatencyBufferSize);
+ return AudioManagerBase::GetPreferredLowLatencyOutputStreamParameters(
+ input_params);
}
void AudioManagerMac::CreateDeviceListener() {
diff --git a/media/audio/mac/audio_manager_mac.h b/media/audio/mac/audio_manager_mac.h
index 5eba595..cdfa241 100644
--- a/media/audio/mac/audio_manager_mac.h
+++ b/media/audio/mac/audio_manager_mac.h
@@ -25,8 +25,6 @@ class MEDIA_EXPORT AudioManagerMac : public AudioManagerBase {
virtual bool HasAudioInputDevices() OVERRIDE;
virtual void GetAudioInputDeviceNames(media::AudioDeviceNames* device_names)
OVERRIDE;
- virtual AudioParameters GetInputStreamParameters(
- const std::string& device_id) OVERRIDE;
// Implementation of AudioManagerBase.
virtual AudioOutputStream* MakeLinearOutputStream(
@@ -37,13 +35,12 @@ class MEDIA_EXPORT AudioManagerMac : public AudioManagerBase {
const AudioParameters& params, const std::string& device_id) OVERRIDE;
virtual AudioInputStream* MakeLowLatencyInputStream(
const AudioParameters& params, const std::string& device_id) OVERRIDE;
+ virtual AudioParameters GetPreferredLowLatencyOutputStreamParameters(
+ const AudioParameters& input_params) OVERRIDE;
protected:
virtual ~AudioManagerMac();
- virtual AudioParameters GetPreferredOutputStreamParameters(
- const AudioParameters& input_params) OVERRIDE;
-
private:
// Helper methods for constructing AudioDeviceListenerMac on the audio thread.
void CreateDeviceListener();
diff --git a/media/audio/mock_audio_manager.cc b/media/audio/mock_audio_manager.cc
index 1209e19..4cd5f21 100644
--- a/media/audio/mock_audio_manager.cc
+++ b/media/audio/mock_audio_manager.cc
@@ -6,7 +6,6 @@
#include "base/logging.h"
#include "base/message_loop_proxy.h"
-#include "media/audio/audio_parameters.h"
namespace media {
@@ -71,13 +70,4 @@ void MockAudioManager::RemoveOutputDeviceChangeListener(
AudioDeviceListener* listener) {
}
-AudioParameters MockAudioManager::GetDefaultOutputStreamParameters() {
- return AudioParameters();
-}
-
-AudioParameters MockAudioManager::GetInputStreamParameters(
- const std::string& device_id) {
- return AudioParameters();
-}
-
} // namespace media.
diff --git a/media/audio/mock_audio_manager.h b/media/audio/mock_audio_manager.h
index 3a0e907..1046c8c 100644
--- a/media/audio/mock_audio_manager.h
+++ b/media/audio/mock_audio_manager.h
@@ -53,10 +53,6 @@ class MockAudioManager : public media::AudioManager {
virtual void RemoveOutputDeviceChangeListener(
AudioDeviceListener* listener) OVERRIDE;
- virtual AudioParameters GetDefaultOutputStreamParameters() OVERRIDE;
- virtual AudioParameters GetInputStreamParameters(
- const std::string& device_id) OVERRIDE;
-
private:
virtual ~MockAudioManager();
diff --git a/media/audio/openbsd/audio_manager_openbsd.cc b/media/audio/openbsd/audio_manager_openbsd.cc
index 67fba91..84304a5 100644
--- a/media/audio/openbsd/audio_manager_openbsd.cc
+++ b/media/audio/openbsd/audio_manager_openbsd.cc
@@ -7,11 +7,9 @@
#include "base/command_line.h"
#include "base/stl_util.h"
#include "media/audio/audio_output_dispatcher.h"
-#include "media/audio/audio_parameters.h"
#if defined(USE_PULSEAUDIO)
#include "media/audio/pulse/pulse_output.h"
#endif
-#include "media/base/channel_layout.h"
#include "media/base/limits.h"
#include "media/base/media_switches.h"
@@ -22,9 +20,6 @@ namespace media {
// Maximum number of output streams that can be open simultaneously.
static const int kMaxOutputStreams = 50;
-// Default sample rate for input and output streams.
-static const int kDefaultSampleRate = 48000;
-
// Implementation of AudioManager.
static bool HasAudioHardware() {
int fd;
@@ -48,15 +43,6 @@ bool AudioManagerOpenBSD::HasAudioInputDevices() {
return HasAudioHardware();
}
-AudioParameters AudioManagerOpenBSD::GetInputStreamParameters(
- const std::string& device_id) {
- static const int kDefaultInputBufferSize = 1024;
-
- return AudioParameters(
- AudioParameters::AUDIO_PCM_LOW_LATENCY, CHANNEL_LAYOUT_STEREO,
- kDefaultSampleRate, 16, kDefaultInputBufferSize);
-}
-
AudioManagerOpenBSD::AudioManagerOpenBSD() {
SetMaxOutputStreamsAllowed(kMaxOutputStreams);
}
@@ -91,29 +77,6 @@ AudioInputStream* AudioManagerOpenBSD::MakeLowLatencyInputStream(
return NULL;
}
-AudioParameters AudioManagerOpenBSD::GetPreferredOutputStreamParameters(
- const AudioParameters& input_params) {
- static const int kDefaultOutputBufferSize = 512;
-
- ChannelLayout channel_layout = CHANNEL_LAYOUT_STEREO;
- int sample_rate = kDefaultSampleRate;
- int buffer_size = kDefaultOutputBufferSize;
- int bits_per_sample = 16;
- int input_channels = 0;
- if (input_params.IsValid()) {
- sample_rate = input_params.sample_rate();
- bits_per_sample = input_params.bits_per_sample();
- channel_layout = input_params.channel_layout();
- input_channels = input_params.input_channels();
- buffer_size = std::min(buffer_size, input_params.frames_per_buffer());
- }
-
- return AudioParameters(
- AudioParameters::AUDIO_PCM_LOW_LATENCY, channel_layout, input_channels,
- sample_rate, bits_per_sample, buffer_size);
-}
-
-
AudioOutputStream* AudioManagerOpenBSD::MakeOutputStream(
const AudioParameters& params) {
#if defined(USE_PULSEAUDIO)
@@ -126,7 +89,6 @@ AudioOutputStream* AudioManagerOpenBSD::MakeOutputStream(
return NULL;
}
-// TODO(xians): Merge AudioManagerOpenBSD with AudioManagerPulse;
// static
AudioManager* CreateAudioManager() {
return new AudioManagerOpenBSD();
diff --git a/media/audio/openbsd/audio_manager_openbsd.h b/media/audio/openbsd/audio_manager_openbsd.h
index 14c5636..aeba43e 100644
--- a/media/audio/openbsd/audio_manager_openbsd.h
+++ b/media/audio/openbsd/audio_manager_openbsd.h
@@ -19,8 +19,6 @@ class MEDIA_EXPORT AudioManagerOpenBSD : public AudioManagerBase {
// Implementation of AudioManager.
virtual bool HasAudioOutputDevices() OVERRIDE;
virtual bool HasAudioInputDevices() OVERRIDE;
- virtual AudioParameters GetInputStreamParameters(
- const std::string& device_id) OVERRIDE;
// Implementation of AudioManagerBase.
virtual AudioOutputStream* MakeLinearOutputStream(
@@ -35,9 +33,6 @@ class MEDIA_EXPORT AudioManagerOpenBSD : public AudioManagerBase {
protected:
virtual ~AudioManagerOpenBSD();
- virtual AudioParameters GetPreferredOutputStreamParameters(
- const AudioParameters& input_params) OVERRIDE;
-
private:
// Called by MakeLinearOutputStream and MakeLowLatencyOutputStream.
AudioOutputStream* MakeOutputStream(const AudioParameters& params);
diff --git a/media/audio/pulse/audio_manager_pulse.cc b/media/audio/pulse/audio_manager_pulse.cc
index 9787c9e..e7513705 100644
--- a/media/audio/pulse/audio_manager_pulse.cc
+++ b/media/audio/pulse/audio_manager_pulse.cc
@@ -11,13 +11,12 @@
#include "base/nix/xdg_util.h"
#include "base/process_util.h"
#include "base/stl_util.h"
-#include "media/audio/audio_parameters.h"
+#include "media/audio/audio_util.h"
#include "media/audio/linux/audio_manager_linux.h"
#include "media/audio/pulse/pulse_input.h"
#include "media/audio/pulse/pulse_output.h"
#include "media/audio/pulse/pulse_stubs.h"
#include "media/audio/pulse/pulse_util.h"
-#include "media/base/channel_layout.h"
using media_audio_pulse::kModulePulse;
using media_audio_pulse::InitializeStubs;
@@ -94,16 +93,6 @@ void AudioManagerPulse::GetAudioInputDeviceNames(
}
}
-AudioParameters AudioManagerPulse::GetInputStreamParameters(
- const std::string& device_id) {
- static const int kDefaultInputBufferSize = 1024;
-
- // TODO(xians): add support for querying native channel layout for pulse.
- return AudioParameters(
- AudioParameters::AUDIO_PCM_LOW_LATENCY, CHANNEL_LAYOUT_STEREO,
- GetNativeSampleRate(), 16, kDefaultInputBufferSize);
-}
-
AudioOutputStream* AudioManagerPulse::MakeLinearOutputStream(
const AudioParameters& params) {
DCHECK_EQ(AudioParameters::AUDIO_PCM_LINEAR, params.format());
@@ -128,25 +117,6 @@ AudioInputStream* AudioManagerPulse::MakeLowLatencyInputStream(
return MakeInputStream(params, device_id);
}
-AudioParameters AudioManagerPulse::GetPreferredOutputStreamParameters(
- const AudioParameters& input_params) {
- static const int kDefaultOutputBufferSize = 512;
-
- ChannelLayout channel_layout = CHANNEL_LAYOUT_STEREO;
- int buffer_size = kDefaultOutputBufferSize;
- int bits_per_sample = 16;
- int input_channels = 0;
- if (input_params.IsValid()) {
- bits_per_sample = input_params.bits_per_sample();
- channel_layout = input_params.channel_layout();
- input_channels = input_params.input_channels();
- buffer_size = std::min(buffer_size, input_params.frames_per_buffer());
- }
- return AudioParameters(
- AudioParameters::AUDIO_PCM_LOW_LATENCY, channel_layout, input_channels,
- GetNativeSampleRate(), bits_per_sample, buffer_size);
-}
-
AudioOutputStream* AudioManagerPulse::MakeOutputStream(
const AudioParameters& params) {
return new PulseAudioOutputStream(params, this);
@@ -158,6 +128,20 @@ AudioInputStream* AudioManagerPulse::MakeInputStream(
input_mainloop_, input_context_);
}
+AudioParameters AudioManagerPulse::GetPreferredLowLatencyOutputStreamParameters(
+ const AudioParameters& input_params) {
+ // TODO(xians): figure out the optimized buffer size for the Pulse IO.
+ int buffer_size = GetAudioHardwareBufferSize();
+ if (input_params.frames_per_buffer() < buffer_size)
+ buffer_size = input_params.frames_per_buffer();
+
+ // TODO(dalecurtis): This should include bits per channel and channel layout
+ // eventually.
+ return AudioParameters(
+ AudioParameters::AUDIO_PCM_LOW_LATENCY, input_params.channel_layout(),
+ input_params.sample_rate(), 16, buffer_size);
+}
+
int AudioManagerPulse::GetNativeSampleRate() {
DCHECK(input_mainloop_);
DCHECK(input_context_);
diff --git a/media/audio/pulse/audio_manager_pulse.h b/media/audio/pulse/audio_manager_pulse.h
index 9dacb9c..cff2903 100644
--- a/media/audio/pulse/audio_manager_pulse.h
+++ b/media/audio/pulse/audio_manager_pulse.h
@@ -27,8 +27,6 @@ class MEDIA_EXPORT AudioManagerPulse : public AudioManagerBase {
virtual void ShowAudioInputSettings() OVERRIDE;
virtual void GetAudioInputDeviceNames(media::AudioDeviceNames* device_names)
OVERRIDE;
- virtual AudioParameters GetInputStreamParameters(
- const std::string& device_id) OVERRIDE;
// Implementation of AudioManagerBase.
virtual AudioOutputStream* MakeLinearOutputStream(
@@ -39,11 +37,11 @@ class MEDIA_EXPORT AudioManagerPulse : public AudioManagerBase {
const AudioParameters& params, const std::string& device_id) OVERRIDE;
virtual AudioInputStream* MakeLowLatencyInputStream(
const AudioParameters& params, const std::string& device_id) OVERRIDE;
-
- protected:
- virtual AudioParameters GetPreferredOutputStreamParameters(
+ virtual AudioParameters GetPreferredLowLatencyOutputStreamParameters(
const AudioParameters& input_params) OVERRIDE;
+ int GetNativeSampleRate();
+
private:
bool Init();
void DestroyPulse();
@@ -66,9 +64,6 @@ class MEDIA_EXPORT AudioManagerPulse : public AudioManagerBase {
AudioInputStream* MakeInputStream(const AudioParameters& params,
const std::string& device_id);
- // Gets the native sample rate of Pulse.
- int GetNativeSampleRate();
-
pa_threaded_mainloop* input_mainloop_;
pa_context* input_context_;
AudioDeviceNames* devices_;
diff --git a/media/audio/win/audio_manager_win.cc b/media/audio/win/audio_manager_win.cc
index 7bfc638..bd809b8 100644
--- a/media/audio/win/audio_manager_win.cc
+++ b/media/audio/win/audio_manager_win.cc
@@ -20,7 +20,6 @@
#include "base/process_util.h"
#include "base/string_number_conversions.h"
#include "base/string_util.h"
-#include "media/audio/audio_parameters.h"
#include "media/audio/audio_util.h"
#include "media/audio/win/audio_device_listener_win.h"
#include "media/audio/win/audio_low_latency_input_win.h"
@@ -32,7 +31,6 @@
#include "media/audio/win/wavein_input_win.h"
#include "media/audio/win/waveout_output_win.h"
#include "media/base/bind_to_loop.h"
-#include "media/base/channel_layout.h"
#include "media/base/limits.h"
#include "media/base/media_switches.h"
@@ -63,10 +61,6 @@ static const int kWinMaxChannels = 8;
// play.
static const int kNumInputBuffers = 3;
-// Buffer size to use for input and output stream when a proper size can't be
-// determined from the system
-static const int kFallbackBufferSize = 2048;
-
static int GetVersionPartAsInt(DWORDLONG num) {
return static_cast<int>(num & 0xffff);
}
@@ -261,26 +255,6 @@ void AudioManagerWin::GetAudioInputDeviceNames(
}
}
-AudioParameters AudioManagerWin::GetInputStreamParameters(
- const std::string& device_id) {
- int sample_rate = 0;
- ChannelLayout channel_layout = CHANNEL_LAYOUT_NONE;
- if (!CoreAudioUtil::IsSupported()) {
- sample_rate = 48000;
- channel_layout = CHANNEL_LAYOUT_STEREO;
- } else {
- sample_rate = WASAPIAudioInputStream::HardwareSampleRate(device_id);
- channel_layout =
- WASAPIAudioInputStream::HardwareChannelCount(device_id) == 1 ?
- CHANNEL_LAYOUT_MONO : CHANNEL_LAYOUT_STEREO;
- }
-
- // TODO(Henrika): improve the default buffer size value for input stream.
- return AudioParameters(
- AudioParameters::AUDIO_PCM_LOW_LATENCY, channel_layout,
- sample_rate, 16, kFallbackBufferSize);
-}
-
// Factory for the implementations of AudioOutputStream for AUDIO_PCM_LINEAR
// mode.
// - PCMWaveOutAudioOutputStream: Based on the waveOut API.
@@ -355,60 +329,6 @@ AudioInputStream* AudioManagerWin::MakeLowLatencyInputStream(
return stream;
}
-AudioParameters AudioManagerWin::GetPreferredOutputStreamParameters(
- const AudioParameters& input_params) {
- const CommandLine* cmd_line = CommandLine::ForCurrentProcess();
- ChannelLayout channel_layout = CHANNEL_LAYOUT_STEREO;
- int sample_rate = 0;
- int buffer_size = 0;
- int bits_per_sample = 16;
- int input_channels = 0;
- if (!CoreAudioUtil::IsSupported()) {
- // Fall back to Windows Wave implementation on Windows XP or lower.
- // Use 48kHz as default input sample rate, kFallbackBufferSize as
- // default buffer size.
- sample_rate = 48000;
- buffer_size = kFallbackBufferSize;
- } else if (cmd_line->HasSwitch(switches::kEnableExclusiveAudio)) {
- // TODO(crogers): tune these values for best possible WebAudio performance.
- // WebRTC works well at 48kHz and a buffer size of 480 samples will be used
- // for this case. Note that exclusive mode is experimental.
- // This sample rate will be combined with a buffer size of 256 samples,
- // which corresponds to an output delay of ~5.33ms.
- sample_rate = 48000;
- buffer_size = 256;
- } else {
- // Hardware sample-rate on Windows can be configured, so we must query.
- // TODO(henrika): improve possibility to specify an audio endpoint.
- // Use the default device (same as for Wave) for now to be compatible.
- sample_rate = WASAPIAudioOutputStream::HardwareSampleRate();
-
- AudioParameters params;
- HRESULT hr = CoreAudioUtil::GetPreferredAudioParameters(eRender, eConsole,
- &params);
- buffer_size = FAILED(hr) ? kFallbackBufferSize : params.frames_per_buffer();
- channel_layout = WASAPIAudioOutputStream::HardwareChannelLayout();
- }
-
- if (input_params.IsValid()) {
- input_channels = input_params.input_channels();
- if (!CoreAudioUtil::IsSupported()) {
- // If WASAPI isn't supported we'll fallback to WaveOut, which will take
- // care of resampling and bits per sample changes. By setting these
- // equal to the input values, AudioOutputResampler will skip resampling
- // and bit per sample differences (since the input parameters will match
- // the output parameters).
- sample_rate = input_params.sample_rate();
- bits_per_sample = input_params.bits_per_sample();
- channel_layout = input_params.channel_layout();
- }
- }
-
- return AudioParameters(
- AudioParameters::AUDIO_PCM_LOW_LATENCY, channel_layout, input_channels,
- sample_rate, bits_per_sample, buffer_size);
-}
-
AudioInputStream* AudioManagerWin::CreatePCMWaveInAudioInputStream(
const AudioParameters& params,
const std::string& device_id) {
@@ -432,4 +352,26 @@ AudioManager* CreateAudioManager() {
return new AudioManagerWin();
}
+AudioParameters AudioManagerWin::GetPreferredLowLatencyOutputStreamParameters(
+ const AudioParameters& input_params) {
+ // If WASAPI isn't supported we'll fallback to WaveOut, which will take care
+ // of resampling and bits per sample changes. By setting these equal to the
+ // input values, AudioOutputResampler will skip resampling and bit per sample
+ // differences (since the input parameters will match the output parameters).
+ int sample_rate = input_params.sample_rate();
+ int bits_per_sample = input_params.bits_per_sample();
+ ChannelLayout channel_layout = input_params.channel_layout();
+ int input_channels = input_params.input_channels();
+ if (CoreAudioUtil::IsSupported()) {
+ sample_rate = GetAudioHardwareSampleRate();
+ bits_per_sample = 16;
+ channel_layout = WASAPIAudioOutputStream::HardwareChannelLayout();
+ }
+
+ // TODO(dalecurtis): This should include hardware bits per channel eventually.
+ return AudioParameters(
+ AudioParameters::AUDIO_PCM_LOW_LATENCY, channel_layout, input_channels,
+ sample_rate, bits_per_sample, GetAudioHardwareBufferSize());
+}
+
} // namespace media
diff --git a/media/audio/win/audio_manager_win.h b/media/audio/win/audio_manager_win.h
index 46d7aa4..bb8f395 100644
--- a/media/audio/win/audio_manager_win.h
+++ b/media/audio/win/audio_manager_win.h
@@ -27,8 +27,6 @@ class MEDIA_EXPORT AudioManagerWin : public AudioManagerBase {
virtual void ShowAudioInputSettings() OVERRIDE;
virtual void GetAudioInputDeviceNames(media::AudioDeviceNames* device_names)
OVERRIDE;
- virtual AudioParameters GetInputStreamParameters(
- const std::string& device_id) OVERRIDE;
// Implementation of AudioManagerBase.
virtual AudioOutputStream* MakeLinearOutputStream(
@@ -39,13 +37,12 @@ class MEDIA_EXPORT AudioManagerWin : public AudioManagerBase {
const AudioParameters& params, const std::string& device_id) OVERRIDE;
virtual AudioInputStream* MakeLowLatencyInputStream(
const AudioParameters& params, const std::string& device_id) OVERRIDE;
+ virtual AudioParameters GetPreferredLowLatencyOutputStreamParameters(
+ const AudioParameters& input_params) OVERRIDE;
protected:
virtual ~AudioManagerWin();
- virtual AudioParameters GetPreferredOutputStreamParameters(
- const AudioParameters& input_params) OVERRIDE;
-
private:
enum EnumerationType {
kUninitializedEnumeration = 0,
diff --git a/media/audio/win/audio_output_win_unittest.cc b/media/audio/win/audio_output_win_unittest.cc
index 198ee99..40b4d81 100644
--- a/media/audio/win/audio_output_win_unittest.cc
+++ b/media/audio/win/audio_output_win_unittest.cc
@@ -461,8 +461,7 @@ TEST(WinAudioTest, PCMWaveStreamPlay200HzToneLowLatency) {
// Use 10 ms buffer size for WASAPI and 50 ms buffer size for Wave.
// Take the existing native sample rate into account.
- const AudioParameters params = audio_man->GetDefaultOutputStreamParameters();
- int sample_rate = params.sample_rate();
+ int sample_rate = static_cast<int>(media::GetAudioHardwareSampleRate());
uint32 samples_10_ms = sample_rate / 100;
int n = 1;
(base::win::GetVersion() <= base::win::VERSION_XP) ? n = 5 : n = 1;