This source file includes following definitions.
- stop_event_
- Start
- Stop
- Run
- playing_
- Initialize
- Start
- Stop
- Play
- Pause
- SetVolume
#include "media/audio/clockless_audio_sink.h"
#include "base/threading/simple_thread.h"
#include "base/time/time.h"
#include "media/base/audio_renderer_sink.h"
namespace media {
class ClocklessAudioSinkThread : public base::DelegateSimpleThread::Delegate {
public:
explicit ClocklessAudioSinkThread(const AudioParameters& params,
AudioRendererSink::RenderCallback* callback)
: callback_(callback),
audio_bus_(AudioBus::Create(params)),
stop_event_(new base::WaitableEvent(false, false)) {}
void Start() {
stop_event_->Reset();
thread_.reset(new base::DelegateSimpleThread(this, "ClocklessAudioSink"));
thread_->Start();
}
base::TimeDelta Stop() {
stop_event_->Signal();
thread_->Join();
return playback_time_;
}
private:
virtual void Run() OVERRIDE {
base::TimeTicks start;
while (!stop_event_->IsSignaled()) {
int frames_received = callback_->Render(audio_bus_.get(), 0);
if (frames_received <= 0) {
base::PlatformThread::YieldCurrentThread();
} else if (start.is_null()) {
start = base::TimeTicks::HighResNow();
} else {
playback_time_ = base::TimeTicks::HighResNow() - start;
}
}
}
AudioRendererSink::RenderCallback* callback_;
scoped_ptr<AudioBus> audio_bus_;
scoped_ptr<base::WaitableEvent> stop_event_;
scoped_ptr<base::DelegateSimpleThread> thread_;
base::TimeDelta playback_time_;
};
ClocklessAudioSink::ClocklessAudioSink()
: initialized_(false),
playing_(false) {}
ClocklessAudioSink::~ClocklessAudioSink() {}
void ClocklessAudioSink::Initialize(const AudioParameters& params,
RenderCallback* callback) {
DCHECK(!initialized_);
thread_.reset(new ClocklessAudioSinkThread(params, callback));
initialized_ = true;
}
void ClocklessAudioSink::Start() {
DCHECK(!playing_);
}
void ClocklessAudioSink::Stop() {
DCHECK(initialized_);
if (!playing_)
return;
playback_time_ = thread_->Stop();
}
void ClocklessAudioSink::Play() {
DCHECK(initialized_);
if (playing_)
return;
playing_ = true;
thread_->Start();
}
void ClocklessAudioSink::Pause() {
Stop();
}
bool ClocklessAudioSink::SetVolume(double volume) {
return volume == 0.0;
}
}