blob: dfba587f183025c64934bdd938b71b3e3c35bead [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;
81 ImageReader yuvReader;
82
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 {
105 closeCamera(cameraId);
106 } catch (Exception e) {
107 if (prior != null) {
108 Log.e(TAG, "Prior exception received: " + prior);
109 }
110 prior = e;
111 }
112 if (prior != null) throw prior; // Rethrow last exception.
Yin-Chia Yeh5855ea62014-07-28 12:22:41 -0700113 }
114 }
115 }
116
117 public void testDualTextureViewPreview() throws Exception {
118 for (String cameraId : mCameraIds) {
Ruben Brunk7b6aac12014-09-12 19:45:48 -0700119 Exception prior = null;
Yin-Chia Yeh5855ea62014-07-28 12:22:41 -0700120 try {
121 openCamera(cameraId);
Eino-Ville Talvalaa0d88252015-07-07 18:03:49 -0700122 if (!getStaticInfo(cameraId).isColorOutputSupported()) {
123 Log.i(TAG, "Camera " + cameraId + " does not support color outputs, skipping");
124 continue;
125 }
Yin-Chia Yeha8c74172014-08-07 12:51:30 -0700126 int maxNumStreamsProc =
127 getStaticInfo(cameraId).getMaxNumOutputStreamsProcessedChecked();
Yin-Chia Yeh5855ea62014-07-28 12:22:41 -0700128 if (maxNumStreamsProc < 2) {
129 continue;
130 }
Yin-Chia Yeha8c74172014-08-07 12:51:30 -0700131 List<TextureView> views = Arrays.asList(mTextureView[0], mTextureView[1]);
Ruben Brunk7b6aac12014-09-12 19:45:48 -0700132 textureViewPreview(cameraId, views, /*ImageReader*/null);
133 } catch (Exception e) {
134 prior = e;
Yin-Chia Yeh5855ea62014-07-28 12:22:41 -0700135 } finally {
Ruben Brunk7b6aac12014-09-12 19:45:48 -0700136 try {
137 closeCamera(cameraId);
138 } catch (Exception e) {
139 if (prior != null) {
140 Log.e(TAG, "Prior exception received: " + prior);
141 }
142 prior = e;
143 }
144 if (prior != null) throw prior; // Rethrow last exception.
Yin-Chia Yeh5855ea62014-07-28 12:22:41 -0700145 }
146 }
147 }
148
149 public void testDualTextureViewAndImageReaderPreview() throws Exception {
150 for (String cameraId : mCameraIds) {
Ruben Brunk7b6aac12014-09-12 19:45:48 -0700151 Exception prior = null;
152
153 ImageVerifierListener yuvListener;
154 ImageReader yuvReader;
155
Yin-Chia Yeh5855ea62014-07-28 12:22:41 -0700156 try {
157 openCamera(cameraId);
Eino-Ville Talvalaa0d88252015-07-07 18:03:49 -0700158 if (!getStaticInfo(cameraId).isColorOutputSupported()) {
159 Log.i(TAG, "Camera " + cameraId + " does not support color outputs, skipping");
160 continue;
161 }
Ruben Brunk7b6aac12014-09-12 19:45:48 -0700162 Size previewSize = getOrderedPreviewSizes(cameraId).get(0);
163 yuvListener =
164 new ImageVerifierListener(previewSize, ImageFormat.YUV_420_888);
165 yuvReader = makeImageReader(previewSize,
166 ImageFormat.YUV_420_888, MAX_READER_IMAGES, yuvListener, mHandler);
Yin-Chia Yeha8c74172014-08-07 12:51:30 -0700167 int maxNumStreamsProc =
168 getStaticInfo(cameraId).getMaxNumOutputStreamsProcessedChecked();
Yin-Chia Yeh5855ea62014-07-28 12:22:41 -0700169 if (maxNumStreamsProc < 3) {
170 continue;
171 }
Yin-Chia Yeha8c74172014-08-07 12:51:30 -0700172 List<TextureView> views = Arrays.asList(mTextureView[0], mTextureView[1]);
Ruben Brunk7b6aac12014-09-12 19:45:48 -0700173 textureViewPreview(cameraId, views, yuvReader);
174 } catch (Exception e) {
175 prior = e;
Yin-Chia Yeh5855ea62014-07-28 12:22:41 -0700176 } finally {
Ruben Brunk7b6aac12014-09-12 19:45:48 -0700177 try {
178 closeCamera(cameraId);
179 } catch (Exception e) {
180 if (prior != null) {
181 Log.e(TAG, "Prior exception received: " + prior);
182 }
183 prior = e;
184 }
185 if (prior != null) throw prior; // Rethrow last exception.
Yin-Chia Yeha8c74172014-08-07 12:51:30 -0700186 }
187 }
188 }
189
190 public void testDualCameraPreview() throws Exception {
191 final int NUM_CAMERAS_TESTED = 2;
192 if (mCameraIds.length < NUM_CAMERAS_TESTED) {
193 return;
194 }
195
196 try {
197 for (int i = 0; i < NUM_CAMERAS_TESTED; i++) {
198 openCamera(mCameraIds[i]);
Eino-Ville Talvalaa0d88252015-07-07 18:03:49 -0700199 if (!getStaticInfo(mCameraIds[i]).isColorOutputSupported()) {
200 Log.i(TAG, "Camera " + mCameraIds[i] +
201 " does not support color outputs, skipping");
202 continue;
203 }
Yin-Chia Yeha8c74172014-08-07 12:51:30 -0700204 List<TextureView> views = Arrays.asList(mTextureView[i]);
Ruben Brunk7b6aac12014-09-12 19:45:48 -0700205
206 startTextureViewPreview(mCameraIds[i], views, /*ImageReader*/null);
Yin-Chia Yeha8c74172014-08-07 12:51:30 -0700207 }
208 // TODO: check the framerate is correct
209 SystemClock.sleep(PREVIEW_TIME_MS);
210 for (int i = 0; i < NUM_CAMERAS_TESTED; i++) {
211 stopPreview(mCameraIds[i]);
212 }
213 } catch (BlockingOpenException e) {
214 // The only error accepted is ERROR_MAX_CAMERAS_IN_USE, which means HAL doesn't support
215 // concurrent camera streaming
216 assertEquals("Camera device open failed",
Eino-Ville Talvalac0dd0222014-09-04 15:07:58 -0700217 CameraDevice.StateCallback.ERROR_MAX_CAMERAS_IN_USE, e.getCode());
Yin-Chia Yeha8c74172014-08-07 12:51:30 -0700218 Log.i(TAG, "Camera HAL does not support dual camera preview. Skip the test");
219 } finally {
220 for (int i = 0; i < NUM_CAMERAS_TESTED; i++) {
221 closeCamera(mCameraIds[i]);
Yin-Chia Yeh5855ea62014-07-28 12:22:41 -0700222 }
223 }
224 }
225
226 /**
Yin-Chia Yeha8c74172014-08-07 12:51:30 -0700227 * Start camera preview using input texture views and/or one image reader
Yin-Chia Yeh5855ea62014-07-28 12:22:41 -0700228 */
Yin-Chia Yeha8c74172014-08-07 12:51:30 -0700229 private void startTextureViewPreview(
Ruben Brunk7b6aac12014-09-12 19:45:48 -0700230 String cameraId, List<TextureView> views, ImageReader imageReader)
Yin-Chia Yeh5855ea62014-07-28 12:22:41 -0700231 throws Exception {
Yin-Chia Yeha8c74172014-08-07 12:51:30 -0700232 int numPreview = views.size();
233 Size previewSize = getOrderedPreviewSizes(cameraId).get(0);
Yin-Chia Yeh5855ea62014-07-28 12:22:41 -0700234 CameraPreviewListener[] previewListener =
235 new CameraPreviewListener[numPreview];
236 SurfaceTexture[] previewTexture = new SurfaceTexture[numPreview];
237 List<Surface> surfaces = new ArrayList<Surface>();
238
239 // Prepare preview surface.
Yin-Chia Yeha8c74172014-08-07 12:51:30 -0700240 int i = 0;
241 for (TextureView view : views) {
Yin-Chia Yeh5855ea62014-07-28 12:22:41 -0700242 previewListener[i] = new CameraPreviewListener();
243 view.setSurfaceTextureListener(previewListener[i]);
244 previewTexture[i] = getAvailableSurfaceTexture(WAIT_FOR_COMMAND_TO_COMPLETE, view);
245 assertNotNull("Unable to get preview surface texture", previewTexture[i]);
246 previewTexture[i].setDefaultBufferSize(previewSize.getWidth(), previewSize.getHeight());
247 // Correct the preview display rotation.
248 updatePreviewDisplayRotation(previewSize, view);
249 surfaces.add(new Surface(previewTexture[i]));
Yin-Chia Yeha8c74172014-08-07 12:51:30 -0700250 i++;
Yin-Chia Yeh5855ea62014-07-28 12:22:41 -0700251 }
Ruben Brunk7b6aac12014-09-12 19:45:48 -0700252 if (imageReader != null) {
253 surfaces.add(imageReader.getSurface());
Yin-Chia Yeh5855ea62014-07-28 12:22:41 -0700254 }
255
Yin-Chia Yeha8c74172014-08-07 12:51:30 -0700256 startPreview(cameraId, surfaces, null);
Yin-Chia Yeh5855ea62014-07-28 12:22:41 -0700257
Yin-Chia Yeha8c74172014-08-07 12:51:30 -0700258 i = 0;
259 for (TextureView view : views) {
Yin-Chia Yeh5855ea62014-07-28 12:22:41 -0700260 boolean previewDone =
261 previewListener[i].waitForPreviewDone(WAIT_FOR_COMMAND_TO_COMPLETE);
262 assertTrue("Unable to start preview " + i, previewDone);
263 view.setSurfaceTextureListener(null);
Yin-Chia Yeha8c74172014-08-07 12:51:30 -0700264 i++;
Yin-Chia Yeh5855ea62014-07-28 12:22:41 -0700265 }
Yin-Chia Yeha8c74172014-08-07 12:51:30 -0700266 }
267
268 /**
269 * Test camera preview using input texture views and/or one image reader
270 */
271 private void textureViewPreview(
Ruben Brunk7b6aac12014-09-12 19:45:48 -0700272 String cameraId, List<TextureView> views, ImageReader testImagerReader)
Yin-Chia Yeha8c74172014-08-07 12:51:30 -0700273 throws Exception {
274 startTextureViewPreview(cameraId, views, testImagerReader);
Yin-Chia Yeh5855ea62014-07-28 12:22:41 -0700275
276 // TODO: check the framerate is correct
277 SystemClock.sleep(PREVIEW_TIME_MS);
278
Yin-Chia Yeha8c74172014-08-07 12:51:30 -0700279 stopPreview(cameraId);
Yin-Chia Yeh5855ea62014-07-28 12:22:41 -0700280 }
281}