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, | |
196 std::unique_ptr<MediaCodecBridge>&& media_codec); | |
Tima Vaisburd
2016/06/16 18:41:27
It seems passing unique_ptr by value is more idiom
liberato (no reviews please)
2016/06/27 16:13:37
good -- i was really confused why it wasn't workin
| |
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 DoPendingWork(); | |
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 // This should be called when a new key is added. Decoding will resume if it | |
212 // was stopped in the WAITING_FOR_KEY state. | |
213 void OnKeyAdded(); | |
214 | |
215 // Return our codec, if we have one. | |
216 MediaCodecBridge* GetCodec() const; | |
217 | |
218 protected: | |
219 enum State { | |
220 STATE_READY, | |
221 STATE_WAITING_FOR_KEY, | |
222 STATE_DRAINING, | |
223 STATE_DRAINED, | |
224 STATE_ERROR, | |
225 }; | |
226 | |
227 // Information about dequeued input buffer. | |
228 struct InputBuffer { | |
229 InputBuffer() {} | |
230 InputBuffer(int i, bool p) : index(i), is_pending(p) {} | |
231 | |
232 // The codec input buffers are referred to by this index. | |
233 int index = kInvalidBufferIndex; | |
234 | |
235 // True if we tried to enqueue this buffer before. | |
236 bool is_pending = false; | |
237 }; | |
238 | |
239 // Enqueues one pending input buffer into MediaCodec if MediaCodec has room, | |
240 // and if the client has any input to give us. | |
241 // Returns true if any input was processed. | |
242 bool ProcessOneInputBuffer(); | |
243 | |
244 // Get data for |input_buffer| from the client and queue it. | |
245 void EnqueueInputBuffer(const InputBuffer& input_buffer); | |
246 | |
247 // Dequeues an empty input buffer from the codec and returns the information | |
248 // about it. InputBuffer.index is the index of the dequeued buffer or -1 if | |
249 // the codec is busy or an error occured. InputBuffer.is_pending is set to | |
250 // true if we tried to enqueue this buffer before. In this case the buffer is | |
251 // already filled with data. | |
252 // In the case of an error sets STATE_ERROR. | |
253 InputBuffer DequeueInputBuffer(); | |
254 | |
255 // Dequeues one output buffer from MediaCodec if it is immediately available, | |
256 // and sends it to the client. | |
257 // Returns true if an output buffer was received from MediaCodec. | |
258 bool ProcessOneOutputBuffer(); | |
259 | |
260 // Start the timer immediately if |start| is true or stop it based on elapsed | |
261 // idle time if |start| is false. | |
262 void ManageTimer(bool start); | |
263 | |
264 // Helper method to change the state. | |
265 void SetState(State new_state); | |
266 | |
267 // A helper function for logging. | |
268 static const char* AsString(State state); | |
269 | |
270 // Used to post tasks. This class is single threaded and every method should | |
271 // run on this task runner. | |
272 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | |
273 | |
274 State state_; | |
275 | |
276 // The client that we notify about MediaCodec events. | |
277 Client* client_; | |
278 | |
279 // The MediaCodec instance that we're using. | |
280 std::unique_ptr<MediaCodecBridge> media_codec_; | |
281 | |
282 // Repeating timer that kicks MediaCodec operation. | |
283 base::RepeatingTimer io_timer_; | |
284 | |
285 // Time at which we last did useful work on |io_timer_|. | |
286 base::TimeTicks idle_time_begin_; | |
287 | |
288 // Index of the dequeued and filled buffer that we keep trying to enqueue. | |
289 // Such buffer appears in MEDIA_CODEC_NO_KEY processing. The -1 value means | |
290 // there is no such buffer. | |
291 int pending_input_buf_index_; | |
292 | |
293 // When processing a pending input buffer, this is the data that was returned | |
294 // to us by the client. |memory| has been cleared, since the codec has it. | |
295 InputData pending_input_buf_data_; | |
296 | |
297 // When an EOS is queued, we defer its completion callback until the EOS | |
298 // arrives at the output queue. This is valid when we're in STATE_DRAINING. | |
299 DecodeCB pending_eos_completion_cb_; | |
300 | |
301 // NOTE: Weak pointers must be invalidated before all other member variables. | |
302 base::WeakPtrFactory<MediaCodecLoop> weak_factory_; | |
303 | |
304 DISALLOW_COPY_AND_ASSIGN(MediaCodecLoop); | |
305 }; | |
306 | |
307 } // namespace media | |
308 | |
309 #endif // MEDIA_BASE_ANDROID_MEDIA_CODEC_LOOP_H_ | |
OLD | NEW |