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

Side by Side Diff: content/renderer/media/webrtc/webrtc_audio_sink.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: Reworked unit tests around structural changes, and added exhaustive media_stream_audio_unittest.cc. 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 unified diff | Download patch
OLDNEW
1 // Copyright 2014 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/webrtc/webrtc_local_audio_track_adapter.h" 5 #include "content/renderer/media/webrtc/webrtc_audio_sink.h"
6 6
7 #include <algorithm>
8 #include <limits>
9
10 #include "base/bind.h"
11 #include "base/bind_helpers.h"
7 #include "base/location.h" 12 #include "base/location.h"
8 #include "base/logging.h" 13 #include "base/logging.h"
9 #include "content/renderer/media/media_stream_audio_processor.h" 14 #include "base/message_loop/message_loop.h"
10 #include "content/renderer/media/webrtc/peer_connection_dependency_factory.h"
11 #include "content/renderer/media/webrtc/webrtc_audio_sink_adapter.h"
12 #include "content/renderer/media/webrtc_local_audio_track.h"
13 #include "content/renderer/render_thread_impl.h"
14 #include "third_party/webrtc/api/mediastreaminterface.h"
15 15
16 namespace content { 16 namespace content {
17 17
18 static const char kAudioTrackKind[] = "audio"; 18 WebRtcAudioSink::WebRtcAudioSink(
19
20 scoped_refptr<WebRtcLocalAudioTrackAdapter>
21 WebRtcLocalAudioTrackAdapter::Create(
22 const std::string& label, 19 const std::string& label,
23 webrtc::AudioSourceInterface* track_source) { 20 scoped_refptr<webrtc::AudioSourceInterface> track_source,
24 // TODO(tommi): Change this so that the signaling thread is one of the 21 scoped_refptr<base::SingleThreadTaskRunner> signaling_task_runner)
25 // parameters to this method. 22 : adapter_(new rtc::RefCountedObject<Adapter>(
26 scoped_refptr<base::SingleThreadTaskRunner> signaling_task_runner; 23 label, std::move(track_source), std::move(signaling_task_runner))),
27 RenderThreadImpl* current = RenderThreadImpl::current(); 24 fifo_(base::Bind(&WebRtcAudioSink::DeliverRebufferedAudio,
28 if (current) { 25 base::Unretained(this))) {
29 PeerConnectionDependencyFactory* pc_factory = 26 DVLOG(1) << "WebRtcAudioSink::WebRtcAudioSink()";
30 current->GetPeerConnectionDependencyFactory();
31 signaling_task_runner = pc_factory->GetWebRtcSignalingThread();
32 LOG_IF(ERROR, !signaling_task_runner) << "No signaling thread!";
33 } else {
34 LOG(WARNING) << "Assuming single-threaded operation for unit test.";
35 }
36
37 rtc::RefCountedObject<WebRtcLocalAudioTrackAdapter>* adapter =
38 new rtc::RefCountedObject<WebRtcLocalAudioTrackAdapter>(
39 label, track_source, std::move(signaling_task_runner));
40 return adapter;
41 } 27 }
42 28
43 WebRtcLocalAudioTrackAdapter::WebRtcLocalAudioTrackAdapter( 29 WebRtcAudioSink::~WebRtcAudioSink() {
30 DCHECK(thread_checker_.CalledOnValidThread());
31 DVLOG(1) << "WebRtcAudioSink::~WebRtcAudioSink()";
32 }
33
34 void WebRtcAudioSink::SetAudioProcessor(
35 scoped_refptr<MediaStreamAudioProcessor> processor) {
36 DCHECK(thread_checker_.CalledOnValidThread());
37 DCHECK(processor.get());
38 adapter_->set_processor(std::move(processor));
39 }
40
41 void WebRtcAudioSink::SetLevel(
42 scoped_refptr<MediaStreamAudioLevelCalculator::Level> level) {
43 DCHECK(thread_checker_.CalledOnValidThread());
44 DCHECK(level.get());
45 adapter_->set_level(std::move(level));
46 }
47
48 void WebRtcAudioSink::OnEnabledChanged(bool enabled) {
49 DCHECK(thread_checker_.CalledOnValidThread());
50 adapter_->signaling_task_runner()->PostTask(
51 FROM_HERE,
52 base::Bind(
53 base::IgnoreResult(&WebRtcAudioSink::Adapter::set_enabled),
54 adapter_, enabled));
55 }
56
57 void WebRtcAudioSink::OnData(const media::AudioBus& audio_bus,
58 base::TimeTicks estimated_capture_time) {
59 DCHECK(audio_thread_checker_.CalledOnValidThread());
60 // The following will result in zero, one, or multiple synchronous calls to
61 // DeliverRebufferedAudio().
62 fifo_.Push(audio_bus);
63 }
64
65 void WebRtcAudioSink::OnSetFormat(const media::AudioParameters& params) {
66 // On a format change, the thread delivering audio might have also changed.
67 audio_thread_checker_.DetachFromThread();
68 DCHECK(audio_thread_checker_.CalledOnValidThread());
69
70 DCHECK(params.IsValid());
71 params_ = params;
72 fifo_.Reset(params_.frames_per_buffer());
73 const int num_pcm16_data_elements =
74 params_.frames_per_buffer() * params_.channels();
75 interleaved_data_.reset(new int16_t[num_pcm16_data_elements]);
76 }
77
78 void WebRtcAudioSink::DeliverRebufferedAudio(const media::AudioBus& audio_bus,
79 int frame_delay) {
80 DCHECK(audio_thread_checker_.CalledOnValidThread());
81 DCHECK(params_.IsValid());
82
83 // TODO(miu): Why doesn't a WebRTC sink care about reference time passed to
84 // OnData(), and the |frame_delay| here? How is AV sync achieved otherwise?
85
86 // TODO(henrika): Remove this conversion once the interface in libjingle
87 // supports float vectors.
88 audio_bus.ToInterleaved(audio_bus.frames(),
89 sizeof(interleaved_data_[0]),
90 interleaved_data_.get());
91 adapter_->DeliverPCMToWebRtcSinks(interleaved_data_.get(),
92 params_.sample_rate(),
93 audio_bus.channels(),
94 audio_bus.frames());
95 }
96
97 WebRtcAudioSink::Adapter::Adapter(
44 const std::string& label, 98 const std::string& label,
45 webrtc::AudioSourceInterface* track_source, 99 scoped_refptr<webrtc::AudioSourceInterface> source,
46 scoped_refptr<base::SingleThreadTaskRunner> signaling_task_runner) 100 scoped_refptr<base::SingleThreadTaskRunner> signaling_task_runner)
47 : webrtc::MediaStreamTrack<webrtc::AudioTrackInterface>(label), 101 : webrtc::MediaStreamTrack<webrtc::AudioTrackInterface>(label),
48 owner_(NULL), 102 source_(std::move(source)),
49 track_source_(track_source), 103 signaling_task_runner_(std::move(signaling_task_runner)) {
50 signaling_task_runner_(std::move(signaling_task_runner)) {} 104 DCHECK(signaling_task_runner_);
51
52 WebRtcLocalAudioTrackAdapter::~WebRtcLocalAudioTrackAdapter() {
53 } 105 }
54 106
55 void WebRtcLocalAudioTrackAdapter::Initialize(WebRtcLocalAudioTrack* owner) { 107 WebRtcAudioSink::Adapter::~Adapter() {}
56 DCHECK(!owner_); 108
57 DCHECK(owner); 109 void WebRtcAudioSink::Adapter::DeliverPCMToWebRtcSinks(
58 owner_ = owner; 110 const int16_t* audio_data,
111 int sample_rate,
112 size_t number_of_channels,
113 size_t number_of_frames) {
114 base::AutoLock auto_lock(lock_);
115 for (webrtc::AudioTrackSinkInterface* sink : sinks_) {
116 sink->OnData(audio_data, sizeof(int16_t) * 8, sample_rate,
117 number_of_channels, number_of_frames);
118 }
59 } 119 }
60 120
61 void WebRtcLocalAudioTrackAdapter::SetAudioProcessor( 121 std::string WebRtcAudioSink::Adapter::kind() const {
62 scoped_refptr<MediaStreamAudioProcessor> processor) { 122 return webrtc::MediaStreamTrackInterface::kAudioKind;
63 DCHECK(processor.get());
64 DCHECK(!audio_processor_);
65 audio_processor_ = std::move(processor);
66 } 123 }
67 124
68 void WebRtcLocalAudioTrackAdapter::SetLevel( 125 bool WebRtcAudioSink::Adapter::set_enabled(bool enable) {
69 scoped_refptr<MediaStreamAudioLevelCalculator::Level> level) { 126 DCHECK(!signaling_task_runner_ ||
70 DCHECK(level.get()); 127 signaling_task_runner_->RunsTasksOnCurrentThread());
71 DCHECK(!level_);
72 level_ = std::move(level);
73 }
74
75 std::string WebRtcLocalAudioTrackAdapter::kind() const {
76 return kAudioTrackKind;
77 }
78
79 bool WebRtcLocalAudioTrackAdapter::set_enabled(bool enable) {
80 // If we're not called on the signaling thread, we need to post a task to
81 // change the state on the correct thread.
82 if (signaling_task_runner_ &&
83 !signaling_task_runner_->BelongsToCurrentThread()) {
84 signaling_task_runner_->PostTask(FROM_HERE,
85 base::Bind(
86 base::IgnoreResult(&WebRtcLocalAudioTrackAdapter::set_enabled),
87 this, enable));
88 return true;
89 }
90
91 return webrtc::MediaStreamTrack<webrtc::AudioTrackInterface>:: 128 return webrtc::MediaStreamTrack<webrtc::AudioTrackInterface>::
92 set_enabled(enable); 129 set_enabled(enable);
93 } 130 }
94 131
95 void WebRtcLocalAudioTrackAdapter::AddSink( 132 void WebRtcAudioSink::Adapter::AddSink(webrtc::AudioTrackSinkInterface* sink) {
133 DCHECK(!signaling_task_runner_ ||
134 signaling_task_runner_->RunsTasksOnCurrentThread());
135 DCHECK(sink);
136 base::AutoLock auto_lock(lock_);
137 DCHECK(std::find(sinks_.begin(), sinks_.end(), sink) == sinks_.end());
138 sinks_.push_back(sink);
139 }
140
141 void WebRtcAudioSink::Adapter::RemoveSink(
96 webrtc::AudioTrackSinkInterface* sink) { 142 webrtc::AudioTrackSinkInterface* sink) {
97 DCHECK(!signaling_task_runner_ || 143 DCHECK(!signaling_task_runner_ ||
98 signaling_task_runner_->RunsTasksOnCurrentThread()); 144 signaling_task_runner_->RunsTasksOnCurrentThread());
99 DCHECK(sink); 145 base::AutoLock auto_lock(lock_);
100 #ifndef NDEBUG 146 const auto it = std::find(sinks_.begin(), sinks_.end(), sink);
101 // Verify that |sink| has not been added. 147 if (it != sinks_.end())
102 for (ScopedVector<WebRtcAudioSinkAdapter>::const_iterator it = 148 sinks_.erase(it);
103 sink_adapters_.begin();
104 it != sink_adapters_.end(); ++it) {
105 DCHECK(!(*it)->IsEqual(sink));
106 }
107 #endif
108
109 std::unique_ptr<WebRtcAudioSinkAdapter> adapter(
110 new WebRtcAudioSinkAdapter(sink));
111 owner_->AddSink(adapter.get());
112 sink_adapters_.push_back(adapter.release());
113 } 149 }
114 150
115 void WebRtcLocalAudioTrackAdapter::RemoveSink( 151 bool WebRtcAudioSink::Adapter::GetSignalLevel(int* level) {
116 webrtc::AudioTrackSinkInterface* sink) {
117 DCHECK(!signaling_task_runner_ ||
118 signaling_task_runner_->RunsTasksOnCurrentThread());
119 DCHECK(sink);
120 for (ScopedVector<WebRtcAudioSinkAdapter>::iterator it =
121 sink_adapters_.begin();
122 it != sink_adapters_.end(); ++it) {
123 if ((*it)->IsEqual(sink)) {
124 owner_->RemoveSink(*it);
125 sink_adapters_.erase(it);
126 return;
127 }
128 }
129 }
130
131 bool WebRtcLocalAudioTrackAdapter::GetSignalLevel(int* level) {
132 DCHECK(!signaling_task_runner_ || 152 DCHECK(!signaling_task_runner_ ||
133 signaling_task_runner_->RunsTasksOnCurrentThread()); 153 signaling_task_runner_->RunsTasksOnCurrentThread());
134 154
135 // |level_| is only set once, so it's safe to read without first acquiring a 155 // |level_| is only set once, so it's safe to read without first acquiring a
136 // mutex. 156 // mutex.
137 if (!level_) 157 if (!level_)
138 return false; 158 return false;
139 const float signal_level = level_->GetCurrent(); 159 const float signal_level = level_->GetCurrent();
140 DCHECK_GE(signal_level, 0.0f); 160 DCHECK_GE(signal_level, 0.0f);
141 DCHECK_LE(signal_level, 1.0f); 161 DCHECK_LE(signal_level, 1.0f);
142 // Convert from float in range [0.0,1.0] to an int in range [0,32767]. 162 // Convert from float in range [0.0,1.0] to an int in range [0,32767].
143 *level = static_cast<int>(signal_level * std::numeric_limits<int16_t>::max() + 163 *level = static_cast<int>(signal_level * std::numeric_limits<int16_t>::max() +
144 0.5f /* rounding to nearest int */); 164 0.5f /* rounding to nearest int */);
145 return true; 165 return true;
146 } 166 }
147 167
148 rtc::scoped_refptr<webrtc::AudioProcessorInterface> 168 rtc::scoped_refptr<webrtc::AudioProcessorInterface>
149 WebRtcLocalAudioTrackAdapter::GetAudioProcessor() { 169 WebRtcAudioSink::Adapter::GetAudioProcessor() {
150 DCHECK(!signaling_task_runner_ || 170 DCHECK(!signaling_task_runner_ ||
151 signaling_task_runner_->RunsTasksOnCurrentThread()); 171 signaling_task_runner_->RunsTasksOnCurrentThread());
152 return audio_processor_.get(); 172 return audio_processor_.get();
153 } 173 }
154 174
155 webrtc::AudioSourceInterface* WebRtcLocalAudioTrackAdapter::GetSource() const { 175 webrtc::AudioSourceInterface* WebRtcAudioSink::Adapter::GetSource() const {
156 DCHECK(!signaling_task_runner_ || 176 DCHECK(!signaling_task_runner_ ||
157 signaling_task_runner_->RunsTasksOnCurrentThread()); 177 signaling_task_runner_->RunsTasksOnCurrentThread());
158 return track_source_; 178 return source_.get();
159 } 179 }
160 180
161 } // namespace content 181 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698