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