Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 // Audio rendering unit utilizing AudioDevice. | 5 // Audio rendering unit utilizing AudioDevice. |
| 6 // | 6 // |
| 7 // This class lives inside three threads during it's lifetime, namely: | 7 // This class lives inside three threads during it's lifetime, namely: |
| 8 // 1. Render thread. | 8 // 1. Render thread. |
| 9 // This object is created on the render thread. | 9 // This object is created on the render thread. |
| 10 // 2. Pipeline thread | 10 // 2. Pipeline thread |
| 11 // OnInitialize() is called here with the audio format. | 11 // OnInitialize() is called here with the audio format. |
| 12 // Play/Pause/Seek also happens here. | 12 // Play/Pause/Seek also happens here. |
| 13 // 3. Audio thread created by the AudioDevice. | 13 // 3. Audio thread created by the AudioDevice. |
| 14 // Render() is called here where audio data is decoded into raw PCM data. | 14 // Render() is called here where audio data is decoded into raw PCM data. |
| 15 | 15 |
| 16 #ifndef CONTENT_RENDERER_MEDIA_AUDIO_RENDERER_IMPL_H_ | 16 #ifndef CONTENT_RENDERER_MEDIA_AUDIO_RENDERER_IMPL_H_ |
| 17 #define CONTENT_RENDERER_MEDIA_AUDIO_RENDERER_IMPL_H_ | 17 #define CONTENT_RENDERER_MEDIA_AUDIO_RENDERER_IMPL_H_ |
| 18 #pragma once | 18 #pragma once |
| 19 | 19 |
| 20 #include <vector> | 20 #include <vector> |
| 21 | 21 |
| 22 #include "base/gtest_prod_util.h" | 22 #include "base/gtest_prod_util.h" |
| 23 #include "base/memory/scoped_ptr.h" | 23 #include "base/memory/scoped_ptr.h" |
| 24 #include "base/synchronization/lock.h" | 24 #include "base/synchronization/lock.h" |
| 25 #include "content/renderer/media/audio_device.h" | 25 #include "content/renderer/media/audio_device.h" |
| 26 #include "media/audio/audio_io.h" | 26 #include "media/audio/audio_io.h" |
| 27 #include "media/audio/audio_parameters.h" | 27 #include "media/audio/audio_parameters.h" |
| 28 #include "media/filters/audio_renderer_base.h" | 28 #include "media/filters/audio_renderer_base.h" |
| 29 #include "media/filters/audio_renderer_sink.h" | |
| 29 | 30 |
| 30 class AudioMessageFilter; | 31 class AudioMessageFilter; |
| 31 | 32 |
| 32 class CONTENT_EXPORT AudioRendererImpl | 33 class CONTENT_EXPORT AudioRendererImpl |
| 33 : public media::AudioRendererBase, | 34 : public media::AudioRendererBase, |
| 34 public AudioDevice::RenderCallback { | 35 public AudioDevice::RenderCallback { |
| 35 public: | 36 public: |
| 36 // Methods called on Render thread ------------------------------------------ | 37 // Methods called on Render thread ------------------------------------------ |
| 37 AudioRendererImpl(); | 38 AudioRendererImpl(); |
| 38 virtual ~AudioRendererImpl(); | 39 virtual ~AudioRendererImpl(); |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 67 // Helper methods. | 68 // Helper methods. |
| 68 // Convert number of bytes to duration of time using information about the | 69 // Convert number of bytes to duration of time using information about the |
| 69 // number of channels, sample rate and sample bits. | 70 // number of channels, sample rate and sample bits. |
| 70 base::TimeDelta ConvertToDuration(int bytes); | 71 base::TimeDelta ConvertToDuration(int bytes); |
| 71 | 72 |
| 72 // Methods called on pipeline thread ---------------------------------------- | 73 // Methods called on pipeline thread ---------------------------------------- |
| 73 void DoPlay(); | 74 void DoPlay(); |
| 74 void DoPause(); | 75 void DoPause(); |
| 75 void DoSeek(); | 76 void DoSeek(); |
| 76 | 77 |
| 77 // AudioDevice::RenderCallback implementation. | 78 // media::AudioRendererSink::RenderCallback implementation. |
| 78 virtual void Render(const std::vector<float*>& audio_data, | 79 virtual void Render(const std::vector<float*>& audio_data, |
| 79 size_t number_of_frames, | 80 size_t number_of_frames, |
| 80 size_t audio_delay_milliseconds) OVERRIDE; | 81 size_t audio_delay_milliseconds) OVERRIDE; |
| 81 | 82 |
| 83 // Detach from normal playback with AudioDevice and use the given sink. | |
| 84 // Passing in NULL means to use the default sink which is the AudioDevice. | |
|
tommi (sloooow) - chröme
2011/12/19 15:26:24
should this documentation perhaps rather be in the
Chris Rogers
2011/12/19 21:40:35
I think you're right given that it's part of the i
tommi (sloooow) - chröme
2011/12/20 15:05:46
That sounds reasonable to me.
| |
| 85 virtual void SetAudioRendererSink(media::AudioRendererSink* sink) OVERRIDE; | |
| 86 | |
| 82 // Accessors used by tests. | 87 // Accessors used by tests. |
| 83 base::Time earliest_end_time() const { | 88 base::Time earliest_end_time() const { |
| 84 return earliest_end_time_; | 89 return earliest_end_time_; |
| 85 } | 90 } |
| 86 | 91 |
| 87 void set_earliest_end_time(const base::Time& earliest_end_time) { | 92 void set_earliest_end_time(const base::Time& earliest_end_time) { |
| 88 earliest_end_time_ = earliest_end_time; | 93 earliest_end_time_ = earliest_end_time; |
| 89 } | 94 } |
| 90 | 95 |
| 91 uint32 bytes_per_second() const { | 96 uint32 bytes_per_second() const { |
| 92 return bytes_per_second_; | 97 return bytes_per_second_; |
| 93 } | 98 } |
| 94 | 99 |
| 95 // Estimate earliest time when current buffer can stop playing. | 100 // Estimate earliest time when current buffer can stop playing. |
| 96 void UpdateEarliestEndTime(int bytes_filled, | 101 void UpdateEarliestEndTime(int bytes_filled, |
| 97 base::TimeDelta request_delay, | 102 base::TimeDelta request_delay, |
| 98 base::Time time_now); | 103 base::Time time_now); |
| 99 | 104 |
| 100 // Used to calculate audio delay given bytes. | 105 // Used to calculate audio delay given bytes. |
| 101 uint32 bytes_per_second_; | 106 uint32 bytes_per_second_; |
| 102 | 107 |
| 103 // A flag that indicates this filter is called to stop. | 108 // A flag that indicates this filter is called to stop. |
| 104 bool stopped_; | 109 bool stopped_; |
| 105 | 110 |
| 106 // audio_device_ is the sink (destination) for rendered audio. | 111 // sink_ is the sink (destination) for rendered audio. |
| 112 media::AudioRendererSink* sink_; | |
| 113 | |
| 114 // Protects access to sink_ | |
| 115 base::Lock sink_lock_; | |
| 116 | |
| 117 // audio_device_ is the default sink. | |
| 107 scoped_refptr<AudioDevice> audio_device_; | 118 scoped_refptr<AudioDevice> audio_device_; |
| 108 | 119 |
| 120 // Set to true when OnInitialize() is called. | |
| 121 bool is_initialized_; | |
| 122 | |
| 109 // We're supposed to know amount of audio data OS or hardware buffered, but | 123 // We're supposed to know amount of audio data OS or hardware buffered, but |
| 110 // that is not always so -- on my Linux box | 124 // that is not always so -- on my Linux box |
| 111 // AudioBuffersState::hardware_delay_bytes never reaches 0. | 125 // AudioBuffersState::hardware_delay_bytes never reaches 0. |
| 112 // | 126 // |
| 113 // As a result we cannot use it to find when stream ends. If we just ignore | 127 // As a result we cannot use it to find when stream ends. If we just ignore |
| 114 // buffered data we will notify host that stream ended before it is actually | 128 // buffered data we will notify host that stream ended before it is actually |
| 115 // did so, I've seen it done ~140ms too early when playing ~150ms file. | 129 // did so, I've seen it done ~140ms too early when playing ~150ms file. |
| 116 // | 130 // |
| 117 // Instead of trying to invent OS-specific solution for each and every OS we | 131 // Instead of trying to invent OS-specific solution for each and every OS we |
| 118 // are supporting, use simple workaround: every time we fill the buffer we | 132 // are supporting, use simple workaround: every time we fill the buffer we |
| 119 // remember when it should stop playing, and do not assume that buffer is | 133 // remember when it should stop playing, and do not assume that buffer is |
| 120 // empty till that time. Workaround is not bulletproof, as we don't exactly | 134 // empty till that time. Workaround is not bulletproof, as we don't exactly |
| 121 // know when that particular data would start playing, but it is much better | 135 // know when that particular data would start playing, but it is much better |
| 122 // than nothing. | 136 // than nothing. |
| 123 base::Time earliest_end_time_; | 137 base::Time earliest_end_time_; |
| 124 | 138 |
| 125 AudioParameters audio_parameters_; | 139 AudioParameters audio_parameters_; |
| 126 | 140 |
| 127 DISALLOW_COPY_AND_ASSIGN(AudioRendererImpl); | 141 DISALLOW_COPY_AND_ASSIGN(AudioRendererImpl); |
| 128 }; | 142 }; |
| 129 | 143 |
| 130 #endif // CONTENT_RENDERER_MEDIA_AUDIO_RENDERER_IMPL_H_ | 144 #endif // CONTENT_RENDERER_MEDIA_AUDIO_RENDERER_IMPL_H_ |
| OLD | NEW |