| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/external_media_stream_audio_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 | 6 |
| 18 namespace content { | 7 namespace content { |
| 19 | 8 |
| 20 WebAudioCapturerSource::WebAudioCapturerSource( | 9 ExternalMediaStreamAudioSource::ExternalMediaStreamAudioSource( |
| 21 blink::WebMediaStreamSource* blink_source) | 10 scoped_refptr<media::AudioCapturerSource> source, |
| 22 : track_(NULL), | 11 int sample_rate, |
| 23 audio_format_changed_(false), | 12 media::ChannelLayout channel_layout, |
| 24 fifo_(base::Bind(&WebAudioCapturerSource::DeliverRebufferedAudio, | 13 int frames_per_buffer, |
| 25 base::Unretained(this))), | 14 bool is_remote) |
| 26 blink_source_(*blink_source) { | 15 : MediaStreamAudioSource(!is_remote), source_(std::move(source)), |
| 27 DCHECK(blink_source); | 16 is_started_(false) { |
| 28 DCHECK(!blink_source_.isNull()); | 17 DVLOG(1) |
| 29 DVLOG(1) << "WebAudioCapturerSource::WebAudioCapturerSource()"; | 18 << "ExternalMediaStreamAudioSource::ExternalMediaStreamAudioSource()"; |
| 30 blink_source_.addAudioConsumer(this); | 19 DCHECK(source_.get()); |
| 20 MediaStreamAudioSource::SetFormat(media::AudioParameters( |
| 21 media::AudioParameters::AUDIO_PCM_LOW_LATENCY, channel_layout, |
| 22 sample_rate, |
| 23 16, // Legacy parameter (data is always in 32-bit float format). |
| 24 frames_per_buffer)); |
| 31 } | 25 } |
| 32 | 26 |
| 33 WebAudioCapturerSource::~WebAudioCapturerSource() { | 27 ExternalMediaStreamAudioSource::~ExternalMediaStreamAudioSource() { |
| 34 DCHECK(thread_checker_.CalledOnValidThread()); | 28 DVLOG(1) |
| 35 DVLOG(1) << "WebAudioCapturerSource::~WebAudioCapturerSource()"; | 29 << "ExternalMediaStreamAudioSource::~ExternalMediaStreamAudioSource()"; |
| 36 DeregisterFromBlinkSource(); | 30 // Ensure the source is stopped. |
| 31 MediaStreamAudioSource::StopSource(); |
| 37 } | 32 } |
| 38 | 33 |
| 39 void WebAudioCapturerSource::setFormat( | 34 void ExternalMediaStreamAudioSource::DoStopSource() { |
| 40 size_t number_of_channels, float sample_rate) { | |
| 41 DCHECK(thread_checker_.CalledOnValidThread()); | 35 DCHECK(thread_checker_.CalledOnValidThread()); |
| 42 DVLOG(1) << "WebAudioCapturerSource::setFormat(sample_rate=" | 36 if (is_stopped_) |
| 43 << sample_rate << ")"; | 37 return; |
| 44 | 38 if (is_started_) { |
| 45 // If the channel count is greater than 8, use discrete layout. However, | 39 source_->Stop(); |
| 46 // anything beyond 8 is ignored by the subsequent (WebRTC) audio pipeline. | 40 VLOG(1) << "Stopped externally-provided " |
| 47 ChannelLayout channel_layout = | 41 << (is_local_source() ? "local" : "remote") |
| 48 number_of_channels > 8 ? media::CHANNEL_LAYOUT_DISCRETE | 42 << " source with audio parameters={" |
| 49 : media::GuessChannelLayout(number_of_channels); | 43 << GetAudioParameters().AsHumanReadableString() << "}."; |
| 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 } | 44 } |
| 45 is_stopped_ = true; |
| 69 } | 46 } |
| 70 | 47 |
| 71 void WebAudioCapturerSource::Start(WebRtcLocalAudioTrack* track) { | 48 bool ExternalMediaStreamAudioSource::EnsureSourceIsStarted() { |
| 72 DCHECK(thread_checker_.CalledOnValidThread()); | 49 DCHECK(thread_checker_.CalledOnValidThread()); |
| 73 DCHECK(track); | 50 if (is_stopped_) |
| 74 base::AutoLock auto_lock(lock_); | 51 return false; |
| 75 track_ = track; | 52 if (is_started_) |
| 53 return true; |
| 54 VLOG(1) << "Starting externally-provided " |
| 55 << (is_local_source() ? "local" : "remote") |
| 56 << " source with audio parameters={" |
| 57 << GetAudioParameters().AsHumanReadableString() << "}."; |
| 58 source_->Initialize(GetAudioParameters(), this, -1); |
| 59 source_->Start(); |
| 60 is_started_ = true; |
| 61 return true; |
| 76 } | 62 } |
| 77 | 63 |
| 78 void WebAudioCapturerSource::Stop() { | 64 void ExternalMediaStreamAudioSource::Capture(const media::AudioBus* audio_bus, |
| 79 DCHECK(thread_checker_.CalledOnValidThread()); | 65 int audio_delay_milliseconds, |
| 80 { | 66 double volume, |
| 81 base::AutoLock auto_lock(lock_); | 67 bool key_pressed) { |
| 82 track_ = NULL; | 68 DCHECK(audio_bus); |
| 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 | 69 // TODO(miu): Plumbing is needed to determine the actual capture timestamp |
| 93 // of the audio, instead of just snapshotting TimeTicks::Now(), for proper | 70 // of the audio, instead of just snapshotting TimeTicks::Now(), for proper |
| 94 // audio/video sync. http://crbug.com/335335 | 71 // audio/video sync. http://crbug.com/335335 |
| 95 current_reference_time_ = base::TimeTicks::Now(); | 72 MediaStreamAudioSource::DeliverDataToTracks( |
| 96 | 73 *audio_bus, base::TimeTicks::Now() - base::TimeDelta::FromMilliseconds( |
| 97 base::AutoLock auto_lock(lock_); | 74 audio_delay_milliseconds)); |
| 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 } | 75 } |
| 116 | 76 |
| 117 void WebAudioCapturerSource::DeliverRebufferedAudio( | 77 void ExternalMediaStreamAudioSource::OnCaptureError(const std::string& why) { |
| 118 const media::AudioBus& audio_bus, | 78 // As of this writing, this method doesn't get called for anything useful, |
| 119 int frame_delay) { | 79 // and all other implementors just log the message, but don't disconnect sinks |
| 120 lock_.AssertAcquired(); | 80 // or take any other action. So, just log the error. |
| 121 const base::TimeTicks reference_time = | 81 LOG(ERROR) << why; |
| 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 } | 82 } |
| 135 | 83 |
| 136 } // namespace content | 84 } // namespace content |
| OLD | NEW |