OLD | NEW |
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/track_audio_renderer.h" | 5 #include "content/renderer/media/track_audio_renderer.h" |
6 | 6 |
7 #include "base/location.h" | 7 #include "base/location.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/metrics/histogram.h" | 9 #include "base/metrics/histogram.h" |
10 #include "base/synchronization/lock.h" | 10 #include "base/synchronization/lock.h" |
11 #include "base/thread_task_runner_handle.h" | 11 #include "base/thread_task_runner_handle.h" |
12 #include "base/trace_event/trace_event.h" | 12 #include "base/trace_event/trace_event.h" |
13 #include "content/renderer/media/audio_device_factory.h" | 13 #include "content/renderer/media/audio_device_factory.h" |
14 #include "content/renderer/media/media_stream_audio_track.h" | 14 #include "content/renderer/media/media_stream_audio_track.h" |
| 15 #include "content/renderer/media/webrtc_audio_renderer.h" |
15 #include "media/base/audio_bus.h" | 16 #include "media/base/audio_bus.h" |
16 #include "media/base/audio_shifter.h" | 17 #include "media/base/audio_shifter.h" |
17 | 18 |
18 namespace content { | 19 namespace content { |
19 | 20 |
20 namespace { | 21 namespace { |
21 | 22 |
22 enum LocalRendererSinkStates { | 23 enum LocalRendererSinkStates { |
23 kSinkStarted = 0, | 24 kSinkStarted = 0, |
24 kSinkNeverStarted, | 25 kSinkNeverStarted, |
25 kSinkStatesMax // Must always be last! | 26 kSinkStatesMax // Must always be last! |
26 }; | 27 }; |
27 | 28 |
28 // Translates |num_samples_rendered| into a TimeDelta duration and adds it to | 29 // Translates |num_samples_rendered| into a TimeDelta duration and adds it to |
29 // |prior_elapsed_render_time|. | 30 // |prior_elapsed_render_time|. |
30 base::TimeDelta ComputeTotalElapsedRenderTime( | 31 base::TimeDelta ComputeTotalElapsedRenderTime( |
31 base::TimeDelta prior_elapsed_render_time, | 32 base::TimeDelta prior_elapsed_render_time, |
32 int64_t num_samples_rendered, | 33 int64_t num_samples_rendered, |
33 int sample_rate) { | 34 int sample_rate) { |
34 return prior_elapsed_render_time + base::TimeDelta::FromMicroseconds( | 35 return prior_elapsed_render_time + base::TimeDelta::FromMicroseconds( |
35 num_samples_rendered * base::Time::kMicrosecondsPerSecond / sample_rate); | 36 num_samples_rendered * base::Time::kMicrosecondsPerSecond / sample_rate); |
36 } | 37 } |
37 | 38 |
38 } // namespace | 39 } // namespace |
39 | 40 |
40 // media::AudioRendererSink::RenderCallback implementation | 41 // media::AudioRendererSink::RenderCallback implementation |
41 int TrackAudioRenderer::Render(media::AudioBus* audio_bus, | 42 int TrackAudioRenderer::Render(media::AudioBus* audio_bus, |
42 uint32_t audio_delay_milliseconds, | 43 uint32_t frames_delayed, |
43 uint32_t frames_skipped) { | 44 uint32_t frames_skipped) { |
44 TRACE_EVENT0("audio", "TrackAudioRenderer::Render"); | 45 TRACE_EVENT0("audio", "TrackAudioRenderer::Render"); |
45 base::AutoLock auto_lock(thread_lock_); | 46 base::AutoLock auto_lock(thread_lock_); |
46 | 47 |
47 if (!audio_shifter_) { | 48 if (!audio_shifter_) { |
48 audio_bus->Zero(); | 49 audio_bus->Zero(); |
49 return 0; | 50 return 0; |
50 } | 51 } |
51 | 52 |
| 53 // Source sample rate equals to output one, see MaybeStartSink(), so using it. |
| 54 uint32_t audio_delay_milliseconds = static_cast<double>(frames_delayed) * |
| 55 base::Time::kMillisecondsPerSecond / |
| 56 source_params_.sample_rate(); |
| 57 |
52 // TODO(miu): Plumbing is needed to determine the actual playout timestamp | 58 // TODO(miu): Plumbing is needed to determine the actual playout timestamp |
53 // of the audio, instead of just snapshotting TimeTicks::Now(), for proper | 59 // of the audio, instead of just snapshotting TimeTicks::Now(), for proper |
54 // audio/video sync. http://crbug.com/335335 | 60 // audio/video sync. http://crbug.com/335335 |
55 const base::TimeTicks playout_time = | 61 const base::TimeTicks playout_time = |
56 base::TimeTicks::Now() + | 62 base::TimeTicks::Now() + |
57 base::TimeDelta::FromMilliseconds(audio_delay_milliseconds); | 63 base::TimeDelta::FromMilliseconds(audio_delay_milliseconds); |
58 DVLOG(2) << "Pulling audio out of shifter to be played " | 64 DVLOG(2) << "Pulling audio out of shifter to be played " |
59 << audio_delay_milliseconds << " ms from now."; | 65 << audio_delay_milliseconds << " ms from now."; |
60 audio_shifter_->Pull(audio_bus, playout_time); | 66 audio_shifter_->Pull(audio_bus, playout_time); |
61 num_samples_rendered_ += audio_bus->frames(); | 67 num_samples_rendered_ += audio_bus->frames(); |
(...skipping 236 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
298 const media::OutputDeviceInfo& device_info = sink_->GetOutputDeviceInfo(); | 304 const media::OutputDeviceInfo& device_info = sink_->GetOutputDeviceInfo(); |
299 if (device_info.device_status() != media::OUTPUT_DEVICE_STATUS_OK) | 305 if (device_info.device_status() != media::OUTPUT_DEVICE_STATUS_OK) |
300 return; | 306 return; |
301 | 307 |
302 // Output parameters consist of the same channel layout and sample rate as the | 308 // Output parameters consist of the same channel layout and sample rate as the |
303 // source, but having the buffer duration preferred by the hardware. | 309 // source, but having the buffer duration preferred by the hardware. |
304 const media::AudioParameters& hardware_params = device_info.output_params(); | 310 const media::AudioParameters& hardware_params = device_info.output_params(); |
305 media::AudioParameters sink_params( | 311 media::AudioParameters sink_params( |
306 hardware_params.format(), source_params_.channel_layout(), | 312 hardware_params.format(), source_params_.channel_layout(), |
307 source_params_.sample_rate(), source_params_.bits_per_sample(), | 313 source_params_.sample_rate(), source_params_.bits_per_sample(), |
308 hardware_params.frames_per_buffer() * source_params_.sample_rate() / | 314 WebRtcAudioRenderer::GetOptimalBufferSize( |
309 hardware_params.sample_rate()); | 315 source_params_.sample_rate(), hardware_params.frames_per_buffer())); |
310 DVLOG(1) << ("TrackAudioRenderer::MaybeStartSink() -- Starting sink. " | 316 DVLOG(1) << ("TrackAudioRenderer::MaybeStartSink() -- Starting sink. " |
311 "source_params_={") | 317 "source_params_={") |
312 << source_params_.AsHumanReadableString() << "}, sink parameters={" | 318 << source_params_.AsHumanReadableString() << "}, hardware_params_={" |
| 319 << hardware_params.AsHumanReadableString() << "}, sink parameters={" |
313 << sink_params.AsHumanReadableString() << '}'; | 320 << sink_params.AsHumanReadableString() << '}'; |
314 sink_->Initialize(sink_params, this); | 321 sink_->Initialize(sink_params, this); |
315 sink_->Start(); | 322 sink_->Start(); |
316 sink_->SetVolume(volume_); | 323 sink_->SetVolume(volume_); |
317 sink_->Play(); // Not all the sinks play on start. | 324 sink_->Play(); // Not all the sinks play on start. |
318 sink_started_ = true; | 325 sink_started_ = true; |
319 if (IsLocalRenderer()) { | 326 if (IsLocalRenderer()) { |
320 UMA_HISTOGRAM_ENUMERATION("Media.LocalRendererSinkStates", kSinkStarted, | 327 UMA_HISTOGRAM_ENUMERATION("Media.LocalRendererSinkStates", kSinkStarted, |
321 kSinkStatesMax); | 328 kSinkStatesMax); |
322 } | 329 } |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
374 if (source_params_.IsValid()) { | 381 if (source_params_.IsValid()) { |
375 prior_elapsed_render_time_ = | 382 prior_elapsed_render_time_ = |
376 ComputeTotalElapsedRenderTime(prior_elapsed_render_time_, | 383 ComputeTotalElapsedRenderTime(prior_elapsed_render_time_, |
377 num_samples_rendered_, | 384 num_samples_rendered_, |
378 source_params_.sample_rate()); | 385 source_params_.sample_rate()); |
379 num_samples_rendered_ = 0; | 386 num_samples_rendered_ = 0; |
380 } | 387 } |
381 } | 388 } |
382 | 389 |
383 } // namespace content | 390 } // namespace content |
OLD | NEW |