| 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/webaudio_capturer_source.h" | 5 #include "content/renderer/media/webaudio_capturer_source.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/time/time.h" | 8 #include "base/time/time.h" |
| 9 #include "content/renderer/media/webrtc_audio_capturer.h" | |
| 10 #include "content/renderer/media/webrtc_local_audio_track.h" | 9 #include "content/renderer/media/webrtc_local_audio_track.h" |
| 11 | 10 |
| 12 using media::AudioBus; | 11 using media::AudioBus; |
| 13 using media::AudioFifo; | 12 using media::AudioFifo; |
| 14 using media::AudioParameters; | 13 using media::AudioParameters; |
| 15 using media::ChannelLayout; | 14 using media::ChannelLayout; |
| 16 using media::CHANNEL_LAYOUT_MONO; | 15 using media::CHANNEL_LAYOUT_MONO; |
| 17 using media::CHANNEL_LAYOUT_STEREO; | 16 using media::CHANNEL_LAYOUT_STEREO; |
| 18 | 17 |
| 19 static const int kMaxNumberOfBuffersInFifo = 5; | 18 static const int kMaxNumberOfBuffersInFifo = 5; |
| 20 | 19 |
| 21 namespace content { | 20 namespace content { |
| 22 | 21 |
| 23 WebAudioCapturerSource::WebAudioCapturerSource() | 22 WebAudioCapturerSource::WebAudioCapturerSource() |
| 24 : track_(NULL), | 23 : track_(NULL), |
| 25 capturer_(NULL), | |
| 26 audio_format_changed_(false) { | 24 audio_format_changed_(false) { |
| 27 } | 25 } |
| 28 | 26 |
| 29 WebAudioCapturerSource::~WebAudioCapturerSource() { | 27 WebAudioCapturerSource::~WebAudioCapturerSource() { |
| 30 } | 28 } |
| 31 | 29 |
| 32 void WebAudioCapturerSource::setFormat( | 30 void WebAudioCapturerSource::setFormat( |
| 33 size_t number_of_channels, float sample_rate) { | 31 size_t number_of_channels, float sample_rate) { |
| 34 DCHECK(thread_checker_.CalledOnValidThread()); | 32 DCHECK(thread_checker_.CalledOnValidThread()); |
| 35 DVLOG(1) << "WebAudioCapturerSource::setFormat(sample_rate=" | 33 DVLOG(1) << "WebAudioCapturerSource::setFormat(sample_rate=" |
| (...skipping 18 matching lines...) Expand all Loading... |
| 54 | 52 |
| 55 wrapper_bus_ = AudioBus::CreateWrapper(params_.channels()); | 53 wrapper_bus_ = AudioBus::CreateWrapper(params_.channels()); |
| 56 capture_bus_ = AudioBus::Create(params_); | 54 capture_bus_ = AudioBus::Create(params_); |
| 57 audio_data_.reset( | 55 audio_data_.reset( |
| 58 new int16[params_.frames_per_buffer() * params_.channels()]); | 56 new int16[params_.frames_per_buffer() * params_.channels()]); |
| 59 fifo_.reset(new AudioFifo( | 57 fifo_.reset(new AudioFifo( |
| 60 params_.channels(), | 58 params_.channels(), |
| 61 kMaxNumberOfBuffersInFifo * params_.frames_per_buffer())); | 59 kMaxNumberOfBuffersInFifo * params_.frames_per_buffer())); |
| 62 } | 60 } |
| 63 | 61 |
| 64 void WebAudioCapturerSource::Start( | 62 void WebAudioCapturerSource::Start(WebRtcLocalAudioTrack* track) { |
| 65 WebRtcLocalAudioTrack* track, WebRtcAudioCapturer* capturer) { | |
| 66 DCHECK(thread_checker_.CalledOnValidThread()); | 63 DCHECK(thread_checker_.CalledOnValidThread()); |
| 67 DCHECK(track); | 64 DCHECK(track); |
| 68 base::AutoLock auto_lock(lock_); | 65 base::AutoLock auto_lock(lock_); |
| 69 track_ = track; | 66 track_ = track; |
| 70 capturer_ = capturer; | |
| 71 } | 67 } |
| 72 | 68 |
| 73 void WebAudioCapturerSource::Stop() { | 69 void WebAudioCapturerSource::Stop() { |
| 74 DCHECK(thread_checker_.CalledOnValidThread()); | 70 DCHECK(thread_checker_.CalledOnValidThread()); |
| 75 base::AutoLock auto_lock(lock_); | 71 base::AutoLock auto_lock(lock_); |
| 76 track_ = NULL; | 72 track_ = NULL; |
| 77 capturer_ = NULL; | |
| 78 } | 73 } |
| 79 | 74 |
| 80 void WebAudioCapturerSource::consumeAudio( | 75 void WebAudioCapturerSource::consumeAudio( |
| 81 const blink::WebVector<const float*>& audio_data, | 76 const blink::WebVector<const float*>& audio_data, |
| 82 size_t number_of_frames) { | 77 size_t number_of_frames) { |
| 83 base::AutoLock auto_lock(lock_); | 78 base::AutoLock auto_lock(lock_); |
| 84 if (!track_) | 79 if (!track_) |
| 85 return; | 80 return; |
| 86 | 81 |
| 87 // Update the downstream client if the audio format has been changed. | 82 // Update the downstream client if the audio format has been changed. |
| (...skipping 13 matching lines...) Expand all Loading... |
| 101 | 96 |
| 102 // Handle mismatch between WebAudio buffer-size and WebRTC. | 97 // Handle mismatch between WebAudio buffer-size and WebRTC. |
| 103 int available = fifo_->max_frames() - fifo_->frames(); | 98 int available = fifo_->max_frames() - fifo_->frames(); |
| 104 if (available < static_cast<int>(number_of_frames)) { | 99 if (available < static_cast<int>(number_of_frames)) { |
| 105 NOTREACHED() << "WebAudioCapturerSource::Consume() : FIFO overrun."; | 100 NOTREACHED() << "WebAudioCapturerSource::Consume() : FIFO overrun."; |
| 106 return; | 101 return; |
| 107 } | 102 } |
| 108 | 103 |
| 109 fifo_->Push(wrapper_bus_.get()); | 104 fifo_->Push(wrapper_bus_.get()); |
| 110 int capture_frames = params_.frames_per_buffer(); | 105 int capture_frames = params_.frames_per_buffer(); |
| 111 base::TimeDelta delay; | |
| 112 int volume = 0; | |
| 113 bool key_pressed = false; | |
| 114 if (capturer_) { | |
| 115 capturer_->GetAudioProcessingParams(&delay, &volume, &key_pressed); | |
| 116 } | |
| 117 | |
| 118 // Turn off audio processing if the delay value is 0, since in such case, | |
| 119 // it indicates the data is not from microphone. | |
| 120 // TODO(xians): remove the flag when supporting one APM per audio track. | |
| 121 // See crbug/264611 for details. | |
| 122 bool need_audio_processing = (delay.InMilliseconds() != 0); | |
| 123 while (fifo_->frames() >= capture_frames) { | 106 while (fifo_->frames() >= capture_frames) { |
| 124 fifo_->Consume(capture_bus_.get(), 0, capture_frames); | 107 fifo_->Consume(capture_bus_.get(), 0, capture_frames); |
| 125 // TODO(xians): Avoid this interleave/deinterleave operation. | 108 // TODO(xians): Avoid this interleave/deinterleave operation. |
| 126 capture_bus_->ToInterleaved(capture_bus_->frames(), | 109 capture_bus_->ToInterleaved(capture_bus_->frames(), |
| 127 params_.bits_per_sample() / 8, | 110 params_.bits_per_sample() / 8, |
| 128 audio_data_.get()); | 111 audio_data_.get()); |
| 129 track_->Capture(audio_data_.get(), delay, volume, key_pressed, | 112 track_->Capture(audio_data_.get(), false); |
| 130 need_audio_processing, false); | |
| 131 } | 113 } |
| 132 } | 114 } |
| 133 | 115 |
| 134 } // namespace content | 116 } // namespace content |
| OLD | NEW |