1 /*
2 * Copyright (c) 2021-2023 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 "anonymous_string.h"
17 #include "dcamera_hisysevent_adapter.h"
18 #include "dcamera_sink_frame_info.h"
19 #include "dcamera_softbus_adapter.h"
20 #include "distributed_camera_constants.h"
21 #include "distributed_camera_errno.h"
22 #include "distributed_hardware_log.h"
23 #include <securec.h>
24 #include "softbus_bus_center.h"
25 #include "softbus_common.h"
26 #include "softbus_errcode.h"
27 #include "dcamera_utils_tools.h"
28 #include "dcamera_frame_info.h"
29
30 namespace OHOS {
31 namespace DistributedHardware {
32 IMPLEMENT_SINGLE_INSTANCE(DCameraSoftbusAdapter);
33
DCameraSourceOnSessionOpend(int32_t sessionId,int32_t result)34 static int32_t DCameraSourceOnSessionOpend(int32_t sessionId, int32_t result)
35 {
36 return DCameraSoftbusAdapter::GetInstance().OnSourceSessionOpened(sessionId, result);
37 }
38
DCameraSourceOnSessionClosed(int32_t sessionId)39 static void DCameraSourceOnSessionClosed(int32_t sessionId)
40 {
41 DCameraSoftbusAdapter::GetInstance().OnSourceSessionClosed(sessionId);
42 return;
43 }
44
DCameraSourceOnBytesReceived(int32_t sessionId,const void * data,uint32_t dataLen)45 static void DCameraSourceOnBytesReceived(int32_t sessionId, const void *data, uint32_t dataLen)
46 {
47 DCameraSoftbusAdapter::GetInstance().OnSourceBytesReceived(sessionId, data, dataLen);
48 return;
49 }
50
DCameraSourceOnMessageReceived(int32_t sessionId,const void * data,uint32_t dataLen)51 static void DCameraSourceOnMessageReceived(int32_t sessionId, const void *data, uint32_t dataLen)
52 {
53 DCameraSoftbusAdapter::GetInstance().OnSourceMessageReceived(sessionId, data, dataLen);
54 return;
55 }
56
DCameraSourceOnStreamReceived(int32_t sessionId,const StreamData * data,const StreamData * ext,const StreamFrameInfo * param)57 static void DCameraSourceOnStreamReceived(int32_t sessionId, const StreamData *data, const StreamData *ext,
58 const StreamFrameInfo *param)
59 {
60 DCameraSoftbusAdapter::GetInstance().OnSourceStreamReceived(sessionId, data, ext, param);
61 return;
62 }
63
DCameraSinkOnSessionOpend(int32_t sessionId,int32_t result)64 static int32_t DCameraSinkOnSessionOpend(int32_t sessionId, int32_t result)
65 {
66 return DCameraSoftbusAdapter::GetInstance().OnSinkSessionOpened(sessionId, result);
67 }
68
DCameraSinkOnSessionClosed(int32_t sessionId)69 static void DCameraSinkOnSessionClosed(int32_t sessionId)
70 {
71 DCameraSoftbusAdapter::GetInstance().OnSinkSessionClosed(sessionId);
72 return;
73 }
74
DCameraSinkOnBytesReceived(int32_t sessionId,const void * data,uint32_t dataLen)75 static void DCameraSinkOnBytesReceived(int32_t sessionId, const void *data, uint32_t dataLen)
76 {
77 DCameraSoftbusAdapter::GetInstance().OnSinkBytesReceived(sessionId, data, dataLen);
78 return;
79 }
80
DCameraSinkOnMessageReceived(int32_t sessionId,const void * data,uint32_t dataLen)81 static void DCameraSinkOnMessageReceived(int32_t sessionId, const void *data, uint32_t dataLen)
82 {
83 DCameraSoftbusAdapter::GetInstance().OnSinkMessageReceived(sessionId, data, dataLen);
84 return;
85 }
86
DCameraSinkOnStreamReceived(int32_t sessionId,const StreamData * data,const StreamData * ext,const StreamFrameInfo * param)87 static void DCameraSinkOnStreamReceived(int32_t sessionId, const StreamData *data, const StreamData *ext,
88 const StreamFrameInfo *param)
89 {
90 DCameraSoftbusAdapter::GetInstance().OnSinkStreamReceived(sessionId, data, ext, param);
91 return;
92 }
93
DCameraSoftbusAdapter()94 DCameraSoftbusAdapter::DCameraSoftbusAdapter()
95 {
96 ISessionListener sourceListener;
97 sourceListener.OnSessionOpened = DCameraSourceOnSessionOpend;
98 sourceListener.OnSessionClosed = DCameraSourceOnSessionClosed;
99 sourceListener.OnBytesReceived = DCameraSourceOnBytesReceived;
100 sourceListener.OnMessageReceived = DCameraSourceOnMessageReceived;
101 sourceListener.OnStreamReceived = DCameraSourceOnStreamReceived;
102 sessListeners_[DCAMERA_CHANNLE_ROLE_SOURCE] = sourceListener;
103
104 ISessionListener sinkListener;
105 sinkListener.OnSessionOpened = DCameraSinkOnSessionOpend;
106 sinkListener.OnSessionClosed = DCameraSinkOnSessionClosed;
107 sinkListener.OnBytesReceived = DCameraSinkOnBytesReceived;
108 sinkListener.OnMessageReceived = DCameraSinkOnMessageReceived;
109 sinkListener.OnStreamReceived = DCameraSinkOnStreamReceived;
110 sessListeners_[DCAMERA_CHANNLE_ROLE_SINK] = sinkListener;
111 }
112
~DCameraSoftbusAdapter()113 DCameraSoftbusAdapter::~DCameraSoftbusAdapter()
114 {
115 }
116
CreateSoftbusSessionServer(std::string sessionName,DCAMERA_CHANNEL_ROLE role)117 int32_t DCameraSoftbusAdapter::CreateSoftbusSessionServer(std::string sessionName, DCAMERA_CHANNEL_ROLE role)
118 {
119 std::lock_guard<std::mutex> autoLock(optLock_);
120 if (sessionTotal_.find(sessionName) != sessionTotal_.end()) {
121 sessionTotal_[sessionName]++;
122 DHLOGI("DCameraSoftbusAdapter sessionName already exist %s totalnum: %d", sessionName.c_str(),
123 sessionTotal_[sessionName]);
124 return DCAMERA_OK;
125 }
126
127 int32_t ret = CreateSessionServer(PKG_NAME.c_str(), sessionName.c_str(), &sessListeners_[role]);
128 if (ret != DCAMERA_OK) {
129 DHLOGE("DCameraSoftbusAdapter CreateSessionServer failed, ret: %d", ret);
130 ReportDcamerOptFail(DCAMERA_OPT_FAIL, DCAMERA_CREATE_SESSIONSVR_ERROR,
131 CreateMsg("create session server failed, sessionName: %s", sessionName.c_str()));
132 return ret;
133 }
134 sessionTotal_[sessionName]++;
135 DHLOGI("DCameraSoftbusAdapter sessionName create %s totalnum: %d", sessionName.c_str(),
136 sessionTotal_[sessionName]);
137 return DCAMERA_OK;
138 }
139
DestroySoftbusSessionServer(std::string sessionName)140 int32_t DCameraSoftbusAdapter::DestroySoftbusSessionServer(std::string sessionName)
141 {
142 std::lock_guard<std::mutex> autoLock(optLock_);
143 if (sessionTotal_.find(sessionName) == sessionTotal_.end()) {
144 DHLOGI("DCameraSoftbusAdapter sessionName already destroy %s", sessionName.c_str());
145 return DCAMERA_OK;
146 }
147
148 sessionTotal_[sessionName]--;
149 DHLOGI("DCameraSoftbusAdapter sessionName destroy %s totalnum: %d", sessionName.c_str(),
150 sessionTotal_[sessionName]);
151 uint32_t total_ = sessionTotal_[sessionName];
152 if (total_ == 0) {
153 RemoveSessionServer(PKG_NAME.c_str(), sessionName.c_str());
154 sessionTotal_.erase(sessionName);
155 }
156 return DCAMERA_OK;
157 }
158
OpenSoftbusSession(std::string mySessName,std::string peerSessName,int32_t sessionMode,std::string peerDevId)159 int32_t DCameraSoftbusAdapter::OpenSoftbusSession(std::string mySessName, std::string peerSessName,
160 int32_t sessionMode, std::string peerDevId)
161 {
162 SessionAttribute attr = { 0 };
163 int32_t ret = ConstructSessionAttribute(sessionMode, attr);
164 if (ret != DCAMERA_OK) {
165 DHLOGE("ConstructSessionAttribute failed, ret is: %d", ret);
166 return DCAMERA_BAD_VALUE;
167 }
168 int32_t sessionId = OpenSession(mySessName.c_str(), peerSessName.c_str(), peerDevId.c_str(), "0", &attr);
169 if (sessionId < 0) {
170 DHLOGE("DCameraSoftbusAdapter OpenSoftbusSession failed %d", sessionId);
171 ReportDcamerOptFail(DCAMERA_OPT_FAIL, DCAMERA_OPEN_SESSION_ERROR,
172 CreateMsg("open softbus session failed, mySessName: %s, peerSessName: %s, peerDevId: %s",
173 mySessName.c_str(), peerSessName.c_str(), peerDevId.c_str()));
174 return DCAMERA_BAD_OPERATE;
175 }
176 return DCAMERA_OK;
177 }
178
CloseSoftbusSession(int32_t sessionId)179 int32_t DCameraSoftbusAdapter::CloseSoftbusSession(int32_t sessionId)
180 {
181 DHLOGI("close softbus sessionId: %d", sessionId);
182 CloseSession(sessionId);
183 {
184 std::lock_guard<std::mutex> autoLock(idMapLock_);
185 sessionIdMap_.erase(sessionId);
186 }
187 DHLOGI("close softbus sessionId: %d end", sessionId);
188 return DCAMERA_OK;
189 }
190
SendSofbusBytes(int32_t sessionId,std::shared_ptr<DataBuffer> & buffer)191 int32_t DCameraSoftbusAdapter::SendSofbusBytes(int32_t sessionId, std::shared_ptr<DataBuffer>& buffer)
192 {
193 return SendBytes(sessionId, buffer->Data(), buffer->Size());
194 }
195
SendSofbusStream(int32_t sessionId,std::shared_ptr<DataBuffer> & buffer)196 int32_t DCameraSoftbusAdapter::SendSofbusStream(int32_t sessionId, std::shared_ptr<DataBuffer>& buffer)
197 {
198 StreamData streamData = { reinterpret_cast<char *>(buffer->Data()), buffer->Size() };
199 int64_t timeStamp;
200 if (!buffer->FindInt64(TIME_STAMP_US, timeStamp)) {
201 DHLOGD("SendSofbusStream find %s failed.", TIME_STAMP_US.c_str());
202 }
203 int32_t frameType;
204 if (!buffer->FindInt32(FRAME_TYPE, frameType)) {
205 DHLOGD("SendSofbusStream find %s failed.", FRAME_TYPE.c_str());
206 }
207 int32_t index;
208 if (!buffer->FindInt32(INDEX, index)) {
209 DHLOGD("SendSofbusStream find %s failed.", INDEX.c_str());
210 }
211 int64_t startEncodeT;
212 if (!buffer->FindInt64(START_ENCODE_TIME_US, startEncodeT)) {
213 DHLOGD("SendSofbusStream find %s failed.", START_ENCODE_TIME_US.c_str());
214 }
215 int64_t finishEncodeT;
216 if (!buffer->FindInt64(FINISH_ENCODE_TIME_US, finishEncodeT)) {
217 DHLOGD("SendSofbusStream find %s failed.", FINISH_ENCODE_TIME_US.c_str());
218 }
219 std::string jsonStr = "";
220 DCameraSinkFrameInfo sinkFrameInfo;
221 sinkFrameInfo.pts_ = timeStamp;
222 sinkFrameInfo.type_ = frameType;
223 sinkFrameInfo.index_ = index;
224 sinkFrameInfo.startEncodeT_ = startEncodeT;
225 sinkFrameInfo.finishEncodeT_ = finishEncodeT;
226 sinkFrameInfo.sendT_ = GetNowTimeStampUs();
227 sinkFrameInfo.Marshal(jsonStr);
228 StreamData ext = { const_cast<char *>(jsonStr.c_str()), jsonStr.length() };
229 StreamFrameInfo param = { 0 };
230 int32_t ret = SendStream(sessionId, &streamData, &ext, ¶m);
231 if (ret != SOFTBUS_OK) {
232 DHLOGD("SendSofbusStream failed, ret is %d", ret);
233 return DCAMERA_BAD_VALUE;
234 }
235 return DCAMERA_OK;
236 }
237
DCameraSoftbusGetSessionById(int32_t sessionId,std::shared_ptr<DCameraSoftbusSession> & session)238 int32_t DCameraSoftbusAdapter::DCameraSoftbusGetSessionById(int32_t sessionId,
239 std::shared_ptr<DCameraSoftbusSession>& session)
240 {
241 DHLOGI("get softbus session by sessionId: %d", sessionId);
242 std::lock_guard<std::mutex> autoLock(idMapLock_);
243 auto iter = sessionIdMap_.find(sessionId);
244 if (iter == sessionIdMap_.end()) {
245 DHLOGE("get softbus session by id not find session %d", sessionId);
246 return DCAMERA_NOT_FOUND;
247 }
248 session = iter->second;
249 return DCAMERA_OK;
250 }
251
DCameraSoftbusSourceGetSession(int32_t sessionId,std::shared_ptr<DCameraSoftbusSession> & session)252 int32_t DCameraSoftbusAdapter::DCameraSoftbusSourceGetSession(int32_t sessionId,
253 std::shared_ptr<DCameraSoftbusSession>& session)
254 {
255 char peerSessionName[DCAMERA_SESSION_NAME_MAX_LEN] = "";
256 char peerDevId[NETWORK_ID_BUF_LEN] = "";
257 int32_t ret = GetPeerSessionName(sessionId, peerSessionName, sizeof(peerSessionName));
258 if (ret != DCAMERA_OK) {
259 DHLOGI("DCameraSoftbusAdapter DCameraSoftbusSourceGetSession sessionId: %d GetPeerSessionName failed: %d",
260 sessionId, ret);
261 return ret;
262 }
263
264 ret = GetPeerDeviceId(sessionId, peerDevId, sizeof(peerDevId));
265 if (ret != DCAMERA_OK) {
266 DHLOGI("DCameraSoftbusAdapter DCameraSoftbusSourceGetSession sessionId: %d GetPeerDeviceId failed: %d",
267 sessionId, ret);
268 return ret;
269 }
270
271 auto iter = sourceSessions_.find(std::string(peerDevId) + std::string(peerSessionName));
272 if (iter == sourceSessions_.end()) {
273 DHLOGE("DCameraSoftbusAdapter DCameraSoftbusSourceGetSession not find session %d", sessionId);
274 return DCAMERA_NOT_FOUND;
275 }
276 session = iter->second;
277 return DCAMERA_OK;
278 }
279
OnSourceSessionOpened(int32_t sessionId,int32_t result)280 int32_t DCameraSoftbusAdapter::OnSourceSessionOpened(int32_t sessionId, int32_t result)
281 {
282 DHLOGI("DCameraSoftbusAdapter OnSourceSessionOpened sessionId: %d, result: %d", sessionId, result);
283 std::shared_ptr<DCameraSoftbusSession> session = nullptr;
284 int32_t ret = DCameraSoftbusSourceGetSession(sessionId, session);
285 if (ret != DCAMERA_OK) {
286 DHLOGE("DCameraSoftbusAdapter OnSourceSessionOpened not find session %d", sessionId);
287 return DCAMERA_NOT_FOUND;
288 }
289
290 ret = session->OnSessionOpend(sessionId, result);
291 if (ret != DCAMERA_OK) {
292 DHLOGE("DCameraSoftbusAdapter OnSourceSessionOpened failed %d sessionId: %d", ret, sessionId);
293 } else {
294 std::lock_guard<std::mutex> autoLock(idMapLock_);
295 sessionIdMap_.emplace(sessionId, session);
296 }
297 DHLOGI("DCameraSoftbusAdapter OnSourceSessionOpened sessionId: %d, result: %d end", sessionId, result);
298 return ret;
299 }
300
OnSourceSessionClosed(int32_t sessionId)301 void DCameraSoftbusAdapter::OnSourceSessionClosed(int32_t sessionId)
302 {
303 DHLOGI("DCameraSoftbusAdapter OnSourceSessionClosed sessionId: %d", sessionId);
304 std::shared_ptr<DCameraSoftbusSession> session = nullptr;
305 int32_t ret = DCameraSoftbusGetSessionById(sessionId, session);
306 if (ret != DCAMERA_OK) {
307 DHLOGE("DCameraSoftbusAdapter OnSourceSessionClosed not find session %d", sessionId);
308 return;
309 }
310 {
311 std::lock_guard<std::mutex> autoLock(idMapLock_);
312 sessionIdMap_.erase(sessionId);
313 }
314 session->OnSessionClose(sessionId);
315 DHLOGI("DCameraSoftbusAdapter OnSourceSessionClosed sessionId: %d end", sessionId);
316 return;
317 }
318
OnSourceBytesReceived(int32_t sessionId,const void * data,uint32_t dataLen)319 void DCameraSoftbusAdapter::OnSourceBytesReceived(int32_t sessionId, const void *data, uint32_t dataLen)
320 {
321 if (dataLen == 0 || dataLen > DCAMERA_MAX_RECV_DATA_LEN || data == nullptr) {
322 DHLOGE("DCameraSoftbusAdapter OnSourceBytesReceived dataLen: %d, sessionId: %d", dataLen, sessionId);
323 return;
324 }
325 std::shared_ptr<DCameraSoftbusSession> session = nullptr;
326 int32_t ret = DCameraSoftbusSourceGetSession(sessionId, session);
327 if (ret != DCAMERA_OK) {
328 DHLOGE("DCameraSoftbusAdapter OnSourceBytesReceived not find session %d", sessionId);
329 return;
330 }
331
332 std::shared_ptr<DataBuffer> buffer = std::make_shared<DataBuffer>(dataLen);
333 ret = memcpy_s(buffer->Data(), buffer->Capacity(), data, dataLen);
334 if (ret != EOK) {
335 DHLOGE("DCameraSoftbusAdapter OnSourceBytesReceived memcpy_s failed ret: %d", ret);
336 return;
337 }
338 session->OnDataReceived(buffer);
339 return;
340 }
341
OnSourceMessageReceived(int32_t sessionId,const void * data,uint32_t dataLen)342 void DCameraSoftbusAdapter::OnSourceMessageReceived(int32_t sessionId, const void *data, uint32_t dataLen)
343 {
344 (void)sessionId;
345 (void)data;
346 (void)dataLen;
347 return;
348 }
349
OnSourceStreamReceived(int32_t sessionId,const StreamData * data,const StreamData * ext,const StreamFrameInfo * param)350 void DCameraSoftbusAdapter::OnSourceStreamReceived(int32_t sessionId, const StreamData *data, const StreamData *ext,
351 const StreamFrameInfo *param)
352 {
353 int64_t recvT = GetNowTimeStampUs();
354 if (data == nullptr) {
355 DHLOGE("DCameraSoftbusAdapter::OnSourceStreamReceived, data is null, sessionId: %d.", sessionId);
356 return;
357 }
358 int32_t dataLen = data->bufLen;
359 if (dataLen <= 0 || dataLen > static_cast<int32_t>(DCAMERA_MAX_RECV_DATA_LEN)) {
360 DHLOGE("DCameraSoftbusAdapter OnSourceStreamReceived dataLen: %d, sessionId: %d", dataLen, sessionId);
361 return;
362 }
363 std::shared_ptr<DCameraSoftbusSession> session = nullptr;
364 int32_t ret = DCameraSoftbusSourceGetSession(sessionId, session);
365 if (ret != DCAMERA_OK) {
366 DHLOGE("DCameraSoftbusAdapter OnSourceStreamReceived not find session %d", sessionId);
367 return;
368 }
369
370 std::shared_ptr<DataBuffer> buffer = std::make_shared<DataBuffer>(data->bufLen);
371 buffer->SetInt64(RECV_TIME_US, recvT);
372 ret = memcpy_s(buffer->Data(), buffer->Capacity(), reinterpret_cast<uint8_t *>(data->buf), data->bufLen);
373 if (ret != EOK) {
374 DHLOGE("DCameraSoftbusAdapter OnSourceStreamReceived memcpy_s failed ret: %d", ret);
375 return;
376 }
377 ret = HandleSourceStreamExt(buffer, ext);
378 if (ret != DCAMERA_OK) {
379 DHLOGE("Handle source stream ext failed, ret is: %d", ret);
380 }
381 session->OnDataReceived(buffer);
382 }
383
HandleSourceStreamExt(std::shared_ptr<DataBuffer> & buffer,const StreamData * ext)384 int32_t DCameraSoftbusAdapter::HandleSourceStreamExt(std::shared_ptr<DataBuffer>& buffer, const StreamData *ext)
385 {
386 if (ext == nullptr) {
387 DHLOGE("Source stream ext is null.");
388 return DCAMERA_BAD_VALUE;
389 }
390 int32_t extLen = ext->bufLen;
391 if (extLen <= 0 || extLen > DCAMERA_MAX_RECV_EXT_LEN) {
392 DHLOGD("ExtLen is: %d.", extLen);
393 return DCAMERA_BAD_VALUE;
394 }
395
396 std::string jsonStr(reinterpret_cast<const char*>(ext->buf), ext->bufLen);
397 DCameraSinkFrameInfo sinkFrameInfo;
398 int32_t ret = sinkFrameInfo.Unmarshal(jsonStr);
399 if (ret != DCAMERA_OK) {
400 DHLOGE("Unmarshal sinkFrameInfo failed.");
401 return DCAMERA_BAD_VALUE;
402 }
403 int64_t recvT;
404 if (!buffer->FindInt64(RECV_TIME_US, recvT)) {
405 DHLOGD("HandleSourceStreamExt find %s failed.", RECV_TIME_US.c_str());
406 }
407 DCameraFrameInfo frameInfo;
408 frameInfo.type = sinkFrameInfo.type_;
409 frameInfo.pts = sinkFrameInfo.pts_;
410 frameInfo.index = sinkFrameInfo.index_;
411 frameInfo.ver = sinkFrameInfo.ver_;
412 frameInfo.timePonit.startEncode = sinkFrameInfo.startEncodeT_;
413 frameInfo.timePonit.finishEncode = sinkFrameInfo.finishEncodeT_;
414 frameInfo.timePonit.send = sinkFrameInfo.sendT_;
415 frameInfo.timePonit.recv = recvT;
416 buffer->frameInfo_ = frameInfo;
417 return DCAMERA_OK;
418 }
419
DCameraSoftbusSinkGetSession(int32_t sessionId,std::shared_ptr<DCameraSoftbusSession> & session)420 int32_t DCameraSoftbusAdapter::DCameraSoftbusSinkGetSession(int32_t sessionId,
421 std::shared_ptr<DCameraSoftbusSession>& session)
422 {
423 char mySessionName[DCAMERA_SESSION_NAME_MAX_LEN] = "";
424 int ret = GetMySessionName(sessionId, mySessionName, sizeof(mySessionName));
425 if (ret != DCAMERA_OK) {
426 DHLOGI("DCameraSoftbusAdapter DCameraSoftbusSinkGetSession sessionId: %d GetPeerSessionName failed: %d",
427 sessionId, ret);
428 return ret;
429 }
430
431 auto iter = sinkSessions_.find(std::string(mySessionName));
432 if (iter == sinkSessions_.end()) {
433 DHLOGE("DCameraSoftbusAdapter DCameraSoftbusSinkGetSession not find session %d", sessionId);
434 return DCAMERA_NOT_FOUND;
435 }
436 session = iter->second;
437 return DCAMERA_OK;
438 }
439
OnSinkSessionOpened(int32_t sessionId,int32_t result)440 int32_t DCameraSoftbusAdapter::OnSinkSessionOpened(int32_t sessionId, int32_t result)
441 {
442 DHLOGI("DCameraSoftbusAdapter OnSinkSessionOpened sessionId: %d, result: %d", sessionId, result);
443 std::shared_ptr<DCameraSoftbusSession> session = nullptr;
444 int32_t ret = DCameraSoftbusSinkGetSession(sessionId, session);
445 if (ret != DCAMERA_OK) {
446 DHLOGE("DCameraSoftbusAdapter OnSinkSessionOpened not find session %d", sessionId);
447 return DCAMERA_NOT_FOUND;
448 }
449
450 ret = session->OnSessionOpend(sessionId, result);
451 if (ret != DCAMERA_OK) {
452 DHLOGE("DCameraSoftbusAdapter OnSinkSessionOpened not find session %d", sessionId);
453 } else {
454 std::lock_guard<std::mutex> autoLock(idMapLock_);
455 sessionIdMap_.emplace(sessionId, session);
456 }
457 DHLOGI("DCameraSoftbusAdapter OnSinkSessionOpened sessionId: %d, result: %d end", sessionId, result);
458 return ret;
459 }
460
OnSinkSessionClosed(int32_t sessionId)461 void DCameraSoftbusAdapter::OnSinkSessionClosed(int32_t sessionId)
462 {
463 DHLOGI("DCameraSoftbusAdapter OnSinkSessionClosed sessionId: %d", sessionId);
464 std::shared_ptr<DCameraSoftbusSession> session = nullptr;
465 int32_t ret = DCameraSoftbusGetSessionById(sessionId, session);
466 if (ret != DCAMERA_OK) {
467 DHLOGE("DCameraSoftbusAdapter OnSinkSessionClosed not find session %d", sessionId);
468 return;
469 }
470 {
471 std::lock_guard<std::mutex> autoLock(idMapLock_);
472 sessionIdMap_.erase(sessionId);
473 }
474 session->OnSessionClose(sessionId);
475 return;
476 }
477
OnSinkBytesReceived(int32_t sessionId,const void * data,uint32_t dataLen)478 void DCameraSoftbusAdapter::OnSinkBytesReceived(int32_t sessionId, const void *data, uint32_t dataLen)
479 {
480 if (dataLen == 0 || dataLen > DCAMERA_MAX_RECV_DATA_LEN || data == nullptr) {
481 DHLOGE("DCameraSoftbusAdapter OnSinkBytesReceived dataLen: %d, sessionId: %d", dataLen, sessionId);
482 return;
483 }
484 std::shared_ptr<DCameraSoftbusSession> session = nullptr;
485 int32_t ret = DCameraSoftbusSinkGetSession(sessionId, session);
486 if (ret != DCAMERA_OK) {
487 DHLOGE("DCameraSoftbusAdapter OnSinkBytesReceived not find session %d", sessionId);
488 return;
489 }
490
491 std::shared_ptr<DataBuffer> buffer = std::make_shared<DataBuffer>(dataLen);
492 ret = memcpy_s(buffer->Data(), buffer->Capacity(), data, dataLen);
493 if (ret != EOK) {
494 DHLOGE("DCameraSoftbusAdapter OnSinkBytesReceived memcpy_s failed ret: %d", ret);
495 return;
496 }
497 session->OnDataReceived(buffer);
498 return;
499 }
500
OnSinkMessageReceived(int32_t sessionId,const void * data,uint32_t dataLen)501 void DCameraSoftbusAdapter::OnSinkMessageReceived(int32_t sessionId, const void *data, uint32_t dataLen)
502 {
503 return;
504 }
505
OnSinkStreamReceived(int32_t sessionId,const StreamData * data,const StreamData * ext,const StreamFrameInfo * param)506 void DCameraSoftbusAdapter::OnSinkStreamReceived(int32_t sessionId, const StreamData *data, const StreamData *ext,
507 const StreamFrameInfo *param)
508 {
509 if (data == nullptr) {
510 DHLOGE("DCameraSoftbusAdapter::OnSinkStreamReceived, data is null, sessionId: %d.", sessionId);
511 return;
512 }
513 int32_t dataLen = data->bufLen;
514 if (dataLen <= 0 || dataLen > static_cast<int32_t>(DCAMERA_MAX_RECV_DATA_LEN)) {
515 DHLOGE("DCameraSoftbusAdapter OnSinkStreamReceived dataLen: %d sessionId: %d", dataLen, sessionId);
516 return;
517 }
518 std::shared_ptr<DCameraSoftbusSession> session = nullptr;
519 int32_t ret = DCameraSoftbusSinkGetSession(sessionId, session);
520 if (ret != DCAMERA_OK) {
521 DHLOGE("DCameraSoftbusAdapter OnSinkStreamReceived not find session %d", sessionId);
522 return;
523 }
524
525 std::shared_ptr<DataBuffer> buffer = std::make_shared<DataBuffer>(data->bufLen);
526 ret = memcpy_s(buffer->Data(), buffer->Capacity(), reinterpret_cast<uint8_t *>(data->buf), data->bufLen);
527 if (ret != EOK) {
528 DHLOGE("DCameraSoftbusAdapter OnSinkStreamReceived memcpy_s failed ret: %d", ret);
529 return;
530 }
531 session->OnDataReceived(buffer);
532 return;
533 }
534
GetLocalNetworkId(std::string & myDevId)535 int32_t DCameraSoftbusAdapter::GetLocalNetworkId(std::string& myDevId)
536 {
537 NodeBasicInfo basicInfo = { { 0 } };
538 int32_t ret = GetLocalNodeDeviceInfo(PKG_NAME.c_str(), &basicInfo);
539 if (ret != DCAMERA_OK) {
540 DHLOGE("DCameraSoftbusAdapter GetLocalNodeDeviceInfo failed ret: %d", ret);
541 return ret;
542 }
543
544 myDevId = std::string(basicInfo.networkId);
545 return DCAMERA_OK;
546 }
547
ConstructSessionAttribute(int32_t sessionMode,SessionAttribute & attr)548 int32_t DCameraSoftbusAdapter::ConstructSessionAttribute(int32_t sessionMode, SessionAttribute& attr)
549 {
550 int dataType = TYPE_STREAM;
551 int streamType = INVALID;
552 switch (sessionMode) {
553 case DCAMERA_SESSION_MODE_CTRL:
554 dataType = TYPE_BYTES;
555 break;
556 case DCAMERA_SESSION_MODE_VIDEO:
557 streamType = COMMON_VIDEO_STREAM;
558 break;
559 case DCAMERA_SESSION_MODE_JPEG:
560 streamType = RAW_STREAM;
561 break;
562 default:
563 DHLOGE("Bad sessionMode %d", sessionMode);
564 return DCAMERA_BAD_VALUE;
565 }
566 attr.dataType = dataType;
567 attr.linkTypeNum = DCAMERA_LINK_TYPE_MAX;
568 LinkType linkTypeList[DCAMERA_LINK_TYPE_MAX] = {
569 LINK_TYPE_WIFI_P2P,
570 LINK_TYPE_WIFI_WLAN_5G,
571 LINK_TYPE_WIFI_WLAN_2G,
572 LINK_TYPE_BR,
573 };
574
575 if (dataType == TYPE_BYTES && (DCAMERA_LINK_TYPE_INDEX_2 < DCAMERA_LINK_TYPE_MAX)) {
576 linkTypeList[0] = LINK_TYPE_WIFI_WLAN_2G;
577 linkTypeList[DCAMERA_LINK_TYPE_INDEX_2] = LINK_TYPE_WIFI_P2P;
578 }
579 int32_t ret = memcpy_s(attr.linkType, DCAMERA_LINK_TYPE_MAX * sizeof(LinkType), linkTypeList,
580 DCAMERA_LINK_TYPE_MAX * sizeof(LinkType));
581 if (ret != EOK) {
582 DHLOGE("LinkType memcpy_s failed %d", ret);
583 return DCAMERA_BAD_VALUE;
584 }
585 attr.attr.streamAttr.streamType = streamType;
586 return DCAMERA_OK;
587 }
588 } // namespace DistributedHardware
589 } // namespace OHOS
590