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 [[maybe_unused]] const hardware::hidl_string& cameraDeviceName,
189 getCameraDeviceInterface_V1_x_cb _hidl_cb) override {
190 _hidl_cb(Status::OK, nullptr); //TODO: impl. of ver. 1.0 device interface
191 // otherwise enumeration will fail.
192 return hardware::Void();
193 }
194
195 using getCameraDeviceInterface_V3_x_cb = std::function<void(Status status,
196 const sp<device::V3_2::ICameraDevice>& device)>;
getCameraDeviceInterface_V3_xTestICameraProvider197 virtual hardware::Return<void> getCameraDeviceInterface_V3_x(
198 const hardware::hidl_string&,
199 getCameraDeviceInterface_V3_x_cb _hidl_cb) override {
200 _hidl_cb(Status::OK, mDeviceInterface);
201 return hardware::Void();
202 }
203
notifyDeviceStateChangeTestICameraProvider204 virtual hardware::Return<void> notifyDeviceStateChange(
205 hardware::hidl_bitfield<DeviceState> newState) override {
206 mCalledCounter[NOTIFY_DEVICE_STATE]++;
207 mCurrentState = newState;
208 return hardware::Void();
209 }
210
linkToDeathTestICameraProvider211 virtual ::android::hardware::Return<bool> linkToDeath(
212 const ::android::sp<::android::hardware::hidl_death_recipient>& recipient,
213 uint64_t cookie) {
214 if (mInitialDeathRecipient.get() == nullptr) {
215 mInitialDeathRecipient =
216 std::make_unique<::android::hardware::hidl_binder_death_recipient>(recipient,
217 cookie, this);
218 }
219 return true;
220 }
221
signalInitialBinderDeathRecipientTestICameraProvider222 void signalInitialBinderDeathRecipient() {
223 if (mInitialDeathRecipient.get() != nullptr) {
224 mInitialDeathRecipient->binderDied(nullptr /*who*/);
225 }
226 }
227
228 std::unique_ptr<::android::hardware::hidl_binder_death_recipient> mInitialDeathRecipient;
229
230 enum MethodNames {
231 SET_CALLBACK,
232 GET_VENDOR_TAGS,
233 IS_SET_TORCH_MODE_SUPPORTED,
234 NOTIFY_DEVICE_STATE,
235 GET_CAMERA_ID_LIST,
236
237 METHOD_NAME_COUNT
238 };
239 int mCalledCounter[METHOD_NAME_COUNT] {0};
240
241 hardware::hidl_bitfield<DeviceState> mCurrentState = 0xFFFFFFFF; // Unlikely to be a real state
242 };
243
244 /**
245 * Simple test version of the interaction proxy, to use to inject onRegistered calls to the
246 * CameraProviderManager
247 */
248 struct TestInteractionProxy : public CameraProviderManager::HidlServiceInteractionProxy {
249 sp<hidl::manager::V1_0::IServiceNotification> mManagerNotificationInterface;
250 sp<TestICameraProvider> mTestCameraProvider;
251
TestInteractionProxyTestInteractionProxy252 TestInteractionProxy() {}
253
setProviderTestInteractionProxy254 void setProvider(sp<TestICameraProvider> provider) {
255 mTestCameraProvider = provider;
256 }
257
258 std::vector<std::string> mLastRequestedServiceNames;
259
~TestInteractionProxyTestInteractionProxy260 virtual ~TestInteractionProxy() {}
261
registerForNotificationsTestInteractionProxy262 virtual bool registerForNotifications(
263 [[maybe_unused]] const std::string &serviceName,
264 const sp<hidl::manager::V1_0::IServiceNotification> ¬ification) override {
265 mManagerNotificationInterface = notification;
266 return true;
267 }
268
tryGetServiceTestInteractionProxy269 virtual sp<hardware::camera::provider::V2_4::ICameraProvider> tryGetService(
270 const std::string &serviceName) override {
271 // If no provider has been given, act like the HAL isn't available and return null.
272 if (mTestCameraProvider == nullptr) return nullptr;
273 return getService(serviceName);
274 }
275
getServiceTestInteractionProxy276 virtual sp<hardware::camera::provider::V2_4::ICameraProvider> getService(
277 const std::string &serviceName) override {
278 // If no provider has been given, fail; in reality, getService would
279 // block for HALs that don't start correctly, so we should never use
280 // getService when we don't have a valid HAL running
281 if (mTestCameraProvider == nullptr) {
282 ADD_FAILURE() << "getService called with no valid provider; would block indefinitely";
283 // Real getService would block, but that's bad in unit tests. So
284 // just record an error and return nullptr
285 return nullptr;
286 }
287 mLastRequestedServiceNames.push_back(serviceName);
288 return mTestCameraProvider;
289 }
290
listServicesTestInteractionProxy291 virtual hardware::hidl_vec<hardware::hidl_string> listServices() override {
292 // Always provide a list even if there's no actual provider yet, to
293 // simulate stuck HAL situations as well
294 hardware::hidl_vec<hardware::hidl_string> ret = {"test/0"};
295 return ret;
296 }
297
298 };
299
300 struct TestStatusListener : public CameraProviderManager::StatusListener {
301 int mPhysicalCameraStatusChangeCount = 0;
302
~TestStatusListenerTestStatusListener303 ~TestStatusListener() {}
304
onDeviceStatusChangedTestStatusListener305 void onDeviceStatusChanged(const String8 &,
306 CameraDeviceStatus) override {}
onDeviceStatusChangedTestStatusListener307 void onDeviceStatusChanged(const String8 &, const String8 &,
308 CameraDeviceStatus) override {
309 mPhysicalCameraStatusChangeCount++;
310 }
onTorchStatusChangedTestStatusListener311 void onTorchStatusChanged(const String8 &,
312 TorchModeStatus) override {}
onTorchStatusChangedTestStatusListener313 void onTorchStatusChanged(const String8 &,
314 TorchModeStatus, SystemCameraKind) override {}
onNewProviderRegisteredTestStatusListener315 void onNewProviderRegistered() override {}
316 };
317
TEST(CameraProviderManagerTest,InitializeDynamicDepthTest)318 TEST(CameraProviderManagerTest, InitializeDynamicDepthTest) {
319 std::vector<hardware::hidl_string> deviceNames;
320 deviceNames.push_back("device@3.2/test/0");
321 hardware::hidl_vec<common::V1_0::VendorTagSection> vendorSection;
322 status_t res;
323 sp<CameraProviderManager> providerManager = new CameraProviderManager();
324 sp<TestStatusListener> statusListener = new TestStatusListener();
325 TestInteractionProxy serviceProxy;
326
327 android::hardware::hidl_vec<uint8_t> chars;
328 CameraMetadata meta;
329 int32_t charKeys[] = { ANDROID_DEPTH_DEPTH_IS_EXCLUSIVE,
330 ANDROID_DEPTH_AVAILABLE_DEPTH_STREAM_CONFIGURATIONS };
331 meta.update(ANDROID_REQUEST_AVAILABLE_CHARACTERISTICS_KEYS, charKeys,
332 sizeof(charKeys) / sizeof(charKeys[0]));
333 uint8_t depthIsExclusive = ANDROID_DEPTH_DEPTH_IS_EXCLUSIVE_FALSE;
334 meta.update(ANDROID_DEPTH_DEPTH_IS_EXCLUSIVE, &depthIsExclusive, 1);
335 int32_t sizes[] = { HAL_PIXEL_FORMAT_BLOB,
336 640, 480, ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_OUTPUT };
337 meta.update(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS, sizes,
338 sizeof(sizes) / sizeof(sizes[0]));
339 sizes[0] = HAL_PIXEL_FORMAT_Y16;
340 meta.update(ANDROID_DEPTH_AVAILABLE_DEPTH_STREAM_CONFIGURATIONS, sizes,
341 sizeof(sizes) / sizeof(sizes[0]));
342 int64_t durations[] = { HAL_PIXEL_FORMAT_BLOB, 640, 480, 0 };
343 meta.update(ANDROID_SCALER_AVAILABLE_MIN_FRAME_DURATIONS, durations,
344 sizeof(durations) / sizeof(durations[0]));
345 meta.update(ANDROID_SCALER_AVAILABLE_STALL_DURATIONS, durations,
346 sizeof(durations) / sizeof(durations[0]));
347 durations[0]= HAL_PIXEL_FORMAT_Y16;
348 meta.update(ANDROID_DEPTH_AVAILABLE_DEPTH_MIN_FRAME_DURATIONS, durations,
349 sizeof(durations) / sizeof(durations[0]));
350 meta.update(ANDROID_DEPTH_AVAILABLE_DEPTH_STALL_DURATIONS, durations,
351 sizeof(durations) / sizeof(durations[0]));
352 camera_metadata_t* metaBuffer = const_cast<camera_metadata_t*>(meta.getAndLock());
353 chars.setToExternal(reinterpret_cast<uint8_t*>(metaBuffer),
354 get_camera_metadata_size(metaBuffer));
355
356 sp<TestICameraProvider> provider = new TestICameraProvider(deviceNames,
357 vendorSection, chars);
358 serviceProxy.setProvider(provider);
359
360 res = providerManager->initialize(statusListener, &serviceProxy);
361 ASSERT_EQ(res, OK) << "Unable to initialize provider manager";
362 }
363
TEST(CameraProviderManagerTest,InitializeTest)364 TEST(CameraProviderManagerTest, InitializeTest) {
365 std::vector<hardware::hidl_string> deviceNames;
366 deviceNames.push_back("device@3.2/test/0");
367 deviceNames.push_back("device@1.0/test/0");
368 deviceNames.push_back("device@3.2/test/1");
369 hardware::hidl_vec<common::V1_0::VendorTagSection> vendorSection;
370 status_t res;
371 sp<CameraProviderManager> providerManager = new CameraProviderManager();
372 sp<TestStatusListener> statusListener = new TestStatusListener();
373 TestInteractionProxy serviceProxy;
374 sp<TestICameraProvider> provider = new TestICameraProvider(deviceNames,
375 vendorSection);
376 serviceProxy.setProvider(provider);
377
378 int numProviders = static_cast<int>(serviceProxy.listServices().size());
379
380 res = providerManager->initialize(statusListener, &serviceProxy);
381 ASSERT_EQ(res, OK) << "Unable to initialize provider manager";
382 // Check that both "legacy" and "external" providers (really the same object) are called
383 // once for all the init methods
384 EXPECT_EQ(provider->mCalledCounter[TestICameraProvider::SET_CALLBACK], numProviders) <<
385 "Only one call to setCallback per provider expected during init";
386 EXPECT_EQ(provider->mCalledCounter[TestICameraProvider::GET_VENDOR_TAGS], numProviders) <<
387 "Only one call to getVendorTags per provider expected during init";
388 EXPECT_EQ(provider->mCalledCounter[TestICameraProvider::IS_SET_TORCH_MODE_SUPPORTED],
389 numProviders) <<
390 "Only one call to isSetTorchModeSupported per provider expected during init";
391 EXPECT_EQ(provider->mCalledCounter[TestICameraProvider::GET_CAMERA_ID_LIST], numProviders) <<
392 "Only one call to getCameraIdList per provider expected during init";
393 EXPECT_EQ(provider->mCalledCounter[TestICameraProvider::NOTIFY_DEVICE_STATE], numProviders) <<
394 "Only one call to notifyDeviceState per provider expected during init";
395
396 hardware::hidl_string testProviderFqInterfaceName =
397 "android.hardware.camera.provider@2.4::ICameraProvider";
398 hardware::hidl_string testProviderInstanceName = "test/0";
399 serviceProxy.mManagerNotificationInterface->onRegistration(
400 testProviderFqInterfaceName,
401 testProviderInstanceName, false);
402
403 ASSERT_EQ(serviceProxy.mLastRequestedServiceNames.back(), testProviderInstanceName) <<
404 "Incorrect instance requested from service manager";
405 }
406
TEST(CameraProviderManagerTest,MultipleVendorTagTest)407 TEST(CameraProviderManagerTest, MultipleVendorTagTest) {
408 hardware::hidl_string sectionName = "VendorTestSection";
409 hardware::hidl_string tagName = "VendorTestTag";
410 uint32_t tagId = VENDOR_SECTION << 16;
411 hardware::hidl_vec<common::V1_0::VendorTagSection> vendorSection;
412 CameraMetadataType tagType = CameraMetadataType::BYTE;
413 vendorSection.resize(1);
414 vendorSection[0].sectionName = sectionName;
415 vendorSection[0].tags.resize(1);
416 vendorSection[0].tags[0].tagId = tagId;
417 vendorSection[0].tags[0].tagName = tagName;
418 vendorSection[0].tags[0].tagType = tagType;
419 std::vector<hardware::hidl_string> deviceNames = {"device@3.2/test/0"};
420
421 sp<CameraProviderManager> providerManager = new CameraProviderManager();
422 sp<TestStatusListener> statusListener = new TestStatusListener();
423 TestInteractionProxy serviceProxy;
424
425 sp<TestICameraProvider> provider = new TestICameraProvider(deviceNames,
426 vendorSection);
427 serviceProxy.setProvider(provider);
428
429 auto res = providerManager->initialize(statusListener, &serviceProxy);
430 ASSERT_EQ(res, OK) << "Unable to initialize provider manager";
431
432 hardware::hidl_string testProviderInstanceName = "test/0";
433 hardware::hidl_string testProviderFqInterfaceName =
434 "android.hardware.camera.provider@2.4::ICameraProvider";
435 serviceProxy.mManagerNotificationInterface->onRegistration(
436 testProviderFqInterfaceName, testProviderInstanceName, false);
437 ASSERT_EQ(serviceProxy.mLastRequestedServiceNames.back(), testProviderInstanceName) <<
438 "Incorrect instance requested from service manager";
439
440 hardware::hidl_string sectionNameSecond = "SecondVendorTestSection";
441 hardware::hidl_string secondTagName = "SecondVendorTestTag";
442 CameraMetadataType secondTagType = CameraMetadataType::DOUBLE;
443 vendorSection[0].sectionName = sectionNameSecond;
444 vendorSection[0].tags[0].tagId = tagId;
445 vendorSection[0].tags[0].tagName = secondTagName;
446 vendorSection[0].tags[0].tagType = secondTagType;
447 deviceNames = {"device@3.2/test2/1"};
448
449 sp<TestICameraProvider> secondProvider = new TestICameraProvider(
450 deviceNames, vendorSection);
451 serviceProxy.setProvider(secondProvider);
452 hardware::hidl_string testProviderSecondInstanceName = "test2/0";
453 serviceProxy.mManagerNotificationInterface->onRegistration(
454 testProviderFqInterfaceName, testProviderSecondInstanceName, false);
455 ASSERT_EQ(serviceProxy.mLastRequestedServiceNames.back(),
456 testProviderSecondInstanceName) <<
457 "Incorrect instance requested from service manager";
458
459 ASSERT_EQ(NO_ERROR , providerManager->setUpVendorTags());
460 sp<VendorTagDescriptorCache> vendorCache =
461 VendorTagDescriptorCache::getGlobalVendorTagCache();
462 ASSERT_NE(nullptr, vendorCache.get());
463
464 metadata_vendor_id_t vendorId = std::hash<std::string> {} (
465 testProviderInstanceName.c_str());
466 metadata_vendor_id_t vendorIdSecond = std::hash<std::string> {} (
467 testProviderSecondInstanceName.c_str());
468
469 hardware::hidl_string resultTag = vendorCache->getTagName(tagId, vendorId);
470 ASSERT_EQ(resultTag, tagName);
471
472 resultTag = vendorCache->getTagName(tagId, vendorIdSecond);
473 ASSERT_EQ(resultTag, secondTagName);
474
475 // Check whether we can create two separate CameraMetadata instances
476 // using different tag vendor vendors.
477 camera_metadata *metaBuffer = allocate_camera_metadata(10, 20);
478 ASSERT_NE(nullptr, metaBuffer);
479 set_camera_metadata_vendor_id(metaBuffer, vendorId);
480 CameraMetadata metadata(metaBuffer);
481
482 uint8_t byteVal = 10;
483 ASSERT_TRUE(metadata.isEmpty());
484 ASSERT_EQ(OK, metadata.update(tagId, &byteVal, 1));
485 ASSERT_FALSE(metadata.isEmpty());
486 ASSERT_TRUE(metadata.exists(tagId));
487
488 metaBuffer = allocate_camera_metadata(10, 20);
489 ASSERT_NE(nullptr, metaBuffer);
490 set_camera_metadata_vendor_id(metaBuffer, vendorIdSecond);
491 CameraMetadata secondMetadata(metaBuffer);
492
493 ASSERT_TRUE(secondMetadata.isEmpty());
494 double doubleVal = 1.0f;
495 ASSERT_EQ(OK, secondMetadata.update(tagId, &doubleVal, 1));
496 ASSERT_FALSE(secondMetadata.isEmpty());
497 ASSERT_TRUE(secondMetadata.exists(tagId));
498
499 // Check whether CameraMetadata copying works as expected
500 CameraMetadata metadataCopy(metadata);
501 ASSERT_FALSE(metadataCopy.isEmpty());
502 ASSERT_TRUE(metadataCopy.exists(tagId));
503 ASSERT_EQ(OK, metadataCopy.update(tagId, &byteVal, 1));
504 ASSERT_TRUE(metadataCopy.exists(tagId));
505
506 // Check whether values are as expected
507 camera_metadata_entry_t entry = metadata.find(tagId);
508 ASSERT_EQ(1u, entry.count);
509 ASSERT_EQ(byteVal, entry.data.u8[0]);
510 entry = secondMetadata.find(tagId);
511 ASSERT_EQ(1u, entry.count);
512 ASSERT_EQ(doubleVal, entry.data.d[0]);
513
514 // Swap and erase
515 secondMetadata.swap(metadataCopy);
516 ASSERT_TRUE(metadataCopy.exists(tagId));
517 ASSERT_TRUE(secondMetadata.exists(tagId));
518 ASSERT_EQ(OK, secondMetadata.erase(tagId));
519 ASSERT_TRUE(secondMetadata.isEmpty());
520 doubleVal = 0.0f;
521 ASSERT_EQ(OK, metadataCopy.update(tagId, &doubleVal, 1));
522 entry = metadataCopy.find(tagId);
523 ASSERT_EQ(1u, entry.count);
524 ASSERT_EQ(doubleVal, entry.data.d[0]);
525
526 // Append
527 uint8_t sceneMode = ANDROID_CONTROL_SCENE_MODE_ACTION;
528 secondMetadata.update(ANDROID_CONTROL_SCENE_MODE, &sceneMode, 1);
529 // Append from two different vendor tag providers is not supported!
530 ASSERT_NE(OK, metadataCopy.append(secondMetadata));
531 ASSERT_EQ(OK, metadataCopy.erase(tagId));
532 metadataCopy.update(ANDROID_CONTROL_SCENE_MODE, &sceneMode, 1);
533 // However appending from same vendor tag provider should be fine
534 ASSERT_EQ(OK, metadata.append(secondMetadata));
535 // Append from a metadata without vendor tag provider should be supported
536 CameraMetadata regularMetadata(10, 20);
537 uint8_t controlMode = ANDROID_CONTROL_MODE_AUTO;
538 regularMetadata.update(ANDROID_CONTROL_MODE, &controlMode, 1);
539 ASSERT_EQ(OK, secondMetadata.append(regularMetadata));
540 ASSERT_EQ(2u, secondMetadata.entryCount());
541 ASSERT_EQ(2u, metadata.entryCount());
542
543 // Dump
544 metadata.dump(1, 2);
545 metadataCopy.dump(1, 2);
546 secondMetadata.dump(1, 2);
547 }
548
TEST(CameraProviderManagerTest,NotifyStateChangeTest)549 TEST(CameraProviderManagerTest, NotifyStateChangeTest) {
550 std::vector<hardware::hidl_string> deviceNames {
551 "device@3.2/test/0",
552 "device@1.0/test/0",
553 "device@3.2/test/1"};
554
555 hardware::hidl_vec<common::V1_0::VendorTagSection> vendorSection;
556 status_t res;
557 sp<CameraProviderManager> providerManager = new CameraProviderManager();
558 sp<TestStatusListener> statusListener = new TestStatusListener();
559 TestInteractionProxy serviceProxy;
560 sp<TestICameraProvider> provider = new TestICameraProvider(deviceNames,
561 vendorSection);
562 serviceProxy.setProvider(provider);
563
564 res = providerManager->initialize(statusListener, &serviceProxy);
565 ASSERT_EQ(res, OK) << "Unable to initialize provider manager";
566
567 ASSERT_EQ(provider->mCurrentState,
568 static_cast<hardware::hidl_bitfield<DeviceState>>(DeviceState::NORMAL))
569 << "Initial device state not set";
570
571 res = providerManager->notifyDeviceStateChange(
572 static_cast<hardware::hidl_bitfield<DeviceState>>(DeviceState::FOLDED));
573
574 ASSERT_EQ(res, OK) << "Unable to call notifyDeviceStateChange";
575 ASSERT_EQ(provider->mCurrentState,
576 static_cast<hardware::hidl_bitfield<DeviceState>>(DeviceState::FOLDED))
577 << "Unable to change device state";
578
579 }
580
581 // Test that CameraProviderManager doesn't get stuck when the camera HAL isn't really working
TEST(CameraProviderManagerTest,BadHalStartupTest)582 TEST(CameraProviderManagerTest, BadHalStartupTest) {
583
584 std::vector<hardware::hidl_string> deviceNames;
585 deviceNames.push_back("device@3.2/test/0");
586 deviceNames.push_back("device@1.0/test/0");
587 deviceNames.push_back("device@3.2/test/1");
588 hardware::hidl_vec<common::V1_0::VendorTagSection> vendorSection;
589 status_t res;
590
591 sp<CameraProviderManager> providerManager = new CameraProviderManager();
592 sp<TestStatusListener> statusListener = new TestStatusListener();
593 TestInteractionProxy serviceProxy;
594 sp<TestICameraProvider> provider = new TestICameraProvider(deviceNames,
595 vendorSection);
596
597 // Not setting up provider in the service proxy yet, to test cases where a
598 // HAL isn't starting right
599 res = providerManager->initialize(statusListener, &serviceProxy);
600 ASSERT_EQ(res, OK) << "Unable to initialize provider manager";
601
602 // Now set up provider and trigger a registration
603 serviceProxy.setProvider(provider);
604 int numProviders = static_cast<int>(serviceProxy.listServices().size());
605
606 hardware::hidl_string testProviderFqInterfaceName =
607 "android.hardware.camera.provider@2.4::ICameraProvider";
608 hardware::hidl_string testProviderInstanceName = "test/0";
609 serviceProxy.mManagerNotificationInterface->onRegistration(
610 testProviderFqInterfaceName,
611 testProviderInstanceName, false);
612
613 // Check that new provider is called once for all the init methods
614 EXPECT_EQ(provider->mCalledCounter[TestICameraProvider::SET_CALLBACK], numProviders) <<
615 "Only one call to setCallback per provider expected during register";
616 EXPECT_EQ(provider->mCalledCounter[TestICameraProvider::GET_VENDOR_TAGS], numProviders) <<
617 "Only one call to getVendorTags per provider expected during register";
618 EXPECT_EQ(provider->mCalledCounter[TestICameraProvider::IS_SET_TORCH_MODE_SUPPORTED],
619 numProviders) <<
620 "Only one call to isSetTorchModeSupported per provider expected during init";
621 EXPECT_EQ(provider->mCalledCounter[TestICameraProvider::GET_CAMERA_ID_LIST], numProviders) <<
622 "Only one call to getCameraIdList per provider expected during init";
623 EXPECT_EQ(provider->mCalledCounter[TestICameraProvider::NOTIFY_DEVICE_STATE], numProviders) <<
624 "Only one call to notifyDeviceState per provider expected during init";
625
626 ASSERT_EQ(serviceProxy.mLastRequestedServiceNames.back(), testProviderInstanceName) <<
627 "Incorrect instance requested from service manager";
628 }
629
630 // Test that CameraProviderManager can handle races between provider death notifications and
631 // provider registration callbacks
TEST(CameraProviderManagerTest,BinderDeathRegistrationRaceTest)632 TEST(CameraProviderManagerTest, BinderDeathRegistrationRaceTest) {
633
634 std::vector<hardware::hidl_string> deviceNames;
635 deviceNames.push_back("device@3.2/test/0");
636 deviceNames.push_back("device@3.2/test/1");
637 hardware::hidl_vec<common::V1_0::VendorTagSection> vendorSection;
638 status_t res;
639
640 sp<CameraProviderManager> providerManager = new CameraProviderManager();
641 sp<TestStatusListener> statusListener = new TestStatusListener();
642 TestInteractionProxy serviceProxy;
643 sp<TestICameraProvider> provider = new TestICameraProvider(deviceNames,
644 vendorSection);
645
646 // Not setting up provider in the service proxy yet, to test cases where a
647 // HAL isn't starting right
648 res = providerManager->initialize(statusListener, &serviceProxy);
649 ASSERT_EQ(res, OK) << "Unable to initialize provider manager";
650
651 // Now set up provider and trigger a registration
652 serviceProxy.setProvider(provider);
653
654 hardware::hidl_string testProviderFqInterfaceName =
655 "android.hardware.camera.provider@2.4::ICameraProvider";
656 hardware::hidl_string testProviderInstanceName = "test/0";
657 serviceProxy.mManagerNotificationInterface->onRegistration(
658 testProviderFqInterfaceName,
659 testProviderInstanceName, false);
660
661 // Simulate artificial delay of the registration callback which arrives before the
662 // death notification
663 serviceProxy.mManagerNotificationInterface->onRegistration(
664 testProviderFqInterfaceName,
665 testProviderInstanceName, false);
666
667 provider->signalInitialBinderDeathRecipient();
668
669 auto deviceCount = static_cast<unsigned> (providerManager->getCameraCount().second);
670 ASSERT_EQ(deviceCount, deviceNames.size()) <<
671 "Unexpected amount of camera devices";
672 }
673
674 // Test that CameraProviderManager does not trigger
675 // onDeviceStatusChanged(NOT_PRESENT) for physical camera before initialize()
676 // returns.
TEST(CameraProviderManagerTest,PhysicalCameraAvailabilityCallbackRaceTest)677 TEST(CameraProviderManagerTest, PhysicalCameraAvailabilityCallbackRaceTest) {
678 std::vector<hardware::hidl_string> deviceNames;
679 deviceNames.push_back("device@3.2/test/0");
680 hardware::hidl_vec<common::V1_0::VendorTagSection> vendorSection;
681
682 sp<CameraProviderManager> providerManager = new CameraProviderManager();
683 sp<TestStatusListener> statusListener = new TestStatusListener();
684 TestInteractionProxy serviceProxy;
685
686 android::hardware::hidl_vec<uint8_t> chars;
687 CameraMetadata meta;
688 int32_t charKeys[] = { ANDROID_REQUEST_AVAILABLE_CAPABILITIES };
689 meta.update(ANDROID_REQUEST_AVAILABLE_CHARACTERISTICS_KEYS, charKeys,
690 sizeof(charKeys) / sizeof(charKeys[0]));
691 uint8_t capabilities[] = { ANDROID_REQUEST_AVAILABLE_CAPABILITIES_LOGICAL_MULTI_CAMERA };
692 meta.update(ANDROID_REQUEST_AVAILABLE_CAPABILITIES, capabilities,
693 sizeof(capabilities)/sizeof(capabilities[0]));
694 uint8_t physicalCameraIds[] = { '2', '\0', '3', '\0' };
695 meta.update(ANDROID_LOGICAL_MULTI_CAMERA_PHYSICAL_IDS, physicalCameraIds,
696 sizeof(physicalCameraIds)/sizeof(physicalCameraIds[0]));
697 camera_metadata_t* metaBuffer = const_cast<camera_metadata_t*>(meta.getAndLock());
698 chars.setToExternal(reinterpret_cast<uint8_t*>(metaBuffer),
699 get_camera_metadata_size(metaBuffer));
700
701 sp<TestICameraProvider> provider = new TestICameraProvider(deviceNames,
702 vendorSection, chars, "device@3.2/test/0", "2");
703 serviceProxy.setProvider(provider);
704
705 status_t res = providerManager->initialize(statusListener, &serviceProxy);
706 ASSERT_EQ(res, OK) << "Unable to initialize provider manager";
707
708 ASSERT_EQ(statusListener->mPhysicalCameraStatusChangeCount, 0)
709 << "Unexpected physical camera status change callback upon provider init.";
710
711 std::unordered_map<std::string, std::set<std::string>> unavailablePhysicalIds;
712 auto cameraIds = providerManager->getCameraDeviceIds(&unavailablePhysicalIds);
713 ASSERT_TRUE(unavailablePhysicalIds.count("0") > 0 && unavailablePhysicalIds["0"].count("2") > 0)
714 << "Unavailable physical camera Ids not set properly.";
715 }
716