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

Side by Side Diff: content/renderer/media/track_audio_renderer.cc

Issue 1633423002: MediaStream audio rendering: Bypass audio processing for non-WebRTC cases. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add comment to TrackAudioRenderer header to explain it does not handle remote WebRTC tracks. Created 4 years, 10 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 (c) 2012 The Chromium Authors. All rights reserved. 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 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_local_audio_renderer.h" 5 #include "content/renderer/media/track_audio_renderer.h"
6
7 #include <utility>
8 6
9 #include "base/location.h" 7 #include "base/location.h"
10 #include "base/logging.h" 8 #include "base/logging.h"
11 #include "base/metrics/histogram.h" 9 #include "base/metrics/histogram.h"
12 #include "base/synchronization/lock.h" 10 #include "base/synchronization/lock.h"
13 #include "base/thread_task_runner_handle.h" 11 #include "base/thread_task_runner_handle.h"
14 #include "base/trace_event/trace_event.h" 12 #include "base/trace_event/trace_event.h"
15 #include "content/renderer/media/audio_device_factory.h" 13 #include "content/renderer/media/audio_device_factory.h"
16 #include "content/renderer/media/media_stream_dispatcher.h" 14 #include "content/renderer/media/media_stream_audio_track.h"
17 #include "content/renderer/media/webrtc_audio_capturer.h"
18 #include "content/renderer/media/webrtc_audio_renderer.h"
19 #include "content/renderer/render_frame_impl.h"
20 #include "media/audio/audio_output_device.h" 15 #include "media/audio/audio_output_device.h"
21 #include "media/base/audio_bus.h" 16 #include "media/base/audio_bus.h"
22 #include "media/base/audio_shifter.h" 17 #include "media/base/audio_shifter.h"
23 18
24 namespace content { 19 namespace content {
25 20
26 namespace { 21 namespace {
27 22
28 enum LocalRendererSinkStates { 23 enum LocalRendererSinkStates {
29 kSinkStarted = 0, 24 kSinkStarted = 0,
30 kSinkNeverStarted, 25 kSinkNeverStarted,
31 kSinkStatesMax // Must always be last! 26 kSinkStatesMax // Must always be last!
32 }; 27 };
33 28
29 // Translates |num_samples_rendered| into a TimeDelta duration and adds it to
30 // |prior_elapsed_render_time|.
31 base::TimeDelta ComputeTotalElapsedRenderTime(
32 base::TimeDelta prior_elapsed_render_time,
33 int64_t num_samples_rendered,
34 int sample_rate) {
35 return prior_elapsed_render_time + base::TimeDelta::FromMicroseconds(
36 num_samples_rendered * base::Time::kMicrosecondsPerSecond / sample_rate);
37 }
38
34 } // namespace 39 } // namespace
35 40
36 // media::AudioRendererSink::RenderCallback implementation 41 // media::AudioRendererSink::RenderCallback implementation
37 int WebRtcLocalAudioRenderer::Render(media::AudioBus* audio_bus, 42 int TrackAudioRenderer::Render(media::AudioBus* audio_bus,
38 uint32_t audio_delay_milliseconds, 43 uint32_t audio_delay_milliseconds,
39 uint32_t frames_skipped) { 44 uint32_t frames_skipped) {
40 TRACE_EVENT0("audio", "WebRtcLocalAudioRenderer::Render"); 45 TRACE_EVENT0("audio", "TrackAudioRenderer::Render");
41 base::AutoLock auto_lock(thread_lock_); 46 base::AutoLock auto_lock(thread_lock_);
42 47
43 if (!playing_ || !volume_ || !audio_shifter_) { 48 if (!audio_shifter_) {
44 audio_bus->Zero(); 49 audio_bus->Zero();
45 return 0; 50 return 0;
46 } 51 }
47 52
48 audio_shifter_->Pull( 53 // TODO(miu): Plumbing is needed to determine the actual playout timestamp
49 audio_bus, 54 // of the audio, instead of just snapshotting TimeTicks::Now(), for proper
50 base::TimeTicks::Now() - 55 // audio/video sync. http://crbug.com/335335
51 base::TimeDelta::FromMilliseconds(audio_delay_milliseconds)); 56 const base::TimeTicks playout_time =
52 57 base::TimeTicks::Now() +
58 base::TimeDelta::FromMilliseconds(audio_delay_milliseconds);
59 DVLOG(2) << "Pulling audio out of shifter to be played "
60 << audio_delay_milliseconds << " ms from now.";
61 audio_shifter_->Pull(audio_bus, playout_time);
62 num_samples_rendered_ += audio_bus->frames();
53 return audio_bus->frames(); 63 return audio_bus->frames();
54 } 64 }
55 65
56 void WebRtcLocalAudioRenderer::OnRenderError() { 66 void TrackAudioRenderer::OnRenderError() {
57 NOTIMPLEMENTED(); 67 NOTIMPLEMENTED();
58 } 68 }
59 69
60 // content::MediaStreamAudioSink implementation 70 // content::MediaStreamAudioSink implementation
61 void WebRtcLocalAudioRenderer::OnData(const media::AudioBus& audio_bus, 71 void TrackAudioRenderer::OnData(const media::AudioBus& audio_bus,
62 base::TimeTicks estimated_capture_time) { 72 base::TimeTicks reference_time) {
63 DCHECK(capture_thread_checker_.CalledOnValidThread()); 73 DCHECK(audio_thread_checker_.CalledOnValidThread());
64 DCHECK(!estimated_capture_time.is_null()); 74 DCHECK(!reference_time.is_null());
65 75
66 TRACE_EVENT0("audio", "WebRtcLocalAudioRenderer::CaptureData"); 76 TRACE_EVENT0("audio", "TrackAudioRenderer::CaptureData");
67 77
68 base::AutoLock auto_lock(thread_lock_); 78 base::AutoLock auto_lock(thread_lock_);
69 if (!playing_ || !volume_ || !audio_shifter_) 79 if (!audio_shifter_)
70 return; 80 return;
71 81
72 scoped_ptr<media::AudioBus> audio_data( 82 scoped_ptr<media::AudioBus> audio_data(
73 media::AudioBus::Create(audio_bus.channels(), audio_bus.frames())); 83 media::AudioBus::Create(audio_bus.channels(), audio_bus.frames()));
74 audio_bus.CopyTo(audio_data.get()); 84 audio_bus.CopyTo(audio_data.get());
75 audio_shifter_->Push(std::move(audio_data), estimated_capture_time); 85 // Note: For remote audio sources, |reference_time| is the local playout time,
76 const base::TimeTicks now = base::TimeTicks::Now(); 86 // the ideal point-in-time at which the first audio sample should be played
77 total_render_time_ += now - last_render_time_; 87 // out in the future. For local sources, |reference_time| is the
78 last_render_time_ = now; 88 // point-in-time at which the first audio sample was captured in the past. In
89 // either case, AudioShifter will auto-detect and do the right thing when
90 // audio is pulled from it.
91 audio_shifter_->Push(std::move(audio_data), reference_time);
79 } 92 }
80 93
81 void WebRtcLocalAudioRenderer::OnSetFormat( 94 void TrackAudioRenderer::OnSetFormat(const media::AudioParameters& params) {
82 const media::AudioParameters& params) { 95 DVLOG(1) << "TrackAudioRenderer::OnSetFormat()";
83 DVLOG(1) << "WebRtcLocalAudioRenderer::OnSetFormat()";
84 // If the source is restarted, we might have changed to another capture 96 // If the source is restarted, we might have changed to another capture
85 // thread. 97 // thread.
86 capture_thread_checker_.DetachFromThread(); 98 audio_thread_checker_.DetachFromThread();
87 DCHECK(capture_thread_checker_.CalledOnValidThread()); 99 DCHECK(audio_thread_checker_.CalledOnValidThread());
100
101 // If the parameters changed, the audio in the AudioShifter is invalid and
102 // should be dropped.
103 {
104 base::AutoLock auto_lock(thread_lock_);
105 if (audio_shifter_ &&
106 (audio_shifter_->sample_rate() != params.sample_rate() ||
107 audio_shifter_->channels() != params.channels())) {
108 HaltAudioFlowWhileLockHeld();
109 }
110 }
88 111
89 // Post a task on the main render thread to reconfigure the |sink_| with the 112 // Post a task on the main render thread to reconfigure the |sink_| with the
90 // new format. 113 // new format.
91 task_runner_->PostTask( 114 task_runner_->PostTask(
92 FROM_HERE, 115 FROM_HERE,
93 base::Bind(&WebRtcLocalAudioRenderer::ReconfigureSink, this, params)); 116 base::Bind(&TrackAudioRenderer::ReconfigureSink, this, params));
94 } 117 }
95 118
96 // WebRtcLocalAudioRenderer::WebRtcLocalAudioRenderer implementation. 119 TrackAudioRenderer::TrackAudioRenderer(
97 WebRtcLocalAudioRenderer::WebRtcLocalAudioRenderer(
98 const blink::WebMediaStreamTrack& audio_track, 120 const blink::WebMediaStreamTrack& audio_track,
99 int source_render_frame_id, 121 int playout_render_frame_id,
100 int session_id, 122 int session_id,
101 const std::string& device_id, 123 const std::string& device_id,
102 const url::Origin& security_origin) 124 const url::Origin& security_origin)
103 : audio_track_(audio_track), 125 : audio_track_(audio_track),
104 source_render_frame_id_(source_render_frame_id), 126 playout_render_frame_id_(playout_render_frame_id),
105 session_id_(session_id), 127 session_id_(session_id),
106 task_runner_(base::ThreadTaskRunnerHandle::Get()), 128 task_runner_(base::ThreadTaskRunnerHandle::Get()),
129 num_samples_rendered_(0),
107 playing_(false), 130 playing_(false),
108 output_device_id_(device_id), 131 output_device_id_(device_id),
109 security_origin_(security_origin), 132 security_origin_(security_origin),
110 volume_(0.0), 133 volume_(0.0),
111 sink_started_(false) { 134 sink_started_(false) {
112 DVLOG(1) << "WebRtcLocalAudioRenderer::WebRtcLocalAudioRenderer()"; 135 DCHECK(MediaStreamAudioTrack::GetTrack(audio_track_));
136 DVLOG(1) << "TrackAudioRenderer::TrackAudioRenderer()";
113 } 137 }
114 138
115 WebRtcLocalAudioRenderer::~WebRtcLocalAudioRenderer() { 139 TrackAudioRenderer::~TrackAudioRenderer() {
116 DCHECK(task_runner_->BelongsToCurrentThread()); 140 DCHECK(task_runner_->BelongsToCurrentThread());
117 DCHECK(!sink_.get()); 141 DCHECK(!sink_.get());
118 DVLOG(1) << "WebRtcLocalAudioRenderer::~WebRtcLocalAudioRenderer()"; 142 DVLOG(1) << "TrackAudioRenderer::~TrackAudioRenderer()";
119 } 143 }
120 144
121 void WebRtcLocalAudioRenderer::Start() { 145 void TrackAudioRenderer::Start() {
122 DVLOG(1) << "WebRtcLocalAudioRenderer::Start()"; 146 DVLOG(1) << "TrackAudioRenderer::Start()";
123 DCHECK(task_runner_->BelongsToCurrentThread()); 147 DCHECK(task_runner_->BelongsToCurrentThread());
148 DCHECK_EQ(playing_, false);
124 149
125 // We get audio data from |audio_track_|... 150 // We get audio data from |audio_track_|...
126 MediaStreamAudioSink::AddToAudioTrack(this, audio_track_); 151 MediaStreamAudioSink::AddToAudioTrack(this, audio_track_);
127 // ...and |sink_| will get audio data from us. 152 // ...and |sink_| will get audio data from us.
128 DCHECK(!sink_.get()); 153 DCHECK(!sink_.get());
129 sink_ = 154 sink_ =
130 AudioDeviceFactory::NewOutputDevice(source_render_frame_id_, session_id_, 155 AudioDeviceFactory::NewOutputDevice(playout_render_frame_id_, session_id_,
131 output_device_id_, security_origin_); 156 output_device_id_, security_origin_);
132 157
133 base::AutoLock auto_lock(thread_lock_); 158 base::AutoLock auto_lock(thread_lock_);
134 last_render_time_ = base::TimeTicks::Now(); 159 prior_elapsed_render_time_ = base::TimeDelta();
135 playing_ = false; 160 num_samples_rendered_ = 0;
136 } 161 }
137 162
138 void WebRtcLocalAudioRenderer::Stop() { 163 void TrackAudioRenderer::Stop() {
139 DVLOG(1) << "WebRtcLocalAudioRenderer::Stop()"; 164 DVLOG(1) << "TrackAudioRenderer::Stop()";
140 DCHECK(task_runner_->BelongsToCurrentThread()); 165 DCHECK(task_runner_->BelongsToCurrentThread());
141 166
142 { 167 Pause();
143 base::AutoLock auto_lock(thread_lock_);
144 playing_ = false;
145 audio_shifter_.reset();
146 }
147 168
148 // Stop the output audio stream, i.e, stop asking for data to render. 169 // Stop the output audio stream, i.e, stop asking for data to render.
149 // It is safer to call Stop() on the |sink_| to clean up the resources even 170 // It is safer to call Stop() on the |sink_| to clean up the resources even
150 // when the |sink_| is never started. 171 // when the |sink_| is never started.
151 if (sink_.get()) { 172 if (sink_.get()) {
152 sink_->Stop(); 173 sink_->Stop();
153 sink_ = NULL; 174 sink_ = NULL;
154 } 175 }
155 176
156 if (!sink_started_) { 177 if (!sink_started_ && IsLocalRenderer()) {
157 UMA_HISTOGRAM_ENUMERATION("Media.LocalRendererSinkStates", 178 UMA_HISTOGRAM_ENUMERATION("Media.LocalRendererSinkStates",
158 kSinkNeverStarted, kSinkStatesMax); 179 kSinkNeverStarted, kSinkStatesMax);
159 } 180 }
160 sink_started_ = false; 181 sink_started_ = false;
161 182
162 // Ensure that the capturer stops feeding us with captured audio. 183 // Ensure that the capturer stops feeding us with captured audio.
163 MediaStreamAudioSink::RemoveFromAudioTrack(this, audio_track_); 184 MediaStreamAudioSink::RemoveFromAudioTrack(this, audio_track_);
164 } 185 }
165 186
166 void WebRtcLocalAudioRenderer::Play() { 187 void TrackAudioRenderer::Play() {
167 DVLOG(1) << "WebRtcLocalAudioRenderer::Play()"; 188 DVLOG(1) << "TrackAudioRenderer::Play()";
168 DCHECK(task_runner_->BelongsToCurrentThread()); 189 DCHECK(task_runner_->BelongsToCurrentThread());
169 190
170 if (!sink_.get()) 191 if (!sink_.get())
171 return; 192 return;
172 193
173 { 194 playing_ = true;
174 base::AutoLock auto_lock(thread_lock_);
175 // Resumes rendering by ensuring that WebRtcLocalAudioRenderer::Render()
176 // now reads data from the local FIFO.
177 playing_ = true;
178 last_render_time_ = base::TimeTicks::Now();
179 }
180 195
181 // Note: If volume_ is currently muted, the |sink_| will not be started yet.
182 MaybeStartSink(); 196 MaybeStartSink();
183 } 197 }
184 198
185 void WebRtcLocalAudioRenderer::Pause() { 199 void TrackAudioRenderer::Pause() {
186 DVLOG(1) << "WebRtcLocalAudioRenderer::Pause()"; 200 DVLOG(1) << "TrackAudioRenderer::Pause()";
187 DCHECK(task_runner_->BelongsToCurrentThread()); 201 DCHECK(task_runner_->BelongsToCurrentThread());
188 202
189 if (!sink_.get()) 203 if (!sink_.get())
190 return; 204 return;
191 205
206 playing_ = false;
207
192 base::AutoLock auto_lock(thread_lock_); 208 base::AutoLock auto_lock(thread_lock_);
193 // Temporarily suspends rendering audio. 209 HaltAudioFlowWhileLockHeld();
194 // WebRtcLocalAudioRenderer::Render() will return early during this state
195 // and only zeros will be provided to the active sink.
196 playing_ = false;
197 } 210 }
198 211
199 void WebRtcLocalAudioRenderer::SetVolume(float volume) { 212 void TrackAudioRenderer::SetVolume(float volume) {
200 DVLOG(1) << "WebRtcLocalAudioRenderer::SetVolume(" << volume << ")"; 213 DVLOG(1) << "TrackAudioRenderer::SetVolume(" << volume << ")";
214 DCHECK(task_runner_->BelongsToCurrentThread());
215
216 // Cache the volume. Whenever |sink_| is re-created, call SetVolume() with
217 // this cached volume.
218 volume_ = volume;
219 if (sink_.get())
220 sink_->SetVolume(volume);
221 }
222
223 media::OutputDevice* TrackAudioRenderer::GetOutputDevice() {
224 DCHECK(task_runner_->BelongsToCurrentThread());
225 return this;
226 }
227
228 base::TimeDelta TrackAudioRenderer::GetCurrentRenderTime() const {
229 DCHECK(task_runner_->BelongsToCurrentThread());
230 base::AutoLock auto_lock(thread_lock_);
231 if (source_params_.IsValid()) {
232 return ComputeTotalElapsedRenderTime(prior_elapsed_render_time_,
233 num_samples_rendered_,
234 source_params_.sample_rate());
235 }
236 return prior_elapsed_render_time_;
237 }
238
239 bool TrackAudioRenderer::IsLocalRenderer() const {
240 DCHECK(task_runner_->BelongsToCurrentThread());
241 return MediaStreamAudioTrack::GetTrack(audio_track_)->is_local_track();
242 }
243
244 void TrackAudioRenderer::SwitchOutputDevice(
245 const std::string& device_id,
246 const url::Origin& security_origin,
247 const media::SwitchOutputDeviceCB& callback) {
248 DVLOG(1) << "TrackAudioRenderer::SwitchOutputDevice()";
201 DCHECK(task_runner_->BelongsToCurrentThread()); 249 DCHECK(task_runner_->BelongsToCurrentThread());
202 250
203 { 251 {
204 base::AutoLock auto_lock(thread_lock_); 252 base::AutoLock auto_lock(thread_lock_);
205 // Cache the volume. 253 HaltAudioFlowWhileLockHeld();
206 volume_ = volume;
207 } 254 }
208 255
209 // Lazily start the |sink_| when the local renderer is unmuted during
210 // playing.
211 MaybeStartSink();
212
213 if (sink_.get())
214 sink_->SetVolume(volume);
215 }
216
217 media::OutputDevice* WebRtcLocalAudioRenderer::GetOutputDevice() {
218 DCHECK(task_runner_->BelongsToCurrentThread());
219 return this;
220 }
221
222 base::TimeDelta WebRtcLocalAudioRenderer::GetCurrentRenderTime() const {
223 DCHECK(task_runner_->BelongsToCurrentThread());
224 base::AutoLock auto_lock(thread_lock_);
225 if (!sink_.get())
226 return base::TimeDelta();
227 return total_render_time();
228 }
229
230 bool WebRtcLocalAudioRenderer::IsLocalRenderer() const {
231 return true;
232 }
233
234 void WebRtcLocalAudioRenderer::SwitchOutputDevice(
235 const std::string& device_id,
236 const url::Origin& security_origin,
237 const media::SwitchOutputDeviceCB& callback) {
238 DVLOG(1) << "WebRtcLocalAudioRenderer::SwitchOutputDevice()";
239 DCHECK(task_runner_->BelongsToCurrentThread());
240
241 scoped_refptr<media::AudioOutputDevice> new_sink = 256 scoped_refptr<media::AudioOutputDevice> new_sink =
242 AudioDeviceFactory::NewOutputDevice(source_render_frame_id_, session_id_, 257 AudioDeviceFactory::NewOutputDevice(playout_render_frame_id_, session_id_,
243 device_id, security_origin); 258 device_id, security_origin);
244 if (new_sink->GetDeviceStatus() != media::OUTPUT_DEVICE_STATUS_OK) { 259 if (new_sink->GetDeviceStatus() != media::OUTPUT_DEVICE_STATUS_OK) {
245 callback.Run(new_sink->GetDeviceStatus()); 260 callback.Run(new_sink->GetDeviceStatus());
246 return; 261 return;
247 } 262 }
248 263
249 output_device_id_ = device_id; 264 output_device_id_ = device_id;
250 security_origin_ = security_origin; 265 security_origin_ = security_origin;
251 bool was_sink_started = sink_started_; 266 bool was_sink_started = sink_started_;
252 267
253 if (sink_.get()) 268 if (sink_.get())
254 sink_->Stop(); 269 sink_->Stop();
255 270
256 sink_started_ = false; 271 sink_started_ = false;
257 sink_ = new_sink; 272 sink_ = new_sink;
258 int frames_per_buffer = sink_->GetOutputParameters().frames_per_buffer();
259 sink_params_ = source_params_;
260 sink_params_.set_frames_per_buffer(WebRtcAudioRenderer::GetOptimalBufferSize(
261 source_params_.sample_rate(), frames_per_buffer));
262
263 if (was_sink_started) 273 if (was_sink_started)
264 MaybeStartSink(); 274 MaybeStartSink();
265 275
266 callback.Run(media::OUTPUT_DEVICE_STATUS_OK); 276 callback.Run(media::OUTPUT_DEVICE_STATUS_OK);
267 } 277 }
268 278
269 media::AudioParameters WebRtcLocalAudioRenderer::GetOutputParameters() { 279 media::AudioParameters TrackAudioRenderer::GetOutputParameters() {
270 DCHECK(task_runner_->BelongsToCurrentThread()); 280 DCHECK(task_runner_->BelongsToCurrentThread());
271 if (!sink_.get()) 281 if (!sink_ || !source_params_.IsValid())
272 return media::AudioParameters(); 282 return media::AudioParameters();
273 283
274 return sink_->GetOutputParameters(); 284 // Output parameters consist of the same channel layout and sample rate as the
285 // source, but having the buffer duration preferred by the hardware.
286 const media::AudioParameters& preferred_params = sink_->GetOutputParameters();
287 return media::AudioParameters(
288 preferred_params.format(), source_params_.channel_layout(),
289 source_params_.sample_rate(), source_params_.bits_per_sample(),
290 preferred_params.frames_per_buffer() * source_params_.sample_rate() /
291 preferred_params.sample_rate());
275 } 292 }
276 293
277 media::OutputDeviceStatus WebRtcLocalAudioRenderer::GetDeviceStatus() { 294 media::OutputDeviceStatus TrackAudioRenderer::GetDeviceStatus() {
278 DCHECK(task_runner_->BelongsToCurrentThread()); 295 DCHECK(task_runner_->BelongsToCurrentThread());
279 if (!sink_.get()) 296 if (!sink_.get())
280 return media::OUTPUT_DEVICE_STATUS_ERROR_INTERNAL; 297 return media::OUTPUT_DEVICE_STATUS_ERROR_INTERNAL;
281 298
282 return sink_->GetDeviceStatus(); 299 return sink_->GetDeviceStatus();
283 } 300 }
284 301
285 void WebRtcLocalAudioRenderer::MaybeStartSink() { 302 void TrackAudioRenderer::MaybeStartSink() {
286 DCHECK(task_runner_->BelongsToCurrentThread()); 303 DCHECK(task_runner_->BelongsToCurrentThread());
287 DVLOG(1) << "WebRtcLocalAudioRenderer::MaybeStartSink()"; 304 DVLOG(1) << "TrackAudioRenderer::MaybeStartSink()";
288 305
289 if (!sink_.get() || !source_params_.IsValid()) 306 if (!sink_.get() || !source_params_.IsValid() || !playing_)
290 return; 307 return;
291 308
292 { 309 // Re-create the AudioShifter to drop old audio data and reset to a starting
293 // Clear up the old data in the FIFO. 310 // state. MaybeStartSink() is always called in a situation where either the
294 base::AutoLock auto_lock(thread_lock_); 311 // source or sink has changed somehow and so all of AudioShifter's internal
295 audio_shifter_->Flush(); 312 // time-sync state is invalid.
313 CreateAudioShifter();
314
315 if (sink_started_ ||
316 sink_->GetDeviceStatus() != media::OUTPUT_DEVICE_STATUS_OK) {
317 return;
296 } 318 }
297 319
298 if (!sink_params_.IsValid() || !playing_ || !volume_ || sink_started_ || 320 DVLOG(1) << ("TrackAudioRenderer::MaybeStartSink() -- Starting sink. "
299 sink_->GetDeviceStatus() != media::OUTPUT_DEVICE_STATUS_OK) 321 "source_params_={")
300 return; 322 << source_params_.AsHumanReadableString() << "}, sink parameters={"
301 323 << GetOutputParameters().AsHumanReadableString() << '}';
302 DVLOG(1) << "WebRtcLocalAudioRenderer::MaybeStartSink() -- Starting sink_."; 324 sink_->Initialize(GetOutputParameters(), this);
303 sink_->Initialize(sink_params_, this);
304 sink_->Start(); 325 sink_->Start();
326 sink_->SetVolume(volume_);
305 sink_started_ = true; 327 sink_started_ = true;
306 UMA_HISTOGRAM_ENUMERATION("Media.LocalRendererSinkStates", 328 if (IsLocalRenderer()) {
307 kSinkStarted, kSinkStatesMax); 329 UMA_HISTOGRAM_ENUMERATION("Media.LocalRendererSinkStates", kSinkStarted,
330 kSinkStatesMax);
331 }
308 } 332 }
309 333
310 void WebRtcLocalAudioRenderer::ReconfigureSink( 334 void TrackAudioRenderer::ReconfigureSink(const media::AudioParameters& params) {
311 const media::AudioParameters& params) {
312 DCHECK(task_runner_->BelongsToCurrentThread()); 335 DCHECK(task_runner_->BelongsToCurrentThread());
313 336
314 DVLOG(1) << "WebRtcLocalAudioRenderer::ReconfigureSink()"; 337 DVLOG(1) << "TrackAudioRenderer::ReconfigureSink()";
315 338
316 if (source_params_.Equals(params)) 339 if (source_params_.Equals(params))
317 return; 340 return;
318
319 // Reset the |source_params_|, |sink_params_| and |loopback_fifo_| to match
320 // the new format.
321
322 source_params_ = params; 341 source_params_ = params;
323 {
324 // Note: The max buffer is fairly large, but will rarely be used.
325 // Cast needs the buffer to hold at least one second of audio.
326 // The clock accuracy is set to 20ms because clock accuracy is
327 // ~15ms on windows.
328 media::AudioShifter* const new_shifter = new media::AudioShifter(
329 base::TimeDelta::FromSeconds(2),
330 base::TimeDelta::FromMilliseconds(20),
331 base::TimeDelta::FromSeconds(20),
332 source_params_.sample_rate(),
333 params.channels());
334
335 base::AutoLock auto_lock(thread_lock_);
336 audio_shifter_.reset(new_shifter);
337 }
338 342
339 if (!sink_.get()) 343 if (!sink_.get())
340 return; // WebRtcLocalAudioRenderer has not yet been started. 344 return; // TrackAudioRenderer has not yet been started.
341 345
342 // Stop |sink_| and re-create a new one to be initialized with different audio 346 // Stop |sink_| and re-create a new one to be initialized with different audio
343 // parameters. Then, invoke MaybeStartSink() to restart everything again. 347 // parameters. Then, invoke MaybeStartSink() to restart everything again.
344 sink_->Stop(); 348 sink_->Stop();
345 sink_started_ = false; 349 sink_started_ = false;
346 sink_ = 350 sink_ =
347 AudioDeviceFactory::NewOutputDevice(source_render_frame_id_, session_id_, 351 AudioDeviceFactory::NewOutputDevice(playout_render_frame_id_, session_id_,
348 output_device_id_, security_origin_); 352 output_device_id_, security_origin_);
349 int frames_per_buffer = sink_->GetOutputParameters().frames_per_buffer();
350 sink_params_ = source_params_;
351 sink_params_.set_frames_per_buffer(WebRtcAudioRenderer::GetOptimalBufferSize(
352 source_params_.sample_rate(), frames_per_buffer));
353 MaybeStartSink(); 353 MaybeStartSink();
354 } 354 }
355 355
356 void TrackAudioRenderer::CreateAudioShifter() {
357 DCHECK(task_runner_->BelongsToCurrentThread());
358
359 // Note 1: The max buffer is fairly large to cover the case where
360 // remotely-sourced audio is delivered well ahead of its scheduled playout
361 // time (e.g., content streaming with a very large end-to-end
362 // latency). However, there is no penalty for making it large in the
363 // low-latency use cases since AudioShifter will discard data as soon as it is
364 // no longer needed.
365 //
366 // Note 2: The clock accuracy is set to 20ms because clock accuracy is
367 // ~15ms on Windows machines without a working high-resolution clock. See
368 // comments in base/time/time.h for details.
369 media::AudioShifter* const new_shifter = new media::AudioShifter(
370 base::TimeDelta::FromSeconds(5), base::TimeDelta::FromMilliseconds(20),
371 base::TimeDelta::FromSeconds(20), source_params_.sample_rate(),
372 source_params_.channels());
373
374 base::AutoLock auto_lock(thread_lock_);
375 audio_shifter_.reset(new_shifter);
376 }
377
378 void TrackAudioRenderer::HaltAudioFlowWhileLockHeld() {
379 thread_lock_.AssertAcquired();
380
381 audio_shifter_.reset();
382
383 if (source_params_.IsValid()) {
384 prior_elapsed_render_time_ =
385 ComputeTotalElapsedRenderTime(prior_elapsed_render_time_,
386 num_samples_rendered_,
387 source_params_.sample_rate());
388 num_samples_rendered_ = 0;
389 }
390 }
391
356 } // namespace content 392 } // namespace content
OLDNEW
« no previous file with comments | « content/renderer/media/track_audio_renderer.h ('k') | content/renderer/media/webrtc_local_audio_renderer.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698