1 /*
2  * Copyright (C) 2019 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 "NativeExtractorUnitTest"
19 #include <log/log.h>
20 
21 #include <NdkMediaExtractor.h>
22 #include <jni.h>
23 #include <sys/stat.h>
24 
25 #include <cstdlib>
26 #include <cstring>
27 
setExtractorDataSource(AMediaExtractor * extractor,FILE * fp)28 static media_status_t setExtractorDataSource(AMediaExtractor* extractor, FILE* fp) {
29     media_status_t status = AMEDIA_ERROR_BASE;
30     struct stat buf {};
31     if (fp && !fstat(fileno(fp), &buf))
32         status = AMediaExtractor_setDataSourceFd(extractor, fileno(fp), 0, buf.st_size);
33     if (status != AMEDIA_OK) ALOGE("error: AMediaExtractor_setDataSourceFd failed %d", status);
34     return status;
35 }
36 
nativeTestGetTrackCountBeforeSetDataSource(JNIEnv *,jobject)37 static jboolean nativeTestGetTrackCountBeforeSetDataSource(JNIEnv*, jobject) {
38     AMediaExtractor* extractor = AMediaExtractor_new();
39     bool isPass = (0 == AMediaExtractor_getTrackCount(extractor));
40     if (!isPass) ALOGE("error: received valid trackCount before setDataSource");
41     AMediaExtractor_delete(extractor);
42     return static_cast<jboolean>(isPass);
43 }
44 
nativeTestSelectTrackBeforeSetDataSource(JNIEnv *,jobject)45 static jboolean nativeTestSelectTrackBeforeSetDataSource(JNIEnv*, jobject) {
46     AMediaExtractor* extractor = AMediaExtractor_new();
47     bool isPass = (AMEDIA_OK != AMediaExtractor_selectTrack(extractor, 0));
48     if (!isPass) ALOGE("error: selectTrack succeeds before setDataSource");
49     AMediaExtractor_delete(extractor);
50     return static_cast<jboolean>(isPass);
51 }
52 
nativeTestSelectTrackForInvalidIndex(JNIEnv * env,jobject,jstring jsrcPath)53 static jboolean nativeTestSelectTrackForInvalidIndex(JNIEnv* env, jobject, jstring jsrcPath) {
54     bool isPass = false;
55     const char* csrcPath = env->GetStringUTFChars(jsrcPath, nullptr);
56     AMediaExtractor* extractor = AMediaExtractor_new();
57     FILE* srcFp = fopen(csrcPath, "rbe");
58     if (AMEDIA_OK == setExtractorDataSource(extractor, srcFp)) {
59         if (AMEDIA_OK !=
60             AMediaExtractor_selectTrack(extractor, AMediaExtractor_getTrackCount(extractor))) {
61             isPass = true;
62         } else {
63             ALOGE("error: selectTrack succeeds for out of bounds track index: %zu",
64                   AMediaExtractor_getTrackCount(extractor));
65         }
66     }
67     AMediaExtractor_delete(extractor);
68     if (srcFp) fclose(srcFp);
69     env->ReleaseStringUTFChars(jsrcPath, csrcPath);
70     return static_cast<jboolean>(isPass);
71 }
72 
nativeTestIdempotentSelectTrack(JNIEnv * env,jobject,jstring jsrcPath)73 static jboolean nativeTestIdempotentSelectTrack(JNIEnv* env, jobject, jstring jsrcPath) {
74     bool isPass = false;
75     const char* csrcPath = env->GetStringUTFChars(jsrcPath, nullptr);
76     AMediaExtractor* extractor = AMediaExtractor_new();
77     FILE* srcFp = fopen(csrcPath, "rbe");
78     if (AMEDIA_OK == setExtractorDataSource(extractor, srcFp)) {
79         isPass = AMediaExtractor_selectTrack(extractor, 0) == AMEDIA_OK;
80         isPass &= AMediaExtractor_selectTrack(extractor, 0) == AMEDIA_OK;
81         if (!isPass) ALOGE("error: multiple selection of same track has failed");
82     }
83     AMediaExtractor_delete(extractor);
84     if (srcFp) fclose(srcFp);
85     env->ReleaseStringUTFChars(jsrcPath, csrcPath);
86     return static_cast<jboolean>(isPass);
87 }
88 
nativeTestUnselectTrackBeforeSetDataSource(JNIEnv *,jobject)89 static jboolean nativeTestUnselectTrackBeforeSetDataSource(JNIEnv*, jobject) {
90     AMediaExtractor* extractor = AMediaExtractor_new();
91     bool isPass = (AMEDIA_OK != AMediaExtractor_unselectTrack(extractor, 0));
92     if (!isPass) ALOGE("error: unselectTrack succeeds before setDataSource");
93     AMediaExtractor_delete(extractor);
94     return static_cast<jboolean>(isPass);
95 }
96 
nativeTestUnselectTrackForInvalidIndex(JNIEnv * env,jobject,jstring jsrcPath)97 static jboolean nativeTestUnselectTrackForInvalidIndex(JNIEnv* env, jobject, jstring jsrcPath) {
98     bool isPass = false;
99     const char* csrcPath = env->GetStringUTFChars(jsrcPath, nullptr);
100     AMediaExtractor* extractor = AMediaExtractor_new();
101     FILE* srcFp = fopen(csrcPath, "rbe");
102     if (AMEDIA_OK == setExtractorDataSource(extractor, srcFp)) {
103         if (AMEDIA_OK !=
104             AMediaExtractor_unselectTrack(extractor, AMediaExtractor_getTrackCount(extractor))) {
105             isPass = true;
106         } else {
107             ALOGE("error: unselectTrack succeeds for out of bounds track index: %zu",
108                   AMediaExtractor_getTrackCount(extractor));
109         }
110     }
111     AMediaExtractor_delete(extractor);
112     if (srcFp) fclose(srcFp);
113     env->ReleaseStringUTFChars(jsrcPath, csrcPath);
114     return static_cast<jboolean>(isPass);
115 }
116 
nativeTestUnselectTrackForUnSelectedTrackIndex(JNIEnv * env,jobject,jstring jsrcPath)117 static jboolean nativeTestUnselectTrackForUnSelectedTrackIndex(JNIEnv* env, jobject,
118                                                                jstring jsrcPath) {
119     bool isPass = false;
120     const char* csrcPath = env->GetStringUTFChars(jsrcPath, nullptr);
121     AMediaExtractor* extractor = AMediaExtractor_new();
122     FILE* srcFp = fopen(csrcPath, "rbe");
123     if (AMEDIA_OK == setExtractorDataSource(extractor, srcFp)) {
124         isPass = (AMediaExtractor_unselectTrack(extractor, 0) == AMEDIA_OK);
125         if (!isPass) ALOGE("error: un-selection of non-selected track has failed");
126     }
127     AMediaExtractor_delete(extractor);
128     if (srcFp) fclose(srcFp);
129     env->ReleaseStringUTFChars(jsrcPath, csrcPath);
130     return static_cast<jboolean>(isPass);
131 }
132 
nativeTestIdempotentUnselectTrack(JNIEnv * env,jobject,jstring jsrcPath)133 static jboolean nativeTestIdempotentUnselectTrack(JNIEnv* env, jobject, jstring jsrcPath) {
134     bool isPass = false;
135     const char* csrcPath = env->GetStringUTFChars(jsrcPath, nullptr);
136     AMediaExtractor* extractor = AMediaExtractor_new();
137     FILE* srcFp = fopen(csrcPath, "rbe");
138     if (AMEDIA_OK == setExtractorDataSource(extractor, srcFp)) {
139         isPass = AMediaExtractor_selectTrack(extractor, 0) == AMEDIA_OK;
140         if (isPass) {
141             isPass = AMediaExtractor_unselectTrack(extractor, 0) == AMEDIA_OK;
142             isPass &= AMediaExtractor_unselectTrack(extractor, 0) == AMEDIA_OK;
143             if (!isPass) ALOGE("error: multiple unselection of selected track has failed");
144         } else {
145             ALOGE("error: selection of track 0 has failed for file %s", csrcPath);
146         }
147     }
148     AMediaExtractor_delete(extractor);
149     if (srcFp) fclose(srcFp);
150     env->ReleaseStringUTFChars(jsrcPath, csrcPath);
151     return static_cast<jboolean>(isPass);
152 }
153 
nativeTestSeekToBeforeSetDataSource(JNIEnv *,jobject)154 static jboolean nativeTestSeekToBeforeSetDataSource(JNIEnv*, jobject) {
155     AMediaExtractor* extractor = AMediaExtractor_new();
156     bool isPass = AMediaExtractor_seekTo(extractor, 0, AMEDIAEXTRACTOR_SEEK_NEXT_SYNC) != AMEDIA_OK;
157     if (!isPass) ALOGE("error: seekTo() succeeds before setting data source");
158     AMediaExtractor_delete(extractor);
159     return static_cast<jboolean>(isPass);
160 }
161 
nativeTestSeekToBeforeSelectTrack(JNIEnv * env,jobject,jstring jsrcPath)162 static jboolean nativeTestSeekToBeforeSelectTrack(JNIEnv* env, jobject, jstring jsrcPath) {
163     bool isPass = false;
164     const char* csrcPath = env->GetStringUTFChars(jsrcPath, nullptr);
165     AMediaExtractor* extractor = AMediaExtractor_new();
166     FILE* srcFp = fopen(csrcPath, "rbe");
167     if (AMEDIA_OK == setExtractorDataSource(extractor, srcFp)) {
168         isPass = AMediaExtractor_seekTo(extractor, 0, AMEDIAEXTRACTOR_SEEK_NEXT_SYNC) != AMEDIA_OK;
169         if (!isPass) ALOGE("error: seekTo() succeeds before selecting track");
170     }
171     AMediaExtractor_delete(extractor);
172     if (srcFp) fclose(srcFp);
173     env->ReleaseStringUTFChars(jsrcPath, csrcPath);
174     return static_cast<jboolean>(isPass);
175 }
176 
nativeTestGetCachedDurationBeforeSetDataSource(JNIEnv *,jobject)177 static jboolean nativeTestGetCachedDurationBeforeSetDataSource(JNIEnv*, jobject) {
178     AMediaExtractor* extractor = AMediaExtractor_new();
179     bool isPass = AMediaExtractor_getCachedDuration(extractor) == -1;
180     if (!isPass) ALOGE("error: getCachedDuration returns unexpected value before setDataSource");
181     AMediaExtractor_delete(extractor);
182     return static_cast<jboolean>(isPass);
183 }
184 
nativeTestIfGetFileFormatSucceedsBeforeSetDataSource(JNIEnv *,jobject)185 static jboolean nativeTestIfGetFileFormatSucceedsBeforeSetDataSource(JNIEnv*, jobject) {
186     AMediaExtractor* extractor = AMediaExtractor_new();
187     AMediaFormat* empty = AMediaFormat_new();
188     AMediaFormat* format = AMediaExtractor_getFileFormat(extractor);
189     bool isPass = true;
190     if (format == nullptr ||
191         strcmp(AMediaFormat_toString(empty), AMediaFormat_toString(format)) != 0) {
192         isPass = false;
193         ALOGE("error: getFileFormat before set data source yields unexpected result");
194     }
195     if (format) AMediaFormat_delete(format);
196     AMediaFormat_delete(empty);
197     AMediaExtractor_delete(extractor);
198     return static_cast<jboolean>(isPass);
199 }
200 
nativeTestAdvanceBeforeSetDataSource(JNIEnv *,jobject)201 static jboolean nativeTestAdvanceBeforeSetDataSource(JNIEnv*, jobject) {
202     AMediaExtractor* extractor = AMediaExtractor_new();
203     bool isPass = !AMediaExtractor_advance(extractor);
204     if (!isPass) ALOGE("error: advance succeeds before setDataSource");
205     AMediaExtractor_delete(extractor);
206     return static_cast<jboolean>(isPass);
207 }
208 
nativeTestAdvanceBeforeSelectTrack(JNIEnv * env,jobject,jstring jsrcPath)209 static jboolean nativeTestAdvanceBeforeSelectTrack(JNIEnv* env, jobject, jstring jsrcPath) {
210     bool isPass = false;
211     const char* csrcPath = env->GetStringUTFChars(jsrcPath, nullptr);
212     AMediaExtractor* extractor = AMediaExtractor_new();
213     FILE* srcFp = fopen(csrcPath, "rbe");
214     if (AMEDIA_OK == setExtractorDataSource(extractor, srcFp)) {
215         isPass = !AMediaExtractor_advance(extractor);
216         if (!isPass) ALOGE("error: advance succeeds without any active tracks");
217     }
218     AMediaExtractor_delete(extractor);
219     if (srcFp) fclose(srcFp);
220     env->ReleaseStringUTFChars(jsrcPath, csrcPath);
221     return static_cast<jboolean>(isPass);
222 }
223 
nativeTestGetSampleFlagsBeforeSetDataSource(JNIEnv *,jobject)224 static jboolean nativeTestGetSampleFlagsBeforeSetDataSource(JNIEnv*, jobject) {
225     AMediaExtractor* extractor = AMediaExtractor_new();
226     bool isPass = AMediaExtractor_getSampleFlags(extractor) == -1;
227     if (!isPass) ALOGE("error: received valid sample flag before setDataSource");
228     AMediaExtractor_delete(extractor);
229     return static_cast<jboolean>(isPass);
230 }
231 
nativeTestGetSampleFlagsBeforeSelectTrack(JNIEnv * env,jobject,jstring jsrcPath)232 static jboolean nativeTestGetSampleFlagsBeforeSelectTrack(JNIEnv* env, jobject, jstring jsrcPath) {
233     bool isPass = false;
234     const char* csrcPath = env->GetStringUTFChars(jsrcPath, nullptr);
235     AMediaExtractor* extractor = AMediaExtractor_new();
236     FILE* srcFp = fopen(csrcPath, "rbe");
237     if (AMEDIA_OK == setExtractorDataSource(extractor, srcFp)) {
238         isPass = AMediaExtractor_getSampleFlags(extractor) == -1;
239         if (!isPass) ALOGE("error: received valid sample flag without any active tracks");
240     }
241     AMediaExtractor_delete(extractor);
242     if (srcFp) fclose(srcFp);
243     env->ReleaseStringUTFChars(jsrcPath, csrcPath);
244     return static_cast<jboolean>(isPass);
245 }
246 
nativeTestGetSampleTimeBeforeSetDataSource(JNIEnv *,jobject)247 static jboolean nativeTestGetSampleTimeBeforeSetDataSource(JNIEnv*, jobject) {
248     AMediaExtractor* extractor = AMediaExtractor_new();
249     bool isPass = AMediaExtractor_getSampleTime(extractor) == -1;
250     if (!isPass) ALOGE("error: received valid pts before setDataSource");
251     AMediaExtractor_delete(extractor);
252     return static_cast<jboolean>(isPass);
253 }
254 
nativeTestGetSampleTimeBeforeSelectTrack(JNIEnv * env,jobject,jstring jsrcPath)255 static jboolean nativeTestGetSampleTimeBeforeSelectTrack(JNIEnv* env, jobject, jstring jsrcPath) {
256     bool isPass = false;
257     AMediaExtractor* extractor = AMediaExtractor_new();
258     const char* csrcPath = env->GetStringUTFChars(jsrcPath, nullptr);
259     FILE* srcFp = fopen(csrcPath, "rbe");
260     if (AMEDIA_OK == setExtractorDataSource(extractor, srcFp)) {
261         isPass = AMediaExtractor_getSampleTime(extractor) == -1;
262         if (!isPass) ALOGE("error: received valid pts without any active tracks");
263     }
264     AMediaExtractor_delete(extractor);
265     if (srcFp) fclose(srcFp);
266     env->ReleaseStringUTFChars(jsrcPath, csrcPath);
267     return static_cast<jboolean>(isPass);
268 }
269 
nativeTestGetSampleSizeBeforeSetDataSource(JNIEnv *,jobject)270 static jboolean nativeTestGetSampleSizeBeforeSetDataSource(JNIEnv*, jobject) {
271     AMediaExtractor* extractor = AMediaExtractor_new();
272     bool isPass = AMediaExtractor_getSampleSize(extractor) == -1;
273     if (!isPass) ALOGE("error: received valid sample size before setDataSource");
274     AMediaExtractor_delete(extractor);
275     return static_cast<jboolean>(isPass);
276 }
277 
nativeTestGetSampleSizeBeforeSelectTrack(JNIEnv * env,jobject,jstring jsrcPath)278 static jboolean nativeTestGetSampleSizeBeforeSelectTrack(JNIEnv* env, jobject, jstring jsrcPath) {
279     bool isPass = false;
280     const char* csrcPath = env->GetStringUTFChars(jsrcPath, nullptr);
281     AMediaExtractor* extractor = AMediaExtractor_new();
282     FILE* srcFp = fopen(csrcPath, "rbe");
283     if (AMEDIA_OK == setExtractorDataSource(extractor, srcFp)) {
284         isPass = AMediaExtractor_getSampleSize(extractor) == -1;
285         if (!isPass) ALOGE("error: received valid sample size without any active tracks");
286     }
287     AMediaExtractor_delete(extractor);
288     if (srcFp) fclose(srcFp);
289     env->ReleaseStringUTFChars(jsrcPath, csrcPath);
290     return static_cast<jboolean>(isPass);
291 }
292 
nativeTestIfGetSampleFormatBeforeSetDataSource(JNIEnv *,jobject)293 static jboolean nativeTestIfGetSampleFormatBeforeSetDataSource(JNIEnv*, jobject) {
294     AMediaExtractor* extractor = AMediaExtractor_new();
295     AMediaFormat* format = AMediaFormat_new();
296     bool isPass = AMediaExtractor_getSampleFormat(extractor, format) != AMEDIA_OK;
297     if (!isPass) ALOGE("error: getSampleFormat succeeds before setDataSource");
298     AMediaFormat_delete(format);
299     AMediaExtractor_delete(extractor);
300     return static_cast<jboolean>(isPass);
301 }
302 
nativeTestIfGetSampleFormatBeforeSelectTrack(JNIEnv * env,jobject,jstring jsrcPath)303 static jboolean nativeTestIfGetSampleFormatBeforeSelectTrack(JNIEnv* env, jobject,
304                                                              jstring jsrcPath) {
305     bool isPass = false;
306     const char* csrcPath = env->GetStringUTFChars(jsrcPath, nullptr);
307     AMediaExtractor* extractor = AMediaExtractor_new();
308     AMediaFormat* format = AMediaFormat_new();
309     FILE* srcFp = fopen(csrcPath, "rbe");
310     if (AMEDIA_OK == setExtractorDataSource(extractor, srcFp)) {
311         isPass = AMediaExtractor_getSampleFormat(extractor, format) != AMEDIA_OK;
312         if (!isPass) ALOGE("error: getSampleFormat succeeds without any active tracks");
313     }
314     AMediaFormat_delete(format);
315     AMediaExtractor_delete(extractor);
316     if (srcFp) fclose(srcFp);
317     env->ReleaseStringUTFChars(jsrcPath, csrcPath);
318     return static_cast<jboolean>(isPass);
319 }
320 
nativeTestGetSampleTrackIndexBeforeSetDataSource(JNIEnv *,jobject)321 static jboolean nativeTestGetSampleTrackIndexBeforeSetDataSource(JNIEnv*, jobject) {
322     AMediaExtractor* extractor = AMediaExtractor_new();
323     bool isPass = AMediaExtractor_getSampleTrackIndex(extractor) == -1;
324     if (!isPass) ALOGE("error: received valid track index before setDataSource");
325     AMediaExtractor_delete(extractor);
326     return static_cast<jboolean>(isPass);
327 }
328 
nativeTestGetSampleTrackIndexBeforeSelectTrack(JNIEnv * env,jobject,jstring jsrcPath)329 static jboolean nativeTestGetSampleTrackIndexBeforeSelectTrack(JNIEnv* env, jobject,
330                                                                jstring jsrcPath) {
331     bool isPass = false;
332     AMediaExtractor* extractor = AMediaExtractor_new();
333     const char* csrcPath = env->GetStringUTFChars(jsrcPath, nullptr);
334     FILE* srcFp = fopen(csrcPath, "rbe");
335     if (AMEDIA_OK == setExtractorDataSource(extractor, srcFp)) {
336         isPass = AMediaExtractor_getSampleTrackIndex(extractor) == -1;
337         if (!isPass) ALOGE("error: received valid track index without any active tracks");
338     }
339     AMediaExtractor_delete(extractor);
340     if (srcFp) fclose(srcFp);
341     env->ReleaseStringUTFChars(jsrcPath, csrcPath);
342     return static_cast<jboolean>(isPass);
343 }
344 
nativeTestGetTrackFormatBeforeSetDataSource(JNIEnv *,jobject)345 static jboolean nativeTestGetTrackFormatBeforeSetDataSource(JNIEnv*, jobject) {
346     bool isPass = true;
347     AMediaExtractor* extractor = AMediaExtractor_new();
348     AMediaFormat* empty = AMediaFormat_new();
349     AMediaFormat* format = AMediaExtractor_getTrackFormat(extractor, 0);
350     if (format == nullptr ||
351         strcmp(AMediaFormat_toString(empty), AMediaFormat_toString(format)) != 0) {
352         isPass = false;
353         ALOGE("error: getTrackFormat before setDataSource yields unexpected result");
354     }
355     if (format) AMediaFormat_delete(format);
356     AMediaFormat_delete(empty);
357     AMediaExtractor_delete(extractor);
358     return static_cast<jboolean>(isPass);
359 }
360 
nativeTestGetTrackFormatForInvalidIndex(JNIEnv * env,jobject,jstring jsrcPath)361 static jboolean nativeTestGetTrackFormatForInvalidIndex(JNIEnv* env, jobject, jstring jsrcPath) {
362     bool isPass = true;
363     const char* csrcPath = env->GetStringUTFChars(jsrcPath, nullptr);
364     AMediaExtractor* extractor = AMediaExtractor_new();
365     FILE* srcFp = fopen(csrcPath, "rbe");
366     if (AMEDIA_OK == setExtractorDataSource(extractor, srcFp)) {
367         AMediaFormat* format =
368                 AMediaExtractor_getTrackFormat(extractor, AMediaExtractor_getTrackCount(extractor));
369         AMediaFormat* empty = AMediaFormat_new();
370         if (format == nullptr ||
371             strcmp(AMediaFormat_toString(empty), AMediaFormat_toString(format)) != 0) {
372             isPass = false;
373             ALOGE("error: getTrackFormat for out of bound track index %zu yields unexpected result",
374                   AMediaExtractor_getTrackCount(extractor));
375         }
376         if (format) AMediaFormat_delete(format);
377         AMediaFormat_delete(empty);
378     } else {
379         isPass = false;
380     }
381     AMediaExtractor_delete(extractor);
382     if (srcFp) fclose(srcFp);
383     env->ReleaseStringUTFChars(jsrcPath, csrcPath);
384     return static_cast<jboolean>(isPass);
385 }
386 
nativeTestReadSampleDataBeforeSetDataSource(JNIEnv *,jobject)387 static jboolean nativeTestReadSampleDataBeforeSetDataSource(JNIEnv*, jobject) {
388     AMediaExtractor* extractor = AMediaExtractor_new();
389     const int maxSampleSize = 512 * 1024;
390     auto buffer = new uint8_t[maxSampleSize];
391     bool isPass = AMediaExtractor_readSampleData(extractor, buffer, maxSampleSize) < 0;
392     if (!isPass) ALOGE("error: readSampleData return non-negative readBytes before setDataSource");
393     delete[] buffer;
394     AMediaExtractor_delete(extractor);
395     return static_cast<jboolean>(isPass);
396 }
397 
nativeTestReadSampleDataBeforeSelectTrack(JNIEnv * env,jobject,jstring jsrcPath)398 static jboolean nativeTestReadSampleDataBeforeSelectTrack(JNIEnv* env, jobject, jstring jsrcPath) {
399     bool isPass = false;
400     const char* csrcPath = env->GetStringUTFChars(jsrcPath, nullptr);
401     AMediaExtractor* extractor = AMediaExtractor_new();
402     const int maxSampleSize = (512 * 1024);
403     auto buffer = new uint8_t[maxSampleSize];
404     FILE* srcFp = fopen(csrcPath, "rbe");
405     if (AMEDIA_OK == setExtractorDataSource(extractor, srcFp)) {
406         isPass = AMediaExtractor_readSampleData(extractor, buffer, maxSampleSize) < 0;
407         if (!isPass) {
408             ALOGE("error: readSampleData returns non-negative readBytes with out active tracks");
409         }
410     }
411     delete[] buffer;
412     AMediaExtractor_delete(extractor);
413     if (srcFp) fclose(srcFp);
414     env->ReleaseStringUTFChars(jsrcPath, csrcPath);
415     return static_cast<jboolean>(isPass);
416 }
417 
nativeTestIfNullLocationIsRejectedBySetDataSource(JNIEnv *,jobject)418 static jboolean nativeTestIfNullLocationIsRejectedBySetDataSource(JNIEnv*, jobject) {
419     AMediaExtractor* extractor = AMediaExtractor_new();
420     bool isPass = AMediaExtractor_setDataSource(extractor, nullptr) != AMEDIA_OK;
421     if (!isPass) ALOGE("error: setDataSource succeeds with null location");
422     AMediaExtractor_delete(extractor);
423     return static_cast<jboolean>(isPass);
424 }
425 
registerAndroidMediaV2CtsExtractorUnitTestApi(JNIEnv * env)426 int registerAndroidMediaV2CtsExtractorUnitTestApi(JNIEnv* env) {
427     const JNINativeMethod methodTable[] = {
428             {"nativeTestGetTrackCountBeforeSetDataSource", "()Z",
429              (void*)nativeTestGetTrackCountBeforeSetDataSource},
430             {"nativeTestSelectTrackBeforeSetDataSource", "()Z",
431              (void*)nativeTestSelectTrackBeforeSetDataSource},
432             {"nativeTestSelectTrackForInvalidIndex", "(Ljava/lang/String;)Z",
433              (void*)nativeTestSelectTrackForInvalidIndex},
434             {"nativeTestIdempotentSelectTrack", "(Ljava/lang/String;)Z",
435              (void*)nativeTestIdempotentSelectTrack},
436             {"nativeTestUnselectTrackBeforeSetDataSource", "()Z",
437              (void*)nativeTestUnselectTrackBeforeSetDataSource},
438             {"nativeTestUnselectTrackForInvalidIndex", "(Ljava/lang/String;)Z",
439              (void*)nativeTestUnselectTrackForInvalidIndex},
440             {"nativeTestUnselectTrackForUnSelectedTrackIndex", "(Ljava/lang/String;)Z",
441              (void*)nativeTestUnselectTrackForUnSelectedTrackIndex},
442             {"nativeTestIdempotentUnselectTrack", "(Ljava/lang/String;)Z",
443              (void*)nativeTestIdempotentUnselectTrack},
444             {"nativeTestSeekToBeforeSetDataSource", "()Z",
445              (void*)nativeTestSeekToBeforeSetDataSource},
446             {"nativeTestSeekToBeforeSelectTrack", "(Ljava/lang/String;)Z",
447              (void*)nativeTestSeekToBeforeSelectTrack},
448             {"nativeTestGetCachedDurationBeforeSetDataSource", "()Z",
449              (void*)nativeTestGetCachedDurationBeforeSetDataSource},
450             {"nativeTestIfGetFileFormatSucceedsBeforeSetDataSource", "()Z",
451              (void*)nativeTestIfGetFileFormatSucceedsBeforeSetDataSource},
452             {"nativeTestAdvanceBeforeSetDataSource", "()Z",
453              (void*)nativeTestAdvanceBeforeSetDataSource},
454             {"nativeTestAdvanceBeforeSelectTrack", "(Ljava/lang/String;)Z",
455              (void*)nativeTestAdvanceBeforeSelectTrack},
456             {"nativeTestGetSampleFlagsBeforeSetDataSource", "()Z",
457              (void*)nativeTestGetSampleFlagsBeforeSetDataSource},
458             {"nativeTestGetSampleFlagsBeforeSelectTrack", "(Ljava/lang/String;)Z",
459              (void*)nativeTestGetSampleFlagsBeforeSelectTrack},
460             {"nativeTestGetSampleTimeBeforeSetDataSource", "()Z",
461              (void*)nativeTestGetSampleTimeBeforeSetDataSource},
462             {"nativeTestGetSampleTimeBeforeSelectTrack", "(Ljava/lang/String;)Z",
463              (void*)nativeTestGetSampleTimeBeforeSelectTrack},
464             {"nativeTestGetSampleSizeBeforeSetDataSource", "()Z",
465              (void*)nativeTestGetSampleSizeBeforeSetDataSource},
466             {"nativeTestGetSampleSizeBeforeSelectTrack", "(Ljava/lang/String;)Z",
467              (void*)nativeTestGetSampleSizeBeforeSelectTrack},
468             {"nativeTestIfGetSampleFormatBeforeSetDataSource", "()Z",
469              (void*)nativeTestIfGetSampleFormatBeforeSetDataSource},
470             {"nativeTestIfGetSampleFormatBeforeSelectTrack", "(Ljava/lang/String;)Z",
471              (void*)nativeTestIfGetSampleFormatBeforeSelectTrack},
472             {"nativeTestGetSampleTrackIndexBeforeSetDataSource", "()Z",
473              (void*)nativeTestGetSampleTrackIndexBeforeSetDataSource},
474             {"nativeTestGetSampleTrackIndexBeforeSelectTrack", "(Ljava/lang/String;)Z",
475              (void*)nativeTestGetSampleTrackIndexBeforeSelectTrack},
476             {"nativeTestGetTrackFormatBeforeSetDataSource", "()Z",
477              (void*)nativeTestGetTrackFormatBeforeSetDataSource},
478             {"nativeTestGetTrackFormatForInvalidIndex", "(Ljava/lang/String;)Z",
479              (void*)nativeTestGetTrackFormatForInvalidIndex},
480             {"nativeTestReadSampleDataBeforeSetDataSource", "()Z",
481              (void*)nativeTestReadSampleDataBeforeSetDataSource},
482             {"nativeTestReadSampleDataBeforeSelectTrack", "(Ljava/lang/String;)Z",
483              (void*)nativeTestReadSampleDataBeforeSelectTrack},
484             {"nativeTestIfNullLocationIsRejectedBySetDataSource", "()Z",
485              (void*)nativeTestIfNullLocationIsRejectedBySetDataSource},
486     };
487     jclass c = env->FindClass("android/mediav2/cts/ExtractorUnitTest$TestApiNative");
488     return env->RegisterNatives(c, methodTable, sizeof(methodTable) / sizeof(JNINativeMethod));
489 }
490