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/virtual_audio_input_stream.h"
10 #include "base/bind.h"
11 #include "base/single_thread_task_runner.h"
12 #include "media/audio/virtual_audio_output_stream.h"
16 // LoopbackAudioConverter works similar to AudioConverter and converts input
17 // streams to different audio parameters. Then, the LoopbackAudioConverter can
18 // be used as an input to another AudioConverter. This allows us to
19 // use converted audio from AudioOutputStreams as input to an AudioConverter.
20 // For example, this allows converting multiple streams into a common format and
21 // using the converted audio as input to another AudioConverter (i.e. a mixer).
22 class LoopbackAudioConverter
: public AudioConverter::InputCallback
{
24 LoopbackAudioConverter(const AudioParameters
& input_params
,
25 const AudioParameters
& output_params
)
26 : audio_converter_(input_params
, output_params
, false) {}
28 ~LoopbackAudioConverter() override
{}
30 void AddInput(AudioConverter::InputCallback
* input
) {
31 audio_converter_
.AddInput(input
);
34 void RemoveInput(AudioConverter::InputCallback
* input
) {
35 audio_converter_
.RemoveInput(input
);
39 double ProvideInput(AudioBus
* audio_bus
,
40 base::TimeDelta buffer_delay
) override
{
41 audio_converter_
.ConvertWithDelay(buffer_delay
, audio_bus
);
45 AudioConverter audio_converter_
;
47 DISALLOW_COPY_AND_ASSIGN(LoopbackAudioConverter
);
50 VirtualAudioInputStream::VirtualAudioInputStream(
51 const AudioParameters
& params
,
52 const scoped_refptr
<base::SingleThreadTaskRunner
>& worker_task_runner
,
53 const AfterCloseCallback
& after_close_cb
)
54 : worker_task_runner_(worker_task_runner
),
55 after_close_cb_(after_close_cb
),
57 buffer_(new uint8
[params
.GetBytesPerBuffer()]),
59 mixer_(params_
, params_
, false),
60 num_attached_output_streams_(0),
61 fake_worker_(worker_task_runner_
, params_
),
62 audio_bus_(AudioBus::Create(params
)) {
63 DCHECK(params_
.IsValid());
64 DCHECK(worker_task_runner_
.get());
66 // VAIS can be constructed on any thread, but will DCHECK that all
67 // AudioInputStream methods are called from the same thread.
68 thread_checker_
.DetachFromThread();
71 VirtualAudioInputStream::~VirtualAudioInputStream() {
74 // Sanity-check: Contract for Add/RemoveOutputStream() requires that all
75 // output streams be removed before VirtualAudioInputStream is destroyed.
76 DCHECK_EQ(0, num_attached_output_streams_
);
78 for (AudioConvertersMap::iterator it
= converters_
.begin();
79 it
!= converters_
.end(); ++it
) {
84 bool VirtualAudioInputStream::Open() {
85 DCHECK(thread_checker_
.CalledOnValidThread());
86 memset(buffer_
.get(), 0, params_
.GetBytesPerBuffer());
90 void VirtualAudioInputStream::Start(AudioInputCallback
* callback
) {
91 DCHECK(thread_checker_
.CalledOnValidThread());
93 fake_worker_
.Start(base::Bind(
94 &VirtualAudioInputStream::PumpAudio
, base::Unretained(this)));
97 void VirtualAudioInputStream::Stop() {
98 DCHECK(thread_checker_
.CalledOnValidThread());
103 void VirtualAudioInputStream::AddOutputStream(
104 VirtualAudioOutputStream
* stream
, const AudioParameters
& output_params
) {
105 DCHECK(thread_checker_
.CalledOnValidThread());
107 base::AutoLock
scoped_lock(converter_network_lock_
);
109 AudioConvertersMap::iterator converter
= converters_
.find(output_params
);
110 if (converter
== converters_
.end()) {
111 std::pair
<AudioConvertersMap::iterator
, bool> result
= converters_
.insert(
112 std::make_pair(output_params
,
113 new LoopbackAudioConverter(output_params
, params_
)));
114 converter
= result
.first
;
116 // Add to main mixer if we just added a new AudioTransform.
117 mixer_
.AddInput(converter
->second
);
119 converter
->second
->AddInput(stream
);
120 ++num_attached_output_streams_
;
123 void VirtualAudioInputStream::RemoveOutputStream(
124 VirtualAudioOutputStream
* stream
, const AudioParameters
& output_params
) {
125 DCHECK(thread_checker_
.CalledOnValidThread());
127 base::AutoLock
scoped_lock(converter_network_lock_
);
129 DCHECK(converters_
.find(output_params
) != converters_
.end());
130 converters_
[output_params
]->RemoveInput(stream
);
132 --num_attached_output_streams_
;
133 DCHECK_LE(0, num_attached_output_streams_
);
136 void VirtualAudioInputStream::PumpAudio() {
137 DCHECK(worker_task_runner_
->BelongsToCurrentThread());
140 base::AutoLock
scoped_lock(converter_network_lock_
);
141 // Because the audio is being looped-back, the delay until it will be played
143 mixer_
.ConvertWithDelay(base::TimeDelta(), audio_bus_
.get());
145 // Because the audio is being looped-back, the delay since since it was
147 callback_
->OnData(this, audio_bus_
.get(), 0, 1.0);
150 void VirtualAudioInputStream::Close() {
151 DCHECK(thread_checker_
.CalledOnValidThread());
153 Stop(); // Make sure callback_ is no longer being used.
155 // If a non-null AfterCloseCallback was provided to the constructor, invoke it
156 // here. The callback is moved to a stack-local first since |this| could be
157 // destroyed during Run().
158 if (!after_close_cb_
.is_null()) {
159 const AfterCloseCallback cb
= after_close_cb_
;
160 after_close_cb_
.Reset();
165 double VirtualAudioInputStream::GetMaxVolume() {
169 void VirtualAudioInputStream::SetVolume(double volume
) {}
171 double VirtualAudioInputStream::GetVolume() {
175 bool VirtualAudioInputStream::SetAutomaticGainControl(bool enabled
) {
179 bool VirtualAudioInputStream::GetAutomaticGainControl() {
183 bool VirtualAudioInputStream::IsMuted() {