1 /*
2  * Copyright (c) 2021 - 2023 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 CAMERA_HOST_CAMERA_HOST_VDI_IMPL_H
17 #define CAMERA_HOST_CAMERA_HOST_VDI_IMPL_H
18 #include <mutex>
19 #include <map>
20 #include "v1_0/icamera_host_vdi.h"
21 #include "v1_0/icamera_device_vdi.h"
22 #include "camera_device_vdi_impl.h"
23 #include "utils.h"
24 
25 namespace OHOS::Camera {
26 using namespace OHOS::VDI::Camera::V1_0;
27 
28 class CameraHostVdiImpl : public ICameraHostVdi {
29 public:
30     VdiCamRetCode Init();
31     int32_t SetCallback(const sptr<ICameraHostVdiCallback> &callbackObj) override;
32     int32_t GetCameraIds(std::vector<std::string> &cameraIds) override;
33     int32_t GetCameraAbility(const std::string &cameraId,
34         std::vector<uint8_t> &cameraAbility) override;
35     int32_t OpenCamera(const std::string &cameraId, const sptr<ICameraDeviceVdiCallback> &callbackObj,
36         sptr<ICameraDeviceVdi> &device) override;
37     int32_t SetFlashlight(const std::string &cameraId, bool isEnable) override;
38 
39 public:
40     CameraHostVdiImpl();
41     virtual ~CameraHostVdiImpl();
42     CameraHostVdiImpl(const CameraHostVdiImpl &other) = delete;
43     CameraHostVdiImpl(CameraHostVdiImpl &&other) = delete;
44     CameraHostVdiImpl &operator=(const CameraHostVdiImpl &other) = delete;
45     CameraHostVdiImpl &operator=(CameraHostVdiImpl &&other) = delete;
46 
47 private:
48     RetCode CameraPowerUp(const std::string &cameraId,
49         const std::vector<std::string> &phyCameraIds);
50     void CameraPowerDown(const std::vector<std::string> &phyCameraIds);
51     RetCode CameraIdInvalid(const std::string &cameraId);
52     RetCode SetFlashlight(const std::vector<std::string> &phyCameraIds,
53         bool isEnable, VdiFlashlightStatus &flashlightStatus);
54     void OnCameraStatus(CameraId cameraId, VdiCameraStatus status, const std::shared_ptr<CameraAbility> ability);
55 
56     int32_t CloseAllCameras() override;
57 
58 private:
59     // key: cameraId, value: CameraDevice
60     using CameraDeviceMap = std::map<std::string, std::shared_ptr<CameraDeviceVdiImpl>>;
61     std::mutex mtx;
62     CameraDeviceMap cameraDeviceMap_;
63     OHOS::sptr<ICameraHostVdiCallback> cameraHostCallback_;
64     // to keep remote object OHOS::sptr<ICameraDeviceVdi> alive
65     std::map<std::string, OHOS::sptr<ICameraDeviceVdi>> deviceBackup_ = {};
66 };
67 } // end namespace OHOS::Camera
68 #endif // CAMERA_HOST_CAMERA_HOST_VDI_IMPL_H
69