Chromium Code Reviews| Index: media/audio/virtual_audio_input_stream.cc |
| diff --git a/media/audio/virtual_audio_input_stream.cc b/media/audio/virtual_audio_input_stream.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..e3f06d37323ff6654bbef00720f73cdd66dd6327 |
| --- /dev/null |
| +++ b/media/audio/virtual_audio_input_stream.cc |
| @@ -0,0 +1,168 @@ |
| +// 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 "base/bind.h" |
| +#include "base/message_loop.h" |
| +#include "media/audio/virtual_audio_output_stream.h" |
| + |
| +namespace media { |
| + |
| +// AudioConverter that can also be used an as InputCallback. This allows us to |
| +// use converted audio from an AudioOutputStream 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, |
|
DaleCurtis
2012/11/28 23:43:18
This is weird. ~AudioConverter is not virtual eith
justinlin
2012/11/29 10:08:33
Done.
|
| + public AudioConverter::InputCallback { |
| + public: |
| + LoopbackAudioConverter(const AudioParameters& input_params, |
| + const AudioParameters& output_params) |
| + : AudioConverter(input_params, output_params, false) {} |
| + |
| + virtual ~LoopbackAudioConverter() {} |
| + |
| + private: |
| + virtual double ProvideInput(AudioBus* audio_bus, |
| + base::TimeDelta buffer_delay) OVERRIDE { |
| + Convert(audio_bus); |
| + return 1.0; |
| + } |
| + |
| + DISALLOW_COPY_AND_ASSIGN(LoopbackAudioConverter); |
| +}; |
| + |
| +VirtualAudioInputStream* VirtualAudioInputStream::MakeStream( |
| + AudioManagerBase* manager, const AudioParameters& params) { |
| + return new VirtualAudioInputStream(manager, params); |
| +} |
| + |
| +VirtualAudioInputStream::VirtualAudioInputStream(AudioManagerBase* manager, |
| + const AudioParameters& params) |
| + : audio_manager_(manager), |
| + callback_(NULL), |
| + buffer_duration_ms_(params.frames_per_buffer() |
| + * 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.
|
| + / params.sample_rate()), |
| + buffer_(new uint8[params.GetBytesPerBuffer()]), |
| + params_(params), |
| + audio_bus_(AudioBus::Create(params_)), |
| + mixer_(params_, params_, false), |
| + num_attached_outputs_streams_(0) { |
| +} |
| + |
| +VirtualAudioInputStream::~VirtualAudioInputStream() { |
| + for (AudioConvertersMap::iterator it = converters_.begin(); |
| + it != converters_.end(); ++it) { |
| + delete it->second; |
| + } |
| + DCHECK_EQ(0, num_attached_outputs_streams_); |
| +} |
| + |
| +bool VirtualAudioInputStream::Open() { |
| + memset(buffer_.get(), 0, params_.GetBytesPerBuffer()); |
| + return true; |
| +} |
| + |
| +void VirtualAudioInputStream::Start(AudioInputCallback* callback) { |
| + DCHECK(audio_manager_->GetMessageLoop()->BelongsToCurrentThread()); |
| + callback_ = callback; |
| + on_more_data_cb_.Reset(base::Bind( |
| + &VirtualAudioInputStream::ReadAudio, base::Unretained(this))); |
| + next_read_time_ = base::Time::Now(); |
| + audio_manager_->GetMessageLoop()->PostTask( |
| + FROM_HERE, on_more_data_cb_.callback()); |
| +} |
| + |
| +void VirtualAudioInputStream::Stop() { |
|
DaleCurtis
2012/11/28 23:43:18
DCHECK(belongs to current thread).
justinlin
2012/11/29 10:08:33
Done.
|
| + on_more_data_cb_.Cancel(); |
| +} |
| + |
| +void VirtualAudioInputStream::AddOutputStream( |
| + VirtualAudioOutputStream* stream, const AudioParameters& params) { |
| + DCHECK(audio_manager_->GetMessageLoop()->BelongsToCurrentThread()); |
| + 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
|
| + |
| + AudioConvertersMap::iterator converter = converters_.find(params); |
| + if (converter == converters_.end()) { |
| + std::pair<AudioConvertersMap::iterator, bool> result = converters_.insert( |
| + std::make_pair<AudioParameters, LoopbackAudioConverter*>( |
| + 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.
|
| + converter = result.first; |
| + |
| + // Add to main mixer if we just added a new AudioTransform. |
| + mixer_.AddInput(converter->second); |
| + } |
| + converter->second->AddInput(stream); |
| + ++num_attached_outputs_streams_; |
| +} |
| + |
| +void VirtualAudioInputStream::RemoveOutputStream( |
| + VirtualAudioOutputStream* stream) { |
| + DCHECK(audio_manager_->GetMessageLoop()->BelongsToCurrentThread()); |
| + DCHECK(output_params_.find(stream) != output_params_.end()); |
| + const AudioParameters& params = output_params_[stream]; |
| + |
| + DCHECK(converters_.find(params) != converters_.end()); |
| + converters_[params]->RemoveInput(stream); |
| + |
| + output_params_.erase(stream); |
| + --num_attached_outputs_streams_; |
| +} |
| + |
| +void VirtualAudioInputStream::ReadAudio() { |
| + DCHECK(audio_manager_->GetMessageLoop()->BelongsToCurrentThread()); |
| + DCHECK(callback_); |
| + |
| + mixer_.Convert(audio_bus_.get()); |
| + 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); |
|
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.
|
| + |
| + next_read_time_ += base::TimeDelta::FromMilliseconds(buffer_duration_ms_); |
| + base::TimeDelta delay = next_read_time_ - base::Time::Now(); |
| + // Try to catchup if we fall behind. |
| + 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
|
| + delay = base::TimeDelta(); |
| + |
| + MessageLoop* message_loop = MessageLoop::current(); |
| + if (message_loop) { |
| + message_loop->PostDelayedTask( |
| + FROM_HERE, on_more_data_cb_.callback(), delay); |
| + } |
| +} |
| + |
| +void VirtualAudioInputStream::Close() { |
| + DCHECK(audio_manager_->GetMessageLoop()->BelongsToCurrentThread()); |
| + if (callback_) { |
| + DCHECK(on_more_data_cb_.IsCancelled()); |
| + callback_->OnClose(this); |
| + callback_ = NULL; |
| + } |
| + audio_manager_->ReleaseInputStream(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 |