1 /*
2 * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <iostream>
17 #include <map>
18 #include <string>
19 #include <unordered_set>
20
21 #include "access_token.h"
22 #include "accesstoken_kit.h"
23 #include "camera_util.h"
24 #include "iservice_registry.h"
25 #include "media_log.h"
26 #include "system_ability_definition.h"
27 #include "ipc_skeleton.h"
28 #include "hcamera_service.h"
29
30 namespace OHOS {
31 namespace CameraStandard {
REGISTER_SYSTEM_ABILITY_BY_ID(HCameraService,CAMERA_SERVICE_ID,true)32 REGISTER_SYSTEM_ABILITY_BY_ID(HCameraService, CAMERA_SERVICE_ID, true)
33 HCameraService::HCameraService(int32_t systemAbilityId, bool runOnCreate)
34 : SystemAbility(systemAbilityId, runOnCreate),
35 cameraHostManager_(nullptr),
36 cameraDeviceCallback_(nullptr),
37 streamOperatorCallback_(nullptr),
38 cameraServiceCallback_(nullptr)
39 {
40 }
41
~HCameraService()42 HCameraService::~HCameraService()
43 {
44 }
45
OnStart()46 void HCameraService::OnStart()
47 {
48 if (!cameraHostManager_) {
49 cameraHostManager_ = new HCameraHostManager(this);
50 }
51 if (cameraHostManager_->Init() != CAMERA_OK) {
52 MEDIA_ERR_LOG("HCameraService OnStart failed to init camera host manager.");
53 }
54 bool res = Publish(this);
55 if (res) {
56 MEDIA_INFO_LOG("HCameraService OnStart res=%{public}d", res);
57 }
58 }
59
OnDump()60 void HCameraService::OnDump()
61 {
62 MEDIA_INFO_LOG("HCameraService::OnDump called");
63 }
64
OnStop()65 void HCameraService::OnStop()
66 {
67 MEDIA_INFO_LOG("HCameraService::OnStop called");
68
69 if (cameraHostManager_) {
70 cameraHostManager_->DeInit();
71 delete cameraHostManager_;
72 cameraHostManager_ = nullptr;
73 }
74 }
75
GetCameras(std::vector<std::string> & cameraIds,std::vector<std::shared_ptr<Camera::CameraMetadata>> & cameraAbilityList)76 int32_t HCameraService::GetCameras(std::vector<std::string> &cameraIds,
77 std::vector<std::shared_ptr<Camera::CameraMetadata>> &cameraAbilityList)
78 {
79 int32_t ret = cameraHostManager_->GetCameras(cameraIds);
80 if (ret != CAMERA_OK) {
81 MEDIA_ERR_LOG("HCameraService::GetCameras failed");
82 return ret;
83 }
84
85 std::shared_ptr<Camera::CameraMetadata> cameraAbility;
86 for (auto id : cameraIds) {
87 ret = cameraHostManager_->GetCameraAbility(id, cameraAbility);
88 if (ret != CAMERA_OK) {
89 MEDIA_ERR_LOG("HCameraService::GetCameraAbility failed");
90 return ret;
91 }
92 cameraAbilityList.emplace_back(cameraAbility);
93 }
94
95 return ret;
96 }
97
CreateCameraDevice(std::string cameraId,sptr<ICameraDeviceService> & device)98 int32_t HCameraService::CreateCameraDevice(std::string cameraId, sptr<ICameraDeviceService> &device)
99 {
100 sptr<HCameraDevice> cameraDevice;
101
102 OHOS::Security::AccessToken::AccessTokenID callerToken = IPCSkeleton::GetCallingTokenID();
103 std::string permissionName = "ohos.permission.CAMERA";
104
105 int permission_result
106 = OHOS::Security::AccessToken::TypePermissionState::PERMISSION_DENIED;
107 if (OHOS::Security::AccessToken::AccessTokenKit::GetTokenTypeFlag(callerToken)
108 == OHOS::Security::AccessToken::ATokenTypeEnum::TOKEN_NATIVE) {
109 permission_result = OHOS::Security::AccessToken::AccessTokenKit::VerifyNativeToken(callerToken,
110 permissionName);
111 } else if (OHOS::Security::AccessToken::AccessTokenKit::GetTokenTypeFlag(callerToken)
112 == OHOS::Security::AccessToken::ATokenTypeEnum::TOKEN_HAP) {
113 permission_result = OHOS::Security::AccessToken::AccessTokenKit::VerifyAccessToken(callerToken,
114 permissionName);
115 } else {
116 MEDIA_ERR_LOG("HCameraService::CreateCameraDevice: Unsupported Access Token Type");
117 return CAMERA_INVALID_ARG;
118 }
119 if (permission_result != OHOS::Security::AccessToken::TypePermissionState::PERMISSION_GRANTED) {
120 MEDIA_ERR_LOG("HCameraService::CreateCameraDevice: Permission to Access Camera Denied!!!!");
121 return CAMERA_ALLOC_ERROR;
122 } else {
123 MEDIA_DEBUG_LOG("HCameraService::CreateCameraDevice: Permission to Access Camera Granted!!!!");
124 }
125
126 if (cameraDeviceCallback_ == nullptr) {
127 cameraDeviceCallback_ = new CameraDeviceCallback();
128 }
129 cameraDevice = new HCameraDevice(cameraHostManager_, cameraDeviceCallback_, cameraId);
130 if (cameraDevice == nullptr) {
131 MEDIA_ERR_LOG("HCameraService::CreateCameraDevice HCameraDevice allocation failed");
132 return CAMERA_ALLOC_ERROR;
133 }
134 devices_.insert(std::make_pair(cameraId, cameraDevice));
135 device = cameraDevice;
136 return CAMERA_OK;
137 }
138
CreateCaptureSession(sptr<ICaptureSession> & session)139 int32_t HCameraService::CreateCaptureSession(sptr<ICaptureSession> &session)
140 {
141 sptr<HCaptureSession> captureSession;
142 if (streamOperatorCallback_ == nullptr) {
143 streamOperatorCallback_ = new StreamOperatorCallback();
144 }
145
146 captureSession = new HCaptureSession(cameraHostManager_, streamOperatorCallback_);
147 if (captureSession == nullptr) {
148 MEDIA_ERR_LOG("HCameraService::CreateCaptureSession HCaptureSession allocation failed");
149 return CAMERA_ALLOC_ERROR;
150 }
151 session = captureSession;
152 return CAMERA_OK;
153 }
154
CreatePhotoOutput(const sptr<OHOS::IBufferProducer> & producer,int32_t format,sptr<IStreamCapture> & photoOutput)155 int32_t HCameraService::CreatePhotoOutput(const sptr<OHOS::IBufferProducer> &producer, int32_t format,
156 sptr<IStreamCapture> &photoOutput)
157 {
158 if (producer == nullptr) {
159 MEDIA_ERR_LOG("HCameraService::CreatePhotoOutput producer is null");
160 return CAMERA_INVALID_ARG;
161 }
162 sptr<HStreamCapture> streamCapture = new HStreamCapture(producer, format);
163 if (streamCapture == nullptr) {
164 MEDIA_ERR_LOG("HCameraService::CreatePhotoOutput HStreamCapture allocation failed");
165 return CAMERA_ALLOC_ERROR;
166 }
167 photoOutput = streamCapture;
168 return CAMERA_OK;
169 }
170
CreatePreviewOutput(const sptr<OHOS::IBufferProducer> & producer,int32_t format,sptr<IStreamRepeat> & previewOutput)171 int32_t HCameraService::CreatePreviewOutput(const sptr<OHOS::IBufferProducer> &producer, int32_t format,
172 sptr<IStreamRepeat> &previewOutput)
173 {
174 sptr<HStreamRepeat> streamRepeatPreview;
175
176 if (producer == nullptr) {
177 MEDIA_ERR_LOG("HCameraService::CreatePreviewOutput producer is null");
178 return CAMERA_INVALID_ARG;
179 }
180 streamRepeatPreview = new HStreamRepeat(producer, format);
181 if (streamRepeatPreview == nullptr) {
182 MEDIA_ERR_LOG("HCameraService::CreatePreviewOutput HStreamRepeat allocation failed");
183 return CAMERA_ALLOC_ERROR;
184 }
185 previewOutput = streamRepeatPreview;
186 return CAMERA_OK;
187 }
188
CreateCustomPreviewOutput(const sptr<OHOS::IBufferProducer> & producer,int32_t format,int32_t width,int32_t height,sptr<IStreamRepeat> & previewOutput)189 int32_t HCameraService::CreateCustomPreviewOutput(const sptr<OHOS::IBufferProducer> &producer, int32_t format,
190 int32_t width, int32_t height, sptr<IStreamRepeat> &previewOutput)
191 {
192 sptr<HStreamRepeat> streamRepeatPreview;
193
194 if (producer == nullptr || width == 0 || height == 0) {
195 MEDIA_ERR_LOG("HCameraService::CreateCustomPreviewOutput producer is null or invalid custom size is set");
196 return CAMERA_INVALID_ARG;
197 }
198 streamRepeatPreview = new HStreamRepeat(producer, format, width, height);
199 if (streamRepeatPreview == nullptr) {
200 MEDIA_ERR_LOG("HCameraService::CreateCustomPreviewOutput HStreamRepeat allocation failed");
201 return CAMERA_ALLOC_ERROR;
202 }
203 previewOutput = streamRepeatPreview;
204 return CAMERA_OK;
205 }
206
CreateVideoOutput(const sptr<OHOS::IBufferProducer> & producer,int32_t format,sptr<IStreamRepeat> & videoOutput)207 int32_t HCameraService::CreateVideoOutput(const sptr<OHOS::IBufferProducer> &producer, int32_t format,
208 sptr<IStreamRepeat> &videoOutput)
209 {
210 sptr<HStreamRepeat> streamRepeatVideo;
211
212 if (producer == nullptr) {
213 MEDIA_ERR_LOG("HCameraService::CreateVideoOutput producer is null");
214 return CAMERA_INVALID_ARG;
215 }
216 streamRepeatVideo = new HStreamRepeat(producer, format, true);
217 if (streamRepeatVideo == nullptr) {
218 MEDIA_ERR_LOG("HCameraService::CreateVideoOutput HStreamRepeat allocation failed");
219 return CAMERA_ALLOC_ERROR;
220 }
221 videoOutput = streamRepeatVideo;
222 return CAMERA_OK;
223 }
224
OnCameraStatus(const std::string & cameraId,CameraStatus status)225 void HCameraService::OnCameraStatus(const std::string& cameraId, CameraStatus status)
226 {
227 if (cameraServiceCallback_) {
228 cameraServiceCallback_->OnCameraStatusChanged(cameraId, status);
229 }
230 }
231
OnFlashlightStatus(const std::string & cameraId,FlashStatus status)232 void HCameraService::OnFlashlightStatus(const std::string& cameraId, FlashStatus status)
233 {
234 if (cameraServiceCallback_) {
235 cameraServiceCallback_->OnFlashlightStatusChanged(cameraId, status);
236 }
237 }
238
SetCallback(sptr<ICameraServiceCallback> & callback)239 int32_t HCameraService::SetCallback(sptr<ICameraServiceCallback> &callback)
240 {
241 if (callback == nullptr) {
242 MEDIA_ERR_LOG("HCameraService::SetCallback callback is null");
243 return CAMERA_INVALID_ARG;
244 }
245 cameraServiceCallback_ = callback;
246 return CAMERA_OK;
247 }
248
CameraSummary(std::vector<std::string> cameraIds,std::string & dumpString)249 void HCameraService::CameraSummary(std::vector<std::string> cameraIds,
250 std::string& dumpString)
251 {
252 dumpString += "# Number of Cameras:[" + std::to_string(cameraIds.size()) + "]:\n";
253 dumpString += "# Number of Active Cameras:[" + std::to_string(devices_.size()) + "]:\n";
254 HCaptureSession::CameraSessionSummary(dumpString);
255 }
256
CameraDumpAbility(common_metadata_header_t * metadataEntry,std::string & dumpString)257 void HCameraService::CameraDumpAbility(common_metadata_header_t *metadataEntry,
258 std::string& dumpString)
259 {
260 camera_metadata_item_t item;
261 int ret;
262 dumpString += " ## Camera Ability List: \n";
263
264 ret = Camera::FindCameraMetadataItem(metadataEntry, OHOS_ABILITY_CAMERA_POSITION, &item);
265 if (ret == CAM_META_SUCCESS) {
266 std::map<int, std::string>::const_iterator iter =
267 g_cameraPos.find(item.data.u8[0]);
268 if (iter != g_cameraPos.end()) {
269 dumpString += " Camera Position:["
270 + iter->second
271 + "]: ";
272 }
273 }
274
275 ret = Camera::FindCameraMetadataItem(metadataEntry, OHOS_ABILITY_CAMERA_TYPE, &item);
276 if (ret == CAM_META_SUCCESS) {
277 std::map<int, std::string>::const_iterator iter =
278 g_cameraType.find(item.data.u8[0]);
279 if (iter != g_cameraType.end()) {
280 dumpString += "Camera Type:["
281 + iter->second
282 + "]: ";
283 }
284 }
285
286 ret = Camera::FindCameraMetadataItem(metadataEntry, OHOS_ABILITY_CAMERA_CONNECTION_TYPE, &item);
287 if (ret == CAM_META_SUCCESS) {
288 std::map<int, std::string>::const_iterator iter =
289 g_cameraConType.find(item.data.u8[0]);
290 if (iter != g_cameraConType.end()) {
291 dumpString += "Camera Connection Type:["
292 + iter->second
293 + "]:\n";
294 }
295 }
296 }
297
CameraDumpStreaminfo(common_metadata_header_t * metadataEntry,std::string & dumpString)298 void HCameraService::CameraDumpStreaminfo(common_metadata_header_t *metadataEntry,
299 std::string& dumpString)
300 {
301 camera_metadata_item_t item;
302 int ret;
303 uint32_t unitLen = 3;
304 uint32_t widthOffset = 1;
305 uint32_t heightOffset = 2;
306 dumpString += " ### Camera Available stream configuration List: \n";
307 ret = Camera::FindCameraMetadataItem(metadataEntry, OHOS_ABILITY_STREAM_AVAILABLE_BASIC_CONFIGURATIONS, &item);
308 if (ret == CAM_META_SUCCESS) {
309 dumpString += " Number Stream Info: "
310 + std::to_string(item.count/unitLen) + "\n";
311 for (uint32_t index = 0; index < item.count; index += unitLen) {
312 std::map<int, std::string>::const_iterator iter =
313 g_cameraFormat.find(item.data.i32[index]);
314 if (iter != g_cameraFormat.end()) {
315 dumpString += " Format:["
316 + iter->second
317 + "]: ";
318 dumpString += "Size:[Width:"
319 + std::to_string(item.data.i32[index + widthOffset])
320 + " Height:"
321 + std::to_string(item.data.i32[index + heightOffset])
322 + "]:\n";
323 }
324 }
325 }
326 }
327
CameraDumpZoom(common_metadata_header_t * metadataEntry,std::string & dumpString)328 void HCameraService::CameraDumpZoom(common_metadata_header_t *metadataEntry,
329 std::string& dumpString)
330 {
331 dumpString += " ## Zoom Related Info: \n";
332 camera_metadata_item_t item;
333 int ret;
334 int32_t minIndex = 0;
335 int32_t maxIndex = 1;
336 uint32_t zoomRangeCount = 2;
337 ret = Camera::FindCameraMetadataItem(metadataEntry, OHOS_ABILITY_ZOOM_CAP, &item);
338 if ((ret == CAM_META_SUCCESS) && (item.count == zoomRangeCount)) {
339 dumpString += " Available Zoom Capability:["
340 + std::to_string(item.data.i32[minIndex]) + " "
341 + std::to_string(item.data.i32[maxIndex])
342 + "]:\n";
343 }
344
345 ret = Camera::FindCameraMetadataItem(metadataEntry, OHOS_ABILITY_SCENE_ZOOM_CAP, &item);
346 if ((ret == CAM_META_SUCCESS) && (item.count == zoomRangeCount)) {
347 dumpString += " Available scene Zoom Capability:["
348 + std::to_string(item.data.i32[minIndex]) + " "
349 + std::to_string(item.data.i32[maxIndex])
350 + "]:\n";
351 }
352
353 ret = Camera::FindCameraMetadataItem(metadataEntry, OHOS_ABILITY_ZOOM_RATIO_RANGE, &item);
354 if ((ret == CAM_META_SUCCESS) && (item.count == zoomRangeCount)) {
355 dumpString += " Available Zoom Ratio Range:["
356 + std::to_string(item.data.f[minIndex])
357 + std::to_string(item.data.f[maxIndex])
358 + "]:\n";
359 }
360
361 ret = Camera::FindCameraMetadataItem(metadataEntry, OHOS_CONTROL_ZOOM_RATIO, &item);
362 if (ret == CAM_META_SUCCESS) {
363 dumpString += " Set Zoom Ratio:["
364 + std::to_string(item.data.f[0])
365 + "]:\n";
366 }
367 }
368
CameraDumpFlash(common_metadata_header_t * metadataEntry,std::string & dumpString)369 void HCameraService::CameraDumpFlash(common_metadata_header_t *metadataEntry,
370 std::string& dumpString)
371 {
372 camera_metadata_item_t item;
373 int ret;
374 dumpString += " ## Flash Related Info: \n";
375 dumpString += " Available Flash Modes:[";
376 ret = Camera::FindCameraMetadataItem(metadataEntry, OHOS_ABILITY_DEVICE_AVAILABLE_FLASHMODES, &item);
377 if (ret == CAM_META_SUCCESS) {
378 for (uint32_t i = 0; i < item.count; i++) {
379 std::map<int, std::string>::const_iterator iter =
380 g_cameraFlashMode.find(item.data.u8[i]);
381 if (iter != g_cameraFlashMode.end()) {
382 dumpString += " " + iter->second;
383 }
384 }
385 dumpString += "]:\n";
386 }
387
388 ret = Camera::FindCameraMetadataItem(metadataEntry, OHOS_CONTROL_FLASHMODE, &item);
389 if (ret == CAM_META_SUCCESS) {
390 std::map<int, std::string>::const_iterator iter =
391 g_cameraFlashMode.find(item.data.u8[0]);
392 if (iter != g_cameraFlashMode.end()) {
393 dumpString += " Set Flash Mode:["
394 + iter->second
395 + "]:\n";
396 }
397 }
398 }
399
CameraDumpAF(common_metadata_header_t * metadataEntry,std::string & dumpString)400 void HCameraService::CameraDumpAF(common_metadata_header_t *metadataEntry,
401 std::string& dumpString)
402 {
403 camera_metadata_item_t item;
404 int ret;
405 dumpString += " ## AF Related Info: \n";
406 dumpString += " Available Focus Modes:[";
407
408 ret = Camera::FindCameraMetadataItem(metadataEntry, OHOS_CONTROL_AF_AVAILABLE_MODES, &item);
409 if (ret == CAM_META_SUCCESS) {
410 for (uint32_t i = 0; i < item.count; i++) {
411 std::map<int, std::string>::const_iterator iter =
412 g_cameraFocusMode.find(item.data.u8[i]);
413 if (iter != g_cameraFocusMode.end()) {
414 dumpString += " " + iter->second;
415 }
416 }
417 dumpString += "]:\n";
418 }
419
420 ret = Camera::FindCameraMetadataItem(metadataEntry, OHOS_CONTROL_AF_MODE, &item);
421 if (ret == CAM_META_SUCCESS) {
422 std::map<int, std::string>::const_iterator iter =
423 g_cameraFocusMode.find(item.data.u8[0]);
424 if (iter != g_cameraFocusMode.end()) {
425 dumpString += " Set Focus Mode:["
426 + iter->second
427 + "]:\n";
428 }
429 }
430 }
431
CameraDumpSensorInfo(common_metadata_header_t * metadataEntry,std::string & dumpString)432 void HCameraService::CameraDumpSensorInfo(common_metadata_header_t *metadataEntry,
433 std::string& dumpString)
434 {
435 camera_metadata_item_t item;
436 int ret;
437 dumpString += " ## Sensor Info Info: \n";
438 int32_t leftIndex = 0;
439 int32_t topIndex = 1;
440 int32_t rightIndex = 2;
441 int32_t bottomIndex = 3;
442 ret = Camera::FindCameraMetadataItem(metadataEntry, OHOS_SENSOR_INFO_ACTIVE_ARRAY_SIZE, &item);
443 if (ret == CAM_META_SUCCESS) {
444 dumpString += " Array:["
445 + std::to_string(item.data.i32[leftIndex]) + " "
446 + std::to_string(item.data.i32[topIndex]) + " "
447 + std::to_string(item.data.i32[rightIndex]) + " "
448 + std::to_string(item.data.i32[bottomIndex])
449 + "]:\n";
450 }
451 }
452
Dump(int fd,const std::vector<std::u16string> & args)453 int32_t HCameraService::Dump(int fd, const std::vector<std::u16string>& args)
454 {
455 std::unordered_set<std::u16string> argSets;
456 std::u16string arg1(u"summary");
457 std::u16string arg2(u"ability");
458 std::u16string arg3(u"clientwiseinfo");
459 for (decltype(args.size()) index = 0; index < args.size(); ++index) {
460 argSets.insert(args[index]);
461 }
462 std::string dumpString;
463 std::vector<std::string> cameraIds;
464 std::vector<std::shared_ptr<Camera::CameraMetadata>> cameraAbilityList;
465 int32_t capIdx = 0;
466 std::shared_ptr<Camera::CameraMetadata> metadata;
467 int ret;
468
469 ret = GetCameras(cameraIds, cameraAbilityList);
470 if ((ret != CAMERA_OK) || cameraIds.empty()
471 || (cameraAbilityList.empty())) {
472 return CAMERA_INVALID_STATE;
473 }
474 if (args.size() == 0 || argSets.count(arg1) != 0) {
475 dumpString += "-------- Summary -------\n";
476 CameraSummary(cameraIds, dumpString);
477 }
478 if (args.size() == 0 || argSets.count(arg2) != 0) {
479 dumpString += "-------- CameraInfo -------\n";
480 for (auto& it : cameraIds) {
481 metadata = cameraAbilityList[capIdx++];
482 common_metadata_header_t *metadataEntry = metadata->get();
483 dumpString += "# Camera ID:[" + it + "]: \n";
484 CameraDumpAbility(metadataEntry, dumpString);
485 CameraDumpStreaminfo(metadataEntry, dumpString);
486 CameraDumpZoom(metadataEntry, dumpString);
487 CameraDumpFlash(metadataEntry, dumpString);
488 CameraDumpAF(metadataEntry, dumpString);
489 CameraDumpSensorInfo(metadataEntry, dumpString);
490 }
491 }
492 if (args.size() == 0 || argSets.count(arg3) != 0) {
493 dumpString += "-------- Clientwise Info -------\n";
494 HCaptureSession::dumpSessions(dumpString);
495 }
496
497 if (dumpString.size() == 0) {
498 MEDIA_ERR_LOG("Dump string empty!");
499 return CAMERA_INVALID_STATE;
500 }
501
502 (void)write(fd, dumpString.c_str(), dumpString.size());
503 return CAMERA_OK;
504 }
505 } // namespace CameraStandard
506 } // namespace OHOS
507