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/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 { | |
watk
2016/06/09 02:04:34
I think I like this better if you drop the Info an
liberato (no reviews please)
2016/06/10 21:34:10
Done. also moved to protected since the client ne
| |
120 const int | |
DaleCurtis
2016/06/08 22:55:01
Odd formatting. Did clang-format do this?
liberato (no reviews please)
2016/06/10 21:34:09
yeah. i moved the comment above it, and now it's
| |
121 buf_index; // The codec input buffers are referred to by this index. | |
watk
2016/06/09 02:04:34
nit: "buf_" feels redundant now
liberato (no reviews please)
2016/06/10 21:34:09
Done.
| |
122 const bool is_pending; // True if we tried to enqueue this buffer before. | |
123 InputBufferInfo(int i, bool p) : buf_index(i), is_pending(p) {} | |
124 }; | |
125 | |
126 // Data that the client wants to put into an input buffer. | |
127 struct InputBufferData { | |
watk
2016/06/09 02:04:34
InputBufferInfo and InputBufferData are pretty sim
liberato (no reviews please)
2016/06/10 21:34:10
Done.
| |
128 InputBufferData(); | |
129 InputBufferData(const InputBufferData&); | |
130 InputBufferData(const uint8_t* m, size_t l, const DecodeCB& cb); | |
131 ~InputBufferData(); | |
watk
2016/06/09 02:04:34
You might be able to get away with the default con
liberato (no reviews please)
2016/06/10 21:34:09
i've removed the unused constructor. the copy con
| |
132 | |
133 const uint8_t* memory; | |
134 size_t length; | |
135 | |
136 std::string key_id; | |
137 std::string iv; | |
138 std::vector<SubsampleEntry> subsamples; | |
139 | |
140 base::TimeDelta presentation_time; | |
141 | |
142 // Called when this is queued. | |
143 DecodeCB completion_cb; | |
144 | |
145 bool is_eos; | |
146 bool is_encrypted; | |
147 }; | |
148 | |
149 // Information about the MediaCodec's output buffer. | |
watk
2016/06/09 02:04:33
Maybe: "Information about a MediaCodec output buff
liberato (no reviews please)
2016/06/10 21:34:10
Done.
| |
150 struct OutputBufferInfo { | |
151 int buf_index; // The codec output buffers are referred to by this index. | |
watk
2016/06/09 02:04:34
Same comment about "buf_"
liberato (no reviews please)
2016/06/10 21:34:09
Done.
| |
152 size_t offset; // Position in the buffer where data starts. | |
153 size_t size; // The size of the buffer (includes offset). | |
154 base::TimeDelta pts; // Presentation timestamp. | |
155 bool is_eos; // true if this buffer is the end of stream. | |
156 bool is_key_frame; // true if this buffer is a key frame. | |
watk
2016/06/09 02:04:33
These two comments should be capitalized. (Or dele
liberato (no reviews please)
2016/06/10 21:34:10
Caps: done. not sure about MediaCodecBridge.
| |
157 }; | |
158 | |
159 class Client { | |
watk
2016/06/09 02:04:34
Protected non-virtual destructor? And DISALLOW_COP
liberato (no reviews please)
2016/06/10 21:34:09
destructor: good idea. done.
DISALLOW_COPY_AND_AS
| |
160 public: | |
161 // Return true if and only if there is input that is pending to be | |
162 // queued with MediaCodec. ProvideInputData() will not be called more than | |
163 // once in response to this returning true once. It is not guaranteed that | |
164 // ProvideInputData will be called at all. | |
165 virtual bool IsAnyInputPending() = 0; | |
166 | |
167 // Fills and returns an input buffer for MediaCodecLoop to queue. It is | |
168 // assumed that IsAnyInputPending() has already verified that there is data | |
169 // to be queued. | |
watk
2016/06/09 02:04:34
Could be more explicit: "It is an error to call th
liberato (no reviews please)
2016/06/10 21:34:10
that's too strict, since IsAnyInputPending is not
| |
170 virtual InputBufferData ProvideInputData() = 0; | |
171 | |
172 // Called when an EOS buffer is dequeued from the output. | |
173 virtual void OnDecodedEos(const OutputBufferInfo& 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 DoIOTask when it does release it. | |
179 // If this returns false, then we transition to STATE_ERROR. | |
180 virtual bool OnDecodedFrame(const OutputBufferInfo& 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 OnError() = 0; | |
188 }; | |
189 | |
190 // Handy enum for "no buffer". | |
191 enum { kInvalidBufferIndex = -1 }; | |
192 | |
193 // We will take ownership of |media_codec|. We will not destroy it until | |
194 // we are destructed. | |
195 MediaCodecLoop(Client* client, | |
196 std::unique_ptr<MediaCodecBridge>&& media_codec); | |
197 ~MediaCodecLoop(); | |
198 | |
199 // Does the MediaCodec processing cycle: enqueues an input buffer, then | |
200 // dequeues output buffers. This should be called by the client when more | |
201 // work becomes available, such as when new input data arrives. If codec | |
202 // output buffers are freed after OnDecodedFrame returns, then this should | |
203 // also be called. | |
204 void DoIOTask(); | |
205 | |
206 // Try to flush this media codec. Returns true on success, false on failure. | |
207 // Failures can result in a state change to the Error state. If this returns | |
208 // false but the state is still READY, then the codec may continue to be used. | |
209 bool TryFlush(); | |
210 | |
211 // Callback called by the client when a new key is available. This allows | |
212 // decoding to resume if it was stopped in the WAITING_FOR_KEY state. | |
213 void OnKeyAdded(); | |
214 | |
215 // Return our codec. It is guaranteed that this will be non-null, if we were | |
216 // given a non-null codec during construction. | |
watk
2016/06/09 02:04:33
Can we explicitly require that media_codec is not
liberato (no reviews please)
2016/06/10 21:34:09
Done.
| |
217 MediaCodecBridge* GetCodec() const; | |
218 | |
219 protected: | |
220 // Possible states. | |
watk
2016/06/09 02:04:33
Comment seems redundant
liberato (no reviews please)
2016/06/10 21:34:10
Done.
| |
221 enum State { | |
222 STATE_READY, | |
223 STATE_WAITING_FOR_KEY, | |
224 STATE_DRAINING, | |
225 STATE_DRAINED, | |
226 STATE_ERROR, | |
227 }; | |
228 | |
229 // Enqueues pending input buffers into MediaCodec as long as it can happen | |
230 // without delay in dequeuing and enqueueing input buffers. | |
231 // Returns true if any input was processed. | |
232 bool QueueInput(); | |
233 | |
234 // Enqueues one pending input buffer into MediaCodec if MediaCodec has room. | |
235 // Returns true if any input was processed. | |
236 bool QueueOneInputBuffer(); | |
237 | |
238 // Get data for |input_info| from the client and queue it. | |
239 void EnqueueInputBuffer(const InputBufferInfo& input_info); | |
240 | |
241 // A helper method for QueueInput(). Dequeues an empty input buffer from the | |
242 // codec and returns the information about it. OutputBufferInfo.buf_index is | |
243 // the index of the dequeued buffer or -1 if the codec is busy or an error | |
244 // occured. OutputBufferInfo.is_pending is set to true if we tried to enqueue | |
245 // this buffer before. In this case the buffer is already filled with data. | |
246 // In the case of an error sets STATE_ERROR. | |
247 InputBufferInfo DequeueInputBuffer(); | |
248 | |
249 // Dequeues all output buffers from MediaCodec that are immediately available. | |
250 // Returns true if any output buffer was received from MediaCodec. | |
251 bool DequeueOutput(); | |
252 | |
253 // Start the timer immediately if |start| is true or stop it based on elapsed | |
254 // idle time if |start| is false. | |
255 void ManageTimer(bool start); | |
256 | |
257 // Helper method to change the state. | |
258 void SetState(State new_state); | |
259 | |
260 // A helper function for logging. | |
261 static const char* AsString(State state); | |
262 | |
263 // Used to post tasks. This class is single threaded and every method should | |
264 // run on this task runner. | |
265 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | |
266 | |
267 State state_; | |
268 | |
269 // The client that we notify about MediaCodec events. | |
270 Client* client_; | |
271 | |
272 // The MediaCodec instance that we're using. | |
273 std::unique_ptr<MediaCodecBridge> media_codec_; | |
274 | |
275 // Repeating timer that kicks MediaCodec operation. | |
276 base::RepeatingTimer io_timer_; | |
277 | |
278 // Time at which we last did useful work on |io_timer_|. | |
279 base::TimeTicks idle_time_begin_; | |
280 | |
281 // Index of the dequeued and filled buffer that we keep trying to enqueue. | |
282 // Such buffer appears in MEDIA_CODEC_NO_KEY processing. The -1 value means | |
283 // there is no such buffer. | |
284 int pending_input_buf_index_; | |
285 | |
286 // When processing a pending input bfufer, this is the data that was returned | |
287 // to us by the client. |memory| has been cleared, since the codec has it. | |
288 InputBufferData pending_input_buf_data_; | |
289 | |
290 // When an EOS is queued, we defer its completion callback until the EOS | |
291 // arrives at the output queue. This is valid when we're in STATE_DRAINING. | |
292 DecodeCB pending_eos_completion_cb_; | |
293 | |
294 base::WeakPtrFactory<MediaCodecLoop> weak_factory_; | |
295 | |
296 DISALLOW_COPY_AND_ASSIGN(MediaCodecLoop); | |
297 }; | |
298 | |
299 } // namespace media | |
300 | |
301 #endif // MEDIA_BASE_ANDROID_MEDIA_CODEC_LOOP_H_ | |
OLD | NEW |