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

Side by Side Diff: content/renderer/media/webrtc/media_stream_remote_audio_track.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 unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2015 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/webrtc/media_stream_remote_audio_track.h"
6
7 #include <stddef.h>
8
9 #include <list>
10
11 #include "base/logging.h"
12 #include "content/public/renderer/media_stream_audio_sink.h"
13 #include "third_party/webrtc/api/mediastreaminterface.h"
14
15 namespace content {
16
17 class MediaStreamRemoteAudioSource::AudioSink
18 : public webrtc::AudioTrackSinkInterface {
19 public:
20 AudioSink() {
21 }
22 ~AudioSink() override {
23 DCHECK(sinks_.empty());
24 }
25
26 void Add(MediaStreamAudioSink* sink, MediaStreamAudioTrack* track,
27 bool enabled) {
28 DCHECK(thread_checker_.CalledOnValidThread());
29 SinkInfo info(sink, track, enabled);
30 base::AutoLock lock(lock_);
31 sinks_.push_back(info);
32 }
33
34 void Remove(MediaStreamAudioSink* sink, MediaStreamAudioTrack* track) {
35 DCHECK(thread_checker_.CalledOnValidThread());
36 base::AutoLock lock(lock_);
37 sinks_.remove_if([&sink, &track](const SinkInfo& info) {
38 return info.sink == sink && info.track == track;
39 });
40 }
41
42 void SetEnabled(MediaStreamAudioTrack* track, bool enabled) {
43 DCHECK(thread_checker_.CalledOnValidThread());
44 base::AutoLock lock(lock_);
45 for (SinkInfo& info : sinks_) {
46 if (info.track == track)
47 info.enabled = enabled;
48 }
49 }
50
51 void RemoveAll(MediaStreamAudioTrack* track) {
52 base::AutoLock lock(lock_);
53 sinks_.remove_if([&track](const SinkInfo& info) {
54 return info.track == track;
55 });
56 }
57
58 bool IsNeeded() const {
59 DCHECK(thread_checker_.CalledOnValidThread());
60 return !sinks_.empty();
61 }
62
63 private:
64 void OnData(const void* audio_data, int bits_per_sample, int sample_rate,
65 size_t number_of_channels, size_t number_of_frames) override {
66 if (!audio_bus_ ||
67 static_cast<size_t>(audio_bus_->channels()) != number_of_channels ||
68 static_cast<size_t>(audio_bus_->frames()) != number_of_frames) {
69 audio_bus_ = media::AudioBus::Create(number_of_channels,
70 number_of_frames);
71 }
72
73 audio_bus_->FromInterleaved(audio_data, number_of_frames,
74 bits_per_sample / 8);
75
76 bool format_changed = false;
77 if (params_.format() != media::AudioParameters::AUDIO_PCM_LOW_LATENCY ||
78 static_cast<size_t>(params_.channels()) != number_of_channels ||
79 params_.sample_rate() != sample_rate ||
80 static_cast<size_t>(params_.frames_per_buffer()) != number_of_frames) {
81 params_ = media::AudioParameters(
82 media::AudioParameters::AUDIO_PCM_LOW_LATENCY,
83 media::GuessChannelLayout(number_of_channels),
84 sample_rate, 16, number_of_frames);
85 format_changed = true;
86 }
87
88 // TODO(tommi): We should get the timestamp from WebRTC.
89 base::TimeTicks estimated_capture_time(base::TimeTicks::Now());
90
91 base::AutoLock lock(lock_);
92 for (const SinkInfo& info : sinks_) {
93 if (info.enabled) {
94 if (format_changed)
95 info.sink->OnSetFormat(params_);
96 info.sink->OnData(*audio_bus_.get(), estimated_capture_time);
97 }
98 }
99 }
100
101 mutable base::Lock lock_;
102 struct SinkInfo {
103 SinkInfo(MediaStreamAudioSink* sink, MediaStreamAudioTrack* track,
104 bool enabled) : sink(sink), track(track), enabled(enabled) {}
105 MediaStreamAudioSink* sink;
106 MediaStreamAudioTrack* track;
107 bool enabled;
108 };
109 std::list<SinkInfo> sinks_;
110 base::ThreadChecker thread_checker_;
111 media::AudioParameters params_; // Only used on the callback thread.
112 std::unique_ptr<media::AudioBus>
113 audio_bus_; // Only used on the callback thread.
114 };
115
116 MediaStreamRemoteAudioTrack::MediaStreamRemoteAudioTrack(
117 const blink::WebMediaStreamSource& source, bool enabled)
118 : MediaStreamAudioTrack(false), source_(source), enabled_(enabled) {
119 DCHECK(source.getExtraData()); // Make sure the source has a native source.
120 }
121
122 MediaStreamRemoteAudioTrack::~MediaStreamRemoteAudioTrack() {
123 DCHECK(main_render_thread_checker_.CalledOnValidThread());
124 // Ensure the track is stopped.
125 MediaStreamAudioTrack::Stop();
126 }
127
128 void MediaStreamRemoteAudioTrack::SetEnabled(bool enabled) {
129 DCHECK(main_render_thread_checker_.CalledOnValidThread());
130
131 // This affects the shared state of the source for whether or not it's a part
132 // of the mixed audio that's rendered for remote tracks from WebRTC.
133 // All tracks from the same source will share this state and thus can step
134 // on each other's toes.
135 // This is also why we can't check the |enabled_| state for equality with
136 // |enabled| before setting the mixing enabled state. |enabled_| and the
137 // shared state might not be the same.
138 source()->SetEnabledForMixing(enabled);
139
140 enabled_ = enabled;
141 source()->SetSinksEnabled(this, enabled);
142 }
143
144 void MediaStreamRemoteAudioTrack::OnStop() {
145 DCHECK(main_render_thread_checker_.CalledOnValidThread());
146 DVLOG(1) << "MediaStreamRemoteAudioTrack::OnStop()";
147
148 source()->RemoveAll(this);
149
150 // Stop means that a track should be stopped permanently. But
151 // since there is no proper way of doing that on a remote track, we can
152 // at least disable the track. Blink will not call down to the content layer
153 // after a track has been stopped.
154 SetEnabled(false);
155 }
156
157 void MediaStreamRemoteAudioTrack::AddSink(MediaStreamAudioSink* sink) {
158 DCHECK(main_render_thread_checker_.CalledOnValidThread());
159 return source()->AddSink(sink, this, enabled_);
160 }
161
162 void MediaStreamRemoteAudioTrack::RemoveSink(MediaStreamAudioSink* sink) {
163 DCHECK(main_render_thread_checker_.CalledOnValidThread());
164 return source()->RemoveSink(sink, this);
165 }
166
167 media::AudioParameters MediaStreamRemoteAudioTrack::GetOutputFormat() const {
168 DCHECK(main_render_thread_checker_.CalledOnValidThread());
169 // This method is not implemented on purpose and should be removed.
170 // TODO(tommi): See comment for GetOutputFormat in MediaStreamAudioTrack.
171 NOTIMPLEMENTED();
172 return media::AudioParameters();
173 }
174
175 webrtc::AudioTrackInterface* MediaStreamRemoteAudioTrack::GetAudioAdapter() {
176 DCHECK(main_render_thread_checker_.CalledOnValidThread());
177 return source()->GetAudioAdapter();
178 }
179
180 MediaStreamRemoteAudioSource* MediaStreamRemoteAudioTrack::source() const {
181 return static_cast<MediaStreamRemoteAudioSource*>(source_.getExtraData());
182 }
183
184 MediaStreamRemoteAudioSource::MediaStreamRemoteAudioSource(
185 const scoped_refptr<webrtc::AudioTrackInterface>& track) : track_(track) {}
186
187 MediaStreamRemoteAudioSource::~MediaStreamRemoteAudioSource() {
188 DCHECK(thread_checker_.CalledOnValidThread());
189 }
190
191 void MediaStreamRemoteAudioSource::SetEnabledForMixing(bool enabled) {
192 DCHECK(thread_checker_.CalledOnValidThread());
193 track_->set_enabled(enabled);
194 }
195
196 void MediaStreamRemoteAudioSource::AddSink(MediaStreamAudioSink* sink,
197 MediaStreamAudioTrack* track,
198 bool enabled) {
199 DCHECK(thread_checker_.CalledOnValidThread());
200 if (!sink_) {
201 sink_.reset(new AudioSink());
202 track_->AddSink(sink_.get());
203 }
204
205 sink_->Add(sink, track, enabled);
206 }
207
208 void MediaStreamRemoteAudioSource::RemoveSink(MediaStreamAudioSink* sink,
209 MediaStreamAudioTrack* track) {
210 DCHECK(thread_checker_.CalledOnValidThread());
211 DCHECK(sink_);
212
213 sink_->Remove(sink, track);
214
215 if (!sink_->IsNeeded()) {
216 track_->RemoveSink(sink_.get());
217 sink_.reset();
218 }
219 }
220
221 void MediaStreamRemoteAudioSource::SetSinksEnabled(MediaStreamAudioTrack* track,
222 bool enabled) {
223 if (sink_)
224 sink_->SetEnabled(track, enabled);
225 }
226
227 void MediaStreamRemoteAudioSource::RemoveAll(MediaStreamAudioTrack* track) {
228 if (sink_)
229 sink_->RemoveAll(track);
230 }
231
232 webrtc::AudioTrackInterface* MediaStreamRemoteAudioSource::GetAudioAdapter() {
233 DCHECK(thread_checker_.CalledOnValidThread());
234 return track_.get();
235 }
236
237 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698