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.media.MediaRecorder;
22 import android.os.Bundle;
23 import android.util.Log;
24 import android.view.SurfaceHolder;
25 import android.view.SurfaceView;
26 import android.view.ViewGroup;
27 
28 import java.io.File;
29 
30 /**
31  * Activity implementing basic access of camera using MediaRecorder API.
32  *
33  * <p />
34  * This will log all errors to {@link android.hardware.multiprocess.camera.cts.ErrorLoggingService}.
35  */
36 public class MediaRecorderCameraActivity extends Activity implements SurfaceHolder.Callback {
37     private static final String TAG = "MediaRecorderCameraActivity";
38 
39     private static final int VIDEO_WIDTH = 640;
40     private static final int VIDEO_HEIGHT = 480;
41     private static final int LAYOUT_WIDTH = VIDEO_WIDTH;
42     private static final int LAYOUT_HEIGHT = VIDEO_HEIGHT;
43 
44     private String mOutputPath;
45     private File mOutFile;
46     private SurfaceView mSurfaceView;
47     private ErrorLoggingService.ErrorServiceConnection mErrorServiceConnection;
48     private MediaRecorder mMediaRecorder;
49 
50     @Override
onCreate(Bundle savedInstanceState)51     protected void onCreate(Bundle savedInstanceState) {
52         Log.i(TAG, "onCreate called.");
53         super.onCreate(savedInstanceState);
54 
55         setContentView(R.layout.surface_view);
56 
57         mErrorServiceConnection = new ErrorLoggingService.ErrorServiceConnection(this);
58         mErrorServiceConnection.start();
59 
60         mMediaRecorder = new MediaRecorder();
61 
62         File filesDir = getPackageManager().isInstantApp()
63                 ? getFilesDir()
64                 : getExternalFilesDir(null);
65 
66         mOutputPath = new File(filesDir, "record.out").getAbsolutePath();
67     }
68 
69     @Override
onResume()70     protected void onResume() {
71         Log.i(TAG, "onResume called.");
72         super.onResume();
73         try {
74 
75             mSurfaceView = (SurfaceView)this.findViewById(R.id.surface_view);
76             ViewGroup.LayoutParams lp = mSurfaceView.getLayoutParams();
77             lp.width = LAYOUT_WIDTH;
78             lp.height = LAYOUT_HEIGHT;
79             mSurfaceView.setLayoutParams(lp);
80 
81             SurfaceHolder holder = mSurfaceView.getHolder();
82             holder.setFixedSize(LAYOUT_WIDTH, LAYOUT_HEIGHT);
83             holder.addCallback(this);
84 
85         } catch (Throwable e) {
86             mErrorServiceConnection.logAsync(TestConstants.EVENT_CAMERA_ERROR, TAG +
87                     " camera exception during connection: " + e);
88             Log.e(TAG, "Runtime error: " + e);
89         }
90     }
91 
92     @Override
onPause()93     protected void onPause() {
94         Log.i(TAG, "onPause called.");
95         super.onPause();
96     }
97 
98     @Override
onDestroy()99     protected void onDestroy() {
100         Log.i(TAG, "onDestroy called.");
101         super.onDestroy();
102         if (mErrorServiceConnection != null) {
103             mErrorServiceConnection.stop();
104             mErrorServiceConnection = null;
105         }
106 
107         if (mOutFile != null && mOutFile.exists()) {
108             mOutFile.delete();
109         }
110 
111         if (mMediaRecorder != null) {
112             mMediaRecorder.stop();
113             mMediaRecorder.release();
114         }
115     }
116 
117     @Override
surfaceCreated(SurfaceHolder holder)118     public void surfaceCreated(SurfaceHolder holder) {
119     }
120 
121     @Override
surfaceChanged(SurfaceHolder holder, int format, int width, int height)122     public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
123         try {
124             mOutFile = new File(mOutputPath);
125             mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
126             mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
127             mMediaRecorder.setPreviewDisplay(mSurfaceView.getHolder().getSurface());
128             mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT);
129             mMediaRecorder.setVideoSize(VIDEO_WIDTH, VIDEO_HEIGHT);
130             mMediaRecorder.setOutputFile(mOutputPath);
131             mMediaRecorder.prepare();
132             mMediaRecorder.start();
133 
134             mErrorServiceConnection.logAsync(TestConstants.EVENT_CAMERA_CONNECT,
135                     TAG + " camera connected");
136         } catch (Throwable e) {
137             mErrorServiceConnection.logAsync(TestConstants.EVENT_CAMERA_ERROR, TAG +
138                     " camera exception during connection: " + e);
139             Log.e(TAG, "Runtime error: " + e);
140         }
141     }
142 
143     @Override
surfaceDestroyed(SurfaceHolder holder)144     public void surfaceDestroyed(SurfaceHolder holder) {
145     }
146 }
147