1 /*
2  * Copyright (C) 2020 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.mediav2.cts;
18 
19 import android.media.Image;
20 import android.media.MediaCodec;
21 import android.media.MediaCodecInfo;
22 import android.media.MediaFormat;
23 import android.os.Bundle;
24 import android.util.Pair;
25 
26 import androidx.test.filters.SmallTest;
27 
28 import org.junit.After;
29 import org.junit.Ignore;
30 import org.junit.Rule;
31 import org.junit.Test;
32 import org.junit.experimental.runners.Enclosed;
33 import org.junit.rules.Timeout;
34 import org.junit.runner.RunWith;
35 
36 import java.io.IOException;
37 import java.nio.ByteBuffer;
38 import java.util.concurrent.TimeUnit;
39 
40 import static org.junit.Assert.assertFalse;
41 import static org.junit.Assert.assertNotNull;
42 import static org.junit.Assert.assertNull;
43 import static org.junit.Assert.assertTrue;
44 import static org.junit.Assert.fail;
45 
46 @RunWith(Enclosed.class)
47 public class CodecUnitTest {
48     static final int PER_TEST_TIMEOUT_MS = 10000;
49     static final long STALL_TIME_MS = 1000;
50 
51     @SmallTest
52     public static class TestApi extends CodecTestBase {
53         @Rule
54         public Timeout timeout = new Timeout(PER_TEST_TIMEOUT_MS, TimeUnit.MILLISECONDS);
55 
56         @After
hasSeenError()57         public void hasSeenError() {
58             assertFalse(mAsyncHandle.hasSeenError());
59         }
60 
TestApi()61         public TestApi() {
62             mAsyncHandle = new CodecAsyncHandler();
63         }
64 
enqueueInput(int bufferIndex)65         void enqueueInput(int bufferIndex) {
66             fail("something went wrong, shouldn't have reached here");
67         }
68 
dequeueOutput(int bufferIndex, MediaCodec.BufferInfo info)69         void dequeueOutput(int bufferIndex, MediaCodec.BufferInfo info) {
70             if ((info.flags & MediaCodec.BUFFER_FLAG_END_OF_STREAM) != 0) {
71                 mSawOutputEOS = true;
72             }
73             mCodec.releaseOutputBuffer(bufferIndex, false);
74         }
75 
getSampleAudioFormat()76         private MediaFormat getSampleAudioFormat() {
77             MediaFormat format = new MediaFormat();
78             String mime = MediaFormat.MIMETYPE_AUDIO_AAC;
79             format.setString(MediaFormat.KEY_MIME, mime);
80             format.setInteger(MediaFormat.KEY_BIT_RATE, 64000);
81             format.setInteger(MediaFormat.KEY_SAMPLE_RATE, 16000);
82             format.setInteger(MediaFormat.KEY_CHANNEL_COUNT, 1);
83             return format;
84         }
85 
getSampleVideoFormat()86         private MediaFormat getSampleVideoFormat() {
87             MediaFormat format = new MediaFormat();
88             String mime = MediaFormat.MIMETYPE_VIDEO_AVC;
89             format.setString(MediaFormat.KEY_MIME, mime);
90             format.setInteger(MediaFormat.KEY_BIT_RATE, 256000);
91             format.setInteger(MediaFormat.KEY_WIDTH, 352);
92             format.setInteger(MediaFormat.KEY_HEIGHT, 288);
93             format.setInteger(MediaFormat.KEY_FRAME_RATE, 30);
94             format.setFloat(MediaFormat.KEY_I_FRAME_INTERVAL, 1.0f);
95             format.setInteger(MediaFormat.KEY_COLOR_FORMAT,
96                     MediaCodecInfo.CodecCapabilities.COLOR_FormatYUV420Flexible);
97             return format;
98         }
99 
updateBitrate(int bitrate)100         private Bundle updateBitrate(int bitrate) {
101             final Bundle bitrateUpdate = new Bundle();
102             bitrateUpdate.putInt(MediaCodec.PARAMETER_KEY_VIDEO_BITRATE, bitrate);
103             return bitrateUpdate;
104         }
105 
testConfigureCodecForIncompleteFormat(MediaFormat format, String[] keys, boolean isEncoder)106         void testConfigureCodecForIncompleteFormat(MediaFormat format, String[] keys,
107                 boolean isEncoder) throws IOException {
108             String mime = format.getString(MediaFormat.KEY_MIME);
109             if (isEncoder) {
110                 mCodec = MediaCodec.createEncoderByType(mime);
111             } else {
112                 mCodec = MediaCodec.createDecoderByType(mime);
113             }
114             for (String key : keys) {
115                 MediaFormat formatClone = new MediaFormat(format);
116                 formatClone.removeKey(key);
117                 try {
118                     mCodec.configure(formatClone, null, null,
119                             isEncoder ? MediaCodec.CONFIGURE_FLAG_ENCODE : 0);
120                     fail("codec configure succeeds with missing mandatory keys :: " + key);
121                 } catch (Exception e) {
122                     if (!(e instanceof IllegalArgumentException)) {
123                         fail("codec configure rec/exp :: " + e.toString() +
124                                 " / IllegalArgumentException");
125                     }
126                 }
127             }
128             try {
129                 mCodec.configure(format, null, null,
130                         isEncoder ? MediaCodec.CONFIGURE_FLAG_ENCODE : 0);
131             } catch (Exception e) {
132                 fail("configure failed unexpectedly");
133             } finally {
134                 mCodec.release();
135             }
136         }
137 
testConfigureCodecForBadFlags(boolean isEncoder)138         void testConfigureCodecForBadFlags(boolean isEncoder) throws IOException {
139             MediaFormat format = getSampleAudioFormat();
140             String mime = format.getString(MediaFormat.KEY_MIME);
141             if (isEncoder) {
142                 mCodec = MediaCodec.createEncoderByType(mime);
143             } else {
144                 mCodec = MediaCodec.createDecoderByType(mime);
145             }
146             try {
147                 mCodec.configure(format, null, null,
148                         isEncoder ? 0 : MediaCodec.CONFIGURE_FLAG_ENCODE);
149                 fail("codec configure succeeds with bad configure flag");
150             } catch (Exception e) {
151                 if (!(e instanceof IllegalArgumentException)) {
152                     fail("codec configure rec/exp :: " + e.toString() +
153                             " / IllegalArgumentException");
154                 }
155             } finally {
156                 mCodec.release();
157             }
158         }
159 
tryConfigureCodecInInvalidState(MediaFormat format, boolean isAsync, String msg)160         void tryConfigureCodecInInvalidState(MediaFormat format, boolean isAsync, String msg) {
161             try {
162                 configureCodec(format, isAsync, false, true);
163                 fail(msg);
164             } catch (IllegalStateException e) {
165                 // expected
166             }
167         }
168 
tryDequeueInputBufferInInvalidState(String msg)169         void tryDequeueInputBufferInInvalidState(String msg) {
170             try {
171                 mCodec.dequeueInputBuffer(Q_DEQ_TIMEOUT_US);
172                 fail(msg);
173             } catch (IllegalStateException e) {
174                 // expected
175             }
176         }
177 
tryDequeueOutputBufferInInvalidState(String msg)178         void tryDequeueOutputBufferInInvalidState(String msg) {
179             try {
180                 MediaCodec.BufferInfo info = new MediaCodec.BufferInfo();
181                 mCodec.dequeueOutputBuffer(info, Q_DEQ_TIMEOUT_US);
182                 fail(msg);
183             } catch (IllegalStateException e) {
184                 // expected
185             }
186         }
187 
tryFlushInInvalidState(String msg)188         void tryFlushInInvalidState(String msg) {
189             try {
190                 flushCodec();
191                 fail(msg);
192             } catch (IllegalStateException e) {
193                 // expected
194             }
195         }
196 
tryGetMetaData(String msg)197         void tryGetMetaData(String msg) {
198             try {
199                 mCodec.getName();
200             } catch (IllegalStateException e) {
201                 fail("get name resulted in" + e.getMessage());
202             }
203 
204             try {
205                 mCodec.getCanonicalName();
206             } catch (IllegalStateException e) {
207                 fail("get canonical name resulted in" + e.getMessage());
208             }
209 
210             try {
211                 mCodec.getCodecInfo();
212             } catch (IllegalStateException e) {
213                 fail("get codec info resulted in" + e.getMessage());
214             }
215 
216             try {
217                 mCodec.getMetrics();
218             } catch (IllegalStateException e) {
219                 fail("get metrics resulted in" + e.getMessage());
220             }
221         }
222 
tryGetInputBufferInInvalidState(String msg)223         void tryGetInputBufferInInvalidState(String msg) {
224             try {
225                 mCodec.getInputBuffer(0);
226                 fail(msg);
227             } catch (IllegalStateException e) {
228                 // expected
229             }
230         }
231 
tryGetInputFormatInInvalidState(String msg)232         void tryGetInputFormatInInvalidState(String msg) {
233             try {
234                 mCodec.getInputFormat();
235                 fail(msg);
236             } catch (IllegalStateException e) {
237                 // expected
238             }
239         }
240 
tryGetOutputBufferInInvalidState(String msg)241         void tryGetOutputBufferInInvalidState(String msg) {
242             try {
243                 mCodec.getOutputBuffer(0);
244                 fail(msg);
245             } catch (IllegalStateException e) {
246                 // expected
247             }
248         }
249 
tryGetOutputFormatInInvalidState(String msg)250         void tryGetOutputFormatInInvalidState(String msg) {
251             try {
252                 mCodec.getOutputFormat();
253                 fail(msg);
254             } catch (IllegalStateException e) {
255                 // expected
256             }
257 
258             try {
259                 mCodec.getOutputFormat(0);
260                 fail(msg);
261             } catch (IllegalStateException e) {
262                 // expected
263             }
264         }
265 
tryStartInInvalidState(String msg)266         void tryStartInInvalidState(String msg) {
267             try {
268                 mCodec.start();
269                 fail(msg);
270             } catch (IllegalStateException e) {
271                 // expected
272             }
273         }
274 
tryGetInputImageInInvalidState(String msg)275         void tryGetInputImageInInvalidState(String msg) {
276             try {
277                 mCodec.getInputImage(0);
278                 fail(msg);
279             } catch (IllegalStateException e) {
280                 // expected
281             }
282         }
283 
tryGetOutputImageInInvalidState(String msg)284         void tryGetOutputImageInInvalidState(String msg) {
285             try {
286                 mCodec.getOutputImage(0);
287                 fail(msg);
288             } catch (IllegalStateException e) {
289                 // expected
290             }
291         }
292 
tryQueueInputBufferInInvalidState(String msg)293         void tryQueueInputBufferInInvalidState(String msg) {
294             try {
295                 mCodec.queueInputBuffer(0, 0, 0, 0, MediaCodec.BUFFER_FLAG_END_OF_STREAM);
296                 fail(msg);
297             } catch (IllegalStateException e) {
298                 // expected
299             }
300         }
301 
tryReleaseOutputBufferInInvalidState(String msg)302         void tryReleaseOutputBufferInInvalidState(String msg) {
303             try {
304                 mCodec.releaseOutputBuffer(0, false);
305                 fail(msg);
306             } catch (IllegalStateException e) {
307                 // expected
308             }
309         }
310 
311         @Test
testCreateByCodecNameForNull()312         public void testCreateByCodecNameForNull() throws IOException {
313             try {
314                 mCodec = MediaCodec.createByCodecName(null);
315                 fail("createByCodecName succeeds with null argument");
316             } catch (NullPointerException e) {
317                 // expected
318             } finally {
319                 if (mCodec != null) mCodec.release();
320             }
321         }
322 
323         @Test
testCreateByCodecNameForInvalidName()324         public void testCreateByCodecNameForInvalidName() throws IOException {
325             try {
326                 mCodec = MediaCodec.createByCodecName("invalid name");
327                 fail("createByCodecName succeeds with invalid name");
328             } catch (IllegalArgumentException e) {
329                 // expected
330             } finally {
331                 if (mCodec != null) mCodec.release();
332             }
333         }
334 
335         @Test
testCreateDecoderByTypeForNull()336         public void testCreateDecoderByTypeForNull() throws IOException {
337             try {
338                 mCodec = MediaCodec.createDecoderByType(null);
339                 fail("createDecoderByType succeeds with null argument");
340             } catch (NullPointerException e) {
341                 // expected
342             } finally {
343                 if (mCodec != null) mCodec.release();
344             }
345         }
346 
347         @Test
testCreateDecoderByTypeForInvalidMime()348         public void testCreateDecoderByTypeForInvalidMime() throws IOException {
349             try {
350                 mCodec = MediaCodec.createDecoderByType("invalid mime");
351                 fail("createDecoderByType succeeds with invalid mime");
352             } catch (IllegalArgumentException e) {
353                 // expected
354             } finally {
355                 if (mCodec != null) mCodec.release();
356             }
357         }
358 
359         @Test
testCreateEncoderByTypeForNull()360         public void testCreateEncoderByTypeForNull() throws IOException {
361             try {
362                 mCodec = MediaCodec.createEncoderByType(null);
363                 fail("createEncoderByType succeeds with null argument");
364             } catch (NullPointerException e) {
365                 // expected
366             } finally {
367                 if (mCodec != null) mCodec.release();
368             }
369         }
370 
371         @Test
testCreateEncoderByTypeForInvalidMime()372         public void testCreateEncoderByTypeForInvalidMime() throws IOException {
373             try {
374                 mCodec = MediaCodec.createEncoderByType("invalid mime");
375                 fail("createEncoderByType succeeds with invalid mime");
376             } catch (IllegalArgumentException e) {
377                 // expected
378             } finally {
379                 if (mCodec != null) mCodec.release();
380             }
381         }
382 
383         @Test
384         @Ignore("TODO(b/151302868)")
testConfigureForNullFormat()385         public void testConfigureForNullFormat() throws IOException {
386             mCodec = MediaCodec.createEncoderByType(MediaFormat.MIMETYPE_AUDIO_AAC);
387             mCodec.configure(null, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
388             mCodec.release();
389         }
390 
391         @Test
392         @Ignore("TODO(b/151302868)")
testConfigureForEmptyFormat()393         public void testConfigureForEmptyFormat() throws IOException {
394             mCodec = MediaCodec.createEncoderByType(MediaFormat.MIMETYPE_AUDIO_AAC);
395             mCodec.configure(new MediaFormat(), null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
396             mCodec.release();
397         }
398 
399         @Test
400         @Ignore("TODO(b/151302868)")
testConfigureAudioDecodeForIncompleteFormat()401         public void testConfigureAudioDecodeForIncompleteFormat() throws IOException {
402             MediaFormat format = getSampleAudioFormat();
403             String[] mandatoryKeys =
404                     new String[]{MediaFormat.KEY_MIME, MediaFormat.KEY_CHANNEL_COUNT,
405                             MediaFormat.KEY_SAMPLE_RATE};
406             testConfigureCodecForIncompleteFormat(format, mandatoryKeys, false);
407         }
408 
409         @Test
410         @Ignore("TODO(b/151302868)")
testConfigureAudioEncodeForIncompleteFormat()411         public void testConfigureAudioEncodeForIncompleteFormat() throws IOException {
412             MediaFormat format = getSampleAudioFormat();
413             String[] mandatoryKeys =
414                     new String[]{MediaFormat.KEY_MIME, MediaFormat.KEY_CHANNEL_COUNT,
415                             MediaFormat.KEY_SAMPLE_RATE, MediaFormat.KEY_BIT_RATE};
416             testConfigureCodecForIncompleteFormat(format, mandatoryKeys, true);
417         }
418 
419         @Test
420         @Ignore("TODO(b/151302868)")
testConfigureVideoDecodeForIncompleteFormat()421         public void testConfigureVideoDecodeForIncompleteFormat() throws IOException {
422             MediaFormat format = getSampleVideoFormat();
423             String[] mandatoryKeys =
424                     new String[]{MediaFormat.KEY_MIME, MediaFormat.KEY_WIDTH,
425                             MediaFormat.KEY_HEIGHT};
426             testConfigureCodecForIncompleteFormat(format, mandatoryKeys, false);
427         }
428 
429         @Test
430         @Ignore("TODO(b/151302868, b/151303041)")
testConfigureVideoEncodeForIncompleteFormat()431         public void testConfigureVideoEncodeForIncompleteFormat() throws IOException {
432             MediaFormat format = getSampleVideoFormat();
433             String[] mandatoryKeys =
434                     new String[]{MediaFormat.KEY_MIME, MediaFormat.KEY_WIDTH,
435                             MediaFormat.KEY_HEIGHT, MediaFormat.KEY_I_FRAME_INTERVAL,
436                             MediaFormat.KEY_FRAME_RATE, MediaFormat.KEY_BIT_RATE,
437                             MediaFormat.KEY_COLOR_FORMAT};
438             testConfigureCodecForIncompleteFormat(format, mandatoryKeys, true);
439         }
440 
441         @Test
442         @Ignore("TODO(b/151304147)")
testConfigureEncoderForBadFlags()443         public void testConfigureEncoderForBadFlags() throws IOException {
444             testConfigureCodecForBadFlags(true);
445         }
446 
447         @Test
448         @Ignore("TODO(b/151304147)")
testConfigureDecoderForBadFlags()449         public void testConfigureDecoderForBadFlags() throws IOException {
450             testConfigureCodecForBadFlags(false);
451         }
452 
453         @Test
testConfigureInInitState()454         public void testConfigureInInitState() throws IOException {
455             MediaFormat format = getSampleAudioFormat();
456             String mime = format.getString(MediaFormat.KEY_MIME);
457             mCodec = MediaCodec.createEncoderByType(mime);
458             boolean[] boolStates = {true, false};
459             for (boolean isAsync : boolStates) {
460                 configureCodec(format, isAsync, false, true);
461                 // configure in initialized state
462                 tryConfigureCodecInInvalidState(format, isAsync,
463                         "codec configure succeeds in initialized state");
464                 mCodec.stop();
465             }
466             mCodec.release();
467         }
468 
469         @Test
470         @Ignore("TODO(b/151894670)")
testConfigureAfterStart()471         public void testConfigureAfterStart() throws IOException, InterruptedException {
472             MediaFormat format = getSampleAudioFormat();
473             String mime = format.getString(MediaFormat.KEY_MIME);
474             mCodec = MediaCodec.createEncoderByType(mime);
475             boolean[] boolStates = {true, false};
476             for (boolean isAsync : boolStates) {
477                 configureCodec(format, isAsync, false, true);
478                 mCodec.start();
479                 // configure in running state
480                 tryConfigureCodecInInvalidState(format, isAsync,
481                         "codec configure succeeds after Start()");
482                 queueEOS();
483                 waitForAllOutputs();
484                 mCodec.stop();
485             }
486             mCodec.release();
487         }
488 
489         @Test
490         @Ignore("TODO(b/151894670)")
testConfigureAfterQueueInputBuffer()491         public void testConfigureAfterQueueInputBuffer() throws IOException, InterruptedException {
492             MediaFormat format = getSampleAudioFormat();
493             String mime = format.getString(MediaFormat.KEY_MIME);
494             mCodec = MediaCodec.createEncoderByType(mime);
495             boolean[] boolStates = {true, false};
496             for (boolean isAsync : boolStates) {
497                 configureCodec(format, isAsync, false, true);
498                 mCodec.start();
499                 queueEOS();
500                 // configure in running state
501                 tryConfigureCodecInInvalidState(format, isAsync,
502                         "codec configure succeeds after QueueInputBuffer()");
503                 waitForAllOutputs();
504                 mCodec.stop();
505             }
506             mCodec.release();
507         }
508 
509         @Test
testConfigureInEOSState()510         public void testConfigureInEOSState() throws IOException, InterruptedException {
511             MediaFormat format = getSampleAudioFormat();
512             String mime = format.getString(MediaFormat.KEY_MIME);
513             mCodec = MediaCodec.createEncoderByType(mime);
514             boolean[] boolStates = {true, false};
515             for (boolean isAsync : boolStates) {
516                 configureCodec(format, isAsync, false, true);
517                 mCodec.start();
518                 queueEOS();
519                 waitForAllOutputs();
520                 // configure in eos state
521                 tryConfigureCodecInInvalidState(format, isAsync,
522                         "codec configure succeeds in eos state");
523                 mCodec.stop();
524             }
525             mCodec.release();
526         }
527 
528         @Test
529         @Ignore("TODO(b/147576107)")
testConfigureInFlushState()530         public void testConfigureInFlushState() throws IOException, InterruptedException {
531             MediaFormat format = getSampleAudioFormat();
532             String mime = format.getString(MediaFormat.KEY_MIME);
533             mCodec = MediaCodec.createEncoderByType(mime);
534             boolean[] boolStates = {true, false};
535             for (boolean isAsync : boolStates) {
536                 configureCodec(format, isAsync, false, true);
537                 mCodec.start();
538                 flushCodec();
539                 // configure in flush state
540                 tryConfigureCodecInInvalidState(format, isAsync,
541                         "codec configure succeeds in flush state");
542                 if (mIsCodecInAsyncMode) mCodec.start();
543                 queueEOS();
544                 waitForAllOutputs();
545                 mCodec.stop();
546             }
547             mCodec.release();
548         }
549 
550         @Test
testConfigureInUnInitState()551         public void testConfigureInUnInitState() throws IOException {
552             MediaFormat format = getSampleAudioFormat();
553             String mime = format.getString(MediaFormat.KEY_MIME);
554             mCodec = MediaCodec.createEncoderByType(mime);
555             boolean[] boolStates = {true, false};
556             for (boolean isAsync : boolStates) {
557                 configureCodec(format, isAsync, false, true);
558                 mCodec.stop();
559                 // configure in uninitialized state
560                 try {
561                     configureCodec(format, isAsync, false, true);
562                 } catch (Exception e) {
563                     fail("codec configure fails in uninitialized state");
564                 }
565                 mCodec.stop();
566             }
567             mCodec.release();
568         }
569 
570         @Test
testConfigureInReleaseState()571         public void testConfigureInReleaseState() throws IOException {
572             MediaFormat format = getSampleAudioFormat();
573             String mime = format.getString(MediaFormat.KEY_MIME);
574             mCodec = MediaCodec.createEncoderByType(mime);
575             mCodec.release();
576             tryConfigureCodecInInvalidState(format, false,
577                     "codec configure succeeds in release state");
578         }
579 
580         @Test
testDequeueInputBufferInUnInitState()581         public void testDequeueInputBufferInUnInitState() throws IOException {
582             MediaFormat format = getSampleAudioFormat();
583             String mime = format.getString(MediaFormat.KEY_MIME);
584             mCodec = MediaCodec.createEncoderByType(mime);
585             boolean[] boolStates = {true, false};
586             for (boolean isAsync : boolStates) {
587                 // dequeue buffer in uninitialized state
588                 tryDequeueInputBufferInInvalidState(
589                         "dequeue input buffer succeeds in uninitialized state");
590                 configureCodec(format, isAsync, false, true);
591                 mCodec.start();
592                 mCodec.stop();
593                 // dequeue buffer in stopped state
594                 tryDequeueInputBufferInInvalidState(
595                         "dequeue input buffer succeeds in stopped state");
596             }
597             mCodec.release();
598         }
599 
600         @Test
testDequeueInputBufferInInitState()601         public void testDequeueInputBufferInInitState() throws IOException {
602             MediaFormat format = getSampleAudioFormat();
603             String mime = format.getString(MediaFormat.KEY_MIME);
604             mCodec = MediaCodec.createEncoderByType(mime);
605             boolean[] boolStates = {true, false};
606             for (boolean isAsync : boolStates) {
607                 configureCodec(format, isAsync, false, true);
608                 // dequeue buffer in initialized state
609                 tryDequeueInputBufferInInvalidState(
610                         "dequeue input buffer succeeds in initialized state");
611                 mCodec.stop();
612             }
613             mCodec.release();
614         }
615 
616         @Test
testDequeueInputBufferInRunningState()617         public void testDequeueInputBufferInRunningState()
618                 throws IOException, InterruptedException {
619             MediaFormat format = getSampleAudioFormat();
620             String mime = format.getString(MediaFormat.KEY_MIME);
621             mCodec = MediaCodec.createEncoderByType(mime);
622             boolean[] boolStates = {true, false};
623             for (boolean isAsync : boolStates) {
624                 configureCodec(format, isAsync, false, true);
625                 mCodec.start();
626                 if (mIsCodecInAsyncMode) {
627                     // dequeue buffer in running state
628                     tryDequeueInputBufferInInvalidState(
629                             "dequeue input buffer succeeds in running state, async mode");
630                 }
631                 queueEOS();
632                 waitForAllOutputs();
633                 mCodec.stop();
634             }
635             mCodec.release();
636         }
637 
638         @Test
testDequeueInputBufferInReleaseState()639         public void testDequeueInputBufferInReleaseState() throws IOException {
640             MediaFormat format = getSampleAudioFormat();
641             String mime = format.getString(MediaFormat.KEY_MIME);
642             mCodec = MediaCodec.createEncoderByType(mime);
643             mCodec.release();
644             // dequeue buffer in released state
645             tryDequeueInputBufferInInvalidState(
646                     "dequeue input buffer succeeds in release state");
647         }
648 
649         @Test
testDequeueOutputBufferInUnInitState()650         public void testDequeueOutputBufferInUnInitState() throws IOException {
651             MediaFormat format = getSampleAudioFormat();
652             String mime = format.getString(MediaFormat.KEY_MIME);
653             mCodec = MediaCodec.createEncoderByType(mime);
654             boolean[] boolStates = {true, false};
655             for (boolean isAsync : boolStates) {
656                 // dequeue buffer in uninitialized state
657                 tryDequeueOutputBufferInInvalidState(
658                         "dequeue output buffer succeeds in uninitialized state");
659                 configureCodec(format, isAsync, false, true);
660                 mCodec.start();
661                 mCodec.stop();
662                 // dequeue buffer in stopped state
663                 tryDequeueOutputBufferInInvalidState(
664                         "dequeue output buffer succeeds in stopped state");
665             }
666             mCodec.release();
667         }
668 
669         @Test
testDequeueOutputBufferInInitState()670         public void testDequeueOutputBufferInInitState() throws IOException {
671             MediaFormat format = getSampleAudioFormat();
672             String mime = format.getString(MediaFormat.KEY_MIME);
673             mCodec = MediaCodec.createEncoderByType(mime);
674             boolean[] boolStates = {true, false};
675             for (boolean isAsync : boolStates) {
676                 configureCodec(format, isAsync, false, true);
677                 // dequeue buffer in initialized state
678                 tryDequeueOutputBufferInInvalidState(
679                         "dequeue output buffer succeeds in initialized state");
680                 mCodec.stop();
681             }
682             mCodec.release();
683         }
684 
685         @Test
testDequeueOutputBufferInRunningState()686         public void testDequeueOutputBufferInRunningState()
687                 throws IOException, InterruptedException {
688             MediaFormat format = getSampleAudioFormat();
689             String mime = format.getString(MediaFormat.KEY_MIME);
690             mCodec = MediaCodec.createEncoderByType(mime);
691             boolean[] boolStates = {true, false};
692             for (boolean isAsync : boolStates) {
693                 configureCodec(format, isAsync, false, true);
694                 mCodec.start();
695                 if (mIsCodecInAsyncMode) {
696                     // dequeue buffer in running state
697                     tryDequeueOutputBufferInInvalidState(
698                             "dequeue output buffer succeeds in running state, async mode");
699                 }
700                 queueEOS();
701                 waitForAllOutputs();
702                 mCodec.stop();
703             }
704             mCodec.release();
705         }
706 
707         @Test
testDequeueOutputBufferInReleaseState()708         public void testDequeueOutputBufferInReleaseState() throws IOException {
709             MediaFormat format = getSampleAudioFormat();
710             String mime = format.getString(MediaFormat.KEY_MIME);
711             mCodec = MediaCodec.createEncoderByType(mime);
712             mCodec.release();
713             // dequeue buffer in released state
714             tryDequeueOutputBufferInInvalidState(
715                     "dequeue output buffer succeeds in release state");
716         }
717 
718         @Test
testFlushInUnInitState()719         public void testFlushInUnInitState() throws IOException {
720             MediaFormat format = getSampleAudioFormat();
721             String mime = format.getString(MediaFormat.KEY_MIME);
722             mCodec = MediaCodec.createEncoderByType(mime);
723             boolean[] boolStates = {true, false};
724             for (boolean isAsync : boolStates) {
725                 // flush uninitialized state
726                 tryFlushInInvalidState("codec flush succeeds in uninitialized state");
727                 configureCodec(format, isAsync, false, true);
728                 mCodec.start();
729                 mCodec.stop();
730                 // flush in stopped state
731                 tryFlushInInvalidState("codec flush succeeds in stopped state");
732                 mCodec.reset();
733             }
734             mCodec.release();
735         }
736 
737         @Test
testFlushInInitState()738         public void testFlushInInitState() throws IOException {
739             MediaFormat format = getSampleAudioFormat();
740             String mime = format.getString(MediaFormat.KEY_MIME);
741             mCodec = MediaCodec.createEncoderByType(mime);
742             boolean[] boolStates = {true, false};
743             for (boolean isAsync : boolStates) {
744                 configureCodec(format, isAsync, false, true);
745                 // flush in initialized state
746                 tryFlushInInvalidState("codec flush succeeds in initialized state");
747                 mCodec.stop();
748             }
749             mCodec.release();
750         }
751 
752         @Test
753         @Ignore("TODO(b/147576107)")
testFlushInRunningState()754         public void testFlushInRunningState() throws IOException, InterruptedException {
755             MediaFormat format = getSampleAudioFormat();
756             String mime = format.getString(MediaFormat.KEY_MIME);
757             mCodec = MediaCodec.createEncoderByType(mime);
758             configureCodec(format, true, false, true);
759             mCodec.start();
760             flushCodec();
761             Thread.sleep(STALL_TIME_MS);
762             assertTrue("received input buffer callback before start",
763                     mAsyncHandle.isInputQueueEmpty());
764             mCodec.start();
765             Thread.sleep(STALL_TIME_MS);
766             assertFalse("did not receive input buffer callback after start",
767                     mAsyncHandle.isInputQueueEmpty());
768             mCodec.stop();
769             mCodec.release();
770         }
771 
772         @Test
testFlushInReleaseState()773         public void testFlushInReleaseState() throws IOException {
774             MediaFormat format = getSampleAudioFormat();
775             String mime = format.getString(MediaFormat.KEY_MIME);
776             mCodec = MediaCodec.createEncoderByType(mime);
777             mCodec.release();
778             tryFlushInInvalidState("codec flush succeeds in release state");
779         }
780 
781         @Test
testGetMetaDataInUnInitState()782         public void testGetMetaDataInUnInitState() throws IOException, InterruptedException {
783             MediaFormat format = getSampleAudioFormat();
784             String mime = format.getString(MediaFormat.KEY_MIME);
785             mCodec = MediaCodec.createEncoderByType(mime);
786             boolean[] boolStates = {true, false};
787             for (boolean isAsync : boolStates) {
788                 tryGetMetaData("codec get metadata call fails in uninitialized state");
789                 configureCodec(format, isAsync, false, true);
790                 mCodec.start();
791                 queueEOS();
792                 waitForAllOutputs();
793                 mCodec.stop();
794                 tryGetMetaData("codec get metadata call fails in stopped state");
795                 mCodec.reset();
796             }
797             mCodec.release();
798         }
799 
800         @Test
testGetMetaDataInInitState()801         public void testGetMetaDataInInitState() throws IOException {
802             MediaFormat format = getSampleAudioFormat();
803             String mime = format.getString(MediaFormat.KEY_MIME);
804             mCodec = MediaCodec.createEncoderByType(mime);
805             boolean[] boolStates = {true, false};
806             for (boolean isAsync : boolStates) {
807                 configureCodec(format, isAsync, false, true);
808                 tryGetMetaData("codec get metadata call fails in initialized state");
809                 mCodec.stop();
810             }
811             mCodec.release();
812         }
813 
814         @Test
testGetMetaDataInRunningState()815         public void testGetMetaDataInRunningState() throws IOException, InterruptedException {
816             MediaFormat format = getSampleAudioFormat();
817             String mime = format.getString(MediaFormat.KEY_MIME);
818             mCodec = MediaCodec.createEncoderByType(mime);
819             boolean[] boolStates = {true, false};
820             for (boolean isAsync : boolStates) {
821                 configureCodec(format, isAsync, false, true);
822                 mCodec.start();
823                 tryGetMetaData("codec get metadata call fails in running state");
824                 queueEOS();
825                 waitForAllOutputs();
826                 tryGetMetaData("codec get metadata call fails in eos state");
827                 mCodec.stop();
828                 mCodec.reset();
829             }
830             mCodec.release();
831         }
832 
833         @Test
testGetMetaDataInReleaseState()834         public void testGetMetaDataInReleaseState() throws IOException {
835             MediaFormat format = getSampleAudioFormat();
836             String mime = format.getString(MediaFormat.KEY_MIME);
837             mCodec = MediaCodec.createEncoderByType(mime);
838             mCodec.release();
839             try {
840                 mCodec.getCanonicalName();
841                 fail("get canonical name succeeds after codec release");
842             } catch (IllegalStateException e) {
843                 // expected
844             }
845 
846             try {
847                 mCodec.getCodecInfo();
848                 fail("get codec info succeeds after codec release");
849             } catch (IllegalStateException e) {
850                 // expected
851             }
852 
853             try {
854                 mCodec.getName();
855                 fail("get name succeeds after codec release");
856             } catch (IllegalStateException e) {
857                 // expected
858             }
859 
860             try {
861                 mCodec.getMetrics();
862                 fail("get metrics succeeds after codec release");
863             } catch (IllegalStateException e) {
864                 // expected
865             }
866         }
867 
868         @Test
testSetCallBackInUnInitState()869         public void testSetCallBackInUnInitState() throws IOException, InterruptedException {
870             MediaFormat format = getSampleAudioFormat();
871             String mime = format.getString(MediaFormat.KEY_MIME);
872             mCodec = MediaCodec.createEncoderByType(mime);
873 
874             boolean isAsync = true;
875             // set component in async mode
876             mAsyncHandle.setCallBack(mCodec, isAsync);
877             mIsCodecInAsyncMode = isAsync;
878             // configure component to sync mode
879             configureCodec(format, !isAsync, false, true);
880             mCodec.start();
881             queueEOS();
882             waitForAllOutputs();
883             mCodec.stop();
884 
885             // set component in sync mode
886             mAsyncHandle.setCallBack(mCodec, !isAsync);
887             mIsCodecInAsyncMode = !isAsync;
888             // configure component in async mode
889             configureCodec(format, isAsync, false, true);
890             mCodec.start();
891             queueEOS();
892             waitForAllOutputs();
893             mCodec.stop();
894             mCodec.release();
895         }
896 
897         @Test
testSetCallBackInInitState()898         public void testSetCallBackInInitState() throws IOException, InterruptedException {
899             MediaFormat format = getSampleAudioFormat();
900             String mime = format.getString(MediaFormat.KEY_MIME);
901             mCodec = MediaCodec.createEncoderByType(mime);
902 
903             // configure component in async mode
904             boolean isAsync = true;
905             configureCodec(format, isAsync, false, true);
906             // change component to sync mode
907             mAsyncHandle.setCallBack(mCodec, !isAsync);
908             mIsCodecInAsyncMode = !isAsync;
909             mCodec.start();
910             queueEOS();
911             waitForAllOutputs();
912             mCodec.stop();
913 
914             // configure component in sync mode
915             configureCodec(format, !isAsync, false, true);
916             // change the component to operate in async mode
917             mAsyncHandle.setCallBack(mCodec, isAsync);
918             mIsCodecInAsyncMode = isAsync;
919             mCodec.start();
920             queueEOS();
921             waitForAllOutputs();
922             mCodec.stop();
923             mCodec.release();
924         }
925 
926         @Test
927         @Ignore("TODO(b/151305056)")
testSetCallBackInRunningState()928         public void testSetCallBackInRunningState() throws IOException, InterruptedException {
929             MediaFormat format = getSampleAudioFormat();
930             String mime = format.getString(MediaFormat.KEY_MIME);
931             mCodec = MediaCodec.createEncoderByType(mime);
932             boolean isAsync = false;
933             // configure codec in sync mode
934             configureCodec(format, isAsync, false, true);
935             mCodec.start();
936             // set call back should fail once the component is sailed to running state
937             try {
938                 mAsyncHandle.setCallBack(mCodec, !isAsync);
939                 mIsCodecInAsyncMode = !isAsync;
940                 fail("set call back succeeds in running state");
941             } catch (IllegalStateException e) {
942                 // expected
943             }
944             queueEOS();
945             waitForAllOutputs();
946             mCodec.stop();
947 
948             // configure codec in async mode
949             configureCodec(format, !isAsync, false, true);
950             mCodec.start();
951             // set call back should fail once the component is sailed to running state
952             try {
953                 mAsyncHandle.setCallBack(mCodec, isAsync);
954                 mIsCodecInAsyncMode = isAsync;
955                 fail("set call back succeeds in running state");
956             } catch (IllegalStateException e) {
957                 // expected
958             }
959             queueEOS();
960             waitForAllOutputs();
961             mCodec.stop();
962             mCodec.release();
963         }
964 
965         @Test
testSetCallBackInReleaseState()966         public void testSetCallBackInReleaseState() throws IOException {
967             MediaFormat format = getSampleAudioFormat();
968             String mime = format.getString(MediaFormat.KEY_MIME);
969             mCodec = MediaCodec.createEncoderByType(mime);
970             mCodec.release();
971             // set callbacks in release state
972             try {
973                 mAsyncHandle.setCallBack(mCodec, false);
974                 fail("set call back succeeds in released state");
975             } catch (IllegalStateException e) {
976                 // expected
977             }
978         }
979 
980         @Test
testGetInputBufferInUnInitState()981         public void testGetInputBufferInUnInitState() throws IOException {
982             MediaFormat format = getSampleAudioFormat();
983             String mime = format.getString(MediaFormat.KEY_MIME);
984             mCodec = MediaCodec.createEncoderByType(mime);
985             boolean[] boolStates = {true, false};
986             for (boolean isAsync : boolStates) {
987                 tryGetInputBufferInInvalidState("getInputBuffer succeeds in uninitialized state");
988                 configureCodec(format, isAsync, false, true);
989                 mCodec.start();
990                 mCodec.stop();
991                 tryGetInputBufferInInvalidState("getInputBuffer succeeds in stopped state");
992                 mCodec.reset();
993             }
994             mCodec.release();
995         }
996 
997         @Test
testGetInputBufferInInitState()998         public void testGetInputBufferInInitState() throws IOException {
999             MediaFormat format = getSampleAudioFormat();
1000             String mime = format.getString(MediaFormat.KEY_MIME);
1001             mCodec = MediaCodec.createEncoderByType(mime);
1002             boolean[] boolStates = {true, false};
1003             for (boolean isAsync : boolStates) {
1004                 configureCodec(format, isAsync, false, true);
1005                 tryGetInputBufferInInvalidState("getInputBuffer succeeds in initialized state");
1006                 mCodec.reset();
1007             }
1008             mCodec.release();
1009         }
1010 
1011         @Test
1012         @Ignore("TODO(b/151304147)")
testGetInputBufferInRunningState()1013         public void testGetInputBufferInRunningState() throws IOException, InterruptedException {
1014             MediaFormat format = getSampleAudioFormat();
1015             String mime = format.getString(MediaFormat.KEY_MIME);
1016             mCodec = MediaCodec.createEncoderByType(mime);
1017             boolean[] boolStates = {true, false};
1018             for (boolean isAsync : boolStates) {
1019                 configureCodec(format, isAsync, false, true);
1020                 mCodec.start();
1021                 try {
1022                     ByteBuffer buffer = mCodec.getInputBuffer(-1);
1023                     assertNull("getInputBuffer succeeds for bad buffer index " + -1, buffer);
1024                 } catch (Exception e) {
1025                     fail("getInputBuffer rec/exp :: " + e.toString() + " / null");
1026                 }
1027                 int bufferIndex = mIsCodecInAsyncMode ? mAsyncHandle.getInput().first :
1028                         mCodec.dequeueInputBuffer(-1);
1029                 ByteBuffer buffer = mCodec.getInputBuffer(bufferIndex);
1030                 assertNotNull(buffer);
1031                 ByteBuffer bufferDup = mCodec.getInputBuffer(bufferIndex);
1032                 assertNotNull(bufferDup);
1033                 enqueueEOS(bufferIndex);
1034                 waitForAllOutputs();
1035                 mCodec.stop();
1036                 mCodec.reset();
1037             }
1038             mCodec.release();
1039         }
1040 
1041         @Test
testGetInputBufferInReleaseState()1042         public void testGetInputBufferInReleaseState() throws IOException {
1043             MediaFormat format = getSampleAudioFormat();
1044             String mime = format.getString(MediaFormat.KEY_MIME);
1045             mCodec = MediaCodec.createEncoderByType(mime);
1046             mCodec.release();
1047             tryGetInputBufferInInvalidState("getInputBuffer succeeds in release state");
1048         }
1049 
1050         @Test
testGetInputFormatInUnInitState()1051         public void testGetInputFormatInUnInitState() throws IOException {
1052             MediaFormat format = getSampleAudioFormat();
1053             String mime = format.getString(MediaFormat.KEY_MIME);
1054             mCodec = MediaCodec.createEncoderByType(mime);
1055             boolean[] boolStates = {true, false};
1056             for (boolean isAsync : boolStates) {
1057                 tryGetInputFormatInInvalidState("getInputFormat succeeds in uninitialized state");
1058                 configureCodec(format, isAsync, false, true);
1059                 mCodec.start();
1060                 mCodec.stop();
1061                 tryGetInputFormatInInvalidState("getInputFormat succeeds in stopped state");
1062                 mCodec.reset();
1063             }
1064             mCodec.release();
1065         }
1066 
1067         @Test
testGetInputFormatInInitState()1068         public void testGetInputFormatInInitState() throws IOException {
1069             MediaFormat format = getSampleAudioFormat();
1070             String mime = format.getString(MediaFormat.KEY_MIME);
1071             mCodec = MediaCodec.createEncoderByType(mime);
1072             boolean[] boolStates = {true, false};
1073             for (boolean isAsync : boolStates) {
1074                 configureCodec(format, isAsync, false, true);
1075                 try {
1076                     mCodec.getInputFormat();
1077                 } catch (Exception e) {
1078                     fail("getInputFormat fails in initialized state");
1079                 }
1080                 mCodec.start();
1081                 mCodec.stop();
1082             }
1083             mCodec.release();
1084         }
1085 
1086         @Test
testGetInputFormatInRunningState()1087         public void testGetInputFormatInRunningState() throws IOException {
1088             MediaFormat format = getSampleAudioFormat();
1089             String mime = format.getString(MediaFormat.KEY_MIME);
1090             mCodec = MediaCodec.createEncoderByType(mime);
1091             boolean[] boolStates = {true, false};
1092             for (boolean isAsync : boolStates) {
1093                 configureCodec(format, isAsync, false, true);
1094                 mCodec.start();
1095                 try {
1096                     mCodec.getInputFormat();
1097                 } catch (Exception e) {
1098                     fail("getInputFormat fails in running state");
1099                 }
1100                 mCodec.stop();
1101             }
1102             mCodec.release();
1103         }
1104 
1105         @Test
testGetInputFormatInReleaseState()1106         public void testGetInputFormatInReleaseState() throws IOException {
1107             MediaFormat format = getSampleAudioFormat();
1108             String mime = format.getString(MediaFormat.KEY_MIME);
1109             mCodec = MediaCodec.createEncoderByType(mime);
1110             mCodec.release();
1111             tryGetInputFormatInInvalidState("getInputFormat succeeds in release state");
1112         }
1113 
1114         @Test
testGetOutputBufferInUnInitState()1115         public void testGetOutputBufferInUnInitState() throws IOException {
1116             MediaFormat format = getSampleAudioFormat();
1117             String mime = format.getString(MediaFormat.KEY_MIME);
1118             mCodec = MediaCodec.createEncoderByType(mime);
1119             boolean[] boolStates = {true, false};
1120             for (boolean isAsync : boolStates) {
1121                 tryGetOutputBufferInInvalidState("getOutputBuffer succeeds in uninitialized state");
1122                 configureCodec(format, isAsync, false, true);
1123                 mCodec.start();
1124                 mCodec.stop();
1125                 tryGetOutputBufferInInvalidState("getOutputBuffer succeeds in stopped state");
1126                 mCodec.reset();
1127             }
1128             mCodec.release();
1129         }
1130 
1131         @Test
testGetOutputBufferInInitState()1132         public void testGetOutputBufferInInitState() throws IOException {
1133             MediaFormat format = getSampleAudioFormat();
1134             String mime = format.getString(MediaFormat.KEY_MIME);
1135             mCodec = MediaCodec.createEncoderByType(mime);
1136             boolean[] boolStates = {true, false};
1137             for (boolean isAsync : boolStates) {
1138                 configureCodec(format, isAsync, false, true);
1139                 tryGetOutputBufferInInvalidState("getOutputBuffer succeeds in initialized state");
1140                 mCodec.reset();
1141             }
1142             mCodec.release();
1143         }
1144 
1145         @Test
1146         @Ignore("TODO(b/151304147)")
testGetOutputBufferInRunningState()1147         public void testGetOutputBufferInRunningState() throws IOException, InterruptedException {
1148             MediaFormat format = getSampleAudioFormat();
1149             MediaCodec.BufferInfo outInfo = new MediaCodec.BufferInfo();
1150             String mime = format.getString(MediaFormat.KEY_MIME);
1151             mCodec = MediaCodec.createEncoderByType(mime);
1152             boolean[] boolStates = {true, false};
1153             for (boolean isAsync : boolStates) {
1154                 configureCodec(format, isAsync, false, true);
1155                 mCodec.start();
1156                 try {
1157                     ByteBuffer buffer = mCodec.getOutputBuffer(-1);
1158                     assertNull("getOutputBuffer succeeds for bad buffer index " + -1, buffer);
1159                 } catch (Exception e) {
1160                     fail("getOutputBuffer rec/exp :: " + e.toString() + " / null");
1161                 }
1162                 queueEOS();
1163                 int bufferIndex = 0;
1164                 while (!mSawOutputEOS) {
1165                     if (mIsCodecInAsyncMode) {
1166                         Pair<Integer, MediaCodec.BufferInfo> element = mAsyncHandle.getOutput();
1167                         bufferIndex = element.first;
1168                         ByteBuffer buffer = mCodec.getOutputBuffer(bufferIndex);
1169                         assertNotNull(buffer);
1170                         dequeueOutput(element.first, element.second);
1171                     } else {
1172                         bufferIndex = mCodec.dequeueOutputBuffer(outInfo, Q_DEQ_TIMEOUT_US);
1173                         if (bufferIndex >= 0) {
1174                             ByteBuffer buffer = mCodec.getOutputBuffer(bufferIndex);
1175                             assertNotNull(buffer);
1176                             dequeueOutput(bufferIndex, outInfo);
1177                         }
1178                     }
1179                 }
1180                 try {
1181                     ByteBuffer buffer = mCodec.getOutputBuffer(bufferIndex);
1182                     assertNull("getOutputBuffer succeeds for buffer index not owned by client",
1183                             buffer);
1184                 } catch (Exception e) {
1185                     fail("getOutputBuffer rec/exp :: " + e.toString() + " / null");
1186                 }
1187                 mCodec.stop();
1188                 mCodec.reset();
1189             }
1190             mCodec.release();
1191         }
1192 
1193         @Test
testGetOutputBufferInReleaseState()1194         public void testGetOutputBufferInReleaseState() throws IOException {
1195             MediaFormat format = getSampleAudioFormat();
1196             String mime = format.getString(MediaFormat.KEY_MIME);
1197             mCodec = MediaCodec.createEncoderByType(mime);
1198             mCodec.release();
1199             tryGetOutputBufferInInvalidState("getOutputBuffer succeeds in release state");
1200         }
1201 
1202         @Test
testGetOutputFormatInUnInitState()1203         public void testGetOutputFormatInUnInitState() throws IOException {
1204             MediaFormat format = getSampleAudioFormat();
1205             String mime = format.getString(MediaFormat.KEY_MIME);
1206             mCodec = MediaCodec.createEncoderByType(mime);
1207             boolean[] boolStates = {true, false};
1208             for (boolean isAsync : boolStates) {
1209                 tryGetOutputFormatInInvalidState("getOutputFormat succeeds in uninitialized state");
1210                 configureCodec(format, isAsync, false, true);
1211                 mCodec.start();
1212                 mCodec.stop();
1213                 tryGetOutputFormatInInvalidState("getOutputFormat succeeds in stopped state");
1214                 mCodec.reset();
1215             }
1216             mCodec.release();
1217         }
1218 
1219         @Test
testGetOutputFormatInInitState()1220         public void testGetOutputFormatInInitState() throws IOException {
1221             MediaFormat format = getSampleAudioFormat();
1222             String mime = format.getString(MediaFormat.KEY_MIME);
1223             mCodec = MediaCodec.createEncoderByType(mime);
1224             boolean[] boolStates = {true, false};
1225             for (boolean isAsync : boolStates) {
1226                 configureCodec(format, isAsync, false, true);
1227                 try {
1228                     mCodec.getOutputFormat();
1229                 } catch (Exception e) {
1230                     fail("getOutputFormat fails in initialized state");
1231                 }
1232                 try {
1233                     mCodec.getOutputFormat(0);
1234                     fail("getOutputFormat succeeds in released state");
1235                 } catch (IllegalStateException e) {
1236                     // expected
1237                 }
1238                 mCodec.start();
1239                 mCodec.stop();
1240             }
1241             mCodec.release();
1242         }
1243 
1244         @Test
1245         @Ignore("TODO(b/151304147)")
testGetOutputFormatInRunningState()1246         public void testGetOutputFormatInRunningState() throws IOException, InterruptedException {
1247             MediaFormat format = getSampleAudioFormat();
1248             MediaCodec.BufferInfo outInfo = new MediaCodec.BufferInfo();
1249             String mime = format.getString(MediaFormat.KEY_MIME);
1250             mCodec = MediaCodec.createEncoderByType(mime);
1251             boolean[] boolStates = {true, false};
1252             for (boolean isAsync : boolStates) {
1253                 configureCodec(format, isAsync, false, true);
1254                 mCodec.start();
1255                 queueEOS();
1256                 try {
1257                     mCodec.getOutputFormat();
1258                 } catch (Exception e) {
1259                     fail("getOutputFormat fails in running state");
1260                 }
1261                 try {
1262                     MediaFormat outputFormat = mCodec.getOutputFormat(-1);
1263                     assertNull("getOutputFormat succeeds for bad buffer index " + -1, outputFormat);
1264                 } catch (Exception e) {
1265                     fail("getOutputFormat rec/exp :: " + e.toString() + " / null");
1266                 }
1267                 int bufferIndex = 0;
1268                 while (!mSawOutputEOS) {
1269                     if (mIsCodecInAsyncMode) {
1270                         Pair<Integer, MediaCodec.BufferInfo> element = mAsyncHandle.getOutput();
1271                         bufferIndex = element.first;
1272                         MediaFormat outputFormat = mCodec.getOutputFormat(bufferIndex);
1273                         assertNotNull(outputFormat);
1274                         dequeueOutput(element.first, element.second);
1275                     } else {
1276                         bufferIndex = mCodec.dequeueOutputBuffer(outInfo, Q_DEQ_TIMEOUT_US);
1277                         if (bufferIndex >= 0) {
1278                             MediaFormat outputFormat = mCodec.getOutputFormat(bufferIndex);
1279                             assertNotNull(outputFormat);
1280                             dequeueOutput(bufferIndex, outInfo);
1281                         }
1282                     }
1283                 }
1284                 try {
1285                     MediaFormat outputFormat = mCodec.getOutputFormat(bufferIndex);
1286                     assertNull("getOutputFormat succeeds for index not owned by client",
1287                             outputFormat);
1288                 } catch (Exception e) {
1289                     fail("getOutputFormat rec/exp :: " + e.toString() + " / null");
1290                 }
1291                 mCodec.stop();
1292             }
1293             mCodec.release();
1294         }
1295 
1296         @Test
testGetOutputFormatInReleaseState()1297         public void testGetOutputFormatInReleaseState() throws IOException {
1298             MediaFormat format = getSampleAudioFormat();
1299             String mime = format.getString(MediaFormat.KEY_MIME);
1300             mCodec = MediaCodec.createEncoderByType(mime);
1301             mCodec.release();
1302             tryGetOutputFormatInInvalidState("getOutputFormat succeeds in release state");
1303         }
1304 
1305         @Test
testSetParametersInUnInitState()1306         public void testSetParametersInUnInitState() throws IOException {
1307             MediaFormat format = getSampleVideoFormat();
1308             String mime = format.getString(MediaFormat.KEY_MIME);
1309             int bitrate = format.getInteger(MediaFormat.KEY_BIT_RATE);
1310             mCodec = MediaCodec.createEncoderByType(mime);
1311             // call set param in uninitialized state
1312             mCodec.setParameters(null);
1313             mCodec.setParameters(updateBitrate(bitrate >> 1));
1314             boolean[] boolStates = {true, false};
1315             for (boolean isAsync : boolStates) {
1316                 configureCodec(format, isAsync, false, true);
1317                 mCodec.start();
1318                 mCodec.stop();
1319                 mCodec.setParameters(null);
1320                 mCodec.setParameters(updateBitrate(bitrate >> 1));
1321                 mCodec.reset();
1322             }
1323             mCodec.release();
1324         }
1325 
1326         @Test
testSetParametersInInitState()1327         public void testSetParametersInInitState() throws IOException {
1328             MediaFormat format = getSampleVideoFormat();
1329             String mime = format.getString(MediaFormat.KEY_MIME);
1330             int bitrate = format.getInteger(MediaFormat.KEY_BIT_RATE);
1331             mCodec = MediaCodec.createEncoderByType(mime);
1332             boolean[] boolStates = {true, false};
1333             for (boolean isAsync : boolStates) {
1334                 configureCodec(format, isAsync, false, true);
1335                 mCodec.setParameters(null);
1336                 mCodec.setParameters(updateBitrate(bitrate >> 1));
1337                 mCodec.start();
1338                 mCodec.stop();
1339                 mCodec.reset();
1340             }
1341             mCodec.release();
1342         }
1343 
1344         @Test
testSetParametersInRunningState()1345         public void testSetParametersInRunningState() throws IOException, InterruptedException {
1346             MediaFormat format = getSampleVideoFormat();
1347             String mime = format.getString(MediaFormat.KEY_MIME);
1348             int bitrate = format.getInteger(MediaFormat.KEY_BIT_RATE);
1349             mCodec = MediaCodec.createEncoderByType(mime);
1350             boolean[] boolStates = {true, false};
1351             for (boolean isAsync : boolStates) {
1352                 configureCodec(format, isAsync, false, true);
1353                 mCodec.start();
1354                 mCodec.setParameters(null);
1355                 mCodec.setParameters(updateBitrate(bitrate >> 1));
1356                 queueEOS();
1357                 mCodec.setParameters(null);
1358                 mCodec.setParameters(updateBitrate(bitrate << 1));
1359                 waitForAllOutputs();
1360                 mCodec.setParameters(null);
1361                 mCodec.setParameters(updateBitrate(bitrate));
1362                 mCodec.stop();
1363                 mCodec.reset();
1364             }
1365             mCodec.release();
1366         }
1367 
1368         @Test
testSetParametersInReleaseState()1369         public void testSetParametersInReleaseState() throws IOException {
1370             MediaFormat format = getSampleVideoFormat();
1371             String mime = format.getString(MediaFormat.KEY_MIME);
1372             int bitrate = format.getInteger(MediaFormat.KEY_BIT_RATE);
1373             mCodec = MediaCodec.createEncoderByType(mime);
1374             mCodec.release();
1375             try {
1376                 mCodec.setParameters(updateBitrate(bitrate >> 1));
1377                 fail("Codec set parameter succeeds in release mode");
1378             } catch (IllegalStateException e) {
1379                 // expected
1380             }
1381         }
1382 
1383         @Test
testStartInUnInitState()1384         public void testStartInUnInitState() throws IOException {
1385             MediaFormat format = getSampleAudioFormat();
1386             String mime = format.getString(MediaFormat.KEY_MIME);
1387             mCodec = MediaCodec.createEncoderByType(mime);
1388             // call start in uninitialized state
1389             tryStartInInvalidState("codec start succeeds before initialization");
1390             configureCodec(format, false, false, true);
1391             mCodec.start();
1392             mCodec.stop();
1393             // call start in stopped state
1394             tryStartInInvalidState("codec start succeeds in stopped state");
1395             mCodec.release();
1396         }
1397 
1398         @Test
testStartInRunningState()1399         public void testStartInRunningState() throws IOException {
1400             MediaFormat format = getSampleAudioFormat();
1401             String mime = format.getString(MediaFormat.KEY_MIME);
1402             mCodec = MediaCodec.createEncoderByType(mime);
1403             configureCodec(format, false, false, true);
1404             mCodec.start();
1405             // call start in running state
1406             tryStartInInvalidState("codec start succeeds in running state");
1407             mCodec.stop();
1408             mCodec.release();
1409         }
1410 
1411         @Test
testStartInReleaseState()1412         public void testStartInReleaseState() throws IOException {
1413             MediaFormat format = getSampleAudioFormat();
1414             String mime = format.getString(MediaFormat.KEY_MIME);
1415             mCodec = MediaCodec.createEncoderByType(mime);
1416             mCodec.release();
1417             // call start in release state
1418             tryStartInInvalidState("codec start succeeds in release state");
1419         }
1420 
1421         @Test
testStopInUnInitState()1422         public void testStopInUnInitState() throws IOException {
1423             MediaFormat format = getSampleAudioFormat();
1424             String mime = format.getString(MediaFormat.KEY_MIME);
1425             mCodec = MediaCodec.createEncoderByType(mime);
1426             mCodec.stop();
1427             boolean[] boolStates = {true, false};
1428             for (boolean isAsync : boolStates) {
1429                 configureCodec(format, isAsync, false, true);
1430                 mCodec.start();
1431                 mCodec.stop();
1432                 mCodec.stop();
1433             }
1434             mCodec.release();
1435         }
1436 
1437         @Test
testStopInInitState()1438         public void testStopInInitState() throws IOException {
1439             MediaFormat format = getSampleAudioFormat();
1440             String mime = format.getString(MediaFormat.KEY_MIME);
1441             mCodec = MediaCodec.createEncoderByType(mime);
1442             boolean[] boolStates = {true, false};
1443             for (boolean isAsync : boolStates) {
1444                 configureCodec(format, isAsync, false, true);
1445                 mCodec.stop();
1446             }
1447             mCodec.release();
1448         }
1449 
1450         @Test
testStopInRunningState()1451         public void testStopInRunningState() throws IOException, InterruptedException {
1452             MediaFormat format = getSampleAudioFormat();
1453             String mime = format.getString(MediaFormat.KEY_MIME);
1454             mCodec = MediaCodec.createEncoderByType(mime);
1455             boolean[] boolStates = {true, false};
1456             for (boolean isAsync : boolStates) {
1457                 configureCodec(format, isAsync, false, true);
1458                 mCodec.start();
1459                 queueEOS();
1460                 mCodec.stop();
1461             }
1462             mCodec.release();
1463         }
1464 
1465         @Test
testStopInReleaseState()1466         public void testStopInReleaseState() throws IOException {
1467             MediaFormat format = getSampleAudioFormat();
1468             String mime = format.getString(MediaFormat.KEY_MIME);
1469             mCodec = MediaCodec.createEncoderByType(mime);
1470             mCodec.release();
1471             try {
1472                 mCodec.stop();
1473                 fail("Codec stop succeeds in release mode");
1474             } catch (IllegalStateException e) {
1475                 // expected
1476             }
1477         }
1478 
1479         @Test
testResetInUnInitState()1480         public void testResetInUnInitState() throws IOException {
1481             MediaFormat format = getSampleAudioFormat();
1482             String mime = format.getString(MediaFormat.KEY_MIME);
1483             mCodec = MediaCodec.createEncoderByType(mime);
1484             mCodec.reset();
1485             boolean[] boolStates = {true, false};
1486             for (boolean isAsync : boolStates) {
1487                 configureCodec(format, isAsync, false, true);
1488                 mCodec.start();
1489                 mCodec.stop();
1490                 mCodec.reset();
1491             }
1492             mCodec.release();
1493         }
1494 
1495         @Test
testResetInInitState()1496         public void testResetInInitState() throws IOException {
1497             MediaFormat format = getSampleAudioFormat();
1498             String mime = format.getString(MediaFormat.KEY_MIME);
1499             mCodec = MediaCodec.createEncoderByType(mime);
1500             boolean[] boolStates = {true, false};
1501             for (boolean isAsync : boolStates) {
1502                 configureCodec(format, isAsync, false, true);
1503                 mCodec.reset();
1504             }
1505             mCodec.release();
1506         }
1507 
1508         @Test
testResetInRunningState()1509         public void testResetInRunningState() throws IOException, InterruptedException {
1510             MediaFormat format = getSampleAudioFormat();
1511             String mime = format.getString(MediaFormat.KEY_MIME);
1512             mCodec = MediaCodec.createEncoderByType(mime);
1513             boolean[] boolStates = {true, false};
1514             for (boolean isAsync : boolStates) {
1515                 configureCodec(format, isAsync, false, true);
1516                 mCodec.start();
1517                 queueEOS();
1518                 mCodec.reset();
1519             }
1520             mCodec.release();
1521         }
1522 
1523         @Test
testResetInReleaseState()1524         public void testResetInReleaseState() throws IOException {
1525             MediaFormat format = getSampleAudioFormat();
1526             String mime = format.getString(MediaFormat.KEY_MIME);
1527             mCodec = MediaCodec.createEncoderByType(mime);
1528             mCodec.release();
1529             try {
1530                 mCodec.reset();
1531                 fail("Codec reset succeeds in release mode");
1532             } catch (IllegalStateException e) {
1533                 // expected
1534             }
1535         }
1536 
1537         @Test
testGetInputImageInUnInitState()1538         public void testGetInputImageInUnInitState() throws IOException {
1539             MediaFormat format = getSampleVideoFormat();
1540             String mime = format.getString(MediaFormat.KEY_MIME);
1541             mCodec = MediaCodec.createEncoderByType(mime);
1542             boolean[] boolStates = {true, false};
1543             for (boolean isAsync : boolStates) {
1544                 tryGetInputImageInInvalidState("getInputImage succeeds in uninitialized state");
1545                 configureCodec(format, isAsync, false, true);
1546                 mCodec.start();
1547                 mCodec.stop();
1548                 tryGetInputImageInInvalidState("getInputImage succeeds in stopped state");
1549                 mCodec.reset();
1550             }
1551             mCodec.release();
1552         }
1553 
1554         @Test
testGetInputImageInInitState()1555         public void testGetInputImageInInitState() throws IOException {
1556             MediaFormat format = getSampleVideoFormat();
1557             String mime = format.getString(MediaFormat.KEY_MIME);
1558             mCodec = MediaCodec.createEncoderByType(mime);
1559             boolean[] boolStates = {true, false};
1560             for (boolean isAsync : boolStates) {
1561                 configureCodec(format, isAsync, false, true);
1562                 tryGetInputImageInInvalidState("getInputImage succeeds in initialized state");
1563                 mCodec.reset();
1564             }
1565             mCodec.release();
1566         }
1567 
1568         @Test
1569         @Ignore("TODO(b/151304147)")
testGetInputImageInRunningStateVideo()1570         public void testGetInputImageInRunningStateVideo()
1571                 throws IOException, InterruptedException {
1572             MediaFormat format = getSampleVideoFormat();
1573             String mime = format.getString(MediaFormat.KEY_MIME);
1574             mCodec = MediaCodec.createEncoderByType(mime);
1575             boolean[] boolStates = {true, false};
1576             for (boolean isAsync : boolStates) {
1577                 configureCodec(format, isAsync, false, true);
1578                 mCodec.start();
1579                 try {
1580                     Image img = mCodec.getInputImage(-1);
1581                     assertNull("getInputImage succeeds for bad buffer index " + -1, img);
1582                 } catch (Exception e) {
1583                     fail("getInputImage rec/exp :: " + e.toString() + " / null");
1584                 }
1585                 int bufferIndex = mIsCodecInAsyncMode ? mAsyncHandle.getInput().first :
1586                         mCodec.dequeueInputBuffer(-1);
1587                 Image img = mCodec.getInputImage(bufferIndex);
1588                 assertNotNull(img);
1589                 Image imgDup = mCodec.getInputImage(bufferIndex);
1590                 assertNotNull(imgDup);
1591                 enqueueEOS(bufferIndex);
1592                 waitForAllOutputs();
1593                 mCodec.stop();
1594                 mCodec.reset();
1595             }
1596             mCodec.release();
1597         }
1598 
1599         @Test
1600         @Ignore("TODO(b/151304147)")
testGetInputImageInRunningStateAudio()1601         public void testGetInputImageInRunningStateAudio()
1602                 throws IOException, InterruptedException {
1603             MediaFormat format = getSampleAudioFormat();
1604             String mime = format.getString(MediaFormat.KEY_MIME);
1605             mCodec = MediaCodec.createEncoderByType(mime);
1606             boolean[] boolStates = {true, false};
1607             for (boolean isAsync : boolStates) {
1608                 configureCodec(format, isAsync, false, true);
1609                 mCodec.start();
1610                 try {
1611                     Image img = mCodec.getInputImage(-1);
1612                     assertNull("getInputImage succeeds for bad buffer index " + -1, img);
1613                 } catch (Exception e) {
1614                     fail("getInputImage rec/exp :: " + e.toString() + " / null");
1615                 }
1616                 int bufferIndex = mIsCodecInAsyncMode ? mAsyncHandle.getInput().first :
1617                         mCodec.dequeueInputBuffer(-1);
1618                 Image img = mCodec.getInputImage(bufferIndex);
1619                 assertNull("getInputImage returns non null for buffers that do not hold raw img",
1620                         img);
1621                 enqueueEOS(bufferIndex);
1622                 waitForAllOutputs();
1623                 mCodec.stop();
1624                 mCodec.reset();
1625             }
1626             mCodec.release();
1627         }
1628 
1629         @Test
testGetInputImageInReleaseState()1630         public void testGetInputImageInReleaseState() throws IOException {
1631             MediaFormat format = getSampleVideoFormat();
1632             String mime = format.getString(MediaFormat.KEY_MIME);
1633             mCodec = MediaCodec.createEncoderByType(mime);
1634             mCodec.release();
1635             tryGetInputImageInInvalidState("getInputImage succeeds in release state");
1636         }
1637 
1638         @Test
testGetOutputImageInUnInitState()1639         public void testGetOutputImageInUnInitState() throws IOException {
1640             MediaFormat format = getSampleVideoFormat();
1641             String mime = format.getString(MediaFormat.KEY_MIME);
1642             mCodec = MediaCodec.createDecoderByType(mime);
1643             boolean[] boolStates = {true, false};
1644             for (boolean isAsync : boolStates) {
1645                 tryGetOutputImageInInvalidState("getOutputImage succeeds in uninitialized state");
1646                 configureCodec(format, isAsync, false, false);
1647                 mCodec.start();
1648                 mCodec.stop();
1649                 tryGetOutputImageInInvalidState("getOutputImage succeeds in stopped state");
1650                 mCodec.reset();
1651             }
1652             mCodec.release();
1653         }
1654 
1655         @Test
testGetOutputImageInInitState()1656         public void testGetOutputImageInInitState() throws IOException {
1657             MediaFormat format = getSampleVideoFormat();
1658             String mime = format.getString(MediaFormat.KEY_MIME);
1659             mCodec = MediaCodec.createDecoderByType(mime);
1660             boolean[] boolStates = {true, false};
1661             for (boolean isAsync : boolStates) {
1662                 configureCodec(format, isAsync, false, false);
1663                 tryGetOutputImageInInvalidState("getOutputImage succeeds in initialized state");
1664                 mCodec.reset();
1665             }
1666             mCodec.release();
1667         }
1668 
1669         @Test
1670         @Ignore("TODO(b/151304147)")
testGetOutputImageInRunningState()1671         public void testGetOutputImageInRunningState() throws IOException, InterruptedException {
1672             MediaFormat format = getSampleVideoFormat();
1673             MediaCodec.BufferInfo outInfo = new MediaCodec.BufferInfo();
1674             String mime = format.getString(MediaFormat.KEY_MIME);
1675             mCodec = MediaCodec.createDecoderByType(mime);
1676             boolean[] boolStates = {true, false};
1677             for (boolean isAsync : boolStates) {
1678                 configureCodec(format, isAsync, false, false);
1679                 mCodec.start();
1680                 try {
1681                     Image img = mCodec.getOutputImage(-1);
1682                     assertNull("getOutputImage succeeds for bad buffer index " + -1, img);
1683                 } catch (Exception e) {
1684                     fail("getOutputImage rec/exp :: " + e.toString() + " / null");
1685                 }
1686                 queueEOS();
1687                 int bufferIndex = 0;
1688                 while (!mSawOutputEOS) {
1689                     if (mIsCodecInAsyncMode) {
1690                         Pair<Integer, MediaCodec.BufferInfo> element = mAsyncHandle.getOutput();
1691                         bufferIndex = element.first;
1692                         dequeueOutput(element.first, element.second);
1693                     } else {
1694                         bufferIndex = mCodec.dequeueOutputBuffer(outInfo, Q_DEQ_TIMEOUT_US);
1695                         if (bufferIndex >= 0) {
1696                             dequeueOutput(bufferIndex, outInfo);
1697                         }
1698                     }
1699                 }
1700                 try {
1701                     Image img = mCodec.getOutputImage(bufferIndex);
1702                     assertNull("getOutputImage succeeds for buffer index not owned by client", img);
1703                 } catch (Exception e) {
1704                     fail("getOutputBuffer rec/exp :: " + e.toString() + " / null");
1705                 }
1706                 mCodec.stop();
1707                 mCodec.reset();
1708             }
1709             mCodec.release();
1710         }
1711 
1712         @Test
testGetOutputImageInReleaseState()1713         public void testGetOutputImageInReleaseState() throws IOException {
1714             MediaFormat format = getSampleVideoFormat();
1715             String mime = format.getString(MediaFormat.KEY_MIME);
1716             mCodec = MediaCodec.createDecoderByType(mime);
1717             mCodec.release();
1718             tryGetOutputImageInInvalidState("getOutputImage succeeds in release state");
1719         }
1720 
1721         @Test
testQueueInputBufferInUnInitState()1722         public void testQueueInputBufferInUnInitState() throws IOException {
1723             MediaFormat format = getSampleAudioFormat();
1724             String mime = format.getString(MediaFormat.KEY_MIME);
1725             mCodec = MediaCodec.createEncoderByType(mime);
1726             boolean[] boolStates = {true, false};
1727             for (boolean isAsync : boolStates) {
1728                 tryQueueInputBufferInInvalidState(
1729                         "queueInputBuffer succeeds in uninitialized state");
1730                 configureCodec(format, isAsync, false, true);
1731                 mCodec.start();
1732                 mCodec.stop();
1733                 tryQueueInputBufferInInvalidState("queueInputBuffer succeeds in stopped state");
1734                 mCodec.reset();
1735             }
1736             mCodec.release();
1737         }
1738 
1739         @Test
testQueueInputBufferInInitState()1740         public void testQueueInputBufferInInitState() throws IOException {
1741             MediaFormat format = getSampleAudioFormat();
1742             String mime = format.getString(MediaFormat.KEY_MIME);
1743             mCodec = MediaCodec.createEncoderByType(mime);
1744             boolean[] boolStates = {true, false};
1745             for (boolean isAsync : boolStates) {
1746                 configureCodec(format, isAsync, false, true);
1747                 tryQueueInputBufferInInvalidState("queueInputBuffer succeeds in initialized state");
1748                 mCodec.start();
1749                 mCodec.stop();
1750                 mCodec.reset();
1751             }
1752             mCodec.release();
1753         }
1754 
1755         @Test
testQueueInputBufferWithBadIndex()1756         public void testQueueInputBufferWithBadIndex() throws IOException {
1757             MediaFormat format = getSampleAudioFormat();
1758             String mime = format.getString(MediaFormat.KEY_MIME);
1759             mCodec = MediaCodec.createEncoderByType(mime);
1760             boolean[] boolStates = {true, false};
1761             for (boolean isAsync : boolStates) {
1762                 configureCodec(format, isAsync, false, true);
1763                 mCodec.start();
1764                 try {
1765                     mCodec.queueInputBuffer(-1, 0, 0, 0, MediaCodec.BUFFER_FLAG_END_OF_STREAM);
1766                     fail("queueInputBuffer succeeds with bad buffer index :: " + -1);
1767                 } catch (Exception e) {
1768                     // expected
1769                 }
1770                 mCodec.stop();
1771                 mCodec.reset();
1772             }
1773             mCodec.release();
1774         }
1775 
1776         @Test
testQueueInputBufferWithBadSize()1777         public void testQueueInputBufferWithBadSize() throws IOException, InterruptedException {
1778             MediaFormat format = getSampleAudioFormat();
1779             String mime = format.getString(MediaFormat.KEY_MIME);
1780             mCodec = MediaCodec.createEncoderByType(mime);
1781             boolean[] boolStates = {true, false};
1782             for (boolean isAsync : boolStates) {
1783                 configureCodec(format, isAsync, false, true);
1784                 mCodec.start();
1785                 int bufferIndex = mIsCodecInAsyncMode ? mAsyncHandle.getInput().first :
1786                         mCodec.dequeueInputBuffer(-1);
1787                 ByteBuffer buffer = mCodec.getInputBuffer(bufferIndex);
1788                 assertNotNull(buffer);
1789                 try {
1790                     mCodec.queueInputBuffer(bufferIndex, 0, buffer.capacity() + 100, 0,
1791                             MediaCodec.BUFFER_FLAG_END_OF_STREAM);
1792                     fail("queueInputBuffer succeeds with bad size param :: " + buffer.capacity() +
1793                             100);
1794                 } catch (Exception e) {
1795                     // expected
1796                 }
1797                 mCodec.stop();
1798                 mCodec.reset();
1799             }
1800             mCodec.release();
1801         }
1802 
1803         @Test
testQueueInputBufferWithBadBuffInfo()1804         public void testQueueInputBufferWithBadBuffInfo() throws IOException, InterruptedException {
1805             MediaFormat format = getSampleAudioFormat();
1806             String mime = format.getString(MediaFormat.KEY_MIME);
1807             mCodec = MediaCodec.createEncoderByType(mime);
1808             boolean[] boolStates = {true, false};
1809             for (boolean isAsync : boolStates) {
1810                 configureCodec(format, isAsync, false, true);
1811                 mCodec.start();
1812                 int bufferIndex = mIsCodecInAsyncMode ? mAsyncHandle.getInput().first :
1813                         mCodec.dequeueInputBuffer(-1);
1814                 ByteBuffer buffer = mCodec.getInputBuffer(bufferIndex);
1815                 assertNotNull(buffer);
1816                 try {
1817                     mCodec.queueInputBuffer(bufferIndex, 16, buffer.capacity(), 0,
1818                             MediaCodec.BUFFER_FLAG_END_OF_STREAM);
1819                     fail("queueInputBuffer succeeds with bad offset and size param");
1820                 } catch (Exception e) {
1821                     // expected
1822                 }
1823                 mCodec.stop();
1824                 mCodec.reset();
1825             }
1826             mCodec.release();
1827         }
1828 
1829         @Test
1830         @Ignore("TODO(b/151305059)")
testQueueInputBufferWithBadOffset()1831         public void testQueueInputBufferWithBadOffset() throws IOException, InterruptedException {
1832             MediaFormat format = getSampleAudioFormat();
1833             String mime = format.getString(MediaFormat.KEY_MIME);
1834             mCodec = MediaCodec.createEncoderByType(mime);
1835             boolean[] boolStates = {true, false};
1836             for (boolean isAsync : boolStates) {
1837                 configureCodec(format, isAsync, false, true);
1838                 mCodec.start();
1839                 int bufferIndex = mIsCodecInAsyncMode ? mAsyncHandle.getInput().first :
1840                         mCodec.dequeueInputBuffer(-1);
1841                 ByteBuffer buffer = mCodec.getInputBuffer(bufferIndex);
1842                 assertNotNull(buffer);
1843                 try {
1844                     mCodec.queueInputBuffer(bufferIndex, -1, buffer.capacity(), 0,
1845                             MediaCodec.BUFFER_FLAG_END_OF_STREAM);
1846                     fail("queueInputBuffer succeeds with bad offset param :: " + -1);
1847                 } catch (Exception e) {
1848                     // expected
1849                 }
1850                 mCodec.stop();
1851                 mCodec.reset();
1852             }
1853             mCodec.release();
1854         }
1855 
1856         @Test
testQueueInputBufferInReleaseState()1857         public void testQueueInputBufferInReleaseState() throws IOException {
1858             MediaFormat format = getSampleAudioFormat();
1859             String mime = format.getString(MediaFormat.KEY_MIME);
1860             mCodec = MediaCodec.createEncoderByType(mime);
1861             mCodec.release();
1862             tryQueueInputBufferInInvalidState("queueInputBuffer succeeds in release state");
1863         }
1864 
1865         @Test
testReleaseOutputBufferInUnInitState()1866         public void testReleaseOutputBufferInUnInitState() throws IOException {
1867             MediaFormat format = getSampleAudioFormat();
1868             String mime = format.getString(MediaFormat.KEY_MIME);
1869             mCodec = MediaCodec.createEncoderByType(mime);
1870             boolean[] boolStates = {true, false};
1871             for (boolean isAsync : boolStates) {
1872                 tryReleaseOutputBufferInInvalidState(
1873                         "releaseOutputBuffer succeeds in uninitialized state");
1874                 configureCodec(format, isAsync, false, true);
1875                 mCodec.start();
1876                 mCodec.stop();
1877                 tryReleaseOutputBufferInInvalidState(
1878                         "releaseOutputBuffer succeeds in stopped state");
1879                 mCodec.reset();
1880             }
1881             mCodec.release();
1882         }
1883 
1884         @Test
testReleaseOutputBufferInInitState()1885         public void testReleaseOutputBufferInInitState() throws IOException {
1886             MediaFormat format = getSampleAudioFormat();
1887             String mime = format.getString(MediaFormat.KEY_MIME);
1888             mCodec = MediaCodec.createEncoderByType(mime);
1889             boolean[] boolStates = {true, false};
1890             for (boolean isAsync : boolStates) {
1891                 configureCodec(format, isAsync, false, true);
1892                 tryReleaseOutputBufferInInvalidState(
1893                         "releaseOutputBuffer succeeds in initialized state");
1894                 mCodec.reset();
1895             }
1896             mCodec.release();
1897         }
1898 
1899         @Test
testReleaseOutputBufferInRunningState()1900         public void testReleaseOutputBufferInRunningState()
1901                 throws IOException, InterruptedException {
1902             MediaFormat format = getSampleAudioFormat();
1903             MediaCodec.BufferInfo outInfo = new MediaCodec.BufferInfo();
1904             String mime = format.getString(MediaFormat.KEY_MIME);
1905             mCodec = MediaCodec.createEncoderByType(mime);
1906             boolean[] boolStates = {true, false};
1907             for (boolean isAsync : boolStates) {
1908                 configureCodec(format, isAsync, false, true);
1909                 mCodec.start();
1910                 try {
1911                     mCodec.releaseOutputBuffer(-1, false);
1912                     fail("releaseOutputBuffer succeeds for bad buffer index " + -1);
1913                 } catch (MediaCodec.CodecException e) {
1914                     // expected
1915                 }
1916                 queueEOS();
1917                 int bufferIndex = 0;
1918                 while (!mSawOutputEOS) {
1919                     if (mIsCodecInAsyncMode) {
1920                         Pair<Integer, MediaCodec.BufferInfo> element = mAsyncHandle.getOutput();
1921                         bufferIndex = element.first;
1922                         ByteBuffer buffer = mCodec.getOutputBuffer(bufferIndex);
1923                         assertNotNull(buffer);
1924                         dequeueOutput(element.first, element.second);
1925                     } else {
1926                         bufferIndex = mCodec.dequeueOutputBuffer(outInfo, Q_DEQ_TIMEOUT_US);
1927                         if (bufferIndex >= 0) {
1928                             ByteBuffer buffer = mCodec.getOutputBuffer(bufferIndex);
1929                             assertNotNull(buffer);
1930                             dequeueOutput(bufferIndex, outInfo);
1931                         }
1932                     }
1933                 }
1934                 try {
1935                     mCodec.releaseOutputBuffer(bufferIndex, false);
1936                     fail("releaseOutputBuffer succeeds for buffer index not owned by client");
1937                 } catch (MediaCodec.CodecException e) {
1938                     // expected
1939                 }
1940                 mCodec.stop();
1941                 mCodec.reset();
1942             }
1943             mCodec.release();
1944         }
1945 
1946         @Test
testReleaseOutputBufferInReleaseState()1947         public void testReleaseOutputBufferInReleaseState() throws IOException {
1948             MediaFormat format = getSampleAudioFormat();
1949             String mime = format.getString(MediaFormat.KEY_MIME);
1950             mCodec = MediaCodec.createEncoderByType(mime);
1951             mCodec.release();
1952             tryReleaseOutputBufferInInvalidState(
1953                     "releaseOutputBuffer succeeds in release state");
1954         }
1955 
1956         @Test
testReleaseIdempotent()1957         public void testReleaseIdempotent() throws IOException {
1958             MediaFormat format = getSampleAudioFormat();
1959             String mime = format.getString(MediaFormat.KEY_MIME);
1960             mCodec = MediaCodec.createEncoderByType(mime);
1961             mCodec.release();
1962             mCodec.release();
1963         }
1964     }
1965 
1966     @SmallTest
1967     public static class TestApiNative {
1968         @Rule
1969         public Timeout timeout = new Timeout(PER_TEST_TIMEOUT_MS, TimeUnit.MILLISECONDS);
1970 
1971         static {
1972             System.loadLibrary("ctsmediav2codec_jni");
1973         }
1974 
1975         @Test
testCreateByCodecNameForNull()1976         public void testCreateByCodecNameForNull() {
1977             assertTrue(nativeTestCreateByCodecNameForNull());
1978         }
1979 
nativeTestCreateByCodecNameForNull()1980         private native boolean nativeTestCreateByCodecNameForNull();
1981 
1982         @Test
testCreateByCodecNameForInvalidName()1983         public void testCreateByCodecNameForInvalidName() {
1984             assertTrue(nativeTestCreateByCodecNameForInvalidName());
1985         }
1986 
nativeTestCreateByCodecNameForInvalidName()1987         private native boolean nativeTestCreateByCodecNameForInvalidName();
1988 
1989         @Test
testCreateDecoderByTypeForNull()1990         public void testCreateDecoderByTypeForNull() {
1991             assertTrue(nativeTestCreateDecoderByTypeForNull());
1992         }
1993 
nativeTestCreateDecoderByTypeForNull()1994         private native boolean nativeTestCreateDecoderByTypeForNull();
1995 
1996         @Test
testCreateDecoderByTypeForInvalidMime()1997         public void testCreateDecoderByTypeForInvalidMime() {
1998             assertTrue(nativeTestCreateDecoderByTypeForInvalidMime());
1999         }
2000 
nativeTestCreateDecoderByTypeForInvalidMime()2001         private native boolean nativeTestCreateDecoderByTypeForInvalidMime();
2002 
2003         @Test
testCreateEncoderByTypeForNull()2004         public void testCreateEncoderByTypeForNull() {
2005             assertTrue(nativeTestCreateEncoderByTypeForNull());
2006         }
2007 
nativeTestCreateEncoderByTypeForNull()2008         private native boolean nativeTestCreateEncoderByTypeForNull();
2009 
2010         @Test
testCreateEncoderByTypeForInvalidMime()2011         public void testCreateEncoderByTypeForInvalidMime() {
2012             assertTrue(nativeTestCreateEncoderByTypeForInvalidMime());
2013         }
2014 
nativeTestCreateEncoderByTypeForInvalidMime()2015         private native boolean nativeTestCreateEncoderByTypeForInvalidMime();
2016 
2017         @Test
2018         @Ignore("TODO(b/151302868)")
testConfigureForNullFormat()2019         public void testConfigureForNullFormat() {
2020             assertTrue(nativeTestConfigureForNullFormat());
2021         }
2022 
nativeTestConfigureForNullFormat()2023         private native boolean nativeTestConfigureForNullFormat();
2024 
2025         @Test
testConfigureForEmptyFormat()2026         public void testConfigureForEmptyFormat() {
2027             assertTrue(nativeTestConfigureForEmptyFormat());
2028         }
2029 
nativeTestConfigureForEmptyFormat()2030         private native boolean nativeTestConfigureForEmptyFormat();
2031 
2032         @Test
2033         @Ignore("TODO(b/151303041)")
testConfigureCodecForIncompleteFormat()2034         public void testConfigureCodecForIncompleteFormat() {
2035             boolean[] boolStates = {false, true};
2036             for (boolean isEncoder : boolStates) {
2037                 for (boolean isAudio : boolStates) {
2038                     assertTrue(
2039                             "testConfigureCodecForIncompleteFormat failed for isAudio " + isAudio +
2040                                     ", isEncoder " + isEncoder,
2041                             nativeTestConfigureCodecForIncompleteFormat(isAudio, isEncoder));
2042                 }
2043             }
2044         }
2045 
nativeTestConfigureCodecForIncompleteFormat(boolean isAudio, boolean isEncoder)2046         private native boolean nativeTestConfigureCodecForIncompleteFormat(boolean isAudio,
2047                 boolean isEncoder);
2048 
2049         @Test
testConfigureEncoderForBadFlags()2050         public void testConfigureEncoderForBadFlags() {
2051             assertTrue(nativeTestConfigureEncoderForBadFlags());
2052         }
2053 
nativeTestConfigureEncoderForBadFlags()2054         private native boolean nativeTestConfigureEncoderForBadFlags();
2055 
2056         @Test
testConfigureDecoderForBadFlags()2057         public void testConfigureDecoderForBadFlags() {
2058             assertTrue(nativeTestConfigureDecoderForBadFlags());
2059         }
2060 
nativeTestConfigureDecoderForBadFlags()2061         private native boolean nativeTestConfigureDecoderForBadFlags();
2062 
2063         @Test
testConfigureInInitState()2064         public void testConfigureInInitState() {
2065             assertTrue(nativeTestConfigureInInitState());
2066         }
2067 
nativeTestConfigureInInitState()2068         private native boolean nativeTestConfigureInInitState();
2069 
2070         @Test
testConfigureInRunningState()2071         public void testConfigureInRunningState() {
2072             assertTrue(nativeTestConfigureInRunningState());
2073         }
2074 
nativeTestConfigureInRunningState()2075         private native boolean nativeTestConfigureInRunningState();
2076 
2077         @Test
testConfigureInUnInitState()2078         public void testConfigureInUnInitState() {
2079             assertTrue(nativeTestConfigureInUnInitState());
2080         }
2081 
nativeTestConfigureInUnInitState()2082         private native boolean nativeTestConfigureInUnInitState();
2083 
2084         @Test
testDequeueInputBufferInInitState()2085         public void testDequeueInputBufferInInitState() {
2086             assertTrue(nativeTestDequeueInputBufferInInitState());
2087         }
2088 
nativeTestDequeueInputBufferInInitState()2089         private native boolean nativeTestDequeueInputBufferInInitState();
2090 
2091         @Test
testDequeueInputBufferInRunningState()2092         public void testDequeueInputBufferInRunningState() {
2093             assertTrue(nativeTestDequeueInputBufferInRunningState());
2094         }
2095 
nativeTestDequeueInputBufferInRunningState()2096         private native boolean nativeTestDequeueInputBufferInRunningState();
2097 
2098         @Test
testDequeueInputBufferInUnInitState()2099         public void testDequeueInputBufferInUnInitState() {
2100             assertTrue(nativeTestDequeueInputBufferInUnInitState());
2101         }
2102 
nativeTestDequeueInputBufferInUnInitState()2103         private native boolean nativeTestDequeueInputBufferInUnInitState();
2104 
2105         @Test
testDequeueOutputBufferInInitState()2106         public void testDequeueOutputBufferInInitState() {
2107             assertTrue(nativeTestDequeueOutputBufferInInitState());
2108         }
2109 
nativeTestDequeueOutputBufferInInitState()2110         private native boolean nativeTestDequeueOutputBufferInInitState();
2111 
2112         @Test
testDequeueOutputBufferInRunningState()2113         public void testDequeueOutputBufferInRunningState() {
2114             assertTrue(nativeTestDequeueOutputBufferInRunningState());
2115         }
2116 
nativeTestDequeueOutputBufferInRunningState()2117         private native boolean nativeTestDequeueOutputBufferInRunningState();
2118 
2119         @Test
testDequeueOutputBufferInUnInitState()2120         public void testDequeueOutputBufferInUnInitState() {
2121             assertTrue(nativeTestDequeueOutputBufferInUnInitState());
2122         }
2123 
nativeTestDequeueOutputBufferInUnInitState()2124         private native boolean nativeTestDequeueOutputBufferInUnInitState();
2125 
2126         @Test
testFlushInInitState()2127         public void testFlushInInitState() {
2128             assertTrue(nativeTestFlushInInitState());
2129         }
2130 
nativeTestFlushInInitState()2131         private native boolean nativeTestFlushInInitState();
2132 
2133         @Test
testFlushInRunningState()2134         public void testFlushInRunningState() {
2135             assertTrue(nativeTestFlushInRunningState());
2136         }
2137 
nativeTestFlushInRunningState()2138         private native boolean nativeTestFlushInRunningState();
2139 
2140         @Test
testFlushInUnInitState()2141         public void testFlushInUnInitState() {
2142             assertTrue(nativeTestFlushInUnInitState());
2143         }
2144 
nativeTestFlushInUnInitState()2145         private native boolean nativeTestFlushInUnInitState();
2146 
2147         @Test
testGetNameInInitState()2148         public void testGetNameInInitState() {
2149             assertTrue(nativeTestGetNameInInitState());
2150         }
2151 
nativeTestGetNameInInitState()2152         private native boolean nativeTestGetNameInInitState();
2153 
2154         @Test
testGetNameInRunningState()2155         public void testGetNameInRunningState() {
2156             assertTrue(nativeTestGetNameInRunningState());
2157         }
2158 
nativeTestGetNameInRunningState()2159         private native boolean nativeTestGetNameInRunningState();
2160 
2161         @Test
testGetNameInUnInitState()2162         public void testGetNameInUnInitState() {
2163             assertTrue(nativeTestGetNameInUnInitState());
2164         }
2165 
nativeTestGetNameInUnInitState()2166         private native boolean nativeTestGetNameInUnInitState();
2167 
2168         @Test
2169         @Ignore("TODO(b/148523403)")
testSetAsyncNotifyCallbackInInitState()2170         public void testSetAsyncNotifyCallbackInInitState() {
2171             assertTrue(nativeTestSetAsyncNotifyCallbackInInitState());
2172         }
2173 
nativeTestSetAsyncNotifyCallbackInInitState()2174         private native boolean nativeTestSetAsyncNotifyCallbackInInitState();
2175 
2176         @Test
2177         @Ignore("TODO(b/152553625)")
testSetAsyncNotifyCallbackInRunningState()2178         public void testSetAsyncNotifyCallbackInRunningState() {
2179             assertTrue(nativeTestSetAsyncNotifyCallbackInRunningState());
2180         }
2181 
nativeTestSetAsyncNotifyCallbackInRunningState()2182         private native boolean nativeTestSetAsyncNotifyCallbackInRunningState();
2183 
2184         @Test
testSetAsyncNotifyCallbackInUnInitState()2185         public void testSetAsyncNotifyCallbackInUnInitState() {
2186             assertTrue(nativeTestSetAsyncNotifyCallbackInUnInitState());
2187         }
2188 
nativeTestSetAsyncNotifyCallbackInUnInitState()2189         private native boolean nativeTestSetAsyncNotifyCallbackInUnInitState();
2190 
2191         @Test
tesGetInputBufferInInitState()2192         public void tesGetInputBufferInInitState() {
2193             assertTrue(nativeTestGetInputBufferInInitState());
2194         }
2195 
nativeTestGetInputBufferInInitState()2196         private native boolean nativeTestGetInputBufferInInitState();
2197 
2198         @Test
testGetInputBufferInRunningState()2199         public void testGetInputBufferInRunningState() {
2200             assertTrue(nativeTestGetInputBufferInRunningState());
2201         }
2202 
nativeTestGetInputBufferInRunningState()2203         private native boolean nativeTestGetInputBufferInRunningState();
2204 
2205         @Test
testGetInputBufferInUnInitState()2206         public void testGetInputBufferInUnInitState() {
2207             assertTrue(nativeTestGetInputBufferInUnInitState());
2208         }
2209 
nativeTestGetInputBufferInUnInitState()2210         private native boolean nativeTestGetInputBufferInUnInitState();
2211 
2212         @Test
testGetInputFormatInInitState()2213         public void testGetInputFormatInInitState() {
2214             assertTrue(nativeTestGetInputFormatInInitState());
2215         }
2216 
nativeTestGetInputFormatInInitState()2217         private native boolean nativeTestGetInputFormatInInitState();
2218 
2219         @Test
testGetInputFormatInRunningState()2220         public void testGetInputFormatInRunningState() {
2221             assertTrue(nativeTestGetInputFormatInRunningState());
2222         }
2223 
nativeTestGetInputFormatInRunningState()2224         private native boolean nativeTestGetInputFormatInRunningState();
2225 
2226         @Test
testGetInputFormatInUnInitState()2227         public void testGetInputFormatInUnInitState() {
2228             assertTrue(nativeTestGetInputFormatInUnInitState());
2229         }
2230 
nativeTestGetInputFormatInUnInitState()2231         private native boolean nativeTestGetInputFormatInUnInitState();
2232 
2233         @Test
testGetOutputBufferInInitState()2234         public void testGetOutputBufferInInitState() {
2235             assertTrue(nativeTestGetOutputBufferInInitState());
2236         }
2237 
nativeTestGetOutputBufferInInitState()2238         private native boolean nativeTestGetOutputBufferInInitState();
2239 
2240         @Test
testGetOutputBufferInRunningState()2241         public void testGetOutputBufferInRunningState() {
2242             assertTrue(nativeTestGetOutputBufferInRunningState());
2243         }
2244 
nativeTestGetOutputBufferInRunningState()2245         private native boolean nativeTestGetOutputBufferInRunningState();
2246 
2247         @Test
testGetOutputBufferInUnInitState()2248         public void testGetOutputBufferInUnInitState() {
2249             assertTrue(nativeTestGetOutputBufferInUnInitState());
2250         }
2251 
nativeTestGetOutputBufferInUnInitState()2252         private native boolean nativeTestGetOutputBufferInUnInitState();
2253 
2254         @Test
testGetOutputFormatInInitState()2255         public void testGetOutputFormatInInitState() {
2256             assertTrue(nativeTestGetOutputFormatInInitState());
2257         }
2258 
nativeTestGetOutputFormatInInitState()2259         private native boolean nativeTestGetOutputFormatInInitState();
2260 
2261         @Test
testGetOutputFormatInRunningState()2262         public void testGetOutputFormatInRunningState() {
2263             assertTrue(nativeTestGetOutputFormatInRunningState());
2264         }
2265 
nativeTestGetOutputFormatInRunningState()2266         private native boolean nativeTestGetOutputFormatInRunningState();
2267 
2268         @Test
testGetOutputFormatInUnInitState()2269         public void testGetOutputFormatInUnInitState() {
2270             assertTrue(nativeTestGetOutputFormatInUnInitState());
2271         }
2272 
nativeTestGetOutputFormatInUnInitState()2273         private native boolean nativeTestGetOutputFormatInUnInitState();
2274 
2275         @Test
2276         @Ignore("TODO(b/)")
testSetParametersInInitState()2277         public void testSetParametersInInitState() {
2278             assertTrue(nativeTestSetParametersInInitState());
2279         }
2280 
nativeTestSetParametersInInitState()2281         private native boolean nativeTestSetParametersInInitState();
2282 
2283         @Test
testSetParametersInRunningState()2284         public void testSetParametersInRunningState() {
2285             assertTrue(nativeTestSetParametersInRunningState());
2286         }
2287 
nativeTestSetParametersInRunningState()2288         private native boolean nativeTestSetParametersInRunningState();
2289 
2290         @Test
2291         @Ignore("TODO(b/)")
testSetParametersInUnInitState()2292         public void testSetParametersInUnInitState() {
2293             assertTrue(nativeTestSetParametersInUnInitState());
2294         }
2295 
nativeTestSetParametersInUnInitState()2296         private native boolean nativeTestSetParametersInUnInitState();
2297 
2298         @Test
testStartInRunningState()2299         public void testStartInRunningState() {
2300             assertTrue(nativeTestStartInRunningState());
2301         }
2302 
nativeTestStartInRunningState()2303         private native boolean nativeTestStartInRunningState();
2304 
2305         @Test
testStartInUnInitState()2306         public void testStartInUnInitState() {
2307             assertTrue(nativeTestStartInUnInitState());
2308         }
2309 
nativeTestStartInUnInitState()2310         private native boolean nativeTestStartInUnInitState();
2311 
2312         @Test
testStopInInitState()2313         public void testStopInInitState() {
2314             assertTrue(nativeTestStopInInitState());
2315         }
2316 
nativeTestStopInInitState()2317         private native boolean nativeTestStopInInitState();
2318 
2319         @Test
testStopInRunningState()2320         public void testStopInRunningState() {
2321             assertTrue(nativeTestStopInRunningState());
2322         }
2323 
nativeTestStopInRunningState()2324         private native boolean nativeTestStopInRunningState();
2325 
2326         @Test
testStopInUnInitState()2327         public void testStopInUnInitState() {
2328             assertTrue(nativeTestStopInUnInitState());
2329         }
2330 
nativeTestStopInUnInitState()2331         private native boolean nativeTestStopInUnInitState();
2332 
2333         @Test
testQueueInputBufferInInitState()2334         public void testQueueInputBufferInInitState() {
2335             assertTrue(nativeTestQueueInputBufferInInitState());
2336         }
2337 
nativeTestQueueInputBufferInInitState()2338         private native boolean nativeTestQueueInputBufferInInitState();
2339 
2340         @Test
testQueueInputBufferWithBadIndex()2341         public void testQueueInputBufferWithBadIndex() {
2342             assertTrue(nativeTestQueueInputBufferWithBadIndex());
2343         }
2344 
nativeTestQueueInputBufferWithBadIndex()2345         private native boolean nativeTestQueueInputBufferWithBadIndex();
2346 
2347         @Test
testQueueInputBufferWithBadSize()2348         public void testQueueInputBufferWithBadSize() {
2349             assertTrue(nativeTestQueueInputBufferWithBadSize());
2350         }
2351 
nativeTestQueueInputBufferWithBadSize()2352         private native boolean nativeTestQueueInputBufferWithBadSize();
2353 
2354         @Test
testQueueInputBufferWithBadBuffInfo()2355         public void testQueueInputBufferWithBadBuffInfo() {
2356             assertTrue(nativeTestQueueInputBufferWithBadBuffInfo());
2357         }
2358 
nativeTestQueueInputBufferWithBadBuffInfo()2359         private native boolean nativeTestQueueInputBufferWithBadBuffInfo();
2360 
2361         @Test
testQueueInputBufferWithBadOffset()2362         public void testQueueInputBufferWithBadOffset() {
2363             assertTrue(nativeTestQueueInputBufferWithBadOffset());
2364         }
2365 
nativeTestQueueInputBufferWithBadOffset()2366         private native boolean nativeTestQueueInputBufferWithBadOffset();
2367 
2368         @Test
testQueueInputBufferInUnInitState()2369         public void testQueueInputBufferInUnInitState() {
2370             assertTrue(nativeTestQueueInputBufferInUnInitState());
2371         }
2372 
nativeTestQueueInputBufferInUnInitState()2373         private native boolean nativeTestQueueInputBufferInUnInitState();
2374 
2375         @Test
testReleaseOutputBufferInInitState()2376         public void testReleaseOutputBufferInInitState() {
2377             assertTrue(nativeTestReleaseOutputBufferInInitState());
2378         }
2379 
nativeTestReleaseOutputBufferInInitState()2380         private native boolean nativeTestReleaseOutputBufferInInitState();
2381 
2382         @Test
testReleaseOutputBufferInRunningState()2383         public void testReleaseOutputBufferInRunningState() {
2384             assertTrue(nativeTestReleaseOutputBufferInRunningState());
2385         }
2386 
nativeTestReleaseOutputBufferInRunningState()2387         private native boolean nativeTestReleaseOutputBufferInRunningState();
2388 
2389         @Test
testReleaseOutputBufferInUnInitState()2390         public void testReleaseOutputBufferInUnInitState() {
2391             assertTrue(nativeTestReleaseOutputBufferInUnInitState());
2392         }
2393 
nativeTestReleaseOutputBufferInUnInitState()2394         private native boolean nativeTestReleaseOutputBufferInUnInitState();
2395 
2396         @Test
testGetBufferFormatInInitState()2397         public void testGetBufferFormatInInitState() {
2398             assertTrue(nativeTestGetBufferFormatInInitState());
2399         }
2400 
nativeTestGetBufferFormatInInitState()2401         private native boolean nativeTestGetBufferFormatInInitState();
2402 
2403         @Test
testGetBufferFormatInRunningState()2404         public void testGetBufferFormatInRunningState() {
2405             assertTrue(nativeTestGetBufferFormatInRunningState());
2406         }
2407 
nativeTestGetBufferFormatInRunningState()2408         private native boolean nativeTestGetBufferFormatInRunningState();
2409 
2410         @Test
testGetBufferFormatInUnInitState()2411         public void testGetBufferFormatInUnInitState() {
2412             assertTrue(nativeTestGetBufferFormatInUnInitState());
2413         }
2414 
nativeTestGetBufferFormatInUnInitState()2415         private native boolean nativeTestGetBufferFormatInUnInitState();
2416     }
2417 }
2418