1 /*
2 * Copyright (C) 2019 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #define LOG_TAG "GCH_HidlCameraProvider"
18 //#define LOG_NDEBUG 0
19 #include <log/log.h>
20 #include <regex>
21
22 #include "camera_device.h"
23 #include "hidl_camera_device.h"
24 #include "hidl_camera_provider.h"
25 #include "hidl_utils.h"
26
27 namespace android {
28 namespace hardware {
29 namespace camera {
30 namespace provider {
31 namespace V2_6 {
32 namespace implementation {
33
34 namespace hidl_utils = ::android::hardware::camera::implementation::hidl_utils;
35
36 using ::android::google_camera_hal::CameraDevice;
37
38 const std::string HidlCameraProvider::kProviderName = "internal";
39 // "device@<version>/internal/<id>"
40 const std::regex HidlCameraProvider::kDeviceNameRegex(
41 "device@([0-9]+\\.[0-9]+)/internal/(.+)");
42
Create()43 std::unique_ptr<HidlCameraProvider> HidlCameraProvider::Create() {
44 auto provider = std::unique_ptr<HidlCameraProvider>(new HidlCameraProvider());
45 if (provider == nullptr) {
46 ALOGE("%s: Cannot create a HidlCameraProvider.", __FUNCTION__);
47 return nullptr;
48 }
49
50 status_t res = provider->Initialize();
51 if (res != OK) {
52 ALOGE("%s: Initializing HidlCameraProvider failed: %s(%d)", __FUNCTION__,
53 strerror(-res), res);
54 return nullptr;
55 }
56
57 return provider;
58 }
59
Initialize()60 status_t HidlCameraProvider::Initialize() {
61 google_camera_provider_ = CameraProvider::Create();
62 if (google_camera_provider_ == nullptr) {
63 ALOGE("%s: Creating CameraProvider failed.", __FUNCTION__);
64 return NO_INIT;
65 }
66
67 camera_provider_callback_ = {
68 .camera_device_status_change = google_camera_hal::CameraDeviceStatusChangeFunc(
69 [this](std::string camera_id,
70 google_camera_hal::CameraDeviceStatus new_status) {
71 if (callbacks_ == nullptr) {
72 ALOGE("%s: callbacks_ is null", __FUNCTION__);
73 return;
74 }
75 CameraDeviceStatus hidl_camera_device_status;
76 status_t res = hidl_utils::ConvertToHidlCameraDeviceStatus(
77 new_status, &hidl_camera_device_status);
78 if (res != OK) {
79 ALOGE(
80 "%s: Converting to hidl camera device status failed: %s(%d)",
81 __FUNCTION__, strerror(-res), res);
82 return;
83 }
84
85 std::unique_lock<std::mutex> lock(callbacks_lock_);
86 callbacks_->cameraDeviceStatusChange(
87 "device@" +
88 device::V3_5::implementation::HidlCameraDevice::kDeviceVersion +
89 "/" + kProviderName + "/" + camera_id,
90 hidl_camera_device_status);
91 }),
92 .physical_camera_device_status_change = google_camera_hal::
93 PhysicalCameraDeviceStatusChangeFunc([this](
94 std::string camera_id,
95 std::string physical_camera_id,
96 google_camera_hal::CameraDeviceStatus
97 new_status) {
98 if (callbacks_ == nullptr) {
99 ALOGE("%s: callbacks_ is null", __FUNCTION__);
100 return;
101 }
102 auto castResult =
103 provider::V2_6::ICameraProviderCallback::castFrom(callbacks_);
104 if (!castResult.isOk()) {
105 ALOGE("%s: callbacks_ cannot be casted to version 2.6",
106 __FUNCTION__);
107 return;
108 }
109 sp<provider::V2_6::ICameraProviderCallback> callbacks_2_6_ =
110 castResult;
111 if (callbacks_2_6_ == nullptr) {
112 ALOGE("%s: callbacks_2_6_ is null", __FUNCTION__);
113 return;
114 }
115
116 CameraDeviceStatus hidl_camera_device_status;
117 status_t res = hidl_utils::ConvertToHidlCameraDeviceStatus(
118 new_status, &hidl_camera_device_status);
119 if (res != OK) {
120 ALOGE(
121 "%s: Converting to hidl camera device status failed: %s(%d)",
122 __FUNCTION__, strerror(-res), res);
123 return;
124 }
125
126 std::unique_lock<std::mutex> lock(callbacks_lock_);
127 callbacks_2_6_->physicalCameraDeviceStatusChange(
128 "device@" +
129 device::V3_5::implementation::HidlCameraDevice::kDeviceVersion +
130 "/" + kProviderName + "/" + camera_id,
131 physical_camera_id, hidl_camera_device_status);
132 }),
133 .torch_mode_status_change = google_camera_hal::TorchModeStatusChangeFunc(
134 [this](std::string camera_id,
135 google_camera_hal::TorchModeStatus new_status) {
136 if (callbacks_ == nullptr) {
137 ALOGE("%s: callbacks_ is null", __FUNCTION__);
138 return;
139 }
140
141 TorchModeStatus hidl_torch_status;
142 status_t res = hidl_utils::ConvertToHidlTorchModeStatus(
143 new_status, &hidl_torch_status);
144 if (res != OK) {
145 ALOGE("%s: Converting to hidl torch status failed: %s(%d)",
146 __FUNCTION__, strerror(-res), res);
147 return;
148 }
149
150 std::unique_lock<std::mutex> lock(callbacks_lock_);
151 callbacks_->torchModeStatusChange(
152 "device@" +
153 device::V3_5::implementation::HidlCameraDevice::kDeviceVersion +
154 "/" + kProviderName + "/" + camera_id,
155 hidl_torch_status);
156 }),
157 };
158
159 google_camera_provider_->SetCallback(&camera_provider_callback_);
160 return OK;
161 }
162
setCallback(const sp<ICameraProviderCallback> & callback)163 Return<Status> HidlCameraProvider::setCallback(
164 const sp<ICameraProviderCallback>& callback) {
165 {
166 std::unique_lock<std::mutex> lock(callbacks_lock_);
167 callbacks_ = callback;
168 }
169
170 google_camera_provider_->TriggerDeferredCallbacks();
171
172 return Status::OK;
173 }
174
getVendorTags(getVendorTags_cb _hidl_cb)175 Return<void> HidlCameraProvider::getVendorTags(getVendorTags_cb _hidl_cb) {
176 hidl_vec<VendorTagSection> hidl_vendor_tag_sections;
177 std::vector<google_camera_hal::VendorTagSection> hal_vendor_tag_sections;
178
179 status_t res =
180 google_camera_provider_->GetVendorTags(&hal_vendor_tag_sections);
181 if (res != OK) {
182 ALOGE("%s: Getting vendor tags failed: %s(%d)", __FUNCTION__,
183 strerror(-res), res);
184 _hidl_cb(Status::INTERNAL_ERROR, hidl_vendor_tag_sections);
185 return Void();
186 }
187
188 res = hidl_utils::ConvertToHidlVendorTagSections(hal_vendor_tag_sections,
189 &hidl_vendor_tag_sections);
190 if (res != OK) {
191 ALOGE("%s: Converting to hidl vendor tags failed: %s(%d)", __FUNCTION__,
192 strerror(-res), res);
193 _hidl_cb(Status::INTERNAL_ERROR, hidl_vendor_tag_sections);
194 return Void();
195 }
196
197 _hidl_cb(Status::OK, hidl_vendor_tag_sections);
198 return Void();
199 }
200
getCameraIdList(getCameraIdList_cb _hidl_cb)201 Return<void> HidlCameraProvider::getCameraIdList(getCameraIdList_cb _hidl_cb) {
202 std::vector<uint32_t> camera_ids;
203 hidl_vec<hidl_string> hidl_camera_ids;
204
205 status_t res = google_camera_provider_->GetCameraIdList(&camera_ids);
206 if (res != OK) {
207 ALOGE("%s: Getting camera ID list failed: %s(%d)", __FUNCTION__,
208 strerror(-res), res);
209 _hidl_cb(Status::INTERNAL_ERROR, hidl_camera_ids);
210 return Void();
211 }
212
213 hidl_camera_ids.resize(camera_ids.size());
214 for (uint32_t i = 0; i < hidl_camera_ids.size(); i++) {
215 // camera ID is in the form of "device@<major>.<minor>/<type>/<id>"
216 hidl_camera_ids[i] =
217 "device@" +
218 device::V3_5::implementation::HidlCameraDevice::kDeviceVersion + "/" +
219 kProviderName + "/" + std::to_string(camera_ids[i]);
220 }
221
222 _hidl_cb(Status::OK, hidl_camera_ids);
223 return Void();
224 }
225
getConcurrentStreamingCameraIds(getConcurrentStreamingCameraIds_cb _hidl_cb)226 Return<void> HidlCameraProvider::getConcurrentStreamingCameraIds(
227 getConcurrentStreamingCameraIds_cb _hidl_cb) {
228 hidl_vec<hidl_vec<hidl_string>> hidl_camera_id_combinations;
229 std::vector<std::unordered_set<uint32_t>> camera_id_combinations;
230 status_t res = google_camera_provider_->GetConcurrentStreamingCameraIds(
231 &camera_id_combinations);
232 if (res != OK) {
233 ALOGE(
234 "%s: Getting the combinations of concurrent streaming camera ids "
235 "failed: %s(%d)",
236 __FUNCTION__, strerror(-res), res);
237 _hidl_cb(Status::INTERNAL_ERROR, hidl_camera_id_combinations);
238 return Void();
239 }
240 hidl_camera_id_combinations.resize(camera_id_combinations.size());
241 int i = 0;
242 for (auto& combination : camera_id_combinations) {
243 hidl_vec<hidl_string> hidl_combination(combination.size());
244 int c = 0;
245 for (auto& camera_id : combination) {
246 hidl_combination[c] = std::to_string(camera_id);
247 c++;
248 }
249 hidl_camera_id_combinations[i] = hidl_combination;
250 i++;
251 }
252 _hidl_cb(Status::OK, hidl_camera_id_combinations);
253 return Void();
254 }
255
isConcurrentStreamCombinationSupported(const hidl_vec<CameraIdAndStreamCombination> & configs,isConcurrentStreamCombinationSupported_cb _hidl_cb)256 Return<void> HidlCameraProvider::isConcurrentStreamCombinationSupported(
257 const hidl_vec<CameraIdAndStreamCombination>& configs,
258 isConcurrentStreamCombinationSupported_cb _hidl_cb) {
259 std::vector<google_camera_hal::CameraIdAndStreamConfiguration>
260 devices_stream_configs(configs.size());
261 status_t res = OK;
262 size_t c = 0;
263 for (auto& config : configs) {
264 res = hidl_utils::ConverToHalStreamConfig(
265 config.streamConfiguration,
266 &devices_stream_configs[c].stream_configuration);
267 if (res != OK) {
268 ALOGE("%s: ConverToHalStreamConfig failed", __FUNCTION__);
269 _hidl_cb(Status::INTERNAL_ERROR, false);
270 return Void();
271 }
272 uint32_t camera_id = atoi(config.cameraId.c_str());
273 devices_stream_configs[c].camera_id = camera_id;
274 c++;
275 }
276 bool is_supported = false;
277 res = google_camera_provider_->IsConcurrentStreamCombinationSupported(
278 devices_stream_configs, &is_supported);
279 if (res != OK) {
280 ALOGE("%s: ConverToHalStreamConfig failed", __FUNCTION__);
281 _hidl_cb(Status::INTERNAL_ERROR, false);
282 return Void();
283 }
284 _hidl_cb(Status::OK, is_supported);
285 return Void();
286 }
287
isSetTorchModeSupported(isSetTorchModeSupported_cb _hidl_cb)288 Return<void> HidlCameraProvider::isSetTorchModeSupported(
289 isSetTorchModeSupported_cb _hidl_cb) {
290 bool is_supported = google_camera_provider_->IsSetTorchModeSupported();
291 _hidl_cb(Status::OK, is_supported);
292 return Void();
293 }
294
getCameraDeviceInterface_V1_x(const hidl_string &,getCameraDeviceInterface_V1_x_cb _hidl_cb)295 Return<void> HidlCameraProvider::getCameraDeviceInterface_V1_x(
296 const hidl_string& /*cameraDeviceName*/,
297 getCameraDeviceInterface_V1_x_cb _hidl_cb) {
298 _hidl_cb(Status::OPERATION_NOT_SUPPORTED, nullptr);
299 return Void();
300 }
301
ParseDeviceName(const hidl_string & device_name,std::string * device_version,std::string * camera_id)302 bool HidlCameraProvider::ParseDeviceName(const hidl_string& device_name,
303 std::string* device_version,
304 std::string* camera_id) {
305 std::string device_name_std(device_name.c_str());
306 std::smatch sm;
307
308 if (std::regex_match(device_name_std, sm,
309 HidlCameraProvider::kDeviceNameRegex)) {
310 if (device_version != nullptr) {
311 *device_version = sm[1];
312 }
313 if (camera_id != nullptr) {
314 *camera_id = sm[2];
315 }
316 return true;
317 }
318 return false;
319 }
320
getCameraDeviceInterface_V3_x(const hidl_string & camera_device_name,getCameraDeviceInterface_V3_x_cb _hidl_cb)321 Return<void> HidlCameraProvider::getCameraDeviceInterface_V3_x(
322 const hidl_string& camera_device_name,
323 getCameraDeviceInterface_V3_x_cb _hidl_cb) {
324 std::unique_ptr<CameraDevice> google_camera_device;
325
326 // Parse camera_device_name.
327 std::string camera_id, device_version;
328
329 bool match = ParseDeviceName(camera_device_name, &device_version, &camera_id);
330 if (!match) {
331 ALOGE("%s: Device name parse fail. ", __FUNCTION__);
332 _hidl_cb(Status::ILLEGAL_ARGUMENT, nullptr);
333 return Void();
334 }
335
336 status_t res = google_camera_provider_->CreateCameraDevice(
337 atoi(camera_id.c_str()), &google_camera_device);
338 if (res != OK) {
339 ALOGE("%s: Creating CameraDevice failed: %s(%d)", __FUNCTION__,
340 strerror(-res), res);
341 _hidl_cb(hidl_utils::ConvertToHidlStatus(res), nullptr);
342 return Void();
343 }
344
345 auto hidl_camera_device =
346 device::V3_5::implementation::HidlCameraDevice::Create(
347 std::move(google_camera_device));
348 if (hidl_camera_device == nullptr) {
349 ALOGE("%s: Creating HidlCameraDevice failed", __FUNCTION__);
350 _hidl_cb(Status::INTERNAL_ERROR, nullptr);
351 return Void();
352 }
353
354 _hidl_cb(Status::OK, hidl_camera_device.release());
355 return Void();
356 }
357
notifyDeviceStateChange(hardware::hidl_bitfield<DeviceState>)358 Return<void> HidlCameraProvider::notifyDeviceStateChange(
359 hardware::hidl_bitfield<DeviceState> /*newState*/) {
360 return Void();
361 }
362
HIDL_FETCH_ICameraProvider(const char * name)363 ICameraProvider* HIDL_FETCH_ICameraProvider(const char* name) {
364 std::string provider_name = HidlCameraProvider::kProviderName + "/0";
365 if (provider_name.compare(name) != 0) {
366 ALOGE("%s: Unknown provider name: %s", __FUNCTION__, name);
367 return nullptr;
368 }
369
370 auto provider = HidlCameraProvider::Create();
371 if (provider == nullptr) {
372 ALOGE("%s: Cannot create a HidlCameraProvider.", __FUNCTION__);
373 return nullptr;
374 }
375
376 return provider.release();
377 }
378
379 } // namespace implementation
380 } // namespace V2_6
381 } // namespace provider
382 } // namespace camera
383 } // namespace hardware
384 } // namespace android
385