use of android.media.MediaRecorder in project android_frameworks_base by ResurrectionRemix.
the class MediaEncoderFilter method startRecording.
private void startRecording(FilterContext context) {
if (mLogVerbose)
Log.v(TAG, "Starting recording");
// Create a frame representing the screen
MutableFrameFormat screenFormat = new MutableFrameFormat(FrameFormat.TYPE_BYTE, FrameFormat.TARGET_GPU);
screenFormat.setBytesPerSample(4);
int width, height;
boolean widthHeightSpecified = mWidth > 0 && mHeight > 0;
// of that in the profile.
if (mProfile != null && !widthHeightSpecified) {
width = mProfile.videoFrameWidth;
height = mProfile.videoFrameHeight;
} else {
width = mWidth;
height = mHeight;
}
screenFormat.setDimensions(width, height);
mScreen = (GLFrame) context.getFrameManager().newBoundFrame(screenFormat, GLFrame.EXISTING_FBO_BINDING, 0);
// Initialize the media recorder
mMediaRecorder = new MediaRecorder();
updateMediaRecorderParams();
try {
mMediaRecorder.prepare();
} catch (IllegalStateException e) {
throw e;
} catch (IOException e) {
throw new RuntimeException("IOException in" + "MediaRecorder.prepare()!", e);
} catch (Exception e) {
throw new RuntimeException("Unknown Exception in" + "MediaRecorder.prepare()!", e);
}
// Make sure start() is called before trying to
// register the surface. The native window handle needed to create
// the surface is initiated in start()
mMediaRecorder.start();
if (mLogVerbose)
Log.v(TAG, "Open: registering surface from Mediarecorder");
mSurfaceId = context.getGLEnvironment().registerSurfaceFromMediaRecorder(mMediaRecorder);
mNumFramesEncoded = 0;
mRecordingActive = true;
}
use of android.media.MediaRecorder in project android_frameworks_base by ResurrectionRemix.
the class MediaRecorderTest method recordVideoWithPara.
private boolean recordVideoWithPara(VideoEncoderCap videoCap, AudioEncoderCap audioCap, boolean highQuality) {
boolean recordSuccess = false;
int videoEncoder = videoCap.mCodec;
int audioEncoder = audioCap.mCodec;
int videoWidth = highQuality ? videoCap.mMaxFrameWidth : videoCap.mMinFrameWidth;
int videoHeight = highQuality ? videoCap.mMaxFrameHeight : videoCap.mMinFrameHeight;
int videoFps = highQuality ? videoCap.mMaxFrameRate : videoCap.mMinFrameRate;
int videoBitrate = highQuality ? videoCap.mMaxBitRate : videoCap.mMinBitRate;
int audioBitrate = highQuality ? audioCap.mMaxBitRate : audioCap.mMinBitRate;
int audioChannels = highQuality ? audioCap.mMaxChannels : audioCap.mMinChannels;
int audioSamplingRate = highQuality ? audioCap.mMaxSampleRate : audioCap.mMinSampleRate;
//Overide the fps if the min_camera_fps is set
if (MediaFrameworkTestRunner.mMinCameraFps != 0 && MediaFrameworkTestRunner.mMinCameraFps > videoFps) {
videoFps = MediaFrameworkTestRunner.mMinCameraFps;
}
if (videoFps < MIN_VIDEO_FPS) {
videoFps = MIN_VIDEO_FPS;
}
mSurfaceHolder = MediaFrameworkTest.mSurfaceView.getHolder();
String filename = ("/sdcard/" + videoEncoder + "_" + audioEncoder + "_" + highQuality + ".3gp");
try {
Log.v(TAG, "video encoder : " + videoEncoder);
Log.v(TAG, "audio encoder : " + audioEncoder);
Log.v(TAG, "quality : " + (highQuality ? "high" : "low"));
Log.v(TAG, "encoder : " + MediaProfileReader.getVideoCodecName(videoEncoder));
Log.v(TAG, "audio : " + MediaProfileReader.getAudioCodecName(audioEncoder));
Log.v(TAG, "videoWidth : " + videoWidth);
Log.v(TAG, "videoHeight : " + videoHeight);
Log.v(TAG, "videoFPS : " + videoFps);
Log.v(TAG, "videobitrate : " + videoBitrate);
Log.v(TAG, "audioBitrate : " + audioBitrate);
Log.v(TAG, "audioChannel : " + audioChannels);
Log.v(TAG, "AudioSampleRate : " + audioSamplingRate);
MediaRecorder mMediaRecorder = new MediaRecorder();
mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mMediaRecorder.setOutputFile(filename);
mMediaRecorder.setVideoFrameRate(videoFps);
mMediaRecorder.setVideoSize(videoWidth, videoHeight);
mMediaRecorder.setVideoEncodingBitRate(videoBitrate);
mMediaRecorder.setAudioEncodingBitRate(audioBitrate);
mMediaRecorder.setAudioChannels(audioChannels);
mMediaRecorder.setAudioSamplingRate(audioSamplingRate);
mMediaRecorder.setVideoEncoder(videoEncoder);
mMediaRecorder.setAudioEncoder(audioEncoder);
mMediaRecorder.setPreviewDisplay(mSurfaceHolder.getSurface());
mMediaRecorder.prepare();
mMediaRecorder.start();
Thread.sleep(MediaNames.RECORDED_TIME);
mMediaRecorder.stop();
mMediaRecorder.release();
recordSuccess = validateVideo(filename, videoWidth, videoHeight);
} catch (Exception e) {
Log.v(TAG, e.toString());
return false;
}
return recordSuccess;
}
use of android.media.MediaRecorder in project android_frameworks_base by ResurrectionRemix.
the class MediaRecorderTest method validateGetSurface.
private boolean validateGetSurface(boolean useSurface) {
Log.v(TAG, "validateGetSurface, useSurface=" + useSurface);
MediaRecorder recorder = new MediaRecorder();
Surface surface;
boolean success = true;
try {
/* initialization */
if (!useSurface) {
mCamera = Camera.open(CAMERA_ID);
Camera.Parameters parameters = mCamera.getParameters();
parameters.setPreviewSize(352, 288);
parameters.set("orientation", "portrait");
mCamera.setParameters(parameters);
mCamera.unlock();
recorder.setCamera(mCamera);
mSurfaceHolder = MediaFrameworkTest.mSurfaceView.getHolder();
recorder.setPreviewDisplay(mSurfaceHolder.getSurface());
}
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
int videoSource = useSurface ? MediaRecorder.VideoSource.SURFACE : MediaRecorder.VideoSource.CAMERA;
recorder.setVideoSource(videoSource);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setOutputFile(MediaNames.RECORDED_SURFACE_3GP);
recorder.setVideoFrameRate(30);
recorder.setVideoSize(352, 288);
recorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
/* Test: getSurface() before prepare()
* should throw IllegalStateException
*/
try {
surface = recorder.getSurface();
throw new Exception("getSurface failed to throw IllegalStateException");
} catch (IllegalStateException e) {
// OK
}
recorder.prepare();
/* Test: getSurface() after prepare()
* should succeed for surface source
* should fail for camera source
*/
try {
surface = recorder.getSurface();
if (!useSurface) {
throw new Exception("getSurface failed to throw IllegalStateException");
}
} catch (IllegalStateException e) {
if (useSurface) {
throw new Exception("getSurface failed to throw IllegalStateException");
}
}
recorder.start();
/* Test: getSurface() after start()
* should succeed for surface source
* should fail for camera source
*/
try {
surface = recorder.getSurface();
if (!useSurface) {
throw new Exception("getSurface failed to throw IllegalStateException");
}
} catch (IllegalStateException e) {
if (useSurface) {
throw new Exception("getSurface failed to throw IllegalStateException");
}
}
try {
recorder.stop();
} catch (Exception e) {
// stop() could fail if the recording is empty, as we didn't render anything.
// ignore any failure in stop, we just want it stopped.
}
/* Test: getSurface() after stop()
* should throw IllegalStateException
*/
try {
surface = recorder.getSurface();
throw new Exception("getSurface failed to throw IllegalStateException");
} catch (IllegalStateException e) {
// OK
}
} catch (Exception e) {
// fail
success = false;
}
try {
if (mCamera != null) {
mCamera.lock();
mCamera.release();
mCamera = null;
}
recorder.release();
} catch (Exception e) {
success = false;
}
return success;
}
use of android.media.MediaRecorder in project android_frameworks_base by ResurrectionRemix.
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;
}
use of android.media.MediaRecorder in project android_frameworks_base by ResurrectionRemix.
the class MediaRecorderStressTest method recordVideoAndPlayback.
// Helper method for record & playback testing with different camcorder profiles
private void recordVideoAndPlayback(int profile) throws Exception {
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");
}
}
Aggregations