1 /*
2 * Copyright (C) 2019 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_TAG "VtsHalCameraServiceV2_0TargetTest"
18 //#define LOG_NDEBUG 0
19
20 #include <android/frameworks/cameraservice/device/2.0/ICameraDeviceUser.h>
21 #include <android/frameworks/cameraservice/device/2.1/ICameraDeviceUser.h>
22 #include <android/frameworks/cameraservice/service/2.0/ICameraService.h>
23 #include <android/frameworks/cameraservice/service/2.1/ICameraService.h>
24 #include <system/camera_metadata.h>
25 #include <system/graphics.h>
26
27 #include <fmq/MessageQueue.h>
28 #include <utils/Condition.h>
29 #include <utils/Mutex.h>
30 #include <utils/StrongPointer.h>
31
32 #include <gtest/gtest.h>
33 #include <hidl/GtestPrinter.h>
34 #include <hidl/ServiceManagement.h>
35 #include <stdint.h>
36 #include <unistd.h>
37
38 #include <stdio.h>
39 #include <algorithm>
40 #include <mutex>
41 #include <string>
42 #include <unordered_set>
43 #include <vector>
44
45 #include <media/NdkImageReader.h>
46
47 #include <android/log.h>
48
49 #include <CameraMetadata.h>
50
51 namespace android {
52
53 using android::Condition;
54 using android::Mutex;
55 using android::sp;
56 using android::frameworks::cameraservice::common::V2_0::Status;
57 using android::frameworks::cameraservice::device::V2_0::CaptureRequest;
58 using android::frameworks::cameraservice::device::V2_0::CaptureResultExtras;
59 using android::frameworks::cameraservice::device::V2_0::ErrorCode;
60 using android::frameworks::cameraservice::device::V2_0::FmqSizeOrMetadata;
61 using android::frameworks::cameraservice::device::V2_0::ICameraDeviceCallback;
62 using android::frameworks::cameraservice::device::V2_0::ICameraDeviceUser;
63 using android::frameworks::cameraservice::device::V2_0::OutputConfiguration;
64 using android::frameworks::cameraservice::device::V2_0::PhysicalCaptureResultInfo;
65 using android::frameworks::cameraservice::device::V2_0::StreamConfigurationMode;
66 using android::frameworks::cameraservice::device::V2_0::SubmitInfo;
67 using android::frameworks::cameraservice::device::V2_0::TemplateId;
68 using android::frameworks::cameraservice::service::V2_0::CameraDeviceStatus;
69 using android::frameworks::cameraservice::service::V2_0::CameraStatusAndId;
70 using android::frameworks::cameraservice::service::V2_0::ICameraService;
71 using android::frameworks::cameraservice::service::V2_0::ICameraServiceListener;
72 using android::frameworks::cameraservice::service::V2_1::PhysicalCameraStatusAndId;
73 using android::hardware::hidl_string;
74 using android::hardware::hidl_vec;
75 using android::hardware::Return;
76 using android::hardware::Void;
77 using android::hardware::camera::common::V1_0::helper::CameraMetadata;
78 using camera_metadata_enum_android_depth_available_depth_stream_configurations::
79 ANDROID_DEPTH_AVAILABLE_DEPTH_STREAM_CONFIGURATIONS_OUTPUT;
80 using RequestMetadataQueue = hardware::MessageQueue<uint8_t, hardware::kSynchronizedReadWrite>;
81
82 static constexpr int kCaptureRequestCount = 10;
83 static constexpr int kVGAImageWidth = 640;
84 static constexpr int kVGAImageHeight = 480;
85 static constexpr int kNumRequests = 4;
86
87 #define ASSERT_NOT_NULL(x) ASSERT_TRUE((x) != nullptr)
88
89 #define SETUP_TIMEOUT 2000000000 // ns
90 #define IDLE_TIMEOUT 2000000000 // ns
91
92 // Stub listener implementation
93 class CameraServiceListener : public ICameraServiceListener {
94 std::map<hidl_string, CameraDeviceStatus> mCameraStatuses;
95 mutable Mutex mLock;
96
97 public:
~CameraServiceListener()98 virtual ~CameraServiceListener(){};
99
onStatusChanged(const CameraStatusAndId & statusAndId)100 virtual Return<void> onStatusChanged(const CameraStatusAndId& statusAndId) override {
101 Mutex::Autolock l(mLock);
102 mCameraStatuses[statusAndId.cameraId] = statusAndId.deviceStatus;
103 return Void();
104 };
105 };
106
107 class CameraServiceListener2_1
108 : public android::frameworks::cameraservice::service::V2_1::ICameraServiceListener {
109 std::map<hidl_string, CameraDeviceStatus> mCameraStatuses;
110 std::map<hidl_string, std::set<hidl_string>> mUnavailablePhysicalCameras;
111 mutable Mutex mLock;
112
113 public:
~CameraServiceListener2_1()114 virtual ~CameraServiceListener2_1(){};
115
onStatusChanged(const CameraStatusAndId & statusAndId)116 virtual Return<void> onStatusChanged(const CameraStatusAndId& statusAndId) override {
117 Mutex::Autolock l(mLock);
118 mCameraStatuses[statusAndId.cameraId] = statusAndId.deviceStatus;
119 return Void();
120 };
121
onPhysicalCameraStatusChanged(const PhysicalCameraStatusAndId & statusAndId)122 virtual Return<void> onPhysicalCameraStatusChanged(
123 const PhysicalCameraStatusAndId& statusAndId) override {
124 Mutex::Autolock l(mLock);
125 ALOGI("%s: Physical camera %s : %s status changed to %d", __FUNCTION__,
126 statusAndId.cameraId.c_str(), statusAndId.physicalCameraId.c_str(),
127 statusAndId.deviceStatus);
128
129 EXPECT_NE(mCameraStatuses.find(statusAndId.cameraId), mCameraStatuses.end());
130 EXPECT_EQ(mCameraStatuses[statusAndId.cameraId], CameraDeviceStatus::STATUS_PRESENT);
131
132 if (statusAndId.deviceStatus == CameraDeviceStatus::STATUS_NOT_PRESENT) {
133 auto res = mUnavailablePhysicalCameras[statusAndId.cameraId].emplace(
134 statusAndId.physicalCameraId);
135 EXPECT_TRUE(res.second);
136 } else {
137 auto res = mUnavailablePhysicalCameras[statusAndId.cameraId].erase(
138 statusAndId.physicalCameraId);
139 EXPECT_EQ(res, 1);
140 }
141 return Void();
142 };
143
initializeStatuses(const hidl_vec<android::frameworks::cameraservice::service::V2_1::CameraStatusAndId> & statuses)144 void initializeStatuses(
145 const hidl_vec<android::frameworks::cameraservice::service::V2_1::CameraStatusAndId>&
146 statuses) {
147 Mutex::Autolock l(mLock);
148
149 for (auto& status : statuses) {
150 mCameraStatuses[status.v2_0.cameraId] = status.v2_0.deviceStatus;
151 for (auto& physicalId : status.unavailPhysicalCameraIds) {
152 mUnavailablePhysicalCameras[status.v2_0.cameraId].emplace(physicalId);
153 }
154 }
155 }
156 };
157
158 // ICameraDeviceCallback implementation
159 class CameraDeviceCallbacks : public ICameraDeviceCallback {
160 public:
161 enum Status {
162 IDLE,
163 ERROR,
164 PREPARED,
165 RUNNING,
166 RESULT_RECEIVED,
167 UNINITIALIZED,
168 REPEATING_REQUEST_ERROR,
169 };
170
171 protected:
172 bool mError = false;
173 Status mLastStatus = UNINITIALIZED;
174 mutable std::vector<Status> mStatusesHit;
175 mutable Mutex mLock;
176 mutable Condition mStatusCondition;
177
178 public:
CameraDeviceCallbacks()179 CameraDeviceCallbacks() {}
180
~CameraDeviceCallbacks()181 virtual ~CameraDeviceCallbacks() {}
182
onDeviceError(ErrorCode errorCode,const CaptureResultExtras & resultExtras)183 virtual Return<void> onDeviceError(ErrorCode errorCode,
184 const CaptureResultExtras& resultExtras) override {
185 (void)resultExtras;
186 ALOGE("%s: onDeviceError occurred with: %d", __FUNCTION__, static_cast<int>(errorCode));
187 Mutex::Autolock l(mLock);
188 mError = true;
189 mLastStatus = ERROR;
190 mStatusesHit.push_back(mLastStatus);
191 mStatusCondition.broadcast();
192 return Void();
193 }
194
onDeviceIdle()195 virtual Return<void> onDeviceIdle() override {
196 Mutex::Autolock l(mLock);
197 mLastStatus = IDLE;
198 mStatusesHit.push_back(mLastStatus);
199 mStatusCondition.broadcast();
200 return Void();
201 }
202
onCaptureStarted(const CaptureResultExtras & resultExtras,uint64_t timestamp)203 virtual Return<void> onCaptureStarted(const CaptureResultExtras& resultExtras,
204 uint64_t timestamp) override {
205 (void)resultExtras;
206 (void)timestamp;
207 Mutex::Autolock l(mLock);
208 mLastStatus = RUNNING;
209 mStatusesHit.push_back(mLastStatus);
210 mStatusCondition.broadcast();
211 return Void();
212 }
213
onResultReceived(const FmqSizeOrMetadata & sizeOrMetadata,const CaptureResultExtras & resultExtras,const hidl_vec<PhysicalCaptureResultInfo> & physicalResultInfos)214 virtual Return<void> onResultReceived(
215 const FmqSizeOrMetadata& sizeOrMetadata, const CaptureResultExtras& resultExtras,
216 const hidl_vec<PhysicalCaptureResultInfo>& physicalResultInfos) override {
217 (void)sizeOrMetadata;
218 (void)resultExtras;
219 (void)physicalResultInfos;
220 Mutex::Autolock l(mLock);
221 mLastStatus = RESULT_RECEIVED;
222 mStatusesHit.push_back(mLastStatus);
223 mStatusCondition.broadcast();
224 return Void();
225 }
226
onRepeatingRequestError(uint64_t lastFrameNumber,int32_t stoppedSequenceId)227 virtual Return<void> onRepeatingRequestError(uint64_t lastFrameNumber,
228 int32_t stoppedSequenceId) override {
229 (void)lastFrameNumber;
230 (void)stoppedSequenceId;
231 Mutex::Autolock l(mLock);
232 mLastStatus = REPEATING_REQUEST_ERROR;
233 mStatusesHit.push_back(mLastStatus);
234 mStatusCondition.broadcast();
235 return Void();
236 }
237
238 // Test helper functions:
239
hadError() const240 bool hadError() const {
241 Mutex::Autolock l(mLock);
242 return mError;
243 }
waitForStatus(Status status) const244 bool waitForStatus(Status status) const {
245 Mutex::Autolock l(mLock);
246 if (mLastStatus == status) {
247 return true;
248 }
249
250 while (std::find(mStatusesHit.begin(), mStatusesHit.end(), status) == mStatusesHit.end()) {
251 if (mStatusCondition.waitRelative(mLock, IDLE_TIMEOUT) != android::OK) {
252 mStatusesHit.clear();
253 return false;
254 }
255 }
256 mStatusesHit.clear();
257
258 return true;
259 }
260
clearStatus() const261 void clearStatus() const {
262 Mutex::Autolock l(mLock);
263 mStatusesHit.clear();
264 }
265
waitForIdle() const266 bool waitForIdle() const { return waitForStatus(IDLE); }
267 };
268
convertFromHidlCloned(const hidl_vec<uint8_t> & metadata,CameraMetadata * rawMetadata)269 static bool convertFromHidlCloned(const hidl_vec<uint8_t>& metadata, CameraMetadata* rawMetadata) {
270 const camera_metadata* buffer = (camera_metadata_t*)(metadata.data());
271 size_t expectedSize = metadata.size();
272 int ret = validate_camera_metadata_structure(buffer, &expectedSize);
273 if (ret == OK || ret == CAMERA_METADATA_VALIDATION_SHIFTED) {
274 *rawMetadata = buffer;
275 } else {
276 ALOGE("%s: Malformed camera metadata received from caller", __FUNCTION__);
277 return false;
278 }
279 return true;
280 }
281
282 struct StreamConfiguration {
283 int32_t width = -1;
284 int32_t height = -1;
285 };
286
287 class VtsHalCameraServiceV2_0TargetTest : public ::testing::TestWithParam<std::string> {
288 public:
SetUp()289 void SetUp() override {
290 cs = ICameraService::getService(GetParam());
291
292 auto castResult =
293 android::frameworks::cameraservice::service::V2_1::ICameraService::castFrom(cs);
294 if (castResult.isOk()) {
295 cs2_1 = castResult;
296 }
297 }
298
TearDown()299 void TearDown() override {}
300 // creates an outputConfiguration with no deferred streams
createOutputConfiguration(const std::vector<native_handle_t * > & nhs)301 OutputConfiguration createOutputConfiguration(const std::vector<native_handle_t*>& nhs) {
302 OutputConfiguration output;
303 output.rotation = OutputConfiguration::Rotation::R0;
304 output.windowGroupId = -1;
305 output.windowHandles.resize(nhs.size());
306 output.width = 0;
307 output.height = 0;
308 output.isDeferred = false;
309 for (size_t i = 0; i < nhs.size(); i++) {
310 output.windowHandles[i] = nhs[i];
311 }
312 return output;
313 }
314
initializeCaptureRequestPartial(CaptureRequest * captureRequest,int32_t streamId,const hidl_string & cameraId,size_t settingsSize)315 void initializeCaptureRequestPartial(CaptureRequest* captureRequest, int32_t streamId,
316 const hidl_string& cameraId, size_t settingsSize) {
317 captureRequest->physicalCameraSettings.resize(1);
318 captureRequest->physicalCameraSettings[0].id = cameraId;
319 captureRequest->streamAndWindowIds.resize(1);
320 captureRequest->streamAndWindowIds[0].streamId = streamId;
321 captureRequest->streamAndWindowIds[0].windowId = 0;
322 // Write the settings metadata into the fmq.
323 captureRequest->physicalCameraSettings[0].settings.fmqMetadataSize(settingsSize);
324 }
325
doesCapabilityExist(const CameraMetadata & characteristics,int capability)326 bool doesCapabilityExist(const CameraMetadata& characteristics, int capability) {
327 camera_metadata_ro_entry rawEntry =
328 characteristics.find(ANDROID_REQUEST_AVAILABLE_CAPABILITIES);
329 EXPECT_TRUE(rawEntry.count > 0);
330 for (size_t i = 0; i < rawEntry.count; i++) {
331 if (rawEntry.data.u8[i] == capability) {
332 return true;
333 }
334 }
335 return false;
336 }
337
isSecureOnlyDevice(const CameraMetadata & characteristics)338 bool isSecureOnlyDevice(const CameraMetadata& characteristics) {
339 camera_metadata_ro_entry rawEntry =
340 characteristics.find(ANDROID_REQUEST_AVAILABLE_CAPABILITIES);
341 EXPECT_TRUE(rawEntry.count > 0);
342 if (rawEntry.count == 1 &&
343 rawEntry.data.u8[0] == ANDROID_REQUEST_AVAILABLE_CAPABILITIES_SECURE_IMAGE_DATA) {
344 return true;
345 }
346 return false;
347 }
348
349 // Return the first advertised available stream sizes for the given format
350 // and use-case.
getStreamConfiguration(const CameraMetadata & characteristics,uint32_t tag,int32_t chosenUse,int32_t chosenFormat)351 StreamConfiguration getStreamConfiguration(const CameraMetadata& characteristics, uint32_t tag,
352 int32_t chosenUse, int32_t chosenFormat) {
353 camera_metadata_ro_entry rawEntry = characteristics.find(tag);
354 StreamConfiguration streamConfig;
355 const size_t STREAM_FORMAT_OFFSET = 0;
356 const size_t STREAM_WIDTH_OFFSET = 1;
357 const size_t STREAM_HEIGHT_OFFSET = 2;
358 const size_t STREAM_INOUT_OFFSET = 3;
359 const size_t STREAM_CONFIG_SIZE = 4;
360 if (rawEntry.count < STREAM_CONFIG_SIZE) {
361 return streamConfig;
362 }
363 EXPECT_TRUE((rawEntry.count % STREAM_CONFIG_SIZE) == 0);
364 for (size_t i = 0; i < rawEntry.count; i += STREAM_CONFIG_SIZE) {
365 int32_t format = rawEntry.data.i32[i + STREAM_FORMAT_OFFSET];
366 int32_t use = rawEntry.data.i32[i + STREAM_INOUT_OFFSET];
367 if (format == chosenFormat && use == chosenUse) {
368 streamConfig.width = rawEntry.data.i32[i + STREAM_WIDTH_OFFSET];
369 streamConfig.height = rawEntry.data.i32[i + STREAM_HEIGHT_OFFSET];
370 return streamConfig;
371 }
372 }
373 return streamConfig;
374 }
375
376 sp<ICameraService> cs = nullptr;
377 sp<android::frameworks::cameraservice::service::V2_1::ICameraService> cs2_1 = nullptr;
378 };
379
380 // Basic HIDL calls for ICameraService
TEST_P(VtsHalCameraServiceV2_0TargetTest,BasicCameraLifeCycleTest)381 TEST_P(VtsHalCameraServiceV2_0TargetTest, BasicCameraLifeCycleTest) {
382 sp<CameraServiceListener> listener(new CameraServiceListener());
383 hidl_vec<CameraStatusAndId> cameraStatuses{};
384 Status status = Status::NO_ERROR;
385 auto remoteRet =
386 cs->addListener(listener, [&status, &cameraStatuses](Status s, auto& retStatuses) {
387 status = s;
388 cameraStatuses = retStatuses;
389 });
390 EXPECT_TRUE(remoteRet.isOk() && status == Status::NO_ERROR);
391 for (const auto& it : cameraStatuses) {
392 CameraMetadata rawMetadata;
393 if (it.deviceStatus != CameraDeviceStatus::STATUS_PRESENT) {
394 continue;
395 }
396 remoteRet = cs->getCameraCharacteristics(
397 it.cameraId, [&status, &rawMetadata](auto s, const hidl_vec<uint8_t>& metadata) {
398 status = s;
399 bool cStatus = convertFromHidlCloned(metadata, &rawMetadata);
400 EXPECT_TRUE(cStatus);
401 });
402 EXPECT_TRUE(remoteRet.isOk() && status == Status::NO_ERROR);
403 EXPECT_FALSE(rawMetadata.isEmpty());
404 sp<CameraDeviceCallbacks> callbacks(new CameraDeviceCallbacks());
405 sp<ICameraDeviceUser> deviceRemote = nullptr;
406 remoteRet = cs->connectDevice(callbacks, it.cameraId,
407 [&status, &deviceRemote](auto s, auto& device) {
408 status = s;
409 deviceRemote = device;
410 });
411 EXPECT_TRUE(remoteRet.isOk() && status == Status::NO_ERROR);
412 EXPECT_TRUE(deviceRemote != nullptr);
413
414 std::shared_ptr<RequestMetadataQueue> requestMQ = nullptr;
415 remoteRet = deviceRemote->getCaptureRequestMetadataQueue([&requestMQ](const auto& mqD) {
416 requestMQ = std::make_shared<RequestMetadataQueue>(mqD);
417 EXPECT_TRUE(requestMQ->isValid() && (requestMQ->availableToWrite() >= 0));
418 });
419 EXPECT_TRUE(remoteRet.isOk());
420 AImageReader* reader = nullptr;
421 bool isDepthOnlyDevice =
422 !doesCapabilityExist(rawMetadata,
423 ANDROID_REQUEST_AVAILABLE_CAPABILITIES_BACKWARD_COMPATIBLE) &&
424 doesCapabilityExist(rawMetadata, ANDROID_REQUEST_AVAILABLE_CAPABILITIES_DEPTH_OUTPUT);
425 int chosenImageFormat = AIMAGE_FORMAT_YUV_420_888;
426 int chosenImageWidth = kVGAImageWidth;
427 int chosenImageHeight = kVGAImageHeight;
428 bool isSecureOnlyCamera = isSecureOnlyDevice(rawMetadata);
429 status_t mStatus = OK;
430 if (isSecureOnlyCamera) {
431 StreamConfiguration secureStreamConfig =
432 getStreamConfiguration(rawMetadata, ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS,
433 ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_OUTPUT,
434 HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED);
435 EXPECT_TRUE(secureStreamConfig.width != -1);
436 EXPECT_TRUE(secureStreamConfig.height != -1);
437 chosenImageFormat = AIMAGE_FORMAT_PRIVATE;
438 chosenImageWidth = secureStreamConfig.width;
439 chosenImageHeight = secureStreamConfig.height;
440 mStatus = AImageReader_newWithUsage(
441 chosenImageWidth, chosenImageHeight, chosenImageFormat,
442 AHARDWAREBUFFER_USAGE_PROTECTED_CONTENT, kCaptureRequestCount, &reader);
443
444 } else {
445 if (isDepthOnlyDevice) {
446 StreamConfiguration depthStreamConfig = getStreamConfiguration(
447 rawMetadata, ANDROID_DEPTH_AVAILABLE_DEPTH_STREAM_CONFIGURATIONS,
448 ANDROID_DEPTH_AVAILABLE_DEPTH_STREAM_CONFIGURATIONS_OUTPUT,
449 HAL_PIXEL_FORMAT_Y16);
450 EXPECT_TRUE(depthStreamConfig.width != -1);
451 EXPECT_TRUE(depthStreamConfig.height != -1);
452 chosenImageFormat = AIMAGE_FORMAT_DEPTH16;
453 chosenImageWidth = depthStreamConfig.width;
454 chosenImageHeight = depthStreamConfig.height;
455 }
456 mStatus = AImageReader_new(chosenImageWidth, chosenImageHeight, chosenImageFormat,
457 kCaptureRequestCount, &reader);
458 }
459
460 EXPECT_EQ(mStatus, AMEDIA_OK);
461 native_handle_t* wh = nullptr;
462 mStatus = AImageReader_getWindowNativeHandle(reader, &wh);
463 EXPECT_TRUE(mStatus == AMEDIA_OK && wh != nullptr);
464 OutputConfiguration output = createOutputConfiguration({wh});
465 Return<Status> ret = deviceRemote->beginConfigure();
466 EXPECT_TRUE(ret.isOk() && ret == Status::NO_ERROR);
467 int32_t streamId = -1;
468 remoteRet = deviceRemote->createStream(output, [&status, &streamId](Status s, auto sId) {
469 status = s;
470 streamId = sId;
471 });
472 EXPECT_TRUE(remoteRet.isOk() && status == Status::NO_ERROR);
473 EXPECT_TRUE(streamId >= 0);
474 hidl_vec<uint8_t> hidlParams;
475 ret = deviceRemote->endConfigure(StreamConfigurationMode::NORMAL_MODE, hidlParams);
476 EXPECT_TRUE(ret.isOk() && ret == Status::NO_ERROR);
477 hidl_vec<uint8_t> settingsMetadata;
478 remoteRet = deviceRemote->createDefaultRequest(
479 TemplateId::PREVIEW, [&status, &settingsMetadata](auto s, const hidl_vec<uint8_t> m) {
480 status = s;
481 settingsMetadata = m;
482 });
483 EXPECT_TRUE(remoteRet.isOk() && status == Status::NO_ERROR);
484 EXPECT_GE(settingsMetadata.size(), 0);
485 hidl_vec<CaptureRequest> captureRequests;
486 captureRequests.resize(kNumRequests);
487 for (int i = 0; i < kNumRequests; i++) {
488 CaptureRequest& captureRequest = captureRequests[i];
489 initializeCaptureRequestPartial(&captureRequest, streamId, it.cameraId,
490 settingsMetadata.size());
491 // Write the settings metadata into the fmq.
492 bool written = requestMQ->write(settingsMetadata.data(), settingsMetadata.size());
493 EXPECT_TRUE(written);
494 }
495 SubmitInfo info;
496
497 // Test a single capture
498 remoteRet = deviceRemote->submitRequestList(captureRequests, false,
499 [&status, &info](auto s, auto& submitInfo) {
500 status = s;
501 info = submitInfo;
502 });
503 EXPECT_TRUE(remoteRet.isOk() && status == Status::NO_ERROR);
504 EXPECT_GE(info.requestId, 0);
505 EXPECT_TRUE(callbacks->waitForStatus(CameraDeviceCallbacks::Status::RESULT_RECEIVED));
506 EXPECT_TRUE(callbacks->waitForIdle());
507
508 // Test repeating requests
509 CaptureRequest captureRequest;
510
511 initializeCaptureRequestPartial(&captureRequest, streamId, it.cameraId,
512 settingsMetadata.size());
513
514 bool written = requestMQ->write(settingsMetadata.data(), settingsMetadata.size());
515 EXPECT_TRUE(written);
516
517 remoteRet = deviceRemote->submitRequestList({captureRequest}, true,
518 [&status, &info](auto s, auto& submitInfo) {
519 status = s;
520 info = submitInfo;
521 });
522 EXPECT_TRUE(callbacks->waitForStatus(CameraDeviceCallbacks::Status::RESULT_RECEIVED));
523 int64_t lastFrameNumber = -1;
524 remoteRet =
525 deviceRemote->cancelRepeatingRequest([&status, &lastFrameNumber](auto s, int64_t lf) {
526 status = s;
527 lastFrameNumber = lf;
528 });
529 EXPECT_TRUE(remoteRet.isOk() && status == Status::NO_ERROR);
530 EXPECT_GE(lastFrameNumber, 0);
531
532 // Test waitUntilIdle()
533 auto statusRet = deviceRemote->waitUntilIdle();
534 EXPECT_TRUE(statusRet.isOk() && statusRet == Status::NO_ERROR);
535
536 // Test deleteStream()
537 statusRet = deviceRemote->deleteStream(streamId);
538 EXPECT_TRUE(statusRet.isOk() && statusRet == Status::NO_ERROR);
539
540 /**
541 * For camera device V2.1, test newly added functions.
542 * TODO: Refactor the device 2.1 test into a separate test for service 2.2.
543 */
544 auto castResult =
545 android::frameworks::cameraservice::device::V2_1::ICameraDeviceUser::castFrom(
546 deviceRemote);
547 sp<android::frameworks::cameraservice::device::V2_1::ICameraDeviceUser> deviceRemote2_1;
548 if (castResult.isOk()) {
549 deviceRemote2_1 = castResult;
550 }
551 if (deviceRemote2_1 != nullptr) {
552 // Reconfigure a capture session using v2.1 version of the device
553 ret = deviceRemote2_1->beginConfigure();
554 EXPECT_TRUE(ret.isOk() && ret == Status::NO_ERROR);
555 remoteRet =
556 deviceRemote2_1->createStream(output, [&status, &streamId](Status s, auto sId) {
557 status = s;
558 streamId = sId;
559 });
560 EXPECT_TRUE(remoteRet.isOk() && status == Status::NO_ERROR);
561 EXPECT_TRUE(streamId >= 0);
562 ret = deviceRemote2_1->endConfigure_2_1(StreamConfigurationMode::NORMAL_MODE,
563 hidlParams, systemTime());
564 EXPECT_TRUE(ret.isOk() && ret == Status::NO_ERROR);
565 }
566
567 remoteRet = deviceRemote->disconnect();
568 EXPECT_TRUE(remoteRet.isOk());
569 }
570 Return<Status> ret = cs->removeListener(listener);
571 EXPECT_TRUE(ret.isOk() && ret == Status::NO_ERROR);
572 }
573
TEST_P(VtsHalCameraServiceV2_0TargetTest,CameraServiceListener2_1Test)574 TEST_P(VtsHalCameraServiceV2_0TargetTest, CameraServiceListener2_1Test) {
575 sp<CameraServiceListener2_1> listener2_1(new CameraServiceListener2_1());
576 hidl_vec<android::frameworks::cameraservice::service::V2_1::CameraStatusAndId>
577 cameraStatuses2_1{};
578 Status status = Status::NO_ERROR;
579
580 if (cs2_1 == nullptr) return;
581
582 auto remoteRet = cs2_1->addListener_2_1(
583 listener2_1, [&status, &cameraStatuses2_1](Status s, auto& retStatuses) {
584 status = s;
585 cameraStatuses2_1 = retStatuses;
586 });
587 EXPECT_TRUE(remoteRet.isOk() && status == Status::NO_ERROR);
588 listener2_1->initializeStatuses(cameraStatuses2_1);
589
590 for (const auto& it : cameraStatuses2_1) {
591 CameraMetadata rawMetadata;
592 remoteRet = cs2_1->getCameraCharacteristics(
593 it.v2_0.cameraId, [&status, &rawMetadata](auto s, const hidl_vec<uint8_t>& metadata) {
594 status = s;
595 bool cStatus = convertFromHidlCloned(metadata, &rawMetadata);
596 EXPECT_TRUE(cStatus);
597 });
598 EXPECT_TRUE(remoteRet.isOk() && status == Status::NO_ERROR);
599 EXPECT_FALSE(rawMetadata.isEmpty());
600 bool isLogicalCamera = doesCapabilityExist(
601 rawMetadata, ANDROID_REQUEST_AVAILABLE_CAPABILITIES_LOGICAL_MULTI_CAMERA);
602 if (!isLogicalCamera) {
603 EXPECT_TRUE(it.unavailPhysicalCameraIds.size() == 0);
604 continue;
605 }
606 camera_metadata_entry entry = rawMetadata.find(ANDROID_LOGICAL_MULTI_CAMERA_PHYSICAL_IDS);
607 EXPECT_GT(entry.count, 0);
608
609 std::unordered_set<std::string> validPhysicalIds;
610 const uint8_t* ids = entry.data.u8;
611 size_t start = 0;
612 for (size_t i = 0; i < entry.count; i++) {
613 if (ids[i] == '\0') {
614 if (start != i) {
615 std::string currentId(reinterpret_cast<const char*>(ids + start));
616 validPhysicalIds.emplace(currentId);
617 }
618 start = i + 1;
619 }
620 }
621
622 std::unordered_set<std::string> unavailablePhysicalIds(it.unavailPhysicalCameraIds.begin(),
623 it.unavailPhysicalCameraIds.end());
624 EXPECT_EQ(unavailablePhysicalIds.size(), it.unavailPhysicalCameraIds.size());
625 for (auto& unavailablePhysicalId : unavailablePhysicalIds) {
626 EXPECT_NE(validPhysicalIds.find(unavailablePhysicalId), validPhysicalIds.end());
627 }
628 }
629
630 auto remoteStatus = cs2_1->removeListener(listener2_1);
631 EXPECT_TRUE(remoteStatus.isOk() && remoteStatus == Status::NO_ERROR);
632 }
633
634 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(VtsHalCameraServiceV2_0TargetTest);
635 INSTANTIATE_TEST_SUITE_P(
636 PerInstance, VtsHalCameraServiceV2_0TargetTest,
637 testing::ValuesIn(android::hardware::getAllHalInstanceNames(ICameraService::descriptor)),
638 android::hardware::PrintInstanceNameToString);
639 } // namespace android
640