| 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 |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 68 // Convert number of bytes to duration of time using information about the | 68 // Convert number of bytes to duration of time using information about the |
| 69 // number of channels, sample rate and sample bits. | 69 // number of channels, sample rate and sample bits. |
| 70 base::TimeDelta ConvertToDuration(int bytes); | 70 base::TimeDelta ConvertToDuration(int bytes); |
| 71 | 71 |
| 72 // Methods called on pipeline thread ---------------------------------------- | 72 // Methods called on pipeline thread ---------------------------------------- |
| 73 void DoPlay(); | 73 void DoPlay(); |
| 74 void DoPause(); | 74 void DoPause(); |
| 75 void DoSeek(); | 75 void DoSeek(); |
| 76 | 76 |
| 77 // AudioDevice::RenderCallback implementation. | 77 // AudioDevice::RenderCallback implementation. |
| 78 virtual void Render(const std::vector<float*>& audio_data, | 78 virtual size_t Render(const std::vector<float*>& audio_data, |
| 79 size_t number_of_frames, | 79 size_t number_of_frames, |
| 80 size_t audio_delay_milliseconds) OVERRIDE; | 80 size_t audio_delay_milliseconds) OVERRIDE; |
| 81 | 81 |
| 82 // Accessors used by tests. | 82 // Accessors used by tests. |
| 83 base::Time earliest_end_time() const { | 83 base::Time earliest_end_time() const { |
| 84 return earliest_end_time_; | 84 return earliest_end_time_; |
| 85 } | 85 } |
| 86 | 86 |
| 87 void set_earliest_end_time(const base::Time& earliest_end_time) { | 87 void set_earliest_end_time(const base::Time& earliest_end_time) { |
| 88 earliest_end_time_ = earliest_end_time; | 88 earliest_end_time_ = earliest_end_time; |
| 89 } | 89 } |
| 90 | 90 |
| 91 uint32 bytes_per_second() const { | 91 uint32 bytes_per_second() const { |
| 92 return bytes_per_second_; | 92 return bytes_per_second_; |
| 93 } | 93 } |
| 94 | 94 |
| 95 // Estimate earliest time when current buffer can stop playing. | 95 // Estimate earliest time when current buffer can stop playing. |
| 96 void UpdateEarliestEndTime(int bytes_filled, | 96 void UpdateEarliestEndTime(int bytes_filled, |
| 97 base::TimeDelta request_delay, | 97 base::TimeDelta request_delay, |
| 98 base::Time time_now); | 98 base::Time time_now); |
| 99 | 99 |
| 100 // Used to calculate audio delay given bytes. | 100 // Used to calculate audio delay given bytes. |
| 101 uint32 bytes_per_second_; | 101 uint32 bytes_per_second_; |
| 102 | 102 |
| 103 // A flag that indicates this filter is called to stop. | 103 // A flag that indicates this filter is called to stop. |
| 104 bool stopped_; | 104 bool stopped_; |
| 105 | 105 |
| 106 // A flag that indicates that pre-populating call done. |
| 107 bool prepopulated_; |
| 108 |
| 106 // audio_device_ is the sink (destination) for rendered audio. | 109 // audio_device_ is the sink (destination) for rendered audio. |
| 107 scoped_refptr<AudioDevice> audio_device_; | 110 scoped_refptr<AudioDevice> audio_device_; |
| 108 | 111 |
| 109 // We're supposed to know amount of audio data OS or hardware buffered, but | 112 // We're supposed to know amount of audio data OS or hardware buffered, but |
| 110 // that is not always so -- on my Linux box | 113 // that is not always so -- on my Linux box |
| 111 // AudioBuffersState::hardware_delay_bytes never reaches 0. | 114 // AudioBuffersState::hardware_delay_bytes never reaches 0. |
| 112 // | 115 // |
| 113 // As a result we cannot use it to find when stream ends. If we just ignore | 116 // 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 | 117 // 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. | 118 // did so, I've seen it done ~140ms too early when playing ~150ms file. |
| 116 // | 119 // |
| 117 // Instead of trying to invent OS-specific solution for each and every OS we | 120 // 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 | 121 // 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 | 122 // 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 | 123 // 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 | 124 // know when that particular data would start playing, but it is much better |
| 122 // than nothing. | 125 // than nothing. |
| 123 base::Time earliest_end_time_; | 126 base::Time earliest_end_time_; |
| 124 | 127 |
| 125 AudioParameters audio_parameters_; | 128 AudioParameters audio_parameters_; |
| 126 | 129 |
| 127 DISALLOW_COPY_AND_ASSIGN(AudioRendererImpl); | 130 DISALLOW_COPY_AND_ASSIGN(AudioRendererImpl); |
| 128 }; | 131 }; |
| 129 | 132 |
| 130 #endif // CONTENT_RENDERER_MEDIA_AUDIO_RENDERER_IMPL_H_ | 133 #endif // CONTENT_RENDERER_MEDIA_AUDIO_RENDERER_IMPL_H_ |
| OLD | NEW |