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 |
257 // Returns true is we are in the process of sync stop. | 277 // Returns true is we are in the process of sync stop. |
wolenetz
2015/08/20 23:39:12
nit:s/is/if/
Tima Vaisburd
2015/08/21 20:18:24
Done.
| |
258 bool InEmergencyStop() const { return GetState() == kInEmergencyStop; } | 278 bool InEmergencyStop() const { return GetState() == kInEmergencyStop; } |
259 | 279 |
280 const char* AsString(RenderMode render_mode); | |
281 | |
260 // Protected data. | 282 // Protected data. |
261 | 283 |
262 // Object for posting tasks on Media thread. | 284 // Object for posting tasks on Media thread. |
263 scoped_refptr<base::SingleThreadTaskRunner> media_task_runner_; | 285 scoped_refptr<base::SingleThreadTaskRunner> media_task_runner_; |
264 | 286 |
265 // Controls Android MediaCodec | 287 // Controls Android MediaCodec |
266 scoped_ptr<MediaCodecBridge> media_codec_bridge_; | 288 scoped_ptr<MediaCodecBridge> media_codec_bridge_; |
267 | 289 |
268 // We call MediaCodecBridge on this thread for both | 290 // We call MediaCodecBridge on this thread for both |
269 // input and output buffers. | 291 // input and output buffers. |
270 base::Thread decoder_thread_; | 292 base::Thread decoder_thread_; |
271 | 293 |
272 // The queue of access units. | 294 // The queue of access units. |
273 AccessUnitQueue au_queue_; | 295 AccessUnitQueue au_queue_; |
274 | 296 |
275 // Flag forces reconfiguration even if |media_codec_bridge_| exists. Currently | 297 // Flag forces reconfiguration even if |media_codec_bridge_| exists. Currently |
276 // is set by video decoder when the video surface changes. | 298 // is set by video decoder when the video surface changes. |
277 bool needs_reconfigure_; | 299 bool needs_reconfigure_; |
278 | 300 |
279 private: | 301 private: |
280 enum DecoderState { | 302 enum DecoderState { |
281 kStopped = 0, | 303 kStopped = 0, |
282 kPrefetching, | 304 kPrefetching, |
283 kPrefetched, | 305 kPrefetched, |
306 kPrerolling, | |
307 kPrerolled, | |
284 kRunning, | 308 kRunning, |
285 kStopping, | 309 kStopping, |
286 kInEmergencyStop, | 310 kInEmergencyStop, |
287 kError, | 311 kError, |
288 }; | 312 }; |
289 | 313 |
290 // Helper method that processes an error from MediaCodec. | 314 // Helper method that processes an error from MediaCodec. |
291 void OnCodecError(); | 315 void OnCodecError(); |
292 | 316 |
293 // Requests data. Ensures there is no more than one request at a time. | 317 // Requests data. Ensures there is no more than one request at a time. |
(...skipping 23 matching lines...) Expand all Loading... | |
317 const char* AsString(DecoderState state); | 341 const char* AsString(DecoderState state); |
318 | 342 |
319 // Private Data. | 343 // Private Data. |
320 | 344 |
321 // External data request callback that is passed to decoder. | 345 // External data request callback that is passed to decoder. |
322 base::Closure external_request_data_cb_; | 346 base::Closure external_request_data_cb_; |
323 | 347 |
324 // These notifications are called on corresponding conditions. | 348 // These notifications are called on corresponding conditions. |
325 base::Closure prefetch_done_cb_; | 349 base::Closure prefetch_done_cb_; |
326 base::Closure starvation_cb_; | 350 base::Closure starvation_cb_; |
351 base::Closure preroll_done_cb_; | |
327 base::Closure stop_done_cb_; | 352 base::Closure stop_done_cb_; |
328 base::Closure error_cb_; | 353 base::Closure error_cb_; |
329 | 354 |
330 // Data request callback that is posted by decoder internally. | 355 // Data request callback that is posted by decoder internally. |
331 base::Closure request_data_cb_; | 356 base::Closure request_data_cb_; |
332 | 357 |
333 // Callback used to post OnCodecError method. | 358 // Callback used to post OnCodecError method. |
334 base::Closure internal_error_cb_; | 359 base::Closure internal_error_cb_; |
335 | 360 |
361 // Callback for posting OnPrerollDone method. | |
362 base::Closure internal_preroll_done_cb_; | |
363 | |
336 // Internal state. | 364 // Internal state. |
337 DecoderState state_; | 365 DecoderState state_; |
338 mutable base::Lock state_lock_; | 366 mutable base::Lock state_lock_; |
339 | 367 |
368 // Preroll timestamp is set if we need preroll and cleared after we done it. | |
369 base::TimeDelta preroll_timestamp_; | |
370 | |
371 // Indicates that playback should start with preroll. | |
372 bool needs_preroll_; | |
373 | |
340 // Flag is set when the EOS is enqueued into MediaCodec. Reset by Flush. | 374 // Flag is set when the EOS is enqueued into MediaCodec. Reset by Flush. |
341 bool eos_enqueued_; | 375 bool eos_enqueued_; |
342 | 376 |
343 // Flag is set when the EOS is received in MediaCodec output. Reset by Flush. | 377 // Flag is set when the EOS is received in MediaCodec output. Reset by Flush. |
344 bool completed_; | 378 bool completed_; |
345 | 379 |
346 // Flag to ensure we post last frame notification once. | 380 // Flag to ensure we post last frame notification once. |
347 bool last_frame_posted_; | 381 bool last_frame_posted_; |
348 | 382 |
349 // Indicates whether the data request is in progress. | 383 // Indicates whether the data request is in progress. |
350 bool is_data_request_in_progress_; | 384 bool is_data_request_in_progress_; |
351 | 385 |
352 // Indicates whether the incoming data should be ignored. | 386 // Indicates whether the incoming data should be ignored. |
353 bool is_incoming_data_invalid_; | 387 bool is_incoming_data_invalid_; |
354 | 388 |
355 #ifndef NDEBUG | 389 #ifndef NDEBUG |
356 // When set, we check that the following video frame is the key frame. | 390 // When set, we check that the following video frame is the key frame. |
357 bool verify_next_frame_is_key_; | 391 bool verify_next_frame_is_key_; |
358 #endif | 392 #endif |
359 | 393 |
360 // NOTE: Weak pointers must be invalidated before all other member variables. | 394 // NOTE: Weak pointers must be invalidated before all other member variables. |
361 base::WeakPtrFactory<MediaCodecDecoder> weak_factory_; | 395 base::WeakPtrFactory<MediaCodecDecoder> weak_factory_; |
362 | 396 |
363 DISALLOW_COPY_AND_ASSIGN(MediaCodecDecoder); | 397 DISALLOW_COPY_AND_ASSIGN(MediaCodecDecoder); |
364 }; | 398 }; |
365 | 399 |
366 } // namespace media | 400 } // namespace media |
367 | 401 |
368 #endif // MEDIA_BASE_ANDROID_MEDIA_CODEC_DECODER_H_ | 402 #endif // MEDIA_BASE_ANDROID_MEDIA_CODEC_DECODER_H_ |
OLD | NEW |