• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_INPUT_H
17 #define OHOS_DCAMERA_SOURCE_INPUT_H
18 
19 #include "icamera_channel.h"
20 #include "icamera_channel_listener.h"
21 #include "icamera_input.h"
22 #include "icamera_source_data_process.h"
23 
24 #include "dcamera_source_dev.h"
25 #include "distributed_camera_errno.h"
26 
27 namespace OHOS {
28 namespace DistributedHardware {
29 class DCameraSourceInput : public ICameraInput,
30     public std::enable_shared_from_this<DCameraSourceInput> {
31 public:
32     DCameraSourceInput(std::string devId, std::string dhId, std::shared_ptr<DCameraSourceDev>& camDev);
33     ~DCameraSourceInput() override;
34 
35     int32_t ConfigStreams(std::vector<std::shared_ptr<DCStreamInfo>>& streamInfos) override;
36     int32_t ReleaseStreams(std::vector<int>& streamIds, bool& isAllRelease) override;
37     int32_t ReleaseAllStreams() override;
38     int32_t StartCapture(std::vector<std::shared_ptr<DCCaptureInfo>>& captureInfos) override;
39     int32_t StopCapture(std::vector<int>& streamIds, bool& isAllStop) override;
40     int32_t StopAllCapture() override;
41     int32_t OpenChannel(std::vector<DCameraIndex>& indexs) override;
42     int32_t CloseChannel() override;
43     int32_t Init() override;
44     int32_t UnInit() override;
45     int32_t UpdateSettings(std::vector<std::shared_ptr<DCameraSettings>>& settings) override;
46 
47     void OnSessionState(DCStreamType streamType, int32_t state);
48     void OnSessionError(DCStreamType streamType, int32_t eventType, int32_t eventReason, std::string detail);
49     void OnDataReceived(DCStreamType streamType, std::vector<std::shared_ptr<DataBuffer>>& buffers);
50 
51 private:
52     void FinshFrameAsyncTrace(DCStreamType streamType);
53     void PostChannelDisconnectedEvent();
54     int32_t EstablishContinuousFrameSession(std::vector<DCameraIndex>& indexs);
55     int32_t EstablishSnapshotFrameSession(std::vector<DCameraIndex>& indexs);
56     int32_t WaitForOpenChannelCompletion(bool needWait);
57 
58 private:
59     std::map<DCStreamType, std::shared_ptr<ICameraChannel>> channels_;
60     std::map<DCStreamType, std::shared_ptr<ICameraChannelListener>> listeners_;
61     std::map<DCStreamType, std::shared_ptr<ICameraSourceDataProcess>> dataProcess_;
62     std::map<DCStreamType, DCameraChannelState> channelState_;
63     std::string devId_;
64     std::string dhId_;
65     std::weak_ptr<DCameraSourceDev> camDev_;
66 
67     bool isInit = false;
68 
69     static constexpr uint8_t CHANNEL_REL_SECONDS = 5;
70     std::atomic<bool> isChannelConnected_ = false;
71     std::mutex channelMtx_;
72     std::condition_variable channelCond_;
73 
74     static constexpr std::chrono::seconds TIMEOUT_3_SEC = std::chrono::seconds(3);
75     std::atomic<bool> isOpenChannelFinished_ = false;
76     std::mutex isOpenChannelMtx_;
77     std::condition_variable isOpenChannelCond_;
78     std::atomic<int32_t> continuousFrameResult_ = DCAMERA_OK;
79     std::atomic<int32_t> snapshotFrameResult_ = DCAMERA_OK;
80     std::shared_ptr<AppExecFwk::EventRunner> runner_ = AppExecFwk::EventRunner::Create(true);
81     std::shared_ptr<AppExecFwk::EventHandler> handler_ = std::make_shared<AppExecFwk::EventHandler>(runner_);
82 };
83 } // namespace DistributedHardware
84 } // namespace OHOS
85 #endif // OHOS_DCAMERA_SOURCE_INPUT_H
86