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 #include "dcamera_channel_sink_impl.h"
17
18 #include "dcamera_softbus_adapter.h"
19 #include "dcamera_utils_tools.h"
20
21 #include "anonymous_string.h"
22 #include "distributed_camera_constants.h"
23 #include "distributed_camera_errno.h"
24 #include "distributed_hardware_log.h"
25
26 namespace OHOS {
27 namespace DistributedHardware {
DCameraChannelSinkImpl()28 DCameraChannelSinkImpl::DCameraChannelSinkImpl()
29 {
30 softbusSession_ = nullptr;
31 }
32
~DCameraChannelSinkImpl()33 DCameraChannelSinkImpl::~DCameraChannelSinkImpl()
34 {
35 }
36
CloseSession()37 int32_t DCameraChannelSinkImpl::CloseSession()
38 {
39 DHLOGI("DCameraChannelSinkImpl CloseSession name: %{public}s", GetAnonyString(mySessionName_).c_str());
40 if (softbusSession_ == nullptr) {
41 DHLOGE("DCameraChannelSinkImpl CloseSession %{public}s failed", GetAnonyString(mySessionName_).c_str());
42 return DCAMERA_BAD_OPERATE;
43 }
44 int32_t ret = softbusSession_->CloseSession();
45 if (ret != DCAMERA_OK) {
46 DHLOGE("DCameraChannelSinkImpl CloseSession %{public}s ret: %{public}d",
47 GetAnonyString(mySessionName_).c_str(), ret);
48 }
49
50 return ret;
51 }
52
CreateSession(std::vector<DCameraIndex> & camIndexs,std::string sessionFlag,DCameraSessionMode sessionMode,std::shared_ptr<ICameraChannelListener> & listener)53 int32_t DCameraChannelSinkImpl::CreateSession(std::vector<DCameraIndex>& camIndexs, std::string sessionFlag,
54 DCameraSessionMode sessionMode, std::shared_ptr<ICameraChannelListener>& listener)
55 {
56 if (camIndexs.size() > DCAMERA_MAX_NUM || listener == nullptr) {
57 return DCAMERA_BAD_VALUE;
58 }
59 std::string myDevId;
60 int32_t ret = GetLocalDeviceNetworkId(myDevId);
61 if (ret != DCAMERA_OK) {
62 DHLOGE("DCameraChannelSinkImpl get local device networkId error, ret%{public}d", ret);
63 return ret;
64 }
65 camIndexs_.assign(camIndexs.begin(), camIndexs.end());
66 listener_ = listener;
67 if (!ManageSelectChannel::GetInstance().GetSinkConnect()) {
68 mySessionName_ = SESSION_HEAD + camIndexs[0].dhId_ + std::string("_") + sessionFlag;
69 } else {
70 mySessionName_ = SESSION_HEAD + camIndexs[0].dhId_ + std::string("_") + sessionFlag + "_sender";
71 }
72 mode_ = sessionMode;
73 std::string peerDevId = camIndexs[0].devId_;
74 std::string dhId = camIndexs[0].dhId_;
75 std::string peerSessionName = SESSION_HEAD + sessionFlag;
76 DHLOGI("DCameraChannelSinkImpl CreateSession Listen Start, devId: %{public}s", GetAnonyString(myDevId).c_str());
77 // sink_server_listen
78 softbusSession_ = std::make_shared<DCameraSoftbusSession>(dhId, myDevId, mySessionName_, peerDevId,
79 peerSessionName, listener, sessionMode);
80 ret = softbusSession_->CreateSocketServer();
81 if (ret != DCAMERA_OK) {
82 DHLOGE("DCameraChannelSinkImpl CreateSession Error, ret %{public}d", ret);
83 return ret;
84 }
85 DCameraSoftbusAdapter::GetInstance().sinkSessions_[mySessionName_] = softbusSession_;
86 DHLOGI("DCameraChannelSinkImpl CreateSession Listen End, devId: %{public}s", GetAnonyString(myDevId).c_str());
87 return DCAMERA_OK;
88 }
89
ReleaseSession()90 int32_t DCameraChannelSinkImpl::ReleaseSession()
91 {
92 DHLOGI("DCameraChannelSinkImpl ReleaseSession name: %{public}s", GetAnonyString(mySessionName_).c_str());
93 if (softbusSession_ == nullptr) {
94 return DCAMERA_OK;
95 }
96 softbusSession_->ReleaseSession();
97 DCameraSoftbusAdapter::GetInstance().sinkSessions_.erase(softbusSession_->GetMySessionName());
98 softbusSession_ = nullptr;
99 return DCAMERA_OK;
100 }
101
SendData(std::shared_ptr<DataBuffer> & buffer)102 int32_t DCameraChannelSinkImpl::SendData(std::shared_ptr<DataBuffer>& buffer)
103 {
104 if (softbusSession_ == nullptr) {
105 DHLOGE("DCameraChannelSinkImpl SendData %{public}s failed", GetAnonyString(mySessionName_).c_str());
106 return DCAMERA_BAD_OPERATE;
107 }
108 int32_t ret = softbusSession_->SendData(mode_, buffer);
109 if (ret != DCAMERA_OK) {
110 DHLOGE("DCameraChannelSinkImpl SendData %{public}s failed, ret: %{public}d",
111 GetAnonyString(mySessionName_).c_str(), ret);
112 }
113 return ret;
114 }
115 } // namespace DistributedHardware
116 } // namespace OHOS
117