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 // Low-latency audio rendering unit utilizing audio output stream provided | 5 // Low-latency audio rendering unit utilizing audio output stream provided |
6 // by browser process through IPC. | 6 // by browser process through IPC. |
7 // | 7 // |
8 // Relationship of classes. | 8 // Relationship of classes. |
9 // | 9 // |
10 // AudioOutputController AudioDevice | 10 // AudioOutputController AudioDevice |
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
150 return &socket_; | 150 return &socket_; |
151 } | 151 } |
152 void Close() { | 152 void Close() { |
153 // Close() should be thread-safe, obtain the lock. | 153 // Close() should be thread-safe, obtain the lock. |
154 base::AutoLock auto_lock(lock_); | 154 base::AutoLock auto_lock(lock_); |
155 socket_.Close(); | 155 socket_.Close(); |
156 } | 156 } |
157 | 157 |
158 private: | 158 private: |
159 // Magic required by ref_counted.h to avoid any code deleting the object | 159 // Magic required by ref_counted.h to avoid any code deleting the object |
160 // accidently while there are references to it. | 160 // accidentally while there are references to it. |
161 friend class base::RefCountedThreadSafe<AudioSocket>; | 161 friend class base::RefCountedThreadSafe<AudioSocket>; |
162 ~AudioSocket() { } | 162 ~AudioSocket() { } |
163 | 163 |
164 base::Lock lock_; | 164 base::Lock lock_; |
165 base::SyncSocket socket_; | 165 base::SyncSocket socket_; |
166 }; | 166 }; |
167 | 167 |
168 // Magic required by ref_counted.h to avoid any code deleting the object | 168 // Magic required by ref_counted.h to avoid any code deleting the object |
169 // accidently while there are references to it. | 169 // accidently while there are references to it. |
170 friend class base::RefCountedThreadSafe<AudioDevice>; | 170 friend class base::RefCountedThreadSafe<AudioDevice>; |
171 virtual ~AudioDevice(); | 171 virtual ~AudioDevice(); |
172 | 172 |
173 // Methods called on IO thread ---------------------------------------------- | 173 // Methods called on IO thread ---------------------------------------------- |
174 // The following methods are tasks posted on the IO thread that needs to | 174 // The following methods are tasks posted on the IO thread that needs to |
175 // be executed on that thread. They interact with AudioMessageFilter and | 175 // be executed on that thread. They interact with AudioMessageFilter and |
176 // sends IPC messages on that thread. | 176 // sends IPC messages on that thread. |
177 void InitializeOnIOThread(const AudioParameters& params); | 177 void InitializeOnIOThread(const AudioParameters& params); |
178 void PlayOnIOThread(); | 178 void PlayOnIOThread(); |
179 void PauseOnIOThread(bool flush); | 179 void PauseOnIOThread(bool flush); |
180 void ShutDownOnIOThread(base::WaitableEvent* completion); | 180 void ShutDownOnIOThread(); |
181 void SetVolumeOnIOThread(double volume); | 181 void SetVolumeOnIOThread(double volume); |
182 | 182 |
183 void Send(IPC::Message* message); | 183 void Send(IPC::Message* message); |
184 | 184 |
185 // Method called on the audio thread ---------------------------------------- | 185 // Method called on the audio thread ---------------------------------------- |
186 // Calls the client's callback for rendering audio. | 186 // Calls the client's callback for rendering audio. |
187 // Returns actual number of filled frames that callback returned. This length | 187 // Returns actual number of filled frames that callback returned. This length |
188 // is passed to host at the end of the shared memory (i.e. buffer). In case of | 188 // is passed to host at the end of the shared memory (i.e. buffer). In case of |
189 // continuous stream host just ignores it and assumes buffer is always filled | 189 // continuous stream host just ignores it and assumes buffer is always filled |
190 // to its capacity. | 190 // to its capacity. |
(...skipping 22 matching lines...) Expand all Loading... |
213 | 213 |
214 // The client stores the last reported audio delay in this member. | 214 // The client stores the last reported audio delay in this member. |
215 // The delay shall reflect the amount of audio which still resides in | 215 // The delay shall reflect the amount of audio which still resides in |
216 // the output buffer, i.e., the expected audio output delay. | 216 // the output buffer, i.e., the expected audio output delay. |
217 int audio_delay_milliseconds_; | 217 int audio_delay_milliseconds_; |
218 | 218 |
219 // The current volume scaling [0.0, 1.0] of the audio stream. | 219 // The current volume scaling [0.0, 1.0] of the audio stream. |
220 double volume_; | 220 double volume_; |
221 | 221 |
222 // Callbacks for rendering audio occur on this thread. | 222 // Callbacks for rendering audio occur on this thread. |
| 223 // Must only be modified on the IO thread and when the thread is not running. |
223 scoped_ptr<base::DelegateSimpleThread> audio_thread_; | 224 scoped_ptr<base::DelegateSimpleThread> audio_thread_; |
224 | 225 |
225 // Cached audio message filter (lives on the main render thread). | 226 // Cached audio message filter (lives on the main render thread). |
226 scoped_refptr<AudioMessageFilter> filter_; | 227 scoped_refptr<AudioMessageFilter> filter_; |
227 | 228 |
228 // Our stream ID on the message filter. Only accessed on the IO thread. | 229 // Our stream ID on the message filter. Only accessed on the IO thread. |
| 230 // Must only be modified on the IO thread. |
229 int32 stream_id_; | 231 int32 stream_id_; |
230 | 232 |
231 // State of Play() / Pause() calls before OnLowLatencyCreated() is called. | 233 // State of Play() / Pause() calls before OnLowLatencyCreated() is called. |
232 bool play_on_start_; | 234 bool play_on_start_; |
233 | 235 |
234 // Set to |true| when OnLowLatencyCreated() is called. | 236 // Set to |true| when OnLowLatencyCreated() is called. |
235 // Set to |false| when ShutDownOnIOThread() is called. | 237 // Set to |false| when ShutDownOnIOThread() is called. |
236 // This is for use with play_on_start_ to track Play() / Pause() state. | 238 // This is for use with play_on_start_ to track Play() / Pause() state. |
| 239 // Must only be touched from the IO thread. |
237 bool is_started_; | 240 bool is_started_; |
238 | 241 |
239 // Data transfer between browser and render process uses a combination | 242 // Data transfer between browser and render process uses a combination |
240 // of sync sockets and shared memory to provide lowest possible latency. | 243 // of sync sockets and shared memory to provide lowest possible latency. |
| 244 // These variables must only be set on the IO thread while the audio_thread_ |
| 245 // is not running. |
241 base::SharedMemoryHandle shared_memory_handle_; | 246 base::SharedMemoryHandle shared_memory_handle_; |
242 scoped_refptr<AudioSocket> audio_socket_; | 247 scoped_refptr<AudioSocket> audio_socket_; |
243 int memory_length_; | 248 int memory_length_; |
244 | 249 |
245 // Protects lifetime of: | |
246 // audio_socket_ | |
247 // audio_thread_ | |
248 base::Lock lock_; | |
249 | |
250 DISALLOW_COPY_AND_ASSIGN(AudioDevice); | 250 DISALLOW_COPY_AND_ASSIGN(AudioDevice); |
251 }; | 251 }; |
252 | 252 |
253 #endif // CONTENT_RENDERER_MEDIA_AUDIO_DEVICE_H_ | 253 #endif // CONTENT_RENDERER_MEDIA_AUDIO_DEVICE_H_ |
OLD | NEW |