Search in sources :

Example 46 with MediaRecorder

use of android.media.MediaRecorder in project android_frameworks_base by crdroidandroid.

the class MediaRecorderStressTest method testStressTimeLapse.

// Test case for stressing time lapse
@LargeTest
public void testStressTimeLapse() throws Exception {
    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");
    }
}
Also used : CamcorderProfile(android.media.CamcorderProfile) IOException(java.io.IOException) IOException(java.io.IOException) SurfaceHolder(android.view.SurfaceHolder) MediaRecorder(android.media.MediaRecorder) MediaPlayer(android.media.MediaPlayer) LargeTest(android.test.suitebuilder.annotation.LargeTest)

Example 47 with MediaRecorder

use of android.media.MediaRecorder in project android_frameworks_base by crdroidandroid.

the class CodecTest method mediaRecorderRecord.

public static boolean mediaRecorderRecord(String filePath) {
    Log.v(TAG, "SoundRecording - " + filePath);
    //This test is only for the short media file
    int duration = 0;
    try {
        MediaRecorder mRecorder = new MediaRecorder();
        mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
        mRecorder.setOutputFile(filePath);
        mRecorder.prepare();
        mRecorder.start();
        Thread.sleep(500);
        mRecorder.stop();
        Log.v(TAG, "sound recorded");
        mRecorder.release();
    } catch (Exception e) {
        Log.v(TAG, e.toString());
    }
    //Verify the recorded file
    MediaPlayer mp = new MediaPlayer();
    try {
        mp.setDataSource(filePath);
        mp.prepare();
        duration = mp.getDuration();
        Log.v(TAG, "Duration " + duration);
        mp.release();
    } catch (Exception e) {
    }
    //Check the record media file length is greate than zero
    if (duration > 0)
        return true;
    else
        return false;
}
Also used : MediaRecorder(android.media.MediaRecorder) IOException(java.io.IOException) MediaPlayer(android.media.MediaPlayer)

Example 48 with MediaRecorder

use of android.media.MediaRecorder in project android_packages_apps_Camera by CyanogenMod.

the class VideoModule method initializeRecorder.

// Prepares media recorder.
private void initializeRecorder() {
    Log.v(TAG, "initializeRecorder");
    // If the mCameraDevice is null, then this activity is going to finish
    if (mActivity.mCameraDevice == null)
        return;
    if (!ApiHelper.HAS_SURFACE_TEXTURE_RECORDING && ApiHelper.HAS_SURFACE_TEXTURE) {
        // Set the SurfaceView to visible so the surface gets created.
        // surfaceCreated() is called immediately when the visibility is
        // changed to visible. Thus, mSurfaceViewReady should become true
        // right after calling setVisibility().
        mPreviewSurfaceView.setVisibility(View.VISIBLE);
        if (!mSurfaceViewReady)
            return;
    }
    Intent intent = mActivity.getIntent();
    Bundle myExtras = intent.getExtras();
    mVideoWidth = mProfile.videoFrameWidth;
    mVideoHeight = mProfile.videoFrameHeight;
    long requestedSizeLimit = 0;
    closeVideoFileDescriptor();
    if (mIsVideoCaptureIntent && myExtras != null) {
        Uri saveUri = (Uri) myExtras.getParcelable(MediaStore.EXTRA_OUTPUT);
        if (saveUri != null) {
            try {
                mVideoFileDescriptor = mContentResolver.openFileDescriptor(saveUri, "rw");
                mCurrentVideoUri = saveUri;
            } catch (java.io.FileNotFoundException ex) {
                // invalid uri
                Log.e(TAG, ex.toString());
            }
        }
        requestedSizeLimit = myExtras.getLong(MediaStore.EXTRA_SIZE_LIMIT);
    }
    mMediaRecorder = new MediaRecorder();
    setupMediaRecorderPreviewDisplay();
    // Unlock the camera object before passing it to media recorder.
    mActivity.mCameraDevice.unlock();
    mMediaRecorder.setCamera(mActivity.mCameraDevice.getCamera());
    if (!mCaptureTimeLapse) {
        mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
    }
    mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
    mMediaRecorder.setProfile(mProfile);
    mMediaRecorder.setMaxDuration(mMaxVideoDurationInMs);
    if (mCaptureTimeLapse) {
        double fps = 1000 / (double) mTimeBetweenTimeLapseFrameCaptureMs;
        setCaptureRate(mMediaRecorder, fps);
    }
    setRecordLocation();
    // instead.
    if (mVideoFileDescriptor != null) {
        mMediaRecorder.setOutputFile(mVideoFileDescriptor.getFileDescriptor());
    } else {
        generateVideoFilename(mProfile.fileFormat);
        mMediaRecorder.setOutputFile(mVideoFilename);
    }
    // Set maximum file size.
    long maxFileSize = mActivity.getStorageSpace() - Storage.LOW_STORAGE_THRESHOLD;
    if (requestedSizeLimit > 0 && requestedSizeLimit < maxFileSize) {
        maxFileSize = requestedSizeLimit;
    }
    try {
        mMediaRecorder.setMaxFileSize(maxFileSize);
    } catch (RuntimeException exception) {
    // We are going to ignore failure of setMaxFileSize here, as
    // a) The composer selected may simply not support it, or
    // b) The underlying media framework may not handle 64-bit range
    // on the size restriction.
    }
    // See android.hardware.Camera.Parameters.setRotation for
    // documentation.
    // Note that mOrientation here is the device orientation, which is the opposite of
    // what activity.getWindowManager().getDefaultDisplay().getRotation() would return,
    // which is the orientation the graphics need to rotate in order to render correctly.
    int rotation = 0;
    if (mOrientation != OrientationEventListener.ORIENTATION_UNKNOWN) {
        CameraInfo info = CameraHolder.instance().getCameraInfo()[mCameraId];
        if (info.facing == CameraInfo.CAMERA_FACING_FRONT) {
            rotation = (info.orientation - mOrientation + 360) % 360;
        } else {
            // back-facing camera
            rotation = (info.orientation + mOrientation) % 360;
        }
    }
    mMediaRecorder.setOrientationHint(rotation);
    try {
        mMediaRecorder.prepare();
    } catch (IOException e) {
        Log.e(TAG, "prepare failed for " + mVideoFilename, e);
        releaseMediaRecorder();
        throw new RuntimeException(e);
    }
    mMediaRecorder.setOnErrorListener(this);
    mMediaRecorder.setOnInfoListener(this);
}
Also used : Bundle(android.os.Bundle) Intent(android.content.Intent) MediaRecorder(android.media.MediaRecorder) IOException(java.io.IOException) Uri(android.net.Uri) CameraInfo(android.hardware.Camera.CameraInfo)

Example 49 with MediaRecorder

use of android.media.MediaRecorder in project android_frameworks_base by crdroidandroid.

the class Camera2RecordingTest method videoSnapshotHelper.

/**
     * Simple wrapper to wrap normal/burst video snapshot tests
     */
private void videoSnapshotHelper(boolean burstTest) throws Exception {
    for (String id : mCameraIds) {
        try {
            Log.i(TAG, "Testing video snapshot for camera " + id);
            // Re-use the MediaRecorder object for the same camera device.
            mMediaRecorder = new MediaRecorder();
            openDevice(id);
            if (!mStaticInfo.isColorOutputSupported()) {
                Log.i(TAG, "Camera " + id + " does not support color outputs, skipping");
                continue;
            }
            initSupportedVideoSize(id);
            // Test iteration starts...
            for (int iteration = 0; iteration < getIterationCount(); ++iteration) {
                Log.v(TAG, String.format("Video snapshot: %d/%d", iteration + 1, getIterationCount()));
                videoSnapshotTestByCamera(burstTest);
                getResultPrinter().printStatus(getIterationCount(), iteration + 1, id);
                Thread.sleep(getTestWaitIntervalMs());
            }
        } finally {
            closeDevice();
            releaseRecorder();
        }
    }
}
Also used : MediaRecorder(android.media.MediaRecorder)

Example 50 with MediaRecorder

use of android.media.MediaRecorder in project android_frameworks_base by crdroidandroid.

the class MediaRecorderTest method recordVideoFromSurface.

private boolean recordVideoFromSurface(int frameRate, int captureRate, int width, int height, int videoFormat, int outFormat, String outFile, boolean videoOnly, Surface persistentSurface) {
    Log.v(TAG, "recordVideoFromSurface");
    MediaRecorder recorder = new MediaRecorder();
    // normal capture at 33ms / frame
    int sleepTime = 33;
    Surface surface = null;
    try {
        if (!videoOnly) {
            recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        }
        recorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);
        recorder.setOutputFormat(outFormat);
        recorder.setOutputFile(outFile);
        recorder.setVideoFrameRate(frameRate);
        if (captureRate > 0) {
            recorder.setCaptureRate(captureRate);
            sleepTime = 1000 / captureRate;
        }
        recorder.setVideoSize(width, height);
        recorder.setVideoEncoder(videoFormat);
        if (!videoOnly) {
            recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
        }
        if (persistentSurface != null) {
            Log.v(TAG, "using persistent surface");
            surface = persistentSurface;
            recorder.setInputSurface(surface);
        }
        recorder.prepare();
        if (persistentSurface == null) {
            surface = recorder.getSurface();
        }
        Paint paint = new Paint();
        paint.setTextSize(16);
        paint.setColor(Color.RED);
        int i;
        /* Test: draw 10 frames at 30fps before start
             * these should be dropped and not causing malformed stream.
             */
        for (i = 0; i < 10; i++) {
            Canvas canvas = surface.lockCanvas(null);
            int background = (i * 255 / 99);
            canvas.drawARGB(255, background, background, background);
            String text = "Frame #" + i;
            canvas.drawText(text, 100, 100, paint);
            surface.unlockCanvasAndPost(canvas);
            Thread.sleep(sleepTime);
        }
        Log.v(TAG, "start");
        recorder.start();
        /* Test: draw another 90 frames at 30fps after start */
        for (i = 10; i < 100; i++) {
            Canvas canvas = surface.lockCanvas(null);
            int background = (i * 255 / 99);
            canvas.drawARGB(255, background, background, background);
            String text = "Frame #" + i;
            canvas.drawText(text, 100, 100, paint);
            surface.unlockCanvasAndPost(canvas);
            Thread.sleep(sleepTime);
        }
        Log.v(TAG, "stop");
        recorder.stop();
    } catch (Exception e) {
        Log.v(TAG, "record video failed: " + e.toString());
        return false;
    } finally {
        recorder.release();
        // release surface if not using persistent surface
        if (persistentSurface == null && surface != null) {
            surface.release();
        }
    }
    return true;
}
Also used : Canvas(android.graphics.Canvas) MediaRecorder(android.media.MediaRecorder) Paint(android.graphics.Paint) Paint(android.graphics.Paint) Surface(android.view.Surface)

Aggregations

MediaRecorder (android.media.MediaRecorder)107 IOException (java.io.IOException)58 MediaPlayer (android.media.MediaPlayer)18 Paint (android.graphics.Paint)15 CamcorderProfile (android.media.CamcorderProfile)15 SurfaceHolder (android.view.SurfaceHolder)12 Surface (android.view.Surface)11 File (java.io.File)9 Camera (android.hardware.Camera)8 MutableFrameFormat (android.filterfw.core.MutableFrameFormat)6 Point (android.filterfw.geometry.Point)6 StreamConfigurationMap (android.hardware.camera2.params.StreamConfigurationMap)6 LargeTest (android.test.suitebuilder.annotation.LargeTest)6 Size (android.util.Size)6 Canvas (android.graphics.Canvas)5 Range (android.util.Range)5 SimpleCaptureCallback (com.android.mediaframeworktest.helpers.CameraTestUtils.SimpleCaptureCallback)5 Activity (android.app.Activity)4 Point (android.graphics.Point)4 Uri (android.net.Uri)3