2222import java .io .BufferedWriter ;
2323import java .io .File ;
2424import java .io .FileWriter ;
25+ import java .io .IOException ;
2526import java .io .Writer ;
2627import java .util .concurrent .Semaphore ;
2728import java .util .concurrent .TimeUnit ;
2829
2930import android .hardware .Camera ;
31+ import android .media .CamcorderProfile ;
3032import android .media .MediaPlayer ;
3133import android .media .MediaRecorder ;
3234import android .os .Handler ;
3941
4042/**
4143 * Junit / Instrumentation test case for the media player api
42-
43- */
44- public class MediaRecorderStressTest extends ActivityInstrumentationTestCase2 <MediaFrameworkTest > {
45-
46-
44+ */
45+ public class MediaRecorderStressTest extends ActivityInstrumentationTestCase2 <MediaFrameworkTest > {
46+
4747 private String TAG = "MediaRecorderStressTest" ;
4848 private MediaRecorder mRecorder ;
4949 private Camera mCamera ;
50-
50+
5151 private static final int NUMBER_OF_CAMERA_STRESS_LOOPS = 100 ;
5252 private static final int NUMBER_OF_RECORDER_STRESS_LOOPS = 100 ;
5353 private static final int NUMBER_OF_RECORDERANDPLAY_STRESS_LOOPS = 50 ;
5454 private static final int NUMBER_OF_SWTICHING_LOOPS_BW_CAMERA_AND_RECORDER = 200 ;
55- private static final long WAIT_TIME_CAMERA_TEST = 3000 ; // 3 second
56- private static final long WAIT_TIME_RECORDER_TEST = 6000 ; // 6 second
55+ private static final int NUMBER_OF_TIME_LAPSE_LOOPS = 25 ;
56+ private static final int TIME_LAPSE_PLAYBACK_WAIT_TIME = 5 * 1000 ; // 5 seconds
57+ private static final long WAIT_TIME_CAMERA_TEST = 3 * 1000 ; // 3 seconds
58+ private static final long WAIT_TIME_RECORDER_TEST = 6 * 1000 ; // 6 seconds
5759 private static final String OUTPUT_FILE = "/sdcard/temp" ;
5860 private static final String OUTPUT_FILE_EXT = ".3gp" ;
5961 private static final String MEDIA_STRESS_OUTPUT =
6062 "/sdcard/mediaStressOutput.txt" ;
6163 private final CameraErrorCallback mCameraErrorCallback = new CameraErrorCallback ();
6264 private final RecorderErrorCallback mRecorderErrorCallback = new RecorderErrorCallback ();
6365
64- private final static int WAIT_TIMEOUT = 10000 ;
66+ private final static int WAIT_TIMEOUT = 10 * 1000 ; // 10 seconds
6567 private Thread mLooperThread ;
6668 private Handler mHandler ;
6769
@@ -306,7 +308,7 @@ public void validateRecordedVideo(String recorded_file) {
306308 }
307309 }
308310
309- public void removeRecodedVideo (String filename ){
311+ public void removeRecordedVideo (String filename ){
310312 File video = new File (filename );
311313 Log .v (TAG , "remove recorded video " + filename );
312314 video .delete ();
@@ -381,7 +383,7 @@ public void run() {
381383 mp .release ();
382384 validateRecordedVideo (filename );
383385 if (remove_video ) {
384- removeRecodedVideo (filename );
386+ removeRecordedVideo (filename );
385387 }
386388 output .write (", " + i );
387389 }
@@ -392,4 +394,90 @@ public void run() {
392394 output .write ("\n \n " );
393395 output .close ();
394396 }
397+
398+ // Test case for stressing time lapse
399+ @ LargeTest
400+ public void testStressTimeLapse () throws Exception {
401+ SurfaceHolder mSurfaceHolder ;
402+ mSurfaceHolder = MediaFrameworkTest .mSurfaceView .getHolder ();
403+ int record_duration = MediaRecorderStressTestRunner .mTimeLapseDuration ;
404+ boolean remove_video = MediaRecorderStressTestRunner .mRemoveVideo ;
405+ double captureRate = MediaRecorderStressTestRunner .mCaptureRate ;
406+ String filename ;
407+ File stressOutFile = new File (MEDIA_STRESS_OUTPUT );
408+ Writer output = new BufferedWriter (new FileWriter (stressOutFile , true ));
409+ output .write ("Start camera time lapse stress:\n " );
410+ output .write ("Total number of loops: " + NUMBER_OF_TIME_LAPSE_LOOPS + "\n " );
411+
412+ try {
413+ output .write ("No of loop: " );
414+ for (int i = 0 ; i < NUMBER_OF_TIME_LAPSE_LOOPS ; i ++) {
415+ filename = OUTPUT_FILE + i + OUTPUT_FILE_EXT ;
416+ Log .v (TAG , filename );
417+ runOnLooper (new Runnable () {
418+ @ Override
419+ public void run () {
420+ mRecorder = new MediaRecorder ();
421+ }
422+ });
423+
424+ // Set callback
425+ mRecorder .setOnErrorListener (mRecorderErrorCallback );
426+
427+ // Set video source
428+ mRecorder .setVideoSource (MediaRecorder .VideoSource .CAMERA );
429+
430+ // Set camcorder profile for time lapse
431+ CamcorderProfile profile =
432+ CamcorderProfile .get (CamcorderProfile .QUALITY_TIME_LAPSE_HIGH );
433+ mRecorder .setProfile (profile );
434+
435+ // Set the timelapse setting; 0.1 = 10 sec timelapse, 0.5 = 2 sec timelapse, etc.
436+ // http://developer.android.com/guide/topics/media/camera.html#time-lapse-video
437+ mRecorder .setCaptureRate (captureRate );
438+
439+ // Set output file
440+ mRecorder .setOutputFile (filename );
441+
442+ // Set the preview display
443+ Log .v (TAG , "mediaRecorder setPreviewDisplay" );
444+ mRecorder .setPreviewDisplay (mSurfaceHolder .getSurface ());
445+
446+ mRecorder .prepare ();
447+ mRecorder .start ();
448+ Thread .sleep (record_duration );
449+ Log .v (TAG , "Before stop" );
450+ mRecorder .stop ();
451+ mRecorder .release ();
452+
453+ // Start the playback
454+ MediaPlayer mp = new MediaPlayer ();
455+ mp .setDataSource (filename );
456+ mp .setDisplay (mSurfaceHolder );
457+ mp .prepare ();
458+ mp .start ();
459+ Thread .sleep (TIME_LAPSE_PLAYBACK_WAIT_TIME );
460+ mp .release ();
461+ validateRecordedVideo (filename );
462+ if (remove_video ) {
463+ removeRecordedVideo (filename );
464+ }
465+ output .write (", " + i );
466+ }
467+ }
468+ catch (IllegalStateException e ) {
469+ assertTrue ("Camera time lapse stress test IllegalStateException" , false );
470+ Log .v (TAG , e .toString ());
471+ }
472+ catch (IOException e ) {
473+ assertTrue ("Camera time lapse stress test IOException" , false );
474+ Log .v (TAG , e .toString ());
475+ }
476+ catch (Exception e ) {
477+ assertTrue ("Camera time lapse stress test Exception" , false );
478+ Log .v (TAG , e .toString ());
479+ }
480+ output .write ("\n \n " );
481+ output .close ();
482+ }
395483}
0 commit comments