• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2025 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 #include "dcamera_sink_dev.h"
17 
18 #include "anonymous_string.h"
19 #include "dcamera_channel_info_cmd.h"
20 #include "dcamera_info_cmd.h"
21 #include "dcamera_protocol.h"
22 #include "dcamera_sink_access_control.h"
23 #include "dcamera_sink_controller.h"
24 #include "distributed_camera_errno.h"
25 #include "distributed_hardware_log.h"
26 
27 namespace OHOS {
28 namespace DistributedHardware {
DCameraSinkDev(const std::string & dhId,const sptr<IDCameraSinkCallback> & sinkCallback)29 DCameraSinkDev::DCameraSinkDev(const std::string& dhId, const sptr<IDCameraSinkCallback> &sinkCallback)
30     : dhId_(dhId), sinkCallback_(sinkCallback)
31 {
32     DHLOGI("DCameraSinkDev Constructor dhId: %{public}s", GetAnonyString(dhId_).c_str());
33     isInit_ = false;
34 }
35 
~DCameraSinkDev()36 DCameraSinkDev::~DCameraSinkDev()
37 {
38     if (isInit_) {
39         UnInit();
40     }
41 }
42 
Init()43 int32_t DCameraSinkDev::Init()
44 {
45     DHLOGI("Init dhId: %{public}s", GetAnonyString(dhId_).c_str());
46     accessControl_ = std::make_shared<DCameraSinkAccessControl>();
47     controller_ = std::make_shared<DCameraSinkController>(accessControl_, sinkCallback_);
48     controller_->SetTokenId(tokenId_);
49     DCameraIndex index("", dhId_);
50     std::vector<DCameraIndex> indexs;
51     indexs.push_back(index);
52     int32_t ret = controller_->Init(indexs);
53     if (ret != DCAMERA_OK) {
54         DHLOGE("init controller failed, dhId: %{public}s, ret: %{public}d", GetAnonyString(dhId_).c_str(), ret);
55         return ret;
56     }
57 
58     isInit_ = true;
59     DHLOGI("DCameraSinkDev Init %{public}s success", GetAnonyString(dhId_).c_str());
60     return DCAMERA_OK;
61 }
62 
UnInit()63 int32_t DCameraSinkDev::UnInit()
64 {
65     if (controller_ != nullptr) {
66         int32_t ret = controller_->UnInit();
67         if (ret != DCAMERA_OK) {
68             DHLOGE("release controller failed, dhId: %{public}s, ret: %{public}d",
69                    GetAnonyString(dhId_).c_str(), ret);
70         }
71     }
72     isInit_ = false;
73     return DCAMERA_OK;
74 }
75 
SubscribeLocalHardware(const std::string & parameters)76 int32_t DCameraSinkDev::SubscribeLocalHardware(const std::string& parameters)
77 {
78     DHLOGI("enter");
79     (void)parameters;
80     return DCAMERA_OK;
81 }
82 
UnsubscribeLocalHardware()83 int32_t DCameraSinkDev::UnsubscribeLocalHardware()
84 {
85     DHLOGI("enter");
86     return DCAMERA_OK;
87 }
88 
StopCapture()89 int32_t DCameraSinkDev::StopCapture()
90 {
91     DHLOGI("StopCapture dhId: %{public}s", GetAnonyString(dhId_).c_str());
92     CHECK_AND_RETURN_RET_LOG(controller_ == nullptr, DCAMERA_BAD_VALUE, "controller_ is null.");
93     return controller_->StopCapture();
94 }
95 
ChannelNeg(std::string & channelInfo)96 int32_t DCameraSinkDev::ChannelNeg(std::string& channelInfo)
97 {
98     DHLOGI("ChannelNeg dhId: %{public}s", GetAnonyString(dhId_).c_str());
99     if (channelInfo.empty()) {
100         DHLOGE("channelInfo is empty");
101         return DCAMERA_BAD_VALUE;
102     }
103 
104     DCameraChannelInfoCmd channelInfoCmd;
105     int32_t ret = channelInfoCmd.Unmarshal(channelInfo);
106     if (ret != DCAMERA_OK) {
107         DHLOGE("channelInfo unmarshal failed, dhId: %{public}s, ret: %{public}d",
108                GetAnonyString(dhId_).c_str(), ret);
109         return ret;
110     }
111     CHECK_AND_RETURN_RET_LOG(controller_ == nullptr, DCAMERA_BAD_VALUE, "controller_ is null.");
112     return controller_->ChannelNeg(channelInfoCmd.value_);
113 }
114 
GetCameraInfo(std::string & cameraInfo)115 int32_t DCameraSinkDev::GetCameraInfo(std::string& cameraInfo)
116 {
117     DHLOGI("GetCameraInfo dhId: %{public}s", GetAnonyString(dhId_).c_str());
118     std::shared_ptr<DCameraInfo> info = std::make_shared<DCameraInfo>();
119     CHECK_AND_RETURN_RET_LOG(controller_ == nullptr, DCAMERA_BAD_VALUE, "controller_ is null.");
120     int32_t ret = controller_->GetCameraInfo(info);
121     if (ret != DCAMERA_OK) {
122         DHLOGE("get state failed, dhId: %{public}s, ret: %{public}d", GetAnonyString(dhId_).c_str(), ret);
123         return ret;
124     }
125 
126     DCameraInfoCmd cameraInfoCmd;
127     cameraInfoCmd.type_ = DCAMERA_PROTOCOL_TYPE_MESSAGE;
128     cameraInfoCmd.dhId_ = dhId_;
129     cameraInfoCmd.command_ = DCAMERA_PROTOCOL_CMD_GET_INFO;
130     cameraInfoCmd.value_ = info;
131     ret = cameraInfoCmd.Marshal(cameraInfo);
132     if (ret != DCAMERA_OK) {
133         DHLOGE("cameraInfoCmd marshal failed, dhId: %{public}s, ret: %{public}d", GetAnonyString(dhId_).c_str(), ret);
134         return ret;
135     }
136     DHLOGI("GetCameraInfo %{public}s success", GetAnonyString(dhId_).c_str());
137     return DCAMERA_OK;
138 }
139 
OpenChannel(std::string & openInfo)140 int32_t DCameraSinkDev::OpenChannel(std::string& openInfo)
141 {
142     DHLOGI("DCameraSinkDev OpenChannel Begin, dhId: %{public}s", GetAnonyString(dhId_).c_str());
143     if (openInfo.empty()) {
144         DHLOGE("openInfo is empty");
145         return DCAMERA_BAD_VALUE;
146     }
147 
148     DCameraOpenInfoCmd cmd;
149     int32_t ret = cmd.Unmarshal(openInfo);
150     if (ret != DCAMERA_OK) {
151         DHLOGE("openInfo unmarshal failed, dhId: %{public}s, ret: %{public}d", GetAnonyString(dhId_).c_str(), ret);
152         return ret;
153     }
154     CHECK_AND_RETURN_RET_LOG(controller_ == nullptr, DCAMERA_BAD_VALUE, "controller_ is null.");
155     return controller_->OpenChannel(cmd.value_);
156 }
157 
CloseChannel()158 int32_t DCameraSinkDev::CloseChannel()
159 {
160     DHLOGI("CloseChannel dhId: %{public}s", GetAnonyString(dhId_).c_str());
161     CHECK_AND_RETURN_RET_LOG(controller_ == nullptr, DCAMERA_BAD_VALUE, "controller_ is null.");
162     return controller_->CloseChannel();
163 }
164 
GetDhid()165 std::string DCameraSinkDev::GetDhid()
166 {
167     return GetAnonyString(dhId_);
168 }
169 
PauseDistributedHardware(const std::string & networkId)170 int32_t DCameraSinkDev::PauseDistributedHardware(const std::string &networkId)
171 {
172     DHLOGI("Pause distributed hardware dhId: %{public}s", GetAnonyString(dhId_).c_str());
173     if (networkId.empty()) {
174         DHLOGE("networkId is empty");
175         return DCAMERA_BAD_VALUE;
176     }
177     if (controller_ == nullptr) {
178         DHLOGE("controller_ is nullptr.");
179         return DCAMERA_BAD_VALUE;
180     }
181 
182     return controller_->PauseDistributedHardware(networkId);
183 }
184 
ResumeDistributedHardware(const std::string & networkId)185 int32_t DCameraSinkDev::ResumeDistributedHardware(const std::string &networkId)
186 {
187     DHLOGI("Resume distributed hardware dhId: %{public}s", GetAnonyString(dhId_).c_str());
188     if (networkId.empty()) {
189         DHLOGE("networkId is empty");
190         return DCAMERA_BAD_VALUE;
191     }
192     if (controller_ == nullptr) {
193         DHLOGE("controller_ is nullptr.");
194         return DCAMERA_BAD_VALUE;
195     }
196 
197     return controller_->ResumeDistributedHardware(networkId);
198 }
199 
StopDistributedHardware(const std::string & networkId)200 int32_t DCameraSinkDev::StopDistributedHardware(const std::string &networkId)
201 {
202     DHLOGI("Stop distributed hardware dhId: %{public}s", GetAnonyString(dhId_).c_str());
203     if (networkId.empty()) {
204         DHLOGE("networkId is empty");
205         return DCAMERA_BAD_VALUE;
206     }
207     if (controller_ == nullptr) {
208         DHLOGE("controller_ is nullptr.");
209         return DCAMERA_BAD_VALUE;
210     }
211 
212     return controller_->StopDistributedHardware(networkId);
213 }
214 
SetTokenId(uint64_t token)215 void DCameraSinkDev::SetTokenId(uint64_t token)
216 {
217     tokenId_ = token;
218 }
219 } // namespace DistributedHardware
220 } // namespace OHOS