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 OHOS_DCAMERA_SOURCE_DEV_H
17 #define OHOS_DCAMERA_SOURCE_DEV_H
18 
19 #include <set>
20 
21 #include "dcamera_index.h"
22 #include "dcamera_source_event.h"
23 #include "dcamera_source_state_machine.h"
24 #include "event_handler.h"
25 #include "icamera_controller.h"
26 #include "icamera_state_listener.h"
27 #include "icamera_input.h"
28 #include "idistributed_camera_source.h"
29 
30 #include "v1_1/id_camera_provider_callback.h"
31 #include "v1_1/id_camera_provider.h"
32 
33 namespace OHOS {
34 namespace DistributedHardware {
35 enum DcameraBusinessState : int32_t {
36     UNKNOWN,
37     IDLE,
38     RUNNING,
39     PAUSING
40 };
41 const uint32_t EVENT_SOURCE_DEV_PROCESS = 0;
42 const uint32_t EVENT_HICOLLIE = 1;
43 const uint32_t EVENT_PROCESS_HDF_NOTIFY = 2;
44 class DCameraSourceDev : public std::enable_shared_from_this<DCameraSourceDev> {
45 public:
46     explicit DCameraSourceDev(std::string devId, std::string dhId, std::shared_ptr<ICameraStateListener>& stateLisener);
47     virtual ~DCameraSourceDev();
48 
49     int32_t InitDCameraSourceDev();
50     int32_t RegisterDistributedHardware(const std::string& devId, const std::string& dhId, const std::string& reqId,
51         const EnableParam& param);
52     int32_t UnRegisterDistributedHardware(const std::string devId, const std::string dhId, const std::string reqId);
53     int32_t DCameraNotify(std::string& eventStr);
54 
55     int32_t OpenSession(DCameraIndex& camIndex);
56     int32_t CloseSession(DCameraIndex& camIndex);
57     int32_t ConfigCameraStreams(const std::vector<std::shared_ptr<DCStreamInfo>>& streamInfos);
58     int32_t ReleaseCameraStreams(const std::vector<int>& streamIds);
59     int32_t StartCameraCapture(const std::vector<std::shared_ptr<DCCaptureInfo>>& captureInfos);
60     int32_t StopCameraCapture(const std::vector<int>& streamIds);
61     int32_t UpdateCameraSettings(const std::vector<std::shared_ptr<DCameraSettings>>& settings);
62     int32_t ProcessHDFEvent(const DCameraHDFEvent& event);
63 
64     int32_t GetStateInfo();
65     std::string GetVersion();
66     int32_t OnChannelConnectedEvent();
67     int32_t OnChannelDisconnectedEvent();
68     int32_t PostHicollieEvent();
69     void SetHicollieFlag(bool flag);
70     bool GetHicollieFlag();
71     int32_t GetFullCaps();
72 
73     class DCameraSourceDevEventHandler : public AppExecFwk::EventHandler {
74         public:
75             DCameraSourceDevEventHandler(const std::shared_ptr<AppExecFwk::EventRunner> &runner,
76                 std::shared_ptr<DCameraSourceDev> srcDevPtr);
77             ~DCameraSourceDevEventHandler() override = default;
78             void ProcessEvent(const AppExecFwk::InnerEvent::Pointer &event) override;
79         private:
80             std::weak_ptr<DCameraSourceDev> srcDevPtrWPtr_;
81     };
82 
83 public:
84     virtual int32_t Register(std::shared_ptr<DCameraRegistParam>& param);
85     virtual int32_t UnRegister(std::shared_ptr<DCameraRegistParam>& param);
86     virtual int32_t OpenCamera();
87     virtual int32_t CloseCamera();
88     virtual int32_t ConfigStreams(std::vector<std::shared_ptr<DCStreamInfo>>& streamInfos);
89     virtual int32_t ReleaseStreams(std::vector<int>& streamIds, bool& isAllRelease);
90     virtual int32_t ReleaseAllStreams();
91     virtual int32_t StartCapture(std::vector<std::shared_ptr<DCCaptureInfo>>& captureInfos);
92     virtual int32_t StopCapture(std::vector<int>& streamIds, bool& isAllStop);
93     virtual int32_t StopAllCapture();
94     virtual int32_t UpdateSettings(std::vector<std::shared_ptr<DCameraSettings>>& settings);
95     virtual int32_t CameraEventNotify(std::shared_ptr<DCameraEvent>& events);
96 
97 private:
98     using DCameraNotifyFunc = void (DCameraSourceDev::*)(DCAMERA_EVENT eventType, DCameraSourceEvent& event,
99         int32_t result);
100 
101     void NotifyResult(DCAMERA_EVENT eventType, DCameraSourceEvent& event, int32_t result);
102     void NotifyRegisterResult(DCAMERA_EVENT eventType, DCameraSourceEvent& event, int32_t result);
103     void NotifyHalResult(DCAMERA_EVENT eventType, DCameraSourceEvent& event, int32_t result);
104     void HitraceAndHisyseventImpl(std::vector<std::shared_ptr<DCCaptureInfo>>& captureInfos);
105     int32_t ParseEnableParam(std::shared_ptr<DCameraRegistParam>& param, std::string& ability);
106     void DoProcessData(const AppExecFwk::InnerEvent::Pointer &event);
107     void DoProcesHDFEvent(const AppExecFwk::InnerEvent::Pointer &event);
108     void DoHicollieProcess();
109 
110 private:
111     std::string devId_;
112     std::string dhId_;
113     std::string version_;
114     std::set<DCameraIndex> actualDevInfo_;
115     std::shared_ptr<ICameraStateListener> stateListener_;
116     std::shared_ptr<DCameraSourceDevEventHandler> srcDevEventHandler_ = nullptr;
117     std::shared_ptr<DCameraSourceStateMachine> stateMachine_;
118     std::shared_ptr<ICameraController> controller_;
119     std::shared_ptr<ICameraInput> input_;
120     std::atomic<bool> hicollieFlag_ = true;
121     sptr<IDCameraProviderCallback> hdiCallback_;
122     int32_t sceneMode_ = 0;
123 
124     std::map<uint32_t, DCameraNotifyFunc> memberFuncMap_;
125     std::map<uint32_t, DCameraEventResult> eventResultMap_;
126 };
127 } // namespace DistributedHardware
128 } // namespace OHOS
129 #endif // OHOS_DCAMERA_SOURCE_DEV_H
130