Chromium Code Reviews| 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 // 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/atomicops.h" | |
| 22 #include "base/gtest_prod_util.h" | 23 #include "base/gtest_prod_util.h" |
| 23 #include "base/memory/scoped_ptr.h" | 24 #include "base/memory/scoped_ptr.h" |
| 25 #include "base/message_loop_proxy.h" | |
| 24 #include "base/synchronization/lock.h" | 26 #include "base/synchronization/lock.h" |
| 27 #include "base/time.h" | |
| 25 #include "content/renderer/media/audio_device.h" | 28 #include "content/renderer/media/audio_device.h" |
| 26 #include "media/audio/audio_io.h" | 29 #include "media/audio/audio_io.h" |
| 27 #include "media/audio/audio_parameters.h" | 30 #include "media/audio/audio_parameters.h" |
| 28 #include "media/base/audio_renderer_sink.h" | 31 #include "media/base/audio_renderer_sink.h" |
| 29 #include "media/filters/audio_renderer_base.h" | 32 #include "media/filters/audio_renderer_base.h" |
| 30 | 33 |
| 31 class AudioMessageFilter; | 34 class AudioMessageFilter; |
| 32 | 35 |
| 33 class CONTENT_EXPORT AudioRendererImpl | 36 class CONTENT_EXPORT AudioRendererImpl |
| 34 : public media::AudioRendererBase, | 37 : public media::AudioRendererBase, |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 70 // Helper methods. | 73 // Helper methods. |
| 71 // Convert number of bytes to duration of time using information about the | 74 // Convert number of bytes to duration of time using information about the |
| 72 // number of channels, sample rate and sample bits. | 75 // number of channels, sample rate and sample bits. |
| 73 base::TimeDelta ConvertToDuration(int bytes); | 76 base::TimeDelta ConvertToDuration(int bytes); |
| 74 | 77 |
| 75 // Methods called on pipeline thread ---------------------------------------- | 78 // Methods called on pipeline thread ---------------------------------------- |
| 76 void DoPlay(); | 79 void DoPlay(); |
| 77 void DoPause(); | 80 void DoPause(); |
| 78 void DoSeek(); | 81 void DoSeek(); |
| 79 | 82 |
| 83 // Methods called on IO thread ---------------------------------------------- | |
| 84 void DoSignalEndOfStream(base::subtle::Atomic32 stream_id); | |
| 85 | |
| 80 // media::AudioRendererSink::RenderCallback implementation. | 86 // media::AudioRendererSink::RenderCallback implementation. |
| 81 virtual size_t Render(const std::vector<float*>& audio_data, | 87 virtual size_t Render(const std::vector<float*>& audio_data, |
| 82 size_t number_of_frames, | 88 size_t number_of_frames, |
| 83 size_t audio_delay_milliseconds) OVERRIDE; | 89 size_t audio_delay_milliseconds) OVERRIDE; |
| 84 virtual void OnError() OVERRIDE; | 90 virtual void OnError() OVERRIDE; |
| 85 | 91 |
| 92 // Returns delay in ms before call to OnRenderEndOfStream(). | |
| 93 // Made virtual so test can override. | |
| 94 virtual int64 OnRenderEndOfStreamDelay(); | |
| 95 | |
| 86 // Accessors used by tests. | 96 // Accessors used by tests. |
| 87 base::Time earliest_end_time() const { | 97 base::Time earliest_end_time() { |
| 98 base::AutoLock auto_lock(earliest_end_time_lock_); | |
| 88 return earliest_end_time_; | 99 return earliest_end_time_; |
| 89 } | 100 } |
| 90 | 101 |
| 91 void set_earliest_end_time(const base::Time& earliest_end_time) { | 102 void set_earliest_end_time(const base::Time& earliest_end_time) { |
| 103 base::AutoLock auto_lock(earliest_end_time_lock_); | |
| 92 earliest_end_time_ = earliest_end_time; | 104 earliest_end_time_ = earliest_end_time; |
| 93 } | 105 } |
| 94 | 106 |
| 95 uint32 bytes_per_second() const { | 107 uint32 bytes_per_second() const { |
| 96 return bytes_per_second_; | 108 return bytes_per_second_; |
| 97 } | 109 } |
| 98 | 110 |
| 99 // Estimate earliest time when current buffer can stop playing. | 111 // Estimate earliest time when current buffer can stop playing. |
| 100 void UpdateEarliestEndTime(int bytes_filled, | 112 void UpdateEarliestEndTime(int bytes_filled, |
| 101 base::TimeDelta request_delay, | 113 base::TimeDelta request_delay, |
| 102 base::Time time_now); | 114 base::Time time_now); |
| 103 | 115 |
| 104 // Used to calculate audio delay given bytes. | 116 // Used to calculate audio delay given bytes. |
| 105 uint32 bytes_per_second_; | 117 uint32 bytes_per_second_; |
| 106 | 118 |
| 107 // A flag that indicates this filter is called to stop. | 119 // A flag that indicates this filter is called to stop. |
| 108 bool stopped_; | 120 bool stopped_; |
| 109 | 121 |
| 110 // The sink (destination) for rendered audio. | 122 // The sink (destination) for rendered audio. |
| 111 scoped_refptr<media::AudioRendererSink> sink_; | 123 scoped_refptr<media::AudioRendererSink> sink_; |
| 112 | 124 |
| 113 // Set to true when OnInitialize() is called. | 125 // Set to true when OnInitialize() is called. |
| 114 bool is_initialized_; | 126 bool is_initialized_; |
| 115 | 127 |
| 128 // Set to true when we post delayed task to signal 'ended' event. | |
| 129 // Made Atomic, not bool, because it can be accessed simultaneously | |
| 130 // by pipeline and audio threads. | |
| 131 base::subtle::Atomic32 ended_event_scheduled_; | |
| 132 | |
| 116 // We're supposed to know amount of audio data OS or hardware buffered, but | 133 // We're supposed to know amount of audio data OS or hardware buffered, but |
| 117 // that is not always so -- on my Linux box | 134 // that is not always so -- on my Linux box |
| 118 // AudioBuffersState::hardware_delay_bytes never reaches 0. | 135 // AudioBuffersState::hardware_delay_bytes never reaches 0. |
| 119 // | 136 // |
| 120 // As a result we cannot use it to find when stream ends. If we just ignore | 137 // As a result we cannot use it to find when stream ends. If we just ignore |
| 121 // buffered data we will notify host that stream ended before it is actually | 138 // buffered data we will notify host that stream ended before it is actually |
| 122 // did so, I've seen it done ~140ms too early when playing ~150ms file. | 139 // did so, I've seen it done ~140ms too early when playing ~150ms file. |
| 123 // | 140 // |
| 124 // Instead of trying to invent OS-specific solution for each and every OS we | 141 // Instead of trying to invent OS-specific solution for each and every OS we |
| 125 // are supporting, use simple workaround: every time we fill the buffer we | 142 // are supporting, use simple workaround: every time we fill the buffer we |
| 126 // remember when it should stop playing, and do not assume that buffer is | 143 // remember when it should stop playing, and do not assume that buffer is |
| 127 // empty till that time. Workaround is not bulletproof, as we don't exactly | 144 // empty till that time. Workaround is not bulletproof, as we don't exactly |
| 128 // know when that particular data would start playing, but it is much better | 145 // know when that particular data would start playing, but it is much better |
| 129 // than nothing. | 146 // than nothing. |
| 147 // Access protected by lock as it can be accessed by pipeline and audio | |
| 148 // threads, and there is no Atomic64 on 32-bit systems. | |
| 130 base::Time earliest_end_time_; | 149 base::Time earliest_end_time_; |
| 150 base::Lock earliest_end_time_lock_; | |
| 131 | 151 |
| 132 AudioParameters audio_parameters_; | 152 AudioParameters audio_parameters_; |
| 133 | 153 |
| 154 // Use message loop proxy, not message loop itself, to avoid crash | |
| 155 // because of message loop that ended while we are still playing. | |
| 156 // We don't need complex shutdown operations, just not posting tasks | |
| 157 // is enough, so we can use message loop proxy. | |
| 158 scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy_; | |
| 159 | |
| 160 // Stream id -- necessary because there is no way to cancel scheduled task. | |
| 161 // We want be sure we are still playing the same stream when delayed task is | |
| 162 // called. If seek or pause happened after scheduling but before task was | |
| 163 // called, there would be mismatch between expected and actual ids and delayed | |
| 164 // task would not do anything. | |
| 165 base::subtle::Atomic32 stream_id_; | |
|
Ami GONE FROM CHROMIUM
2012/02/23 20:18:25
I might be missing something, but ISTM this member
enal1
2012/02/23 20:28:04
++stream_id_ happens only on the pipeline thread,
| |
| 166 | |
| 134 DISALLOW_COPY_AND_ASSIGN(AudioRendererImpl); | 167 DISALLOW_COPY_AND_ASSIGN(AudioRendererImpl); |
| 135 }; | 168 }; |
| 136 | 169 |
| 137 #endif // CONTENT_RENDERER_MEDIA_AUDIO_RENDERER_IMPL_H_ | 170 #endif // CONTENT_RENDERER_MEDIA_AUDIO_RENDERER_IMPL_H_ |
| OLD | NEW |