1 /*
2  * Copyright (C) 2008 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 com.android.mediaframeworktest.functional;
18 
19 import android.media.MediaMetadataRetriever;
20 import android.test.AndroidTestCase;
21 import android.util.Log;
22 
23 import androidx.test.filters.MediumTest;
24 
25 import com.android.mediaframeworktest.MediaNames;
26 import com.android.mediaframeworktest.MediaProfileReader;
27 
28 import java.io.IOException;
29 
30 /** This metadata test suite test the basic functionality of the MediaMetadataRetriever */
31 public class MediaMetadataTest extends AndroidTestCase {
32 
33     private static final String TAG = "MediaMetadataTest";
34 
35     public enum METADATA_EXPECTEDRESULT {
36         FILE_PATH,
37         CD_TRACK,
38         ALBUM,
39         ARTIST,
40         AUTHOR,
41         COMPOSER,
42         DATE,
43         GENRE,
44         TITLE,
45         YEAR,
46         DURATION,
47         NUM_TRACKS,
48         WRITER
49     }
50 
51     public enum MP3_TEST_FILE {
52         ID3V1V2,
53         ID3V2,
54         ID3V1
55     }
56 
57     public static METADATA_EXPECTEDRESULT meta;
58     public static MP3_TEST_FILE mp3_test_file;
59 
60     @MediumTest
testID3V1V2Metadata()61     public static void testID3V1V2Metadata() throws Exception {
62         validateMetatData(mp3_test_file.ID3V1V2.ordinal(), MediaNames.META_DATA_MP3);
63     }
64 
65     @MediumTest
testID3V2Metadata()66     public static void testID3V2Metadata() throws Exception {
67         validateMetatData(mp3_test_file.ID3V2.ordinal(), MediaNames.META_DATA_MP3);
68     }
69 
70     @MediumTest
testID3V1Metadata()71     public static void testID3V1Metadata() throws Exception {
72         validateMetatData(mp3_test_file.ID3V1.ordinal(), MediaNames.META_DATA_MP3);
73     }
74 
validateMetatData(int fileIndex, String[][] metadataFile)75     private static void validateMetatData(int fileIndex, String[][] metadataFile)
76             throws IOException {
77         Log.v(TAG, "filePath = " + metadataFile[fileIndex][0]);
78         if ((metadataFile[fileIndex][0].endsWith("wma") && !MediaProfileReader.getWMAEnable())
79                 || (metadataFile[fileIndex][0].endsWith("wmv")
80                         && !MediaProfileReader.getWMVEnable())) {
81             return;
82         }
83         String value = null;
84         MediaMetadataRetriever retriever = new MediaMetadataRetriever();
85         try {
86             retriever.setDataSource(metadataFile[fileIndex][0]);
87         } catch (Exception e) {
88             Log.v(TAG, "Failed: " + metadataFile[fileIndex][0] + " " + e.toString());
89             // Set the test case failure whenever it failed to setDataSource
90             assertTrue("Failed to setDataSource ", false);
91         }
92 
93         // METADATA_KEY_CD_TRACK_NUMBER should return the TCRK value
94         value = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_CD_TRACK_NUMBER);
95         Log.v(TAG, "CD_TRACK_NUMBER : " + value);
96         assertEquals(TAG, metadataFile[fileIndex][meta.CD_TRACK.ordinal()], value);
97 
98         value = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ALBUM);
99         Log.v(TAG, "Album : " + value);
100         assertEquals(TAG, metadataFile[fileIndex][meta.ALBUM.ordinal()], value);
101 
102         value = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ARTIST);
103         Log.v(TAG, "Artist : " + value);
104         assertEquals(TAG, metadataFile[fileIndex][meta.ARTIST.ordinal()], value);
105 
106         value = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_AUTHOR);
107         Log.v(TAG, "Author : " + value);
108         assertEquals(TAG, metadataFile[fileIndex][meta.AUTHOR.ordinal()], value);
109 
110         value = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_COMPOSER);
111         Log.v(TAG, "Composer : " + value);
112         assertEquals(TAG, metadataFile[fileIndex][meta.COMPOSER.ordinal()], value);
113 
114         value = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DATE);
115         Log.v(TAG, "Date : " + value);
116         assertEquals(TAG, metadataFile[fileIndex][meta.DATE.ordinal()], value);
117 
118         value = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_GENRE);
119         Log.v(TAG, "Genre : " + value);
120         assertEquals(TAG, metadataFile[fileIndex][meta.GENRE.ordinal()], value);
121 
122         value = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_TITLE);
123         Log.v(TAG, "Title : " + value);
124         assertEquals(TAG, metadataFile[fileIndex][meta.TITLE.ordinal()], value);
125 
126         value = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_YEAR);
127         Log.v(TAG, "Year : " + value);
128         assertEquals(TAG, metadataFile[fileIndex][meta.YEAR.ordinal()], value);
129 
130         value = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
131         Log.v(
132                 TAG,
133                 "Expected = "
134                         + metadataFile[fileIndex][meta.DURATION.ordinal()]
135                         + "reult = "
136                         + value);
137         // Only require that the returned duration is within 100ms of the expected
138         // one as PV and stagefright differ slightly in their implementation.
139         int durationDifferenceMs =
140                 Math.abs(
141                         Integer.parseInt(metadataFile[fileIndex][meta.DURATION.ordinal()])
142                                 - Integer.parseInt(value));
143         assertTrue(TAG, durationDifferenceMs < 100);
144 
145         // METADATA_KEY_NUM_TRACKS should return the total number of tracks in the media
146         // include the video and audio
147         value = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_NUM_TRACKS);
148         Log.v(TAG, "Track : " + value);
149         assertEquals(TAG, metadataFile[fileIndex][meta.NUM_TRACKS.ordinal()], value);
150 
151         value = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_WRITER);
152         Log.v(TAG, "Writer : " + value);
153         assertEquals(TAG, metadataFile[fileIndex][meta.WRITER.ordinal()], value);
154 
155         retriever.release();
156     }
157 }
158