| 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 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 42 : ScopedLoopObserver(io_loop), | 42 : ScopedLoopObserver(io_loop), |
| 43 callback_(NULL), | 43 callback_(NULL), |
| 44 ipc_(ipc), | 44 ipc_(ipc), |
| 45 stream_id_(0), | 45 stream_id_(0), |
| 46 play_on_start_(true), | 46 play_on_start_(true), |
| 47 is_started_(false) { | 47 is_started_(false) { |
| 48 CHECK(ipc_); | 48 CHECK(ipc_); |
| 49 } | 49 } |
| 50 | 50 |
| 51 void AudioOutputDevice::Initialize(const AudioParameters& params, | 51 void AudioOutputDevice::Initialize(const AudioParameters& params, |
| 52 RenderCallback* callback) { | 52 RenderCallback* callback) { |
| 53 CHECK_EQ(0, stream_id_) << | 53 CHECK_EQ(0, stream_id_) << |
| 54 "AudioOutputDevice::Initialize() must be called before Start()"; | 54 "AudioOutputDevice::Initialize() must be called before Start()"; |
| 55 | 55 |
| 56 CHECK(!callback_); // Calling Initialize() twice? | 56 CHECK(!callback_); // Calling Initialize() twice? |
| 57 | 57 |
| 58 audio_parameters_ = params; | 58 audio_parameters_ = params; |
| 59 callback_ = callback; | 59 callback_ = callback; |
| 60 } | 60 } |
| 61 | 61 |
| 62 AudioOutputDevice::~AudioOutputDevice() { | 62 AudioOutputDevice::~AudioOutputDevice() { |
| (...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 187 if (!audio_thread_.IsStopped()) | 187 if (!audio_thread_.IsStopped()) |
| 188 callback_->OnRenderError(); | 188 callback_->OnRenderError(); |
| 189 } | 189 } |
| 190 } | 190 } |
| 191 | 191 |
| 192 void AudioOutputDevice::OnStreamCreated( | 192 void AudioOutputDevice::OnStreamCreated( |
| 193 base::SharedMemoryHandle handle, | 193 base::SharedMemoryHandle handle, |
| 194 base::SyncSocket::Handle socket_handle, | 194 base::SyncSocket::Handle socket_handle, |
| 195 int length) { | 195 int length) { |
| 196 DCHECK(message_loop()->BelongsToCurrentThread()); | 196 DCHECK(message_loop()->BelongsToCurrentThread()); |
| 197 DCHECK_GE(length, audio_parameters_.GetBytesPerBuffer()); | |
| 198 #if defined(OS_WIN) | 197 #if defined(OS_WIN) |
| 199 DCHECK(handle); | 198 DCHECK(handle); |
| 200 DCHECK(socket_handle); | 199 DCHECK(socket_handle); |
| 201 #else | 200 #else |
| 202 DCHECK_GE(handle.fd, 0); | 201 DCHECK_GE(handle.fd, 0); |
| 203 DCHECK_GE(socket_handle, 0); | 202 DCHECK_GE(socket_handle, 0); |
| 204 #endif | 203 #endif |
| 205 | 204 |
| 206 // We should only get this callback if stream_id_ is valid. If it is not, | 205 // 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 | 206 // the IPC layer should have closed the shared memory and socket handles |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 259 SetActualDataSizeInBytes(&shared_memory_, memory_length_, 0); | 258 SetActualDataSizeInBytes(&shared_memory_, memory_length_, 0); |
| 260 return; | 259 return; |
| 261 } | 260 } |
| 262 | 261 |
| 263 // Convert the number of pending bytes in the render buffer | 262 // Convert the number of pending bytes in the render buffer |
| 264 // into milliseconds. | 263 // into milliseconds. |
| 265 int audio_delay_milliseconds = pending_data / bytes_per_ms_; | 264 int audio_delay_milliseconds = pending_data / bytes_per_ms_; |
| 266 | 265 |
| 267 TRACE_EVENT0("audio", "AudioOutputDevice::FireRenderCallback"); | 266 TRACE_EVENT0("audio", "AudioOutputDevice::FireRenderCallback"); |
| 268 | 267 |
| 269 // Update the audio-delay measurement then ask client to render audio. | 268 // Update the audio-delay measurement then ask client to render audio. Since |
| 269 // |audio_bus_| is wrapping the shared memory the Render() call is writing |
| 270 // directly into the shared memory. |
| 270 size_t num_frames = render_callback_->Render( | 271 size_t num_frames = render_callback_->Render( |
| 271 audio_bus_.get(), audio_delay_milliseconds); | 272 audio_bus_.get(), audio_delay_milliseconds); |
| 272 | 273 |
| 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. | 274 // Let the host know we are done. |
| 275 // TODO(dalecurtis): Technically this is not always correct. Due to channel |
| 276 // padding for alignment, there may be more data available than this. We're |
| 277 // relying on AudioSyncReader::Read() to parse this with that in mind. Rename |
| 278 // these methods to Set/GetActualFrameCount(). |
| 280 SetActualDataSizeInBytes( | 279 SetActualDataSizeInBytes( |
| 281 &shared_memory_, memory_length_, | 280 &shared_memory_, memory_length_, |
| 282 num_frames * audio_parameters_.GetBytesPerFrame()); | 281 num_frames * sizeof(*audio_bus_->channel(0)) * audio_bus_->channels()); |
| 283 } | 282 } |
| 284 | 283 |
| 285 } // namespace media. | 284 } // namespace media. |
| OLD | NEW |