blob: c0cd1a9ba9f2b6d9b210552c790d144f5f363941 [file] [log] [blame]
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001/*
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>
Emilian Peev71c73a22017-03-21 16:35:51 +000023#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>
Emilian Peevc93cac22020-08-17 16:00:10 -070026#include <hidl/HidlBinderSupport.h>
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080027#include <gtest/gtest.h>
Emilian Peevc93cac22020-08-17 16:00:10 -070028#include <utility>
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080029
30using namespace android;
31using namespace android::hardware::camera;
32using android::hardware::camera::common::V1_0::Status;
Emilian Peev71c73a22017-03-21 16:35:51 +000033using android::hardware::camera::common::V1_0::VendorTag;
34using android::hardware::camera::common::V1_0::VendorTagSection;
35using android::hardware::camera::common::V1_0::CameraMetadataType;
36using android::hardware::camera::device::V3_2::ICameraDeviceCallback;
37using android::hardware::camera::device::V3_2::ICameraDeviceSession;
Eino-Ville Talvala63f36112018-12-06 14:57:03 -080038using android::hardware::camera::provider::V2_5::DeviceState;
Emilian Peev71c73a22017-03-21 16:35:51 +000039
40/**
41 * Basic test implementation of a camera ver. 3.2 device interface
42 */
43struct TestDeviceInterface : public device::V3_2::ICameraDevice {
44 std::vector<hardware::hidl_string> mDeviceNames;
Emilian Peev2a245e12020-04-07 16:54:14 -070045 android::hardware::hidl_vec<uint8_t> mCharacteristics;
46
47 TestDeviceInterface(std::vector<hardware::hidl_string> deviceNames,
48 android::hardware::hidl_vec<uint8_t> chars) :
49 mDeviceNames(deviceNames), mCharacteristics(chars) {}
50
Emilian Peev71c73a22017-03-21 16:35:51 +000051 TestDeviceInterface(std::vector<hardware::hidl_string> deviceNames) :
52 mDeviceNames(deviceNames) {}
Emilian Peev2a245e12020-04-07 16:54:14 -070053
Emilian Peev71c73a22017-03-21 16:35:51 +000054 using getResourceCost_cb = std::function<void(
55 hardware::camera::common::V1_0::Status status,
56 const hardware::camera::common::V1_0::CameraResourceCost& resourceCost)>;
57 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)>;
68 hardware::Return<void> getCameraCharacteristics(
69 getCameraCharacteristics_cb _hidl_cb) override {
Emilian Peev2a245e12020-04-07 16:54:14 -070070 _hidl_cb(Status::OK, mCharacteristics);
Emilian Peev71c73a22017-03-21 16:35:51 +000071 return hardware::Void();
72 }
73
74 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)>;
82 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
90 hardware::Return<void> dumpState(
91 const ::android::hardware::hidl_handle&) override {
92 return hardware::Void();
93 }
94};
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080095
96/**
97 * Basic test implementation of a camera provider
98 */
Eino-Ville Talvala63f36112018-12-06 14:57:03 -080099struct TestICameraProvider : virtual public provider::V2_5::ICameraProvider {
Emilian Peev71c73a22017-03-21 16:35:51 +0000100 sp<provider::V2_4::ICameraProviderCallback> mCallbacks;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800101 std::vector<hardware::hidl_string> mDeviceNames;
Emilian Peev71c73a22017-03-21 16:35:51 +0000102 sp<device::V3_2::ICameraDevice> mDeviceInterface;
103 hardware::hidl_vec<common::V1_0::VendorTagSection> mVendorTagSections;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800104
Shuzhen Wang3d316f32022-10-25 20:33:34 +0000105 // Whether to call a physical camera unavailable callback upon setCallback
106 bool mHasPhysicalCameraUnavailableCallback;
107 hardware::hidl_string mLogicalCameraId;
108 hardware::hidl_string mUnavailablePhysicalCameraId;
109
Emilian Peev71c73a22017-03-21 16:35:51 +0000110 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)),
Shuzhen Wang3d316f32022-10-25 20:33:34 +0000114 mVendorTagSections (vendorSection),
115 mHasPhysicalCameraUnavailableCallback(false) {}
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800116
Emilian Peev2a245e12020-04-07 16:54:14 -0700117 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)),
Shuzhen Wang3d316f32022-10-25 20:33:34 +0000122 mVendorTagSections (vendorSection),
123 mHasPhysicalCameraUnavailableCallback(false) {}
124
125 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) {}
Emilian Peev2a245e12020-04-07 16:54:14 -0700136
Emilian Peev71c73a22017-03-21 16:35:51 +0000137 virtual hardware::Return<Status> setCallback(
138 const sp<provider::V2_4::ICameraProviderCallback>& callbacks) override {
Eino-Ville Talvala63f36112018-12-06 14:57:03 -0800139 mCalledCounter[SET_CALLBACK]++;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800140 mCallbacks = callbacks;
Shuzhen Wang3d316f32022-10-25 20:33:34 +0000141 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 }
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800156 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)>;
Emilian Peev71c73a22017-03-21 16:35:51 +0000161 hardware::Return<void> getVendorTags(getVendorTags_cb _hidl_cb) override {
Eino-Ville Talvala63f36112018-12-06 14:57:03 -0800162 mCalledCounter[GET_VENDOR_TAGS]++;
Emilian Peev71c73a22017-03-21 16:35:51 +0000163 _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)>;
170 virtual ::hardware::Return<void> isSetTorchModeSupported(
171 isSetTorchModeSupported_cb _hidl_cb) override {
Eino-Ville Talvala63f36112018-12-06 14:57:03 -0800172 mCalledCounter[IS_SET_TORCH_MODE_SUPPORTED]++;
Emilian Peev71c73a22017-03-21 16:35:51 +0000173 _hidl_cb(Status::OK, false);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800174 return hardware::Void();
175 }
176
177 using getCameraIdList_cb = std::function<void(Status status,
178 const hardware::hidl_vec<hardware::hidl_string>& cameraDeviceNames)>;
179 virtual hardware::Return<void> getCameraIdList(getCameraIdList_cb _hidl_cb) override {
Eino-Ville Talvala63f36112018-12-06 14:57:03 -0800180 mCalledCounter[GET_CAMERA_ID_LIST]++;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800181 _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)>;
187 virtual hardware::Return<void> getCameraDeviceInterface_V1_x(
Jing Mikec7f9b132023-03-12 11:12:04 +0800188 [[maybe_unused]] const hardware::hidl_string& cameraDeviceName,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800189 getCameraDeviceInterface_V1_x_cb _hidl_cb) override {
Emilian Peev71c73a22017-03-21 16:35:51 +0000190 _hidl_cb(Status::OK, nullptr); //TODO: impl. of ver. 1.0 device interface
191 // otherwise enumeration will fail.
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800192 return hardware::Void();
193 }
194
195 using getCameraDeviceInterface_V3_x_cb = std::function<void(Status status,
196 const sp<device::V3_2::ICameraDevice>& device)>;
197 virtual hardware::Return<void> getCameraDeviceInterface_V3_x(
Emilian Peev71c73a22017-03-21 16:35:51 +0000198 const hardware::hidl_string&,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800199 getCameraDeviceInterface_V3_x_cb _hidl_cb) override {
Emilian Peev71c73a22017-03-21 16:35:51 +0000200 _hidl_cb(Status::OK, mDeviceInterface);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800201 return hardware::Void();
202 }
203
Eino-Ville Talvala63f36112018-12-06 14:57:03 -0800204 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
Emilian Peevc93cac22020-08-17 16:00:10 -0700211 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
222 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
Eino-Ville Talvala63f36112018-12-06 14:57:03 -0800230 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
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800242};
243
244/**
245 * Simple test version of the interaction proxy, to use to inject onRegistered calls to the
246 * CameraProviderManager
247 */
Jayant Chowdhary0bd38522021-11-05 17:49:27 -0700248struct TestInteractionProxy : public CameraProviderManager::HidlServiceInteractionProxy {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800249 sp<hidl::manager::V1_0::IServiceNotification> mManagerNotificationInterface;
Emilian Peev71c73a22017-03-21 16:35:51 +0000250 sp<TestICameraProvider> mTestCameraProvider;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800251
Emilian Peev71c73a22017-03-21 16:35:51 +0000252 TestInteractionProxy() {}
Eino-Ville Talvala998d1fe2019-10-23 10:34:53 -0700253
Emilian Peev71c73a22017-03-21 16:35:51 +0000254 void setProvider(sp<TestICameraProvider> provider) {
255 mTestCameraProvider = provider;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800256 }
Emilian Peev71c73a22017-03-21 16:35:51 +0000257
Eino-Ville Talvalabb6e4142018-08-21 14:17:40 -0700258 std::vector<std::string> mLastRequestedServiceNames;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800259
260 virtual ~TestInteractionProxy() {}
261
262 virtual bool registerForNotifications(
Jing Mikec7f9b132023-03-12 11:12:04 +0800263 [[maybe_unused]] const std::string &serviceName,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800264 const sp<hidl::manager::V1_0::IServiceNotification> &notification) override {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800265 mManagerNotificationInterface = notification;
266 return true;
267 }
268
Eino-Ville Talvalaec960602019-10-15 11:46:16 -0700269 virtual sp<hardware::camera::provider::V2_4::ICameraProvider> tryGetService(
270 const std::string &serviceName) override {
Eino-Ville Talvala998d1fe2019-10-23 10:34:53 -0700271 // If no provider has been given, act like the HAL isn't available and return null.
272 if (mTestCameraProvider == nullptr) return nullptr;
Eino-Ville Talvalaec960602019-10-15 11:46:16 -0700273 return getService(serviceName);
274 }
275
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800276 virtual sp<hardware::camera::provider::V2_4::ICameraProvider> getService(
277 const std::string &serviceName) override {
Eino-Ville Talvala998d1fe2019-10-23 10:34:53 -0700278 // 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 }
Eino-Ville Talvalabb6e4142018-08-21 14:17:40 -0700287 mLastRequestedServiceNames.push_back(serviceName);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800288 return mTestCameraProvider;
289 }
290
Yin-Chia Yeh177b0c12019-06-25 10:53:03 -0700291 virtual hardware::hidl_vec<hardware::hidl_string> listServices() override {
Eino-Ville Talvala998d1fe2019-10-23 10:34:53 -0700292 // Always provide a list even if there's no actual provider yet, to
293 // simulate stuck HAL situations as well
Yin-Chia Yeh177b0c12019-06-25 10:53:03 -0700294 hardware::hidl_vec<hardware::hidl_string> ret = {"test/0"};
295 return ret;
296 }
297
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800298};
299
Emilian Peev71c73a22017-03-21 16:35:51 +0000300struct TestStatusListener : public CameraProviderManager::StatusListener {
Shuzhen Wang3d316f32022-10-25 20:33:34 +0000301 int mPhysicalCameraStatusChangeCount = 0;
302
Emilian Peev71c73a22017-03-21 16:35:51 +0000303 ~TestStatusListener() {}
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800304
Austin Borger1c1bee02023-06-01 16:51:35 -0700305 void onDeviceStatusChanged(const std::string &,
Jayant Chowdhary0bd38522021-11-05 17:49:27 -0700306 CameraDeviceStatus) override {}
Austin Borger1c1bee02023-06-01 16:51:35 -0700307 void onDeviceStatusChanged(const std::string &, const std::string &,
Shuzhen Wang3d316f32022-10-25 20:33:34 +0000308 CameraDeviceStatus) override {
309 mPhysicalCameraStatusChangeCount++;
310 }
Austin Borger1c1bee02023-06-01 16:51:35 -0700311 void onTorchStatusChanged(const std::string &,
Jayant Chowdhary0bd38522021-11-05 17:49:27 -0700312 TorchModeStatus) override {}
Austin Borger1c1bee02023-06-01 16:51:35 -0700313 void onTorchStatusChanged(const std::string &,
Jayant Chowdhary0bd38522021-11-05 17:49:27 -0700314 TorchModeStatus, SystemCameraKind) override {}
Eino-Ville Talvala7cffc832018-06-03 17:32:53 -0700315 void onNewProviderRegistered() override {}
Emilian Peev71c73a22017-03-21 16:35:51 +0000316};
317
Emilian Peev2a245e12020-04-07 16:54:14 -0700318TEST(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
Emilian Peev71c73a22017-03-21 16:35:51 +0000364TEST(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;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800370 status_t res;
371 sp<CameraProviderManager> providerManager = new CameraProviderManager();
Emilian Peev71c73a22017-03-21 16:35:51 +0000372 sp<TestStatusListener> statusListener = new TestStatusListener();
373 TestInteractionProxy serviceProxy;
374 sp<TestICameraProvider> provider = new TestICameraProvider(deviceNames,
375 vendorSection);
376 serviceProxy.setProvider(provider);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800377
Yin-Chia Yeh177b0c12019-06-25 10:53:03 -0700378 int numProviders = static_cast<int>(serviceProxy.listServices().size());
379
Emilian Peev71c73a22017-03-21 16:35:51 +0000380 res = providerManager->initialize(statusListener, &serviceProxy);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800381 ASSERT_EQ(res, OK) << "Unable to initialize provider manager";
Eino-Ville Talvala63f36112018-12-06 14:57:03 -0800382 // Check that both "legacy" and "external" providers (really the same object) are called
383 // once for all the init methods
Yin-Chia Yeh177b0c12019-06-25 10:53:03 -0700384 EXPECT_EQ(provider->mCalledCounter[TestICameraProvider::SET_CALLBACK], numProviders) <<
Eino-Ville Talvala63f36112018-12-06 14:57:03 -0800385 "Only one call to setCallback per provider expected during init";
Yin-Chia Yeh177b0c12019-06-25 10:53:03 -0700386 EXPECT_EQ(provider->mCalledCounter[TestICameraProvider::GET_VENDOR_TAGS], numProviders) <<
Eino-Ville Talvala63f36112018-12-06 14:57:03 -0800387 "Only one call to getVendorTags per provider expected during init";
Yin-Chia Yeh177b0c12019-06-25 10:53:03 -0700388 EXPECT_EQ(provider->mCalledCounter[TestICameraProvider::IS_SET_TORCH_MODE_SUPPORTED],
389 numProviders) <<
Eino-Ville Talvala63f36112018-12-06 14:57:03 -0800390 "Only one call to isSetTorchModeSupported per provider expected during init";
Yin-Chia Yeh177b0c12019-06-25 10:53:03 -0700391 EXPECT_EQ(provider->mCalledCounter[TestICameraProvider::GET_CAMERA_ID_LIST], numProviders) <<
Eino-Ville Talvala63f36112018-12-06 14:57:03 -0800392 "Only one call to getCameraIdList per provider expected during init";
Yin-Chia Yeh177b0c12019-06-25 10:53:03 -0700393 EXPECT_EQ(provider->mCalledCounter[TestICameraProvider::NOTIFY_DEVICE_STATE], numProviders) <<
Eino-Ville Talvala63f36112018-12-06 14:57:03 -0800394 "Only one call to notifyDeviceState per provider expected during init";
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800395
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800396 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
Eino-Ville Talvalabb6e4142018-08-21 14:17:40 -0700403 ASSERT_EQ(serviceProxy.mLastRequestedServiceNames.back(), testProviderInstanceName) <<
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800404 "Incorrect instance requested from service manager";
405}
Emilian Peev71c73a22017-03-21 16:35:51 +0000406
407TEST(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);
Eino-Ville Talvalabb6e4142018-08-21 14:17:40 -0700437 ASSERT_EQ(serviceProxy.mLastRequestedServiceNames.back(), testProviderInstanceName) <<
Emilian Peev71c73a22017-03-21 16:35:51 +0000438 "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);
Eino-Ville Talvalabb6e4142018-08-21 14:17:40 -0700455 ASSERT_EQ(serviceProxy.mLastRequestedServiceNames.back(),
Emilian Peev71c73a22017-03-21 16:35:51 +0000456 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));
Emilian Peev68dbd4e2017-04-11 11:44:29 +0100535 // Append from a metadata without vendor tag provider should be supported
Emilian Peev71c73a22017-03-21 16:35:51 +0000536 CameraMetadata regularMetadata(10, 20);
537 uint8_t controlMode = ANDROID_CONTROL_MODE_AUTO;
538 regularMetadata.update(ANDROID_CONTROL_MODE, &controlMode, 1);
Emilian Peev68dbd4e2017-04-11 11:44:29 +0100539 ASSERT_EQ(OK, secondMetadata.append(regularMetadata));
540 ASSERT_EQ(2u, secondMetadata.entryCount());
Emilian Peev71c73a22017-03-21 16:35:51 +0000541 ASSERT_EQ(2u, metadata.entryCount());
542
543 // Dump
544 metadata.dump(1, 2);
545 metadataCopy.dump(1, 2);
546 secondMetadata.dump(1, 2);
547}
Eino-Ville Talvala63f36112018-12-06 14:57:03 -0800548
549TEST(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}
Eino-Ville Talvala998d1fe2019-10-23 10:34:53 -0700580
581// Test that CameraProviderManager doesn't get stuck when the camera HAL isn't really working
582TEST(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}
Emilian Peevc93cac22020-08-17 16:00:10 -0700629
630// Test that CameraProviderManager can handle races between provider death notifications and
631// provider registration callbacks
632TEST(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}
Shuzhen Wang3d316f32022-10-25 20:33:34 +0000673
674// Test that CameraProviderManager does not trigger
675// onDeviceStatusChanged(NOT_PRESENT) for physical camera before initialize()
676// returns.
677TEST(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}