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