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 |
145 // Magic required by ref_counted.h to avoid any code deleting the object | 172 // Magic required by ref_counted.h to avoid any code deleting the object |
146 // accidentally while there are references to it. | 173 // accidentally while there are references to it. |
147 friend class base::RefCountedThreadSafe<AudioDevice>; | 174 friend class base::RefCountedThreadSafe<AudioDevice>; |
148 virtual ~AudioDevice(); | 175 virtual ~AudioDevice(); |
149 | 176 |
150 // Methods called on IO thread ---------------------------------------------- | 177 // Methods called on IO thread ---------------------------------------------- |
151 // The following methods are tasks posted on the IO thread that needs to | 178 // The following methods are tasks posted on the IO thread that needs to |
152 // be executed on that thread. They interact with AudioMessageFilter and | 179 // be executed on that thread. They interact with AudioMessageFilter and |
153 // sends IPC messages on that thread. | 180 // sends IPC messages on that thread. |
154 void InitializeOnIOThread(const AudioParameters& params); | 181 void InitializeOnIOThread(const AudioParameters& params); |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
214 // Set to |false| when ShutDownOnIOThread() is called. | 241 // Set to |false| when ShutDownOnIOThread() is called. |
215 // This is for use with play_on_start_ to track Play() / Pause() state. | 242 // This is for use with play_on_start_ to track Play() / Pause() state. |
216 // Must only be touched from the IO thread. | 243 // Must only be touched from the IO thread. |
217 bool is_started_; | 244 bool is_started_; |
218 | 245 |
219 // Data transfer between browser and render process uses a combination | 246 // Data transfer between browser and render process uses a combination |
220 // of sync sockets and shared memory to provide lowest possible latency. | 247 // of sync sockets and shared memory to provide lowest possible latency. |
221 // These variables must only be set on the IO thread while the audio_thread_ | 248 // These variables must only be set on the IO thread while the audio_thread_ |
222 // is not running. | 249 // is not running. |
223 base::SharedMemoryHandle shared_memory_handle_; | 250 base::SharedMemoryHandle shared_memory_handle_; |
224 scoped_ptr<base::CancelableSyncSocket> audio_socket_; | 251 scoped_refptr<AudioSocket> audio_socket_; |
225 int memory_length_; | 252 int memory_length_; |
226 | 253 |
227 DISALLOW_COPY_AND_ASSIGN(AudioDevice); | 254 DISALLOW_COPY_AND_ASSIGN(AudioDevice); |
228 }; | 255 }; |
229 | 256 |
230 #endif // CONTENT_RENDERER_MEDIA_AUDIO_DEVICE_H_ | 257 #endif // CONTENT_RENDERER_MEDIA_AUDIO_DEVICE_H_ |
OLD | NEW |