1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
|
/*
* Copyright (C) 2013 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.mediaframeworktest.integration;
import android.graphics.ImageFormat;
import android.graphics.SurfaceTexture;
import android.hardware.camera2.CameraMetadata;
import android.hardware.camera2.CameraCharacteristics;
import android.hardware.camera2.CaptureRequest;
import android.hardware.camera2.ICameraDeviceCallbacks;
import android.hardware.camera2.ICameraDeviceUser;
import android.hardware.camera2.impl.CameraMetadataNative;
import android.hardware.camera2.impl.CaptureResultExtras;
import android.hardware.camera2.params.OutputConfiguration;
import android.hardware.camera2.utils.BinderHolder;
import android.media.Image;
import android.media.ImageReader;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.RemoteException;
import android.os.SystemClock;
import android.test.AndroidTestCase;
import android.test.suitebuilder.annotation.SmallTest;
import android.util.Log;
import android.view.Surface;
import static android.hardware.camera2.CameraDevice.TEMPLATE_PREVIEW;
import com.android.mediaframeworktest.MediaFrameworkIntegrationTestRunner;
import org.mockito.ArgumentMatcher;
import org.mockito.ArgumentCaptor;
import static org.mockito.Mockito.*;
public class CameraDeviceBinderTest extends AndroidTestCase {
private static String TAG = "CameraDeviceBinderTest";
// Number of streaming callbacks need to check.
private static int NUM_CALLBACKS_CHECKED = 10;
// Wait for capture result timeout value: 1500ms
private final static int WAIT_FOR_COMPLETE_TIMEOUT_MS = 1500;
// Wait for flush timeout value: 1000ms
private final static int WAIT_FOR_FLUSH_TIMEOUT_MS = 1000;
// Wait for idle timeout value: 2000ms
private final static int WAIT_FOR_IDLE_TIMEOUT_MS = 2000;
// Wait while camera device starts working on requests
private final static int WAIT_FOR_WORK_MS = 300;
// Default size is VGA, which is mandatory camera supported image size by CDD.
private static final int DEFAULT_IMAGE_WIDTH = 640;
private static final int DEFAULT_IMAGE_HEIGHT = 480;
private static final int MAX_NUM_IMAGES = 5;
private int mCameraId;
private ICameraDeviceUser mCameraUser;
private CameraBinderTestUtils mUtils;
private ICameraDeviceCallbacks.Stub mMockCb;
private Surface mSurface;
private OutputConfiguration mOutputConfiguration;
private HandlerThread mHandlerThread;
private Handler mHandler;
ImageReader mImageReader;
public CameraDeviceBinderTest() {
}
private class ImageDropperListener implements ImageReader.OnImageAvailableListener {
@Override
public void onImageAvailable(ImageReader reader) {
Image image = reader.acquireNextImage();
if (image != null) image.close();
}
}
public class DummyCameraDeviceCallbacks extends ICameraDeviceCallbacks.Stub {
/*
* (non-Javadoc)
* @see
* android.hardware.camera2.ICameraDeviceCallbacks#onDeviceError(int,
* android.hardware.camera2.CaptureResultExtras)
*/
public void onDeviceError(int errorCode, CaptureResultExtras resultExtras)
throws RemoteException {
// TODO Auto-generated method stub
}
/*
* (non-Javadoc)
* @see android.hardware.camera2.ICameraDeviceCallbacks#onDeviceIdle()
*/
public void onDeviceIdle() throws RemoteException {
// TODO Auto-generated method stub
}
/*
* (non-Javadoc)
* @see
* android.hardware.camera2.ICameraDeviceCallbacks#onCaptureStarted(
* android.hardware.camera2.CaptureResultExtras, long)
*/
public void onCaptureStarted(CaptureResultExtras resultExtras, long timestamp)
throws RemoteException {
// TODO Auto-generated method stub
}
/*
* (non-Javadoc)
* @see
* android.hardware.camera2.ICameraDeviceCallbacks#onResultReceived(
* android.hardware.camera2.impl.CameraMetadataNative,
* android.hardware.camera2.CaptureResultExtras)
*/
public void onResultReceived(CameraMetadataNative result, CaptureResultExtras resultExtras)
throws RemoteException {
// TODO Auto-generated method stub
}
/*
* (non-Javadoc)
* @see android.hardware.camera2.ICameraDeviceCallbacks#onPrepared()
*/
@Override
public void onPrepared(int streamId) throws RemoteException {
// TODO Auto-generated method stub
}
}
class IsMetadataNotEmpty extends ArgumentMatcher<CameraMetadataNative> {
@Override
public boolean matches(Object obj) {
return !((CameraMetadataNative) obj).isEmpty();
}
}
private void createDefaultSurface() {
mImageReader =
ImageReader.newInstance(DEFAULT_IMAGE_WIDTH,
DEFAULT_IMAGE_HEIGHT,
ImageFormat.YUV_420_888,
MAX_NUM_IMAGES);
mImageReader.setOnImageAvailableListener(new ImageDropperListener(), mHandler);
mSurface = mImageReader.getSurface();
mOutputConfiguration = new OutputConfiguration(mSurface);
}
private CaptureRequest.Builder createDefaultBuilder(boolean needStream) throws Exception {
CameraMetadataNative metadata = new CameraMetadataNative();
assertTrue(metadata.isEmpty());
int status = mCameraUser.createDefaultRequest(TEMPLATE_PREVIEW, /* out */metadata);
assertEquals(CameraBinderTestUtils.NO_ERROR, status);
assertFalse(metadata.isEmpty());
CaptureRequest.Builder request = new CaptureRequest.Builder(metadata, /*reprocess*/false);
assertFalse(request.isEmpty());
assertFalse(metadata.isEmpty());
if (needStream) {
int streamId = mCameraUser.createStream(mOutputConfiguration);
assertEquals(0, streamId);
request.addTarget(mSurface);
}
return request;
}
private int submitCameraRequest(CaptureRequest request, boolean streaming) throws Exception {
int requestId = mCameraUser.submitRequest(request, streaming, null);
assertTrue(
"Request IDs should be non-negative (expected: >= 0, actual: " + requestId + ")",
requestId >= 0);
return requestId;
}
@Override
protected void setUp() throws Exception {
super.setUp();
/**
* Workaround for mockito and JB-MR2 incompatibility
*
* Avoid java.lang.IllegalArgumentException: dexcache == null
* https://code.google.com/p/dexmaker/issues/detail?id=2
*/
System.setProperty("dexmaker.dexcache", getContext().getCacheDir().toString());
mUtils = new CameraBinderTestUtils(getContext());
// This cannot be set in the constructor, since the onCreate isn't
// called yet
mCameraId = MediaFrameworkIntegrationTestRunner.mCameraId;
ICameraDeviceCallbacks.Stub dummyCallbacks = new DummyCameraDeviceCallbacks();
String clientPackageName = getContext().getPackageName();
mMockCb = spy(dummyCallbacks);
BinderHolder holder = new BinderHolder();
mUtils.getCameraService().connectDevice(mMockCb, mCameraId,
clientPackageName, CameraBinderTestUtils.USE_CALLING_UID, holder);
mCameraUser = ICameraDeviceUser.Stub.asInterface(holder.getBinder());
assertNotNull(String.format("Camera %s was null", mCameraId), mCameraUser);
mHandlerThread = new HandlerThread(TAG);
mHandlerThread.start();
mHandler = new Handler(mHandlerThread.getLooper());
createDefaultSurface();
Log.v(TAG, String.format("Camera %s connected", mCameraId));
}
@Override
protected void tearDown() throws Exception {
mCameraUser.disconnect();
mCameraUser = null;
mSurface.release();
mImageReader.close();
mHandlerThread.quitSafely();
}
@SmallTest
public void testCreateDefaultRequest() throws Exception {
CameraMetadataNative metadata = new CameraMetadataNative();
assertTrue(metadata.isEmpty());
int status = mCameraUser.createDefaultRequest(TEMPLATE_PREVIEW, /* out */metadata);
assertEquals(CameraBinderTestUtils.NO_ERROR, status);
assertFalse(metadata.isEmpty());
}
@SmallTest
public void testCreateStream() throws Exception {
int streamId = mCameraUser.createStream(mOutputConfiguration);
assertEquals(0, streamId);
assertEquals(CameraBinderTestUtils.ALREADY_EXISTS,
mCameraUser.createStream(mOutputConfiguration));
assertEquals(CameraBinderTestUtils.NO_ERROR, mCameraUser.deleteStream(streamId));
}
@SmallTest
public void testDeleteInvalidStream() throws Exception {
assertEquals(CameraBinderTestUtils.BAD_VALUE, mCameraUser.deleteStream(-1));
assertEquals(CameraBinderTestUtils.BAD_VALUE, mCameraUser.deleteStream(0));
assertEquals(CameraBinderTestUtils.BAD_VALUE, mCameraUser.deleteStream(1));
assertEquals(CameraBinderTestUtils.BAD_VALUE, mCameraUser.deleteStream(0xC0FFEE));
}
@SmallTest
public void testCreateStreamTwo() throws Exception {
// Create first stream
int streamId = mCameraUser.createStream(mOutputConfiguration);
assertEquals(0, streamId);
assertEquals(CameraBinderTestUtils.ALREADY_EXISTS,
mCameraUser.createStream(mOutputConfiguration));
// Create second stream with a different surface.
SurfaceTexture surfaceTexture = new SurfaceTexture(/* ignored */0);
surfaceTexture.setDefaultBufferSize(640, 480);
Surface surface2 = new Surface(surfaceTexture);
OutputConfiguration output2 = new OutputConfiguration(surface2);
int streamId2 = mCameraUser.createStream(output2);
assertEquals(1, streamId2);
// Clean up streams
assertEquals(CameraBinderTestUtils.NO_ERROR, mCameraUser.deleteStream(streamId));
assertEquals(CameraBinderTestUtils.NO_ERROR, mCameraUser.deleteStream(streamId2));
}
@SmallTest
public void testSubmitBadRequest() throws Exception {
CaptureRequest.Builder builder = createDefaultBuilder(/* needStream */false);
CaptureRequest request1 = builder.build();
int status = mCameraUser.submitRequest(request1, /* streaming */false, null);
assertEquals("Expected submitRequest to return BAD_VALUE " +
"since we had 0 surface targets set.", CameraBinderTestUtils.BAD_VALUE, status);
builder.addTarget(mSurface);
CaptureRequest request2 = builder.build();
status = mCameraUser.submitRequest(request2, /* streaming */false, null);
assertEquals("Expected submitRequest to return BAD_VALUE since " +
"the target surface wasn't registered with createStream.",
CameraBinderTestUtils.BAD_VALUE, status);
}
@SmallTest
public void testSubmitGoodRequest() throws Exception {
CaptureRequest.Builder builder = createDefaultBuilder(/* needStream */true);
CaptureRequest request = builder.build();
// Submit valid request twice.
int requestId1 = submitCameraRequest(request, /* streaming */false);
int requestId2 = submitCameraRequest(request, /* streaming */false);
assertNotSame("Request IDs should be unique for multiple requests", requestId1, requestId2);
}
@SmallTest
public void testSubmitStreamingRequest() throws Exception {
CaptureRequest.Builder builder = createDefaultBuilder(/* needStream */true);
CaptureRequest request = builder.build();
// Submit valid request once (non-streaming), and another time
// (streaming)
int requestId1 = submitCameraRequest(request, /* streaming */false);
int requestIdStreaming = submitCameraRequest(request, /* streaming */true);
assertNotSame("Request IDs should be unique for multiple requests", requestId1,
requestIdStreaming);
int status = mCameraUser.cancelRequest(-1, null);
assertEquals("Invalid request IDs should not be cancellable",
CameraBinderTestUtils.BAD_VALUE, status);
status = mCameraUser.cancelRequest(requestId1, null);
assertEquals("Non-streaming request IDs should not be cancellable",
CameraBinderTestUtils.BAD_VALUE, status);
status = mCameraUser.cancelRequest(requestIdStreaming, null);
assertEquals("Streaming request IDs should be cancellable", CameraBinderTestUtils.NO_ERROR,
status);
}
@SmallTest
public void testCameraInfo() throws RemoteException {
CameraMetadataNative info = new CameraMetadataNative();
int status = mCameraUser.getCameraInfo(/*out*/info);
assertEquals(CameraBinderTestUtils.NO_ERROR, status);
assertFalse(info.isEmpty());
assertNotNull(info.get(CameraCharacteristics.SCALER_AVAILABLE_FORMATS));
}
@SmallTest
public void testCameraCharacteristics() throws RemoteException {
CameraMetadataNative info = new CameraMetadataNative();
int status = mUtils.getCameraService().getCameraCharacteristics(mCameraId, /*out*/info);
assertEquals(CameraBinderTestUtils.NO_ERROR, status);
assertFalse(info.isEmpty());
assertNotNull(info.get(CameraCharacteristics.SCALER_AVAILABLE_FORMATS));
}
@SmallTest
public void testWaitUntilIdle() throws Exception {
CaptureRequest.Builder builder = createDefaultBuilder(/* needStream */true);
int requestIdStreaming = submitCameraRequest(builder.build(), /* streaming */true);
// Test Bad case first: waitUntilIdle when there is active repeating request
int status = mCameraUser.waitUntilIdle();
assertEquals("waitUntilIdle is invalid operation when there is active repeating request",
CameraBinderTestUtils.INVALID_OPERATION, status);
// Test good case, waitUntilIdle when there is no active repeating request
status = mCameraUser.cancelRequest(requestIdStreaming, null);
assertEquals(CameraBinderTestUtils.NO_ERROR, status);
status = mCameraUser.waitUntilIdle();
assertEquals(CameraBinderTestUtils.NO_ERROR, status);
}
@SmallTest
public void testCaptureResultCallbacks() throws Exception {
IsMetadataNotEmpty matcher = new IsMetadataNotEmpty();
CaptureRequest request = createDefaultBuilder(/* needStream */true).build();
// Test both single request and streaming request.
verify(mMockCb, timeout(WAIT_FOR_COMPLETE_TIMEOUT_MS).times(1)).onResultReceived(
argThat(matcher),
any(CaptureResultExtras.class));
verify(mMockCb, timeout(WAIT_FOR_COMPLETE_TIMEOUT_MS).atLeast(NUM_CALLBACKS_CHECKED))
.onResultReceived(
argThat(matcher),
any(CaptureResultExtras.class));
}
@SmallTest
public void testCaptureStartedCallbacks() throws Exception {
CaptureRequest request = createDefaultBuilder(/* needStream */true).build();
ArgumentCaptor<Long> timestamps = ArgumentCaptor.forClass(Long.class);
// Test both single request and streaming request.
int requestId1 = submitCameraRequest(request, /* streaming */false);
verify(mMockCb, timeout(WAIT_FOR_COMPLETE_TIMEOUT_MS).times(1)).onCaptureStarted(
any(CaptureResultExtras.class),
anyLong());
int streamingId = submitCameraRequest(request, /* streaming */true);
verify(mMockCb, timeout(WAIT_FOR_COMPLETE_TIMEOUT_MS).atLeast(NUM_CALLBACKS_CHECKED))
.onCaptureStarted(
any(CaptureResultExtras.class),
timestamps.capture());
long timestamp = 0; // All timestamps should be larger than 0.
for (Long nextTimestamp : timestamps.getAllValues()) {
Log.v(TAG, "next t: " + nextTimestamp + " current t: " + timestamp);
assertTrue("Captures are out of order", timestamp < nextTimestamp);
timestamp = nextTimestamp;
}
}
@SmallTest
public void testIdleCallback() throws Exception {
int status;
CaptureRequest request = createDefaultBuilder(/* needStream */true).build();
// Try streaming
int streamingId = submitCameraRequest(request, /* streaming */true);
// Wait a bit to fill up the queue
SystemClock.sleep(WAIT_FOR_WORK_MS);
// Cancel and make sure we eventually quiesce
status = mCameraUser.cancelRequest(streamingId, null);
verify(mMockCb, timeout(WAIT_FOR_IDLE_TIMEOUT_MS).times(1)).onDeviceIdle();
// Submit a few capture requests
int requestId1 = submitCameraRequest(request, /* streaming */false);
int requestId2 = submitCameraRequest(request, /* streaming */false);
int requestId3 = submitCameraRequest(request, /* streaming */false);
int requestId4 = submitCameraRequest(request, /* streaming */false);
int requestId5 = submitCameraRequest(request, /* streaming */false);
// And wait for more idle
verify(mMockCb, timeout(WAIT_FOR_IDLE_TIMEOUT_MS).times(2)).onDeviceIdle();
}
@SmallTest
public void testFlush() throws Exception {
int status;
// Initial flush should work
status = mCameraUser.flush(null);
assertEquals(CameraBinderTestUtils.NO_ERROR, status);
// Then set up a stream
CaptureRequest request = createDefaultBuilder(/* needStream */true).build();
// Flush should still be a no-op, really
status = mCameraUser.flush(null);
assertEquals(CameraBinderTestUtils.NO_ERROR, status);
// Submit a few capture requests
int requestId1 = submitCameraRequest(request, /* streaming */false);
int requestId2 = submitCameraRequest(request, /* streaming */false);
int requestId3 = submitCameraRequest(request, /* streaming */false);
int requestId4 = submitCameraRequest(request, /* streaming */false);
int requestId5 = submitCameraRequest(request, /* streaming */false);
// Then flush and wait for idle
status = mCameraUser.flush(null);
assertEquals(CameraBinderTestUtils.NO_ERROR, status);
verify(mMockCb, timeout(WAIT_FOR_FLUSH_TIMEOUT_MS).times(1)).onDeviceIdle();
// Now a streaming request
int streamingId = submitCameraRequest(request, /* streaming */true);
// Wait a bit to fill up the queue
SystemClock.sleep(WAIT_FOR_WORK_MS);
// Then flush and wait for the idle callback
status = mCameraUser.flush(null);
assertEquals(CameraBinderTestUtils.NO_ERROR, status);
verify(mMockCb, timeout(WAIT_FOR_FLUSH_TIMEOUT_MS).times(2)).onDeviceIdle();
// TODO: When errors are hooked up, count that errors + successful
// requests equal to 5.
}
}
|