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 #ifndef MEDIA_AUDIO_AUDIO_OUTPUT_CONTROLLER_H_ | 5 #ifndef MEDIA_AUDIO_AUDIO_OUTPUT_CONTROLLER_H_ |
6 #define MEDIA_AUDIO_AUDIO_OUTPUT_CONTROLLER_H_ | 6 #define MEDIA_AUDIO_AUDIO_OUTPUT_CONTROLLER_H_ |
7 | 7 |
8 #include "base/callback.h" | 8 #include "base/callback.h" |
9 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
11 #include "base/memory/weak_ptr.h" | 11 #include "base/memory/weak_ptr.h" |
12 #include "base/synchronization/lock.h" | 12 #include "base/synchronization/lock.h" |
13 #include "base/time.h" | 13 #include "base/time.h" |
14 #include "media/audio/audio_buffers_state.h" | 14 #include "media/audio/audio_buffers_state.h" |
15 #include "media/audio/audio_io.h" | 15 #include "media/audio/audio_io.h" |
16 #include "media/audio/audio_manager.h" | 16 #include "media/audio/audio_manager.h" |
17 #include "media/audio/simple_sources.h" | 17 #include "media/audio/simple_sources.h" |
18 | 18 |
19 namespace base { | 19 namespace base { |
20 class WaitableEvent; | 20 class WaitableEvent; |
21 } // namespace base | 21 } // namespace base |
22 | 22 |
23 class MessageLoop; | 23 class MessageLoop; |
24 | 24 |
25 // An AudioOutputController controls an AudioOutputStream and provides data | 25 // An AudioOutputController controls an AudioOutputStream and provides data |
26 // to this output stream. It has an important function that it executes | 26 // to this output stream. It has an important function that it executes |
27 // audio operations like play, pause, stop, etc. on a separate thread, | 27 // audio operations like play, pause, stop, etc. on a separate thread, |
28 // namely the audio controller thread. | 28 // namely the audio manager thread. |
29 // | 29 // |
30 // All the public methods of AudioOutputController are non-blocking. | 30 // All the public methods of AudioOutputController are non-blocking. |
31 // The actual operations are performed on the audio thread. | 31 // The actual operations are performed on the audio manager thread. |
32 // | 32 // |
33 // Here is a state diagram for the AudioOutputController for default low | 33 // Here is a state diagram for the AudioOutputController: |
34 // latency mode; in normal latency mode there is no "starting" or "paused when | |
35 // starting" states, "created" immediately switches to "playing": | |
36 // | 34 // |
37 // .-----------------------> [ Closed / Error ] <------. | 35 // .-----------------------> [ Closed / Error ] <------. |
38 // | ^ | | 36 // | ^ | |
39 // | | | | 37 // | | | |
40 // [ Created ] --> [ Starting ] --> [ Playing ] --> [ Paused ] | 38 // [ Created ] --> [ Starting ] --> [ Playing ] --> [ Paused ] |
41 // ^ | ^ | ^ | 39 // ^ | ^ | ^ |
42 // | | | | | | 40 // | | | | | |
43 // | | `----------------' | | 41 // | | `----------------' | |
44 // | V | | 42 // | V | |
45 // | [ PausedWhenStarting ] ------------------------' | 43 // | [ PausedWhenStarting ] ------------------------' |
46 // | | 44 // | |
47 // *[ Empty ] | 45 // *[ Empty ] |
48 // | 46 // |
49 // * Initial state | 47 // * Initial state |
50 // | 48 // |
51 // There are two modes of buffering operations supported by this class. | 49 // The AudioOutputStream can request data from the AudioOutputController via the |
50 // AudioSourceCallback interface. AudioOutputController uses the SyncReader | |
51 // passed to it via construction to synchronously fulfill this read request. | |
52 // | 52 // |
53 // Regular latency mode: | 53 // The audio manager thread is owned by the AudioManager that the |
54 // In this mode we receive signals from AudioOutputController and then we | 54 // AudioOutputController holds a reference to. When performing tasks on this |
55 // enqueue data into it. | 55 // thread, the controller must not add or release references to the |
56 // | |
57 // Low latency mode: | |
58 // In this mode a DataSource object is given to the AudioOutputController | |
59 // and AudioOutputController reads from it synchronously. | |
60 // | |
61 // The audio thread itself is owned by the AudioManager that the | |
62 // AudioOutputController holds a reference to. When performing tasks on the | |
63 // audio thread, the controller must not add or release references to the | |
64 // AudioManager or itself (since it in turn holds a reference to the manager), | 56 // AudioManager or itself (since it in turn holds a reference to the manager), |
65 // for delayed tasks as it can slow down or even prevent normal shut down. | 57 // for delayed tasks as it can slow down or even prevent normal shut down. |
66 // So, for tasks on the audio thread, the controller uses WeakPtr which enables | 58 // So, for tasks on the audio thread, the controller uses WeakPtr which enables |
67 // us to safely cancel pending polling tasks. | 59 // us to safely cancel pending polling tasks. |
68 // The owner of the audio thread, AudioManager, will take care of properly | 60 // |
69 // shutting it down. | 61 // AudioManager will take care of properly shutting down the audio manager |
62 // thread. | |
70 // | 63 // |
71 #include "media/base/media_export.h" | 64 #include "media/base/media_export.h" |
72 | 65 |
73 namespace media { | 66 namespace media { |
74 | 67 |
75 class MEDIA_EXPORT AudioOutputController | 68 class MEDIA_EXPORT AudioOutputController |
76 : public base::RefCountedThreadSafe<AudioOutputController>, | 69 : public base::RefCountedThreadSafe<AudioOutputController>, |
77 public AudioOutputStream::AudioSourceCallback { | 70 public AudioOutputStream::AudioSourceCallback { |
78 public: | 71 public: |
79 // Value sent by the controller to the renderer in low-latency mode | 72 // Value sent by the controller to the renderer in low-latency mode |
80 // indicating that the stream is paused. | 73 // indicating that the stream is paused. |
81 static const int kPauseMark; | 74 static const int kPauseMark; |
82 | 75 |
83 // An event handler that receives events from the AudioOutputController. The | 76 // An event handler that receives events from the AudioOutputController. The |
84 // following methods are called on the audio controller thread. | 77 // following methods are called on the audio manager thread. |
85 class MEDIA_EXPORT EventHandler { | 78 class MEDIA_EXPORT EventHandler { |
86 public: | 79 public: |
87 virtual ~EventHandler() {} | 80 virtual ~EventHandler() {} |
88 virtual void OnCreated(AudioOutputController* controller) = 0; | 81 virtual void OnCreated(AudioOutputController* controller) = 0; |
89 virtual void OnPlaying(AudioOutputController* controller) = 0; | 82 virtual void OnPlaying(AudioOutputController* controller) = 0; |
90 virtual void OnPaused(AudioOutputController* controller) = 0; | 83 virtual void OnPaused(AudioOutputController* controller) = 0; |
91 virtual void OnError(AudioOutputController* controller, int error_code) = 0; | 84 virtual void OnError(AudioOutputController* controller, int error_code) = 0; |
92 | |
93 // Audio controller asks for more data. | |
94 // |pending_bytes| is the number of bytes still on the controller. | |
95 // |timestamp| is then time when |pending_bytes| is recorded. | |
96 virtual void OnMoreData(AudioOutputController* controller, | |
97 AudioBuffersState buffers_state) = 0; | |
98 }; | 85 }; |
99 | 86 |
100 // A synchronous reader interface used by AudioOutputController for | 87 // A synchronous reader interface used by AudioOutputController for |
101 // synchronous reading. | 88 // synchronous reading. |
102 class SyncReader { | 89 class SyncReader { |
103 public: | 90 public: |
104 virtual ~SyncReader() {} | 91 virtual ~SyncReader() {} |
105 | 92 |
106 // Notify the synchronous reader the number of bytes in the | 93 // Notify the synchronous reader the number of bytes in the |
107 // AudioOutputController not yet played. This is used by SyncReader to | 94 // AudioOutputController not yet played. This is used by SyncReader to |
108 // prepare more data and perform synchronization. | 95 // prepare more data and perform synchronization. |
109 virtual void UpdatePendingBytes(uint32 bytes) = 0; | 96 virtual void UpdatePendingBytes(uint32 bytes) = 0; |
110 | 97 |
111 // Read certain amount of data into |data|. This method returns if some | 98 // Read certain amount of data into |data|. This method returns if some |
112 // data is available. | 99 // data is available. |
113 virtual uint32 Read(void* data, uint32 size) = 0; | 100 virtual uint32 Read(void* data, uint32 size) = 0; |
114 | 101 |
115 // Close this synchronous reader. | 102 // Close this synchronous reader. |
116 virtual void Close() = 0; | 103 virtual void Close() = 0; |
117 | 104 |
118 // Poll if data is ready. | 105 // Poll if data is ready. |
119 // Not reliable, as there is no guarantee that renderer is "new-style" | 106 // Not reliable, as there is no guarantee that renderer is "new-style" |
120 // renderer that writes metadata into buffer. After several unsuccessful | 107 // renderer that writes metadata into buffer. After several unsuccessful |
121 // attempts caller should assume the data is ready even if that function | 108 // attempts caller should assume the data is ready even if that function |
122 // returns false. | 109 // returns false. |
123 virtual bool DataReady() = 0; | 110 virtual bool DataReady() = 0; |
124 }; | 111 }; |
125 | 112 |
126 // Factory method for creating an AudioOutputController. | 113 // Factory method for creating an AudioOutputController. |
127 // If successful, an audio controller thread is created. The audio device | 114 // This posts a method on the |audio_manager|'s thread to create an |
henrika (OOO until Aug 14)
2012/02/01 13:11:29
Would it be possible to make this comment a bit mo
vrk (LEFT CHROMIUM)
2012/02/02 21:05:41
Done.
| |
128 // will be created on the audio controller thread and when that is done | 115 // AudioOutputStream. After the stream is created successfully, OnCreated() |
129 // event handler will receive a OnCreated() call. | 116 // is called on the |event_handler|, and is *not* guaranteed to be called on |
117 // the same thread on which this object was created. | |
130 static scoped_refptr<AudioOutputController> Create( | 118 static scoped_refptr<AudioOutputController> Create( |
131 AudioManager* audio_manager, | 119 AudioManager* audio_manager, |
132 EventHandler* event_handler, | 120 EventHandler* event_handler, |
133 const AudioParameters& params, | 121 const AudioParameters& params, |
134 // Soft limit for buffer capacity in this controller. This parameter | |
135 // is used only in regular latency mode. | |
136 uint32 buffer_capacity); | |
137 | |
138 // Factory method for creating a low latency audio stream. | |
139 static scoped_refptr<AudioOutputController> CreateLowLatency( | |
140 AudioManager* audio_manager, | |
141 EventHandler* event_handler, | |
142 const AudioParameters& params, | |
143 // External synchronous reader for audio controller. | 122 // External synchronous reader for audio controller. |
144 SyncReader* sync_reader); | 123 SyncReader* sync_reader); |
145 | 124 |
146 // Methods to control playback of the stream. | 125 // Methods to control playback of the stream. |
147 | 126 |
148 // Starts the playback of this audio output stream. | 127 // Starts the playback of this audio output stream. |
149 void Play(); | 128 void Play(); |
150 | 129 |
151 // Pause this audio output stream. | 130 // Pause this audio output stream. |
152 void Pause(); | 131 void Pause(); |
153 | 132 |
154 // Discard all audio data buffered in this output stream. This method only | 133 // Discard all audio data buffered in this output stream. This method only |
155 // has effect when the stream is paused. | 134 // has effect when the stream is paused. |
156 void Flush(); | 135 void Flush(); |
157 | 136 |
158 // Closes the audio output stream. The state is changed and the resources | 137 // Closes the audio output stream. The state is changed and the resources |
159 // are freed on the audio thread. closed_task is executed after that. | 138 // are freed on the audio manager thread. closed_task is executed after that. |
160 // Callbacks (EventHandler and SyncReader) must exist until closed_task is | 139 // Callbacks (EventHandler and SyncReader) must exist until closed_task is |
161 // called. | 140 // called. |
162 // | 141 // |
163 // It is safe to call this method more than once. Calls after the first one | 142 // It is safe to call this method more than once. Calls after the first one |
164 // will have no effect. | 143 // will have no effect. |
165 void Close(const base::Closure& closed_task); | 144 void Close(const base::Closure& closed_task); |
166 | 145 |
167 // Sets the volume of the audio output stream. | 146 // Sets the volume of the audio output stream. |
168 void SetVolume(double volume); | 147 void SetVolume(double volume); |
169 | 148 |
170 // Enqueue audio |data| into the controller. This method is used only in | |
171 // the regular latency mode and it is illegal to call this method when | |
172 // SyncReader is present. | |
173 void EnqueueData(const uint8* data, uint32 size); | |
174 | |
175 bool LowLatencyMode() const { return sync_reader_ != NULL; } | |
176 | |
177 /////////////////////////////////////////////////////////////////////////// | 149 /////////////////////////////////////////////////////////////////////////// |
178 // AudioSourceCallback methods. | 150 // AudioSourceCallback methods. |
179 virtual uint32 OnMoreData(AudioOutputStream* stream, | 151 virtual uint32 OnMoreData(AudioOutputStream* stream, |
180 uint8* dest, | 152 uint8* dest, |
181 uint32 max_size, | 153 uint32 max_size, |
182 AudioBuffersState buffers_state) OVERRIDE; | 154 AudioBuffersState buffers_state) OVERRIDE; |
183 virtual void OnError(AudioOutputStream* stream, int code) OVERRIDE; | 155 virtual void OnError(AudioOutputStream* stream, int code) OVERRIDE; |
184 virtual void WaitTillDataReady() OVERRIDE; | 156 virtual void WaitTillDataReady() OVERRIDE; |
185 | 157 |
186 protected: | 158 protected: |
(...skipping 12 matching lines...) Expand all Loading... | |
199 friend class base::RefCountedThreadSafe<AudioOutputController>; | 171 friend class base::RefCountedThreadSafe<AudioOutputController>; |
200 virtual ~AudioOutputController(); | 172 virtual ~AudioOutputController(); |
201 | 173 |
202 private: | 174 private: |
203 // We are polling sync reader if data became available. | 175 // We are polling sync reader if data became available. |
204 static const int kPollNumAttempts; | 176 static const int kPollNumAttempts; |
205 static const int kPollPauseInMilliseconds; | 177 static const int kPollPauseInMilliseconds; |
206 | 178 |
207 AudioOutputController(AudioManager* audio_manager, | 179 AudioOutputController(AudioManager* audio_manager, |
208 EventHandler* handler, | 180 EventHandler* handler, |
209 uint32 capacity, SyncReader* sync_reader); | 181 SyncReader* sync_reader); |
210 | 182 |
211 // The following methods are executed on the audio controller thread. | 183 // The following methods are executed on the audio manager thread. |
212 void DoCreate(const AudioParameters& params); | 184 void DoCreate(const AudioParameters& params); |
213 void DoPlay(); | 185 void DoPlay(); |
214 void PollAndStartIfDataReady(); | 186 void PollAndStartIfDataReady(); |
215 void DoPause(); | 187 void DoPause(); |
216 void DoFlush(); | 188 void DoFlush(); |
217 void DoClose(const base::Closure& closed_task); | 189 void DoClose(const base::Closure& closed_task); |
218 void DoSetVolume(double volume); | 190 void DoSetVolume(double volume); |
219 void DoReportError(int code); | 191 void DoReportError(int code); |
220 | 192 |
221 // Helper method to submit a OnMoreData() call to the event handler. | |
222 void SubmitOnMoreData_Locked(); | |
223 | |
224 // Helper method that starts physical stream. | 193 // Helper method that starts physical stream. |
225 void StartStream(); | 194 void StartStream(); |
226 | 195 |
227 // Helper method that stops, closes, and NULLs |*stream_|. | 196 // Helper method that stops, closes, and NULLs |*stream_|. |
228 // Signals event when done if it is not NULL. | 197 // Signals event when done if it is not NULL. |
229 void DoStopCloseAndClearStream(base::WaitableEvent *done); | 198 void DoStopCloseAndClearStream(base::WaitableEvent *done); |
230 | 199 |
231 scoped_refptr<AudioManager> audio_manager_; | 200 scoped_refptr<AudioManager> audio_manager_; |
232 // |handler_| may be called only if |state_| is not kClosed. | 201 // |handler_| may be called only if |state_| is not kClosed. |
233 EventHandler* handler_; | 202 EventHandler* handler_; |
234 AudioOutputStream* stream_; | 203 AudioOutputStream* stream_; |
235 | 204 |
236 // The current volume of the audio stream. | 205 // The current volume of the audio stream. |
237 double volume_; | 206 double volume_; |
238 | 207 |
239 // |state_| is written on the audio controller thread and is read on the | 208 // |state_| is written on the audio manager thread and is read on the |
240 // hardware audio thread. These operations need to be locked. But lock | 209 // hardware audio thread. These operations need to be locked. But lock |
241 // is not required for reading on the audio controller thread. | 210 // is not required for reading on the audio manager thread. |
242 State state_; | 211 State state_; |
243 | 212 |
244 AudioBuffersState buffers_state_; | 213 // The |lock_| must be acquired whenever we access |state_| from a thread |
245 | 214 // other than the audio manager thread. |
246 // The |lock_| must be acquired whenever we access |buffer_|. | |
247 base::Lock lock_; | 215 base::Lock lock_; |
248 | 216 |
249 media::SeekableBuffer buffer_; | |
250 | |
251 bool pending_request_; | |
252 | |
253 // SyncReader is used only in low latency mode for synchronous reading. | 217 // SyncReader is used only in low latency mode for synchronous reading. |
254 SyncReader* sync_reader_; | 218 SyncReader* sync_reader_; |
255 | 219 |
256 // The message loop of audio thread that this object runs on. | 220 // The message loop of audio manager thread that this object runs on. |
257 scoped_refptr<base::MessageLoopProxy> message_loop_; | 221 scoped_refptr<base::MessageLoopProxy> message_loop_; |
258 | 222 |
259 // When starting stream we wait for data to become available. | 223 // When starting stream we wait for data to become available. |
260 // Number of times left. | 224 // Number of times left. |
261 int number_polling_attempts_left_; | 225 int number_polling_attempts_left_; |
262 | 226 |
263 // Used to post delayed tasks to ourselves that we can cancel. | 227 // Used to post delayed tasks to ourselves that we can cancel. |
264 // We don't want the tasks to hold onto a reference as it will slow down | 228 // We don't want the tasks to hold onto a reference as it will slow down |
265 // shutdown and force it to wait for the most delayed task. | 229 // shutdown and force it to wait for the most delayed task. |
266 // Also, if we're shutting down, we do not want to poll for more data. | 230 // Also, if we're shutting down, we do not want to poll for more data. |
267 base::WeakPtrFactory<AudioOutputController> weak_this_; | 231 base::WeakPtrFactory<AudioOutputController> weak_this_; |
268 | 232 |
269 DISALLOW_COPY_AND_ASSIGN(AudioOutputController); | 233 DISALLOW_COPY_AND_ASSIGN(AudioOutputController); |
270 }; | 234 }; |
271 | 235 |
272 } // namespace media | 236 } // namespace media |
273 | 237 |
274 #endif // MEDIA_AUDIO_AUDIO_OUTPUT_CONTROLLER_H_ | 238 #endif // MEDIA_AUDIO_AUDIO_OUTPUT_CONTROLLER_H_ |
OLD | NEW |