blob: 2795bde0c6edd462ab17a6dd05e45fee8f114df4 [file] [log] [blame]
Yin-Chia Yeh5855ea62014-07-28 12:22:41 -07001/*
2 * Copyright (C) 2014 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
17package android.hardware.camera2.cts;
18
19import static android.hardware.camera2.cts.CameraTestUtils.*;
Yin-Chia Yeha8c74172014-08-07 12:51:30 -070020
Yin-Chia Yeh5855ea62014-07-28 12:22:41 -070021import android.graphics.ImageFormat;
22import android.graphics.SurfaceTexture;
Yin-Chia Yeha8c74172014-08-07 12:51:30 -070023import android.hardware.camera2.CameraDevice;
Yin-Chia Yeh5855ea62014-07-28 12:22:41 -070024import android.hardware.camera2.cts.CameraTestUtils.ImageVerifierListener;
25import android.hardware.camera2.cts.testcases.Camera2MultiViewTestCase;
26import android.hardware.camera2.cts.testcases.Camera2MultiViewTestCase.CameraPreviewListener;
27import android.media.ImageReader;
28import android.os.SystemClock;
Yin-Chia Yeha8c74172014-08-07 12:51:30 -070029import android.util.Log;
Yin-Chia Yeh5855ea62014-07-28 12:22:41 -070030import android.util.Size;
31import android.view.Surface;
32import android.view.TextureView;
33
Yin-Chia Yeha8c74172014-08-07 12:51:30 -070034import com.android.ex.camera2.blocking.BlockingCameraManager.BlockingOpenException;
35
Yin-Chia Yeh5855ea62014-07-28 12:22:41 -070036import java.util.ArrayList;
Yin-Chia Yeha8c74172014-08-07 12:51:30 -070037import java.util.Arrays;
Yin-Chia Yeh5855ea62014-07-28 12:22:41 -070038import java.util.List;
39
40/**
41 * CameraDevice test by using combination of SurfaceView, TextureView and ImageReader
42 */
43public class MultiViewTest extends Camera2MultiViewTestCase {
Yin-Chia Yeha8c74172014-08-07 12:51:30 -070044 private static final String TAG = "MultiViewTest";
Yin-Chia Yeh5855ea62014-07-28 12:22:41 -070045 private final static long WAIT_FOR_COMMAND_TO_COMPLETE = 2000;
46 private final static long PREVIEW_TIME_MS = 2000;
47
Yin-Chia Yeh5855ea62014-07-28 12:22:41 -070048 public void testTextureViewPreview() throws Exception {
49 for (String cameraId : mCameraIds) {
Eino-Ville Talvalaa0d88252015-07-07 18:03:49 -070050 Exception prior = null;
51
52 try {
53 openCamera(cameraId);
54 if (!getStaticInfo(cameraId).isColorOutputSupported()) {
55 Log.i(TAG, "Camera " + cameraId + " does not support color outputs, skipping");
56 continue;
57 }
58 List<TextureView> views = Arrays.asList(mTextureView[0]);
59 textureViewPreview(cameraId, views, /*ImageReader*/null);
60 } catch (Exception e) {
61 prior = e;
62 } finally {
63 try {
64 closeCamera(cameraId);
65 } catch (Exception e) {
66 if (prior != null) {
67 Log.e(TAG, "Prior exception received: " + prior);
68 }
69 prior = e;
70 }
71 if (prior != null) throw prior; // Rethrow last exception.
72 }
Yin-Chia Yeh5855ea62014-07-28 12:22:41 -070073 }
74 }
75
76 public void testTextureViewPreviewWithImageReader() throws Exception {
77 for (String cameraId : mCameraIds) {
Ruben Brunk7b6aac12014-09-12 19:45:48 -070078 Exception prior = null;
79
80 ImageVerifierListener yuvListener;
Yin-Chia Yehc3770de2015-10-02 16:56:14 -070081 ImageReader yuvReader = null;
Ruben Brunk7b6aac12014-09-12 19:45:48 -070082
Yin-Chia Yeh5855ea62014-07-28 12:22:41 -070083 try {
84 openCamera(cameraId);
Eino-Ville Talvalaa0d88252015-07-07 18:03:49 -070085 if (!getStaticInfo(cameraId).isColorOutputSupported()) {
86 Log.i(TAG, "Camera " + cameraId + " does not support color outputs, skipping");
87 continue;
88 }
Ruben Brunk7b6aac12014-09-12 19:45:48 -070089 Size previewSize = getOrderedPreviewSizes(cameraId).get(0);
90 yuvListener =
91 new ImageVerifierListener(previewSize, ImageFormat.YUV_420_888);
92 yuvReader = makeImageReader(previewSize,
93 ImageFormat.YUV_420_888, MAX_READER_IMAGES, yuvListener, mHandler);
Yin-Chia Yeha8c74172014-08-07 12:51:30 -070094 int maxNumStreamsProc =
95 getStaticInfo(cameraId).getMaxNumOutputStreamsProcessedChecked();
Yin-Chia Yeh5855ea62014-07-28 12:22:41 -070096 if (maxNumStreamsProc < 2) {
97 continue;
98 }
Yin-Chia Yeha8c74172014-08-07 12:51:30 -070099 List<TextureView> views = Arrays.asList(mTextureView[0]);
Ruben Brunk7b6aac12014-09-12 19:45:48 -0700100 textureViewPreview(cameraId, views, yuvReader);
101 } catch (Exception e) {
102 prior = e;
Yin-Chia Yeh5855ea62014-07-28 12:22:41 -0700103 } finally {
Ruben Brunk7b6aac12014-09-12 19:45:48 -0700104 try {
Yin-Chia Yehc3770de2015-10-02 16:56:14 -0700105 if (yuvReader != null) {
106 yuvReader.close();
107 }
Ruben Brunk7b6aac12014-09-12 19:45:48 -0700108 closeCamera(cameraId);
109 } catch (Exception e) {
110 if (prior != null) {
111 Log.e(TAG, "Prior exception received: " + prior);
112 }
113 prior = e;
114 }
115 if (prior != null) throw prior; // Rethrow last exception.
Yin-Chia Yeh5855ea62014-07-28 12:22:41 -0700116 }
117 }
118 }
119
120 public void testDualTextureViewPreview() throws Exception {
121 for (String cameraId : mCameraIds) {
Ruben Brunk7b6aac12014-09-12 19:45:48 -0700122 Exception prior = null;
Yin-Chia Yeh5855ea62014-07-28 12:22:41 -0700123 try {
124 openCamera(cameraId);
Eino-Ville Talvalaa0d88252015-07-07 18:03:49 -0700125 if (!getStaticInfo(cameraId).isColorOutputSupported()) {
126 Log.i(TAG, "Camera " + cameraId + " does not support color outputs, skipping");
127 continue;
128 }
Yin-Chia Yeha8c74172014-08-07 12:51:30 -0700129 int maxNumStreamsProc =
130 getStaticInfo(cameraId).getMaxNumOutputStreamsProcessedChecked();
Yin-Chia Yeh5855ea62014-07-28 12:22:41 -0700131 if (maxNumStreamsProc < 2) {
132 continue;
133 }
Yin-Chia Yeha8c74172014-08-07 12:51:30 -0700134 List<TextureView> views = Arrays.asList(mTextureView[0], mTextureView[1]);
Ruben Brunk7b6aac12014-09-12 19:45:48 -0700135 textureViewPreview(cameraId, views, /*ImageReader*/null);
136 } catch (Exception e) {
137 prior = e;
Yin-Chia Yeh5855ea62014-07-28 12:22:41 -0700138 } finally {
Ruben Brunk7b6aac12014-09-12 19:45:48 -0700139 try {
140 closeCamera(cameraId);
141 } catch (Exception e) {
142 if (prior != null) {
143 Log.e(TAG, "Prior exception received: " + prior);
144 }
145 prior = e;
146 }
147 if (prior != null) throw prior; // Rethrow last exception.
Yin-Chia Yeh5855ea62014-07-28 12:22:41 -0700148 }
149 }
150 }
151
152 public void testDualTextureViewAndImageReaderPreview() throws Exception {
153 for (String cameraId : mCameraIds) {
Ruben Brunk7b6aac12014-09-12 19:45:48 -0700154 Exception prior = null;
155
156 ImageVerifierListener yuvListener;
Yin-Chia Yehc3770de2015-10-02 16:56:14 -0700157 ImageReader yuvReader = null;
Ruben Brunk7b6aac12014-09-12 19:45:48 -0700158
Yin-Chia Yeh5855ea62014-07-28 12:22:41 -0700159 try {
160 openCamera(cameraId);
Eino-Ville Talvalaa0d88252015-07-07 18:03:49 -0700161 if (!getStaticInfo(cameraId).isColorOutputSupported()) {
162 Log.i(TAG, "Camera " + cameraId + " does not support color outputs, skipping");
163 continue;
164 }
Ruben Brunk7b6aac12014-09-12 19:45:48 -0700165 Size previewSize = getOrderedPreviewSizes(cameraId).get(0);
166 yuvListener =
167 new ImageVerifierListener(previewSize, ImageFormat.YUV_420_888);
168 yuvReader = makeImageReader(previewSize,
169 ImageFormat.YUV_420_888, MAX_READER_IMAGES, yuvListener, mHandler);
Yin-Chia Yeha8c74172014-08-07 12:51:30 -0700170 int maxNumStreamsProc =
171 getStaticInfo(cameraId).getMaxNumOutputStreamsProcessedChecked();
Yin-Chia Yeh5855ea62014-07-28 12:22:41 -0700172 if (maxNumStreamsProc < 3) {
173 continue;
174 }
Yin-Chia Yeha8c74172014-08-07 12:51:30 -0700175 List<TextureView> views = Arrays.asList(mTextureView[0], mTextureView[1]);
Ruben Brunk7b6aac12014-09-12 19:45:48 -0700176 textureViewPreview(cameraId, views, yuvReader);
177 } catch (Exception e) {
178 prior = e;
Yin-Chia Yeh5855ea62014-07-28 12:22:41 -0700179 } finally {
Ruben Brunk7b6aac12014-09-12 19:45:48 -0700180 try {
Yin-Chia Yehc3770de2015-10-02 16:56:14 -0700181 if (yuvReader != null) {
182 yuvReader.close();
183 }
Ruben Brunk7b6aac12014-09-12 19:45:48 -0700184 closeCamera(cameraId);
185 } catch (Exception e) {
186 if (prior != null) {
187 Log.e(TAG, "Prior exception received: " + prior);
188 }
189 prior = e;
190 }
191 if (prior != null) throw prior; // Rethrow last exception.
Yin-Chia Yeha8c74172014-08-07 12:51:30 -0700192 }
193 }
194 }
195
196 public void testDualCameraPreview() throws Exception {
197 final int NUM_CAMERAS_TESTED = 2;
198 if (mCameraIds.length < NUM_CAMERAS_TESTED) {
199 return;
200 }
201
202 try {
203 for (int i = 0; i < NUM_CAMERAS_TESTED; i++) {
204 openCamera(mCameraIds[i]);
Eino-Ville Talvalaa0d88252015-07-07 18:03:49 -0700205 if (!getStaticInfo(mCameraIds[i]).isColorOutputSupported()) {
206 Log.i(TAG, "Camera " + mCameraIds[i] +
207 " does not support color outputs, skipping");
208 continue;
209 }
Yin-Chia Yeha8c74172014-08-07 12:51:30 -0700210 List<TextureView> views = Arrays.asList(mTextureView[i]);
Ruben Brunk7b6aac12014-09-12 19:45:48 -0700211
212 startTextureViewPreview(mCameraIds[i], views, /*ImageReader*/null);
Yin-Chia Yeha8c74172014-08-07 12:51:30 -0700213 }
214 // TODO: check the framerate is correct
215 SystemClock.sleep(PREVIEW_TIME_MS);
216 for (int i = 0; i < NUM_CAMERAS_TESTED; i++) {
217 stopPreview(mCameraIds[i]);
218 }
219 } catch (BlockingOpenException e) {
220 // The only error accepted is ERROR_MAX_CAMERAS_IN_USE, which means HAL doesn't support
221 // concurrent camera streaming
222 assertEquals("Camera device open failed",
Eino-Ville Talvalac0dd0222014-09-04 15:07:58 -0700223 CameraDevice.StateCallback.ERROR_MAX_CAMERAS_IN_USE, e.getCode());
Yin-Chia Yeha8c74172014-08-07 12:51:30 -0700224 Log.i(TAG, "Camera HAL does not support dual camera preview. Skip the test");
225 } finally {
226 for (int i = 0; i < NUM_CAMERAS_TESTED; i++) {
227 closeCamera(mCameraIds[i]);
Yin-Chia Yeh5855ea62014-07-28 12:22:41 -0700228 }
229 }
230 }
231
232 /**
Yin-Chia Yeha8c74172014-08-07 12:51:30 -0700233 * Start camera preview using input texture views and/or one image reader
Yin-Chia Yeh5855ea62014-07-28 12:22:41 -0700234 */
Yin-Chia Yeha8c74172014-08-07 12:51:30 -0700235 private void startTextureViewPreview(
Ruben Brunk7b6aac12014-09-12 19:45:48 -0700236 String cameraId, List<TextureView> views, ImageReader imageReader)
Yin-Chia Yeh5855ea62014-07-28 12:22:41 -0700237 throws Exception {
Yin-Chia Yeha8c74172014-08-07 12:51:30 -0700238 int numPreview = views.size();
239 Size previewSize = getOrderedPreviewSizes(cameraId).get(0);
Yin-Chia Yeh5855ea62014-07-28 12:22:41 -0700240 CameraPreviewListener[] previewListener =
241 new CameraPreviewListener[numPreview];
242 SurfaceTexture[] previewTexture = new SurfaceTexture[numPreview];
243 List<Surface> surfaces = new ArrayList<Surface>();
244
245 // Prepare preview surface.
Yin-Chia Yeha8c74172014-08-07 12:51:30 -0700246 int i = 0;
247 for (TextureView view : views) {
Yin-Chia Yeh5855ea62014-07-28 12:22:41 -0700248 previewListener[i] = new CameraPreviewListener();
249 view.setSurfaceTextureListener(previewListener[i]);
250 previewTexture[i] = getAvailableSurfaceTexture(WAIT_FOR_COMMAND_TO_COMPLETE, view);
251 assertNotNull("Unable to get preview surface texture", previewTexture[i]);
252 previewTexture[i].setDefaultBufferSize(previewSize.getWidth(), previewSize.getHeight());
253 // Correct the preview display rotation.
254 updatePreviewDisplayRotation(previewSize, view);
255 surfaces.add(new Surface(previewTexture[i]));
Yin-Chia Yeha8c74172014-08-07 12:51:30 -0700256 i++;
Yin-Chia Yeh5855ea62014-07-28 12:22:41 -0700257 }
Ruben Brunk7b6aac12014-09-12 19:45:48 -0700258 if (imageReader != null) {
259 surfaces.add(imageReader.getSurface());
Yin-Chia Yeh5855ea62014-07-28 12:22:41 -0700260 }
261
Yin-Chia Yeha8c74172014-08-07 12:51:30 -0700262 startPreview(cameraId, surfaces, null);
Yin-Chia Yeh5855ea62014-07-28 12:22:41 -0700263
Yin-Chia Yeha8c74172014-08-07 12:51:30 -0700264 i = 0;
265 for (TextureView view : views) {
Yin-Chia Yeh5855ea62014-07-28 12:22:41 -0700266 boolean previewDone =
267 previewListener[i].waitForPreviewDone(WAIT_FOR_COMMAND_TO_COMPLETE);
268 assertTrue("Unable to start preview " + i, previewDone);
269 view.setSurfaceTextureListener(null);
Yin-Chia Yeha8c74172014-08-07 12:51:30 -0700270 i++;
Yin-Chia Yeh5855ea62014-07-28 12:22:41 -0700271 }
Yin-Chia Yeha8c74172014-08-07 12:51:30 -0700272 }
273
274 /**
275 * Test camera preview using input texture views and/or one image reader
276 */
277 private void textureViewPreview(
Ruben Brunk7b6aac12014-09-12 19:45:48 -0700278 String cameraId, List<TextureView> views, ImageReader testImagerReader)
Yin-Chia Yeha8c74172014-08-07 12:51:30 -0700279 throws Exception {
280 startTextureViewPreview(cameraId, views, testImagerReader);
Yin-Chia Yeh5855ea62014-07-28 12:22:41 -0700281
282 // TODO: check the framerate is correct
283 SystemClock.sleep(PREVIEW_TIME_MS);
284
Yin-Chia Yeha8c74172014-08-07 12:51:30 -0700285 stopPreview(cameraId);
Yin-Chia Yeh5855ea62014-07-28 12:22:41 -0700286 }
287}