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 package android.mediav2.cts;
18 
19 import android.content.res.AssetFileDescriptor;
20 import android.media.MediaDataSource;
21 import android.media.MediaExtractor;
22 import android.os.Build;
23 import android.os.ParcelFileDescriptor;
24 import android.util.Log;
25 
26 import androidx.test.filters.SmallTest;
27 
28 import org.junit.Ignore;
29 import org.junit.Rule;
30 import org.junit.Test;
31 import org.junit.experimental.runners.Enclosed;
32 import org.junit.rules.TestName;
33 import org.junit.runner.RunWith;
34 
35 import com.android.compatibility.common.util.ApiLevelUtil;
36 
37 import java.io.File;
38 import java.io.FileDescriptor;
39 import java.io.FileOutputStream;
40 import java.io.IOException;
41 import java.nio.ByteBuffer;
42 
43 import static org.junit.Assert.assertTrue;
44 import static org.junit.Assert.fail;
45 
46 @RunWith(Enclosed.class)
47 public class ExtractorUnitTest {
48     private static final int MAX_SAMPLE_SIZE = 4 * 1024 * 1024;
49     private static final String mInpPrefix = WorkDir.getMediaDirString();
50     private static final String mInpMedia = "ForBiggerEscapes.mp4";
51     private static final String TAG = "ExtractorUnitTest";
52 
53     private static boolean sIsAtLeastS = ApiLevelUtil.isAtLeast(Build.VERSION_CODES.S);
54 
55     @SmallTest
56     public static class TestApi {
57         @Rule
58         public TestName testName = new TestName();
59 
60         @Test
testGetTrackCountBeforeSetDataSource()61         public void testGetTrackCountBeforeSetDataSource() {
62             MediaExtractor extractor = new MediaExtractor();
63             try {
64                 assertTrue("received valid trackCount before setDataSource",
65                         extractor.getTrackCount() <= 0);
66             } catch (Exception e) {
67                 // expected
68             } finally {
69                 extractor.release();
70             }
71         }
72 
73         @Test
testGetTrackCountAfterRelease()74         public void testGetTrackCountAfterRelease() {
75             MediaExtractor extractor = new MediaExtractor();
76             extractor.release();
77             try {
78                 extractor.getTrackCount();
79                 fail("getTrackCount succeeds after release");
80             } catch (Exception e) {
81                 // expected
82             }
83         }
84 
85         @Test
testSelectTrackBeforeSetDataSource()86         public void testSelectTrackBeforeSetDataSource() {
87             MediaExtractor extractor = new MediaExtractor();
88             try {
89                 extractor.selectTrack(0);
90                 fail("selectTrack succeeds before setDataSource");
91             } catch (Exception e) {
92                 // expected
93             } finally {
94                 extractor.release();
95             }
96         }
97 
98         @Test
testSelectTrackForInvalidIndex()99         public void testSelectTrackForInvalidIndex() throws IOException {
100             MediaExtractor extractor = new MediaExtractor();
101             Preconditions.assertTestFileExists(mInpPrefix + mInpMedia);
102             extractor.setDataSource(mInpPrefix + mInpMedia);
103             try {
104                 try {
105                     extractor.selectTrack(-1);
106                     fail("selectTrack succeeds for track index: -1");
107                 } catch (Exception e) {
108                     // expected
109                 }
110                 try {
111                     extractor.selectTrack(extractor.getTrackCount());
112                     fail("selectTrack succeeds for out of bounds track index: " +
113                             extractor.getTrackCount());
114                 } catch (Exception e) {
115                     // expected
116                 }
117             } finally {
118                 extractor.release();
119             }
120         }
121 
122         @Test
testIdempotentSelectTrack()123         public void testIdempotentSelectTrack() throws IOException {
124             MediaExtractor extractor = new MediaExtractor();
125             Preconditions.assertTestFileExists(mInpPrefix + mInpMedia);
126             extractor.setDataSource(mInpPrefix + mInpMedia);
127             try {
128                 extractor.selectTrack(0);
129                 extractor.selectTrack(0);
130             } catch (Exception e) {
131                 fail("multiple selection of same track has failed");
132             } finally {
133                 extractor.release();
134             }
135         }
136 
137         @Test
testSelectTrackAfterRelease()138         public void testSelectTrackAfterRelease() {
139             MediaExtractor extractor = new MediaExtractor();
140             extractor.release();
141             try {
142                 extractor.selectTrack(0);
143                 fail("selectTrack succeeds after release");
144             } catch (Exception e) {
145                 // expected
146             }
147         }
148 
149         @Test
testUnselectTrackBeforeSetDataSource()150         public void testUnselectTrackBeforeSetDataSource() {
151             MediaExtractor extractor = new MediaExtractor();
152             try {
153                 extractor.unselectTrack(0);
154                 fail("unselectTrack succeeds before setDataSource");
155             } catch (Exception e) {
156                 // expected
157             } finally {
158                 extractor.release();
159             }
160         }
161 
162         @Test
testUnselectTrackForInvalidIndex()163         public void testUnselectTrackForInvalidIndex() throws IOException {
164             MediaExtractor extractor = new MediaExtractor();
165             Preconditions.assertTestFileExists(mInpPrefix + mInpMedia);
166             extractor.setDataSource(mInpPrefix + mInpMedia);
167             try {
168                 try {
169                     extractor.unselectTrack(-1);
170                     fail("unselectTrack succeeds for track index: -1");
171                 } catch (Exception e) {
172                     // expected
173                 }
174                 try {
175                     extractor.unselectTrack(extractor.getTrackCount());
176                     fail("unselectTrack succeeds for out of bounds track index: " +
177                             extractor.getTrackCount());
178                 } catch (Exception e) {
179                     // expected
180                 }
181             } finally {
182                 extractor.release();
183             }
184         }
185 
186         @Test
testUnselectTrackForUnSelectedTrackIndex()187         public void testUnselectTrackForUnSelectedTrackIndex() throws IOException {
188             MediaExtractor extractor = new MediaExtractor();
189             Preconditions.assertTestFileExists(mInpPrefix + mInpMedia);
190             extractor.setDataSource(mInpPrefix + mInpMedia);
191             try {
192                 extractor.unselectTrack(0);
193             } catch (Exception e) {
194                 fail("un-selection of non-selected track has failed");
195             } finally {
196                 extractor.release();
197             }
198         }
199 
200         @Test
testIdempotentUnselectTrack()201         public void testIdempotentUnselectTrack() throws IOException {
202             MediaExtractor extractor = new MediaExtractor();
203             Preconditions.assertTestFileExists(mInpPrefix + mInpMedia);
204             extractor.setDataSource(mInpPrefix + mInpMedia);
205             try {
206                 extractor.selectTrack(0);
207                 extractor.unselectTrack(0);
208                 extractor.unselectTrack(0);
209             } catch (Exception e) {
210                 fail("multiple un-selection of selected track has failed");
211             } finally {
212                 extractor.release();
213             }
214         }
215 
216         @Test
testUnselectTrackAfterRelease()217         public void testUnselectTrackAfterRelease() {
218             MediaExtractor extractor = new MediaExtractor();
219             extractor.release();
220             try {
221                 extractor.unselectTrack(0);
222                 fail("unselectTrack succeeds after release");
223             } catch (Exception e) {
224                 // expected
225             }
226         }
227 
228         @Test
testSeekToBeforeSetDataSource()229         public void testSeekToBeforeSetDataSource() {
230             MediaExtractor extractor = new MediaExtractor();
231             try {
232                 extractor.seekTo(33000, MediaExtractor.SEEK_TO_CLOSEST_SYNC);
233                 if (extractor.getSampleTime() != -1 || extractor.getSampleSize() != -1 ||
234                         extractor.getSampleFlags() != -1) {
235                     fail("seekTo() succeeds before setting data source, returns non-negative " +
236                             "sampleTime / sampleSize / sampleFlags");
237                 }
238             } catch (Exception e) {
239                 // expected
240             } finally {
241                 extractor.release();
242             }
243         }
244 
245         @Test
testSeekToBeforeSelectTrack()246         public void testSeekToBeforeSelectTrack() throws IOException {
247             MediaExtractor extractor = new MediaExtractor();
248             Preconditions.assertTestFileExists(mInpPrefix + mInpMedia);
249             extractor.setDataSource(mInpPrefix + mInpMedia);
250             try {
251                 extractor.seekTo(33000, MediaExtractor.SEEK_TO_CLOSEST_SYNC);
252                 if (extractor.getSampleTime() != -1 || extractor.getSampleSize() != -1 ||
253                         extractor.getSampleFlags() != -1) {
254                     fail("seekTo() succeeds before selectTrack, returns non-negative " +
255                             "sampleTime / sampleSize / sampleFlags");
256                 }
257             } catch (Exception e) {
258                 // expected
259             } finally {
260                 extractor.release();
261             }
262         }
263 
264         @Test
testSeekToForInvalidMode()265         public void testSeekToForInvalidMode() throws IOException {
266             MediaExtractor extractor = new MediaExtractor();
267             Preconditions.assertTestFileExists(mInpPrefix + mInpMedia);
268             extractor.setDataSource(mInpPrefix + mInpMedia);
269             extractor.selectTrack(0);
270             long pts = 33000;
271             try {
272                 extractor.seekTo(pts, (int) pts);
273                 fail("seekTo() succeeds for invalid mode: " + pts);
274             } catch (Exception e) {
275                 // expected
276             } finally {
277                 extractor.release();
278             }
279         }
280 
281         @Test
testSeekToAfterRelease()282         public void testSeekToAfterRelease() throws IOException {
283             MediaExtractor extractor = new MediaExtractor();
284             Preconditions.assertTestFileExists(mInpPrefix + mInpMedia);
285             extractor.setDataSource(mInpPrefix + mInpMedia);
286             extractor.release();
287             try {
288                 extractor.seekTo(33000, MediaExtractor.SEEK_TO_CLOSEST_SYNC);
289                 fail("seekTo() succeeds after release");
290             } catch (Exception e) {
291                 // expected
292             }
293         }
294 
295         @Test
296         @Ignore("TODO(b/148205432)")
testGetCachedDurationBeforeSetDataSource()297         public void testGetCachedDurationBeforeSetDataSource() {
298             MediaExtractor extractor = new MediaExtractor();
299             try {
300                 assertTrue("received valid cachedDuration before setDataSource",
301                         extractor.getCachedDuration() == -1);
302             } catch (Exception e) {
303                 // expected
304             } finally {
305                 extractor.release();
306             }
307         }
308 
309         @Test
testGetCachedDurationAfterRelease()310         public void testGetCachedDurationAfterRelease() {
311             MediaExtractor extractor = new MediaExtractor();
312             extractor.release();
313             try {
314                 extractor.getCachedDuration();
315                 fail("cachedDuration succeeds after release");
316             } catch (Exception e) {
317                 // expected
318             }
319         }
320 
321         @Test
322         @Ignore("TODO(b/148204634)")
testHasCacheReachedEOSBeforeSetDataSource()323         public void testHasCacheReachedEOSBeforeSetDataSource() {
324             MediaExtractor extractor = new MediaExtractor();
325             try {
326                 assertTrue("unexpected value received from hasCacheReachedEndOfStream before" +
327                         " setDataSource", !extractor.hasCacheReachedEndOfStream());
328             } catch (Exception e) {
329                 // expected
330             } finally {
331                 extractor.release();
332             }
333         }
334 
335         @Test
testHasCacheReachedEOSAfterRelease()336         public void testHasCacheReachedEOSAfterRelease() {
337             MediaExtractor extractor = new MediaExtractor();
338             extractor.release();
339             try {
340                 extractor.hasCacheReachedEndOfStream();
341                 fail("hasCacheReachedEndOfStream succeeds after release");
342             } catch (Exception e) {
343                 // expected
344             }
345         }
346 
347         @Test
testGetMetricsBeforeSetDataSource()348         public void testGetMetricsBeforeSetDataSource() {
349             MediaExtractor extractor = new MediaExtractor();
350             try {
351                 assertTrue("received valid metrics before setDataSource",
352                         extractor.getMetrics() == null);
353             } catch (Exception e) {
354                 // expected
355             } finally {
356                 extractor.release();
357             }
358         }
359 
360         @Test
testGetMetricsAfterRelease()361         public void testGetMetricsAfterRelease() {
362             MediaExtractor extractor = new MediaExtractor();
363             extractor.release();
364             try {
365                 extractor.getMetrics();
366                 fail("getMetrics() succeeds after release");
367             } catch (Exception e) {
368                 // expected
369             }
370         }
371 
372         @Test
testIdempotentRelease()373         public void testIdempotentRelease() {
374             MediaExtractor extractor = new MediaExtractor();
375             try {
376                 extractor.release();
377                 extractor.release();
378             } catch (Exception e) {
379                 fail(e.getMessage());
380             }
381         }
382 
383         @Test
testAdvanceBeforeSetDataSource()384         public void testAdvanceBeforeSetDataSource() {
385             MediaExtractor extractor = new MediaExtractor();
386             try {
387                 assertTrue("advance succeeds before setDataSource", !extractor.advance());
388             } catch (Exception e) {
389                 // expected
390             } finally {
391                 extractor.release();
392             }
393         }
394 
395         @Test
testAdvanceBeforeSelectTrack()396         public void testAdvanceBeforeSelectTrack() throws IOException {
397             MediaExtractor extractor = new MediaExtractor();
398             Preconditions.assertTestFileExists(mInpPrefix + mInpMedia);
399             extractor.setDataSource(mInpPrefix + mInpMedia);
400             try {
401                 assertTrue("advance succeeds without any active tracks", !extractor.advance());
402             } catch (Exception e) {
403                 // expected
404             } finally {
405                 extractor.release();
406             }
407         }
408 
409         @Test
testAdvanceAfterRelease()410         public void testAdvanceAfterRelease() {
411             MediaExtractor extractor = new MediaExtractor();
412             extractor.release();
413             try {
414                 extractor.advance();
415                 fail("advance succeeds after release");
416             } catch (Exception e) {
417                 // expected
418             }
419         }
420 
421         @Test
testGetSampleFlagsBeforeSetDataSource()422         public void testGetSampleFlagsBeforeSetDataSource() {
423             MediaExtractor extractor = new MediaExtractor();
424             try {
425                 assertTrue("received valid sampleFlag before setDataSource",
426                         extractor.getSampleFlags() == -1);
427             } catch (Exception e) {
428                 // expected
429             } finally {
430                 extractor.release();
431             }
432         }
433 
434         @Test
testGetSampleFlagsBeforeSelectTrack()435         public void testGetSampleFlagsBeforeSelectTrack() throws IOException {
436             MediaExtractor extractor = new MediaExtractor();
437             Preconditions.assertTestFileExists(mInpPrefix + mInpMedia);
438             extractor.setDataSource(mInpPrefix + mInpMedia);
439             try {
440                 assertTrue("received valid sampleFlag without any active tracks",
441                         extractor.getSampleFlags() == -1);
442             } catch (Exception e) {
443                 // expected
444             } finally {
445                 extractor.release();
446             }
447         }
448 
449         @Test
testGetSampleFlagsAfterRelease()450         public void testGetSampleFlagsAfterRelease() {
451             MediaExtractor extractor = new MediaExtractor();
452             extractor.release();
453             try {
454                 extractor.getSampleFlags();
455                 fail("getSampleFlags succeeds after release");
456             } catch (Exception e) {
457                 // expected
458             }
459         }
460 
461         @Test
testGetSampleTimeBeforeSetDataSource()462         public void testGetSampleTimeBeforeSetDataSource() {
463             MediaExtractor extractor = new MediaExtractor();
464             try {
465                 assertTrue("received valid sampleTime before setDataSource",
466                         extractor.getSampleTime() == -1);
467             } catch (Exception e) {
468                 // expected
469             } finally {
470                 extractor.release();
471             }
472         }
473 
474         @Test
testGetSampleTimeBeforeSelectTrack()475         public void testGetSampleTimeBeforeSelectTrack() throws IOException {
476             MediaExtractor extractor = new MediaExtractor();
477             Preconditions.assertTestFileExists(mInpPrefix + mInpMedia);
478             extractor.setDataSource(mInpPrefix + mInpMedia);
479             try {
480                 assertTrue("received valid sampleTime without any active tracks",
481                         extractor.getSampleTime() == -1);
482             } catch (Exception e) {
483                 // expected
484             } finally {
485                 extractor.release();
486             }
487         }
488 
489         @Test
testGetSampleTimeAfterRelease()490         public void testGetSampleTimeAfterRelease() {
491             MediaExtractor extractor = new MediaExtractor();
492             extractor.release();
493             try {
494                 extractor.getSampleTime();
495                 fail("getSampleTime succeeds after release");
496             } catch (Exception e) {
497                 // expected
498             }
499         }
500 
501         @Test
testGetSampleSizeBeforeSetDataSource()502         public void testGetSampleSizeBeforeSetDataSource() {
503             MediaExtractor extractor = new MediaExtractor();
504             try {
505                 assertTrue("received valid sampleSize before setDataSource",
506                         extractor.getSampleSize() == -1);
507             } catch (Exception e) {
508                 // expected
509             } finally {
510                 extractor.release();
511             }
512         }
513 
514         @Test
testGetSampleSizeBeforeSelectTrack()515         public void testGetSampleSizeBeforeSelectTrack() throws IOException {
516             MediaExtractor extractor = new MediaExtractor();
517             Preconditions.assertTestFileExists(mInpPrefix + mInpMedia);
518             extractor.setDataSource(mInpPrefix + mInpMedia);
519             try {
520                 assertTrue("received valid sampleSize without any active tracks",
521                         extractor.getSampleSize() == -1);
522             } catch (Exception e) {
523                 // expected
524             } finally {
525                 extractor.release();
526             }
527         }
528 
529         @Test
testGetSampleSizeAfterRelease()530         public void testGetSampleSizeAfterRelease() {
531             MediaExtractor extractor = new MediaExtractor();
532             extractor.release();
533             try {
534                 extractor.getSampleSize();
535                 fail("getSampleSize succeeds after release");
536             } catch (Exception e) {
537                 // expected
538             }
539         }
540 
541         @Test
testGetSampleTrackIndexBeforeSetDataSource()542         public void testGetSampleTrackIndexBeforeSetDataSource() {
543             MediaExtractor extractor = new MediaExtractor();
544             try {
545                 assertTrue("received valid sampleTrackIndex before setDataSource",
546                         extractor.getSampleTrackIndex() == -1);
547             } catch (Exception e) {
548                 // expected
549             } finally {
550                 extractor.release();
551             }
552         }
553 
554         @Test
testGetSampleTrackIndexBeforeSelectTrack()555         public void testGetSampleTrackIndexBeforeSelectTrack() throws IOException {
556             MediaExtractor extractor = new MediaExtractor();
557             Preconditions.assertTestFileExists(mInpPrefix + mInpMedia);
558             extractor.setDataSource(mInpPrefix + mInpMedia);
559             try {
560                 assertTrue("received valid sampleTrackIndex without any active tracks",
561                         extractor.getSampleTrackIndex() == -1);
562             } catch (Exception e) {
563                 // expected
564             } finally {
565                 extractor.release();
566             }
567         }
568 
569         @Test
testGetSampleTrackIndexAfterRelease()570         public void testGetSampleTrackIndexAfterRelease() {
571             MediaExtractor extractor = new MediaExtractor();
572             extractor.release();
573             try {
574                 extractor.getSampleTrackIndex();
575                 fail("getSampleTrackIndex succeeds after release");
576             } catch (Exception e) {
577                 // expected
578             }
579         }
580 
581         @Test
testGetTrackFormatBeforeSetDataSource()582         public void testGetTrackFormatBeforeSetDataSource() {
583             MediaExtractor extractor = new MediaExtractor();
584             try {
585                 extractor.getTrackFormat(0);
586                 fail("getTrackFormat succeeds before setDataSource");
587             } catch (Exception e) {
588                 // expected
589             } finally {
590                 extractor.release();
591             }
592         }
593 
594         @Test
testGetTrackFormatForInvalidIndex()595         public void testGetTrackFormatForInvalidIndex() throws IOException {
596             MediaExtractor extractor = new MediaExtractor();
597             Preconditions.assertTestFileExists(mInpPrefix + mInpMedia);
598             extractor.setDataSource(mInpPrefix + mInpMedia);
599             try {
600                 try {
601                     extractor.getTrackFormat(-1);
602                     fail("getTrackFormat succeeds for track index: -1");
603                 } catch (Exception e) {
604                     // expected
605                 }
606                 try {
607                     extractor.getTrackFormat(extractor.getTrackCount());
608                     fail("getTrackFormat succeeds for out of bounds track index: " +
609                             extractor.getTrackCount());
610                 } catch (Exception e) {
611                     // expected
612                 }
613             } finally {
614                 extractor.release();
615             }
616         }
617 
618         @Test
testGetTrackFormatAfterRelease()619         public void testGetTrackFormatAfterRelease() {
620             MediaExtractor extractor = new MediaExtractor();
621             extractor.release();
622             try {
623                 extractor.getTrackFormat(0);
624                 fail("getTrackFormat succeeds after release");
625             } catch (Exception e) {
626                 // expected
627             }
628         }
629 
630         @Test
testReadSampleDataBeforeSetDataSource()631         public void testReadSampleDataBeforeSetDataSource() {
632             MediaExtractor extractor = new MediaExtractor();
633             ByteBuffer byteBuffer = ByteBuffer.allocate(MAX_SAMPLE_SIZE);
634             try {
635                 assertTrue("readSampleData returns val >= 0 before setDataSource",
636                         extractor.readSampleData(byteBuffer, 0) < 0);
637             } catch (Exception e) {
638                 // expected
639             } finally {
640                 extractor.release();
641             }
642         }
643 
644         @Test
testReadSampleDataBeforeSelectTrack()645         public void testReadSampleDataBeforeSelectTrack() throws IOException {
646             MediaExtractor extractor = new MediaExtractor();
647             ByteBuffer byteBuffer = ByteBuffer.allocate(MAX_SAMPLE_SIZE);
648             Preconditions.assertTestFileExists(mInpPrefix + mInpMedia);
649             extractor.setDataSource(mInpPrefix + mInpMedia);
650             try {
651                 assertTrue("readSampleData returns val >= 0 without any active tracks",
652                         extractor.readSampleData(byteBuffer, 0) < 0);
653             } catch (Exception e) {
654                 // expected
655             } finally {
656                 extractor.release();
657             }
658         }
659 
660         @Test
testIfInvalidOffsetIsRejectedByReadSampleData()661         public void testIfInvalidOffsetIsRejectedByReadSampleData() throws IOException {
662             MediaExtractor extractor = new MediaExtractor();
663             ByteBuffer byteBuffer = ByteBuffer.allocate(MAX_SAMPLE_SIZE);
664             Preconditions.assertTestFileExists(mInpPrefix + mInpMedia);
665             extractor.setDataSource(mInpPrefix + mInpMedia);
666             extractor.selectTrack(0);
667             try {
668                 // readSampleData with negative offset
669                 try {
670                     extractor.readSampleData(byteBuffer, -1);
671                     fail("readSampleData succeeds with negative offset");
672                 } catch (Exception e) {
673                     // expected
674                 }
675 
676                 // readSampleData with byteBuffer's capacity - offset < frame size
677                 int sampleSize = (int) extractor.getSampleSize();
678                 try {
679                     extractor.readSampleData(byteBuffer, MAX_SAMPLE_SIZE - sampleSize + 1);
680                     fail("readSampleData succeeds when available size of byteBuffer is less than " +
681                             "frame size");
682                 } catch (Exception e) {
683                     // expected
684                 }
685             } finally {
686                 extractor.release();
687             }
688         }
689 
690         @Test
testReadSampleDataAfterRelease()691         public void testReadSampleDataAfterRelease() {
692             MediaExtractor extractor = new MediaExtractor();
693             ByteBuffer byteBuffer = ByteBuffer.allocate(MAX_SAMPLE_SIZE);
694             extractor.release();
695             try {
696                 extractor.readSampleData(byteBuffer, 0);
697                 fail("readSampleData succeeds after release");
698             } catch (Exception e) {
699                 // expected
700             }
701         }
702 
703         @Test
testIfInvalidDataSourceIsRejectedBySetDataSource()704         public void testIfInvalidDataSourceIsRejectedBySetDataSource() throws IOException {
705             MediaExtractor extractor = new MediaExtractor();
706             Preconditions.assertTestFileExists(mInpPrefix + mInpMedia);
707             TestMediaDataSource dataSource =
708                     TestMediaDataSource.fromString(mInpPrefix + mInpMedia, false, true);
709             try {
710                 extractor.setDataSource(dataSource);
711                 fail("setDataSource succeeds with malformed media data source");
712             } catch (Exception e) {
713                 // expected
714             } finally {
715                 assertTrue(dataSource.isClosed());
716                 extractor.release();
717             }
718         }
719 
720         @Test
testIfNullFDIsRejectedBySetDataSource()721         public void testIfNullFDIsRejectedBySetDataSource() {
722             MediaExtractor extractor = new MediaExtractor();
723             try {
724                 extractor.setDataSource((FileDescriptor) null);
725                 fail("setDataSource succeeds with null fd");
726             } catch (Exception e) {
727                 // expected
728             } finally {
729                 extractor.release();
730             }
731         }
732 
733         @Test
testIfWriteOnlyAssetFDIsRejectedBySetDataSource()734         public void testIfWriteOnlyAssetFDIsRejectedBySetDataSource() throws IOException {
735             File inpFile = File.createTempFile("ExtractorTestApisetDSAFD", ".in");
736             MediaExtractor extractor = new MediaExtractor();
737             try (ParcelFileDescriptor parcelFD = ParcelFileDescriptor
738                     .open(inpFile, ParcelFileDescriptor.MODE_WRITE_ONLY);
739                  AssetFileDescriptor afd = new AssetFileDescriptor(parcelFD, 0,
740                          AssetFileDescriptor.UNKNOWN_LENGTH)) {
741                 extractor.setDataSource(afd);
742                 fail("setDataSource succeeds write only afd");
743             } catch (Exception e) {
744                 // expected
745             } finally {
746                 extractor.release();
747             }
748             inpFile.delete();
749         }
750 
751         @Test
testIfWriteOnlyFDIsRejectedBySetDataSource()752         public void testIfWriteOnlyFDIsRejectedBySetDataSource() throws IOException {
753             MediaExtractor extractor = new MediaExtractor();
754             File inpFile = File.createTempFile("ExtractorTestApisetDSFD", ".in");
755             try (FileOutputStream fOut = new FileOutputStream(inpFile)) {
756                 extractor.setDataSource(fOut.getFD());
757                 fail("setDataSource succeeds write only fd");
758             } catch (Exception e) {
759                 // expected
760             } finally {
761                 extractor.release();
762             }
763             inpFile.delete();
764         }
765 
766         @Test
testIfNullMediaDataSourceIsRejectedBySetDataSource()767         public void testIfNullMediaDataSourceIsRejectedBySetDataSource() {
768             MediaExtractor extractor = new MediaExtractor();
769             try {
770                 extractor.setDataSource((MediaDataSource) null);
771                 fail("setDataSource succeeds with null data source");
772             } catch (Exception e) {
773                 // expected
774             } finally {
775                 extractor.release();
776             }
777         }
778 
779         @Test
testIfNullFileIsRejectedBySetDataSource()780         public void testIfNullFileIsRejectedBySetDataSource() {
781             MediaExtractor extractor = new MediaExtractor();
782             try {
783                 extractor.setDataSource((String) null);
784                 fail("setDataSource succeeds with null file path");
785             } catch (Exception e) {
786                 // expected
787             } finally {
788                 extractor.release();
789             }
790         }
791 
792         @Test
testIfNullAssetFDIsRejectedBySetDataSource()793         public void testIfNullAssetFDIsRejectedBySetDataSource() {
794             MediaExtractor extractor = new MediaExtractor();
795             try {
796                 extractor.setDataSource((AssetFileDescriptor) null);
797                 fail("setDataSource succeeds with null asset fd");
798             } catch (Exception e) {
799                 // expected
800             } finally {
801                 extractor.release();
802             }
803         }
804     }
805 
806     @SmallTest
807     public static class TestApiNative {
808         @Rule
809         public TestName testName = new TestName();
810 
811         static {
812             System.loadLibrary("ctsmediav2extractor_jni");
813         }
814 
815         @Test
testGetTrackCountBeforeSetDataSource()816         public void testGetTrackCountBeforeSetDataSource() {
817             assertTrue(nativeTestGetTrackCountBeforeSetDataSource());
818         }
nativeTestGetTrackCountBeforeSetDataSource()819         private native boolean nativeTestGetTrackCountBeforeSetDataSource();
820 
821         @Test
testSelectTrackBeforeSetDataSource()822         public void testSelectTrackBeforeSetDataSource() {
823             assertTrue(nativeTestSelectTrackBeforeSetDataSource());
824         }
nativeTestSelectTrackBeforeSetDataSource()825         private native boolean nativeTestSelectTrackBeforeSetDataSource();
826 
827         @Test
testSelectTrackForInvalidIndex()828         public void testSelectTrackForInvalidIndex() {
829             Preconditions.assertTestFileExists(mInpPrefix + mInpMedia);
830             assertTrue(nativeTestSelectTrackForInvalidIndex(mInpPrefix + mInpMedia));
831         }
nativeTestSelectTrackForInvalidIndex(String srcPath)832         private native boolean nativeTestSelectTrackForInvalidIndex(String srcPath);
833 
834         @Test
testIdempotentSelectTrack()835         public void testIdempotentSelectTrack() {
836             Preconditions.assertTestFileExists(mInpPrefix + mInpMedia);
837             assertTrue(nativeTestIdempotentSelectTrack(mInpPrefix + mInpMedia));
838         }
nativeTestIdempotentSelectTrack(String srcPath)839         private native boolean nativeTestIdempotentSelectTrack(String srcPath);
840 
841         @Test
testUnselectTrackBeforeSetDataSource()842         public void testUnselectTrackBeforeSetDataSource() {
843             assertTrue(nativeTestUnselectTrackBeforeSetDataSource());
844         }
nativeTestUnselectTrackBeforeSetDataSource()845         private native boolean nativeTestUnselectTrackBeforeSetDataSource();
846 
847         @Test
testUnselectTrackForInvalidIndex()848         public void testUnselectTrackForInvalidIndex() {
849             Preconditions.assertTestFileExists(mInpPrefix + mInpMedia);
850             assertTrue(nativeTestUnselectTrackForInvalidIndex(mInpPrefix + mInpMedia));
851         }
nativeTestUnselectTrackForInvalidIndex(String srcPath)852         private native boolean nativeTestUnselectTrackForInvalidIndex(String srcPath);
853 
854         @Test
testUnselectTrackForUnSelectedTrackIndex()855         public void testUnselectTrackForUnSelectedTrackIndex() {
856             Preconditions.assertTestFileExists(mInpPrefix + mInpMedia);
857             assertTrue(nativeTestUnselectTrackForUnSelectedTrackIndex(mInpPrefix + mInpMedia));
858         }
nativeTestUnselectTrackForUnSelectedTrackIndex(String srcPath)859         private native boolean nativeTestUnselectTrackForUnSelectedTrackIndex(String srcPath);
860 
861         @Test
testIdempotentUnselectTrack()862         public void testIdempotentUnselectTrack() {
863             Preconditions.assertTestFileExists(mInpPrefix + mInpMedia);
864             assertTrue(nativeTestIdempotentUnselectTrack(mInpPrefix + mInpMedia));
865         }
nativeTestIdempotentUnselectTrack(String srcPath)866         private native boolean nativeTestIdempotentUnselectTrack(String srcPath);
867 
868         @Test
testSeekToBeforeSetDataSource()869         public void testSeekToBeforeSetDataSource() {
870             assertTrue(nativeTestSeekToBeforeSetDataSource());
871         }
nativeTestSeekToBeforeSetDataSource()872         private native boolean nativeTestSeekToBeforeSetDataSource();
873 
874         @Test
testSeekToBeforeSelectTrack()875         public void testSeekToBeforeSelectTrack() {
876             Preconditions.assertTestFileExists(mInpPrefix + mInpMedia);
877             assertTrue(nativeTestSeekToBeforeSelectTrack(mInpPrefix + mInpMedia));
878         }
nativeTestSeekToBeforeSelectTrack(String srcPath)879         private native boolean nativeTestSeekToBeforeSelectTrack(String srcPath);
880 
881         @Test
882         @Ignore("TODO(b/148205432)")
testGetCachedDurationBeforeSetDataSource()883         public void testGetCachedDurationBeforeSetDataSource() {
884             assertTrue(nativeTestGetCachedDurationBeforeSetDataSource());
885         }
nativeTestGetCachedDurationBeforeSetDataSource()886         private native boolean nativeTestGetCachedDurationBeforeSetDataSource();
887 
888         @Test
testIfGetFileFormatSucceedsBeforeSetDataSource()889         public void testIfGetFileFormatSucceedsBeforeSetDataSource() {
890             assertTrue(nativeTestIfGetFileFormatSucceedsBeforeSetDataSource());
891         }
nativeTestIfGetFileFormatSucceedsBeforeSetDataSource()892         private native boolean nativeTestIfGetFileFormatSucceedsBeforeSetDataSource();
893 
894         @Test
testAdvanceBeforeSetDataSource()895         public void testAdvanceBeforeSetDataSource() {
896             assertTrue(nativeTestAdvanceBeforeSetDataSource());
897         }
nativeTestAdvanceBeforeSetDataSource()898         private native boolean nativeTestAdvanceBeforeSetDataSource();
899 
900         @Test
testAdvanceBeforeSelectTrack()901         public void testAdvanceBeforeSelectTrack() {
902             Preconditions.assertTestFileExists(mInpPrefix + mInpMedia);
903             assertTrue(nativeTestAdvanceBeforeSelectTrack(mInpPrefix + mInpMedia));
904         }
nativeTestAdvanceBeforeSelectTrack(String srcPath)905         private native boolean nativeTestAdvanceBeforeSelectTrack(String srcPath);
906 
907         @Test
testGetSampleFlagsBeforeSetDataSource()908         public void testGetSampleFlagsBeforeSetDataSource() {
909             assertTrue(nativeTestGetSampleFlagsBeforeSetDataSource());
910         }
nativeTestGetSampleFlagsBeforeSetDataSource()911         private native boolean nativeTestGetSampleFlagsBeforeSetDataSource();
912 
913         @Test
testGetSampleFlagsBeforeSelectTrack()914         public void testGetSampleFlagsBeforeSelectTrack() {
915             Preconditions.assertTestFileExists(mInpPrefix + mInpMedia);
916             assertTrue(nativeTestGetSampleFlagsBeforeSelectTrack(mInpPrefix + mInpMedia));
917         }
nativeTestGetSampleFlagsBeforeSelectTrack(String srcPath)918         private native boolean nativeTestGetSampleFlagsBeforeSelectTrack(String srcPath);
919 
920         @Test
testGetSampleTimeBeforeSetDataSource()921         public void testGetSampleTimeBeforeSetDataSource() {
922             assertTrue(nativeTestGetSampleTimeBeforeSetDataSource());
923         }
nativeTestGetSampleTimeBeforeSetDataSource()924         private native boolean nativeTestGetSampleTimeBeforeSetDataSource();
925 
926         @Test
testGetSampleTimeBeforeSelectTrack()927         public void testGetSampleTimeBeforeSelectTrack() {
928             Preconditions.assertTestFileExists(mInpPrefix + mInpMedia);
929             assertTrue(nativeTestGetSampleTimeBeforeSelectTrack(mInpPrefix + mInpMedia));
930         }
nativeTestGetSampleTimeBeforeSelectTrack(String srcPath)931         private native boolean nativeTestGetSampleTimeBeforeSelectTrack(String srcPath);
932 
933         @Test
testGetSampleSizeBeforeSetDataSource()934         public void testGetSampleSizeBeforeSetDataSource() {
935             assertTrue(nativeTestGetSampleSizeBeforeSetDataSource());
936         }
nativeTestGetSampleSizeBeforeSetDataSource()937         private native boolean nativeTestGetSampleSizeBeforeSetDataSource();
938 
939         @Test
testGetSampleSizeBeforeSelectTrack()940         public void testGetSampleSizeBeforeSelectTrack() {
941             Preconditions.assertTestFileExists(mInpPrefix + mInpMedia);
942             assertTrue(nativeTestGetSampleSizeBeforeSelectTrack(mInpPrefix + mInpMedia));
943         }
nativeTestGetSampleSizeBeforeSelectTrack(String srcPath)944         private native boolean nativeTestGetSampleSizeBeforeSelectTrack(String srcPath);
945 
946         @Test
testIfGetSampleFormatBeforeSetDataSource()947         public void testIfGetSampleFormatBeforeSetDataSource() {
948             assertTrue(nativeTestIfGetSampleFormatBeforeSetDataSource());
949         }
nativeTestIfGetSampleFormatBeforeSetDataSource()950         private native boolean nativeTestIfGetSampleFormatBeforeSetDataSource();
951 
952         @Test
testIfGetSampleFormatBeforeSelectTrack()953         public void testIfGetSampleFormatBeforeSelectTrack() {
954             Preconditions.assertTestFileExists(mInpPrefix + mInpMedia);
955             assertTrue(nativeTestIfGetSampleFormatBeforeSelectTrack(mInpPrefix + mInpMedia));
956         }
nativeTestIfGetSampleFormatBeforeSelectTrack(String srcPath)957         private native boolean nativeTestIfGetSampleFormatBeforeSelectTrack(String srcPath);
958 
959         @Test
testGetSampleTrackIndexBeforeSetDataSource()960         public void testGetSampleTrackIndexBeforeSetDataSource() {
961             assertTrue(nativeTestGetSampleTrackIndexBeforeSetDataSource());
962         }
nativeTestGetSampleTrackIndexBeforeSetDataSource()963         private native boolean nativeTestGetSampleTrackIndexBeforeSetDataSource();
964 
965         @Test
testGetSampleTrackIndexBeforeSelectTrack()966         public void testGetSampleTrackIndexBeforeSelectTrack() {
967             Preconditions.assertTestFileExists(mInpPrefix + mInpMedia);
968             assertTrue(
969                     nativeTestGetSampleTrackIndexBeforeSelectTrack(mInpPrefix + mInpMedia));
970         }
nativeTestGetSampleTrackIndexBeforeSelectTrack(String srcPath)971         private native boolean nativeTestGetSampleTrackIndexBeforeSelectTrack(String srcPath);
972 
973         @Test
testGetTrackFormatBeforeSetDataSource()974         public void testGetTrackFormatBeforeSetDataSource() {
975             assertTrue(nativeTestGetTrackFormatBeforeSetDataSource());
976         }
nativeTestGetTrackFormatBeforeSetDataSource()977         private native boolean nativeTestGetTrackFormatBeforeSetDataSource();
978 
979         @Test
testGetTrackFormatForInvalidIndex()980         public void testGetTrackFormatForInvalidIndex() {
981             Preconditions.assertTestFileExists(mInpPrefix + mInpMedia);
982             assertTrue(nativeTestGetTrackFormatForInvalidIndex(mInpPrefix + mInpMedia));
983         }
nativeTestGetTrackFormatForInvalidIndex(String srcPath)984         private native boolean nativeTestGetTrackFormatForInvalidIndex(String srcPath);
985 
986         @Test
testReadSampleDataBeforeSetDataSource()987         public void testReadSampleDataBeforeSetDataSource() {
988             assertTrue(nativeTestReadSampleDataBeforeSetDataSource());
989         }
nativeTestReadSampleDataBeforeSetDataSource()990         private native boolean nativeTestReadSampleDataBeforeSetDataSource();
991 
992         @Test
testReadSampleDataBeforeSelectTrack()993         public void testReadSampleDataBeforeSelectTrack() {
994             Preconditions.assertTestFileExists(mInpPrefix + mInpMedia);
995             assertTrue(nativeTestReadSampleDataBeforeSelectTrack(mInpPrefix + mInpMedia));
996         }
nativeTestReadSampleDataBeforeSelectTrack(String srcPath)997         private native boolean nativeTestReadSampleDataBeforeSelectTrack(String srcPath);
998 
999         @Test
testIfNullLocationIsRejectedBySetDataSource()1000         public void testIfNullLocationIsRejectedBySetDataSource() {
1001             assertTrue(nativeTestIfNullLocationIsRejectedBySetDataSource());
1002         }
nativeTestIfNullLocationIsRejectedBySetDataSource()1003         private native boolean nativeTestIfNullLocationIsRejectedBySetDataSource();
1004 
1005         @Test
testVideoSampleFileOffsetByGetSampleFormat()1006         public void testVideoSampleFileOffsetByGetSampleFormat() {
1007             if (!sIsAtLeastS) {
1008                 Log.d(TAG, "testVideoSampleFileOffsetByGetSampleFormat requires Android 12");
1009                 return;
1010             }
1011             Preconditions.assertTestFileExists(mInpPrefix + mInpMedia);
1012             assertTrue(nativeTestVideoSampleFileOffsetByGetSampleFormat(mInpPrefix + mInpMedia));
1013         }
nativeTestVideoSampleFileOffsetByGetSampleFormat(String srcPath)1014         private native boolean nativeTestVideoSampleFileOffsetByGetSampleFormat(String srcPath);
1015 
1016         @Test
testAudioSampleFileOffsetByGetSampleFormat()1017         public void testAudioSampleFileOffsetByGetSampleFormat() {
1018             if (!sIsAtLeastS) {
1019                 Log.d(TAG, "testAudioSampleFileOffsetByGetSampleFormat requires Android 12");
1020                 return;
1021             }
1022             Preconditions.assertTestFileExists(mInpPrefix + mInpMedia);
1023             assertTrue(nativeTestAudioSampleFileOffsetByGetSampleFormat(mInpPrefix + mInpMedia));
1024         }
nativeTestAudioSampleFileOffsetByGetSampleFormat(String srcPath)1025         private native boolean nativeTestAudioSampleFileOffsetByGetSampleFormat(String srcPath);
1026     }
1027 }
1028