1 /*
2  * Copyright 2015 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.media.cts;
18 
19 import com.android.cts.media.R;
20 
21 import android.content.res.AssetFileDescriptor;
22 import android.content.res.Resources;
23 import android.media.MediaDataSource;
24 import android.media.MediaExtractor;
25 import android.test.AndroidTestCase;
26 
27 import java.io.IOException;
28 import java.nio.ByteBuffer;
29 
30 public class MediaExtractorTest extends AndroidTestCase {
31     protected Resources mResources;
32     protected MediaExtractor mExtractor;
33 
34     @Override
setUp()35     protected void setUp() throws Exception {
36         super.setUp();
37         mResources = getContext().getResources();
38         mExtractor = new MediaExtractor();
39     }
40 
41     @Override
tearDown()42     protected void tearDown() throws Exception {
43         super.tearDown();
44         mExtractor.release();
45     }
46 
getDataSourceFor(int resid)47     protected TestMediaDataSource getDataSourceFor(int resid) throws Exception {
48         AssetFileDescriptor afd = mResources.openRawResourceFd(resid);
49         return TestMediaDataSource.fromAssetFd(afd);
50     }
51 
setDataSource(int resid)52     protected TestMediaDataSource setDataSource(int resid) throws Exception {
53         TestMediaDataSource ds = getDataSourceFor(resid);
54         mExtractor.setDataSource(ds);
55         return ds;
56     }
57 
testNullMediaDataSourceIsRejected()58     public void testNullMediaDataSourceIsRejected() throws Exception {
59         try {
60             mExtractor.setDataSource((MediaDataSource)null);
61             fail("Expected IllegalArgumentException.");
62         } catch (IllegalArgumentException ex) {
63             // Expected, test passed.
64         }
65     }
66 
testMediaDataSourceIsClosedOnRelease()67     public void testMediaDataSourceIsClosedOnRelease() throws Exception {
68         TestMediaDataSource dataSource = setDataSource(R.raw.testvideo);
69         mExtractor.release();
70         assertTrue(dataSource.isClosed());
71     }
72 
testExtractorFailsIfMediaDataSourceThrows()73     public void testExtractorFailsIfMediaDataSourceThrows() throws Exception {
74         TestMediaDataSource dataSource = getDataSourceFor(R.raw.testvideo);
75         dataSource.throwFromReadAt();
76         try {
77             mExtractor.setDataSource(dataSource);
78             fail("Expected IOException.");
79         } catch (IOException e) {
80             // Expected.
81         }
82     }
83 
testExtractorFailsIfMediaDataSourceReturnsAnError()84     public void testExtractorFailsIfMediaDataSourceReturnsAnError() throws Exception {
85         TestMediaDataSource dataSource = getDataSourceFor(R.raw.testvideo);
86         dataSource.returnFromReadAt(-2);
87         try {
88             mExtractor.setDataSource(dataSource);
89             fail("Expected IOException.");
90         } catch (IOException e) {
91             // Expected.
92         }
93     }
94 
95     // Smoke test MediaExtractor reading from a DataSource.
testExtractFromAMediaDataSource()96     public void testExtractFromAMediaDataSource() throws Exception {
97         TestMediaDataSource dataSource = setDataSource(R.raw.testvideo);
98         // 1MB is enough for any sample.
99         final ByteBuffer buf = ByteBuffer.allocate(1024*1024);
100         final int trackCount = mExtractor.getTrackCount();
101 
102         for (int i = 0; i < trackCount; i++) {
103             mExtractor.selectTrack(i);
104         }
105 
106         for (int i = 0; i < trackCount; i++) {
107             assertTrue(mExtractor.readSampleData(buf, 0) > 0);
108             assertTrue(mExtractor.advance());
109         }
110     }
111 }
112