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 "build/build_config.h" |
14 #include "media/audio/shared_memory_util.h" | 15 #include "media/audio/shared_memory_util.h" |
15 | 16 |
16 using base::Time; | 17 using base::Time; |
17 using base::TimeDelta; | 18 using base::TimeDelta; |
18 using base::WaitableEvent; | 19 using base::WaitableEvent; |
19 | 20 |
20 namespace media { | 21 namespace media { |
21 | 22 |
22 // Polling-related constants. | 23 // Polling-related constants. |
23 const int AudioOutputController::kPollNumAttempts = 3; | 24 const int AudioOutputController::kPollNumAttempts = 3; |
(...skipping 288 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
312 } | 313 } |
313 } | 314 } |
314 | 315 |
315 int frames = sync_reader_->Read(source, dest); | 316 int frames = sync_reader_->Read(source, dest); |
316 sync_reader_->UpdatePendingBytes( | 317 sync_reader_->UpdatePendingBytes( |
317 buffers_state.total_bytes() + frames * params_.GetBytesPerFrame()); | 318 buffers_state.total_bytes() + frames * params_.GetBytesPerFrame()); |
318 return frames; | 319 return frames; |
319 } | 320 } |
320 | 321 |
321 void AudioOutputController::WaitTillDataReady() { | 322 void AudioOutputController::WaitTillDataReady() { |
322 if (!sync_reader_->DataReady()) { | 323 #if defined(OS_WIN) || defined(OS_MACOSX) |
323 // In the different place we use different mechanism to poll, get max | 324 base::Time start = base::Time::Now(); |
324 // polling delay from constants used there. | 325 // Wait for up to 1.5 seconds for DataReady(). 1.5 seconds was chosen because |
325 const base::TimeDelta kMaxPollingDelay = TimeDelta::FromMilliseconds( | 326 // it's larger than the playback time of the WaveOut buffer size using the |
326 kPollNumAttempts * kPollPauseInMilliseconds); | 327 // minimum supported sample rate: 4096 / 3000 = ~1.4 seconds. Even a client |
327 Time start_time = Time::Now(); | 328 // expecting real time playout should be able to fill in this time. |
328 do { | 329 const base::TimeDelta max_wait = base::TimeDelta::FromMilliseconds(1500); |
329 base::PlatformThread::Sleep(TimeDelta::FromMilliseconds(1)); | 330 while (!sync_reader_->DataReady() && |
330 } while (!sync_reader_->DataReady() && | 331 ((base::Time::Now() - start) < max_wait)) { |
331 Time::Now() - start_time < kMaxPollingDelay); | 332 base::PlatformThread::YieldCurrentThread(); |
332 } | 333 } |
| 334 #else |
| 335 // WaitTillDataReady() is deprecated and should not be used. |
| 336 CHECK(false); |
| 337 #endif |
333 } | 338 } |
334 | 339 |
335 void AudioOutputController::OnError(AudioOutputStream* stream, int code) { | 340 void AudioOutputController::OnError(AudioOutputStream* stream, int code) { |
336 // Handle error on the audio controller thread. | 341 // Handle error on the audio controller thread. |
337 message_loop_->PostTask(FROM_HERE, base::Bind( | 342 message_loop_->PostTask(FROM_HERE, base::Bind( |
338 &AudioOutputController::DoReportError, this, code)); | 343 &AudioOutputController::DoReportError, this, code)); |
339 } | 344 } |
340 | 345 |
341 void AudioOutputController::DoStopCloseAndClearStream(WaitableEvent* done) { | 346 void AudioOutputController::DoStopCloseAndClearStream(WaitableEvent* done) { |
342 DCHECK(message_loop_->BelongsToCurrentThread()); | 347 DCHECK(message_loop_->BelongsToCurrentThread()); |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
386 case kPausedWhenStarting: | 391 case kPausedWhenStarting: |
387 case kPaused: | 392 case kPaused: |
388 // From the outside these three states are equivalent. | 393 // From the outside these three states are equivalent. |
389 return; | 394 return; |
390 default: | 395 default: |
391 NOTREACHED() << "Invalid original state."; | 396 NOTREACHED() << "Invalid original state."; |
392 } | 397 } |
393 } | 398 } |
394 | 399 |
395 } // namespace media | 400 } // namespace media |
OLD | NEW |