Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(357)

Unified Diff: content/renderer/media/webaudio_media_stream_source.cc

Issue 1834323002: MediaStream audio: Refactor 3 separate "glue" implementations into one. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: REBASE + Workaround to ensure MediaStreamAudioProcessor is destroyed on the main thread. Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: content/renderer/media/webaudio_media_stream_source.cc
diff --git a/content/renderer/media/webaudio_capturer_source.cc b/content/renderer/media/webaudio_media_stream_source.cc
similarity index 38%
rename from content/renderer/media/webaudio_capturer_source.cc
rename to content/renderer/media/webaudio_media_stream_source.cc
index 3fca41942df83562b16d38c44f0c6af72069713d..7f4c75d37ff6f15c6fc451b96e8345aa83aad7b6 100644
--- a/content/renderer/media/webaudio_capturer_source.cc
+++ b/content/renderer/media/webaudio_media_stream_source.cc
@@ -2,91 +2,84 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
-#include "content/renderer/media/webaudio_capturer_source.h"
+#include "content/renderer/media/webaudio_media_stream_source.h"
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/logging.h"
-#include "content/renderer/media/webrtc_local_audio_track.h"
-
-using media::AudioBus;
-using media::AudioParameters;
-using media::ChannelLayout;
-using media::CHANNEL_LAYOUT_MONO;
-using media::CHANNEL_LAYOUT_STEREO;
namespace content {
-WebAudioCapturerSource::WebAudioCapturerSource(
+WebAudioMediaStreamSource::WebAudioMediaStreamSource(
blink::WebMediaStreamSource* blink_source)
- : track_(NULL),
- audio_format_changed_(false),
- fifo_(base::Bind(&WebAudioCapturerSource::DeliverRebufferedAudio,
+ : MediaStreamAudioSource(false /* is_remote */),
+ is_registered_consumer_(false),
+ fifo_(base::Bind(&WebAudioMediaStreamSource::DeliverRebufferedAudio,
base::Unretained(this))),
blink_source_(*blink_source) {
- DCHECK(blink_source);
- DCHECK(!blink_source_.isNull());
- DVLOG(1) << "WebAudioCapturerSource::WebAudioCapturerSource()";
- blink_source_.addAudioConsumer(this);
+ DVLOG(1) << "WebAudioMediaStreamSource::WebAudioMediaStreamSource()";
}
-WebAudioCapturerSource::~WebAudioCapturerSource() {
- DCHECK(thread_checker_.CalledOnValidThread());
- DVLOG(1) << "WebAudioCapturerSource::~WebAudioCapturerSource()";
- DeregisterFromBlinkSource();
+WebAudioMediaStreamSource::~WebAudioMediaStreamSource() {
+ DVLOG(1) << "WebAudioMediaStreamSource::~WebAudioMediaStreamSource()";
+ EnsureSourceIsStopped();
}
-void WebAudioCapturerSource::setFormat(
- size_t number_of_channels, float sample_rate) {
+void WebAudioMediaStreamSource::setFormat(size_t number_of_channels,
+ float sample_rate) {
DCHECK(thread_checker_.CalledOnValidThread());
- DVLOG(1) << "WebAudioCapturerSource::setFormat(sample_rate="
- << sample_rate << ")";
+ VLOG(1) << "WebAudio media stream source changed format to: channels="
+ << number_of_channels << ", sample_rate=" << sample_rate;
// If the channel count is greater than 8, use discrete layout. However,
- // anything beyond 8 is ignored by the subsequent (WebRTC) audio pipeline.
- ChannelLayout channel_layout =
+ // anything beyond 8 is ignored by some audio tracks/sinks.
+ media::ChannelLayout channel_layout =
number_of_channels > 8 ? media::CHANNEL_LAYOUT_DISCRETE
: media::GuessChannelLayout(number_of_channels);
- base::AutoLock auto_lock(lock_);
-
- // Set the format used by this WebAudioCapturerSource. We are using 10ms data
- // as buffer size since that is the native buffer size of WebRtc packet
+ // Set the format used by this WebAudioMediaStreamSource. We are using 10ms
+ // data as a buffer size since that is the native buffer size of WebRtc packet
// running on.
+ //
+ // TODO(miu): Re-evaluate whether this is needed. For now (this refactoring),
+ // I did not want to change behavior. http://crbug.com/577874
fifo_.Reset(sample_rate / 100);
- params_.Reset(media::AudioParameters::AUDIO_PCM_LOW_LATENCY, channel_layout,
- sample_rate, 16, fifo_.frames_per_buffer());
-
+ media::AudioParameters params(media::AudioParameters::AUDIO_PCM_LOW_LATENCY,
+ channel_layout, sample_rate, 16,
+ fifo_.frames_per_buffer());
// Take care of the discrete channel layout case.
- params_.set_channels_for_discrete(number_of_channels);
+ params.set_channels_for_discrete(number_of_channels);
+ MediaStreamAudioSource::SetFormat(params);
- audio_format_changed_ = true;
-
- if (!wrapper_bus_ ||
- wrapper_bus_->channels() != static_cast<int>(number_of_channels)) {
- wrapper_bus_ = AudioBus::CreateWrapper(params_.channels());
- }
+ if (!wrapper_bus_ || wrapper_bus_->channels() != params.channels())
+ wrapper_bus_ = media::AudioBus::CreateWrapper(params.channels());
}
-void WebAudioCapturerSource::Start(WebRtcLocalAudioTrack* track) {
+bool WebAudioMediaStreamSource::EnsureSourceIsStarted() {
DCHECK(thread_checker_.CalledOnValidThread());
- DCHECK(track);
- base::AutoLock auto_lock(lock_);
- track_ = track;
+ if (is_registered_consumer_)
+ return true;
+ if (blink_source_.isNull() || !blink_source_.requiresAudioConsumer())
+ return false;
+ VLOG(1) << "Starting WebAudio media stream source.";
+ blink_source_.addAudioConsumer(this);
+ is_registered_consumer_ = true;
+ return true;
}
-void WebAudioCapturerSource::Stop() {
+void WebAudioMediaStreamSource::EnsureSourceIsStopped() {
DCHECK(thread_checker_.CalledOnValidThread());
- {
- base::AutoLock auto_lock(lock_);
- track_ = NULL;
- }
- // DeregisterFromBlinkSource() should not be called while |lock_| is acquired,
- // as it could result in a deadlock.
- DeregisterFromBlinkSource();
+ if (!is_registered_consumer_)
+ return;
+ is_registered_consumer_ = false;
+ DCHECK(!blink_source_.isNull());
+ blink_source_.removeAudioConsumer(this);
+ blink_source_.reset();
+ VLOG(1) << "Stopped WebAudio media stream source. Final audio parameters={"
+ << GetAudioParameters().AsHumanReadableString() << "}.";
}
-void WebAudioCapturerSource::consumeAudio(
+void WebAudioMediaStreamSource::consumeAudio(
const blink::WebVector<const float*>& audio_data,
size_t number_of_frames) {
// TODO(miu): Plumbing is needed to determine the actual capture timestamp
@@ -94,18 +87,8 @@ void WebAudioCapturerSource::consumeAudio(
// audio/video sync. http://crbug.com/335335
current_reference_time_ = base::TimeTicks::Now();
- base::AutoLock auto_lock(lock_);
- if (!track_)
- return;
-
- // Update the downstream client if the audio format has been changed.
- if (audio_format_changed_) {
- track_->OnSetFormat(params_);
- audio_format_changed_ = false;
- }
-
wrapper_bus_->set_frames(number_of_frames);
- DCHECK_EQ(params_.channels(), static_cast<int>(audio_data.size()));
+ DCHECK_EQ(wrapper_bus_->channels(), static_cast<int>(audio_data.size()));
for (size_t i = 0; i < audio_data.size(); ++i)
wrapper_bus_->SetChannelData(i, const_cast<float*>(audio_data[i]));
@@ -114,23 +97,15 @@ void WebAudioCapturerSource::consumeAudio(
fifo_.Push(*wrapper_bus_);
}
-void WebAudioCapturerSource::DeliverRebufferedAudio(
+void WebAudioMediaStreamSource::DeliverRebufferedAudio(
const media::AudioBus& audio_bus,
int frame_delay) {
- lock_.AssertAcquired();
const base::TimeTicks reference_time =
current_reference_time_ +
- base::TimeDelta::FromMicroseconds(frame_delay *
- base::Time::kMicrosecondsPerSecond /
- params_.sample_rate());
- track_->Capture(audio_bus, reference_time);
-}
-
-void WebAudioCapturerSource::DeregisterFromBlinkSource() {
- if (!blink_source_.isNull()) {
- blink_source_.removeAudioConsumer(this);
- blink_source_.reset();
- }
+ base::TimeDelta::FromMicroseconds(
+ frame_delay * base::Time::kMicrosecondsPerSecond /
+ MediaStreamAudioSource::GetAudioParameters().sample_rate());
+ MediaStreamAudioSource::DeliverDataToTracks(audio_bus, reference_time);
}
} // namespace content
« no previous file with comments | « content/renderer/media/webaudio_media_stream_source.h ('k') | content/renderer/media/webrtc/media_stream_remote_audio_track.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698