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_source_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 {
DCameraChannelSourceImpl()28 DCameraChannelSourceImpl::DCameraChannelSourceImpl()
29 {
30 }
31
~DCameraChannelSourceImpl()32 DCameraChannelSourceImpl::~DCameraChannelSourceImpl()
33 {
34 }
35
CloseSession()36 int32_t DCameraChannelSourceImpl::CloseSession()
37 {
38 DHLOGI("DCameraChannelSourceImpl CloseSession name: %{public}s", GetAnonyString(mySessionName_).c_str());
39 if (softbusSessions_.empty()) {
40 DHLOGE("DCameraChannelSourceImpl CloseSession %{public}s failed", GetAnonyString(mySessionName_).c_str());
41 return DCAMERA_BAD_OPERATE;
42 }
43 int32_t ret = DCAMERA_OK;
44 for (auto iter = softbusSessions_.begin(); iter != softbusSessions_.end(); iter++) {
45 if ((*iter) == nullptr) {
46 continue;
47 }
48 int32_t retOpen = (*iter)->CloseSession();
49 if (retOpen != DCAMERA_OK) {
50 DHLOGE("DCameraChannelSourceImpl CloseSession %{public}s failed, ret: %{public}d",
51 GetAnonyString(mySessionName_).c_str(), retOpen);
52 ret = DCAMERA_BAD_OPERATE;
53 }
54 }
55
56 return ret;
57 }
58
CreateSession(std::vector<DCameraIndex> & camIndexs,std::string sessionFlag,DCameraSessionMode sessionMode,std::shared_ptr<ICameraChannelListener> & listener)59 int32_t DCameraChannelSourceImpl::CreateSession(std::vector<DCameraIndex>& camIndexs, std::string sessionFlag,
60 DCameraSessionMode sessionMode, std::shared_ptr<ICameraChannelListener>& listener)
61 {
62 if (camIndexs.size() > DCAMERA_MAX_NUM || listener == nullptr) {
63 return DCAMERA_BAD_VALUE;
64 }
65 if (!softbusSessions_.empty()) {
66 DHLOGI("DCameraChannelSourceImpl session has already create %{public}s", sessionFlag.c_str());
67 return DCAMERA_OK;
68 }
69 std::string myDevId;
70 int32_t ret = GetLocalDeviceNetworkId(myDevId);
71 if (ret != DCAMERA_OK) {
72 DHLOGE("DCameraChannelSourceImpl get local networkId error. ret %{public}d", ret);
73 return ret;
74 }
75 camIndexs_.assign(camIndexs.begin(), camIndexs.end());
76 listener_ = listener;
77 mySessionName_ = SESSION_HEAD + sessionFlag;
78 mode_ = sessionMode;
79 DHLOGI("DCameraChannelSourceImpl CreateSession Start, name: %{public}s devId: %{public}s",
80 GetAnonyString(mySessionName_).c_str(), GetAnonyString(myDevId).c_str());
81 for (auto iter = camIndexs.begin(); iter != camIndexs.end(); iter++) {
82 std::string peerDevId = (*iter).devId_;
83 std::string peerSessionName = SESSION_HEAD + (*iter).dhId_ + std::string("_") + sessionFlag;
84 // source_bind
85 std::shared_ptr<DCameraSoftbusSession> softbusSess = std::make_shared<DCameraSoftbusSession>(myDevId,
86 mySessionName_, peerDevId, peerSessionName, listener, sessionMode);
87 int32_t socketId = softbusSess->BindSocketServer();
88 if (socketId == 0 || socketId == DCAMERA_BAD_VALUE) {
89 DHLOGE("DCameraChannelSourceImpl Create Session failed. socketId: %{public}d.", socketId);
90 return DCAMERA_BAD_VALUE;
91 }
92 DCameraSoftbusAdapter::GetInstance().RecordSourceSocketSession(socketId, softbusSess);
93 softbusSessions_.push_back(softbusSess);
94 }
95 DHLOGI("DCameraChannelSourceImpl CreateSession End");
96 return DCAMERA_OK;
97 }
98
ReleaseSession()99 int32_t DCameraChannelSourceImpl::ReleaseSession()
100 {
101 DHLOGI("DCameraChannelSourceImpl ReleaseSession name: %{public}s", GetAnonyString(mySessionName_).c_str());
102 for (auto iter = softbusSessions_.begin(); iter != softbusSessions_.end(); iter++) {
103 if ((*iter) == nullptr) {
104 continue;
105 }
106 (*iter)->ReleaseSession();
107 }
108 std::vector<std::shared_ptr<DCameraSoftbusSession>>().swap(softbusSessions_);
109 return DCAMERA_OK;
110 }
111
SendData(std::shared_ptr<DataBuffer> & buffer)112 int32_t DCameraChannelSourceImpl::SendData(std::shared_ptr<DataBuffer>& buffer)
113 {
114 if (softbusSessions_.empty()) {
115 DHLOGE("DCameraChannelSourceImpl SendData %{public}s failed", GetAnonyString(mySessionName_).c_str());
116 return DCAMERA_BAD_OPERATE;
117 }
118 int32_t ret = DCAMERA_OK;
119 for (auto iter = softbusSessions_.begin(); iter != softbusSessions_.end(); iter++) {
120 if ((*iter) == nullptr) {
121 continue;
122 }
123 int32_t retSend = (*iter)->SendData(mode_, buffer);
124 if (retSend != DCAMERA_OK) {
125 DHLOGE("DCameraChannelSourceImpl SendData %{public}s failed, ret: %{public}d",
126 GetAnonyString(mySessionName_).c_str(), retSend);
127 ret = DCAMERA_BAD_OPERATE;
128 }
129 }
130 return ret;
131 }
132 } // namespace DistributedHardware
133 } // namespace OHOS
134