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/audio_output_device.h" | 5 #include "media/audio/audio_output_device.h" |
6 | 6 |
7 #include "base/debug/trace_event.h" | 7 #include "base/debug/trace_event.h" |
8 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
9 #include "base/threading/thread_restrictions.h" | 9 #include "base/threading/thread_restrictions.h" |
10 #include "base/time.h" | 10 #include "base/time.h" |
(...skipping 15 matching lines...) Expand all Loading... |
26 AudioRendererSink::RenderCallback* render_callback); | 26 AudioRendererSink::RenderCallback* render_callback); |
27 virtual ~AudioThreadCallback(); | 27 virtual ~AudioThreadCallback(); |
28 | 28 |
29 virtual void MapSharedMemory() OVERRIDE; | 29 virtual void MapSharedMemory() OVERRIDE; |
30 | 30 |
31 // Called whenever we receive notifications about pending data. | 31 // Called whenever we receive notifications about pending data. |
32 virtual void Process(int pending_data) OVERRIDE; | 32 virtual void Process(int pending_data) OVERRIDE; |
33 | 33 |
34 private: | 34 private: |
35 AudioRendererSink::RenderCallback* render_callback_; | 35 AudioRendererSink::RenderCallback* render_callback_; |
| 36 scoped_ptr<AudioBus> audio_bus_; |
36 DISALLOW_COPY_AND_ASSIGN(AudioThreadCallback); | 37 DISALLOW_COPY_AND_ASSIGN(AudioThreadCallback); |
37 }; | 38 }; |
38 | 39 |
39 AudioOutputDevice::AudioOutputDevice( | 40 AudioOutputDevice::AudioOutputDevice( |
40 AudioOutputIPC* ipc, | 41 AudioOutputIPC* ipc, |
41 const scoped_refptr<base::MessageLoopProxy>& io_loop) | 42 const scoped_refptr<base::MessageLoopProxy>& io_loop) |
42 : ScopedLoopObserver(io_loop), | 43 : ScopedLoopObserver(io_loop), |
43 callback_(NULL), | 44 callback_(NULL), |
44 ipc_(ipc), | 45 ipc_(ipc), |
45 stream_id_(0), | 46 stream_id_(0), |
46 play_on_start_(true), | 47 play_on_start_(true), |
47 is_started_(false) { | 48 is_started_(false) { |
48 CHECK(ipc_); | 49 CHECK(ipc_); |
49 } | 50 } |
50 | 51 |
51 void AudioOutputDevice::Initialize(const AudioParameters& params, | 52 void AudioOutputDevice::Initialize(const AudioParameters& params, |
52 RenderCallback* callback) { | 53 RenderCallback* callback) { |
53 CHECK_EQ(0, stream_id_) << | 54 CHECK_EQ(0, stream_id_) << |
54 "AudioOutputDevice::Initialize() must be called before Start()"; | 55 "AudioOutputDevice::Initialize() must be called before Start()"; |
55 | 56 |
56 CHECK(!callback_); // Calling Initialize() twice? | 57 CHECK(!callback_); // Calling Initialize() twice? |
57 | 58 |
58 audio_parameters_ = params; | 59 audio_parameters_ = params; |
59 callback_ = callback; | 60 callback_ = callback; |
60 } | 61 } |
61 | 62 |
62 AudioOutputDevice::~AudioOutputDevice() { | 63 AudioOutputDevice::~AudioOutputDevice() { |
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
187 if (!audio_thread_.IsStopped()) | 188 if (!audio_thread_.IsStopped()) |
188 callback_->OnRenderError(); | 189 callback_->OnRenderError(); |
189 } | 190 } |
190 } | 191 } |
191 | 192 |
192 void AudioOutputDevice::OnStreamCreated( | 193 void AudioOutputDevice::OnStreamCreated( |
193 base::SharedMemoryHandle handle, | 194 base::SharedMemoryHandle handle, |
194 base::SyncSocket::Handle socket_handle, | 195 base::SyncSocket::Handle socket_handle, |
195 int length) { | 196 int length) { |
196 DCHECK(message_loop()->BelongsToCurrentThread()); | 197 DCHECK(message_loop()->BelongsToCurrentThread()); |
197 DCHECK_GE(length, audio_parameters_.GetBytesPerBuffer()); | |
198 #if defined(OS_WIN) | 198 #if defined(OS_WIN) |
199 DCHECK(handle); | 199 DCHECK(handle); |
200 DCHECK(socket_handle); | 200 DCHECK(socket_handle); |
201 #else | 201 #else |
202 DCHECK_GE(handle.fd, 0); | 202 DCHECK_GE(handle.fd, 0); |
203 DCHECK_GE(socket_handle, 0); | 203 DCHECK_GE(socket_handle, 0); |
204 #endif | 204 #endif |
205 | 205 |
206 // We should only get this callback if stream_id_ is valid. If it is not, | 206 // We should only get this callback if stream_id_ is valid. If it is not, |
207 // the IPC layer should have closed the shared memory and socket handles | 207 // the IPC layer should have closed the shared memory and socket handles |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
243 AudioRendererSink::RenderCallback* render_callback) | 243 AudioRendererSink::RenderCallback* render_callback) |
244 : AudioDeviceThread::Callback(audio_parameters, memory, memory_length), | 244 : AudioDeviceThread::Callback(audio_parameters, memory, memory_length), |
245 render_callback_(render_callback) { | 245 render_callback_(render_callback) { |
246 } | 246 } |
247 | 247 |
248 AudioOutputDevice::AudioThreadCallback::~AudioThreadCallback() { | 248 AudioOutputDevice::AudioThreadCallback::~AudioThreadCallback() { |
249 } | 249 } |
250 | 250 |
251 void AudioOutputDevice::AudioThreadCallback::MapSharedMemory() { | 251 void AudioOutputDevice::AudioThreadCallback::MapSharedMemory() { |
252 shared_memory_.Map(TotalSharedMemorySizeInBytes(memory_length_)); | 252 shared_memory_.Map(TotalSharedMemorySizeInBytes(memory_length_)); |
| 253 DCHECK_EQ(memory_length_, AudioBus::CalculateMemorySize(audio_parameters_)); |
| 254 audio_bus_ = AudioBus::WrapMemory(audio_parameters_, shared_memory_.memory()); |
253 } | 255 } |
254 | 256 |
255 // Called whenever we receive notifications about pending data. | 257 // Called whenever we receive notifications about pending data. |
256 void AudioOutputDevice::AudioThreadCallback::Process(int pending_data) { | 258 void AudioOutputDevice::AudioThreadCallback::Process(int pending_data) { |
257 if (pending_data == kPauseMark) { | 259 if (pending_data == kPauseMark) { |
258 memset(shared_memory_.memory(), 0, memory_length_); | 260 memset(shared_memory_.memory(), 0, memory_length_); |
259 SetActualDataSizeInBytes(&shared_memory_, memory_length_, 0); | 261 SetActualDataSizeInBytes(&shared_memory_, memory_length_, 0); |
260 return; | 262 return; |
261 } | 263 } |
262 | 264 |
263 // Convert the number of pending bytes in the render buffer | 265 // Convert the number of pending bytes in the render buffer |
264 // into milliseconds. | 266 // into milliseconds. |
265 int audio_delay_milliseconds = pending_data / bytes_per_ms_; | 267 int audio_delay_milliseconds = pending_data / bytes_per_ms_; |
266 | 268 |
267 TRACE_EVENT0("audio", "AudioOutputDevice::FireRenderCallback"); | 269 TRACE_EVENT0("audio", "AudioOutputDevice::FireRenderCallback"); |
268 | 270 |
269 // Update the audio-delay measurement then ask client to render audio. | 271 // Update the audio-delay measurement then ask client to render audio. Since |
| 272 // |audio_bus_| is wrapping the shared memory the Render() call is writing |
| 273 // directly into the shared memory. |
270 size_t num_frames = render_callback_->Render( | 274 size_t num_frames = render_callback_->Render( |
271 audio_bus_.get(), audio_delay_milliseconds); | 275 audio_bus_.get(), audio_delay_milliseconds); |
272 | 276 |
273 // Interleave, scale, and clip to int. | |
274 // TODO(dalecurtis): Remove this when we have float everywhere: | |
275 // http://crbug.com/114700 | |
276 audio_bus_->ToInterleaved(num_frames, audio_parameters_.bits_per_sample() / 8, | |
277 shared_memory_.memory()); | |
278 | |
279 // Let the host know we are done. | 277 // Let the host know we are done. |
| 278 // TODO(dalecurtis): Technically this is not always correct. Due to channel |
| 279 // padding for alignment, there may be more data available than this. We're |
| 280 // relying on AudioSyncReader::Read() to parse this with that in mind. Rename |
| 281 // these methods to Set/GetActualFrameCount(). |
280 SetActualDataSizeInBytes( | 282 SetActualDataSizeInBytes( |
281 &shared_memory_, memory_length_, | 283 &shared_memory_, memory_length_, |
282 num_frames * audio_parameters_.GetBytesPerFrame()); | 284 num_frames * sizeof(*audio_bus_->channel(0)) * audio_bus_->channels()); |
283 } | 285 } |
284 | 286 |
285 } // namespace media. | 287 } // namespace media. |
OLD | NEW |