| 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 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 137 // Called when async stop request is completed. | 137 // Called when async stop request is completed. |
| 138 // error_cb: | 138 // error_cb: |
| 139 // Called when a MediaCodec error occurred. If this happens, a player has | 139 // Called when a MediaCodec error occurred. If this happens, a player has |
| 140 // to either call ReleaseDecoderResources() or destroy the decoder object. | 140 // to either call ReleaseDecoderResources() or destroy the decoder object. |
| 141 // decoder_thread_name: | 141 // decoder_thread_name: |
| 142 // The thread name to be passed to decoder thread constructor. | 142 // The thread name to be passed to decoder thread constructor. |
| 143 MediaCodecDecoder( | 143 MediaCodecDecoder( |
| 144 const scoped_refptr<base::SingleThreadTaskRunner>& media_task_runner, | 144 const scoped_refptr<base::SingleThreadTaskRunner>& media_task_runner, |
| 145 const base::Closure& external_request_data_cb, | 145 const base::Closure& external_request_data_cb, |
| 146 const base::Closure& starvation_cb, | 146 const base::Closure& starvation_cb, |
| 147 const base::Closure& preroll_done_cb, |
| 147 const base::Closure& stop_done_cb, | 148 const base::Closure& stop_done_cb, |
| 148 const base::Closure& error_cb, | 149 const base::Closure& error_cb, |
| 149 const char* decoder_thread_name); | 150 const char* decoder_thread_name); |
| 150 virtual ~MediaCodecDecoder(); | 151 virtual ~MediaCodecDecoder(); |
| 151 | 152 |
| 152 virtual const char* class_name() const; | 153 virtual const char* class_name() const; |
| 153 | 154 |
| 154 // MediaCodecDecoder exists through the whole lifetime of the player | 155 // MediaCodecDecoder exists through the whole lifetime of the player |
| 155 // to support dynamic addition and removal of the streams. | 156 // to support dynamic addition and removal of the streams. |
| 156 // This method returns true if the current stream (audio or video) | 157 // This method returns true if the current stream (audio or video) |
| 157 // is currently active. | 158 // is currently active. |
| 158 virtual bool HasStream() const = 0; | 159 virtual bool HasStream() const = 0; |
| 159 | 160 |
| 160 // Stores configuration for the use of upcoming Configure() | 161 // Stores configuration for the use of upcoming Configure() |
| 161 virtual void SetDemuxerConfigs(const DemuxerConfigs& configs) = 0; | 162 virtual void SetDemuxerConfigs(const DemuxerConfigs& configs) = 0; |
| 162 | 163 |
| 163 // Stops decoder thread, releases the MediaCodecBridge and other resources. | 164 // Stops decoder thread, releases the MediaCodecBridge and other resources. |
| 164 virtual void ReleaseDecoderResources(); | 165 virtual void ReleaseDecoderResources(); |
| 165 | 166 |
| 166 // Flushes the MediaCodec, after that resets the AccessUnitQueue and blocks | 167 // Flushes the MediaCodec, after that resets the AccessUnitQueue and blocks |
| 167 // the input. Decoder thread should not be running. | 168 // the input. Decoder thread should not be running. |
| 168 virtual void Flush(); | 169 virtual void Flush(); |
| 169 | 170 |
| 170 // Releases MediaCodecBridge. | 171 // Releases MediaCodecBridge. |
| 171 void ReleaseMediaCodec(); | 172 void ReleaseMediaCodec(); |
| 172 | 173 |
| 173 // Returns corresponding conditions. | 174 // Returns corresponding conditions. |
| 174 bool IsPrefetchingOrPlaying() const; | 175 bool IsPrefetchingOrPlaying() const; |
| 176 bool IsPrerollDone() const; |
| 175 bool IsStopped() const; | 177 bool IsStopped() const; |
| 176 bool IsCompleted() const; | 178 bool IsCompleted() const; |
| 177 | 179 |
| 178 base::android::ScopedJavaLocalRef<jobject> GetMediaCrypto(); | 180 base::android::ScopedJavaLocalRef<jobject> GetMediaCrypto(); |
| 179 | 181 |
| 180 // Starts prefetching: accumulates enough data in AccessUnitQueue. | 182 // Starts prefetching: accumulates enough data in AccessUnitQueue. |
| 181 // Decoder thread is not running. | 183 // Decoder thread is not running. |
| 182 void Prefetch(const base::Closure& prefetch_done_cb); | 184 void Prefetch(const base::Closure& prefetch_done_cb); |
| 183 | 185 |
| 184 // Configures MediaCodec. | 186 // Configures MediaCodec. |
| 185 ConfigStatus Configure(); | 187 ConfigStatus Configure(); |
| 186 | 188 |
| 189 // Sets preroll timestamp. The rendering starts after we reached it. |
| 190 void SetPrerollTimestamp(base::TimeDelta preroll_timestamp); |
| 191 |
| 187 // Starts the decoder thread and resumes the playback. | 192 // Starts the decoder thread and resumes the playback. |
| 188 bool Start(base::TimeDelta current_time); | 193 bool Start(base::TimeDelta start_timestamp); |
| 194 |
| 195 void ResumeAfterPreroll(); |
| 189 | 196 |
| 190 // Stops the playback process synchronously. This method stops the decoder | 197 // Stops the playback process synchronously. This method stops the decoder |
| 191 // thread synchronously, and then releases all MediaCodec buffers. | 198 // thread synchronously, and then releases all MediaCodec buffers. |
| 192 void SyncStop(); | 199 void SyncStop(); |
| 193 | 200 |
| 194 // Requests to stop the playback and returns. | 201 // Requests to stop the playback and returns. |
| 195 // Decoder will stop asynchronously after all the dequeued output buffers | 202 // Decoder will stop asynchronously after all the dequeued output buffers |
| 196 // are rendered. | 203 // are rendered. |
| 197 void RequestToStop(); | 204 void RequestToStop(); |
| 198 | 205 |
| 199 // Notification posted when asynchronous stop is done or playback completed. | 206 // Notification posted when asynchronous stop is done or playback completed. |
| 200 void OnLastFrameRendered(bool completed); | 207 void OnLastFrameRendered(bool completed); |
| 201 | 208 |
| 209 // Notification posted when last prerolled frame has been returned to codec. |
| 210 void OnPrerollDone(); |
| 211 |
| 202 // Puts the incoming data into AccessUnitQueue. | 212 // Puts the incoming data into AccessUnitQueue. |
| 203 void OnDemuxerDataAvailable(const DemuxerData& data); | 213 void OnDemuxerDataAvailable(const DemuxerData& data); |
| 204 | 214 |
| 205 protected: | 215 protected: |
| 206 // Returns true if the new DemuxerConfigs requires MediaCodec | 216 // Returns true if the new DemuxerConfigs requires MediaCodec |
| 207 // reconfiguration. | 217 // reconfiguration. |
| 208 virtual bool IsCodecReconfigureNeeded(const DemuxerConfigs& curr, | 218 virtual bool IsCodecReconfigureNeeded(const DemuxerConfigs& curr, |
| 209 const DemuxerConfigs& next) const = 0; | 219 const DemuxerConfigs& next) const = 0; |
| 210 | 220 |
| 211 // Does the part of MediaCodecBridge configuration that is specific | 221 // Does the part of MediaCodecBridge configuration that is specific |
| 212 // to audio or video. | 222 // to audio or video. |
| 213 virtual ConfigStatus ConfigureInternal() = 0; | 223 virtual ConfigStatus ConfigureInternal() = 0; |
| 214 | 224 |
| 215 // Associates PTS with device time so we can calculate delays. | 225 // Associates PTS with device time so we can calculate delays. |
| 216 // We use delays for video decoder only. | 226 // We use delays for video decoder only. |
| 217 virtual void SynchronizePTSWithTime(base::TimeDelta current_time) {} | 227 virtual void AssociateCurrentTimeWithPTS(base::TimeDelta current_time) {} |
| 228 |
| 229 // Invalidate delay calculation. We use delays for video decoder only. |
| 230 virtual void DissociatePTSFromTime() {} |
| 218 | 231 |
| 219 // Processes the change of the output format, varies by stream. | 232 // Processes the change of the output format, varies by stream. |
| 220 virtual void OnOutputFormatChanged() = 0; | 233 virtual void OnOutputFormatChanged() = 0; |
| 221 | 234 |
| 222 // Renders the decoded frame and releases output buffer, or posts | 235 // Renders the decoded frame and releases output buffer, or posts |
| 223 // a delayed task to do it at a later time, | 236 // a delayed task to do it at a later time, |
| 224 virtual void Render(int buffer_index, | 237 virtual void Render(int buffer_index, |
| 225 size_t size, | 238 size_t size, |
| 226 bool render_output, | 239 bool render_output, |
| 227 base::TimeDelta pts, | 240 base::TimeDelta pts, |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 261 | 274 |
| 262 // Flag forces reconfiguration even if |media_codec_bridge_| exists. Currently | 275 // Flag forces reconfiguration even if |media_codec_bridge_| exists. Currently |
| 263 // is set by video decoder when the video surface changes. | 276 // is set by video decoder when the video surface changes. |
| 264 bool needs_reconfigure_; | 277 bool needs_reconfigure_; |
| 265 | 278 |
| 266 private: | 279 private: |
| 267 enum DecoderState { | 280 enum DecoderState { |
| 268 kStopped = 0, | 281 kStopped = 0, |
| 269 kPrefetching, | 282 kPrefetching, |
| 270 kPrefetched, | 283 kPrefetched, |
| 284 kPrerolling, |
| 285 kPrerolled, |
| 271 kRunning, | 286 kRunning, |
| 272 kStopping, | 287 kStopping, |
| 273 kInEmergencyStop, | 288 kInEmergencyStop, |
| 274 kError, | 289 kError, |
| 275 }; | 290 }; |
| 276 | 291 |
| 277 // Helper method that processes an error from MediaCodec. | 292 // Helper method that processes an error from MediaCodec. |
| 278 void OnCodecError(); | 293 void OnCodecError(); |
| 279 | 294 |
| 280 // Requests data. Ensures there is no more than one request at a time. | 295 // Requests data. Ensures there is no more than one request at a time. |
| (...skipping 23 matching lines...) Expand all Loading... |
| 304 const char* AsString(DecoderState state); | 319 const char* AsString(DecoderState state); |
| 305 | 320 |
| 306 // Private Data. | 321 // Private Data. |
| 307 | 322 |
| 308 // External data request callback that is passed to decoder. | 323 // External data request callback that is passed to decoder. |
| 309 base::Closure external_request_data_cb_; | 324 base::Closure external_request_data_cb_; |
| 310 | 325 |
| 311 // These notifications are called on corresponding conditions. | 326 // These notifications are called on corresponding conditions. |
| 312 base::Closure prefetch_done_cb_; | 327 base::Closure prefetch_done_cb_; |
| 313 base::Closure starvation_cb_; | 328 base::Closure starvation_cb_; |
| 329 base::Closure preroll_done_cb_; |
| 314 base::Closure stop_done_cb_; | 330 base::Closure stop_done_cb_; |
| 315 base::Closure error_cb_; | 331 base::Closure error_cb_; |
| 316 | 332 |
| 317 // Data request callback that is posted by decoder internally. | 333 // Data request callback that is posted by decoder internally. |
| 318 base::Closure request_data_cb_; | 334 base::Closure request_data_cb_; |
| 319 | 335 |
| 320 // Callback used to post OnCodecError method. | 336 // Callback used to post OnCodecError method. |
| 321 base::Closure internal_error_cb_; | 337 base::Closure internal_error_cb_; |
| 322 | 338 |
| 339 // Callback for posting OnPrerollDone method. |
| 340 base::Closure internal_preroll_done_cb_; |
| 341 |
| 323 // Internal state. | 342 // Internal state. |
| 324 DecoderState state_; | 343 DecoderState state_; |
| 325 mutable base::Lock state_lock_; | 344 mutable base::Lock state_lock_; |
| 326 | 345 |
| 346 // Preroll timestamp is set if we need preroll and cleared after we done it. |
| 347 base::TimeDelta preroll_timestamp_; |
| 348 |
| 349 // Frame period used in the calculations related to prerolling. |
| 350 base::TimeDelta estimated_frame_period_; |
| 351 |
| 327 // Flag is set when the EOS is enqueued into MediaCodec. Reset by Flush. | 352 // Flag is set when the EOS is enqueued into MediaCodec. Reset by Flush. |
| 328 bool eos_enqueued_; | 353 bool eos_enqueued_; |
| 329 | 354 |
| 330 // Flag is set when the EOS is received in MediaCodec output. Reset by Flush. | 355 // Flag is set when the EOS is received in MediaCodec output. Reset by Flush. |
| 331 bool completed_; | 356 bool completed_; |
| 332 | 357 |
| 333 // Flag to ensure we post last frame notification once. | 358 // Flag to ensure we post last frame notification once. |
| 334 bool last_frame_posted_; | 359 bool last_frame_posted_; |
| 335 | 360 |
| 336 // Indicates whether the data request is in progress. | 361 // Indicates whether the data request is in progress. |
| 337 bool is_data_request_in_progress_; | 362 bool is_data_request_in_progress_; |
| 338 | 363 |
| 339 // Indicates whether the incoming data should be ignored. | 364 // Indicates whether the incoming data should be ignored. |
| 340 bool is_incoming_data_invalid_; | 365 bool is_incoming_data_invalid_; |
| 341 | 366 |
| 342 #ifndef NDEBUG | 367 #ifndef NDEBUG |
| 343 // When set, we check that the following video frame is the key frame. | 368 // When set, we check that the following video frame is the key frame. |
| 344 bool verify_next_frame_is_key_; | 369 bool verify_next_frame_is_key_; |
| 345 #endif | 370 #endif |
| 346 | 371 |
| 347 // NOTE: Weak pointers must be invalidated before all other member variables. | 372 // NOTE: Weak pointers must be invalidated before all other member variables. |
| 348 base::WeakPtrFactory<MediaCodecDecoder> weak_factory_; | 373 base::WeakPtrFactory<MediaCodecDecoder> weak_factory_; |
| 349 | 374 |
| 350 DISALLOW_COPY_AND_ASSIGN(MediaCodecDecoder); | 375 DISALLOW_COPY_AND_ASSIGN(MediaCodecDecoder); |
| 351 }; | 376 }; |
| 352 | 377 |
| 353 } // namespace media | 378 } // namespace media |
| 354 | 379 |
| 355 #endif // MEDIA_BASE_ANDROID_MEDIA_CODEC_DECODER_H_ | 380 #endif // MEDIA_BASE_ANDROID_MEDIA_CODEC_DECODER_H_ |
| OLD | NEW |