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         if (!dupMime || strcmp(dupMime, mime) != 0) {
749             AMediaFormat_delete(dupFormat);
750             ALOGE("getInputFormat fails in initialized state");
751             return false;
752         }
753         AMediaFormat_delete(dupFormat);
754         CHECK_STATUS(AMediaCodec_stop(mCodec), "AMediaCodec_stop failed");
755     }
756     return !hasSeenError();
757 }
758 
testGetInputFormatInRunningState()759 bool NativeCodecUnitTest::testGetInputFormatInRunningState() {
760     bool isAudio = true;
761     bool isEncoder = false;
762     if (!setupCodec(isAudio, isEncoder)) return false;
763     const bool boolStates[]{true, false};
764     const char* mime = nullptr;
765     AMediaFormat_getString(mFormat, AMEDIAFORMAT_KEY_MIME, &mime);
766     for (auto isAsync : boolStates) {
767         if (!configureCodec(mFormat, isAsync, false, isEncoder)) return false;
768         CHECK_STATUS(AMediaCodec_start(mCodec), "AMediaCodec_start failed");
769         AMediaFormat* dupFormat = AMediaCodec_getInputFormat(mCodec);
770         const char* dupMime = nullptr;
771         AMediaFormat_getString(dupFormat, AMEDIAFORMAT_KEY_MIME, &dupMime);
772         if (!dupMime || strcmp(dupMime, mime) != 0) {
773             AMediaFormat_delete(dupFormat);
774             ALOGE("getInputFormat fails in running state");
775             return false;
776         }
777         AMediaFormat_delete(dupFormat);
778         CHECK_STATUS(AMediaCodec_stop(mCodec), "AMediaCodec_stop failed");
779     }
780     return !hasSeenError();
781 }
782 
testGetInputFormatInUnInitState()783 bool NativeCodecUnitTest::testGetInputFormatInUnInitState() {
784     bool isAudio = true;
785     bool isEncoder = true;
786     if (!setupCodec(isAudio, isEncoder)) return false;
787     const bool boolStates[]{true, false};
788     const char* mime = nullptr;
789     AMediaFormat_getString(mFormat, AMEDIAFORMAT_KEY_MIME, &mime);
790     for (auto isAsync : boolStates) {
791         AMediaFormat* dupFormat = AMediaCodec_getInputFormat(mCodec);
792         const char* dupMime = nullptr;
793         AMediaFormat_getString(dupFormat, AMEDIAFORMAT_KEY_MIME, &dupMime);
794         if (dupMime) {
795             AMediaFormat_delete(dupFormat);
796             ALOGE("getInputFormat succeeds in uninitialized state");
797             return false;
798         }
799         AMediaFormat_delete(dupFormat);
800         if (!configureCodec(mFormat, isAsync, false, isEncoder)) return false;
801         CHECK_STATUS(AMediaCodec_start(mCodec), "AMediaCodec_start failed");
802         CHECK_STATUS(AMediaCodec_stop(mCodec), "AMediaCodec_stop failed");
803         dupFormat = AMediaCodec_getInputFormat(mCodec);
804         dupMime = nullptr;
805         AMediaFormat_getString(dupFormat, AMEDIAFORMAT_KEY_MIME, &dupMime);
806         if (dupMime) {
807             AMediaFormat_delete(dupFormat);
808             ALOGE("getInputFormat succeeds in stopped state");
809             return false;
810         }
811         AMediaFormat_delete(dupFormat);
812     }
813     return !hasSeenError();
814 }
815 
testGetOutputBufferInInitState()816 bool NativeCodecUnitTest::testGetOutputBufferInInitState() {
817     bool isAudio = true;
818     bool isEncoder = true;
819     if (!setupCodec(isAudio, isEncoder)) return false;
820     const bool boolStates[]{true, false};
821     for (auto isAsync : boolStates) {
822         if (!configureCodec(mFormat, isAsync, false, isEncoder)) return false;
823         size_t bufSize;
824         if (AMediaCodec_getOutputBuffer(mCodec, 0, &bufSize) != nullptr) {
825             ALOGE("GetOutputBuffer succeeds in initialized state");
826             return false;
827         }
828         CHECK_STATUS(AMediaCodec_stop(mCodec), "AMediaCodec_stop failed");
829     }
830     return true;
831 }
832 
testGetOutputBufferInRunningState()833 bool NativeCodecUnitTest::testGetOutputBufferInRunningState() {
834     bool isAudio = true;
835     bool isEncoder = true;
836     if (!setupCodec(isAudio, isEncoder)) return false;
837     AMediaCodecBufferInfo outInfo;
838     const bool boolStates[]{true, false};
839     for (auto isAsync : boolStates) {
840         if (!configureCodec(mFormat, isAsync, false, isEncoder)) return false;
841         CHECK_STATUS(AMediaCodec_start(mCodec), "AMediaCodec_start failed");
842         size_t bufSize;
843         if (AMediaCodec_getOutputBuffer(mCodec, -1, &bufSize)) {
844             ALOGE("GetOutputBuffer succeeds for bad buffer index -1");
845             return false;
846         }
847         if (!queueEOS()) return false;
848         bool isOk = true;
849         if (!hasSeenError()) {
850             int bufferIndex = 0;
851             size_t buffSize;
852             while (!mSawOutputEOS && isOk) {
853                 if (mIsCodecInAsyncMode) {
854                     callbackObject element = mAsyncHandle.getOutput();
855                     bufferIndex = element.bufferIndex;
856                     if (element.bufferIndex >= 0) {
857                         if (!AMediaCodec_getOutputBuffer(mCodec, bufferIndex, &buffSize)) {
858                             ALOGE("GetOutputBuffer fails for valid bufffer index");
859                             return false;
860                         }
861                         isOk = dequeueOutput(element.bufferIndex, &element.bufferInfo);
862                     }
863                 } else {
864                     bufferIndex = AMediaCodec_dequeueOutputBuffer(mCodec, &outInfo, kQDeQTimeOutUs);
865                     if (bufferIndex >= 0) {
866                         if (!AMediaCodec_getOutputBuffer(mCodec, bufferIndex, &buffSize)) {
867                             ALOGE("GetOutputBuffer fails for valid bufffer index");
868                             return false;
869                         }
870                         isOk = dequeueOutput(bufferIndex, &outInfo);
871                     }
872                 }
873                 if (hasSeenError() || !isOk) {
874                     ALOGE("Got unexpected error");
875                     return false;
876                 }
877             }
878             if (AMediaCodec_getOutputBuffer(mCodec, bufferIndex, &bufSize) != nullptr) {
879                 ALOGE("getOutputBuffer succeeds for buffer index not owned by client");
880                 return false;
881             }
882         } else {
883             ALOGE("Got unexpected error");
884             return false;
885         }
886         CHECK_STATUS(AMediaCodec_stop(mCodec), "AMediaCodec_stop failed");
887     }
888     return !hasSeenError();
889 }
890 
testGetOutputBufferInUnInitState()891 bool NativeCodecUnitTest::testGetOutputBufferInUnInitState() {
892     bool isAudio = true;
893     bool isEncoder = true;
894     if (!setupCodec(isAudio, isEncoder)) return false;
895     const bool boolStates[]{true, false};
896     for (auto isAsync : boolStates) {
897         size_t bufSize;
898         if (AMediaCodec_getOutputBuffer(mCodec, 0, &bufSize) != nullptr) {
899             ALOGE("GetOutputBuffer succeeds in uninitialized state");
900             return false;
901         }
902         if (!configureCodec(mFormat, isAsync, false, isEncoder)) return false;
903         CHECK_STATUS(AMediaCodec_start(mCodec), "AMediaCodec_start failed");
904         CHECK_STATUS(AMediaCodec_stop(mCodec), "AMediaCodec_stop failed");
905         if (AMediaCodec_getOutputBuffer(mCodec, 0, &bufSize) != nullptr) {
906             ALOGE("GetOutputBuffer succeeds in stopped state");
907             return false;
908         }
909     }
910     return !hasSeenError();
911 }
912 
testGetOutputFormatInInitState()913 bool NativeCodecUnitTest::testGetOutputFormatInInitState() {
914     bool isAudio = true;
915     bool isEncoder = true;
916     if (!setupCodec(isAudio, isEncoder)) return false;
917     const char* mime = nullptr;
918     AMediaFormat_getString(mFormat, AMEDIAFORMAT_KEY_MIME, &mime);
919     const bool boolStates[]{true, false};
920     for (auto isAsync : boolStates) {
921         if (!configureCodec(mFormat, isAsync, false, isEncoder)) return false;
922         AMediaFormat* dupFormat = AMediaCodec_getOutputFormat(mCodec);
923         const char* dupMime = nullptr;
924         AMediaFormat_getString(dupFormat, AMEDIAFORMAT_KEY_MIME, &dupMime);
925         if (!dupMime || strcmp(dupMime, mime) != 0) {
926             AMediaFormat_delete(dupFormat);
927             ALOGE("getOutputFormat fails in initialized state");
928             return false;
929         }
930         AMediaFormat_delete(dupFormat);
931         CHECK_STATUS(AMediaCodec_stop(mCodec), "AMediaCodec_stop failed");
932     }
933     return !hasSeenError();
934 }
935 
testGetOutputFormatInRunningState()936 bool NativeCodecUnitTest::testGetOutputFormatInRunningState() {
937     bool isAudio = true;
938     bool isEncoder = true;
939     if (!setupCodec(isAudio, isEncoder)) return false;
940     const char* mime = nullptr;
941     AMediaFormat_getString(mFormat, AMEDIAFORMAT_KEY_MIME, &mime);
942     const bool boolStates[]{true, false};
943     for (auto isAsync : boolStates) {
944         if (!configureCodec(mFormat, isAsync, false, isEncoder)) return false;
945         CHECK_STATUS(AMediaCodec_start(mCodec), "AMediaCodec_start failed");
946         AMediaFormat* dupFormat = AMediaCodec_getOutputFormat(mCodec);
947         const char* dupMime = nullptr;
948         AMediaFormat_getString(dupFormat, AMEDIAFORMAT_KEY_MIME, &dupMime);
949         if (!dupMime || strcmp(dupMime, mime) != 0) {
950             AMediaFormat_delete(dupFormat);
951             ALOGE("getOutputFormat fails in running state");
952             return false;
953         }
954         AMediaFormat_delete(dupFormat);
955         CHECK_STATUS(AMediaCodec_stop(mCodec), "AMediaCodec_stop failed");
956     }
957     return !hasSeenError();
958 }
959 
testGetOutputFormatInUnInitState()960 bool NativeCodecUnitTest::testGetOutputFormatInUnInitState() {
961     bool isAudio = true;
962     bool isEncoder = true;
963     if (!setupCodec(isAudio, isEncoder)) return false;
964     const bool boolStates[]{true, false};
965     for (auto isAsync : boolStates) {
966         AMediaFormat* dupFormat = AMediaCodec_getOutputFormat(mCodec);
967         const char* dupMime = nullptr;
968         AMediaFormat_getString(dupFormat, AMEDIAFORMAT_KEY_MIME, &dupMime);
969         if (dupMime) {
970             AMediaFormat_delete(dupFormat);
971             ALOGE("getOutputFormat succeeds in uninitialized state");
972             return false;
973         }
974         AMediaFormat_delete(dupFormat);
975         if (!configureCodec(mFormat, isAsync, false, isEncoder)) return false;
976         CHECK_STATUS(AMediaCodec_start(mCodec), "AMediaCodec_start failed");
977         CHECK_STATUS(AMediaCodec_stop(mCodec), "AMediaCodec_stop failed");
978         dupFormat = AMediaCodec_getOutputFormat(mCodec);
979         dupMime = nullptr;
980         AMediaFormat_getString(dupFormat, AMEDIAFORMAT_KEY_MIME, &dupMime);
981         if (dupMime) {
982             AMediaFormat_delete(dupFormat);
983             ALOGE("getOutputFormat succeeds in stopped state");
984             return false;
985         }
986         AMediaFormat_delete(dupFormat);
987     }
988     return !hasSeenError();
989 }
990 
testSetParametersInInitState()991 bool NativeCodecUnitTest::testSetParametersInInitState() {
992     bool isAudio = false;
993     bool isEncoder = true;
994     if (!setupCodec(isAudio, isEncoder)) return false;
995     const bool boolStates[]{true, false};
996     for (auto isAsync : boolStates) {
997         if (!configureCodec(mFormat, isAsync, false, isEncoder)) return false;
998         int bitrate;
999         AMediaFormat_getInt32(mFormat, AMEDIAFORMAT_KEY_BIT_RATE, &bitrate);
1000         AMediaFormat* params = AMediaFormat_new();
1001         AMediaFormat_setInt32(params, TBD_AMEDIACODEC_PARAMETER_KEY_VIDEO_BITRATE, bitrate >> 1);
1002         if (AMEDIA_OK == AMediaCodec_setParameters(mCodec, params)) {
1003             ALOGE("SetParameters succeeds in initialized state");
1004             AMediaFormat_delete(params);
1005             return false;
1006         }
1007         AMediaFormat_delete(params);
1008         CHECK_STATUS(AMediaCodec_stop(mCodec), "AMediaCodec_stop failed");
1009     }
1010     return !hasSeenError();
1011 }
1012 
testSetParametersInRunningState()1013 bool NativeCodecUnitTest::testSetParametersInRunningState() {
1014     bool isAudio = false;
1015     bool isEncoder = true;
1016     if (!setupCodec(isAudio, isEncoder)) return false;
1017     const bool boolStates[]{true, false};
1018     int bitrate;
1019     AMediaFormat_getInt32(mFormat, AMEDIAFORMAT_KEY_BIT_RATE, &bitrate);
1020     for (auto isAsync : boolStates) {
1021         if (!configureCodec(mFormat, isAsync, false, isEncoder)) return false;
1022         CHECK_STATUS(AMediaCodec_start(mCodec), "AMediaCodec_start failed");
1023         // behaviour of setParams with null argument is acceptable according to SDK
1024         AMediaCodec_setParameters(mCodec, nullptr);
1025         AMediaFormat* params = AMediaFormat_new();
1026         AMediaFormat_setInt32(params, TBD_AMEDIACODEC_PARAMETER_KEY_VIDEO_BITRATE, bitrate >> 1);
1027         if (AMEDIA_OK != AMediaCodec_setParameters(mCodec, params)) {
1028             ALOGE("SetParameters fails in running state");
1029             AMediaFormat_delete(params);
1030             return false;
1031         }
1032         if (!queueEOS()) return false;
1033         AMediaCodec_setParameters(mCodec, nullptr);
1034         AMediaFormat_setInt32(mFormat, TBD_AMEDIACODEC_PARAMETER_KEY_VIDEO_BITRATE, bitrate << 1);
1035         if (AMEDIA_OK != AMediaCodec_setParameters(mCodec, mFormat)) {
1036             ALOGE("SetParameters fails in running state");
1037             AMediaFormat_delete(params);
1038             return false;
1039         }
1040         if (!waitForAllOutputs()) return false;
1041         AMediaCodec_setParameters(mCodec, nullptr);
1042         AMediaFormat_setInt32(mFormat, TBD_AMEDIACODEC_PARAMETER_KEY_VIDEO_BITRATE, bitrate);
1043         if (AMEDIA_OK != AMediaCodec_setParameters(mCodec, mFormat)) {
1044             ALOGE("SetParameters fails in running state");
1045             AMediaFormat_delete(params);
1046             return false;
1047         }
1048         AMediaFormat_delete(params);
1049         CHECK_STATUS(AMediaCodec_stop(mCodec), "AMediaCodec_stop failed");
1050     }
1051     return !hasSeenError();
1052 }
1053 
testSetParametersInUnInitState()1054 bool NativeCodecUnitTest::testSetParametersInUnInitState() {
1055     bool isAudio = false;
1056     bool isEncoder = true;
1057     if (!setupCodec(isAudio, isEncoder)) return false;
1058     const bool boolStates[]{true, false};
1059     for (auto isAsync : boolStates) {
1060         if (!configureCodec(mFormat, isAsync, false, isEncoder)) return false;
1061         CHECK_STATUS(AMediaCodec_start(mCodec), "AMediaCodec_start failed");
1062         CHECK_STATUS(AMediaCodec_stop(mCodec), "AMediaCodec_stop failed");
1063         int bitrate;
1064         AMediaFormat_getInt32(mFormat, AMEDIAFORMAT_KEY_BIT_RATE, &bitrate);
1065         AMediaFormat* params = AMediaFormat_new();
1066         AMediaFormat_setInt32(params, TBD_AMEDIACODEC_PARAMETER_KEY_VIDEO_BITRATE, bitrate >> 1);
1067         if (AMEDIA_OK == AMediaCodec_setParameters(mCodec, params)) {
1068             ALOGE("SetParameters succeeds in stopped state");
1069             AMediaFormat_delete(params);
1070             return false;
1071         }
1072         AMediaFormat_delete(params);
1073     }
1074     return !hasSeenError();
1075 }
1076 
testStartInRunningState()1077 bool NativeCodecUnitTest::testStartInRunningState() {
1078     bool isAudio = true;
1079     bool isEncoder = true;
1080     if (!setupCodec(isAudio, isEncoder)) return false;
1081     if (!configureCodec(mFormat, false, false, isEncoder)) return false;
1082     CHECK_STATUS(AMediaCodec_start(mCodec), "AMediaCodec_start failed");
1083     if (AMEDIA_OK == AMediaCodec_start(mCodec)) {
1084         ALOGE("Start succeeds in running state");
1085         return false;
1086     }
1087     CHECK_STATUS(AMediaCodec_stop(mCodec), "AMediaCodec_stop failed");
1088     return !hasSeenError();
1089 }
1090 
testStartInUnInitState()1091 bool NativeCodecUnitTest::testStartInUnInitState() {
1092     bool isAudio = true;
1093     bool isEncoder = true;
1094     if (!setupCodec(isAudio, isEncoder)) return false;
1095     if (AMEDIA_OK == AMediaCodec_start(mCodec)) {
1096         ALOGE("codec start succeeds before initialization");
1097         return false;
1098     }
1099     if (!configureCodec(mFormat, false, false, isEncoder)) return false;
1100     CHECK_STATUS(AMediaCodec_start(mCodec), "AMediaCodec_start failed");
1101     CHECK_STATUS(AMediaCodec_stop(mCodec), "AMediaCodec_stop failed");
1102     if (AMEDIA_OK == AMediaCodec_start(mCodec)) {
1103         ALOGE("codec start succeeds in stopped state");
1104         return false;
1105     }
1106     return !hasSeenError();
1107 }
1108 
testStopInInitState()1109 bool NativeCodecUnitTest::testStopInInitState() {
1110     bool isAudio = true;
1111     bool isEncoder = true;
1112     if (!setupCodec(isAudio, isEncoder)) return false;
1113     const bool boolStates[]{true, false};
1114     for (auto isAsync : boolStates) {
1115         if (!configureCodec(mFormat, isAsync, false, isEncoder)) return false;
1116         CHECK_STATUS(AMediaCodec_stop(mCodec), "Stop fails in initialized state");
1117     }
1118     return !hasSeenError();
1119 }
1120 
testStopInRunningState()1121 bool NativeCodecUnitTest::testStopInRunningState() {
1122     bool isAudio = true;
1123     bool isEncoder = true;
1124     if (!setupCodec(isAudio, isEncoder)) return false;
1125     const bool boolStates[]{true, false};
1126     for (auto isAsync : boolStates) {
1127         if (!configureCodec(mFormat, isAsync, false, isEncoder)) return false;
1128         CHECK_STATUS(AMediaCodec_start(mCodec), "AMediaCodec_start failed");
1129         if (!queueEOS()) return false;
1130         CHECK_STATUS(AMediaCodec_stop(mCodec), "Stop fails in running state");
1131     }
1132     return !hasSeenError();
1133 }
1134 
testStopInUnInitState()1135 bool NativeCodecUnitTest::testStopInUnInitState() {
1136     bool isAudio = true;
1137     bool isEncoder = true;
1138     if (!setupCodec(isAudio, isEncoder)) return false;
1139     const bool boolStates[]{true, false};
1140     for (auto isAsync : boolStates) {
1141         if (!configureCodec(mFormat, isAsync, false, isEncoder)) return false;
1142         CHECK_STATUS(AMediaCodec_start(mCodec), "AMediaCodec_start failed");
1143         CHECK_STATUS(AMediaCodec_stop(mCodec), "AMediaCodec_stop failed");
1144         CHECK_STATUS(AMediaCodec_stop(mCodec), "Stop fails in stopped state");
1145     }
1146     return !hasSeenError();
1147 }
1148 
testQueueInputBufferInInitState()1149 bool NativeCodecUnitTest::testQueueInputBufferInInitState() {
1150     bool isAudio = true;
1151     bool isEncoder = true;
1152     if (!setupCodec(isAudio, isEncoder)) return false;
1153     const bool boolStates[]{true, false};
1154     for (auto isAsync : boolStates) {
1155         if (!configureCodec(mFormat, isAsync, false, isEncoder)) return false;
1156         if (AMEDIA_OK == AMediaCodec_queueInputBuffer(mCodec, 0, 0, 0, 0,
1157                                                       AMEDIACODEC_BUFFER_FLAG_END_OF_STREAM)) {
1158             ALOGE("queueInputBuffer succeeds in initialized state");
1159             return false;
1160         }
1161         CHECK_STATUS(AMediaCodec_stop(mCodec), "AMediaCodec_stop failed");
1162     }
1163     return !hasSeenError();
1164 }
1165 
testQueueInputBufferWithBadIndex()1166 bool NativeCodecUnitTest::testQueueInputBufferWithBadIndex() {
1167     bool isAudio = true;
1168     bool isEncoder = true;
1169     if (!setupCodec(isAudio, isEncoder)) return false;
1170     const bool boolStates[]{true, false};
1171     for (auto isAsync : boolStates) {
1172         if (!configureCodec(mFormat, isAsync, false, isEncoder)) return false;
1173         CHECK_STATUS(AMediaCodec_start(mCodec), "AMediaCodec_start failed");
1174         if (AMEDIA_OK == AMediaCodec_queueInputBuffer(mCodec, -1, 0, 0, 0,
1175                                                       AMEDIACODEC_BUFFER_FLAG_END_OF_STREAM)) {
1176             ALOGE("queueInputBuffer succeeds with bad buffer index :: -1");
1177             return false;
1178         }
1179         CHECK_STATUS(AMediaCodec_stop(mCodec), "AMediaCodec_stop failed");
1180     }
1181     return !hasSeenError();
1182 }
1183 
testQueueInputBufferWithBadSize()1184 bool NativeCodecUnitTest::testQueueInputBufferWithBadSize() {
1185     bool isAudio = true;
1186     bool isEncoder = true;
1187     if (!setupCodec(isAudio, isEncoder)) return false;
1188     const bool boolStates[]{true, false};
1189     for (auto isAsync : boolStates) {
1190         if (!configureCodec(mFormat, isAsync, false, isEncoder)) return false;
1191         CHECK_STATUS(AMediaCodec_start(mCodec), "AMediaCodec_start failed");
1192         int bufferIndex = mIsCodecInAsyncMode ? mAsyncHandle.getInput().bufferIndex
1193                                               : AMediaCodec_dequeueInputBuffer(mCodec, -1);
1194         size_t bufSize;
1195         uint8_t* buf = AMediaCodec_getInputBuffer(mCodec, bufferIndex, &bufSize);
1196         if (buf == nullptr) {
1197             ALOGE("getInputBuffer fails for valid index");
1198             return false;
1199         } else {
1200             if (AMEDIA_OK == AMediaCodec_queueInputBuffer(mCodec, 0, 0, bufSize + 100, 0,
1201                                                           AMEDIACODEC_BUFFER_FLAG_END_OF_STREAM)) {
1202                 ALOGE("queueInputBuffer succeeds for bad size %zu, buffer capacity %zu ",
1203                       bufSize + 100, bufSize);
1204                 return false;
1205             }
1206         }
1207         CHECK_STATUS(AMediaCodec_stop(mCodec), "AMediaCodec_stop failed");
1208     }
1209     return !hasSeenError();
1210 }
1211 
testQueueInputBufferWithBadBuffInfo()1212 bool NativeCodecUnitTest::testQueueInputBufferWithBadBuffInfo() {
1213     bool isAudio = true;
1214     bool isEncoder = true;
1215     if (!setupCodec(isAudio, isEncoder)) return false;
1216     const bool boolStates[]{true, false};
1217     for (auto isAsync : boolStates) {
1218         if (!configureCodec(mFormat, isAsync, false, isEncoder)) return false;
1219         CHECK_STATUS(AMediaCodec_start(mCodec), "AMediaCodec_start failed");
1220         int bufferIndex = mIsCodecInAsyncMode ? mAsyncHandle.getInput().bufferIndex
1221                                               : AMediaCodec_dequeueInputBuffer(mCodec, -1);
1222         size_t bufSize;
1223         uint8_t* buf = AMediaCodec_getInputBuffer(mCodec, bufferIndex, &bufSize);
1224         if (buf == nullptr) {
1225             ALOGE("getInputBuffer fails for valid index");
1226             return false;
1227         } else {
1228             if (AMEDIA_OK == AMediaCodec_queueInputBuffer(mCodec, 0, 16, bufSize, 0,
1229                                                           AMEDIACODEC_BUFFER_FLAG_END_OF_STREAM)) {
1230                 ALOGE("queueInputBuffer succeeds with bad offset and size param");
1231                 return false;
1232             }
1233         }
1234         CHECK_STATUS(AMediaCodec_stop(mCodec), "AMediaCodec_stop failed");
1235     }
1236     return !hasSeenError();
1237 }
1238 
testQueueInputBufferWithBadOffset()1239 bool NativeCodecUnitTest::testQueueInputBufferWithBadOffset() {
1240     bool isAudio = true;
1241     bool isEncoder = true;
1242     if (!setupCodec(isAudio, isEncoder)) return false;
1243     const bool boolStates[]{true, false};
1244     for (auto isAsync : boolStates) {
1245         if (!configureCodec(mFormat, isAsync, false, isEncoder)) return false;
1246         CHECK_STATUS(AMediaCodec_start(mCodec), "AMediaCodec_start failed");
1247         if (AMEDIA_OK == AMediaCodec_queueInputBuffer(mCodec, 0, -1, 0, 0,
1248                                                       AMEDIACODEC_BUFFER_FLAG_END_OF_STREAM)) {
1249             ALOGE("queueInputBuffer succeeds with bad buffer offset :: -1");
1250             return false;
1251         }
1252         CHECK_STATUS(AMediaCodec_stop(mCodec), "AMediaCodec_stop failed");
1253     }
1254     return !hasSeenError();
1255 }
1256 
testQueueInputBufferInUnInitState()1257 bool NativeCodecUnitTest::testQueueInputBufferInUnInitState() {
1258     bool isAudio = true;
1259     bool isEncoder = true;
1260     if (!setupCodec(isAudio, isEncoder)) return false;
1261     const bool boolStates[]{true, false};
1262     for (auto isAsync : boolStates) {
1263         if (AMEDIA_OK == AMediaCodec_queueInputBuffer(mCodec, 0, 0, 0, 0,
1264                                                       AMEDIACODEC_BUFFER_FLAG_END_OF_STREAM)) {
1265             ALOGE("queueInputBuffer succeeds in uninitialized state");
1266             return false;
1267         }
1268         if (!configureCodec(mFormat, isAsync, false, isEncoder)) return false;
1269         CHECK_STATUS(AMediaCodec_start(mCodec), "AMediaCodec_start failed");
1270         CHECK_STATUS(AMediaCodec_stop(mCodec), "AMediaCodec_stop failed");
1271         if (AMEDIA_OK == AMediaCodec_queueInputBuffer(mCodec, 0, 0, 0, 0,
1272                                                       AMEDIACODEC_BUFFER_FLAG_END_OF_STREAM)) {
1273             ALOGE("queueInputBuffer succeeds in stopped state");
1274             return false;
1275         }
1276     }
1277     return !hasSeenError();
1278 }
1279 
testReleaseOutputBufferInInitState()1280 bool NativeCodecUnitTest::testReleaseOutputBufferInInitState() {
1281     bool isAudio = true;
1282     bool isEncoder = true;
1283     if (!setupCodec(isAudio, isEncoder)) return false;
1284     const bool boolStates[]{true, false};
1285     for (auto isAsync : boolStates) {
1286         if (!configureCodec(mFormat, isAsync, false, isEncoder)) return false;
1287         if (AMEDIA_OK == AMediaCodec_releaseOutputBuffer(mCodec, 0, false)) {
1288             ALOGE("ReleaseOutputBuffer succeeds in initialized state");
1289             return false;
1290         }
1291         CHECK_STATUS(AMediaCodec_stop(mCodec), "AMediaCodec_stop failed");
1292     }
1293     return !hasSeenError();
1294 }
1295 
testReleaseOutputBufferInRunningState()1296 bool NativeCodecUnitTest::testReleaseOutputBufferInRunningState() {
1297     bool isAudio = true;
1298     bool isEncoder = true;
1299     if (!setupCodec(isAudio, isEncoder)) return false;
1300     AMediaCodecBufferInfo outInfo;
1301     const bool boolStates[]{true, false};
1302     for (auto isAsync : boolStates) {
1303         if (!configureCodec(mFormat, isAsync, false, isEncoder)) return false;
1304         CHECK_STATUS(AMediaCodec_start(mCodec), "AMediaCodec_start failed");
1305         if (AMEDIA_OK == AMediaCodec_releaseOutputBuffer(mCodec, -1, false)) {
1306             ALOGE("ReleaseOutputBuffer succeeds for bad buffer index -1");
1307             return false;
1308         }
1309         if (!queueEOS()) return false;
1310         if (!hasSeenError()) {
1311             int bufferIndex = 0;
1312             size_t buffSize;
1313             bool isOk = true;
1314             while (!mSawOutputEOS && isOk) {
1315                 if (mIsCodecInAsyncMode) {
1316                     callbackObject element = mAsyncHandle.getOutput();
1317                     bufferIndex = element.bufferIndex;
1318                     if (element.bufferIndex >= 0) {
1319                         if (!AMediaCodec_getOutputBuffer(mCodec, bufferIndex, &buffSize)) {
1320                             ALOGE("GetOutputBuffer fails for valid buffer index");
1321                             return false;
1322                         }
1323                         isOk = dequeueOutput(element.bufferIndex, &element.bufferInfo);
1324                     }
1325                 } else {
1326                     bufferIndex = AMediaCodec_dequeueOutputBuffer(mCodec, &outInfo, kQDeQTimeOutUs);
1327                     if (bufferIndex >= 0) {
1328                         if (!AMediaCodec_getOutputBuffer(mCodec, bufferIndex, &buffSize)) {
1329                             ALOGE("GetOutputBuffer fails for valid bufffer index");
1330                             return false;
1331                         }
1332                         isOk = dequeueOutput(bufferIndex, &outInfo);
1333                     }
1334                 }
1335                 if (hasSeenError() || !isOk) {
1336                     ALOGE("Got unexpected error");
1337                     return false;
1338                 }
1339             }
1340             if (AMEDIA_OK == AMediaCodec_releaseOutputBuffer(mCodec, bufferIndex, false)) {
1341                 ALOGE("ReleaseOutputBuffer succeeds for buffer index not owned by client");
1342                 return false;
1343             }
1344         } else {
1345             ALOGE("Got unexpected error");
1346             return false;
1347         }
1348         CHECK_STATUS(AMediaCodec_stop(mCodec), "AMediaCodec_stop failed");
1349     }
1350     return !hasSeenError();
1351 }
1352 
testReleaseOutputBufferInUnInitState()1353 bool NativeCodecUnitTest::testReleaseOutputBufferInUnInitState() {
1354     bool isAudio = true;
1355     bool isEncoder = true;
1356     if (!setupCodec(isAudio, isEncoder)) return false;
1357     const bool boolStates[]{true, false};
1358     for (auto isAsync : boolStates) {
1359         if (AMEDIA_OK == AMediaCodec_releaseOutputBuffer(mCodec, 0, false)) {
1360             ALOGE("ReleaseOutputBuffer succeeds in uninitialized state");
1361             return false;
1362         }
1363         if (!configureCodec(mFormat, isAsync, false, isEncoder)) return false;
1364         CHECK_STATUS(AMediaCodec_start(mCodec), "AMediaCodec_start failed");
1365         CHECK_STATUS(AMediaCodec_stop(mCodec), "AMediaCodec_stop failed");
1366         if (AMEDIA_OK == AMediaCodec_releaseOutputBuffer(mCodec, 0, false)) {
1367             ALOGE("ReleaseOutputBuffer succeeds in stopped state");
1368             return false;
1369         }
1370     }
1371     return !hasSeenError();
1372 }
1373 
testGetBufferFormatInInitState()1374 bool NativeCodecUnitTest::testGetBufferFormatInInitState() {
1375     bool isAudio = true;
1376     bool isEncoder = true;
1377     if (!setupCodec(isAudio, isEncoder)) return false;
1378     const bool boolStates[]{true, false};
1379     for (auto isAsync : boolStates) {
1380         if (!configureCodec(mFormat, isAsync, false, isEncoder)) return false;
1381         AMediaFormat* dupFormat = AMediaCodec_getBufferFormat(mCodec, 0);
1382         const char* dupMime = nullptr;
1383         AMediaFormat_getString(dupFormat, AMEDIAFORMAT_KEY_MIME, &dupMime);
1384         if (dupMime) {
1385             AMediaFormat_delete(dupFormat);
1386             ALOGE("GetBufferFormat succeeds in initialized state");
1387             return false;
1388         }
1389         AMediaFormat_delete(dupFormat);
1390         CHECK_STATUS(AMediaCodec_stop(mCodec), "AMediaCodec_stop failed");
1391     }
1392     return !hasSeenError();
1393 }
1394 
testGetBufferFormatInRunningState()1395 bool NativeCodecUnitTest::testGetBufferFormatInRunningState() {
1396     bool isAudio = true;
1397     bool isEncoder = true;
1398     if (!setupCodec(isAudio, isEncoder)) return false;
1399     const char* mime = nullptr;
1400     AMediaFormat_getString(mFormat, AMEDIAFORMAT_KEY_MIME, &mime);
1401     AMediaCodecBufferInfo outInfo;
1402     const bool boolStates[]{true, false};
1403     for (auto isAsync : boolStates) {
1404         if (!configureCodec(mFormat, isAsync, false, isEncoder)) return false;
1405         CHECK_STATUS(AMediaCodec_start(mCodec), "AMediaCodec_start failed");
1406         AMediaFormat* dupFormat = AMediaCodec_getBufferFormat(mCodec, -1);
1407         const char* dupMime = nullptr;
1408         AMediaFormat_getString(dupFormat, AMEDIAFORMAT_KEY_MIME, &dupMime);
1409         if (dupMime) {
1410             AMediaFormat_delete(dupFormat);
1411             ALOGE("GetBufferFormat succeeds for bad buffer index -1");
1412             return false;
1413         }
1414         AMediaFormat_delete(dupFormat);
1415         if (!queueEOS()) return false;
1416         if (!hasSeenError()) {
1417             int bufferIndex = 0;
1418             bool isOk = true;
1419             while (!mSawOutputEOS && isOk) {
1420                 if (mIsCodecInAsyncMode) {
1421                     callbackObject element = mAsyncHandle.getOutput();
1422                     bufferIndex = element.bufferIndex;
1423                     if (element.bufferIndex >= 0) {
1424                         dupFormat = AMediaCodec_getBufferFormat(mCodec, bufferIndex);
1425                         dupMime = nullptr;
1426                         AMediaFormat_getString(dupFormat, AMEDIAFORMAT_KEY_MIME, &dupMime);
1427                         if (!dupMime || strcmp(dupMime, mime) != 0) {
1428                             AMediaFormat_delete(dupFormat);
1429                             ALOGE("GetBufferFormat fails in running state");
1430                             return false;
1431                         }
1432                         AMediaFormat_delete(dupFormat);
1433                         isOk = dequeueOutput(element.bufferIndex, &element.bufferInfo);
1434                     }
1435                 } else {
1436                     bufferIndex = AMediaCodec_dequeueOutputBuffer(mCodec, &outInfo, kQDeQTimeOutUs);
1437                     if (bufferIndex >= 0) {
1438                         dupFormat = AMediaCodec_getBufferFormat(mCodec, bufferIndex);
1439                         dupMime = nullptr;
1440                         AMediaFormat_getString(dupFormat, AMEDIAFORMAT_KEY_MIME, &dupMime);
1441                         if (!dupMime || strcmp(dupMime, mime) != 0) {
1442                             AMediaFormat_delete(dupFormat);
1443                             ALOGE("GetBufferFormat fails in running state");
1444                             return false;
1445                         }
1446                         AMediaFormat_delete(dupFormat);
1447                         isOk = dequeueOutput(bufferIndex, &outInfo);
1448                     }
1449                 }
1450                 if (hasSeenError() || !isOk) {
1451                     ALOGE("Got unexpected error");
1452                     return false;
1453                 }
1454             }
1455             dupFormat = AMediaCodec_getBufferFormat(mCodec, bufferIndex);
1456             dupMime = nullptr;
1457             AMediaFormat_getString(dupFormat, AMEDIAFORMAT_KEY_MIME, &dupMime);
1458             if (dupMime) {
1459                 AMediaFormat_delete(dupFormat);
1460                 ALOGE("GetBufferFormat succeeds for buffer index not owned by client");
1461                 return false;
1462             }
1463             AMediaFormat_delete(dupFormat);
1464         } else {
1465             ALOGE("Got unexpected error");
1466             return false;
1467         }
1468         CHECK_STATUS(AMediaCodec_stop(mCodec), "AMediaCodec_stop failed");
1469     }
1470     return !hasSeenError();
1471 }
1472 
testGetBufferFormatInUnInitState()1473 bool NativeCodecUnitTest::testGetBufferFormatInUnInitState() {
1474     bool isAudio = true;
1475     bool isEncoder = true;
1476     if (!setupCodec(isAudio, isEncoder)) return false;
1477     const bool boolStates[]{true, false};
1478     for (auto isAsync : boolStates) {
1479         AMediaFormat* dupFormat = AMediaCodec_getBufferFormat(mCodec, 0);
1480         const char* dupMime = nullptr;
1481         AMediaFormat_getString(dupFormat, AMEDIAFORMAT_KEY_MIME, &dupMime);
1482         if (dupMime) {
1483             AMediaFormat_delete(dupFormat);
1484             ALOGE("GetBufferFormat succeeds in uninitialized state");
1485             return false;
1486         }
1487         AMediaFormat_delete(dupFormat);
1488         if (!configureCodec(mFormat, isAsync, false, isEncoder)) return false;
1489         CHECK_STATUS(AMediaCodec_start(mCodec), "AMediaCodec_start failed");
1490         CHECK_STATUS(AMediaCodec_stop(mCodec), "AMediaCodec_stop failed");
1491         dupFormat = AMediaCodec_getBufferFormat(mCodec, 0);
1492         dupMime = nullptr;
1493         AMediaFormat_getString(dupFormat, AMEDIAFORMAT_KEY_MIME, &dupMime);
1494         if (dupMime) {
1495             AMediaFormat_delete(dupFormat);
1496             ALOGE("GetBufferFormat succeeds in stopped state");
1497             return false;
1498         }
1499         AMediaFormat_delete(dupFormat);
1500     }
1501     return !hasSeenError();
1502 }
1503 
nativeTestCreateByCodecNameForNull(JNIEnv *,jobject)1504 static jboolean nativeTestCreateByCodecNameForNull(JNIEnv*, jobject) {
1505     bool isPass = true;
1506     AMediaCodec* codec = AMediaCodec_createCodecByName(nullptr);
1507     if (codec) {
1508         AMediaCodec_delete(codec);
1509         ALOGE("AMediaCodec_createCodecByName succeeds with null argument");
1510         isPass = false;
1511     }
1512     return static_cast<jboolean>(isPass);
1513 }
1514 
nativeTestCreateByCodecNameForInvalidName(JNIEnv *,jobject)1515 static jboolean nativeTestCreateByCodecNameForInvalidName(JNIEnv*, jobject) {
1516     bool isPass = true;
1517     AMediaCodec* codec = AMediaCodec_createCodecByName("invalid name");
1518     if (codec) {
1519         AMediaCodec_delete(codec);
1520         ALOGE("AMediaCodec_createCodecByName succeeds with invalid name");
1521         isPass = false;
1522     }
1523     return static_cast<jboolean>(isPass);
1524 }
1525 
nativeTestCreateDecoderByTypeForNull(JNIEnv *,jobject)1526 static jboolean nativeTestCreateDecoderByTypeForNull(JNIEnv*, jobject) {
1527     bool isPass = true;
1528     AMediaCodec* codec = AMediaCodec_createDecoderByType(nullptr);
1529     if (codec) {
1530         AMediaCodec_delete(codec);
1531         ALOGE("AMediaCodec_createDecoderByType succeeds with null argument");
1532         isPass = false;
1533     }
1534     return static_cast<jboolean>(isPass);
1535 }
1536 
nativeTestCreateDecoderByTypeForInvalidMime(JNIEnv *,jobject)1537 static jboolean nativeTestCreateDecoderByTypeForInvalidMime(JNIEnv*, jobject) {
1538     bool isPass = true;
1539     AMediaCodec* codec = AMediaCodec_createDecoderByType("invalid name");
1540     if (codec) {
1541         AMediaCodec_delete(codec);
1542         ALOGE("AMediaCodec_createDecoderByType succeeds with invalid name");
1543         isPass = false;
1544     }
1545     return static_cast<jboolean>(isPass);
1546 }
1547 
nativeTestCreateEncoderByTypeForNull(JNIEnv *,jobject)1548 static jboolean nativeTestCreateEncoderByTypeForNull(JNIEnv*, jobject) {
1549     bool isPass = true;
1550     AMediaCodec* codec = AMediaCodec_createEncoderByType(nullptr);
1551     if (codec) {
1552         AMediaCodec_delete(codec);
1553         ALOGE("AMediaCodec_createEncoderByType succeeds with null argument");
1554         isPass = false;
1555     }
1556     return static_cast<jboolean>(isPass);
1557 }
1558 
nativeTestCreateEncoderByTypeForInvalidMime(JNIEnv *,jobject)1559 static jboolean nativeTestCreateEncoderByTypeForInvalidMime(JNIEnv*, jobject) {
1560     bool isPass = true;
1561     AMediaCodec* codec = AMediaCodec_createEncoderByType("invalid name");
1562     if (codec) {
1563         AMediaCodec_delete(codec);
1564         ALOGE("AMediaCodec_createEncoderByType succeeds with invalid name");
1565         isPass = false;
1566     }
1567     return static_cast<jboolean>(isPass);
1568 }
1569 
nativeTestConfigureForNullFormat(JNIEnv *,jobject)1570 static jboolean nativeTestConfigureForNullFormat(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     bool isPass = (AMEDIA_OK != AMediaCodec_configure(codec, nullptr, nullptr, nullptr,
1577                                                       AMEDIACODEC_CONFIGURE_FLAG_ENCODE));
1578     if (!isPass) {
1579         ALOGE("codec configure succeeds with null format");
1580     }
1581     AMediaCodec_delete(codec);
1582     return static_cast<jboolean>(isPass);
1583 }
1584 
nativeTestConfigureForEmptyFormat(JNIEnv *,jobject)1585 static jboolean nativeTestConfigureForEmptyFormat(JNIEnv*, jobject) {
1586     AMediaCodec* codec = AMediaCodec_createEncoderByType(AMEDIA_MIMETYPE_AUDIO_AAC);
1587     if (!codec) {
1588         ALOGE("unable to create codec %s", AMEDIA_MIMETYPE_AUDIO_AAC);
1589         return static_cast<jboolean>(false);
1590     }
1591     AMediaFormat* format = AMediaFormat_new();
1592     bool isPass = (AMEDIA_OK != AMediaCodec_configure(codec, format, nullptr, nullptr,
1593                                                       AMEDIACODEC_CONFIGURE_FLAG_ENCODE));
1594     if (!isPass) {
1595         ALOGE("codec configure succeeds with empty format");
1596     }
1597     AMediaFormat_delete(format);
1598     AMediaCodec_delete(codec);
1599     return static_cast<jboolean>(isPass);
1600 }
1601 
nativeTestConfigureCodecForIncompleteFormat(JNIEnv *,jobject,bool isAudio,bool isEncoder)1602 static jboolean nativeTestConfigureCodecForIncompleteFormat(JNIEnv*, jobject, bool isAudio,
1603                                                             bool isEncoder) {
1604     auto* nativeCodecUnitTest = new NativeCodecUnitTest(AMEDIA_MIMETYPE_AUDIO_AAC);
1605     bool isPass = nativeCodecUnitTest->testConfigureCodecForIncompleteFormat(isAudio, isEncoder);
1606     delete nativeCodecUnitTest;
1607     return static_cast<jboolean>(isPass);
1608 }
1609 
nativeTestConfigureEncoderForBadFlags(JNIEnv *,jobject)1610 static jboolean nativeTestConfigureEncoderForBadFlags(JNIEnv*, jobject) {
1611     auto* nativeCodecUnitTest = new NativeCodecUnitTest(AMEDIA_MIMETYPE_AUDIO_AAC);
1612     bool isEncoder = true;
1613     bool isPass = nativeCodecUnitTest->testConfigureCodecForBadFlags(isEncoder);
1614     delete nativeCodecUnitTest;
1615     return static_cast<jboolean>(isPass);
1616 }
1617 
nativeTestConfigureDecoderForBadFlags(JNIEnv *,jobject)1618 static jboolean nativeTestConfigureDecoderForBadFlags(JNIEnv*, jobject) {
1619     auto* nativeCodecUnitTest = new NativeCodecUnitTest(AMEDIA_MIMETYPE_AUDIO_AAC);
1620     bool isEncoder = false;
1621     bool isPass = nativeCodecUnitTest->testConfigureCodecForBadFlags(isEncoder);
1622     delete nativeCodecUnitTest;
1623     return static_cast<jboolean>(isPass);
1624 }
1625 
nativeTestConfigureInInitState(JNIEnv *,jobject)1626 static jboolean nativeTestConfigureInInitState(JNIEnv*, jobject) {
1627     auto* nativeCodecUnitTest = new NativeCodecUnitTest(AMEDIA_MIMETYPE_AUDIO_AAC);
1628     bool isPass = nativeCodecUnitTest->testConfigureInInitState();
1629     delete nativeCodecUnitTest;
1630     return static_cast<jboolean>(isPass);
1631 }
1632 
nativeTestConfigureInRunningState(JNIEnv *,jobject)1633 static jboolean nativeTestConfigureInRunningState(JNIEnv*, jobject) {
1634     auto* nativeCodecUnitTest = new NativeCodecUnitTest(AMEDIA_MIMETYPE_AUDIO_AAC);
1635     bool isPass = nativeCodecUnitTest->testConfigureInRunningState();
1636     delete nativeCodecUnitTest;
1637     return static_cast<jboolean>(isPass);
1638 }
1639 
nativeTestConfigureInUnInitState(JNIEnv *,jobject)1640 static jboolean nativeTestConfigureInUnInitState(JNIEnv*, jobject) {
1641     auto* nativeCodecUnitTest = new NativeCodecUnitTest(AMEDIA_MIMETYPE_AUDIO_AAC);
1642     bool isPass = nativeCodecUnitTest->testConfigureInUnInitState();
1643     delete nativeCodecUnitTest;
1644     return static_cast<jboolean>(isPass);
1645 }
1646 
nativeTestDequeueInputBufferInInitState(JNIEnv *,jobject)1647 static jboolean nativeTestDequeueInputBufferInInitState(JNIEnv*, jobject) {
1648     auto* nativeCodecUnitTest = new NativeCodecUnitTest(AMEDIA_MIMETYPE_AUDIO_AAC);
1649     bool isPass = nativeCodecUnitTest->testDequeueInputBufferInInitState();
1650     delete nativeCodecUnitTest;
1651     return static_cast<jboolean>(isPass);
1652 }
1653 
nativeTestDequeueInputBufferInRunningState(JNIEnv *,jobject)1654 static jboolean nativeTestDequeueInputBufferInRunningState(JNIEnv*, jobject) {
1655     auto* nativeCodecUnitTest = new NativeCodecUnitTest(AMEDIA_MIMETYPE_AUDIO_AAC);
1656     bool isPass = nativeCodecUnitTest->testDequeueInputBufferInRunningState();
1657     delete nativeCodecUnitTest;
1658     return static_cast<jboolean>(isPass);
1659 }
1660 
nativeTestDequeueInputBufferInUnInitState(JNIEnv *,jobject)1661 static jboolean nativeTestDequeueInputBufferInUnInitState(JNIEnv*, jobject) {
1662     auto* nativeCodecUnitTest = new NativeCodecUnitTest(AMEDIA_MIMETYPE_AUDIO_AAC);
1663     bool isPass = nativeCodecUnitTest->testDequeueInputBufferInUnInitState();
1664     delete nativeCodecUnitTest;
1665     return static_cast<jboolean>(isPass);
1666 }
1667 
nativeTestDequeueOutputBufferInInitState(JNIEnv *,jobject)1668 static jboolean nativeTestDequeueOutputBufferInInitState(JNIEnv*, jobject) {
1669     auto* nativeCodecUnitTest = new NativeCodecUnitTest(AMEDIA_MIMETYPE_AUDIO_AAC);
1670     bool isPass = nativeCodecUnitTest->testDequeueOutputBufferInInitState();
1671     delete nativeCodecUnitTest;
1672     return static_cast<jboolean>(isPass);
1673 }
1674 
nativeTestDequeueOutputBufferInRunningState(JNIEnv *,jobject)1675 static jboolean nativeTestDequeueOutputBufferInRunningState(JNIEnv*, jobject) {
1676     auto* nativeCodecUnitTest = new NativeCodecUnitTest(AMEDIA_MIMETYPE_AUDIO_AAC);
1677     bool isPass = nativeCodecUnitTest->testDequeueOutputBufferInRunningState();
1678     delete nativeCodecUnitTest;
1679     return static_cast<jboolean>(isPass);
1680 }
1681 
nativeTestDequeueOutputBufferInUnInitState(JNIEnv *,jobject)1682 static jboolean nativeTestDequeueOutputBufferInUnInitState(JNIEnv*, jobject) {
1683     auto* nativeCodecUnitTest = new NativeCodecUnitTest(AMEDIA_MIMETYPE_AUDIO_AAC);
1684     bool isPass = nativeCodecUnitTest->testDequeueOutputBufferInUnInitState();
1685     delete nativeCodecUnitTest;
1686     return static_cast<jboolean>(isPass);
1687 }
1688 
nativeTestFlushInInitState(JNIEnv *,jobject)1689 static jboolean nativeTestFlushInInitState(JNIEnv*, jobject) {
1690     auto* nativeCodecUnitTest = new NativeCodecUnitTest(AMEDIA_MIMETYPE_AUDIO_AAC);
1691     bool isPass = nativeCodecUnitTest->testFlushInInitState();
1692     delete nativeCodecUnitTest;
1693     return static_cast<jboolean>(isPass);
1694 }
1695 
nativeTestFlushInRunningState(JNIEnv *,jobject)1696 static jboolean nativeTestFlushInRunningState(JNIEnv*, jobject) {
1697     auto* nativeCodecUnitTest = new NativeCodecUnitTest(AMEDIA_MIMETYPE_AUDIO_AAC);
1698     bool isPass = nativeCodecUnitTest->testFlushInRunningState();
1699     delete nativeCodecUnitTest;
1700     return static_cast<jboolean>(isPass);
1701 }
1702 
nativeTestFlushInUnInitState(JNIEnv *,jobject)1703 static jboolean nativeTestFlushInUnInitState(JNIEnv*, jobject) {
1704     auto* nativeCodecUnitTest = new NativeCodecUnitTest(AMEDIA_MIMETYPE_AUDIO_AAC);
1705     bool isPass = nativeCodecUnitTest->testFlushInUnInitState();
1706     delete nativeCodecUnitTest;
1707     return static_cast<jboolean>(isPass);
1708 }
1709 
nativeTestGetNameInInitState(JNIEnv *,jobject)1710 static jboolean nativeTestGetNameInInitState(JNIEnv*, jobject) {
1711     auto* nativeCodecUnitTest = new NativeCodecUnitTest(AMEDIA_MIMETYPE_AUDIO_AAC);
1712     bool isPass = nativeCodecUnitTest->testGetNameInInitState();
1713     delete nativeCodecUnitTest;
1714     return static_cast<jboolean>(isPass);
1715 }
1716 
nativeTestGetNameInRunningState(JNIEnv *,jobject)1717 static jboolean nativeTestGetNameInRunningState(JNIEnv*, jobject) {
1718     auto* nativeCodecUnitTest = new NativeCodecUnitTest(AMEDIA_MIMETYPE_AUDIO_AAC);
1719     bool isPass = nativeCodecUnitTest->testGetNameInRunningState();
1720     delete nativeCodecUnitTest;
1721     return static_cast<jboolean>(isPass);
1722 }
1723 
nativeTestGetNameInUnInitState(JNIEnv *,jobject)1724 static jboolean nativeTestGetNameInUnInitState(JNIEnv*, jobject) {
1725     auto* nativeCodecUnitTest = new NativeCodecUnitTest(AMEDIA_MIMETYPE_AUDIO_AAC);
1726     bool isPass = nativeCodecUnitTest->testGetNameInUnInitState();
1727     delete nativeCodecUnitTest;
1728     return static_cast<jboolean>(isPass);
1729 }
1730 
nativeTestSetAsyncNotifyCallbackInInitState(JNIEnv *,jobject)1731 static jboolean nativeTestSetAsyncNotifyCallbackInInitState(JNIEnv*, jobject) {
1732     auto* nativeCodecUnitTest = new NativeCodecUnitTest(AMEDIA_MIMETYPE_AUDIO_AAC);
1733     bool isPass = nativeCodecUnitTest->testSetAsyncNotifyCallbackInInitState();
1734     delete nativeCodecUnitTest;
1735     return static_cast<jboolean>(isPass);
1736 }
1737 
nativeTestSetAsyncNotifyCallbackInRunningState(JNIEnv *,jobject)1738 static jboolean nativeTestSetAsyncNotifyCallbackInRunningState(JNIEnv*, jobject) {
1739     auto* nativeCodecUnitTest = new NativeCodecUnitTest(AMEDIA_MIMETYPE_AUDIO_AAC);
1740     bool isPass = nativeCodecUnitTest->testSetAsyncNotifyCallbackInRunningState();
1741     delete nativeCodecUnitTest;
1742     return static_cast<jboolean>(isPass);
1743 }
1744 
nativeTestSetAsyncNotifyCallbackInUnInitState(JNIEnv *,jobject)1745 static jboolean nativeTestSetAsyncNotifyCallbackInUnInitState(JNIEnv*, jobject) {
1746     auto* nativeCodecUnitTest = new NativeCodecUnitTest(AMEDIA_MIMETYPE_AUDIO_AAC);
1747     bool isPass = nativeCodecUnitTest->testSetAsyncNotifyCallbackInUnInitState();
1748     delete nativeCodecUnitTest;
1749     return static_cast<jboolean>(isPass);
1750 }
1751 
nativeTestGetInputBufferInInitState(JNIEnv *,jobject)1752 static jboolean nativeTestGetInputBufferInInitState(JNIEnv*, jobject) {
1753     auto* nativeCodecUnitTest = new NativeCodecUnitTest(AMEDIA_MIMETYPE_AUDIO_AAC);
1754     bool isPass = nativeCodecUnitTest->testGetInputBufferInInitState();
1755     delete nativeCodecUnitTest;
1756     return static_cast<jboolean>(isPass);
1757 }
1758 
nativeTestGetInputBufferInRunningState(JNIEnv *,jobject)1759 static jboolean nativeTestGetInputBufferInRunningState(JNIEnv*, jobject) {
1760     auto* nativeCodecUnitTest = new NativeCodecUnitTest(AMEDIA_MIMETYPE_AUDIO_AAC);
1761     bool isPass = nativeCodecUnitTest->testGetInputBufferInRunningState();
1762     delete nativeCodecUnitTest;
1763     return static_cast<jboolean>(isPass);
1764 }
1765 
nativeTestGetInputBufferInUnInitState(JNIEnv *,jobject)1766 static jboolean nativeTestGetInputBufferInUnInitState(JNIEnv*, jobject) {
1767     auto* nativeCodecUnitTest = new NativeCodecUnitTest(AMEDIA_MIMETYPE_AUDIO_AAC);
1768     bool isPass = nativeCodecUnitTest->testGetInputBufferInUnInitState();
1769     delete nativeCodecUnitTest;
1770     return static_cast<jboolean>(isPass);
1771 }
1772 
nativeTestGetInputFormatInInitState(JNIEnv *,jobject)1773 static jboolean nativeTestGetInputFormatInInitState(JNIEnv*, jobject) {
1774     auto* nativeCodecUnitTest = new NativeCodecUnitTest(AMEDIA_MIMETYPE_AUDIO_AAC);
1775     bool isPass = nativeCodecUnitTest->testGetInputFormatInInitState();
1776     delete nativeCodecUnitTest;
1777     return static_cast<jboolean>(isPass);
1778 }
1779 
nativeTestGetInputFormatInRunningState(JNIEnv *,jobject)1780 static jboolean nativeTestGetInputFormatInRunningState(JNIEnv*, jobject) {
1781     auto* nativeCodecUnitTest = new NativeCodecUnitTest(AMEDIA_MIMETYPE_AUDIO_AAC);
1782     bool isPass = nativeCodecUnitTest->testGetInputFormatInRunningState();
1783     delete nativeCodecUnitTest;
1784     return static_cast<jboolean>(isPass);
1785 }
1786 
nativeTestGetInputFormatInUnInitState(JNIEnv *,jobject)1787 static jboolean nativeTestGetInputFormatInUnInitState(JNIEnv*, jobject) {
1788     auto* nativeCodecUnitTest = new NativeCodecUnitTest(AMEDIA_MIMETYPE_AUDIO_AAC);
1789     bool isPass = nativeCodecUnitTest->testGetInputFormatInUnInitState();
1790     delete nativeCodecUnitTest;
1791     return static_cast<jboolean>(isPass);
1792 }
1793 
nativeTestGetOutputBufferInInitState(JNIEnv *,jobject)1794 static jboolean nativeTestGetOutputBufferInInitState(JNIEnv*, jobject) {
1795     auto* nativeCodecUnitTest = new NativeCodecUnitTest(AMEDIA_MIMETYPE_AUDIO_AAC);
1796     bool isPass = nativeCodecUnitTest->testGetOutputBufferInInitState();
1797     delete nativeCodecUnitTest;
1798     return static_cast<jboolean>(isPass);
1799 }
1800 
nativeTestGetOutputBufferInRunningState(JNIEnv *,jobject)1801 static jboolean nativeTestGetOutputBufferInRunningState(JNIEnv*, jobject) {
1802     auto* nativeCodecUnitTest = new NativeCodecUnitTest(AMEDIA_MIMETYPE_AUDIO_AAC);
1803     bool isPass = nativeCodecUnitTest->testGetOutputBufferInRunningState();
1804     delete nativeCodecUnitTest;
1805     return static_cast<jboolean>(isPass);
1806 }
1807 
nativeTestGetOutputBufferInUnInitState(JNIEnv *,jobject)1808 static jboolean nativeTestGetOutputBufferInUnInitState(JNIEnv*, jobject) {
1809     auto* nativeCodecUnitTest = new NativeCodecUnitTest(AMEDIA_MIMETYPE_AUDIO_AAC);
1810     bool isPass = nativeCodecUnitTest->testGetOutputBufferInUnInitState();
1811     delete nativeCodecUnitTest;
1812     return static_cast<jboolean>(isPass);
1813 }
1814 
nativeTestGetOutputFormatInInitState(JNIEnv *,jobject)1815 static jboolean nativeTestGetOutputFormatInInitState(JNIEnv*, jobject) {
1816     auto* nativeCodecUnitTest = new NativeCodecUnitTest(AMEDIA_MIMETYPE_AUDIO_AAC);
1817     bool isPass = nativeCodecUnitTest->testGetOutputFormatInInitState();
1818     delete nativeCodecUnitTest;
1819     return static_cast<jboolean>(isPass);
1820 }
1821 
nativeTestGetOutputFormatInRunningState(JNIEnv *,jobject)1822 static jboolean nativeTestGetOutputFormatInRunningState(JNIEnv*, jobject) {
1823     auto* nativeCodecUnitTest = new NativeCodecUnitTest(AMEDIA_MIMETYPE_AUDIO_AAC);
1824     bool isPass = nativeCodecUnitTest->testGetOutputFormatInRunningState();
1825     delete nativeCodecUnitTest;
1826     return static_cast<jboolean>(isPass);
1827 }
1828 
nativeTestGetOutputFormatInUnInitState(JNIEnv *,jobject)1829 static jboolean nativeTestGetOutputFormatInUnInitState(JNIEnv*, jobject) {
1830     auto* nativeCodecUnitTest = new NativeCodecUnitTest(AMEDIA_MIMETYPE_AUDIO_AAC);
1831     bool isPass = nativeCodecUnitTest->testGetOutputFormatInUnInitState();
1832     delete nativeCodecUnitTest;
1833     return static_cast<jboolean>(isPass);
1834 }
1835 
nativeTestSetParametersInInitState(JNIEnv *,jobject)1836 static jboolean nativeTestSetParametersInInitState(JNIEnv*, jobject) {
1837     auto* nativeCodecUnitTest = new NativeCodecUnitTest(AMEDIA_MIMETYPE_AUDIO_AAC);
1838     bool isPass = nativeCodecUnitTest->testSetParametersInInitState();
1839     delete nativeCodecUnitTest;
1840     return static_cast<jboolean>(isPass);
1841 }
1842 
nativeTestSetParametersInRunningState(JNIEnv *,jobject)1843 static jboolean nativeTestSetParametersInRunningState(JNIEnv*, jobject) {
1844     auto* nativeCodecUnitTest = new NativeCodecUnitTest(AMEDIA_MIMETYPE_AUDIO_AAC);
1845     bool isPass = nativeCodecUnitTest->testSetParametersInRunningState();
1846     delete nativeCodecUnitTest;
1847     return static_cast<jboolean>(isPass);
1848 }
1849 
nativeTestSetParametersInUnInitState(JNIEnv *,jobject)1850 static jboolean nativeTestSetParametersInUnInitState(JNIEnv*, jobject) {
1851     auto* nativeCodecUnitTest = new NativeCodecUnitTest(AMEDIA_MIMETYPE_AUDIO_AAC);
1852     bool isPass = nativeCodecUnitTest->testSetParametersInUnInitState();
1853     delete nativeCodecUnitTest;
1854     return static_cast<jboolean>(isPass);
1855 }
1856 
nativeTestStartInRunningState(JNIEnv *,jobject)1857 static jboolean nativeTestStartInRunningState(JNIEnv*, jobject) {
1858     auto* nativeCodecUnitTest = new NativeCodecUnitTest(AMEDIA_MIMETYPE_AUDIO_AAC);
1859     bool isPass = nativeCodecUnitTest->testStartInRunningState();
1860     delete nativeCodecUnitTest;
1861     return static_cast<jboolean>(isPass);
1862 }
1863 
nativeTestStartInUnInitState(JNIEnv *,jobject)1864 static jboolean nativeTestStartInUnInitState(JNIEnv*, jobject) {
1865     auto* nativeCodecUnitTest = new NativeCodecUnitTest(AMEDIA_MIMETYPE_AUDIO_AAC);
1866     bool isPass = nativeCodecUnitTest->testStartInUnInitState();
1867     delete nativeCodecUnitTest;
1868     return static_cast<jboolean>(isPass);
1869 }
1870 
nativeTestStopInInitState(JNIEnv *,jobject)1871 static jboolean nativeTestStopInInitState(JNIEnv*, jobject) {
1872     auto* nativeCodecUnitTest = new NativeCodecUnitTest(AMEDIA_MIMETYPE_AUDIO_AAC);
1873     bool isPass = nativeCodecUnitTest->testStopInInitState();
1874     delete nativeCodecUnitTest;
1875     return static_cast<jboolean>(isPass);
1876 }
1877 
nativeTestStopInRunningState(JNIEnv *,jobject)1878 static jboolean nativeTestStopInRunningState(JNIEnv*, jobject) {
1879     auto* nativeCodecUnitTest = new NativeCodecUnitTest(AMEDIA_MIMETYPE_AUDIO_AAC);
1880     bool isPass = nativeCodecUnitTest->testStopInRunningState();
1881     delete nativeCodecUnitTest;
1882     return static_cast<jboolean>(isPass);
1883 }
1884 
nativeTestStopInUnInitState(JNIEnv *,jobject)1885 static jboolean nativeTestStopInUnInitState(JNIEnv*, jobject) {
1886     auto* nativeCodecUnitTest = new NativeCodecUnitTest(AMEDIA_MIMETYPE_AUDIO_AAC);
1887     bool isPass = nativeCodecUnitTest->testStopInUnInitState();
1888     delete nativeCodecUnitTest;
1889     return static_cast<jboolean>(isPass);
1890 }
1891 
nativeTestQueueInputBufferInInitState(JNIEnv *,jobject)1892 static jboolean nativeTestQueueInputBufferInInitState(JNIEnv*, jobject) {
1893     auto* nativeCodecUnitTest = new NativeCodecUnitTest(AMEDIA_MIMETYPE_AUDIO_AAC);
1894     bool isPass = nativeCodecUnitTest->testQueueInputBufferInInitState();
1895     delete nativeCodecUnitTest;
1896     return static_cast<jboolean>(isPass);
1897 }
1898 
nativeTestQueueInputBufferWithBadIndex(JNIEnv *,jobject)1899 static jboolean nativeTestQueueInputBufferWithBadIndex(JNIEnv*, jobject) {
1900     auto* nativeCodecUnitTest = new NativeCodecUnitTest(AMEDIA_MIMETYPE_AUDIO_AAC);
1901     bool isPass = nativeCodecUnitTest->testQueueInputBufferWithBadIndex();
1902     delete nativeCodecUnitTest;
1903     return static_cast<jboolean>(isPass);
1904 }
1905 
nativeTestQueueInputBufferWithBadSize(JNIEnv *,jobject)1906 static jboolean nativeTestQueueInputBufferWithBadSize(JNIEnv*, jobject) {
1907     auto* nativeCodecUnitTest = new NativeCodecUnitTest(AMEDIA_MIMETYPE_AUDIO_AAC);
1908     bool isPass = nativeCodecUnitTest->testQueueInputBufferWithBadSize();
1909     delete nativeCodecUnitTest;
1910     return static_cast<jboolean>(isPass);
1911 }
1912 
nativeTestQueueInputBufferWithBadBuffInfo(JNIEnv *,jobject)1913 static jboolean nativeTestQueueInputBufferWithBadBuffInfo(JNIEnv*, jobject) {
1914     auto* nativeCodecUnitTest = new NativeCodecUnitTest(AMEDIA_MIMETYPE_AUDIO_AAC);
1915     bool isPass = nativeCodecUnitTest->testQueueInputBufferWithBadBuffInfo();
1916     delete nativeCodecUnitTest;
1917     return static_cast<jboolean>(isPass);
1918 }
1919 
nativeTestQueueInputBufferWithBadOffset(JNIEnv *,jobject)1920 static jboolean nativeTestQueueInputBufferWithBadOffset(JNIEnv*, jobject) {
1921     auto* nativeCodecUnitTest = new NativeCodecUnitTest(AMEDIA_MIMETYPE_AUDIO_AAC);
1922     bool isPass = nativeCodecUnitTest->testQueueInputBufferWithBadOffset();
1923     delete nativeCodecUnitTest;
1924     return static_cast<jboolean>(isPass);
1925 }
1926 
nativeTestQueueInputBufferInUnInitState(JNIEnv *,jobject)1927 static jboolean nativeTestQueueInputBufferInUnInitState(JNIEnv*, jobject) {
1928     auto* nativeCodecUnitTest = new NativeCodecUnitTest(AMEDIA_MIMETYPE_AUDIO_AAC);
1929     bool isPass = nativeCodecUnitTest->testQueueInputBufferInUnInitState();
1930     delete nativeCodecUnitTest;
1931     return static_cast<jboolean>(isPass);
1932 }
1933 
nativeTestReleaseOutputBufferInInitState(JNIEnv *,jobject)1934 static jboolean nativeTestReleaseOutputBufferInInitState(JNIEnv*, jobject) {
1935     auto* nativeCodecUnitTest = new NativeCodecUnitTest(AMEDIA_MIMETYPE_AUDIO_AAC);
1936     bool isPass = nativeCodecUnitTest->testReleaseOutputBufferInInitState();
1937     delete nativeCodecUnitTest;
1938     return static_cast<jboolean>(isPass);
1939 }
1940 
nativeTestReleaseOutputBufferInRunningState(JNIEnv *,jobject)1941 static jboolean nativeTestReleaseOutputBufferInRunningState(JNIEnv*, jobject) {
1942     auto* nativeCodecUnitTest = new NativeCodecUnitTest(AMEDIA_MIMETYPE_AUDIO_AAC);
1943     bool isPass = nativeCodecUnitTest->testReleaseOutputBufferInRunningState();
1944     delete nativeCodecUnitTest;
1945     return static_cast<jboolean>(isPass);
1946 }
1947 
nativeTestReleaseOutputBufferInUnInitState(JNIEnv *,jobject)1948 static jboolean nativeTestReleaseOutputBufferInUnInitState(JNIEnv*, jobject) {
1949     auto* nativeCodecUnitTest = new NativeCodecUnitTest(AMEDIA_MIMETYPE_AUDIO_AAC);
1950     bool isPass = nativeCodecUnitTest->testReleaseOutputBufferInUnInitState();
1951     delete nativeCodecUnitTest;
1952     return static_cast<jboolean>(isPass);
1953 }
1954 
nativeTestGetBufferFormatInInitState(JNIEnv *,jobject)1955 static jboolean nativeTestGetBufferFormatInInitState(JNIEnv*, jobject) {
1956     auto* nativeCodecUnitTest = new NativeCodecUnitTest(AMEDIA_MIMETYPE_AUDIO_AAC);
1957     bool isPass = nativeCodecUnitTest->testGetBufferFormatInInitState();
1958     delete nativeCodecUnitTest;
1959     return static_cast<jboolean>(isPass);
1960 }
1961 
nativeTestGetBufferFormatInRunningState(JNIEnv *,jobject)1962 static jboolean nativeTestGetBufferFormatInRunningState(JNIEnv*, jobject) {
1963     auto* nativeCodecUnitTest = new NativeCodecUnitTest(AMEDIA_MIMETYPE_AUDIO_AAC);
1964     bool isPass = nativeCodecUnitTest->testGetBufferFormatInRunningState();
1965     delete nativeCodecUnitTest;
1966     return static_cast<jboolean>(isPass);
1967 }
1968 
nativeTestGetBufferFormatInUnInitState(JNIEnv *,jobject)1969 static jboolean nativeTestGetBufferFormatInUnInitState(JNIEnv*, jobject) {
1970     auto* nativeCodecUnitTest = new NativeCodecUnitTest(AMEDIA_MIMETYPE_AUDIO_AAC);
1971     bool isPass = nativeCodecUnitTest->testGetBufferFormatInUnInitState();
1972     delete nativeCodecUnitTest;
1973     return static_cast<jboolean>(isPass);
1974 }
1975 
registerAndroidMediaV2CtsCodecUnitTest(JNIEnv * env)1976 int registerAndroidMediaV2CtsCodecUnitTest(JNIEnv* env) {
1977     const JNINativeMethod methodTable[] = {
1978             {"nativeTestCreateByCodecNameForNull", "()Z",
1979              (void*)nativeTestCreateByCodecNameForNull},
1980             {"nativeTestCreateByCodecNameForInvalidName", "()Z",
1981              (void*)nativeTestCreateByCodecNameForInvalidName},
1982             {"nativeTestCreateDecoderByTypeForNull", "()Z",
1983              (void*)nativeTestCreateDecoderByTypeForNull},
1984             {"nativeTestCreateDecoderByTypeForInvalidMime", "()Z",
1985              (void*)nativeTestCreateDecoderByTypeForInvalidMime},
1986             {"nativeTestCreateEncoderByTypeForNull", "()Z",
1987              (void*)nativeTestCreateEncoderByTypeForNull},
1988             {"nativeTestCreateEncoderByTypeForInvalidMime", "()Z",
1989              (void*)nativeTestCreateEncoderByTypeForInvalidMime},
1990             {"nativeTestConfigureForNullFormat", "()Z", (void*)nativeTestConfigureForNullFormat},
1991             {"nativeTestConfigureForEmptyFormat", "()Z", (void*)nativeTestConfigureForEmptyFormat},
1992             {"nativeTestConfigureCodecForIncompleteFormat", "(ZZ)Z",
1993              (void*)nativeTestConfigureCodecForIncompleteFormat},
1994             {"nativeTestConfigureEncoderForBadFlags", "()Z",
1995              (void*)nativeTestConfigureEncoderForBadFlags},
1996             {"nativeTestConfigureDecoderForBadFlags", "()Z",
1997              (void*)nativeTestConfigureDecoderForBadFlags},
1998             {"nativeTestConfigureInInitState", "()Z", (void*)nativeTestConfigureInInitState},
1999             {"nativeTestConfigureInRunningState", "()Z", (void*)nativeTestConfigureInRunningState},
2000             {"nativeTestConfigureInUnInitState", "()Z", (void*)nativeTestConfigureInUnInitState},
2001             {"nativeTestDequeueInputBufferInInitState", "()Z",
2002              (void*)nativeTestDequeueInputBufferInInitState},
2003             {"nativeTestDequeueInputBufferInRunningState", "()Z",
2004              (void*)nativeTestDequeueInputBufferInRunningState},
2005             {"nativeTestDequeueInputBufferInUnInitState", "()Z",
2006              (void*)nativeTestDequeueInputBufferInUnInitState},
2007             {"nativeTestDequeueOutputBufferInInitState", "()Z",
2008              (void*)nativeTestDequeueOutputBufferInInitState},
2009             {"nativeTestDequeueOutputBufferInRunningState", "()Z",
2010              (void*)nativeTestDequeueOutputBufferInRunningState},
2011             {"nativeTestDequeueOutputBufferInUnInitState", "()Z",
2012              (void*)nativeTestDequeueOutputBufferInUnInitState},
2013             {"nativeTestFlushInInitState", "()Z", (void*)nativeTestFlushInInitState},
2014             {"nativeTestFlushInRunningState", "()Z", (void*)nativeTestFlushInRunningState},
2015             {"nativeTestFlushInUnInitState", "()Z", (void*)nativeTestFlushInUnInitState},
2016             {"nativeTestGetNameInInitState", "()Z", (void*)nativeTestGetNameInInitState},
2017             {"nativeTestGetNameInRunningState", "()Z", (void*)nativeTestGetNameInRunningState},
2018             {"nativeTestGetNameInUnInitState", "()Z", (void*)nativeTestGetNameInUnInitState},
2019             {"nativeTestSetAsyncNotifyCallbackInInitState", "()Z",
2020              (void*)nativeTestSetAsyncNotifyCallbackInInitState},
2021             {"nativeTestSetAsyncNotifyCallbackInRunningState", "()Z",
2022              (void*)nativeTestSetAsyncNotifyCallbackInRunningState},
2023             {"nativeTestSetAsyncNotifyCallbackInUnInitState", "()Z",
2024              (void*)nativeTestSetAsyncNotifyCallbackInUnInitState},
2025             {"nativeTestGetInputBufferInInitState", "()Z",
2026              (void*)nativeTestGetInputBufferInInitState},
2027             {"nativeTestGetInputBufferInRunningState", "()Z",
2028              (void*)nativeTestGetInputBufferInRunningState},
2029             {"nativeTestGetInputBufferInUnInitState", "()Z",
2030              (void*)nativeTestGetInputBufferInUnInitState},
2031             {"nativeTestGetInputFormatInInitState", "()Z",
2032              (void*)nativeTestGetInputFormatInInitState},
2033             {"nativeTestGetInputFormatInRunningState", "()Z",
2034              (void*)nativeTestGetInputFormatInRunningState},
2035             {"nativeTestGetInputFormatInUnInitState", "()Z",
2036              (void*)nativeTestGetInputFormatInUnInitState},
2037             {"nativeTestGetOutputBufferInInitState", "()Z",
2038              (void*)nativeTestGetOutputBufferInInitState},
2039             {"nativeTestGetOutputBufferInRunningState", "()Z",
2040              (void*)nativeTestGetOutputBufferInRunningState},
2041             {"nativeTestGetOutputBufferInUnInitState", "()Z",
2042              (void*)nativeTestGetOutputBufferInUnInitState},
2043             {"nativeTestGetOutputFormatInInitState", "()Z",
2044              (void*)nativeTestGetOutputFormatInInitState},
2045             {"nativeTestGetOutputFormatInRunningState", "()Z",
2046              (void*)nativeTestGetOutputFormatInRunningState},
2047             {"nativeTestGetOutputFormatInUnInitState", "()Z",
2048              (void*)nativeTestGetOutputFormatInUnInitState},
2049             {"nativeTestSetParametersInInitState", "()Z",
2050              (void*)nativeTestSetParametersInInitState},
2051             {"nativeTestSetParametersInRunningState", "()Z",
2052              (void*)nativeTestSetParametersInRunningState},
2053             {"nativeTestSetParametersInUnInitState", "()Z",
2054              (void*)nativeTestSetParametersInUnInitState},
2055             {"nativeTestStartInRunningState", "()Z", (void*)nativeTestStartInRunningState},
2056             {"nativeTestStartInUnInitState", "()Z", (void*)nativeTestStartInUnInitState},
2057             {"nativeTestStopInInitState", "()Z", (void*)nativeTestStopInInitState},
2058             {"nativeTestStopInRunningState", "()Z", (void*)nativeTestStopInRunningState},
2059             {"nativeTestStopInUnInitState", "()Z", (void*)nativeTestStopInUnInitState},
2060             {"nativeTestQueueInputBufferInInitState", "()Z",
2061              (void*)nativeTestQueueInputBufferInInitState},
2062             {"nativeTestQueueInputBufferWithBadIndex", "()Z",
2063              (void*)nativeTestQueueInputBufferWithBadIndex},
2064             {"nativeTestQueueInputBufferWithBadSize", "()Z",
2065              (void*)nativeTestQueueInputBufferWithBadSize},
2066             {"nativeTestQueueInputBufferWithBadBuffInfo", "()Z",
2067              (void*)nativeTestQueueInputBufferWithBadBuffInfo},
2068             {"nativeTestQueueInputBufferWithBadOffset", "()Z",
2069              (void*)nativeTestQueueInputBufferWithBadOffset},
2070             {"nativeTestQueueInputBufferInUnInitState", "()Z",
2071              (void*)nativeTestQueueInputBufferInUnInitState},
2072             {"nativeTestReleaseOutputBufferInInitState", "()Z",
2073              (void*)nativeTestReleaseOutputBufferInInitState},
2074             {"nativeTestReleaseOutputBufferInRunningState", "()Z",
2075              (void*)nativeTestReleaseOutputBufferInRunningState},
2076             {"nativeTestReleaseOutputBufferInUnInitState", "()Z",
2077              (void*)nativeTestReleaseOutputBufferInUnInitState},
2078             {"nativeTestGetBufferFormatInInitState", "()Z",
2079              (void*)nativeTestGetBufferFormatInInitState},
2080             {"nativeTestGetBufferFormatInRunningState", "()Z",
2081              (void*)nativeTestGetBufferFormatInRunningState},
2082             {"nativeTestGetBufferFormatInUnInitState", "()Z",
2083              (void*)nativeTestGetBufferFormatInUnInitState},
2084 
2085     };
2086     jclass c = env->FindClass("android/mediav2/cts/CodecUnitTest$TestApiNative");
2087     return env->RegisterNatives(c, methodTable, sizeof(methodTable) / sizeof(JNINativeMethod));
2088 }
2089