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