1 // Copyright 2013 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.
5 #include "media/audio/clockless_audio_sink.h"
7 #include "base/threading/simple_thread.h"
8 #include "base/time/time.h"
9 #include "media/base/audio_renderer_sink.h"
13 // Internal to ClocklessAudioSink. Class is used to call Render() on a seperate
14 // thread, running as fast as it can read the data.
15 class ClocklessAudioSinkThread
: public base::DelegateSimpleThread::Delegate
{
17 explicit ClocklessAudioSinkThread(const AudioParameters
& params
,
18 AudioRendererSink::RenderCallback
* callback
)
19 : callback_(callback
),
20 audio_bus_(AudioBus::Create(params
)),
21 stop_event_(new base::WaitableEvent(false, false)) {}
25 thread_
.reset(new base::DelegateSimpleThread(this, "ClocklessAudioSink"));
29 // Generate a signal to stop calling Render().
30 base::TimeDelta
Stop() {
31 stop_event_
->Signal();
33 return playback_time_
;
37 // Call Render() repeatedly, keeping track of the rendering time.
38 virtual void Run() OVERRIDE
{
39 base::TimeTicks start
;
40 while (!stop_event_
->IsSignaled()) {
41 int frames_received
= callback_
->Render(audio_bus_
.get(), 0);
42 if (frames_received
<= 0) {
43 // No data received, so let other threads run to provide data.
44 base::PlatformThread::YieldCurrentThread();
45 } else if (start
.is_null()) {
46 // First time we processed some audio, so record the starting time.
47 start
= base::TimeTicks::HighResNow();
49 // Keep track of the last time data was rendered.
50 playback_time_
= base::TimeTicks::HighResNow() - start
;
55 AudioRendererSink::RenderCallback
* callback_
;
56 scoped_ptr
<AudioBus
> audio_bus_
;
57 scoped_ptr
<base::WaitableEvent
> stop_event_
;
58 scoped_ptr
<base::DelegateSimpleThread
> thread_
;
59 base::TimeDelta playback_time_
;
62 ClocklessAudioSink::ClocklessAudioSink()
63 : initialized_(false),
66 ClocklessAudioSink::~ClocklessAudioSink() {}
68 void ClocklessAudioSink::Initialize(const AudioParameters
& params
,
69 RenderCallback
* callback
) {
70 DCHECK(!initialized_
);
71 thread_
.reset(new ClocklessAudioSinkThread(params
, callback
));
75 void ClocklessAudioSink::Start() {
79 void ClocklessAudioSink::Stop() {
85 playback_time_
= thread_
->Stop();
88 void ClocklessAudioSink::Play() {
98 void ClocklessAudioSink::Pause() {
102 bool ClocklessAudioSink::SetVolume(double volume
) {
103 // Audio is always muted.
104 return volume
== 0.0;