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_DEVICE_H
17 #define DISTRIBUTED_CAMERA_DEVICE_H
18 
19 #include <atomic>
20 #include <vector>
21 #include <string>
22 #include <sstream>
23 #include "dmetadata_processor.h"
24 #include "dstream_operator.h"
25 
26 #include "v1_0/icamera_device.h"
27 #include "v1_3/icamera_device.h"
28 #include "v1_0/icamera_device_callback.h"
29 #include "v1_1/id_camera_provider_callback.h"
30 #include "v1_0/types.h"
31 #include "v1_1/types.h"
32 #include "v1_0/istream_operator_callback.h"
33 #include "v1_2/istream_operator_callback.h"
34 #include "v1_3/istream_operator_callback.h"
35 #include "v1_3/istream_operator.h"
36 
37 namespace OHOS {
38 namespace DistributedHardware {
39 using HDI::Camera::V1_3::IStreamOperator;
40 using HDI::Camera::V1_0::ICameraDeviceCallback;
41 class DCameraDevice : public HDI::Camera::V1_3::ICameraDevice {
42 public:
43     DCameraDevice(const DHBase &dhBase, const std::string& sinkAbilityInfo, const std::string& sourceCodecInfo);
44     DCameraDevice() = default;
45     virtual ~DCameraDevice() = default;
46     DCameraDevice(const DCameraDevice &other) = delete;
47     DCameraDevice(DCameraDevice &&other) = delete;
48     DCameraDevice& operator=(const DCameraDevice &other) = delete;
49     DCameraDevice& operator=(DCameraDevice &&other) = delete;
50 
51 public:
52     int32_t GetStreamOperator_V1_3(const sptr<HDI::Camera::V1_3::IStreamOperatorCallback> &callbackObj,
53         sptr<HDI::Camera::V1_3::IStreamOperator> &streamOperator) override;
54     int32_t GetSecureCameraSeq(uint64_t &seqId) override;
55 
56     int32_t GetStreamOperator_V1_2(const sptr<HDI::Camera::V1_2::IStreamOperatorCallback> &callbackObj,
57         sptr<HDI::Camera::V1_2::IStreamOperator> &streamOperator) override;
58     int32_t GetStatus(const std::vector<uint8_t> &metaIn, std::vector<uint8_t> &metaOut) override;
59     int32_t Reset() override;
60 
61     int32_t GetStreamOperator_V1_1(const sptr<HDI::Camera::V1_0::IStreamOperatorCallback> &callbackObj,
62         sptr<HDI::Camera::V1_1::IStreamOperator> &streamOperator) override;
63     int32_t GetDefaultSettings(std::vector<uint8_t> &settings) override;
64 
65     int32_t GetStreamOperator(const sptr<HDI::Camera::V1_0::IStreamOperatorCallback> &callbackObj,
66         sptr<HDI::Camera::V1_0::IStreamOperator> &streamOperator) override;
67     int32_t UpdateSettings(const std::vector<uint8_t> &settings) override;
68     int32_t SetResultMode(ResultCallbackMode mode) override;
69     int32_t GetEnabledResults(std::vector<int32_t> &results) override;
70     int32_t EnableResult(const std::vector<int32_t> &results) override;
71     int32_t DisableResult(const std::vector<int32_t> &results) override;
72     int32_t Close() override;
73 
74     int32_t GetSettings(std::vector<uint8_t> &settings);
75     CamRetCode OpenDCamera(const OHOS::sptr<ICameraDeviceCallback> &callback);
76     CamRetCode GetDCameraAbility(std::shared_ptr<CameraAbility> &ability);
77     DCamRetCode AcquireBuffer(int streamId, DCameraBuffer &buffer);
78     DCamRetCode ShutterBuffer(int streamId, const DCameraBuffer &buffer);
79     DCamRetCode OnSettingsResult(const std::shared_ptr<DCameraSettings> &result);
80     DCamRetCode Notify(const std::shared_ptr<DCameraHDFEvent> &event);
81     void SetProviderCallback(const OHOS::sptr<IDCameraProviderCallback> &callback);
82     OHOS::sptr<IDCameraProviderCallback> GetProviderCallback();
83     std::string GetDCameraId();
84     bool IsOpened();
85     void SetDcameraAbility(const std::string& sinkAbilityInfo);
86 
87 private:
88     void Init(const std::string &sinkAbilityInfo);
89     DCamRetCode CreateDStreamOperator();
90     std::string GenerateCameraId(const DHBase &dhBase);
91     void NotifyStartCaptureError();
92     void NotifyCameraError(const ErrorType type);
93     void IsOpenSessFailedState(bool state);
94     CamRetCode TriggerGetFullCaps();
95     void SetRefreshFlag(bool flag);
96     bool GetRefreshFlag();
97 private:
98     bool isOpened_;
99     std::string dCameraId_;
100     DHBase dhBase_;
101     std::string dCameraAbilityInfo_;
102     std::string sourceCodecInfo_;
103     OHOS::sptr<ICameraDeviceCallback> dCameraDeviceCallback_;
104     OHOS::sptr<IDCameraProviderCallback> dCameraProviderCallback_;
105     OHOS::sptr<DStreamOperator> dCameraStreamOperator_;
106     std::shared_ptr<DMetadataProcessor> dMetadataProcessor_;
107 
108     std::mutex openSesslock_;
109     std::condition_variable openSessCV_;
110     bool isOpenSessFailed_ = false;
111     std::mutex isOpenSessFailedlock_;
112     std::mutex getFullLock_;
113     std::condition_variable getFullWaitCond_;
114     std::atomic<bool> refreshFlag_ = false;
115 };
116 } // namespace DistributedHardware
117 } // namespace OHOS
118 #endif // DISTRIBUTED_CAMERA_DEVICE_H
119