Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 1 | // Copyright 2013 The Chromium Authors. All rights reserved. |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 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_device_impl.h" |
| 6 | |
| 7 | #include "base/bind.h" |
| 8 | #include "base/metrics/histogram.h" |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 9 | #include "base/strings/string_util.h" |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 10 | #include "base/win/windows_version.h" |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 11 | #include "content/renderer/media/webrtc_audio_capturer.h" |
| 12 | #include "content/renderer/media/webrtc_audio_renderer.h" |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 13 | #include "content/renderer/render_thread_impl.h" |
| 14 | #include "media/audio/audio_parameters.h" |
| 15 | #include "media/audio/audio_util.h" |
| 16 | #include "media/audio/sample_rates.h" |
| 17 | |
| 18 | using media::AudioParameters; |
| 19 | using media::ChannelLayout; |
| 20 | |
| 21 | namespace content { |
| 22 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 23 | WebRtcAudioDeviceImpl::WebRtcAudioDeviceImpl() |
| 24 | : ref_count_(0), |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 25 | audio_transport_callback_(NULL), |
| 26 | input_delay_ms_(0), |
| 27 | output_delay_ms_(0), |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 28 | initialized_(false), |
| 29 | playing_(false), |
| 30 | recording_(false), |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 31 | agc_is_enabled_(false), |
| 32 | microphone_volume_(0) { |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 33 | DVLOG(1) << "WebRtcAudioDeviceImpl::WebRtcAudioDeviceImpl()"; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | WebRtcAudioDeviceImpl::~WebRtcAudioDeviceImpl() { |
| 37 | DVLOG(1) << "WebRtcAudioDeviceImpl::~WebRtcAudioDeviceImpl()"; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 38 | DCHECK(thread_checker_.CalledOnValidThread()); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 39 | Terminate(); |
| 40 | } |
| 41 | |
| 42 | int32_t WebRtcAudioDeviceImpl::AddRef() { |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 43 | DCHECK(thread_checker_.CalledOnValidThread()); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 44 | return base::subtle::Barrier_AtomicIncrement(&ref_count_, 1); |
| 45 | } |
| 46 | |
| 47 | int32_t WebRtcAudioDeviceImpl::Release() { |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 48 | DCHECK(thread_checker_.CalledOnValidThread()); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 49 | int ret = base::subtle::Barrier_AtomicIncrement(&ref_count_, -1); |
| 50 | if (ret == 0) { |
| 51 | delete this; |
| 52 | } |
| 53 | return ret; |
| 54 | } |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 55 | int WebRtcAudioDeviceImpl::CaptureData(const std::vector<int>& channels, |
| 56 | const int16* audio_data, |
| 57 | int sample_rate, |
| 58 | int number_of_channels, |
| 59 | int number_of_frames, |
| 60 | int audio_delay_milliseconds, |
| 61 | int current_volume, |
| 62 | bool need_audio_processing) { |
| 63 | int total_delay_ms = 0; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 64 | { |
| 65 | base::AutoLock auto_lock(lock_); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 66 | if (!recording_) |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 67 | return 0; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 68 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 69 | // Store the reported audio delay locally. |
| 70 | input_delay_ms_ = audio_delay_milliseconds; |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 71 | total_delay_ms = input_delay_ms_ + output_delay_ms_; |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 72 | DVLOG(2) << "total delay: " << input_delay_ms_ + output_delay_ms_; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 73 | } |
| 74 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 75 | // Write audio samples in blocks of 10 milliseconds to the registered |
| 76 | // webrtc::AudioTransport sink. Keep writing until our internal byte |
| 77 | // buffer is empty. |
Torne (Richard Coles) | b2df76e | 2013-05-13 16:52:09 +0100 | [diff] [blame] | 78 | // TODO(niklase): Wire up the key press detection. |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 79 | const int16* audio_buffer = audio_data; |
| 80 | const int samples_per_10_msec = (sample_rate / 100); |
| 81 | int accumulated_audio_samples = 0; |
Torne (Richard Coles) | b2df76e | 2013-05-13 16:52:09 +0100 | [diff] [blame] | 82 | bool key_pressed = false; |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 83 | uint32_t new_volume = 0; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 84 | while (accumulated_audio_samples < number_of_frames) { |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 85 | // Deliver 10ms of recorded 16-bit linear PCM audio. |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 86 | int new_mic_level = audio_transport_callback_->OnDataAvailable( |
| 87 | &channels[0], |
| 88 | channels.size(), |
| 89 | audio_buffer, |
| 90 | sample_rate, |
| 91 | number_of_channels, |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 92 | samples_per_10_msec, |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 93 | total_delay_ms, |
| 94 | current_volume, |
Torne (Richard Coles) | b2df76e | 2013-05-13 16:52:09 +0100 | [diff] [blame] | 95 | key_pressed, |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 96 | need_audio_processing); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 97 | |
| 98 | accumulated_audio_samples += samples_per_10_msec; |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 99 | audio_buffer += samples_per_10_msec * number_of_channels; |
| 100 | |
| 101 | // The latest non-zero new microphone level will be returned. |
| 102 | if (new_mic_level) |
| 103 | new_volume = new_mic_level; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 104 | } |
| 105 | |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 106 | return new_volume; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 107 | } |
| 108 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 109 | void WebRtcAudioDeviceImpl::SetCaptureFormat( |
| 110 | const media::AudioParameters& params) { |
| 111 | DVLOG(1) << "WebRtcAudioDeviceImpl::SetCaptureFormat()"; |
| 112 | DCHECK(thread_checker_.CalledOnValidThread()); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 113 | } |
| 114 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 115 | void WebRtcAudioDeviceImpl::RenderData(uint8* audio_data, |
| 116 | int number_of_channels, |
| 117 | int number_of_frames, |
| 118 | int audio_delay_milliseconds) { |
| 119 | DCHECK_LE(number_of_frames, output_buffer_size()); |
| 120 | { |
| 121 | base::AutoLock auto_lock(lock_); |
| 122 | // Store the reported audio delay locally. |
| 123 | output_delay_ms_ = audio_delay_milliseconds; |
| 124 | } |
| 125 | |
| 126 | const int channels = number_of_channels; |
| 127 | DCHECK_LE(channels, output_channels()); |
| 128 | |
| 129 | int samples_per_sec = output_sample_rate(); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 130 | int samples_per_10_msec = (samples_per_sec / 100); |
| 131 | int bytes_per_sample = output_audio_parameters_.bits_per_sample() / 8; |
| 132 | const int bytes_per_10_msec = |
| 133 | channels * samples_per_10_msec * bytes_per_sample; |
| 134 | |
| 135 | uint32_t num_audio_samples = 0; |
| 136 | int accumulated_audio_samples = 0; |
| 137 | |
| 138 | // Get audio samples in blocks of 10 milliseconds from the registered |
| 139 | // webrtc::AudioTransport source. Keep reading until our internal buffer |
| 140 | // is full. |
| 141 | while (accumulated_audio_samples < number_of_frames) { |
| 142 | // Get 10ms and append output to temporary byte buffer. |
| 143 | audio_transport_callback_->NeedMorePlayData(samples_per_10_msec, |
| 144 | bytes_per_sample, |
| 145 | channels, |
| 146 | samples_per_sec, |
| 147 | audio_data, |
| 148 | num_audio_samples); |
| 149 | accumulated_audio_samples += num_audio_samples; |
| 150 | audio_data += bytes_per_10_msec; |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | void WebRtcAudioDeviceImpl::SetRenderFormat(const AudioParameters& params) { |
| 155 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 156 | output_audio_parameters_ = params; |
| 157 | } |
| 158 | |
| 159 | void WebRtcAudioDeviceImpl::RemoveAudioRenderer(WebRtcAudioRenderer* renderer) { |
| 160 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 161 | DCHECK_EQ(renderer, renderer_); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 162 | base::AutoLock auto_lock(lock_); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 163 | renderer_ = NULL; |
| 164 | playing_ = false; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | int32_t WebRtcAudioDeviceImpl::RegisterAudioCallback( |
| 168 | webrtc::AudioTransport* audio_callback) { |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 169 | DVLOG(1) << "WebRtcAudioDeviceImpl::RegisterAudioCallback()"; |
| 170 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 171 | DCHECK_EQ(audio_transport_callback_ == NULL, audio_callback != NULL); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 172 | audio_transport_callback_ = audio_callback; |
| 173 | return 0; |
| 174 | } |
| 175 | |
| 176 | int32_t WebRtcAudioDeviceImpl::Init() { |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 177 | DVLOG(1) << "WebRtcAudioDeviceImpl::Init()"; |
| 178 | DCHECK(thread_checker_.CalledOnValidThread()); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 179 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 180 | // We need to return a success to continue the initialization of WebRtc VoE |
| 181 | // because failure on the capturer_ initialization should not prevent WebRTC |
| 182 | // from working. See issue http://crbug.com/144421 for details. |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 183 | initialized_ = true; |
| 184 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 185 | return 0; |
| 186 | } |
| 187 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 188 | int32_t WebRtcAudioDeviceImpl::Terminate() { |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 189 | DVLOG(1) << "WebRtcAudioDeviceImpl::Terminate()"; |
| 190 | DCHECK(thread_checker_.CalledOnValidThread()); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 191 | |
| 192 | // Calling Terminate() multiple times in a row is OK. |
| 193 | if (!initialized_) |
| 194 | return 0; |
| 195 | |
| 196 | StopRecording(); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 197 | StopPlayout(); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 198 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 199 | // It is necessary to stop the |renderer_| before going away. |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 200 | if (renderer_.get()) { |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 201 | // Grab a local reference while we call Stop(), which will trigger a call to |
| 202 | // RemoveAudioRenderer that clears our reference to the audio renderer. |
| 203 | scoped_refptr<WebRtcAudioRenderer> local_renderer(renderer_); |
| 204 | local_renderer->Stop(); |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 205 | DCHECK(!renderer_.get()); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 206 | } |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 207 | |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 208 | capturers_.clear(); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 209 | |
| 210 | initialized_ = false; |
| 211 | return 0; |
| 212 | } |
| 213 | |
| 214 | bool WebRtcAudioDeviceImpl::Initialized() const { |
| 215 | return initialized_; |
| 216 | } |
| 217 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 218 | int32_t WebRtcAudioDeviceImpl::PlayoutIsAvailable(bool* available) { |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 219 | *available = initialized_; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 220 | return 0; |
| 221 | } |
| 222 | |
| 223 | bool WebRtcAudioDeviceImpl::PlayoutIsInitialized() const { |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 224 | return initialized_; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | int32_t WebRtcAudioDeviceImpl::RecordingIsAvailable(bool* available) { |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 228 | *available = (!capturers_.empty()); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 229 | return 0; |
| 230 | } |
| 231 | |
| 232 | bool WebRtcAudioDeviceImpl::RecordingIsInitialized() const { |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 233 | DVLOG(1) << "WebRtcAudioDeviceImpl::RecordingIsInitialized()"; |
| 234 | DCHECK(thread_checker_.CalledOnValidThread()); |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 235 | return (!capturers_.empty()); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | int32_t WebRtcAudioDeviceImpl::StartPlayout() { |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 239 | DVLOG(1) << "WebRtcAudioDeviceImpl::StartPlayout()"; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 240 | LOG_IF(ERROR, !audio_transport_callback_) << "Audio transport is missing"; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 241 | { |
| 242 | base::AutoLock auto_lock(lock_); |
| 243 | if (!audio_transport_callback_) |
| 244 | return 0; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | if (playing_) { |
| 248 | // webrtc::VoiceEngine assumes that it is OK to call Start() twice and |
| 249 | // that the call is ignored the second time. |
| 250 | return 0; |
| 251 | } |
| 252 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 253 | playing_ = true; |
| 254 | start_render_time_ = base::Time::Now(); |
| 255 | return 0; |
| 256 | } |
| 257 | |
| 258 | int32_t WebRtcAudioDeviceImpl::StopPlayout() { |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 259 | DVLOG(1) << "WebRtcAudioDeviceImpl::StopPlayout()"; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 260 | if (!playing_) { |
| 261 | // webrtc::VoiceEngine assumes that it is OK to call Stop() just in case. |
| 262 | return 0; |
| 263 | } |
| 264 | |
| 265 | // Add histogram data to be uploaded as part of an UMA logging event. |
| 266 | // This histogram keeps track of total playout times. |
| 267 | if (!start_render_time_.is_null()) { |
| 268 | base::TimeDelta render_time = base::Time::Now() - start_render_time_; |
| 269 | UMA_HISTOGRAM_LONG_TIMES("WebRTC.AudioRenderTime", render_time); |
| 270 | } |
| 271 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 272 | playing_ = false; |
| 273 | return 0; |
| 274 | } |
| 275 | |
| 276 | bool WebRtcAudioDeviceImpl::Playing() const { |
| 277 | return playing_; |
| 278 | } |
| 279 | |
| 280 | int32_t WebRtcAudioDeviceImpl::StartRecording() { |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 281 | DVLOG(1) << "WebRtcAudioDeviceImpl::StartRecording()"; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 282 | DCHECK(initialized_); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 283 | LOG_IF(ERROR, !audio_transport_callback_) << "Audio transport is missing"; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 284 | if (!audio_transport_callback_) { |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 285 | return -1; |
| 286 | } |
| 287 | |
Torne (Richard Coles) | 7d4cd47 | 2013-06-19 11:58:07 +0100 | [diff] [blame] | 288 | { |
| 289 | base::AutoLock auto_lock(lock_); |
| 290 | if (recording_) |
| 291 | return 0; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 292 | |
Torne (Richard Coles) | 7d4cd47 | 2013-06-19 11:58:07 +0100 | [diff] [blame] | 293 | recording_ = true; |
| 294 | } |
| 295 | |
| 296 | start_capture_time_ = base::Time::Now(); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 297 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 298 | return 0; |
| 299 | } |
| 300 | |
| 301 | int32_t WebRtcAudioDeviceImpl::StopRecording() { |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 302 | DVLOG(1) << "WebRtcAudioDeviceImpl::StopRecording()"; |
Torne (Richard Coles) | 7d4cd47 | 2013-06-19 11:58:07 +0100 | [diff] [blame] | 303 | { |
| 304 | base::AutoLock auto_lock(lock_); |
| 305 | if (!recording_) |
| 306 | return 0; |
| 307 | |
| 308 | recording_ = false; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 309 | } |
| 310 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 311 | // Add histogram data to be uploaded as part of an UMA logging event. |
| 312 | // This histogram keeps track of total recording times. |
| 313 | if (!start_capture_time_.is_null()) { |
| 314 | base::TimeDelta capture_time = base::Time::Now() - start_capture_time_; |
| 315 | UMA_HISTOGRAM_LONG_TIMES("WebRTC.AudioCaptureTime", capture_time); |
| 316 | } |
| 317 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 318 | return 0; |
| 319 | } |
| 320 | |
| 321 | bool WebRtcAudioDeviceImpl::Recording() const { |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 322 | base::AutoLock auto_lock(lock_); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 323 | return recording_; |
| 324 | } |
| 325 | |
| 326 | int32_t WebRtcAudioDeviceImpl::SetAGC(bool enable) { |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 327 | DVLOG(1) << "WebRtcAudioDeviceImpl::SetAGC(enable=" << enable << ")"; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 328 | DCHECK(initialized_); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 329 | |
| 330 | // Return early if we are not changing the AGC state. |
| 331 | if (enable == agc_is_enabled_) |
| 332 | return 0; |
| 333 | |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 334 | // Set the AGC on all the capturers. It depends on the source of the |
| 335 | // capturer whether AGC is supported or not. |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 336 | // The current implementation does not support changing the AGC state while |
| 337 | // recording. Using this approach simplifies the design and it is also |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 338 | // inline with the latest WebRTC standard. |
| 339 | for (CapturerList::const_iterator iter = capturers_.begin(); |
| 340 | iter != capturers_.end(); ++iter) { |
| 341 | if (!(*iter)->is_recording()) |
| 342 | (*iter)->SetAutomaticGainControl(enable); |
| 343 | } |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 344 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 345 | agc_is_enabled_ = enable; |
| 346 | return 0; |
| 347 | } |
| 348 | |
| 349 | bool WebRtcAudioDeviceImpl::AGC() const { |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 350 | DVLOG(1) << "WebRtcAudioDeviceImpl::AGC()"; |
| 351 | DCHECK(thread_checker_.CalledOnValidThread()); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 352 | // To reduce the usage of IPC messages, an internal AGC state is used. |
| 353 | // TODO(henrika): investigate if there is a need for a "deeper" getter. |
| 354 | return agc_is_enabled_; |
| 355 | } |
| 356 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 357 | int32_t WebRtcAudioDeviceImpl::SetMicrophoneVolume(uint32_t volume) { |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 358 | DVLOG(1) << "WebRtcAudioDeviceImpl::SetMicrophoneVolume(" << volume << ")"; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 359 | DCHECK(initialized_); |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 360 | |
| 361 | // Only one microphone is supported at the moment, which is represented by |
| 362 | // the default capturer. |
| 363 | scoped_refptr<WebRtcAudioCapturer> capturer(GetDefaultCapturer()); |
| 364 | if (!capturer.get()) |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 365 | return -1; |
| 366 | |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 367 | capturer->SetVolume(volume); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 368 | return 0; |
| 369 | } |
| 370 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 371 | // TODO(henrika): sort out calling thread once we start using this API. |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 372 | int32_t WebRtcAudioDeviceImpl::MicrophoneVolume(uint32_t* volume) const { |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 373 | DVLOG(1) << "WebRtcAudioDeviceImpl::MicrophoneVolume()"; |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 374 | // We only support one microphone now, which is accessed via the default |
| 375 | // capturer. |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 376 | DCHECK(initialized_); |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 377 | scoped_refptr<WebRtcAudioCapturer> capturer(GetDefaultCapturer()); |
| 378 | if (!capturer.get()) |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 379 | return -1; |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 380 | |
| 381 | *volume = static_cast<uint32_t>(capturer->Volume()); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 382 | return 0; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | int32_t WebRtcAudioDeviceImpl::MaxMicrophoneVolume(uint32_t* max_volume) const { |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 386 | DCHECK(initialized_); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 387 | *max_volume = kMaxVolumeLevel; |
| 388 | return 0; |
| 389 | } |
| 390 | |
| 391 | int32_t WebRtcAudioDeviceImpl::MinMicrophoneVolume(uint32_t* min_volume) const { |
| 392 | *min_volume = 0; |
| 393 | return 0; |
| 394 | } |
| 395 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 396 | int32_t WebRtcAudioDeviceImpl::StereoPlayoutIsAvailable(bool* available) const { |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 397 | DCHECK(initialized_); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 398 | *available = (output_channels() == 2); |
| 399 | return 0; |
| 400 | } |
| 401 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 402 | int32_t WebRtcAudioDeviceImpl::StereoRecordingIsAvailable( |
| 403 | bool* available) const { |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 404 | DCHECK(initialized_); |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 405 | // TODO(xians): These kind of hardware methods do not make much sense since we |
| 406 | // support multiple sources. Remove or figure out new APIs for such methods. |
| 407 | scoped_refptr<WebRtcAudioCapturer> capturer(GetDefaultCapturer()); |
| 408 | if (!capturer.get()) |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 409 | return -1; |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 410 | |
| 411 | *available = (capturer->audio_parameters().channels() == 2); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 412 | return 0; |
| 413 | } |
| 414 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 415 | int32_t WebRtcAudioDeviceImpl::PlayoutDelay(uint16_t* delay_ms) const { |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 416 | base::AutoLock auto_lock(lock_); |
| 417 | *delay_ms = static_cast<uint16_t>(output_delay_ms_); |
| 418 | return 0; |
| 419 | } |
| 420 | |
| 421 | int32_t WebRtcAudioDeviceImpl::RecordingDelay(uint16_t* delay_ms) const { |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 422 | base::AutoLock auto_lock(lock_); |
| 423 | *delay_ms = static_cast<uint16_t>(input_delay_ms_); |
| 424 | return 0; |
| 425 | } |
| 426 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 427 | int32_t WebRtcAudioDeviceImpl::RecordingSampleRate( |
| 428 | uint32_t* samples_per_sec) const { |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 429 | // We use the default capturer as the recording sample rate. |
| 430 | scoped_refptr<WebRtcAudioCapturer> capturer(GetDefaultCapturer()); |
| 431 | if (!capturer.get()) |
| 432 | return -1; |
| 433 | |
| 434 | *samples_per_sec = static_cast<uint32_t>( |
| 435 | capturer->audio_parameters().sample_rate()); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 436 | return 0; |
| 437 | } |
| 438 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 439 | int32_t WebRtcAudioDeviceImpl::PlayoutSampleRate( |
| 440 | uint32_t* samples_per_sec) const { |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 441 | *samples_per_sec = static_cast<uint32_t>(output_sample_rate()); |
| 442 | return 0; |
| 443 | } |
| 444 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 445 | bool WebRtcAudioDeviceImpl::SetAudioRenderer(WebRtcAudioRenderer* renderer) { |
| 446 | DCHECK(thread_checker_.CalledOnValidThread()); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 447 | DCHECK(renderer); |
| 448 | |
| 449 | base::AutoLock auto_lock(lock_); |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 450 | if (renderer_.get()) |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 451 | return false; |
| 452 | |
| 453 | if (!renderer->Initialize(this)) |
| 454 | return false; |
| 455 | |
| 456 | renderer_ = renderer; |
| 457 | return true; |
| 458 | } |
| 459 | |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 460 | void WebRtcAudioDeviceImpl::AddAudioCapturer( |
| 461 | const scoped_refptr<WebRtcAudioCapturer>& capturer) { |
| 462 | DVLOG(1) << "WebRtcAudioDeviceImpl::AddAudioCapturer()"; |
| 463 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 464 | DCHECK(capturer.get()); |
| 465 | |
| 466 | // Enable/disable the AGC on the new capture. |
| 467 | DCHECK(!capturer->is_recording()); |
| 468 | capturer->SetAutomaticGainControl(agc_is_enabled_); |
| 469 | |
| 470 | // We only support one microphone today, which means the list can contain |
| 471 | // only one capturer with a valid device id. |
| 472 | DCHECK(capturer->device_id().empty() || !GetDefaultCapturer()); |
| 473 | base::AutoLock auto_lock(lock_); |
| 474 | capturers_.push_back(capturer); |
| 475 | } |
| 476 | |
| 477 | scoped_refptr<WebRtcAudioCapturer> |
| 478 | WebRtcAudioDeviceImpl::GetDefaultCapturer() const { |
| 479 | base::AutoLock auto_lock(lock_); |
| 480 | for (CapturerList::const_iterator iter = capturers_.begin(); |
| 481 | iter != capturers_.end(); ++iter) { |
| 482 | if (!(*iter)->device_id().empty()) |
| 483 | return *iter; |
| 484 | } |
| 485 | |
| 486 | return NULL; |
| 487 | } |
| 488 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 489 | } // namespace content |