blob: 8eb75f3f14e8818de603309354589dd1a8d923bd [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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
17package com.android.mediaframeworktest.unit;
18
19import android.util.Log;
20import android.media.MediaMetadataRetriever;
21import android.graphics.Bitmap;
22import java.io.FileOutputStream;
23import android.test.AndroidTestCase;
24import com.android.mediaframeworktest.MediaNames;
James Dong38b03a72009-11-02 12:11:57 -080025import com.android.mediaframeworktest.MediaProfileReader;
Yu Shan Emily Lau3e7b3c02009-03-24 22:20:13 -070026import android.test.suitebuilder.annotation.*;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080027
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028public class MediaMetadataRetrieverTest extends AndroidTestCase {
29
30 private static final String TAG = "MediaMetadataRetrieverTest";
31
32 // Test album art extraction.
Yu Shan Emily Lau3e7b3c02009-03-24 22:20:13 -070033 @MediumTest
James Dongdf9b3492011-01-04 15:03:48 -080034 public static void testGetEmbeddedPicture() throws Exception {
35 Log.v(TAG, "testGetEmbeddedPicture starts.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036 MediaMetadataRetriever retriever = new MediaMetadataRetriever();
James Dong1b7babd2010-02-16 14:38:23 -080037 boolean supportWMA = MediaProfileReader.getWMAEnable();
James Dong454f69c2010-03-17 15:09:24 -070038 boolean hasFailed = false;
James Dong1b7babd2010-02-16 14:38:23 -080039 boolean supportWMV = MediaProfileReader.getWMVEnable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040 for (int i = 0, n = MediaNames.ALBUMART_TEST_FILES.length; i < n; ++i) {
41 try {
42 Log.v(TAG, "File " + i + ": " + MediaNames.ALBUMART_TEST_FILES[i]);
James Dong38b03a72009-11-02 12:11:57 -080043 if ((MediaNames.ALBUMART_TEST_FILES[i].endsWith(".wma") && !supportWMA) ||
44 (MediaNames.ALBUMART_TEST_FILES[i].endsWith(".wmv") && !supportWMV)
45 ) {
46 Log.v(TAG, "windows media is not supported and thus we will skip the test for this file");
47 continue;
48 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049 retriever.setDataSource(MediaNames.ALBUMART_TEST_FILES[i]);
James Dongdf9b3492011-01-04 15:03:48 -080050 byte[] albumArt = retriever.getEmbeddedPicture();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051
52 // TODO:
53 // A better test would be to compare the retrieved album art with the
54 // known result.
55 if (albumArt == null) { // Do we have expect in JUnit?
James Dongdf9b3492011-01-04 15:03:48 -080056 Log.e(TAG, "Fails to get embedded picture for " + MediaNames.ALBUMART_TEST_FILES[i]);
James Dong454f69c2010-03-17 15:09:24 -070057 hasFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058 }
59 } catch(Exception e) {
James Dong454f69c2010-03-17 15:09:24 -070060 Log.e(TAG, "Fails to setDataSource for " + MediaNames.ALBUMART_TEST_FILES[i]);
61 hasFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062 }
63 Thread.yield(); // Don't be evil
64 }
65 retriever.release();
James Dongdf9b3492011-01-04 15:03:48 -080066 Log.v(TAG, "testGetEmbeddedPicture completes.");
James Dong454f69c2010-03-17 15:09:24 -070067 assertTrue(!hasFailed);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068 }
69
70 // Test frame capture
James Dong525ad182009-09-28 10:57:17 -070071 @LargeTest
72 public static void testThumbnailCapture() throws Exception {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080073 MediaMetadataRetriever retriever = new MediaMetadataRetriever();
James Dong1b7babd2010-02-16 14:38:23 -080074 boolean supportWMA = MediaProfileReader.getWMAEnable();
75 boolean supportWMV = MediaProfileReader.getWMVEnable();
James Dong454f69c2010-03-17 15:09:24 -070076 boolean hasFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077 Log.v(TAG, "Thumbnail processing starts");
78 long startedAt = System.currentTimeMillis();
Yu Shan Emily Lauc4fe54a2011-06-13 19:26:50 -070079 for(int i = 0, n = MediaNames.THUMBNAIL_METADATA_TEST_FILES.length; i < n; ++i) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080 try {
Yu Shan Emily Lauc4fe54a2011-06-13 19:26:50 -070081 Log.v(TAG, "File " + i + ": " + MediaNames.THUMBNAIL_METADATA_TEST_FILES[i]);
82 if ((MediaNames.THUMBNAIL_METADATA_TEST_FILES[i].endsWith(".wma") && !supportWMA) ||
83 (MediaNames.THUMBNAIL_METADATA_TEST_FILES[i].endsWith(".wmv") && !supportWMV)
James Dong38b03a72009-11-02 12:11:57 -080084 ) {
85 Log.v(TAG, "windows media is not supported and thus we will skip the test for this file");
86 continue;
87 }
Yu Shan Emily Lauc4fe54a2011-06-13 19:26:50 -070088 retriever.setDataSource(MediaNames.THUMBNAIL_METADATA_TEST_FILES[i]);
James Dongf6bd1ea2010-12-03 17:00:48 -080089 Bitmap bitmap = retriever.getFrameAtTime(-1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090 assertTrue(bitmap != null);
91 try {
Yu Shan Emily Lauc4fe54a2011-06-13 19:26:50 -070092 java.io.OutputStream stream = new FileOutputStream(MediaNames.THUMBNAIL_METADATA_TEST_FILES[i] + ".jpg");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093 bitmap.compress(Bitmap.CompressFormat.JPEG, 75, stream);
94 stream.close();
95 } catch (Exception e) {
Yu Shan Emily Lauc4fe54a2011-06-13 19:26:50 -070096 Log.e(TAG, "Fails to convert the bitmap to a JPEG file for " + MediaNames.THUMBNAIL_METADATA_TEST_FILES[i]);
James Dong454f69c2010-03-17 15:09:24 -070097 hasFailed = true;
James Dong7411c1b2010-11-29 11:57:27 -080098 Log.e(TAG, e.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080099 }
100 } catch(Exception e) {
Yu Shan Emily Lauc4fe54a2011-06-13 19:26:50 -0700101 Log.e(TAG, "Fails to setDataSource for file " + MediaNames.THUMBNAIL_METADATA_TEST_FILES[i]);
James Dong454f69c2010-03-17 15:09:24 -0700102 hasFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800103 }
104 Thread.yield(); // Don't be evil
105 }
106 long endedAt = System.currentTimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800107 retriever.release();
James Dong454f69c2010-03-17 15:09:24 -0700108 assertTrue(!hasFailed);
Yu Shan Emily Lauc4fe54a2011-06-13 19:26:50 -0700109 Log.v(TAG, "Average processing time per thumbnail: " + (endedAt - startedAt)/MediaNames.THUMBNAIL_METADATA_TEST_FILES.length + " ms");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800110 }
111
112 @LargeTest
113 public static void testMetadataRetrieval() throws Exception {
James Dong1b7babd2010-02-16 14:38:23 -0800114 boolean supportWMA = MediaProfileReader.getWMAEnable();
115 boolean supportWMV = MediaProfileReader.getWMVEnable();
James Dong454f69c2010-03-17 15:09:24 -0700116 boolean hasFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117 MediaMetadataRetriever retriever = new MediaMetadataRetriever();
Yu Shan Emily Lauc4fe54a2011-06-13 19:26:50 -0700118 for(int i = 0, n = MediaNames.THUMBNAIL_METADATA_TEST_FILES.length; i < n; ++i) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800119 try {
Yu Shan Emily Lauc4fe54a2011-06-13 19:26:50 -0700120 Log.v(TAG, "File " + i + ": " + MediaNames.THUMBNAIL_METADATA_TEST_FILES[i]);
121 if ((MediaNames.THUMBNAIL_METADATA_TEST_FILES[i].endsWith(".wma") && !supportWMA) ||
122 (MediaNames.THUMBNAIL_METADATA_TEST_FILES[i].endsWith(".wmv") && !supportWMV)
James Dong38b03a72009-11-02 12:11:57 -0800123 ) {
124 Log.v(TAG, "windows media is not supported and thus we will skip the test for this file");
125 continue;
126 }
Yu Shan Emily Lauc4fe54a2011-06-13 19:26:50 -0700127 retriever.setDataSource(MediaNames.THUMBNAIL_METADATA_TEST_FILES[i]);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800128 extractAllSupportedMetadataValues(retriever);
129 } catch(Exception e) {
Yu Shan Emily Lauc4fe54a2011-06-13 19:26:50 -0700130 Log.e(TAG, "Fails to setDataSource for file " + MediaNames.THUMBNAIL_METADATA_TEST_FILES[i]);
James Dong454f69c2010-03-17 15:09:24 -0700131 hasFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800132 }
133 Thread.yield(); // Don't be evil
134 }
135 retriever.release();
James Dong454f69c2010-03-17 15:09:24 -0700136 assertTrue(!hasFailed);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800137 }
138
139 // If the specified call order and valid media file is used, no exception
140 // should be thrown.
Yu Shan Emily Lau3e7b3c02009-03-24 22:20:13 -0700141 @MediumTest
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800142 public static void testBasicNormalMethodCallSequence() throws Exception {
James Dong454f69c2010-03-17 15:09:24 -0700143 boolean hasFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800144 MediaMetadataRetriever retriever = new MediaMetadataRetriever();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800145 try {
146 retriever.setDataSource(MediaNames.TEST_PATH_1);
James Dongf6bd1ea2010-12-03 17:00:48 -0800147 Bitmap bitmap = retriever.getFrameAtTime(-1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800148 assertTrue(bitmap != null);
149 try {
150 java.io.OutputStream stream = new FileOutputStream("/sdcard/thumbnailout.jpg");
151 bitmap.compress(Bitmap.CompressFormat.JPEG, 75, stream);
152 stream.close();
153 } catch (Exception e) {
154 throw new Exception("Fails to convert the bitmap to a JPEG file for " + MediaNames.TEST_PATH_1, e);
155 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800156 extractAllSupportedMetadataValues(retriever);
157 } catch(Exception e) {
James Dong454f69c2010-03-17 15:09:24 -0700158 Log.e(TAG, "Fails to setDataSource for " + MediaNames.TEST_PATH_1, e);
159 hasFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800160 }
161 retriever.release();
James Dong454f69c2010-03-17 15:09:24 -0700162 assertTrue(!hasFailed);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800163 }
164
James Dongf6bd1ea2010-12-03 17:00:48 -0800165 // If setDataSource() has not been called, both getFrameAtTime() and extractMetadata() must
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800166 // return null.
Yu Shan Emily Lau3e7b3c02009-03-24 22:20:13 -0700167 @MediumTest
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800168 public static void testBasicAbnormalMethodCallSequence() {
James Dong454f69c2010-03-17 15:09:24 -0700169 boolean hasFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800170 MediaMetadataRetriever retriever = new MediaMetadataRetriever();
James Dong454f69c2010-03-17 15:09:24 -0700171 if (retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ALBUM) != null) {
172 Log.e(TAG, "No album metadata expected, but is available");
173 hasFailed = true;
174 }
James Dongf6bd1ea2010-12-03 17:00:48 -0800175 if (retriever.getFrameAtTime(-1) != null) {
James Dong454f69c2010-03-17 15:09:24 -0700176 Log.e(TAG, "No frame expected, but is available");
177 hasFailed = true;
178 }
179 assertTrue(!hasFailed);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800180 }
181
182 // Test setDataSource()
Yu Shan Emily Lau3e7b3c02009-03-24 22:20:13 -0700183 @MediumTest
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800184 public static void testSetDataSource() {
185 MediaMetadataRetriever retriever = new MediaMetadataRetriever();
James Dong454f69c2010-03-17 15:09:24 -0700186 boolean hasFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800187
188 // Null pointer argument
189 try {
190 String path = null;
191 retriever.setDataSource(path);
James Dong454f69c2010-03-17 15:09:24 -0700192 Log.e(TAG, "IllegalArgumentException failed to be thrown.");
193 hasFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800194 } catch(Exception e) {
James Dong454f69c2010-03-17 15:09:24 -0700195 if (!(e instanceof IllegalArgumentException)) {
196 Log.e(TAG, "Expected a IllegalArgumentException, but got a different exception");
197 hasFailed = true;
198 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800199 }
200
201 // Use mem:// path
202 try {
203 retriever.setDataSource(MediaNames.TEST_PATH_5);
James Dong454f69c2010-03-17 15:09:24 -0700204 Log.e(TAG, "IllegalArgumentException failed to be thrown.");
205 hasFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800206 } catch(Exception e) {
James Dong454f69c2010-03-17 15:09:24 -0700207 if (!(e instanceof IllegalArgumentException)) {
208 Log.e(TAG, "Expected a IllegalArgumentException, but got a different exception");
209 hasFailed = true;
210 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800211 }
212
213 // The pathname does not correspond to any existing file
214 try {
215 retriever.setDataSource(MediaNames.TEST_PATH_4);
James Dong454f69c2010-03-17 15:09:24 -0700216 Log.e(TAG, "RuntimeException failed to be thrown.");
217 hasFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800218 } catch(Exception e) {
James Dong454f69c2010-03-17 15:09:24 -0700219 if (!(e instanceof RuntimeException)) {
220 Log.e(TAG, "Expected a RuntimeException, but got a different exception");
221 hasFailed = true;
222 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800223 }
224
225 // The pathname does correspond to a file, but this file
226 // is not a valid media file
227 try {
228 retriever.setDataSource(MediaNames.TEST_PATH_3);
James Dong454f69c2010-03-17 15:09:24 -0700229 Log.e(TAG, "RuntimeException failed to be thrown.");
230 hasFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800231 } catch(Exception e) {
James Dong454f69c2010-03-17 15:09:24 -0700232 if (!(e instanceof RuntimeException)) {
233 Log.e(TAG, "Expected a RuntimeException, but got a different exception");
234 hasFailed = true;
235 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800236 }
237
238 retriever.release();
James Dong454f69c2010-03-17 15:09:24 -0700239 assertTrue(!hasFailed);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800240 }
241
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800242 // TODO:
243 // Encode and test for the correct mix of metadata elements on a per-file basis?
244 // We should be able to compare the actual returned metadata with the expected metadata
245 // with each given sample test file.
246 private static void extractAllSupportedMetadataValues(MediaMetadataRetriever retriever) {
247 String value = null;
248 Log.v(TAG, (value = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_CD_TRACK_NUMBER)) == null? "not found": value);
249 Log.v(TAG, (value = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION)) == null? "not found": value);
250 Log.v(TAG, (value = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_NUM_TRACKS)) == null? "not found": value);
251 Log.v(TAG, (value = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ALBUM)) == null? "not found": value);
252 Log.v(TAG, (value = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ARTIST)) == null? "not found": value);
253 Log.v(TAG, (value = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_AUTHOR)) == null? "not found": value);
254 Log.v(TAG, (value = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_COMPOSER)) == null? "not found": value);
255 Log.v(TAG, (value = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DATE)) == null? "not found": value);
256 Log.v(TAG, (value = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_GENRE)) == null? "not found": value);
257 Log.v(TAG, (value = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_TITLE)) == null? "not found": value);
258 Log.v(TAG, (value = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_YEAR)) == null? "not found": value);
259 }
260}