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