Create an initial chrome://supervised-user-internals page
[chromium-blink-merge.git] / media / audio / clockless_audio_sink.cc
blob940ab2ea69b407b7da4f6f65b16064f2963a3c17
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/bind.h"
8 #include "base/location.h"
9 #include "base/single_thread_task_runner.h"
10 #include "base/threading/simple_thread.h"
11 #include "media/base/audio_hash.h"
13 namespace media {
15 // Internal to ClocklessAudioSink. Class is used to call Render() on a seperate
16 // thread, running as fast as it can read the data.
17 class ClocklessAudioSinkThread : public base::DelegateSimpleThread::Delegate {
18 public:
19 ClocklessAudioSinkThread(const AudioParameters& params,
20 AudioRendererSink::RenderCallback* callback,
21 bool hashing)
22 : callback_(callback),
23 audio_bus_(AudioBus::Create(params)),
24 stop_event_(new base::WaitableEvent(false, false)) {
25 if (hashing)
26 audio_hash_.reset(new AudioHash());
29 void Start() {
30 stop_event_->Reset();
31 thread_.reset(new base::DelegateSimpleThread(this, "ClocklessAudioSink"));
32 thread_->Start();
35 // Generate a signal to stop calling Render().
36 base::TimeDelta Stop() {
37 stop_event_->Signal();
38 thread_->Join();
39 return playback_time_;
42 std::string GetAudioHash() {
43 DCHECK(audio_hash_);
44 return audio_hash_->ToString();
47 private:
48 // Call Render() repeatedly, keeping track of the rendering time.
49 void Run() override {
50 base::TimeTicks start;
51 while (!stop_event_->IsSignaled()) {
52 const int frames_received = callback_->Render(audio_bus_.get(), 0);
53 DCHECK_GE(frames_received, 0);
54 if (audio_hash_)
55 audio_hash_->Update(audio_bus_.get(), frames_received);
56 if (!frames_received) {
57 // No data received, so let other threads run to provide data.
58 base::PlatformThread::YieldCurrentThread();
59 } else if (start.is_null()) {
60 // First time we processed some audio, so record the starting time.
61 start = base::TimeTicks::Now();
62 } else {
63 // Keep track of the last time data was rendered.
64 playback_time_ = base::TimeTicks::Now() - start;
69 AudioRendererSink::RenderCallback* callback_;
70 scoped_ptr<AudioBus> audio_bus_;
71 scoped_ptr<base::WaitableEvent> stop_event_;
72 scoped_ptr<base::DelegateSimpleThread> thread_;
73 base::TimeDelta playback_time_;
74 scoped_ptr<AudioHash> audio_hash_;
77 ClocklessAudioSink::ClocklessAudioSink()
78 : initialized_(false), playing_(false), hashing_(false) {}
80 ClocklessAudioSink::~ClocklessAudioSink() {}
82 void ClocklessAudioSink::Initialize(const AudioParameters& params,
83 RenderCallback* callback) {
84 DCHECK(!initialized_);
85 thread_.reset(new ClocklessAudioSinkThread(params, callback, hashing_));
86 initialized_ = true;
89 void ClocklessAudioSink::Start() {
90 DCHECK(initialized_);
91 DCHECK(!playing_);
94 void ClocklessAudioSink::Stop() {
95 if (initialized_)
96 Pause();
99 void ClocklessAudioSink::Play() {
100 DCHECK(initialized_);
102 if (playing_)
103 return;
105 playing_ = true;
106 thread_->Start();
109 void ClocklessAudioSink::Pause() {
110 DCHECK(initialized_);
112 if (!playing_)
113 return;
115 playing_ = false;
116 playback_time_ = thread_->Stop();
119 bool ClocklessAudioSink::SetVolume(double volume) {
120 // Audio is always muted.
121 return volume == 0.0;
124 OutputDevice* ClocklessAudioSink::GetOutputDevice() {
125 return nullptr;
128 void ClocklessAudioSink::StartAudioHashForTesting() {
129 DCHECK(!initialized_);
130 hashing_ = true;
133 std::string ClocklessAudioSink::GetAudioHashForTesting() {
134 return thread_ && hashing_ ? thread_->GetAudioHash() : std::string();
137 } // namespace media