• 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 #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 CreatePreviewOutputStream(
672         sptr<IStreamRepeat>& streamPtr, Profile& profile, const sptr<OHOS::IBufferProducer>& producer);
673 
674     int32_t CreateVideoOutputStream(
675         sptr<IStreamRepeat>& streamPtr, Profile& profile, const sptr<OHOS::IBufferProducer>& producer);
676 
677     int32_t CreatePhotoOutputStream(
678         sptr<IStreamCapture>& streamPtr, Profile& profile, const sptr<OHOS::IBufferProducer>& producer);
679     /**
680     * @brief clear remote stub obj.
681     *
682     */
683     int32_t DestroyStubObj();
684 
685     static const std::string surfaceFormat;
686 
687     void OnCameraServerAlive();
688 
689     virtual bool GetIsFoldable();
690 
691     virtual FoldStatus GetFoldStatus();
692 
ClearCameraDeviceListCache()693     inline void ClearCameraDeviceListCache()
694     {
695         std::lock_guard<std::mutex> lock(cameraDeviceListMutex_);
696         cameraDeviceList_.clear();
697     }
698 
ClearCameraDeviceAbilitySupportMap()699     inline void ClearCameraDeviceAbilitySupportMap()
700     {
701         std::lock_guard<std::mutex> lock(cameraDeviceAbilitySupportMapMutex_);
702         cameraDeviceAbilitySupportMap_.clear();
703     }
704 
705     void GetCameraOutputStatus(int32_t pid, int32_t &status);
706     int CreateCameraDevice(std::string cameraId, sptr<ICameraDeviceService> *pICameraDeviceService);
GetServiceProxy()707     inline sptr<ICameraService> GetServiceProxy()
708     {
709         std::lock_guard<std::mutex> lock(serviceProxyMutex_);
710         return serviceProxyPrivate_;
711     }
712 protected:
713     // Only for UT
CameraManager(sptr<ICameraService> serviceProxy)714     explicit CameraManager(sptr<ICameraService> serviceProxy) : serviceProxyPrivate_(serviceProxy)
715     {
716         // Construct method add mutex lock is not necessary. Ignore g_instanceMutex.
717         CameraManager::g_cameraManager = this;
718     }
719 
720 private:
721     struct ProfilesWrapper {
722         std::vector<Profile> photoProfiles = {};
723         std::vector<Profile> previewProfiles = {};
724         std::vector<VideoProfile> vidProfiles = {};
725     };
726 
727     enum CameraAbilitySupportCacheKey { CAMERA_ABILITY_SUPPORT_TORCH, CAMERA_ABILITY_SUPPORT_MUTE };
728 
729     explicit CameraManager();
730     void InitCameraManager();
731     void SetCameraServiceCallback(sptr<ICameraServiceCallback>& callback);
732     void SetCameraMuteServiceCallback(sptr<ICameraMuteServiceCallback>& callback);
733     void SetTorchServiceCallback(sptr<ITorchServiceCallback>& callback);
734     void SetFoldServiceCallback(sptr<IFoldServiceCallback>& callback);
735     void CreateAndSetCameraServiceCallback();
736     void CreateAndSetCameraMuteServiceCallback();
737     void CreateAndSetTorchServiceCallback();
738     void CreateAndSetFoldServiceCallback();
739     int32_t CreateMetadataOutputInternal(sptr<MetadataOutput>& pMetadataOutput,
740         const std::vector<MetadataObjectType>& metadataObjectTypes = {});
741 
742     sptr<CaptureSession> CreateCaptureSessionImpl(SceneMode mode, sptr<ICaptureSession> session);
743     int32_t CreateListenerObject();
744     void CameraServerDied(pid_t pid);
745     int32_t AddServiceProxyDeathRecipient();
746     void RemoveServiceProxyDeathRecipient();
747 
748     void ParseProfileLevel(
749         ProfilesWrapper& profilesWrapper, const int32_t modeName, const camera_metadata_item_t& item);
750     void CreateProfileLevel4StreamType(ProfilesWrapper& profilesWrapper, int32_t specId, StreamInfo& streamInfo);
751     void GetSupportedMetadataObjectType(
752         common_metadata_header_t* metadata, std::vector<MetadataObjectType>& objectTypes);
753     void CreateProfile4StreamType(ProfilesWrapper& profilesWrapper, OutputCapStreamType streamType, uint32_t modeIndex,
754         uint32_t streamIndex, ExtendInfo extendInfo);
755     static const std::unordered_map<camera_format_t, CameraFormat> metaToFwCameraFormat_;
756     static const std::unordered_map<CameraFormat, camera_format_t> fwToMetaCameraFormat_;
757     static const std::unordered_map<DepthDataAccuracyType, DepthDataAccuracy> metaToFwDepthDataAccuracy_;
758     void ParseExtendCapability(
759         ProfilesWrapper& profilesWrapper, const int32_t modeName, const camera_metadata_item_t& item);
760     void ParseBasicCapability(ProfilesWrapper& profilesWrapper, std::shared_ptr<OHOS::Camera::CameraMetadata> metadata,
761         const camera_metadata_item_t& item);
762     void CreateDepthProfile4StreamType(OutputCapStreamType streamType, uint32_t modeIndex,
763         uint32_t streamIndex, ExtendInfo extendInfo);
764     void CreateProfile4StreamType(OutputCapStreamType streamType, uint32_t modeIndex,
765         uint32_t streamIndex, ExtendInfo extendInfo);
766     void ParseExtendCapability(const int32_t modeName, const camera_metadata_item_t& item);
767     void ParseBasicCapability(
768         std::shared_ptr<OHOS::Camera::CameraMetadata> metadata, const camera_metadata_item_t& item);
769     void ParseDepthCapability(const int32_t modeName, const camera_metadata_item_t& item);
770     void AlignVideoFpsProfile(std::vector<sptr<CameraDevice>>& cameraObjList);
771     void SetProfile(std::vector<sptr<CameraDevice>>& cameraObjList);
772     SceneMode GetFallbackConfigMode(SceneMode profileMode, ProfilesWrapper& profilesWrapper);
773     void ParseCapability(ProfilesWrapper& profilesWrapper, sptr<CameraDevice>& camera, const int32_t modeName,
774         camera_metadata_item_t& item, std::shared_ptr<OHOS::Camera::CameraMetadata> metadata);
775     camera_format_t GetCameraMetadataFormat(CameraFormat format);
776     std::vector<dmDeviceInfo> GetDmDeviceInfo();
777     dmDeviceInfo GetDmDeviceInfo(const std::string& cameraId, const std::vector<dmDeviceInfo>& dmDeviceInfoList);
778     int32_t SetTorchLevel(float level);
779     int32_t ValidCreateOutputStream(Profile& profile, const sptr<OHOS::IBufferProducer>& producer);
780     int32_t SubscribeSystemAbility();
781     int32_t UnSubscribeSystemAbility();
782     int32_t RefreshServiceProxy();
783     std::vector<sptr<CameraDevice>> GetCameraDeviceListFromServer();
784     bool IsSystemApp();
785     vector<CameraFormat> GetSupportPhotoFormat(const int32_t modeName,
786         std::shared_ptr<OHOS::Camera::CameraMetadata> metadata);
787     void FillSupportPhotoFormats(std::vector<Profile>& profiles);
788 
SetServiceProxy(sptr<ICameraService> proxy)789     inline void SetServiceProxy(sptr<ICameraService> proxy)
790     {
791         std::lock_guard<std::mutex> lock(serviceProxyMutex_);
792         serviceProxyPrivate_ = proxy;
793     }
794 
GetCameraDeviceList()795     inline std::vector<sptr<CameraDevice>> GetCameraDeviceList()
796     {
797         std::lock_guard<std::mutex> lock(cameraDeviceListMutex_);
798         if (cameraDeviceList_.empty()) {
799             cameraDeviceList_ = GetCameraDeviceListFromServer();
800         }
801         return cameraDeviceList_;
802     }
803 
IsCameraDeviceListCached()804     inline bool IsCameraDeviceListCached()
805     {
806         std::lock_guard<std::mutex> lock(cameraDeviceListMutex_);
807         return !cameraDeviceList_.empty();
808     }
809 
CacheCameraDeviceAbilitySupportValue(CameraAbilitySupportCacheKey key,bool value)810     inline void CacheCameraDeviceAbilitySupportValue(CameraAbilitySupportCacheKey key, bool value)
811     {
812         std::lock_guard<std::mutex> lock(cameraDeviceAbilitySupportMapMutex_);
813         cameraDeviceAbilitySupportMap_[key] = value;
814     }
815 
GetCameraDeviceAbilitySupportValue(CameraAbilitySupportCacheKey key,bool & value)816     inline bool GetCameraDeviceAbilitySupportValue(CameraAbilitySupportCacheKey key, bool& value)
817     {
818         std::lock_guard<std::mutex> lock(cameraDeviceAbilitySupportMapMutex_);
819         auto it = cameraDeviceAbilitySupportMap_.find(key);
820         if (it == cameraDeviceAbilitySupportMap_.end()) {
821             return false;
822         }
823         value = it->second;
824         return true;
825     }
826 
827     std::mutex cameraDeviceListMutex_;
828     std::vector<sptr<CameraDevice>> cameraDeviceList_ = {};
829 
830     std::mutex cameraDeviceAbilitySupportMapMutex_;
831     std::unordered_map<CameraAbilitySupportCacheKey, bool> cameraDeviceAbilitySupportMap_;
832 
833     std::mutex serviceProxyMutex_;
834     sptr<ICameraService> serviceProxyPrivate_;
835     std::mutex deathRecipientMutex_;
836     sptr<CameraDeathRecipient> deathRecipient_ = nullptr;
837 
838     static sptr<CameraManager> g_cameraManager;
839     static std::mutex g_instanceMutex;
840     sptr<ICameraServiceCallback> cameraSvcCallback_;
841     sptr<ICameraMuteServiceCallback> cameraMuteSvcCallback_;
842     sptr<ITorchServiceCallback> torchSvcCallback_;
843     sptr<IFoldServiceCallback> foldSvcCallback_;
844 
845     SafeMap<std::thread::id, std::shared_ptr<CameraManagerCallback>> cameraMngrCallbackMap_;
846     SafeMap<std::thread::id, std::shared_ptr<CameraMuteListener>> cameraMuteListenerMap_;
847     SafeMap<std::thread::id, std::shared_ptr<TorchListener>> torchListenerMap_;
848     SafeMap<std::thread::id, std::shared_ptr<FoldListener>> foldListenerMap_;
849 
850     std::map<std::string, dmDeviceInfo> distributedCamInfoAndId_;
851 
852     std::map<std::string, std::vector<Profile>> modePhotoProfiles_ = {};
853     std::map<std::string, std::vector<Profile>> modePreviewProfiles_ = {};
854     std::vector<DepthProfile> depthProfiles_ = {};
855 
856     std::vector<CameraFormat> photoFormats_ = {};
857     sptr<CameraInput> cameraInput_;
858     TorchMode torchMode_ = TorchMode::TORCH_MODE_OFF;
859     sptr<CameraServiceSystemAbilityListener> saListener_ = nullptr;
860     std::string foldScreenType_;
861     bool isSystemApp_ = false;
862 };
863 
864 class CameraMuteServiceCallback : public HCameraMuteServiceCallbackStub {
865 public:
CameraMuteServiceCallback(sptr<CameraManager> cameraManager)866     explicit CameraMuteServiceCallback(sptr<CameraManager> cameraManager) : cameraManager_(cameraManager) {}
867     int32_t OnCameraMute(bool muteMode) override;
868 
869 private:
870     wptr<CameraManager> cameraManager_ = nullptr;
871 };
872 
873 class CameraStatusServiceCallback : public HCameraServiceCallbackStub {
874 public:
CameraStatusServiceCallback(sptr<CameraManager> cameraManager)875     explicit CameraStatusServiceCallback(sptr<CameraManager> cameraManager) : cameraManager_(cameraManager) {}
876     int32_t OnCameraStatusChanged(const std::string& cameraId, const CameraStatus status,
877         const std::string& bundleName) override;
878     int32_t OnFlashlightStatusChanged(const std::string& cameraId, const FlashStatus status) override;
879 
880 private:
881     wptr<CameraManager> cameraManager_ = nullptr;
882 };
883 
884 class TorchServiceCallback : public HTorchServiceCallbackStub {
885 public:
TorchServiceCallback(sptr<CameraManager> cameraManager)886     explicit TorchServiceCallback(sptr<CameraManager> cameraManager) : cameraManager_(cameraManager) {}
887     int32_t OnTorchStatusChange(const TorchStatus status) override;
888 
889 private:
890     wptr<CameraManager> cameraManager_ = nullptr;
891 };
892 
893 class FoldServiceCallback : public HFoldServiceCallbackStub {
894 public:
FoldServiceCallback(sptr<CameraManager> cameraManager)895     explicit FoldServiceCallback(sptr<CameraManager> cameraManager) : cameraManager_(cameraManager) {}
896     int32_t OnFoldStatusChanged(const FoldStatus status) override;
897 
898 private:
899     wptr<CameraManager> cameraManager_ = nullptr;
900 };
901 } // namespace CameraStandard
902 } // namespace OHOS
903 #endif // OHOS_CAMERA_CAMERA_MANAGER_H
904