• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.os.Environment;
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 final String OUTPUT_PATH = new File(Environment.getExternalStorageDirectory(),
46                 "record.out").getAbsolutePath();
47 
48     private File mOutFile;
49     private SurfaceView mSurfaceView;
50     private ErrorLoggingService.ErrorServiceConnection mErrorServiceConnection;
51     private MediaRecorder mMediaRecorder;
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 
66     @Override
onResume()67     protected void onResume() {
68         Log.i(TAG, "onResume called.");
69         super.onResume();
70         try {
71 
72             mSurfaceView = (SurfaceView)this.findViewById(R.id.surface_view);
73             ViewGroup.LayoutParams lp = mSurfaceView.getLayoutParams();
74             lp.width = LAYOUT_WIDTH;
75             lp.height = LAYOUT_HEIGHT;
76             mSurfaceView.setLayoutParams(lp);
77 
78             SurfaceHolder holder = mSurfaceView.getHolder();
79             holder.setFixedSize(LAYOUT_WIDTH, LAYOUT_HEIGHT);
80             holder.addCallback(this);
81 
82         } catch (Throwable e) {
83             mErrorServiceConnection.logAsync(TestConstants.EVENT_CAMERA_ERROR, TAG +
84                     " camera exception during connection: " + e);
85             Log.e(TAG, "Runtime error: " + e);
86         }
87     }
88 
89     @Override
onPause()90     protected void onPause() {
91         Log.i(TAG, "onPause called.");
92         super.onPause();
93     }
94 
95     @Override
onDestroy()96     protected void onDestroy() {
97         Log.i(TAG, "onDestroy called.");
98         super.onDestroy();
99         if (mErrorServiceConnection != null) {
100             mErrorServiceConnection.stop();
101             mErrorServiceConnection = null;
102         }
103 
104         if (mOutFile != null && mOutFile.exists()) {
105             mOutFile.delete();
106         }
107 
108         if (mMediaRecorder != null) {
109             mMediaRecorder.stop();
110             mMediaRecorder.release();
111         }
112     }
113 
114     @Override
surfaceCreated(SurfaceHolder holder)115     public void surfaceCreated(SurfaceHolder holder) {
116     }
117 
118     @Override
surfaceChanged(SurfaceHolder holder, int format, int width, int height)119     public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
120         try {
121             mOutFile = new File(OUTPUT_PATH);
122             mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
123             mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
124             mMediaRecorder.setPreviewDisplay(mSurfaceView.getHolder().getSurface());
125             mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT);
126             mMediaRecorder.setVideoSize(VIDEO_WIDTH, VIDEO_HEIGHT);
127             mMediaRecorder.setOutputFile(OUTPUT_PATH);
128             mMediaRecorder.prepare();
129             mMediaRecorder.start();
130 
131             mErrorServiceConnection.logAsync(TestConstants.EVENT_CAMERA_CONNECT,
132                     TAG + " camera connected");
133         } catch (Throwable e) {
134             mErrorServiceConnection.logAsync(TestConstants.EVENT_CAMERA_ERROR, TAG +
135                     " camera exception during connection: " + e);
136             Log.e(TAG, "Runtime error: " + e);
137         }
138     }
139 
140     @Override
surfaceDestroyed(SurfaceHolder holder)141     public void surfaceDestroyed(SurfaceHolder holder) {
142     }
143 }
144