1 /*
2 * Copyright (C) 2015 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 #define LOG_NDEBUG 0
18 #define LOG_TAG "CameraBinderTests"
19
20 #include <binder/IInterface.h>
21 #include <binder/IServiceManager.h>
22 #include <binder/Parcel.h>
23 #include <binder/ProcessState.h>
24 #include <utils/Errors.h>
25 #include <utils/Log.h>
26 #include <utils/List.h>
27 #include <utils/String8.h>
28 #include <utils/String16.h>
29 #include <utils/Condition.h>
30 #include <utils/Mutex.h>
31 #include <system/graphics.h>
32 #include <hardware/camera3.h>
33 #include <hardware/gralloc.h>
34
35 #include <camera/CameraMetadata.h>
36 #include <android/hardware/ICameraService.h>
37 #include <android/hardware/ICameraServiceListener.h>
38 #include <android/hardware/BnCameraServiceListener.h>
39 #include <android/hardware/camera2/ICameraDeviceUser.h>
40 #include <android/hardware/camera2/ICameraDeviceCallbacks.h>
41 #include <android/hardware/camera2/BnCameraDeviceCallbacks.h>
42 #include <camera/camera2/CaptureRequest.h>
43 #include <camera/camera2/OutputConfiguration.h>
44 #include <camera/camera2/SessionConfiguration.h>
45 #include <camera/camera2/SubmitInfo.h>
46 #include <camera/CameraUtils.h>
47 #include <camera/StringUtils.h>
48
49 #include <gui/BufferItemConsumer.h>
50 #include <gui/IGraphicBufferProducer.h>
51 #include <gui/Surface.h>
52
53 #include <gtest/gtest.h>
54 #include <unistd.h>
55 #include <stdint.h>
56 #include <utility>
57 #include <vector>
58 #include <map>
59 #include <algorithm>
60
61 using namespace android;
62 using ::android::hardware::ICameraService;
63 using ::android::hardware::camera2::ICameraDeviceUser;
64
65 #define ASSERT_NOT_NULL(x) \
66 ASSERT_TRUE((x) != nullptr)
67
68 #define SETUP_TIMEOUT 2000000000 // ns
69 #define IDLE_TIMEOUT 2000000000 // ns
70
71 // Stub listener implementation
72 class TestCameraServiceListener : public hardware::BnCameraServiceListener {
73 std::map<std::string, int32_t> mCameraTorchStatuses;
74 std::map<std::string, int32_t> mCameraStatuses;
75 mutable Mutex mLock;
76 mutable Condition mCondition;
77 mutable Condition mTorchCondition;
78 public:
~TestCameraServiceListener()79 virtual ~TestCameraServiceListener() {};
80
onStatusChanged(int32_t status,const std::string & cameraId,int32_t)81 virtual binder::Status onStatusChanged(int32_t status, const std::string& cameraId,
82 [[maybe_unused]] int32_t /*deviceId*/) override {
83 Mutex::Autolock l(mLock);
84 mCameraStatuses[cameraId] = status;
85 mCondition.broadcast();
86 return binder::Status::ok();
87 }
88
onPhysicalCameraStatusChanged(int32_t,const std::string &,const std::string &,int32_t)89 virtual binder::Status onPhysicalCameraStatusChanged([[maybe_unused]] int32_t /*status*/,
90 [[maybe_unused]] const std::string& /*cameraId*/,
91 [[maybe_unused]] const std::string& /*physicalCameraId*/,
92 [[maybe_unused]] int32_t /*deviceId*/) override {
93 // No op
94 return binder::Status::ok();
95 }
96
onTorchStatusChanged(int32_t status,const std::string & cameraId,int32_t)97 virtual binder::Status onTorchStatusChanged(int32_t status,
98 const std::string& cameraId, [[maybe_unused]] int32_t /*deviceId*/) override {
99 Mutex::Autolock l(mLock);
100 mCameraTorchStatuses[cameraId] = status;
101 mTorchCondition.broadcast();
102 return binder::Status::ok();
103 }
104
onTorchStrengthLevelChanged(const std::string &,int32_t,int32_t)105 virtual binder::Status onTorchStrengthLevelChanged(
106 [[maybe_unused]] const std::string& /*cameraId*/,
107 [[maybe_unused]] int32_t /*torchStrength*/,
108 [[maybe_unused]] int32_t /*deviceId*/) override {
109 // No op
110 return binder::Status::ok();
111 }
112
onCameraAccessPrioritiesChanged()113 virtual binder::Status onCameraAccessPrioritiesChanged() override {
114 // No op
115 return binder::Status::ok();
116 }
117
onCameraOpened(const std::string &,const std::string &,int32_t)118 virtual binder::Status onCameraOpened([[maybe_unused]] const std::string& /*cameraId*/,
119 [[maybe_unused]] const std::string& /*clientPackageName*/,
120 [[maybe_unused]] int32_t /*deviceId*/) {
121 // No op
122 return binder::Status::ok();
123 }
124
onCameraClosed(const std::string &,int32_t)125 virtual binder::Status onCameraClosed([[maybe_unused]] const std::string& /*cameraId*/,
126 [[maybe_unused]] int32_t /*deviceId*/) override {
127 // No op
128 return binder::Status::ok();
129 }
130
waitForNumCameras(size_t num) const131 bool waitForNumCameras(size_t num) const {
132 Mutex::Autolock l(mLock);
133
134 if (mCameraStatuses.size() == num) {
135 return true;
136 }
137
138 while (mCameraStatuses.size() < num) {
139 if (mCondition.waitRelative(mLock, SETUP_TIMEOUT) != OK) {
140 return false;
141 }
142 }
143 return true;
144 }
145
waitForTorchState(int32_t status,int32_t cameraId) const146 bool waitForTorchState(int32_t status, int32_t cameraId) const {
147 Mutex::Autolock l(mLock);
148
149 const auto& iter = mCameraTorchStatuses.find(std::to_string(cameraId));
150 if (iter != mCameraTorchStatuses.end() && iter->second == status) {
151 return true;
152 }
153
154 bool foundStatus = false;
155 while (!foundStatus) {
156 if (mTorchCondition.waitRelative(mLock, SETUP_TIMEOUT) != OK) {
157 return false;
158 }
159 const auto& iter =
160 mCameraTorchStatuses.find(std::to_string(cameraId));
161 foundStatus = (iter != mCameraTorchStatuses.end() && iter->second == status);
162 }
163 return true;
164 }
165
getTorchStatus(int32_t cameraId) const166 int32_t getTorchStatus(int32_t cameraId) const {
167 Mutex::Autolock l(mLock);
168 const auto& iter = mCameraTorchStatuses.find(std::to_string(cameraId));
169 if (iter == mCameraTorchStatuses.end()) {
170 return hardware::ICameraServiceListener::TORCH_STATUS_UNKNOWN;
171 }
172 return iter->second;
173 }
174
getStatus(const std::string & cameraId) const175 int32_t getStatus(const std::string& cameraId) const {
176 Mutex::Autolock l(mLock);
177 const auto& iter = mCameraStatuses.find(cameraId);
178 if (iter == mCameraStatuses.end()) {
179 return hardware::ICameraServiceListener::STATUS_UNKNOWN;
180 }
181 return iter->second;
182 }
183 };
184
185 // Callback implementation
186 class TestCameraDeviceCallbacks : public hardware::camera2::BnCameraDeviceCallbacks {
187 public:
188 enum Status {
189 IDLE,
190 ERROR,
191 PREPARED,
192 RUNNING,
193 SENT_RESULT,
194 UNINITIALIZED,
195 REPEATING_REQUEST_ERROR,
196 REQUEST_QUEUE_EMPTY,
197 };
198
199 protected:
200 bool mError;
201 int32_t mLastStatus;
202 mutable std::vector<int32_t> mStatusesHit;
203 mutable Mutex mLock;
204 mutable Condition mStatusCondition;
205 public:
TestCameraDeviceCallbacks()206 TestCameraDeviceCallbacks() : mError(false), mLastStatus(UNINITIALIZED) {}
207
~TestCameraDeviceCallbacks()208 virtual ~TestCameraDeviceCallbacks() {}
209
onDeviceError(int errorCode,const CaptureResultExtras & resultExtras)210 virtual binder::Status onDeviceError(int errorCode,
211 const CaptureResultExtras& resultExtras) {
212 (void) resultExtras;
213 ALOGE("%s: onDeviceError occurred with: %d", __FUNCTION__, static_cast<int>(errorCode));
214 Mutex::Autolock l(mLock);
215 mError = true;
216 mLastStatus = ERROR;
217 mStatusesHit.push_back(mLastStatus);
218 mStatusCondition.broadcast();
219 return binder::Status::ok();
220 }
221
onDeviceIdle()222 virtual binder::Status onDeviceIdle() {
223 Mutex::Autolock l(mLock);
224 mLastStatus = IDLE;
225 mStatusesHit.push_back(mLastStatus);
226 mStatusCondition.broadcast();
227 return binder::Status::ok();
228 }
229
onCaptureStarted(const CaptureResultExtras & resultExtras,int64_t timestamp)230 virtual binder::Status onCaptureStarted(const CaptureResultExtras& resultExtras,
231 int64_t timestamp) {
232 (void) resultExtras;
233 (void) timestamp;
234 Mutex::Autolock l(mLock);
235 mLastStatus = RUNNING;
236 mStatusesHit.push_back(mLastStatus);
237 mStatusCondition.broadcast();
238 return binder::Status::ok();
239 }
240
onResultReceived(const CameraMetadata & metadata,const CaptureResultExtras & resultExtras,const std::vector<PhysicalCaptureResultInfo> & physicalResultInfos)241 virtual binder::Status onResultReceived(const CameraMetadata& metadata,
242 const CaptureResultExtras& resultExtras,
243 const std::vector<PhysicalCaptureResultInfo>& physicalResultInfos) {
244 (void) metadata;
245 (void) resultExtras;
246 (void) physicalResultInfos;
247 Mutex::Autolock l(mLock);
248 mLastStatus = SENT_RESULT;
249 mStatusesHit.push_back(mLastStatus);
250 mStatusCondition.broadcast();
251 return binder::Status::ok();
252 }
253
onPrepared(int streamId)254 virtual binder::Status onPrepared(int streamId) {
255 (void) streamId;
256 Mutex::Autolock l(mLock);
257 mLastStatus = PREPARED;
258 mStatusesHit.push_back(mLastStatus);
259 mStatusCondition.broadcast();
260 return binder::Status::ok();
261 }
262
onRepeatingRequestError(int64_t lastFrameNumber,int32_t stoppedSequenceId)263 virtual binder::Status onRepeatingRequestError(
264 int64_t lastFrameNumber, int32_t stoppedSequenceId) {
265 (void) lastFrameNumber;
266 (void) stoppedSequenceId;
267 Mutex::Autolock l(mLock);
268 mLastStatus = REPEATING_REQUEST_ERROR;
269 mStatusesHit.push_back(mLastStatus);
270 mStatusCondition.broadcast();
271 return binder::Status::ok();
272 }
273
onRequestQueueEmpty()274 virtual binder::Status onRequestQueueEmpty() {
275 Mutex::Autolock l(mLock);
276 mLastStatus = REQUEST_QUEUE_EMPTY;
277 mStatusesHit.push_back(mLastStatus);
278 mStatusCondition.broadcast();
279 return binder::Status::ok();
280 }
281
282 // Test helper functions:
283
hadError() const284 bool hadError() const {
285 Mutex::Autolock l(mLock);
286 return mError;
287 }
288
waitForStatus(Status status) const289 bool waitForStatus(Status status) const {
290 Mutex::Autolock l(mLock);
291 if (mLastStatus == status) {
292 return true;
293 }
294
295 while (std::find(mStatusesHit.begin(), mStatusesHit.end(), status)
296 == mStatusesHit.end()) {
297
298 if (mStatusCondition.waitRelative(mLock, IDLE_TIMEOUT) != OK) {
299 mStatusesHit.clear();
300 return false;
301 }
302 }
303 mStatusesHit.clear();
304
305 return true;
306 }
307
clearStatus() const308 void clearStatus() const {
309 Mutex::Autolock l(mLock);
310 mStatusesHit.clear();
311 }
312
waitForIdle() const313 bool waitForIdle() const {
314 return waitForStatus(IDLE);
315 }
316 };
317
318 namespace {
319 Mutex gLock;
320 class DeathNotifier : public IBinder::DeathRecipient
321 {
322 public:
DeathNotifier()323 DeathNotifier() {}
324
binderDied(const wp<IBinder> &)325 virtual void binderDied(const wp<IBinder>& /*who*/) {
326 ALOGV("binderDied");
327 Mutex::Autolock _l(gLock);
328 ALOGW("Camera service died!");
329 }
330 };
331 sp<DeathNotifier> gDeathNotifier;
332 } // anonymous namespace
333
334 // Exercise basic binder calls for the camera service
TEST(CameraServiceBinderTest,CheckBinderCameraService)335 TEST(CameraServiceBinderTest, CheckBinderCameraService) {
336 ProcessState::self()->startThreadPool();
337 sp<IServiceManager> sm = defaultServiceManager();
338 sp<IBinder> binder = sm->getService(String16("media.camera"));
339 ASSERT_NOT_NULL(binder);
340 if (gDeathNotifier == NULL) {
341 gDeathNotifier = new DeathNotifier();
342 }
343 binder->linkToDeath(gDeathNotifier);
344 sp<hardware::ICameraService> service =
345 interface_cast<hardware::ICameraService>(binder);
346
347 binder::Status res;
348
349 int32_t numCameras = 0;
350 res = service->getNumberOfCameras(hardware::ICameraService::CAMERA_TYPE_ALL, kDefaultDeviceId,
351 /*devicePolicy*/0, &numCameras);
352 EXPECT_TRUE(res.isOk()) << res;
353 EXPECT_LE(0, numCameras);
354
355 // Check listener binder calls
356 sp<TestCameraServiceListener> listener(new TestCameraServiceListener());
357 std::vector<hardware::CameraStatus> statuses;
358 res = service->addListener(listener, &statuses);
359 EXPECT_TRUE(res.isOk()) << res;
360
361 EXPECT_EQ(numCameras, static_cast<const int>(statuses.size()));
362 for (const auto &it : statuses) {
363 listener->onStatusChanged(it.status, it.cameraId, kDefaultDeviceId);
364 }
365
366 for (int32_t i = 0; i < numCameras; i++) {
367 std::string cameraId = std::to_string(i);
368 bool isSupported = false;
369 res = service->supportsCameraApi(cameraId,
370 hardware::ICameraService::API_VERSION_2, &isSupported);
371 EXPECT_TRUE(res.isOk()) << res;
372
373 // We only care about binder calls for the Camera2 API. Camera1 is deprecated.
374 if (!isSupported) {
375 continue;
376 }
377
378 // Check metadata binder call
379 CameraMetadata metadata;
380 res = service->getCameraCharacteristics(cameraId,
381 /*targetSdkVersion*/__ANDROID_API_FUTURE__, /*overrideToPortrait*/false,
382 kDefaultDeviceId, /*devicePolicy*/0, &metadata);
383 EXPECT_TRUE(res.isOk()) << res;
384 EXPECT_FALSE(metadata.isEmpty());
385
386 // Make sure we're available, or skip device tests otherwise
387 int32_t s = listener->getStatus(cameraId);
388 EXPECT_EQ(::android::hardware::ICameraServiceListener::STATUS_PRESENT, s);
389 if (s != ::android::hardware::ICameraServiceListener::STATUS_PRESENT) {
390 continue;
391 }
392
393 // Check connect binder calls
394 sp<TestCameraDeviceCallbacks> callbacks(new TestCameraDeviceCallbacks());
395 sp<hardware::camera2::ICameraDeviceUser> device;
396 res = service->connectDevice(callbacks, cameraId, "meeeeeeeee!",
397 {}, hardware::ICameraService::USE_CALLING_UID, /*oomScoreOffset*/ 0,
398 /*targetSdkVersion*/__ANDROID_API_FUTURE__,
399 /*overrideToPortrait*/false, kDefaultDeviceId, /*devicePolicy*/0, /*out*/&device);
400 EXPECT_TRUE(res.isOk()) << res;
401 ASSERT_NE(nullptr, device.get());
402 device->disconnect();
403 EXPECT_FALSE(callbacks->hadError());
404
405 int32_t torchStatus = listener->getTorchStatus(i);
406 if (torchStatus == hardware::ICameraServiceListener::TORCH_STATUS_AVAILABLE_OFF) {
407 // Check torch calls
408 res = service->setTorchMode(cameraId,
409 /*enabled*/true, callbacks, kDefaultDeviceId, /*devicePolicy*/0);
410 EXPECT_TRUE(res.isOk()) << res;
411 EXPECT_TRUE(listener->waitForTorchState(
412 hardware::ICameraServiceListener::TORCH_STATUS_AVAILABLE_ON, i));
413 res = service->setTorchMode(cameraId,
414 /*enabled*/false, callbacks, kDefaultDeviceId, /*devicePolicy*/0);
415 EXPECT_TRUE(res.isOk()) << res;
416 EXPECT_TRUE(listener->waitForTorchState(
417 hardware::ICameraServiceListener::TORCH_STATUS_AVAILABLE_OFF, i));
418 }
419 }
420
421 res = service->removeListener(listener);
422 EXPECT_TRUE(res.isOk()) << res;
423 }
424
425 // Test fixture for client focused binder tests
426 class CameraClientBinderTest : public testing::Test {
427 protected:
428 sp<hardware::ICameraService> service;
429 int32_t numCameras;
430 std::vector<std::pair<sp<TestCameraDeviceCallbacks>, sp<hardware::camera2::ICameraDeviceUser>>>
431 openDeviceList;
432 sp<TestCameraServiceListener> serviceListener;
433
434 std::pair<sp<TestCameraDeviceCallbacks>, sp<hardware::camera2::ICameraDeviceUser>>
openNewDevice(const std::string & deviceId)435 openNewDevice(const std::string& deviceId) {
436 sp<TestCameraDeviceCallbacks> callbacks(new TestCameraDeviceCallbacks());
437 sp<hardware::camera2::ICameraDeviceUser> device;
438 {
439 SCOPED_TRACE("openNewDevice");
440 binder::Status res = service->connectDevice(callbacks, deviceId, "meeeeeeeee!",
441 {}, hardware::ICameraService::USE_CALLING_UID, /*oomScoreOffset*/ 0,
442 /*targetSdkVersion*/__ANDROID_API_FUTURE__,
443 /*overrideToPortrait*/false, kDefaultDeviceId, /*devicePolicy*/0,
444 /*out*/&device);
445 EXPECT_TRUE(res.isOk()) << res;
446 }
447 auto p = std::make_pair(callbacks, device);
448 openDeviceList.push_back(p);
449 return p;
450 }
451
closeDevice(std::pair<sp<TestCameraDeviceCallbacks>,sp<hardware::camera2::ICameraDeviceUser>> & p)452 void closeDevice(std::pair<sp<TestCameraDeviceCallbacks>,
453 sp<hardware::camera2::ICameraDeviceUser>>& p) {
454 if (p.second.get() != nullptr) {
455 binder::Status res = p.second->disconnect();
456 EXPECT_TRUE(res.isOk()) << res;
457 {
458 SCOPED_TRACE("closeDevice");
459 EXPECT_FALSE(p.first->hadError());
460 }
461 }
462 auto iter = std::find(openDeviceList.begin(), openDeviceList.end(), p);
463 if (iter != openDeviceList.end()) {
464 openDeviceList.erase(iter);
465 }
466 }
467
SetUp()468 virtual void SetUp() {
469 ProcessState::self()->startThreadPool();
470 sp<IServiceManager> sm = defaultServiceManager();
471 sp<IBinder> binder = sm->getService(String16("media.camera"));
472 service = interface_cast<hardware::ICameraService>(binder);
473 serviceListener = new TestCameraServiceListener();
474 std::vector<hardware::CameraStatus> statuses;
475 service->addListener(serviceListener, &statuses);
476 for (const auto &it : statuses) {
477 serviceListener->onStatusChanged(it.status, it.cameraId, kDefaultDeviceId);
478 }
479 service->getNumberOfCameras(hardware::ICameraService::CAMERA_TYPE_BACKWARD_COMPATIBLE,
480 kDefaultDeviceId, /*devicePolicy*/0, &numCameras);
481 }
482
TearDown()483 virtual void TearDown() {
484 service = nullptr;
485 numCameras = 0;
486 for (auto& p : openDeviceList) {
487 closeDevice(p);
488 }
489 }
490 };
491
TEST_F(CameraClientBinderTest,CheckBinderCameraDeviceUser)492 TEST_F(CameraClientBinderTest, CheckBinderCameraDeviceUser) {
493 ASSERT_NOT_NULL(service);
494 EXPECT_TRUE(serviceListener->waitForNumCameras(numCameras));
495 for (int32_t i = 0; i < numCameras; i++) {
496 std::string cameraId = std::to_string(i);
497 // Make sure we're available, or skip device tests otherwise
498 int32_t s = serviceListener->getStatus(cameraId);
499 EXPECT_EQ(hardware::ICameraServiceListener::STATUS_PRESENT, s);
500 if (s != hardware::ICameraServiceListener::STATUS_PRESENT) {
501 continue;
502 }
503 binder::Status res;
504 auto p = openNewDevice(cameraId);
505 sp<TestCameraDeviceCallbacks> callbacks = p.first;
506 sp<hardware::camera2::ICameraDeviceUser> device = p.second;
507
508 // Setup a buffer queue; I'm just using the vendor opaque format here as that is
509 // guaranteed to be present
510 sp<IGraphicBufferProducer> gbProducer;
511 sp<IGraphicBufferConsumer> gbConsumer;
512 BufferQueue::createBufferQueue(&gbProducer, &gbConsumer);
513 sp<BufferItemConsumer> opaqueConsumer = new BufferItemConsumer(gbConsumer,
514 GRALLOC_USAGE_SW_READ_NEVER, /*maxImages*/2, /*controlledByApp*/true);
515 EXPECT_TRUE(opaqueConsumer.get() != nullptr);
516 opaqueConsumer->setName(String8("nom nom nom"));
517
518 // Set to VGA dimens for default, as that is guaranteed to be present
519 EXPECT_EQ(OK, gbConsumer->setDefaultBufferSize(640, 480));
520 EXPECT_EQ(OK, gbConsumer->setDefaultBufferFormat(HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED));
521
522 sp<Surface> surface(new Surface(gbProducer, /*controlledByApp*/false));
523
524 std::string noPhysicalId;
525 OutputConfiguration output(gbProducer, /*rotation*/0, noPhysicalId);
526
527 // Can we configure?
528 res = device->beginConfigure();
529 EXPECT_TRUE(res.isOk()) << res;
530 status_t streamId;
531 res = device->createStream(output, &streamId);
532 EXPECT_TRUE(res.isOk()) << res;
533 EXPECT_LE(0, streamId);
534 CameraMetadata sessionParams;
535 std::vector<int> offlineStreamIds;
536 res = device->endConfigure(/*isConstrainedHighSpeed*/ false, sessionParams,
537 ns2ms(systemTime()), &offlineStreamIds);
538 EXPECT_TRUE(res.isOk()) << res;
539 EXPECT_FALSE(callbacks->hadError());
540
541 // Session configuration must also be supported in this case
542 SessionConfiguration sessionConfiguration = { /*inputWidth*/ 0, /*inputHeight*/0,
543 /*inputFormat*/ -1, CAMERA3_STREAM_CONFIGURATION_NORMAL_MODE};
544 sessionConfiguration.addOutputConfiguration(output);
545 bool queryStatus;
546 res = device->isSessionConfigurationSupported(sessionConfiguration, &queryStatus);
547 EXPECT_TRUE(res.isOk() ||
548 (res.serviceSpecificErrorCode() == ICameraService::ERROR_INVALID_OPERATION))
549 << res;
550 if (res.isOk()) {
551 EXPECT_TRUE(queryStatus);
552 }
553
554 // Can we make requests?
555 CameraMetadata requestTemplate;
556 res = device->createDefaultRequest(/*preview template*/1,
557 /*out*/&requestTemplate);
558 EXPECT_TRUE(res.isOk()) << res;
559
560 hardware::camera2::CaptureRequest request;
561 request.mPhysicalCameraSettings.push_back({cameraId, requestTemplate});
562 request.mSurfaceList.add(surface);
563 request.mIsReprocess = false;
564 int64_t lastFrameNumber = 0;
565 int64_t lastFrameNumberPrev = 0;
566 callbacks->clearStatus();
567
568 hardware::camera2::utils::SubmitInfo info;
569 res = device->submitRequest(request, /*streaming*/true, /*out*/&info);
570 EXPECT_TRUE(res.isOk()) << res;
571 EXPECT_TRUE(callbacks->waitForStatus(TestCameraDeviceCallbacks::SENT_RESULT));
572 EXPECT_LE(0, info.mRequestId);
573
574 // Can we stop requests?
575 res = device->cancelRequest(info.mRequestId, /*out*/&lastFrameNumber);
576 EXPECT_TRUE(res.isOk()) << res;
577 EXPECT_TRUE(callbacks->waitForIdle());
578 EXPECT_FALSE(callbacks->hadError());
579
580 // Can we do it again?
581 lastFrameNumberPrev = info.mLastFrameNumber;
582 lastFrameNumber = 0;
583 requestTemplate.clear();
584 res = device->createDefaultRequest(hardware::camera2::ICameraDeviceUser::TEMPLATE_PREVIEW,
585 /*out*/&requestTemplate);
586 EXPECT_TRUE(res.isOk()) << res;
587 hardware::camera2::CaptureRequest request2;
588 request2.mPhysicalCameraSettings.push_back({cameraId, requestTemplate});
589 request2.mSurfaceList.add(surface);
590 request2.mIsReprocess = false;
591 callbacks->clearStatus();
592 hardware::camera2::utils::SubmitInfo info2;
593 res = device->submitRequest(request2, /*streaming*/true,
594 /*out*/&info2);
595 EXPECT_TRUE(res.isOk()) << res;
596 EXPECT_EQ(hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES,
597 info2.mLastFrameNumber);
598 lastFrameNumber = 0;
599 EXPECT_TRUE(callbacks->waitForStatus(TestCameraDeviceCallbacks::SENT_RESULT));
600 EXPECT_LE(0, info2.mRequestId);
601 res = device->cancelRequest(info2.mRequestId, /*out*/&lastFrameNumber);
602 EXPECT_TRUE(res.isOk()) << res;
603 EXPECT_TRUE(callbacks->waitForIdle());
604 EXPECT_LE(lastFrameNumberPrev, lastFrameNumber);
605 sleep(/*second*/1); // allow some time for errors to show up, if any
606 EXPECT_FALSE(callbacks->hadError());
607
608 // Can we do it with a request list?
609 lastFrameNumberPrev = lastFrameNumber;
610 lastFrameNumber = 0;
611 requestTemplate.clear();
612 CameraMetadata requestTemplate2;
613 res = device->createDefaultRequest(hardware::camera2::ICameraDeviceUser::TEMPLATE_PREVIEW,
614 /*out*/&requestTemplate);
615 EXPECT_TRUE(res.isOk()) << res;
616 res = device->createDefaultRequest(hardware::camera2::ICameraDeviceUser::TEMPLATE_PREVIEW,
617 /*out*/&requestTemplate2);
618 EXPECT_TRUE(res.isOk()) << res;
619 android::hardware::camera2::CaptureRequest request3;
620 android::hardware::camera2::CaptureRequest request4;
621 request3.mPhysicalCameraSettings.push_back({cameraId, requestTemplate});
622 request3.mSurfaceList.add(surface);
623 request3.mIsReprocess = false;
624 request4.mPhysicalCameraSettings.push_back({cameraId, requestTemplate2});
625 request4.mSurfaceList.add(surface);
626 request4.mIsReprocess = false;
627 std::vector<hardware::camera2::CaptureRequest> requestList;
628 requestList.push_back(request3);
629 requestList.push_back(request4);
630
631 callbacks->clearStatus();
632 hardware::camera2::utils::SubmitInfo info3;
633 res = device->submitRequestList(requestList, /*streaming*/false,
634 /*out*/&info3);
635 EXPECT_TRUE(res.isOk()) << res;
636 EXPECT_LE(0, info3.mRequestId);
637 EXPECT_TRUE(callbacks->waitForStatus(TestCameraDeviceCallbacks::SENT_RESULT));
638 EXPECT_TRUE(callbacks->waitForIdle());
639 EXPECT_LE(lastFrameNumberPrev, info3.mLastFrameNumber);
640 sleep(/*second*/1); // allow some time for errors to show up, if any
641 EXPECT_FALSE(callbacks->hadError());
642
643 // Can we unconfigure?
644 res = device->beginConfigure();
645 EXPECT_TRUE(res.isOk()) << res;
646 res = device->deleteStream(streamId);
647 EXPECT_TRUE(res.isOk()) << res;
648 res = device->endConfigure(/*isConstrainedHighSpeed*/ false, sessionParams,
649 ns2ms(systemTime()), &offlineStreamIds);
650 EXPECT_TRUE(res.isOk()) << res;
651
652 sleep(/*second*/1); // allow some time for errors to show up, if any
653 EXPECT_FALSE(callbacks->hadError());
654
655 closeDevice(p);
656 }
657 }
658
TEST_F(CameraClientBinderTest,CheckBinderCaptureRequest)659 TEST_F(CameraClientBinderTest, CheckBinderCaptureRequest) {
660 sp<CaptureRequest> requestOriginal, requestParceled;
661 sp<IGraphicBufferProducer> gbProducer;
662 sp<IGraphicBufferConsumer> gbConsumer;
663 BufferQueue::createBufferQueue(&gbProducer, &gbConsumer);
664 sp<Surface> surface(new Surface(gbProducer, /*controlledByApp*/false));
665 Vector<sp<Surface>> surfaceList;
666 surfaceList.push_back(surface);
667 std::string physicalDeviceId1 = "0";
668 std::string physicalDeviceId2 = "1";
669 CameraMetadata physicalDeviceSettings1, physicalDeviceSettings2;
670 uint8_t intent1 = ANDROID_CONTROL_CAPTURE_INTENT_PREVIEW;
671 uint8_t intent2 = ANDROID_CONTROL_CAPTURE_INTENT_VIDEO_RECORD;
672 EXPECT_EQ(OK, physicalDeviceSettings1.update(ANDROID_CONTROL_CAPTURE_INTENT, &intent1, 1));
673 EXPECT_EQ(OK, physicalDeviceSettings2.update(ANDROID_CONTROL_CAPTURE_INTENT, &intent2, 1));
674
675 requestParceled = new CaptureRequest();
676 Parcel p;
677 EXPECT_TRUE(requestParceled->readFromParcel(&p) != OK);
678 p.writeInt32(0);
679 p.setDataPosition(0);
680 EXPECT_TRUE(requestParceled->readFromParcel(&p) != OK);
681 p.freeData();
682 p.writeInt32(-1);
683 p.setDataPosition(0);
684 EXPECT_TRUE(requestParceled->readFromParcel(&p) != OK);
685 p.freeData();
686 p.writeInt32(1);
687 p.setDataPosition(0);
688 EXPECT_TRUE(requestParceled->readFromParcel(&p) != OK);
689
690 requestOriginal = new CaptureRequest();
691 requestOriginal->mPhysicalCameraSettings.push_back({physicalDeviceId1,
692 physicalDeviceSettings1});
693 requestOriginal->mPhysicalCameraSettings.push_back({physicalDeviceId2,
694 physicalDeviceSettings2});
695 requestOriginal->mSurfaceList.push_back(surface);
696 requestOriginal->mIsReprocess = false;
697 requestOriginal->mSurfaceConverted = false;
698
699 p.freeData();
700 EXPECT_TRUE(requestOriginal->writeToParcel(&p) == OK);
701 p.setDataPosition(0);
702 EXPECT_TRUE(requestParceled->readFromParcel(&p) == OK);
703 EXPECT_EQ(requestParceled->mIsReprocess, false);
704 EXPECT_FALSE(requestParceled->mSurfaceList.empty());
705 EXPECT_EQ(2u, requestParceled->mPhysicalCameraSettings.size());
706 auto it = requestParceled->mPhysicalCameraSettings.begin();
707 EXPECT_EQ(physicalDeviceId1, it->id);
708 EXPECT_TRUE(it->settings.exists(ANDROID_CONTROL_CAPTURE_INTENT));
709 auto entry = it->settings.find(ANDROID_CONTROL_CAPTURE_INTENT);
710 EXPECT_EQ(entry.data.u8[0], intent1);
711 it++;
712 EXPECT_EQ(physicalDeviceId2, it->id);
713 EXPECT_TRUE(it->settings.exists(ANDROID_CONTROL_CAPTURE_INTENT));
714 entry = it->settings.find(ANDROID_CONTROL_CAPTURE_INTENT);
715 EXPECT_EQ(entry.data.u8[0], intent2);
716 }
717