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