| 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_device_impl.h" | 5 #include "content/renderer/media/webrtc_audio_device_impl.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.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 "base/win/windows_version.h" | 10 #include "base/win/windows_version.h" |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 122 // Report unexpected sample rates using a unique histogram name. | 122 // Report unexpected sample rates using a unique histogram name. |
| 123 if (dir == kAudioOutput) { | 123 if (dir == kAudioOutput) { |
| 124 UMA_HISTOGRAM_COUNTS("WebRTC.AudioOutputFramesPerBufferUnexpected", | 124 UMA_HISTOGRAM_COUNTS("WebRTC.AudioOutputFramesPerBufferUnexpected", |
| 125 param); | 125 param); |
| 126 } else { | 126 } else { |
| 127 UMA_HISTOGRAM_COUNTS("WebRTC.AudioInputFramesPerBufferUnexpected", param); | 127 UMA_HISTOGRAM_COUNTS("WebRTC.AudioInputFramesPerBufferUnexpected", param); |
| 128 } | 128 } |
| 129 } | 129 } |
| 130 } | 130 } |
| 131 | 131 |
| 132 WebRtcAudioDeviceImpl::WebRtcAudioDeviceImpl() | 132 WebRtcAudioDeviceImpl::WebRtcAudioDeviceImpl(int render_view_id) |
| 133 : ref_count_(0), | 133 : ref_count_(0), |
| 134 render_loop_(base::MessageLoopProxy::current()), | 134 render_loop_(base::MessageLoopProxy::current()), |
| 135 audio_transport_callback_(NULL), | 135 audio_transport_callback_(NULL), |
| 136 input_delay_ms_(0), | 136 input_delay_ms_(0), |
| 137 output_delay_ms_(0), | 137 output_delay_ms_(0), |
| 138 last_error_(AudioDeviceModule::kAdmErrNone), | 138 last_error_(AudioDeviceModule::kAdmErrNone), |
| 139 last_process_time_(base::TimeTicks::Now()), | 139 last_process_time_(base::TimeTicks::Now()), |
| 140 session_id_(0), | 140 session_id_(0), |
| 141 bytes_per_sample_(0), | 141 bytes_per_sample_(0), |
| 142 initialized_(false), | 142 initialized_(false), |
| 143 playing_(false), | 143 playing_(false), |
| 144 recording_(false), | 144 recording_(false), |
| 145 agc_is_enabled_(false) { | 145 agc_is_enabled_(false) { |
| 146 DVLOG(1) << "WebRtcAudioDeviceImpl::WebRtcAudioDeviceImpl()"; | 146 DVLOG(1) << "WebRtcAudioDeviceImpl::WebRtcAudioDeviceImpl()"; |
| 147 // TODO(henrika): remove this restriction when factory is used for the | 147 // TODO(henrika): remove this restriction when factory is used for the |
| 148 // input side as well. | 148 // input side as well. |
| 149 DCHECK(RenderThreadImpl::current()) << | 149 DCHECK(RenderThreadImpl::current()) << |
| 150 "WebRtcAudioDeviceImpl must be constructed on the render thread"; | 150 "WebRtcAudioDeviceImpl must be constructed on the render thread"; |
| 151 audio_output_device_ = AudioDeviceFactory::NewOutputDevice(); | 151 audio_input_device_ = AudioDeviceFactory::NewInputDevice(render_view_id); |
| 152 DCHECK(audio_input_device_); |
| 153 audio_output_device_ = AudioDeviceFactory::NewOutputDevice(render_view_id); |
| 152 DCHECK(audio_output_device_); | 154 DCHECK(audio_output_device_); |
| 153 } | 155 } |
| 154 | 156 |
| 155 WebRtcAudioDeviceImpl::~WebRtcAudioDeviceImpl() { | 157 WebRtcAudioDeviceImpl::~WebRtcAudioDeviceImpl() { |
| 156 DVLOG(1) << "WebRtcAudioDeviceImpl::~WebRtcAudioDeviceImpl()"; | 158 DVLOG(1) << "WebRtcAudioDeviceImpl::~WebRtcAudioDeviceImpl()"; |
| 157 if (playing_) | 159 if (playing_) |
| 158 StopPlayout(); | 160 StopPlayout(); |
| 159 if (recording_) | 161 if (recording_) |
| 160 StopRecording(); | 162 StopRecording(); |
| 161 if (initialized_) | 163 if (initialized_) |
| (...skipping 239 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 401 base::Bind(&WebRtcAudioDeviceImpl::InitOnRenderThread, | 403 base::Bind(&WebRtcAudioDeviceImpl::InitOnRenderThread, |
| 402 this, &error, &event)); | 404 this, &error, &event)); |
| 403 event.Wait(); | 405 event.Wait(); |
| 404 return error; | 406 return error; |
| 405 } | 407 } |
| 406 | 408 |
| 407 // Calling Init() multiple times in a row is OK. | 409 // Calling Init() multiple times in a row is OK. |
| 408 if (initialized_) | 410 if (initialized_) |
| 409 return 0; | 411 return 0; |
| 410 | 412 |
| 411 DCHECK(!audio_input_device_); | |
| 412 DCHECK(!input_buffer_.get()); | 413 DCHECK(!input_buffer_.get()); |
| 413 DCHECK(!output_buffer_.get()); | 414 DCHECK(!output_buffer_.get()); |
| 414 | 415 |
| 415 // TODO(henrika): it could be possible to allow one of the directions (input | 416 // TODO(henrika): it could be possible to allow one of the directions (input |
| 416 // or output) to use a non-supported rate. As an example: if only the | 417 // or output) to use a non-supported rate. As an example: if only the |
| 417 // output rate is OK, we could finalize Init() and only set up an | 418 // output rate is OK, we could finalize Init() and only set up an |
| 418 // AudioOutputDevice. | 419 // AudioOutputDevice. |
| 419 | 420 |
| 420 // Ask the browser for the default audio output hardware sample-rate. | 421 // Ask the browser for the default audio output hardware sample-rate. |
| 421 // This request is based on a synchronous IPC message. | 422 // This request is based on a synchronous IPC message. |
| (...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 565 // after a successful initialization. | 566 // after a successful initialization. |
| 566 output_audio_parameters_.Reset( | 567 output_audio_parameters_.Reset( |
| 567 AudioParameters::AUDIO_PCM_LOW_LATENCY, out_channel_layout, | 568 AudioParameters::AUDIO_PCM_LOW_LATENCY, out_channel_layout, |
| 568 out_sample_rate, 16, out_buffer_size); | 569 out_sample_rate, 16, out_buffer_size); |
| 569 | 570 |
| 570 input_audio_parameters_.Reset( | 571 input_audio_parameters_.Reset( |
| 571 in_format, in_channel_layout, in_sample_rate, | 572 in_format, in_channel_layout, in_sample_rate, |
| 572 16, in_buffer_size); | 573 16, in_buffer_size); |
| 573 | 574 |
| 574 // Create and configure the audio capturing client. | 575 // Create and configure the audio capturing client. |
| 575 audio_input_device_ = AudioDeviceFactory::NewInputDevice(); | |
| 576 audio_input_device_->Initialize(input_audio_parameters_, this, this); | 576 audio_input_device_->Initialize(input_audio_parameters_, this, this); |
| 577 | 577 |
| 578 UMA_HISTOGRAM_ENUMERATION("WebRTC.AudioOutputChannelLayout", | 578 UMA_HISTOGRAM_ENUMERATION("WebRTC.AudioOutputChannelLayout", |
| 579 out_channel_layout, media::CHANNEL_LAYOUT_MAX); | 579 out_channel_layout, media::CHANNEL_LAYOUT_MAX); |
| 580 UMA_HISTOGRAM_ENUMERATION("WebRTC.AudioInputChannelLayout", | 580 UMA_HISTOGRAM_ENUMERATION("WebRTC.AudioInputChannelLayout", |
| 581 in_channel_layout, media::CHANNEL_LAYOUT_MAX); | 581 in_channel_layout, media::CHANNEL_LAYOUT_MAX); |
| 582 AddHistogramFramesPerBuffer(kAudioOutput, out_buffer_size); | 582 AddHistogramFramesPerBuffer(kAudioOutput, out_buffer_size); |
| 583 AddHistogramFramesPerBuffer(kAudioInput, in_buffer_size); | 583 AddHistogramFramesPerBuffer(kAudioInput, in_buffer_size); |
| 584 | 584 |
| 585 // Configure the audio rendering client. | 585 // Configure the audio rendering client. |
| 586 audio_output_device_->Initialize(output_audio_parameters_, this); | 586 audio_output_device_->Initialize(output_audio_parameters_, this); |
| 587 | 587 |
| 588 DCHECK(audio_input_device_); | |
| 589 | |
| 590 // Allocate local audio buffers based on the parameters above. | 588 // Allocate local audio buffers based on the parameters above. |
| 591 // It is assumed that each audio sample contains 16 bits and each | 589 // It is assumed that each audio sample contains 16 bits and each |
| 592 // audio frame contains one or two audio samples depending on the | 590 // audio frame contains one or two audio samples depending on the |
| 593 // number of channels. | 591 // number of channels. |
| 594 input_buffer_.reset(new int16[input_buffer_size() * input_channels()]); | 592 input_buffer_.reset(new int16[input_buffer_size() * input_channels()]); |
| 595 output_buffer_.reset(new int16[output_buffer_size() * output_channels()]); | 593 output_buffer_.reset(new int16[output_buffer_size() * output_channels()]); |
| 596 | 594 |
| 597 DCHECK(input_buffer_.get()); | 595 DCHECK(input_buffer_.get()); |
| 598 DCHECK(output_buffer_.get()); | 596 DCHECK(output_buffer_.get()); |
| 599 | 597 |
| (...skipping 17 matching lines...) Expand all Loading... |
| 617 event->Signal(); | 615 event->Signal(); |
| 618 } | 616 } |
| 619 | 617 |
| 620 int32_t WebRtcAudioDeviceImpl::Terminate() { | 618 int32_t WebRtcAudioDeviceImpl::Terminate() { |
| 621 DVLOG(1) << "Terminate()"; | 619 DVLOG(1) << "Terminate()"; |
| 622 | 620 |
| 623 // Calling Terminate() multiple times in a row is OK. | 621 // Calling Terminate() multiple times in a row is OK. |
| 624 if (!initialized_) | 622 if (!initialized_) |
| 625 return 0; | 623 return 0; |
| 626 | 624 |
| 627 DCHECK(audio_input_device_); | |
| 628 DCHECK(input_buffer_.get()); | 625 DCHECK(input_buffer_.get()); |
| 629 DCHECK(output_buffer_.get()); | 626 DCHECK(output_buffer_.get()); |
| 630 | 627 |
| 631 // Release all resources allocated in Init(). | 628 // Release all resources allocated in Init(). |
| 632 audio_input_device_ = NULL; | |
| 633 input_buffer_.reset(); | 629 input_buffer_.reset(); |
| 634 output_buffer_.reset(); | 630 output_buffer_.reset(); |
| 635 | 631 |
| 636 initialized_ = false; | 632 initialized_ = false; |
| 637 return 0; | 633 return 0; |
| 638 } | 634 } |
| 639 | 635 |
| 640 bool WebRtcAudioDeviceImpl::Initialized() const { | 636 bool WebRtcAudioDeviceImpl::Initialized() const { |
| 641 return initialized_; | 637 return initialized_; |
| 642 } | 638 } |
| (...skipping 514 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1157 } | 1153 } |
| 1158 | 1154 |
| 1159 int32_t WebRtcAudioDeviceImpl::GetLoudspeakerStatus(bool* enabled) const { | 1155 int32_t WebRtcAudioDeviceImpl::GetLoudspeakerStatus(bool* enabled) const { |
| 1160 NOTIMPLEMENTED(); | 1156 NOTIMPLEMENTED(); |
| 1161 return -1; | 1157 return -1; |
| 1162 } | 1158 } |
| 1163 | 1159 |
| 1164 void WebRtcAudioDeviceImpl::SetSessionId(int session_id) { | 1160 void WebRtcAudioDeviceImpl::SetSessionId(int session_id) { |
| 1165 session_id_ = session_id; | 1161 session_id_ = session_id; |
| 1166 } | 1162 } |
| OLD | NEW |