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