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