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 | |
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. | |
watk
2016/06/15 00:29:18
The "similar to AVDA" comment will be very soon ou
liberato (no reviews please)
2016/06/15 20:27:30
Done.
| |
61 // | |
62 // This implementation detects the MediaCodec idle run (no input or output | |
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] | |
watk
2016/06/15 00:29:18
Should this be any state? (not only Ready)?
liberato (no reviews please)
2016/06/15 20:27:30
Done.
| |
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 // | | | |
watk
2016/06/15 00:29:17
Should we have a flushing state in this diagram?
liberato (no reviews please)
2016/06/15 20:27:30
it has draining / drained. is that what you mean?
| |
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 // Information about a MediaCodec output buffer. | |
140 struct OutputBuffer { | |
DaleCurtis
2016/06/14 19:29:03
Assign defaults?
liberato (no reviews please)
2016/06/14 21:56:45
thanks, meant to do it in the last one.
| |
141 int index; // The codec output buffers are referred to by this index. | |
142 size_t offset; // Position in the buffer where data starts. | |
143 size_t size; // The size of the buffer (includes offset). | |
144 base::TimeDelta pts; // Presentation timestamp. | |
145 bool is_eos; // True if this buffer is the end of stream. | |
146 bool is_key_frame; // True if this buffer is a key frame. | |
147 }; | |
148 | |
149 class Client { | |
150 public: | |
151 // Return true if and only if there is input that is pending to be | |
152 // queued with MediaCodec. ProvideInputData() will not be called more than | |
153 // once in response to this returning true once. It is not guaranteed that | |
154 // ProvideInputData will be called at all. | |
155 virtual bool IsAnyInputPending() const = 0; | |
156 | |
157 // Fills and returns an input buffer for MediaCodecLoop to queue. It is | |
158 // an error for MediaCodecLoop to call this while !IsAnyInputPending(). | |
159 virtual InputData ProvideInputData() = 0; | |
160 | |
161 // Called when an EOS buffer is dequeued from the output. | |
162 virtual void OnDecodedEos(const OutputBuffer& out) = 0; | |
163 | |
164 // Processes the output buffer after it comes from MediaCodec. The client | |
165 // has the responsibility to release the codec buffer, though it doesn't | |
166 // need to do so before this call returns. If it does not do so before | |
167 // returning, then the client must call DoIOTask when it does release it. | |
168 // If this returns false, then we transition to STATE_ERROR. | |
169 virtual bool OnDecodedFrame(const OutputBuffer& out) = 0; | |
170 | |
171 // Processes the output format change on |media_codec|. Returns true on | |
172 // success, or false to transition to the error state. | |
173 virtual bool OnOutputFormatChanged() = 0; | |
174 | |
175 // Notify the client when our state transitions to STATE_ERROR. | |
176 virtual void OnCodecLoopError() = 0; | |
177 | |
178 protected: | |
179 ~Client() {} | |
DaleCurtis
2016/06/14 19:29:03
virtual per style guide.
liberato (no reviews please)
2016/06/14 21:56:45
Done.
watk
2016/06/15 00:29:17
My bad Frank. I didn't realize the style guide say
| |
180 }; | |
181 | |
182 // Handy enum for "no buffer". | |
183 enum { kInvalidBufferIndex = -1 }; | |
184 | |
185 // We will take ownership of |media_codec|. We will not destroy it until | |
186 // we are destructed. |media_codec| may not be null. | |
187 MediaCodecLoop(Client* client, | |
188 std::unique_ptr<MediaCodecBridge>&& media_codec); | |
189 ~MediaCodecLoop(); | |
190 | |
191 // Does the MediaCodec processing cycle: enqueues an input buffer, then | |
192 // dequeues output buffers. This should be called by the client when more | |
193 // work becomes available, such as when new input data arrives. If codec | |
194 // output buffers are freed after OnDecodedFrame returns, then this should | |
195 // also be called. | |
196 void DoIOTask(); | |
watk
2016/06/15 00:29:17
Should we rename this now? I'm trying to think of
liberato (no reviews please)
2016/06/15 20:27:30
agree that it should be renamed. don't like 'star
| |
197 | |
198 // Try to flush this media codec. Returns true on success, false on failure. | |
199 // Failures can result in a state change to the Error state. If this returns | |
200 // false but the state is still READY, then the codec may continue to be used. | |
watk
2016/06/15 00:29:18
This can return false if the state is DRAINED, but
liberato (no reviews please)
2016/06/15 20:27:30
i don't think that the codec is still usable after
| |
201 bool TryFlush(); | |
202 | |
203 // Callback called by the client when a new key is available. This allows | |
watk
2016/06/15 00:29:17
Comment feels focused on the implementation rather
liberato (no reviews please)
2016/06/15 20:27:30
Done.
| |
204 // decoding to resume if it was stopped in the WAITING_FOR_KEY state. | |
205 void OnKeyAdded(); | |
206 | |
207 // Return our codec. It is guaranteed that this will be non-null. | |
watk
2016/06/15 00:29:18
Not technically true at the moment :) Maybe guaran
liberato (no reviews please)
2016/06/15 20:27:30
Done.
| |
208 MediaCodecBridge* GetCodec() const; | |
209 | |
210 protected: | |
211 enum State { | |
212 STATE_READY, | |
213 STATE_WAITING_FOR_KEY, | |
214 STATE_DRAINING, | |
215 STATE_DRAINED, | |
216 STATE_ERROR, | |
217 }; | |
218 | |
219 // Information about dequeued input buffer. | |
220 struct InputBuffer { | |
221 InputBuffer() : index(kInvalidBufferIndex), is_pending(false) {} | |
DaleCurtis
2016/06/14 19:29:03
Drop and use inline values instead?
liberato (no reviews please)
2016/06/14 21:56:45
Done.
| |
222 InputBuffer(int i, bool p) : index(i), is_pending(p) {} | |
223 | |
224 // The codec input buffers are referred to by this index. | |
225 int index; | |
226 bool is_pending; // True if we tried to enqueue this buffer before. | |
227 }; | |
228 | |
229 // Enqueues one pending input buffer into MediaCodec if MediaCodec has room, | |
230 // and if the client has any input to give us. | |
231 // Returns true if any input was processed. | |
232 bool ProcessOneInputBuffer(); | |
233 | |
234 // Get data for |input_buffer| from the client and queue it. | |
235 void EnqueueInputBuffer(const InputBuffer& input_buffer); | |
236 | |
237 // Dequeues an empty input buffer from the codec and returns the information | |
238 // about it. InputBuffer.index is the index of the dequeued buffer or -1 if | |
239 // the codec is busy or an error occured. InputBuffer.is_pending is set to | |
240 // true if we tried to enqueue this buffer before. In this case the buffer is | |
241 // already filled with data. | |
242 // In the case of an error sets STATE_ERROR. | |
243 InputBuffer DequeueInputBuffer(); | |
244 | |
245 // Dequeues one output buffer from MediaCodec if it is immediately available, | |
246 // and sends it to the client. | |
247 // Returns true if an output buffer was received from MediaCodec. | |
248 bool ProcessOneOutputBuffer(); | |
249 | |
250 // Start the timer immediately if |start| is true or stop it based on elapsed | |
251 // idle time if |start| is false. | |
252 void ManageTimer(bool start); | |
253 | |
254 // Helper method to change the state. | |
255 void SetState(State new_state); | |
256 | |
257 // A helper function for logging. | |
258 static const char* AsString(State state); | |
259 | |
260 // Used to post tasks. This class is single threaded and every method should | |
261 // run on this task runner. | |
262 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | |
263 | |
264 State state_; | |
265 | |
266 // The client that we notify about MediaCodec events. | |
267 Client* client_; | |
268 | |
269 // The MediaCodec instance that we're using. | |
270 std::unique_ptr<MediaCodecBridge> media_codec_; | |
271 | |
272 // Repeating timer that kicks MediaCodec operation. | |
273 base::RepeatingTimer io_timer_; | |
274 | |
275 // Time at which we last did useful work on |io_timer_|. | |
276 base::TimeTicks idle_time_begin_; | |
277 | |
278 // Index of the dequeued and filled buffer that we keep trying to enqueue. | |
279 // Such buffer appears in MEDIA_CODEC_NO_KEY processing. The -1 value means | |
280 // there is no such buffer. | |
281 int pending_input_buf_index_; | |
282 | |
283 // When processing a pending input bfufer, this is the data that was returned | |
watk
2016/06/15 00:29:18
bfufer
liberato (no reviews please)
2016/06/15 20:27:30
Dnoe.
watk
2016/06/15 20:52:11
s/Dnoe/Done :)
| |
284 // to us by the client. |memory| has been cleared, since the codec has it. | |
285 InputData pending_input_buf_data_; | |
286 | |
287 // When an EOS is queued, we defer its completion callback until the EOS | |
288 // arrives at the output queue. This is valid when we're in STATE_DRAINING. | |
289 DecodeCB pending_eos_completion_cb_; | |
290 | |
291 base::WeakPtrFactory<MediaCodecLoop> weak_factory_; | |
DaleCurtis
2016/06/14 19:29:03
Annotate with comment indicating this must always
liberato (no reviews please)
2016/06/14 21:56:44
done.
also, i think clang now yells at you if it
| |
292 | |
293 DISALLOW_COPY_AND_ASSIGN(MediaCodecLoop); | |
294 }; | |
295 | |
296 } // namespace media | |
297 | |
298 #endif // MEDIA_BASE_ANDROID_MEDIA_CODEC_LOOP_H_ | |
OLD | NEW |