11c1b0f19Sopenharmony_ci/* 21c1b0f19Sopenharmony_ci * Copyright (c) 2021-2024 Huawei Device Co., Ltd. 31c1b0f19Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 41c1b0f19Sopenharmony_ci * you may not use this file except in compliance with the License. 51c1b0f19Sopenharmony_ci * You may obtain a copy of the License at 61c1b0f19Sopenharmony_ci * 71c1b0f19Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 81c1b0f19Sopenharmony_ci * 91c1b0f19Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 101c1b0f19Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 111c1b0f19Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 121c1b0f19Sopenharmony_ci * See the License for the specific language governing permissions and 131c1b0f19Sopenharmony_ci * limitations under the License. 141c1b0f19Sopenharmony_ci */ 151c1b0f19Sopenharmony_ci 161c1b0f19Sopenharmony_ci#include "dcamera_softbus_session.h" 171c1b0f19Sopenharmony_ci 181c1b0f19Sopenharmony_ci#include <securec.h> 191c1b0f19Sopenharmony_ci 201c1b0f19Sopenharmony_ci#include "anonymous_string.h" 211c1b0f19Sopenharmony_ci#include "dcamera_softbus_adapter.h" 221c1b0f19Sopenharmony_ci#include "dcamera_utils_tools.h" 231c1b0f19Sopenharmony_ci#include "distributed_camera_constants.h" 241c1b0f19Sopenharmony_ci#include "distributed_camera_errno.h" 251c1b0f19Sopenharmony_ci#include "distributed_hardware_log.h" 261c1b0f19Sopenharmony_ci 271c1b0f19Sopenharmony_cinamespace OHOS { 281c1b0f19Sopenharmony_cinamespace DistributedHardware { 291c1b0f19Sopenharmony_ciDCameraSoftbusSession::DCameraSoftbusSession() 301c1b0f19Sopenharmony_ci{ 311c1b0f19Sopenharmony_ci sessionId_ = -1; 321c1b0f19Sopenharmony_ci state_ = DCAMERA_SOFTBUS_STATE_CLOSED; 331c1b0f19Sopenharmony_ci mode_ = DCAMERA_SESSION_MODE_CTRL; 341c1b0f19Sopenharmony_ci ResetAssembleFrag(); 351c1b0f19Sopenharmony_ci} 361c1b0f19Sopenharmony_ci 371c1b0f19Sopenharmony_ciDCameraSoftbusSession::DCameraSoftbusSession(std::string myDevId, std::string mySessionName, std::string peerDevId, 381c1b0f19Sopenharmony_ci std::string peerSessionName, std::shared_ptr<ICameraChannelListener> listener, DCameraSessionMode mode) 391c1b0f19Sopenharmony_ci : myDevId_(myDevId), mySessionName_(mySessionName), peerDevId_(peerDevId), peerSessionName_(peerSessionName), 401c1b0f19Sopenharmony_ci listener_(listener), sessionId_(-1), state_(DCAMERA_SOFTBUS_STATE_CLOSED), mode_(mode) 411c1b0f19Sopenharmony_ci{ 421c1b0f19Sopenharmony_ci sendFuncMap_[DCAMERA_SESSION_MODE_CTRL] = &DCameraSoftbusSession::SendBytes; 431c1b0f19Sopenharmony_ci sendFuncMap_[DCAMERA_SESSION_MODE_VIDEO] = &DCameraSoftbusSession::SendStream; 441c1b0f19Sopenharmony_ci sendFuncMap_[DCAMERA_SESSION_MODE_JPEG] = &DCameraSoftbusSession::SendBytes; 451c1b0f19Sopenharmony_ci auto runner = AppExecFwk::EventRunner::Create(mySessionName); 461c1b0f19Sopenharmony_ci eventHandler_ = std::make_shared<AppExecFwk::EventHandler>(runner); 471c1b0f19Sopenharmony_ci ResetAssembleFrag(); 481c1b0f19Sopenharmony_ci} 491c1b0f19Sopenharmony_ci 501c1b0f19Sopenharmony_ciDCameraSoftbusSession::~DCameraSoftbusSession() 511c1b0f19Sopenharmony_ci{ 521c1b0f19Sopenharmony_ci if (sessionId_ != -1) { 531c1b0f19Sopenharmony_ci int32_t ret = DCameraSoftbusAdapter::GetInstance().CloseSoftbusSession(sessionId_); 541c1b0f19Sopenharmony_ci if (ret != DCAMERA_OK) { 551c1b0f19Sopenharmony_ci DHLOGE("DCameraSoftbusSession delete failed, ret: %{public}d, sessId: %{public}d peerDevId: %{public}s " 561c1b0f19Sopenharmony_ci "peerSessionName: %{public}s", ret, sessionId_, GetAnonyString(peerDevId_).c_str(), 571c1b0f19Sopenharmony_ci GetAnonyString(peerSessionName_).c_str()); 581c1b0f19Sopenharmony_ci } 591c1b0f19Sopenharmony_ci } 601c1b0f19Sopenharmony_ci sendFuncMap_.clear(); 611c1b0f19Sopenharmony_ci eventHandler_ = nullptr; 621c1b0f19Sopenharmony_ci} 631c1b0f19Sopenharmony_ci 641c1b0f19Sopenharmony_ciint32_t DCameraSoftbusSession::CloseSession() 651c1b0f19Sopenharmony_ci{ 661c1b0f19Sopenharmony_ci DHLOGI("close session sessionId: %{public}d peerDevId: %{public}s peerSessionName: %{public}s", sessionId_, 671c1b0f19Sopenharmony_ci GetAnonyString(peerDevId_).c_str(), GetAnonyString(peerSessionName_).c_str()); 681c1b0f19Sopenharmony_ci if (sessionId_ == -1) { 691c1b0f19Sopenharmony_ci DHLOGI("current session has already close peerDevId: %{public}s peerSessionName: %{public}s", 701c1b0f19Sopenharmony_ci GetAnonyString(peerDevId_).c_str(), GetAnonyString(peerSessionName_).c_str()); 711c1b0f19Sopenharmony_ci return DCAMERA_OK; 721c1b0f19Sopenharmony_ci } 731c1b0f19Sopenharmony_ci int32_t ret = DCameraSoftbusAdapter::GetInstance().CloseSoftbusSession(sessionId_); 741c1b0f19Sopenharmony_ci if (ret != DCAMERA_OK) { 751c1b0f19Sopenharmony_ci DHLOGE("close session failed, ret: %{public}d, peerDevId: %{public}s peerSessionName: %{public}s", ret, 761c1b0f19Sopenharmony_ci GetAnonyString(peerDevId_).c_str(), GetAnonyString(peerSessionName_).c_str()); 771c1b0f19Sopenharmony_ci return ret; 781c1b0f19Sopenharmony_ci } 791c1b0f19Sopenharmony_ci 801c1b0f19Sopenharmony_ci sessionId_ = -1; 811c1b0f19Sopenharmony_ci state_ = DCAMERA_SOFTBUS_STATE_CLOSED; 821c1b0f19Sopenharmony_ci return DCAMERA_OK; 831c1b0f19Sopenharmony_ci} 841c1b0f19Sopenharmony_ci 851c1b0f19Sopenharmony_ciint32_t DCameraSoftbusSession::OnSessionOpened(int32_t socket, std::string networkId) 861c1b0f19Sopenharmony_ci{ 871c1b0f19Sopenharmony_ci DHLOGI("open current session start, socket: %{public}d", socket); 881c1b0f19Sopenharmony_ci sessionId_ = socket; 891c1b0f19Sopenharmony_ci state_ = DCAMERA_SOFTBUS_STATE_OPENED; 901c1b0f19Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(listener_ == nullptr, DCAMERA_BAD_VALUE, "listener_ is null."); 911c1b0f19Sopenharmony_ci listener_->OnSessionState(DCAMERA_CHANNEL_STATE_CONNECTED, networkId); 921c1b0f19Sopenharmony_ci DHLOGI("open current session end, socket: %{public}d", socket); 931c1b0f19Sopenharmony_ci return DCAMERA_OK; 941c1b0f19Sopenharmony_ci} 951c1b0f19Sopenharmony_ci 961c1b0f19Sopenharmony_ciint32_t DCameraSoftbusSession::OnSessionClose(int32_t sessionId) 971c1b0f19Sopenharmony_ci{ 981c1b0f19Sopenharmony_ci DHLOGI("OnSessionClose sessionId: %{public}d peerDevId: %{public}s peerSessionName: %{public}s", sessionId, 991c1b0f19Sopenharmony_ci GetAnonyString(peerDevId_).c_str(), GetAnonyString(peerSessionName_).c_str()); 1001c1b0f19Sopenharmony_ci sessionId_ = -1; 1011c1b0f19Sopenharmony_ci state_ = DCAMERA_SOFTBUS_STATE_CLOSED; 1021c1b0f19Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(listener_ == nullptr, DCAMERA_BAD_VALUE, "listener_ is null."); 1031c1b0f19Sopenharmony_ci listener_->OnSessionState(DCAMERA_CHANNEL_STATE_DISCONNECTED, ""); 1041c1b0f19Sopenharmony_ci return DCAMERA_OK; 1051c1b0f19Sopenharmony_ci} 1061c1b0f19Sopenharmony_ci 1071c1b0f19Sopenharmony_ciint32_t DCameraSoftbusSession::OnDataReceived(std::shared_ptr<DataBuffer>& buffer) 1081c1b0f19Sopenharmony_ci{ 1091c1b0f19Sopenharmony_ci auto recvDataFunc = [this, buffer]() mutable { 1101c1b0f19Sopenharmony_ci DealRecvData(buffer); 1111c1b0f19Sopenharmony_ci }; 1121c1b0f19Sopenharmony_ci if (eventHandler_ != nullptr) { 1131c1b0f19Sopenharmony_ci eventHandler_->PostTask(recvDataFunc); 1141c1b0f19Sopenharmony_ci } 1151c1b0f19Sopenharmony_ci return DCAMERA_OK; 1161c1b0f19Sopenharmony_ci} 1171c1b0f19Sopenharmony_ci 1181c1b0f19Sopenharmony_civoid DCameraSoftbusSession::DealRecvData(std::shared_ptr<DataBuffer>& buffer) 1191c1b0f19Sopenharmony_ci{ 1201c1b0f19Sopenharmony_ci if (mode_ == DCAMERA_SESSION_MODE_VIDEO) { 1211c1b0f19Sopenharmony_ci PostData(buffer); 1221c1b0f19Sopenharmony_ci return; 1231c1b0f19Sopenharmony_ci } 1241c1b0f19Sopenharmony_ci PackRecvData(buffer); 1251c1b0f19Sopenharmony_ci return; 1261c1b0f19Sopenharmony_ci} 1271c1b0f19Sopenharmony_ci 1281c1b0f19Sopenharmony_civoid DCameraSoftbusSession::PackRecvData(std::shared_ptr<DataBuffer>& buffer) 1291c1b0f19Sopenharmony_ci{ 1301c1b0f19Sopenharmony_ci if (buffer == nullptr) { 1311c1b0f19Sopenharmony_ci DHLOGE("Data buffer is null"); 1321c1b0f19Sopenharmony_ci return; 1331c1b0f19Sopenharmony_ci } 1341c1b0f19Sopenharmony_ci uint64_t bufferSize; 1351c1b0f19Sopenharmony_ci if (buffer->Size() < BINARY_HEADER_FRAG_LEN) { 1361c1b0f19Sopenharmony_ci bufferSize = static_cast<uint64_t>(buffer->Size()); 1371c1b0f19Sopenharmony_ci DHLOGE("pack recv data error, size: %{public}" PRIu64", sess: %{public}s peerSess: %{public}s", 1381c1b0f19Sopenharmony_ci bufferSize, mySessionName_.c_str(), peerSessionName_.c_str()); 1391c1b0f19Sopenharmony_ci return; 1401c1b0f19Sopenharmony_ci } 1411c1b0f19Sopenharmony_ci uint8_t *ptrPacket = buffer->Data(); 1421c1b0f19Sopenharmony_ci SessionDataHeader headerPara; 1431c1b0f19Sopenharmony_ci GetFragDataLen(ptrPacket, headerPara); 1441c1b0f19Sopenharmony_ci if (buffer->Size() != (headerPara.dataLen + BINARY_HEADER_FRAG_LEN) || headerPara.dataLen > headerPara.totalLen || 1451c1b0f19Sopenharmony_ci headerPara.dataLen > BINARY_DATA_MAX_LEN || headerPara.totalLen > BINARY_DATA_MAX_TOTAL_LEN) { 1461c1b0f19Sopenharmony_ci bufferSize = static_cast<uint64_t>(buffer->Size()); 1471c1b0f19Sopenharmony_ci DHLOGE("pack recv data failed, size: %{public}" PRIu64", dataLen: %{public}d, totalLen: %{public}d sess: " 1481c1b0f19Sopenharmony_ci "%{public}s peerSess: %{public}s", bufferSize, headerPara.dataLen, headerPara.totalLen, 1491c1b0f19Sopenharmony_ci mySessionName_.c_str(), peerSessionName_.c_str()); 1501c1b0f19Sopenharmony_ci return; 1511c1b0f19Sopenharmony_ci } 1521c1b0f19Sopenharmony_ci bufferSize = static_cast<uint64_t>(buffer->Size()); 1531c1b0f19Sopenharmony_ci DHLOGD("pack recv data Assemble, size: %{public}" PRIu64", dataLen: %{public}d, totalLen: %{public}d, nowTime: " 1541c1b0f19Sopenharmony_ci "%{public}" PRId64" start", bufferSize, headerPara.dataLen, headerPara.totalLen, GetNowTimeStampUs()); 1551c1b0f19Sopenharmony_ci if (headerPara.fragFlag == FRAG_START_END) { 1561c1b0f19Sopenharmony_ci AssembleNoFrag(buffer, headerPara); 1571c1b0f19Sopenharmony_ci } else { 1581c1b0f19Sopenharmony_ci AssembleFrag(buffer, headerPara); 1591c1b0f19Sopenharmony_ci } 1601c1b0f19Sopenharmony_ci bufferSize = static_cast<uint64_t>(buffer->Size()); 1611c1b0f19Sopenharmony_ci DHLOGD("pack recv data Assemble, size: %{public}" PRIu64", dataLen: %{public}d, totalLen: %{public}d, nowTime: " 1621c1b0f19Sopenharmony_ci "%{public}" PRId64" end", bufferSize, headerPara.dataLen, headerPara.totalLen, GetNowTimeStampUs()); 1631c1b0f19Sopenharmony_ci} 1641c1b0f19Sopenharmony_ci 1651c1b0f19Sopenharmony_civoid DCameraSoftbusSession::AssembleNoFrag(std::shared_ptr<DataBuffer>& buffer, SessionDataHeader& headerPara) 1661c1b0f19Sopenharmony_ci{ 1671c1b0f19Sopenharmony_ci if (headerPara.dataLen != headerPara.totalLen) { 1681c1b0f19Sopenharmony_ci DHLOGE("DCameraSoftbusSession PackRecvData failed, dataLen: %{public}d, totalLen: %{public}d, sess: " 1691c1b0f19Sopenharmony_ci "%{public}s peerSess: %{public}s", headerPara.dataLen, headerPara.totalLen, mySessionName_.c_str(), 1701c1b0f19Sopenharmony_ci peerSessionName_.c_str()); 1711c1b0f19Sopenharmony_ci return; 1721c1b0f19Sopenharmony_ci } 1731c1b0f19Sopenharmony_ci if (buffer == nullptr) { 1741c1b0f19Sopenharmony_ci DHLOGE("Data buffer is null"); 1751c1b0f19Sopenharmony_ci return; 1761c1b0f19Sopenharmony_ci } 1771c1b0f19Sopenharmony_ci std::shared_ptr<DataBuffer> postData = std::make_shared<DataBuffer>(headerPara.dataLen); 1781c1b0f19Sopenharmony_ci int32_t ret = memcpy_s(postData->Data(), postData->Size(), buffer->Data() + BINARY_HEADER_FRAG_LEN, 1791c1b0f19Sopenharmony_ci buffer->Size() - BINARY_HEADER_FRAG_LEN); 1801c1b0f19Sopenharmony_ci if (ret != EOK) { 1811c1b0f19Sopenharmony_ci DHLOGE("DCameraSoftbusSession PackRecvData failed, ret: %{public}d, sess: %{public}s peerSess: %{public}s", 1821c1b0f19Sopenharmony_ci ret, mySessionName_.c_str(), peerSessionName_.c_str()); 1831c1b0f19Sopenharmony_ci return; 1841c1b0f19Sopenharmony_ci } 1851c1b0f19Sopenharmony_ci PostData(postData); 1861c1b0f19Sopenharmony_ci} 1871c1b0f19Sopenharmony_ci 1881c1b0f19Sopenharmony_civoid DCameraSoftbusSession::AssembleFrag(std::shared_ptr<DataBuffer>& buffer, SessionDataHeader& headerPara) 1891c1b0f19Sopenharmony_ci{ 1901c1b0f19Sopenharmony_ci if (buffer == nullptr) { 1911c1b0f19Sopenharmony_ci DHLOGE("Data buffer is null"); 1921c1b0f19Sopenharmony_ci return; 1931c1b0f19Sopenharmony_ci } 1941c1b0f19Sopenharmony_ci if (headerPara.fragFlag == FRAG_START) { 1951c1b0f19Sopenharmony_ci isWaiting_ = true; 1961c1b0f19Sopenharmony_ci nowSeq_ = headerPara.seqNum; 1971c1b0f19Sopenharmony_ci nowSubSeq_ = headerPara.subSeq; 1981c1b0f19Sopenharmony_ci offset_ = 0; 1991c1b0f19Sopenharmony_ci totalLen_ = headerPara.totalLen; 2001c1b0f19Sopenharmony_ci packBuffer_ = std::make_shared<DataBuffer>(headerPara.totalLen); 2011c1b0f19Sopenharmony_ci int32_t ret = memcpy_s(packBuffer_->Data(), packBuffer_->Size(), buffer->Data() + BINARY_HEADER_FRAG_LEN, 2021c1b0f19Sopenharmony_ci buffer->Size() - BINARY_HEADER_FRAG_LEN); 2031c1b0f19Sopenharmony_ci if (ret != EOK) { 2041c1b0f19Sopenharmony_ci DHLOGE("DCameraSoftbusSession AssembleFrag failed, ret: %{public}d, sess: %{public}s peerSess: %{public}s", 2051c1b0f19Sopenharmony_ci ret, mySessionName_.c_str(), peerSessionName_.c_str()); 2061c1b0f19Sopenharmony_ci ResetAssembleFrag(); 2071c1b0f19Sopenharmony_ci return; 2081c1b0f19Sopenharmony_ci } 2091c1b0f19Sopenharmony_ci offset_ += headerPara.dataLen; 2101c1b0f19Sopenharmony_ci } 2111c1b0f19Sopenharmony_ci 2121c1b0f19Sopenharmony_ci if (headerPara.fragFlag == FRAG_MID || headerPara.fragFlag == FRAG_END) { 2131c1b0f19Sopenharmony_ci int32_t ret = CheckUnPackBuffer(headerPara); 2141c1b0f19Sopenharmony_ci if (ret != DCAMERA_OK) { 2151c1b0f19Sopenharmony_ci ResetAssembleFrag(); 2161c1b0f19Sopenharmony_ci return; 2171c1b0f19Sopenharmony_ci } 2181c1b0f19Sopenharmony_ci 2191c1b0f19Sopenharmony_ci nowSubSeq_ = headerPara.subSeq; 2201c1b0f19Sopenharmony_ci ret = memcpy_s(packBuffer_->Data() + offset_, packBuffer_->Size() - offset_, 2211c1b0f19Sopenharmony_ci buffer->Data() + BINARY_HEADER_FRAG_LEN, buffer->Size() - BINARY_HEADER_FRAG_LEN); 2221c1b0f19Sopenharmony_ci if (ret != EOK) { 2231c1b0f19Sopenharmony_ci DHLOGE("DCameraSoftbusSession AssembleFrag failed, memcpy_s ret: %{public}d, sess: %{public}s peerSess: " 2241c1b0f19Sopenharmony_ci "%{public}s", ret, mySessionName_.c_str(), peerSessionName_.c_str()); 2251c1b0f19Sopenharmony_ci ResetAssembleFrag(); 2261c1b0f19Sopenharmony_ci return; 2271c1b0f19Sopenharmony_ci } 2281c1b0f19Sopenharmony_ci offset_ += headerPara.dataLen; 2291c1b0f19Sopenharmony_ci } 2301c1b0f19Sopenharmony_ci 2311c1b0f19Sopenharmony_ci if (headerPara.fragFlag == FRAG_END) { 2321c1b0f19Sopenharmony_ci PostData(packBuffer_); 2331c1b0f19Sopenharmony_ci ResetAssembleFrag(); 2341c1b0f19Sopenharmony_ci } 2351c1b0f19Sopenharmony_ci} 2361c1b0f19Sopenharmony_ci 2371c1b0f19Sopenharmony_ciint32_t DCameraSoftbusSession::CheckUnPackBuffer(SessionDataHeader& headerPara) 2381c1b0f19Sopenharmony_ci{ 2391c1b0f19Sopenharmony_ci if (!isWaiting_) { 2401c1b0f19Sopenharmony_ci DHLOGE("DCameraSoftbusSession AssembleFrag failed, not start one, sess: %{public}s peerSess: %{public}s", 2411c1b0f19Sopenharmony_ci mySessionName_.c_str(), peerSessionName_.c_str()); 2421c1b0f19Sopenharmony_ci return DCAMERA_BAD_VALUE; 2431c1b0f19Sopenharmony_ci } 2441c1b0f19Sopenharmony_ci 2451c1b0f19Sopenharmony_ci if (nowSeq_ != headerPara.seqNum) { 2461c1b0f19Sopenharmony_ci DHLOGE("DCameraSoftbusSession AssembleFrag seq error nowSeq: %{public}d actualSeq: %{public}d, sess: " 2471c1b0f19Sopenharmony_ci "%{public}s peerSess: %{public}s", nowSeq_, headerPara.seqNum, mySessionName_.c_str(), 2481c1b0f19Sopenharmony_ci peerSessionName_.c_str()); 2491c1b0f19Sopenharmony_ci return DCAMERA_BAD_VALUE; 2501c1b0f19Sopenharmony_ci } 2511c1b0f19Sopenharmony_ci 2521c1b0f19Sopenharmony_ci if (nowSubSeq_ + 1 != headerPara.subSeq) { 2531c1b0f19Sopenharmony_ci DHLOGE("DCameraSoftbusSession AssembleFrag subSeq error nowSeq: %{public}d actualSeq: %{public}d, " 2541c1b0f19Sopenharmony_ci "sess: %{public}s peerSess: %{public}s", nowSubSeq_, headerPara.subSeq, mySessionName_.c_str(), 2551c1b0f19Sopenharmony_ci peerSessionName_.c_str()); 2561c1b0f19Sopenharmony_ci return DCAMERA_BAD_VALUE; 2571c1b0f19Sopenharmony_ci } 2581c1b0f19Sopenharmony_ci 2591c1b0f19Sopenharmony_ci if (totalLen_ < headerPara.dataLen + offset_) { 2601c1b0f19Sopenharmony_ci DHLOGE("DCameraSoftbusSession AssembleFrag len error cap: %{public}d size: %{public}d, dataLen: " 2611c1b0f19Sopenharmony_ci "%{public}d sess: %{public}s peerSess: %{public}s", totalLen_, offset_, headerPara.dataLen, 2621c1b0f19Sopenharmony_ci mySessionName_.c_str(), peerSessionName_.c_str()); 2631c1b0f19Sopenharmony_ci return DCAMERA_BAD_VALUE; 2641c1b0f19Sopenharmony_ci } 2651c1b0f19Sopenharmony_ci return DCAMERA_OK; 2661c1b0f19Sopenharmony_ci} 2671c1b0f19Sopenharmony_ci 2681c1b0f19Sopenharmony_civoid DCameraSoftbusSession::ResetAssembleFrag() 2691c1b0f19Sopenharmony_ci{ 2701c1b0f19Sopenharmony_ci isWaiting_ = false; 2711c1b0f19Sopenharmony_ci nowSeq_ = 0; 2721c1b0f19Sopenharmony_ci nowSubSeq_ = 0; 2731c1b0f19Sopenharmony_ci offset_ = 0; 2741c1b0f19Sopenharmony_ci totalLen_ = 0; 2751c1b0f19Sopenharmony_ci packBuffer_ = nullptr; 2761c1b0f19Sopenharmony_ci} 2771c1b0f19Sopenharmony_ci 2781c1b0f19Sopenharmony_civoid DCameraSoftbusSession::PostData(std::shared_ptr<DataBuffer>& buffer) 2791c1b0f19Sopenharmony_ci{ 2801c1b0f19Sopenharmony_ci std::vector<std::shared_ptr<DataBuffer>> buffers; 2811c1b0f19Sopenharmony_ci buffers.push_back(buffer); 2821c1b0f19Sopenharmony_ci CHECK_AND_RETURN_LOG(listener_ == nullptr, "listener_ is null."); 2831c1b0f19Sopenharmony_ci listener_->OnDataReceived(buffers); 2841c1b0f19Sopenharmony_ci} 2851c1b0f19Sopenharmony_ci 2861c1b0f19Sopenharmony_civoid DCameraSoftbusSession::GetFragDataLen(uint8_t *ptrPacket, SessionDataHeader& headerPara) 2871c1b0f19Sopenharmony_ci{ 2881c1b0f19Sopenharmony_ci headerPara.version = U16Get(ptrPacket); 2891c1b0f19Sopenharmony_ci headerPara.fragFlag = ptrPacket[BINARY_HEADER_FRAG_OFFSET]; 2901c1b0f19Sopenharmony_ci headerPara.dataType = U32Get(ptrPacket + BINARY_HEADER_DATATYPE_OFFSET); 2911c1b0f19Sopenharmony_ci headerPara.seqNum = U32Get(ptrPacket + BINARY_HEADER_SEQNUM_OFFSET); 2921c1b0f19Sopenharmony_ci headerPara.totalLen = U32Get(ptrPacket + BINARY_HEADER_TOTALLEN_OFFSET); 2931c1b0f19Sopenharmony_ci headerPara.subSeq = U16Get(ptrPacket + BINARY_HEADER_SUBSEQ_OFFSET); 2941c1b0f19Sopenharmony_ci headerPara.dataLen = U32Get(ptrPacket + BINARY_HEADER_DATALEN_OFFSET); 2951c1b0f19Sopenharmony_ci} 2961c1b0f19Sopenharmony_ci 2971c1b0f19Sopenharmony_ciuint16_t DCameraSoftbusSession::U16Get(const uint8_t *ptr) 2981c1b0f19Sopenharmony_ci{ 2991c1b0f19Sopenharmony_ci return (ptr[0] << DCAMERA_SHIFT_8) | ptr[1]; 3001c1b0f19Sopenharmony_ci} 3011c1b0f19Sopenharmony_ci 3021c1b0f19Sopenharmony_ciuint32_t DCameraSoftbusSession::U32Get(const uint8_t *ptr) 3031c1b0f19Sopenharmony_ci{ 3041c1b0f19Sopenharmony_ci return (ptr[0] << DCAMERA_SHIFT_24) | (ptr[1] << DCAMERA_SHIFT_16) | (ptr[2] << DCAMERA_SHIFT_8) | ptr[3]; 3051c1b0f19Sopenharmony_ci} 3061c1b0f19Sopenharmony_ci 3071c1b0f19Sopenharmony_ciint32_t DCameraSoftbusSession::SendData(DCameraSessionMode mode, std::shared_ptr<DataBuffer>& buffer) 3081c1b0f19Sopenharmony_ci{ 3091c1b0f19Sopenharmony_ci auto itFunc = sendFuncMap_.find(mode); 3101c1b0f19Sopenharmony_ci if (itFunc == sendFuncMap_.end()) { 3111c1b0f19Sopenharmony_ci return DCAMERA_NOT_FOUND; 3121c1b0f19Sopenharmony_ci } 3131c1b0f19Sopenharmony_ci auto memberFunc = itFunc->second; 3141c1b0f19Sopenharmony_ci switch (mode) { 3151c1b0f19Sopenharmony_ci case DCAMERA_SESSION_MODE_VIDEO: 3161c1b0f19Sopenharmony_ci return SendStream(buffer); 3171c1b0f19Sopenharmony_ci case DCAMERA_SESSION_MODE_CTRL: 3181c1b0f19Sopenharmony_ci case DCAMERA_SESSION_MODE_JPEG: 3191c1b0f19Sopenharmony_ci return UnPackSendData(buffer, memberFunc); 3201c1b0f19Sopenharmony_ci default: 3211c1b0f19Sopenharmony_ci return UnPackSendData(buffer, memberFunc); 3221c1b0f19Sopenharmony_ci } 3231c1b0f19Sopenharmony_ci return DCAMERA_NOT_FOUND; 3241c1b0f19Sopenharmony_ci} 3251c1b0f19Sopenharmony_ci 3261c1b0f19Sopenharmony_ciint32_t DCameraSoftbusSession::CreateSocketServer() 3271c1b0f19Sopenharmony_ci{ 3281c1b0f19Sopenharmony_ci int32_t ret = DCameraSoftbusAdapter::GetInstance().CreatSoftBusSinkSocketServer(mySessionName_, 3291c1b0f19Sopenharmony_ci DCAMERA_CHANNLE_ROLE_SINK, mode_, peerDevId_, peerSessionName_); 3301c1b0f19Sopenharmony_ci if (ret != DCAMERA_OK) { 3311c1b0f19Sopenharmony_ci DHLOGE("DCameraSoftbusSession CreateSocketServer Error, ret %{public}d", ret); 3321c1b0f19Sopenharmony_ci return ret; 3331c1b0f19Sopenharmony_ci } 3341c1b0f19Sopenharmony_ci return DCAMERA_OK; 3351c1b0f19Sopenharmony_ci} 3361c1b0f19Sopenharmony_ci 3371c1b0f19Sopenharmony_ciint32_t DCameraSoftbusSession::BindSocketServer() 3381c1b0f19Sopenharmony_ci{ 3391c1b0f19Sopenharmony_ci int32_t socketId = DCameraSoftbusAdapter::GetInstance().CreateSoftBusSourceSocketClient(myDevId_, peerSessionName_, 3401c1b0f19Sopenharmony_ci peerDevId_, mode_, DCAMERA_CHANNLE_ROLE_SOURCE); 3411c1b0f19Sopenharmony_ci if (socketId <= 0) { 3421c1b0f19Sopenharmony_ci DHLOGE("DCameraSoftbusSession BindSocketServer Error, socketId %{public}d", socketId); 3431c1b0f19Sopenharmony_ci return socketId; 3441c1b0f19Sopenharmony_ci } 3451c1b0f19Sopenharmony_ci OnSessionOpened(socketId, myDevId_); 3461c1b0f19Sopenharmony_ci return socketId; 3471c1b0f19Sopenharmony_ci} 3481c1b0f19Sopenharmony_ci 3491c1b0f19Sopenharmony_civoid DCameraSoftbusSession::ReleaseSession() 3501c1b0f19Sopenharmony_ci{ 3511c1b0f19Sopenharmony_ci DCameraSoftbusAdapter::GetInstance().DestroySoftbusSessionServer(mySessionName_); 3521c1b0f19Sopenharmony_ci} 3531c1b0f19Sopenharmony_ci 3541c1b0f19Sopenharmony_ciint32_t DCameraSoftbusSession::UnPackSendData(std::shared_ptr<DataBuffer>& buffer, DCameraSendFuc memberFunc) 3551c1b0f19Sopenharmony_ci{ 3561c1b0f19Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(buffer == nullptr, DCAMERA_BAD_VALUE, "Data buffer is null"); 3571c1b0f19Sopenharmony_ci uint16_t subSeq = 0; 3581c1b0f19Sopenharmony_ci uint32_t seq = 0; 3591c1b0f19Sopenharmony_ci uint32_t totalLen = buffer->Size(); 3601c1b0f19Sopenharmony_ci SessionDataHeader headPara = { PROTOCOL_VERSION, FRAG_START, mode_, seq, totalLen, subSeq }; 3611c1b0f19Sopenharmony_ci if (buffer->Size() <= BINARY_DATA_PACKET_MAX_LEN) { 3621c1b0f19Sopenharmony_ci headPara.fragFlag = FRAG_START_END; 3631c1b0f19Sopenharmony_ci headPara.dataLen = buffer->Size(); 3641c1b0f19Sopenharmony_ci std::shared_ptr<DataBuffer> unpackData = std::make_shared<DataBuffer>(buffer->Size() + BINARY_HEADER_FRAG_LEN); 3651c1b0f19Sopenharmony_ci MakeFragDataHeader(headPara, unpackData->Data(), BINARY_HEADER_FRAG_LEN); 3661c1b0f19Sopenharmony_ci int32_t ret = memcpy_s(unpackData->Data() + BINARY_HEADER_FRAG_LEN, unpackData->Size() - BINARY_HEADER_FRAG_LEN, 3671c1b0f19Sopenharmony_ci buffer->Data(), buffer->Size()); 3681c1b0f19Sopenharmony_ci if (ret != EOK) { 3691c1b0f19Sopenharmony_ci DHLOGE("DCameraSoftbusSession UnPackSendData START_END memcpy_s failed, ret: %{public}d, sess: %{public}s " 3701c1b0f19Sopenharmony_ci "peerSess: %{public}s", ret, mySessionName_.c_str(), peerSessionName_.c_str()); 3711c1b0f19Sopenharmony_ci return ret; 3721c1b0f19Sopenharmony_ci } 3731c1b0f19Sopenharmony_ci return SendBytes(unpackData); 3741c1b0f19Sopenharmony_ci } 3751c1b0f19Sopenharmony_ci uint32_t offset = 0; 3761c1b0f19Sopenharmony_ci while (totalLen > offset) { 3771c1b0f19Sopenharmony_ci SetHeadParaDataLen(headPara, totalLen, offset); 3781c1b0f19Sopenharmony_ci uint64_t bufferSize = static_cast<uint64_t>(buffer->Size()); 3791c1b0f19Sopenharmony_ci DHLOGD("DCameraSoftbusSession UnPackSendData, size: %" PRIu64", dataLen: %{public}d, totalLen: %{public}d, " 3801c1b0f19Sopenharmony_ci "nowTime: %{public}" PRId64" start:", bufferSize, headPara.dataLen, headPara.totalLen, GetNowTimeStampUs()); 3811c1b0f19Sopenharmony_ci std::shared_ptr<DataBuffer> unpackData = 3821c1b0f19Sopenharmony_ci std::make_shared<DataBuffer>(headPara.dataLen + BINARY_HEADER_FRAG_LEN); 3831c1b0f19Sopenharmony_ci MakeFragDataHeader(headPara, unpackData->Data(), BINARY_HEADER_FRAG_LEN); 3841c1b0f19Sopenharmony_ci int ret = memcpy_s(unpackData->Data() + BINARY_HEADER_FRAG_LEN, unpackData->Size() - BINARY_HEADER_FRAG_LEN, 3851c1b0f19Sopenharmony_ci buffer->Data() + offset, headPara.dataLen); 3861c1b0f19Sopenharmony_ci if (ret != EOK) { 3871c1b0f19Sopenharmony_ci DHLOGE("DCameraSoftbusSession UnPackSendData memcpy_s failed, ret: %{public}d, sess: %{public}s peerSess: " 3881c1b0f19Sopenharmony_ci "%{public}s", ret, mySessionName_.c_str(), peerSessionName_.c_str()); 3891c1b0f19Sopenharmony_ci return ret; 3901c1b0f19Sopenharmony_ci } 3911c1b0f19Sopenharmony_ci ret = SendBytes(unpackData); 3921c1b0f19Sopenharmony_ci if (ret != DCAMERA_OK) { 3931c1b0f19Sopenharmony_ci DHLOGE("DCameraSoftbusSession sendData failed, ret: %{public}d, sess: %{public}s peerSess: %{public}s", 3941c1b0f19Sopenharmony_ci ret, mySessionName_.c_str(), peerSessionName_.c_str()); 3951c1b0f19Sopenharmony_ci return ret; 3961c1b0f19Sopenharmony_ci } 3971c1b0f19Sopenharmony_ci DHLOGD("DCameraSoftbusSession UnPackSendData, size: %" PRIu64", dataLen: %{public}d, totalLen: %{public}d, " 3981c1b0f19Sopenharmony_ci "nowTime: %{public}" PRId64" end:", bufferSize, headPara.dataLen, headPara.totalLen, GetNowTimeStampUs()); 3991c1b0f19Sopenharmony_ci headPara.subSeq++; 4001c1b0f19Sopenharmony_ci headPara.fragFlag = FRAG_MID; 4011c1b0f19Sopenharmony_ci offset += headPara.dataLen; 4021c1b0f19Sopenharmony_ci } 4031c1b0f19Sopenharmony_ci return DCAMERA_OK; 4041c1b0f19Sopenharmony_ci} 4051c1b0f19Sopenharmony_ci 4061c1b0f19Sopenharmony_civoid DCameraSoftbusSession::SetHeadParaDataLen(SessionDataHeader& headPara, const uint32_t totalLen, 4071c1b0f19Sopenharmony_ci const uint32_t offset) 4081c1b0f19Sopenharmony_ci{ 4091c1b0f19Sopenharmony_ci if (totalLen >= offset) { 4101c1b0f19Sopenharmony_ci if (totalLen - offset > BINARY_DATA_PACKET_MAX_LEN) { 4111c1b0f19Sopenharmony_ci headPara.dataLen = BINARY_DATA_PACKET_MAX_LEN - BINARY_DATA_PACKET_RESERVED_BUFFER; 4121c1b0f19Sopenharmony_ci } else { 4131c1b0f19Sopenharmony_ci headPara.fragFlag = FRAG_END; 4141c1b0f19Sopenharmony_ci headPara.dataLen = totalLen - offset; 4151c1b0f19Sopenharmony_ci } 4161c1b0f19Sopenharmony_ci } 4171c1b0f19Sopenharmony_ci} 4181c1b0f19Sopenharmony_ci 4191c1b0f19Sopenharmony_civoid DCameraSoftbusSession::MakeFragDataHeader(const SessionDataHeader& headPara, uint8_t *header, uint32_t len) 4201c1b0f19Sopenharmony_ci{ 4211c1b0f19Sopenharmony_ci uint32_t headerLen = sizeof(uint8_t) * HEADER_UINT8_NUM + sizeof(uint16_t) * HEADER_UINT16_NUM + 4221c1b0f19Sopenharmony_ci sizeof(uint32_t) * HEADER_UINT32_NUM; 4231c1b0f19Sopenharmony_ci if (headerLen > len) { 4241c1b0f19Sopenharmony_ci DHLOGE("MakeFragDataHeader %{public}d over len %{public}d", headerLen, len); 4251c1b0f19Sopenharmony_ci return; 4261c1b0f19Sopenharmony_ci } 4271c1b0f19Sopenharmony_ci uint32_t i = 0; 4281c1b0f19Sopenharmony_ci header[i++] = headPara.version >> DCAMERA_SHIFT_8; 4291c1b0f19Sopenharmony_ci header[i++] = headPara.version & UINT16_SHIFT_MASK_0; 4301c1b0f19Sopenharmony_ci header[i++] = headPara.fragFlag; 4311c1b0f19Sopenharmony_ci header[i++] = (headPara.dataType & UINT32_SHIFT_MASK_24) >> DCAMERA_SHIFT_24; 4321c1b0f19Sopenharmony_ci header[i++] = (headPara.dataType & UINT32_SHIFT_MASK_16) >> DCAMERA_SHIFT_16; 4331c1b0f19Sopenharmony_ci header[i++] = (headPara.dataType & UINT32_SHIFT_MASK_8) >> DCAMERA_SHIFT_8; 4341c1b0f19Sopenharmony_ci header[i++] = (headPara.dataType & UINT32_SHIFT_MASK_0); 4351c1b0f19Sopenharmony_ci header[i++] = (headPara.seqNum & UINT32_SHIFT_MASK_24) >> DCAMERA_SHIFT_24; 4361c1b0f19Sopenharmony_ci header[i++] = (headPara.seqNum & UINT32_SHIFT_MASK_16) >> DCAMERA_SHIFT_16; 4371c1b0f19Sopenharmony_ci header[i++] = (headPara.seqNum & UINT32_SHIFT_MASK_8) >> DCAMERA_SHIFT_8; 4381c1b0f19Sopenharmony_ci header[i++] = (headPara.seqNum & UINT32_SHIFT_MASK_0); 4391c1b0f19Sopenharmony_ci header[i++] = (headPara.totalLen & UINT32_SHIFT_MASK_24) >> DCAMERA_SHIFT_24; 4401c1b0f19Sopenharmony_ci header[i++] = (headPara.totalLen & UINT32_SHIFT_MASK_16) >> DCAMERA_SHIFT_16; 4411c1b0f19Sopenharmony_ci header[i++] = (headPara.totalLen & UINT32_SHIFT_MASK_8) >> DCAMERA_SHIFT_8; 4421c1b0f19Sopenharmony_ci header[i++] = (headPara.totalLen & UINT32_SHIFT_MASK_0); 4431c1b0f19Sopenharmony_ci header[i++] = headPara.subSeq >> DCAMERA_SHIFT_8; 4441c1b0f19Sopenharmony_ci header[i++] = headPara.subSeq & UINT16_SHIFT_MASK_0; 4451c1b0f19Sopenharmony_ci header[i++] = (headPara.dataLen & UINT32_SHIFT_MASK_24) >> DCAMERA_SHIFT_24; 4461c1b0f19Sopenharmony_ci header[i++] = (headPara.dataLen & UINT32_SHIFT_MASK_16) >> DCAMERA_SHIFT_16; 4471c1b0f19Sopenharmony_ci header[i++] = (headPara.dataLen & UINT32_SHIFT_MASK_8) >> DCAMERA_SHIFT_8; 4481c1b0f19Sopenharmony_ci header[i++] = (headPara.dataLen & UINT32_SHIFT_MASK_0); 4491c1b0f19Sopenharmony_ci} 4501c1b0f19Sopenharmony_ci 4511c1b0f19Sopenharmony_ciint32_t DCameraSoftbusSession::SendBytes(std::shared_ptr<DataBuffer>& buffer) 4521c1b0f19Sopenharmony_ci{ 4531c1b0f19Sopenharmony_ci if (state_ != DCAMERA_SOFTBUS_STATE_OPENED) { 4541c1b0f19Sopenharmony_ci DHLOGE("DCameraSoftbusSession SendBytes session state %{public}d is not opened sessionId: %{public}d " 4551c1b0f19Sopenharmony_ci "peerDev: %{public}s peerName: %{public}s", state_, sessionId_, GetAnonyString(peerDevId_).c_str(), 4561c1b0f19Sopenharmony_ci GetAnonyString(peerSessionName_).c_str()); 4571c1b0f19Sopenharmony_ci return DCAMERA_WRONG_STATE; 4581c1b0f19Sopenharmony_ci } 4591c1b0f19Sopenharmony_ci 4601c1b0f19Sopenharmony_ci int32_t ret = DCameraSoftbusAdapter::GetInstance().SendSofbusBytes(sessionId_, buffer); 4611c1b0f19Sopenharmony_ci if (ret != DCAMERA_OK) { 4621c1b0f19Sopenharmony_ci DHLOGE("DCameraSoftbusSession SendBytes sessionId: %{public}d failed: %{public}d peerDevId: %{public}s " 4631c1b0f19Sopenharmony_ci "peerSessionName: %{public}s", sessionId_, ret, GetAnonyString(peerDevId_).c_str(), 4641c1b0f19Sopenharmony_ci GetAnonyString(peerSessionName_).c_str()); 4651c1b0f19Sopenharmony_ci } 4661c1b0f19Sopenharmony_ci return ret; 4671c1b0f19Sopenharmony_ci} 4681c1b0f19Sopenharmony_ci 4691c1b0f19Sopenharmony_ciint32_t DCameraSoftbusSession::SendStream(std::shared_ptr<DataBuffer>& buffer) 4701c1b0f19Sopenharmony_ci{ 4711c1b0f19Sopenharmony_ci if (state_ != DCAMERA_SOFTBUS_STATE_OPENED) { 4721c1b0f19Sopenharmony_ci DHLOGE("DCameraSoftbusSession SendStream session state %{public}d is not opened sessionId: %{public}d " 4731c1b0f19Sopenharmony_ci "peerDev: %{public}s peerName: %{public}s", state_, sessionId_, GetAnonyString(peerDevId_).c_str(), 4741c1b0f19Sopenharmony_ci GetAnonyString(peerSessionName_).c_str()); 4751c1b0f19Sopenharmony_ci return DCAMERA_WRONG_STATE; 4761c1b0f19Sopenharmony_ci } 4771c1b0f19Sopenharmony_ci 4781c1b0f19Sopenharmony_ci int32_t ret = DCameraSoftbusAdapter::GetInstance().SendSofbusStream(sessionId_, buffer); 4791c1b0f19Sopenharmony_ci if (ret != DCAMERA_OK) { 4801c1b0f19Sopenharmony_ci DHLOGE("DCameraSoftbusSession SendStream sessionId: %{public}d failed: %{public}d peerDevId: %{public}s " 4811c1b0f19Sopenharmony_ci "peerSessionName: %{public}s", sessionId_, ret, GetAnonyString(peerDevId_).c_str(), 4821c1b0f19Sopenharmony_ci GetAnonyString(peerSessionName_).c_str()); 4831c1b0f19Sopenharmony_ci } 4841c1b0f19Sopenharmony_ci return ret; 4851c1b0f19Sopenharmony_ci} 4861c1b0f19Sopenharmony_ci 4871c1b0f19Sopenharmony_cistd::string DCameraSoftbusSession::GetPeerDevId() 4881c1b0f19Sopenharmony_ci{ 4891c1b0f19Sopenharmony_ci return peerDevId_; 4901c1b0f19Sopenharmony_ci} 4911c1b0f19Sopenharmony_ci 4921c1b0f19Sopenharmony_cistd::string DCameraSoftbusSession::GetPeerSessionName() 4931c1b0f19Sopenharmony_ci{ 4941c1b0f19Sopenharmony_ci return peerSessionName_; 4951c1b0f19Sopenharmony_ci} 4961c1b0f19Sopenharmony_ci 4971c1b0f19Sopenharmony_cistd::string DCameraSoftbusSession::GetMySessionName() 4981c1b0f19Sopenharmony_ci{ 4991c1b0f19Sopenharmony_ci return mySessionName_; 5001c1b0f19Sopenharmony_ci} 5011c1b0f19Sopenharmony_ci 5021c1b0f19Sopenharmony_ciint32_t DCameraSoftbusSession::GetSessionId() 5031c1b0f19Sopenharmony_ci{ 5041c1b0f19Sopenharmony_ci return sessionId_; 5051c1b0f19Sopenharmony_ci} 5061c1b0f19Sopenharmony_ci} // namespace DistributedHardware 5071c1b0f19Sopenharmony_ci} // namespace OHOS 508