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 //#define LOG_NDEBUG 0
18 #define LOG_TAG "NativeCodecUnitTest"
19 #include <NdkMediaExtractor.h>
20 #include <jni.h>
21 #include <log/log.h>
22 #include <sys/stat.h>
23 
24 #include <thread>
25 
26 #include "NativeCodecTestBase.h"
27 #include "NativeMediaCommon.h"
28 
29 class NativeCodecUnitTest final : CodecTestBase {
30   private:
31     AMediaFormat* mFormat;
32     bool enqueueInput(size_t bufferIndex) override;
33     bool dequeueOutput(size_t bufferIndex, AMediaCodecBufferInfo* bufferInfo) override;
34 
35     const long kStallTimeMs = 1000;
36 
37   public:
38     NativeCodecUnitTest(const char* mime);
39     ~NativeCodecUnitTest();
40 
41     bool setupCodec(bool isAudio, bool isEncoder);
42 
43     bool testConfigureCodecForIncompleteFormat(bool isAudio, bool isEncoder);
44     bool testConfigureCodecForBadFlags(bool isEncoder);
45     bool testConfigureInInitState();
46     bool testConfigureInRunningState();
47     bool testConfigureInUnInitState();
48     bool testDequeueInputBufferInInitState();
49     bool testDequeueInputBufferInRunningState();
50     bool testDequeueInputBufferInUnInitState();
51     bool testDequeueOutputBufferInInitState();
52     bool testDequeueOutputBufferInRunningState();
53     bool testDequeueOutputBufferInUnInitState();
54     bool testFlushInInitState();
55     bool testFlushInRunningState();
56     bool testFlushInUnInitState();
57     bool testGetNameInInitState();
58     bool testGetNameInRunningState();
59     bool testGetNameInUnInitState();
60     bool testSetAsyncNotifyCallbackInInitState();
61     bool testSetAsyncNotifyCallbackInRunningState();
62     bool testSetAsyncNotifyCallbackInUnInitState();
63     bool testGetInputBufferInInitState();
64     bool testGetInputBufferInRunningState();
65     bool testGetInputBufferInUnInitState();
66     bool testGetInputFormatInInitState();
67     bool testGetInputFormatInRunningState();
68     bool testGetInputFormatInUnInitState();
69     bool testGetOutputBufferInInitState();
70     bool testGetOutputBufferInRunningState();
71     bool testGetOutputBufferInUnInitState();
72     bool testGetOutputFormatInInitState();
73     bool testGetOutputFormatInRunningState();
74     bool testGetOutputFormatInUnInitState();
75     bool testSetParametersInInitState();
76     bool testSetParametersInRunningState();
77     bool testSetParametersInUnInitState();
78     bool testStartInRunningState();
79     bool testStartInUnInitState();
80     bool testStopInInitState();
81     bool testStopInRunningState();
82     bool testStopInUnInitState();
83     bool testQueueInputBufferInInitState();
84     bool testQueueInputBufferWithBadIndex();
85     bool testQueueInputBufferWithBadSize();
86     bool testQueueInputBufferWithBadBuffInfo();
87     bool testQueueInputBufferWithBadOffset();
88     bool testQueueInputBufferInUnInitState();
89     bool testReleaseOutputBufferInInitState();
90     bool testReleaseOutputBufferInRunningState();
91     bool testReleaseOutputBufferInUnInitState();
92     bool testGetBufferFormatInInitState();
93     bool testGetBufferFormatInRunningState();
94     bool testGetBufferFormatInUnInitState();
95 };
96 
NativeCodecUnitTest(const char * mime)97 NativeCodecUnitTest::NativeCodecUnitTest(const char* mime) : CodecTestBase(mime) {
98     mFormat = nullptr;
99 }
100 
~NativeCodecUnitTest()101 NativeCodecUnitTest::~NativeCodecUnitTest() {
102     if (mFormat) AMediaFormat_delete(mFormat);
103     mFormat = nullptr;
104 }
105 
enqueueInput(size_t bufferIndex)106 bool NativeCodecUnitTest::enqueueInput(size_t bufferIndex) {
107     (void)bufferIndex;
108     return false;
109 }
110 
dequeueOutput(size_t bufferIndex,AMediaCodecBufferInfo * info)111 bool NativeCodecUnitTest::dequeueOutput(size_t bufferIndex, AMediaCodecBufferInfo* info) {
112     if ((info->flags & AMEDIACODEC_BUFFER_FLAG_END_OF_STREAM) != 0) {
113         mSawOutputEOS = true;
114     }
115     CHECK_STATUS(AMediaCodec_releaseOutputBuffer(mCodec, bufferIndex, false),
116                  "AMediaCodec_releaseOutputBuffer failed");
117     return !hasSeenError();
118 }
119 
getSampleAudioFormat()120 AMediaFormat* getSampleAudioFormat() {
121     AMediaFormat* format = AMediaFormat_new();
122     AMediaFormat_setString(format, AMEDIAFORMAT_KEY_MIME, AMEDIA_MIMETYPE_AUDIO_AAC);
123     AMediaFormat_setInt32(format, AMEDIAFORMAT_KEY_BIT_RATE, 64000);
124     AMediaFormat_setInt32(format, AMEDIAFORMAT_KEY_SAMPLE_RATE, 16000);
125     AMediaFormat_setInt32(format, AMEDIAFORMAT_KEY_CHANNEL_COUNT, 1);
126     return format;
127 }
128 
getSampleVideoFormat()129 AMediaFormat* getSampleVideoFormat() {
130     AMediaFormat* format = AMediaFormat_new();
131     AMediaFormat_setString(format, AMEDIAFORMAT_KEY_MIME, AMEDIA_MIMETYPE_VIDEO_AVC);
132     AMediaFormat_setInt32(format, AMEDIAFORMAT_KEY_BIT_RATE, 512000);
133     AMediaFormat_setInt32(format, AMEDIAFORMAT_KEY_WIDTH, 352);
134     AMediaFormat_setInt32(format, AMEDIAFORMAT_KEY_HEIGHT, 288);
135     AMediaFormat_setInt32(format, AMEDIAFORMAT_KEY_FRAME_RATE, 30);
136     AMediaFormat_setFloat(format, AMEDIAFORMAT_KEY_I_FRAME_INTERVAL, 1.0F);
137     AMediaFormat_setInt32(format, AMEDIAFORMAT_KEY_COLOR_FORMAT, COLOR_FormatYUV420Flexible);
138     return format;
139 }
140 
setupCodec(bool isAudio,bool isEncoder)141 bool NativeCodecUnitTest::setupCodec(bool isAudio, bool isEncoder) {
142     bool isPass = true;
143     mFormat = isAudio ? getSampleAudioFormat() : getSampleVideoFormat();
144     const char* mime = nullptr;
145     AMediaFormat_getString(mFormat, AMEDIAFORMAT_KEY_MIME, &mime);
146     mCodec = isEncoder ? AMediaCodec_createEncoderByType(mime)
147                        : AMediaCodec_createDecoderByType(mime);
148     if (!mCodec) {
149         ALOGE("unable to create codec %s", mime);
150         isPass = false;
151     }
152     return isPass;
153 }
154 
155 /* Structure to keep format key and their value to initialize format. Value can be of type
156  * string(stringVal) or int(intVal). At once, only one of stringVal or intVal is initialise with
157  * valid value. */
158 struct formatKey {
159     const char* key = nullptr;
160     const char* stringVal = nullptr;
161     int32_t intVal = 0;
162 };
163 
setUpDefaultFormatElementsList(std::vector<formatKey * > & vec,bool isAudio,bool isEncoder)164 void setUpDefaultFormatElementsList(std::vector<formatKey*>& vec, bool isAudio, bool isEncoder) {
165     if (isAudio) {
166         vec.push_back(new formatKey{AMEDIAFORMAT_KEY_MIME, AMEDIA_MIMETYPE_AUDIO_AAC, -1});
167         vec.push_back(new formatKey{AMEDIAFORMAT_KEY_SAMPLE_RATE, nullptr, 16000});
168         vec.push_back(new formatKey{AMEDIAFORMAT_KEY_CHANNEL_COUNT, nullptr, 1});
169         if (isEncoder) {
170             vec.push_back(new formatKey{AMEDIAFORMAT_KEY_BIT_RATE, nullptr, 64000});
171         }
172     } else {
173         vec.push_back(new formatKey{AMEDIAFORMAT_KEY_MIME, AMEDIA_MIMETYPE_VIDEO_AVC, -1});
174         vec.push_back(new formatKey{AMEDIAFORMAT_KEY_WIDTH, nullptr, 176});
175         vec.push_back(new formatKey{AMEDIAFORMAT_KEY_HEIGHT, nullptr, 144});
176         if (isEncoder) {
177             vec.push_back(new formatKey{AMEDIAFORMAT_KEY_FRAME_RATE, nullptr, 24});
178             vec.push_back(new formatKey{AMEDIAFORMAT_KEY_I_FRAME_INTERVAL, nullptr, 1});
179             vec.push_back(new formatKey{AMEDIAFORMAT_KEY_BIT_RATE, nullptr, 256000});
180             vec.push_back(new formatKey{AMEDIAFORMAT_KEY_COLOR_FORMAT, nullptr,
181                                         COLOR_FormatYUV420Flexible});
182         }
183     }
184 }
185 
deleteDefaultFormatElementsList(std::vector<formatKey * > & vector)186 void deleteDefaultFormatElementsList(std::vector<formatKey*>& vector) {
187     for (int i = 0; i < vector.size(); i++) delete vector.at(i);
188 }
189 
getSampleFormat(std::vector<formatKey * > vector,int skipIndex)190 AMediaFormat* getSampleFormat(std::vector<formatKey*> vector, int skipIndex) {
191     AMediaFormat* format = AMediaFormat_new();
192     for (int i = 0; i < vector.size(); i++) {
193         if (skipIndex == i) continue;
194         formatKey* element = vector.at(i);
195         if (element->stringVal) {
196             AMediaFormat_setString(format, element->key, element->stringVal);
197         } else {
198             AMediaFormat_setInt32(format, element->key, element->intVal);
199         }
200     }
201     return format;
202 }
203 
testConfigureCodecForIncompleteFormat(bool isAudio,bool isEncoder)204 bool NativeCodecUnitTest::testConfigureCodecForIncompleteFormat(bool isAudio, bool isEncoder) {
205     const char* mime = isAudio ? AMEDIA_MIMETYPE_AUDIO_AAC : AMEDIA_MIMETYPE_VIDEO_AVC;
206     mCodec = isEncoder ? AMediaCodec_createEncoderByType(mime)
207                        : AMediaCodec_createDecoderByType(mime);
208     if (!mCodec) {
209         ALOGE("unable to create codec %s", mime);
210         return false;
211     }
212     std::vector<formatKey*> vector;
213     bool isPass = true;
214     setUpDefaultFormatElementsList(vector, isAudio, isEncoder);
215     AMediaFormat* format = nullptr;
216     int i;
217     for (i = 0; i < vector.size(); i++) {
218         if (!isPass) break;
219         format = getSampleFormat(vector, i);
220         if (AMEDIA_OK == AMediaCodec_configure(mCodec, format, nullptr, nullptr,
221                                                isEncoder ? AMEDIACODEC_CONFIGURE_FLAG_ENCODE : 0)) {
222             ALOGE("codec configure succeeds for format with missing key %s", vector.at(i)->key);
223             isPass = false;
224         }
225         AMediaFormat_delete(format);
226     }
227     format = getSampleFormat(vector, i);
228     if (AMEDIA_OK != AMediaCodec_configure(mCodec, format, nullptr, nullptr,
229                                            isEncoder ? AMEDIACODEC_CONFIGURE_FLAG_ENCODE : 0)) {
230         ALOGE("codec configure fails for valid format %s", AMediaFormat_toString(format));
231         isPass = false;
232     }
233     AMediaFormat_delete(format);
234     deleteDefaultFormatElementsList(vector);
235     return isPass;
236 }
237 
testConfigureCodecForBadFlags(bool isEncoder)238 bool NativeCodecUnitTest::testConfigureCodecForBadFlags(bool isEncoder) {
239     bool isAudio = true;
240     if (!setupCodec(isAudio, isEncoder)) return false;
241     bool isPass = true;
242     if (AMEDIA_OK == AMediaCodec_configure(mCodec, mFormat, nullptr, nullptr,
243                                            isEncoder ? 0 : AMEDIACODEC_CONFIGURE_FLAG_ENCODE)) {
244         isPass = false;
245         ALOGE("codec configure succeeds with bad configure flag");
246     }
247     AMediaCodec_stop(mCodec);
248     return isPass;
249 }
250 
testConfigureInInitState()251 bool NativeCodecUnitTest::testConfigureInInitState() {
252     bool isAudio = true;
253     bool isEncoder = true;
254     if (!setupCodec(isAudio, isEncoder)) return false;
255     const bool boolStates[]{true, false};
256     for (auto isAsync : boolStates) {
257         if (!configureCodec(mFormat, isAsync, true, isEncoder)) return false;
258         if (AMEDIA_OK == AMediaCodec_configure(mCodec, mFormat, nullptr, nullptr,
259                                                isEncoder ? AMEDIACODEC_CONFIGURE_FLAG_ENCODE : 0)) {
260             ALOGE("codec configure succeeds in initialized state");
261             return false;
262         }
263         CHECK_STATUS(AMediaCodec_stop(mCodec), "AMediaCodec_stop failed");
264     }
265     return !hasSeenError();
266 }
267 
testConfigureInRunningState()268 bool NativeCodecUnitTest::testConfigureInRunningState() {
269     bool isAudio = true;
270     bool isEncoder = true;
271     if (!setupCodec(isAudio, isEncoder)) return false;
272     const bool boolStates[]{true, false};
273     for (auto isAsync : boolStates) {
274         if (!configureCodec(mFormat, isAsync, false, isEncoder)) return false;
275         CHECK_STATUS(AMediaCodec_start(mCodec), "AMediaCodec_start failed");
276         if (AMEDIA_OK == AMediaCodec_configure(mCodec, mFormat, nullptr, nullptr,
277                                                isEncoder ? AMEDIACODEC_CONFIGURE_FLAG_ENCODE : 0)) {
278             ALOGE("codec configure succeeds in initialized state");
279             return false;
280         }
281         if (!flushCodec()) return false;
282         if (AMEDIA_OK == AMediaCodec_configure(mCodec, mFormat, nullptr, nullptr,
283                                                isEncoder ? AMEDIACODEC_CONFIGURE_FLAG_ENCODE : 0)) {
284             ALOGE("codec configure succeeds in flush state");
285             return false;
286         }
287         if (mIsCodecInAsyncMode) {
288             CHECK_STATUS(AMediaCodec_start(mCodec), "AMediaCodec_start failed");
289         }
290         if (!queueEOS()) return false;
291         if (AMEDIA_OK == AMediaCodec_configure(mCodec, mFormat, nullptr, nullptr,
292                                                isEncoder ? AMEDIACODEC_CONFIGURE_FLAG_ENCODE : 0)) {
293             ALOGE("codec configure succeeds in running state");
294             return false;
295         }
296         if (!waitForAllOutputs()) return false;
297         if (AMEDIA_OK == AMediaCodec_configure(mCodec, mFormat, nullptr, nullptr,
298                                                isEncoder ? AMEDIACODEC_CONFIGURE_FLAG_ENCODE : 0)) {
299             ALOGE("codec configure succeeds in eos state");
300             return false;
301         }
302         CHECK_STATUS(AMediaCodec_stop(mCodec), "AMediaCodec_stop failed");
303     }
304     return !hasSeenError();
305 }
306 
testConfigureInUnInitState()307 bool NativeCodecUnitTest::testConfigureInUnInitState() {
308     bool isAudio = true;
309     bool isEncoder = true;
310     if (!setupCodec(isAudio, isEncoder)) return false;
311     const bool boolStates[]{true, false};
312     for (auto isAsync : boolStates) {
313         if (!configureCodec(mFormat, isAsync, false, isEncoder)) return false;
314         CHECK_STATUS(AMediaCodec_stop(mCodec), "AMediaCodec_stop failed");
315         CHECK_STATUS(AMediaCodec_configure(mCodec, mFormat, nullptr, nullptr,
316                                            isEncoder ? AMEDIACODEC_CONFIGURE_FLAG_ENCODE : 0),
317                      "codec configure fails in uninitialized state");
318         CHECK_STATUS(AMediaCodec_stop(mCodec), "AMediaCodec_stop failed");
319     }
320     return !hasSeenError();
321 }
322 
testDequeueInputBufferInInitState()323 bool NativeCodecUnitTest::testDequeueInputBufferInInitState() {
324     bool isAudio = true;
325     bool isEncoder = true;
326     if (!setupCodec(isAudio, isEncoder)) return false;
327     const bool boolStates[]{true, false};
328     for (auto isAsync : boolStates) {
329         if (!configureCodec(mFormat, isAsync, false, isEncoder)) return false;
330         if (AMediaCodec_dequeueInputBuffer(mCodec, kQDeQTimeOutUs) >=
331             AMEDIACODEC_INFO_TRY_AGAIN_LATER) {
332             ALOGE("dequeue input buffer succeeds in uninitialized state");
333             return false;
334         }
335         CHECK_STATUS(AMediaCodec_stop(mCodec), "AMediaCodec_stop failed");
336     }
337     return !hasSeenError();
338 }
339 
testDequeueInputBufferInRunningState()340 bool NativeCodecUnitTest::testDequeueInputBufferInRunningState() {
341     bool isAudio = true;
342     bool isEncoder = true;
343     if (!setupCodec(isAudio, isEncoder)) return false;
344     const bool boolStates[]{true, false};
345     for (auto isAsync : boolStates) {
346         if (!configureCodec(mFormat, isAsync, false, isEncoder)) return false;
347         CHECK_STATUS(AMediaCodec_start(mCodec), "AMediaCodec_start failed");
348         if (mIsCodecInAsyncMode) {
349             if (AMediaCodec_dequeueInputBuffer(mCodec, kQDeQTimeOutUs) >=
350                 AMEDIACODEC_INFO_TRY_AGAIN_LATER) {
351                 ALOGE("dequeue input buffer succeeds in running state in async mode");
352                 return false;
353             }
354         }
355         if (!queueEOS()) return false;
356         if (!waitForAllOutputs()) return false;
357         CHECK_STATUS(AMediaCodec_stop(mCodec), "AMediaCodec_stop failed");
358     }
359     return !hasSeenError();
360 }
361 
testDequeueInputBufferInUnInitState()362 bool NativeCodecUnitTest::testDequeueInputBufferInUnInitState() {
363     bool isAudio = true;
364     bool isEncoder = true;
365     if (!setupCodec(isAudio, isEncoder)) return false;
366     const bool boolStates[]{true, false};
367     for (auto isAsync : boolStates) {
368         if (AMediaCodec_dequeueInputBuffer(mCodec, kQDeQTimeOutUs) >=
369             AMEDIACODEC_INFO_TRY_AGAIN_LATER) {
370             ALOGE("dequeue input buffer succeeds in uninitialized state");
371             return false;
372         }
373         if (!configureCodec(mFormat, isAsync, false, isEncoder)) return false;
374         CHECK_STATUS(AMediaCodec_start(mCodec), "AMediaCodec_start failed");
375         CHECK_STATUS(AMediaCodec_stop(mCodec), "AMediaCodec_stop failed");
376         if (AMediaCodec_dequeueInputBuffer(mCodec, kQDeQTimeOutUs) >= -1) {
377             ALOGE("dequeue input buffer succeeds in stopped state");
378             return false;
379         }
380     }
381     return !hasSeenError();
382 }
383 
testDequeueOutputBufferInInitState()384 bool NativeCodecUnitTest::testDequeueOutputBufferInInitState() {
385     bool isAudio = true;
386     bool isEncoder = true;
387     if (!setupCodec(isAudio, isEncoder)) return false;
388     const bool boolStates[]{true, false};
389     for (auto isAsync : boolStates) {
390         if (!configureCodec(mFormat, isAsync, false, isEncoder)) return false;
391         AMediaCodecBufferInfo outInfo;
392         if (AMediaCodec_dequeueOutputBuffer(mCodec, &outInfo, kQDeQTimeOutUs) >=
393             AMEDIACODEC_INFO_OUTPUT_BUFFERS_CHANGED) {
394             ALOGE("dequeue output buffer succeeds in uninitialized state");
395             return false;
396         }
397         CHECK_STATUS(AMediaCodec_stop(mCodec), "AMediaCodec_stop failed");
398     }
399     return !hasSeenError();
400 }
401 
testDequeueOutputBufferInRunningState()402 bool NativeCodecUnitTest::testDequeueOutputBufferInRunningState() {
403     bool isAudio = true;
404     bool isEncoder = true;
405     if (!setupCodec(isAudio, isEncoder)) return false;
406     const bool boolStates[]{true, false};
407     for (auto isAsync : boolStates) {
408         if (!configureCodec(mFormat, isAsync, false, isEncoder)) return false;
409         CHECK_STATUS(AMediaCodec_start(mCodec), "AMediaCodec_start failed");
410         if (mIsCodecInAsyncMode) {
411             AMediaCodecBufferInfo outInfo;
412             if (AMediaCodec_dequeueOutputBuffer(mCodec, &outInfo, kQDeQTimeOutUs) >=
413                 AMEDIACODEC_INFO_OUTPUT_BUFFERS_CHANGED) {
414                 ALOGE("dequeue output buffer succeeds in running state in async mode");
415                 return false;
416             }
417         }
418         if (!queueEOS()) return false;
419         if (!waitForAllOutputs()) return false;
420         CHECK_STATUS(AMediaCodec_stop(mCodec), "AMediaCodec_stop failed");
421     }
422     return !hasSeenError();
423 }
424 
testDequeueOutputBufferInUnInitState()425 bool NativeCodecUnitTest::testDequeueOutputBufferInUnInitState() {
426     bool isAudio = true;
427     bool isEncoder = true;
428     if (!setupCodec(isAudio, isEncoder)) return false;
429     const bool boolStates[]{true, false};
430     for (auto isAsync : boolStates) {
431         AMediaCodecBufferInfo outInfo;
432         if (AMediaCodec_dequeueOutputBuffer(mCodec, &outInfo, kQDeQTimeOutUs) >=
433             AMEDIACODEC_INFO_OUTPUT_BUFFERS_CHANGED) {
434             ALOGE("dequeue output buffer succeeds in uninitialized state");
435             return false;
436         }
437         if (!configureCodec(mFormat, isAsync, false, isEncoder)) return false;
438         CHECK_STATUS(AMediaCodec_start(mCodec), "AMediaCodec_start failed");
439         CHECK_STATUS(AMediaCodec_stop(mCodec), "AMediaCodec_stop failed");
440         if (AMediaCodec_dequeueOutputBuffer(mCodec, &outInfo, kQDeQTimeOutUs) >=
441             AMEDIACODEC_INFO_OUTPUT_BUFFERS_CHANGED) {
442             ALOGE("dequeue output buffer succeeds in stopped state");
443             return false;
444         }
445     }
446     return !hasSeenError();
447 }
448 
testFlushInInitState()449 bool NativeCodecUnitTest::testFlushInInitState() {
450     bool isAudio = true;
451     bool isEncoder = true;
452     if (!setupCodec(isAudio, isEncoder)) return false;
453     const bool boolStates[]{true, false};
454     for (auto isAsync : boolStates) {
455         if (!configureCodec(mFormat, isAsync, false, isEncoder)) return false;
456         if (flushCodec()) {
457             ALOGE("codec flush succeeds in uninitialized state");
458             return false;
459         }
460         CHECK_STATUS(AMediaCodec_stop(mCodec), "AMediaCodec_stop failed");
461     }
462     return !hasSeenError();
463 }
464 
testFlushInRunningState()465 bool NativeCodecUnitTest::testFlushInRunningState() {
466     bool isAudio = true;
467     bool isEncoder = true;
468     if (!setupCodec(isAudio, isEncoder)) return false;
469     bool isAsync = true;
470     if (!configureCodec(mFormat, isAsync, false, isEncoder)) return false;
471     CHECK_STATUS(AMediaCodec_start(mCodec), "AMediaCodec_start failed");
472     if (!flushCodec()) return false;
473     std::this_thread::sleep_for(std::chrono::milliseconds(kStallTimeMs));
474     if (!mAsyncHandle.isInputQueueEmpty()) {
475         ALOGE("received input buffer callback before start");
476         return false;
477     }
478     CHECK_STATUS(AMediaCodec_start(mCodec), "AMediaCodec_start failed");
479     std::this_thread::sleep_for(std::chrono::milliseconds(kStallTimeMs));
480     if (mAsyncHandle.isInputQueueEmpty()) {
481         ALOGE("did not receive input buffer callback after start");
482         return false;
483     }
484     CHECK_STATUS(AMediaCodec_stop(mCodec), "AMediaCodec_stop failed");
485     return !hasSeenError();
486 }
487 
testFlushInUnInitState()488 bool NativeCodecUnitTest::testFlushInUnInitState() {
489     bool isAudio = true;
490     bool isEncoder = true;
491     if (!setupCodec(isAudio, isEncoder)) return false;
492     const bool boolStates[]{true, false};
493     for (auto isAsync : boolStates) {
494         if (flushCodec()) {
495             ALOGE("codec flush succeeds in uninitialized state");
496             return false;
497         }
498         if (!configureCodec(mFormat, isAsync, false, isEncoder)) return false;
499         CHECK_STATUS(AMediaCodec_start(mCodec), "AMediaCodec_start failed");
500         CHECK_STATUS(AMediaCodec_stop(mCodec), "AMediaCodec_stop failed");
501         if (flushCodec()) {
502             ALOGE("codec flush succeeds in uninitialized state");
503             return false;
504         }
505     }
506     return !hasSeenError();
507 }
508 
testGetNameInInitState()509 bool NativeCodecUnitTest::testGetNameInInitState() {
510     bool isAudio = true;
511     bool isEncoder = true;
512     if (!setupCodec(isAudio, isEncoder)) return false;
513     const bool boolStates[]{true, false};
514     for (auto isAsync : boolStates) {
515         if (!configureCodec(mFormat, isAsync, false, isEncoder)) return false;
516         char* name = nullptr;
517         if (AMEDIA_OK != AMediaCodec_getName(mCodec, &name) || !name) {
518             ALOGE("codec get metadata call fails in initialized state");
519             if (name) AMediaCodec_releaseName(mCodec, name);
520             return false;
521         }
522         if (name) AMediaCodec_releaseName(mCodec, name);
523         CHECK_STATUS(AMediaCodec_stop(mCodec), "AMediaCodec_stop failed");
524     }
525     return !hasSeenError();
526 }
527 
testGetNameInRunningState()528 bool NativeCodecUnitTest::testGetNameInRunningState() {
529     bool isAudio = true;
530     bool isEncoder = true;
531     if (!setupCodec(isAudio, isEncoder)) return false;
532     const bool boolStates[]{true, false};
533     for (auto isAsync : boolStates) {
534         if (!configureCodec(mFormat, isAsync, false, isEncoder)) return false;
535         CHECK_STATUS(AMediaCodec_start(mCodec), "AMediaCodec_start failed");
536         char* name = nullptr;
537         if (AMEDIA_OK != AMediaCodec_getName(mCodec, &name) || !name) {
538             ALOGE("codec get metadata call fails in running state");
539             if (name) AMediaCodec_releaseName(mCodec, name);
540             return false;
541         }
542         if (name) AMediaCodec_releaseName(mCodec, name);
543         name = nullptr;
544         if (!queueEOS()) return false;
545         if (!waitForAllOutputs()) return false;
546         if (AMEDIA_OK != AMediaCodec_getName(mCodec, &name) || !name) {
547             ALOGE("codec get metadata call fails in running state");
548             if (name) AMediaCodec_releaseName(mCodec, name);
549             return false;
550         }
551         if (name) AMediaCodec_releaseName(mCodec, name);
552         CHECK_STATUS(AMediaCodec_stop(mCodec), "AMediaCodec_stop failed");
553     }
554     return !hasSeenError();
555 }
556 
testGetNameInUnInitState()557 bool NativeCodecUnitTest::testGetNameInUnInitState() {
558     bool isAudio = true;
559     bool isEncoder = true;
560     if (!setupCodec(isAudio, isEncoder)) return false;
561     const bool boolStates[]{true, false};
562     char* name = nullptr;
563     if (AMEDIA_OK != AMediaCodec_getName(mCodec, &name) || !name) {
564         ALOGE("codec get metadata call fails in uninitialized state");
565         if (name) AMediaCodec_releaseName(mCodec, name);
566         return false;
567     }
568     if (name) AMediaCodec_releaseName(mCodec, name);
569     name = nullptr;
570     for (auto isAsync : boolStates) {
571         if (!configureCodec(mFormat, isAsync, false, isEncoder)) return false;
572         CHECK_STATUS(AMediaCodec_start(mCodec), "AMediaCodec_start failed");
573         CHECK_STATUS(AMediaCodec_stop(mCodec), "AMediaCodec_stop failed");
574         if (AMEDIA_OK != AMediaCodec_getName(mCodec, &name) || !name) {
575             ALOGE("codec get metadata call fails in uninitialized state");
576             if (name) AMediaCodec_releaseName(mCodec, name);
577             return false;
578         }
579         if (name) AMediaCodec_releaseName(mCodec, name);
580     }
581     return !hasSeenError();
582 }
583 
testSetAsyncNotifyCallbackInInitState()584 bool NativeCodecUnitTest::testSetAsyncNotifyCallbackInInitState() {
585     bool isAudio = true;
586     bool isEncoder = true;
587     if (!setupCodec(isAudio, isEncoder)) return false;
588     bool isAsync = true;
589 
590     // configure component in sync mode
591     if (!configureCodec(mFormat, !isAsync, false, isEncoder)) return false;
592     // setCallBack in async mode
593     CHECK_STATUS(mAsyncHandle.setCallBack(mCodec, isAsync),
594                  "AMediaCodec_setAsyncNotifyCallback failed");
595     mIsCodecInAsyncMode = true;
596     CHECK_STATUS(AMediaCodec_start(mCodec), "AMediaCodec_start failed");
597     if (!queueEOS()) return false;
598     if (!waitForAllOutputs()) return false;
599     CHECK_STATUS(AMediaCodec_stop(mCodec), "AMediaCodec_stop failed");
600 
601     // configure component in async mode
602     if (!configureCodec(mFormat, isAsync, false, isEncoder)) return false;
603     CHECK_STATUS(AMediaCodec_start(mCodec), "AMediaCodec_start failed");
604     if (!queueEOS()) return false;
605     if (!waitForAllOutputs()) return false;
606     CHECK_STATUS(AMediaCodec_stop(mCodec), "AMediaCodec_stop failed");
607 
608     // configure component in async mode
609     if (!configureCodec(mFormat, isAsync, false, isEncoder)) return false;
610     CHECK_STATUS(AMediaCodec_stop(mCodec), "AMediaCodec_stop failed");
611     // configure component in sync mode
612     if (!reConfigureCodec(mFormat, !isAsync, false, isEncoder)) return false;
613     CHECK_STATUS(AMediaCodec_start(mCodec), "AMediaCodec_start failed");
614     if (!queueEOS()) return false;
615     if (!waitForAllOutputs()) return false;
616     CHECK_STATUS(AMediaCodec_stop(mCodec), "AMediaCodec_stop failed");
617     return !hasSeenError();
618 }
619 
testSetAsyncNotifyCallbackInRunningState()620 bool NativeCodecUnitTest::testSetAsyncNotifyCallbackInRunningState() {
621     bool isAudio = true;
622     bool isEncoder = true;
623     if (!setupCodec(isAudio, isEncoder)) return false;
624     const bool boolStates[]{true, false};
625     for (auto isAsync : boolStates) {
626         if (!configureCodec(mFormat, isAsync, false, isEncoder)) return false;
627         CHECK_STATUS(AMediaCodec_start(mCodec), "AMediaCodec_start failed");
628         // setCallBack in async mode
629         if (AMEDIA_OK == mAsyncHandle.setCallBack(mCodec, true)) {
630             ALOGE("setAsyncNotifyCallback call succeeds in running state");
631             return false;
632         }
633         if (!queueEOS()) return false;
634         if (!waitForAllOutputs()) return false;
635         CHECK_STATUS(AMediaCodec_stop(mCodec), "AMediaCodec_stop failed");
636     }
637     return !hasSeenError();
638 }
639 
testSetAsyncNotifyCallbackInUnInitState()640 bool NativeCodecUnitTest::testSetAsyncNotifyCallbackInUnInitState() {
641     bool isAudio = true;
642     bool isEncoder = true;
643     if (!setupCodec(isAudio, isEncoder)) return false;
644     bool isAsync = true;
645     // setCallBack in async mode
646     CHECK_STATUS(mAsyncHandle.setCallBack(mCodec, isAsync),
647                  "AMediaCodec_setAsyncNotifyCallback fails in uninitalized state");
648     CHECK_STATUS(AMediaCodec_stop(mCodec), "AMediaCodec_stop failed");
649     // configure component in sync mode
650     if (!reConfigureCodec(mFormat, !isAsync, false, isEncoder)) return false;
651     CHECK_STATUS(AMediaCodec_start(mCodec), "AMediaCodec_start failed");
652     if (!queueEOS()) return false;
653     if (!waitForAllOutputs()) return false;
654     CHECK_STATUS(AMediaCodec_stop(mCodec), "AMediaCodec_stop failed");
655 
656     // setCallBack in async mode
657     CHECK_STATUS(mAsyncHandle.setCallBack(mCodec, isAsync),
658                  "AMediaCodec_setAsyncNotifyCallback fails in stopped state");
659     CHECK_STATUS(AMediaCodec_stop(mCodec), "AMediaCodec_stop failed");
660     // configure component in sync mode
661     if (!reConfigureCodec(mFormat, !isAsync, false, isEncoder)) return false;
662     CHECK_STATUS(AMediaCodec_start(mCodec), "AMediaCodec_start failed");
663     if (!queueEOS()) return false;
664     if (!waitForAllOutputs()) return false;
665     CHECK_STATUS(AMediaCodec_stop(mCodec), "AMediaCodec_stop failed");
666     return !hasSeenError();
667 }
668 
testGetInputBufferInInitState()669 bool NativeCodecUnitTest::testGetInputBufferInInitState() {
670     bool isAudio = true;
671     bool isEncoder = true;
672     if (!setupCodec(isAudio, isEncoder)) return false;
673     const bool boolStates[]{true, false};
674     for (auto isAsync : boolStates) {
675         if (!configureCodec(mFormat, isAsync, false, isEncoder)) return false;
676         size_t bufSize;
677         uint8_t* buf = AMediaCodec_getInputBuffer(mCodec, 0, &bufSize);
678         if (buf != nullptr) {
679             ALOGE("getInputBuffer succeeds in initialized state");
680             return false;
681         }
682         CHECK_STATUS(AMediaCodec_stop(mCodec), "AMediaCodec_stop failed");
683     }
684     return !hasSeenError();
685 }
686 
testGetInputBufferInRunningState()687 bool NativeCodecUnitTest::testGetInputBufferInRunningState() {
688     bool isAudio = true;
689     bool isEncoder = true;
690     if (!setupCodec(isAudio, isEncoder)) return false;
691     const bool boolStates[]{true, false};
692     for (auto isAsync : boolStates) {
693         if (!configureCodec(mFormat, isAsync, false, isEncoder)) return false;
694         CHECK_STATUS(AMediaCodec_start(mCodec), "AMediaCodec_start failed");
695         size_t bufSize;
696         if (AMediaCodec_getInputBuffer(mCodec, -1, &bufSize) != nullptr) {
697             ALOGE("getInputBuffer succeeds for bad buffer index -1");
698             return false;
699         }
700         int bufferIndex = mIsCodecInAsyncMode ? mAsyncHandle.getInput().bufferIndex
701                                               : AMediaCodec_dequeueInputBuffer(mCodec, -1);
702         uint8_t* buf = AMediaCodec_getInputBuffer(mCodec, bufferIndex, &bufSize);
703         if (buf == nullptr) {
704             ALOGE("getInputBuffer fails for valid index");
705             return false;
706         }
707         if (!enqueueEOS(bufferIndex)) return false;
708         if (!waitForAllOutputs()) return false;
709         CHECK_STATUS(AMediaCodec_stop(mCodec), "AMediaCodec_stop failed");
710     }
711     return !hasSeenError();
712 }
713 
testGetInputBufferInUnInitState()714 bool NativeCodecUnitTest::testGetInputBufferInUnInitState() {
715     bool isAudio = true;
716     bool isEncoder = true;
717     if (!setupCodec(isAudio, isEncoder)) return false;
718     const bool boolStates[]{true, false};
719     for (auto isAsync : boolStates) {
720         size_t bufSize;
721         if (AMediaCodec_getInputBuffer(mCodec, 0, &bufSize) != nullptr) {
722             ALOGE("getInputBuffer succeeds in uninitialized state");
723             return false;
724         }
725         if (!configureCodec(mFormat, isAsync, false, isEncoder)) return false;
726         CHECK_STATUS(AMediaCodec_start(mCodec), "AMediaCodec_start failed");
727         CHECK_STATUS(AMediaCodec_stop(mCodec), "AMediaCodec_stop failed");
728         if (AMediaCodec_getInputBuffer(mCodec, 0, &bufSize) != nullptr) {
729             ALOGE("getInputBuffer succeeds in stopped state");
730             return false;
731         }
732     }
733     return !hasSeenError();
734 }
735 
testGetInputFormatInInitState()736 bool NativeCodecUnitTest::testGetInputFormatInInitState() {
737     bool isAudio = true;
738     bool isEncoder = false;
739     if (!setupCodec(isAudio, isEncoder)) return false;
740     const bool boolStates[]{true, false};
741     const char* mime = nullptr;
742     AMediaFormat_getString(mFormat, AMEDIAFORMAT_KEY_MIME, &mime);
743     for (auto isAsync : boolStates) {
744         if (!configureCodec(mFormat, isAsync, false, isEncoder)) return false;
745         AMediaFormat* dupFormat = AMediaCodec_getInputFormat(mCodec);
746         const char* dupMime = nullptr;
747         AMediaFormat_getString(dupFormat, AMEDIAFORMAT_KEY_MIME, &dupMime);
748         AMediaFormat_delete(dupFormat);
749         if (!dupMime || strcmp(dupMime, mime) != 0) {
750             ALOGE("getInputFormat fails in initialized state");
751             return false;
752         }
753         CHECK_STATUS(AMediaCodec_stop(mCodec), "AMediaCodec_stop failed");
754     }
755     return !hasSeenError();
756 }
757 
testGetInputFormatInRunningState()758 bool NativeCodecUnitTest::testGetInputFormatInRunningState() {
759     bool isAudio = true;
760     bool isEncoder = false;
761     if (!setupCodec(isAudio, isEncoder)) return false;
762     const bool boolStates[]{true, false};
763     const char* mime = nullptr;
764     AMediaFormat_getString(mFormat, AMEDIAFORMAT_KEY_MIME, &mime);
765     for (auto isAsync : boolStates) {
766         if (!configureCodec(mFormat, isAsync, false, isEncoder)) return false;
767         CHECK_STATUS(AMediaCodec_start(mCodec), "AMediaCodec_start failed");
768         AMediaFormat* dupFormat = AMediaCodec_getInputFormat(mCodec);
769         const char* dupMime = nullptr;
770         AMediaFormat_getString(dupFormat, AMEDIAFORMAT_KEY_MIME, &dupMime);
771         AMediaFormat_delete(dupFormat);
772         if (!dupMime || strcmp(dupMime, mime) != 0) {
773             ALOGE("getInputFormat fails in running state");
774             return false;
775         }
776         CHECK_STATUS(AMediaCodec_stop(mCodec), "AMediaCodec_stop failed");
777     }
778     return !hasSeenError();
779 }
780 
testGetInputFormatInUnInitState()781 bool NativeCodecUnitTest::testGetInputFormatInUnInitState() {
782     bool isAudio = true;
783     bool isEncoder = true;
784     if (!setupCodec(isAudio, isEncoder)) return false;
785     const bool boolStates[]{true, false};
786     const char* mime = nullptr;
787     AMediaFormat_getString(mFormat, AMEDIAFORMAT_KEY_MIME, &mime);
788     for (auto isAsync : boolStates) {
789         AMediaFormat* dupFormat = AMediaCodec_getInputFormat(mCodec);
790         const char* dupMime = nullptr;
791         AMediaFormat_getString(dupFormat, AMEDIAFORMAT_KEY_MIME, &dupMime);
792         AMediaFormat_delete(dupFormat);
793         if (dupMime) {
794             ALOGE("getInputFormat succeeds in uninitialized state");
795             return false;
796         }
797         if (!configureCodec(mFormat, isAsync, false, isEncoder)) return false;
798         CHECK_STATUS(AMediaCodec_start(mCodec), "AMediaCodec_start failed");
799         CHECK_STATUS(AMediaCodec_stop(mCodec), "AMediaCodec_stop failed");
800         dupFormat = AMediaCodec_getInputFormat(mCodec);
801         dupMime = nullptr;
802         AMediaFormat_getString(dupFormat, AMEDIAFORMAT_KEY_MIME, &dupMime);
803         AMediaFormat_delete(dupFormat);
804         if (dupMime) {
805             ALOGE("getInputFormat succeeds in stopped state");
806             return false;
807         }
808     }
809     return !hasSeenError();
810 }
811 
testGetOutputBufferInInitState()812 bool NativeCodecUnitTest::testGetOutputBufferInInitState() {
813     bool isAudio = true;
814     bool isEncoder = true;
815     if (!setupCodec(isAudio, isEncoder)) return false;
816     const bool boolStates[]{true, false};
817     for (auto isAsync : boolStates) {
818         if (!configureCodec(mFormat, isAsync, false, isEncoder)) return false;
819         size_t bufSize;
820         if (AMediaCodec_getOutputBuffer(mCodec, 0, &bufSize) != nullptr) {
821             ALOGE("GetOutputBuffer succeeds in initialized state");
822             return false;
823         }
824         CHECK_STATUS(AMediaCodec_stop(mCodec), "AMediaCodec_stop failed");
825     }
826     return true;
827 }
828 
testGetOutputBufferInRunningState()829 bool NativeCodecUnitTest::testGetOutputBufferInRunningState() {
830     bool isAudio = true;
831     bool isEncoder = true;
832     if (!setupCodec(isAudio, isEncoder)) return false;
833     AMediaCodecBufferInfo outInfo;
834     const bool boolStates[]{true, false};
835     for (auto isAsync : boolStates) {
836         if (!configureCodec(mFormat, isAsync, false, isEncoder)) return false;
837         CHECK_STATUS(AMediaCodec_start(mCodec), "AMediaCodec_start failed");
838         size_t bufSize;
839         if (AMediaCodec_getOutputBuffer(mCodec, -1, &bufSize)) {
840             ALOGE("GetOutputBuffer succeeds for bad buffer index -1");
841             return false;
842         }
843         if (!queueEOS()) return false;
844         bool isOk = true;
845         if (!hasSeenError()) {
846             int bufferIndex = 0;
847             size_t buffSize;
848             while (!mSawOutputEOS && isOk) {
849                 if (mIsCodecInAsyncMode) {
850                     callbackObject element = mAsyncHandle.getOutput();
851                     bufferIndex = element.bufferIndex;
852                     if (element.bufferIndex >= 0) {
853                         if (!AMediaCodec_getOutputBuffer(mCodec, bufferIndex, &buffSize)) {
854                             ALOGE("GetOutputBuffer fails for valid bufffer index");
855                             return false;
856                         }
857                         isOk = dequeueOutput(element.bufferIndex, &element.bufferInfo);
858                     }
859                 } else {
860                     bufferIndex = AMediaCodec_dequeueOutputBuffer(mCodec, &outInfo, kQDeQTimeOutUs);
861                     if (bufferIndex >= 0) {
862                         if (!AMediaCodec_getOutputBuffer(mCodec, bufferIndex, &buffSize)) {
863                             ALOGE("GetOutputBuffer fails for valid bufffer index");
864                             return false;
865                         }
866                         isOk = dequeueOutput(bufferIndex, &outInfo);
867                     }
868                 }
869                 if (hasSeenError() || !isOk) {
870                     ALOGE("Got unexpected error");
871                     return false;
872                 }
873             }
874             if (AMediaCodec_getOutputBuffer(mCodec, bufferIndex, &bufSize) != nullptr) {
875                 ALOGE("getOutputBuffer succeeds for buffer index not owned by client");
876                 return false;
877             }
878         } else {
879             ALOGE("Got unexpected error");
880             return false;
881         }
882         CHECK_STATUS(AMediaCodec_stop(mCodec), "AMediaCodec_stop failed");
883     }
884     return !hasSeenError();
885 }
886 
testGetOutputBufferInUnInitState()887 bool NativeCodecUnitTest::testGetOutputBufferInUnInitState() {
888     bool isAudio = true;
889     bool isEncoder = true;
890     if (!setupCodec(isAudio, isEncoder)) return false;
891     const bool boolStates[]{true, false};
892     for (auto isAsync : boolStates) {
893         size_t bufSize;
894         if (AMediaCodec_getOutputBuffer(mCodec, 0, &bufSize) != nullptr) {
895             ALOGE("GetOutputBuffer succeeds in uninitialized state");
896             return false;
897         }
898         if (!configureCodec(mFormat, isAsync, false, isEncoder)) return false;
899         CHECK_STATUS(AMediaCodec_start(mCodec), "AMediaCodec_start failed");
900         CHECK_STATUS(AMediaCodec_stop(mCodec), "AMediaCodec_stop failed");
901         if (AMediaCodec_getOutputBuffer(mCodec, 0, &bufSize) != nullptr) {
902             ALOGE("GetOutputBuffer succeeds in stopped state");
903             return false;
904         }
905     }
906     return !hasSeenError();
907 }
908 
testGetOutputFormatInInitState()909 bool NativeCodecUnitTest::testGetOutputFormatInInitState() {
910     bool isAudio = true;
911     bool isEncoder = true;
912     if (!setupCodec(isAudio, isEncoder)) return false;
913     const char* mime = nullptr;
914     AMediaFormat_getString(mFormat, AMEDIAFORMAT_KEY_MIME, &mime);
915     const bool boolStates[]{true, false};
916     for (auto isAsync : boolStates) {
917         if (!configureCodec(mFormat, isAsync, false, isEncoder)) return false;
918         AMediaFormat* dupFormat = AMediaCodec_getOutputFormat(mCodec);
919         const char* dupMime = nullptr;
920         AMediaFormat_getString(dupFormat, AMEDIAFORMAT_KEY_MIME, &dupMime);
921         AMediaFormat_delete(dupFormat);
922         if (!dupMime || strcmp(dupMime, mime) != 0) {
923             ALOGE("getOutputFormat fails in initialized state");
924             return false;
925         }
926         CHECK_STATUS(AMediaCodec_stop(mCodec), "AMediaCodec_stop failed");
927     }
928     return !hasSeenError();
929 }
930 
testGetOutputFormatInRunningState()931 bool NativeCodecUnitTest::testGetOutputFormatInRunningState() {
932     bool isAudio = true;
933     bool isEncoder = true;
934     if (!setupCodec(isAudio, isEncoder)) return false;
935     const char* mime = nullptr;
936     AMediaFormat_getString(mFormat, AMEDIAFORMAT_KEY_MIME, &mime);
937     const bool boolStates[]{true, false};
938     for (auto isAsync : boolStates) {
939         if (!configureCodec(mFormat, isAsync, false, isEncoder)) return false;
940         CHECK_STATUS(AMediaCodec_start(mCodec), "AMediaCodec_start failed");
941         AMediaFormat* dupFormat = AMediaCodec_getOutputFormat(mCodec);
942         const char* dupMime = nullptr;
943         AMediaFormat_getString(dupFormat, AMEDIAFORMAT_KEY_MIME, &dupMime);
944         AMediaFormat_delete(dupFormat);
945         if (!dupMime || strcmp(dupMime, mime) != 0) {
946             ALOGE("getOutputFormat fails in running state");
947             return false;
948         }
949         CHECK_STATUS(AMediaCodec_stop(mCodec), "AMediaCodec_stop failed");
950     }
951     return !hasSeenError();
952 }
953 
testGetOutputFormatInUnInitState()954 bool NativeCodecUnitTest::testGetOutputFormatInUnInitState() {
955     bool isAudio = true;
956     bool isEncoder = true;
957     if (!setupCodec(isAudio, isEncoder)) return false;
958     const bool boolStates[]{true, false};
959     for (auto isAsync : boolStates) {
960         AMediaFormat* dupFormat = AMediaCodec_getOutputFormat(mCodec);
961         const char* dupMime = nullptr;
962         AMediaFormat_getString(dupFormat, AMEDIAFORMAT_KEY_MIME, &dupMime);
963         AMediaFormat_delete(dupFormat);
964         if (dupMime) {
965             ALOGE("getOutputFormat succeeds in uninitialized state");
966             return false;
967         }
968         if (!configureCodec(mFormat, isAsync, false, isEncoder)) return false;
969         CHECK_STATUS(AMediaCodec_start(mCodec), "AMediaCodec_start failed");
970         CHECK_STATUS(AMediaCodec_stop(mCodec), "AMediaCodec_stop failed");
971         dupFormat = AMediaCodec_getOutputFormat(mCodec);
972         dupMime = nullptr;
973         AMediaFormat_getString(dupFormat, AMEDIAFORMAT_KEY_MIME, &dupMime);
974         AMediaFormat_delete(dupFormat);
975         if (dupMime) {
976             ALOGE("getOutputFormat succeeds in stopped state");
977             return false;
978         }
979     }
980     return !hasSeenError();
981 }
982 
testSetParametersInInitState()983 bool NativeCodecUnitTest::testSetParametersInInitState() {
984     bool isAudio = false;
985     bool isEncoder = true;
986     if (!setupCodec(isAudio, isEncoder)) return false;
987     const bool boolStates[]{true, false};
988     for (auto isAsync : boolStates) {
989         if (!configureCodec(mFormat, isAsync, false, isEncoder)) return false;
990         int bitrate;
991         AMediaFormat_getInt32(mFormat, AMEDIAFORMAT_KEY_BIT_RATE, &bitrate);
992         AMediaFormat* params = AMediaFormat_new();
993         AMediaFormat_setInt32(params, TBD_AMEDIACODEC_PARAMETER_KEY_VIDEO_BITRATE, bitrate >> 1);
994         if (AMEDIA_OK == AMediaCodec_setParameters(mCodec, params)) {
995             ALOGE("SetParameters succeeds in initialized state");
996             AMediaFormat_delete(params);
997             return false;
998         }
999         AMediaFormat_delete(params);
1000         CHECK_STATUS(AMediaCodec_stop(mCodec), "AMediaCodec_stop failed");
1001     }
1002     return !hasSeenError();
1003 }
1004 
testSetParametersInRunningState()1005 bool NativeCodecUnitTest::testSetParametersInRunningState() {
1006     bool isAudio = false;
1007     bool isEncoder = true;
1008     if (!setupCodec(isAudio, isEncoder)) return false;
1009     const bool boolStates[]{true, false};
1010     int bitrate;
1011     AMediaFormat_getInt32(mFormat, AMEDIAFORMAT_KEY_BIT_RATE, &bitrate);
1012     for (auto isAsync : boolStates) {
1013         if (!configureCodec(mFormat, isAsync, false, isEncoder)) return false;
1014         CHECK_STATUS(AMediaCodec_start(mCodec), "AMediaCodec_start failed");
1015         // behaviour of setParams with null argument is acceptable according to SDK
1016         AMediaCodec_setParameters(mCodec, nullptr);
1017         AMediaFormat* params = AMediaFormat_new();
1018         AMediaFormat_setInt32(params, TBD_AMEDIACODEC_PARAMETER_KEY_VIDEO_BITRATE, bitrate >> 1);
1019         if (AMEDIA_OK != AMediaCodec_setParameters(mCodec, params)) {
1020             ALOGE("SetParameters fails in running state");
1021             AMediaFormat_delete(params);
1022             return false;
1023         }
1024         if (!queueEOS()) return false;
1025         AMediaCodec_setParameters(mCodec, nullptr);
1026         AMediaFormat_setInt32(mFormat, TBD_AMEDIACODEC_PARAMETER_KEY_VIDEO_BITRATE, bitrate << 1);
1027         if (AMEDIA_OK != AMediaCodec_setParameters(mCodec, mFormat)) {
1028             ALOGE("SetParameters fails in running state");
1029             AMediaFormat_delete(params);
1030             return false;
1031         }
1032         if (!waitForAllOutputs()) return false;
1033         AMediaCodec_setParameters(mCodec, nullptr);
1034         AMediaFormat_setInt32(mFormat, TBD_AMEDIACODEC_PARAMETER_KEY_VIDEO_BITRATE, bitrate);
1035         if (AMEDIA_OK != AMediaCodec_setParameters(mCodec, mFormat)) {
1036             ALOGE("SetParameters fails in running state");
1037             AMediaFormat_delete(params);
1038             return false;
1039         }
1040         AMediaFormat_delete(params);
1041         CHECK_STATUS(AMediaCodec_stop(mCodec), "AMediaCodec_stop failed");
1042     }
1043     return !hasSeenError();
1044 }
1045 
testSetParametersInUnInitState()1046 bool NativeCodecUnitTest::testSetParametersInUnInitState() {
1047     bool isAudio = false;
1048     bool isEncoder = true;
1049     if (!setupCodec(isAudio, isEncoder)) return false;
1050     const bool boolStates[]{true, false};
1051     for (auto isAsync : boolStates) {
1052         if (!configureCodec(mFormat, isAsync, false, isEncoder)) return false;
1053         CHECK_STATUS(AMediaCodec_start(mCodec), "AMediaCodec_start failed");
1054         CHECK_STATUS(AMediaCodec_stop(mCodec), "AMediaCodec_stop failed");
1055         int bitrate;
1056         AMediaFormat_getInt32(mFormat, AMEDIAFORMAT_KEY_BIT_RATE, &bitrate);
1057         AMediaFormat* params = AMediaFormat_new();
1058         AMediaFormat_setInt32(params, TBD_AMEDIACODEC_PARAMETER_KEY_VIDEO_BITRATE, bitrate >> 1);
1059         if (AMEDIA_OK == AMediaCodec_setParameters(mCodec, params)) {
1060             ALOGE("SetParameters succeeds in stopped state");
1061             AMediaFormat_delete(params);
1062             return false;
1063         }
1064         AMediaFormat_delete(params);
1065     }
1066     return !hasSeenError();
1067 }
1068 
testStartInRunningState()1069 bool NativeCodecUnitTest::testStartInRunningState() {
1070     bool isAudio = true;
1071     bool isEncoder = true;
1072     if (!setupCodec(isAudio, isEncoder)) return false;
1073     if (!configureCodec(mFormat, false, false, isEncoder)) return false;
1074     CHECK_STATUS(AMediaCodec_start(mCodec), "AMediaCodec_start failed");
1075     if (AMEDIA_OK == AMediaCodec_start(mCodec)) {
1076         ALOGE("Start succeeds in running state");
1077         return false;
1078     }
1079     CHECK_STATUS(AMediaCodec_stop(mCodec), "AMediaCodec_stop failed");
1080     return !hasSeenError();
1081 }
1082 
testStartInUnInitState()1083 bool NativeCodecUnitTest::testStartInUnInitState() {
1084     bool isAudio = true;
1085     bool isEncoder = true;
1086     if (!setupCodec(isAudio, isEncoder)) return false;
1087     if (AMEDIA_OK == AMediaCodec_start(mCodec)) {
1088         ALOGE("codec start succeeds before initialization");
1089         return false;
1090     }
1091     if (!configureCodec(mFormat, false, false, isEncoder)) return false;
1092     CHECK_STATUS(AMediaCodec_start(mCodec), "AMediaCodec_start failed");
1093     CHECK_STATUS(AMediaCodec_stop(mCodec), "AMediaCodec_stop failed");
1094     if (AMEDIA_OK == AMediaCodec_start(mCodec)) {
1095         ALOGE("codec start succeeds in stopped state");
1096         return false;
1097     }
1098     return !hasSeenError();
1099 }
1100 
testStopInInitState()1101 bool NativeCodecUnitTest::testStopInInitState() {
1102     bool isAudio = true;
1103     bool isEncoder = true;
1104     if (!setupCodec(isAudio, isEncoder)) return false;
1105     const bool boolStates[]{true, false};
1106     for (auto isAsync : boolStates) {
1107         if (!configureCodec(mFormat, isAsync, false, isEncoder)) return false;
1108         CHECK_STATUS(AMediaCodec_stop(mCodec), "Stop fails in initialized state");
1109     }
1110     return !hasSeenError();
1111 }
1112 
testStopInRunningState()1113 bool NativeCodecUnitTest::testStopInRunningState() {
1114     bool isAudio = true;
1115     bool isEncoder = true;
1116     if (!setupCodec(isAudio, isEncoder)) return false;
1117     const bool boolStates[]{true, false};
1118     for (auto isAsync : boolStates) {
1119         if (!configureCodec(mFormat, isAsync, false, isEncoder)) return false;
1120         CHECK_STATUS(AMediaCodec_start(mCodec), "AMediaCodec_start failed");
1121         if (!queueEOS()) return false;
1122         CHECK_STATUS(AMediaCodec_stop(mCodec), "Stop fails in running state");
1123     }
1124     return !hasSeenError();
1125 }
1126 
testStopInUnInitState()1127 bool NativeCodecUnitTest::testStopInUnInitState() {
1128     bool isAudio = true;
1129     bool isEncoder = true;
1130     if (!setupCodec(isAudio, isEncoder)) return false;
1131     const bool boolStates[]{true, false};
1132     for (auto isAsync : boolStates) {
1133         if (!configureCodec(mFormat, isAsync, false, isEncoder)) return false;
1134         CHECK_STATUS(AMediaCodec_start(mCodec), "AMediaCodec_start failed");
1135         CHECK_STATUS(AMediaCodec_stop(mCodec), "AMediaCodec_stop failed");
1136         CHECK_STATUS(AMediaCodec_stop(mCodec), "Stop fails in stopped state");
1137     }
1138     return !hasSeenError();
1139 }
1140 
testQueueInputBufferInInitState()1141 bool NativeCodecUnitTest::testQueueInputBufferInInitState() {
1142     bool isAudio = true;
1143     bool isEncoder = true;
1144     if (!setupCodec(isAudio, isEncoder)) return false;
1145     const bool boolStates[]{true, false};
1146     for (auto isAsync : boolStates) {
1147         if (!configureCodec(mFormat, isAsync, false, isEncoder)) return false;
1148         if (AMEDIA_OK == AMediaCodec_queueInputBuffer(mCodec, 0, 0, 0, 0,
1149                                                       AMEDIACODEC_BUFFER_FLAG_END_OF_STREAM)) {
1150             ALOGE("queueInputBuffer succeeds in initialized state");
1151             return false;
1152         }
1153         CHECK_STATUS(AMediaCodec_stop(mCodec), "AMediaCodec_stop failed");
1154     }
1155     return !hasSeenError();
1156 }
1157 
testQueueInputBufferWithBadIndex()1158 bool NativeCodecUnitTest::testQueueInputBufferWithBadIndex() {
1159     bool isAudio = true;
1160     bool isEncoder = true;
1161     if (!setupCodec(isAudio, isEncoder)) return false;
1162     const bool boolStates[]{true, false};
1163     for (auto isAsync : boolStates) {
1164         if (!configureCodec(mFormat, isAsync, false, isEncoder)) return false;
1165         CHECK_STATUS(AMediaCodec_start(mCodec), "AMediaCodec_start failed");
1166         if (AMEDIA_OK == AMediaCodec_queueInputBuffer(mCodec, -1, 0, 0, 0,
1167                                                       AMEDIACODEC_BUFFER_FLAG_END_OF_STREAM)) {
1168             ALOGE("queueInputBuffer succeeds with bad buffer index :: -1");
1169             return false;
1170         }
1171         CHECK_STATUS(AMediaCodec_stop(mCodec), "AMediaCodec_stop failed");
1172     }
1173     return !hasSeenError();
1174 }
1175 
testQueueInputBufferWithBadSize()1176 bool NativeCodecUnitTest::testQueueInputBufferWithBadSize() {
1177     bool isAudio = true;
1178     bool isEncoder = true;
1179     if (!setupCodec(isAudio, isEncoder)) return false;
1180     const bool boolStates[]{true, false};
1181     for (auto isAsync : boolStates) {
1182         if (!configureCodec(mFormat, isAsync, false, isEncoder)) return false;
1183         CHECK_STATUS(AMediaCodec_start(mCodec), "AMediaCodec_start failed");
1184         int bufferIndex = mIsCodecInAsyncMode ? mAsyncHandle.getInput().bufferIndex
1185                                               : AMediaCodec_dequeueInputBuffer(mCodec, -1);
1186         size_t bufSize;
1187         uint8_t* buf = AMediaCodec_getInputBuffer(mCodec, bufferIndex, &bufSize);
1188         if (buf == nullptr) {
1189             ALOGE("getInputBuffer fails for valid index");
1190             return false;
1191         } else {
1192             if (AMEDIA_OK == AMediaCodec_queueInputBuffer(mCodec, 0, 0, bufSize + 100, 0,
1193                                                           AMEDIACODEC_BUFFER_FLAG_END_OF_STREAM)) {
1194                 ALOGE("queueInputBuffer succeeds for bad size %d, buffer capacity %d, ",
1195                       (int)bufSize + 100, (int)bufSize);
1196                 return false;
1197             }
1198         }
1199         CHECK_STATUS(AMediaCodec_stop(mCodec), "AMediaCodec_stop failed");
1200     }
1201     return !hasSeenError();
1202 }
1203 
testQueueInputBufferWithBadBuffInfo()1204 bool NativeCodecUnitTest::testQueueInputBufferWithBadBuffInfo() {
1205     bool isAudio = true;
1206     bool isEncoder = true;
1207     if (!setupCodec(isAudio, isEncoder)) return false;
1208     const bool boolStates[]{true, false};
1209     for (auto isAsync : boolStates) {
1210         if (!configureCodec(mFormat, isAsync, false, isEncoder)) return false;
1211         CHECK_STATUS(AMediaCodec_start(mCodec), "AMediaCodec_start failed");
1212         int bufferIndex = mIsCodecInAsyncMode ? mAsyncHandle.getInput().bufferIndex
1213                                               : AMediaCodec_dequeueInputBuffer(mCodec, -1);
1214         size_t bufSize;
1215         uint8_t* buf = AMediaCodec_getInputBuffer(mCodec, bufferIndex, &bufSize);
1216         if (buf == nullptr) {
1217             ALOGE("getInputBuffer fails for valid index");
1218             return false;
1219         } else {
1220             if (AMEDIA_OK == AMediaCodec_queueInputBuffer(mCodec, 0, 16, bufSize, 0,
1221                                                           AMEDIACODEC_BUFFER_FLAG_END_OF_STREAM)) {
1222                 ALOGE("queueInputBuffer succeeds with bad offset and size param");
1223                 return false;
1224             }
1225         }
1226         CHECK_STATUS(AMediaCodec_stop(mCodec), "AMediaCodec_stop failed");
1227     }
1228     return !hasSeenError();
1229 }
1230 
testQueueInputBufferWithBadOffset()1231 bool NativeCodecUnitTest::testQueueInputBufferWithBadOffset() {
1232     bool isAudio = true;
1233     bool isEncoder = true;
1234     if (!setupCodec(isAudio, isEncoder)) return false;
1235     const bool boolStates[]{true, false};
1236     for (auto isAsync : boolStates) {
1237         if (!configureCodec(mFormat, isAsync, false, isEncoder)) return false;
1238         CHECK_STATUS(AMediaCodec_start(mCodec), "AMediaCodec_start failed");
1239         if (AMEDIA_OK == AMediaCodec_queueInputBuffer(mCodec, 0, -1, 0, 0,
1240                                                       AMEDIACODEC_BUFFER_FLAG_END_OF_STREAM)) {
1241             ALOGE("queueInputBuffer succeeds with bad buffer offset :: -1");
1242             return false;
1243         }
1244         CHECK_STATUS(AMediaCodec_stop(mCodec), "AMediaCodec_stop failed");
1245     }
1246     return !hasSeenError();
1247 }
1248 
testQueueInputBufferInUnInitState()1249 bool NativeCodecUnitTest::testQueueInputBufferInUnInitState() {
1250     bool isAudio = true;
1251     bool isEncoder = true;
1252     if (!setupCodec(isAudio, isEncoder)) return false;
1253     const bool boolStates[]{true, false};
1254     for (auto isAsync : boolStates) {
1255         if (AMEDIA_OK == AMediaCodec_queueInputBuffer(mCodec, 0, 0, 0, 0,
1256                                                       AMEDIACODEC_BUFFER_FLAG_END_OF_STREAM)) {
1257             ALOGE("queueInputBuffer succeeds in uninitialized state");
1258             return false;
1259         }
1260         if (!configureCodec(mFormat, isAsync, false, isEncoder)) return false;
1261         CHECK_STATUS(AMediaCodec_start(mCodec), "AMediaCodec_start failed");
1262         CHECK_STATUS(AMediaCodec_stop(mCodec), "AMediaCodec_stop failed");
1263         if (AMEDIA_OK == AMediaCodec_queueInputBuffer(mCodec, 0, 0, 0, 0,
1264                                                       AMEDIACODEC_BUFFER_FLAG_END_OF_STREAM)) {
1265             ALOGE("queueInputBuffer succeeds in stopped state");
1266             return false;
1267         }
1268     }
1269     return !hasSeenError();
1270 }
1271 
testReleaseOutputBufferInInitState()1272 bool NativeCodecUnitTest::testReleaseOutputBufferInInitState() {
1273     bool isAudio = true;
1274     bool isEncoder = true;
1275     if (!setupCodec(isAudio, isEncoder)) return false;
1276     const bool boolStates[]{true, false};
1277     for (auto isAsync : boolStates) {
1278         if (!configureCodec(mFormat, isAsync, false, isEncoder)) return false;
1279         if (AMEDIA_OK == AMediaCodec_releaseOutputBuffer(mCodec, 0, false)) {
1280             ALOGE("ReleaseOutputBuffer succeeds in initialized state");
1281             return false;
1282         }
1283         CHECK_STATUS(AMediaCodec_stop(mCodec), "AMediaCodec_stop failed");
1284     }
1285     return !hasSeenError();
1286 }
1287 
testReleaseOutputBufferInRunningState()1288 bool NativeCodecUnitTest::testReleaseOutputBufferInRunningState() {
1289     bool isAudio = true;
1290     bool isEncoder = true;
1291     if (!setupCodec(isAudio, isEncoder)) return false;
1292     AMediaCodecBufferInfo outInfo;
1293     const bool boolStates[]{true, false};
1294     for (auto isAsync : boolStates) {
1295         if (!configureCodec(mFormat, isAsync, false, isEncoder)) return false;
1296         CHECK_STATUS(AMediaCodec_start(mCodec), "AMediaCodec_start failed");
1297         if (AMEDIA_OK == AMediaCodec_releaseOutputBuffer(mCodec, -1, false)) {
1298             ALOGE("ReleaseOutputBuffer succeeds for bad buffer index -1");
1299             return false;
1300         }
1301         if (!queueEOS()) return false;
1302         if (!hasSeenError()) {
1303             int bufferIndex = 0;
1304             size_t buffSize;
1305             bool isOk = true;
1306             while (!mSawOutputEOS && isOk) {
1307                 if (mIsCodecInAsyncMode) {
1308                     callbackObject element = mAsyncHandle.getOutput();
1309                     bufferIndex = element.bufferIndex;
1310                     if (element.bufferIndex >= 0) {
1311                         if (!AMediaCodec_getOutputBuffer(mCodec, bufferIndex, &buffSize)) {
1312                             ALOGE("GetOutputBuffer fails for valid buffer index");
1313                             return false;
1314                         }
1315                         isOk = dequeueOutput(element.bufferIndex, &element.bufferInfo);
1316                     }
1317                 } else {
1318                     bufferIndex = AMediaCodec_dequeueOutputBuffer(mCodec, &outInfo, kQDeQTimeOutUs);
1319                     if (bufferIndex >= 0) {
1320                         if (!AMediaCodec_getOutputBuffer(mCodec, bufferIndex, &buffSize)) {
1321                             ALOGE("GetOutputBuffer fails for valid bufffer index");
1322                             return false;
1323                         }
1324                         isOk = dequeueOutput(bufferIndex, &outInfo);
1325                     }
1326                 }
1327                 if (hasSeenError() || !isOk) {
1328                     ALOGE("Got unexpected error");
1329                     return false;
1330                 }
1331             }
1332             if (AMEDIA_OK == AMediaCodec_releaseOutputBuffer(mCodec, bufferIndex, false)) {
1333                 ALOGE("ReleaseOutputBuffer succeeds for buffer index not owned by client");
1334                 return false;
1335             }
1336         } else {
1337             ALOGE("Got unexpected error");
1338             return false;
1339         }
1340         CHECK_STATUS(AMediaCodec_stop(mCodec), "AMediaCodec_stop failed");
1341     }
1342     return !hasSeenError();
1343 }
1344 
testReleaseOutputBufferInUnInitState()1345 bool NativeCodecUnitTest::testReleaseOutputBufferInUnInitState() {
1346     bool isAudio = true;
1347     bool isEncoder = true;
1348     if (!setupCodec(isAudio, isEncoder)) return false;
1349     const bool boolStates[]{true, false};
1350     for (auto isAsync : boolStates) {
1351         if (AMEDIA_OK == AMediaCodec_releaseOutputBuffer(mCodec, 0, false)) {
1352             ALOGE("ReleaseOutputBuffer succeeds in uninitialized state");
1353             return false;
1354         }
1355         if (!configureCodec(mFormat, isAsync, false, isEncoder)) return false;
1356         CHECK_STATUS(AMediaCodec_start(mCodec), "AMediaCodec_start failed");
1357         CHECK_STATUS(AMediaCodec_stop(mCodec), "AMediaCodec_stop failed");
1358         if (AMEDIA_OK == AMediaCodec_releaseOutputBuffer(mCodec, 0, false)) {
1359             ALOGE("ReleaseOutputBuffer succeeds in stopped state");
1360             return false;
1361         }
1362     }
1363     return !hasSeenError();
1364 }
1365 
testGetBufferFormatInInitState()1366 bool NativeCodecUnitTest::testGetBufferFormatInInitState() {
1367     bool isAudio = true;
1368     bool isEncoder = true;
1369     if (!setupCodec(isAudio, isEncoder)) return false;
1370     const bool boolStates[]{true, false};
1371     for (auto isAsync : boolStates) {
1372         if (!configureCodec(mFormat, isAsync, false, isEncoder)) return false;
1373         AMediaFormat* dupFormat = AMediaCodec_getBufferFormat(mCodec, 0);
1374         const char* dupMime = nullptr;
1375         AMediaFormat_getString(dupFormat, AMEDIAFORMAT_KEY_MIME, &dupMime);
1376         AMediaFormat_delete(dupFormat);
1377         if (dupMime) {
1378             ALOGE("GetBufferFormat succeeds in initialized state");
1379             return false;
1380         }
1381         CHECK_STATUS(AMediaCodec_stop(mCodec), "AMediaCodec_stop failed");
1382     }
1383     return !hasSeenError();
1384 }
1385 
testGetBufferFormatInRunningState()1386 bool NativeCodecUnitTest::testGetBufferFormatInRunningState() {
1387     bool isAudio = true;
1388     bool isEncoder = true;
1389     if (!setupCodec(isAudio, isEncoder)) return false;
1390     const char* mime = nullptr;
1391     AMediaFormat_getString(mFormat, AMEDIAFORMAT_KEY_MIME, &mime);
1392     AMediaCodecBufferInfo outInfo;
1393     const bool boolStates[]{true, false};
1394     for (auto isAsync : boolStates) {
1395         if (!configureCodec(mFormat, isAsync, false, isEncoder)) return false;
1396         CHECK_STATUS(AMediaCodec_start(mCodec), "AMediaCodec_start failed");
1397         AMediaFormat* dupFormat = AMediaCodec_getBufferFormat(mCodec, -1);
1398         const char* dupMime = nullptr;
1399         AMediaFormat_getString(dupFormat, AMEDIAFORMAT_KEY_MIME, &dupMime);
1400         AMediaFormat_delete(dupFormat);
1401         if (dupMime) {
1402             ALOGE("GetBufferFormat succeeds for bad buffer index -1");
1403             return false;
1404         }
1405         if (!queueEOS()) return false;
1406         if (!hasSeenError()) {
1407             int bufferIndex = 0;
1408             bool isOk = true;
1409             while (!mSawOutputEOS && isOk) {
1410                 if (mIsCodecInAsyncMode) {
1411                     callbackObject element = mAsyncHandle.getOutput();
1412                     bufferIndex = element.bufferIndex;
1413                     if (element.bufferIndex >= 0) {
1414                         dupFormat = AMediaCodec_getBufferFormat(mCodec, bufferIndex);
1415                         dupMime = nullptr;
1416                         AMediaFormat_getString(dupFormat, AMEDIAFORMAT_KEY_MIME, &dupMime);
1417                         AMediaFormat_delete(dupFormat);
1418                         if (!dupMime || strcmp(dupMime, mime) != 0) {
1419                             ALOGE("GetBufferFormat fails in running state");
1420                             return false;
1421                         }
1422                         isOk = dequeueOutput(element.bufferIndex, &element.bufferInfo);
1423                     }
1424                 } else {
1425                     bufferIndex = AMediaCodec_dequeueOutputBuffer(mCodec, &outInfo, kQDeQTimeOutUs);
1426                     if (bufferIndex >= 0) {
1427                         dupFormat = AMediaCodec_getBufferFormat(mCodec, bufferIndex);
1428                         dupMime = nullptr;
1429                         AMediaFormat_getString(dupFormat, AMEDIAFORMAT_KEY_MIME, &dupMime);
1430                         AMediaFormat_delete(dupFormat);
1431                         if (!dupMime || strcmp(dupMime, mime) != 0) {
1432                             ALOGE("GetBufferFormat fails in running state");
1433                             return false;
1434                         }
1435                         isOk = dequeueOutput(bufferIndex, &outInfo);
1436                     }
1437                 }
1438                 if (hasSeenError() || !isOk) {
1439                     ALOGE("Got unexpected error");
1440                     return false;
1441                 }
1442             }
1443             dupFormat = AMediaCodec_getBufferFormat(mCodec, bufferIndex);
1444             dupMime = nullptr;
1445             AMediaFormat_getString(dupFormat, AMEDIAFORMAT_KEY_MIME, &dupMime);
1446             AMediaFormat_delete(dupFormat);
1447             if (dupMime) {
1448                 ALOGE("GetBufferFormat succeeds for buffer index not owned by client");
1449                 return false;
1450             }
1451         } else {
1452             ALOGE("Got unexpected error");
1453             return false;
1454         }
1455         CHECK_STATUS(AMediaCodec_stop(mCodec), "AMediaCodec_stop failed");
1456     }
1457     return !hasSeenError();
1458 }
1459 
testGetBufferFormatInUnInitState()1460 bool NativeCodecUnitTest::testGetBufferFormatInUnInitState() {
1461     bool isAudio = true;
1462     bool isEncoder = true;
1463     if (!setupCodec(isAudio, isEncoder)) return false;
1464     const bool boolStates[]{true, false};
1465     for (auto isAsync : boolStates) {
1466         AMediaFormat* dupFormat = AMediaCodec_getBufferFormat(mCodec, 0);
1467         const char* dupMime = nullptr;
1468         AMediaFormat_getString(dupFormat, AMEDIAFORMAT_KEY_MIME, &dupMime);
1469         AMediaFormat_delete(dupFormat);
1470         if (dupMime) {
1471             ALOGE("GetBufferFormat succeeds in uninitialized state");
1472             return false;
1473         }
1474         if (!configureCodec(mFormat, isAsync, false, isEncoder)) return false;
1475         CHECK_STATUS(AMediaCodec_start(mCodec), "AMediaCodec_start failed");
1476         CHECK_STATUS(AMediaCodec_stop(mCodec), "AMediaCodec_stop failed");
1477         dupFormat = AMediaCodec_getBufferFormat(mCodec, 0);
1478         dupMime = nullptr;
1479         AMediaFormat_getString(dupFormat, AMEDIAFORMAT_KEY_MIME, &dupMime);
1480         AMediaFormat_delete(dupFormat);
1481         if (dupMime) {
1482             ALOGE("GetBufferFormat succeeds in stopped state");
1483             return false;
1484         }
1485     }
1486     return !hasSeenError();
1487 }
1488 
nativeTestCreateByCodecNameForNull(JNIEnv *,jobject)1489 static jboolean nativeTestCreateByCodecNameForNull(JNIEnv*, jobject) {
1490     bool isPass = true;
1491     AMediaCodec* codec = AMediaCodec_createCodecByName(nullptr);
1492     if (codec) {
1493         AMediaCodec_delete(codec);
1494         ALOGE("AMediaCodec_createCodecByName succeeds with null argument");
1495         isPass = false;
1496     }
1497     return static_cast<jboolean>(isPass);
1498 }
1499 
nativeTestCreateByCodecNameForInvalidName(JNIEnv *,jobject)1500 static jboolean nativeTestCreateByCodecNameForInvalidName(JNIEnv*, jobject) {
1501     bool isPass = true;
1502     AMediaCodec* codec = AMediaCodec_createCodecByName("invalid name");
1503     if (codec) {
1504         AMediaCodec_delete(codec);
1505         ALOGE("AMediaCodec_createCodecByName succeeds with invalid name");
1506         isPass = false;
1507     }
1508     return static_cast<jboolean>(isPass);
1509 }
1510 
nativeTestCreateDecoderByTypeForNull(JNIEnv *,jobject)1511 static jboolean nativeTestCreateDecoderByTypeForNull(JNIEnv*, jobject) {
1512     bool isPass = true;
1513     AMediaCodec* codec = AMediaCodec_createDecoderByType(nullptr);
1514     if (codec) {
1515         AMediaCodec_delete(codec);
1516         ALOGE("AMediaCodec_createDecoderByType succeeds with null argument");
1517         isPass = false;
1518     }
1519     return static_cast<jboolean>(isPass);
1520 }
1521 
nativeTestCreateDecoderByTypeForInvalidMime(JNIEnv *,jobject)1522 static jboolean nativeTestCreateDecoderByTypeForInvalidMime(JNIEnv*, jobject) {
1523     bool isPass = true;
1524     AMediaCodec* codec = AMediaCodec_createDecoderByType("invalid name");
1525     if (codec) {
1526         AMediaCodec_delete(codec);
1527         ALOGE("AMediaCodec_createDecoderByType succeeds with invalid name");
1528         isPass = false;
1529     }
1530     return static_cast<jboolean>(isPass);
1531 }
1532 
nativeTestCreateEncoderByTypeForNull(JNIEnv *,jobject)1533 static jboolean nativeTestCreateEncoderByTypeForNull(JNIEnv*, jobject) {
1534     bool isPass = true;
1535     AMediaCodec* codec = AMediaCodec_createEncoderByType(nullptr);
1536     if (codec) {
1537         AMediaCodec_delete(codec);
1538         ALOGE("AMediaCodec_createEncoderByType succeeds with null argument");
1539         isPass = false;
1540     }
1541     return static_cast<jboolean>(isPass);
1542 }
1543 
nativeTestCreateEncoderByTypeForInvalidMime(JNIEnv *,jobject)1544 static jboolean nativeTestCreateEncoderByTypeForInvalidMime(JNIEnv*, jobject) {
1545     bool isPass = true;
1546     AMediaCodec* codec = AMediaCodec_createEncoderByType("invalid name");
1547     if (codec) {
1548         AMediaCodec_delete(codec);
1549         ALOGE("AMediaCodec_createEncoderByType succeeds with invalid name");
1550         isPass = false;
1551     }
1552     return static_cast<jboolean>(isPass);
1553 }
1554 
nativeTestConfigureForNullFormat(JNIEnv *,jobject)1555 static jboolean nativeTestConfigureForNullFormat(JNIEnv*, jobject) {
1556     AMediaCodec* codec = AMediaCodec_createEncoderByType(AMEDIA_MIMETYPE_AUDIO_AAC);
1557     if (!codec) {
1558         ALOGE("unable to create codec %s", AMEDIA_MIMETYPE_AUDIO_AAC);
1559         return static_cast<jboolean>(false);
1560     }
1561     bool isPass = (AMEDIA_OK != AMediaCodec_configure(codec, nullptr, nullptr, nullptr,
1562                                                       AMEDIACODEC_CONFIGURE_FLAG_ENCODE));
1563     if (!isPass) {
1564         ALOGE("codec configure succeeds with null format");
1565     }
1566     AMediaCodec_delete(codec);
1567     return static_cast<jboolean>(isPass);
1568 }
1569 
nativeTestConfigureForEmptyFormat(JNIEnv *,jobject)1570 static jboolean nativeTestConfigureForEmptyFormat(JNIEnv*, jobject) {
1571     AMediaCodec* codec = AMediaCodec_createEncoderByType(AMEDIA_MIMETYPE_AUDIO_AAC);
1572     if (!codec) {
1573         ALOGE("unable to create codec %s", AMEDIA_MIMETYPE_AUDIO_AAC);
1574         return static_cast<jboolean>(false);
1575     }
1576     AMediaFormat* format = AMediaFormat_new();
1577     bool isPass = (AMEDIA_OK != AMediaCodec_configure(codec, format, nullptr, nullptr,
1578                                                       AMEDIACODEC_CONFIGURE_FLAG_ENCODE));
1579     if (!isPass) {
1580         ALOGE("codec configure succeeds with empty format");
1581     }
1582     AMediaFormat_delete(format);
1583     AMediaCodec_delete(codec);
1584     return static_cast<jboolean>(isPass);
1585 }
1586 
nativeTestConfigureCodecForIncompleteFormat(JNIEnv *,jobject,bool isAudio,bool isEncoder)1587 static jboolean nativeTestConfigureCodecForIncompleteFormat(JNIEnv*, jobject, bool isAudio,
1588                                                             bool isEncoder) {
1589     auto* nativeCodecUnitTest = new NativeCodecUnitTest(AMEDIA_MIMETYPE_AUDIO_AAC);
1590     bool isPass = nativeCodecUnitTest->testConfigureCodecForIncompleteFormat(isAudio, isEncoder);
1591     delete nativeCodecUnitTest;
1592     return static_cast<jboolean>(isPass);
1593 }
1594 
nativeTestConfigureEncoderForBadFlags(JNIEnv *,jobject)1595 static jboolean nativeTestConfigureEncoderForBadFlags(JNIEnv*, jobject) {
1596     auto* nativeCodecUnitTest = new NativeCodecUnitTest(AMEDIA_MIMETYPE_AUDIO_AAC);
1597     bool isEncoder = true;
1598     bool isPass = nativeCodecUnitTest->testConfigureCodecForBadFlags(isEncoder);
1599     delete nativeCodecUnitTest;
1600     return static_cast<jboolean>(isPass);
1601 }
1602 
nativeTestConfigureDecoderForBadFlags(JNIEnv *,jobject)1603 static jboolean nativeTestConfigureDecoderForBadFlags(JNIEnv*, jobject) {
1604     auto* nativeCodecUnitTest = new NativeCodecUnitTest(AMEDIA_MIMETYPE_AUDIO_AAC);
1605     bool isEncoder = false;
1606     bool isPass = nativeCodecUnitTest->testConfigureCodecForBadFlags(isEncoder);
1607     delete nativeCodecUnitTest;
1608     return static_cast<jboolean>(isPass);
1609 }
1610 
nativeTestConfigureInInitState(JNIEnv *,jobject)1611 static jboolean nativeTestConfigureInInitState(JNIEnv*, jobject) {
1612     auto* nativeCodecUnitTest = new NativeCodecUnitTest(AMEDIA_MIMETYPE_AUDIO_AAC);
1613     bool isPass = nativeCodecUnitTest->testConfigureInInitState();
1614     delete nativeCodecUnitTest;
1615     return static_cast<jboolean>(isPass);
1616 }
1617 
nativeTestConfigureInRunningState(JNIEnv *,jobject)1618 static jboolean nativeTestConfigureInRunningState(JNIEnv*, jobject) {
1619     auto* nativeCodecUnitTest = new NativeCodecUnitTest(AMEDIA_MIMETYPE_AUDIO_AAC);
1620     bool isPass = nativeCodecUnitTest->testConfigureInRunningState();
1621     delete nativeCodecUnitTest;
1622     return static_cast<jboolean>(isPass);
1623 }
1624 
nativeTestConfigureInUnInitState(JNIEnv *,jobject)1625 static jboolean nativeTestConfigureInUnInitState(JNIEnv*, jobject) {
1626     auto* nativeCodecUnitTest = new NativeCodecUnitTest(AMEDIA_MIMETYPE_AUDIO_AAC);
1627     bool isPass = nativeCodecUnitTest->testConfigureInUnInitState();
1628     delete nativeCodecUnitTest;
1629     return static_cast<jboolean>(isPass);
1630 }
1631 
nativeTestDequeueInputBufferInInitState(JNIEnv *,jobject)1632 static jboolean nativeTestDequeueInputBufferInInitState(JNIEnv*, jobject) {
1633     auto* nativeCodecUnitTest = new NativeCodecUnitTest(AMEDIA_MIMETYPE_AUDIO_AAC);
1634     bool isPass = nativeCodecUnitTest->testDequeueInputBufferInInitState();
1635     delete nativeCodecUnitTest;
1636     return static_cast<jboolean>(isPass);
1637 }
1638 
nativeTestDequeueInputBufferInRunningState(JNIEnv *,jobject)1639 static jboolean nativeTestDequeueInputBufferInRunningState(JNIEnv*, jobject) {
1640     auto* nativeCodecUnitTest = new NativeCodecUnitTest(AMEDIA_MIMETYPE_AUDIO_AAC);
1641     bool isPass = nativeCodecUnitTest->testDequeueInputBufferInRunningState();
1642     delete nativeCodecUnitTest;
1643     return static_cast<jboolean>(isPass);
1644 }
1645 
nativeTestDequeueInputBufferInUnInitState(JNIEnv *,jobject)1646 static jboolean nativeTestDequeueInputBufferInUnInitState(JNIEnv*, jobject) {
1647     auto* nativeCodecUnitTest = new NativeCodecUnitTest(AMEDIA_MIMETYPE_AUDIO_AAC);
1648     bool isPass = nativeCodecUnitTest->testDequeueInputBufferInUnInitState();
1649     delete nativeCodecUnitTest;
1650     return static_cast<jboolean>(isPass);
1651 }
1652 
nativeTestDequeueOutputBufferInInitState(JNIEnv *,jobject)1653 static jboolean nativeTestDequeueOutputBufferInInitState(JNIEnv*, jobject) {
1654     auto* nativeCodecUnitTest = new NativeCodecUnitTest(AMEDIA_MIMETYPE_AUDIO_AAC);
1655     bool isPass = nativeCodecUnitTest->testDequeueOutputBufferInInitState();
1656     delete nativeCodecUnitTest;
1657     return static_cast<jboolean>(isPass);
1658 }
1659 
nativeTestDequeueOutputBufferInRunningState(JNIEnv *,jobject)1660 static jboolean nativeTestDequeueOutputBufferInRunningState(JNIEnv*, jobject) {
1661     auto* nativeCodecUnitTest = new NativeCodecUnitTest(AMEDIA_MIMETYPE_AUDIO_AAC);
1662     bool isPass = nativeCodecUnitTest->testDequeueOutputBufferInRunningState();
1663     delete nativeCodecUnitTest;
1664     return static_cast<jboolean>(isPass);
1665 }
1666 
nativeTestDequeueOutputBufferInUnInitState(JNIEnv *,jobject)1667 static jboolean nativeTestDequeueOutputBufferInUnInitState(JNIEnv*, jobject) {
1668     auto* nativeCodecUnitTest = new NativeCodecUnitTest(AMEDIA_MIMETYPE_AUDIO_AAC);
1669     bool isPass = nativeCodecUnitTest->testDequeueOutputBufferInUnInitState();
1670     delete nativeCodecUnitTest;
1671     return static_cast<jboolean>(isPass);
1672 }
1673 
nativeTestFlushInInitState(JNIEnv *,jobject)1674 static jboolean nativeTestFlushInInitState(JNIEnv*, jobject) {
1675     auto* nativeCodecUnitTest = new NativeCodecUnitTest(AMEDIA_MIMETYPE_AUDIO_AAC);
1676     bool isPass = nativeCodecUnitTest->testFlushInInitState();
1677     delete nativeCodecUnitTest;
1678     return static_cast<jboolean>(isPass);
1679 }
1680 
nativeTestFlushInRunningState(JNIEnv *,jobject)1681 static jboolean nativeTestFlushInRunningState(JNIEnv*, jobject) {
1682     auto* nativeCodecUnitTest = new NativeCodecUnitTest(AMEDIA_MIMETYPE_AUDIO_AAC);
1683     bool isPass = nativeCodecUnitTest->testFlushInRunningState();
1684     delete nativeCodecUnitTest;
1685     return static_cast<jboolean>(isPass);
1686 }
1687 
nativeTestFlushInUnInitState(JNIEnv *,jobject)1688 static jboolean nativeTestFlushInUnInitState(JNIEnv*, jobject) {
1689     auto* nativeCodecUnitTest = new NativeCodecUnitTest(AMEDIA_MIMETYPE_AUDIO_AAC);
1690     bool isPass = nativeCodecUnitTest->testFlushInUnInitState();
1691     delete nativeCodecUnitTest;
1692     return static_cast<jboolean>(isPass);
1693 }
1694 
nativeTestGetNameInInitState(JNIEnv *,jobject)1695 static jboolean nativeTestGetNameInInitState(JNIEnv*, jobject) {
1696     auto* nativeCodecUnitTest = new NativeCodecUnitTest(AMEDIA_MIMETYPE_AUDIO_AAC);
1697     bool isPass = nativeCodecUnitTest->testGetNameInInitState();
1698     delete nativeCodecUnitTest;
1699     return static_cast<jboolean>(isPass);
1700 }
1701 
nativeTestGetNameInRunningState(JNIEnv *,jobject)1702 static jboolean nativeTestGetNameInRunningState(JNIEnv*, jobject) {
1703     auto* nativeCodecUnitTest = new NativeCodecUnitTest(AMEDIA_MIMETYPE_AUDIO_AAC);
1704     bool isPass = nativeCodecUnitTest->testGetNameInRunningState();
1705     delete nativeCodecUnitTest;
1706     return static_cast<jboolean>(isPass);
1707 }
1708 
nativeTestGetNameInUnInitState(JNIEnv *,jobject)1709 static jboolean nativeTestGetNameInUnInitState(JNIEnv*, jobject) {
1710     auto* nativeCodecUnitTest = new NativeCodecUnitTest(AMEDIA_MIMETYPE_AUDIO_AAC);
1711     bool isPass = nativeCodecUnitTest->testGetNameInUnInitState();
1712     delete nativeCodecUnitTest;
1713     return static_cast<jboolean>(isPass);
1714 }
1715 
nativeTestSetAsyncNotifyCallbackInInitState(JNIEnv *,jobject)1716 static jboolean nativeTestSetAsyncNotifyCallbackInInitState(JNIEnv*, jobject) {
1717     auto* nativeCodecUnitTest = new NativeCodecUnitTest(AMEDIA_MIMETYPE_AUDIO_AAC);
1718     bool isPass = nativeCodecUnitTest->testSetAsyncNotifyCallbackInInitState();
1719     delete nativeCodecUnitTest;
1720     return static_cast<jboolean>(isPass);
1721 }
1722 
nativeTestSetAsyncNotifyCallbackInRunningState(JNIEnv *,jobject)1723 static jboolean nativeTestSetAsyncNotifyCallbackInRunningState(JNIEnv*, jobject) {
1724     auto* nativeCodecUnitTest = new NativeCodecUnitTest(AMEDIA_MIMETYPE_AUDIO_AAC);
1725     bool isPass = nativeCodecUnitTest->testSetAsyncNotifyCallbackInRunningState();
1726     delete nativeCodecUnitTest;
1727     return static_cast<jboolean>(isPass);
1728 }
1729 
nativeTestSetAsyncNotifyCallbackInUnInitState(JNIEnv *,jobject)1730 static jboolean nativeTestSetAsyncNotifyCallbackInUnInitState(JNIEnv*, jobject) {
1731     auto* nativeCodecUnitTest = new NativeCodecUnitTest(AMEDIA_MIMETYPE_AUDIO_AAC);
1732     bool isPass = nativeCodecUnitTest->testSetAsyncNotifyCallbackInUnInitState();
1733     delete nativeCodecUnitTest;
1734     return static_cast<jboolean>(isPass);
1735 }
1736 
nativeTestGetInputBufferInInitState(JNIEnv *,jobject)1737 static jboolean nativeTestGetInputBufferInInitState(JNIEnv*, jobject) {
1738     auto* nativeCodecUnitTest = new NativeCodecUnitTest(AMEDIA_MIMETYPE_AUDIO_AAC);
1739     bool isPass = nativeCodecUnitTest->testGetInputBufferInInitState();
1740     delete nativeCodecUnitTest;
1741     return static_cast<jboolean>(isPass);
1742 }
1743 
nativeTestGetInputBufferInRunningState(JNIEnv *,jobject)1744 static jboolean nativeTestGetInputBufferInRunningState(JNIEnv*, jobject) {
1745     auto* nativeCodecUnitTest = new NativeCodecUnitTest(AMEDIA_MIMETYPE_AUDIO_AAC);
1746     bool isPass = nativeCodecUnitTest->testGetInputBufferInRunningState();
1747     delete nativeCodecUnitTest;
1748     return static_cast<jboolean>(isPass);
1749 }
1750 
nativeTestGetInputBufferInUnInitState(JNIEnv *,jobject)1751 static jboolean nativeTestGetInputBufferInUnInitState(JNIEnv*, jobject) {
1752     auto* nativeCodecUnitTest = new NativeCodecUnitTest(AMEDIA_MIMETYPE_AUDIO_AAC);
1753     bool isPass = nativeCodecUnitTest->testGetInputBufferInUnInitState();
1754     delete nativeCodecUnitTest;
1755     return static_cast<jboolean>(isPass);
1756 }
1757 
nativeTestGetInputFormatInInitState(JNIEnv *,jobject)1758 static jboolean nativeTestGetInputFormatInInitState(JNIEnv*, jobject) {
1759     auto* nativeCodecUnitTest = new NativeCodecUnitTest(AMEDIA_MIMETYPE_AUDIO_AAC);
1760     bool isPass = nativeCodecUnitTest->testGetInputFormatInInitState();
1761     delete nativeCodecUnitTest;
1762     return static_cast<jboolean>(isPass);
1763 }
1764 
nativeTestGetInputFormatInRunningState(JNIEnv *,jobject)1765 static jboolean nativeTestGetInputFormatInRunningState(JNIEnv*, jobject) {
1766     auto* nativeCodecUnitTest = new NativeCodecUnitTest(AMEDIA_MIMETYPE_AUDIO_AAC);
1767     bool isPass = nativeCodecUnitTest->testGetInputFormatInRunningState();
1768     delete nativeCodecUnitTest;
1769     return static_cast<jboolean>(isPass);
1770 }
1771 
nativeTestGetInputFormatInUnInitState(JNIEnv *,jobject)1772 static jboolean nativeTestGetInputFormatInUnInitState(JNIEnv*, jobject) {
1773     auto* nativeCodecUnitTest = new NativeCodecUnitTest(AMEDIA_MIMETYPE_AUDIO_AAC);
1774     bool isPass = nativeCodecUnitTest->testGetInputFormatInUnInitState();
1775     delete nativeCodecUnitTest;
1776     return static_cast<jboolean>(isPass);
1777 }
1778 
nativeTestGetOutputBufferInInitState(JNIEnv *,jobject)1779 static jboolean nativeTestGetOutputBufferInInitState(JNIEnv*, jobject) {
1780     auto* nativeCodecUnitTest = new NativeCodecUnitTest(AMEDIA_MIMETYPE_AUDIO_AAC);
1781     bool isPass = nativeCodecUnitTest->testGetOutputBufferInInitState();
1782     delete nativeCodecUnitTest;
1783     return static_cast<jboolean>(isPass);
1784 }
1785 
nativeTestGetOutputBufferInRunningState(JNIEnv *,jobject)1786 static jboolean nativeTestGetOutputBufferInRunningState(JNIEnv*, jobject) {
1787     auto* nativeCodecUnitTest = new NativeCodecUnitTest(AMEDIA_MIMETYPE_AUDIO_AAC);
1788     bool isPass = nativeCodecUnitTest->testGetOutputBufferInRunningState();
1789     delete nativeCodecUnitTest;
1790     return static_cast<jboolean>(isPass);
1791 }
1792 
nativeTestGetOutputBufferInUnInitState(JNIEnv *,jobject)1793 static jboolean nativeTestGetOutputBufferInUnInitState(JNIEnv*, jobject) {
1794     auto* nativeCodecUnitTest = new NativeCodecUnitTest(AMEDIA_MIMETYPE_AUDIO_AAC);
1795     bool isPass = nativeCodecUnitTest->testGetOutputBufferInUnInitState();
1796     delete nativeCodecUnitTest;
1797     return static_cast<jboolean>(isPass);
1798 }
1799 
nativeTestGetOutputFormatInInitState(JNIEnv *,jobject)1800 static jboolean nativeTestGetOutputFormatInInitState(JNIEnv*, jobject) {
1801     auto* nativeCodecUnitTest = new NativeCodecUnitTest(AMEDIA_MIMETYPE_AUDIO_AAC);
1802     bool isPass = nativeCodecUnitTest->testGetOutputFormatInInitState();
1803     delete nativeCodecUnitTest;
1804     return static_cast<jboolean>(isPass);
1805 }
1806 
nativeTestGetOutputFormatInRunningState(JNIEnv *,jobject)1807 static jboolean nativeTestGetOutputFormatInRunningState(JNIEnv*, jobject) {
1808     auto* nativeCodecUnitTest = new NativeCodecUnitTest(AMEDIA_MIMETYPE_AUDIO_AAC);
1809     bool isPass = nativeCodecUnitTest->testGetOutputFormatInRunningState();
1810     delete nativeCodecUnitTest;
1811     return static_cast<jboolean>(isPass);
1812 }
1813 
nativeTestGetOutputFormatInUnInitState(JNIEnv *,jobject)1814 static jboolean nativeTestGetOutputFormatInUnInitState(JNIEnv*, jobject) {
1815     auto* nativeCodecUnitTest = new NativeCodecUnitTest(AMEDIA_MIMETYPE_AUDIO_AAC);
1816     bool isPass = nativeCodecUnitTest->testGetOutputFormatInUnInitState();
1817     delete nativeCodecUnitTest;
1818     return static_cast<jboolean>(isPass);
1819 }
1820 
nativeTestSetParametersInInitState(JNIEnv *,jobject)1821 static jboolean nativeTestSetParametersInInitState(JNIEnv*, jobject) {
1822     auto* nativeCodecUnitTest = new NativeCodecUnitTest(AMEDIA_MIMETYPE_AUDIO_AAC);
1823     bool isPass = nativeCodecUnitTest->testSetParametersInInitState();
1824     delete nativeCodecUnitTest;
1825     return static_cast<jboolean>(isPass);
1826 }
1827 
nativeTestSetParametersInRunningState(JNIEnv *,jobject)1828 static jboolean nativeTestSetParametersInRunningState(JNIEnv*, jobject) {
1829     auto* nativeCodecUnitTest = new NativeCodecUnitTest(AMEDIA_MIMETYPE_AUDIO_AAC);
1830     bool isPass = nativeCodecUnitTest->testSetParametersInRunningState();
1831     delete nativeCodecUnitTest;
1832     return static_cast<jboolean>(isPass);
1833 }
1834 
nativeTestSetParametersInUnInitState(JNIEnv *,jobject)1835 static jboolean nativeTestSetParametersInUnInitState(JNIEnv*, jobject) {
1836     auto* nativeCodecUnitTest = new NativeCodecUnitTest(AMEDIA_MIMETYPE_AUDIO_AAC);
1837     bool isPass = nativeCodecUnitTest->testSetParametersInUnInitState();
1838     delete nativeCodecUnitTest;
1839     return static_cast<jboolean>(isPass);
1840 }
1841 
nativeTestStartInRunningState(JNIEnv *,jobject)1842 static jboolean nativeTestStartInRunningState(JNIEnv*, jobject) {
1843     auto* nativeCodecUnitTest = new NativeCodecUnitTest(AMEDIA_MIMETYPE_AUDIO_AAC);
1844     bool isPass = nativeCodecUnitTest->testStartInRunningState();
1845     delete nativeCodecUnitTest;
1846     return static_cast<jboolean>(isPass);
1847 }
1848 
nativeTestStartInUnInitState(JNIEnv *,jobject)1849 static jboolean nativeTestStartInUnInitState(JNIEnv*, jobject) {
1850     auto* nativeCodecUnitTest = new NativeCodecUnitTest(AMEDIA_MIMETYPE_AUDIO_AAC);
1851     bool isPass = nativeCodecUnitTest->testStartInUnInitState();
1852     delete nativeCodecUnitTest;
1853     return static_cast<jboolean>(isPass);
1854 }
1855 
nativeTestStopInInitState(JNIEnv *,jobject)1856 static jboolean nativeTestStopInInitState(JNIEnv*, jobject) {
1857     auto* nativeCodecUnitTest = new NativeCodecUnitTest(AMEDIA_MIMETYPE_AUDIO_AAC);
1858     bool isPass = nativeCodecUnitTest->testStopInInitState();
1859     delete nativeCodecUnitTest;
1860     return static_cast<jboolean>(isPass);
1861 }
1862 
nativeTestStopInRunningState(JNIEnv *,jobject)1863 static jboolean nativeTestStopInRunningState(JNIEnv*, jobject) {
1864     auto* nativeCodecUnitTest = new NativeCodecUnitTest(AMEDIA_MIMETYPE_AUDIO_AAC);
1865     bool isPass = nativeCodecUnitTest->testStopInRunningState();
1866     delete nativeCodecUnitTest;
1867     return static_cast<jboolean>(isPass);
1868 }
1869 
nativeTestStopInUnInitState(JNIEnv *,jobject)1870 static jboolean nativeTestStopInUnInitState(JNIEnv*, jobject) {
1871     auto* nativeCodecUnitTest = new NativeCodecUnitTest(AMEDIA_MIMETYPE_AUDIO_AAC);
1872     bool isPass = nativeCodecUnitTest->testStopInUnInitState();
1873     delete nativeCodecUnitTest;
1874     return static_cast<jboolean>(isPass);
1875 }
1876 
nativeTestQueueInputBufferInInitState(JNIEnv *,jobject)1877 static jboolean nativeTestQueueInputBufferInInitState(JNIEnv*, jobject) {
1878     auto* nativeCodecUnitTest = new NativeCodecUnitTest(AMEDIA_MIMETYPE_AUDIO_AAC);
1879     bool isPass = nativeCodecUnitTest->testQueueInputBufferInInitState();
1880     delete nativeCodecUnitTest;
1881     return static_cast<jboolean>(isPass);
1882 }
1883 
nativeTestQueueInputBufferWithBadIndex(JNIEnv *,jobject)1884 static jboolean nativeTestQueueInputBufferWithBadIndex(JNIEnv*, jobject) {
1885     auto* nativeCodecUnitTest = new NativeCodecUnitTest(AMEDIA_MIMETYPE_AUDIO_AAC);
1886     bool isPass = nativeCodecUnitTest->testQueueInputBufferWithBadIndex();
1887     delete nativeCodecUnitTest;
1888     return static_cast<jboolean>(isPass);
1889 }
1890 
nativeTestQueueInputBufferWithBadSize(JNIEnv *,jobject)1891 static jboolean nativeTestQueueInputBufferWithBadSize(JNIEnv*, jobject) {
1892     auto* nativeCodecUnitTest = new NativeCodecUnitTest(AMEDIA_MIMETYPE_AUDIO_AAC);
1893     bool isPass = nativeCodecUnitTest->testQueueInputBufferWithBadSize();
1894     delete nativeCodecUnitTest;
1895     return static_cast<jboolean>(isPass);
1896 }
1897 
nativeTestQueueInputBufferWithBadBuffInfo(JNIEnv *,jobject)1898 static jboolean nativeTestQueueInputBufferWithBadBuffInfo(JNIEnv*, jobject) {
1899     auto* nativeCodecUnitTest = new NativeCodecUnitTest(AMEDIA_MIMETYPE_AUDIO_AAC);
1900     bool isPass = nativeCodecUnitTest->testQueueInputBufferWithBadBuffInfo();
1901     delete nativeCodecUnitTest;
1902     return static_cast<jboolean>(isPass);
1903 }
1904 
nativeTestQueueInputBufferWithBadOffset(JNIEnv *,jobject)1905 static jboolean nativeTestQueueInputBufferWithBadOffset(JNIEnv*, jobject) {
1906     auto* nativeCodecUnitTest = new NativeCodecUnitTest(AMEDIA_MIMETYPE_AUDIO_AAC);
1907     bool isPass = nativeCodecUnitTest->testQueueInputBufferWithBadOffset();
1908     delete nativeCodecUnitTest;
1909     return static_cast<jboolean>(isPass);
1910 }
1911 
nativeTestQueueInputBufferInUnInitState(JNIEnv *,jobject)1912 static jboolean nativeTestQueueInputBufferInUnInitState(JNIEnv*, jobject) {
1913     auto* nativeCodecUnitTest = new NativeCodecUnitTest(AMEDIA_MIMETYPE_AUDIO_AAC);
1914     bool isPass = nativeCodecUnitTest->testQueueInputBufferInUnInitState();
1915     delete nativeCodecUnitTest;
1916     return static_cast<jboolean>(isPass);
1917 }
1918 
nativeTestReleaseOutputBufferInInitState(JNIEnv *,jobject)1919 static jboolean nativeTestReleaseOutputBufferInInitState(JNIEnv*, jobject) {
1920     auto* nativeCodecUnitTest = new NativeCodecUnitTest(AMEDIA_MIMETYPE_AUDIO_AAC);
1921     bool isPass = nativeCodecUnitTest->testReleaseOutputBufferInInitState();
1922     delete nativeCodecUnitTest;
1923     return static_cast<jboolean>(isPass);
1924 }
1925 
nativeTestReleaseOutputBufferInRunningState(JNIEnv *,jobject)1926 static jboolean nativeTestReleaseOutputBufferInRunningState(JNIEnv*, jobject) {
1927     auto* nativeCodecUnitTest = new NativeCodecUnitTest(AMEDIA_MIMETYPE_AUDIO_AAC);
1928     bool isPass = nativeCodecUnitTest->testReleaseOutputBufferInRunningState();
1929     delete nativeCodecUnitTest;
1930     return static_cast<jboolean>(isPass);
1931 }
1932 
nativeTestReleaseOutputBufferInUnInitState(JNIEnv *,jobject)1933 static jboolean nativeTestReleaseOutputBufferInUnInitState(JNIEnv*, jobject) {
1934     auto* nativeCodecUnitTest = new NativeCodecUnitTest(AMEDIA_MIMETYPE_AUDIO_AAC);
1935     bool isPass = nativeCodecUnitTest->testReleaseOutputBufferInUnInitState();
1936     delete nativeCodecUnitTest;
1937     return static_cast<jboolean>(isPass);
1938 }
1939 
nativeTestGetBufferFormatInInitState(JNIEnv *,jobject)1940 static jboolean nativeTestGetBufferFormatInInitState(JNIEnv*, jobject) {
1941     auto* nativeCodecUnitTest = new NativeCodecUnitTest(AMEDIA_MIMETYPE_AUDIO_AAC);
1942     bool isPass = nativeCodecUnitTest->testGetBufferFormatInInitState();
1943     delete nativeCodecUnitTest;
1944     return static_cast<jboolean>(isPass);
1945 }
1946 
nativeTestGetBufferFormatInRunningState(JNIEnv *,jobject)1947 static jboolean nativeTestGetBufferFormatInRunningState(JNIEnv*, jobject) {
1948     auto* nativeCodecUnitTest = new NativeCodecUnitTest(AMEDIA_MIMETYPE_AUDIO_AAC);
1949     bool isPass = nativeCodecUnitTest->testGetBufferFormatInRunningState();
1950     delete nativeCodecUnitTest;
1951     return static_cast<jboolean>(isPass);
1952 }
1953 
nativeTestGetBufferFormatInUnInitState(JNIEnv *,jobject)1954 static jboolean nativeTestGetBufferFormatInUnInitState(JNIEnv*, jobject) {
1955     auto* nativeCodecUnitTest = new NativeCodecUnitTest(AMEDIA_MIMETYPE_AUDIO_AAC);
1956     bool isPass = nativeCodecUnitTest->testGetBufferFormatInUnInitState();
1957     delete nativeCodecUnitTest;
1958     return static_cast<jboolean>(isPass);
1959 }
1960 
registerAndroidMediaV2CtsCodecUnitTest(JNIEnv * env)1961 int registerAndroidMediaV2CtsCodecUnitTest(JNIEnv* env) {
1962     const JNINativeMethod methodTable[] = {
1963             {"nativeTestCreateByCodecNameForNull", "()Z",
1964              (void*)nativeTestCreateByCodecNameForNull},
1965             {"nativeTestCreateByCodecNameForInvalidName", "()Z",
1966              (void*)nativeTestCreateByCodecNameForInvalidName},
1967             {"nativeTestCreateDecoderByTypeForNull", "()Z",
1968              (void*)nativeTestCreateDecoderByTypeForNull},
1969             {"nativeTestCreateDecoderByTypeForInvalidMime", "()Z",
1970              (void*)nativeTestCreateDecoderByTypeForInvalidMime},
1971             {"nativeTestCreateEncoderByTypeForNull", "()Z",
1972              (void*)nativeTestCreateEncoderByTypeForNull},
1973             {"nativeTestCreateEncoderByTypeForInvalidMime", "()Z",
1974              (void*)nativeTestCreateEncoderByTypeForInvalidMime},
1975             {"nativeTestConfigureForNullFormat", "()Z", (void*)nativeTestConfigureForNullFormat},
1976             {"nativeTestConfigureForEmptyFormat", "()Z", (void*)nativeTestConfigureForEmptyFormat},
1977             {"nativeTestConfigureCodecForIncompleteFormat", "(ZZ)Z",
1978              (void*)nativeTestConfigureCodecForIncompleteFormat},
1979             {"nativeTestConfigureEncoderForBadFlags", "()Z",
1980              (void*)nativeTestConfigureEncoderForBadFlags},
1981             {"nativeTestConfigureDecoderForBadFlags", "()Z",
1982              (void*)nativeTestConfigureDecoderForBadFlags},
1983             {"nativeTestConfigureInInitState", "()Z", (void*)nativeTestConfigureInInitState},
1984             {"nativeTestConfigureInRunningState", "()Z", (void*)nativeTestConfigureInRunningState},
1985             {"nativeTestConfigureInUnInitState", "()Z", (void*)nativeTestConfigureInUnInitState},
1986             {"nativeTestDequeueInputBufferInInitState", "()Z",
1987              (void*)nativeTestDequeueInputBufferInInitState},
1988             {"nativeTestDequeueInputBufferInRunningState", "()Z",
1989              (void*)nativeTestDequeueInputBufferInRunningState},
1990             {"nativeTestDequeueInputBufferInUnInitState", "()Z",
1991              (void*)nativeTestDequeueInputBufferInUnInitState},
1992             {"nativeTestDequeueOutputBufferInInitState", "()Z",
1993              (void*)nativeTestDequeueOutputBufferInInitState},
1994             {"nativeTestDequeueOutputBufferInRunningState", "()Z",
1995              (void*)nativeTestDequeueOutputBufferInRunningState},
1996             {"nativeTestDequeueOutputBufferInUnInitState", "()Z",
1997              (void*)nativeTestDequeueOutputBufferInUnInitState},
1998             {"nativeTestFlushInInitState", "()Z", (void*)nativeTestFlushInInitState},
1999             {"nativeTestFlushInRunningState", "()Z", (void*)nativeTestFlushInRunningState},
2000             {"nativeTestFlushInUnInitState", "()Z", (void*)nativeTestFlushInUnInitState},
2001             {"nativeTestGetNameInInitState", "()Z", (void*)nativeTestGetNameInInitState},
2002             {"nativeTestGetNameInRunningState", "()Z", (void*)nativeTestGetNameInRunningState},
2003             {"nativeTestGetNameInUnInitState", "()Z", (void*)nativeTestGetNameInUnInitState},
2004             {"nativeTestSetAsyncNotifyCallbackInInitState", "()Z",
2005              (void*)nativeTestSetAsyncNotifyCallbackInInitState},
2006             {"nativeTestSetAsyncNotifyCallbackInRunningState", "()Z",
2007              (void*)nativeTestSetAsyncNotifyCallbackInRunningState},
2008             {"nativeTestSetAsyncNotifyCallbackInUnInitState", "()Z",
2009              (void*)nativeTestSetAsyncNotifyCallbackInUnInitState},
2010             {"nativeTestGetInputBufferInInitState", "()Z",
2011              (void*)nativeTestGetInputBufferInInitState},
2012             {"nativeTestGetInputBufferInRunningState", "()Z",
2013              (void*)nativeTestGetInputBufferInRunningState},
2014             {"nativeTestGetInputBufferInUnInitState", "()Z",
2015              (void*)nativeTestGetInputBufferInUnInitState},
2016             {"nativeTestGetInputFormatInInitState", "()Z",
2017              (void*)nativeTestGetInputFormatInInitState},
2018             {"nativeTestGetInputFormatInRunningState", "()Z",
2019              (void*)nativeTestGetInputFormatInRunningState},
2020             {"nativeTestGetInputFormatInUnInitState", "()Z",
2021              (void*)nativeTestGetInputFormatInUnInitState},
2022             {"nativeTestGetOutputBufferInInitState", "()Z",
2023              (void*)nativeTestGetOutputBufferInInitState},
2024             {"nativeTestGetOutputBufferInRunningState", "()Z",
2025              (void*)nativeTestGetOutputBufferInRunningState},
2026             {"nativeTestGetOutputBufferInUnInitState", "()Z",
2027              (void*)nativeTestGetOutputBufferInUnInitState},
2028             {"nativeTestGetOutputFormatInInitState", "()Z",
2029              (void*)nativeTestGetOutputFormatInInitState},
2030             {"nativeTestGetOutputFormatInRunningState", "()Z",
2031              (void*)nativeTestGetOutputFormatInRunningState},
2032             {"nativeTestGetOutputFormatInUnInitState", "()Z",
2033              (void*)nativeTestGetOutputFormatInUnInitState},
2034             {"nativeTestSetParametersInInitState", "()Z",
2035              (void*)nativeTestSetParametersInInitState},
2036             {"nativeTestSetParametersInRunningState", "()Z",
2037              (void*)nativeTestSetParametersInRunningState},
2038             {"nativeTestSetParametersInUnInitState", "()Z",
2039              (void*)nativeTestSetParametersInUnInitState},
2040             {"nativeTestStartInRunningState", "()Z", (void*)nativeTestStartInRunningState},
2041             {"nativeTestStartInUnInitState", "()Z", (void*)nativeTestStartInUnInitState},
2042             {"nativeTestStopInInitState", "()Z", (void*)nativeTestStopInInitState},
2043             {"nativeTestStopInRunningState", "()Z", (void*)nativeTestStopInRunningState},
2044             {"nativeTestStopInUnInitState", "()Z", (void*)nativeTestStopInUnInitState},
2045             {"nativeTestQueueInputBufferInInitState", "()Z",
2046              (void*)nativeTestQueueInputBufferInInitState},
2047             {"nativeTestQueueInputBufferWithBadIndex", "()Z",
2048              (void*)nativeTestQueueInputBufferWithBadIndex},
2049             {"nativeTestQueueInputBufferWithBadSize", "()Z",
2050              (void*)nativeTestQueueInputBufferWithBadSize},
2051             {"nativeTestQueueInputBufferWithBadBuffInfo", "()Z",
2052              (void*)nativeTestQueueInputBufferWithBadBuffInfo},
2053             {"nativeTestQueueInputBufferWithBadOffset", "()Z",
2054              (void*)nativeTestQueueInputBufferWithBadOffset},
2055             {"nativeTestQueueInputBufferInUnInitState", "()Z",
2056              (void*)nativeTestQueueInputBufferInUnInitState},
2057             {"nativeTestReleaseOutputBufferInInitState", "()Z",
2058              (void*)nativeTestReleaseOutputBufferInInitState},
2059             {"nativeTestReleaseOutputBufferInRunningState", "()Z",
2060              (void*)nativeTestReleaseOutputBufferInRunningState},
2061             {"nativeTestReleaseOutputBufferInUnInitState", "()Z",
2062              (void*)nativeTestReleaseOutputBufferInUnInitState},
2063             {"nativeTestGetBufferFormatInInitState", "()Z",
2064              (void*)nativeTestGetBufferFormatInInitState},
2065             {"nativeTestGetBufferFormatInRunningState", "()Z",
2066              (void*)nativeTestGetBufferFormatInRunningState},
2067             {"nativeTestGetBufferFormatInUnInitState", "()Z",
2068              (void*)nativeTestGetBufferFormatInUnInitState},
2069 
2070     };
2071     jclass c = env->FindClass("android/mediav2/cts/CodecUnitTest$TestApiNative");
2072     return env->RegisterNatives(c, methodTable, sizeof(methodTable) / sizeof(JNINativeMethod));
2073 }
2074