Chromium Code Reviews| 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_local_audio_track.h" | 5 #include "content/renderer/media/webrtc_local_audio_track.h" |
| 6 | 6 |
| 7 #include "content/renderer/media/webaudio_capturer_source.h" | 7 #include "content/renderer/media/webaudio_capturer_source.h" |
| 8 #include "content/renderer/media/webrtc_audio_capturer.h" | 8 #include "content/renderer/media/webrtc_audio_capturer.h" |
| 9 #include "content/renderer/media/webrtc_audio_capturer_sink_owner.h" | 9 #include "content/renderer/media/webrtc_audio_capturer_sink_owner.h" |
| 10 #include "content/renderer/media/webrtc_local_audio_source_provider.h" | 10 #include "content/renderer/media/webrtc_local_audio_source_provider.h" |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 50 return false; | 50 return false; |
| 51 } | 51 } |
| 52 | 52 |
| 53 } // namespace. | 53 } // namespace. |
| 54 | 54 |
| 55 // This is a temporary audio buffer with parameters used to send data to | 55 // This is a temporary audio buffer with parameters used to send data to |
| 56 // callbacks. | 56 // callbacks. |
| 57 class WebRtcLocalAudioTrack::ConfiguredBuffer : | 57 class WebRtcLocalAudioTrack::ConfiguredBuffer : |
| 58 public base::RefCounted<WebRtcLocalAudioTrack::ConfiguredBuffer> { | 58 public base::RefCounted<WebRtcLocalAudioTrack::ConfiguredBuffer> { |
| 59 public: | 59 public: |
| 60 ConfiguredBuffer() : sink_buffer_size_(0) {} | 60 ConfiguredBuffer() : sink_buffer_size_(0) {} |
|
no longer working on chromium
2013/09/27 12:31:52
, zero_output_(false)
| |
| 61 | 61 |
| 62 void Initialize(const media::AudioParameters& params) { | 62 void Initialize(const media::AudioParameters& params) { |
| 63 DCHECK(params.IsValid()); | 63 DCHECK(params.IsValid()); |
| 64 params_ = params; | 64 params_ = params; |
| 65 | 65 |
| 66 // Use 10ms as the sink buffer size since that is the native packet size | 66 // Use 10ms as the sink buffer size since that is the native packet size |
| 67 // WebRtc is running on. | 67 // WebRtc is running on. |
| 68 sink_buffer_size_ = params.sample_rate() / 100; | 68 sink_buffer_size_ = params.sample_rate() / 100; |
| 69 audio_wrapper_ = | 69 audio_wrapper_ = |
| 70 media::AudioBus::Create(params.channels(), sink_buffer_size_); | 70 media::AudioBus::Create(params.channels(), sink_buffer_size_); |
| 71 buffer_.reset(new int16[sink_buffer_size_ * params.channels()]); | 71 buffer_.reset(new int16[sink_buffer_size_ * params.channels()]); |
| 72 | 72 |
| 73 // The size of the FIFO should be at least twice of the source buffer size | 73 // The size of the FIFO should be at least twice of the source buffer size |
| 74 // or twice of the sink buffer size. | 74 // or twice of the sink buffer size. |
| 75 int buffer_size = std::max( | 75 int buffer_size = std::max( |
| 76 kMaxNumberOfBuffersInFifo * params.frames_per_buffer(), | 76 kMaxNumberOfBuffersInFifo * params.frames_per_buffer(), |
| 77 kMaxNumberOfBuffersInFifo * sink_buffer_size_); | 77 kMaxNumberOfBuffersInFifo * sink_buffer_size_); |
| 78 fifo_.reset(new media::AudioFifo(params.channels(), buffer_size)); | 78 fifo_.reset(new media::AudioFifo(params.channels(), buffer_size)); |
| 79 } | 79 } |
| 80 | 80 |
| 81 void Push(media::AudioBus* audio_source) { | 81 void Push(media::AudioBus* audio_source) { |
| 82 DCHECK(fifo_->frames() + audio_source->frames() <= fifo_->max_frames()); | 82 DCHECK(fifo_->frames() + audio_source->frames() <= fifo_->max_frames()); |
|
no longer working on chromium
2013/09/27 12:31:52
zero_output_ = false;
| |
| 83 fifo_->Push(audio_source); | 83 fifo_->Push(audio_source); |
| 84 } | 84 } |
| 85 | 85 |
| 86 bool Consume() { | 86 bool Consume() { |
|
no longer working on chromium
2013/09/27 12:31:52
add
if (zero_output_)
return true;
| |
| 87 if (fifo_->frames() < audio_wrapper_->frames()) | 87 if (fifo_->frames() < audio_wrapper_->frames()) |
| 88 return false; | 88 return false; |
| 89 | 89 |
| 90 fifo_->Consume(audio_wrapper_.get(), 0, audio_wrapper_->frames()); | 90 fifo_->Consume(audio_wrapper_.get(), 0, audio_wrapper_->frames()); |
| 91 audio_wrapper_->ToInterleaved(audio_wrapper_->frames(), | 91 audio_wrapper_->ToInterleaved(audio_wrapper_->frames(), |
| 92 params_.bits_per_sample() / 8, | 92 params_.bits_per_sample() / 8, |
| 93 buffer()); | 93 buffer()); |
| 94 return true; | 94 return true; |
| 95 } | 95 } |
| 96 | 96 |
|
no longer working on chromium
2013/09/27 12:31:52
add function:
void ZeroOutput() {
if (zero_outp
| |
| 97 int16* buffer() const { return buffer_.get(); } | 97 int16* buffer() const { return buffer_.get(); } |
| 98 const media::AudioParameters& params() const { return params_; } | 98 const media::AudioParameters& params() const { return params_; } |
| 99 int sink_buffer_size() const { return sink_buffer_size_; } | 99 int sink_buffer_size() const { return sink_buffer_size_; } |
| 100 | 100 |
| 101 private: | 101 private: |
| 102 ~ConfiguredBuffer() {} | 102 ~ConfiguredBuffer() {} |
| 103 friend class base::RefCounted<WebRtcLocalAudioTrack::ConfiguredBuffer>; | 103 friend class base::RefCounted<WebRtcLocalAudioTrack::ConfiguredBuffer>; |
| 104 | 104 |
| 105 media::AudioParameters params_; | 105 media::AudioParameters params_; |
| 106 scoped_ptr<media::AudioBus> audio_wrapper_; | 106 scoped_ptr<media::AudioBus> audio_wrapper_; |
| 107 scoped_ptr<media::AudioFifo> fifo_; | 107 scoped_ptr<media::AudioFifo> fifo_; |
| 108 scoped_ptr<int16[]> buffer_; | 108 scoped_ptr<int16[]> buffer_; |
| 109 int sink_buffer_size_; | 109 int sink_buffer_size_; |
|
no longer working on chromium
2013/09/27 12:31:52
add bool zero_output_;
| |
| 110 }; | 110 }; |
| 111 | 111 |
| 112 scoped_refptr<WebRtcLocalAudioTrack> WebRtcLocalAudioTrack::Create( | 112 scoped_refptr<WebRtcLocalAudioTrack> WebRtcLocalAudioTrack::Create( |
| 113 const std::string& id, | 113 const std::string& id, |
| 114 const scoped_refptr<WebRtcAudioCapturer>& capturer, | 114 const scoped_refptr<WebRtcAudioCapturer>& capturer, |
| 115 WebAudioCapturerSource* webaudio_source, | 115 WebAudioCapturerSource* webaudio_source, |
| 116 webrtc::AudioSourceInterface* track_source, | 116 webrtc::AudioSourceInterface* track_source, |
| 117 const webrtc::MediaConstraintsInterface* constraints) { | 117 const webrtc::MediaConstraintsInterface* constraints) { |
| 118 talk_base::RefCountedObject<WebRtcLocalAudioTrack>* track = | 118 talk_base::RefCountedObject<WebRtcLocalAudioTrack>* track = |
| 119 new talk_base::RefCountedObject<WebRtcLocalAudioTrack>( | 119 new talk_base::RefCountedObject<WebRtcLocalAudioTrack>( |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 146 void WebRtcLocalAudioTrack::Capture(media::AudioBus* audio_source, | 146 void WebRtcLocalAudioTrack::Capture(media::AudioBus* audio_source, |
| 147 int audio_delay_milliseconds, | 147 int audio_delay_milliseconds, |
| 148 int volume, | 148 int volume, |
| 149 bool key_pressed) { | 149 bool key_pressed) { |
| 150 scoped_refptr<WebRtcAudioCapturer> capturer; | 150 scoped_refptr<WebRtcAudioCapturer> capturer; |
| 151 std::vector<int> voe_channels; | 151 std::vector<int> voe_channels; |
| 152 int sample_rate = 0; | 152 int sample_rate = 0; |
| 153 int number_of_channels = 0; | 153 int number_of_channels = 0; |
| 154 int number_of_frames = 0; | 154 int number_of_frames = 0; |
| 155 SinkList sinks; | 155 SinkList sinks; |
| 156 scoped_refptr<ConfiguredBuffer> current_buffer; | 156 scoped_refptr<ConfiguredBuffer> current_buffer; |
|
no longer working on chromium
2013/09/27 12:31:52
add bool enabled = false;
| |
| 157 { | 157 { |
| 158 base::AutoLock auto_lock(lock_); | 158 base::AutoLock auto_lock(lock_); |
| 159 // When the track is disabled, we simply return here. | 159 // When the track is disabled, we simply return here. |
| 160 // TODO(xians): Figure out if we should feed zero to sinks instead, in | 160 // TODO(xians): Figure out if we should feed zero to sinks instead, in |
| 161 // order to inject VAD data in such case. | 161 // order to inject VAD data in such case. |
| 162 if (!enabled()) | 162 if (!enabled()) { |
|
no longer working on chromium
2013/09/27 12:31:52
remove this if check
| |
| 163 return; | 163 volume = 0; |
|
no longer working on chromium
2013/09/26 20:52:31
This is wrong, it will confuse the APM that the cu
| |
| 164 } | |
| 164 | 165 |
| 165 capturer = capturer_; | 166 capturer = capturer_; |
| 166 voe_channels = voe_channels_; | 167 voe_channels = voe_channels_; |
| 167 current_buffer = buffer_; | 168 current_buffer = buffer_; |
| 168 sample_rate = current_buffer->params().sample_rate(); | 169 sample_rate = current_buffer->params().sample_rate(); |
| 169 number_of_channels = current_buffer->params().channels(); | 170 number_of_channels = current_buffer->params().channels(); |
| 170 number_of_frames = current_buffer->sink_buffer_size(); | 171 number_of_frames = current_buffer->sink_buffer_size(); |
| 171 sinks = sinks_; | 172 sinks = sinks_; |
|
no longer working on chromium
2013/09/27 12:31:52
enabled = enabled();
| |
| 172 } | 173 } |
| 173 | 174 |
| 174 // Push the data to the fifo. | 175 // Push the data to the fifo. |
| 175 current_buffer->Push(audio_source); | 176 current_buffer->Push(audio_source); |
|
no longer working on chromium
2013/09/27 12:31:52
if (enabled) {
// Push the data to the fifo.
c
| |
| 176 // Only turn off the audio processing when the constrain is set to false as | 177 // Only turn off the audio processing when the constrain is set to false as |
| 177 // well as there is no correct delay value. | 178 // well as there is no correct delay value. |
| 178 bool need_audio_processing = need_audio_processing_ ? | 179 bool need_audio_processing = need_audio_processing_ ? |
| 179 need_audio_processing_ : (audio_delay_milliseconds != 0); | 180 need_audio_processing_ : (audio_delay_milliseconds != 0); |
| 180 int current_volume = volume; | 181 int current_volume = volume; |
| 181 while (current_buffer->Consume()) { | 182 while (current_buffer->Consume()) { |
| 182 // Feed the data to the sinks. | 183 // Feed the data to the sinks. |
| 183 for (SinkList::const_iterator it = sinks.begin(); it != sinks.end(); ++it) { | 184 for (SinkList::const_iterator it = sinks.begin(); it != sinks.end(); ++it) { |
| 184 int new_volume = (*it)->CaptureData(voe_channels, | 185 int new_volume = (*it)->CaptureData(voe_channels, |
| 185 current_buffer->buffer(), | 186 current_buffer->buffer(), |
| 186 sample_rate, | 187 sample_rate, |
| 187 number_of_channels, | 188 number_of_channels, |
| 188 number_of_frames, | 189 number_of_frames, |
| 189 audio_delay_milliseconds, | 190 audio_delay_milliseconds, |
| 190 current_volume, | 191 current_volume, |
| 191 need_audio_processing, | 192 need_audio_processing, |
| 192 key_pressed); | 193 key_pressed); |
|
no longer working on chromium
2013/09/27 12:31:52
if (!enabled)
break;
| |
| 193 if (new_volume != 0 && capturer.get()) { | 194 if (new_volume != 0 && capturer.get()) { |
| 194 // Feed the new volume to WebRtc while changing the volume on the | 195 // Feed the new volume to WebRtc while changing the volume on the |
| 195 // browser. | 196 // browser. |
| 196 capturer->SetVolume(new_volume); | 197 capturer->SetVolume(new_volume); |
| 197 current_volume = new_volume; | 198 current_volume = new_volume; |
| 198 } | 199 } |
| 199 } | 200 } |
| 200 } | 201 } |
| 201 } | 202 } |
| 202 | 203 |
| (...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 340 sinks = sinks_; | 341 sinks = sinks_; |
| 341 webaudio_source_ = NULL; | 342 webaudio_source_ = NULL; |
| 342 capturer_ = NULL; | 343 capturer_ = NULL; |
| 343 } | 344 } |
| 344 | 345 |
| 345 for (SinkList::const_iterator it = sinks.begin(); it != sinks.end(); ++it) | 346 for (SinkList::const_iterator it = sinks.begin(); it != sinks.end(); ++it) |
| 346 (*it)->Reset(); | 347 (*it)->Reset(); |
| 347 } | 348 } |
| 348 | 349 |
| 349 } // namespace content | 350 } // namespace content |
| OLD | NEW |