| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "content/renderer/media/webaudio_capturer_source.h" | |
| 6 | |
| 7 #include "base/logging.h" | |
| 8 #include "base/time/time.h" | |
| 9 #include "content/renderer/media/webrtc_local_audio_track.h" | |
| 10 | |
| 11 using media::AudioBus; | |
| 12 using media::AudioFifo; | |
| 13 using media::AudioParameters; | |
| 14 using media::ChannelLayout; | |
| 15 using media::CHANNEL_LAYOUT_MONO; | |
| 16 using media::CHANNEL_LAYOUT_STEREO; | |
| 17 | |
| 18 static const int kMaxNumberOfBuffersInFifo = 5; | |
| 19 | |
| 20 namespace content { | |
| 21 | |
| 22 WebAudioCapturerSource::WebAudioCapturerSource( | |
| 23 const blink::WebMediaStreamSource& blink_source) | |
| 24 : track_(NULL), | |
| 25 audio_format_changed_(false), | |
| 26 blink_source_(blink_source) { | |
| 27 } | |
| 28 | |
| 29 WebAudioCapturerSource::~WebAudioCapturerSource() { | |
| 30 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 31 removeFromBlinkSource(); | |
| 32 } | |
| 33 | |
| 34 void WebAudioCapturerSource::setFormat( | |
| 35 size_t number_of_channels, float sample_rate) { | |
| 36 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 37 DVLOG(1) << "WebAudioCapturerSource::setFormat(sample_rate=" | |
| 38 << sample_rate << ")"; | |
| 39 | |
| 40 // If the channel count is greater than 8, use discrete layout. However, | |
| 41 // anything beyond 8 is ignored by the subsequent (WebRTC) audio pipeline. | |
| 42 ChannelLayout channel_layout = | |
| 43 number_of_channels > 8 ? media::CHANNEL_LAYOUT_DISCRETE | |
| 44 : media::GuessChannelLayout(number_of_channels); | |
| 45 | |
| 46 base::AutoLock auto_lock(lock_); | |
| 47 | |
| 48 // Set the format used by this WebAudioCapturerSource. We are using 10ms data | |
| 49 // as buffer size since that is the native buffer size of WebRtc packet | |
| 50 // running on. | |
| 51 params_.Reset(media::AudioParameters::AUDIO_PCM_LOW_LATENCY, channel_layout, | |
| 52 sample_rate, 16, sample_rate / 100); | |
| 53 | |
| 54 // Take care of the discrete channel layout case. | |
| 55 params_.set_channels_for_discrete(number_of_channels); | |
| 56 | |
| 57 audio_format_changed_ = true; | |
| 58 | |
| 59 wrapper_bus_ = AudioBus::CreateWrapper(params_.channels()); | |
| 60 capture_bus_ = AudioBus::Create(params_); | |
| 61 | |
| 62 fifo_.reset(new AudioFifo( | |
| 63 params_.channels(), | |
| 64 kMaxNumberOfBuffersInFifo * params_.frames_per_buffer())); | |
| 65 } | |
| 66 | |
| 67 void WebAudioCapturerSource::Start(WebRtcLocalAudioTrack* track) { | |
| 68 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 69 DCHECK(track); | |
| 70 base::AutoLock auto_lock(lock_); | |
| 71 track_ = track; | |
| 72 } | |
| 73 | |
| 74 void WebAudioCapturerSource::Stop() { | |
| 75 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 76 { | |
| 77 base::AutoLock auto_lock(lock_); | |
| 78 track_ = NULL; | |
| 79 } | |
| 80 // removeFromBlinkSource() should not be called while |lock_| is acquired, | |
| 81 // as it could result in a deadlock. | |
| 82 removeFromBlinkSource(); | |
| 83 } | |
| 84 | |
| 85 void WebAudioCapturerSource::consumeAudio( | |
| 86 const blink::WebVector<const float*>& audio_data, | |
| 87 size_t number_of_frames) { | |
| 88 base::AutoLock auto_lock(lock_); | |
| 89 if (!track_) | |
| 90 return; | |
| 91 | |
| 92 // Update the downstream client if the audio format has been changed. | |
| 93 if (audio_format_changed_) { | |
| 94 track_->OnSetFormat(params_); | |
| 95 audio_format_changed_ = false; | |
| 96 } | |
| 97 | |
| 98 wrapper_bus_->set_frames(number_of_frames); | |
| 99 | |
| 100 // Make sure WebKit is honoring what it told us up front | |
| 101 // about the channels. | |
| 102 DCHECK_EQ(params_.channels(), static_cast<int>(audio_data.size())); | |
| 103 | |
| 104 for (size_t i = 0; i < audio_data.size(); ++i) | |
| 105 wrapper_bus_->SetChannelData(i, const_cast<float*>(audio_data[i])); | |
| 106 | |
| 107 // Handle mismatch between WebAudio buffer-size and WebRTC. | |
| 108 int available = fifo_->max_frames() - fifo_->frames(); | |
| 109 if (available < static_cast<int>(number_of_frames)) { | |
| 110 NOTREACHED() << "WebAudioCapturerSource::Consume() : FIFO overrun."; | |
| 111 return; | |
| 112 } | |
| 113 | |
| 114 // Compute the estimated capture time of the first sample frame of audio that | |
| 115 // will be consumed from the FIFO in the loop below. | |
| 116 base::TimeTicks estimated_capture_time = base::TimeTicks::Now() - | |
| 117 fifo_->frames() * base::TimeDelta::FromSeconds(1) / params_.sample_rate(); | |
| 118 | |
| 119 fifo_->Push(wrapper_bus_.get()); | |
| 120 while (fifo_->frames() >= capture_bus_->frames()) { | |
| 121 fifo_->Consume(capture_bus_.get(), 0, capture_bus_->frames()); | |
| 122 track_->Capture(*capture_bus_, estimated_capture_time, false); | |
| 123 | |
| 124 // Advance the estimated capture time for the next FIFO consume operation. | |
| 125 estimated_capture_time += | |
| 126 capture_bus_->frames() * base::TimeDelta::FromSeconds(1) / | |
| 127 params_.sample_rate(); | |
| 128 } | |
| 129 } | |
| 130 | |
| 131 // If registered as audio consumer in |blink_source_|, deregister from | |
| 132 // |blink_source_| and stop keeping a reference to |blink_source_|. | |
| 133 // Failure to call this method when stopping the track might leave an invalid | |
| 134 // WebAudioCapturerSource reference still registered as an audio consumer on | |
| 135 // the blink side. | |
| 136 void WebAudioCapturerSource::removeFromBlinkSource() { | |
| 137 if (!blink_source_.isNull()) { | |
| 138 blink_source_.removeAudioConsumer(this); | |
| 139 blink_source_.reset(); | |
| 140 } | |
| 141 } | |
| 142 | |
| 143 } // namespace content | |
| OLD | NEW |