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_controller.h" | 5 #include "media/audio/audio_output_controller.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/debug/trace_event.h" | 8 #include "base/debug/trace_event.h" |
9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
10 #include "base/synchronization/waitable_event.h" | 10 #include "base/synchronization/waitable_event.h" |
11 #include "base/threading/platform_thread.h" | 11 #include "base/threading/platform_thread.h" |
12 #include "base/threading/thread_restrictions.h" | 12 #include "base/threading/thread_restrictions.h" |
13 #include "base/time.h" | 13 #include "base/time.h" |
14 #include "media/audio/shared_memory_util.h" | 14 #include "media/audio/shared_memory_util.h" |
15 | 15 |
16 using base::Time; | 16 using base::Time; |
17 using base::TimeDelta; | 17 using base::TimeDelta; |
18 using base::WaitableEvent; | 18 using base::WaitableEvent; |
19 | 19 |
20 namespace media { | 20 namespace media { |
21 | 21 |
22 // Polling-related constants. | 22 // Polling-related constants. |
23 const int AudioOutputController::kPollNumAttempts = 3; | 23 const int AudioOutputController::kPollNumAttempts = 3; |
24 const int AudioOutputController::kPollPauseInMilliseconds = 3; | 24 const int AudioOutputController::kPollPauseInMilliseconds = 3; |
25 | 25 |
26 AudioOutputController::AudioOutputController(EventHandler* handler, | 26 AudioOutputController::AudioOutputController(EventHandler* handler, |
27 SyncReader* sync_reader) | 27 SyncReader* sync_reader, |
| 28 const AudioParameters& params) |
28 : handler_(handler), | 29 : handler_(handler), |
29 stream_(NULL), | 30 stream_(NULL), |
30 volume_(1.0), | 31 volume_(1.0), |
31 state_(kEmpty), | 32 state_(kEmpty), |
32 sync_reader_(sync_reader), | 33 sync_reader_(sync_reader), |
33 message_loop_(NULL), | 34 message_loop_(NULL), |
34 number_polling_attempts_left_(0), | 35 number_polling_attempts_left_(0), |
| 36 params_(params), |
35 ALLOW_THIS_IN_INITIALIZER_LIST(weak_this_(this)) { | 37 ALLOW_THIS_IN_INITIALIZER_LIST(weak_this_(this)) { |
36 } | 38 } |
37 | 39 |
38 AudioOutputController::~AudioOutputController() { | 40 AudioOutputController::~AudioOutputController() { |
39 DCHECK_EQ(kClosed, state_); | 41 DCHECK_EQ(kClosed, state_); |
40 DCHECK(message_loop_); | 42 DCHECK(message_loop_); |
41 | 43 |
42 if (!message_loop_.get() || message_loop_->BelongsToCurrentThread()) { | 44 if (!message_loop_.get() || message_loop_->BelongsToCurrentThread()) { |
43 DoStopCloseAndClearStream(NULL); | 45 DoStopCloseAndClearStream(NULL); |
44 } else { | 46 } else { |
(...skipping 16 matching lines...) Expand all Loading... |
61 const AudioParameters& params, | 63 const AudioParameters& params, |
62 SyncReader* sync_reader) { | 64 SyncReader* sync_reader) { |
63 DCHECK(audio_manager); | 65 DCHECK(audio_manager); |
64 DCHECK(sync_reader); | 66 DCHECK(sync_reader); |
65 | 67 |
66 if (!params.IsValid() || !audio_manager) | 68 if (!params.IsValid() || !audio_manager) |
67 return NULL; | 69 return NULL; |
68 | 70 |
69 // Starts the audio controller thread. | 71 // Starts the audio controller thread. |
70 scoped_refptr<AudioOutputController> controller(new AudioOutputController( | 72 scoped_refptr<AudioOutputController> controller(new AudioOutputController( |
71 event_handler, sync_reader)); | 73 event_handler, sync_reader, params)); |
72 | 74 |
73 controller->message_loop_ = audio_manager->GetMessageLoop(); | 75 controller->message_loop_ = audio_manager->GetMessageLoop(); |
74 controller->message_loop_->PostTask(FROM_HERE, base::Bind( | 76 controller->message_loop_->PostTask(FROM_HERE, base::Bind( |
75 &AudioOutputController::DoCreate, controller, | 77 &AudioOutputController::DoCreate, controller, |
76 base::Unretained(audio_manager), params)); | 78 base::Unretained(audio_manager))); |
77 return controller; | 79 return controller; |
78 } | 80 } |
79 | 81 |
80 void AudioOutputController::Play() { | 82 void AudioOutputController::Play() { |
81 DCHECK(message_loop_); | 83 DCHECK(message_loop_); |
82 message_loop_->PostTask(FROM_HERE, base::Bind( | 84 message_loop_->PostTask(FROM_HERE, base::Bind( |
83 &AudioOutputController::DoPlay, this)); | 85 &AudioOutputController::DoPlay, this)); |
84 } | 86 } |
85 | 87 |
86 void AudioOutputController::Pause() { | 88 void AudioOutputController::Pause() { |
(...skipping 14 matching lines...) Expand all Loading... |
101 message_loop_->PostTaskAndReply(FROM_HERE, base::Bind( | 103 message_loop_->PostTaskAndReply(FROM_HERE, base::Bind( |
102 &AudioOutputController::DoClose, this), closed_task); | 104 &AudioOutputController::DoClose, this), closed_task); |
103 } | 105 } |
104 | 106 |
105 void AudioOutputController::SetVolume(double volume) { | 107 void AudioOutputController::SetVolume(double volume) { |
106 DCHECK(message_loop_); | 108 DCHECK(message_loop_); |
107 message_loop_->PostTask(FROM_HERE, base::Bind( | 109 message_loop_->PostTask(FROM_HERE, base::Bind( |
108 &AudioOutputController::DoSetVolume, this, volume)); | 110 &AudioOutputController::DoSetVolume, this, volume)); |
109 } | 111 } |
110 | 112 |
111 void AudioOutputController::DoCreate(AudioManager* audio_manager, | 113 void AudioOutputController::DoCreate(AudioManager* audio_manager) { |
112 const AudioParameters& params) { | |
113 DCHECK(message_loop_->BelongsToCurrentThread()); | 114 DCHECK(message_loop_->BelongsToCurrentThread()); |
114 | 115 |
115 // Close() can be called before DoCreate() is executed. | 116 // Close() can be called before DoCreate() is executed. |
116 if (state_ == kClosed) | 117 if (state_ == kClosed) |
117 return; | 118 return; |
118 DCHECK_EQ(kEmpty, state_); | 119 DCHECK_EQ(kEmpty, state_); |
119 | 120 |
120 DoStopCloseAndClearStream(NULL); | 121 DoStopCloseAndClearStream(NULL); |
121 stream_ = audio_manager->MakeAudioOutputStreamProxy(params); | 122 stream_ = audio_manager->MakeAudioOutputStreamProxy(params_); |
122 if (!stream_) { | 123 if (!stream_) { |
123 // TODO(hclam): Define error types. | 124 // TODO(hclam): Define error types. |
124 handler_->OnError(this, 0); | 125 handler_->OnError(this, 0); |
125 return; | 126 return; |
126 } | 127 } |
127 | 128 |
128 if (!stream_->Open()) { | 129 if (!stream_->Open()) { |
129 DoStopCloseAndClearStream(NULL); | 130 DoStopCloseAndClearStream(NULL); |
130 | 131 |
131 // TODO(hclam): Define error types. | 132 // TODO(hclam): Define error types. |
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
275 return; | 276 return; |
276 } | 277 } |
277 } | 278 } |
278 | 279 |
279 void AudioOutputController::DoReportError(int code) { | 280 void AudioOutputController::DoReportError(int code) { |
280 DCHECK(message_loop_->BelongsToCurrentThread()); | 281 DCHECK(message_loop_->BelongsToCurrentThread()); |
281 if (state_ != kClosed) | 282 if (state_ != kClosed) |
282 handler_->OnError(this, code); | 283 handler_->OnError(this, code); |
283 } | 284 } |
284 | 285 |
285 uint32 AudioOutputController::OnMoreData(uint8* dest, | 286 int AudioOutputController::OnMoreData(AudioBus* audio_bus, |
286 uint32 max_size, | 287 AudioBuffersState buffers_state) { |
287 AudioBuffersState buffers_state) { | |
288 TRACE_EVENT0("audio", "AudioOutputController::OnMoreData"); | 288 TRACE_EVENT0("audio", "AudioOutputController::OnMoreData"); |
289 | 289 |
290 { | 290 { |
291 // Check state and do nothing if we are not playing. | 291 // Check state and do nothing if we are not playing. |
292 // We are on the hardware audio thread, so lock is needed. | 292 // We are on the hardware audio thread, so lock is needed. |
293 base::AutoLock auto_lock(lock_); | 293 base::AutoLock auto_lock(lock_); |
294 if (state_ != kPlaying) { | 294 if (state_ != kPlaying) { |
295 return 0; | 295 return 0; |
296 } | 296 } |
297 } | 297 } |
298 uint32 size = sync_reader_->Read(dest, max_size); | 298 int frames = sync_reader_->Read(audio_bus); |
299 sync_reader_->UpdatePendingBytes(buffers_state.total_bytes() + size); | 299 sync_reader_->UpdatePendingBytes( |
300 return size; | 300 buffers_state.total_bytes() + frames * params_.GetBytesPerFrame()); |
| 301 return frames; |
301 } | 302 } |
302 | 303 |
303 void AudioOutputController::WaitTillDataReady() { | 304 void AudioOutputController::WaitTillDataReady() { |
304 if (!sync_reader_->DataReady()) { | 305 if (!sync_reader_->DataReady()) { |
305 // In the different place we use different mechanism to poll, get max | 306 // In the different place we use different mechanism to poll, get max |
306 // polling delay from constants used there. | 307 // polling delay from constants used there. |
307 const base::TimeDelta kMaxPollingDelay = TimeDelta::FromMilliseconds( | 308 const base::TimeDelta kMaxPollingDelay = TimeDelta::FromMilliseconds( |
308 kPollNumAttempts * kPollPauseInMilliseconds); | 309 kPollNumAttempts * kPollPauseInMilliseconds); |
309 Time start_time = Time::Now(); | 310 Time start_time = Time::Now(); |
310 do { | 311 do { |
(...skipping 19 matching lines...) Expand all Loading... |
330 stream_ = NULL; | 331 stream_ = NULL; |
331 weak_this_.InvalidateWeakPtrs(); | 332 weak_this_.InvalidateWeakPtrs(); |
332 } | 333 } |
333 | 334 |
334 // Should be last in the method, do not touch "this" from here on. | 335 // Should be last in the method, do not touch "this" from here on. |
335 if (done != NULL) | 336 if (done != NULL) |
336 done->Signal(); | 337 done->Signal(); |
337 } | 338 } |
338 | 339 |
339 } // namespace media | 340 } // namespace media |
OLD | NEW |