| 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" |
| 11 #include "content/renderer/media/audio_device_factory.h" |
| 11 #include "content/renderer/media/audio_hardware.h" | 12 #include "content/renderer/media/audio_hardware.h" |
| 12 #include "content/renderer/render_thread_impl.h" | 13 #include "content/renderer/render_thread_impl.h" |
| 13 #include "media/audio/audio_util.h" | 14 #include "media/audio/audio_util.h" |
| 14 #include "media/audio/audio_parameters.h" | 15 #include "media/audio/audio_parameters.h" |
| 15 #include "media/audio/sample_rates.h" | 16 #include "media/audio/sample_rates.h" |
| 16 | 17 |
| 18 using content::AudioDeviceFactory; |
| 17 using media::AudioParameters; | 19 using media::AudioParameters; |
| 18 | 20 |
| 19 static const int64 kMillisecondsBetweenProcessCalls = 5000; | 21 static const int64 kMillisecondsBetweenProcessCalls = 5000; |
| 20 static const double kMaxVolumeLevel = 255.0; | 22 static const double kMaxVolumeLevel = 255.0; |
| 21 | 23 |
| 22 // Supported hardware sample rates for input and output sides. | 24 // Supported hardware sample rates for input and output sides. |
| 23 #if defined(OS_WIN) || defined(OS_MACOSX) | 25 #if defined(OS_WIN) || defined(OS_MACOSX) |
| 24 // media::GetAudioInput[Output]HardwareSampleRate() asks the audio layer | 26 // media::GetAudioInput[Output]HardwareSampleRate() asks the audio layer |
| 25 // for its current sample rate (set by the user) on Windows and Mac OS X. | 27 // for its current sample rate (set by the user) on Windows and Mac OS X. |
| 26 // The listed rates below adds restrictions and WebRtcAudioDeviceImpl::Init() | 28 // The listed rates below adds restrictions and WebRtcAudioDeviceImpl::Init() |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 133 input_delay_ms_(0), | 135 input_delay_ms_(0), |
| 134 output_delay_ms_(0), | 136 output_delay_ms_(0), |
| 135 last_error_(AudioDeviceModule::kAdmErrNone), | 137 last_error_(AudioDeviceModule::kAdmErrNone), |
| 136 last_process_time_(base::TimeTicks::Now()), | 138 last_process_time_(base::TimeTicks::Now()), |
| 137 session_id_(0), | 139 session_id_(0), |
| 138 bytes_per_sample_(0), | 140 bytes_per_sample_(0), |
| 139 initialized_(false), | 141 initialized_(false), |
| 140 playing_(false), | 142 playing_(false), |
| 141 recording_(false), | 143 recording_(false), |
| 142 agc_is_enabled_(false) { | 144 agc_is_enabled_(false) { |
| 143 DVLOG(1) << "WebRtcAudioDeviceImpl::WebRtcAudioDeviceImpl()"; | 145 DVLOG(1) << "WebRtcAudioDeviceImpl::WebRtcAudioDeviceImpl()"; |
| 144 DCHECK(RenderThreadImpl::current()) << | 146 // TODO(henrika): remove this restriction when factory is used for the |
| 145 "WebRtcAudioDeviceImpl must be constructed on the render thread"; | 147 // input side as well. |
| 148 DCHECK(RenderThreadImpl::current()) << |
| 149 "WebRtcAudioDeviceImpl must be constructed on the render thread"; |
| 150 audio_output_device_ = AudioDeviceFactory::Create(); |
| 151 DCHECK(audio_output_device_); |
| 146 } | 152 } |
| 147 | 153 |
| 148 WebRtcAudioDeviceImpl::~WebRtcAudioDeviceImpl() { | 154 WebRtcAudioDeviceImpl::~WebRtcAudioDeviceImpl() { |
| 149 DVLOG(1) << "WebRtcAudioDeviceImpl::~WebRtcAudioDeviceImpl()"; | 155 DVLOG(1) << "WebRtcAudioDeviceImpl::~WebRtcAudioDeviceImpl()"; |
| 150 if (playing_) | 156 if (playing_) |
| 151 StopPlayout(); | 157 StopPlayout(); |
| 152 if (recording_) | 158 if (recording_) |
| 153 StopRecording(); | 159 StopRecording(); |
| 154 if (initialized_) | 160 if (initialized_) |
| 155 Terminate(); | 161 Terminate(); |
| (...skipping 247 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 403 this, &error, &event)); | 409 this, &error, &event)); |
| 404 event.Wait(); | 410 event.Wait(); |
| 405 return error; | 411 return error; |
| 406 } | 412 } |
| 407 | 413 |
| 408 // Calling Init() multiple times in a row is OK. | 414 // Calling Init() multiple times in a row is OK. |
| 409 if (initialized_) | 415 if (initialized_) |
| 410 return 0; | 416 return 0; |
| 411 | 417 |
| 412 DCHECK(!audio_input_device_); | 418 DCHECK(!audio_input_device_); |
| 413 DCHECK(!audio_output_device_); | |
| 414 DCHECK(!input_buffer_.get()); | 419 DCHECK(!input_buffer_.get()); |
| 415 DCHECK(!output_buffer_.get()); | 420 DCHECK(!output_buffer_.get()); |
| 416 | 421 |
| 417 // TODO(henrika): it could be possible to allow one of the directions (input | 422 // TODO(henrika): it could be possible to allow one of the directions (input |
| 418 // or output) to use a non-supported rate. As an example: if only the | 423 // or output) to use a non-supported rate. As an example: if only the |
| 419 // output rate is OK, we could finalize Init() and only set up an AudioDevice. | 424 // output rate is OK, we could finalize Init() and only set up an AudioDevice. |
| 420 | 425 |
| 421 // Ask the browser for the default audio output hardware sample-rate. | 426 // Ask the browser for the default audio output hardware sample-rate. |
| 422 // This request is based on a synchronous IPC message. | 427 // This request is based on a synchronous IPC message. |
| 423 int out_sample_rate = audio_hardware::GetOutputSampleRate(); | 428 int out_sample_rate = audio_hardware::GetOutputSampleRate(); |
| (...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 576 audio_input_device_ = new AudioInputDevice( | 581 audio_input_device_ = new AudioInputDevice( |
| 577 input_audio_parameters_, this, this); | 582 input_audio_parameters_, this, this); |
| 578 | 583 |
| 579 UMA_HISTOGRAM_ENUMERATION("WebRTC.AudioOutputChannelLayout", | 584 UMA_HISTOGRAM_ENUMERATION("WebRTC.AudioOutputChannelLayout", |
| 580 out_channel_layout, CHANNEL_LAYOUT_MAX); | 585 out_channel_layout, CHANNEL_LAYOUT_MAX); |
| 581 UMA_HISTOGRAM_ENUMERATION("WebRTC.AudioInputChannelLayout", | 586 UMA_HISTOGRAM_ENUMERATION("WebRTC.AudioInputChannelLayout", |
| 582 in_channel_layout, CHANNEL_LAYOUT_MAX); | 587 in_channel_layout, CHANNEL_LAYOUT_MAX); |
| 583 AddHistogramFramesPerBuffer(kAudioOutput, out_buffer_size); | 588 AddHistogramFramesPerBuffer(kAudioOutput, out_buffer_size); |
| 584 AddHistogramFramesPerBuffer(kAudioInput, in_buffer_size); | 589 AddHistogramFramesPerBuffer(kAudioInput, in_buffer_size); |
| 585 | 590 |
| 586 // Create and configure the audio rendering client. | 591 // Configure the audio rendering client. |
| 587 audio_output_device_ = new AudioDevice(output_audio_parameters_, this); | 592 audio_output_device_->Initialize(output_audio_parameters_, this); |
| 588 | 593 |
| 589 DCHECK(audio_input_device_); | 594 DCHECK(audio_input_device_); |
| 590 DCHECK(audio_output_device_); | |
| 591 | 595 |
| 592 // Allocate local audio buffers based on the parameters above. | 596 // Allocate local audio buffers based on the parameters above. |
| 593 // It is assumed that each audio sample contains 16 bits and each | 597 // It is assumed that each audio sample contains 16 bits and each |
| 594 // audio frame contains one or two audio samples depending on the | 598 // audio frame contains one or two audio samples depending on the |
| 595 // number of channels. | 599 // number of channels. |
| 596 input_buffer_.reset(new int16[input_buffer_size() * input_channels()]); | 600 input_buffer_.reset(new int16[input_buffer_size() * input_channels()]); |
| 597 output_buffer_.reset(new int16[output_buffer_size() * output_channels()]); | 601 output_buffer_.reset(new int16[output_buffer_size() * output_channels()]); |
| 598 | 602 |
| 599 DCHECK(input_buffer_.get()); | 603 DCHECK(input_buffer_.get()); |
| 600 DCHECK(output_buffer_.get()); | 604 DCHECK(output_buffer_.get()); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 620 } | 624 } |
| 621 | 625 |
| 622 int32_t WebRtcAudioDeviceImpl::Terminate() { | 626 int32_t WebRtcAudioDeviceImpl::Terminate() { |
| 623 DVLOG(1) << "Terminate()"; | 627 DVLOG(1) << "Terminate()"; |
| 624 | 628 |
| 625 // Calling Terminate() multiple times in a row is OK. | 629 // Calling Terminate() multiple times in a row is OK. |
| 626 if (!initialized_) | 630 if (!initialized_) |
| 627 return 0; | 631 return 0; |
| 628 | 632 |
| 629 DCHECK(audio_input_device_); | 633 DCHECK(audio_input_device_); |
| 630 DCHECK(audio_output_device_); | |
| 631 DCHECK(input_buffer_.get()); | 634 DCHECK(input_buffer_.get()); |
| 632 DCHECK(output_buffer_.get()); | 635 DCHECK(output_buffer_.get()); |
| 633 | 636 |
| 634 // Release all resources allocated in Init(). | 637 // Release all resources allocated in Init(). |
| 635 audio_input_device_ = NULL; | 638 audio_input_device_ = NULL; |
| 636 audio_output_device_ = NULL; | |
| 637 input_buffer_.reset(); | 639 input_buffer_.reset(); |
| 638 output_buffer_.reset(); | 640 output_buffer_.reset(); |
| 639 | 641 |
| 640 initialized_ = false; | 642 initialized_ = false; |
| 641 return 0; | 643 return 0; |
| 642 } | 644 } |
| 643 | 645 |
| 644 bool WebRtcAudioDeviceImpl::Initialized() const { | 646 bool WebRtcAudioDeviceImpl::Initialized() const { |
| 645 return initialized_; | 647 return initialized_; |
| 646 } | 648 } |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 690 } | 692 } |
| 691 | 693 |
| 692 int32_t WebRtcAudioDeviceImpl::SetRecordingDevice(WindowsDeviceType device) { | 694 int32_t WebRtcAudioDeviceImpl::SetRecordingDevice(WindowsDeviceType device) { |
| 693 DVLOG(2) << "WARNING: WebRtcAudioDeviceImpl::SetRecordingDevice() " | 695 DVLOG(2) << "WARNING: WebRtcAudioDeviceImpl::SetRecordingDevice() " |
| 694 << "NOT IMPLEMENTED"; | 696 << "NOT IMPLEMENTED"; |
| 695 return 0; | 697 return 0; |
| 696 } | 698 } |
| 697 | 699 |
| 698 int32_t WebRtcAudioDeviceImpl::PlayoutIsAvailable(bool* available) { | 700 int32_t WebRtcAudioDeviceImpl::PlayoutIsAvailable(bool* available) { |
| 699 DVLOG(1) << "PlayoutIsAvailable()"; | 701 DVLOG(1) << "PlayoutIsAvailable()"; |
| 700 *available = (audio_output_device_ != NULL); | 702 *available = initialized(); |
| 701 return 0; | 703 return 0; |
| 702 } | 704 } |
| 703 | 705 |
| 704 int32_t WebRtcAudioDeviceImpl::InitPlayout() { | 706 int32_t WebRtcAudioDeviceImpl::InitPlayout() { |
| 705 DVLOG(2) << "WARNING: WebRtcAudioDeviceImpl::InitPlayout() " | 707 DVLOG(2) << "WARNING: WebRtcAudioDeviceImpl::InitPlayout() " |
| 706 << "NOT IMPLEMENTED"; | 708 << "NOT IMPLEMENTED"; |
| 707 return 0; | 709 return 0; |
| 708 } | 710 } |
| 709 | 711 |
| 710 bool WebRtcAudioDeviceImpl::PlayoutIsInitialized() const { | 712 bool WebRtcAudioDeviceImpl::PlayoutIsInitialized() const { |
| 711 DVLOG(1) << "PlayoutIsInitialized()"; | 713 DVLOG(1) << "PlayoutIsInitialized()"; |
| 712 return (audio_output_device_ != NULL); | 714 return initialized(); |
| 713 } | 715 } |
| 714 | 716 |
| 715 int32_t WebRtcAudioDeviceImpl::RecordingIsAvailable(bool* available) { | 717 int32_t WebRtcAudioDeviceImpl::RecordingIsAvailable(bool* available) { |
| 716 DVLOG(1) << "RecordingIsAvailable()"; | 718 DVLOG(1) << "RecordingIsAvailable()"; |
| 717 *available = (audio_input_device_ != NULL); | 719 *available = (audio_input_device_ != NULL); |
| 718 return 0; | 720 return 0; |
| 719 } | 721 } |
| 720 | 722 |
| 721 int32_t WebRtcAudioDeviceImpl::InitRecording() { | 723 int32_t WebRtcAudioDeviceImpl::InitRecording() { |
| 722 DVLOG(2) << "WARNING: WebRtcAudioDeviceImpl::InitRecording() " | 724 DVLOG(2) << "WARNING: WebRtcAudioDeviceImpl::InitRecording() " |
| (...skipping 440 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1163 } | 1165 } |
| 1164 | 1166 |
| 1165 int32_t WebRtcAudioDeviceImpl::GetLoudspeakerStatus(bool* enabled) const { | 1167 int32_t WebRtcAudioDeviceImpl::GetLoudspeakerStatus(bool* enabled) const { |
| 1166 NOTIMPLEMENTED(); | 1168 NOTIMPLEMENTED(); |
| 1167 return -1; | 1169 return -1; |
| 1168 } | 1170 } |
| 1169 | 1171 |
| 1170 void WebRtcAudioDeviceImpl::SetSessionId(int session_id) { | 1172 void WebRtcAudioDeviceImpl::SetSessionId(int session_id) { |
| 1171 session_id_ = session_id; | 1173 session_id_ = session_id; |
| 1172 } | 1174 } |
| OLD | NEW |