1 /*
2 * Copyright (c) 2021 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)29 DCameraSinkDev::DCameraSinkDev(const std::string& dhId) : dhId_(dhId)
30 {
31 DHLOGI("DCameraSinkDev Constructor dhId: %s", GetAnonyString(dhId_).c_str());
32 isInit_ = false;
33 }
34
~DCameraSinkDev()35 DCameraSinkDev::~DCameraSinkDev()
36 {
37 if (isInit_) {
38 UnInit();
39 }
40 }
41
Init()42 int32_t DCameraSinkDev::Init()
43 {
44 DHLOGI("DCameraSinkDev::Init dhId: %s", GetAnonyString(dhId_).c_str());
45 accessControl_ = std::make_shared<DCameraSinkAccessControl>();
46 controller_ = std::make_shared<DCameraSinkController>(accessControl_);
47 DCameraIndex index("", dhId_);
48 std::vector<DCameraIndex> indexs;
49 indexs.push_back(index);
50 int32_t ret = controller_->Init(indexs);
51 if (ret != DCAMERA_OK) {
52 DHLOGE("DCameraSinkDev::Init init controller failed, dhId: %s, ret: %d", GetAnonyString(dhId_).c_str(), ret);
53 return ret;
54 }
55
56 isInit_ = true;
57 DHLOGI("DCameraSinkDev::Init %s success", GetAnonyString(dhId_).c_str());
58 return DCAMERA_OK;
59 }
60
UnInit()61 int32_t DCameraSinkDev::UnInit()
62 {
63 if (controller_ != nullptr) {
64 int32_t ret = controller_->UnInit();
65 if (ret != DCAMERA_OK) {
66 DHLOGE("DCameraSinkDev::UnInit release controller failed, dhId: %s, ret: %d",
67 GetAnonyString(dhId_).c_str(), ret);
68 }
69 }
70 isInit_ = false;
71 return DCAMERA_OK;
72 }
73
SubscribeLocalHardware(const std::string & parameters)74 int32_t DCameraSinkDev::SubscribeLocalHardware(const std::string& parameters)
75 {
76 DHLOGI("DCameraSinkDev::SubscribeLocalHardware");
77 return DCAMERA_OK;
78 }
79
UnsubscribeLocalHardware()80 int32_t DCameraSinkDev::UnsubscribeLocalHardware()
81 {
82 DHLOGI("DCameraSinkDev::UnsubscribeLocalHardware");
83 return DCAMERA_OK;
84 }
85
StopCapture()86 int32_t DCameraSinkDev::StopCapture()
87 {
88 DHLOGI("DCameraSinkDev::StopCapture dhId: %s", GetAnonyString(dhId_).c_str());
89 return controller_->StopCapture();
90 }
91
ChannelNeg(std::string & channelInfo)92 int32_t DCameraSinkDev::ChannelNeg(std::string& channelInfo)
93 {
94 DHLOGI("DCameraSinkDev::ChannelNeg dhId: %s", GetAnonyString(dhId_).c_str());
95 if (channelInfo.empty()) {
96 DHLOGE("DCameraSinkDev::ChannelNeg channelInfo is empty");
97 return DCAMERA_BAD_VALUE;
98 }
99
100 DCameraChannelInfoCmd channelInfoCmd;
101 int32_t ret = channelInfoCmd.Unmarshal(channelInfo);
102 if (ret != DCAMERA_OK) {
103 DHLOGE("DCameraSinkDev::ChannelNeg channelInfo unmarshal failed, dhId: %s, ret: %d",
104 GetAnonyString(dhId_).c_str(), ret);
105 return ret;
106 }
107 return controller_->ChannelNeg(channelInfoCmd.value_);
108 }
109
GetCameraInfo(std::string & cameraInfo)110 int32_t DCameraSinkDev::GetCameraInfo(std::string& cameraInfo)
111 {
112 DHLOGI("DCameraSinkDev::GetCameraInfo dhId: %s", GetAnonyString(dhId_).c_str());
113 std::shared_ptr<DCameraInfo> info = std::make_shared<DCameraInfo>();
114 int32_t ret = controller_->GetCameraInfo(info);
115 if (ret != DCAMERA_OK) {
116 DHLOGE("DCameraSinkDev::GetCameraInfo get state failed, dhId: %s, ret: %d",
117 GetAnonyString(dhId_).c_str(), ret);
118 return ret;
119 }
120
121 DCameraInfoCmd cameraInfoCmd;
122 cameraInfoCmd.type_ = DCAMERA_PROTOCOL_TYPE_MESSAGE;
123 cameraInfoCmd.dhId_ = dhId_;
124 cameraInfoCmd.command_ = DCAMERA_PROTOCOL_CMD_GET_INFO;
125 cameraInfoCmd.value_ = info;
126 ret = cameraInfoCmd.Marshal(cameraInfo);
127 if (ret != DCAMERA_OK) {
128 DHLOGE("DCameraSinkDev::GetCameraInfo cameraInfoCmd marshal failed, dhId: %s, ret: %d",
129 GetAnonyString(dhId_).c_str(), ret);
130 return ret;
131 }
132 DHLOGI("DCameraSinkDev::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 dhId: %s", GetAnonyString(dhId_).c_str());
139 if (openInfo.empty()) {
140 DHLOGE("DCameraSinkDev::OpenChannel 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("DCameraSinkDev::OpenChannel openInfo unmarshal failed, dhId: %s, ret: %d",
148 GetAnonyString(dhId_).c_str(), ret);
149 return ret;
150 }
151 return controller_->OpenChannel(cmd.value_);
152 }
153
CloseChannel()154 int32_t DCameraSinkDev::CloseChannel()
155 {
156 DHLOGI("DCameraSinkDev::CloseChannel dhId: %s", GetAnonyString(dhId_).c_str());
157 return controller_->CloseChannel();
158 }
159 } // namespace DistributedHardware
160 } // namespace OHOS