1 /*
2  * Copyright (c) 2021-2024 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 DISTRIBUTED_CAMERA_HOST_H
17 #define DISTRIBUTED_CAMERA_HOST_H
18 
19 #include "dcamera_base.h"
20 #include "dcamera_device.h"
21 
22 #include "v1_0/icamera_host.h"
23 #include "v1_3/icamera_host.h"
24 #include "v1_0/icamera_device_callback.h"
25 #include "v1_0/icamera_host_callback.h"
26 #include "v1_2/icamera_host_callback.h"
27 #include "v1_1/dcamera_types.h"
28 #include "v1_0/types.h"
29 #include "v1_2/types.h"
30 #include "constants.h"
31 #include "iremote_object.h"
32 
33 namespace OHOS {
34 namespace DistributedHardware {
35 using namespace OHOS::HDI::DistributedCamera::V1_1;
36 using HDI::Camera::V1_3::ICameraDevice;
37 using HDI::Camera::V1_0::ICameraDeviceCallback;
38 using HDI::Camera::V1_0::FlashlightStatus;
39 using HDI::Camera::V1_0::CameraStatus;
40 using HDI::Camera::V1_3::ICameraHost;
41 using HDI::Camera::V1_1::PrelaunchConfig;
42 class DCameraHost : public OHOS::HDI::Camera::V1_3::ICameraHost {
43 const uint32_t ABILITYINFO_MAX_LENGTH = 50 * 1024 * 1024;
44 const uint32_t ID_MAX_SIZE = 2 * DEVID_MAX_LENGTH;
45 const size_t MAX_DCAMERAS_NUMBER = 32;
46 public:
47     DCameraHost() = default;
48     ~DCameraHost() override = default;
49     DCameraHost(const DCameraHost &other) = delete;
50     DCameraHost(DCameraHost &&other) = delete;
51     DCameraHost& operator=(const DCameraHost &other) = delete;
52     DCameraHost& operator=(DCameraHost &&other) = delete;
53 
54 public:
55     static OHOS::sptr<DCameraHost> GetInstance();
56 
57     int32_t OpenCamera_V1_3(const std::string &cameraId, const sptr<ICameraDeviceCallback> &callbackObj,
58         sptr<ICameraDevice> &device) override;
59     int32_t OpenSecureCamera(const std::string &cameraId, const sptr<ICameraDeviceCallback> &callbackObj,
60         sptr<ICameraDevice> &device) override;
61     int32_t GetResourceCost(const std::string &cameraId,
62         OHOS::HDI::Camera::V1_3::CameraDeviceResourceCost &resourceCost) override;
63 
64     int32_t OpenCamera_V1_2(const std::string &cameraId, const sptr<ICameraDeviceCallback> &callbackObj,
65         sptr<HDI::Camera::V1_2::ICameraDevice> &device) override;
66     int32_t NotifyDeviceStateChangeInfo(int notifyType, int deviceState) override;
67     int32_t SetCallback_V1_2(const sptr<HDI::Camera::V1_2::ICameraHostCallback> &callbackObj) override;
68     int32_t SetFlashlight_V1_2(float level) override;
69     int32_t PreCameraSwitch(const std::string &cameraId) override;
70     int32_t PrelaunchWithOpMode(const PrelaunchConfig &config, int32_t operationMode) override;
71 
72     int32_t OpenCamera_V1_1(const std::string &cameraId, const sptr<ICameraDeviceCallback> &callbackObj,
73         sptr<HDI::Camera::V1_1::ICameraDevice> &device) override;
74     int32_t Prelaunch(const PrelaunchConfig &config) override;
75 
76     int32_t SetCallback(const sptr<HDI::Camera::V1_0::ICameraHostCallback> &callbackObj) override;
77     int32_t GetCameraIds(std::vector<std::string> &cameraIds) override;
78     int32_t GetCameraAbility(const std::string &cameraId, std::vector<uint8_t> &cameraAbility) override;
79     int32_t OpenCamera(const std::string &cameraId, const sptr<ICameraDeviceCallback> &callbackObj,
80         sptr<HDI::Camera::V1_0::ICameraDevice> &device) override;
81     int32_t SetFlashlight(const std::string &cameraId, bool isEnable) override;
82     int32_t GetCameraAbilityFromDev(const std::string &cameraId,
83         std::shared_ptr<CameraAbility> &cameraAbility);
84     DCamRetCode AddDCameraDevice(const DHBase &dhBase, const std::string &sinkAbilityInfo,
85         const std::string &sourceCodecInfo, const sptr<IDCameraProviderCallback> &callback);
86     DCamRetCode AddDeviceParamCheck(const DHBase &dhBase, const std::string &sinkAbilityInfo,
87         const std::string &sourceCodecInfo, const sptr<IDCameraProviderCallback> &callback);
88     DCamRetCode RemoveDCameraDevice(const DHBase &dhBase);
89     OHOS::sptr<DCameraDevice> GetDCameraDeviceByDHBase(const DHBase &dhBase);
90     void NotifyDCameraStatus(const DHBase &dhBase, int32_t result);
91 
92 private:
93     bool IsCameraIdInvalid(const std::string &cameraId);
94     std::string GetCameraIdByDHBase(const DHBase &dhBase);
95     size_t GetCamDevNum();
96 
97     template<typename Callback, typename Device>
98     int32_t OpenCameraImpl(const std::string &cameraId, const Callback &callbackObj, Device &device);
99 
100 private:
101     class AutoRelease {
102     public:
AutoRelease()103         AutoRelease() {}
~AutoRelease()104         ~AutoRelease()
105         {
106             if (DCameraHost::instance_ != nullptr) {
107                 DCameraHost::instance_ = nullptr;
108             }
109         }
110     };
111     class DCameraHostRecipient : public IRemoteObject::DeathRecipient {
112     public:
113         void OnRemoteDied(const wptr<IRemoteObject> &remote) override;
114     };
115     sptr<DCameraHostRecipient> dCameraHostRecipient_;
116     static AutoRelease autoRelease_;
117     static OHOS::sptr<DCameraHost> instance_;
118 
119     OHOS::sptr<HDI::Camera::V1_0::ICameraHostCallback> dCameraHostCallback_;
120     OHOS::sptr<HDI::Camera::V1_2::ICameraHostCallback> dCameraHostCallback_V1_2_;
121     std::map<std::string, OHOS::sptr<DCameraDevice>> dCameraDeviceMap_;
122     std::mutex deviceMapLock_;
123 };
124 } // namespace DistributedHardware
125 } // namespace OHOS
126 #endif // DISTRIBUTED_CAMERA_HOST_H