OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/renderer/media/webrtc_audio_renderer.h" | 5 #include "content/renderer/media/webrtc_audio_renderer.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/metrics/histogram.h" | 8 #include "base/metrics/histogram.h" |
9 #include "base/string_util.h" | 9 #include "base/string_util.h" |
10 #include "content/renderer/media/audio_device_factory.h" | 10 #include "content/renderer/media/audio_device_factory.h" |
(...skipping 28 matching lines...) Expand all Loading... | |
39 // Android devices. | 39 // Android devices. |
40 const int kValidOutputRates[] = {48000, 44100, 16000}; | 40 const int kValidOutputRates[] = {48000, 44100, 16000}; |
41 #else | 41 #else |
42 const int kValidOutputRates[] = {44100}; | 42 const int kValidOutputRates[] = {44100}; |
43 #endif | 43 #endif |
44 | 44 |
45 // TODO(xians): Merge the following code to WebRtcAudioCapturer, or remove. | 45 // TODO(xians): Merge the following code to WebRtcAudioCapturer, or remove. |
46 enum AudioFramesPerBuffer { | 46 enum AudioFramesPerBuffer { |
47 k160, | 47 k160, |
48 k320, | 48 k320, |
49 k440, // WebRTC works internally with 440 audio frames at 44.1kHz. | 49 k440, |
50 k480, | 50 k480, |
51 k640, | 51 k640, |
52 k880, | 52 k880, |
53 k960, | 53 k960, |
54 k1440, | 54 k1440, |
55 k1920, | 55 k1920, |
56 kUnexpectedAudioBufferSize // Must always be last! | 56 kUnexpectedAudioBufferSize // Must always be last! |
57 }; | 57 }; |
58 | 58 |
59 // Helper method to convert integral values to their respective enum values | 59 // Helper method to convert integral values to their respective enum values |
60 // above, or kUnexpectedAudioBufferSize if no match exists. | 60 // above, or kUnexpectedAudioBufferSize if no match exists. |
61 // We map 441 to k440 to avoid changes in the XML part for histograms. | |
62 // It is still possible to map the histogram result to the actual buffer size. | |
63 // See http://crbug.com/243450 for details. | |
61 AudioFramesPerBuffer AsAudioFramesPerBuffer(int frames_per_buffer) { | 64 AudioFramesPerBuffer AsAudioFramesPerBuffer(int frames_per_buffer) { |
62 switch (frames_per_buffer) { | 65 switch (frames_per_buffer) { |
63 case 160: return k160; | 66 case 160: return k160; |
64 case 320: return k320; | 67 case 320: return k320; |
65 case 440: return k440; | 68 case 441: return k440; |
66 case 480: return k480; | 69 case 480: return k480; |
67 case 640: return k640; | 70 case 640: return k640; |
68 case 880: return k880; | 71 case 880: return k880; |
69 case 960: return k960; | 72 case 960: return k960; |
70 case 1440: return k1440; | 73 case 1440: return k1440; |
71 case 1920: return k1920; | 74 case 1920: return k1920; |
72 } | 75 } |
73 return kUnexpectedAudioBufferSize; | 76 return kUnexpectedAudioBufferSize; |
74 } | 77 } |
75 | 78 |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
140 // on the current platform. | 143 // on the current platform. |
141 if (std::find(&kValidOutputRates[0], | 144 if (std::find(&kValidOutputRates[0], |
142 &kValidOutputRates[0] + arraysize(kValidOutputRates), | 145 &kValidOutputRates[0] + arraysize(kValidOutputRates), |
143 sample_rate) == | 146 sample_rate) == |
144 &kValidOutputRates[arraysize(kValidOutputRates)]) { | 147 &kValidOutputRates[arraysize(kValidOutputRates)]) { |
145 DLOG(ERROR) << sample_rate << " is not a supported output rate."; | 148 DLOG(ERROR) << sample_rate << " is not a supported output rate."; |
146 return false; | 149 return false; |
147 } | 150 } |
148 | 151 |
149 // Set up audio parameters for the source, i.e., the WebRTC client. | 152 // Set up audio parameters for the source, i.e., the WebRTC client. |
153 | |
150 // The WebRTC client only supports multiples of 10ms as buffer size where | 154 // The WebRTC client only supports multiples of 10ms as buffer size where |
151 // 10ms is preferred for lowest possible delay. | 155 // 10ms is preferred for lowest possible delay. |
152 | |
153 media::AudioParameters source_params; | 156 media::AudioParameters source_params; |
154 int buffer_size = 0; | 157 int buffer_size = (sample_rate / 100); |
155 | 158 DVLOG(1) << "Using WebRTC output buffer size: " << buffer_size; |
156 if (sample_rate % 8000 == 0) { | |
ajm
2013/05/28 15:13:05
Since webrtc needs 10 ms buffers it only supports
henrika (OOO until Aug 14)
2013/05/28 16:40:30
You will catch that in the lines above where we en
ajm
2013/05/28 16:43:22
Sounds good, thanks.
| |
157 buffer_size = (sample_rate / 100); | |
158 } else if (sample_rate == 44100) { | |
159 // The resampler in WebRTC does not support 441 as input. We hard code | |
160 // the size to 440 (~0.9977ms) instead and rely on the internal jitter | |
161 // buffer in WebRTC to deal with the resulting drift. | |
162 // TODO(henrika): ensure that WebRTC supports 44100Hz and use 441 instead. | |
163 buffer_size = 440; | |
164 } else { | |
165 return false; | |
166 } | |
167 | 159 |
168 int channels = ChannelLayoutToChannelCount(channel_layout); | 160 int channels = ChannelLayoutToChannelCount(channel_layout); |
169 source_params.Reset(media::AudioParameters::AUDIO_PCM_LOW_LATENCY, | 161 source_params.Reset(media::AudioParameters::AUDIO_PCM_LOW_LATENCY, |
170 channel_layout, channels, 0, | 162 channel_layout, channels, 0, |
171 sample_rate, 16, buffer_size); | 163 sample_rate, 16, buffer_size); |
172 | 164 |
173 // Set up audio parameters for the sink, i.e., the native audio output stream. | 165 // Set up audio parameters for the sink, i.e., the native audio output stream. |
174 // We strive to open up using native parameters to achieve best possible | 166 // We strive to open up using native parameters to achieve best possible |
175 // performance and to ensure that no FIFO is needed on the browser side to | 167 // performance and to ensure that no FIFO is needed on the browser side to |
176 // match the client request. Any mismatch between the source and the sink is | 168 // match the client request. Any mismatch between the source and the sink is |
(...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
353 } | 345 } |
354 | 346 |
355 // De-interleave each channel and convert to 32-bit floating-point | 347 // De-interleave each channel and convert to 32-bit floating-point |
356 // with nominal range -1.0 -> +1.0 to match the callback format. | 348 // with nominal range -1.0 -> +1.0 to match the callback format. |
357 audio_bus->FromInterleaved(buffer_.get(), | 349 audio_bus->FromInterleaved(buffer_.get(), |
358 audio_bus->frames(), | 350 audio_bus->frames(), |
359 sizeof(buffer_[0])); | 351 sizeof(buffer_[0])); |
360 } | 352 } |
361 | 353 |
362 } // namespace content | 354 } // namespace content |
OLD | NEW |