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/basictypes.h" | 7 #include "base/basictypes.h" |
8 #include "base/debug/trace_event.h" | 8 #include "base/debug/trace_event.h" |
9 #include "base/threading/thread_restrictions.h" | 9 #include "base/threading/thread_restrictions.h" |
10 #include "base/time/time.h" | 10 #include "base/time/time.h" |
(...skipping 14 matching lines...) Expand all Loading... |
25 AudioRendererSink::RenderCallback* render_callback); | 25 AudioRendererSink::RenderCallback* render_callback); |
26 virtual ~AudioThreadCallback(); | 26 virtual ~AudioThreadCallback(); |
27 | 27 |
28 virtual void MapSharedMemory() OVERRIDE; | 28 virtual void MapSharedMemory() OVERRIDE; |
29 | 29 |
30 // Called whenever we receive notifications about pending data. | 30 // Called whenever we receive notifications about pending data. |
31 virtual void Process(int pending_data) OVERRIDE; | 31 virtual void Process(int pending_data) OVERRIDE; |
32 | 32 |
33 private: | 33 private: |
34 AudioRendererSink::RenderCallback* render_callback_; | 34 AudioRendererSink::RenderCallback* render_callback_; |
35 scoped_ptr<AudioBus> input_bus_; | |
36 scoped_ptr<AudioBus> output_bus_; | 35 scoped_ptr<AudioBus> output_bus_; |
37 DISALLOW_COPY_AND_ASSIGN(AudioThreadCallback); | 36 DISALLOW_COPY_AND_ASSIGN(AudioThreadCallback); |
38 }; | 37 }; |
39 | 38 |
40 AudioOutputDevice::AudioOutputDevice( | 39 AudioOutputDevice::AudioOutputDevice( |
41 scoped_ptr<AudioOutputIPC> ipc, | 40 scoped_ptr<AudioOutputIPC> ipc, |
42 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner) | 41 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner) |
43 : ScopedTaskRunnerObserver(io_task_runner), | 42 : ScopedTaskRunnerObserver(io_task_runner), |
44 callback_(NULL), | 43 callback_(NULL), |
45 ipc_(ipc.Pass()), | 44 ipc_(ipc.Pass()), |
46 state_(IDLE), | 45 state_(IDLE), |
47 play_on_start_(true), | 46 play_on_start_(true), |
48 session_id_(-1), | 47 session_id_(-1), |
49 stopping_hack_(false) { | 48 stopping_hack_(false) { |
50 CHECK(ipc_); | 49 CHECK(ipc_); |
51 | 50 |
52 // The correctness of the code depends on the relative values assigned in the | 51 // The correctness of the code depends on the relative values assigned in the |
53 // State enum. | 52 // State enum. |
54 COMPILE_ASSERT(IPC_CLOSED < IDLE, invalid_enum_value_assignment_0); | 53 COMPILE_ASSERT(IPC_CLOSED < IDLE, invalid_enum_value_assignment_0); |
55 COMPILE_ASSERT(IDLE < CREATING_STREAM, invalid_enum_value_assignment_1); | 54 COMPILE_ASSERT(IDLE < CREATING_STREAM, invalid_enum_value_assignment_1); |
56 COMPILE_ASSERT(CREATING_STREAM < PAUSED, invalid_enum_value_assignment_2); | 55 COMPILE_ASSERT(CREATING_STREAM < PAUSED, invalid_enum_value_assignment_2); |
57 COMPILE_ASSERT(PAUSED < PLAYING, invalid_enum_value_assignment_3); | 56 COMPILE_ASSERT(PAUSED < PLAYING, invalid_enum_value_assignment_3); |
58 } | 57 } |
59 | 58 |
60 void AudioOutputDevice::InitializeUnifiedStream(const AudioParameters& params, | 59 void AudioOutputDevice::InitializeWithSessionId(const AudioParameters& params, |
61 RenderCallback* callback, | 60 RenderCallback* callback, |
62 int session_id) { | 61 int session_id) { |
63 DCHECK(!callback_) << "Calling InitializeUnifiedStream() twice?"; | 62 DCHECK(!callback_) << "Calling InitializeWithSessionId() twice?"; |
64 DCHECK(params.IsValid()); | 63 DCHECK(params.IsValid()); |
65 audio_parameters_ = params; | 64 audio_parameters_ = params; |
66 callback_ = callback; | 65 callback_ = callback; |
67 session_id_ = session_id; | 66 session_id_ = session_id; |
68 } | 67 } |
69 | 68 |
70 void AudioOutputDevice::Initialize(const AudioParameters& params, | 69 void AudioOutputDevice::Initialize(const AudioParameters& params, |
71 RenderCallback* callback) { | 70 RenderCallback* callback) { |
72 InitializeUnifiedStream(params, callback, 0); | 71 InitializeWithSessionId(params, callback, 0); |
73 } | 72 } |
74 | 73 |
75 AudioOutputDevice::~AudioOutputDevice() { | 74 AudioOutputDevice::~AudioOutputDevice() { |
76 // The current design requires that the user calls Stop() before deleting | 75 // The current design requires that the user calls Stop() before deleting |
77 // this class. | 76 // this class. |
78 DCHECK(audio_thread_.IsStopped()); | 77 DCHECK(audio_thread_.IsStopped()); |
79 } | 78 } |
80 | 79 |
81 void AudioOutputDevice::Start() { | 80 void AudioOutputDevice::Start() { |
82 DCHECK(callback_) << "Initialize hasn't been called"; | 81 DCHECK(callback_) << "Initialize hasn't been called"; |
(...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
272 AudioRendererSink::RenderCallback* render_callback) | 271 AudioRendererSink::RenderCallback* render_callback) |
273 : AudioDeviceThread::Callback(audio_parameters, memory, memory_length, 1), | 272 : AudioDeviceThread::Callback(audio_parameters, memory, memory_length, 1), |
274 render_callback_(render_callback) {} | 273 render_callback_(render_callback) {} |
275 | 274 |
276 AudioOutputDevice::AudioThreadCallback::~AudioThreadCallback() { | 275 AudioOutputDevice::AudioThreadCallback::~AudioThreadCallback() { |
277 } | 276 } |
278 | 277 |
279 void AudioOutputDevice::AudioThreadCallback::MapSharedMemory() { | 278 void AudioOutputDevice::AudioThreadCallback::MapSharedMemory() { |
280 CHECK_EQ(total_segments_, 1); | 279 CHECK_EQ(total_segments_, 1); |
281 CHECK(shared_memory_.Map(memory_length_)); | 280 CHECK(shared_memory_.Map(memory_length_)); |
282 | 281 DCHECK_EQ(memory_length_, AudioBus::CalculateMemorySize(audio_parameters_)); |
283 // Calculate output and input memory size. | |
284 int output_memory_size = AudioBus::CalculateMemorySize(audio_parameters_); | |
285 int input_channels = audio_parameters_.input_channels(); | |
286 int frames = audio_parameters_.frames_per_buffer(); | |
287 int input_memory_size = AudioBus::CalculateMemorySize(input_channels, frames); | |
288 | |
289 int io_size = output_memory_size + input_memory_size; | |
290 | |
291 DCHECK_EQ(memory_length_, io_size); | |
292 | 282 |
293 output_bus_ = | 283 output_bus_ = |
294 AudioBus::WrapMemory(audio_parameters_, shared_memory_.memory()); | 284 AudioBus::WrapMemory(audio_parameters_, shared_memory_.memory()); |
295 | |
296 if (input_channels > 0) { | |
297 // The input data is after the output data. | |
298 char* input_data = | |
299 static_cast<char*>(shared_memory_.memory()) + output_memory_size; | |
300 input_bus_ = AudioBus::WrapMemory(input_channels, frames, input_data); | |
301 } | |
302 } | 285 } |
303 | 286 |
304 // Called whenever we receive notifications about pending data. | 287 // Called whenever we receive notifications about pending data. |
305 void AudioOutputDevice::AudioThreadCallback::Process(int pending_data) { | 288 void AudioOutputDevice::AudioThreadCallback::Process(int pending_data) { |
306 // Negative |pending_data| indicates the browser side stream has stopped. | 289 // Negative |pending_data| indicates the browser side stream has stopped. |
307 if (pending_data < 0) | 290 if (pending_data < 0) |
308 return; | 291 return; |
309 | 292 |
310 // Convert the number of pending bytes in the render buffer into milliseconds. | 293 // Convert the number of pending bytes in the render buffer into milliseconds. |
311 int audio_delay_milliseconds = pending_data / bytes_per_ms_; | 294 int audio_delay_milliseconds = pending_data / bytes_per_ms_; |
312 | 295 |
313 TRACE_EVENT0("audio", "AudioOutputDevice::FireRenderCallback"); | 296 TRACE_EVENT0("audio", "AudioOutputDevice::FireRenderCallback"); |
314 | 297 |
315 // Update the audio-delay measurement then ask client to render audio. Since | 298 // Update the audio-delay measurement then ask client to render audio. Since |
316 // |output_bus_| is wrapping the shared memory the Render() call is writing | 299 // |output_bus_| is wrapping the shared memory the Render() call is writing |
317 // directly into the shared memory. | 300 // directly into the shared memory. |
318 int input_channels = audio_parameters_.input_channels(); | 301 render_callback_->Render(output_bus_.get(), audio_delay_milliseconds); |
319 if (input_bus_ && input_channels > 0) { | |
320 render_callback_->RenderIO( | |
321 input_bus_.get(), output_bus_.get(), audio_delay_milliseconds); | |
322 } else { | |
323 render_callback_->Render(output_bus_.get(), audio_delay_milliseconds); | |
324 } | |
325 } | 302 } |
326 | 303 |
327 } // namespace media. | 304 } // namespace media. |
OLD | NEW |