1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "media/audio/virtual_audio_input_stream.h"
#include <algorithm>
#include <utility>
#include "base/bind.h"
#include "base/message_loop_proxy.h"
#include "media/audio/virtual_audio_output_stream.h"
namespace media {
// LoopbackAudioConverter works similar to AudioConverter and converts input
// streams to different audio parameters. Then, the LoopbackAudioConverter can
// be used as an input to another AudioConverter. This allows us to
// use converted audio from AudioOutputStreams as input to an AudioConverter.
// For example, this allows converting multiple streams into a common format and
// using the converted audio as input to another AudioConverter (i.e. a mixer).
class LoopbackAudioConverter : public AudioConverter::InputCallback {
public:
LoopbackAudioConverter(const AudioParameters& input_params,
const AudioParameters& output_params)
: audio_converter_(input_params, output_params, false) {}
virtual ~LoopbackAudioConverter() {}
void AddInput(AudioConverter::InputCallback* input) {
audio_converter_.AddInput(input);
}
void RemoveInput(AudioConverter::InputCallback* input) {
audio_converter_.RemoveInput(input);
}
private:
virtual double ProvideInput(AudioBus* audio_bus,
base::TimeDelta buffer_delay) OVERRIDE {
audio_converter_.Convert(audio_bus);
return 1.0;
}
AudioConverter audio_converter_;
DISALLOW_COPY_AND_ASSIGN(LoopbackAudioConverter);
};
VirtualAudioInputStream::VirtualAudioInputStream(
const AudioParameters& params,
const scoped_refptr<base::MessageLoopProxy>& message_loop,
const AfterCloseCallback& after_close_cb)
: message_loop_(message_loop),
after_close_cb_(after_close_cb),
callback_(NULL),
buffer_(new uint8[params.GetBytesPerBuffer()]),
params_(params),
mixer_(params_, params_, false),
num_attached_output_streams_(0),
fake_consumer_(message_loop_, params_) {
DCHECK(params_.IsValid());
DCHECK(message_loop_.get());
}
VirtualAudioInputStream::~VirtualAudioInputStream() {
for (AudioConvertersMap::iterator it = converters_.begin();
it != converters_.end(); ++it) {
delete it->second;
}
DCHECK_EQ(0, num_attached_output_streams_);
}
bool VirtualAudioInputStream::Open() {
DCHECK(message_loop_->BelongsToCurrentThread());
memset(buffer_.get(), 0, params_.GetBytesPerBuffer());
return true;
}
void VirtualAudioInputStream::Start(AudioInputCallback* callback) {
DCHECK(message_loop_->BelongsToCurrentThread());
callback_ = callback;
fake_consumer_.Start(base::Bind(
&VirtualAudioInputStream::ReadAudio, base::Unretained(this)));
}
void VirtualAudioInputStream::Stop() {
DCHECK(message_loop_->BelongsToCurrentThread());
fake_consumer_.Stop();
}
void VirtualAudioInputStream::AddOutputStream(
VirtualAudioOutputStream* stream, const AudioParameters& output_params) {
DCHECK(message_loop_->BelongsToCurrentThread());
AudioConvertersMap::iterator converter = converters_.find(output_params);
if (converter == converters_.end()) {
std::pair<AudioConvertersMap::iterator, bool> result = converters_.insert(
std::make_pair(output_params,
new LoopbackAudioConverter(output_params, params_)));
converter = result.first;
// Add to main mixer if we just added a new AudioTransform.
mixer_.AddInput(converter->second);
}
converter->second->AddInput(stream);
++num_attached_output_streams_;
}
void VirtualAudioInputStream::RemoveOutputStream(
VirtualAudioOutputStream* stream, const AudioParameters& output_params) {
DCHECK(message_loop_->BelongsToCurrentThread());
DCHECK(converters_.find(output_params) != converters_.end());
converters_[output_params]->RemoveInput(stream);
--num_attached_output_streams_;
DCHECK_LE(0, num_attached_output_streams_);
}
void VirtualAudioInputStream::ReadAudio(AudioBus* audio_bus) {
DCHECK(message_loop_->BelongsToCurrentThread());
DCHECK(callback_);
mixer_.Convert(audio_bus);
audio_bus->ToInterleaved(params_.frames_per_buffer(),
params_.bits_per_sample() / 8,
buffer_.get());
callback_->OnData(this,
buffer_.get(),
params_.GetBytesPerBuffer(),
params_.GetBytesPerBuffer(),
1.0);
}
void VirtualAudioInputStream::Close() {
DCHECK(message_loop_->BelongsToCurrentThread());
if (callback_) {
callback_->OnClose(this);
callback_ = NULL;
}
// If a non-null AfterCloseCallback was provided to the constructor, invoke it
// here. The callback is moved to a stack-local first since |this| could be
// destroyed during Run().
if (!after_close_cb_.is_null()) {
const AfterCloseCallback cb = after_close_cb_;
after_close_cb_.Reset();
cb.Run(this);
}
}
double VirtualAudioInputStream::GetMaxVolume() {
return 1.0;
}
void VirtualAudioInputStream::SetVolume(double volume) {}
double VirtualAudioInputStream::GetVolume() {
return 1.0;
}
void VirtualAudioInputStream::SetAutomaticGainControl(bool enabled) {}
bool VirtualAudioInputStream::GetAutomaticGainControl() {
return false;
}
} // namespace media
|