blob: 8e6d5cb44577bd6ce4085ae31ca5175e7435e784 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2008 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
Xia Wangdb602852011-09-27 16:00:20 -070017package com.android.mediaframeworktest.functional.mediarecorder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018
19import com.android.mediaframeworktest.MediaFrameworkTest;
20import com.android.mediaframeworktest.MediaNames;
21
22import java.io.*;
23
24import android.content.Context;
Yu Shan Emily Lau20710432009-04-23 21:46:55 -070025import android.hardware.Camera;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026import android.media.MediaPlayer;
27import android.media.MediaRecorder;
James Dong1b7babd2010-02-16 14:38:23 -080028import android.media.EncoderCapabilities;
29import android.media.EncoderCapabilities.VideoEncoderCap;
30import android.media.EncoderCapabilities.AudioEncoderCap;
Yu Shan Emily Lau62f755a2011-02-25 20:07:24 -080031import android.test.ActivityInstrumentationTestCase2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032import android.util.Log;
33import android.view.SurfaceHolder;
34import android.view.SurfaceView;
Yu Shan Emily Lau16193672009-09-14 16:23:12 -070035import com.android.mediaframeworktest.MediaProfileReader;
Yu Shan Emily Lau99b45232011-11-23 19:02:01 -080036import com.android.mediaframeworktest.MediaFrameworkTestRunner;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037
38import android.test.suitebuilder.annotation.LargeTest;
39import android.test.suitebuilder.annotation.Suppress;
James Dong1b7babd2010-02-16 14:38:23 -080040import java.util.List;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041
42
43/**
44 * Junit / Instrumentation test case for the media recorder api
45 */
Yu Shan Emily Lau62f755a2011-02-25 20:07:24 -080046public class MediaRecorderTest extends ActivityInstrumentationTestCase2<MediaFrameworkTest> {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047 private String TAG = "MediaRecorderTest";
48 private int mOutputDuration =0;
49 private int mOutputVideoWidth = 0;
50 private int mOutputVideoHeight= 0 ;
51
52 private SurfaceHolder mSurfaceHolder = null;
53 private MediaRecorder mRecorder;
Yu Shan Emily Lau16193672009-09-14 16:23:12 -070054
55 private int MIN_VIDEO_FPS = 5;
56
Yu Shan Emily Laue35b3e02012-04-26 17:10:29 -070057 private static final int CAMERA_ID = 0;
58
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059 Context mContext;
Yu Shan Emily Lau20710432009-04-23 21:46:55 -070060 Camera mCamera;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061
62 public MediaRecorderTest() {
63 super("com.android.mediaframeworktest", MediaFrameworkTest.class);
64
65 }
66
67 protected void setUp() throws Exception {
Yu Shan Emily Lau62f755a2011-02-25 20:07:24 -080068 getActivity();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069 mRecorder = new MediaRecorder();
Yu Shan Emily Lau62f755a2011-02-25 20:07:24 -080070 super.setUp();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071 }
72
73 private void recordVideo(int frameRate, int width, int height,
74 int videoFormat, int outFormat, String outFile, boolean videoOnly) {
75 Log.v(TAG,"startPreviewAndPrepareRecording");
76 try {
77 if (!videoOnly) {
78 Log.v(TAG, "setAudioSource");
79 mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
80 }
81 mRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
82 mRecorder.setOutputFormat(outFormat);
83 Log.v(TAG, "output format " + outFormat);
84 mRecorder.setOutputFile(outFile);
85 mRecorder.setVideoFrameRate(frameRate);
86 mRecorder.setVideoSize(width, height);
87 Log.v(TAG, "setEncoder");
88 mRecorder.setVideoEncoder(videoFormat);
89 if (!videoOnly) {
90 mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
91 }
92 mSurfaceHolder = MediaFrameworkTest.mSurfaceView.getHolder();
93 Log.v(TAG, "setPreview");
94 mRecorder.setPreviewDisplay(mSurfaceHolder.getSurface());
95 Log.v(TAG, "prepare");
96 mRecorder.prepare();
97 Log.v(TAG, "start");
98 mRecorder.start();
99 Thread.sleep(MediaNames.RECORDED_TIME);
100 Log.v(TAG, "stop");
101 mRecorder.stop();
102 mRecorder.release();
103 } catch (Exception e) {
104 Log.v("record video failed ", e.toString());
105 mRecorder.release();
106 }
107 }
108
James Dong1b7babd2010-02-16 14:38:23 -0800109 private boolean recordVideoWithPara(VideoEncoderCap videoCap, AudioEncoderCap audioCap, boolean highQuality){
Yu Shan Emily Lau16193672009-09-14 16:23:12 -0700110 boolean recordSuccess = false;
James Dong1b7babd2010-02-16 14:38:23 -0800111 int videoEncoder = videoCap.mCodec;
112 int audioEncoder = audioCap.mCodec;
113 int videoWidth = highQuality? videoCap.mMaxFrameWidth: videoCap.mMinFrameWidth;
114 int videoHeight = highQuality? videoCap.mMaxFrameHeight: videoCap.mMinFrameHeight;
115 int videoFps = highQuality? videoCap.mMaxFrameRate: videoCap.mMinFrameRate;
116 int videoBitrate = highQuality? videoCap.mMaxBitRate: videoCap.mMinBitRate;
117 int audioBitrate = highQuality? audioCap.mMaxBitRate: audioCap.mMinBitRate;
118 int audioChannels = highQuality? audioCap.mMaxChannels: audioCap.mMinChannels ;
119 int audioSamplingRate = highQuality? audioCap.mMaxSampleRate: audioCap.mMinSampleRate;
Yu Shan Emily Lau16193672009-09-14 16:23:12 -0700120
Yu Shan Emily Lau99b45232011-11-23 19:02:01 -0800121 //Overide the fps if the min_camera_fps is set
122 if (MediaFrameworkTestRunner.mMinCameraFps != 0 &&
123 MediaFrameworkTestRunner.mMinCameraFps > videoFps){
124 videoFps = MediaFrameworkTestRunner.mMinCameraFps;
125 }
126
Yu Shan Emily Lau16193672009-09-14 16:23:12 -0700127 if (videoFps < MIN_VIDEO_FPS) {
128 videoFps = MIN_VIDEO_FPS;
129 }
Yu Shan Emily Lau99b45232011-11-23 19:02:01 -0800130
Yu Shan Emily Lau16193672009-09-14 16:23:12 -0700131 mSurfaceHolder = MediaFrameworkTest.mSurfaceView.getHolder();
James Dong1b7babd2010-02-16 14:38:23 -0800132 String filename = ("/sdcard/" + videoEncoder + "_" + audioEncoder + "_" + highQuality + ".3gp");
Yu Shan Emily Lau16193672009-09-14 16:23:12 -0700133 try {
James Dong620a42402010-07-07 14:37:48 -0700134 Log.v(TAG, "video encoder : " + videoEncoder);
135 Log.v(TAG, "audio encoder : " + audioEncoder);
James Dong1b7babd2010-02-16 14:38:23 -0800136 Log.v(TAG, "quality : " + (highQuality?"high": "low"));
137 Log.v(TAG, "encoder : " + MediaProfileReader.getVideoCodecName(videoEncoder));
138 Log.v(TAG, "audio : " + MediaProfileReader.getAudioCodecName(audioEncoder));
Yu Shan Emily Lau16193672009-09-14 16:23:12 -0700139 Log.v(TAG, "videoWidth : " + videoWidth);
140 Log.v(TAG, "videoHeight : " + videoHeight);
141 Log.v(TAG, "videoFPS : " + videoFps);
142 Log.v(TAG, "videobitrate : " + videoBitrate);
143 Log.v(TAG, "audioBitrate : " + audioBitrate);
144 Log.v(TAG, "audioChannel : " + audioChannels);
145 Log.v(TAG, "AudioSampleRate : " + audioSamplingRate);
146
147 MediaRecorder mMediaRecorder = new MediaRecorder();
148 mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
149 mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
150 mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
151 mMediaRecorder.setOutputFile(filename);
152 mMediaRecorder.setVideoFrameRate(videoFps);
153 mMediaRecorder.setVideoSize(videoWidth, videoHeight);
James Dong0fc6bc42010-02-26 19:36:35 -0800154 mMediaRecorder.setVideoEncodingBitRate(videoBitrate);
155 mMediaRecorder.setAudioEncodingBitRate(audioBitrate);
156 mMediaRecorder.setAudioChannels(audioChannels);
157 mMediaRecorder.setAudioSamplingRate(audioSamplingRate);
Yu Shan Emily Lau16193672009-09-14 16:23:12 -0700158 mMediaRecorder.setVideoEncoder(videoEncoder);
159 mMediaRecorder.setAudioEncoder(audioEncoder);
160 mMediaRecorder.setPreviewDisplay(mSurfaceHolder.getSurface());
161 mMediaRecorder.prepare();
162 mMediaRecorder.start();
163 Thread.sleep(MediaNames.RECORDED_TIME);
164 mMediaRecorder.stop();
165 mMediaRecorder.release();
166 recordSuccess = validateVideo(filename, videoWidth, videoHeight);
167 } catch (Exception e) {
168 Log.v(TAG, e.toString());
169 return false;
170 }
171 return recordSuccess;
172 }
173
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800174 private boolean invalidRecordSetting(int frameRate, int width, int height,
175 int videoFormat, int outFormat, String outFile, boolean videoOnly) {
176 try {
177 if (!videoOnly) {
178 Log.v(TAG, "setAudioSource");
179 mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
180 }
181 mRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
182 mRecorder.setOutputFormat(outFormat);
183 Log.v(TAG, "output format " + outFormat);
184 mRecorder.setOutputFile(outFile);
185 mRecorder.setVideoFrameRate(frameRate);
186 mRecorder.setVideoSize(width, height);
187 Log.v(TAG, "setEncoder");
188 mRecorder.setVideoEncoder(videoFormat);
189 if (!videoOnly) {
190 mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
191 }
192 mSurfaceHolder = MediaFrameworkTest.mSurfaceView.getHolder();
193 Log.v(TAG, "setPreview");
194 mRecorder.setPreviewDisplay(mSurfaceHolder.getSurface());
195 Log.v(TAG, "prepare");
196 mRecorder.prepare();
197 Log.v(TAG, "start");
198 mRecorder.start();
199 Thread.sleep(MediaNames.RECORDED_TIME);
200 Log.v(TAG, "stop");
201 mRecorder.stop();
202 mRecorder.release();
203 } catch (Exception e) {
204 Log.v("record video failed ", e.toString());
205 mRecorder.release();
206 Log.v(TAG, "reset and release");
207 return true;
208 }
209 return false;
210 }
211
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800212 private void getOutputVideoProperty(String outputFilePath) {
213 MediaPlayer mediaPlayer = new MediaPlayer();
214 try {
215 mediaPlayer.setDataSource(outputFilePath);
216 Log.v(TAG, "file Path = " + outputFilePath);
217 mediaPlayer.setDisplay(MediaFrameworkTest.mSurfaceView.getHolder());
218 Log.v(TAG, "before player prepare");
219 mediaPlayer.prepare();
220 Log.v(TAG, "before getduration");
221 mOutputDuration = mediaPlayer.getDuration();
222 Log.v(TAG, "get video dimension");
Yu Shan Emily Lau17d7ea42009-09-18 16:23:09 -0700223 Thread.sleep(1000);
Yu Shan Emily Laubc95d662009-09-17 23:40:59 -0700224 mOutputVideoHeight = mediaPlayer.getVideoHeight();
225 mOutputVideoWidth = mediaPlayer.getVideoWidth();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800226 mediaPlayer.release();
227 } catch (Exception e) {
228 Log.v(TAG, e.toString());
229 mediaPlayer.release();
230 }
231 }
232
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800233 private boolean validateVideo(String filePath, int width, int height) {
234 boolean validVideo = false;
235 getOutputVideoProperty(filePath);
236 if (mOutputVideoWidth == width && mOutputVideoHeight == height &&
237 mOutputDuration > MediaNames.VALID_VIDEO_DURATION ) {
238 validVideo = true;
239 }
240 Log.v(TAG, "width = " + mOutputVideoWidth + " height = " + mOutputVideoHeight + " Duration = " + mOutputDuration);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800241 return validVideo;
242 }
Yu Shan Emily Lau62f755a2011-02-25 20:07:24 -0800243
Yu Shan Emily Lau20710432009-04-23 21:46:55 -0700244 @LargeTest
245 /*
246 * This test case set the camera in portrait mode.
247 * Verification: validate the video dimension and the duration.
248 */
249 public void testPortraitH263() throws Exception {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800250 boolean videoRecordedResult = false;
Yu Shan Emily Lau20710432009-04-23 21:46:55 -0700251 try {
Yu Shan Emily Laue35b3e02012-04-26 17:10:29 -0700252 mCamera = Camera.open(CAMERA_ID);
Yu Shan Emily Lau20710432009-04-23 21:46:55 -0700253 Camera.Parameters parameters = mCamera.getParameters();
254 parameters.setPreviewSize(352, 288);
255 parameters.set("orientation", "portrait");
256 mCamera.setParameters(parameters);
257 mCamera.unlock();
258 mRecorder.setCamera(mCamera);
259 Thread.sleep(1000);
James Dongb05a6002011-09-07 19:31:24 -0700260 int codec = MediaRecorder.VideoEncoder.H263;
261 int frameRate = MediaProfileReader.getMaxFrameRateForCodec(codec);
262 recordVideo(frameRate, 352, 288, codec,
Yu Shan Emily Lau20710432009-04-23 21:46:55 -0700263 MediaRecorder.OutputFormat.THREE_GPP,
264 MediaNames.RECORDED_PORTRAIT_H263, true);
Yu Shan Emily Lau20710432009-04-23 21:46:55 -0700265 mCamera.lock();
266 mCamera.release();
Yu Shan Emily Laubc95d662009-09-17 23:40:59 -0700267 videoRecordedResult =
268 validateVideo(MediaNames.RECORDED_PORTRAIT_H263, 352, 288);
Yu Shan Emily Lau20710432009-04-23 21:46:55 -0700269 } catch (Exception e) {
270 Log.v(TAG, e.toString());
271 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800272 assertTrue("PortraitH263", videoRecordedResult);
273 }
274
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800275 @LargeTest
276 public void testInvalidVideoPath() throws Exception {
277 boolean isTestInvalidVideoPathSuccessful = false;
278 isTestInvalidVideoPathSuccessful = invalidRecordSetting(15, 176, 144, MediaRecorder.VideoEncoder.H263,
279 MediaRecorder.OutputFormat.THREE_GPP, MediaNames.INVALD_VIDEO_PATH, false);
280 assertTrue("Invalid outputFile Path", isTestInvalidVideoPathSuccessful);
281 }
282
Yu Shan Emily Lau16193672009-09-14 16:23:12 -0700283 @LargeTest
Yu Shan Emily Laudc1af5b2009-09-21 21:13:36 -0700284 //test cases for the new codec
Yu Shan Emily Lau16193672009-09-14 16:23:12 -0700285 public void testDeviceSpecificCodec() throws Exception {
Yu Shan Emily Laudc1af5b2009-09-21 21:13:36 -0700286 int noOfFailure = 0;
Yu Shan Emily Lau16193672009-09-14 16:23:12 -0700287 boolean recordSuccess = false;
288 String deviceType = MediaProfileReader.getDeviceType();
289 Log.v(TAG, "deviceType = " + deviceType);
James Dong1b7babd2010-02-16 14:38:23 -0800290 List<VideoEncoderCap> videoEncoders = MediaProfileReader.getVideoEncoders();
291 List<AudioEncoderCap> audioEncoders = MediaProfileReader.getAudioEncoders();
292 for (int k = 0; k < 2; k++) {
293 for (VideoEncoderCap videoEncoder: videoEncoders) {
294 for (AudioEncoderCap audioEncoder: audioEncoders) {
295 if (k == 0) {
296 recordSuccess = recordVideoWithPara(videoEncoder, audioEncoder, true);
297 } else {
298 recordSuccess = recordVideoWithPara(videoEncoder, audioEncoder, false);
299 }
300 if (!recordSuccess) {
301 Log.v(TAG, "testDeviceSpecificCodec failed");
302 Log.v(TAG, "Encoder = " + videoEncoder.mCodec + "Audio Encoder = " + audioEncoder.mCodec);
303 noOfFailure++;
Yu Shan Emily Lau16193672009-09-14 16:23:12 -0700304 }
305 }
306 }
James Dong1b7babd2010-02-16 14:38:23 -0800307 }
308 if (noOfFailure != 0) {
309 assertTrue("testDeviceSpecificCodec", false);
Yu Shan Emily Laudc1af5b2009-09-21 21:13:36 -0700310 }
Yu Shan Emily Lau16193672009-09-14 16:23:12 -0700311 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800312}