| 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 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 164 // MediaCodecDecoder exists through the whole lifetime of the player | 164 // MediaCodecDecoder exists through the whole lifetime of the player |
| 165 // to support dynamic addition and removal of the streams. | 165 // to support dynamic addition and removal of the streams. |
| 166 // This method returns true if the current stream (audio or video) | 166 // This method returns true if the current stream (audio or video) |
| 167 // is currently active. | 167 // is currently active. |
| 168 virtual bool HasStream() const = 0; | 168 virtual bool HasStream() const = 0; |
| 169 | 169 |
| 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() = 0; |
| 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 NotCompletedAndNeedsPreroll() 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 offset, | 256 size_t offset, |
| 236 size_t size, | 257 size_t size, |
| 237 bool render_output, | 258 RenderMode render_mode, |
| 238 base::TimeDelta pts, | 259 base::TimeDelta pts, |
| 239 bool eos_encountered) = 0; | 260 bool eos_encountered) = 0; |
| 240 | 261 |
| 241 // Returns the number of delayed task (we might have them for video). | 262 // Returns the number of delayed task (we might have them for video). |
| 242 virtual int NumDelayedRenderTasks() const; | 263 virtual int NumDelayedRenderTasks() const; |
| 243 | 264 |
| 244 // Releases output buffers that are dequeued and not released yet (video) | 265 // Releases output buffers that are dequeued and not released yet (video). |
| 245 // if the |release| parameter is set and then remove the references to them. | 266 virtual void ReleaseDelayedBuffers() {} |
| 246 virtual void ClearDelayedBuffers(bool release) {} | |
| 247 | 267 |
| 248 #ifndef NDEBUG | 268 #ifndef NDEBUG |
| 249 // For video, checks that access unit is the key frame or stand-alone EOS. | 269 // For video, checks that access unit is the key frame or stand-alone EOS. |
| 250 virtual void VerifyUnitIsKeyFrame(const AccessUnit* unit) const {} | 270 virtual void VerifyUnitIsKeyFrame(const AccessUnit* unit) const {} |
| 251 #endif | 271 #endif |
| 252 | 272 |
| 253 // Helper methods. | 273 // Helper methods. |
| 254 | 274 |
| 275 // Synchroniously stop decoder thread. |
| 276 void DoEmergencyStop(); |
| 277 |
| 278 // Returns true if we are in the process of sync stop. |
| 279 bool InEmergencyStop() const { return GetState() == kInEmergencyStop; } |
| 280 |
| 255 // Notifies the decoder if the frame is the last one. | 281 // Notifies the decoder if the frame is the last one. |
| 256 void CheckLastFrame(bool eos_encountered, bool has_delayed_tasks); | 282 void CheckLastFrame(bool eos_encountered, bool has_delayed_tasks); |
| 257 | 283 |
| 258 // Returns true is we are in the process of sync stop. | 284 const char* AsString(RenderMode render_mode); |
| 259 bool InEmergencyStop() const { return GetState() == kInEmergencyStop; } | |
| 260 | 285 |
| 261 // Protected data. | 286 // Protected data. |
| 262 | 287 |
| 263 // Object for posting tasks on Media thread. | 288 // Object for posting tasks on Media thread. |
| 264 scoped_refptr<base::SingleThreadTaskRunner> media_task_runner_; | 289 scoped_refptr<base::SingleThreadTaskRunner> media_task_runner_; |
| 265 | 290 |
| 266 // Controls Android MediaCodec | 291 // Controls Android MediaCodec |
| 267 scoped_ptr<MediaCodecBridge> media_codec_bridge_; | 292 scoped_ptr<MediaCodecBridge> media_codec_bridge_; |
| 268 | 293 |
| 269 // We call MediaCodecBridge on this thread for both | 294 // We call MediaCodecBridge on this thread for both |
| 270 // input and output buffers. | 295 // input and output buffers. |
| 271 base::Thread decoder_thread_; | 296 base::Thread decoder_thread_; |
| 272 | 297 |
| 273 // The queue of access units. | 298 // The queue of access units. |
| 274 AccessUnitQueue au_queue_; | 299 AccessUnitQueue au_queue_; |
| 275 | 300 |
| 276 // Flag forces reconfiguration even if |media_codec_bridge_| exists. Currently | 301 // Flag forces reconfiguration even if |media_codec_bridge_| exists. Currently |
| 277 // is set by video decoder when the video surface changes. | 302 // is set by video decoder when the video surface changes. |
| 278 bool needs_reconfigure_; | 303 bool needs_reconfigure_; |
| 279 | 304 |
| 280 private: | 305 private: |
| 281 enum DecoderState { | 306 enum DecoderState { |
| 282 kStopped = 0, | 307 kStopped = 0, |
| 283 kPrefetching, | 308 kPrefetching, |
| 284 kPrefetched, | 309 kPrefetched, |
| 310 kPrerolling, |
| 311 kPrerolled, |
| 285 kRunning, | 312 kRunning, |
| 286 kStopping, | 313 kStopping, |
| 287 kInEmergencyStop, | 314 kInEmergencyStop, |
| 288 kError, | 315 kError, |
| 289 }; | 316 }; |
| 290 | 317 |
| 291 // Helper method that processes an error from MediaCodec. | 318 // Helper method that processes an error from MediaCodec. |
| 292 void OnCodecError(); | 319 void OnCodecError(); |
| 293 | 320 |
| 294 // Requests data. Ensures there is no more than one request at a time. | 321 // Requests data. Ensures there is no more than one request at a time. |
| (...skipping 23 matching lines...) Expand all Loading... |
| 318 const char* AsString(DecoderState state); | 345 const char* AsString(DecoderState state); |
| 319 | 346 |
| 320 // Private Data. | 347 // Private Data. |
| 321 | 348 |
| 322 // External data request callback that is passed to decoder. | 349 // External data request callback that is passed to decoder. |
| 323 base::Closure external_request_data_cb_; | 350 base::Closure external_request_data_cb_; |
| 324 | 351 |
| 325 // These notifications are called on corresponding conditions. | 352 // These notifications are called on corresponding conditions. |
| 326 base::Closure prefetch_done_cb_; | 353 base::Closure prefetch_done_cb_; |
| 327 base::Closure starvation_cb_; | 354 base::Closure starvation_cb_; |
| 355 base::Closure preroll_done_cb_; |
| 328 base::Closure stop_done_cb_; | 356 base::Closure stop_done_cb_; |
| 329 base::Closure error_cb_; | 357 base::Closure error_cb_; |
| 330 | 358 |
| 331 // Data request callback that is posted by decoder internally. | 359 // Data request callback that is posted by decoder internally. |
| 332 base::Closure request_data_cb_; | 360 base::Closure request_data_cb_; |
| 333 | 361 |
| 334 // Callback used to post OnCodecError method. | 362 // Callback used to post OnCodecError method. |
| 335 base::Closure internal_error_cb_; | 363 base::Closure internal_error_cb_; |
| 336 | 364 |
| 365 // Callback for posting OnPrerollDone method. |
| 366 base::Closure internal_preroll_done_cb_; |
| 367 |
| 337 // Internal state. | 368 // Internal state. |
| 338 DecoderState state_; | 369 DecoderState state_; |
| 339 mutable base::Lock state_lock_; | 370 mutable base::Lock state_lock_; |
| 340 | 371 |
| 372 // Preroll timestamp is set if we need preroll and cleared after we done it. |
| 373 base::TimeDelta preroll_timestamp_; |
| 374 |
| 375 // Indicates that playback should start with preroll. |
| 376 bool needs_preroll_; |
| 377 |
| 341 // Flag is set when the EOS is enqueued into MediaCodec. Reset by Flush. | 378 // Flag is set when the EOS is enqueued into MediaCodec. Reset by Flush. |
| 342 bool eos_enqueued_; | 379 bool eos_enqueued_; |
| 343 | 380 |
| 344 // Flag is set when the EOS is received in MediaCodec output. Reset by Flush. | 381 // Flag is set when the EOS is received in MediaCodec output. Reset by Flush. |
| 345 bool completed_; | 382 bool completed_; |
| 346 | 383 |
| 347 // Flag to ensure we post last frame notification once. | 384 // Flag to ensure we post last frame notification once. |
| 348 bool last_frame_posted_; | 385 bool last_frame_posted_; |
| 349 | 386 |
| 350 // Indicates whether the data request is in progress. | 387 // Indicates whether the data request is in progress. |
| 351 bool is_data_request_in_progress_; | 388 bool is_data_request_in_progress_; |
| 352 | 389 |
| 353 // Indicates whether the incoming data should be ignored. | 390 // Indicates whether the incoming data should be ignored. |
| 354 bool is_incoming_data_invalid_; | 391 bool is_incoming_data_invalid_; |
| 355 | 392 |
| 356 #ifndef NDEBUG | 393 #ifndef NDEBUG |
| 357 // When set, we check that the following video frame is the key frame. | 394 // When set, we check that the following video frame is the key frame. |
| 358 bool verify_next_frame_is_key_; | 395 bool verify_next_frame_is_key_; |
| 359 #endif | 396 #endif |
| 360 | 397 |
| 361 // NOTE: Weak pointers must be invalidated before all other member variables. | 398 // NOTE: Weak pointers must be invalidated before all other member variables. |
| 362 base::WeakPtrFactory<MediaCodecDecoder> weak_factory_; | 399 base::WeakPtrFactory<MediaCodecDecoder> weak_factory_; |
| 363 | 400 |
| 364 DISALLOW_COPY_AND_ASSIGN(MediaCodecDecoder); | 401 DISALLOW_COPY_AND_ASSIGN(MediaCodecDecoder); |
| 365 }; | 402 }; |
| 366 | 403 |
| 367 } // namespace media | 404 } // namespace media |
| 368 | 405 |
| 369 #endif // MEDIA_BASE_ANDROID_MEDIA_CODEC_DECODER_H_ | 406 #endif // MEDIA_BASE_ANDROID_MEDIA_CODEC_DECODER_H_ |
| OLD | NEW |