Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "media/audio/virtual_audio_input_stream.h" | 5 #include "media/audio/virtual_audio_input_stream.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/message_loop.h" | 11 #include "base/message_loop.h" |
| 12 #include "base/message_loop_proxy.h" | |
| 12 #include "media/audio/virtual_audio_output_stream.h" | 13 #include "media/audio/virtual_audio_output_stream.h" |
| 13 | 14 |
| 14 namespace media { | 15 namespace media { |
| 15 | 16 |
| 16 // LoopbackAudioConverter works similar to AudioConverter and converts input | 17 // LoopbackAudioConverter works similar to AudioConverter and converts input |
| 17 // streams to different audio parameters. Then, the LoopbackAudioConverter can | 18 // streams to different audio parameters. Then, the LoopbackAudioConverter can |
| 18 // be used as an input to another AudioConverter. This allows us to | 19 // be used as an input to another AudioConverter. This allows us to |
| 19 // use converted audio from AudioOutputStreams as input to an AudioConverter. | 20 // use converted audio from AudioOutputStreams as input to an AudioConverter. |
| 20 // For example, this allows converting multiple streams into a common format and | 21 // 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 // using the converted audio as input to another AudioConverter (i.e. a mixer). |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 40 base::TimeDelta buffer_delay) OVERRIDE { | 41 base::TimeDelta buffer_delay) OVERRIDE { |
| 41 audio_converter_.Convert(audio_bus); | 42 audio_converter_.Convert(audio_bus); |
| 42 return 1.0; | 43 return 1.0; |
| 43 } | 44 } |
| 44 | 45 |
| 45 AudioConverter audio_converter_; | 46 AudioConverter audio_converter_; |
| 46 | 47 |
| 47 DISALLOW_COPY_AND_ASSIGN(LoopbackAudioConverter); | 48 DISALLOW_COPY_AND_ASSIGN(LoopbackAudioConverter); |
| 48 }; | 49 }; |
| 49 | 50 |
| 50 VirtualAudioInputStream* VirtualAudioInputStream::MakeStream( | |
| 51 AudioManagerBase* manager, const AudioParameters& params, | |
| 52 base::MessageLoopProxy* message_loop) { | |
| 53 return new VirtualAudioInputStream(manager, params, message_loop); | |
| 54 } | |
| 55 | |
| 56 VirtualAudioInputStream::VirtualAudioInputStream( | 51 VirtualAudioInputStream::VirtualAudioInputStream( |
| 57 AudioManagerBase* manager, const AudioParameters& params, | 52 const AudioParameters& params, base::MessageLoopProxy* message_loop) |
| 58 base::MessageLoopProxy* message_loop) | 53 : message_loop_(message_loop), |
| 59 : audio_manager_(manager), | |
| 60 message_loop_(message_loop), | |
| 61 callback_(NULL), | 54 callback_(NULL), |
| 62 buffer_duration_ms_(base::TimeDelta::FromMilliseconds( | 55 buffer_duration_(base::TimeDelta::FromMicroseconds( |
| 63 params.frames_per_buffer() * base::Time::kMillisecondsPerSecond / | 56 params.frames_per_buffer() * base::Time::kMicrosecondsPerSecond / |
| 64 static_cast<float>(params.sample_rate()))), | 57 params.sample_rate())), |
| 65 buffer_(new uint8[params.GetBytesPerBuffer()]), | 58 buffer_(new uint8[params.GetBytesPerBuffer()]), |
| 66 params_(params), | 59 params_(params), |
| 67 audio_bus_(AudioBus::Create(params_)), | 60 audio_bus_(AudioBus::Create(params_)), |
| 68 mixer_(params_, params_, false), | 61 mixer_(params_, params_, false), |
| 69 num_attached_outputs_streams_(0) { | 62 num_attached_output_streams_(0) { |
| 63 DCHECK(params_.IsValid()); | |
| 64 DCHECK(message_loop_); | |
| 70 } | 65 } |
| 71 | 66 |
| 72 VirtualAudioInputStream::~VirtualAudioInputStream() { | 67 VirtualAudioInputStream::~VirtualAudioInputStream() { |
| 73 for (AudioConvertersMap::iterator it = converters_.begin(); | 68 for (AudioConvertersMap::iterator it = converters_.begin(); |
| 74 it != converters_.end(); ++it) | 69 it != converters_.end(); ++it) |
| 75 delete it->second; | 70 delete it->second; |
| 76 | 71 |
| 77 DCHECK_EQ(0, num_attached_outputs_streams_); | 72 DCHECK_EQ(0, num_attached_output_streams_); |
| 73 } | |
| 74 | |
| 75 void VirtualAudioInputStream::RunOnceClosed(const base::Closure& cb) { | |
| 76 DCHECK(on_close_cb_.is_null()); | |
| 77 on_close_cb_ = cb; | |
| 78 } | 78 } |
| 79 | 79 |
| 80 bool VirtualAudioInputStream::Open() { | 80 bool VirtualAudioInputStream::Open() { |
| 81 DCHECK(message_loop_->BelongsToCurrentThread()); | |
| 81 memset(buffer_.get(), 0, params_.GetBytesPerBuffer()); | 82 memset(buffer_.get(), 0, params_.GetBytesPerBuffer()); |
| 82 return true; | 83 return true; |
| 83 } | 84 } |
| 84 | 85 |
| 85 void VirtualAudioInputStream::Start(AudioInputCallback* callback) { | 86 void VirtualAudioInputStream::Start(AudioInputCallback* callback) { |
| 86 DCHECK(message_loop_->BelongsToCurrentThread()); | 87 DCHECK(message_loop_->BelongsToCurrentThread()); |
| 87 callback_ = callback; | 88 callback_ = callback; |
| 88 on_more_data_cb_.Reset(base::Bind(&VirtualAudioInputStream::ReadAudio, | 89 on_more_data_cb_.Reset(base::Bind(&VirtualAudioInputStream::ReadAudio, |
| 89 base::Unretained(this))); | 90 base::Unretained(this))); |
| 90 audio_manager_->GetMessageLoop()->PostTask(FROM_HERE, | 91 message_loop_->PostTask(FROM_HERE, on_more_data_cb_.callback()); |
| 91 on_more_data_cb_.callback()); | |
| 92 } | 92 } |
| 93 | 93 |
| 94 void VirtualAudioInputStream::Stop() { | 94 void VirtualAudioInputStream::Stop() { |
| 95 DCHECK(message_loop_->BelongsToCurrentThread()); | 95 DCHECK(message_loop_->BelongsToCurrentThread()); |
| 96 on_more_data_cb_.Cancel(); | 96 on_more_data_cb_.Cancel(); |
| 97 } | 97 } |
| 98 | 98 |
| 99 void VirtualAudioInputStream::AddOutputStream( | 99 void VirtualAudioInputStream::AddOutputStream( |
| 100 VirtualAudioOutputStream* stream, const AudioParameters& output_params) { | 100 VirtualAudioOutputStream* stream, const AudioParameters& output_params) { |
| 101 DCHECK(message_loop_->BelongsToCurrentThread()); | 101 DCHECK(message_loop_->BelongsToCurrentThread()); |
| 102 | 102 |
| 103 AudioConvertersMap::iterator converter = converters_.find(output_params); | 103 AudioConvertersMap::iterator converter = converters_.find(output_params); |
| 104 if (converter == converters_.end()) { | 104 if (converter == converters_.end()) { |
| 105 std::pair<AudioConvertersMap::iterator, bool> result = converters_.insert( | 105 std::pair<AudioConvertersMap::iterator, bool> result = converters_.insert( |
| 106 std::make_pair(output_params, | 106 std::make_pair(output_params, |
| 107 new LoopbackAudioConverter(output_params, params_))); | 107 new LoopbackAudioConverter(output_params, params_))); |
| 108 converter = result.first; | 108 converter = result.first; |
| 109 | 109 |
| 110 // Add to main mixer if we just added a new AudioTransform. | 110 // Add to main mixer if we just added a new AudioTransform. |
| 111 mixer_.AddInput(converter->second); | 111 mixer_.AddInput(converter->second); |
| 112 } | 112 } |
| 113 converter->second->AddInput(stream); | 113 converter->second->AddInput(stream); |
| 114 ++num_attached_outputs_streams_; | 114 ++num_attached_output_streams_; |
| 115 } | 115 } |
| 116 | 116 |
| 117 void VirtualAudioInputStream::RemoveOutputStream( | 117 void VirtualAudioInputStream::RemoveOutputStream( |
| 118 VirtualAudioOutputStream* stream, const AudioParameters& output_params) { | 118 VirtualAudioOutputStream* stream, const AudioParameters& output_params) { |
| 119 DCHECK(message_loop_->BelongsToCurrentThread()); | 119 DCHECK(message_loop_->BelongsToCurrentThread()); |
| 120 | 120 |
| 121 DCHECK(converters_.find(output_params) != converters_.end()); | 121 DCHECK(converters_.find(output_params) != converters_.end()); |
| 122 converters_[output_params]->RemoveInput(stream); | 122 converters_[output_params]->RemoveInput(stream); |
| 123 | 123 |
| 124 --num_attached_outputs_streams_; | 124 --num_attached_output_streams_; |
| 125 DCHECK_LE(0, num_attached_output_streams_); | |
| 125 } | 126 } |
| 126 | 127 |
| 127 void VirtualAudioInputStream::ReadAudio() { | 128 void VirtualAudioInputStream::ReadAudio() { |
| 128 DCHECK(message_loop_->BelongsToCurrentThread()); | 129 DCHECK(message_loop_->BelongsToCurrentThread()); |
| 129 DCHECK(callback_); | 130 DCHECK(callback_); |
| 130 | 131 |
| 131 mixer_.Convert(audio_bus_.get()); | 132 mixer_.Convert(audio_bus_.get()); |
| 132 audio_bus_->ToInterleaved(params_.frames_per_buffer(), | 133 audio_bus_->ToInterleaved(params_.frames_per_buffer(), |
| 133 params_.bits_per_sample() / 8, | 134 params_.bits_per_sample() / 8, |
| 134 buffer_.get()); | 135 buffer_.get()); |
| 135 | 136 |
| 136 callback_->OnData(this, | 137 callback_->OnData(this, |
| 137 buffer_.get(), | 138 buffer_.get(), |
| 138 params_.GetBytesPerBuffer(), | 139 params_.GetBytesPerBuffer(), |
| 139 params_.GetBytesPerBuffer(), | 140 params_.GetBytesPerBuffer(), |
| 140 1.0); | 141 1.0); |
| 141 | 142 |
| 143 // TODO(miu): We want ReadAudio() to be called every buffer_duration_ amount | |
| 144 // of time. However, we're not accounting for the time spent doing the | |
| 145 // conversion and data delivery above, which is significant. Therefore, audio | |
| 146 // data is always being delivered at a slower rate than required, and this | |
| 147 // will cause cut-outs! | |
|
DaleCurtis
2013/01/15 22:02:18
Justin has a fix out for this?
miu
2013/01/16 03:22:18
Yep (https://codereview.chromium.org/11889041/).
| |
| 142 message_loop_->PostDelayedTask(FROM_HERE, | 148 message_loop_->PostDelayedTask(FROM_HERE, |
| 143 on_more_data_cb_.callback(), | 149 on_more_data_cb_.callback(), |
| 144 buffer_duration_ms_); | 150 buffer_duration_); |
| 145 } | 151 } |
| 146 | 152 |
| 147 void VirtualAudioInputStream::Close() { | 153 void VirtualAudioInputStream::Close() { |
| 148 DCHECK(message_loop_->BelongsToCurrentThread()); | 154 DCHECK(message_loop_->BelongsToCurrentThread()); |
| 149 if (callback_) { | 155 if (callback_) { |
| 150 DCHECK(on_more_data_cb_.IsCancelled()); | 156 DCHECK(on_more_data_cb_.IsCancelled()); |
| 151 callback_->OnClose(this); | 157 callback_->OnClose(this); |
| 152 callback_ = NULL; | 158 callback_ = NULL; |
| 153 } | 159 } |
| 154 audio_manager_->ReleaseInputStream(this); | 160 if (!on_close_cb_.is_null()) { |
| 161 const base::Closure cb = on_close_cb_; | |
| 162 on_close_cb_.Reset(); | |
| 163 cb.Run(); | |
| 164 } | |
| 155 } | 165 } |
| 156 | 166 |
| 157 double VirtualAudioInputStream::GetMaxVolume() { | 167 double VirtualAudioInputStream::GetMaxVolume() { |
| 158 return 1.0; | 168 return 1.0; |
| 159 } | 169 } |
| 160 | 170 |
| 161 void VirtualAudioInputStream::SetVolume(double volume) {} | 171 void VirtualAudioInputStream::SetVolume(double volume) {} |
| 162 | 172 |
| 163 double VirtualAudioInputStream::GetVolume() { | 173 double VirtualAudioInputStream::GetVolume() { |
| 164 return 1.0; | 174 return 1.0; |
| 165 } | 175 } |
| 166 | 176 |
| 167 void VirtualAudioInputStream::SetAutomaticGainControl(bool enabled) {} | 177 void VirtualAudioInputStream::SetAutomaticGainControl(bool enabled) {} |
| 168 | 178 |
| 169 bool VirtualAudioInputStream::GetAutomaticGainControl() { | 179 bool VirtualAudioInputStream::GetAutomaticGainControl() { |
| 170 return false; | 180 return false; |
| 171 } | 181 } |
| 172 | 182 |
| 173 } // namespace media | 183 } // namespace media |
| OLD | NEW |