Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef MEDIA_BASE_ANDROID_MEDIA_CODEC_LOOP_H_ | |
| 6 #define MEDIA_BASE_ANDROID_MEDIA_CODEC_LOOP_H_ | |
| 7 | |
| 8 #include <deque> | |
| 9 #include <memory> | |
| 10 #include <utility> | |
| 11 #include <vector> | |
| 12 | |
| 13 #include "base/macros.h" | |
| 14 #include "base/memory/weak_ptr.h" | |
| 15 #include "base/time/time.h" | |
| 16 #include "base/timer/timer.h" | |
| 17 #include "media/base/android/media_codec_bridge.h" | |
| 18 #include "media/base/audio_decoder.h" | |
| 19 #include "media/base/audio_decoder_config.h" | |
| 20 #include "media/base/media_export.h" | |
| 21 | |
| 22 // MediaCodecLoop is based on Android's MediaCodec API. | |
| 23 // 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 // One MediaCodecLoop instance owns a single MediaCodec(Bridge) instance, and | |
| 26 // 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 // example is AndroidVideoDecodeAccelerator. | |
| 29 | |
| 30 // Implementation notes. | |
| 31 // | |
| 32 // The MediaCodec | |
| 33 // (http://developer.android.com/reference/android/media/MediaCodec.html) works | |
|
watk
2016/06/15 00:29:18
nit: you can probably delete this url. I doubt any
liberato (no reviews please)
2016/06/15 20:27:30
Done.
| |
| 34 // by exchanging buffers between the client and the codec itself. On the input | |
| 35 // side an "empty" buffer has to be dequeued from the codec, filled with data | |
| 36 // and queued back. On the output side a "full" buffer with data should be | |
| 37 // dequeued, the data is to be used somehow (copied out, or rendered to a pre- | |
| 38 // defined texture for video) and the buffer has to be returned back (released). | |
| 39 // Neither input nor output dequeue operations are guaranteed to succeed: the | |
| 40 // codec might not have available input buffers yet, or not every encoded buffer | |
| 41 // has arrived to complete an output frame. In such case the client should try | |
| 42 // to dequeue a buffer again at a later time. | |
| 43 // | |
| 44 // There is also a special situation related to an encrypted stream, where the | |
| 45 // enqueuing of a filled input buffer might fail due to lack of the relevant key | |
| 46 // in the CDM module. While MediaCodecLoop does not handle the CDM directly, | |
| 47 // it does understand the codec state. | |
| 48 // | |
| 49 // Much of that logic is common to all users of MediaCodec. The MediaCodecLoop | |
| 50 // provides the main driver loop to talk to MediaCodec. Via the Client | |
| 51 // interface, MediaCodecLoop can request input data, send output buffers, etc. | |
| 52 // MediaCodecLoop does not construct a MediaCodec, but instead takes ownership | |
| 53 // of one that it provided during construction. | |
| 54 // | |
| 55 // Although one can specify a delay in the MediaCodec's dequeue operations, | |
| 56 // this implementation follows the simple logic which is similar to | |
| 57 // AndroidVideoDecodeAccelerator: no delay for either input or output buffers, | |
| 58 // the processing is initated by the timer with short period (10 ms). Using no | |
| 59 // delay for enqueue operations has an extra benefit of not blocking the current | |
| 60 // thread. | |
| 61 // | |
| 62 // This implementation detects the MediaCodec idle run (no input or output | |
|
watk
2016/06/15 00:29:18
"detects the MediaCodec idle run" could be made cl
liberato (no reviews please)
2016/06/15 20:27:30
Done.
| |
| 63 // buffer processing) and after being idle for a predefined time the timer | |
| 64 // stops. The client is responsible for signalling us to wake up via a call | |
| 65 // to DoIOTask. It is okay for the client to call this even when the timer is | |
| 66 // already running. | |
| 67 // | |
| 68 // The current implementation is single threaded. Every method is supposed to | |
| 69 // run on the same thread. | |
| 70 // | |
| 71 // State diagram. | |
| 72 // | |
| 73 // [Ready] | |
| 74 // | | |
| 75 // (MediaCodec error) | |
| 76 // | | |
| 77 // [Error] | |
| 78 // | |
| 79 // [Ready] | |
| 80 // | | |
| 81 // (EOS enqueued) | |
| 82 // | | |
| 83 // [Draining] | |
| 84 // | | |
| 85 // (EOS dequeued on output) | |
| 86 // | | |
| 87 // [Drained] | |
| 88 // | |
| 89 // [Ready] | |
| 90 // | | |
| 91 // (MEDIA_CODEC_NO_KEY) | |
| 92 // | | |
| 93 // [WaitingForKey] | |
| 94 // | | |
| 95 // (OnKeyAdded) | |
| 96 // | | |
| 97 // [Ready] | |
| 98 // | |
| 99 // -[Any state]- | |
| 100 // | | | |
| 101 // (Flush ok) (Flush fails) | |
| 102 // | | | |
| 103 // [Ready] [Error] | |
| 104 | |
| 105 namespace base { | |
| 106 class SingleThreadTaskRunner; | |
| 107 } | |
| 108 | |
| 109 namespace media { | |
| 110 | |
| 111 class MEDIA_EXPORT MediaCodecLoop { | |
| 112 public: | |
| 113 // TODO(liberato): this exists in video_decoder.h and audio_decoder.h too. | |
| 114 using InitCB = base::Callback<void(bool success)>; | |
| 115 using DecodeCB = base::Callback<void(DecodeStatus status)>; | |
| 116 | |
| 117 // Data that the client wants to put into an input buffer. | |
| 118 struct InputData { | |
| 119 InputData(); | |
| 120 InputData(const InputData&); | |
| 121 ~InputData(); | |
| 122 | |
| 123 const uint8_t* memory = nullptr; | |
| 124 size_t length = 0; | |
| 125 | |
| 126 std::string key_id; | |
| 127 std::string iv; | |
| 128 std::vector<SubsampleEntry> subsamples; | |
| 129 | |
| 130 base::TimeDelta presentation_time; | |
| 131 | |
| 132 // Called when this is queued. | |
| 133 DecodeCB completion_cb; | |
| 134 | |
| 135 bool is_eos = false; | |
| 136 bool is_encrypted = false; | |
| 137 }; | |
| 138 | |
| 139 // Handy enum for "no buffer". | |
| 140 enum { kInvalidBufferIndex = -1 }; | |
| 141 | |
| 142 // Information about a MediaCodec output buffer. | |
| 143 struct OutputBuffer { | |
| 144 // The codec output buffers are referred to by this index. | |
| 145 int index = kInvalidBufferIndex; | |
| 146 | |
| 147 // Position in the buffer where data starts. | |
| 148 size_t offset = 0; | |
| 149 | |
| 150 // The size of the buffer (includes offset). | |
| 151 size_t size = 0; | |
| 152 | |
| 153 // Presentation timestamp. | |
| 154 base::TimeDelta pts; | |
| 155 | |
| 156 // True if this buffer is the end of stream. | |
| 157 bool is_eos = false; | |
| 158 | |
| 159 // True if this buffer is a key frame. | |
| 160 bool is_key_frame = false; | |
| 161 }; | |
| 162 | |
| 163 class Client { | |
| 164 public: | |
| 165 // Return true if and only if there is input that is pending to be | |
| 166 // queued with MediaCodec. ProvideInputData() will not be called more than | |
| 167 // once in response to this returning true once. It is not guaranteed that | |
| 168 // ProvideInputData will be called at all. | |
| 169 virtual bool IsAnyInputPending() const = 0; | |
| 170 | |
| 171 // Fills and returns an input buffer for MediaCodecLoop to queue. It is | |
| 172 // an error for MediaCodecLoop to call this while !IsAnyInputPending(). | |
| 173 virtual InputData ProvideInputData() = 0; | |
| 174 | |
| 175 // Called when an EOS buffer is dequeued from the output. | |
| 176 virtual void OnDecodedEos(const OutputBuffer& out) = 0; | |
| 177 | |
| 178 // Processes the output buffer after it comes from MediaCodec. The client | |
| 179 // has the responsibility to release the codec buffer, though it doesn't | |
| 180 // need to do so before this call returns. If it does not do so before | |
| 181 // returning, then the client must call DoIOTask when it does release it. | |
| 182 // If this returns false, then we transition to STATE_ERROR. | |
| 183 virtual bool OnDecodedFrame(const OutputBuffer& out) = 0; | |
| 184 | |
| 185 // Processes the output format change on |media_codec|. Returns true on | |
| 186 // success, or false to transition to the error state. | |
| 187 virtual bool OnOutputFormatChanged() = 0; | |
| 188 | |
| 189 // Notify the client when our state transitions to STATE_ERROR. | |
| 190 virtual void OnCodecLoopError() = 0; | |
| 191 | |
| 192 protected: | |
| 193 virtual ~Client() {} | |
| 194 }; | |
| 195 | |
| 196 // We will take ownership of |media_codec|. We will not destroy it until | |
| 197 // we are destructed. |media_codec| may not be null. | |
| 198 MediaCodecLoop(Client* client, | |
| 199 std::unique_ptr<MediaCodecBridge>&& media_codec); | |
| 200 ~MediaCodecLoop(); | |
| 201 | |
| 202 // Does the MediaCodec processing cycle: enqueues an input buffer, then | |
| 203 // dequeues output buffers. This should be called by the client when more | |
| 204 // work becomes available, such as when new input data arrives. If codec | |
| 205 // output buffers are freed after OnDecodedFrame returns, then this should | |
| 206 // also be called. | |
| 207 void DoIOTask(); | |
| 208 | |
| 209 // Try to flush this media codec. Returns true on success, false on failure. | |
| 210 // Failures can result in a state change to the Error state. If this returns | |
| 211 // false but the state is still READY, then the codec may continue to be used. | |
| 212 bool TryFlush(); | |
| 213 | |
| 214 // Callback called by the client when a new key is available. This allows | |
| 215 // decoding to resume if it was stopped in the WAITING_FOR_KEY state. | |
| 216 void OnKeyAdded(); | |
| 217 | |
| 218 // Return our codec. It is guaranteed that this will be non-null. | |
| 219 MediaCodecBridge* GetCodec() const; | |
| 220 | |
| 221 protected: | |
| 222 enum State { | |
| 223 STATE_READY, | |
| 224 STATE_WAITING_FOR_KEY, | |
| 225 STATE_DRAINING, | |
| 226 STATE_DRAINED, | |
| 227 STATE_ERROR, | |
| 228 }; | |
| 229 | |
| 230 // Information about dequeued input buffer. | |
| 231 struct InputBuffer { | |
| 232 InputBuffer() {} | |
| 233 InputBuffer(int i, bool p) : index(i), is_pending(p) {} | |
| 234 | |
| 235 // The codec input buffers are referred to by this index. | |
| 236 int index = kInvalidBufferIndex; | |
| 237 | |
| 238 // True if we tried to enqueue this buffer before. | |
| 239 bool is_pending = false; | |
| 240 }; | |
| 241 | |
| 242 // Enqueues one pending input buffer into MediaCodec if MediaCodec has room, | |
| 243 // and if the client has any input to give us. | |
| 244 // Returns true if any input was processed. | |
| 245 bool ProcessOneInputBuffer(); | |
| 246 | |
| 247 // Get data for |input_buffer| from the client and queue it. | |
| 248 void EnqueueInputBuffer(const InputBuffer& input_buffer); | |
| 249 | |
| 250 // Dequeues an empty input buffer from the codec and returns the information | |
| 251 // about it. InputBuffer.index is the index of the dequeued buffer or -1 if | |
| 252 // the codec is busy or an error occured. InputBuffer.is_pending is set to | |
| 253 // true if we tried to enqueue this buffer before. In this case the buffer is | |
| 254 // already filled with data. | |
| 255 // In the case of an error sets STATE_ERROR. | |
| 256 InputBuffer DequeueInputBuffer(); | |
| 257 | |
| 258 // Dequeues one output buffer from MediaCodec if it is immediately available, | |
| 259 // and sends it to the client. | |
| 260 // Returns true if an output buffer was received from MediaCodec. | |
| 261 bool ProcessOneOutputBuffer(); | |
| 262 | |
| 263 // Start the timer immediately if |start| is true or stop it based on elapsed | |
| 264 // idle time if |start| is false. | |
| 265 void ManageTimer(bool start); | |
| 266 | |
| 267 // Helper method to change the state. | |
| 268 void SetState(State new_state); | |
| 269 | |
| 270 // A helper function for logging. | |
| 271 static const char* AsString(State state); | |
| 272 | |
| 273 // Used to post tasks. This class is single threaded and every method should | |
| 274 // run on this task runner. | |
| 275 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | |
| 276 | |
| 277 State state_; | |
| 278 | |
| 279 // The client that we notify about MediaCodec events. | |
| 280 Client* client_; | |
| 281 | |
| 282 // The MediaCodec instance that we're using. | |
| 283 std::unique_ptr<MediaCodecBridge> media_codec_; | |
| 284 | |
| 285 // Repeating timer that kicks MediaCodec operation. | |
| 286 base::RepeatingTimer io_timer_; | |
| 287 | |
| 288 // Time at which we last did useful work on |io_timer_|. | |
| 289 base::TimeTicks idle_time_begin_; | |
| 290 | |
| 291 // Index of the dequeued and filled buffer that we keep trying to enqueue. | |
| 292 // Such buffer appears in MEDIA_CODEC_NO_KEY processing. The -1 value means | |
| 293 // there is no such buffer. | |
| 294 int pending_input_buf_index_; | |
| 295 | |
| 296 // When processing a pending input bfufer, this is the data that was returned | |
| 297 // to us by the client. |memory| has been cleared, since the codec has it. | |
| 298 InputData pending_input_buf_data_; | |
| 299 | |
| 300 // When an EOS is queued, we defer its completion callback until the EOS | |
| 301 // arrives at the output queue. This is valid when we're in STATE_DRAINING. | |
| 302 DecodeCB pending_eos_completion_cb_; | |
| 303 | |
| 304 // NOTE: Weak pointers must be invalidated before all other member variables. | |
| 305 base::WeakPtrFactory<MediaCodecLoop> weak_factory_; | |
| 306 | |
| 307 DISALLOW_COPY_AND_ASSIGN(MediaCodecLoop); | |
| 308 }; | |
| 309 | |
| 310 } // namespace media | |
| 311 | |
| 312 #endif // MEDIA_BASE_ANDROID_MEDIA_CODEC_LOOP_H_ | |
| OLD | NEW |