1 /*
2 * Copyright (C) 2016 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 "CameraProviderManagerTest"
19
20 #include "../common/CameraProviderManager.h"
21 #include <android/hidl/manager/1.0/IServiceManager.h>
22 #include <android/hidl/manager/1.0/IServiceNotification.h>
23 #include <android/hardware/camera/device/3.2/ICameraDeviceCallback.h>
24 #include <android/hardware/camera/device/3.2/ICameraDeviceSession.h>
25 #include <camera_metadata_hidden.h>
26 #include <hidl/HidlBinderSupport.h>
27 #include <gtest/gtest.h>
28 #include <utility>
29
30 using namespace android;
31 using namespace android::hardware::camera;
32 using android::hardware::camera::common::V1_0::Status;
33 using android::hardware::camera::common::V1_0::VendorTag;
34 using android::hardware::camera::common::V1_0::VendorTagSection;
35 using android::hardware::camera::common::V1_0::CameraMetadataType;
36 using android::hardware::camera::device::V3_2::ICameraDeviceCallback;
37 using android::hardware::camera::device::V3_2::ICameraDeviceSession;
38 using android::hardware::camera::provider::V2_5::DeviceState;
39
40 /**
41 * Basic test implementation of a camera ver. 3.2 device interface
42 */
43 struct TestDeviceInterface : public device::V3_2::ICameraDevice {
44 std::vector<hardware::hidl_string> mDeviceNames;
45 android::hardware::hidl_vec<uint8_t> mCharacteristics;
46
TestDeviceInterfaceTestDeviceInterface47 TestDeviceInterface(std::vector<hardware::hidl_string> deviceNames,
48 android::hardware::hidl_vec<uint8_t> chars) :
49 mDeviceNames(deviceNames), mCharacteristics(chars) {}
50
TestDeviceInterfaceTestDeviceInterface51 TestDeviceInterface(std::vector<hardware::hidl_string> deviceNames) :
52 mDeviceNames(deviceNames) {}
53
54 using getResourceCost_cb = std::function<void(
55 hardware::camera::common::V1_0::Status status,
56 const hardware::camera::common::V1_0::CameraResourceCost& resourceCost)>;
getResourceCostTestDeviceInterface57 virtual ::android::hardware::Return<void> getResourceCost(
58 getResourceCost_cb _hidl_cb) override {
59 hardware::camera::common::V1_0::CameraResourceCost resourceCost = {100,
60 mDeviceNames};
61 _hidl_cb(Status::OK, resourceCost);
62 return hardware::Void();
63 }
64
65 using getCameraCharacteristics_cb = std::function<void(
66 hardware::camera::common::V1_0::Status status,
67 const hardware::hidl_vec<uint8_t>& cameraCharacteristics)>;
getCameraCharacteristicsTestDeviceInterface68 hardware::Return<void> getCameraCharacteristics(
69 getCameraCharacteristics_cb _hidl_cb) override {
70 _hidl_cb(Status::OK, mCharacteristics);
71 return hardware::Void();
72 }
73
setTorchModeTestDeviceInterface74 hardware::Return<hardware::camera::common::V1_0::Status> setTorchMode(
75 ::android::hardware::camera::common::V1_0::TorchMode) override {
76 return Status::OK;
77 }
78
79 using open_cb = std::function<void(
80 ::android::hardware::camera::common::V1_0::Status status,
81 const ::android::sp<ICameraDeviceSession>& session)>;
openTestDeviceInterface82 hardware::Return<void> open(
83 const ::android::sp<ICameraDeviceCallback>&,
84 open_cb _hidl_cb) override {
85 sp<ICameraDeviceSession> deviceSession = nullptr;
86 _hidl_cb(Status::OK, deviceSession);
87 return hardware::Void();
88 }
89
dumpStateTestDeviceInterface90 hardware::Return<void> dumpState(
91 const ::android::hardware::hidl_handle&) override {
92 return hardware::Void();
93 }
94 };
95
96 /**
97 * Basic test implementation of a camera provider
98 */
99 struct TestICameraProvider : virtual public provider::V2_5::ICameraProvider {
100 sp<provider::V2_4::ICameraProviderCallback> mCallbacks;
101 std::vector<hardware::hidl_string> mDeviceNames;
102 sp<device::V3_2::ICameraDevice> mDeviceInterface;
103 hardware::hidl_vec<common::V1_0::VendorTagSection> mVendorTagSections;
104
105 // Whether to call a physical camera unavailable callback upon setCallback
106 bool mHasPhysicalCameraUnavailableCallback;
107 hardware::hidl_string mLogicalCameraId;
108 hardware::hidl_string mUnavailablePhysicalCameraId;
109
TestICameraProviderTestICameraProvider110 TestICameraProvider(const std::vector<hardware::hidl_string> &devices,
111 const hardware::hidl_vec<common::V1_0::VendorTagSection> &vendorSection) :
112 mDeviceNames(devices),
113 mDeviceInterface(new TestDeviceInterface(devices)),
114 mVendorTagSections (vendorSection),
115 mHasPhysicalCameraUnavailableCallback(false) {}
116
TestICameraProviderTestICameraProvider117 TestICameraProvider(const std::vector<hardware::hidl_string> &devices,
118 const hardware::hidl_vec<common::V1_0::VendorTagSection> &vendorSection,
119 android::hardware::hidl_vec<uint8_t> chars) :
120 mDeviceNames(devices),
121 mDeviceInterface(new TestDeviceInterface(devices, chars)),
122 mVendorTagSections (vendorSection),
123 mHasPhysicalCameraUnavailableCallback(false) {}
124
TestICameraProviderTestICameraProvider125 TestICameraProvider(const std::vector<hardware::hidl_string> &devices,
126 const hardware::hidl_vec<common::V1_0::VendorTagSection> &vendorSection,
127 android::hardware::hidl_vec<uint8_t> chars,
128 const hardware::hidl_string& logicalCameraId,
129 const hardware::hidl_string& unavailablePhysicalCameraId) :
130 mDeviceNames(devices),
131 mDeviceInterface(new TestDeviceInterface(devices, chars)),
132 mVendorTagSections (vendorSection),
133 mHasPhysicalCameraUnavailableCallback(true),
134 mLogicalCameraId(logicalCameraId),
135 mUnavailablePhysicalCameraId(unavailablePhysicalCameraId) {}
136
setCallbackTestICameraProvider137 virtual hardware::Return<Status> setCallback(
138 const sp<provider::V2_4::ICameraProviderCallback>& callbacks) override {
139 mCalledCounter[SET_CALLBACK]++;
140 mCallbacks = callbacks;
141 if (mHasPhysicalCameraUnavailableCallback) {
142 auto cast26 = provider::V2_6::ICameraProviderCallback::castFrom(callbacks);
143 if (!cast26.isOk()) {
144 ADD_FAILURE() << "Failed to cast ICameraProviderCallback to V2_6";
145 } else {
146 sp<provider::V2_6::ICameraProviderCallback> callback26 = cast26;
147 if (callback26 == nullptr) {
148 ADD_FAILURE() << "V2_6::ICameraProviderCallback is null after conversion";
149 } else {
150 callback26->physicalCameraDeviceStatusChange(mLogicalCameraId,
151 mUnavailablePhysicalCameraId,
152 android::hardware::camera::common::V1_0::CameraDeviceStatus::NOT_PRESENT);
153 }
154 }
155 }
156 return hardware::Return<Status>(Status::OK);
157 }
158
159 using getVendorTags_cb = std::function<void(Status status,
160 const hardware::hidl_vec<common::V1_0::VendorTagSection>& sections)>;
getVendorTagsTestICameraProvider161 hardware::Return<void> getVendorTags(getVendorTags_cb _hidl_cb) override {
162 mCalledCounter[GET_VENDOR_TAGS]++;
163 _hidl_cb(Status::OK, mVendorTagSections);
164 return hardware::Void();
165 }
166
167 using isSetTorchModeSupported_cb = std::function<void(
168 ::android::hardware::camera::common::V1_0::Status status,
169 bool support)>;
isSetTorchModeSupportedTestICameraProvider170 virtual ::hardware::Return<void> isSetTorchModeSupported(
171 isSetTorchModeSupported_cb _hidl_cb) override {
172 mCalledCounter[IS_SET_TORCH_MODE_SUPPORTED]++;
173 _hidl_cb(Status::OK, false);
174 return hardware::Void();
175 }
176
177 using getCameraIdList_cb = std::function<void(Status status,
178 const hardware::hidl_vec<hardware::hidl_string>& cameraDeviceNames)>;
getCameraIdListTestICameraProvider179 virtual hardware::Return<void> getCameraIdList(getCameraIdList_cb _hidl_cb) override {
180 mCalledCounter[GET_CAMERA_ID_LIST]++;
181 _hidl_cb(Status::OK, mDeviceNames);
182 return hardware::Void();
183 }
184
185 using getCameraDeviceInterface_V1_x_cb = std::function<void(Status status,
186 const sp<device::V1_0::ICameraDevice>& device)>;
getCameraDeviceInterface_V1_xTestICameraProvider187 virtual hardware::Return<void> getCameraDeviceInterface_V1_x(
188 const hardware::hidl_string& cameraDeviceName,
189 getCameraDeviceInterface_V1_x_cb _hidl_cb) override {
190 (void) cameraDeviceName;
191 _hidl_cb(Status::OK, nullptr); //TODO: impl. of ver. 1.0 device interface
192 // otherwise enumeration will fail.
193 return hardware::Void();
194 }
195
196 using getCameraDeviceInterface_V3_x_cb = std::function<void(Status status,
197 const sp<device::V3_2::ICameraDevice>& device)>;
getCameraDeviceInterface_V3_xTestICameraProvider198 virtual hardware::Return<void> getCameraDeviceInterface_V3_x(
199 const hardware::hidl_string&,
200 getCameraDeviceInterface_V3_x_cb _hidl_cb) override {
201 _hidl_cb(Status::OK, mDeviceInterface);
202 return hardware::Void();
203 }
204
notifyDeviceStateChangeTestICameraProvider205 virtual hardware::Return<void> notifyDeviceStateChange(
206 hardware::hidl_bitfield<DeviceState> newState) override {
207 mCalledCounter[NOTIFY_DEVICE_STATE]++;
208 mCurrentState = newState;
209 return hardware::Void();
210 }
211
linkToDeathTestICameraProvider212 virtual ::android::hardware::Return<bool> linkToDeath(
213 const ::android::sp<::android::hardware::hidl_death_recipient>& recipient,
214 uint64_t cookie) {
215 if (mInitialDeathRecipient.get() == nullptr) {
216 mInitialDeathRecipient =
217 std::make_unique<::android::hardware::hidl_binder_death_recipient>(recipient,
218 cookie, this);
219 }
220 return true;
221 }
222
signalInitialBinderDeathRecipientTestICameraProvider223 void signalInitialBinderDeathRecipient() {
224 if (mInitialDeathRecipient.get() != nullptr) {
225 mInitialDeathRecipient->binderDied(nullptr /*who*/);
226 }
227 }
228
229 std::unique_ptr<::android::hardware::hidl_binder_death_recipient> mInitialDeathRecipient;
230
231 enum MethodNames {
232 SET_CALLBACK,
233 GET_VENDOR_TAGS,
234 IS_SET_TORCH_MODE_SUPPORTED,
235 NOTIFY_DEVICE_STATE,
236 GET_CAMERA_ID_LIST,
237
238 METHOD_NAME_COUNT
239 };
240 int mCalledCounter[METHOD_NAME_COUNT] {0};
241
242 hardware::hidl_bitfield<DeviceState> mCurrentState = 0xFFFFFFFF; // Unlikely to be a real state
243 };
244
245 /**
246 * Simple test version of the interaction proxy, to use to inject onRegistered calls to the
247 * CameraProviderManager
248 */
249 struct TestInteractionProxy : public CameraProviderManager::HidlServiceInteractionProxy {
250 sp<hidl::manager::V1_0::IServiceNotification> mManagerNotificationInterface;
251 sp<TestICameraProvider> mTestCameraProvider;
252
TestInteractionProxyTestInteractionProxy253 TestInteractionProxy() {}
254
setProviderTestInteractionProxy255 void setProvider(sp<TestICameraProvider> provider) {
256 mTestCameraProvider = provider;
257 }
258
259 std::vector<std::string> mLastRequestedServiceNames;
260
~TestInteractionProxyTestInteractionProxy261 virtual ~TestInteractionProxy() {}
262
registerForNotificationsTestInteractionProxy263 virtual bool registerForNotifications(
264 const std::string &serviceName,
265 const sp<hidl::manager::V1_0::IServiceNotification> ¬ification) override {
266 (void) serviceName;
267 mManagerNotificationInterface = notification;
268 return true;
269 }
270
tryGetServiceTestInteractionProxy271 virtual sp<hardware::camera::provider::V2_4::ICameraProvider> tryGetService(
272 const std::string &serviceName) override {
273 // If no provider has been given, act like the HAL isn't available and return null.
274 if (mTestCameraProvider == nullptr) return nullptr;
275 return getService(serviceName);
276 }
277
getServiceTestInteractionProxy278 virtual sp<hardware::camera::provider::V2_4::ICameraProvider> getService(
279 const std::string &serviceName) override {
280 // If no provider has been given, fail; in reality, getService would
281 // block for HALs that don't start correctly, so we should never use
282 // getService when we don't have a valid HAL running
283 if (mTestCameraProvider == nullptr) {
284 ADD_FAILURE() << "getService called with no valid provider; would block indefinitely";
285 // Real getService would block, but that's bad in unit tests. So
286 // just record an error and return nullptr
287 return nullptr;
288 }
289 mLastRequestedServiceNames.push_back(serviceName);
290 return mTestCameraProvider;
291 }
292
listServicesTestInteractionProxy293 virtual hardware::hidl_vec<hardware::hidl_string> listServices() override {
294 // Always provide a list even if there's no actual provider yet, to
295 // simulate stuck HAL situations as well
296 hardware::hidl_vec<hardware::hidl_string> ret = {"test/0"};
297 return ret;
298 }
299
300 };
301
302 struct TestStatusListener : public CameraProviderManager::StatusListener {
303 int mPhysicalCameraStatusChangeCount = 0;
304
~TestStatusListenerTestStatusListener305 ~TestStatusListener() {}
306
onDeviceStatusChangedTestStatusListener307 void onDeviceStatusChanged(const String8 &,
308 CameraDeviceStatus) override {}
onDeviceStatusChangedTestStatusListener309 void onDeviceStatusChanged(const String8 &, const String8 &,
310 CameraDeviceStatus) override {
311 mPhysicalCameraStatusChangeCount++;
312 }
onTorchStatusChangedTestStatusListener313 void onTorchStatusChanged(const String8 &,
314 TorchModeStatus) override {}
onTorchStatusChangedTestStatusListener315 void onTorchStatusChanged(const String8 &,
316 TorchModeStatus, SystemCameraKind) override {}
onNewProviderRegisteredTestStatusListener317 void onNewProviderRegistered() override {}
318 };
319
TEST(CameraProviderManagerTest,InitializeDynamicDepthTest)320 TEST(CameraProviderManagerTest, InitializeDynamicDepthTest) {
321 std::vector<hardware::hidl_string> deviceNames;
322 deviceNames.push_back("device@3.2/test/0");
323 hardware::hidl_vec<common::V1_0::VendorTagSection> vendorSection;
324 status_t res;
325 sp<CameraProviderManager> providerManager = new CameraProviderManager();
326 sp<TestStatusListener> statusListener = new TestStatusListener();
327 TestInteractionProxy serviceProxy;
328
329 android::hardware::hidl_vec<uint8_t> chars;
330 CameraMetadata meta;
331 int32_t charKeys[] = { ANDROID_DEPTH_DEPTH_IS_EXCLUSIVE,
332 ANDROID_DEPTH_AVAILABLE_DEPTH_STREAM_CONFIGURATIONS };
333 meta.update(ANDROID_REQUEST_AVAILABLE_CHARACTERISTICS_KEYS, charKeys,
334 sizeof(charKeys) / sizeof(charKeys[0]));
335 uint8_t depthIsExclusive = ANDROID_DEPTH_DEPTH_IS_EXCLUSIVE_FALSE;
336 meta.update(ANDROID_DEPTH_DEPTH_IS_EXCLUSIVE, &depthIsExclusive, 1);
337 int32_t sizes[] = { HAL_PIXEL_FORMAT_BLOB,
338 640, 480, ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_OUTPUT };
339 meta.update(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS, sizes,
340 sizeof(sizes) / sizeof(sizes[0]));
341 sizes[0] = HAL_PIXEL_FORMAT_Y16;
342 meta.update(ANDROID_DEPTH_AVAILABLE_DEPTH_STREAM_CONFIGURATIONS, sizes,
343 sizeof(sizes) / sizeof(sizes[0]));
344 int64_t durations[] = { HAL_PIXEL_FORMAT_BLOB, 640, 480, 0 };
345 meta.update(ANDROID_SCALER_AVAILABLE_MIN_FRAME_DURATIONS, durations,
346 sizeof(durations) / sizeof(durations[0]));
347 meta.update(ANDROID_SCALER_AVAILABLE_STALL_DURATIONS, durations,
348 sizeof(durations) / sizeof(durations[0]));
349 durations[0]= HAL_PIXEL_FORMAT_Y16;
350 meta.update(ANDROID_DEPTH_AVAILABLE_DEPTH_MIN_FRAME_DURATIONS, durations,
351 sizeof(durations) / sizeof(durations[0]));
352 meta.update(ANDROID_DEPTH_AVAILABLE_DEPTH_STALL_DURATIONS, durations,
353 sizeof(durations) / sizeof(durations[0]));
354 camera_metadata_t* metaBuffer = const_cast<camera_metadata_t*>(meta.getAndLock());
355 chars.setToExternal(reinterpret_cast<uint8_t*>(metaBuffer),
356 get_camera_metadata_size(metaBuffer));
357
358 sp<TestICameraProvider> provider = new TestICameraProvider(deviceNames,
359 vendorSection, chars);
360 serviceProxy.setProvider(provider);
361
362 res = providerManager->initialize(statusListener, &serviceProxy);
363 ASSERT_EQ(res, OK) << "Unable to initialize provider manager";
364 }
365
TEST(CameraProviderManagerTest,InitializeTest)366 TEST(CameraProviderManagerTest, InitializeTest) {
367 std::vector<hardware::hidl_string> deviceNames;
368 deviceNames.push_back("device@3.2/test/0");
369 deviceNames.push_back("device@1.0/test/0");
370 deviceNames.push_back("device@3.2/test/1");
371 hardware::hidl_vec<common::V1_0::VendorTagSection> vendorSection;
372 status_t res;
373 sp<CameraProviderManager> providerManager = new CameraProviderManager();
374 sp<TestStatusListener> statusListener = new TestStatusListener();
375 TestInteractionProxy serviceProxy;
376 sp<TestICameraProvider> provider = new TestICameraProvider(deviceNames,
377 vendorSection);
378 serviceProxy.setProvider(provider);
379
380 int numProviders = static_cast<int>(serviceProxy.listServices().size());
381
382 res = providerManager->initialize(statusListener, &serviceProxy);
383 ASSERT_EQ(res, OK) << "Unable to initialize provider manager";
384 // Check that both "legacy" and "external" providers (really the same object) are called
385 // once for all the init methods
386 EXPECT_EQ(provider->mCalledCounter[TestICameraProvider::SET_CALLBACK], numProviders) <<
387 "Only one call to setCallback per provider expected during init";
388 EXPECT_EQ(provider->mCalledCounter[TestICameraProvider::GET_VENDOR_TAGS], numProviders) <<
389 "Only one call to getVendorTags per provider expected during init";
390 EXPECT_EQ(provider->mCalledCounter[TestICameraProvider::IS_SET_TORCH_MODE_SUPPORTED],
391 numProviders) <<
392 "Only one call to isSetTorchModeSupported per provider expected during init";
393 EXPECT_EQ(provider->mCalledCounter[TestICameraProvider::GET_CAMERA_ID_LIST], numProviders) <<
394 "Only one call to getCameraIdList per provider expected during init";
395 EXPECT_EQ(provider->mCalledCounter[TestICameraProvider::NOTIFY_DEVICE_STATE], numProviders) <<
396 "Only one call to notifyDeviceState per provider expected during init";
397
398 hardware::hidl_string testProviderFqInterfaceName =
399 "android.hardware.camera.provider@2.4::ICameraProvider";
400 hardware::hidl_string testProviderInstanceName = "test/0";
401 serviceProxy.mManagerNotificationInterface->onRegistration(
402 testProviderFqInterfaceName,
403 testProviderInstanceName, false);
404
405 ASSERT_EQ(serviceProxy.mLastRequestedServiceNames.back(), testProviderInstanceName) <<
406 "Incorrect instance requested from service manager";
407 }
408
TEST(CameraProviderManagerTest,MultipleVendorTagTest)409 TEST(CameraProviderManagerTest, MultipleVendorTagTest) {
410 hardware::hidl_string sectionName = "VendorTestSection";
411 hardware::hidl_string tagName = "VendorTestTag";
412 uint32_t tagId = VENDOR_SECTION << 16;
413 hardware::hidl_vec<common::V1_0::VendorTagSection> vendorSection;
414 CameraMetadataType tagType = CameraMetadataType::BYTE;
415 vendorSection.resize(1);
416 vendorSection[0].sectionName = sectionName;
417 vendorSection[0].tags.resize(1);
418 vendorSection[0].tags[0].tagId = tagId;
419 vendorSection[0].tags[0].tagName = tagName;
420 vendorSection[0].tags[0].tagType = tagType;
421 std::vector<hardware::hidl_string> deviceNames = {"device@3.2/test/0"};
422
423 sp<CameraProviderManager> providerManager = new CameraProviderManager();
424 sp<TestStatusListener> statusListener = new TestStatusListener();
425 TestInteractionProxy serviceProxy;
426
427 sp<TestICameraProvider> provider = new TestICameraProvider(deviceNames,
428 vendorSection);
429 serviceProxy.setProvider(provider);
430
431 auto res = providerManager->initialize(statusListener, &serviceProxy);
432 ASSERT_EQ(res, OK) << "Unable to initialize provider manager";
433
434 hardware::hidl_string testProviderInstanceName = "test/0";
435 hardware::hidl_string testProviderFqInterfaceName =
436 "android.hardware.camera.provider@2.4::ICameraProvider";
437 serviceProxy.mManagerNotificationInterface->onRegistration(
438 testProviderFqInterfaceName, testProviderInstanceName, false);
439 ASSERT_EQ(serviceProxy.mLastRequestedServiceNames.back(), testProviderInstanceName) <<
440 "Incorrect instance requested from service manager";
441
442 hardware::hidl_string sectionNameSecond = "SecondVendorTestSection";
443 hardware::hidl_string secondTagName = "SecondVendorTestTag";
444 CameraMetadataType secondTagType = CameraMetadataType::DOUBLE;
445 vendorSection[0].sectionName = sectionNameSecond;
446 vendorSection[0].tags[0].tagId = tagId;
447 vendorSection[0].tags[0].tagName = secondTagName;
448 vendorSection[0].tags[0].tagType = secondTagType;
449 deviceNames = {"device@3.2/test2/1"};
450
451 sp<TestICameraProvider> secondProvider = new TestICameraProvider(
452 deviceNames, vendorSection);
453 serviceProxy.setProvider(secondProvider);
454 hardware::hidl_string testProviderSecondInstanceName = "test2/0";
455 serviceProxy.mManagerNotificationInterface->onRegistration(
456 testProviderFqInterfaceName, testProviderSecondInstanceName, false);
457 ASSERT_EQ(serviceProxy.mLastRequestedServiceNames.back(),
458 testProviderSecondInstanceName) <<
459 "Incorrect instance requested from service manager";
460
461 ASSERT_EQ(NO_ERROR , providerManager->setUpVendorTags());
462 sp<VendorTagDescriptorCache> vendorCache =
463 VendorTagDescriptorCache::getGlobalVendorTagCache();
464 ASSERT_NE(nullptr, vendorCache.get());
465
466 metadata_vendor_id_t vendorId = std::hash<std::string> {} (
467 testProviderInstanceName.c_str());
468 metadata_vendor_id_t vendorIdSecond = std::hash<std::string> {} (
469 testProviderSecondInstanceName.c_str());
470
471 hardware::hidl_string resultTag = vendorCache->getTagName(tagId, vendorId);
472 ASSERT_EQ(resultTag, tagName);
473
474 resultTag = vendorCache->getTagName(tagId, vendorIdSecond);
475 ASSERT_EQ(resultTag, secondTagName);
476
477 // Check whether we can create two separate CameraMetadata instances
478 // using different tag vendor vendors.
479 camera_metadata *metaBuffer = allocate_camera_metadata(10, 20);
480 ASSERT_NE(nullptr, metaBuffer);
481 set_camera_metadata_vendor_id(metaBuffer, vendorId);
482 CameraMetadata metadata(metaBuffer);
483
484 uint8_t byteVal = 10;
485 ASSERT_TRUE(metadata.isEmpty());
486 ASSERT_EQ(OK, metadata.update(tagId, &byteVal, 1));
487 ASSERT_FALSE(metadata.isEmpty());
488 ASSERT_TRUE(metadata.exists(tagId));
489
490 metaBuffer = allocate_camera_metadata(10, 20);
491 ASSERT_NE(nullptr, metaBuffer);
492 set_camera_metadata_vendor_id(metaBuffer, vendorIdSecond);
493 CameraMetadata secondMetadata(metaBuffer);
494
495 ASSERT_TRUE(secondMetadata.isEmpty());
496 double doubleVal = 1.0f;
497 ASSERT_EQ(OK, secondMetadata.update(tagId, &doubleVal, 1));
498 ASSERT_FALSE(secondMetadata.isEmpty());
499 ASSERT_TRUE(secondMetadata.exists(tagId));
500
501 // Check whether CameraMetadata copying works as expected
502 CameraMetadata metadataCopy(metadata);
503 ASSERT_FALSE(metadataCopy.isEmpty());
504 ASSERT_TRUE(metadataCopy.exists(tagId));
505 ASSERT_EQ(OK, metadataCopy.update(tagId, &byteVal, 1));
506 ASSERT_TRUE(metadataCopy.exists(tagId));
507
508 // Check whether values are as expected
509 camera_metadata_entry_t entry = metadata.find(tagId);
510 ASSERT_EQ(1u, entry.count);
511 ASSERT_EQ(byteVal, entry.data.u8[0]);
512 entry = secondMetadata.find(tagId);
513 ASSERT_EQ(1u, entry.count);
514 ASSERT_EQ(doubleVal, entry.data.d[0]);
515
516 // Swap and erase
517 secondMetadata.swap(metadataCopy);
518 ASSERT_TRUE(metadataCopy.exists(tagId));
519 ASSERT_TRUE(secondMetadata.exists(tagId));
520 ASSERT_EQ(OK, secondMetadata.erase(tagId));
521 ASSERT_TRUE(secondMetadata.isEmpty());
522 doubleVal = 0.0f;
523 ASSERT_EQ(OK, metadataCopy.update(tagId, &doubleVal, 1));
524 entry = metadataCopy.find(tagId);
525 ASSERT_EQ(1u, entry.count);
526 ASSERT_EQ(doubleVal, entry.data.d[0]);
527
528 // Append
529 uint8_t sceneMode = ANDROID_CONTROL_SCENE_MODE_ACTION;
530 secondMetadata.update(ANDROID_CONTROL_SCENE_MODE, &sceneMode, 1);
531 // Append from two different vendor tag providers is not supported!
532 ASSERT_NE(OK, metadataCopy.append(secondMetadata));
533 ASSERT_EQ(OK, metadataCopy.erase(tagId));
534 metadataCopy.update(ANDROID_CONTROL_SCENE_MODE, &sceneMode, 1);
535 // However appending from same vendor tag provider should be fine
536 ASSERT_EQ(OK, metadata.append(secondMetadata));
537 // Append from a metadata without vendor tag provider should be supported
538 CameraMetadata regularMetadata(10, 20);
539 uint8_t controlMode = ANDROID_CONTROL_MODE_AUTO;
540 regularMetadata.update(ANDROID_CONTROL_MODE, &controlMode, 1);
541 ASSERT_EQ(OK, secondMetadata.append(regularMetadata));
542 ASSERT_EQ(2u, secondMetadata.entryCount());
543 ASSERT_EQ(2u, metadata.entryCount());
544
545 // Dump
546 metadata.dump(1, 2);
547 metadataCopy.dump(1, 2);
548 secondMetadata.dump(1, 2);
549 }
550
TEST(CameraProviderManagerTest,NotifyStateChangeTest)551 TEST(CameraProviderManagerTest, NotifyStateChangeTest) {
552 std::vector<hardware::hidl_string> deviceNames {
553 "device@3.2/test/0",
554 "device@1.0/test/0",
555 "device@3.2/test/1"};
556
557 hardware::hidl_vec<common::V1_0::VendorTagSection> vendorSection;
558 status_t res;
559 sp<CameraProviderManager> providerManager = new CameraProviderManager();
560 sp<TestStatusListener> statusListener = new TestStatusListener();
561 TestInteractionProxy serviceProxy;
562 sp<TestICameraProvider> provider = new TestICameraProvider(deviceNames,
563 vendorSection);
564 serviceProxy.setProvider(provider);
565
566 res = providerManager->initialize(statusListener, &serviceProxy);
567 ASSERT_EQ(res, OK) << "Unable to initialize provider manager";
568
569 ASSERT_EQ(provider->mCurrentState,
570 static_cast<hardware::hidl_bitfield<DeviceState>>(DeviceState::NORMAL))
571 << "Initial device state not set";
572
573 res = providerManager->notifyDeviceStateChange(
574 static_cast<hardware::hidl_bitfield<DeviceState>>(DeviceState::FOLDED));
575
576 ASSERT_EQ(res, OK) << "Unable to call notifyDeviceStateChange";
577 ASSERT_EQ(provider->mCurrentState,
578 static_cast<hardware::hidl_bitfield<DeviceState>>(DeviceState::FOLDED))
579 << "Unable to change device state";
580
581 }
582
583 // Test that CameraProviderManager doesn't get stuck when the camera HAL isn't really working
TEST(CameraProviderManagerTest,BadHalStartupTest)584 TEST(CameraProviderManagerTest, BadHalStartupTest) {
585
586 std::vector<hardware::hidl_string> deviceNames;
587 deviceNames.push_back("device@3.2/test/0");
588 deviceNames.push_back("device@1.0/test/0");
589 deviceNames.push_back("device@3.2/test/1");
590 hardware::hidl_vec<common::V1_0::VendorTagSection> vendorSection;
591 status_t res;
592
593 sp<CameraProviderManager> providerManager = new CameraProviderManager();
594 sp<TestStatusListener> statusListener = new TestStatusListener();
595 TestInteractionProxy serviceProxy;
596 sp<TestICameraProvider> provider = new TestICameraProvider(deviceNames,
597 vendorSection);
598
599 // Not setting up provider in the service proxy yet, to test cases where a
600 // HAL isn't starting right
601 res = providerManager->initialize(statusListener, &serviceProxy);
602 ASSERT_EQ(res, OK) << "Unable to initialize provider manager";
603
604 // Now set up provider and trigger a registration
605 serviceProxy.setProvider(provider);
606 int numProviders = static_cast<int>(serviceProxy.listServices().size());
607
608 hardware::hidl_string testProviderFqInterfaceName =
609 "android.hardware.camera.provider@2.4::ICameraProvider";
610 hardware::hidl_string testProviderInstanceName = "test/0";
611 serviceProxy.mManagerNotificationInterface->onRegistration(
612 testProviderFqInterfaceName,
613 testProviderInstanceName, false);
614
615 // Check that new provider is called once for all the init methods
616 EXPECT_EQ(provider->mCalledCounter[TestICameraProvider::SET_CALLBACK], numProviders) <<
617 "Only one call to setCallback per provider expected during register";
618 EXPECT_EQ(provider->mCalledCounter[TestICameraProvider::GET_VENDOR_TAGS], numProviders) <<
619 "Only one call to getVendorTags per provider expected during register";
620 EXPECT_EQ(provider->mCalledCounter[TestICameraProvider::IS_SET_TORCH_MODE_SUPPORTED],
621 numProviders) <<
622 "Only one call to isSetTorchModeSupported per provider expected during init";
623 EXPECT_EQ(provider->mCalledCounter[TestICameraProvider::GET_CAMERA_ID_LIST], numProviders) <<
624 "Only one call to getCameraIdList per provider expected during init";
625 EXPECT_EQ(provider->mCalledCounter[TestICameraProvider::NOTIFY_DEVICE_STATE], numProviders) <<
626 "Only one call to notifyDeviceState per provider expected during init";
627
628 ASSERT_EQ(serviceProxy.mLastRequestedServiceNames.back(), testProviderInstanceName) <<
629 "Incorrect instance requested from service manager";
630 }
631
632 // Test that CameraProviderManager can handle races between provider death notifications and
633 // provider registration callbacks
TEST(CameraProviderManagerTest,BinderDeathRegistrationRaceTest)634 TEST(CameraProviderManagerTest, BinderDeathRegistrationRaceTest) {
635
636 std::vector<hardware::hidl_string> deviceNames;
637 deviceNames.push_back("device@3.2/test/0");
638 deviceNames.push_back("device@3.2/test/1");
639 hardware::hidl_vec<common::V1_0::VendorTagSection> vendorSection;
640 status_t res;
641
642 sp<CameraProviderManager> providerManager = new CameraProviderManager();
643 sp<TestStatusListener> statusListener = new TestStatusListener();
644 TestInteractionProxy serviceProxy;
645 sp<TestICameraProvider> provider = new TestICameraProvider(deviceNames,
646 vendorSection);
647
648 // Not setting up provider in the service proxy yet, to test cases where a
649 // HAL isn't starting right
650 res = providerManager->initialize(statusListener, &serviceProxy);
651 ASSERT_EQ(res, OK) << "Unable to initialize provider manager";
652
653 // Now set up provider and trigger a registration
654 serviceProxy.setProvider(provider);
655
656 hardware::hidl_string testProviderFqInterfaceName =
657 "android.hardware.camera.provider@2.4::ICameraProvider";
658 hardware::hidl_string testProviderInstanceName = "test/0";
659 serviceProxy.mManagerNotificationInterface->onRegistration(
660 testProviderFqInterfaceName,
661 testProviderInstanceName, false);
662
663 // Simulate artificial delay of the registration callback which arrives before the
664 // death notification
665 serviceProxy.mManagerNotificationInterface->onRegistration(
666 testProviderFqInterfaceName,
667 testProviderInstanceName, false);
668
669 provider->signalInitialBinderDeathRecipient();
670
671 auto deviceCount = static_cast<unsigned> (providerManager->getCameraCount().second);
672 ASSERT_EQ(deviceCount, deviceNames.size()) <<
673 "Unexpected amount of camera devices";
674 }
675
676 // Test that CameraProviderManager does not trigger
677 // onDeviceStatusChanged(NOT_PRESENT) for physical camera before initialize()
678 // returns.
TEST(CameraProviderManagerTest,PhysicalCameraAvailabilityCallbackRaceTest)679 TEST(CameraProviderManagerTest, PhysicalCameraAvailabilityCallbackRaceTest) {
680 std::vector<hardware::hidl_string> deviceNames;
681 deviceNames.push_back("device@3.2/test/0");
682 hardware::hidl_vec<common::V1_0::VendorTagSection> vendorSection;
683
684 sp<CameraProviderManager> providerManager = new CameraProviderManager();
685 sp<TestStatusListener> statusListener = new TestStatusListener();
686 TestInteractionProxy serviceProxy;
687
688 android::hardware::hidl_vec<uint8_t> chars;
689 CameraMetadata meta;
690 int32_t charKeys[] = { ANDROID_REQUEST_AVAILABLE_CAPABILITIES };
691 meta.update(ANDROID_REQUEST_AVAILABLE_CHARACTERISTICS_KEYS, charKeys,
692 sizeof(charKeys) / sizeof(charKeys[0]));
693 uint8_t capabilities[] = { ANDROID_REQUEST_AVAILABLE_CAPABILITIES_LOGICAL_MULTI_CAMERA };
694 meta.update(ANDROID_REQUEST_AVAILABLE_CAPABILITIES, capabilities,
695 sizeof(capabilities)/sizeof(capabilities[0]));
696 uint8_t physicalCameraIds[] = { '2', '\0', '3', '\0' };
697 meta.update(ANDROID_LOGICAL_MULTI_CAMERA_PHYSICAL_IDS, physicalCameraIds,
698 sizeof(physicalCameraIds)/sizeof(physicalCameraIds[0]));
699 camera_metadata_t* metaBuffer = const_cast<camera_metadata_t*>(meta.getAndLock());
700 chars.setToExternal(reinterpret_cast<uint8_t*>(metaBuffer),
701 get_camera_metadata_size(metaBuffer));
702
703 sp<TestICameraProvider> provider = new TestICameraProvider(deviceNames,
704 vendorSection, chars, "device@3.2/test/0", "2");
705 serviceProxy.setProvider(provider);
706
707 status_t res = providerManager->initialize(statusListener, &serviceProxy);
708 ASSERT_EQ(res, OK) << "Unable to initialize provider manager";
709
710 ASSERT_EQ(statusListener->mPhysicalCameraStatusChangeCount, 0)
711 << "Unexpected physical camera status change callback upon provider init.";
712
713 std::unordered_map<std::string, std::set<std::string>> unavailablePhysicalIds;
714 auto cameraIds = providerManager->getCameraDeviceIds(&unavailablePhysicalIds);
715 ASSERT_TRUE(unavailablePhysicalIds.count("0") > 0 && unavailablePhysicalIds["0"].count("2") > 0)
716 << "Unavailable physical camera Ids not set properly.";
717 }
718