OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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" |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
96 // AudioOutputController not yet played. This is used by SyncReader to | 96 // AudioOutputController not yet played. This is used by SyncReader to |
97 // prepare more data and perform synchronization. | 97 // prepare more data and perform synchronization. |
98 virtual void UpdatePendingBytes(uint32 bytes) = 0; | 98 virtual void UpdatePendingBytes(uint32 bytes) = 0; |
99 | 99 |
100 // Read certain amount of data into |data|. This method returns if some | 100 // Read certain amount of data into |data|. This method returns if some |
101 // data is available. | 101 // data is available. |
102 virtual uint32 Read(void* data, uint32 size) = 0; | 102 virtual uint32 Read(void* data, uint32 size) = 0; |
103 | 103 |
104 // Close this synchronous reader. | 104 // Close this synchronous reader. |
105 virtual void Close() = 0; | 105 virtual void Close() = 0; |
| 106 |
| 107 // Poll if data is ready. |
| 108 // Not reliable, as there is no guarantee that renderer is "new-style" |
| 109 // renderer that writes metadata into buffer. After several unsuccessful |
| 110 // attempts caller should assume the data is ready even if that function |
| 111 // returns false. |
| 112 virtual bool DataReady() = 0; |
106 }; | 113 }; |
107 | 114 |
108 virtual ~AudioOutputController(); | 115 virtual ~AudioOutputController(); |
109 | 116 |
110 // Factory method for creating an AudioOutputController. | 117 // Factory method for creating an AudioOutputController. |
111 // If successful, an audio controller thread is created. The audio device | 118 // If successful, an audio controller thread is created. The audio device |
112 // will be created on the audio controller thread and when that is done | 119 // will be created on the audio controller thread and when that is done |
113 // event handler will receive a OnCreated() call. | 120 // event handler will receive a OnCreated() call. |
114 static scoped_refptr<AudioOutputController> Create( | 121 static scoped_refptr<AudioOutputController> Create( |
115 EventHandler* event_handler, | 122 EventHandler* event_handler, |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
156 | 163 |
157 bool LowLatencyMode() const { return sync_reader_ != NULL; } | 164 bool LowLatencyMode() const { return sync_reader_ != NULL; } |
158 | 165 |
159 /////////////////////////////////////////////////////////////////////////// | 166 /////////////////////////////////////////////////////////////////////////// |
160 // AudioSourceCallback methods. | 167 // AudioSourceCallback methods. |
161 virtual uint32 OnMoreData(AudioOutputStream* stream, uint8* dest, | 168 virtual uint32 OnMoreData(AudioOutputStream* stream, uint8* dest, |
162 uint32 max_size, AudioBuffersState buffers_state); | 169 uint32 max_size, AudioBuffersState buffers_state); |
163 virtual void OnError(AudioOutputStream* stream, int code); | 170 virtual void OnError(AudioOutputStream* stream, int code); |
164 | 171 |
165 private: | 172 private: |
| 173 // We are polling sync reader if data became available. |
| 174 static const int kPollNumAttempts; |
| 175 static const int kPollPauseInMilliseconds; |
| 176 |
166 AudioOutputController(EventHandler* handler, | 177 AudioOutputController(EventHandler* handler, |
167 uint32 capacity, SyncReader* sync_reader); | 178 uint32 capacity, SyncReader* sync_reader); |
168 | 179 |
169 // The following methods are executed on the audio controller thread. | 180 // The following methods are executed on the audio controller thread. |
170 void DoCreate(const AudioParameters& params); | 181 void DoCreate(const AudioParameters& params); |
171 void DoPlay(); | 182 void DoPlay(); |
| 183 void PollAndStartIfDataReady(); |
172 void DoPause(); | 184 void DoPause(); |
173 void DoFlush(); | 185 void DoFlush(); |
174 void DoClose(const base::Closure& closed_task); | 186 void DoClose(const base::Closure& closed_task); |
175 void DoSetVolume(double volume); | 187 void DoSetVolume(double volume); |
176 void DoReportError(int code); | 188 void DoReportError(int code); |
177 | 189 |
178 // Helper method to submit a OnMoreData() call to the event handler. | 190 // Helper method to submit a OnMoreData() call to the event handler. |
179 void SubmitOnMoreData_Locked(); | 191 void SubmitOnMoreData_Locked(); |
180 | 192 |
| 193 // Helper method that starts physical stream. |
| 194 void StartStream(); |
| 195 |
181 // |handler_| may be called only if |state_| is not kClosed. | 196 // |handler_| may be called only if |state_| is not kClosed. |
182 EventHandler* handler_; | 197 EventHandler* handler_; |
183 AudioOutputStream* stream_; | 198 AudioOutputStream* stream_; |
184 | 199 |
185 // The current volume of the audio stream. | 200 // The current volume of the audio stream. |
186 double volume_; | 201 double volume_; |
187 | 202 |
188 // |state_| is written on the audio controller thread and is read on the | 203 // |state_| is written on the audio controller thread and is read on the |
189 // hardware audio thread. These operations need to be locked. But lock | 204 // hardware audio thread. These operations need to be locked. But lock |
190 // is not required for reading on the audio controller thread. | 205 // is not required for reading on the audio controller thread. |
191 State state_; | 206 State state_; |
192 | 207 |
193 AudioBuffersState buffers_state_; | 208 AudioBuffersState buffers_state_; |
194 | 209 |
195 // The |lock_| must be acquired whenever we access |buffer_|. | 210 // The |lock_| must be acquired whenever we access |buffer_|. |
196 base::Lock lock_; | 211 base::Lock lock_; |
197 | 212 |
198 media::SeekableBuffer buffer_; | 213 media::SeekableBuffer buffer_; |
199 | 214 |
200 bool pending_request_; | 215 bool pending_request_; |
201 | 216 |
202 // SyncReader is used only in low latency mode for synchronous reading. | 217 // SyncReader is used only in low latency mode for synchronous reading. |
203 SyncReader* sync_reader_; | 218 SyncReader* sync_reader_; |
204 | 219 |
205 // The message loop of audio thread that this object runs on. | 220 // The message loop of audio thread that this object runs on. |
206 MessageLoop* message_loop_; | 221 MessageLoop* message_loop_; |
207 | 222 |
| 223 // When starting stream we wait for data to become available. |
| 224 // Number of times left. |
| 225 int number_polling_attempts_left_; |
| 226 |
208 DISALLOW_COPY_AND_ASSIGN(AudioOutputController); | 227 DISALLOW_COPY_AND_ASSIGN(AudioOutputController); |
209 }; | 228 }; |
210 | 229 |
211 } // namespace media | 230 } // namespace media |
212 | 231 |
213 #endif // MEDIA_AUDIO_AUDIO_OUTPUT_CONTROLLER_H_ | 232 #endif // MEDIA_AUDIO_AUDIO_OUTPUT_CONTROLLER_H_ |
OLD | NEW |