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

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: addressed Wei's comments and fixed the content_unittest 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
« no previous file with comments | « content/renderer/media/webrtc_audio_renderer.h ('k') | media/base/rtc_audio_renderer.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 void WebRtcAudioRenderer::Initialize(
87 WebRtcAudioRendererSource* source) {
88 base::AutoLock auto_lock(lock_);
89 DCHECK_EQ(state_, UNINITIALIZED);
90 DCHECK(source);
91 DCHECK(!sink_);
92 DCHECK(!source_);
93
94 sink_ = AudioDeviceFactory::NewOutputDevice();
95 DCHECK(sink_);
96
97 // Ask the browser for the default audio output hardware sample-rate.
98 // This request is based on a synchronous IPC message.
99 int sample_rate = GetAudioOutputSampleRate();
100 DVLOG(1) << "Audio output hardware sample rate: " << sample_rate;
101 UMA_HISTOGRAM_ENUMERATION("WebRTC.AudioOutputSampleRate",
102 sample_rate, media::kUnexpectedAudioSampleRate);
103
104 // Verify that the reported output hardware sample rate is supported
105 // on the current platform.
106 if (std::find(&kValidOutputRates[0],
107 &kValidOutputRates[0] + arraysize(kValidOutputRates),
108 sample_rate) ==
109 &kValidOutputRates[arraysize(kValidOutputRates)]) {
110 DLOG(ERROR) << sample_rate << " is not a supported output rate.";
111 return;
112 }
113
114 media::ChannelLayout channel_layout = media::CHANNEL_LAYOUT_STEREO;
115
116 int buffer_size = 0;
117
118 // Windows
119 #if defined(OS_WIN)
120 // Always use stereo rendering on Windows.
121 channel_layout = media::CHANNEL_LAYOUT_STEREO;
122
123 // Render side: AUDIO_PCM_LOW_LATENCY is based on the Core Audio (WASAPI)
124 // API which was introduced in Windows Vista. For lower Windows versions,
125 // a callback-driven Wave implementation is used instead. An output buffer
126 // size of 10ms works well for WASAPI but 30ms is needed for Wave.
127
128 // Use different buffer sizes depending on the current hardware sample rate.
129 if (sample_rate == 96000 || sample_rate == 48000) {
130 buffer_size = (sample_rate / 100);
131 } else {
132 // We do run at 44.1kHz at the actual audio layer, but ask for frames
133 // at 44.0kHz to ensure that we can feed them to the webrtc::VoiceEngine.
134 // TODO(henrika): figure out why we seem to need 20ms here for glitch-
135 // free audio.
136 buffer_size = 2 * 440;
137 }
138
139 // Windows XP and lower can't cope with 10 ms output buffer size.
140 // It must be extended to 30 ms (60 ms will be used internally by WaveOut).
141 if (!media::IsWASAPISupported()) {
142 buffer_size = 3 * buffer_size;
143 DLOG(WARNING) << "Extending the output buffer size by a factor of three "
144 << "since Windows XP has been detected.";
145 }
146 #elif defined(OS_MACOSX)
147 channel_layout = media::CHANNEL_LAYOUT_MONO;
148
149 // Render side: AUDIO_PCM_LOW_LATENCY on Mac OS X is based on a callback-
150 // driven Core Audio implementation. Tests have shown that 10ms is a suitable
151 // frame size to use, both for 48kHz and 44.1kHz.
152
153 // Use different buffer sizes depending on the current hardware sample rate.
154 if (sample_rate == 48000) {
155 buffer_size = 480;
156 } else {
157 // We do run at 44.1kHz at the actual audio layer, but ask for frames
158 // at 44.0kHz to ensure that we can feed them to the webrtc::VoiceEngine.
159 buffer_size = 440;
160 }
161 #elif defined(OS_LINUX) || defined(OS_OPENBSD)
162 channel_layout = media::CHANNEL_LAYOUT_MONO;
163
164 // Based on tests using the current ALSA implementation in Chrome, we have
165 // found that 10ms buffer size on the output side works fine.
166 buffer_size = 480;
167 #else
168 DLOG(ERROR) << "Unsupported platform";
169 return -1;
170 #endif
171
172 // Store utilized parameters to ensure that we can check them
173 // after a successful initialization.
174 params_.Reset(media::AudioParameters::AUDIO_PCM_LOW_LATENCY, channel_layout,
175 sample_rate, 16, buffer_size);
176
177 // Allocate local audio buffers based on the parameters above.
178 // It is assumed that each audio sample contains 16 bits and each
179 // audio frame contains one or two audio samples depending on the
180 // number of channels.
181 buffer_.reset(new int16[params_.frames_per_buffer() * params_.channels()]);
182
183 source_ = source;
184 source->SetRenderFormat(params_);
185
186 // Configure the audio rendering client and start the rendering.
187 sink_->Initialize(params_, this);
188
189 sink_->Start();
190
191 state_ = PAUSED;
192
193 UMA_HISTOGRAM_ENUMERATION("WebRTC.AudioOutputChannelLayout",
194 channel_layout, media::CHANNEL_LAYOUT_MAX);
195 UMA_HISTOGRAM_ENUMERATION("WebRTC.AudioOutputFramesPerBuffer",
196 buffer_size, kUnexpectedAudioBufferSize);
197 AddHistogramFramesPerBuffer(buffer_size);
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) {
no longer working on chromium 2012/10/25 10:21:26 the review tool seems to show the wrong indentatio
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
OLDNEW
« no previous file with comments | « content/renderer/media/webrtc_audio_renderer.h ('k') | media/base/rtc_audio_renderer.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698