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