Yin-Chia Yeh | 5855ea6 | 2014-07-28 12:22:41 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package android.hardware.camera2.cts; |
| 18 | |
| 19 | import static android.hardware.camera2.cts.CameraTestUtils.*; |
Yin-Chia Yeh | a8c7417 | 2014-08-07 12:51:30 -0700 | [diff] [blame] | 20 | |
Yin-Chia Yeh | 5855ea6 | 2014-07-28 12:22:41 -0700 | [diff] [blame] | 21 | import android.graphics.ImageFormat; |
| 22 | import android.graphics.SurfaceTexture; |
Yin-Chia Yeh | a8c7417 | 2014-08-07 12:51:30 -0700 | [diff] [blame] | 23 | import android.hardware.camera2.CameraDevice; |
Yin-Chia Yeh | 5855ea6 | 2014-07-28 12:22:41 -0700 | [diff] [blame] | 24 | import android.hardware.camera2.cts.CameraTestUtils.ImageVerifierListener; |
| 25 | import android.hardware.camera2.cts.testcases.Camera2MultiViewTestCase; |
| 26 | import android.hardware.camera2.cts.testcases.Camera2MultiViewTestCase.CameraPreviewListener; |
| 27 | import android.media.ImageReader; |
| 28 | import android.os.SystemClock; |
Yin-Chia Yeh | a8c7417 | 2014-08-07 12:51:30 -0700 | [diff] [blame] | 29 | import android.util.Log; |
Yin-Chia Yeh | 5855ea6 | 2014-07-28 12:22:41 -0700 | [diff] [blame] | 30 | import android.util.Size; |
| 31 | import android.view.Surface; |
| 32 | import android.view.TextureView; |
| 33 | |
Yin-Chia Yeh | a8c7417 | 2014-08-07 12:51:30 -0700 | [diff] [blame] | 34 | import com.android.ex.camera2.blocking.BlockingCameraManager.BlockingOpenException; |
| 35 | |
Yin-Chia Yeh | 5855ea6 | 2014-07-28 12:22:41 -0700 | [diff] [blame] | 36 | import java.util.ArrayList; |
Yin-Chia Yeh | a8c7417 | 2014-08-07 12:51:30 -0700 | [diff] [blame] | 37 | import java.util.Arrays; |
Yin-Chia Yeh | 5855ea6 | 2014-07-28 12:22:41 -0700 | [diff] [blame] | 38 | import java.util.List; |
| 39 | |
| 40 | /** |
| 41 | * CameraDevice test by using combination of SurfaceView, TextureView and ImageReader |
| 42 | */ |
| 43 | public class MultiViewTest extends Camera2MultiViewTestCase { |
Yin-Chia Yeh | a8c7417 | 2014-08-07 12:51:30 -0700 | [diff] [blame] | 44 | private static final String TAG = "MultiViewTest"; |
Yin-Chia Yeh | 5855ea6 | 2014-07-28 12:22:41 -0700 | [diff] [blame] | 45 | private final static long WAIT_FOR_COMMAND_TO_COMPLETE = 2000; |
| 46 | private final static long PREVIEW_TIME_MS = 2000; |
| 47 | |
Yin-Chia Yeh | 5855ea6 | 2014-07-28 12:22:41 -0700 | [diff] [blame] | 48 | public void testTextureViewPreview() throws Exception { |
| 49 | for (String cameraId : mCameraIds) { |
Eino-Ville Talvala | a0d8825 | 2015-07-07 18:03:49 -0700 | [diff] [blame] | 50 | 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 Yeh | 5855ea6 | 2014-07-28 12:22:41 -0700 | [diff] [blame] | 73 | } |
| 74 | } |
| 75 | |
| 76 | public void testTextureViewPreviewWithImageReader() throws Exception { |
| 77 | for (String cameraId : mCameraIds) { |
Ruben Brunk | 7b6aac1 | 2014-09-12 19:45:48 -0700 | [diff] [blame] | 78 | Exception prior = null; |
| 79 | |
| 80 | ImageVerifierListener yuvListener; |
Yin-Chia Yeh | c3770de | 2015-10-02 16:56:14 -0700 | [diff] [blame] | 81 | ImageReader yuvReader = null; |
Ruben Brunk | 7b6aac1 | 2014-09-12 19:45:48 -0700 | [diff] [blame] | 82 | |
Yin-Chia Yeh | 5855ea6 | 2014-07-28 12:22:41 -0700 | [diff] [blame] | 83 | try { |
| 84 | openCamera(cameraId); |
Eino-Ville Talvala | a0d8825 | 2015-07-07 18:03:49 -0700 | [diff] [blame] | 85 | if (!getStaticInfo(cameraId).isColorOutputSupported()) { |
| 86 | Log.i(TAG, "Camera " + cameraId + " does not support color outputs, skipping"); |
| 87 | continue; |
| 88 | } |
Ruben Brunk | 7b6aac1 | 2014-09-12 19:45:48 -0700 | [diff] [blame] | 89 | 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 Yeh | a8c7417 | 2014-08-07 12:51:30 -0700 | [diff] [blame] | 94 | int maxNumStreamsProc = |
| 95 | getStaticInfo(cameraId).getMaxNumOutputStreamsProcessedChecked(); |
Yin-Chia Yeh | 5855ea6 | 2014-07-28 12:22:41 -0700 | [diff] [blame] | 96 | if (maxNumStreamsProc < 2) { |
| 97 | continue; |
| 98 | } |
Yin-Chia Yeh | a8c7417 | 2014-08-07 12:51:30 -0700 | [diff] [blame] | 99 | List<TextureView> views = Arrays.asList(mTextureView[0]); |
Ruben Brunk | 7b6aac1 | 2014-09-12 19:45:48 -0700 | [diff] [blame] | 100 | textureViewPreview(cameraId, views, yuvReader); |
| 101 | } catch (Exception e) { |
| 102 | prior = e; |
Yin-Chia Yeh | 5855ea6 | 2014-07-28 12:22:41 -0700 | [diff] [blame] | 103 | } finally { |
Ruben Brunk | 7b6aac1 | 2014-09-12 19:45:48 -0700 | [diff] [blame] | 104 | try { |
Yin-Chia Yeh | c3770de | 2015-10-02 16:56:14 -0700 | [diff] [blame] | 105 | if (yuvReader != null) { |
| 106 | yuvReader.close(); |
| 107 | } |
Ruben Brunk | 7b6aac1 | 2014-09-12 19:45:48 -0700 | [diff] [blame] | 108 | 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 Yeh | 5855ea6 | 2014-07-28 12:22:41 -0700 | [diff] [blame] | 116 | } |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | public void testDualTextureViewPreview() throws Exception { |
| 121 | for (String cameraId : mCameraIds) { |
Ruben Brunk | 7b6aac1 | 2014-09-12 19:45:48 -0700 | [diff] [blame] | 122 | Exception prior = null; |
Yin-Chia Yeh | 5855ea6 | 2014-07-28 12:22:41 -0700 | [diff] [blame] | 123 | try { |
| 124 | openCamera(cameraId); |
Eino-Ville Talvala | a0d8825 | 2015-07-07 18:03:49 -0700 | [diff] [blame] | 125 | if (!getStaticInfo(cameraId).isColorOutputSupported()) { |
| 126 | Log.i(TAG, "Camera " + cameraId + " does not support color outputs, skipping"); |
| 127 | continue; |
| 128 | } |
Yin-Chia Yeh | a8c7417 | 2014-08-07 12:51:30 -0700 | [diff] [blame] | 129 | int maxNumStreamsProc = |
| 130 | getStaticInfo(cameraId).getMaxNumOutputStreamsProcessedChecked(); |
Yin-Chia Yeh | 5855ea6 | 2014-07-28 12:22:41 -0700 | [diff] [blame] | 131 | if (maxNumStreamsProc < 2) { |
| 132 | continue; |
| 133 | } |
Yin-Chia Yeh | a8c7417 | 2014-08-07 12:51:30 -0700 | [diff] [blame] | 134 | List<TextureView> views = Arrays.asList(mTextureView[0], mTextureView[1]); |
Ruben Brunk | 7b6aac1 | 2014-09-12 19:45:48 -0700 | [diff] [blame] | 135 | textureViewPreview(cameraId, views, /*ImageReader*/null); |
| 136 | } catch (Exception e) { |
| 137 | prior = e; |
Yin-Chia Yeh | 5855ea6 | 2014-07-28 12:22:41 -0700 | [diff] [blame] | 138 | } finally { |
Ruben Brunk | 7b6aac1 | 2014-09-12 19:45:48 -0700 | [diff] [blame] | 139 | 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 Yeh | 5855ea6 | 2014-07-28 12:22:41 -0700 | [diff] [blame] | 148 | } |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | public void testDualTextureViewAndImageReaderPreview() throws Exception { |
| 153 | for (String cameraId : mCameraIds) { |
Ruben Brunk | 7b6aac1 | 2014-09-12 19:45:48 -0700 | [diff] [blame] | 154 | Exception prior = null; |
| 155 | |
| 156 | ImageVerifierListener yuvListener; |
Yin-Chia Yeh | c3770de | 2015-10-02 16:56:14 -0700 | [diff] [blame] | 157 | ImageReader yuvReader = null; |
Ruben Brunk | 7b6aac1 | 2014-09-12 19:45:48 -0700 | [diff] [blame] | 158 | |
Yin-Chia Yeh | 5855ea6 | 2014-07-28 12:22:41 -0700 | [diff] [blame] | 159 | try { |
| 160 | openCamera(cameraId); |
Eino-Ville Talvala | a0d8825 | 2015-07-07 18:03:49 -0700 | [diff] [blame] | 161 | if (!getStaticInfo(cameraId).isColorOutputSupported()) { |
| 162 | Log.i(TAG, "Camera " + cameraId + " does not support color outputs, skipping"); |
| 163 | continue; |
| 164 | } |
Ruben Brunk | 7b6aac1 | 2014-09-12 19:45:48 -0700 | [diff] [blame] | 165 | 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 Yeh | a8c7417 | 2014-08-07 12:51:30 -0700 | [diff] [blame] | 170 | int maxNumStreamsProc = |
| 171 | getStaticInfo(cameraId).getMaxNumOutputStreamsProcessedChecked(); |
Yin-Chia Yeh | 5855ea6 | 2014-07-28 12:22:41 -0700 | [diff] [blame] | 172 | if (maxNumStreamsProc < 3) { |
| 173 | continue; |
| 174 | } |
Yin-Chia Yeh | a8c7417 | 2014-08-07 12:51:30 -0700 | [diff] [blame] | 175 | List<TextureView> views = Arrays.asList(mTextureView[0], mTextureView[1]); |
Ruben Brunk | 7b6aac1 | 2014-09-12 19:45:48 -0700 | [diff] [blame] | 176 | textureViewPreview(cameraId, views, yuvReader); |
| 177 | } catch (Exception e) { |
| 178 | prior = e; |
Yin-Chia Yeh | 5855ea6 | 2014-07-28 12:22:41 -0700 | [diff] [blame] | 179 | } finally { |
Ruben Brunk | 7b6aac1 | 2014-09-12 19:45:48 -0700 | [diff] [blame] | 180 | try { |
Yin-Chia Yeh | c3770de | 2015-10-02 16:56:14 -0700 | [diff] [blame] | 181 | if (yuvReader != null) { |
| 182 | yuvReader.close(); |
| 183 | } |
Ruben Brunk | 7b6aac1 | 2014-09-12 19:45:48 -0700 | [diff] [blame] | 184 | 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 Yeh | a8c7417 | 2014-08-07 12:51:30 -0700 | [diff] [blame] | 192 | } |
| 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 Talvala | a0d8825 | 2015-07-07 18:03:49 -0700 | [diff] [blame] | 205 | if (!getStaticInfo(mCameraIds[i]).isColorOutputSupported()) { |
| 206 | Log.i(TAG, "Camera " + mCameraIds[i] + |
| 207 | " does not support color outputs, skipping"); |
| 208 | continue; |
| 209 | } |
Yin-Chia Yeh | a8c7417 | 2014-08-07 12:51:30 -0700 | [diff] [blame] | 210 | List<TextureView> views = Arrays.asList(mTextureView[i]); |
Ruben Brunk | 7b6aac1 | 2014-09-12 19:45:48 -0700 | [diff] [blame] | 211 | |
| 212 | startTextureViewPreview(mCameraIds[i], views, /*ImageReader*/null); |
Yin-Chia Yeh | a8c7417 | 2014-08-07 12:51:30 -0700 | [diff] [blame] | 213 | } |
| 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 Talvala | c0dd022 | 2014-09-04 15:07:58 -0700 | [diff] [blame] | 223 | CameraDevice.StateCallback.ERROR_MAX_CAMERAS_IN_USE, e.getCode()); |
Yin-Chia Yeh | a8c7417 | 2014-08-07 12:51:30 -0700 | [diff] [blame] | 224 | 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 Yeh | 5855ea6 | 2014-07-28 12:22:41 -0700 | [diff] [blame] | 228 | } |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | /** |
Yin-Chia Yeh | a8c7417 | 2014-08-07 12:51:30 -0700 | [diff] [blame] | 233 | * Start camera preview using input texture views and/or one image reader |
Yin-Chia Yeh | 5855ea6 | 2014-07-28 12:22:41 -0700 | [diff] [blame] | 234 | */ |
Yin-Chia Yeh | a8c7417 | 2014-08-07 12:51:30 -0700 | [diff] [blame] | 235 | private void startTextureViewPreview( |
Ruben Brunk | 7b6aac1 | 2014-09-12 19:45:48 -0700 | [diff] [blame] | 236 | String cameraId, List<TextureView> views, ImageReader imageReader) |
Yin-Chia Yeh | 5855ea6 | 2014-07-28 12:22:41 -0700 | [diff] [blame] | 237 | throws Exception { |
Yin-Chia Yeh | a8c7417 | 2014-08-07 12:51:30 -0700 | [diff] [blame] | 238 | int numPreview = views.size(); |
| 239 | Size previewSize = getOrderedPreviewSizes(cameraId).get(0); |
Yin-Chia Yeh | 5855ea6 | 2014-07-28 12:22:41 -0700 | [diff] [blame] | 240 | 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 Yeh | a8c7417 | 2014-08-07 12:51:30 -0700 | [diff] [blame] | 246 | int i = 0; |
| 247 | for (TextureView view : views) { |
Yin-Chia Yeh | 5855ea6 | 2014-07-28 12:22:41 -0700 | [diff] [blame] | 248 | 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 Yeh | a8c7417 | 2014-08-07 12:51:30 -0700 | [diff] [blame] | 256 | i++; |
Yin-Chia Yeh | 5855ea6 | 2014-07-28 12:22:41 -0700 | [diff] [blame] | 257 | } |
Ruben Brunk | 7b6aac1 | 2014-09-12 19:45:48 -0700 | [diff] [blame] | 258 | if (imageReader != null) { |
| 259 | surfaces.add(imageReader.getSurface()); |
Yin-Chia Yeh | 5855ea6 | 2014-07-28 12:22:41 -0700 | [diff] [blame] | 260 | } |
| 261 | |
Yin-Chia Yeh | a8c7417 | 2014-08-07 12:51:30 -0700 | [diff] [blame] | 262 | startPreview(cameraId, surfaces, null); |
Yin-Chia Yeh | 5855ea6 | 2014-07-28 12:22:41 -0700 | [diff] [blame] | 263 | |
Yin-Chia Yeh | a8c7417 | 2014-08-07 12:51:30 -0700 | [diff] [blame] | 264 | i = 0; |
| 265 | for (TextureView view : views) { |
Yin-Chia Yeh | 5855ea6 | 2014-07-28 12:22:41 -0700 | [diff] [blame] | 266 | 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 Yeh | a8c7417 | 2014-08-07 12:51:30 -0700 | [diff] [blame] | 270 | i++; |
Yin-Chia Yeh | 5855ea6 | 2014-07-28 12:22:41 -0700 | [diff] [blame] | 271 | } |
Yin-Chia Yeh | a8c7417 | 2014-08-07 12:51:30 -0700 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | /** |
| 275 | * Test camera preview using input texture views and/or one image reader |
| 276 | */ |
| 277 | private void textureViewPreview( |
Ruben Brunk | 7b6aac1 | 2014-09-12 19:45:48 -0700 | [diff] [blame] | 278 | String cameraId, List<TextureView> views, ImageReader testImagerReader) |
Yin-Chia Yeh | a8c7417 | 2014-08-07 12:51:30 -0700 | [diff] [blame] | 279 | throws Exception { |
| 280 | startTextureViewPreview(cameraId, views, testImagerReader); |
Yin-Chia Yeh | 5855ea6 | 2014-07-28 12:22:41 -0700 | [diff] [blame] | 281 | |
| 282 | // TODO: check the framerate is correct |
| 283 | SystemClock.sleep(PREVIEW_TIME_MS); |
| 284 | |
Yin-Chia Yeh | a8c7417 | 2014-08-07 12:51:30 -0700 | [diff] [blame] | 285 | stopPreview(cameraId); |
Yin-Chia Yeh | 5855ea6 | 2014-07-28 12:22:41 -0700 | [diff] [blame] | 286 | } |
| 287 | } |