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 #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: %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: %s", GetAnonyString(dhId_).c_str());
46 accessControl_ = std::make_shared<DCameraSinkAccessControl>();
47 controller_ = std::make_shared<DCameraSinkController>(accessControl_, sinkCallback_);
48 DCameraIndex index("", dhId_);
49 std::vector<DCameraIndex> indexs;
50 indexs.push_back(index);
51 int32_t ret = controller_->Init(indexs);
52 if (ret != DCAMERA_OK) {
53 DHLOGE("init controller failed, dhId: %s, ret: %d", GetAnonyString(dhId_).c_str(), ret);
54 return ret;
55 }
56
57 isInit_ = true;
58 DHLOGI("DCameraSinkDev Init %s success", GetAnonyString(dhId_).c_str());
59 return DCAMERA_OK;
60 }
61
UnInit()62 int32_t DCameraSinkDev::UnInit()
63 {
64 if (controller_ != nullptr) {
65 int32_t ret = controller_->UnInit();
66 if (ret != DCAMERA_OK) {
67 DHLOGE("release controller failed, dhId: %s, ret: %d",
68 GetAnonyString(dhId_).c_str(), ret);
69 }
70 }
71 isInit_ = false;
72 return DCAMERA_OK;
73 }
74
SubscribeLocalHardware(const std::string & parameters)75 int32_t DCameraSinkDev::SubscribeLocalHardware(const std::string& parameters)
76 {
77 DHLOGI("enter");
78 (void)parameters;
79 return DCAMERA_OK;
80 }
81
UnsubscribeLocalHardware()82 int32_t DCameraSinkDev::UnsubscribeLocalHardware()
83 {
84 DHLOGI("enter");
85 return DCAMERA_OK;
86 }
87
StopCapture()88 int32_t DCameraSinkDev::StopCapture()
89 {
90 DHLOGI("StopCapture dhId: %s", GetAnonyString(dhId_).c_str());
91 return controller_->StopCapture();
92 }
93
ChannelNeg(std::string & channelInfo)94 int32_t DCameraSinkDev::ChannelNeg(std::string& channelInfo)
95 {
96 DHLOGI("ChannelNeg dhId: %s", GetAnonyString(dhId_).c_str());
97 if (channelInfo.empty()) {
98 DHLOGE("channelInfo is empty");
99 return DCAMERA_BAD_VALUE;
100 }
101
102 DCameraChannelInfoCmd channelInfoCmd;
103 int32_t ret = channelInfoCmd.Unmarshal(channelInfo);
104 if (ret != DCAMERA_OK) {
105 DHLOGE("channelInfo unmarshal failed, dhId: %s, ret: %d",
106 GetAnonyString(dhId_).c_str(), ret);
107 return ret;
108 }
109 return controller_->ChannelNeg(channelInfoCmd.value_);
110 }
111
GetCameraInfo(std::string & cameraInfo)112 int32_t DCameraSinkDev::GetCameraInfo(std::string& cameraInfo)
113 {
114 DHLOGI("GetCameraInfo dhId: %s", GetAnonyString(dhId_).c_str());
115 std::shared_ptr<DCameraInfo> info = std::make_shared<DCameraInfo>();
116 int32_t ret = controller_->GetCameraInfo(info);
117 if (ret != DCAMERA_OK) {
118 DHLOGE("get state failed, dhId: %s, ret: %d", GetAnonyString(dhId_).c_str(), ret);
119 return ret;
120 }
121
122 DCameraInfoCmd cameraInfoCmd;
123 cameraInfoCmd.type_ = DCAMERA_PROTOCOL_TYPE_MESSAGE;
124 cameraInfoCmd.dhId_ = dhId_;
125 cameraInfoCmd.command_ = DCAMERA_PROTOCOL_CMD_GET_INFO;
126 cameraInfoCmd.value_ = info;
127 ret = cameraInfoCmd.Marshal(cameraInfo);
128 if (ret != DCAMERA_OK) {
129 DHLOGE("cameraInfoCmd marshal failed, dhId: %s, ret: %d", GetAnonyString(dhId_).c_str(), ret);
130 return ret;
131 }
132 DHLOGI("GetCameraInfo %s success", GetAnonyString(dhId_).c_str());
133 return DCAMERA_OK;
134 }
135
OpenChannel(std::string & openInfo)136 int32_t DCameraSinkDev::OpenChannel(std::string& openInfo)
137 {
138 DHLOGI("DCameraSinkDev OpenChannel Begin, dhId: %s", GetAnonyString(dhId_).c_str());
139 if (openInfo.empty()) {
140 DHLOGE("openInfo is empty");
141 return DCAMERA_BAD_VALUE;
142 }
143
144 DCameraOpenInfoCmd cmd;
145 int32_t ret = cmd.Unmarshal(openInfo);
146 if (ret != DCAMERA_OK) {
147 DHLOGE("openInfo unmarshal failed, dhId: %s, ret: %d", GetAnonyString(dhId_).c_str(), ret);
148 return ret;
149 }
150 return controller_->OpenChannel(cmd.value_);
151 }
152
CloseChannel()153 int32_t DCameraSinkDev::CloseChannel()
154 {
155 DHLOGI("CloseChannel dhId: %s", GetAnonyString(dhId_).c_str());
156 return controller_->CloseChannel();
157 }
158
GetDhid()159 std::string DCameraSinkDev::GetDhid()
160 {
161 return GetAnonyString(dhId_);
162 }
163
PauseDistributedHardware(const std::string & networkId)164 int32_t DCameraSinkDev::PauseDistributedHardware(const std::string &networkId)
165 {
166 DHLOGI("Pause distributed hardware dhId: %s", GetAnonyString(dhId_).c_str());
167 if (networkId.empty()) {
168 DHLOGE("networkId is empty");
169 return DCAMERA_BAD_VALUE;
170 }
171 if (controller_ == nullptr) {
172 DHLOGE("controller_ is nullptr.");
173 return DCAMERA_BAD_VALUE;
174 }
175
176 return controller_->PauseDistributedHardware(networkId);
177 }
178
ResumeDistributedHardware(const std::string & networkId)179 int32_t DCameraSinkDev::ResumeDistributedHardware(const std::string &networkId)
180 {
181 DHLOGI("Resume distributed hardware dhId: %s", GetAnonyString(dhId_).c_str());
182 if (networkId.empty()) {
183 DHLOGE("networkId is empty");
184 return DCAMERA_BAD_VALUE;
185 }
186 if (controller_ == nullptr) {
187 DHLOGE("controller_ is nullptr.");
188 return DCAMERA_BAD_VALUE;
189 }
190
191 return controller_->ResumeDistributedHardware(networkId);
192 }
193
StopDistributedHardware(const std::string & networkId)194 int32_t DCameraSinkDev::StopDistributedHardware(const std::string &networkId)
195 {
196 DHLOGI("Stop distributed hardware dhId: %s", GetAnonyString(dhId_).c_str());
197 if (networkId.empty()) {
198 DHLOGE("networkId is empty");
199 return DCAMERA_BAD_VALUE;
200 }
201 if (controller_ == nullptr) {
202 DHLOGE("controller_ is nullptr.");
203 return DCAMERA_BAD_VALUE;
204 }
205
206 return controller_->StopDistributedHardware(networkId);
207 }
208 } // namespace DistributedHardware
209 } // namespace OHOS