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; |
| 81 | ImageReader yuvReader; |
| 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 { |
| 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 Yeh | 5855ea6 | 2014-07-28 12:22:41 -0700 | [diff] [blame] | 113 | } |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | public void testDualTextureViewPreview() throws Exception { |
| 118 | for (String cameraId : mCameraIds) { |
Ruben Brunk | 7b6aac1 | 2014-09-12 19:45:48 -0700 | [diff] [blame] | 119 | Exception prior = null; |
Yin-Chia Yeh | 5855ea6 | 2014-07-28 12:22:41 -0700 | [diff] [blame] | 120 | try { |
| 121 | openCamera(cameraId); |
Eino-Ville Talvala | a0d8825 | 2015-07-07 18:03:49 -0700 | [diff] [blame] | 122 | if (!getStaticInfo(cameraId).isColorOutputSupported()) { |
| 123 | Log.i(TAG, "Camera " + cameraId + " does not support color outputs, skipping"); |
| 124 | continue; |
| 125 | } |
Yin-Chia Yeh | a8c7417 | 2014-08-07 12:51:30 -0700 | [diff] [blame] | 126 | int maxNumStreamsProc = |
| 127 | getStaticInfo(cameraId).getMaxNumOutputStreamsProcessedChecked(); |
Yin-Chia Yeh | 5855ea6 | 2014-07-28 12:22:41 -0700 | [diff] [blame] | 128 | if (maxNumStreamsProc < 2) { |
| 129 | continue; |
| 130 | } |
Yin-Chia Yeh | a8c7417 | 2014-08-07 12:51:30 -0700 | [diff] [blame] | 131 | List<TextureView> views = Arrays.asList(mTextureView[0], mTextureView[1]); |
Ruben Brunk | 7b6aac1 | 2014-09-12 19:45:48 -0700 | [diff] [blame] | 132 | textureViewPreview(cameraId, views, /*ImageReader*/null); |
| 133 | } catch (Exception e) { |
| 134 | prior = e; |
Yin-Chia Yeh | 5855ea6 | 2014-07-28 12:22:41 -0700 | [diff] [blame] | 135 | } finally { |
Ruben Brunk | 7b6aac1 | 2014-09-12 19:45:48 -0700 | [diff] [blame] | 136 | 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 Yeh | 5855ea6 | 2014-07-28 12:22:41 -0700 | [diff] [blame] | 145 | } |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | public void testDualTextureViewAndImageReaderPreview() throws Exception { |
| 150 | for (String cameraId : mCameraIds) { |
Ruben Brunk | 7b6aac1 | 2014-09-12 19:45:48 -0700 | [diff] [blame] | 151 | Exception prior = null; |
| 152 | |
| 153 | ImageVerifierListener yuvListener; |
| 154 | ImageReader yuvReader; |
| 155 | |
Yin-Chia Yeh | 5855ea6 | 2014-07-28 12:22:41 -0700 | [diff] [blame] | 156 | try { |
| 157 | openCamera(cameraId); |
Eino-Ville Talvala | a0d8825 | 2015-07-07 18:03:49 -0700 | [diff] [blame] | 158 | if (!getStaticInfo(cameraId).isColorOutputSupported()) { |
| 159 | Log.i(TAG, "Camera " + cameraId + " does not support color outputs, skipping"); |
| 160 | continue; |
| 161 | } |
Ruben Brunk | 7b6aac1 | 2014-09-12 19:45:48 -0700 | [diff] [blame] | 162 | 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 Yeh | a8c7417 | 2014-08-07 12:51:30 -0700 | [diff] [blame] | 167 | int maxNumStreamsProc = |
| 168 | getStaticInfo(cameraId).getMaxNumOutputStreamsProcessedChecked(); |
Yin-Chia Yeh | 5855ea6 | 2014-07-28 12:22:41 -0700 | [diff] [blame] | 169 | if (maxNumStreamsProc < 3) { |
| 170 | continue; |
| 171 | } |
Yin-Chia Yeh | a8c7417 | 2014-08-07 12:51:30 -0700 | [diff] [blame] | 172 | List<TextureView> views = Arrays.asList(mTextureView[0], mTextureView[1]); |
Ruben Brunk | 7b6aac1 | 2014-09-12 19:45:48 -0700 | [diff] [blame] | 173 | textureViewPreview(cameraId, views, yuvReader); |
| 174 | } catch (Exception e) { |
| 175 | prior = e; |
Yin-Chia Yeh | 5855ea6 | 2014-07-28 12:22:41 -0700 | [diff] [blame] | 176 | } finally { |
Ruben Brunk | 7b6aac1 | 2014-09-12 19:45:48 -0700 | [diff] [blame] | 177 | 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 Yeh | a8c7417 | 2014-08-07 12:51:30 -0700 | [diff] [blame] | 186 | } |
| 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 Talvala | a0d8825 | 2015-07-07 18:03:49 -0700 | [diff] [blame] | 199 | if (!getStaticInfo(mCameraIds[i]).isColorOutputSupported()) { |
| 200 | Log.i(TAG, "Camera " + mCameraIds[i] + |
| 201 | " does not support color outputs, skipping"); |
| 202 | continue; |
| 203 | } |
Yin-Chia Yeh | a8c7417 | 2014-08-07 12:51:30 -0700 | [diff] [blame] | 204 | List<TextureView> views = Arrays.asList(mTextureView[i]); |
Ruben Brunk | 7b6aac1 | 2014-09-12 19:45:48 -0700 | [diff] [blame] | 205 | |
| 206 | startTextureViewPreview(mCameraIds[i], views, /*ImageReader*/null); |
Yin-Chia Yeh | a8c7417 | 2014-08-07 12:51:30 -0700 | [diff] [blame] | 207 | } |
| 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 Talvala | c0dd022 | 2014-09-04 15:07:58 -0700 | [diff] [blame] | 217 | CameraDevice.StateCallback.ERROR_MAX_CAMERAS_IN_USE, e.getCode()); |
Yin-Chia Yeh | a8c7417 | 2014-08-07 12:51:30 -0700 | [diff] [blame] | 218 | 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 Yeh | 5855ea6 | 2014-07-28 12:22:41 -0700 | [diff] [blame] | 222 | } |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | /** |
Yin-Chia Yeh | a8c7417 | 2014-08-07 12:51:30 -0700 | [diff] [blame] | 227 | * 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] | 228 | */ |
Yin-Chia Yeh | a8c7417 | 2014-08-07 12:51:30 -0700 | [diff] [blame] | 229 | private void startTextureViewPreview( |
Ruben Brunk | 7b6aac1 | 2014-09-12 19:45:48 -0700 | [diff] [blame] | 230 | String cameraId, List<TextureView> views, ImageReader imageReader) |
Yin-Chia Yeh | 5855ea6 | 2014-07-28 12:22:41 -0700 | [diff] [blame] | 231 | throws Exception { |
Yin-Chia Yeh | a8c7417 | 2014-08-07 12:51:30 -0700 | [diff] [blame] | 232 | int numPreview = views.size(); |
| 233 | Size previewSize = getOrderedPreviewSizes(cameraId).get(0); |
Yin-Chia Yeh | 5855ea6 | 2014-07-28 12:22:41 -0700 | [diff] [blame] | 234 | 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 Yeh | a8c7417 | 2014-08-07 12:51:30 -0700 | [diff] [blame] | 240 | int i = 0; |
| 241 | for (TextureView view : views) { |
Yin-Chia Yeh | 5855ea6 | 2014-07-28 12:22:41 -0700 | [diff] [blame] | 242 | 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 Yeh | a8c7417 | 2014-08-07 12:51:30 -0700 | [diff] [blame] | 250 | i++; |
Yin-Chia Yeh | 5855ea6 | 2014-07-28 12:22:41 -0700 | [diff] [blame] | 251 | } |
Ruben Brunk | 7b6aac1 | 2014-09-12 19:45:48 -0700 | [diff] [blame] | 252 | if (imageReader != null) { |
| 253 | surfaces.add(imageReader.getSurface()); |
Yin-Chia Yeh | 5855ea6 | 2014-07-28 12:22:41 -0700 | [diff] [blame] | 254 | } |
| 255 | |
Yin-Chia Yeh | a8c7417 | 2014-08-07 12:51:30 -0700 | [diff] [blame] | 256 | startPreview(cameraId, surfaces, null); |
Yin-Chia Yeh | 5855ea6 | 2014-07-28 12:22:41 -0700 | [diff] [blame] | 257 | |
Yin-Chia Yeh | a8c7417 | 2014-08-07 12:51:30 -0700 | [diff] [blame] | 258 | i = 0; |
| 259 | for (TextureView view : views) { |
Yin-Chia Yeh | 5855ea6 | 2014-07-28 12:22:41 -0700 | [diff] [blame] | 260 | 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 Yeh | a8c7417 | 2014-08-07 12:51:30 -0700 | [diff] [blame] | 264 | i++; |
Yin-Chia Yeh | 5855ea6 | 2014-07-28 12:22:41 -0700 | [diff] [blame] | 265 | } |
Yin-Chia Yeh | a8c7417 | 2014-08-07 12:51:30 -0700 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | /** |
| 269 | * Test camera preview using input texture views and/or one image reader |
| 270 | */ |
| 271 | private void textureViewPreview( |
Ruben Brunk | 7b6aac1 | 2014-09-12 19:45:48 -0700 | [diff] [blame] | 272 | String cameraId, List<TextureView> views, ImageReader testImagerReader) |
Yin-Chia Yeh | a8c7417 | 2014-08-07 12:51:30 -0700 | [diff] [blame] | 273 | throws Exception { |
| 274 | startTextureViewPreview(cameraId, views, testImagerReader); |
Yin-Chia Yeh | 5855ea6 | 2014-07-28 12:22:41 -0700 | [diff] [blame] | 275 | |
| 276 | // TODO: check the framerate is correct |
| 277 | SystemClock.sleep(PREVIEW_TIME_MS); |
| 278 | |
Yin-Chia Yeh | a8c7417 | 2014-08-07 12:51:30 -0700 | [diff] [blame] | 279 | stopPreview(cameraId); |
Yin-Chia Yeh | 5855ea6 | 2014-07-28 12:22:41 -0700 | [diff] [blame] | 280 | } |
| 281 | } |