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 <cstring>
17 #include "input/camera_manager.h"
18
19
20 #include "camera_util.h"
21 #include "ipc_skeleton.h"
22 #include "iservice_registry.h"
23 #include "camera_log.h"
24 #include "system_ability_definition.h"
25 #include "camera_error_code.h"
26 #include "icamera_util.h"
27 #include "device_manager_impl.h"
28
29 using namespace std;
30 namespace OHOS {
31 namespace CameraStandard {
32 sptr<CameraManager> CameraManager::cameraManager_;
33
34 const std::string CameraManager::surfaceFormat = "CAMERA_SURFACE_FORMAT";
35
36 const std::unordered_map<camera_format_t, CameraFormat> CameraManager::metaToFwCameraFormat_ = {
37 {OHOS_CAMERA_FORMAT_YCRCB_420_SP, CAMERA_FORMAT_YUV_420_SP},
38 {OHOS_CAMERA_FORMAT_JPEG, CAMERA_FORMAT_JPEG},
39 {OHOS_CAMERA_FORMAT_RGBA_8888, CAMERA_FORMAT_RGBA_8888}
40 };
41
42 const std::unordered_map<CameraFormat, camera_format_t> CameraManager::fwToMetaCameraFormat_ = {
43 {CAMERA_FORMAT_YUV_420_SP, OHOS_CAMERA_FORMAT_YCRCB_420_SP},
44 {CAMERA_FORMAT_JPEG, OHOS_CAMERA_FORMAT_JPEG},
45 {CAMERA_FORMAT_RGBA_8888, OHOS_CAMERA_FORMAT_RGBA_8888},
46 };
47
CameraManager()48 CameraManager::CameraManager()
49 {
50 Init();
51 cameraObjList = {};
52 dcameraObjList = {};
53 }
54
~CameraManager()55 CameraManager::~CameraManager()
56 {
57 serviceProxy_ = nullptr;
58 listenerStub_ = nullptr;
59 deathRecipient_ = nullptr;
60 cameraSvcCallback_ = nullptr;
61 cameraMuteSvcCallback_ = nullptr;
62 cameraMngrCallback_ = nullptr;
63 for (unsigned int i = 0; i < cameraMuteListenerList.size(); i++) {
64 if (cameraMuteListenerList[i]) {
65 cameraMuteListenerList[i] = nullptr;
66 }
67 }
68 cameraMuteListenerList.clear();
69 for (unsigned int i = 0; i < cameraObjList.size(); i++) {
70 cameraObjList[i] = nullptr;
71 }
72 cameraObjList.clear();
73 dcameraObjList.clear();
74 CameraManager::cameraManager_ = nullptr;
75 }
76 class CameraManager::DeviceInitCallBack : public DistributedHardware::DmInitCallback {
77 void OnRemoteDied() override;
78 };
OnRemoteDied()79 void CameraManager::DeviceInitCallBack::OnRemoteDied()
80 {
81 MEDIA_INFO_LOG("CameraManager::DeviceInitCallBack OnRemoteDied");
82 }
CreateListenerObject()83 int32_t CameraManager::CreateListenerObject()
84 {
85 MEDIA_DEBUG_LOG("CreateListenerObject entry");
86 listenerStub_ = new(std::nothrow) CameraListenerStub();
87 CHECK_AND_RETURN_RET_LOG(listenerStub_ != nullptr, CAMERA_ALLOC_ERROR,
88 "failed to new CameraListenerStub object");
89 CHECK_AND_RETURN_RET_LOG(serviceProxy_ != nullptr, CAMERA_ALLOC_ERROR,
90 "Camera service does not exist.");
91
92 sptr<IRemoteObject> object = listenerStub_->AsObject();
93 CHECK_AND_RETURN_RET_LOG(object != nullptr, CAMERA_ALLOC_ERROR, "listener object is nullptr..");
94
95 return serviceProxy_->SetListenerObject(object);
96 }
97
OnCameraStatusChanged(const std::string & cameraId,const CameraStatus status)98 int32_t CameraStatusServiceCallback::OnCameraStatusChanged(const std::string& cameraId, const CameraStatus status)
99 {
100 MEDIA_INFO_LOG("OnCameraStatusChanged entry");
101 CameraStatusInfo cameraStatusInfo;
102 if (camMngr_ != nullptr && camMngr_->GetApplicationCallback() != nullptr) {
103 cameraStatusInfo.cameraDevice = camMngr_->GetCameraDeviceFromId(cameraId);
104 cameraStatusInfo.cameraStatus = status;
105 if (cameraStatusInfo.cameraDevice) {
106 MEDIA_INFO_LOG("cameraId: %{public}s, status: %{public}d",
107 cameraId.c_str(), status);
108 camMngr_->GetApplicationCallback()->OnCameraStatusChanged(cameraStatusInfo);
109 }
110 } else {
111 MEDIA_INFO_LOG("Callback not registered!, Ignore the callback");
112 }
113 return CAMERA_OK;
114 }
115
OnFlashlightStatusChanged(const std::string & cameraId,const FlashStatus status)116 int32_t CameraStatusServiceCallback::OnFlashlightStatusChanged(const std::string& cameraId, const FlashStatus status)
117 {
118 MEDIA_INFO_LOG("cameraId: %{public}s, status: %{public}d", cameraId.c_str(), status);
119 if (camMngr_ != nullptr && camMngr_->GetApplicationCallback() != nullptr) {
120 camMngr_->GetApplicationCallback()->OnFlashlightStatusChanged(cameraId, status);
121 } else {
122 MEDIA_INFO_LOG("Callback not registered!, Ignore the callback");
123 }
124 return CAMERA_OK;
125 }
126
CreateCaptureSession()127 sptr<CaptureSession> CameraManager::CreateCaptureSession()
128 {
129 CAMERA_SYNC_TRACE;
130 sptr<CaptureSession> captureSession = nullptr;
131 int ret = CreateCaptureSession(&captureSession);
132 if (ret != CameraErrorCode::SUCCESS) {
133 MEDIA_ERR_LOG("Failed to CreateCaptureSession with error code:%{public}d", ret);
134 return nullptr;
135 }
136
137 return captureSession;
138 }
139
CreateCaptureSession(sptr<CaptureSession> * pCaptureSession)140 int CameraManager::CreateCaptureSession(sptr<CaptureSession> *pCaptureSession)
141 {
142 CAMERA_SYNC_TRACE;
143 sptr<ICaptureSession> session = nullptr;
144 sptr<CaptureSession> captureSession = nullptr;
145 int32_t retCode = CAMERA_OK;
146
147 if (serviceProxy_ == nullptr) {
148 MEDIA_ERR_LOG("serviceProxy_ is null");
149 return CameraErrorCode::INVALID_ARGUMENT;
150 }
151
152 retCode = serviceProxy_->CreateCaptureSession(session);
153 if (retCode == CAMERA_OK) {
154 if (session != nullptr) {
155 captureSession = new(std::nothrow) CaptureSession(session);
156 } else {
157 MEDIA_ERR_LOG("Failed to CreateCaptureSession with session is null");
158 return CameraErrorCode::SERVICE_FATL_ERROR;
159 }
160 } else {
161 MEDIA_ERR_LOG("Failed to get capture session object from hcamera service!, %{public}d", retCode);
162 return ServiceToCameraError(retCode);
163 }
164
165 *pCaptureSession = captureSession;
166
167 return CameraErrorCode::SUCCESS;
168 }
169
CreatePhotoOutput(sptr<IBufferProducer> & surface)170 sptr<PhotoOutput> CameraManager::CreatePhotoOutput(sptr<IBufferProducer> &surface)
171 {
172 CAMERA_SYNC_TRACE;
173 sptr<PhotoOutput> result = nullptr;
174 return result;
175 }
176
CreatePhotoOutput(Profile & profile,sptr<IBufferProducer> & surface)177 sptr<PhotoOutput> CameraManager::CreatePhotoOutput(Profile &profile, sptr<IBufferProducer> &surface)
178 {
179 CAMERA_SYNC_TRACE;
180 sptr<PhotoOutput> photoOutput = nullptr;
181 int ret = CreatePhotoOutput(profile, surface, &photoOutput);
182 if (ret != CameraErrorCode::SUCCESS) {
183 MEDIA_ERR_LOG("Failed to CreatePhotoOutput with error code:%{public}d", ret);
184 return nullptr;
185 }
186
187 return photoOutput;
188 }
189
CreatePhotoOutput(Profile & profile,sptr<IBufferProducer> & surface,sptr<PhotoOutput> * pPhotoOutput)190 int CameraManager::CreatePhotoOutput(Profile &profile, sptr<IBufferProducer> &surface, sptr<PhotoOutput> *pPhotoOutput)
191 {
192 CAMERA_SYNC_TRACE;
193 sptr<IStreamCapture> streamCapture = nullptr;
194 sptr<PhotoOutput> photoOutput = nullptr;
195 int32_t retCode = CAMERA_OK;
196 camera_format_t metaFormat;
197
198 if ((serviceProxy_ == nullptr) || (surface == nullptr)) {
199 MEDIA_ERR_LOG("serviceProxy_ is null or PhotoOutputSurface/profile is null");
200 return CameraErrorCode::INVALID_ARGUMENT;
201 }
202
203 if ((profile.GetCameraFormat() == CAMERA_FORMAT_INVALID) ||
204 (profile.GetSize().width == 0) ||
205 (profile.GetSize().height == 0)) {
206 MEDIA_ERR_LOG("invalid fomrat or width or height is zero");
207 return CameraErrorCode::INVALID_ARGUMENT;
208 }
209
210 metaFormat = GetCameraMetadataFormat(profile.GetCameraFormat());
211 retCode = serviceProxy_->CreatePhotoOutput(surface, metaFormat, profile.GetSize().width,
212 profile.GetSize().height, streamCapture);
213 if (retCode == CAMERA_OK) {
214 photoOutput = new(std::nothrow) PhotoOutput(streamCapture);
215 POWERMGR_SYSEVENT_CAMERA_CONFIG(PHOTO, profile.GetSize().width,
216 profile.GetSize().height);
217 } else {
218 MEDIA_ERR_LOG("Failed to get stream capture object from hcamera service!, %{public}d", retCode);
219 return ServiceToCameraError(retCode);
220 }
221
222 *pPhotoOutput = photoOutput;
223
224 return CameraErrorCode::SUCCESS;
225 }
226
CreatePreviewOutput(Profile & profile,sptr<Surface> surface)227 sptr<PreviewOutput> CameraManager::CreatePreviewOutput(Profile &profile, sptr<Surface> surface)
228 {
229 CAMERA_SYNC_TRACE;
230 sptr<PreviewOutput> previewOutput = nullptr;
231 int ret = CreatePreviewOutput(profile, surface, &previewOutput);
232 if (ret != CameraErrorCode::SUCCESS) {
233 MEDIA_ERR_LOG("Failed to CreatePreviewOutput with error code:%{public}d", ret);
234 return nullptr;
235 }
236
237 return previewOutput;
238 }
239
CreatePreviewOutput(Profile & profile,sptr<Surface> surface,sptr<PreviewOutput> * pPreviewOutput)240 int CameraManager::CreatePreviewOutput(Profile &profile, sptr<Surface> surface, sptr<PreviewOutput> *pPreviewOutput)
241 {
242 CAMERA_SYNC_TRACE;
243 sptr<IStreamRepeat> streamRepeat = nullptr;
244 sptr<PreviewOutput> previewOutput = nullptr;
245 int32_t retCode = CAMERA_OK;
246 camera_format_t metaFormat;
247
248 if ((serviceProxy_ == nullptr) || (surface == nullptr)) {
249 MEDIA_ERR_LOG("serviceProxy_ is null or previewOutputSurface/profile is null");
250 return CameraErrorCode::INVALID_ARGUMENT;
251 }
252
253 if ((profile.GetCameraFormat() == CAMERA_FORMAT_INVALID) ||
254 (profile.GetSize().width == 0) ||
255 (profile.GetSize().height == 0)) {
256 MEDIA_ERR_LOG("width or height is zero");
257 return CameraErrorCode::INVALID_ARGUMENT;
258 }
259
260 metaFormat = GetCameraMetadataFormat(profile.GetCameraFormat());
261 retCode = serviceProxy_->CreatePreviewOutput(surface->GetProducer(), metaFormat,
262 profile.GetSize().width, profile.GetSize().height, streamRepeat);
263 if (retCode == CAMERA_OK) {
264 previewOutput = new(std::nothrow) PreviewOutput(streamRepeat);
265 POWERMGR_SYSEVENT_CAMERA_CONFIG(PREVIEW,
266 profile.GetSize().width,
267 profile.GetSize().height);
268 } else {
269 MEDIA_ERR_LOG("Failed to get stream repeat object from hcamera service! %{public}d", retCode);
270 return ServiceToCameraError(retCode);
271 }
272
273 *pPreviewOutput = previewOutput;
274
275 return CameraErrorCode::SUCCESS;
276 }
277
CreateDeferredPreviewOutput(Profile & profile)278 sptr<PreviewOutput> CameraManager::CreateDeferredPreviewOutput(Profile &profile)
279 {
280 CAMERA_SYNC_TRACE;
281 MEDIA_INFO_LOG("CameraManager::CreateDeferredPreviewOutput called");
282 sptr<PreviewOutput> previewOutput = nullptr;
283 int ret = CreateDeferredPreviewOutput(profile, &previewOutput);
284 if (ret != CameraErrorCode::SUCCESS) {
285 MEDIA_ERR_LOG("Failed to CreateDeferredPreviewOutput with error code:%{public}d", ret);
286 return nullptr;
287 }
288
289 return previewOutput;
290 }
291
CreateDeferredPreviewOutput(Profile & profile,sptr<PreviewOutput> * pPreviewOutput)292 int CameraManager::CreateDeferredPreviewOutput(Profile &profile, sptr<PreviewOutput> *pPreviewOutput)
293 {
294 CAMERA_SYNC_TRACE;
295 sptr<IStreamRepeat> streamRepeat = nullptr;
296 sptr<PreviewOutput> previewOutput = nullptr;
297 int32_t retCode = CAMERA_OK;
298 camera_format_t metaFormat;
299
300 if ((serviceProxy_ == nullptr)) {
301 MEDIA_ERR_LOG("serviceProxy_ is null or profile is null");
302 return CameraErrorCode::INVALID_ARGUMENT;
303 }
304
305 if ((profile.GetCameraFormat() == CAMERA_FORMAT_INVALID) ||
306 (profile.GetSize().width == 0) ||
307 (profile.GetSize().height == 0)) {
308 MEDIA_ERR_LOG("width or height is zero");
309 return CameraErrorCode::INVALID_ARGUMENT;
310 }
311
312 metaFormat = GetCameraMetadataFormat(profile.GetCameraFormat());
313 retCode = serviceProxy_->CreateDeferredPreviewOutput(metaFormat, profile.GetSize().width,
314 profile.GetSize().height, streamRepeat);
315 if (retCode == CAMERA_OK) {
316 previewOutput = new(std::nothrow) PreviewOutput(streamRepeat);
317 POWERMGR_SYSEVENT_CAMERA_CONFIG(PREVIEW,
318 profile.GetSize().width,
319 profile.GetSize().height);
320 } else {
321 MEDIA_ERR_LOG("Failed to get stream repeat object from hcamera service!, %{public}d", retCode);
322 return ServiceToCameraError(retCode);
323 }
324
325 *pPreviewOutput = previewOutput;
326
327 return CameraErrorCode::SUCCESS;
328 }
329
CreatePreviewOutput(const sptr<OHOS::IBufferProducer> & producer,int32_t format)330 sptr<PreviewOutput> CameraManager::CreatePreviewOutput(const sptr<OHOS::IBufferProducer> &producer, int32_t format)
331 {
332 CAMERA_SYNC_TRACE;
333 sptr<PreviewOutput> result = nullptr;
334 return result;
335 }
336
CreateCustomPreviewOutput(sptr<Surface> surface,int32_t width,int32_t height)337 sptr<PreviewOutput> CameraManager::CreateCustomPreviewOutput(sptr<Surface> surface, int32_t width, int32_t height)
338 {
339 CAMERA_SYNC_TRACE;
340 sptr<PreviewOutput> result = nullptr;
341 return result;
342 }
343
CreateMetadataOutput()344 sptr<MetadataOutput> CameraManager::CreateMetadataOutput()
345 {
346 CAMERA_SYNC_TRACE;
347 sptr<MetadataOutput> metadataOutput = nullptr;
348 int ret = CreateMetadataOutput(&metadataOutput);
349 if (ret != CameraErrorCode::SUCCESS) {
350 MEDIA_ERR_LOG("Failed to CreateMetadataOutput with error code:%{public}d", ret);
351 return nullptr;
352 }
353
354 return metadataOutput;
355 }
356
CreateMetadataOutput(sptr<MetadataOutput> * pMetadataOutput)357 int CameraManager::CreateMetadataOutput(sptr<MetadataOutput> *pMetadataOutput)
358 {
359 CAMERA_SYNC_TRACE;
360 sptr<IStreamMetadata> streamMetadata = nullptr;
361 sptr<MetadataOutput> metadataOutput = nullptr;
362 int32_t retCode = CAMERA_OK;
363
364 if (!serviceProxy_) {
365 MEDIA_ERR_LOG("serviceProxy_ is null");
366 return CameraErrorCode::INVALID_ARGUMENT;
367 }
368
369 sptr<IConsumerSurface> surface = IConsumerSurface::Create();
370 if (!surface) {
371 MEDIA_ERR_LOG("Failed to create MetadataOutputSurface");
372 return CameraErrorCode::INVALID_ARGUMENT;
373 }
374
375 // only for face recognize
376 int32_t format = OHOS_CAMERA_FORMAT_YCRCB_420_SP;
377 int32_t width = 1920;
378 int32_t height = 1080;
379 surface->SetDefaultWidthAndHeight(width, height);
380 retCode = serviceProxy_->CreateMetadataOutput(surface->GetProducer(), format, streamMetadata);
381 if (retCode) {
382 MEDIA_ERR_LOG("Failed to get stream metadata object from hcamera service! %{public}d", retCode);
383 return ServiceToCameraError(retCode);
384 }
385
386 metadataOutput = new(std::nothrow) MetadataOutput(surface, streamMetadata);
387 sptr<IBufferConsumerListener> listener = new(std::nothrow) MetadataObjectListener(metadataOutput);
388 SurfaceError ret = surface->RegisterConsumerListener(listener);
389 if (ret != SURFACE_ERROR_OK) {
390 MEDIA_ERR_LOG("MetadataOutputSurface consumer listener registration failed");
391 return CameraErrorCode::SERVICE_FATL_ERROR;
392 }
393 *pMetadataOutput = metadataOutput;
394 return CameraErrorCode::SUCCESS;
395 }
396
CreateVideoOutput(sptr<Surface> & surface)397 sptr<VideoOutput> CameraManager::CreateVideoOutput(sptr<Surface> &surface)
398 {
399 CAMERA_SYNC_TRACE;
400 sptr<VideoOutput> result = nullptr;
401 return result;
402 }
403
CreateVideoOutput(VideoProfile & profile,sptr<Surface> & surface)404 sptr<VideoOutput> CameraManager::CreateVideoOutput(VideoProfile &profile, sptr<Surface> &surface)
405 {
406 CAMERA_SYNC_TRACE;
407 sptr<VideoOutput> videoOutput = nullptr;
408 int ret = CreateVideoOutput(profile, surface, &videoOutput);
409 if (ret != CameraErrorCode::SUCCESS) {
410 MEDIA_ERR_LOG("Failed to CreateVideoOutput with error code:%{public}d", ret);
411 return nullptr;
412 }
413
414 return videoOutput;
415 }
416
CreateVideoOutput(VideoProfile & profile,sptr<Surface> & surface,sptr<VideoOutput> * pVideoOutput)417 int CameraManager::CreateVideoOutput(VideoProfile &profile, sptr<Surface> &surface, sptr<VideoOutput> *pVideoOutput)
418 {
419 CAMERA_SYNC_TRACE;
420 sptr<IStreamRepeat> streamRepeat = nullptr;
421 sptr<VideoOutput> videoOutput = nullptr;
422 int32_t retCode = CAMERA_OK;
423 camera_format_t metaFormat;
424
425 if ((serviceProxy_ == nullptr) || (surface == nullptr)) {
426 MEDIA_ERR_LOG("serviceProxy_ is null or VideoOutputSurface/profile is null");
427 return CameraErrorCode::INVALID_ARGUMENT;
428 }
429
430 if ((profile.GetCameraFormat() == CAMERA_FORMAT_INVALID) ||
431 (profile.GetSize().width == 0) ||
432 (profile.GetSize().height == 0)) {
433 MEDIA_ERR_LOG("width or height is zero");
434 return CameraErrorCode::INVALID_ARGUMENT;
435 }
436
437 // todo: need to set FPS range passed in video profile.
438 metaFormat = GetCameraMetadataFormat(profile.GetCameraFormat());
439 retCode = serviceProxy_->CreateVideoOutput(surface->GetProducer(), metaFormat,
440 profile.GetSize().width, profile.GetSize().height, streamRepeat);
441 if (retCode == CAMERA_OK) {
442 videoOutput = new(std::nothrow) VideoOutput(streamRepeat);
443 std::vector<int32_t> videoFrameRates = profile.GetFrameRates();
444 if (videoFrameRates.size() >= 2) { // vaild frame rate range length is 2
445 videoOutput->SetFrameRateRange(videoFrameRates[0], videoFrameRates[1]);
446 }
447 POWERMGR_SYSEVENT_CAMERA_CONFIG(VIDEO,
448 profile.GetSize().width,
449 profile.GetSize().height);
450 } else {
451 MEDIA_ERR_LOG("Failed to get stream repeat object from hcamera service! %{public}d", retCode);
452 return ServiceToCameraError(retCode);
453 }
454
455 *pVideoOutput = videoOutput;
456
457 return CameraErrorCode::SUCCESS;
458 }
459
Init()460 void CameraManager::Init()
461 {
462 CAMERA_SYNC_TRACE;
463 sptr<IRemoteObject> object = nullptr;
464 cameraMngrCallback_ = nullptr;
465 auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
466 if (samgr == nullptr) {
467 MEDIA_ERR_LOG("Failed to get System ability manager");
468 return;
469 }
470 object = samgr->GetSystemAbility(CAMERA_SERVICE_ID);
471 if (object == nullptr) {
472 MEDIA_ERR_LOG("object is null");
473 return;
474 }
475 serviceProxy_ = iface_cast<ICameraService>(object);
476 if (serviceProxy_ == nullptr) {
477 MEDIA_ERR_LOG("serviceProxy_ is null.");
478 return;
479 } else {
480 cameraSvcCallback_ = new(std::nothrow) CameraStatusServiceCallback(this);
481 SetCameraServiceCallback(cameraSvcCallback_);
482 cameraMuteSvcCallback_ = new(std::nothrow) CameraMuteServiceCallback(this);
483 SetCameraMuteServiceCallback(cameraMuteSvcCallback_);
484 }
485 pid_t pid = 0;
486 deathRecipient_ = new(std::nothrow) CameraDeathRecipient(pid);
487 CHECK_AND_RETURN_LOG(deathRecipient_ != nullptr, "failed to new CameraDeathRecipient.");
488
489 deathRecipient_->SetNotifyCb(std::bind(&CameraManager::CameraServerDied, this, std::placeholders::_1));
490 bool result = object->AddDeathRecipient(deathRecipient_);
491 if (!result) {
492 MEDIA_ERR_LOG("failed to add deathRecipient");
493 return;
494 }
495
496 int32_t ret = CreateListenerObject();
497 CHECK_AND_RETURN_LOG(ret == CAMERA_OK, "failed to new MediaListener, ret = %{public}d", ret);
498 }
499
CameraServerDied(pid_t pid)500 void CameraManager::CameraServerDied(pid_t pid)
501 {
502 MEDIA_ERR_LOG("camera server has died, pid:%{public}d!", pid);
503 if (serviceProxy_ != nullptr) {
504 (void)serviceProxy_->AsObject()->RemoveDeathRecipient(deathRecipient_);
505 serviceProxy_ = nullptr;
506 }
507 listenerStub_ = nullptr;
508 deathRecipient_ = nullptr;
509 }
510
CreateCameraDevice(std::string cameraId,sptr<ICameraDeviceService> * pICameraDeviceService)511 int CameraManager::CreateCameraDevice(std::string cameraId, sptr<ICameraDeviceService> *pICameraDeviceService)
512 {
513 CAMERA_SYNC_TRACE;
514 sptr<ICameraDeviceService> device = nullptr;
515 int32_t retCode = CAMERA_OK;
516
517 if (serviceProxy_ == nullptr || cameraId.empty()) {
518 MEDIA_ERR_LOG("serviceProxy_ is null or CameraID is empty: %{public}s", cameraId.c_str());
519 return CameraErrorCode::INVALID_ARGUMENT;
520 }
521 retCode = serviceProxy_->CreateCameraDevice(cameraId, device);
522 if (retCode != CAMERA_OK) {
523 MEDIA_ERR_LOG("ret value from CreateCameraDevice, %{public}d", retCode);
524 return ServiceToCameraError(retCode);
525 }
526
527 *pICameraDeviceService = device;
528
529 return CameraErrorCode::SUCCESS;
530 }
SetCallback(std::shared_ptr<CameraManagerCallback> callback)531 void CameraManager::SetCallback(std::shared_ptr<CameraManagerCallback> callback)
532 {
533 if (callback == nullptr) {
534 MEDIA_INFO_LOG("Application unregistering the callback");
535 }
536 cameraMngrCallback_ = callback;
537 }
538
GetApplicationCallback()539 std::shared_ptr<CameraManagerCallback> CameraManager::GetApplicationCallback()
540 {
541 MEDIA_INFO_LOG("callback! isExist = %{public}d",
542 cameraMngrCallback_ != nullptr);
543 return cameraMngrCallback_;
544 }
545
GetCameraDeviceFromId(std::string cameraId)546 sptr<CameraDevice> CameraManager::GetCameraDeviceFromId(std::string cameraId)
547 {
548 sptr<CameraDevice> cameraObj = nullptr;
549 for (size_t i = 0; i < cameraObjList.size(); i++) {
550 if (cameraObjList[i]->GetID() == cameraId) {
551 cameraObj = cameraObjList[i];
552 break;
553 }
554 }
555 return cameraObj;
556 }
557
GetInstance()558 sptr<CameraManager> &CameraManager::GetInstance()
559 {
560 if (CameraManager::cameraManager_ == nullptr) {
561 MEDIA_INFO_LOG("Initializing camera manager for first time!");
562 CameraManager::cameraManager_ = new(std::nothrow) CameraManager();
563 }
564 return CameraManager::cameraManager_;
565 }
566
GetCameras()567 std::vector<sptr<CameraInfo>> CameraManager::GetCameras()
568 {
569 CAMERA_SYNC_TRACE;
570 dcameraObjList.clear();
571 return dcameraObjList;
572 }
GetDmDeviceInfo()573 bool CameraManager::GetDmDeviceInfo()
574 {
575 std::vector <DistributedHardware::DmDeviceInfo> deviceInfos;
576 auto &deviceManager = DistributedHardware::DeviceManager::GetInstance();
577 std::shared_ptr<DistributedHardware::DmInitCallback> initCallback = std::make_shared<DeviceInitCallBack>();
578 std::string pkgName = std::to_string(IPCSkeleton::GetCallingPid());
579 const string extraInfo = "";
580 deviceManager.InitDeviceManager(pkgName, initCallback);
581 deviceManager.RegisterDevStateCallback(pkgName, extraInfo, NULL);
582 deviceManager.GetTrustedDeviceList(pkgName, extraInfo, deviceInfos);
583 deviceManager.UnInitDeviceManager(pkgName);
584 int size = static_cast<int>(deviceInfos.size());
585 MEDIA_INFO_LOG("CameraManager::size=%{public}d", size);
586 if (size > 0) {
587 distributedCamInfo_.resize(size);
588 for (int i = 0; i < size; i++) {
589 distributedCamInfo_[i].deviceName = deviceInfos[i].deviceName;
590 distributedCamInfo_[i].deviceTypeId = deviceInfos[i].deviceTypeId;
591 distributedCamInfo_[i].networkId = deviceInfos[i].networkId;
592 }
593 return true;
594 } else {
595 return false;
596 }
597 }
isDistributeCamera(std::string cameraId,dmDeviceInfo & deviceInfo)598 bool CameraManager::isDistributeCamera(std::string cameraId, dmDeviceInfo &deviceInfo)
599 {
600 MEDIA_INFO_LOG("CameraManager::cameraId = %{public}s", cameraId.c_str());
601 for (auto distributedCamInfo : distributedCamInfo_) {
602 if (cameraId.find(distributedCamInfo.networkId) != std::string::npos) {
603 deviceInfo = distributedCamInfo;
604 return true;
605 }
606 }
607 return false;
608 }
GetSupportedCameras()609 std::vector<sptr<CameraDevice>> CameraManager::GetSupportedCameras()
610 {
611 CAMERA_SYNC_TRACE;
612 std::lock_guard<std::mutex> lock(mutex_);
613 std::vector<std::string> cameraIds;
614 std::vector<std::shared_ptr<OHOS::Camera::CameraMetadata>> cameraAbilityList;
615 int32_t retCode = -1;
616 sptr<CameraDevice> cameraObj = nullptr;
617 int32_t index = 0;
618
619 for (unsigned int i = 0; i < cameraObjList.size(); i++) {
620 cameraObjList[i] = nullptr;
621 }
622 cameraObjList.clear();
623 if (serviceProxy_ == nullptr) {
624 MEDIA_ERR_LOG("serviceProxy_ is null, returning empty list!");
625 return cameraObjList;
626 }
627 std::vector<sptr<CameraDevice>> supportedCameras;
628 GetDmDeviceInfo();
629 retCode = serviceProxy_->GetCameras(cameraIds, cameraAbilityList);
630 if (retCode == CAMERA_OK) {
631 for (auto& it : cameraIds) {
632 dmDeviceInfo tempDmDeviceInfo;
633 if (isDistributeCamera(it, tempDmDeviceInfo)) {
634 MEDIA_DEBUG_LOG("CameraManager::it is remoted camera");
635 } else {
636 tempDmDeviceInfo.deviceName = "";
637 tempDmDeviceInfo.deviceTypeId = 0;
638 tempDmDeviceInfo.networkId = "";
639 }
640 cameraObj = new(std::nothrow) CameraDevice(it, cameraAbilityList[index++], tempDmDeviceInfo);
641 supportedCameras.emplace_back(cameraObj);
642 }
643 } else {
644 MEDIA_ERR_LOG("Get camera device failed!, retCode: %{public}d", retCode);
645 }
646
647 ChooseDeFaultCameras(supportedCameras);
648 return cameraObjList;
649 }
650
ChooseDeFaultCameras(std::vector<sptr<CameraDevice>> & supportedCameras)651 void CameraManager::ChooseDeFaultCameras(std::vector<sptr<CameraDevice>>& supportedCameras)
652 {
653 for (auto& camera : supportedCameras) {
654 bool hasDefaultCamera = false;
655 for (auto& defaultCamera : cameraObjList) {
656 if ((camera->GetConnectionType() != CAMERA_CONNECTION_USB_PLUGIN) &&
657 (defaultCamera->GetPosition() == camera->GetPosition()) &&
658 (defaultCamera->GetConnectionType() == camera->GetConnectionType())) {
659 hasDefaultCamera = true;
660 MEDIA_INFO_LOG("ChooseDeFaultCameras alreadly has default camera");
661 } else {
662 MEDIA_INFO_LOG("ChooseDeFaultCameras need add default camera");
663 }
664 }
665 if (!hasDefaultCamera) {
666 cameraObjList.emplace_back(camera);
667 }
668 }
669 }
670
CreateCameraInput(sptr<CameraInfo> & camera)671 sptr<CameraInput> CameraManager::CreateCameraInput(sptr<CameraInfo> &camera)
672 {
673 CAMERA_SYNC_TRACE;
674 sptr<CameraInput> cameraInput = nullptr;
675 return cameraInput;
676 }
677
CreateCameraInput(sptr<CameraDevice> & camera)678 sptr<CameraInput> CameraManager::CreateCameraInput(sptr<CameraDevice> &camera)
679 {
680 CAMERA_SYNC_TRACE;
681 sptr<CameraInput> cameraInput = nullptr;
682 int ret = CreateCameraInput(camera, &cameraInput);
683 if (ret != CameraErrorCode::SUCCESS) {
684 MEDIA_ERR_LOG("Failed to CreateCameraInput with error code:%{public}d", ret);
685 return nullptr;
686 }
687
688 return cameraInput;
689 }
690
CreateCameraInput(sptr<CameraDevice> & camera,sptr<CameraInput> * pCameraInput)691 int CameraManager::CreateCameraInput(sptr<CameraDevice> &camera, sptr<CameraInput> *pCameraInput)
692 {
693 CAMERA_SYNC_TRACE;
694 sptr<CameraInput> cameraInput = nullptr;
695 sptr<ICameraDeviceService> deviceObj = nullptr;
696
697 if (camera != nullptr) {
698 int ret = CreateCameraDevice(camera->GetID(), &deviceObj);
699 if (ret == CameraErrorCode::SUCCESS) {
700 cameraInput = new(std::nothrow) CameraInput(deviceObj, camera);
701 } else {
702 MEDIA_ERR_LOG("Returning null in CreateCameraInput");
703 return ret;
704 }
705 } else {
706 MEDIA_ERR_LOG("Camera object is null");
707 return CameraErrorCode::INVALID_ARGUMENT;
708 }
709
710 *pCameraInput = cameraInput;
711
712 return CameraErrorCode::SUCCESS;
713 }
714
CreateCameraInput(CameraPosition position,CameraType cameraType)715 sptr<CameraInput> CameraManager::CreateCameraInput(CameraPosition position, CameraType cameraType)
716 {
717 CAMERA_SYNC_TRACE;
718 sptr<CameraInput> cameraInput = nullptr;
719 int ret = CreateCameraInput(position, cameraType, &cameraInput);
720 if (ret != CameraErrorCode::SUCCESS) {
721 MEDIA_ERR_LOG("Failed to CreateCameraInput with error code:%{public}d", ret);
722 return nullptr;
723 }
724 return cameraInput;
725 }
726
CreateCameraInput(CameraPosition position,CameraType cameraType,sptr<CameraInput> * pCameraInput)727 int CameraManager::CreateCameraInput(CameraPosition position, CameraType cameraType, sptr<CameraInput> *pCameraInput)
728 {
729 CAMERA_SYNC_TRACE;
730 sptr<CameraInput> cameraInput = nullptr;
731 if (cameraObjList.empty()) {
732 this->GetSupportedCameras();
733 }
734 for (size_t i = 0; i < cameraObjList.size(); i++) {
735 if ((cameraObjList[i]->GetPosition() == position) && (cameraObjList[i]->GetCameraType() == cameraType)) {
736 cameraInput = CreateCameraInput(cameraObjList[i]);
737 break;
738 } else {
739 MEDIA_ERR_LOG("No Camera Device for Camera position:%{public}d, Camera Type:%{public}d",
740 position, cameraType);
741 return CameraErrorCode::SERVICE_FATL_ERROR;
742 }
743 }
744
745 *pCameraInput = cameraInput;
746
747 return CameraErrorCode::SUCCESS;
748 }
749
g_IsCapabilitySupported(std::shared_ptr<OHOS::Camera::CameraMetadata> metadata,camera_metadata_item_t & item,uint32_t metadataTag)750 bool g_IsCapabilitySupported(std::shared_ptr<OHOS::Camera::CameraMetadata> metadata,
751 camera_metadata_item_t &item, uint32_t metadataTag)
752 {
753 bool isSupport = true;
754 int32_t retCode = Camera::FindCameraMetadataItem(metadata->get(), metadataTag, &item);
755 if (retCode != CAM_META_SUCCESS || item.count == 0) {
756 MEDIA_ERR_LOG("Failed get metadata info tag = %{public}d, retCode = %{public}d, count = %{public}d",
757 metadataTag, retCode, item.count);
758 isSupport = false;
759 }
760 return isSupport;
761 }
762
ParseBasicCapability(sptr<CameraOutputCapability> cameraOutputCapability,std::shared_ptr<OHOS::Camera::CameraMetadata> metadata,const camera_metadata_item_t & item)763 void CameraManager::ParseBasicCapability(sptr<CameraOutputCapability> cameraOutputCapability,
764 std::shared_ptr<OHOS::Camera::CameraMetadata> metadata, const camera_metadata_item_t &item)
765 {
766 uint32_t widthOffset = 1;
767 uint32_t heightOffset = 2;
768 const uint8_t UNIT_STEP = 3;
769 const uint8_t FPS_STEP = 2;
770
771 CameraFormat format = CAMERA_FORMAT_INVALID;
772 Size size;
773 for (uint32_t i = 0; i < item.count; i += UNIT_STEP) {
774 auto itr = metaToFwCameraFormat_.find(static_cast<camera_format_t>(item.data.i32[i]));
775 if (itr != metaToFwCameraFormat_.end()) {
776 format = itr->second;
777 } else {
778 format = CAMERA_FORMAT_INVALID;
779 MEDIA_ERR_LOG("format %{public}d is not supported now", item.data.i32[i]);
780 continue;
781 }
782 size.width = item.data.i32[i + widthOffset];
783 size.height = item.data.i32[i + heightOffset];
784 Profile profile = Profile(format, size);
785 if (format == CAMERA_FORMAT_JPEG) {
786 photoProfiles_.push_back(profile);
787 } else {
788 previewProfiles_.push_back(profile);
789 camera_metadata_item_t fpsItem;
790 int ret = Camera::FindCameraMetadataItem(metadata->get(), OHOS_ABILITY_FPS_RANGES, &fpsItem);
791 if (ret != CAM_META_SUCCESS) {
792 continue;
793 }
794 for (uint32_t j = 0; j < (fpsItem.count - 1); j += FPS_STEP) {
795 std::vector<int32_t> fps = {fpsItem.data.i32[j], fpsItem.data.i32[j+1]};
796 VideoProfile vidProfile = VideoProfile(format, size, fps);
797 vidProfiles_.push_back(vidProfile);
798 }
799 }
800 }
801 }
802
ParseExtendCapability(sptr<CameraOutputCapability> cameraOutputCapability,const int32_t modeName,const camera_metadata_item_t & item)803 void CameraManager::ParseExtendCapability(sptr<CameraOutputCapability> cameraOutputCapability,
804 const int32_t modeName, const camera_metadata_item_t &item) __attribute__((no_sanitize("cfi")))
805 {
806 ExtendInfo extendInfo = {};
807 std::shared_ptr<CameraStreamInfoParse> modeStreamParse = std::make_shared<CameraStreamInfoParse>();
808 modeStreamParse->getModeInfo(item.data.i32, item.count, extendInfo); // 解析tag中带的数据信息意义
809 for (uint32_t i = 0; i < extendInfo.modeCount; i++) {
810 if (modeName == extendInfo.modeInfo[i].modeName) {
811 for (uint32_t j = 0; j < extendInfo.modeInfo[i].streamTypeCount; j++) {
812 OutputCapStreamType streamType =
813 static_cast<OutputCapStreamType>(extendInfo.modeInfo[i].streamInfo[j].streamType);
814 CreateProfile4StreamType(streamType, i, j, extendInfo);
815 }
816 break;
817 }
818 }
819 }
820
GetSupportedOutputCapability(sptr<CameraDevice> & camera,int32_t modeName)821 sptr<CameraOutputCapability> CameraManager::GetSupportedOutputCapability(sptr<CameraDevice>& camera,
822 int32_t modeName) __attribute__((no_sanitize("cfi")))
823 {
824 sptr<CameraOutputCapability> cameraOutputCapability = nullptr;
825 cameraOutputCapability = new(std::nothrow) CameraOutputCapability();
826 std::shared_ptr<OHOS::Camera::CameraMetadata> metadata = camera->GetMetadata();
827 camera_metadata_item_t item;
828
829 if (g_IsCapabilitySupported(metadata, item, OHOS_ABILITY_STREAM_AVAILABLE_EXTEND_CONFIGURATIONS)) {
830 ParseExtendCapability(cameraOutputCapability, modeName, item);
831 } else if (g_IsCapabilitySupported(metadata, item, OHOS_ABILITY_STREAM_AVAILABLE_BASIC_CONFIGURATIONS)) {
832 ParseBasicCapability(cameraOutputCapability, metadata, item);
833 } else {
834 MEDIA_ERR_LOG("Failed get stream info");
835 return nullptr;
836 }
837
838 cameraOutputCapability->SetPhotoProfiles(photoProfiles_);
839 MEDIA_DEBUG_LOG("SetPhotoProfiles size = %{public}zu", photoProfiles_.size());
840 cameraOutputCapability->SetPreviewProfiles(previewProfiles_);
841 MEDIA_DEBUG_LOG("SetPreviewProfiles size = %{public}zu", previewProfiles_.size());
842 cameraOutputCapability->SetVideoProfiles(vidProfiles_);
843 MEDIA_DEBUG_LOG("SetVideoProfiles size = %{public}zu", vidProfiles_.size());
844 camera_metadata_item_t metadataItem;
845 vector<MetadataObjectType> objectTypes = {};
846 int32_t ret = Camera::FindCameraMetadataItem(metadata->get(), OHOS_STATISTICS_FACE_DETECT_MODE, &metadataItem);
847 if (ret == CAM_META_SUCCESS) {
848 for (uint32_t index = 0; index < metadataItem.count; index++) {
849 if (metadataItem.data.u8[index] == OHOS_CAMERA_FACE_DETECT_MODE_SIMPLE) {
850 objectTypes.push_back(MetadataObjectType::FACE);
851 }
852 }
853 }
854 cameraOutputCapability->SetSupportedMetadataObjectType(objectTypes);
855 photoProfiles_.clear();
856 previewProfiles_.clear();
857 vidProfiles_.clear();
858 return cameraOutputCapability;
859 }
860
CreateProfile4StreamType(OutputCapStreamType streamType,uint32_t modeIndex,uint32_t streamIndex,ExtendInfo extendInfo)861 void CameraManager::CreateProfile4StreamType(OutputCapStreamType streamType, uint32_t modeIndex,
862 uint32_t streamIndex, ExtendInfo extendInfo) __attribute__((no_sanitize("cfi")))
863 {
864 for (uint32_t k = 0; k < extendInfo.modeInfo[modeIndex].streamInfo[streamIndex].detailInfoCount; k++) {
865 CameraFormat format;
866 auto itr = metaToFwCameraFormat_.find(static_cast<camera_format_t>(
867 extendInfo.modeInfo[modeIndex].streamInfo[streamIndex].detailInfo[k].format));
868 if (itr != metaToFwCameraFormat_.end()) {
869 format = itr->second;
870 } else {
871 format = CAMERA_FORMAT_INVALID;
872 continue;
873 }
874 Size size;
875 size.width = extendInfo.modeInfo[modeIndex].streamInfo[streamIndex].detailInfo[k].width;
876 size.height = extendInfo.modeInfo[modeIndex].streamInfo[streamIndex].detailInfo[k].height;
877 Fps fps;
878 fps.fixedFps = extendInfo.modeInfo[modeIndex].streamInfo[streamIndex].detailInfo[k].fixedFps;
879 fps.minFps = extendInfo.modeInfo[modeIndex].streamInfo[streamIndex].detailInfo[k].minFps;
880 fps.maxFps = extendInfo.modeInfo[modeIndex].streamInfo[streamIndex].detailInfo[k].maxFps;
881 std::vector<uint32_t> abilityId;
882 abilityId = extendInfo.modeInfo[modeIndex].streamInfo[streamIndex].detailInfo[k].abilityId;
883
884 if (streamType == OutputCapStreamType::PREVIEW) {
885 Profile previewProfile = Profile(format, size, fps, abilityId);
886 previewProfiles_.push_back(previewProfile);
887 } else if (streamType == OutputCapStreamType::STILL_CAPTURE) {
888 Profile snapProfile = Profile(format, size, fps, abilityId);
889 photoProfiles_.push_back(snapProfile);
890 } else if (streamType == OutputCapStreamType::VIDEO) {
891 std::vector<int32_t> frameRates = {fps.fixedFps, fps.fixedFps};
892 VideoProfile vidProfile = VideoProfile(format, size, frameRates);
893 vidProfiles_.push_back(vidProfile);
894 }
895 }
896 }
897
SetCameraServiceCallback(sptr<ICameraServiceCallback> & callback)898 void CameraManager::SetCameraServiceCallback(sptr<ICameraServiceCallback>& callback)
899 {
900 int32_t retCode = CAMERA_OK;
901
902 if (serviceProxy_ == nullptr) {
903 MEDIA_ERR_LOG("serviceProxy_ is null");
904 return;
905 }
906 retCode = serviceProxy_->SetCallback(callback);
907 if (retCode != CAMERA_OK) {
908 MEDIA_ERR_LOG("Set service Callback failed, retCode: %{public}d", retCode);
909 }
910 return;
911 }
912
GetCameraMetadataFormat(CameraFormat format)913 camera_format_t CameraManager::GetCameraMetadataFormat(CameraFormat format)
914 {
915 camera_format_t metaFormat = OHOS_CAMERA_FORMAT_YCRCB_420_SP;
916
917 auto itr = fwToMetaCameraFormat_.find(format);
918 if (itr != fwToMetaCameraFormat_.end()) {
919 metaFormat = itr->second;
920 }
921
922 return metaFormat;
923 }
924
OnCameraMute(bool muteMode)925 int32_t CameraMuteServiceCallback::OnCameraMute(bool muteMode)
926 {
927 MEDIA_DEBUG_LOG("muteMode is %{public}d", muteMode);
928 if (camMngr_ == nullptr) {
929 MEDIA_INFO_LOG("camMngr_ is nullptr");
930 return CAMERA_OK;
931 }
932 std::vector<shared_ptr<CameraMuteListener>> cameraMuteListenerList = camMngr_->GetCameraMuteListener();
933 if (cameraMuteListenerList.size() > 0) {
934 for (uint32_t i = 0; i < cameraMuteListenerList.size(); ++i) {
935 cameraMuteListenerList[i]->OnCameraMute(muteMode);
936 }
937 } else {
938 MEDIA_INFO_LOG("OnCameraMute not registered!, Ignore the callback");
939 }
940 return CAMERA_OK;
941 }
942
RegisterCameraMuteListener(std::shared_ptr<CameraMuteListener> listener)943 void CameraManager::RegisterCameraMuteListener(std::shared_ptr<CameraMuteListener> listener)
944 {
945 if (listener == nullptr) {
946 MEDIA_INFO_LOG("unregistering the callback");
947 }
948 cameraMuteListenerList.push_back(listener);
949 }
950
SetCameraMuteServiceCallback(sptr<ICameraMuteServiceCallback> & callback)951 void CameraManager::SetCameraMuteServiceCallback(sptr<ICameraMuteServiceCallback>& callback)
952 {
953 int32_t retCode = CAMERA_OK;
954
955 if (serviceProxy_ == nullptr) {
956 MEDIA_ERR_LOG("serviceProxy_ is null");
957 return;
958 }
959 retCode = serviceProxy_->SetMuteCallback(callback);
960 if (retCode != CAMERA_OK) {
961 MEDIA_ERR_LOG("Set Mute service Callback failed, retCode: %{public}d", retCode);
962 }
963 return;
964 }
965
GetCameraMuteListener()966 std::vector<shared_ptr<CameraMuteListener>> CameraManager::GetCameraMuteListener()
967 {
968 return cameraMuteListenerList;
969 }
970
IsCameraMuteSupported()971 bool CameraManager::IsCameraMuteSupported()
972 {
973 const uint8_t MUTE_ON = 1;
974 bool result = false;
975 if (cameraObjList.empty()) {
976 this->GetSupportedCameras();
977 }
978 for (size_t i = 0; i < cameraObjList.size(); i++) {
979 std::shared_ptr<OHOS::Camera::CameraMetadata> metadata = cameraObjList[i]->GetMetadata();
980 camera_metadata_item_t item;
981 int ret = Camera::FindCameraMetadataItem(metadata->get(), OHOS_ABILITY_MUTE_MODES, &item);
982 if (ret == 0) {
983 MEDIA_INFO_LOG("OHOS_ABILITY_MUTE_MODES is %{public}d", item.data.u8[0]);
984 result = (item.data.u8[0] == MUTE_ON) ? true : false;
985 } else {
986 MEDIA_ERR_LOG("Failed to get stream configuration or Invalid stream "
987 "configuation OHOS_ABILITY_MUTE_MODES ret = %{public}d", ret);
988 }
989 if (result == true) {
990 break;
991 }
992 }
993 return result;
994 }
995
IsCameraMuted()996 bool CameraManager::IsCameraMuted()
997 {
998 int32_t retCode = CAMERA_OK;
999 bool isMuted = false;
1000
1001 if (serviceProxy_ == nullptr) {
1002 MEDIA_ERR_LOG("serviceProxy_ is null");
1003 return isMuted;
1004 }
1005 retCode = serviceProxy_->IsCameraMuted(isMuted);
1006 if (retCode != CAMERA_OK) {
1007 MEDIA_ERR_LOG("IsCameraMuted call failed, retCode: %{public}d", retCode);
1008 }
1009 return isMuted;
1010 }
1011
MuteCamera(bool muteMode)1012 void CameraManager::MuteCamera(bool muteMode)
1013 {
1014 std::lock_guard<std::mutex> lock(mutex_);
1015 int32_t retCode = CAMERA_OK;
1016
1017 if (serviceProxy_ == nullptr) {
1018 MEDIA_ERR_LOG("serviceProxy_ is null");
1019 return;
1020 }
1021 retCode = serviceProxy_->MuteCamera(muteMode);
1022 if (retCode != CAMERA_OK) {
1023 MEDIA_ERR_LOG("MuteCamera call failed, retCode: %{public}d", retCode);
1024 }
1025 }
1026
PrelaunchCamera()1027 int32_t CameraManager::PrelaunchCamera()
1028 {
1029 std::lock_guard<std::mutex> lock(mutex_);
1030 if (serviceProxy_ == nullptr) {
1031 MEDIA_ERR_LOG("CameraManager::PrelaunchCamera serviceProxy_ is null");
1032 return SERVICE_FATL_ERROR;
1033 }
1034 int32_t retCode = serviceProxy_->PrelaunchCamera();
1035 if (retCode != CAMERA_OK) {
1036 MEDIA_ERR_LOG("CameraManager::PrelaunchCamera failed, retCode: %{public}d", retCode);
1037 }
1038 return ServiceToCameraError(retCode);
1039 }
1040
IsPrelaunchSupported(sptr<CameraDevice> camera)1041 bool CameraManager::IsPrelaunchSupported(sptr<CameraDevice> camera)
1042 {
1043 bool isPrelaunch = false;
1044 std::shared_ptr<OHOS::Camera::CameraMetadata> metadata = camera->GetMetadata();
1045 camera_metadata_item_t item;
1046 int ret = Camera::FindCameraMetadataItem(metadata->get(), OHOS_ABILITY_PRELAUNCH_AVAILABLE, &item);
1047 if (ret == 0) {
1048 MEDIA_INFO_LOG("CameraManager::IsPrelaunchSupported() OHOS_ABILITY_PRELAUNCH_AVAILABLE is %{public}d",
1049 item.data.u8[0]);
1050 isPrelaunch = (item.data.u8[0] == 1);
1051 } else {
1052 MEDIA_ERR_LOG("Failed to get OHOS_ABILITY_PRELAUNCH_AVAILABLE ret = %{public}d", ret);
1053 }
1054 return isPrelaunch;
1055 }
1056
SetPrelaunchConfig(std::string cameraId)1057 int32_t CameraManager::SetPrelaunchConfig(std::string cameraId)
1058 {
1059 std::lock_guard<std::mutex> lock(mutex_);
1060 if (serviceProxy_ == nullptr) {
1061 MEDIA_ERR_LOG("CameraManager::SetPrelaunchConfig serviceProxy_ is null");
1062 return SERVICE_FATL_ERROR;
1063 }
1064 int32_t retCode = serviceProxy_->SetPrelaunchConfig(cameraId);
1065 if (retCode != CAMERA_OK) {
1066 MEDIA_ERR_LOG("CameraManager::SetPrelaunchConfig failed, retCode: %{public}d", retCode);
1067 }
1068 return ServiceToCameraError(retCode);
1069 }
1070 } // namespace CameraStandard
1071 } // namespace OHOS
1072