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/bind.h" |
| 8 #include "base/bind_helpers.h" |
| 9 #include "base/logging.h" |
| 10 #include "content/renderer/media/webrtc_local_audio_track.h" |
| 11 |
| 12 using media::AudioBus; |
| 13 using media::AudioParameters; |
| 14 using media::ChannelLayout; |
| 15 using media::CHANNEL_LAYOUT_MONO; |
| 16 using media::CHANNEL_LAYOUT_STEREO; |
| 17 |
| 18 namespace content { |
| 19 |
| 20 WebAudioCapturerSource::WebAudioCapturerSource( |
| 21 blink::WebMediaStreamSource* blink_source) |
| 22 : track_(NULL), |
| 23 audio_format_changed_(false), |
| 24 fifo_(base::Bind(&WebAudioCapturerSource::DeliverRebufferedAudio, |
| 25 base::Unretained(this))), |
| 26 blink_source_(*blink_source) { |
| 27 DCHECK(blink_source); |
| 28 DCHECK(!blink_source_.isNull()); |
| 29 DVLOG(1) << "WebAudioCapturerSource::WebAudioCapturerSource()"; |
| 30 blink_source_.addAudioConsumer(this); |
| 31 } |
| 32 |
| 33 WebAudioCapturerSource::~WebAudioCapturerSource() { |
| 34 DCHECK(thread_checker_.CalledOnValidThread()); |
| 35 DVLOG(1) << "WebAudioCapturerSource::~WebAudioCapturerSource()"; |
| 36 DeregisterFromBlinkSource(); |
| 37 } |
| 38 |
| 39 void WebAudioCapturerSource::setFormat( |
| 40 size_t number_of_channels, float sample_rate) { |
| 41 DCHECK(thread_checker_.CalledOnValidThread()); |
| 42 DVLOG(1) << "WebAudioCapturerSource::setFormat(sample_rate=" |
| 43 << sample_rate << ")"; |
| 44 |
| 45 // If the channel count is greater than 8, use discrete layout. However, |
| 46 // anything beyond 8 is ignored by the subsequent (WebRTC) audio pipeline. |
| 47 ChannelLayout channel_layout = |
| 48 number_of_channels > 8 ? media::CHANNEL_LAYOUT_DISCRETE |
| 49 : media::GuessChannelLayout(number_of_channels); |
| 50 |
| 51 base::AutoLock auto_lock(lock_); |
| 52 |
| 53 // Set the format used by this WebAudioCapturerSource. We are using 10ms data |
| 54 // as buffer size since that is the native buffer size of WebRtc packet |
| 55 // running on. |
| 56 fifo_.Reset(sample_rate / 100); |
| 57 params_.Reset(media::AudioParameters::AUDIO_PCM_LOW_LATENCY, channel_layout, |
| 58 sample_rate, 16, fifo_.frames_per_buffer()); |
| 59 |
| 60 // Take care of the discrete channel layout case. |
| 61 params_.set_channels_for_discrete(number_of_channels); |
| 62 |
| 63 audio_format_changed_ = true; |
| 64 |
| 65 if (!wrapper_bus_ || |
| 66 wrapper_bus_->channels() != static_cast<int>(number_of_channels)) { |
| 67 wrapper_bus_ = AudioBus::CreateWrapper(params_.channels()); |
| 68 } |
| 69 } |
| 70 |
| 71 void WebAudioCapturerSource::Start(WebRtcLocalAudioTrack* track) { |
| 72 DCHECK(thread_checker_.CalledOnValidThread()); |
| 73 DCHECK(track); |
| 74 base::AutoLock auto_lock(lock_); |
| 75 track_ = track; |
| 76 } |
| 77 |
| 78 void WebAudioCapturerSource::Stop() { |
| 79 DCHECK(thread_checker_.CalledOnValidThread()); |
| 80 { |
| 81 base::AutoLock auto_lock(lock_); |
| 82 track_ = NULL; |
| 83 } |
| 84 // DeregisterFromBlinkSource() should not be called while |lock_| is acquired, |
| 85 // as it could result in a deadlock. |
| 86 DeregisterFromBlinkSource(); |
| 87 } |
| 88 |
| 89 void WebAudioCapturerSource::consumeAudio( |
| 90 const blink::WebVector<const float*>& audio_data, |
| 91 size_t number_of_frames) { |
| 92 // TODO(miu): Plumbing is needed to determine the actual capture timestamp |
| 93 // of the audio, instead of just snapshotting TimeTicks::Now(), for proper |
| 94 // audio/video sync. http://crbug.com/335335 |
| 95 current_reference_time_ = base::TimeTicks::Now(); |
| 96 |
| 97 base::AutoLock auto_lock(lock_); |
| 98 if (!track_) |
| 99 return; |
| 100 |
| 101 // Update the downstream client if the audio format has been changed. |
| 102 if (audio_format_changed_) { |
| 103 track_->OnSetFormat(params_); |
| 104 audio_format_changed_ = false; |
| 105 } |
| 106 |
| 107 wrapper_bus_->set_frames(number_of_frames); |
| 108 DCHECK_EQ(params_.channels(), static_cast<int>(audio_data.size())); |
| 109 for (size_t i = 0; i < audio_data.size(); ++i) |
| 110 wrapper_bus_->SetChannelData(i, const_cast<float*>(audio_data[i])); |
| 111 |
| 112 // The following will result in zero, one, or multiple synchronous calls to |
| 113 // DeliverRebufferedAudio(). |
| 114 fifo_.Push(*wrapper_bus_); |
| 115 } |
| 116 |
| 117 void WebAudioCapturerSource::DeliverRebufferedAudio( |
| 118 const media::AudioBus& audio_bus, |
| 119 int frame_delay) { |
| 120 lock_.AssertAcquired(); |
| 121 const base::TimeTicks reference_time = |
| 122 current_reference_time_ + |
| 123 base::TimeDelta::FromMicroseconds(frame_delay * |
| 124 base::Time::kMicrosecondsPerSecond / |
| 125 params_.sample_rate()); |
| 126 track_->Capture(audio_bus, reference_time); |
| 127 } |
| 128 |
| 129 void WebAudioCapturerSource::DeregisterFromBlinkSource() { |
| 130 if (!blink_source_.isNull()) { |
| 131 blink_source_.removeAudioConsumer(this); |
| 132 blink_source_.reset(); |
| 133 } |
| 134 } |
| 135 |
| 136 } // namespace content |
OLD | NEW |