OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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_BASE_ANDROID_MEDIA_CODEC_DECODER_H_ | 5 #ifndef MEDIA_BASE_ANDROID_MEDIA_CODEC_DECODER_H_ |
6 #define MEDIA_BASE_ANDROID_MEDIA_CODEC_DECODER_H_ | 6 #define MEDIA_BASE_ANDROID_MEDIA_CODEC_DECODER_H_ |
7 | 7 |
8 #include "base/android/scoped_java_ref.h" | 8 #include "base/android/scoped_java_ref.h" |
9 #include "base/callback.h" | 9 #include "base/callback.h" |
10 #include "base/macros.h" | 10 #include "base/macros.h" |
(...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
170 // Stores configuration for the use of upcoming Configure() | 170 // Stores configuration for the use of upcoming Configure() |
171 virtual void SetDemuxerConfigs(const DemuxerConfigs& configs) = 0; | 171 virtual void SetDemuxerConfigs(const DemuxerConfigs& configs) = 0; |
172 | 172 |
173 // Stops decoder thread, releases the MediaCodecBridge and other resources. | 173 // Stops decoder thread, releases the MediaCodecBridge and other resources. |
174 virtual void ReleaseDecoderResources(); | 174 virtual void ReleaseDecoderResources(); |
175 | 175 |
176 // Flushes the MediaCodec, after that resets the AccessUnitQueue and blocks | 176 // Flushes the MediaCodec, after that resets the AccessUnitQueue and blocks |
177 // the input. Decoder thread should not be running. | 177 // the input. Decoder thread should not be running. |
178 virtual void Flush(); | 178 virtual void Flush(); |
179 | 179 |
180 // Releases MediaCodecBridge. | 180 // Releases MediaCodecBridge and any related buffers or references. |
181 void ReleaseMediaCodec(); | 181 virtual void ReleaseMediaCodec(); |
182 | 182 |
183 // Returns corresponding conditions. | 183 // Returns corresponding conditions. |
184 bool IsPrefetchingOrPlaying() const; | 184 bool IsPrefetchingOrPlaying() const; |
185 bool IsStopped() const; | 185 bool IsStopped() const; |
186 bool IsCompleted() const; | 186 bool IsCompleted() const; |
| 187 bool NeedsPreroll() const; |
187 | 188 |
188 base::android::ScopedJavaLocalRef<jobject> GetMediaCrypto(); | 189 base::android::ScopedJavaLocalRef<jobject> GetMediaCrypto(); |
189 | 190 |
190 // Starts prefetching: accumulates enough data in AccessUnitQueue. | 191 // Starts prefetching: accumulates enough data in AccessUnitQueue. |
191 // Decoder thread is not running. | 192 // Decoder thread is not running. |
192 void Prefetch(const base::Closure& prefetch_done_cb); | 193 void Prefetch(const base::Closure& prefetch_done_cb); |
193 | 194 |
194 // Configures MediaCodec. | 195 // Configures MediaCodec. |
195 ConfigStatus Configure(); | 196 ConfigStatus Configure(); |
196 | 197 |
197 // Starts the decoder thread and resumes the playback. | 198 // Starts the decoder for prerolling. This method starts the decoder thread. |
198 bool Start(base::TimeDelta current_time); | 199 bool Preroll(base::TimeDelta preroll_timestamp, |
| 200 const base::Closure& preroll_done_cb); |
| 201 |
| 202 // Starts the decoder after preroll is not needed, starting decoder thread |
| 203 // if it has not started yet. |
| 204 bool Start(base::TimeDelta start_timestamp); |
199 | 205 |
200 // Stops the playback process synchronously. This method stops the decoder | 206 // Stops the playback process synchronously. This method stops the decoder |
201 // thread synchronously, and then releases all MediaCodec buffers. | 207 // thread synchronously, and then releases all MediaCodec buffers. |
202 void SyncStop(); | 208 void SyncStop(); |
203 | 209 |
204 // Requests to stop the playback and returns. | 210 // Requests to stop the playback and returns. |
205 // Decoder will stop asynchronously after all the dequeued output buffers | 211 // Decoder will stop asynchronously after all the dequeued output buffers |
206 // are rendered. | 212 // are rendered. |
207 void RequestToStop(); | 213 void RequestToStop(); |
208 | 214 |
209 // Notification posted when asynchronous stop is done or playback completed. | 215 // Notification posted when asynchronous stop is done or playback completed. |
210 void OnLastFrameRendered(bool completed); | 216 void OnLastFrameRendered(bool completed); |
211 | 217 |
| 218 // Notification posted when last prerolled frame has been returned to codec. |
| 219 void OnPrerollDone(); |
| 220 |
212 // Puts the incoming data into AccessUnitQueue. | 221 // Puts the incoming data into AccessUnitQueue. |
213 void OnDemuxerDataAvailable(const DemuxerData& data); | 222 void OnDemuxerDataAvailable(const DemuxerData& data); |
214 | 223 |
| 224 // For testing only. Returns true if the decoder is in kPrerolling state. |
| 225 bool IsPrerollingForTests() const; |
| 226 |
215 protected: | 227 protected: |
| 228 enum RenderMode { |
| 229 kRenderSkip = 0, |
| 230 kRenderAfterPreroll, |
| 231 kRenderNow, |
| 232 }; |
| 233 |
216 // Returns true if the new DemuxerConfigs requires MediaCodec | 234 // Returns true if the new DemuxerConfigs requires MediaCodec |
217 // reconfiguration. | 235 // reconfiguration. |
218 virtual bool IsCodecReconfigureNeeded(const DemuxerConfigs& curr, | 236 virtual bool IsCodecReconfigureNeeded(const DemuxerConfigs& curr, |
219 const DemuxerConfigs& next) const = 0; | 237 const DemuxerConfigs& next) const = 0; |
220 | 238 |
221 // Does the part of MediaCodecBridge configuration that is specific | 239 // Does the part of MediaCodecBridge configuration that is specific |
222 // to audio or video. | 240 // to audio or video. |
223 virtual ConfigStatus ConfigureInternal() = 0; | 241 virtual ConfigStatus ConfigureInternal() = 0; |
224 | 242 |
225 // Associates PTS with device time so we can calculate delays. | 243 // Associates PTS with device time so we can calculate delays. |
226 // We use delays for video decoder only. | 244 // We use delays for video decoder only. |
227 virtual void SynchronizePTSWithTime(base::TimeDelta current_time) {} | 245 virtual void AssociateCurrentTimeWithPTS(base::TimeDelta current_time) {} |
| 246 |
| 247 // Invalidate delay calculation. We use delays for video decoder only. |
| 248 virtual void DissociatePTSFromTime() {} |
228 | 249 |
229 // Processes the change of the output format, varies by stream. | 250 // Processes the change of the output format, varies by stream. |
230 virtual void OnOutputFormatChanged() = 0; | 251 virtual void OnOutputFormatChanged() = 0; |
231 | 252 |
232 // Renders the decoded frame and releases output buffer, or posts | 253 // Renders the decoded frame and releases output buffer, or posts |
233 // a delayed task to do it at a later time, | 254 // a delayed task to do it at a later time, |
234 virtual void Render(int buffer_index, | 255 virtual void Render(int buffer_index, |
235 size_t size, | 256 size_t size, |
236 bool render_output, | 257 RenderMode render_mode, |
237 base::TimeDelta pts, | 258 base::TimeDelta pts, |
238 bool eos_encountered) = 0; | 259 bool eos_encountered) = 0; |
239 | 260 |
240 // Returns the number of delayed task (we might have them for video). | 261 // Returns the number of delayed task (we might have them for video). |
241 virtual int NumDelayedRenderTasks() const; | 262 virtual int NumDelayedRenderTasks() const; |
242 | 263 |
243 // Releases output buffers that are dequeued and not released yet (video) | 264 // Releases output buffers that are dequeued and not released yet (video). |
244 // if the |release| parameter is set and then remove the references to them. | 265 virtual void ReleaseDelayedBuffers() {} |
245 virtual void ClearDelayedBuffers(bool release) {} | |
246 | 266 |
247 #ifndef NDEBUG | 267 #ifndef NDEBUG |
248 // For video, checks that access unit is the key frame or stand-alone EOS. | 268 // For video, checks that access unit is the key frame or stand-alone EOS. |
249 virtual void VerifyUnitIsKeyFrame(const AccessUnit* unit) const {} | 269 virtual void VerifyUnitIsKeyFrame(const AccessUnit* unit) const {} |
250 #endif | 270 #endif |
251 | 271 |
252 // Helper methods. | 272 // Helper methods. |
253 | 273 |
254 // Notifies the decoder if the frame is the last one. | 274 // Notifies the decoder if the frame is the last one. |
255 void CheckLastFrame(bool eos_encountered, bool has_delayed_tasks); | 275 void CheckLastFrame(bool eos_encountered, bool has_delayed_tasks); |
256 | 276 |
| 277 const char* AsString(RenderMode render_mode); |
| 278 |
257 // Protected data. | 279 // Protected data. |
258 | 280 |
259 // Object for posting tasks on Media thread. | 281 // Object for posting tasks on Media thread. |
260 scoped_refptr<base::SingleThreadTaskRunner> media_task_runner_; | 282 scoped_refptr<base::SingleThreadTaskRunner> media_task_runner_; |
261 | 283 |
262 // Controls Android MediaCodec | 284 // Controls Android MediaCodec |
263 scoped_ptr<MediaCodecBridge> media_codec_bridge_; | 285 scoped_ptr<MediaCodecBridge> media_codec_bridge_; |
264 | 286 |
265 // We call MediaCodecBridge on this thread for both | 287 // We call MediaCodecBridge on this thread for both |
266 // input and output buffers. | 288 // input and output buffers. |
267 base::Thread decoder_thread_; | 289 base::Thread decoder_thread_; |
268 | 290 |
269 // The queue of access units. | 291 // The queue of access units. |
270 AccessUnitQueue au_queue_; | 292 AccessUnitQueue au_queue_; |
271 | 293 |
272 // Flag forces reconfiguration even if |media_codec_bridge_| exists. Currently | 294 // Flag forces reconfiguration even if |media_codec_bridge_| exists. Currently |
273 // is set by video decoder when the video surface changes. | 295 // is set by video decoder when the video surface changes. |
274 bool needs_reconfigure_; | 296 bool needs_reconfigure_; |
275 | 297 |
276 private: | 298 private: |
277 enum DecoderState { | 299 enum DecoderState { |
278 kStopped = 0, | 300 kStopped = 0, |
279 kPrefetching, | 301 kPrefetching, |
280 kPrefetched, | 302 kPrefetched, |
| 303 kPrerolling, |
| 304 kPrerolled, |
281 kRunning, | 305 kRunning, |
282 kStopping, | 306 kStopping, |
283 kInEmergencyStop, | 307 kInEmergencyStop, |
284 kError, | 308 kError, |
285 }; | 309 }; |
286 | 310 |
287 // Helper method that processes an error from MediaCodec. | 311 // Helper method that processes an error from MediaCodec. |
288 void OnCodecError(); | 312 void OnCodecError(); |
289 | 313 |
290 // Requests data. Ensures there is no more than one request at a time. | 314 // Requests data. Ensures there is no more than one request at a time. |
(...skipping 23 matching lines...) Expand all Loading... |
314 const char* AsString(DecoderState state); | 338 const char* AsString(DecoderState state); |
315 | 339 |
316 // Private Data. | 340 // Private Data. |
317 | 341 |
318 // External data request callback that is passed to decoder. | 342 // External data request callback that is passed to decoder. |
319 base::Closure external_request_data_cb_; | 343 base::Closure external_request_data_cb_; |
320 | 344 |
321 // These notifications are called on corresponding conditions. | 345 // These notifications are called on corresponding conditions. |
322 base::Closure prefetch_done_cb_; | 346 base::Closure prefetch_done_cb_; |
323 base::Closure starvation_cb_; | 347 base::Closure starvation_cb_; |
| 348 base::Closure preroll_done_cb_; |
324 base::Closure stop_done_cb_; | 349 base::Closure stop_done_cb_; |
325 base::Closure error_cb_; | 350 base::Closure error_cb_; |
326 | 351 |
327 // Data request callback that is posted by decoder internally. | 352 // Data request callback that is posted by decoder internally. |
328 base::Closure request_data_cb_; | 353 base::Closure request_data_cb_; |
329 | 354 |
330 // Callback used to post OnCodecError method. | 355 // Callback used to post OnCodecError method. |
331 base::Closure internal_error_cb_; | 356 base::Closure internal_error_cb_; |
332 | 357 |
| 358 // Callback for posting OnPrerollDone method. |
| 359 base::Closure internal_preroll_done_cb_; |
| 360 |
333 // Internal state. | 361 // Internal state. |
334 DecoderState state_; | 362 DecoderState state_; |
335 mutable base::Lock state_lock_; | 363 mutable base::Lock state_lock_; |
336 | 364 |
| 365 // Preroll timestamp is set if we need preroll and cleared after we done it. |
| 366 base::TimeDelta preroll_timestamp_; |
| 367 |
| 368 // Indicates that playback should start with preroll. |
| 369 bool needs_preroll_; |
| 370 |
337 // Flag is set when the EOS is enqueued into MediaCodec. Reset by Flush. | 371 // Flag is set when the EOS is enqueued into MediaCodec. Reset by Flush. |
338 bool eos_enqueued_; | 372 bool eos_enqueued_; |
339 | 373 |
340 // Flag is set when the EOS is received in MediaCodec output. Reset by Flush. | 374 // Flag is set when the EOS is received in MediaCodec output. Reset by Flush. |
341 bool completed_; | 375 bool completed_; |
342 | 376 |
343 // Flag to ensure we post last frame notification once. | 377 // Flag to ensure we post last frame notification once. |
344 bool last_frame_posted_; | 378 bool last_frame_posted_; |
345 | 379 |
346 // Indicates whether the data request is in progress. | 380 // Indicates whether the data request is in progress. |
347 bool is_data_request_in_progress_; | 381 bool is_data_request_in_progress_; |
348 | 382 |
349 // Indicates whether the incoming data should be ignored. | 383 // Indicates whether the incoming data should be ignored. |
350 bool is_incoming_data_invalid_; | 384 bool is_incoming_data_invalid_; |
351 | 385 |
352 #ifndef NDEBUG | 386 #ifndef NDEBUG |
353 // When set, we check that the following video frame is the key frame. | 387 // When set, we check that the following video frame is the key frame. |
354 bool verify_next_frame_is_key_; | 388 bool verify_next_frame_is_key_; |
355 #endif | 389 #endif |
356 | 390 |
357 // NOTE: Weak pointers must be invalidated before all other member variables. | 391 // NOTE: Weak pointers must be invalidated before all other member variables. |
358 base::WeakPtrFactory<MediaCodecDecoder> weak_factory_; | 392 base::WeakPtrFactory<MediaCodecDecoder> weak_factory_; |
359 | 393 |
360 DISALLOW_COPY_AND_ASSIGN(MediaCodecDecoder); | 394 DISALLOW_COPY_AND_ASSIGN(MediaCodecDecoder); |
361 }; | 395 }; |
362 | 396 |
363 } // namespace media | 397 } // namespace media |
364 | 398 |
365 #endif // MEDIA_BASE_ANDROID_MEDIA_CODEC_DECODER_H_ | 399 #endif // MEDIA_BASE_ANDROID_MEDIA_CODEC_DECODER_H_ |
OLD | NEW |