| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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_LOOP_H_ | 5 #ifndef MEDIA_BASE_ANDROID_MEDIA_CODEC_LOOP_H_ |
| 6 #define MEDIA_BASE_ANDROID_MEDIA_CODEC_LOOP_H_ | 6 #define MEDIA_BASE_ANDROID_MEDIA_CODEC_LOOP_H_ |
| 7 | 7 |
| 8 #include <deque> | 8 #include <deque> |
| 9 #include <memory> | 9 #include <memory> |
| 10 #include <utility> | 10 #include <utility> |
| 11 #include <vector> | 11 #include <vector> |
| 12 | 12 |
| 13 #include "base/macros.h" | 13 #include "base/macros.h" |
| 14 #include "base/memory/weak_ptr.h" | 14 #include "base/memory/weak_ptr.h" |
| 15 #include "base/time/tick_clock.h" |
| 15 #include "base/time/time.h" | 16 #include "base/time/time.h" |
| 16 #include "base/timer/timer.h" | 17 #include "base/timer/timer.h" |
| 17 #include "media/base/android/media_codec_bridge.h" | 18 #include "media/base/android/media_codec_bridge.h" |
| 18 #include "media/base/audio_decoder.h" | 19 #include "media/base/decode_status.h" |
| 19 #include "media/base/audio_decoder_config.h" | |
| 20 #include "media/base/media_export.h" | 20 #include "media/base/media_export.h" |
| 21 #include "media/base/subsample_entry.h" |
| 21 | 22 |
| 22 // MediaCodecLoop is based on Android's MediaCodec API. | 23 // MediaCodecLoop is based on Android's MediaCodec API. |
| 23 // The MediaCodec API is required to play encrypted (as in EME) content on | 24 // The MediaCodec API is required to play encrypted (as in EME) content on |
| 24 // Android. It is also a way to employ hardware-accelerated decoding. | 25 // Android. It is also a way to employ hardware-accelerated decoding. |
| 25 // One MediaCodecLoop instance owns a single MediaCodec(Bridge) instance, and | 26 // One MediaCodecLoop instance owns a single MediaCodec(Bridge) instance, and |
| 26 // drives it to perform decoding in conjunction with a MediaCodecLoop::Client. | 27 // drives it to perform decoding in conjunction with a MediaCodecLoop::Client. |
| 27 // The Client provides the input data and consumes the output data. A typical | 28 // The Client provides the input data and consumes the output data. A typical |
| 28 // example is AndroidVideoDecodeAccelerator. | 29 // example is AndroidVideoDecodeAccelerator. |
| 29 | 30 |
| 30 // Implementation notes. | 31 // Implementation notes. |
| (...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 190 | 191 |
| 191 // Notify the client when our state transitions to STATE_ERROR. | 192 // Notify the client when our state transitions to STATE_ERROR. |
| 192 virtual void OnCodecLoopError() = 0; | 193 virtual void OnCodecLoopError() = 0; |
| 193 | 194 |
| 194 protected: | 195 protected: |
| 195 virtual ~Client() {} | 196 virtual ~Client() {} |
| 196 }; | 197 }; |
| 197 | 198 |
| 198 // We will take ownership of |media_codec|. We will not destroy it until | 199 // We will take ownership of |media_codec|. We will not destroy it until |
| 199 // we are destructed. |media_codec| may not be null. | 200 // we are destructed. |media_codec| may not be null. |
| 200 MediaCodecLoop(Client* client, std::unique_ptr<MediaCodecBridge> media_codec); | 201 // |sdk_level| is temporary. It is used only to decouple MediaCodecLoop from |
| 202 // BuildInfo, until we get BuildInfo into a mockable state. |
| 203 MediaCodecLoop(int sdk_level, |
| 204 Client* client, |
| 205 std::unique_ptr<MediaCodecBridge> media_codec, |
| 206 scoped_refptr<base::SingleThreadTaskRunner> = |
| 207 scoped_refptr<base::SingleThreadTaskRunner>()); |
| 201 ~MediaCodecLoop(); | 208 ~MediaCodecLoop(); |
| 202 | 209 |
| 210 // Optionally set the tick clock used for testing. It is our caller's |
| 211 // responsibility to maintain ownership of this, since |
| 212 // FakeSingleThreadTaskRunner maintains a raw ptr to it also. |
| 213 void SetTestTickClock(base::TickClock* test_tick_clock); |
| 214 |
| 203 // Does the MediaCodec processing cycle: enqueues an input buffer, then | 215 // Does the MediaCodec processing cycle: enqueues an input buffer, then |
| 204 // dequeues output buffers. This should be called by the client when more | 216 // dequeues output buffers. This should be called by the client when more |
| 205 // work becomes available, such as when new input data arrives. If codec | 217 // work becomes available, such as when new input data arrives. If codec |
| 206 // output buffers are freed after OnDecodedFrame returns, then this should | 218 // output buffers are freed after OnDecodedFrame returns, then this should |
| 207 // also be called. | 219 // also be called. |
| 208 void DoPendingWork(); | 220 void DoPendingWork(); |
| 209 | 221 |
| 210 // Try to flush this media codec. Returns true on success, false on failure. | 222 // Try to flush this media codec. Returns true on success, false on failure. |
| 211 // Failures can result in a state change to the Error state. If this returns | 223 // Failures can result in a state change to the Error state. If this returns |
| 212 // false but the state is still READY, then the codec may continue to be used. | 224 // false but the state is still READY, then the codec may continue to be used. |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 264 // Returns true if an output buffer was received from MediaCodec. | 276 // Returns true if an output buffer was received from MediaCodec. |
| 265 bool ProcessOneOutputBuffer(); | 277 bool ProcessOneOutputBuffer(); |
| 266 | 278 |
| 267 // Start the timer immediately if |start| is true or stop it based on elapsed | 279 // Start the timer immediately if |start| is true or stop it based on elapsed |
| 268 // idle time if |start| is false. | 280 // idle time if |start| is false. |
| 269 void ManageTimer(bool start); | 281 void ManageTimer(bool start); |
| 270 | 282 |
| 271 // Helper method to change the state. | 283 // Helper method to change the state. |
| 272 void SetState(State new_state); | 284 void SetState(State new_state); |
| 273 | 285 |
| 286 // Helper method to tell us if MediaCodecBridge::Flush() doesn't work. |
| 287 bool CodecNeedsFlushWorkaround() const; |
| 288 |
| 274 // A helper function for logging. | 289 // A helper function for logging. |
| 275 static const char* AsString(State state); | 290 static const char* AsString(State state); |
| 276 | 291 |
| 277 // Used to post tasks. This class is single threaded and every method should | |
| 278 // run on this task runner. | |
| 279 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | |
| 280 | |
| 281 State state_; | 292 State state_; |
| 282 | 293 |
| 283 // The client that we notify about MediaCodec events. | 294 // The client that we notify about MediaCodec events. |
| 284 Client* client_; | 295 Client* client_; |
| 285 | 296 |
| 286 // The MediaCodec instance that we're using. | 297 // The MediaCodec instance that we're using. |
| 287 std::unique_ptr<MediaCodecBridge> media_codec_; | 298 std::unique_ptr<MediaCodecBridge> media_codec_; |
| 288 | 299 |
| 289 // Repeating timer that kicks MediaCodec operation. | 300 // Repeating timer that kicks MediaCodec operation. |
| 290 base::RepeatingTimer io_timer_; | 301 base::RepeatingTimer io_timer_; |
| 291 | 302 |
| 292 // Time at which we last did useful work on |io_timer_|. | 303 // Time at which we last did useful work on |io_timer_|. |
| 293 base::TimeTicks idle_time_begin_; | 304 base::TimeTicks idle_time_begin_; |
| 294 | 305 |
| 295 // Index of the dequeued and filled buffer that we keep trying to enqueue. | 306 // Index of the dequeued and filled buffer that we keep trying to enqueue. |
| 296 // Such buffer appears in MEDIA_CODEC_NO_KEY processing. The -1 value means | 307 // Such buffer appears in MEDIA_CODEC_NO_KEY processing. The -1 value means |
| 297 // there is no such buffer. | 308 // there is no such buffer. |
| 298 int pending_input_buf_index_; | 309 int pending_input_buf_index_; |
| 299 | 310 |
| 300 // When processing a pending input buffer, this is the data that was returned | 311 // When processing a pending input buffer, this is the data that was returned |
| 301 // to us by the client. |memory| has been cleared, since the codec has it. | 312 // to us by the client. |memory| has been cleared, since the codec has it. |
| 302 InputData pending_input_buf_data_; | 313 InputData pending_input_buf_data_; |
| 303 | 314 |
| 315 // Optional clock for use during testing. It may be null. We do not maintain |
| 316 // ownership of it. |
| 317 base::TickClock* test_tick_clock_ = nullptr; |
| 318 |
| 319 // BuildInfo::sdk_int(), eventually. |
| 320 const int sdk_int_; |
| 321 |
| 304 // NOTE: Weak pointers must be invalidated before all other member variables. | 322 // NOTE: Weak pointers must be invalidated before all other member variables. |
| 305 base::WeakPtrFactory<MediaCodecLoop> weak_factory_; | 323 base::WeakPtrFactory<MediaCodecLoop> weak_factory_; |
| 306 | 324 |
| 307 DISALLOW_COPY_AND_ASSIGN(MediaCodecLoop); | 325 DISALLOW_COPY_AND_ASSIGN(MediaCodecLoop); |
| 308 }; | 326 }; |
| 309 | 327 |
| 310 } // namespace media | 328 } // namespace media |
| 311 | 329 |
| 312 #endif // MEDIA_BASE_ANDROID_MEDIA_CODEC_LOOP_H_ | 330 #endif // MEDIA_BASE_ANDROID_MEDIA_CODEC_LOOP_H_ |
| OLD | NEW |