Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(261)

Side by Side Diff: media/base/android/media_codec_loop.h

Issue 2016213003: Separate MediaCodecLoop from MediaCodecAudioDecoder (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: cl feedback. Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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/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 // Data that the client wants to put into an input buffer.
119 struct InputData {
DaleCurtis 2016/06/13 23:16:13 Structs without default values make me nervous; es
liberato (no reviews please) 2016/06/14 17:26:45 Done.
120 InputData();
121 InputData(const InputData&);
122 ~InputData();
123
124 const uint8_t* memory;
125 size_t length;
126
127 std::string key_id;
128 std::string iv;
129 std::vector<SubsampleEntry> subsamples;
130
131 base::TimeDelta presentation_time;
132
133 // Called when this is queued.
134 DecodeCB completion_cb;
135
136 bool is_eos;
137 bool is_encrypted;
138 };
139
140 // Information about a MediaCodec output buffer.
141 struct OutputBuffer {
142 int index; // The codec output buffers are referred to by this index.
143 size_t offset; // Position in the buffer where data starts.
144 size_t size; // The size of the buffer (includes offset).
145 base::TimeDelta pts; // Presentation timestamp.
146 bool is_eos; // True if this buffer is the end of stream.
147 bool is_key_frame; // True if this buffer is a key frame.
148 };
149
150 class Client {
151 protected:
DaleCurtis 2016/06/13 23:16:13 public is generally first in chromium code.
liberato (no reviews please) 2016/06/14 17:26:45 Done.
152 ~Client() {}
153
154 public:
155 // Return true if and only if there is input that is pending to be
156 // queued with MediaCodec. ProvideInputData() will not be called more than
157 // once in response to this returning true once. It is not guaranteed that
158 // ProvideInputData will be called at all.
159 virtual bool IsAnyInputPending() const = 0;
160
161 // Fills and returns an input buffer for MediaCodecLoop to queue. It is
162 // an error for MediaCodecLoop to call this while !IsAnyInputPending().
163 virtual InputData ProvideInputData() = 0;
164
165 // Called when an EOS buffer is dequeued from the output.
166 virtual void OnDecodedEos(const OutputBuffer& out) = 0;
167
168 // Processes the output buffer after it comes from MediaCodec. The client
169 // has the responsibility to release the codec buffer, though it doesn't
170 // need to do so before this call returns. If it does not do so before
171 // returning, then the client must call DoIOTask when it does release it.
172 // If this returns false, then we transition to STATE_ERROR.
173 virtual bool OnDecodedFrame(const OutputBuffer& out) = 0;
174
175 // Processes the output format change on |media_codec|. Returns true on
176 // success, or false to transition to the error state.
177 virtual bool OnOutputFormatChanged() = 0;
178
179 // Notify the client when our state transitions to STATE_ERROR.
180 virtual void OnCodecLoopError() = 0;
181 };
182
183 // Handy enum for "no buffer".
184 enum { kInvalidBufferIndex = -1 };
185
186 // We will take ownership of |media_codec|. We will not destroy it until
187 // we are destructed. |media_codec| may not be null.
188 MediaCodecLoop(Client* client,
189 std::unique_ptr<MediaCodecBridge>&& media_codec);
190 ~MediaCodecLoop();
191
192 // Does the MediaCodec processing cycle: enqueues an input buffer, then
193 // dequeues output buffers. This should be called by the client when more
194 // work becomes available, such as when new input data arrives. If codec
195 // output buffers are freed after OnDecodedFrame returns, then this should
196 // also be called.
197 void DoIOTask();
198
199 // Try to flush this media codec. Returns true on success, false on failure.
200 // Failures can result in a state change to the Error state. If this returns
201 // false but the state is still READY, then the codec may continue to be used.
202 bool TryFlush();
203
204 // Callback called by the client when a new key is available. This allows
205 // decoding to resume if it was stopped in the WAITING_FOR_KEY state.
206 void OnKeyAdded();
207
208 // Return our codec. It is guaranteed that this will be non-null.
209 MediaCodecBridge* GetCodec() const;
210
211 protected:
212 enum State {
213 STATE_READY,
214 STATE_WAITING_FOR_KEY,
215 STATE_DRAINING,
216 STATE_DRAINED,
217 STATE_ERROR,
218 };
219
220 // Information about dequeued input buffer.
221 struct InputBuffer {
222 // The codec input buffers are referred to by this index.
223 const int index;
224 const bool is_pending; // True if we tried to enqueue this buffer before.
225 InputBuffer(int i, bool p) : index(i), is_pending(p) {}
DaleCurtis 2016/06/13 23:16:13 Constructor at the top.
liberato (no reviews please) 2016/06/14 17:26:45 Done.
226 };
227
228 // Enqueues one pending input buffer into MediaCodec if MediaCodec has room,
229 // and if the client has any input to give us.
230 // Returns true if any input was processed.
231 bool ProcessOneInputBuffer();
232
233 // Get data for |input_buffer| from the client and queue it.
234 void EnqueueInputBuffer(const InputBuffer& input_buffer);
235
236 // Dequeues an empty input buffer from the codec and returns the information
237 // about it. InputBuffer.index is the index of the dequeued buffer or -1 if
238 // the codec is busy or an error occured. InputBuffer.is_pending is set to
239 // true if we tried to enqueue this buffer before. In this case the buffer is
240 // already filled with data.
241 // In the case of an error sets STATE_ERROR.
242 InputBuffer DequeueInputBuffer();
243
244 // Dequeues one output buffer from MediaCodec if it is immediately available,
245 // and sends it to the client.
246 // Returns true if an output buffer was received from MediaCodec.
247 bool ProcessOneOutputBuffer();
248
249 // Start the timer immediately if |start| is true or stop it based on elapsed
250 // idle time if |start| is false.
251 void ManageTimer(bool start);
252
253 // Helper method to change the state.
254 void SetState(State new_state);
255
256 // A helper function for logging.
257 static const char* AsString(State state);
258
259 // Used to post tasks. This class is single threaded and every method should
260 // run on this task runner.
261 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
262
263 State state_;
264
265 // The client that we notify about MediaCodec events.
266 Client* client_;
267
268 // The MediaCodec instance that we're using.
269 std::unique_ptr<MediaCodecBridge> media_codec_;
270
271 // Repeating timer that kicks MediaCodec operation.
272 base::RepeatingTimer io_timer_;
273
274 // Time at which we last did useful work on |io_timer_|.
275 base::TimeTicks idle_time_begin_;
276
277 // Index of the dequeued and filled buffer that we keep trying to enqueue.
278 // Such buffer appears in MEDIA_CODEC_NO_KEY processing. The -1 value means
279 // there is no such buffer.
280 int pending_input_buf_index_;
281
282 // When processing a pending input bfufer, this is the data that was returned
283 // to us by the client. |memory| has been cleared, since the codec has it.
284 InputData pending_input_buf_data_;
285
286 // When an EOS is queued, we defer its completion callback until the EOS
287 // arrives at the output queue. This is valid when we're in STATE_DRAINING.
288 DecodeCB pending_eos_completion_cb_;
289
290 base::WeakPtrFactory<MediaCodecLoop> weak_factory_;
291
292 DISALLOW_COPY_AND_ASSIGN(MediaCodecLoop);
293 };
294
295 } // namespace media
296
297 #endif // MEDIA_BASE_ANDROID_MEDIA_CODEC_LOOP_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698