| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 <cmath> | 5 #include <cmath> |
| 6 | 6 |
| 7 #include "media/base/filter_host.h" | 7 #include "media/base/filter_host.h" |
| 8 #include "media/filters/null_audio_renderer.h" | 8 #include "media/filters/null_audio_renderer.h" |
| 9 | 9 |
| 10 namespace media { | 10 namespace media { |
| 11 | 11 |
| 12 // How "long" our buffer should be in terms of milliseconds. In OnInitialize | 12 // How "long" our buffer should be in terms of milliseconds. In OnInitialize |
| 13 // we calculate the size of one second of audio data and use this number to | 13 // we calculate the size of one second of audio data and use this number to |
| 14 // allocate a buffer to pass to FillBuffer. | 14 // allocate a buffer to pass to FillBuffer. |
| 15 static const size_t kBufferSizeInMilliseconds = 100; | 15 static const size_t kBufferSizeInMilliseconds = 100; |
| 16 | 16 |
| 17 NullAudioRenderer::NullAudioRenderer() | 17 NullAudioRenderer::NullAudioRenderer() |
| 18 : AudioRendererBase(kDefaultMaxQueueSize), | 18 : AudioRendererBase(), |
| 19 playback_rate_(0.0f), | |
| 20 bytes_per_millisecond_(0), | 19 bytes_per_millisecond_(0), |
| 21 buffer_size_(0), | 20 buffer_size_(0), |
| 22 thread_(NULL), | 21 thread_(NULL), |
| 23 shutdown_(false) { | 22 shutdown_(false) { |
| 24 } | 23 } |
| 25 | 24 |
| 26 NullAudioRenderer::~NullAudioRenderer() { | 25 NullAudioRenderer::~NullAudioRenderer() { |
| 27 Stop(); | 26 Stop(); |
| 28 } | 27 } |
| 29 | 28 |
| 30 // static | 29 // static |
| 31 bool NullAudioRenderer::IsMediaFormatSupported( | 30 bool NullAudioRenderer::IsMediaFormatSupported( |
| 32 const MediaFormat& media_format) { | 31 const MediaFormat& media_format) { |
| 33 int channels; | 32 int channels; |
| 34 int sample_rate; | 33 int sample_rate; |
| 35 int sample_bits; | 34 int sample_bits; |
| 36 return ParseMediaFormat(media_format, &channels, &sample_rate, &sample_bits); | 35 return ParseMediaFormat(media_format, &channels, &sample_rate, &sample_bits); |
| 37 } | 36 } |
| 38 | 37 |
| 39 void NullAudioRenderer::SetPlaybackRate(float playback_rate) { | |
| 40 playback_rate_ = playback_rate; | |
| 41 } | |
| 42 | |
| 43 void NullAudioRenderer::SetVolume(float volume) { | 38 void NullAudioRenderer::SetVolume(float volume) { |
| 44 // Do nothing. | 39 // Do nothing. |
| 45 } | 40 } |
| 46 | 41 |
| 47 void NullAudioRenderer::ThreadMain() { | 42 void NullAudioRenderer::ThreadMain() { |
| 48 // Loop until we're signaled to stop. | 43 // Loop until we're signaled to stop. |
| 49 while (!shutdown_) { | 44 while (!shutdown_) { |
| 50 float sleep_in_milliseconds = 0.0f; | 45 float sleep_in_milliseconds = 0.0f; |
| 51 | 46 |
| 52 // Only consume buffers when actually playing. | 47 // Only consume buffers when actually playing. |
| 53 if (playback_rate_ > 0.0f) { | 48 if (GetPlaybackRate() > 0.0f) { |
| 54 size_t bytes = FillBuffer(buffer_.get(), | 49 size_t bytes = FillBuffer(buffer_.get(), |
| 55 buffer_size_, | 50 buffer_size_, |
| 56 playback_rate_, | |
| 57 base::TimeDelta()); | 51 base::TimeDelta()); |
| 58 | 52 |
| 59 // Calculate our sleep duration, taking playback rate into consideration. | 53 // Calculate our sleep duration, taking playback rate into consideration. |
| 60 sleep_in_milliseconds = | 54 sleep_in_milliseconds = |
| 61 floor(bytes / static_cast<float>(bytes_per_millisecond_)); | 55 floor(bytes / static_cast<float>(bytes_per_millisecond_)); |
| 62 sleep_in_milliseconds /= playback_rate_; | 56 sleep_in_milliseconds /= GetPlaybackRate(); |
| 63 } else { | 57 } else { |
| 64 // If paused, sleep for 10 milliseconds before polling again. | 58 // If paused, sleep for 10 milliseconds before polling again. |
| 65 sleep_in_milliseconds = 10.0f; | 59 sleep_in_milliseconds = 10.0f; |
| 66 } | 60 } |
| 67 | 61 |
| 68 // Sleep for at least one millisecond so we don't spin the CPU. | 62 // Sleep for at least one millisecond so we don't spin the CPU. |
| 69 PlatformThread::Sleep(std::max(1, static_cast<int>(sleep_in_milliseconds))); | 63 PlatformThread::Sleep(std::max(1, static_cast<int>(sleep_in_milliseconds))); |
| 70 } | 64 } |
| 71 } | 65 } |
| 72 | 66 |
| (...skipping 20 matching lines...) Expand all Loading... |
| 93 | 87 |
| 94 void NullAudioRenderer::OnStop() { | 88 void NullAudioRenderer::OnStop() { |
| 95 shutdown_ = true; | 89 shutdown_ = true; |
| 96 if (thread_) { | 90 if (thread_) { |
| 97 PlatformThread::Join(thread_); | 91 PlatformThread::Join(thread_); |
| 98 thread_ = NULL; | 92 thread_ = NULL; |
| 99 } | 93 } |
| 100 } | 94 } |
| 101 | 95 |
| 102 } // namespace media | 96 } // namespace media |
| OLD | NEW |