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"
11 // Internal to ClocklessAudioSink. Class is used to call Render() on a seperate
12 // thread, running as fast as it can read the data.
13 class ClocklessAudioSinkThread
: public base::DelegateSimpleThread::Delegate
{
15 explicit ClocklessAudioSinkThread(const AudioParameters
& params
,
16 AudioRendererSink::RenderCallback
* callback
)
17 : callback_(callback
),
18 audio_bus_(AudioBus::Create(params
)),
19 stop_event_(new base::WaitableEvent(false, false)) {}
23 thread_
.reset(new base::DelegateSimpleThread(this, "ClocklessAudioSink"));
27 // Generate a signal to stop calling Render().
28 base::TimeDelta
Stop() {
29 stop_event_
->Signal();
31 return playback_time_
;
35 // Call Render() repeatedly, keeping track of the rendering time.
37 base::TimeTicks start
;
38 while (!stop_event_
->IsSignaled()) {
39 int frames_received
= callback_
->Render(audio_bus_
.get(), 0);
40 if (frames_received
<= 0) {
41 // No data received, so let other threads run to provide data.
42 base::PlatformThread::YieldCurrentThread();
43 } else if (start
.is_null()) {
44 // First time we processed some audio, so record the starting time.
45 start
= base::TimeTicks::Now();
47 // Keep track of the last time data was rendered.
48 playback_time_
= base::TimeTicks::Now() - start
;
53 AudioRendererSink::RenderCallback
* callback_
;
54 scoped_ptr
<AudioBus
> audio_bus_
;
55 scoped_ptr
<base::WaitableEvent
> stop_event_
;
56 scoped_ptr
<base::DelegateSimpleThread
> thread_
;
57 base::TimeDelta playback_time_
;
60 ClocklessAudioSink::ClocklessAudioSink()
61 : initialized_(false),
64 ClocklessAudioSink::~ClocklessAudioSink() {}
66 void ClocklessAudioSink::Initialize(const AudioParameters
& params
,
67 RenderCallback
* callback
) {
68 DCHECK(!initialized_
);
69 thread_
.reset(new ClocklessAudioSinkThread(params
, callback
));
73 void ClocklessAudioSink::Start() {
78 void ClocklessAudioSink::Stop() {
83 void ClocklessAudioSink::Play() {
93 void ClocklessAudioSink::Pause() {
100 playback_time_
= thread_
->Stop();
103 bool ClocklessAudioSink::SetVolume(double volume
) {
104 // Audio is always muted.
105 return volume
== 0.0;