• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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("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("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("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("enter");
77     (void)parameters;
78     return DCAMERA_OK;
79 }
80 
UnsubscribeLocalHardware()81 int32_t DCameraSinkDev::UnsubscribeLocalHardware()
82 {
83     DHLOGI("enter");
84     return DCAMERA_OK;
85 }
86 
StopCapture()87 int32_t DCameraSinkDev::StopCapture()
88 {
89     DHLOGI("StopCapture dhId: %s", GetAnonyString(dhId_).c_str());
90     return controller_->StopCapture();
91 }
92 
ChannelNeg(std::string & channelInfo)93 int32_t DCameraSinkDev::ChannelNeg(std::string& channelInfo)
94 {
95     DHLOGI("ChannelNeg dhId: %s", GetAnonyString(dhId_).c_str());
96     if (channelInfo.empty()) {
97         DHLOGE("channelInfo is empty");
98         return DCAMERA_BAD_VALUE;
99     }
100 
101     DCameraChannelInfoCmd channelInfoCmd;
102     int32_t ret = channelInfoCmd.Unmarshal(channelInfo);
103     if (ret != DCAMERA_OK) {
104         DHLOGE("channelInfo unmarshal failed, dhId: %s, ret: %d",
105                GetAnonyString(dhId_).c_str(), ret);
106         return ret;
107     }
108     return controller_->ChannelNeg(channelInfoCmd.value_);
109 }
110 
GetCameraInfo(std::string & cameraInfo)111 int32_t DCameraSinkDev::GetCameraInfo(std::string& cameraInfo)
112 {
113     DHLOGI("GetCameraInfo dhId: %s", GetAnonyString(dhId_).c_str());
114     std::shared_ptr<DCameraInfo> info = std::make_shared<DCameraInfo>();
115     int32_t ret = controller_->GetCameraInfo(info);
116     if (ret != DCAMERA_OK) {
117         DHLOGE("get state failed, dhId: %s, ret: %d", 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("cameraInfoCmd marshal failed, dhId: %s, ret: %d", GetAnonyString(dhId_).c_str(), ret);
129         return ret;
130     }
131     DHLOGI("GetCameraInfo %s success", GetAnonyString(dhId_).c_str());
132     return DCAMERA_OK;
133 }
134 
OpenChannel(std::string & openInfo)135 int32_t DCameraSinkDev::OpenChannel(std::string& openInfo)
136 {
137     DHLOGI("OpenChannel dhId: %s", GetAnonyString(dhId_).c_str());
138     if (openInfo.empty()) {
139         DHLOGE("openInfo is empty");
140         return DCAMERA_BAD_VALUE;
141     }
142 
143     DCameraOpenInfoCmd cmd;
144     int32_t ret = cmd.Unmarshal(openInfo);
145     if (ret != DCAMERA_OK) {
146         DHLOGE("openInfo unmarshal failed, dhId: %s, ret: %d", GetAnonyString(dhId_).c_str(), ret);
147         return ret;
148     }
149     return controller_->OpenChannel(cmd.value_);
150 }
151 
CloseChannel()152 int32_t DCameraSinkDev::CloseChannel()
153 {
154     DHLOGI("CloseChannel dhId: %s", GetAnonyString(dhId_).c_str());
155     return controller_->CloseChannel();
156 }
157 
GetDhid()158 std::string DCameraSinkDev::GetDhid()
159 {
160     return GetAnonyString(dhId_);
161 }
162 } // namespace DistributedHardware
163 } // namespace OHOS