1 /* 2 * Copyright 2017 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.*; 20 21 import com.android.ex.camera2.blocking.BlockingSessionCallback; 22 23 import android.graphics.Bitmap; 24 import android.graphics.BitmapFactory; 25 import android.graphics.ImageFormat; 26 import android.graphics.SurfaceTexture; 27 import android.hardware.Camera; 28 import android.hardware.cts.helpers.CameraUtils; 29 import android.hardware.camera2.CameraCharacteristics; 30 import android.hardware.camera2.CameraDevice; 31 import android.hardware.camera2.CaptureRequest; 32 import android.hardware.camera2.CaptureResult; 33 import android.media.Image; 34 import android.platform.test.annotations.Presubmit; 35 import android.util.Log; 36 import android.util.Size; 37 38 import java.nio.ByteBuffer; 39 40 import android.hardware.camera2.cts.Camera2SurfaceViewCtsActivity; 41 import android.hardware.camera2.cts.CameraTestUtils.SimpleCaptureCallback; 42 import android.hardware.camera2.cts.CameraTestUtils.SimpleImageReaderListener; 43 import android.hardware.camera2.cts.testcases.Camera2SurfaceViewTestCase; 44 45 import org.junit.runners.Parameterized; 46 import org.junit.runner.RunWith; 47 import org.junit.Test; 48 49 /** 50 * Quick-running test for very basic camera operation for all cameras 51 * and both camera APIs. 52 * 53 * May not take more than a few seconds to run, to be suitable for quick 54 * testing. 55 */ 56 57 @RunWith(Parameterized.class) 58 public class FastBasicsTest extends Camera2SurfaceViewTestCase { 59 private static final String TAG = "FastBasicsTest"; 60 private static final boolean VERBOSE = Log.isLoggable(TAG, Log.VERBOSE); 61 private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG); 62 63 private static final int WAIT_FOR_FRAMES_TIMEOUT_MS = 3000; 64 private static final int WAIT_FOR_PICTURE_TIMEOUT_MS = 8000; 65 private static final int FRAMES_TO_WAIT_FOR_CAPTURE = 100; 66 67 @Presubmit 68 @Test testCamera2()69 public void testCamera2() throws Exception { 70 String[] cameraIdsUnderTest = getCameraIdsUnderTest(); 71 for (int i = 0; i < cameraIdsUnderTest.length; i++) { 72 try { 73 Log.i(TAG, "Testing camera2 API for camera device " + cameraIdsUnderTest[i]); 74 75 if (!mAllStaticInfo.get(cameraIdsUnderTest[i]).isColorOutputSupported()) { 76 Log.i(TAG, "Camera " + cameraIdsUnderTest[i] + 77 " does not support color outputs, skipping"); 78 continue; 79 } 80 81 openDevice(cameraIdsUnderTest[i]); 82 camera2TestByCamera(); 83 } finally { 84 closeDevice(); 85 } 86 } 87 } 88 camera2TestByCamera()89 public void camera2TestByCamera() throws Exception { 90 CaptureRequest.Builder previewRequest = 91 mCamera.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW); 92 CaptureRequest.Builder stillCaptureRequest = 93 mCamera.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE); 94 Size previewSize = mOrderedPreviewSizes.get(0); 95 Size stillSize = mOrderedStillSizes.get(0); 96 SimpleCaptureCallback resultListener = new SimpleCaptureCallback(); 97 SimpleImageReaderListener imageListener = new SimpleImageReaderListener(); 98 99 prepareStillCaptureAndStartPreview(previewRequest, stillCaptureRequest, 100 previewSize, stillSize, resultListener, imageListener, false /*isHeic*/); 101 102 CaptureResult result = resultListener.getCaptureResult(WAIT_FOR_FRAMES_TIMEOUT_MS); 103 104 Long timestamp = result.get(CaptureResult.SENSOR_TIMESTAMP); 105 assertNotNull("Can't read a capture result timestamp", timestamp); 106 107 CaptureResult result2 = resultListener.getCaptureResult(WAIT_FOR_FRAMES_TIMEOUT_MS); 108 109 Long timestamp2 = result2.get(CaptureResult.SENSOR_TIMESTAMP); 110 assertNotNull("Can't read a capture result 2 timestamp", timestamp2); 111 112 assertTrue("Bad timestamps", timestamp2 > timestamp); 113 114 // If EnableZsl is supported, disable ZSL in order to compare preview and still timestamps. 115 if (mStaticInfo.isEnableZslSupported()) { 116 stillCaptureRequest.set(CaptureRequest.CONTROL_ENABLE_ZSL, false); 117 } 118 119 CaptureRequest capture = stillCaptureRequest.build(); 120 mSession.capture(capture, resultListener, mHandler); 121 122 CaptureResult stillResult = 123 resultListener.getTotalCaptureResultForRequest(capture, FRAMES_TO_WAIT_FOR_CAPTURE); 124 125 Long timestamp3 = stillResult.get(CaptureResult.SENSOR_TIMESTAMP); 126 assertNotNull("Can't read a still capture result timestamp", timestamp3); 127 128 assertTrue("Bad timestamps", timestamp3 > timestamp2); 129 130 Image img = imageListener.getImage(WAIT_FOR_PICTURE_TIMEOUT_MS); 131 132 ByteBuffer jpegBuffer = img.getPlanes()[0].getBuffer(); 133 byte[] jpegData = new byte[jpegBuffer.remaining()]; 134 jpegBuffer.get(jpegData); 135 136 Bitmap b = BitmapFactory.decodeByteArray(jpegData, 0, jpegData.length); 137 138 assertNotNull("Unable to decode still capture JPEG", b); 139 140 closeImageReader(); 141 } 142 143 private class Camera1Listener 144 implements SurfaceTexture.OnFrameAvailableListener, Camera.PictureCallback { 145 146 private Object mFrameSignal = new Object(); 147 private boolean mGotFrame = false; 148 waitForFrame()149 public boolean waitForFrame() { 150 synchronized(mFrameSignal) { 151 boolean waited = false; 152 while (!waited) { 153 try { 154 mFrameSignal.wait(WAIT_FOR_FRAMES_TIMEOUT_MS); 155 waited = true; 156 } catch (InterruptedException e) { 157 } 158 } 159 return mGotFrame; 160 } 161 } 162 onFrameAvailable(SurfaceTexture s)163 public void onFrameAvailable(SurfaceTexture s) { 164 synchronized(mFrameSignal) { 165 mGotFrame = true; 166 mFrameSignal.notifyAll(); 167 } 168 } 169 170 private Object mPictureSignal = new Object(); 171 private boolean mGotPicture = false; 172 private byte[] mPictureData = null; 173 waitForPicture()174 public byte[] waitForPicture() { 175 Log.i(TAG, "waitForPicture called"); 176 synchronized(mPictureSignal) { 177 boolean waited = false; 178 while (!waited) { 179 try { 180 mPictureSignal.wait(WAIT_FOR_PICTURE_TIMEOUT_MS); 181 waited = true; 182 Log.i(TAG, "waitForPicture returned with mGotPicture = " + mGotPicture); 183 } catch (InterruptedException e) { 184 Log.e(TAG, "waitForPicture gets interrupted exception!"); 185 } 186 } 187 return mPictureData; 188 } 189 } 190 onPictureTaken(byte[] data, Camera camera)191 public void onPictureTaken(byte[] data, Camera camera) { 192 Log.i(TAG, "onPictureTaken called"); 193 synchronized(mPictureSignal) { 194 mPictureData = data; 195 mGotPicture = true; 196 mPictureSignal.notifyAll(); 197 } 198 } 199 } 200 201 @Presubmit 202 @Test testCamera1()203 public void testCamera1() throws Exception { 204 int[] cameraIds = CameraUtils.deriveCameraIdsUnderTest(); 205 for (int i : cameraIds) { 206 Camera camera = null; 207 try { 208 Log.i(TAG, "Testing android.hardware.Camera API for camera device " + i); 209 210 camera = Camera.open(i); 211 212 Camera1Listener listener = new Camera1Listener(); 213 214 SurfaceTexture st = new SurfaceTexture(/*random int*/ 5); 215 st.setOnFrameAvailableListener(listener); 216 217 camera.setPreviewTexture(st); 218 camera.startPreview(); 219 220 assertTrue("No preview received from camera", listener.waitForFrame()); 221 222 camera.takePicture(null, null, listener); 223 224 byte[] picture = listener.waitForPicture(); 225 226 assertNotNull("No still picture received from camera", picture); 227 228 Bitmap b = BitmapFactory.decodeByteArray(picture, 0, picture.length); 229 230 assertNotNull("Still picture could not be decoded into Bitmap", b); 231 232 } finally { 233 if (camera != null) { 234 camera.release(); 235 } 236 } 237 } 238 } 239 } 240