| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2011 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 "content/renderer/audio_device.h" | |
| 6 | |
| 7 #include "base/memory/singleton.h" | |
| 8 #include "base/message_loop.h" | |
| 9 #include "content/common/audio_messages.h" | |
| 10 #include "content/common/child_process.h" | |
| 11 #include "content/common/view_messages.h" | |
| 12 #include "content/renderer/render_thread.h" | |
| 13 #include "media/audio/audio_util.h" | |
| 14 | |
| 15 scoped_refptr<AudioMessageFilter> AudioDevice::filter_; | |
| 16 | |
| 17 namespace { | |
| 18 | |
| 19 // AudioMessageFilterCreator is intended to be used as a singleton so we can | |
| 20 // get access to a shared AudioMessageFilter. | |
| 21 // Example usage: | |
| 22 // AudioMessageFilter* filter = AudioMessageFilterCreator::SharedFilter(); | |
| 23 | |
| 24 class AudioMessageFilterCreator { | |
| 25 public: | |
| 26 AudioMessageFilterCreator() { | |
| 27 int routing_id; | |
| 28 RenderThread::current()->Send( | |
| 29 new ViewHostMsg_GenerateRoutingID(&routing_id)); | |
| 30 filter_ = new AudioMessageFilter(routing_id); | |
| 31 RenderThread::current()->AddFilter(filter_); | |
| 32 } | |
| 33 | |
| 34 static AudioMessageFilter* SharedFilter() { | |
| 35 return GetInstance()->filter_.get(); | |
| 36 } | |
| 37 | |
| 38 static AudioMessageFilterCreator* GetInstance() { | |
| 39 return Singleton<AudioMessageFilterCreator>::get(); | |
| 40 } | |
| 41 | |
| 42 private: | |
| 43 scoped_refptr<AudioMessageFilter> filter_; | |
| 44 }; | |
| 45 | |
| 46 } // namespace | |
| 47 | |
| 48 AudioDevice::AudioDevice(size_t buffer_size, | |
| 49 int channels, | |
| 50 double sample_rate, | |
| 51 RenderCallback* callback) | |
| 52 : buffer_size_(buffer_size), | |
| 53 channels_(channels), | |
| 54 bits_per_sample_(16), | |
| 55 sample_rate_(sample_rate), | |
| 56 callback_(callback), | |
| 57 audio_delay_milliseconds_(0), | |
| 58 volume_(1.0), | |
| 59 stream_id_(0) { | |
| 60 audio_data_.reserve(channels); | |
| 61 for (int i = 0; i < channels; ++i) { | |
| 62 float* channel_data = new float[buffer_size]; | |
| 63 audio_data_.push_back(channel_data); | |
| 64 } | |
| 65 // Lazily create the message filter and share across AudioDevice instances. | |
| 66 filter_ = AudioMessageFilterCreator::SharedFilter(); | |
| 67 } | |
| 68 | |
| 69 AudioDevice::~AudioDevice() { | |
| 70 // Make sure we have been shut down. | |
| 71 DCHECK_EQ(0, stream_id_); | |
| 72 Stop(); | |
| 73 for (int i = 0; i < channels_; ++i) | |
| 74 delete [] audio_data_[i]; | |
| 75 } | |
| 76 | |
| 77 bool AudioDevice::Start() { | |
| 78 // Make sure we don't call Start() more than once. | |
| 79 DCHECK_EQ(0, stream_id_); | |
| 80 if (stream_id_) | |
| 81 return false; | |
| 82 | |
| 83 AudioParameters params; | |
| 84 params.format = AudioParameters::AUDIO_PCM_LOW_LATENCY; | |
| 85 params.channels = channels_; | |
| 86 params.sample_rate = static_cast<int>(sample_rate_); | |
| 87 params.bits_per_sample = bits_per_sample_; | |
| 88 params.samples_per_packet = buffer_size_; | |
| 89 | |
| 90 // Ensure that the initialization task is posted on the I/O thread by | |
| 91 // accessing the I/O message loop directly. This approach avoids a race | |
| 92 // condition which could exist if the message loop of the filter was | |
| 93 // used instead. | |
| 94 DCHECK(ChildProcess::current()) << "Must be in the renderer"; | |
| 95 MessageLoop* message_loop = ChildProcess::current()->io_message_loop(); | |
| 96 if (!message_loop) | |
| 97 return false; | |
| 98 | |
| 99 message_loop->PostTask(FROM_HERE, | |
| 100 NewRunnableMethod(this, &AudioDevice::InitializeOnIOThread, params)); | |
| 101 | |
| 102 return true; | |
| 103 } | |
| 104 | |
| 105 bool AudioDevice::Stop() { | |
| 106 if (!stream_id_) | |
| 107 return false; | |
| 108 | |
| 109 filter_->message_loop()->PostTask(FROM_HERE, | |
| 110 NewRunnableMethod(this, &AudioDevice::ShutDownOnIOThread)); | |
| 111 | |
| 112 if (audio_thread_.get()) { | |
| 113 socket_->Close(); | |
| 114 audio_thread_->Join(); | |
| 115 } | |
| 116 | |
| 117 return true; | |
| 118 } | |
| 119 | |
| 120 bool AudioDevice::SetVolume(double volume) { | |
| 121 if (!stream_id_) | |
| 122 return false; | |
| 123 | |
| 124 if (volume < 0 || volume > 1.0) | |
| 125 return false; | |
| 126 | |
| 127 filter_->message_loop()->PostTask(FROM_HERE, | |
| 128 NewRunnableMethod(this, &AudioDevice::SetVolumeOnIOThread, volume)); | |
| 129 | |
| 130 volume_ = volume; | |
| 131 | |
| 132 return true; | |
| 133 } | |
| 134 | |
| 135 bool AudioDevice::GetVolume(double* volume) { | |
| 136 if (!stream_id_) | |
| 137 return false; | |
| 138 | |
| 139 // Return a locally cached version of the current scaling factor. | |
| 140 *volume = volume_; | |
| 141 | |
| 142 return true; | |
| 143 } | |
| 144 | |
| 145 void AudioDevice::InitializeOnIOThread(const AudioParameters& params) { | |
| 146 stream_id_ = filter_->AddDelegate(this); | |
| 147 filter_->Send(new AudioHostMsg_CreateStream(0, stream_id_, params, true)); | |
| 148 } | |
| 149 | |
| 150 void AudioDevice::StartOnIOThread() { | |
| 151 if (stream_id_) | |
| 152 filter_->Send(new AudioHostMsg_PlayStream(0, stream_id_)); | |
| 153 } | |
| 154 | |
| 155 void AudioDevice::ShutDownOnIOThread() { | |
| 156 // Make sure we don't call shutdown more than once. | |
| 157 if (!stream_id_) | |
| 158 return; | |
| 159 | |
| 160 filter_->Send(new AudioHostMsg_CloseStream(0, stream_id_)); | |
| 161 filter_->RemoveDelegate(stream_id_); | |
| 162 stream_id_ = 0; | |
| 163 } | |
| 164 | |
| 165 void AudioDevice::SetVolumeOnIOThread(double volume) { | |
| 166 if (stream_id_) | |
| 167 filter_->Send(new AudioHostMsg_SetVolume(0, stream_id_, volume)); | |
| 168 } | |
| 169 | |
| 170 void AudioDevice::OnRequestPacket(AudioBuffersState buffers_state) { | |
| 171 // This method does not apply to the low-latency system. | |
| 172 NOTIMPLEMENTED(); | |
| 173 } | |
| 174 | |
| 175 void AudioDevice::OnStateChanged(AudioStreamState state) { | |
| 176 if (state == kAudioStreamError) { | |
| 177 DLOG(WARNING) << "AudioDevice::OnStateChanged(kError)"; | |
| 178 } | |
| 179 NOTIMPLEMENTED(); | |
| 180 } | |
| 181 | |
| 182 void AudioDevice::OnCreated( | |
| 183 base::SharedMemoryHandle handle, uint32 length) { | |
| 184 // Not needed in this simple implementation. | |
| 185 NOTIMPLEMENTED(); | |
| 186 } | |
| 187 | |
| 188 void AudioDevice::OnLowLatencyCreated( | |
| 189 base::SharedMemoryHandle handle, | |
| 190 base::SyncSocket::Handle socket_handle, | |
| 191 uint32 length) { | |
| 192 #if defined(OS_WIN) | |
| 193 DCHECK(handle); | |
| 194 DCHECK(socket_handle); | |
| 195 #else | |
| 196 DCHECK_GE(handle.fd, 0); | |
| 197 DCHECK_GE(socket_handle, 0); | |
| 198 #endif | |
| 199 DCHECK(length); | |
| 200 | |
| 201 // TODO(crogers) : check that length is big enough for buffer_size_ | |
| 202 | |
| 203 shared_memory_.reset(new base::SharedMemory(handle, false)); | |
| 204 shared_memory_->Map(length); | |
| 205 | |
| 206 socket_.reset(new base::SyncSocket(socket_handle)); | |
| 207 // Allow the client to pre-populate the buffer. | |
| 208 FireRenderCallback(); | |
| 209 | |
| 210 audio_thread_.reset( | |
| 211 new base::DelegateSimpleThread(this, "renderer_audio_thread")); | |
| 212 audio_thread_->Start(); | |
| 213 | |
| 214 if (filter_) { | |
| 215 filter_->message_loop()->PostTask(FROM_HERE, | |
| 216 NewRunnableMethod(this, &AudioDevice::StartOnIOThread)); | |
| 217 } | |
| 218 } | |
| 219 | |
| 220 void AudioDevice::OnVolume(double volume) { | |
| 221 NOTIMPLEMENTED(); | |
| 222 } | |
| 223 | |
| 224 // Our audio thread runs here. | |
| 225 void AudioDevice::Run() { | |
| 226 audio_thread_->SetThreadPriority(base::kThreadPriority_RealtimeAudio); | |
| 227 | |
| 228 int pending_data; | |
| 229 const int samples_per_ms = static_cast<int>(sample_rate_) / 1000; | |
| 230 const int bytes_per_ms = channels_ * (bits_per_sample_ / 8) * samples_per_ms; | |
| 231 | |
| 232 while (sizeof(pending_data) == socket_->Receive(&pending_data, | |
| 233 sizeof(pending_data)) && | |
| 234 pending_data >= 0) { | |
| 235 { | |
| 236 // Convert the number of pending bytes in the render buffer | |
| 237 // into milliseconds. | |
| 238 audio_delay_milliseconds_ = pending_data / bytes_per_ms; | |
| 239 } | |
| 240 | |
| 241 FireRenderCallback(); | |
| 242 } | |
| 243 } | |
| 244 | |
| 245 void AudioDevice::FireRenderCallback() { | |
| 246 if (callback_) { | |
| 247 // Update the audio-delay measurement then ask client to render audio. | |
| 248 callback_->Render(audio_data_, buffer_size_, audio_delay_milliseconds_); | |
| 249 | |
| 250 // Interleave, scale, and clip to int16. | |
| 251 int16* output_buffer16 = static_cast<int16*>(shared_memory_data()); | |
| 252 media::InterleaveFloatToInt16(audio_data_, output_buffer16, buffer_size_); | |
| 253 } | |
| 254 } | |
| OLD | NEW |