Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(127)

Side by Side Diff: media/audio/audio_output_controller.h

Issue 9121062: Remove "high"-latency audio code path (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebase ToT yet again Created 8 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « content/renderer/pepper_plugin_delegate_impl.cc ('k') | media/audio/audio_output_controller.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 also creates and opens an AudioOutputStream on the audio manager
128 // will be created on the audio controller thread and when that is done 115 // thread, and if this is successful, the |event_handler| will receive an
129 // event handler will receive a OnCreated() call. 116 // OnCreated() call from the same audio manager thread.
130 static scoped_refptr<AudioOutputController> Create( 117 static scoped_refptr<AudioOutputController> Create(
131 AudioManager* audio_manager, 118 AudioManager* audio_manager,
132 EventHandler* event_handler, 119 EventHandler* event_handler,
133 const AudioParameters& params, 120 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. 121 // External synchronous reader for audio controller.
144 SyncReader* sync_reader); 122 SyncReader* sync_reader);
145 123
146 // Methods to control playback of the stream. 124 // Methods to control playback of the stream.
147 125
148 // Starts the playback of this audio output stream. 126 // Starts the playback of this audio output stream.
149 void Play(); 127 void Play();
150 128
151 // Pause this audio output stream. 129 // Pause this audio output stream.
152 void Pause(); 130 void Pause();
153 131
154 // Discard all audio data buffered in this output stream. This method only 132 // Discard all audio data buffered in this output stream. This method only
155 // has effect when the stream is paused. 133 // has effect when the stream is paused.
156 void Flush(); 134 void Flush();
157 135
158 // Closes the audio output stream. The state is changed and the resources 136 // 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. 137 // are freed on the audio manager thread. closed_task is executed after that.
160 // Callbacks (EventHandler and SyncReader) must exist until closed_task is 138 // Callbacks (EventHandler and SyncReader) must exist until closed_task is
161 // called. 139 // called.
162 // 140 //
163 // It is safe to call this method more than once. Calls after the first one 141 // It is safe to call this method more than once. Calls after the first one
164 // will have no effect. 142 // will have no effect.
165 void Close(const base::Closure& closed_task); 143 void Close(const base::Closure& closed_task);
166 144
167 // Sets the volume of the audio output stream. 145 // Sets the volume of the audio output stream.
168 void SetVolume(double volume); 146 void SetVolume(double volume);
169 147
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 /////////////////////////////////////////////////////////////////////////// 148 ///////////////////////////////////////////////////////////////////////////
178 // AudioSourceCallback methods. 149 // AudioSourceCallback methods.
179 virtual uint32 OnMoreData(AudioOutputStream* stream, 150 virtual uint32 OnMoreData(AudioOutputStream* stream,
180 uint8* dest, 151 uint8* dest,
181 uint32 max_size, 152 uint32 max_size,
182 AudioBuffersState buffers_state) OVERRIDE; 153 AudioBuffersState buffers_state) OVERRIDE;
183 virtual void OnError(AudioOutputStream* stream, int code) OVERRIDE; 154 virtual void OnError(AudioOutputStream* stream, int code) OVERRIDE;
184 virtual void WaitTillDataReady() OVERRIDE; 155 virtual void WaitTillDataReady() OVERRIDE;
185 156
186 protected: 157 protected:
(...skipping 12 matching lines...) Expand all
199 friend class base::RefCountedThreadSafe<AudioOutputController>; 170 friend class base::RefCountedThreadSafe<AudioOutputController>;
200 virtual ~AudioOutputController(); 171 virtual ~AudioOutputController();
201 172
202 private: 173 private:
203 // We are polling sync reader if data became available. 174 // We are polling sync reader if data became available.
204 static const int kPollNumAttempts; 175 static const int kPollNumAttempts;
205 static const int kPollPauseInMilliseconds; 176 static const int kPollPauseInMilliseconds;
206 177
207 AudioOutputController(AudioManager* audio_manager, 178 AudioOutputController(AudioManager* audio_manager,
208 EventHandler* handler, 179 EventHandler* handler,
209 uint32 capacity, SyncReader* sync_reader); 180 SyncReader* sync_reader);
210 181
211 // The following methods are executed on the audio controller thread. 182 // The following methods are executed on the audio manager thread.
212 void DoCreate(const AudioParameters& params); 183 void DoCreate(const AudioParameters& params);
213 void DoPlay(); 184 void DoPlay();
214 void PollAndStartIfDataReady(); 185 void PollAndStartIfDataReady();
215 void DoPause(); 186 void DoPause();
216 void DoFlush(); 187 void DoFlush();
217 void DoClose(const base::Closure& closed_task); 188 void DoClose(const base::Closure& closed_task);
218 void DoSetVolume(double volume); 189 void DoSetVolume(double volume);
219 void DoReportError(int code); 190 void DoReportError(int code);
220 191
221 // Helper method to submit a OnMoreData() call to the event handler.
222 void SubmitOnMoreData_Locked();
223
224 // Helper method that starts physical stream. 192 // Helper method that starts physical stream.
225 void StartStream(); 193 void StartStream();
226 194
227 // Helper method that stops, closes, and NULLs |*stream_|. 195 // Helper method that stops, closes, and NULLs |*stream_|.
228 // Signals event when done if it is not NULL. 196 // Signals event when done if it is not NULL.
229 void DoStopCloseAndClearStream(base::WaitableEvent *done); 197 void DoStopCloseAndClearStream(base::WaitableEvent *done);
230 198
231 scoped_refptr<AudioManager> audio_manager_; 199 scoped_refptr<AudioManager> audio_manager_;
232 // |handler_| may be called only if |state_| is not kClosed. 200 // |handler_| may be called only if |state_| is not kClosed.
233 EventHandler* handler_; 201 EventHandler* handler_;
234 AudioOutputStream* stream_; 202 AudioOutputStream* stream_;
235 203
236 // The current volume of the audio stream. 204 // The current volume of the audio stream.
237 double volume_; 205 double volume_;
238 206
239 // |state_| is written on the audio controller thread and is read on the 207 // |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 208 // hardware audio thread. These operations need to be locked. But lock
241 // is not required for reading on the audio controller thread. 209 // is not required for reading on the audio manager thread.
242 State state_; 210 State state_;
243 211
244 AudioBuffersState buffers_state_; 212 // The |lock_| must be acquired whenever we access |state_| from a thread
245 213 // other than the audio manager thread.
246 // The |lock_| must be acquired whenever we access |buffer_|.
247 base::Lock lock_; 214 base::Lock lock_;
248 215
249 media::SeekableBuffer buffer_;
250
251 bool pending_request_;
252
253 // SyncReader is used only in low latency mode for synchronous reading. 216 // SyncReader is used only in low latency mode for synchronous reading.
254 SyncReader* sync_reader_; 217 SyncReader* sync_reader_;
255 218
256 // The message loop of audio thread that this object runs on. 219 // The message loop of audio manager thread that this object runs on.
257 scoped_refptr<base::MessageLoopProxy> message_loop_; 220 scoped_refptr<base::MessageLoopProxy> message_loop_;
258 221
259 // When starting stream we wait for data to become available. 222 // When starting stream we wait for data to become available.
260 // Number of times left. 223 // Number of times left.
261 int number_polling_attempts_left_; 224 int number_polling_attempts_left_;
262 225
263 // Used to post delayed tasks to ourselves that we can cancel. 226 // 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 227 // 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. 228 // 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. 229 // Also, if we're shutting down, we do not want to poll for more data.
267 base::WeakPtrFactory<AudioOutputController> weak_this_; 230 base::WeakPtrFactory<AudioOutputController> weak_this_;
268 231
269 DISALLOW_COPY_AND_ASSIGN(AudioOutputController); 232 DISALLOW_COPY_AND_ASSIGN(AudioOutputController);
270 }; 233 };
271 234
272 } // namespace media 235 } // namespace media
273 236
274 #endif // MEDIA_AUDIO_AUDIO_OUTPUT_CONTROLLER_H_ 237 #endif // MEDIA_AUDIO_AUDIO_OUTPUT_CONTROLLER_H_
OLDNEW
« no previous file with comments | « content/renderer/pepper_plugin_delegate_impl.cc ('k') | media/audio/audio_output_controller.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698