1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package org.mozilla.gecko.media;
import android.media.MediaCodec;
import android.media.MediaFormat;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.Surface;
import java.io.IOException;
import java.nio.ByteBuffer;
// Implement async API using MediaCodec sync mode (API v16).
// This class uses internal worker thread/handler (mBufferPoller) to poll
// input and output buffer and notifies the client through callbacks.
final class JellyBeanAsyncCodec implements AsyncCodec {
private static final String LOGTAG = "GeckoAsyncCodecAPIv16";
private static final boolean DEBUG = false;
private static final int ERROR_CODEC = -10000;
private abstract class CancelableHandler extends Handler {
private static final int MSG_CANCELLATION = 0x434E434C; // 'CNCL'
protected CancelableHandler(Looper looper) {
super(looper);
}
protected void cancel() {
removeCallbacksAndMessages(null);
sendEmptyMessage(MSG_CANCELLATION);
// Wait until handleMessageLocked() is done.
synchronized (this) { }
}
protected boolean isCanceled() {
return hasMessages(MSG_CANCELLATION);
}
// Subclass should implement this and return true if it handles msg.
// Warning: Never, ever call super.handleMessage() in this method!
protected abstract boolean handleMessageLocked(Message msg);
public final void handleMessage(Message msg) {
// Block cancel() during handleMessageLocked().
synchronized (this) {
if (isCanceled() || handleMessageLocked(msg)) {
return;
}
}
switch (msg.what) {
case MSG_CANCELLATION:
// Just a marker. Nothing to do here.
if (DEBUG) Log.d(LOGTAG, "handler " + this + " done cancellation, codec=" + JellyBeanAsyncCodec.this);
break;
default:
super.handleMessage(msg);
break;
}
}
}
// A handler to invoke AsyncCodec.Callbacks methods.
private final class CallbackSender extends CancelableHandler {
private static final int MSG_INPUT_BUFFER_AVAILABLE = 1;
private static final int MSG_OUTPUT_BUFFER_AVAILABLE = 2;
private static final int MSG_OUTPUT_FORMAT_CHANGE = 3;
private static final int MSG_ERROR = 4;
private Callbacks mCallbacks;
private CallbackSender(Looper looper, Callbacks callbacks) {
super(looper);
mCallbacks = callbacks;
}
public void notifyInputBuffer(int index) {
if (isCanceled()) {
return;
}
Message msg = obtainMessage(MSG_INPUT_BUFFER_AVAILABLE);
msg.arg1 = index;
processMessage(msg);
}
private void processMessage(Message msg) {
if (Looper.myLooper() == getLooper()) {
handleMessage(msg);
} else {
sendMessage(msg);
}
}
public void notifyOutputBuffer(int index, MediaCodec.BufferInfo info) {
if (isCanceled()) {
return;
}
Message msg = obtainMessage(MSG_OUTPUT_BUFFER_AVAILABLE, info);
msg.arg1 = index;
processMessage(msg);
}
public void notifyOutputFormat(MediaFormat format) {
if (isCanceled()) {
return;
}
processMessage(obtainMessage(MSG_OUTPUT_FORMAT_CHANGE, format));
}
public void notifyError(int result) {
Log.e(LOGTAG, "codec error:" + result);
processMessage(obtainMessage(MSG_ERROR, result, 0));
}
protected boolean handleMessageLocked(Message msg) {
switch (msg.what) {
case MSG_INPUT_BUFFER_AVAILABLE: // arg1: buffer index.
mCallbacks.onInputBufferAvailable(JellyBeanAsyncCodec.this,
msg.arg1);
break;
case MSG_OUTPUT_BUFFER_AVAILABLE: // arg1: buffer index, obj: info.
mCallbacks.onOutputBufferAvailable(JellyBeanAsyncCodec.this,
msg.arg1,
(MediaCodec.BufferInfo)msg.obj);
break;
case MSG_OUTPUT_FORMAT_CHANGE: // obj: output format.
mCallbacks.onOutputFormatChanged(JellyBeanAsyncCodec.this,
(MediaFormat)msg.obj);
break;
case MSG_ERROR: // arg1: error code.
mCallbacks.onError(JellyBeanAsyncCodec.this, msg.arg1);
break;
default:
return false;
}
return true;
}
}
// Handler to poll input and output buffers using dequeue(Input|Output)Buffer(),
// with 10ms time-out. Once triggered and successfully gets a buffer, it
// will schedule next polling until EOS or failure. To prevent it from
// automatically polling more buffer, use cancel() it inherits from
// CancelableHandler.
private final class BufferPoller extends CancelableHandler {
private static final int MSG_POLL_INPUT_BUFFERS = 1;
private static final int MSG_POLL_OUTPUT_BUFFERS = 2;
private static final long DEQUEUE_TIMEOUT_US = 10000;
public BufferPoller(Looper looper) {
super(looper);
}
private void schedulePollingIfNotCanceled(int what) {
if (isCanceled()) {
return;
}
schedulePolling(what);
}
private void schedulePolling(int what) {
if (needsBuffer(what)) {
sendEmptyMessage(what);
}
}
private boolean needsBuffer(int what) {
if (mOutputEnded && (what == MSG_POLL_OUTPUT_BUFFERS)) {
return false;
}
if (mInputEnded && (what == MSG_POLL_INPUT_BUFFERS)) {
return false;
}
return true;
}
protected boolean handleMessageLocked(Message msg) {
try {
switch (msg.what) {
case MSG_POLL_INPUT_BUFFERS:
pollInputBuffer();
break;
case MSG_POLL_OUTPUT_BUFFERS:
pollOutputBuffer();
break;
default:
return false;
}
} catch (IllegalStateException e) {
e.printStackTrace();
mCallbackSender.notifyError(ERROR_CODEC);
}
return true;
}
private void pollInputBuffer() {
int result = mCodec.dequeueInputBuffer(DEQUEUE_TIMEOUT_US);
if (result >= 0) {
mCallbackSender.notifyInputBuffer(result);
schedulePollingIfNotCanceled(BufferPoller.MSG_POLL_INPUT_BUFFERS);
} else if (result != MediaCodec.INFO_TRY_AGAIN_LATER) {
mCallbackSender.notifyError(result);
}
}
private void pollOutputBuffer() {
boolean dequeueMoreBuffer = true;
MediaCodec.BufferInfo info = new MediaCodec.BufferInfo();
int result = mCodec.dequeueOutputBuffer(info, DEQUEUE_TIMEOUT_US);
if (result >= 0) {
if ((info.flags & MediaCodec.BUFFER_FLAG_END_OF_STREAM) != 0) {
mOutputEnded = true;
}
mCallbackSender.notifyOutputBuffer(result, info);
if (!hasMessages(MSG_POLL_INPUT_BUFFERS)) {
schedulePollingIfNotCanceled(MSG_POLL_INPUT_BUFFERS);
}
} else if (result == MediaCodec.INFO_OUTPUT_BUFFERS_CHANGED) {
mOutputBuffers = mCodec.getOutputBuffers();
} else if (result == MediaCodec.INFO_OUTPUT_FORMAT_CHANGED) {
mCallbackSender.notifyOutputFormat(mCodec.getOutputFormat());
} else if (result == MediaCodec.INFO_TRY_AGAIN_LATER) {
// When input ended, keep polling remaining output buffer until EOS.
dequeueMoreBuffer = mInputEnded;
} else {
mCallbackSender.notifyError(result);
dequeueMoreBuffer = false;
}
if (dequeueMoreBuffer) {
schedulePollingIfNotCanceled(MSG_POLL_OUTPUT_BUFFERS);
}
}
}
private MediaCodec mCodec;
private ByteBuffer[] mInputBuffers;
private ByteBuffer[] mOutputBuffers;
private AsyncCodec.Callbacks mCallbacks;
private CallbackSender mCallbackSender;
private BufferPoller mBufferPoller;
private volatile boolean mInputEnded;
private volatile boolean mOutputEnded;
// Must be called on a thread with looper.
/* package */ JellyBeanAsyncCodec(String name) throws IOException {
mCodec = MediaCodec.createByCodecName(name);
initBufferPoller(name + " buffer poller");
}
private void initBufferPoller(String name) {
if (mBufferPoller != null) {
Log.e(LOGTAG, "poller already initialized");
return;
}
HandlerThread thread = new HandlerThread(name);
thread.start();
mBufferPoller = new BufferPoller(thread.getLooper());
if (DEBUG) Log.d(LOGTAG, "start poller for codec:" + this + ", thread=" + thread.getThreadId());
}
@Override
public void setCallbacks(AsyncCodec.Callbacks callbacks, Handler handler) {
if (callbacks == null) {
return;
}
Looper looper = (handler == null) ? null : handler.getLooper();
if (looper == null) {
// Use this thread if no handler supplied.
looper = Looper.myLooper();
}
if (looper == null) {
// This thread has no looper. Use poller thread.
looper = mBufferPoller.getLooper();
}
mCallbackSender = new CallbackSender(looper, callbacks);
if (DEBUG) Log.d(LOGTAG, "setCallbacks(): sender=" + mCallbackSender);
}
@Override
public void configure(MediaFormat format, Surface surface, int flags) {
assertCallbacks();
mCodec.configure(format, surface, null, flags);
}
private void assertCallbacks() {
if (mCallbackSender == null) {
throw new IllegalStateException(LOGTAG + ": callback must be supplied with setCallbacks().");
}
}
@Override
public void start() {
assertCallbacks();
mCodec.start();
mInputEnded = false;
mOutputEnded = false;
mInputBuffers = mCodec.getInputBuffers();
mOutputBuffers = mCodec.getOutputBuffers();
mBufferPoller.schedulePolling(BufferPoller.MSG_POLL_INPUT_BUFFERS);
}
@Override
public final void queueInputBuffer(int index, int offset, int size, long presentationTimeUs, int flags) {
assertCallbacks();
mInputEnded = (flags & MediaCodec.BUFFER_FLAG_END_OF_STREAM) != 0;
try {
mCodec.queueInputBuffer(index, offset, size, presentationTimeUs, flags);
} catch (IllegalStateException e) {
e.printStackTrace();
mCallbackSender.notifyError(ERROR_CODEC);
return;
}
mBufferPoller.schedulePolling(BufferPoller.MSG_POLL_INPUT_BUFFERS);
mBufferPoller.schedulePolling(BufferPoller.MSG_POLL_OUTPUT_BUFFERS);
}
@Override
public final void releaseOutputBuffer(int index, boolean render) {
assertCallbacks();
mCodec.releaseOutputBuffer(index, render);
}
@Override
public final ByteBuffer getInputBuffer(int index) {
assertCallbacks();
return mInputBuffers[index];
}
@Override
public final ByteBuffer getOutputBuffer(int index) {
assertCallbacks();
return mOutputBuffers[index];
}
@Override
public void flush() {
assertCallbacks();
mInputEnded = false;
mOutputEnded = false;
cancelPendingTasks();
mCodec.flush();
mBufferPoller.schedulePolling(BufferPoller.MSG_POLL_INPUT_BUFFERS);
}
private void cancelPendingTasks() {
mBufferPoller.cancel();
mCallbackSender.cancel();
}
@Override
public void stop() {
assertCallbacks();
cancelPendingTasks();
mCodec.stop();
}
@Override
public void release() {
assertCallbacks();
cancelPendingTasks();
mCallbackSender = null;
mCodec.release();
stopBufferPoller();
}
private void stopBufferPoller() {
if (mBufferPoller == null) {
Log.e(LOGTAG, "no initialized poller.");
return;
}
mBufferPoller.getLooper().quit();
mBufferPoller = null;
if (DEBUG) Log.d(LOGTAG, "stop poller " + this);
}
}
|