1 /* 2 * Copyright (C) 2016 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package android.hardware.multiprocess.camera.cts; 18 19 import android.app.Activity; 20 import android.camera.cts.R; 21 import android.hardware.Camera; 22 import android.media.MediaRecorder; 23 import android.os.Bundle; 24 import android.util.Log; 25 import android.view.SurfaceHolder; 26 import android.view.SurfaceView; 27 import android.view.ViewGroup; 28 29 import java.io.File; 30 31 /** 32 * Activity implementing basic access of camera using MediaRecorder API. 33 * 34 * <p /> 35 * This will log all errors to {@link android.hardware.multiprocess.camera.cts.ErrorLoggingService}. 36 */ 37 public class MediaRecorderCameraActivity extends Activity implements SurfaceHolder.Callback { 38 private static final String TAG = "MediaRecorderCameraActivity"; 39 40 private static final int VIDEO_WIDTH = 640; 41 private static final int VIDEO_HEIGHT = 480; 42 private static final int LAYOUT_WIDTH = VIDEO_WIDTH; 43 private static final int LAYOUT_HEIGHT = VIDEO_HEIGHT; 44 45 private String mOutputPath; 46 private File mOutFile; 47 private SurfaceView mSurfaceView; 48 private ErrorLoggingService.ErrorServiceConnection mErrorServiceConnection; 49 private MediaRecorder mMediaRecorder; 50 51 private Camera mCamera; 52 53 @Override onCreate(Bundle savedInstanceState)54 protected void onCreate(Bundle savedInstanceState) { 55 Log.i(TAG, "onCreate called."); 56 super.onCreate(savedInstanceState); 57 58 setContentView(R.layout.surface_view); 59 60 mErrorServiceConnection = new ErrorLoggingService.ErrorServiceConnection(this); 61 mErrorServiceConnection.start(); 62 63 mMediaRecorder = new MediaRecorder(); 64 65 File filesDir = getPackageManager().isInstantApp() 66 ? getFilesDir() 67 : getExternalFilesDir(null); 68 69 mOutputPath = new File(filesDir, "record.out").getAbsolutePath(); 70 } 71 72 @Override onResume()73 protected void onResume() { 74 Log.i(TAG, "onResume called."); 75 super.onResume(); 76 try { 77 78 mSurfaceView = (SurfaceView)this.findViewById(R.id.surface_view); 79 ViewGroup.LayoutParams lp = mSurfaceView.getLayoutParams(); 80 lp.width = LAYOUT_WIDTH; 81 lp.height = LAYOUT_HEIGHT; 82 mSurfaceView.setLayoutParams(lp); 83 84 SurfaceHolder holder = mSurfaceView.getHolder(); 85 holder.setFixedSize(LAYOUT_WIDTH, LAYOUT_HEIGHT); 86 holder.addCallback(this); 87 88 } catch (Throwable e) { 89 mErrorServiceConnection.logAsync(TestConstants.EVENT_CAMERA_ERROR, TAG + 90 " camera exception during connection: " + e); 91 Log.e(TAG, "Runtime error: " + e); 92 } 93 } 94 95 @Override onPause()96 protected void onPause() { 97 Log.i(TAG, "onPause called."); 98 super.onPause(); 99 } 100 101 @Override onDestroy()102 protected void onDestroy() { 103 Log.i(TAG, "onDestroy called."); 104 super.onDestroy(); 105 if (mErrorServiceConnection != null) { 106 mErrorServiceConnection.stop(); 107 mErrorServiceConnection = null; 108 } 109 110 if (mOutFile != null && mOutFile.exists()) { 111 mOutFile.delete(); 112 } 113 114 if (mMediaRecorder != null) { 115 mMediaRecorder.stop(); 116 mMediaRecorder.release(); 117 } 118 119 if (mCamera != null) { 120 mCamera.release(); 121 mCamera = null; 122 } 123 } 124 125 @Override surfaceCreated(SurfaceHolder holder)126 public void surfaceCreated(SurfaceHolder holder) { 127 } 128 129 @Override surfaceChanged(SurfaceHolder holder, int format, int width, int height)130 public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { 131 if (Camera.getNumberOfCameras() > 0) { 132 // Open camera 133 mCamera = Camera.open(0); 134 } 135 if (mCamera == null) { 136 mErrorServiceConnection.logAsync(TestConstants.EVENT_CAMERA_ERROR, TAG + 137 " no cameras available."); 138 return; 139 } 140 try { 141 mOutFile = new File(mOutputPath); 142 mCamera.unlock(); 143 mMediaRecorder.setCamera(mCamera); 144 mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA); 145 mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT); 146 mMediaRecorder.setPreviewDisplay(mSurfaceView.getHolder().getSurface()); 147 mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT); 148 mMediaRecorder.setVideoSize(VIDEO_WIDTH, VIDEO_HEIGHT); 149 mMediaRecorder.setOutputFile(mOutputPath); 150 mMediaRecorder.prepare(); 151 mMediaRecorder.start(); 152 153 mErrorServiceConnection.logAsync(TestConstants.EVENT_CAMERA_CONNECT, 154 TAG + " camera connected"); 155 } catch (Throwable e) { 156 mErrorServiceConnection.logAsync(TestConstants.EVENT_CAMERA_ERROR, TAG + 157 " camera exception during connection: " + e); 158 Log.e(TAG, "Runtime error: " + e); 159 } 160 } 161 162 @Override surfaceDestroyed(SurfaceHolder holder)163 public void surfaceDestroyed(SurfaceHolder holder) { 164 } 165 } 166