Add some instrumentation for jank in URLRequest::Start.
[chromium-blink-merge.git] / media / audio / null_audio_sink.cc
blobe737c437c9c8d47a2af1ae90a805cfa1f49bb4ba
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
3 // found in the LICENSE file.
5 #include "media/audio/null_audio_sink.h"
7 #include "base/bind.h"
8 #include "base/single_thread_task_runner.h"
9 #include "media/audio/fake_audio_worker.h"
10 #include "media/base/audio_hash.h"
12 namespace media {
14 NullAudioSink::NullAudioSink(
15 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner)
16 : initialized_(false),
17 playing_(false),
18 callback_(NULL),
19 task_runner_(task_runner) {
22 NullAudioSink::~NullAudioSink() {}
24 void NullAudioSink::Initialize(const AudioParameters& params,
25 RenderCallback* callback) {
26 DCHECK(!initialized_);
27 fake_worker_.reset(new FakeAudioWorker(task_runner_, params));
28 audio_bus_ = AudioBus::Create(params);
29 callback_ = callback;
30 initialized_ = true;
33 void NullAudioSink::Start() {
34 DCHECK(task_runner_->BelongsToCurrentThread());
35 DCHECK(!playing_);
38 void NullAudioSink::Stop() {
39 DCHECK(task_runner_->BelongsToCurrentThread());
41 // Stop may be called at any time, so we have to check before stopping.
42 if (fake_worker_)
43 fake_worker_->Stop();
46 void NullAudioSink::Play() {
47 DCHECK(task_runner_->BelongsToCurrentThread());
48 DCHECK(initialized_);
50 if (playing_)
51 return;
53 fake_worker_->Start(base::Bind(
54 &NullAudioSink::CallRender, base::Unretained(this)));
55 playing_ = true;
58 void NullAudioSink::Pause() {
59 DCHECK(task_runner_->BelongsToCurrentThread());
61 if (!playing_)
62 return;
64 fake_worker_->Stop();
65 playing_ = false;
68 bool NullAudioSink::SetVolume(double volume) {
69 // Audio is always muted.
70 return volume == 0.0;
73 void NullAudioSink::CallRender() {
74 DCHECK(task_runner_->BelongsToCurrentThread());
76 int frames_received = callback_->Render(audio_bus_.get(), 0);
77 if (!audio_hash_ || frames_received <= 0)
78 return;
80 audio_hash_->Update(audio_bus_.get(), frames_received);
83 void NullAudioSink::StartAudioHashForTesting() {
84 DCHECK(!initialized_);
85 audio_hash_.reset(new AudioHash());
88 std::string NullAudioSink::GetAudioHashForTesting() {
89 return audio_hash_ ? audio_hash_->ToString() : std::string();
92 } // namespace media