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 audio output stream provided by browser | 5 // Audio rendering unit utilizing AudioDevice. |
6 // process through IPC. | |
7 // | |
8 // Relationship of classes. | |
9 // | |
10 // AudioRendererHost AudioRendererImpl | |
11 // ^ ^ | |
12 // | | | |
13 // v IPC v | |
14 // RenderMessageFilter <---------> AudioMessageFilter | |
15 // | |
16 // Implementation of interface with audio device is in AudioRendererHost and | |
17 // it provides services and entry points in RenderMessageFilter, allowing | |
18 // usage of IPC calls to interact with audio device. AudioMessageFilter acts | |
19 // as a portal for IPC calls and does no more than delegation. | |
20 // | |
21 // Transportation of audio buffer is done by using shared memory, after | |
22 // OnCreateStream is executed, OnCreated would be called along with a | |
23 // SharedMemoryHandle upon successful creation of audio output stream in the | |
24 // browser process. The same piece of shared memory would be used during the | |
25 // lifetime of this unit. | |
26 // | 6 // |
27 // This class lives inside three threads during it's lifetime, namely: | 7 // This class lives inside three threads during it's lifetime, namely: |
28 // 1. IO thread. | 8 // 1. Render thread. |
29 // The thread within which this class receives all the IPC messages and | 9 // This object is created on the render thread. |
30 // IPC communications can only happen in this thread. | |
31 // 2. Pipeline thread | 10 // 2. Pipeline thread |
32 // Initialization of filter and proper stopping of filters happens here. | 11 // OnInitialize() is called here with the audio format. |
33 // Properties of this filter is also set in this thread. | 12 // Play/Pause/Seek also happens here. |
34 // 3. Audio decoder thread (If there's one.) | 13 // 3. Audio thread created by the AudioDevice. |
35 // Responsible for decoding audio data and gives raw PCM data to this object. | 14 // Render() is called here where audio data is decoded into raw PCM data. |
36 | 15 |
37 #ifndef CONTENT_RENDERER_MEDIA_AUDIO_RENDERER_IMPL_H_ | 16 #ifndef CONTENT_RENDERER_MEDIA_AUDIO_RENDERER_IMPL_H_ |
38 #define CONTENT_RENDERER_MEDIA_AUDIO_RENDERER_IMPL_H_ | 17 #define CONTENT_RENDERER_MEDIA_AUDIO_RENDERER_IMPL_H_ |
39 #pragma once | 18 #pragma once |
40 | 19 |
| 20 #include <vector> |
| 21 |
41 #include "base/gtest_prod_util.h" | 22 #include "base/gtest_prod_util.h" |
42 #include "base/memory/scoped_ptr.h" | 23 #include "base/memory/scoped_ptr.h" |
43 #include "base/message_loop.h" | |
44 #include "base/shared_memory.h" | |
45 #include "base/synchronization/lock.h" | 24 #include "base/synchronization/lock.h" |
46 #include "base/threading/simple_thread.h" | 25 #include "content/renderer/media/audio_device.h" |
47 #include "content/common/content_export.h" | |
48 #include "content/renderer/media/audio_message_filter.h" | |
49 #include "media/audio/audio_io.h" | 26 #include "media/audio/audio_io.h" |
50 #include "media/audio/audio_manager.h" | 27 #include "media/audio/audio_parameters.h" |
51 #include "media/base/filters.h" | |
52 #include "media/filters/audio_renderer_base.h" | 28 #include "media/filters/audio_renderer_base.h" |
53 | 29 |
54 class AudioMessageFilter; | 30 class AudioMessageFilter; |
55 | 31 |
56 class CONTENT_EXPORT AudioRendererImpl | 32 class CONTENT_EXPORT AudioRendererImpl |
57 : public media::AudioRendererBase, | 33 : public media::AudioRendererBase, |
58 public AudioMessageFilter::Delegate, | 34 public AudioDevice::RenderCallback { |
59 public base::DelegateSimpleThread::Delegate, | |
60 public MessageLoop::DestructionObserver { | |
61 public: | 35 public: |
62 // Methods called on Render thread ------------------------------------------ | 36 // Methods called on Render thread ------------------------------------------ |
63 AudioRendererImpl(); | 37 AudioRendererImpl(); |
64 virtual ~AudioRendererImpl(); | 38 virtual ~AudioRendererImpl(); |
65 | 39 |
66 // Methods called on IO thread ---------------------------------------------- | |
67 // AudioMessageFilter::Delegate methods, called by AudioMessageFilter. | |
68 virtual void OnRequestPacket(AudioBuffersState buffers_state) OVERRIDE; | |
69 virtual void OnStateChanged(AudioStreamState state) OVERRIDE; | |
70 virtual void OnCreated(base::SharedMemoryHandle handle, | |
71 uint32 length) OVERRIDE; | |
72 virtual void OnLowLatencyCreated(base::SharedMemoryHandle handle, | |
73 base::SyncSocket::Handle socket_handle, | |
74 uint32 length) OVERRIDE; | |
75 virtual void OnVolume(double volume) OVERRIDE; | |
76 | |
77 // Methods called on pipeline thread ---------------------------------------- | 40 // Methods called on pipeline thread ---------------------------------------- |
78 // media::Filter implementation. | 41 // media::Filter implementation. |
79 virtual void SetPlaybackRate(float rate) OVERRIDE; | 42 virtual void SetPlaybackRate(float rate) OVERRIDE; |
80 virtual void Pause(const base::Closure& callback) OVERRIDE; | 43 virtual void Pause(const base::Closure& callback) OVERRIDE; |
81 virtual void Seek(base::TimeDelta time, | 44 virtual void Seek(base::TimeDelta time, |
82 const media::FilterStatusCB& cb) OVERRIDE; | 45 const media::FilterStatusCB& cb) OVERRIDE; |
83 virtual void Play(const base::Closure& callback) OVERRIDE; | 46 virtual void Play(const base::Closure& callback) OVERRIDE; |
84 | 47 |
85 // media::AudioRenderer implementation. | 48 // media::AudioRenderer implementation. |
86 virtual void SetVolume(float volume) OVERRIDE; | 49 virtual void SetVolume(float volume) OVERRIDE; |
87 | 50 |
88 protected: | 51 protected: |
89 // Methods called on audio renderer thread ---------------------------------- | 52 // Methods called on pipeline thread ---------------------------------------- |
90 // These methods are called from AudioRendererBase. | 53 // These methods are called from AudioRendererBase. |
91 virtual bool OnInitialize(int bits_per_channel, | 54 virtual bool OnInitialize(int bits_per_channel, |
92 ChannelLayout channel_layout, | 55 ChannelLayout channel_layout, |
93 int sample_rate) OVERRIDE; | 56 int sample_rate) OVERRIDE; |
94 virtual void OnStop() OVERRIDE; | 57 virtual void OnStop() OVERRIDE; |
95 | 58 |
96 // Called when the decoder completes a Read(). | |
97 virtual void ConsumeAudioSamples( | |
98 scoped_refptr<media::Buffer> buffer_in) OVERRIDE; | |
99 | |
100 private: | 59 private: |
101 // We are using either low- or high-latency code path. | |
102 enum LatencyType { | |
103 kUninitializedLatency = 0, | |
104 kLowLatency, | |
105 kHighLatency | |
106 }; | |
107 static LatencyType latency_type_; | |
108 | |
109 // For access to constructor and IO thread methods. | 60 // For access to constructor and IO thread methods. |
110 friend class AudioRendererImplTest; | 61 friend class AudioRendererImplTest; |
111 friend class DelegateCaller; | 62 friend class DelegateCaller; |
112 FRIEND_TEST_ALL_PREFIXES(AudioRendererImplTest, Stop); | 63 FRIEND_TEST_ALL_PREFIXES(AudioRendererImplTest, Stop); |
113 FRIEND_TEST_ALL_PREFIXES(AudioRendererImplTest, | 64 FRIEND_TEST_ALL_PREFIXES(AudioRendererImplTest, |
114 DestroyedMessageLoop_ConsumeAudioSamples); | 65 DestroyedMessageLoop_ConsumeAudioSamples); |
115 FRIEND_TEST_ALL_PREFIXES(AudioRendererImplTest, UpdateEarliestEndTime); | 66 FRIEND_TEST_ALL_PREFIXES(AudioRendererImplTest, UpdateEarliestEndTime); |
116 // Helper methods. | 67 // Helper methods. |
117 // 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 |
118 // number of channels, sample rate and sample bits. | 69 // number of channels, sample rate and sample bits. |
119 base::TimeDelta ConvertToDuration(int bytes); | 70 base::TimeDelta ConvertToDuration(int bytes); |
120 | 71 |
121 // Methods call on IO thread ------------------------------------------------ | 72 // Methods called on pipeline thread ---------------------------------------- |
122 // The following methods are tasks posted on the IO thread that needs to | 73 void DoPlay(); |
123 // be executed on that thread. They interact with AudioMessageFilter and | 74 void DoPause(); |
124 // sends IPC messages on that thread. | 75 void DoSeek(); |
125 void CreateStreamTask(const AudioParameters& params); | |
126 void PlayTask(); | |
127 void PauseTask(); | |
128 void SeekTask(); | |
129 void SetVolumeTask(double volume); | |
130 void NotifyPacketReadyTask(); | |
131 void DestroyTask(); | |
132 | 76 |
133 // Called on IO thread when message loop is dying. | 77 // AudioDevice::RenderCallback implementation. |
134 virtual void WillDestroyCurrentMessageLoop() OVERRIDE; | 78 virtual void Render(const std::vector<float*>& audio_data, |
135 | 79 size_t number_of_frames, |
136 // DelegateSimpleThread::Delegate implementation. | 80 size_t audio_delay_milliseconds) OVERRIDE; |
137 virtual void Run() OVERRIDE; | |
138 | |
139 // (Re-)starts playback. | |
140 void NotifyDataAvailableIfNecessary(); | |
141 | |
142 // Creates socket. Virtual so tests can override. | |
143 virtual void CreateSocket(base::SyncSocket::Handle socket_handle); | |
144 | |
145 // Launching audio thread. Virtual so tests can override. | |
146 virtual void CreateAudioThread(); | |
147 | 81 |
148 // Accessors used by tests. | 82 // Accessors used by tests. |
149 static LatencyType latency_type() { | |
150 return latency_type_; | |
151 } | |
152 | |
153 base::Time earliest_end_time() const { | 83 base::Time earliest_end_time() const { |
154 return earliest_end_time_; | 84 return earliest_end_time_; |
155 } | 85 } |
156 | 86 |
157 void set_earliest_end_time(const base::Time& earliest_end_time) { | 87 void set_earliest_end_time(const base::Time& earliest_end_time) { |
158 earliest_end_time_ = earliest_end_time; | 88 earliest_end_time_ = earliest_end_time; |
159 } | 89 } |
160 | 90 |
161 uint32 bytes_per_second() const { | 91 uint32 bytes_per_second() const { |
162 return bytes_per_second_; | 92 return bytes_per_second_; |
163 } | 93 } |
164 | 94 |
165 // Should be called before any class instance is created. | |
166 static void set_latency_type(LatencyType latency_type); | |
167 | |
168 // Helper method for IPC send calls. | |
169 void Send(IPC::Message* message); | |
170 | |
171 // Estimate earliest time when current buffer can stop playing. | 95 // Estimate earliest time when current buffer can stop playing. |
172 void UpdateEarliestEndTime(int bytes_filled, | 96 void UpdateEarliestEndTime(int bytes_filled, |
173 base::TimeDelta request_delay, | 97 base::TimeDelta request_delay, |
174 base::Time time_now); | 98 base::Time time_now); |
175 | 99 |
176 // Used to calculate audio delay given bytes. | 100 // Used to calculate audio delay given bytes. |
177 uint32 bytes_per_second_; | 101 uint32 bytes_per_second_; |
178 | 102 |
179 // Whether the stream has been created yet. | |
180 bool stream_created_; | |
181 | |
182 // ID of the stream created in the browser process. | |
183 int32 stream_id_; | |
184 | |
185 // Memory shared by the browser process for audio buffer. | |
186 scoped_ptr<base::SharedMemory> shared_memory_; | |
187 uint32 shared_memory_size_; | |
188 | |
189 // Cached audio message filter (lives on the main render thread). | |
190 scoped_refptr<AudioMessageFilter> filter_; | |
191 | |
192 // Low latency IPC stuff. | |
193 scoped_ptr<base::SyncSocket> socket_; | |
194 | |
195 // That thread waits for audio input. | |
196 scoped_ptr<base::DelegateSimpleThread> audio_thread_; | |
197 | |
198 // Protects: | |
199 // - |stopped_| | |
200 // - |pending_request_| | |
201 // - |request_buffers_state_| | |
202 base::Lock lock_; | |
203 | |
204 // A flag that indicates this filter is called to stop. | 103 // A flag that indicates this filter is called to stop. |
205 bool stopped_; | 104 bool stopped_; |
206 | 105 |
207 // A flag that indicates an outstanding packet request. | 106 // audio_device_ is the sink (destination) for rendered audio. |
208 bool pending_request_; | 107 scoped_refptr<AudioDevice> audio_device_; |
209 | |
210 // State of the audio buffers at time of the last request. | |
211 AudioBuffersState request_buffers_state_; | |
212 | 108 |
213 // We're supposed to know amount of audio data OS or hardware buffered, but | 109 // We're supposed to know amount of audio data OS or hardware buffered, but |
214 // that is not always so -- on my Linux box | 110 // that is not always so -- on my Linux box |
215 // AudioBuffersState::hardware_delay_bytes never reaches 0. | 111 // AudioBuffersState::hardware_delay_bytes never reaches 0. |
216 // | 112 // |
217 // As a result we cannot use it to find when stream ends. If we just ignore | 113 // As a result we cannot use it to find when stream ends. If we just ignore |
218 // buffered data we will notify host that stream ended before it is actually | 114 // buffered data we will notify host that stream ended before it is actually |
219 // did so, I've seen it done ~140ms too early when playing ~150ms file. | 115 // did so, I've seen it done ~140ms too early when playing ~150ms file. |
220 // | 116 // |
221 // Instead of trying to invent OS-specific solution for each and every OS we | 117 // Instead of trying to invent OS-specific solution for each and every OS we |
222 // are supporting, use simple workaround: every time we fill the buffer we | 118 // are supporting, use simple workaround: every time we fill the buffer we |
223 // remember when it should stop playing, and do not assume that buffer is | 119 // remember when it should stop playing, and do not assume that buffer is |
224 // empty till that time. Workaround is not bulletproof, as we don't exactly | 120 // empty till that time. Workaround is not bulletproof, as we don't exactly |
225 // know when that particular data would start playing, but it is much better | 121 // know when that particular data would start playing, but it is much better |
226 // than nothing. | 122 // than nothing. |
227 base::Time earliest_end_time_; | 123 base::Time earliest_end_time_; |
228 | 124 |
| 125 AudioParameters audio_parameters_; |
| 126 |
229 DISALLOW_COPY_AND_ASSIGN(AudioRendererImpl); | 127 DISALLOW_COPY_AND_ASSIGN(AudioRendererImpl); |
230 }; | 128 }; |
231 | 129 |
232 #endif // CONTENT_RENDERER_MEDIA_AUDIO_RENDERER_IMPL_H_ | 130 #endif // CONTENT_RENDERER_MEDIA_AUDIO_RENDERER_IMPL_H_ |
OLD | NEW |