| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "content/renderer/media/webrtc_audio_renderer.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #include "base/metrics/histogram.h" |
| 9 #include "base/string_util.h" |
| 10 #include "content/renderer/media/audio_device_factory.h" |
| 11 #include "content/renderer/media/audio_hardware.h" |
| 12 #include "content/renderer/media/webrtc_audio_device_impl.h" |
| 13 #include "media/audio/audio_util.h" |
| 14 #include "media/audio/sample_rates.h" |
| 15 |
| 16 namespace content { |
| 17 |
| 18 namespace { |
| 19 |
| 20 // Supported hardware sample rates for output sides. |
| 21 #if defined(OS_WIN) || defined(OS_MACOSX) |
| 22 // media::GetAudioOutputHardwareSampleRate() asks the audio layer |
| 23 // for its current sample rate (set by the user) on Windows and Mac OS X. |
| 24 // The listed rates below adds restrictions and Initialize() |
| 25 // will fail if the user selects any rate outside these ranges. |
| 26 int kValidOutputRates[] = {96000, 48000, 44100}; |
| 27 #elif defined(OS_LINUX) || defined(OS_OPENBSD) |
| 28 int kValidOutputRates[] = {48000, 44100}; |
| 29 #endif |
| 30 |
| 31 // TODO(xians): Merge the following code to WebRtcAudioCapturer, or remove. |
| 32 enum AudioFramesPerBuffer { |
| 33 k160, |
| 34 k320, |
| 35 k440, // WebRTC works internally with 440 audio frames at 44.1kHz. |
| 36 k480, |
| 37 k640, |
| 38 k880, |
| 39 k960, |
| 40 k1440, |
| 41 k1920, |
| 42 kUnexpectedAudioBufferSize // Must always be last! |
| 43 }; |
| 44 |
| 45 // Helper method to convert integral values to their respective enum values |
| 46 // above, or kUnexpectedAudioBufferSize if no match exists. |
| 47 AudioFramesPerBuffer AsAudioFramesPerBuffer(int frames_per_buffer) { |
| 48 switch (frames_per_buffer) { |
| 49 case 160: return k160; |
| 50 case 320: return k320; |
| 51 case 440: return k440; |
| 52 case 480: return k480; |
| 53 case 640: return k640; |
| 54 case 880: return k880; |
| 55 case 960: return k960; |
| 56 case 1440: return k1440; |
| 57 case 1920: return k1920; |
| 58 } |
| 59 return kUnexpectedAudioBufferSize; |
| 60 } |
| 61 |
| 62 void AddHistogramFramesPerBuffer(int param) { |
| 63 AudioFramesPerBuffer afpb = AsAudioFramesPerBuffer(param); |
| 64 if (afpb != kUnexpectedAudioBufferSize) { |
| 65 UMA_HISTOGRAM_ENUMERATION("WebRTC.AudioOutputFramesPerBuffer", |
| 66 afpb, kUnexpectedAudioBufferSize); |
| 67 } else { |
| 68 // Report unexpected sample rates using a unique histogram name. |
| 69 UMA_HISTOGRAM_COUNTS("WebRTC.AudioOutputFramesPerBufferUnexpected", param); |
| 70 } |
| 71 } |
| 72 |
| 73 } // namespace |
| 74 |
| 75 WebRtcAudioRenderer::WebRtcAudioRenderer() |
| 76 : state_(UNINITIALIZED), |
| 77 source_(NULL) { |
| 78 } |
| 79 |
| 80 WebRtcAudioRenderer::~WebRtcAudioRenderer() { |
| 81 DCHECK_EQ(state_, UNINITIALIZED); |
| 82 buffer_.reset(); |
| 83 } |
| 84 |
| 85 bool WebRtcAudioRenderer::Initialize(WebRtcAudioRendererSource* source) { |
| 86 base::AutoLock auto_lock(lock_); |
| 87 DCHECK_EQ(state_, UNINITIALIZED); |
| 88 DCHECK(source); |
| 89 DCHECK(!sink_); |
| 90 DCHECK(!source_); |
| 91 |
| 92 sink_ = AudioDeviceFactory::NewOutputDevice(); |
| 93 DCHECK(sink_); |
| 94 |
| 95 // Ask the browser for the default audio output hardware sample-rate. |
| 96 // This request is based on a synchronous IPC message. |
| 97 int sample_rate = GetAudioOutputSampleRate(); |
| 98 DVLOG(1) << "Audio output hardware sample rate: " << sample_rate; |
| 99 UMA_HISTOGRAM_ENUMERATION("WebRTC.AudioOutputSampleRate", |
| 100 sample_rate, media::kUnexpectedAudioSampleRate); |
| 101 |
| 102 // Verify that the reported output hardware sample rate is supported |
| 103 // on the current platform. |
| 104 if (std::find(&kValidOutputRates[0], |
| 105 &kValidOutputRates[0] + arraysize(kValidOutputRates), |
| 106 sample_rate) == |
| 107 &kValidOutputRates[arraysize(kValidOutputRates)]) { |
| 108 DLOG(ERROR) << sample_rate << " is not a supported output rate."; |
| 109 return false; |
| 110 } |
| 111 |
| 112 media::ChannelLayout channel_layout = media::CHANNEL_LAYOUT_STEREO; |
| 113 |
| 114 int buffer_size = 0; |
| 115 |
| 116 // Windows |
| 117 #if defined(OS_WIN) |
| 118 // Always use stereo rendering on Windows. |
| 119 channel_layout = media::CHANNEL_LAYOUT_STEREO; |
| 120 |
| 121 // Render side: AUDIO_PCM_LOW_LATENCY is based on the Core Audio (WASAPI) |
| 122 // API which was introduced in Windows Vista. For lower Windows versions, |
| 123 // a callback-driven Wave implementation is used instead. An output buffer |
| 124 // size of 10ms works well for WASAPI but 30ms is needed for Wave. |
| 125 |
| 126 // Use different buffer sizes depending on the current hardware sample rate. |
| 127 if (sample_rate == 96000 || sample_rate == 48000) { |
| 128 buffer_size = (sample_rate / 100); |
| 129 } else { |
| 130 // We do run at 44.1kHz at the actual audio layer, but ask for frames |
| 131 // at 44.0kHz to ensure that we can feed them to the webrtc::VoiceEngine. |
| 132 // TODO(henrika): figure out why we seem to need 20ms here for glitch- |
| 133 // free audio. |
| 134 buffer_size = 2 * 440; |
| 135 } |
| 136 |
| 137 // Windows XP and lower can't cope with 10 ms output buffer size. |
| 138 // It must be extended to 30 ms (60 ms will be used internally by WaveOut). |
| 139 if (!media::IsWASAPISupported()) { |
| 140 buffer_size = 3 * buffer_size; |
| 141 DLOG(WARNING) << "Extending the output buffer size by a factor of three " |
| 142 << "since Windows XP has been detected."; |
| 143 } |
| 144 #elif defined(OS_MACOSX) |
| 145 channel_layout = media::CHANNEL_LAYOUT_MONO; |
| 146 |
| 147 // Render side: AUDIO_PCM_LOW_LATENCY on Mac OS X is based on a callback- |
| 148 // driven Core Audio implementation. Tests have shown that 10ms is a suitable |
| 149 // frame size to use, both for 48kHz and 44.1kHz. |
| 150 |
| 151 // Use different buffer sizes depending on the current hardware sample rate. |
| 152 if (sample_rate == 48000) { |
| 153 buffer_size = 480; |
| 154 } else { |
| 155 // We do run at 44.1kHz at the actual audio layer, but ask for frames |
| 156 // at 44.0kHz to ensure that we can feed them to the webrtc::VoiceEngine. |
| 157 buffer_size = 440; |
| 158 } |
| 159 #elif defined(OS_LINUX) || defined(OS_OPENBSD) |
| 160 channel_layout = media::CHANNEL_LAYOUT_MONO; |
| 161 |
| 162 // Based on tests using the current ALSA implementation in Chrome, we have |
| 163 // found that 10ms buffer size on the output side works fine. |
| 164 buffer_size = 480; |
| 165 #else |
| 166 DLOG(ERROR) << "Unsupported platform"; |
| 167 return false; |
| 168 #endif |
| 169 |
| 170 // Store utilized parameters to ensure that we can check them |
| 171 // after a successful initialization. |
| 172 params_.Reset(media::AudioParameters::AUDIO_PCM_LOW_LATENCY, channel_layout, |
| 173 sample_rate, 16, buffer_size); |
| 174 |
| 175 // Allocate local audio buffers based on the parameters above. |
| 176 // It is assumed that each audio sample contains 16 bits and each |
| 177 // audio frame contains one or two audio samples depending on the |
| 178 // number of channels. |
| 179 buffer_.reset(new int16[params_.frames_per_buffer() * params_.channels()]); |
| 180 |
| 181 source_ = source; |
| 182 source->SetRenderFormat(params_); |
| 183 |
| 184 // Configure the audio rendering client and start the rendering. |
| 185 sink_->Initialize(params_, this); |
| 186 |
| 187 sink_->Start(); |
| 188 |
| 189 state_ = PAUSED; |
| 190 |
| 191 UMA_HISTOGRAM_ENUMERATION("WebRTC.AudioOutputChannelLayout", |
| 192 channel_layout, media::CHANNEL_LAYOUT_MAX); |
| 193 UMA_HISTOGRAM_ENUMERATION("WebRTC.AudioOutputFramesPerBuffer", |
| 194 buffer_size, kUnexpectedAudioBufferSize); |
| 195 AddHistogramFramesPerBuffer(buffer_size); |
| 196 |
| 197 return true; |
| 198 } |
| 199 |
| 200 void WebRtcAudioRenderer::Play() { |
| 201 base::AutoLock auto_lock(lock_); |
| 202 if (state_ == UNINITIALIZED) |
| 203 return; |
| 204 |
| 205 state_ = PLAYING; |
| 206 } |
| 207 |
| 208 void WebRtcAudioRenderer::Pause() { |
| 209 base::AutoLock auto_lock(lock_); |
| 210 if (state_ == UNINITIALIZED) |
| 211 return; |
| 212 |
| 213 state_ = PAUSED; |
| 214 } |
| 215 |
| 216 void WebRtcAudioRenderer::Stop() { |
| 217 base::AutoLock auto_lock(lock_); |
| 218 if (state_ == UNINITIALIZED) |
| 219 return; |
| 220 |
| 221 state_ = UNINITIALIZED; |
| 222 source_ = NULL; |
| 223 sink_->Stop(); |
| 224 } |
| 225 |
| 226 void WebRtcAudioRenderer::SetVolume(float volume) { |
| 227 base::AutoLock auto_lock(lock_); |
| 228 if (state_ == UNINITIALIZED) |
| 229 return; |
| 230 |
| 231 sink_->SetVolume(volume); |
| 232 } |
| 233 |
| 234 int WebRtcAudioRenderer::Render(media::AudioBus* audio_bus, |
| 235 int audio_delay_milliseconds) { |
| 236 { |
| 237 base::AutoLock auto_lock(lock_); |
| 238 // Return 0 frames to play out zero if it is not in PLAYING state. |
| 239 if (state_ != PLAYING) |
| 240 return 0; |
| 241 |
| 242 // We need to keep render data for the |source_| reglardless of |state_|, |
| 243 // otherwise the data will be buffered up inside |source_|. |
| 244 source_->RenderData(reinterpret_cast<uint8*>(buffer_.get()), |
| 245 audio_bus->channels(), audio_bus->frames(), |
| 246 audio_delay_milliseconds); |
| 247 } |
| 248 |
| 249 // Deinterleave each channel and convert to 32-bit floating-point |
| 250 // with nominal range -1.0 -> +1.0 to match the callback format. |
| 251 audio_bus->FromInterleaved(buffer_.get(), audio_bus->frames(), |
| 252 params_.bits_per_sample() / 8); |
| 253 return audio_bus->frames(); |
| 254 } |
| 255 |
| 256 void WebRtcAudioRenderer::OnRenderError() { |
| 257 NOTIMPLEMENTED(); |
| 258 LOG(ERROR) << "OnRenderError()"; |
| 259 } |
| 260 |
| 261 } // namespace content |
| OLD | NEW |