Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(340)

Side by Side Diff: content/renderer/media/webrtc_audio_renderer.cc

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

Powered by Google App Engine
This is Rietveld 408576698