-
-
Notifications
You must be signed in to change notification settings - Fork 31
/
ExtractDecodeEditEncodeMuxTest.java
1122 lines (1039 loc) · 45.7 KB
/
ExtractDecodeEditEncodeMuxTest.java
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2013 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.decodeencodetest;
import android.annotation.TargetApi;
import android.content.res.AssetFileDescriptor;
import android.media.MediaCodec;
import android.media.MediaCodecInfo;
import android.media.MediaCodecList;
import android.media.MediaExtractor;
import android.media.MediaFormat;
import android.media.MediaMuxer;
import android.os.Environment;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.Message;
import android.test.AndroidTestCase;
import android.util.Log;
import android.view.Surface;
import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.concurrent.atomic.AtomicReference;
import java.util.LinkedList;
/**
* Test for the integration of MediaMuxer and MediaCodec's encoder.
*
* <p>It uses MediaExtractor to get frames from a test stream, decodes them to a surface, uses a
* shader to edit them, encodes them from the resulting surface, and then uses MediaMuxer to write
* them into a file.
*
* <p>It does not currently check whether the result file is correct, but makes sure that nothing
* fails along the way.
*
* <p>It also tests the way the codec config buffers need to be passed from the MediaCodec to the
* MediaMuxer.
*/
@TargetApi(18)
public class ExtractDecodeEditEncodeMuxTest extends AndroidTestCase {
private static final String TAG = ExtractDecodeEditEncodeMuxTest.class.getSimpleName();
private static final boolean VERBOSE = true; // lots of logging
/** How long to wait for the next buffer to become available. */
private static final int TIMEOUT_USEC = 10000;
/** Where to output the test files. */
private static final File OUTPUT_FILENAME_DIR = Environment.getExternalStorageDirectory();
// parameters for the video encoder
private static final String OUTPUT_VIDEO_MIME_TYPE = "video/avc"; // H.264 Advanced Video Coding
private static final int OUTPUT_VIDEO_BIT_RATE = 2000000; // 2Mbps
private static final int OUTPUT_VIDEO_FRAME_RATE = 15; // 15fps
private static final int OUTPUT_VIDEO_IFRAME_INTERVAL = 10; // 10 seconds between I-frames
private static final int OUTPUT_VIDEO_COLOR_FORMAT =
MediaCodecInfo.CodecCapabilities.COLOR_FormatSurface;
// parameters for the audio encoder
private static final String OUTPUT_AUDIO_MIME_TYPE = "audio/mp4a-latm"; // Advanced Audio Coding
private static final int OUTPUT_AUDIO_CHANNEL_COUNT = 2; // Must match the input stream.
private static final int OUTPUT_AUDIO_BIT_RATE = 128 * 1024;
private static final int OUTPUT_AUDIO_AAC_PROFILE =
MediaCodecInfo.CodecProfileLevel.AACObjectHE;
private static final int OUTPUT_AUDIO_SAMPLE_RATE_HZ = 44100; // Must match the input stream.
/**
* Used for editing the frames.
*
* <p>Swaps green and blue channels by storing an RBGA color in an RGBA buffer.
*/
private static final String FRAGMENT_SHADER =
"#extension GL_OES_EGL_image_external : require\n" +
"precision mediump float;\n" +
"varying vec2 vTextureCoord;\n" +
"uniform samplerExternalOES sTexture;\n" +
"void main() {\n" +
" gl_FragColor = texture2D(sTexture, vTextureCoord).rbga;\n" +
"}\n";
/** Whether to copy the video from the test video. */
private boolean mCopyVideo;
/** Whether to copy the audio from the test video. */
private boolean mCopyAudio;
/** Width of the output frames. */
private int mWidth = -1;
/** Height of the output frames. */
private int mHeight = -1;
/** The raw resource used as the input file. */
private int mSourceResId;
/** The destination file for the encoded output. */
private String mOutputFile;
public void testExtractDecodeEditEncodeMuxQCIF() throws Throwable {
setSize(176, 144);
setSource(R.raw.video_480x360_mp4_h264_500kbps_30fps_aac_stereo_128kbps_44100hz);
setCopyVideo();
TestWrapper.runTest(this);
}
public void testExtractDecodeEditEncodeMuxQVGA() throws Throwable {
setSize(320, 240);
setSource(R.raw.video_480x360_mp4_h264_500kbps_30fps_aac_stereo_128kbps_44100hz);
setCopyVideo();
TestWrapper.runTest(this);
}
public void testExtractDecodeEditEncodeMux720p() throws Throwable {
setSize(1280, 720);
setSource(R.raw.video_480x360_mp4_h264_500kbps_30fps_aac_stereo_128kbps_44100hz);
setCopyVideo();
TestWrapper.runTest(this);
}
public void testExtractDecodeEditEncodeMuxAudio() throws Throwable {
setSize(1280, 720);
setSource(R.raw.video_480x360_mp4_h264_500kbps_30fps_aac_stereo_128kbps_44100hz);
setCopyAudio();
TestWrapper.runTest(this);
}
public void testExtractDecodeEditEncodeMuxAudioVideo() throws Throwable {
setSize(1280, 720);
setSource(R.raw.video_480x360_mp4_h264_500kbps_30fps_aac_stereo_128kbps_44100hz);
setCopyAudio();
setCopyVideo();
TestWrapper.runTest(this);
}
/** Wraps testExtractDecodeEditEncodeMux() */
private static class TestWrapper implements Runnable {
private Throwable mThrowable;
private ExtractDecodeEditEncodeMuxTest mTest;
private TestWrapper(ExtractDecodeEditEncodeMuxTest test) {
mTest = test;
}
@Override
public void run() {
try {
mTest.extractDecodeEditEncodeMux();
} catch (Throwable th) {
mThrowable = th;
}
}
/**
* Entry point.
*/
public static void runTest(ExtractDecodeEditEncodeMuxTest test) throws Throwable {
test.setOutputFile();
TestWrapper wrapper = new TestWrapper(test);
Thread th = new Thread(wrapper, "codec test");
th.start();
th.join();
if (wrapper.mThrowable != null) {
throw wrapper.mThrowable;
}
}
}
/**
* Sets the test to copy the video stream.
*/
private void setCopyVideo() {
mCopyVideo = true;
}
/**
* Sets the test to copy the video stream.
*/
private void setCopyAudio() {
mCopyAudio = true;
}
/**
* Sets the desired frame size.
*/
private void setSize(int width, int height) {
if ((width % 16) != 0 || (height % 16) != 0) {
Log.w(TAG, "WARNING: width or height not multiple of 16");
}
mWidth = width;
mHeight = height;
}
/**
* Sets the raw resource used as the source video.
*/
private void setSource(int resId) {
mSourceResId = resId;
}
/**
* Sets the name of the output file based on the other parameters.
*
* <p>Must be called after {@link #setSize(int, int)} and {@link #setSource(int)}.
*/
private void setOutputFile() {
StringBuilder sb = new StringBuilder();
sb.append(OUTPUT_FILENAME_DIR.getAbsolutePath());
sb.append("/cts-media-");
sb.append(getClass().getSimpleName());
assertTrue("should have called setSource() first", mSourceResId != -1);
sb.append('-');
sb.append(mSourceResId);
if (mCopyVideo) {
assertTrue("should have called setSize() first", mWidth != -1);
assertTrue("should have called setSize() first", mHeight != -1);
sb.append('-');
sb.append("video");
sb.append('-');
sb.append(mWidth);
sb.append('x');
sb.append(mHeight);
}
if (mCopyAudio) {
sb.append('-');
sb.append("audio");
}
sb.append(".mp4");
mOutputFile = sb.toString();
}
private MediaExtractor mVideoExtractor = null;
private MediaExtractor mAudioExtractor = null;
private InputSurface mInputSurface = null;
private OutputSurface mOutputSurface = null;
private MediaCodec mVideoDecoder = null;
private MediaCodec mAudioDecoder = null;
private MediaCodec mVideoEncoder = null;
private MediaCodec mAudioEncoder = null;
private MediaMuxer mMuxer = null;
/**
* Tests encoding and subsequently decoding video from frames generated into a buffer.
* <p>
* We encode several frames of a video test pattern using MediaCodec, then decode the output
* with MediaCodec and do some simple checks.
*/
private void extractDecodeEditEncodeMux() throws Exception {
// Exception that may be thrown during release.
Exception exception = null;
mDecoderOutputVideoFormat = null;
mDecoderOutputAudioFormat = null;
mEncoderOutputVideoFormat = null;
mEncoderOutputAudioFormat = null;
mOutputVideoTrack = -1;
mOutputAudioTrack = -1;
mVideoExtractorDone = false;
mVideoDecoderDone = false;
mVideoEncoderDone = false;
mAudioExtractorDone = false;
mAudioDecoderDone = false;
mAudioEncoderDone = false;
mPendingAudioDecoderOutputBufferIndices = new LinkedList<Integer>();
mPendingAudioDecoderOutputBufferInfos = new LinkedList<MediaCodec.BufferInfo>();
mPendingAudioEncoderInputBufferIndices = new LinkedList<Integer>();
mPendingVideoEncoderOutputBufferIndices = new LinkedList<Integer>();
mPendingVideoEncoderOutputBufferInfos = new LinkedList<MediaCodec.BufferInfo>();
mPendingAudioEncoderOutputBufferIndices = new LinkedList<Integer>();
mPendingAudioEncoderOutputBufferInfos = new LinkedList<MediaCodec.BufferInfo>();
mMuxing = false;
mVideoExtractedFrameCount = 0;
mVideoDecodedFrameCount = 0;
mVideoEncodedFrameCount = 0;
mAudioExtractedFrameCount = 0;
mAudioDecodedFrameCount = 0;
mAudioEncodedFrameCount = 0;
MediaCodecInfo videoCodecInfo = selectCodec(OUTPUT_VIDEO_MIME_TYPE);
if (videoCodecInfo == null) {
// Don't fail CTS if they don't have an AVC codec (not here, anyway).
Log.e(TAG, "Unable to find an appropriate codec for " + OUTPUT_VIDEO_MIME_TYPE);
return;
}
if (VERBOSE) Log.d(TAG, "video found codec: " + videoCodecInfo.getName());
MediaCodecInfo audioCodecInfo = selectCodec(OUTPUT_AUDIO_MIME_TYPE);
if (audioCodecInfo == null) {
// Don't fail CTS if they don't have an AAC codec (not here, anyway).
Log.e(TAG, "Unable to find an appropriate codec for " + OUTPUT_AUDIO_MIME_TYPE);
return;
}
if (VERBOSE) Log.d(TAG, "audio found codec: " + audioCodecInfo.getName());
try {
// Creates a muxer but do not start or add tracks just yet.
mMuxer = createMuxer();
if (mCopyVideo) {
mVideoExtractor = createExtractor();
int videoInputTrack = getAndSelectVideoTrackIndex(mVideoExtractor);
assertTrue("missing video track in test video", videoInputTrack != -1);
MediaFormat inputFormat = mVideoExtractor.getTrackFormat(videoInputTrack);
// We avoid the device-specific limitations on width and height by using values
// that are multiples of 16, which all tested devices seem to be able to handle.
MediaFormat outputVideoFormat =
MediaFormat.createVideoFormat(OUTPUT_VIDEO_MIME_TYPE, mWidth, mHeight);
// Set some properties. Failing to specify some of these can cause the MediaCodec
// configure() call to throw an unhelpful exception.
outputVideoFormat.setInteger(
MediaFormat.KEY_COLOR_FORMAT, OUTPUT_VIDEO_COLOR_FORMAT);
outputVideoFormat.setInteger(MediaFormat.KEY_BIT_RATE, OUTPUT_VIDEO_BIT_RATE);
outputVideoFormat.setInteger(MediaFormat.KEY_FRAME_RATE, OUTPUT_VIDEO_FRAME_RATE);
outputVideoFormat.setInteger(
MediaFormat.KEY_I_FRAME_INTERVAL, OUTPUT_VIDEO_IFRAME_INTERVAL);
if (VERBOSE) Log.d(TAG, "video format: " + outputVideoFormat);
// Create a MediaCodec for the desired codec, then configure it as an encoder with
// our desired properties. Request a Surface to use for input.
AtomicReference<Surface> inputSurfaceReference = new AtomicReference<Surface>();
mVideoEncoder = createVideoEncoder(
videoCodecInfo, outputVideoFormat, inputSurfaceReference);
mInputSurface = new InputSurface(inputSurfaceReference.get());
mInputSurface.makeCurrent();
// Create a MediaCodec for the decoder, based on the extractor's format.
mOutputSurface = new OutputSurface();
mOutputSurface.changeFragmentShader(FRAGMENT_SHADER);
mVideoDecoder = createVideoDecoder(inputFormat, mOutputSurface.getSurface());
mInputSurface.releaseEGLContext();
}
if (mCopyAudio) {
mAudioExtractor = createExtractor();
int audioInputTrack = getAndSelectAudioTrackIndex(mAudioExtractor);
assertTrue("missing audio track in test video", audioInputTrack != -1);
MediaFormat inputFormat = mAudioExtractor.getTrackFormat(audioInputTrack);
MediaFormat outputAudioFormat =
MediaFormat.createAudioFormat(
OUTPUT_AUDIO_MIME_TYPE, OUTPUT_AUDIO_SAMPLE_RATE_HZ,
OUTPUT_AUDIO_CHANNEL_COUNT);
outputAudioFormat.setInteger(MediaFormat.KEY_BIT_RATE, OUTPUT_AUDIO_BIT_RATE);
outputAudioFormat.setInteger(MediaFormat.KEY_AAC_PROFILE, OUTPUT_AUDIO_AAC_PROFILE);
// Create a MediaCodec for the desired codec, then configure it as an encoder with
// our desired properties. Request a Surface to use for input.
mAudioEncoder = createAudioEncoder(audioCodecInfo, outputAudioFormat);
// Create a MediaCodec for the decoder, based on the extractor's format.
mAudioDecoder = createAudioDecoder(inputFormat);
}
awaitEncode();
} finally {
if (VERBOSE) Log.d(TAG, "releasing extractor, decoder, encoder, and muxer");
// Try to release everything we acquired, even if one of the releases fails, in which
// case we save the first exception we got and re-throw at the end (unless something
// other exception has already been thrown). This guarantees the first exception thrown
// is reported as the cause of the error, everything is (attempted) to be released, and
// all other exceptions appear in the logs.
try {
if (mVideoExtractor != null) {
mVideoExtractor.release();
}
} catch(Exception e) {
Log.e(TAG, "error while releasing videoExtractor", e);
if (exception == null) {
exception = e;
}
}
try {
if (mAudioExtractor != null) {
mAudioExtractor.release();
}
} catch(Exception e) {
Log.e(TAG, "error while releasing audioExtractor", e);
if (exception == null) {
exception = e;
}
}
try {
if (mVideoDecoder != null) {
mVideoDecoder.stop();
mVideoDecoder.release();
}
} catch(Exception e) {
Log.e(TAG, "error while releasing videoDecoder", e);
if (exception == null) {
exception = e;
}
}
try {
if (mOutputSurface != null) {
mOutputSurface.release();
}
} catch(Exception e) {
Log.e(TAG, "error while releasing outputSurface", e);
if (exception == null) {
exception = e;
}
}
try {
if (mVideoEncoder != null) {
mVideoEncoder.stop();
mVideoEncoder.release();
}
} catch(Exception e) {
Log.e(TAG, "error while releasing videoEncoder", e);
if (exception == null) {
exception = e;
}
}
try {
if (mAudioDecoder != null) {
mAudioDecoder.stop();
mAudioDecoder.release();
}
} catch(Exception e) {
Log.e(TAG, "error while releasing audioDecoder", e);
if (exception == null) {
exception = e;
}
}
try {
if (mAudioEncoder != null) {
mAudioEncoder.stop();
mAudioEncoder.release();
}
} catch(Exception e) {
Log.e(TAG, "error while releasing audioEncoder", e);
if (exception == null) {
exception = e;
}
}
try {
if (mMuxer != null) {
mMuxer.stop();
mMuxer.release();
}
} catch(Exception e) {
Log.e(TAG, "error while releasing muxer", e);
if (exception == null) {
exception = e;
}
}
try {
if (mInputSurface != null) {
mInputSurface.release();
}
} catch(Exception e) {
Log.e(TAG, "error while releasing inputSurface", e);
if (exception == null) {
exception = e;
}
}
if (mVideoDecoderHandlerThread != null) {
mVideoDecoderHandlerThread.quitSafely();
}
mVideoExtractor = null;
mAudioExtractor = null;
mOutputSurface = null;
mInputSurface = null;
mVideoDecoder = null;
mAudioDecoder = null;
mVideoEncoder = null;
mAudioEncoder = null;
mMuxer = null;
mVideoDecoderHandlerThread = null;
}
if (exception != null) {
throw exception;
}
}
/**
* Creates an extractor that reads its frames from {@link #mSourceResId}.
*/
private MediaExtractor createExtractor() throws IOException {
MediaExtractor extractor;
AssetFileDescriptor srcFd = getContext().getResources().openRawResourceFd(mSourceResId);
extractor = new MediaExtractor();
extractor.setDataSource(srcFd.getFileDescriptor(), srcFd.getStartOffset(),
srcFd.getLength());
return extractor;
}
static class CallbackHandler extends Handler {
CallbackHandler(Looper l) {
super(l);
}
private MediaCodec mCodec;
private boolean mEncoder;
private MediaCodec.Callback mCallback;
private String mMime;
private boolean mSetDone;
@Override
public void handleMessage(Message msg) {
try {
mCodec = mEncoder ? MediaCodec.createEncoderByType(mMime) : MediaCodec.createDecoderByType(mMime);
} catch (IOException ioe) {
}
mCodec.setCallback(mCallback);
synchronized (this) {
mSetDone = true;
notifyAll();
}
}
void create(boolean encoder, String mime, MediaCodec.Callback callback) {
mEncoder = encoder;
mMime = mime;
mCallback = callback;
mSetDone = false;
sendEmptyMessage(0);
synchronized (this) {
while (!mSetDone) {
try {
wait();
} catch (InterruptedException ie) {
}
}
}
}
MediaCodec getCodec() {
return mCodec;
}
}
private HandlerThread mVideoDecoderHandlerThread;
private CallbackHandler mVideoDecoderHandler;
/**
* Creates a decoder for the given format, which outputs to the given surface.
*
* @param inputFormat the format of the stream to decode
* @param surface into which to decode the frames
*/
private MediaCodec createVideoDecoder(MediaFormat inputFormat, Surface surface) throws IOException {
mVideoDecoderHandlerThread = new HandlerThread("DecoderThread");
mVideoDecoderHandlerThread.start();
mVideoDecoderHandler = new CallbackHandler(mVideoDecoderHandlerThread.getLooper());
MediaCodec.Callback callback = new MediaCodec.Callback() {
public void onError(MediaCodec codec, MediaCodec.CodecException exception) {
}
public void onOutputFormatChanged(MediaCodec codec, MediaFormat format) {
mDecoderOutputVideoFormat = codec.getOutputFormat();
if (VERBOSE) {
Log.d(TAG, "video decoder: output format changed: "
+ mDecoderOutputVideoFormat);
}
}
public void onInputBufferAvailable(MediaCodec codec, int index) {
// Extract video from file and feed to decoder.
// We feed packets regardless of whether the muxer is set up or not.
// If the muxer isn't set up yet, the encoder output will be queued up,
// finally blocking the decoder as well.
ByteBuffer decoderInputBuffer = codec.getInputBuffer(index);
while (!mVideoExtractorDone) {
int size = mVideoExtractor.readSampleData(decoderInputBuffer, 0);
long presentationTime = mVideoExtractor.getSampleTime();
if (VERBOSE) {
Log.d(TAG, "video extractor: returned buffer of size " + size);
Log.d(TAG, "video extractor: returned buffer for time " + presentationTime);
}
if (size >= 0) {
codec.queueInputBuffer(
index,
0,
size,
presentationTime,
mVideoExtractor.getSampleFlags());
}
mVideoExtractorDone = !mVideoExtractor.advance();
if (mVideoExtractorDone) {
if (VERBOSE) Log.d(TAG, "video extractor: EOS");
codec.queueInputBuffer(
index,
0,
0,
0,
MediaCodec.BUFFER_FLAG_END_OF_STREAM);
}
mVideoExtractedFrameCount++;
logState();
if (size >= 0)
break;
}
}
public void onOutputBufferAvailable(MediaCodec codec, int index, MediaCodec.BufferInfo info) {
if (VERBOSE) {
Log.d(TAG, "video decoder: returned output buffer: " + index);
Log.d(TAG, "video decoder: returned buffer of size " + info.size);
}
if ((info.flags & MediaCodec.BUFFER_FLAG_CODEC_CONFIG) != 0) {
if (VERBOSE) Log.d(TAG, "video decoder: codec config buffer");
codec.releaseOutputBuffer(index, false);
return;
}
if (VERBOSE) {
Log.d(TAG, "video decoder: returned buffer for time "
+ info.presentationTimeUs);
}
boolean render = info.size != 0;
codec.releaseOutputBuffer(index, render);
if (render) {
mInputSurface.makeCurrent();
if (VERBOSE) Log.d(TAG, "output surface: await new image");
mOutputSurface.awaitNewImage();
// Edit the frame and send it to the encoder.
if (VERBOSE) Log.d(TAG, "output surface: draw image");
mOutputSurface.drawImage();
mInputSurface.setPresentationTime(
info.presentationTimeUs * 1000);
if (VERBOSE) Log.d(TAG, "input surface: swap buffers");
mInputSurface.swapBuffers();
if (VERBOSE) Log.d(TAG, "video encoder: notified of new frame");
mInputSurface.releaseEGLContext();
}
if ((info.flags & MediaCodec.BUFFER_FLAG_END_OF_STREAM) != 0) {
if (VERBOSE) Log.d(TAG, "video decoder: EOS");
mVideoDecoderDone = true;
mVideoEncoder.signalEndOfInputStream();
}
mVideoDecodedFrameCount++;
logState();
}
};
// Create the decoder on a different thread, in order to have the callbacks there.
// This makes sure that the blocking waiting and rendering in onOutputBufferAvailable
// won't block other callbacks (e.g. blocking encoder output callbacks), which
// would otherwise lead to the transcoding pipeline to lock up.
// Since API 23, we could just do setCallback(callback, mVideoDecoderHandler) instead
// of using a custom Handler and passing a message to create the MediaCodec there.
// When the callbacks are received on a different thread, the updating of the variables
// that are used for state logging (mVideoExtractedFrameCount, mVideoDecodedFrameCount,
// mVideoExtractorDone and mVideoDecoderDone) should ideally be synchronized properly
// against accesses from other threads, but that is left out for brevity since it's
// not essential to the actual transcoding.
mVideoDecoderHandler.create(false, getMimeTypeFor(inputFormat), callback);
MediaCodec decoder = mVideoDecoderHandler.getCodec();
decoder.configure(inputFormat, surface, null, 0);
decoder.start();
return decoder;
}
/**
* Creates an encoder for the given format using the specified codec, taking input from a
* surface.
*
* <p>The surface to use as input is stored in the given reference.
*
* @param codecInfo of the codec to use
* @param format of the stream to be produced
* @param surfaceReference to store the surface to use as input
*/
private MediaCodec createVideoEncoder(
MediaCodecInfo codecInfo,
MediaFormat format,
AtomicReference<Surface> surfaceReference) throws IOException {
MediaCodec encoder = MediaCodec.createByCodecName(codecInfo.getName());
encoder.setCallback(new MediaCodec.Callback() {
public void onError(MediaCodec codec, MediaCodec.CodecException exception) {
}
public void onOutputFormatChanged(MediaCodec codec, MediaFormat format) {
if (VERBOSE) Log.d(TAG, "video encoder: output format changed");
if (mOutputVideoTrack >= 0) {
fail("video encoder changed its output format again?");
}
mEncoderOutputVideoFormat = codec.getOutputFormat();
setupMuxer();
}
public void onInputBufferAvailable(MediaCodec codec, int index) {
}
public void onOutputBufferAvailable(MediaCodec codec, int index, MediaCodec.BufferInfo info) {
if (VERBOSE) {
Log.d(TAG, "video encoder: returned output buffer: " + index);
Log.d(TAG, "video encoder: returned buffer of size " + info.size);
}
muxVideo(index, info);
}
});
encoder.configure(format, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
// Must be called before start() is.
surfaceReference.set(encoder.createInputSurface());
encoder.start();
return encoder;
}
/**
* Creates a decoder for the given format.
*
* @param inputFormat the format of the stream to decode
*/
private MediaCodec createAudioDecoder(MediaFormat inputFormat) throws IOException {
MediaCodec decoder = MediaCodec.createDecoderByType(getMimeTypeFor(inputFormat));
decoder.setCallback(new MediaCodec.Callback() {
public void onError(MediaCodec codec, MediaCodec.CodecException exception) {
}
public void onOutputFormatChanged(MediaCodec codec, MediaFormat format) {
mDecoderOutputAudioFormat = codec.getOutputFormat();
if (VERBOSE) {
Log.d(TAG, "audio decoder: output format changed: "
+ mDecoderOutputAudioFormat);
}
}
public void onInputBufferAvailable(MediaCodec codec, int index) {
ByteBuffer decoderInputBuffer = codec.getInputBuffer(index);
while (!mAudioExtractorDone) {
int size = mAudioExtractor.readSampleData(decoderInputBuffer, 0);
long presentationTime = mAudioExtractor.getSampleTime();
if (VERBOSE) {
Log.d(TAG, "audio extractor: returned buffer of size " + size);
Log.d(TAG, "audio extractor: returned buffer for time " + presentationTime);
}
if (size >= 0) {
codec.queueInputBuffer(
index,
0,
size,
presentationTime,
mAudioExtractor.getSampleFlags());
}
mAudioExtractorDone = !mAudioExtractor.advance();
if (mAudioExtractorDone) {
if (VERBOSE) Log.d(TAG, "audio extractor: EOS");
codec.queueInputBuffer(
index,
0,
0,
0,
MediaCodec.BUFFER_FLAG_END_OF_STREAM);
}
mAudioExtractedFrameCount++;
logState();
if (size >= 0)
break;
}
}
public void onOutputBufferAvailable(MediaCodec codec, int index, MediaCodec.BufferInfo info) {
if (VERBOSE) {
Log.d(TAG, "audio decoder: returned output buffer: " + index);
}
if (VERBOSE) {
Log.d(TAG, "audio decoder: returned buffer of size " + info.size);
}
ByteBuffer decoderOutputBuffer = codec.getOutputBuffer(index);
if ((info.flags & MediaCodec.BUFFER_FLAG_CODEC_CONFIG) != 0) {
if (VERBOSE) Log.d(TAG, "audio decoder: codec config buffer");
codec.releaseOutputBuffer(index, false);
return;
}
if (VERBOSE) {
Log.d(TAG, "audio decoder: returned buffer for time "
+ info.presentationTimeUs);
}
mPendingAudioDecoderOutputBufferIndices.add(index);
mPendingAudioDecoderOutputBufferInfos.add(info);
mAudioDecodedFrameCount++;
logState();
tryEncodeAudio();
}
});
decoder.configure(inputFormat, null, null, 0);
decoder.start();
return decoder;
}
/**
* Creates an encoder for the given format using the specified codec.
*
* @param codecInfo of the codec to use
* @param format of the stream to be produced
*/
private MediaCodec createAudioEncoder(MediaCodecInfo codecInfo, MediaFormat format) throws IOException {
MediaCodec encoder = MediaCodec.createByCodecName(codecInfo.getName());
encoder.setCallback(new MediaCodec.Callback() {
public void onError(MediaCodec codec, MediaCodec.CodecException exception) {
}
public void onOutputFormatChanged(MediaCodec codec, MediaFormat format) {
if (VERBOSE) Log.d(TAG, "audio encoder: output format changed");
if (mOutputAudioTrack >= 0) {
fail("audio encoder changed its output format again?");
}
mEncoderOutputAudioFormat = codec.getOutputFormat();
setupMuxer();
}
public void onInputBufferAvailable(MediaCodec codec, int index) {
if (VERBOSE) {
Log.d(TAG, "audio encoder: returned input buffer: " + index);
}
mPendingAudioEncoderInputBufferIndices.add(index);
tryEncodeAudio();
}
public void onOutputBufferAvailable(MediaCodec codec, int index, MediaCodec.BufferInfo info) {
if (VERBOSE) {
Log.d(TAG, "audio encoder: returned output buffer: " + index);
Log.d(TAG, "audio encoder: returned buffer of size " + info.size);
}
muxAudio(index, info);
}
});
encoder.configure(format, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
encoder.start();
return encoder;
}
// No need to have synchronization around this, since both audio encoder and
// decoder callbacks are on the same thread.
private void tryEncodeAudio() {
if (mPendingAudioEncoderInputBufferIndices.size() == 0 ||
mPendingAudioDecoderOutputBufferIndices.size() == 0)
return;
int decoderIndex = mPendingAudioDecoderOutputBufferIndices.poll();
int encoderIndex = mPendingAudioEncoderInputBufferIndices.poll();
MediaCodec.BufferInfo info = mPendingAudioDecoderOutputBufferInfos.poll();
ByteBuffer encoderInputBuffer = mAudioEncoder.getInputBuffer(encoderIndex);
int size = info.size;
long presentationTime = info.presentationTimeUs;
if (VERBOSE) {
Log.d(TAG, "audio decoder: processing pending buffer: "
+ decoderIndex);
}
if (VERBOSE) {
Log.d(TAG, "audio decoder: pending buffer of size " + size);
Log.d(TAG, "audio decoder: pending buffer for time " + presentationTime);
}
if (size >= 0) {
ByteBuffer decoderOutputBuffer = mAudioDecoder.getOutputBuffer(decoderIndex).duplicate();
decoderOutputBuffer.position(info.offset);
decoderOutputBuffer.limit(info.offset + size);
encoderInputBuffer.position(0);
encoderInputBuffer.put(decoderOutputBuffer);
mAudioEncoder.queueInputBuffer(
encoderIndex,
0,
size,
presentationTime,
info.flags);
}
mAudioDecoder.releaseOutputBuffer(decoderIndex, false);
if ((info.flags
& MediaCodec.BUFFER_FLAG_END_OF_STREAM) != 0) {
if (VERBOSE) Log.d(TAG, "audio decoder: EOS");
mAudioDecoderDone = true;
}
logState();
}
private void setupMuxer() {
if (!mMuxing
&& (!mCopyAudio || mEncoderOutputAudioFormat != null)
&& (!mCopyVideo || mEncoderOutputVideoFormat != null)) {
if (mCopyVideo) {
Log.d(TAG, "muxer: adding video track.");
mOutputVideoTrack = mMuxer.addTrack(mEncoderOutputVideoFormat);
}
if (mCopyAudio) {
Log.d(TAG, "muxer: adding audio track.");
mOutputAudioTrack = mMuxer.addTrack(mEncoderOutputAudioFormat);
}
Log.d(TAG, "muxer: starting");
mMuxer.start();
mMuxing = true;
MediaCodec.BufferInfo info;
while ((info = mPendingVideoEncoderOutputBufferInfos.poll()) != null) {
int index = mPendingVideoEncoderOutputBufferIndices.poll().intValue();
muxVideo(index, info);
}
while ((info = mPendingAudioEncoderOutputBufferInfos.poll()) != null) {
int index = mPendingAudioEncoderOutputBufferIndices.poll().intValue();
muxAudio(index, info);
}
}
}
private void muxVideo(int index, MediaCodec.BufferInfo info) {
if (!mMuxing) {
mPendingVideoEncoderOutputBufferIndices.add(new Integer(index));
mPendingVideoEncoderOutputBufferInfos.add(info);
return;
}
ByteBuffer encoderOutputBuffer = mVideoEncoder.getOutputBuffer(index);
if ((info.flags & MediaCodec.BUFFER_FLAG_CODEC_CONFIG) != 0) {
if (VERBOSE) Log.d(TAG, "video encoder: codec config buffer");
// Simply ignore codec config buffers.
mVideoEncoder.releaseOutputBuffer(index, false);
return;
}
if (VERBOSE) {
Log.d(TAG, "video encoder: returned buffer for time "
+ info.presentationTimeUs);
}
if (info.size != 0) {
mMuxer.writeSampleData(
mOutputVideoTrack, encoderOutputBuffer, info);
}
mVideoEncoder.releaseOutputBuffer(index, false);
mVideoEncodedFrameCount++;
if ((info.flags & MediaCodec.BUFFER_FLAG_END_OF_STREAM) != 0) {
if (VERBOSE) Log.d(TAG, "video encoder: EOS");
synchronized (this) {
mVideoEncoderDone = true;
notifyAll();
}
}
logState();
}
private void muxAudio(int index, MediaCodec.BufferInfo info) {
if (!mMuxing) {
mPendingAudioEncoderOutputBufferIndices.add(new Integer(index));
mPendingAudioEncoderOutputBufferInfos.add(info);
return;
}
ByteBuffer encoderOutputBuffer = mAudioEncoder.getOutputBuffer(index);
if ((info.flags & MediaCodec.BUFFER_FLAG_CODEC_CONFIG) != 0) {
if (VERBOSE) Log.d(TAG, "audio encoder: codec config buffer");
// Simply ignore codec config buffers.
mAudioEncoder.releaseOutputBuffer(index, false);
return;
}
if (VERBOSE) {
Log.d(TAG, "audio encoder: returned buffer for time " + info.presentationTimeUs);
}
if (info.size != 0) {
mMuxer.writeSampleData(
mOutputAudioTrack, encoderOutputBuffer, info);
}
mAudioEncoder.releaseOutputBuffer(index, false);
mAudioEncodedFrameCount++;
if ((info.flags & MediaCodec.BUFFER_FLAG_END_OF_STREAM) != 0) {
if (VERBOSE) Log.d(TAG, "audio encoder: EOS");
synchronized (this) {
mAudioEncoderDone = true;
notifyAll();
}
}
logState();
}
/**
* Creates a muxer to write the encoded frames.
*
* <p>The muxer is not started as it needs to be started only after all streams have been added.
*/
private MediaMuxer createMuxer() throws IOException {
return new MediaMuxer(mOutputFile, MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4);
}
private int getAndSelectVideoTrackIndex(MediaExtractor extractor) {
for (int index = 0; index < extractor.getTrackCount(); ++index) {
if (VERBOSE) {
Log.d(TAG, "format for track " + index + " is "
+ getMimeTypeFor(extractor.getTrackFormat(index)));
}
if (isVideoFormat(extractor.getTrackFormat(index))) {
extractor.selectTrack(index);
return index;
}
}
return -1;
}
private int getAndSelectAudioTrackIndex(MediaExtractor extractor) {
for (int index = 0; index < extractor.getTrackCount(); ++index) {
if (VERBOSE) {
Log.d(TAG, "format for track " + index + " is "
+ getMimeTypeFor(extractor.getTrackFormat(index)));
}
if (isAudioFormat(extractor.getTrackFormat(index))) {
extractor.selectTrack(index);
return index;
}
}
return -1;
}
// We will get these from the decoders when notified of a format change.
private MediaFormat mDecoderOutputVideoFormat = null;
private MediaFormat mDecoderOutputAudioFormat = null;
// We will get these from the encoders when notified of a format change.
private MediaFormat mEncoderOutputVideoFormat = null;
private MediaFormat mEncoderOutputAudioFormat = null;