FileDocCategorySizeDatePackage
MediaRecorderTest.javaAPI DocAndroid 1.5 API14144Wed May 06 22:42:00 BST 2009com.android.mediaframeworktest.functional

MediaRecorderTest

public class MediaRecorderTest extends android.test.ActivityInstrumentationTestCase
Junit / Instrumentation test case for the media recorder api

Fields Summary
private String
TAG
private int
mOutputDuration
private int
mOutputVideoWidth
private int
mOutputVideoHeight
private android.view.SurfaceHolder
mSurfaceHolder
private android.media.MediaRecorder
mRecorder
android.content.Context
mContext
Constructors Summary
public MediaRecorderTest()

  
      
        super("com.android.mediaframeworktest", MediaFrameworkTest.class);
       
    
Methods Summary
private voidgetOutputVideoProperty(java.lang.String outputFilePath)

        MediaPlayer mediaPlayer = new MediaPlayer();
        try {
            mediaPlayer.setDataSource(outputFilePath);
            Log.v(TAG, "file Path = " + outputFilePath);
            mediaPlayer.setDisplay(MediaFrameworkTest.mSurfaceView.getHolder());
            Log.v(TAG, "before player prepare");
            mediaPlayer.prepare();
            Log.v(TAG, "before getduration");
            mOutputDuration = mediaPlayer.getDuration();
            Log.v(TAG, "get video dimension");
            mOutputVideoHeight = mediaPlayer.getVideoHeight();
            mOutputVideoWidth = mediaPlayer.getVideoWidth();
            mediaPlayer.release();    
        } catch (Exception e) {
            Log.v(TAG, e.toString());
            mediaPlayer.release();
        }       
    
private booleaninvalidRecordSetting(int frameRate, int width, int height, int videoFormat, int outFormat, java.lang.String outFile, boolean videoOnly)

        try {
            if (!videoOnly) {
                Log.v(TAG, "setAudioSource");
                mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
            }
            mRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
            mRecorder.setOutputFormat(outFormat);
            Log.v(TAG, "output format " + outFormat);          
            mRecorder.setOutputFile(outFile);
            mRecorder.setVideoFrameRate(frameRate);
            mRecorder.setVideoSize(width, height);
            Log.v(TAG, "setEncoder");
            mRecorder.setVideoEncoder(videoFormat);
            if (!videoOnly) {
                mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
            }
            mSurfaceHolder = MediaFrameworkTest.mSurfaceView.getHolder();
            Log.v(TAG, "setPreview");
            mRecorder.setPreviewDisplay(mSurfaceHolder.getSurface());
            Log.v(TAG, "prepare");
            mRecorder.prepare();
            Log.v(TAG, "start");
            mRecorder.start();
            Thread.sleep(MediaNames.RECORDED_TIME);
            Log.v(TAG, "stop");
            mRecorder.stop();
            mRecorder.release();
        } catch (Exception e) {
            Log.v("record video failed ", e.toString());
            mRecorder.release();
            Log.v(TAG, "reset and release");
            return true;
        }
        return false;
    
private voidrecordVideo(int frameRate, int width, int height, int videoFormat, int outFormat, java.lang.String outFile, boolean videoOnly)

        Log.v(TAG,"startPreviewAndPrepareRecording");
        try {
            if (!videoOnly) {
                Log.v(TAG, "setAudioSource");
                mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
            }
            mRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
            mRecorder.setOutputFormat(outFormat);
            Log.v(TAG, "output format " + outFormat);          
            mRecorder.setOutputFile(outFile);
            mRecorder.setVideoFrameRate(frameRate);
            mRecorder.setVideoSize(width, height);
            Log.v(TAG, "setEncoder");
            mRecorder.setVideoEncoder(videoFormat);
            if (!videoOnly) {
                mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
            }
            mSurfaceHolder = MediaFrameworkTest.mSurfaceView.getHolder();
            Log.v(TAG, "setPreview");
            mRecorder.setPreviewDisplay(mSurfaceHolder.getSurface());
            Log.v(TAG, "prepare");
            mRecorder.prepare();
            Log.v(TAG, "start");
            mRecorder.start();
            Thread.sleep(MediaNames.RECORDED_TIME);
            Log.v(TAG, "stop");
            mRecorder.stop();
            mRecorder.release();
        } catch (Exception e) {
            Log.v("record video failed ", e.toString());
            mRecorder.release();
        }
    
private voidremoveFile(java.lang.String filePath)

        File fileRemove = new File(filePath);
        fileRemove.delete();     
    
protected voidsetUp()

        super.setUp(); 
        Log.v(TAG,"create the media recorder");
        mRecorder = new MediaRecorder();
    
public voidtestCIFH263()

       
        boolean videoRecordedResult = false;
        recordVideo(15, 352, 288, MediaRecorder.VideoEncoder.H263, 
               MediaRecorder.OutputFormat.THREE_GPP, MediaNames.RECORDED_CIF_H263, false);      
        videoRecordedResult = validateVideo(MediaNames.RECORDED_CIF_H263, 352, 288);
        assertTrue("CIFH263", videoRecordedResult);
    
public voidtestCIFMP4()

       
        boolean videoRecordedResult = false;
        recordVideo(15, 352, 288, MediaRecorder.VideoEncoder.MPEG_4_SP, 
               MediaRecorder.OutputFormat.MPEG_4, MediaNames.RECORDED_CIF_MP4, false);      
        videoRecordedResult = validateVideo(MediaNames.RECORDED_CIF_MP4, 352, 288);
        assertTrue("CIFMP4", videoRecordedResult);
    
public voidtestCIFMP43GPP()

       
        boolean videoRecordedResult = false;
        recordVideo(15, 352, 288, MediaRecorder.VideoEncoder.MPEG_4_SP, 
               MediaRecorder.OutputFormat.THREE_GPP, MediaNames.RECORDED_VIDEO_3GP, false);      
        videoRecordedResult = validateVideo(MediaNames.RECORDED_VIDEO_3GP, 352, 288);
        assertTrue("CIFMP4 3GPP", videoRecordedResult);
    
public voidtestHVGAH263()

  
        boolean videoRecordedResult = false;
        recordVideo(15, 480, 320, MediaRecorder.VideoEncoder.H263, 
               MediaRecorder.OutputFormat.THREE_GPP, MediaNames.RECORDED_HVGA_H263, false);      
        videoRecordedResult = validateVideo(MediaNames.RECORDED_HVGA_H263, 480, 320);
        assertTrue("HVGAH263", videoRecordedResult);
    
public voidtestHVGAMP4()

  
        boolean videoRecordedResult = false;
        recordVideo(15, 480, 320, MediaRecorder.VideoEncoder.MPEG_4_SP, 
               MediaRecorder.OutputFormat.MPEG_4, MediaNames.RECORDED_HVGA_MP4, false);      
        videoRecordedResult = validateVideo(MediaNames.RECORDED_HVGA_MP4, 480, 320);
        assertTrue("HVGAMP4", videoRecordedResult);
    
public voidtestInvalidFrameRate()

       
        boolean isTestInvalidFrameRateSuccessful = false;
        isTestInvalidFrameRateSuccessful = invalidRecordSetting(50, 176, 144, MediaRecorder.VideoEncoder.H263, 
               MediaRecorder.OutputFormat.THREE_GPP, MediaNames.RECORDED_VIDEO_3GP, false);      
        assertTrue("Invalid FrameRate", isTestInvalidFrameRateSuccessful);
    
public voidtestInvalidVideoPath()

       
        boolean isTestInvalidVideoPathSuccessful = false;
        isTestInvalidVideoPathSuccessful = invalidRecordSetting(15, 176, 144, MediaRecorder.VideoEncoder.H263, 
               MediaRecorder.OutputFormat.THREE_GPP, MediaNames.INVALD_VIDEO_PATH, false);      
        assertTrue("Invalid outputFile Path", isTestInvalidVideoPathSuccessful);
    
public voidtestInvalidVideoSize()

       
        boolean isTestInvalidVideoSizeSuccessful = false;
        isTestInvalidVideoSizeSuccessful = invalidRecordSetting(15, 800, 600, MediaRecorder.VideoEncoder.H263, 
               MediaRecorder.OutputFormat.THREE_GPP, MediaNames.RECORDED_VIDEO_3GP, false);      
        assertTrue("Invalid video Size", isTestInvalidVideoSizeSuccessful);
    
public voidtestPortraitH263()

       
        boolean videoRecordedResult = false;
        recordVideo(15, 144, 176, MediaRecorder.VideoEncoder.H263, 
               MediaRecorder.OutputFormat.MPEG_4, MediaNames.RECORDED_VIDEO_3GP, true);      
        videoRecordedResult = validateVideo(MediaNames.RECORDED_VIDEO_3GP, 144, 176);
        assertTrue("PortraitH263", videoRecordedResult);
    
public voidtestQCIFH263()

        boolean videoRecordedResult = false; 
        recordVideo(15, 176, 144, MediaRecorder.VideoEncoder.H263, 
               MediaRecorder.OutputFormat.THREE_GPP, MediaNames.RECORDED_QCIF_H263, false);      
        videoRecordedResult = validateVideo(MediaNames.RECORDED_QCIF_H263, 176, 144);
        assertTrue("QCIFH263", videoRecordedResult);
    
public voidtestQCIFH2633GPP()

       
        boolean videoRecordedResult = false;
        recordVideo(15, 176, 144, MediaRecorder.VideoEncoder.H263, 
               MediaRecorder.OutputFormat.THREE_GPP, MediaNames.RECORDED_VIDEO_3GP, false);      
        videoRecordedResult = validateVideo(MediaNames.RECORDED_VIDEO_3GP, 176, 144);
        assertTrue("QCIFH263 3GPP", videoRecordedResult);
    
public voidtestQCIFMP4()

       
        boolean videoRecordedResult = false;
        recordVideo(15, 176, 144, MediaRecorder.VideoEncoder.MPEG_4_SP, 
               MediaRecorder.OutputFormat.MPEG_4, MediaNames.RECORDED_QCIF_MP4, false);      
        videoRecordedResult = validateVideo(MediaNames.RECORDED_QCIF_MP4, 176, 144);
        assertTrue("QCIFMP4", videoRecordedResult);
    
public voidtestQVGAH263()

  
        boolean videoRecordedResult = false;
        recordVideo(15, 320, 240, MediaRecorder.VideoEncoder.H263, 
               MediaRecorder.OutputFormat.THREE_GPP, MediaNames.RECORDED_QVGA_H263, false);      
        videoRecordedResult = validateVideo(MediaNames.RECORDED_QVGA_H263, 320, 240);
        assertTrue("QVGAH263", videoRecordedResult);
    
public voidtestQVGAMP4()

  
        boolean videoRecordedResult = false;
        recordVideo(15, 320, 240, MediaRecorder.VideoEncoder.MPEG_4_SP, 
               MediaRecorder.OutputFormat.MPEG_4, MediaNames.RECORDED_QVGA_MP4, false);      
        videoRecordedResult = validateVideo(MediaNames.RECORDED_QVGA_MP4, 320, 240);
        assertTrue("QVGAMP4", videoRecordedResult);
    
public voidtestSQVGAH263()

  
        boolean videoRecordedResult = false;
        recordVideo(15, 240, 160, MediaRecorder.VideoEncoder.H263, 
               MediaRecorder.OutputFormat.THREE_GPP, MediaNames.RECORDED_SQVGA_H263, false);      
        videoRecordedResult = validateVideo(MediaNames.RECORDED_SQVGA_H263, 240, 160);
        assertTrue("SQVGAH263", videoRecordedResult);
    
public voidtestSQVGAMP4()

  
        boolean videoRecordedResult = false;
        recordVideo(15, 240, 160, MediaRecorder.VideoEncoder.MPEG_4_SP, 
               MediaRecorder.OutputFormat.MPEG_4, MediaNames.RECORDED_SQVGA_MP4, false);      
        videoRecordedResult = validateVideo(MediaNames.RECORDED_SQVGA_MP4, 240, 160);
        assertTrue("SQVGAMP4", videoRecordedResult);
    
public voidtestVideoOnly()

       
        boolean videoRecordedResult = false;
        recordVideo(15, 176, 144, MediaRecorder.VideoEncoder.H263, 
               MediaRecorder.OutputFormat.MPEG_4, MediaNames.RECORDED_VIDEO_3GP, true);      
        videoRecordedResult = validateVideo(MediaNames.RECORDED_VIDEO_3GP, 176, 144);
        assertTrue("QCIFH263 Video Only", videoRecordedResult);
    
private booleanvalidateVideo(java.lang.String filePath, int width, int height)

        boolean validVideo = false;
        getOutputVideoProperty(filePath);
        if (mOutputVideoWidth == width && mOutputVideoHeight == height &&
                mOutputDuration > MediaNames.VALID_VIDEO_DURATION ) {
            validVideo = true;
        }
        Log.v(TAG, "width = " + mOutputVideoWidth + " height = " + mOutputVideoHeight + " Duration = " + mOutputDuration);
        removeFile(filePath);
        return validVideo;