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