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_consumer_(worker_task_runner_
, params_
) {
62 DCHECK(params_
.IsValid());
63 DCHECK(worker_task_runner_
.get());
65 // VAIS can be constructed on any thread, but will DCHECK that all
66 // AudioInputStream methods are called from the same thread.
67 thread_checker_
.DetachFromThread();
70 VirtualAudioInputStream::~VirtualAudioInputStream() {
73 // Sanity-check: Contract for Add/RemoveOutputStream() requires that all
74 // output streams be removed before VirtualAudioInputStream is destroyed.
75 DCHECK_EQ(0, num_attached_output_streams_
);
77 for (AudioConvertersMap::iterator it
= converters_
.begin();
78 it
!= converters_
.end(); ++it
) {
83 bool VirtualAudioInputStream::Open() {
84 DCHECK(thread_checker_
.CalledOnValidThread());
85 memset(buffer_
.get(), 0, params_
.GetBytesPerBuffer());
89 void VirtualAudioInputStream::Start(AudioInputCallback
* callback
) {
90 DCHECK(thread_checker_
.CalledOnValidThread());
92 fake_consumer_
.Start(base::Bind(
93 &VirtualAudioInputStream::PumpAudio
, base::Unretained(this)));
96 void VirtualAudioInputStream::Stop() {
97 DCHECK(thread_checker_
.CalledOnValidThread());
98 fake_consumer_
.Stop();
102 void VirtualAudioInputStream::AddOutputStream(
103 VirtualAudioOutputStream
* stream
, const AudioParameters
& output_params
) {
104 DCHECK(thread_checker_
.CalledOnValidThread());
106 base::AutoLock
scoped_lock(converter_network_lock_
);
108 AudioConvertersMap::iterator converter
= converters_
.find(output_params
);
109 if (converter
== converters_
.end()) {
110 std::pair
<AudioConvertersMap::iterator
, bool> result
= converters_
.insert(
111 std::make_pair(output_params
,
112 new LoopbackAudioConverter(output_params
, params_
)));
113 converter
= result
.first
;
115 // Add to main mixer if we just added a new AudioTransform.
116 mixer_
.AddInput(converter
->second
);
118 converter
->second
->AddInput(stream
);
119 ++num_attached_output_streams_
;
122 void VirtualAudioInputStream::RemoveOutputStream(
123 VirtualAudioOutputStream
* stream
, const AudioParameters
& output_params
) {
124 DCHECK(thread_checker_
.CalledOnValidThread());
126 base::AutoLock
scoped_lock(converter_network_lock_
);
128 DCHECK(converters_
.find(output_params
) != converters_
.end());
129 converters_
[output_params
]->RemoveInput(stream
);
131 --num_attached_output_streams_
;
132 DCHECK_LE(0, num_attached_output_streams_
);
135 void VirtualAudioInputStream::PumpAudio(AudioBus
* audio_bus
) {
136 DCHECK(worker_task_runner_
->BelongsToCurrentThread());
139 base::AutoLock
scoped_lock(converter_network_lock_
);
140 // Because the audio is being looped-back, the delay until it will be played
142 mixer_
.ConvertWithDelay(base::TimeDelta(), audio_bus
);
144 // Because the audio is being looped-back, the delay since since it was
146 callback_
->OnData(this, audio_bus
, 0, 1.0);
149 void VirtualAudioInputStream::Close() {
150 DCHECK(thread_checker_
.CalledOnValidThread());
152 Stop(); // Make sure callback_ is no longer being used.
154 // If a non-null AfterCloseCallback was provided to the constructor, invoke it
155 // here. The callback is moved to a stack-local first since |this| could be
156 // destroyed during Run().
157 if (!after_close_cb_
.is_null()) {
158 const AfterCloseCallback cb
= after_close_cb_
;
159 after_close_cb_
.Reset();
164 double VirtualAudioInputStream::GetMaxVolume() {
168 void VirtualAudioInputStream::SetVolume(double volume
) {}
170 double VirtualAudioInputStream::GetVolume() {
174 bool VirtualAudioInputStream::SetAutomaticGainControl(bool enabled
) {
178 bool VirtualAudioInputStream::GetAutomaticGainControl() {
182 bool VirtualAudioInputStream::IsMuted() {