Cast: Stop logging kVideoFrameSentToEncoder and rename a couple events.
[chromium-blink-merge.git] / media / audio / virtual_audio_input_stream.cc
blobf632d3bd995fec5686c99000508bd294da435786
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"
7 #include <algorithm>
8 #include <utility>
10 #include "base/bind.h"
11 #include "base/single_thread_task_runner.h"
12 #include "media/audio/virtual_audio_output_stream.h"
14 namespace media {
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 {
23 public:
24 LoopbackAudioConverter(const AudioParameters& input_params,
25 const AudioParameters& output_params)
26 : audio_converter_(input_params, output_params, false) {}
28 virtual ~LoopbackAudioConverter() {}
30 void AddInput(AudioConverter::InputCallback* input) {
31 audio_converter_.AddInput(input);
34 void RemoveInput(AudioConverter::InputCallback* input) {
35 audio_converter_.RemoveInput(input);
38 private:
39 virtual double ProvideInput(AudioBus* audio_bus,
40 base::TimeDelta buffer_delay) OVERRIDE {
41 audio_converter_.Convert(audio_bus);
42 return 1.0;
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),
56 callback_(NULL),
57 buffer_(new uint8[params.GetBytesPerBuffer()]),
58 params_(params),
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() {
71 DCHECK(!callback_);
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) {
79 delete it->second;
83 bool VirtualAudioInputStream::Open() {
84 DCHECK(thread_checker_.CalledOnValidThread());
85 memset(buffer_.get(), 0, params_.GetBytesPerBuffer());
86 return true;
89 void VirtualAudioInputStream::Start(AudioInputCallback* callback) {
90 DCHECK(thread_checker_.CalledOnValidThread());
91 callback_ = callback;
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();
99 callback_ = NULL;
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 mixer_.Convert(audio_bus);
142 audio_bus->ToInterleaved(params_.frames_per_buffer(),
143 params_.bits_per_sample() / 8,
144 buffer_.get());
145 callback_->OnData(this,
146 buffer_.get(),
147 params_.GetBytesPerBuffer(),
148 params_.GetBytesPerBuffer(),
149 1.0);
152 void VirtualAudioInputStream::Close() {
153 DCHECK(thread_checker_.CalledOnValidThread());
155 Stop(); // Make sure callback_ is no longer being used.
157 // If a non-null AfterCloseCallback was provided to the constructor, invoke it
158 // here. The callback is moved to a stack-local first since |this| could be
159 // destroyed during Run().
160 if (!after_close_cb_.is_null()) {
161 const AfterCloseCallback cb = after_close_cb_;
162 after_close_cb_.Reset();
163 cb.Run(this);
167 double VirtualAudioInputStream::GetMaxVolume() {
168 return 1.0;
171 void VirtualAudioInputStream::SetVolume(double volume) {}
173 double VirtualAudioInputStream::GetVolume() {
174 return 1.0;
177 void VirtualAudioInputStream::SetAutomaticGainControl(bool enabled) {}
179 bool VirtualAudioInputStream::GetAutomaticGainControl() {
180 return false;
183 } // namespace media