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 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
135 virtual void OnRequestPacket(AudioBuffersState buffers_state) OVERRIDE; | 135 virtual void OnRequestPacket(AudioBuffersState buffers_state) OVERRIDE; |
136 virtual void OnStateChanged(AudioStreamState state) OVERRIDE; | 136 virtual void OnStateChanged(AudioStreamState state) OVERRIDE; |
137 virtual void OnCreated(base::SharedMemoryHandle handle, | 137 virtual void OnCreated(base::SharedMemoryHandle handle, |
138 uint32 length) OVERRIDE; | 138 uint32 length) OVERRIDE; |
139 virtual void OnLowLatencyCreated(base::SharedMemoryHandle handle, | 139 virtual void OnLowLatencyCreated(base::SharedMemoryHandle handle, |
140 base::SyncSocket::Handle socket_handle, | 140 base::SyncSocket::Handle socket_handle, |
141 uint32 length) OVERRIDE; | 141 uint32 length) OVERRIDE; |
142 virtual void OnVolume(double volume) OVERRIDE; | 142 virtual void OnVolume(double volume) OVERRIDE; |
143 | 143 |
144 private: | 144 private: |
145 // Encapsulate socket into separate class to avoid double-close. | |
146 // Class is ref-counted to avoid potential race condition if audio device | |
147 // is deleted simultaneously with audio thread stopping. | |
148 class AudioSocket : public base::RefCountedThreadSafe<AudioSocket> { | |
149 public: | |
150 explicit AudioSocket(base::SyncSocket::Handle socket_handle) | |
151 : socket_(socket_handle) { | |
152 } | |
153 base::SyncSocket* socket() { | |
154 return &socket_; | |
155 } | |
156 void Close() { | |
157 // Close() should be thread-safe, obtain the lock. | |
158 base::AutoLock auto_lock(lock_); | |
159 socket_.Close(); | |
160 } | |
161 | |
162 private: | |
163 // Magic required by ref_counted.h to avoid any code deleting the object | |
164 // accidentally while there are references to it. | |
165 friend class base::RefCountedThreadSafe<AudioSocket>; | |
166 ~AudioSocket() { } | |
167 | |
168 base::Lock lock_; | |
169 base::SyncSocket socket_; | |
170 }; | |
171 | |
172 // Magic required by ref_counted.h to avoid any code deleting the object | 145 // Magic required by ref_counted.h to avoid any code deleting the object |
173 // accidentally while there are references to it. | 146 // accidentally while there are references to it. |
174 friend class base::RefCountedThreadSafe<AudioDevice>; | 147 friend class base::RefCountedThreadSafe<AudioDevice>; |
175 virtual ~AudioDevice(); | 148 virtual ~AudioDevice(); |
176 | 149 |
177 // Methods called on IO thread ---------------------------------------------- | 150 // Methods called on IO thread ---------------------------------------------- |
178 // The following methods are tasks posted on the IO thread that needs to | 151 // The following methods are tasks posted on the IO thread that needs to |
179 // be executed on that thread. They interact with AudioMessageFilter and | 152 // be executed on that thread. They interact with AudioMessageFilter and |
180 // sends IPC messages on that thread. | 153 // sends IPC messages on that thread. |
181 void InitializeOnIOThread(const AudioParameters& params); | 154 void InitializeOnIOThread(const AudioParameters& params); |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
241 // Set to |false| when ShutDownOnIOThread() is called. | 214 // Set to |false| when ShutDownOnIOThread() is called. |
242 // This is for use with play_on_start_ to track Play() / Pause() state. | 215 // This is for use with play_on_start_ to track Play() / Pause() state. |
243 // Must only be touched from the IO thread. | 216 // Must only be touched from the IO thread. |
244 bool is_started_; | 217 bool is_started_; |
245 | 218 |
246 // Data transfer between browser and render process uses a combination | 219 // Data transfer between browser and render process uses a combination |
247 // of sync sockets and shared memory to provide lowest possible latency. | 220 // of sync sockets and shared memory to provide lowest possible latency. |
248 // These variables must only be set on the IO thread while the audio_thread_ | 221 // These variables must only be set on the IO thread while the audio_thread_ |
249 // is not running. | 222 // is not running. |
250 base::SharedMemoryHandle shared_memory_handle_; | 223 base::SharedMemoryHandle shared_memory_handle_; |
251 scoped_refptr<AudioSocket> audio_socket_; | 224 scoped_ptr<base::CancelableSyncSocket> audio_socket_; |
252 int memory_length_; | 225 int memory_length_; |
253 | 226 |
254 DISALLOW_COPY_AND_ASSIGN(AudioDevice); | 227 DISALLOW_COPY_AND_ASSIGN(AudioDevice); |
255 }; | 228 }; |
256 | 229 |
257 #endif // CONTENT_RENDERER_MEDIA_AUDIO_DEVICE_H_ | 230 #endif // CONTENT_RENDERER_MEDIA_AUDIO_DEVICE_H_ |
OLD | NEW |