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 #ifndef OHOS_CAMERA_CAMERA_MANAGER_H
17 #define OHOS_CAMERA_CAMERA_MANAGER_H
18 
19 #include <cstdint>
20 #include <iostream>
21 #include <mutex>
22 #include <refbase.h>
23 #include <thread>
24 #include <unordered_map>
25 #include <vector>
26 
27 #include "camera_stream_info_parse.h"
28 #include "deferred_proc_session/deferred_photo_proc_session.h"
29 #include "deferred_proc_session/deferred_video_proc_session.h"
30 #include "hcamera_listener_stub.h"
31 #include "hcamera_service_callback_stub.h"
32 #include "hcamera_service_proxy.h"
33 #include "icamera_device_service.h"
34 #include "input/camera_death_recipient.h"
35 #include "input/camera_device.h"
36 #include "input/camera_info.h"
37 #include "input/camera_input.h"
38 #include "istream_common.h"
39 #include "istream_repeat.h"
40 #include "output/camera_output_capability.h"
41 #include "output/depth_data_output.h"
42 #include "output/metadata_output.h"
43 #include "output/photo_output.h"
44 #include "output/preview_output.h"
45 #include "output/video_output.h"
46 #include "safe_map.h"
47 #include "system_ability_status_change_stub.h"
48 
49 namespace OHOS {
50 namespace CameraStandard {
51 enum CameraDeviceStatus {
52     CAMERA_DEVICE_STATUS_UNAVAILABLE = 0,
53     CAMERA_DEVICE_STATUS_AVAILABLE
54 };
55 
56 enum FlashlightStatus {
57     FLASHLIGHT_STATUS_OFF = 0,
58     FLASHLIGHT_STATUS_ON,
59     FLASHLIGHT_STATUS_UNAVAILABLE
60 };
61 
62 enum TorchMode {
63     TORCH_MODE_OFF = 0,
64     TORCH_MODE_ON,
65     TORCH_MODE_AUTO
66 };
67 
68 struct CameraStatusInfo {
69     sptr<CameraInfo> cameraInfo;
70     sptr<CameraDevice> cameraDevice;
71     CameraStatus cameraStatus;
72     std::string bundleName;
~CameraStatusInfoCameraStatusInfo73     ~CameraStatusInfo()
74     {
75         cameraInfo = nullptr;
76         cameraDevice = nullptr;
77     }
78 };
79 
80 struct TorchStatusInfo {
81     bool isTorchAvailable;
82     bool isTorchActive;
83     float torchLevel;
84 };
85 
86 struct FoldStatusInfo {
87     FoldStatus foldStatus;
88     std::vector<sptr<CameraDevice>> supportedCameras;
89 };
90 
91 typedef enum OutputCapStreamType {
92     PREVIEW = 0,
93     VIDEO_STREAM = 1,
94     STILL_CAPTURE = 2,
95     POST_VIEW = 3,
96     ANALYZE = 4,
97     CUSTOM = 5,
98     DEPTH = 6
99 } OutputCapStreamType;
100 
101 class CameraManagerCallback {
102 public:
103     CameraManagerCallback() = default;
104     virtual ~CameraManagerCallback() = default;
105     virtual void OnCameraStatusChanged(const CameraStatusInfo &cameraStatusInfo) const = 0;
106     virtual void OnFlashlightStatusChanged(const std::string &cameraID, const FlashStatus flashStatus) const = 0;
107 };
108 
109 class CameraMuteListener {
110 public:
111     CameraMuteListener() = default;
112     virtual ~CameraMuteListener() = default;
113     virtual void OnCameraMute(bool muteMode) const = 0;
114 };
115 
116 class TorchListener {
117 public:
118     TorchListener() = default;
119     virtual ~TorchListener() = default;
120     virtual void OnTorchStatusChange(const TorchStatusInfo &torchStatusInfo) const = 0;
121 };
122 
123 class FoldListener {
124 public:
125     FoldListener() = default;
126     virtual ~FoldListener() = default;
127     virtual void OnFoldStatusChanged(const FoldStatusInfo &foldStatusInfo) const = 0;
128 };
129 
130 class CameraServiceSystemAbilityListener : public SystemAbilityStatusChangeStub {
131 public:
132     void OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override;
133     void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override;
134 };
135 class CameraManager : public RefBase {
136 public:
137     virtual ~CameraManager();
138     /**
139      * @brief Get camera manager instance.
140      *
141      * @return Returns pointer to camera manager instance.
142      */
143     static sptr<CameraManager>& GetInstance();
144 
145     /**
146      * @brief Get all available cameras.
147      *
148      * @return Returns vector of cameraDevice of available camera.
149      */
150     std::vector<sptr<CameraDevice>> GetSupportedCameras();
151 
152     /**
153     * @brief Get support modes.
154     *
155     * @return Returns array the mode of current CameraDevice.
156     */
157     std::vector<SceneMode> GetSupportedModes(sptr<CameraDevice>& camera);
158 
159     /**
160      * @brief Get extend output capaility of the mode of the given camera.
161      *
162      * @param Camera device for which extend capability need to be fetched.
163      * @return Returns vector the ability of the mode of cameraDevice of available camera.
164      */
165     sptr<CameraOutputCapability> GetSupportedOutputCapability(sptr<CameraDevice>& camera, int32_t modeName = 0);
166 
167     /**
168      * @brief Create camera input instance with provided camera position and type.
169      *
170      * @param The cameraDevice for which input has to be created.
171      * @return Returns pointer to camera input instance.
172      */
173 
174     sptr<CameraInput> CreateCameraInput(CameraPosition position, CameraType cameraType);
175 
176     /**
177      * @brief Create camera input instance with provided camera position and type.
178      *
179      * @param The cameraDevice for which input has to be created.
180      * @param Returns pointer to camera input instance.
181      * @return Returns error code.
182      */
183     int CreateCameraInput(CameraPosition position, CameraType cameraType, sptr<CameraInput>* pCameraInput);
184 
185     /**
186      * @brief Create camera input instance.
187      *
188      * @param The cameraDevice for which input has to be created.
189      * @return Returns pointer to camera input instance.
190      */
191     sptr<CameraInput> CreateCameraInput(sptr<CameraDevice>& camera);
192 
193     /**
194      * @brief Create camera input instance.
195      *
196      * @param The cameraDevice for which input has to be created.
197      * @param Returns pointer to camera input instance.
198      * @return Returns error code.
199      */
200     int CreateCameraInput(sptr<CameraDevice>& camera, sptr<CameraInput>* pCameraInput);
201 
202     /**
203      * @brief Get all available cameras.
204      *
205      * @return Returns vector of cameraInfo of available camera.
206      */
207     [[deprecated]] std::vector<sptr<CameraInfo>> GetCameras();
208 
209     /**
210      * @brief Create camera input instance.
211      *
212      * @param The cameraInfo for which input has to be created.
213      * @return Returns pointer to camera input instance.
214      */
215     [[deprecated]] sptr<CameraInput> CreateCameraInput(sptr<CameraInfo>& camera);
216 
217     /**
218      * @brief Create capture session.
219      *
220      * @return Returns pointer to capture session.
221      */
222     sptr<CaptureSession> CreateCaptureSession();
223 
224     /**
225     * @brief Create capture session.
226     *
227     * @return Returns pointer to capture session.
228     */
229     sptr<CaptureSession> CreateCaptureSession(SceneMode mode);
230 
231     /**
232      * @brief Create capture session.
233      *
234      * @param Returns pointer to capture session.
235      * @return Returns error code.
236      */
237     int CreateCaptureSession(sptr<CaptureSession>* pCaptureSession);
238 
239     /**
240      * @brief Create photo output instance using surface.
241      *
242      * @param The surface to be used for photo output.
243      * @return Returns pointer to photo output instance.
244      */
245     sptr<PhotoOutput> CreatePhotoOutput(Profile& profile, sptr<IBufferProducer>& surface);
246 
247     /**
248     * @brief Create deferred photo processing session.
249     *
250     * @return Returns pointer to capture session.
251     */
252     static sptr<DeferredPhotoProcSession> CreateDeferredPhotoProcessingSession(int userId,
253         std::shared_ptr<IDeferredPhotoProcSessionCallback> callback);
254 
255     /**
256     * @brief Create deferred photo processing session.
257     *
258     * @param Returns pointer to capture session.
259     * @return Returns error code.
260     */
261     static int CreateDeferredPhotoProcessingSession(int userId,
262         std::shared_ptr<IDeferredPhotoProcSessionCallback> callback,
263         sptr<DeferredPhotoProcSession> *pDeferredPhotoProcSession);
264 
265     /**
266      * @brief Create deferred video processing session.
267      *
268      * @return Returns pointer to capture session.
269      */
270     static sptr<DeferredVideoProcSession> CreateDeferredVideoProcessingSession(int userId,
271         std::shared_ptr<IDeferredVideoProcSessionCallback> callback);
272 
273     /**
274      * @brief Create deferred video processing session.
275      *
276      * @param Returns pointer to capture session.
277      * @return Returns error code.
278      */
279     static int CreateDeferredVideoProcessingSession(int userId,
280         std::shared_ptr<IDeferredVideoProcSessionCallback> callback,
281         sptr<DeferredVideoProcSession> *pDeferredVideoProcSession);
282 
283     /**
284      * @brief Create photo output instance.
285      *
286      * @param profile photo profile.
287      * @param surface photo buffer surface.
288      * @param pPhotoOutput pointer to photo output instance.
289      * @return Returns error code.
290      */
291     int CreatePhotoOutput(Profile& profile, sptr<IBufferProducer>& surface, sptr<PhotoOutput>* pPhotoOutput);
292 
293     /**
294      * @brief Create photo output instance without profile.
295      *
296      * @param surface photo buffer surface.
297      * @param pPhotoOutput pointer to photo output instance.
298      * @return Returns error code.
299      */
300     int CreatePhotoOutputWithoutProfile(sptr<IBufferProducer> surface, sptr<PhotoOutput>* pPhotoOutput);
301 
302     /**
303      * @brief Create photo output instance using surface.
304      *
305      * @param The surface to be used for photo output.
306      * @return Returns pointer to photo output instance.
307      */
308     [[deprecated]] sptr<PhotoOutput> CreatePhotoOutput(sptr<IBufferProducer>& surface);
309 
310     /**
311      * @brief Create photo output instance using IBufferProducer.
312      *
313      * @param The IBufferProducer to be used for photo output.
314      * @param The format to be used for photo capture.
315      * @return Returns pointer to photo output instance.
316      */
317     [[deprecated]] sptr<PhotoOutput> CreatePhotoOutput(const sptr<OHOS::IBufferProducer>& producer, int32_t format);
318 
319     /**
320      * @brief Create video output instance using surface.
321      *
322      * @param The surface to be used for video output.
323      * @return Returns pointer to video output instance.
324      */
325     sptr<VideoOutput> CreateVideoOutput(VideoProfile& profile, sptr<Surface>& surface);
326 
327     /**
328      * @brief Create video output instance using surface.
329      *
330      * @param The surface to be used for video output.
331      * @param Returns pointer to video output instance.
332      * @return Returns error code.
333      */
334     int CreateVideoOutput(VideoProfile& profile, sptr<Surface>& surface, sptr<VideoOutput>* pVideoOutput);
335 
336     /**
337      * @brief Create video output instance without profile.
338      *
339      * @param surface video buffer surface.
340      * @param pVideoOutput pointer to video output instance.
341      * @return Returns error code.
342      */
343     int CreateVideoOutputWithoutProfile(sptr<Surface> surface, sptr<VideoOutput>* pVideoOutput);
344 
345     /**
346      * @brief Create video output instance using surface.
347      *
348      * @param The surface to be used for video output.
349      * @return Returns pointer to video output instance.
350      */
351     [[deprecated]] sptr<VideoOutput> CreateVideoOutput(sptr<Surface>& surface);
352 
353     /**
354      * @brief Create video output instance using IBufferProducer.
355      *
356      * @param The IBufferProducer to be used for video output.
357      * @param The format to be used for video capture.
358      * @return Returns pointer to video output instance.
359      */
360     [[deprecated]] sptr<VideoOutput> CreateVideoOutput(const sptr<OHOS::IBufferProducer>& producer, int32_t format);
361 
362     /**
363      * @brief Create preview output instance using surface.
364      *
365      * @param The surface to be used for preview.
366      * @return Returns pointer to preview output instance.
367      */
368     sptr<PreviewOutput> CreatePreviewOutput(Profile& profile, sptr<Surface> surface);
369 
370     /**
371      * @brief Create preview output instance.
372      *
373      * @param profile preview profile.
374      * @param surface preview buffer surface.
375      * @param pPhotoOutput pointer to photo preview instance.
376      * @return Returns error code.
377      */
378     int CreatePreviewOutput(Profile& profile, sptr<Surface> surface, sptr<PreviewOutput>* pPreviewOutput);
379 
380     /**
381      * @brief Create preview output instance without profile.
382      *
383      * @param surface preview buffer surface.
384      * @param pPhotoOutput pointer to photo preview instance.
385      * @return Returns error code.
386      */
387     int CreatePreviewOutputWithoutProfile(sptr<Surface> surface, sptr<PreviewOutput>* pPreviewOutput);
388 
389     /**
390      * @brief Create preview output instance using surface.
391      *
392      * @param The surface to be used for preview.
393      * @return Returns pointer to preview output instance.
394      */
395     [[deprecated]] sptr<PreviewOutput> CreatePreviewOutput(sptr<Surface> surface);
396 
397     /**
398      * @brief Create preview output instance using IBufferProducer.
399      *
400      * @param The IBufferProducer to be used for preview output.
401      * @param The format to be used for preview.
402      * @return Returns pointer to video preview instance.
403      */
404     [[deprecated]] sptr<PreviewOutput> CreatePreviewOutput(const sptr<OHOS::IBufferProducer>& producer, int32_t format);
405 
406     /**
407      * @brief Create preview output instance using surface.
408      *
409      * @param The surface to be used for preview.
410      * @return Returns pointer to preview output instance.
411      */
412     sptr<PreviewOutput> CreateDeferredPreviewOutput(Profile& profile);
413 
414     /**
415      * @brief Create preview output instance using surface.
416      *
417      * @param The surface to be used for preview.
418      * @param Returns pointer to preview output instance.
419      * @return Returns error code.
420      */
421     int CreateDeferredPreviewOutput(Profile& profile, sptr<PreviewOutput>* pPreviewOutput);
422 
423     /**
424      * @brief Create preview output instance using surface
425      * with custom width and height.
426      *
427      * @param The surface to be used for preview.
428      * @param preview width.
429      * @param preview height.
430      * @return Returns pointer to preview output instance.
431      */
432     [[deprecated]] sptr<PreviewOutput> CreateCustomPreviewOutput(sptr<Surface> surface, int32_t width, int32_t height);
433 
434     /**
435      * @brief Create preview output instance using IBufferProducer
436      * with custom width and height.
437      *
438      * @param The IBufferProducer to be used for preview output.
439      * @param The format to be used for preview.
440      * @param preview width.
441      * @param preview height.
442      * @return Returns pointer to preview output instance.
443      */
444     [[deprecated]] sptr<PreviewOutput> CreateCustomPreviewOutput(
445         const sptr<OHOS::IBufferProducer>& producer, int32_t format, int32_t width, int32_t height);
446 
447     /**
448      * @brief Create metadata output instance.
449      *
450      * @return Returns pointer to metadata output instance.
451      */
452     sptr<MetadataOutput> CreateMetadataOutput();
453 
454     /**
455      * @brief Create metadata output instance.
456      *
457      * @param Returns pointer to metadata output instance.
458      * @return Returns error code.
459      */
460     int CreateMetadataOutput(sptr<MetadataOutput>& pMetadataOutput);
461 
462     /**
463      * @brief Create depth output instance.
464      *
465      * @param depthProfile depth profile.
466      * @param surface depth data buffer surface.
467      * @return pointer to depth data output instance.
468      */
469     sptr<DepthDataOutput> CreateDepthDataOutput(DepthProfile& depthProfile, sptr<IBufferProducer> &surface);
470 
471     /**
472      * @brief Create depth output instance.
473      *
474      * @param depthProfile depth profile.
475      * @param surface depth data buffer surface.
476      * @param pDepthDataOutput pointer to depth data output instance.
477      * @return Returns error code.
478      */
479     int CreateDepthDataOutput(DepthProfile& depthProfile, sptr<IBufferProducer> &surface,
480                               sptr<DepthDataOutput>* pDepthDataOutput);
481 
482     /**
483      * @brief Create metadata output instance.
484      *
485      * @param Returns pointer to metadata output instance.
486      * @return Returns error code.
487      */
488     int CreateMetadataOutput(sptr<MetadataOutput>& pMetadataOutput,
489         std::vector<MetadataObjectType> metadataObjectTypes);
490 
491     /**
492      * @brief Set camera manager callback.
493      *
494      * @param CameraManagerCallback pointer.
495      */
496     void SetCallback(std::shared_ptr<CameraManagerCallback> callback);
497 
498     /**
499      * @brief Get the application callback.
500      *
501      * @return CameraManagerCallback pointer is set by application.
502      */
503     std::shared_ptr<CameraManagerCallback> GetApplicationCallback();
504 
505     /**
506      * @brief Get cameraDevice of specific camera id.
507      *
508      * @param std::string camera id.
509      * @return Returns pointer to cameraDevice of given Id if found else return nullptr.
510      */
511     sptr<CameraDevice> GetCameraDeviceFromId(std::string cameraId);
512 
513     /**
514      * @brief Get cameraInfo of specific camera id.
515      *
516      * @param std::string camera id.
517      * @return Returns pointer to cameraInfo of given Id if found else return nullptr.
518      */
519     [[deprecated]] sptr<CameraInfo> GetCameraInfo(std::string cameraId);
520 
521     /**
522      * @brief Get the support of camera mute mode.
523      *
524      * @return Returns true is supported, false is not supported.
525      */
526     bool IsCameraMuteSupported();
527 
528     /**
529      * @brief Get camera mute mode.
530      *
531      * @return Returns true is in mute, else is not in mute.
532      */
533     bool IsCameraMuted();
534 
535     /**
536      * @brief Mute the camera
537      *
538      * @return.
539      */
540     void MuteCamera(bool muteMode);
541 
542     /**
543      * @brief Mute the camera, and the mute mode can be persisting;
544      *
545      * @param PolicyType policyType.
546      * @param bool muteMode.
547      * @return.
548      */
549     int32_t MuteCameraPersist(PolicyType policyType, bool muteMode);
550 
551     /**
552      * @brief register camera mute listener
553      *
554      * @param CameraMuteListener listener object.
555      * @return.
556      */
557     void RegisterCameraMuteListener(std::shared_ptr<CameraMuteListener> listener);
558 
559     /**
560      * @brief get the camera mute listener
561      *
562      * @return CameraMuteListener point..
563      */
564     std::shared_ptr<CameraMuteListener> GetCameraMuteListener();
565 
566     /**
567      * @brief prelaunch the camera
568      *
569      * @return Server error code.
570      */
571     int32_t PrelaunchCamera();
572 
573     /**
574      * @brief Pre-switch camera
575      *
576      * @return Server error code.
577      */
578     int32_t PreSwitchCamera(const std::string cameraId);
579 
580     /**
581      * @brief set prelaunch config
582      *
583      * @return.
584      */
585     int32_t SetPrelaunchConfig(std::string cameraId, RestoreParamTypeOhos restoreParamType, int activeTime,
586         EffectParam effectParam);
587 
588     /**
589      * @brief Get the support of camera pre launch mode.
590      *
591      * @return Returns true is supported, false is not supported.
592      */
593     bool IsPrelaunchSupported(sptr<CameraDevice> camera);
594 
595     /**
596      * @brief register torch listener
597      *
598      * @param TorchListener listener object.
599      * @return.
600      */
601     void RegisterTorchListener(std::shared_ptr<TorchListener> listener);
602 
603     /**
604      * @brief get the camera mute listener
605      *
606      * @return TorchListener point..
607      */
608     std::shared_ptr<TorchListener> GetTorchListener();
609 
610     /**
611      * @brief register fold status listener
612      *
613      * @param FoldListener listener object.
614      * @return.
615      */
616     void RegisterFoldListener(std::shared_ptr<FoldListener> listener);
617 
618     /**
619      * @brief get the camera fold listener
620      *
621      * @return FoldListener point..
622      */
623     std::shared_ptr<FoldListener> GetFoldListener();
624 
625     SafeMap<std::thread::id, std::shared_ptr<CameraManagerCallback>> GetCameraMngrCallbackMap();
626     SafeMap<std::thread::id, std::shared_ptr<CameraMuteListener>> GetCameraMuteListenerMap();
627     SafeMap<std::thread::id, std::shared_ptr<TorchListener>> GetTorchListenerMap();
628 
629     SafeMap<std::thread::id, std::shared_ptr<FoldListener>> GetFoldListenerMap();
630 
631     /**
632      * @brief check device if support torch
633      *
634      * @return Returns true is supported, false is not supported.
635      */
636     bool IsTorchSupported();
637 
638     /**
639      * @brief check mode if device can support
640      *
641      * @return Returns true is supported, false is not supported.
642      */
643     bool IsTorchModeSupported(TorchMode mode);
644 
645     /**
646      * @brief get current torchmode
647      *
648      * @return Returns current torchmode
649      */
650     TorchMode GetTorchMode();
651 
652     /**
653      * @brief set torch mode
654      *
655      * @return.
656      */
657     int32_t SetTorchMode(TorchMode mode);
658 
659     /**
660      * @brief update torch mode
661      *
662      */
663     void UpdateTorchMode(TorchMode mode);
664 
665     /**
666     * @brief set cameramanager null
667     *
668     */
669     void SetCameraManagerNull();
670 
671     int32_t RequireMemorySize(int32_t memSize);
672 
673     int32_t CreatePreviewOutputStream(
674         sptr<IStreamRepeat>& streamPtr, Profile& profile, const sptr<OHOS::IBufferProducer>& producer);
675 
676     int32_t CreateVideoOutputStream(
677         sptr<IStreamRepeat>& streamPtr, Profile& profile, const sptr<OHOS::IBufferProducer>& producer);
678 
679     int32_t CreatePhotoOutputStream(
680         sptr<IStreamCapture>& streamPtr, Profile& profile, const sptr<OHOS::IBufferProducer>& producer);
681     /**
682     * @brief clear remote stub obj.
683     *
684     */
685     int32_t DestroyStubObj();
686 
687     static const std::string surfaceFormat;
688 
689     void OnCameraServerAlive();
690 
691     virtual bool GetIsFoldable();
692 
693     virtual FoldStatus GetFoldStatus();
694 
ClearCameraDeviceListCache()695     inline void ClearCameraDeviceListCache()
696     {
697         std::lock_guard<std::mutex> lock(cameraDeviceListMutex_);
698         cameraDeviceList_.clear();
699     }
700 
ClearCameraDeviceAbilitySupportMap()701     inline void ClearCameraDeviceAbilitySupportMap()
702     {
703         std::lock_guard<std::mutex> lock(cameraDeviceAbilitySupportMapMutex_);
704         cameraDeviceAbilitySupportMap_.clear();
705     }
706 
707     void GetCameraOutputStatus(int32_t pid, int32_t &status);
708     int CreateCameraDevice(std::string cameraId, sptr<ICameraDeviceService> *pICameraDeviceService);
GetServiceProxy()709     inline sptr<ICameraService> GetServiceProxy()
710     {
711         std::lock_guard<std::mutex> lock(serviceProxyMutex_);
712         return serviceProxyPrivate_;
713     }
714     std::vector<sptr<CameraDevice>> GetSupportedCamerasWithFoldStatus();
715 protected:
716     // Only for UT
CameraManager(sptr<ICameraService> serviceProxy)717     explicit CameraManager(sptr<ICameraService> serviceProxy) : serviceProxyPrivate_(serviceProxy)
718     {
719         // Construct method add mutex lock is not necessary. Ignore g_instanceMutex.
720         CameraManager::g_cameraManager = this;
721     }
722 
723 private:
724     struct ProfilesWrapper {
725         std::vector<Profile> photoProfiles = {};
726         std::vector<Profile> previewProfiles = {};
727         std::vector<VideoProfile> vidProfiles = {};
728     };
729 
730     enum CameraAbilitySupportCacheKey { CAMERA_ABILITY_SUPPORT_TORCH, CAMERA_ABILITY_SUPPORT_MUTE };
731 
732     explicit CameraManager();
733     void InitCameraManager();
734     void SetCameraServiceCallback(sptr<ICameraServiceCallback>& callback);
735     void SetCameraMuteServiceCallback(sptr<ICameraMuteServiceCallback>& callback);
736     void SetTorchServiceCallback(sptr<ITorchServiceCallback>& callback);
737     void SetFoldServiceCallback(sptr<IFoldServiceCallback>& callback);
738     void CreateAndSetCameraServiceCallback();
739     void CreateAndSetCameraMuteServiceCallback();
740     void CreateAndSetTorchServiceCallback();
741     void CreateAndSetFoldServiceCallback();
742     int32_t CreateMetadataOutputInternal(sptr<MetadataOutput>& pMetadataOutput,
743         const std::vector<MetadataObjectType>& metadataObjectTypes = {});
744 
745     sptr<CaptureSession> CreateCaptureSessionImpl(SceneMode mode, sptr<ICaptureSession> session);
746     int32_t CreateListenerObject();
747     void CameraServerDied(pid_t pid);
748     int32_t AddServiceProxyDeathRecipient();
749     void RemoveServiceProxyDeathRecipient();
750 
751     void ParseProfileLevel(
752         ProfilesWrapper& profilesWrapper, const int32_t modeName, const camera_metadata_item_t& item);
753     void CreateProfileLevel4StreamType(ProfilesWrapper& profilesWrapper, int32_t specId, StreamInfo& streamInfo);
754     void GetSupportedMetadataObjectType(
755         common_metadata_header_t* metadata, std::vector<MetadataObjectType>& objectTypes);
756     void CreateProfile4StreamType(ProfilesWrapper& profilesWrapper, OutputCapStreamType streamType, uint32_t modeIndex,
757         uint32_t streamIndex, ExtendInfo extendInfo);
758     static const std::unordered_map<camera_format_t, CameraFormat> metaToFwCameraFormat_;
759     static const std::unordered_map<CameraFormat, camera_format_t> fwToMetaCameraFormat_;
760     static const std::unordered_map<DepthDataAccuracyType, DepthDataAccuracy> metaToFwDepthDataAccuracy_;
761     void ParseExtendCapability(
762         ProfilesWrapper& profilesWrapper, const int32_t modeName, const camera_metadata_item_t& item);
763     void ParseBasicCapability(ProfilesWrapper& profilesWrapper, std::shared_ptr<OHOS::Camera::CameraMetadata> metadata,
764         const camera_metadata_item_t& item);
765     void CreateDepthProfile4StreamType(OutputCapStreamType streamType, uint32_t modeIndex,
766         uint32_t streamIndex, ExtendInfo extendInfo);
767     void CreateProfile4StreamType(OutputCapStreamType streamType, uint32_t modeIndex,
768         uint32_t streamIndex, ExtendInfo extendInfo);
769     void ParseExtendCapability(const int32_t modeName, const camera_metadata_item_t& item);
770     void ParseBasicCapability(
771         std::shared_ptr<OHOS::Camera::CameraMetadata> metadata, const camera_metadata_item_t& item);
772     void ParseDepthCapability(const int32_t modeName, const camera_metadata_item_t& item);
773     void AlignVideoFpsProfile(std::vector<sptr<CameraDevice>>& cameraObjList);
774     void SetProfile(std::vector<sptr<CameraDevice>>& cameraObjList);
775     SceneMode GetFallbackConfigMode(SceneMode profileMode, ProfilesWrapper& profilesWrapper);
776     void ParseCapability(ProfilesWrapper& profilesWrapper, sptr<CameraDevice>& camera, const int32_t modeName,
777         camera_metadata_item_t& item, std::shared_ptr<OHOS::Camera::CameraMetadata> metadata);
778     camera_format_t GetCameraMetadataFormat(CameraFormat format);
779     std::vector<dmDeviceInfo> GetDmDeviceInfo();
780     dmDeviceInfo GetDmDeviceInfo(const std::string& cameraId, const std::vector<dmDeviceInfo>& dmDeviceInfoList);
781     int32_t SetTorchLevel(float level);
782     int32_t ValidCreateOutputStream(Profile& profile, const sptr<OHOS::IBufferProducer>& producer);
783     int32_t SubscribeSystemAbility();
784     int32_t UnSubscribeSystemAbility();
785     int32_t RefreshServiceProxy();
786     std::vector<sptr<CameraDevice>> GetCameraDeviceListFromServer();
787     bool IsSystemApp();
788     vector<CameraFormat> GetSupportPhotoFormat(const int32_t modeName,
789         std::shared_ptr<OHOS::Camera::CameraMetadata> metadata);
790     void FillSupportPhotoFormats(std::vector<Profile>& profiles);
791 
SetServiceProxy(sptr<ICameraService> proxy)792     inline void SetServiceProxy(sptr<ICameraService> proxy)
793     {
794         std::lock_guard<std::mutex> lock(serviceProxyMutex_);
795         serviceProxyPrivate_ = proxy;
796     }
797 
GetCameraDeviceList()798     inline std::vector<sptr<CameraDevice>> GetCameraDeviceList()
799     {
800         std::lock_guard<std::mutex> lock(cameraDeviceListMutex_);
801         if (cameraDeviceList_.empty()) {
802             cameraDeviceList_ = GetCameraDeviceListFromServer();
803         }
804         return cameraDeviceList_;
805     }
806 
IsCameraDeviceListCached()807     inline bool IsCameraDeviceListCached()
808     {
809         std::lock_guard<std::mutex> lock(cameraDeviceListMutex_);
810         return !cameraDeviceList_.empty();
811     }
812 
CacheCameraDeviceAbilitySupportValue(CameraAbilitySupportCacheKey key,bool value)813     inline void CacheCameraDeviceAbilitySupportValue(CameraAbilitySupportCacheKey key, bool value)
814     {
815         std::lock_guard<std::mutex> lock(cameraDeviceAbilitySupportMapMutex_);
816         cameraDeviceAbilitySupportMap_[key] = value;
817     }
818 
GetCameraDeviceAbilitySupportValue(CameraAbilitySupportCacheKey key,bool & value)819     inline bool GetCameraDeviceAbilitySupportValue(CameraAbilitySupportCacheKey key, bool& value)
820     {
821         std::lock_guard<std::mutex> lock(cameraDeviceAbilitySupportMapMutex_);
822         auto it = cameraDeviceAbilitySupportMap_.find(key);
823         if (it == cameraDeviceAbilitySupportMap_.end()) {
824             return false;
825         }
826         value = it->second;
827         return true;
828     }
829 
830     std::mutex cameraDeviceListMutex_;
831     std::vector<sptr<CameraDevice>> cameraDeviceList_ = {};
832 
833     std::mutex cameraDeviceAbilitySupportMapMutex_;
834     std::unordered_map<CameraAbilitySupportCacheKey, bool> cameraDeviceAbilitySupportMap_;
835 
836     std::mutex serviceProxyMutex_;
837     sptr<ICameraService> serviceProxyPrivate_;
838     std::mutex deathRecipientMutex_;
839     sptr<CameraDeathRecipient> deathRecipient_ = nullptr;
840 
841     static sptr<CameraManager> g_cameraManager;
842     static std::mutex g_instanceMutex;
843     sptr<ICameraServiceCallback> cameraSvcCallback_;
844     sptr<ICameraMuteServiceCallback> cameraMuteSvcCallback_;
845     sptr<ITorchServiceCallback> torchSvcCallback_;
846     sptr<IFoldServiceCallback> foldSvcCallback_;
847 
848     SafeMap<std::thread::id, std::shared_ptr<CameraManagerCallback>> cameraMngrCallbackMap_;
849     SafeMap<std::thread::id, std::shared_ptr<CameraMuteListener>> cameraMuteListenerMap_;
850     SafeMap<std::thread::id, std::shared_ptr<TorchListener>> torchListenerMap_;
851     SafeMap<std::thread::id, std::shared_ptr<FoldListener>> foldListenerMap_;
852 
853     std::map<std::string, dmDeviceInfo> distributedCamInfoAndId_;
854 
855     std::map<std::string, std::vector<Profile>> modePhotoProfiles_ = {};
856     std::map<std::string, std::vector<Profile>> modePreviewProfiles_ = {};
857     std::vector<DepthProfile> depthProfiles_ = {};
858 
859     std::vector<CameraFormat> photoFormats_ = {};
860     sptr<CameraInput> cameraInput_;
861     TorchMode torchMode_ = TorchMode::TORCH_MODE_OFF;
862     std::mutex saListenerMuxtex_;
863     sptr<CameraServiceSystemAbilityListener> saListener_ = nullptr;
864     std::string foldScreenType_;
865     bool isSystemApp_ = false;
866 };
867 
868 class CameraMuteServiceCallback : public HCameraMuteServiceCallbackStub {
869 public:
CameraMuteServiceCallback(sptr<CameraManager> cameraManager)870     explicit CameraMuteServiceCallback(sptr<CameraManager> cameraManager) : cameraManager_(cameraManager) {}
871     int32_t OnCameraMute(bool muteMode) override;
872 
873 private:
874     wptr<CameraManager> cameraManager_ = nullptr;
875 };
876 
877 class CameraStatusServiceCallback : public HCameraServiceCallbackStub {
878 public:
CameraStatusServiceCallback(sptr<CameraManager> cameraManager)879     explicit CameraStatusServiceCallback(sptr<CameraManager> cameraManager) : cameraManager_(cameraManager) {}
880     int32_t OnCameraStatusChanged(const std::string& cameraId, const CameraStatus status,
881         const std::string& bundleName) override;
882     int32_t OnFlashlightStatusChanged(const std::string& cameraId, const FlashStatus status) override;
883 
884 private:
885     wptr<CameraManager> cameraManager_ = nullptr;
886 };
887 
888 class TorchServiceCallback : public HTorchServiceCallbackStub {
889 public:
TorchServiceCallback(sptr<CameraManager> cameraManager)890     explicit TorchServiceCallback(sptr<CameraManager> cameraManager) : cameraManager_(cameraManager) {}
891     int32_t OnTorchStatusChange(const TorchStatus status) override;
892 
893 private:
894     wptr<CameraManager> cameraManager_ = nullptr;
895 };
896 
897 class FoldServiceCallback : public HFoldServiceCallbackStub {
898 public:
FoldServiceCallback(sptr<CameraManager> cameraManager)899     explicit FoldServiceCallback(sptr<CameraManager> cameraManager) : cameraManager_(cameraManager) {}
900     int32_t OnFoldStatusChanged(const FoldStatus status) override;
901 
902 private:
903     wptr<CameraManager> cameraManager_ = nullptr;
904 };
905 } // namespace CameraStandard
906 } // namespace OHOS
907 #endif // OHOS_CAMERA_CAMERA_MANAGER_H
908