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/fake_audio_input_stream.h"
8 #include "base/bind_helpers.h"
9 #include "base/command_line.h"
10 #include "base/files/file_path.h"
11 #include "base/single_thread_task_runner.h"
12 #include "base/time/time.h"
13 #include "media/audio/audio_manager_base.h"
14 #include "media/audio/simple_sources.h"
15 #include "media/base/audio_bus.h"
16 #include "media/base/media_switches.h"
20 AudioInputStream
* FakeAudioInputStream::MakeFakeStream(
21 AudioManagerBase
* manager
,
22 const AudioParameters
& params
) {
23 return new FakeAudioInputStream(manager
, params
);
26 FakeAudioInputStream::FakeAudioInputStream(AudioManagerBase
* manager
,
27 const AudioParameters
& params
)
28 : audio_manager_(manager
),
30 fake_audio_worker_(manager
->GetWorkerTaskRunner(), params
),
32 audio_bus_(AudioBus::Create(params
)) {
33 DCHECK(audio_manager_
->GetTaskRunner()->BelongsToCurrentThread());
36 FakeAudioInputStream::~FakeAudioInputStream() {
40 bool FakeAudioInputStream::Open() {
41 DCHECK(audio_manager_
->GetTaskRunner()->BelongsToCurrentThread());
47 void FakeAudioInputStream::Start(AudioInputCallback
* callback
) {
48 DCHECK(audio_manager_
->GetTaskRunner()->BelongsToCurrentThread());
50 fake_audio_worker_
.Start(base::Bind(
51 &FakeAudioInputStream::ReadAudioFromSource
, base::Unretained(this)));
54 void FakeAudioInputStream::Stop() {
55 DCHECK(audio_manager_
->GetTaskRunner()->BelongsToCurrentThread());
56 fake_audio_worker_
.Stop();
60 void FakeAudioInputStream::Close() {
61 DCHECK(audio_manager_
->GetTaskRunner()->BelongsToCurrentThread());
63 audio_manager_
->ReleaseInputStream(this);
66 double FakeAudioInputStream::GetMaxVolume() {
67 DCHECK(audio_manager_
->GetTaskRunner()->BelongsToCurrentThread());
71 void FakeAudioInputStream::SetVolume(double volume
) {
72 DCHECK(audio_manager_
->GetTaskRunner()->BelongsToCurrentThread());
75 double FakeAudioInputStream::GetVolume() {
76 DCHECK(audio_manager_
->GetTaskRunner()->BelongsToCurrentThread());
80 bool FakeAudioInputStream::IsMuted() {
81 DCHECK(audio_manager_
->GetTaskRunner()->BelongsToCurrentThread());
85 bool FakeAudioInputStream::SetAutomaticGainControl(bool enabled
) {
89 bool FakeAudioInputStream::GetAutomaticGainControl() {
93 void FakeAudioInputStream::ReadAudioFromSource() {
94 DCHECK(audio_manager_
->GetWorkerTaskRunner()->BelongsToCurrentThread());
98 audio_source_
= ChooseSource();
100 const int kNoDelay
= 0;
101 audio_source_
->OnMoreData(audio_bus_
.get(), kNoDelay
);
102 callback_
->OnData(this, audio_bus_
.get(), 0, 1.0);
105 using AudioSourceCallback
= AudioOutputStream::AudioSourceCallback
;
106 scoped_ptr
<AudioSourceCallback
> FakeAudioInputStream::ChooseSource() {
107 DCHECK(audio_manager_
->GetWorkerTaskRunner()->BelongsToCurrentThread());
109 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
110 switches::kUseFileForFakeAudioCapture
)) {
111 base::FilePath path_to_wav_file
=
112 base::CommandLine::ForCurrentProcess()->GetSwitchValuePath(
113 switches::kUseFileForFakeAudioCapture
);
114 CHECK(!path_to_wav_file
.empty())
115 << "You must pass the file to use as argument to --"
116 << switches::kUseFileForFakeAudioCapture
<< ".";
118 return make_scoped_ptr(new FileSource(params_
, path_to_wav_file
));
120 return make_scoped_ptr(new BeepingSource(params_
));
123 void FakeAudioInputStream::BeepOnce() {
124 BeepingSource::BeepOnce();