Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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. | |
| 4 | |
| 5 #include "media/audio/virtual_audio_input_stream.h" | |
| 6 | |
| 7 #include <algorithm> | |
| 8 | |
| 9 #include "base/bind.h" | |
| 10 #include "base/message_loop.h" | |
| 11 #include "media/audio/virtual_audio_output_stream.h" | |
| 12 | |
| 13 namespace media { | |
| 14 | |
| 15 // AudioConverter that can also be used an as InputCallback. This allows us to | |
| 16 // use converted audio from an AudioOutputStream as input to an AudioConverter. | |
| 17 // For example, this allows converting multiple streams into a common format and | |
| 18 // using the converted audio as input to another AudioConverter (i.e. a mixer). | |
| 19 class LoopbackAudioConverter | |
| 20 : public AudioConverter, | |
|
DaleCurtis
2012/11/28 23:43:18
This is weird. ~AudioConverter is not virtual eith
justinlin
2012/11/29 10:08:33
Done.
| |
| 21 public AudioConverter::InputCallback { | |
| 22 public: | |
| 23 LoopbackAudioConverter(const AudioParameters& input_params, | |
| 24 const AudioParameters& output_params) | |
| 25 : AudioConverter(input_params, output_params, false) {} | |
| 26 | |
| 27 virtual ~LoopbackAudioConverter() {} | |
| 28 | |
| 29 private: | |
| 30 virtual double ProvideInput(AudioBus* audio_bus, | |
| 31 base::TimeDelta buffer_delay) OVERRIDE { | |
| 32 Convert(audio_bus); | |
| 33 return 1.0; | |
| 34 } | |
| 35 | |
| 36 DISALLOW_COPY_AND_ASSIGN(LoopbackAudioConverter); | |
| 37 }; | |
| 38 | |
| 39 VirtualAudioInputStream* VirtualAudioInputStream::MakeStream( | |
| 40 AudioManagerBase* manager, const AudioParameters& params) { | |
| 41 return new VirtualAudioInputStream(manager, params); | |
| 42 } | |
| 43 | |
| 44 VirtualAudioInputStream::VirtualAudioInputStream(AudioManagerBase* manager, | |
| 45 const AudioParameters& params) | |
| 46 : audio_manager_(manager), | |
| 47 callback_(NULL), | |
| 48 buffer_duration_ms_(params.frames_per_buffer() | |
| 49 * base::Time::kMillisecondsPerSecond | |
|
DaleCurtis
2012/11/28 23:43:18
operators go on the previous line, this is also go
miu
2012/11/29 01:57:40
Agree with Dale. Milliseconds is too-coarse a tim
justinlin
2012/11/29 10:08:33
Well you do the multiplication first, so it should
justinlin
2012/11/29 10:08:33
Done.
| |
| 50 / params.sample_rate()), | |
| 51 buffer_(new uint8[params.GetBytesPerBuffer()]), | |
| 52 params_(params), | |
| 53 audio_bus_(AudioBus::Create(params_)), | |
| 54 mixer_(params_, params_, false), | |
| 55 num_attached_outputs_streams_(0) { | |
| 56 } | |
| 57 | |
| 58 VirtualAudioInputStream::~VirtualAudioInputStream() { | |
| 59 for (AudioConvertersMap::iterator it = converters_.begin(); | |
| 60 it != converters_.end(); ++it) { | |
| 61 delete it->second; | |
| 62 } | |
| 63 DCHECK_EQ(0, num_attached_outputs_streams_); | |
| 64 } | |
| 65 | |
| 66 bool VirtualAudioInputStream::Open() { | |
| 67 memset(buffer_.get(), 0, params_.GetBytesPerBuffer()); | |
| 68 return true; | |
| 69 } | |
| 70 | |
| 71 void VirtualAudioInputStream::Start(AudioInputCallback* callback) { | |
| 72 DCHECK(audio_manager_->GetMessageLoop()->BelongsToCurrentThread()); | |
| 73 callback_ = callback; | |
| 74 on_more_data_cb_.Reset(base::Bind( | |
| 75 &VirtualAudioInputStream::ReadAudio, base::Unretained(this))); | |
| 76 next_read_time_ = base::Time::Now(); | |
| 77 audio_manager_->GetMessageLoop()->PostTask( | |
| 78 FROM_HERE, on_more_data_cb_.callback()); | |
| 79 } | |
| 80 | |
| 81 void VirtualAudioInputStream::Stop() { | |
|
DaleCurtis
2012/11/28 23:43:18
DCHECK(belongs to current thread).
justinlin
2012/11/29 10:08:33
Done.
| |
| 82 on_more_data_cb_.Cancel(); | |
| 83 } | |
| 84 | |
| 85 void VirtualAudioInputStream::AddOutputStream( | |
| 86 VirtualAudioOutputStream* stream, const AudioParameters& params) { | |
| 87 DCHECK(audio_manager_->GetMessageLoop()->BelongsToCurrentThread()); | |
| 88 output_params_.insert(std::make_pair(stream, params)); | |
|
DaleCurtis
2012/11/28 23:43:18
This is unnecessary since every VirtualAudioOutput
justinlin
2012/11/29 10:08:33
Done. Right, don't need it anymore after moving th
| |
| 89 | |
| 90 AudioConvertersMap::iterator converter = converters_.find(params); | |
| 91 if (converter == converters_.end()) { | |
| 92 std::pair<AudioConvertersMap::iterator, bool> result = converters_.insert( | |
| 93 std::make_pair<AudioParameters, LoopbackAudioConverter*>( | |
| 94 params, new LoopbackAudioConverter(params, params_))); | |
|
DaleCurtis
2012/11/28 23:43:18
I'd choose a different name for one of these |para
justinlin
2012/11/29 10:08:33
Done.
| |
| 95 converter = result.first; | |
| 96 | |
| 97 // Add to main mixer if we just added a new AudioTransform. | |
| 98 mixer_.AddInput(converter->second); | |
| 99 } | |
| 100 converter->second->AddInput(stream); | |
| 101 ++num_attached_outputs_streams_; | |
| 102 } | |
| 103 | |
| 104 void VirtualAudioInputStream::RemoveOutputStream( | |
| 105 VirtualAudioOutputStream* stream) { | |
| 106 DCHECK(audio_manager_->GetMessageLoop()->BelongsToCurrentThread()); | |
| 107 DCHECK(output_params_.find(stream) != output_params_.end()); | |
| 108 const AudioParameters& params = output_params_[stream]; | |
| 109 | |
| 110 DCHECK(converters_.find(params) != converters_.end()); | |
| 111 converters_[params]->RemoveInput(stream); | |
| 112 | |
| 113 output_params_.erase(stream); | |
| 114 --num_attached_outputs_streams_; | |
| 115 } | |
| 116 | |
| 117 void VirtualAudioInputStream::ReadAudio() { | |
| 118 DCHECK(audio_manager_->GetMessageLoop()->BelongsToCurrentThread()); | |
| 119 DCHECK(callback_); | |
| 120 | |
| 121 mixer_.Convert(audio_bus_.get()); | |
| 122 audio_bus_->ToInterleaved(params_.frames_per_buffer(), | |
| 123 params_.bits_per_sample() / 8, | |
| 124 buffer_.get()); | |
| 125 | |
| 126 callback_->OnData(this, buffer_.get(), params_.GetBytesPerBuffer(), | |
| 127 params_.GetBytesPerBuffer(), 1.0); | |
|
DaleCurtis
2012/11/28 23:43:18
Can't do mixed indentation like this, either all a
justinlin
2012/11/29 10:08:33
Done.
| |
| 128 | |
| 129 next_read_time_ += base::TimeDelta::FromMilliseconds(buffer_duration_ms_); | |
| 130 base::TimeDelta delay = next_read_time_ - base::Time::Now(); | |
| 131 // Try to catchup if we fall behind. | |
| 132 if (delay < base::TimeDelta()) | |
|
DaleCurtis
2012/11/28 23:43:18
This is risky and not worth the effort IMHO. On sl
miu
2012/11/29 01:57:40
I think that's actually what Justin was attempting
justinlin
2012/11/29 10:08:33
Done.
justinlin
2012/11/29 10:08:33
Yea, I probably misunderstood your last comment. I
| |
| 133 delay = base::TimeDelta(); | |
| 134 | |
| 135 MessageLoop* message_loop = MessageLoop::current(); | |
| 136 if (message_loop) { | |
| 137 message_loop->PostDelayedTask( | |
| 138 FROM_HERE, on_more_data_cb_.callback(), delay); | |
| 139 } | |
| 140 } | |
| 141 | |
| 142 void VirtualAudioInputStream::Close() { | |
| 143 DCHECK(audio_manager_->GetMessageLoop()->BelongsToCurrentThread()); | |
| 144 if (callback_) { | |
| 145 DCHECK(on_more_data_cb_.IsCancelled()); | |
| 146 callback_->OnClose(this); | |
| 147 callback_ = NULL; | |
| 148 } | |
| 149 audio_manager_->ReleaseInputStream(this); | |
| 150 } | |
| 151 | |
| 152 double VirtualAudioInputStream::GetMaxVolume() { | |
| 153 return 1.0; | |
| 154 } | |
| 155 | |
| 156 void VirtualAudioInputStream::SetVolume(double volume) {} | |
| 157 | |
| 158 double VirtualAudioInputStream::GetVolume() { | |
| 159 return 1.0; | |
| 160 } | |
| 161 | |
| 162 void VirtualAudioInputStream::SetAutomaticGainControl(bool enabled) {} | |
| 163 | |
| 164 bool VirtualAudioInputStream::GetAutomaticGainControl() { | |
| 165 return false; | |
| 166 } | |
| 167 | |
| 168 } // namespace media | |
| OLD | NEW |