CodecTest.java revision f013e1afd1e68af5e3b868c26a653bbfb39538f8
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
17package com.android.mediaframeworktest.functional;
18
19
20
21//import android.content.Resources;
22import com.android.mediaframeworktest.MediaFrameworkTest;
23import com.android.mediaframeworktest.MediaNames;
24
25import android.content.res.AssetFileDescriptor;
26import android.graphics.Bitmap;
27import android.graphics.BitmapFactory;
28import android.media.MediaMetadataRetriever;
29import android.media.MediaPlayer;
30import android.media.MediaRecorder;
31import android.os.SystemClock;
32import android.util.Log;
33
34import java.io.IOException;
35import java.io.InputStream;
36/**
37 * Junit / Instrumentation test case for the media player api
38
39 */
40public class CodecTest {
41    private static String TAG = "MediaPlayerApiTest";
42
43    public static String printCpuInfo(){
44        String cm = "dumpsys cpuinfo";
45        String cpuinfo =null;
46        int ch;
47        try{
48            Process  p = Runtime.getRuntime().exec(cm);
49            InputStream in = p.getInputStream();
50            StringBuffer sb = new StringBuffer(512);
51            while ( ( ch = in.read() ) != -1 ){
52                sb.append((char) ch);
53            }
54            cpuinfo = sb.toString();
55        }catch (IOException e){
56            Log.v(TAG, e.toString());
57        }
58        return cpuinfo;
59    }
60
61
62    public static int getDuration(String filePath) {
63        Log.v(TAG, "getDuration - " + filePath);
64        MediaPlayer mp = new MediaPlayer();
65        try{
66            mp.setDataSource(filePath);
67            mp.prepare();
68        }catch (Exception e){}
69        int duration = mp.getDuration();
70        Log.v(TAG, "Duration " + duration);
71        mp.release();
72        Log.v(TAG, "release");
73        return duration;
74    }
75
76    public static boolean getCurrentPosition(String filePath){
77        Log.v(TAG, "GetCurrentPosition - " + filePath);
78        int currentPosition = 0;
79        long t1=0;
80        long t2 =0;
81        MediaPlayer mp = new MediaPlayer();
82        try{
83            mp.setDataSource(filePath);
84            Log.v(TAG, "start playback");
85            mp.prepare();
86            mp.start();
87            t1=SystemClock.uptimeMillis();
88            Thread.sleep(10000);
89            mp.pause();
90            Thread.sleep(MediaNames.PAUSE_WAIT_TIME);
91            t2=SystemClock.uptimeMillis();
92        }catch (Exception e){
93            Log.v(TAG, e.toString());
94        }
95        currentPosition = mp.getCurrentPosition();
96        mp.stop();
97        mp.release();
98        Log.v(TAG, "mp currentPositon = " + currentPosition + " play duration = " + (t2-t1));
99        //The currentposition should be within 10% of the sleep time
100        //For the very short mp3, it should return the length instead of 10 seconds
101        if (filePath.equals(MediaNames.SHORTMP3)){
102            if (currentPosition < 1000 )
103                return true;
104        }
105        if ((currentPosition < ((t2-t1) *1.2)) && (currentPosition > 0))
106            return true;
107        else
108            return false;
109    }
110
111    public static boolean seekTo(String filePath){
112        Log.v(TAG, "seekTo " + filePath);
113        int currentPosition = 0;
114        MediaPlayer mp = new MediaPlayer();
115        try{
116            mp.setDataSource(filePath);
117            mp.prepare();
118            mp.start();
119            mp.seekTo(MediaNames.SEEK_TIME);
120            Thread.sleep(MediaNames.WAIT_TIME);
121            currentPosition = mp.getCurrentPosition();
122        }catch (Exception e){
123            Log.v(TAG, e.getMessage());
124        }
125        mp.stop();
126        mp.release();
127        Log.v(TAG, "CurrentPosition = " + currentPosition);
128        //The currentposition should be at least greater than the 80% of seek time
129        if ((currentPosition > MediaNames.SEEK_TIME *0.8))
130            return true;
131        else
132            return false;
133    }
134
135    public static boolean setLooping(String filePath){
136        int currentPosition = 0;
137        int duration = 0;
138        long t1 =0;
139        long t2 =0;
140        Log.v (TAG, "SetLooping - " + filePath);
141        MediaPlayer mp = new MediaPlayer();
142        try{
143            mp.setDataSource(filePath);
144            mp.prepare();
145            duration = mp.getDuration();
146            Log.v(TAG, "setLooping duration " + duration);
147            mp.setLooping(true);
148            mp.start();
149            Thread.sleep(5000);
150            mp.seekTo(duration - 5000);
151            t1=SystemClock.uptimeMillis();
152            Thread.sleep(20000);
153            t2=SystemClock.uptimeMillis();
154            Log.v(TAG, "pause");
155            //Bug# 1106852 - IllegalStateException will be thrown if pause is called
156            //in here
157            //mp.pause();
158            currentPosition = mp.getCurrentPosition();
159            Log.v(TAG, "looping position " + currentPosition + "duration = " + (t2-t1));
160        }catch (Exception e){
161            Log.v(TAG, "Exception : " + e.toString());
162        }
163        mp.stop();
164        mp.release();
165        //The current position should be within 20% of the sleep time
166        //and should be greater than zero.
167        if ((currentPosition < ((t2-t1-5000)*1.2)) && currentPosition > 0)
168            return true;
169        else
170            return false;
171    }
172
173    public static boolean pause(String filePath) throws Exception {
174        Log.v(TAG, "pause - " + filePath);
175        boolean misPlaying = true;
176        boolean pauseResult = false;
177        long t1=0;
178        long t2=0;
179        MediaPlayer mp = new MediaPlayer();
180        mp.setDataSource(filePath);
181        mp.prepare();
182        int duration = mp.getDuration();
183        mp.start();
184        t1=SystemClock.uptimeMillis();
185        Thread.sleep(5000);
186        mp.pause();
187        Thread.sleep(MediaNames.PAUSE_WAIT_TIME);
188        t2=SystemClock.uptimeMillis();
189        misPlaying = mp.isPlaying();
190        int curPosition = mp.getCurrentPosition();
191        Log.v(TAG, filePath + " pause currentPositon " + curPosition);
192        Log.v(TAG, "isPlaying "+ misPlaying + " wait time " + (t2 - t1) );
193        String cpuinfo = printCpuInfo();
194        Log.v(TAG, cpuinfo);
195        if ((curPosition>0) && (curPosition < ((t2-t1) * 1.3)) && (misPlaying == false))
196            pauseResult = true;
197        mp.stop();
198        mp.release();
199        return pauseResult;
200    }
201
202    public static void prepareStopRelease(String filePath) throws Exception {
203        Log.v(TAG, "prepareStopRelease" + filePath);
204        MediaPlayer mp = new MediaPlayer();
205        mp.setDataSource(filePath);
206        mp.prepare();
207        mp.stop();
208        mp.release();
209    }
210
211    public static void preparePauseRelease(String filePath) throws Exception {
212        Log.v(TAG, "preparePauseRelease" + filePath);
213        MediaPlayer mp = new MediaPlayer();
214        mp.setDataSource(filePath);
215        mp.prepare();
216        mp.pause();
217        mp.release();
218    }
219
220    public static int videoHeight(String filePath) throws Exception {
221        Log.v(TAG, "videoHeight - " + filePath);
222        int videoHeight = 0;
223        MediaPlayer mp = new MediaPlayer();
224        mp.setDataSource(filePath);
225        mp.setDisplay(MediaFrameworkTest.mSurfaceView.getHolder());
226        mp.prepare();
227        videoHeight = mp.getVideoHeight();
228        mp.release();
229        return videoHeight;
230    }
231
232    public static int videoWidth(String filePath) throws Exception {
233        Log.v(TAG, "videoWidth - " + filePath);
234        int videoWidth = 0;
235        MediaPlayer mp = new MediaPlayer();
236        mp.setDataSource(filePath);
237        mp.setDisplay(MediaFrameworkTest.mSurfaceView.getHolder());
238        mp.prepare();
239        videoWidth = mp.getVideoWidth();
240        mp.release();
241        return videoWidth;
242    }
243
244    //This also test the streaming video which may take a long
245    //time to start the playback.
246    public static boolean videoSeekTo(String filePath) throws Exception {
247        Log.v(TAG, "videoSeekTo - " + filePath);
248        int currentPosition = 0;
249        int duration = 0;
250        boolean videoResult = false;
251        MediaPlayer mp = new MediaPlayer();
252        mp.setDataSource(filePath);
253        mp.setDisplay(MediaFrameworkTest.mSurfaceView.getHolder());
254        mp.prepare();
255        mp.start();
256        if (filePath.equals(MediaNames.VIDEO_SHORT_3GP)){
257            mp.pause();
258            Thread.sleep(MediaNames.PAUSE_WAIT_TIME);
259            mp.seekTo(0);
260            mp.start();
261            Thread.sleep(1000);
262            currentPosition = mp.getCurrentPosition();
263            Log.v(TAG,"short position " + currentPosition);
264            if (currentPosition > 100 )
265                return true;
266            else
267                return false;
268        }
269        Thread.sleep(5000);
270        duration = mp.getDuration();
271        Log.v(TAG, "video duration " + duration);
272        mp.pause();
273        Thread.sleep(MediaNames.PAUSE_WAIT_TIME);
274        mp.seekTo(duration - 20000 );
275        mp.start();
276        Thread.sleep(1000);
277        mp.pause();
278        Thread.sleep(MediaNames.PAUSE_WAIT_TIME);
279        mp.seekTo(duration/2);
280        mp.start();
281        Thread.sleep(10000);
282        currentPosition = mp.getCurrentPosition();
283        Log.v(TAG, "video currentPosition " + currentPosition);
284        mp.release();
285        if (currentPosition > (duration /2 )*0.9)
286            return true;
287        else
288            return false;
289
290    }
291
292    public static boolean seekToEnd(String filePath){
293        Log.v(TAG, "seekToEnd - " + filePath);
294        int duration = 0;
295        int currentPosition = 0;
296        boolean isPlaying = false;
297        MediaPlayer mp = new MediaPlayer();
298        try{
299            mp.setDataSource(filePath);
300            Log.v(TAG, "start playback");
301            mp.prepare();
302            duration = mp.getDuration();
303            mp.seekTo(duration - 3000);
304            mp.start();
305            Thread.sleep(6000);
306        }catch (Exception e){}
307        isPlaying = mp.isPlaying();
308        currentPosition = mp.getCurrentPosition();
309        Log.v(TAG, "seekToEnd currentPosition= " + currentPosition + " isPlaying = " + isPlaying);
310        mp.stop();
311        mp.release();
312        Log.v(TAG, "duration = " + duration);
313        if (currentPosition < 0.9 * duration || isPlaying)
314            return false;
315        else
316            return true;
317    }
318
319    public static boolean shortMediaStop(String filePath){
320        Log.v(TAG, "shortMediaStop - " + filePath);
321        //This test is only for the short media file
322        int duration = 0;
323        int currentPosition = 0;
324        boolean isPlaying = false;
325        MediaPlayer mp = new MediaPlayer();
326        try{
327            mp.setDataSource(filePath);
328            Log.v(TAG, "start playback");
329            mp.prepare();
330            duration = mp.getDuration();
331            mp.start();
332            Thread.sleep(10000);
333        }catch (Exception e){}
334        isPlaying = mp.isPlaying();
335        currentPosition = mp.getCurrentPosition();
336        Log.v(TAG, "seekToEnd currentPosition= " + currentPosition + " isPlaying = " + isPlaying);
337        mp.stop();
338        mp.release();
339        Log.v(TAG, "duration = " + duration);
340        if (currentPosition > duration || isPlaying)
341            return false;
342        else
343            return true;
344    }
345
346    public static boolean playToEnd(String filePath){
347        Log.v(TAG, "shortMediaStop - " + filePath);
348        //This test is only for the short media file
349        int duration = 200000;
350        int updateDuration = 0;
351        int currentPosition = 0;
352        boolean isPlaying = false;
353        MediaPlayer mp = new MediaPlayer();
354        try{
355            Thread.sleep(5000);
356            mp.setDataSource(filePath);
357            Log.v(TAG, "start playback");
358            mp.prepare();
359            //duration = mp.getDuration();
360            mp.start();
361            Thread.sleep(50000);
362        }catch (Exception e){}
363        isPlaying = mp.isPlaying();
364        currentPosition = mp.getCurrentPosition();
365        //updateDuration = mp.getDuration();
366        Log.v(TAG, "seekToEnd currentPosition= " + currentPosition + " isPlaying = " + isPlaying);
367        mp.stop();
368        mp.release();
369        //Log.v(TAG, "duration = " + duration);
370        //Log.v(TAG, "Update duration = " + updateDuration);
371        if (currentPosition > duration || isPlaying)
372            return false;
373        else
374            return true;
375    }
376
377    public static boolean seektoBeforeStart(String filePath){
378        Log.v(TAG, "seektoBeforeStart - " + filePath);
379        //This test is only for the short media file
380        int duration = 0;
381        int currentPosition = 0;
382
383        MediaPlayer mp = new MediaPlayer();
384        try{
385            mp.setDataSource(filePath);
386            mp.prepare();
387            duration = mp.getDuration();
388            mp.seekTo(duration - 10000);
389            mp.start();
390            currentPosition=mp.getCurrentPosition();
391            mp.stop();
392            mp.release();
393        }catch (Exception e){}
394        if (currentPosition < duration/2)
395            return false;
396        else
397            return true;
398    }
399
400    public static boolean mediaRecorderRecord(String filePath){
401        Log.v(TAG, "SoundRecording - " + filePath);
402        //This test is only for the short media file
403        int duration = 0;
404        try{
405            MediaRecorder mRecorder = new MediaRecorder();
406            mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
407            mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
408            mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
409            mRecorder.setOutputFile(filePath);
410            mRecorder.prepare();
411            mRecorder.start();
412            Thread.sleep(500);
413            mRecorder.stop();
414            Log.v(TAG, "sound recorded");
415            mRecorder.release();
416        }catch (Exception e){
417            Log.v(TAG, e.toString());
418        }
419
420        //Verify the recorded file
421        MediaPlayer mp = new MediaPlayer();
422        try{
423            mp.setDataSource(filePath);
424            mp.prepare();
425            duration = mp.getDuration();
426            Log.v(TAG,"Duration " + duration);
427            mp.release();
428        }catch (Exception e){}
429        //Check the record media file length is greate than zero
430        if (duration > 0)
431            return true;
432        else
433            return false;
434
435    }
436
437    //Test for mediaMeta Data Thumbnail
438    public static boolean getThumbnail(String filePath, String goldenPath){
439        Log.v(TAG, "getThumbnail - " + filePath);
440
441        int goldenHeight = 0;
442        int goldenWidth = 0;
443        int outputWidth = 0;
444        int outputHeight = 0;
445
446        //This test is only for the short media file
447        try{
448            BitmapFactory mBitmapFactory = new BitmapFactory();
449
450            MediaMetadataRetriever mMediaMetadataRetriever = new MediaMetadataRetriever();
451            try {
452                mMediaMetadataRetriever.setDataSource(filePath);
453            } catch(Exception e) {
454                e.printStackTrace();
455                return false;
456            }
457            Bitmap outThumbnail = mMediaMetadataRetriever.captureFrame();
458
459            //Verify the thumbnail
460            Bitmap goldenBitmap = mBitmapFactory.decodeFile(goldenPath);
461
462            outputWidth = outThumbnail.getWidth();
463            outputHeight = outThumbnail.getHeight();
464            goldenHeight = goldenBitmap.getHeight();
465            goldenWidth = goldenBitmap.getWidth();
466
467            //check the image dimension
468            if ((outputWidth != goldenWidth) || (outputHeight != goldenHeight))
469                return false;
470
471            //Check one line of pixel
472            int x = goldenHeight/2;
473            for (int j=0; j<goldenWidth; j++){
474                if (goldenBitmap.getPixel(x, j) != outThumbnail.getPixel(x, j)){
475                    Log.v(TAG, "pixel = " + goldenBitmap.getPixel(x, j));
476                    return false;
477                }
478            }
479        }catch (Exception e){}
480        return true;
481    }
482
483    //Load midi file from resources
484    public static boolean resourcesPlayback(AssetFileDescriptor afd, int expectedDuration){
485        int duration = 0;
486        try{
487            MediaPlayer mp = new MediaPlayer();
488            mp.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(), afd.getLength());
489            mp.prepare();
490            mp.start();
491            duration = mp.getDuration();
492            Thread.sleep(5000);
493            mp.release();
494        }catch (Exception e){
495            Log.v(TAG,e.getMessage());
496        }
497        if (duration > expectedDuration)
498            return true;
499        else
500            return false;
501    }
502
503    public static boolean prepareAsyncReset(String filePath){
504        //preparesAsync
505        try{
506            MediaPlayer mp = new MediaPlayer();
507            mp.setDataSource(filePath);
508            mp.prepareAsync();
509            mp.reset();
510            mp.release();
511        }catch (Exception e){
512            Log.v(TAG,e.getMessage());
513            return false;
514        }
515        return true;
516    }
517
518
519    public static boolean isLooping(String filePath) {
520        MediaPlayer mp = null;
521
522        try {
523            mp = new MediaPlayer();
524            if (mp.isLooping()) {
525                Log.v(TAG, "MediaPlayer.isLooping() returned true after ctor");
526                return false;
527            }
528            mp.setDataSource(filePath);
529            mp.prepare();
530
531            mp.setLooping(true);
532            if (!mp.isLooping()) {
533                Log.v(TAG, "MediaPlayer.isLooping() returned false after setLooping(true)");
534                return false;
535            }
536
537            mp.setLooping(false);
538            if (mp.isLooping()) {
539                Log.v(TAG, "MediaPlayer.isLooping() returned true after setLooping(false)");
540                return false;
541            }
542        }catch (Exception e){
543            Log.v(TAG, "Exception : " + e.toString());
544            return false;
545        } finally {
546            if (mp != null)
547                mp.release();
548        }
549
550        return true;
551    }
552
553    public static boolean isLoopingAfterReset(String filePath) {
554        MediaPlayer mp = null;
555        try {
556            mp = new MediaPlayer();
557            mp.setDataSource(filePath);
558            mp.prepare();
559
560            mp.setLooping(true);
561            mp.reset();
562            if (mp.isLooping()) {
563                Log.v(TAG, "MediaPlayer.isLooping() returned true after reset()");
564                return false;
565            }
566        }catch (Exception e){
567            Log.v(TAG, "Exception : " + e.toString());
568            return false;
569        } finally {
570            if (mp != null)
571                mp.release();
572        }
573
574        return true;
575    }
576}
577
578