• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 dhId = (*iter).dhId_;
84         std::string peerSessionName = SESSION_HEAD + (*iter).dhId_ + std::string("_") + sessionFlag;
85         // source_bind
86         std::shared_ptr<DCameraSoftbusSession> softbusSess = std::make_shared<DCameraSoftbusSession>(
87             dhId, myDevId, mySessionName_, peerDevId, peerSessionName, listener, sessionMode);
88         int32_t socketId = softbusSess->BindSocketServer();
89         if (socketId == 0 || socketId == DCAMERA_BAD_VALUE) {
90             DHLOGE("DCameraChannelSourceImpl Create Session failed. socketId: %{public}d.", socketId);
91             return DCAMERA_BAD_VALUE;
92         }
93         DCameraSoftbusAdapter::GetInstance().RecordSourceSocketSession(socketId, softbusSess);
94         softbusSessions_.push_back(softbusSess);
95     }
96     DHLOGI("DCameraChannelSourceImpl CreateSession End");
97     return DCAMERA_OK;
98 }
99 
ReleaseSession()100 int32_t DCameraChannelSourceImpl::ReleaseSession()
101 {
102     DHLOGI("DCameraChannelSourceImpl ReleaseSession name: %{public}s", GetAnonyString(mySessionName_).c_str());
103     for (auto iter = softbusSessions_.begin(); iter != softbusSessions_.end(); iter++) {
104         if ((*iter) == nullptr) {
105             continue;
106         }
107         (*iter)->ReleaseSession();
108     }
109     std::vector<std::shared_ptr<DCameraSoftbusSession>>().swap(softbusSessions_);
110     return DCAMERA_OK;
111 }
112 
SendData(std::shared_ptr<DataBuffer> & buffer)113 int32_t DCameraChannelSourceImpl::SendData(std::shared_ptr<DataBuffer>& buffer)
114 {
115     if (softbusSessions_.empty()) {
116         DHLOGE("DCameraChannelSourceImpl SendData %{public}s failed", GetAnonyString(mySessionName_).c_str());
117         return DCAMERA_BAD_OPERATE;
118     }
119     int32_t ret = DCAMERA_OK;
120     for (auto iter = softbusSessions_.begin(); iter != softbusSessions_.end(); iter++) {
121         if ((*iter) == nullptr) {
122             continue;
123         }
124         int32_t retSend = (*iter)->SendData(mode_, buffer);
125         if (retSend != DCAMERA_OK) {
126             DHLOGE("DCameraChannelSourceImpl SendData %{public}s failed, ret: %{public}d",
127                 GetAnonyString(mySessionName_).c_str(), retSend);
128             ret = DCAMERA_BAD_OPERATE;
129         }
130     }
131     return ret;
132 }
133 } // namespace DistributedHardware
134 } // namespace OHOS
135