Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / media / audio / null_audio_sink.cc
blobfc5e900d7059c053ea5507d64fb8b13774a767d5
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/location.h"
9 #include "base/single_thread_task_runner.h"
10 #include "media/audio/fake_audio_worker.h"
11 #include "media/base/audio_hash.h"
13 namespace media {
15 NullAudioSink::NullAudioSink(
16 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner)
17 : initialized_(false),
18 playing_(false),
19 callback_(NULL),
20 task_runner_(task_runner) {
23 NullAudioSink::~NullAudioSink() {}
25 void NullAudioSink::Initialize(const AudioParameters& params,
26 RenderCallback* callback) {
27 DCHECK(!initialized_);
28 fake_worker_.reset(new FakeAudioWorker(task_runner_, params));
29 audio_bus_ = AudioBus::Create(params);
30 callback_ = callback;
31 initialized_ = true;
34 void NullAudioSink::Start() {
35 DCHECK(task_runner_->BelongsToCurrentThread());
36 DCHECK(!playing_);
39 void NullAudioSink::Stop() {
40 DCHECK(task_runner_->BelongsToCurrentThread());
42 // Stop may be called at any time, so we have to check before stopping.
43 if (fake_worker_)
44 fake_worker_->Stop();
47 void NullAudioSink::Play() {
48 DCHECK(task_runner_->BelongsToCurrentThread());
49 DCHECK(initialized_);
51 if (playing_)
52 return;
54 fake_worker_->Start(base::Bind(
55 &NullAudioSink::CallRender, base::Unretained(this)));
56 playing_ = true;
59 void NullAudioSink::Pause() {
60 DCHECK(task_runner_->BelongsToCurrentThread());
62 if (!playing_)
63 return;
65 fake_worker_->Stop();
66 playing_ = false;
69 bool NullAudioSink::SetVolume(double volume) {
70 // Audio is always muted.
71 return volume == 0.0;
74 void NullAudioSink::SwitchOutputDevice(const std::string& device_id,
75 const GURL& security_origin,
76 const SwitchOutputDeviceCB& callback) {
77 DCHECK(task_runner_->BelongsToCurrentThread());
78 callback.Run(SWITCH_OUTPUT_DEVICE_RESULT_ERROR_NOT_SUPPORTED);
81 void NullAudioSink::CallRender() {
82 DCHECK(task_runner_->BelongsToCurrentThread());
84 int frames_received = callback_->Render(audio_bus_.get(), 0);
85 if (!audio_hash_ || frames_received <= 0)
86 return;
88 audio_hash_->Update(audio_bus_.get(), frames_received);
91 void NullAudioSink::StartAudioHashForTesting() {
92 DCHECK(!initialized_);
93 audio_hash_.reset(new AudioHash());
96 std::string NullAudioSink::GetAudioHashForTesting() {
97 return audio_hash_ ? audio_hash_->ToString() : std::string();
100 } // namespace media