FileDocCategorySizeDatePackage
MediaRecorderStressTest.javaAPI DocAndroid 5.1 API15135Thu Mar 12 22:22:30 GMT 2015com.android.mediaframeworktest.stress

MediaRecorderStressTest

public class MediaRecorderStressTest extends android.test.ActivityInstrumentationTestCase2
Junit / Instrumentation test case for the media player api

Fields Summary
private String
TAG
private android.media.MediaRecorder
mRecorder
private android.hardware.Camera
mCamera
private static final int
CAMERA_ID
private static final int
NUMBER_OF_TIME_LAPSE_LOOPS
private static final int
TIME_LAPSE_PLAYBACK_WAIT_TIME
private static final int
USE_TEST_RUNNER_PROFILE
private static final long
WAIT_TIMEOUT
private static final String
OUTPUT_FILE_EXT
private static final String
MEDIA_STRESS_OUTPUT
private final CameraErrorCallback
mCameraErrorCallback
private final RecorderErrorCallback
mRecorderErrorCallback
private android.os.Handler
mHandler
private Thread
mLooperThread
private Writer
mOutput
Constructors Summary
public MediaRecorderStressTest()


      
        super("com.android.mediaframeworktest", MediaFrameworkTest.class);
    
Methods Summary
public voiddefaultStressRecordVideoAndPlayback()

        recordVideoAndPlayback(USE_TEST_RUNNER_PROFILE);
    
private voidrecordVideoAndPlayback(int profile)

        int iterations;
        int recordDuration;
        boolean removeVideo;

        int videoEncoder;
        int audioEncoder;
        int frameRate;
        int videoWidth;
        int videoHeight;
        int bitRate;

        if (profile != USE_TEST_RUNNER_PROFILE) {
            assertTrue(String.format("Camera doesn't support profile %d", profile),
                    CamcorderProfile.hasProfile(CAMERA_ID, profile));
            CamcorderProfile camcorderProfile = CamcorderProfile.get(CAMERA_ID, profile);
            videoEncoder = camcorderProfile.videoCodec;
            audioEncoder = camcorderProfile.audioCodec;
            frameRate = camcorderProfile.videoFrameRate;
            videoWidth = camcorderProfile.videoFrameWidth;
            videoHeight = camcorderProfile.videoFrameHeight;
            bitRate = camcorderProfile.videoBitRate;
        } else {
            videoEncoder = MediaRecorderStressTestRunner.mVideoEncoder;
            audioEncoder = MediaRecorderStressTestRunner.mAudioEncoder;
            frameRate = MediaRecorderStressTestRunner.mFrameRate;
            videoWidth = MediaRecorderStressTestRunner.mVideoWidth;
            videoHeight = MediaRecorderStressTestRunner.mVideoHeight;
            bitRate = MediaRecorderStressTestRunner.mBitRate;
        }
        iterations = MediaRecorderStressTestRunner.mIterations;
        recordDuration = MediaRecorderStressTestRunner.mDuration;
        removeVideo = MediaRecorderStressTestRunner.mRemoveVideo;

        SurfaceHolder surfaceHolder = MediaFrameworkTest.mSurfaceView.getHolder();
        mOutput.write("Total number of loops: " + iterations + "\n");

        try {
            mOutput.write("No of loop: ");
            for (int i = 0; i < iterations; i++) {
                String fileName = String.format("%s/temp%d%s",
                        Environment.getExternalStorageDirectory(), i, OUTPUT_FILE_EXT);
                Log.v(TAG, fileName);

                runOnLooper(new Runnable() {
                    @Override
                    public void run() {
                        mRecorder = new MediaRecorder();
                    }
                });

                Log.v(TAG, "iterations : " + iterations);
                Log.v(TAG, "video encoder : " + videoEncoder);
                Log.v(TAG, "audio encoder : " + audioEncoder);
                Log.v(TAG, "frame rate : " + frameRate);
                Log.v(TAG, "video width : " + videoWidth);
                Log.v(TAG, "video height : " + videoHeight);
                Log.v(TAG, "bit rate : " + bitRate);
                Log.v(TAG, "record duration : " + recordDuration);

                mRecorder.setOnErrorListener(mRecorderErrorCallback);
                mRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
                mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
                mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
                mRecorder.setOutputFile(fileName);
                mRecorder.setVideoFrameRate(frameRate);
                mRecorder.setVideoSize(videoWidth, videoHeight);
                mRecorder.setVideoEncoder(videoEncoder);
                mRecorder.setAudioEncoder(audioEncoder);
                mRecorder.setVideoEncodingBitRate(bitRate);

                Log.v(TAG, "mediaRecorder setPreview");
                mRecorder.setPreviewDisplay(surfaceHolder.getSurface());
                mRecorder.prepare();
                mRecorder.start();
                Thread.sleep(recordDuration);
                Log.v(TAG, "Before stop");
                mRecorder.stop();
                mRecorder.release();

                //start the playback
                MediaPlayer mp = new MediaPlayer();
                mp.setDataSource(fileName);
                mp.setDisplay(MediaFrameworkTest.mSurfaceView.getHolder());
                mp.prepare();
                mp.start();
                Thread.sleep(recordDuration);
                mp.release();
                validateRecordedVideo(fileName);
                if (removeVideo) {
                    removeRecordedVideo(fileName);
                }
                if (i == 0) {
                    mOutput.write(i + 1);
                } else {
                    mOutput.write(String.format(", %d", (i + 1)));
                }
            }
        } catch (Exception e) {
            Log.e(TAG, e.toString());
            fail("Record and playback");
        }
    
public voidremoveRecordedVideo(java.lang.String fileName)

        File video = new File(fileName);
        Log.v(TAG, "remove recorded video " + fileName);
        video.delete();
    
private voidrunOnLooper(java.lang.Runnable command)

        final Semaphore sem = new Semaphore(0);
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                try {
                    command.run();
                } finally {
                    sem.release();
                }
            }
        });
        if (! sem.tryAcquire(WAIT_TIMEOUT, TimeUnit.MILLISECONDS)) {
            fail("Failed to run the command on the looper.");
        }
    
protected voidsetUp()

        final Semaphore sem = new Semaphore(0);
        mLooperThread = new Thread() {
            @Override
            public void run() {
                Log.v(TAG, "starting looper");
                Looper.prepare();
                mHandler = new Handler();
                sem.release();
                Looper.loop();
                Log.v(TAG, "quit looper");
            }
        };
        mLooperThread.start();
        if (! sem.tryAcquire(WAIT_TIMEOUT, TimeUnit.MILLISECONDS)) {
            fail("Failed to start the looper.");
        }
        //Insert a 2 second before launching the test activity. This is
        //the workaround for the race condition of requesting the updated surface.
        Thread.sleep(2000);
        getActivity();
        super.setUp();

        File stressOutFile = new File(String.format("%s/%s",
                Environment.getExternalStorageDirectory(), MEDIA_STRESS_OUTPUT));
        mOutput = new BufferedWriter(new FileWriter(stressOutFile, true));
        mOutput.write(this.getName() + "\n");
    
protected voidtearDown()

        if (mHandler != null) {
            mHandler.getLooper().quit();
            mHandler = null;
        }
        if (mLooperThread != null) {
            mLooperThread.join(WAIT_TIMEOUT);
            if (mLooperThread.isAlive()) {
                fail("Failed to stop the looper.");
            }
            mLooperThread = null;
        }
        mOutput.write("\n\n");
        mOutput.close();
        super.tearDown();
    
public voidtestStressRecordVideoAndPlayback1080P()

        recordVideoAndPlayback(CamcorderProfile.QUALITY_1080P);
    
public voidtestStressRecordVideoAndPlayback480P()

        recordVideoAndPlayback(CamcorderProfile.QUALITY_480P);
    
public voidtestStressRecordVideoAndPlayback720P()

        recordVideoAndPlayback(CamcorderProfile.QUALITY_720P);
    
public voidtestStressTimeLapse()

        SurfaceHolder mSurfaceHolder;
        mSurfaceHolder = MediaFrameworkTest.mSurfaceView.getHolder();
        int recordDuration = MediaRecorderStressTestRunner.mTimeLapseDuration;
        boolean removeVideo = MediaRecorderStressTestRunner.mRemoveVideo;
        double captureRate = MediaRecorderStressTestRunner.mCaptureRate;
        Log.v(TAG, "Start camera time lapse stress:");
        mOutput.write("Total number of loops: " + NUMBER_OF_TIME_LAPSE_LOOPS + "\n");

        try {
            for (int i = 0, n = Camera.getNumberOfCameras(); i < n; i++) {
                mOutput.write("No of loop: camera " + i);
                for (int j = 0; j < NUMBER_OF_TIME_LAPSE_LOOPS; j++) {
                    String fileName = String.format("%s/temp%d_%d%s",
                            Environment.getExternalStorageDirectory(), i, j, OUTPUT_FILE_EXT);
                    Log.v(TAG, fileName);
                    runOnLooper(new Runnable() {
                        @Override
                        public void run() {
                            mRecorder = new MediaRecorder();
                        }
                    });

                    // Set callback
                    mRecorder.setOnErrorListener(mRecorderErrorCallback);

                    // Set video source
                    mRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);

                    // Set camcorder profile for time lapse
                    CamcorderProfile profile =
                        CamcorderProfile.get(j, CamcorderProfile.QUALITY_TIME_LAPSE_HIGH);
                    mRecorder.setProfile(profile);

                    // Set the timelapse setting; 0.1 = 10 sec timelapse, 0.5 = 2 sec timelapse, etc
                    // http://developer.android.com/guide/topics/media/camera.html#time-lapse-video
                    mRecorder.setCaptureRate(captureRate);

                    // Set output file
                    mRecorder.setOutputFile(fileName);

                    // Set the preview display
                    Log.v(TAG, "mediaRecorder setPreviewDisplay");
                    mRecorder.setPreviewDisplay(mSurfaceHolder.getSurface());

                    mRecorder.prepare();
                    mRecorder.start();
                    Thread.sleep(recordDuration);
                    Log.v(TAG, "Before stop");
                    mRecorder.stop();
                    mRecorder.release();

                    // Start the playback
                    MediaPlayer mp = new MediaPlayer();
                    mp.setDataSource(fileName);
                    mp.setDisplay(mSurfaceHolder);
                    mp.prepare();
                    mp.start();
                    Thread.sleep(TIME_LAPSE_PLAYBACK_WAIT_TIME);
                    mp.release();
                    validateRecordedVideo(fileName);
                    if (removeVideo) {
                        removeRecordedVideo(fileName);
                    }

                    if (j == 0) {
                        mOutput.write(j + 1);
                    } else {
                        mOutput.write(String.format(", %d", (j + 1)));
                    }
                }
            }
        } catch (IllegalStateException e) {
            Log.e(TAG, e.toString());
            fail("Camera time lapse stress test IllegalStateException");
        } catch (IOException e) {
            Log.e(TAG, e.toString());
            fail("Camera time lapse stress test IOException");
        } catch (Exception e) {
            Log.e(TAG, e.toString());
            fail("Camera time lapse stress test Exception");
        }
    
public voidvalidateRecordedVideo(java.lang.String recordedFile)

        try {
            MediaPlayer mp = new MediaPlayer();
            mp.setDataSource(recordedFile);
            mp.prepare();
            int duration = mp.getDuration();
            if (duration <= 0){
                fail("stressRecordAndPlayback");
            }
            mp.release();
        } catch (Exception e) {
            fail("stressRecordAndPlayback");
        }