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 "anonymous_string.h"
171c1b0f19Sopenharmony_ci#include "dcamera_hisysevent_adapter.h"
181c1b0f19Sopenharmony_ci#include "dcamera_sink_frame_info.h"
191c1b0f19Sopenharmony_ci#include "dcamera_softbus_adapter.h"
201c1b0f19Sopenharmony_ci#include "distributed_camera_constants.h"
211c1b0f19Sopenharmony_ci#include "distributed_camera_errno.h"
221c1b0f19Sopenharmony_ci#include "distributed_hardware_log.h"
231c1b0f19Sopenharmony_ci#include <securec.h>
241c1b0f19Sopenharmony_ci#include "softbus_bus_center.h"
251c1b0f19Sopenharmony_ci#include "softbus_common.h"
261c1b0f19Sopenharmony_ci#include "softbus_error_code.h"
271c1b0f19Sopenharmony_ci#include "dcamera_utils_tools.h"
281c1b0f19Sopenharmony_ci#include "dcamera_frame_info.h"
291c1b0f19Sopenharmony_ci
301c1b0f19Sopenharmony_cinamespace OHOS {
311c1b0f19Sopenharmony_cinamespace DistributedHardware {
321c1b0f19Sopenharmony_cinamespace {
331c1b0f19Sopenharmony_cistatic QosTV g_qosInfo[] = {
341c1b0f19Sopenharmony_ci    { .qos = QOS_TYPE_MIN_BW, .value = DCAMERA_QOS_TYPE_MIN_BW },
351c1b0f19Sopenharmony_ci    { .qos = QOS_TYPE_MAX_LATENCY, .value = DCAMERA_QOS_TYPE_MAX_LATENCY },
361c1b0f19Sopenharmony_ci    { .qos = QOS_TYPE_MIN_LATENCY, .value = DCAMERA_QOS_TYPE_MIN_LATENCY}
371c1b0f19Sopenharmony_ci};
381c1b0f19Sopenharmony_cistatic uint32_t g_QosTV_Param_Index = static_cast<uint32_t>(sizeof(g_qosInfo) / sizeof(QosTV));
391c1b0f19Sopenharmony_ciconst static std::pair<std::string, std::string> LOCAL_TO_PEER_SESS_NAME_MAP[] = {
401c1b0f19Sopenharmony_ci    {SESSION_HEAD + CAMERA_ID_PREFIX + DEVICE_ID_0 + RECEIVER_SESSION_NAME_CONTROL,
411c1b0f19Sopenharmony_ci     SESSION_HEAD + CAMERA_ID_PREFIX + DEVICE_ID_0 + SENDER_SESSION_NAME_CONTROL},
421c1b0f19Sopenharmony_ci    {SESSION_HEAD + CAMERA_ID_PREFIX + DEVICE_ID_0 + RECEIVER_SESSION_NAME_DATA_SNAPSHOT,
431c1b0f19Sopenharmony_ci     SESSION_HEAD + CAMERA_ID_PREFIX + DEVICE_ID_0 + SENDER_SESSION_NAME_DATA_SNAPSHOT},
441c1b0f19Sopenharmony_ci    {SESSION_HEAD + CAMERA_ID_PREFIX + DEVICE_ID_0 + RECEIVER_SESSION_NAME_DATA_CONTINUE,
451c1b0f19Sopenharmony_ci     SESSION_HEAD + CAMERA_ID_PREFIX + DEVICE_ID_0 + SENDER_SESSION_NAME_DATA_CONTINUE},
461c1b0f19Sopenharmony_ci    {SESSION_HEAD + CAMERA_ID_PREFIX + DEVICE_ID_1 + RECEIVER_SESSION_NAME_CONTROL,
471c1b0f19Sopenharmony_ci     SESSION_HEAD + CAMERA_ID_PREFIX + DEVICE_ID_1 + SENDER_SESSION_NAME_CONTROL},
481c1b0f19Sopenharmony_ci    {SESSION_HEAD + CAMERA_ID_PREFIX + DEVICE_ID_1 + RECEIVER_SESSION_NAME_DATA_SNAPSHOT,
491c1b0f19Sopenharmony_ci     SESSION_HEAD + CAMERA_ID_PREFIX + DEVICE_ID_1 + SENDER_SESSION_NAME_DATA_SNAPSHOT},
501c1b0f19Sopenharmony_ci    {SESSION_HEAD + CAMERA_ID_PREFIX + DEVICE_ID_1 + RECEIVER_SESSION_NAME_DATA_CONTINUE,
511c1b0f19Sopenharmony_ci     SESSION_HEAD + CAMERA_ID_PREFIX + DEVICE_ID_1 + SENDER_SESSION_NAME_DATA_CONTINUE},
521c1b0f19Sopenharmony_ci};
531c1b0f19Sopenharmony_ci}
541c1b0f19Sopenharmony_ciIMPLEMENT_SINGLE_INSTANCE(DCameraSoftbusAdapter);
551c1b0f19Sopenharmony_ci
561c1b0f19Sopenharmony_cistatic void DCameraSourceOnBind(int32_t socket, PeerSocketInfo info)
571c1b0f19Sopenharmony_ci{
581c1b0f19Sopenharmony_ci    return;
591c1b0f19Sopenharmony_ci}
601c1b0f19Sopenharmony_ci
611c1b0f19Sopenharmony_cistatic void DCameraSourceOnShutDown(int32_t socket, ShutdownReason reason)
621c1b0f19Sopenharmony_ci{
631c1b0f19Sopenharmony_ci    DCameraSoftbusAdapter::GetInstance().SourceOnShutDown(socket, reason);
641c1b0f19Sopenharmony_ci    return;
651c1b0f19Sopenharmony_ci}
661c1b0f19Sopenharmony_ci
671c1b0f19Sopenharmony_cistatic void DCameraSourceOnBytes(int32_t socket, const void *data, uint32_t dataLen)
681c1b0f19Sopenharmony_ci{
691c1b0f19Sopenharmony_ci    DCameraSoftbusAdapter::GetInstance().SourceOnBytes(socket, data, dataLen);
701c1b0f19Sopenharmony_ci    return;
711c1b0f19Sopenharmony_ci}
721c1b0f19Sopenharmony_ci
731c1b0f19Sopenharmony_cistatic void DCameraSourceOnMessage(int32_t socket, const void *data, uint32_t dataLen)
741c1b0f19Sopenharmony_ci{
751c1b0f19Sopenharmony_ci    DCameraSoftbusAdapter::GetInstance().SourceOnMessage(socket, data, dataLen);
761c1b0f19Sopenharmony_ci    return;
771c1b0f19Sopenharmony_ci}
781c1b0f19Sopenharmony_ci
791c1b0f19Sopenharmony_cistatic void DCameraSourceOnStream(int32_t socket, const StreamData *data, const StreamData *ext,
801c1b0f19Sopenharmony_ci    const StreamFrameInfo *param)
811c1b0f19Sopenharmony_ci{
821c1b0f19Sopenharmony_ci    DCameraSoftbusAdapter::GetInstance().SourceOnStream(socket, data, ext, param);
831c1b0f19Sopenharmony_ci    return;
841c1b0f19Sopenharmony_ci}
851c1b0f19Sopenharmony_ci
861c1b0f19Sopenharmony_ci// sink
871c1b0f19Sopenharmony_cistatic void DCameraSinkOnBind(int32_t socket, PeerSocketInfo info)
881c1b0f19Sopenharmony_ci{
891c1b0f19Sopenharmony_ci    DCameraSoftbusAdapter::GetInstance().SinkOnBind(socket, info);
901c1b0f19Sopenharmony_ci    return;
911c1b0f19Sopenharmony_ci}
921c1b0f19Sopenharmony_ci
931c1b0f19Sopenharmony_cistatic void DCameraSinkOnShutDown(int32_t socket, ShutdownReason reason)
941c1b0f19Sopenharmony_ci{
951c1b0f19Sopenharmony_ci    DCameraSoftbusAdapter::GetInstance().SinkOnShutDown(socket, reason);
961c1b0f19Sopenharmony_ci    return;
971c1b0f19Sopenharmony_ci}
981c1b0f19Sopenharmony_ci
991c1b0f19Sopenharmony_cistatic void DCameraSinkOnBytes(int32_t socket, const void *data, uint32_t dataLen)
1001c1b0f19Sopenharmony_ci{
1011c1b0f19Sopenharmony_ci    DCameraSoftbusAdapter::GetInstance().SinkOnBytes(socket, data, dataLen);
1021c1b0f19Sopenharmony_ci    return;
1031c1b0f19Sopenharmony_ci}
1041c1b0f19Sopenharmony_ci
1051c1b0f19Sopenharmony_cistatic void DCameraSinkOnMessage(int32_t socket, const void *data, uint32_t dataLen)
1061c1b0f19Sopenharmony_ci{
1071c1b0f19Sopenharmony_ci    DCameraSoftbusAdapter::GetInstance().SinkOnMessage(socket, data, dataLen);
1081c1b0f19Sopenharmony_ci    return;
1091c1b0f19Sopenharmony_ci}
1101c1b0f19Sopenharmony_ci
1111c1b0f19Sopenharmony_cistatic void DCameraSinkOnStream(int32_t socket, const StreamData *data, const StreamData *ext,
1121c1b0f19Sopenharmony_ci    const StreamFrameInfo *param)
1131c1b0f19Sopenharmony_ci{
1141c1b0f19Sopenharmony_ci    DCameraSoftbusAdapter::GetInstance().SinkOnStream(socket, data, ext, param);
1151c1b0f19Sopenharmony_ci    return;
1161c1b0f19Sopenharmony_ci}
1171c1b0f19Sopenharmony_ci
1181c1b0f19Sopenharmony_ciDCameraSoftbusAdapter::DCameraSoftbusAdapter()
1191c1b0f19Sopenharmony_ci{
1201c1b0f19Sopenharmony_ci    sessionModeAndDataTypeMap_[DCAMERA_SESSION_MODE_CTRL] = TransDataType::DATA_TYPE_BYTES;
1211c1b0f19Sopenharmony_ci    sessionModeAndDataTypeMap_[DCAMERA_SESSION_MODE_VIDEO] = TransDataType::DATA_TYPE_VIDEO_STREAM;
1221c1b0f19Sopenharmony_ci    sessionModeAndDataTypeMap_[DCAMERA_SESSION_MODE_JPEG] = TransDataType::DATA_TYPE_BYTES;
1231c1b0f19Sopenharmony_ci
1241c1b0f19Sopenharmony_ci    ISocketListener sourceListener;
1251c1b0f19Sopenharmony_ci    sourceListener.OnBind = DCameraSourceOnBind;
1261c1b0f19Sopenharmony_ci    sourceListener.OnShutdown = DCameraSourceOnShutDown;
1271c1b0f19Sopenharmony_ci    sourceListener.OnBytes = DCameraSourceOnBytes;
1281c1b0f19Sopenharmony_ci    sourceListener.OnMessage = DCameraSourceOnMessage;
1291c1b0f19Sopenharmony_ci    sourceListener.OnStream = DCameraSourceOnStream;
1301c1b0f19Sopenharmony_ci    sessListeners_[DCAMERA_CHANNLE_ROLE_SOURCE] = sourceListener;
1311c1b0f19Sopenharmony_ci
1321c1b0f19Sopenharmony_ci    ISocketListener sinkListener;
1331c1b0f19Sopenharmony_ci    sinkListener.OnBind = DCameraSinkOnBind;
1341c1b0f19Sopenharmony_ci    sinkListener.OnShutdown = DCameraSinkOnShutDown;
1351c1b0f19Sopenharmony_ci    sinkListener.OnBytes = DCameraSinkOnBytes;
1361c1b0f19Sopenharmony_ci    sinkListener.OnMessage = DCameraSinkOnMessage;
1371c1b0f19Sopenharmony_ci    sinkListener.OnStream = DCameraSinkOnStream;
1381c1b0f19Sopenharmony_ci    sessListeners_[DCAMERA_CHANNLE_ROLE_SINK] = sinkListener;
1391c1b0f19Sopenharmony_ci}
1401c1b0f19Sopenharmony_ci
1411c1b0f19Sopenharmony_ciDCameraSoftbusAdapter::~DCameraSoftbusAdapter()
1421c1b0f19Sopenharmony_ci{
1431c1b0f19Sopenharmony_ci}
1441c1b0f19Sopenharmony_ci
1451c1b0f19Sopenharmony_ciint32_t DCameraSoftbusAdapter::CreatSoftBusSinkSocketServer(std::string mySessionName, DCAMERA_CHANNEL_ROLE role,
1461c1b0f19Sopenharmony_ci    DCameraSessionMode sessionMode, std::string peerDevId, std::string peerSessionName)
1471c1b0f19Sopenharmony_ci{
1481c1b0f19Sopenharmony_ci    DHLOGI("create socket server start, mySessionName: %{public}s,peerSessionName: %{public}s",
1491c1b0f19Sopenharmony_ci        GetAnonyString(mySessionName).c_str(), GetAnonyString(peerSessionName).c_str());
1501c1b0f19Sopenharmony_ci    SocketInfo serverSocketInfo {};
1511c1b0f19Sopenharmony_ci    if (!ManageSelectChannel::GetInstance().GetSinkConnect()) {
1521c1b0f19Sopenharmony_ci        serverSocketInfo = {
1531c1b0f19Sopenharmony_ci            .name =  const_cast<char*>(mySessionName.c_str()),
1541c1b0f19Sopenharmony_ci            .peerName = const_cast<char*>(peerSessionName.c_str()),
1551c1b0f19Sopenharmony_ci            .peerNetworkId = const_cast<char*>(peerDevId.c_str()),
1561c1b0f19Sopenharmony_ci            .pkgName = const_cast<char*>(PKG_NAME.c_str()),
1571c1b0f19Sopenharmony_ci            .dataType = sessionModeAndDataTypeMap_[sessionMode],
1581c1b0f19Sopenharmony_ci        };
1591c1b0f19Sopenharmony_ci    } else {
1601c1b0f19Sopenharmony_ci        serverSocketInfo = {
1611c1b0f19Sopenharmony_ci            .name =  const_cast<char*>(mySessionName.c_str()),
1621c1b0f19Sopenharmony_ci            .pkgName = const_cast<char*>(PKG_NAME.c_str()),
1631c1b0f19Sopenharmony_ci            .dataType = sessionModeAndDataTypeMap_[sessionMode],
1641c1b0f19Sopenharmony_ci        };
1651c1b0f19Sopenharmony_ci    }
1661c1b0f19Sopenharmony_ci    int32_t socketId = Socket(serverSocketInfo);
1671c1b0f19Sopenharmony_ci    if (socketId < 0) {
1681c1b0f19Sopenharmony_ci        DHLOGE("create socket server error, socket is invalid, socketId: %{public}d", socketId);
1691c1b0f19Sopenharmony_ci        return DCAMERA_BAD_VALUE;
1701c1b0f19Sopenharmony_ci    }
1711c1b0f19Sopenharmony_ci    {
1721c1b0f19Sopenharmony_ci        std::lock_guard<std::mutex> autoLock(mySocketSetLock_);
1731c1b0f19Sopenharmony_ci        if (mySocketSet_.find(socketId) != mySocketSet_.end()) {
1741c1b0f19Sopenharmony_ci            DHLOGI("current socketId had Listened");
1751c1b0f19Sopenharmony_ci            return DCAMERA_OK;
1761c1b0f19Sopenharmony_ci        }
1771c1b0f19Sopenharmony_ci        mySocketSet_.insert(socketId);
1781c1b0f19Sopenharmony_ci    }
1791c1b0f19Sopenharmony_ci    int32_t ret = Listen(socketId, g_qosInfo, g_QosTV_Param_Index, &sessListeners_[role]);
1801c1b0f19Sopenharmony_ci    if (ret != DCAMERA_OK) {
1811c1b0f19Sopenharmony_ci        DHLOGE("create socket server error, ret: %{public}d", ret);
1821c1b0f19Sopenharmony_ci        Shutdown(socketId);
1831c1b0f19Sopenharmony_ci        return DCAMERA_BAD_VALUE;
1841c1b0f19Sopenharmony_ci    }
1851c1b0f19Sopenharmony_ci    if (!ManageSelectChannel::GetInstance().GetSinkConnect()) {
1861c1b0f19Sopenharmony_ci        std::lock_guard<std::mutex> autoLock(mySessionNamePeerDevIdLock_);
1871c1b0f19Sopenharmony_ci        std::string peerDevIdMySessionName = peerDevId + std::string("_") + mySessionName;
1881c1b0f19Sopenharmony_ci        peerDevIdMySessionNameMap_[peerDevIdMySessionName] = mySessionName;
1891c1b0f19Sopenharmony_ci    }
1901c1b0f19Sopenharmony_ci    DHLOGI("create socket server end, mySessionName: %{public}s, peerSessionName: %{public}s",
1911c1b0f19Sopenharmony_ci        GetAnonyString(mySessionName).c_str(), GetAnonyString(peerSessionName).c_str());
1921c1b0f19Sopenharmony_ci    return DCAMERA_OK;
1931c1b0f19Sopenharmony_ci}
1941c1b0f19Sopenharmony_ci
1951c1b0f19Sopenharmony_ciint32_t DCameraSoftbusAdapter::CreateSoftBusSourceSocketClient(std::string myDevId, std::string peerSessionName,
1961c1b0f19Sopenharmony_ci    std::string peerDevId, DCameraSessionMode sessionMode, DCAMERA_CHANNEL_ROLE role)
1971c1b0f19Sopenharmony_ci{
1981c1b0f19Sopenharmony_ci    DHLOGI("create socket client start, myDevId: %{public}s, peerSessionName: %{public}s",
1991c1b0f19Sopenharmony_ci        GetAnonyString(myDevId).c_str(), GetAnonyString(peerSessionName).c_str());
2001c1b0f19Sopenharmony_ci    SocketInfo clientSocketInfo {};
2011c1b0f19Sopenharmony_ci    std::string myDevIdPeerSessionName = "";
2021c1b0f19Sopenharmony_ci    std::string srcSessionName = "";
2031c1b0f19Sopenharmony_ci    if (!ManageSelectChannel::GetInstance().GetSrcConnect()) {
2041c1b0f19Sopenharmony_ci        myDevIdPeerSessionName = myDevId + std::string("_") + peerSessionName;
2051c1b0f19Sopenharmony_ci        clientSocketInfo = {
2061c1b0f19Sopenharmony_ci            .name = const_cast<char*>(myDevIdPeerSessionName.c_str()),
2071c1b0f19Sopenharmony_ci            .peerName = const_cast<char*>(peerSessionName.c_str()),
2081c1b0f19Sopenharmony_ci            .peerNetworkId = const_cast<char*>(peerDevId.c_str()),
2091c1b0f19Sopenharmony_ci            .pkgName = const_cast<char*>(PKG_NAME.c_str()),
2101c1b0f19Sopenharmony_ci            .dataType = sessionModeAndDataTypeMap_[sessionMode],
2111c1b0f19Sopenharmony_ci        };
2121c1b0f19Sopenharmony_ci    } else {
2131c1b0f19Sopenharmony_ci        srcSessionName = peerSessionName + "_receiver";
2141c1b0f19Sopenharmony_ci        peerSessionName = peerSessionName + "_sender";
2151c1b0f19Sopenharmony_ci        clientSocketInfo = {
2161c1b0f19Sopenharmony_ci            .name = const_cast<char*>(srcSessionName.c_str()),
2171c1b0f19Sopenharmony_ci            .peerName = const_cast<char*>(peerSessionName.c_str()),
2181c1b0f19Sopenharmony_ci            .peerNetworkId = const_cast<char*>(peerDevId.c_str()),
2191c1b0f19Sopenharmony_ci            .pkgName = const_cast<char*>(PKG_NAME.c_str()),
2201c1b0f19Sopenharmony_ci            .dataType = sessionModeAndDataTypeMap_[sessionMode],
2211c1b0f19Sopenharmony_ci        };
2221c1b0f19Sopenharmony_ci    }
2231c1b0f19Sopenharmony_ci    int socketId = Socket(clientSocketInfo);
2241c1b0f19Sopenharmony_ci    if (socketId < 0) {
2251c1b0f19Sopenharmony_ci        DHLOGE("create socket client error, socket is invalid");
2261c1b0f19Sopenharmony_ci        return DCAMERA_BAD_VALUE;
2271c1b0f19Sopenharmony_ci    }
2281c1b0f19Sopenharmony_ci    int ret = Bind(socketId, g_qosInfo, g_QosTV_Param_Index, &sessListeners_[role]);
2291c1b0f19Sopenharmony_ci    if (ret != DCAMERA_OK) {
2301c1b0f19Sopenharmony_ci        DHLOGE("create socket client error");
2311c1b0f19Sopenharmony_ci        Shutdown(socketId);
2321c1b0f19Sopenharmony_ci        return DCAMERA_BAD_VALUE;
2331c1b0f19Sopenharmony_ci    }
2341c1b0f19Sopenharmony_ci    sourceSocketId_ = socketId;
2351c1b0f19Sopenharmony_ci    DHLOGI("create socket client end, myDevId: %{public}s, peerSessionName: %{public}s",
2361c1b0f19Sopenharmony_ci        GetAnonyString(myDevId).c_str(), GetAnonyString(peerSessionName).c_str());
2371c1b0f19Sopenharmony_ci    return socketId;
2381c1b0f19Sopenharmony_ci}
2391c1b0f19Sopenharmony_ci
2401c1b0f19Sopenharmony_ciint32_t DCameraSoftbusAdapter::DestroySoftbusSessionServer(std::string sessionName)
2411c1b0f19Sopenharmony_ci{
2421c1b0f19Sopenharmony_ci    std::lock_guard<std::mutex> autoLock(optLock_);
2431c1b0f19Sopenharmony_ci    if (sessionTotal_.find(sessionName) == sessionTotal_.end()) {
2441c1b0f19Sopenharmony_ci        DHLOGI("current sessionName already destroy, sessionName: %{public}s", GetAnonyString(sessionName).c_str());
2451c1b0f19Sopenharmony_ci        return DCAMERA_OK;
2461c1b0f19Sopenharmony_ci    }
2471c1b0f19Sopenharmony_ci
2481c1b0f19Sopenharmony_ci    sessionTotal_[sessionName]--;
2491c1b0f19Sopenharmony_ci    DHLOGI("sessionName destroy %{public}s totalnum: %{public}d", GetAnonyString(sessionName).c_str(),
2501c1b0f19Sopenharmony_ci        sessionTotal_[sessionName]);
2511c1b0f19Sopenharmony_ci    uint32_t total_ = sessionTotal_[sessionName];
2521c1b0f19Sopenharmony_ci    if (total_ == 0) {
2531c1b0f19Sopenharmony_ci        sessionTotal_.erase(sessionName);
2541c1b0f19Sopenharmony_ci    }
2551c1b0f19Sopenharmony_ci    return DCAMERA_OK;
2561c1b0f19Sopenharmony_ci}
2571c1b0f19Sopenharmony_ci
2581c1b0f19Sopenharmony_ciint32_t DCameraSoftbusAdapter::CloseSoftbusSession(int32_t socket)
2591c1b0f19Sopenharmony_ci{
2601c1b0f19Sopenharmony_ci    DHLOGI("Shutdown softbus socket start: %{public}d", socket);
2611c1b0f19Sopenharmony_ci    Shutdown(socket); // shutdown socket
2621c1b0f19Sopenharmony_ci    {
2631c1b0f19Sopenharmony_ci        std::lock_guard<std::mutex> autoLock(sinkSocketLock_);
2641c1b0f19Sopenharmony_ci        sinkSocketSessionMap_.erase(socket);
2651c1b0f19Sopenharmony_ci    }
2661c1b0f19Sopenharmony_ci    {
2671c1b0f19Sopenharmony_ci        std::lock_guard<std::mutex> autoLock(sourceSocketLock_);
2681c1b0f19Sopenharmony_ci        sourceSocketSessionMap_.erase(socket);
2691c1b0f19Sopenharmony_ci    }
2701c1b0f19Sopenharmony_ci    DHLOGI("Shutdown softbus socket: %{public}d end", socket);
2711c1b0f19Sopenharmony_ci    return DCAMERA_OK;
2721c1b0f19Sopenharmony_ci}
2731c1b0f19Sopenharmony_ci
2741c1b0f19Sopenharmony_ciint32_t DCameraSoftbusAdapter::SendSofbusBytes(int32_t socket, std::shared_ptr<DataBuffer>& buffer)
2751c1b0f19Sopenharmony_ci{
2761c1b0f19Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(buffer == nullptr, DCAMERA_BAD_VALUE, "Data buffer is null");
2771c1b0f19Sopenharmony_ci    return SendBytes(socket, buffer->Data(), buffer->Size());
2781c1b0f19Sopenharmony_ci}
2791c1b0f19Sopenharmony_ci
2801c1b0f19Sopenharmony_ciint32_t DCameraSoftbusAdapter::SendSofbusStream(int32_t socket, std::shared_ptr<DataBuffer>& buffer)
2811c1b0f19Sopenharmony_ci{
2821c1b0f19Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(buffer == nullptr, DCAMERA_BAD_VALUE, "Data buffer is null");
2831c1b0f19Sopenharmony_ci    StreamData streamData = { reinterpret_cast<char *>(buffer->Data()), buffer->Size() };
2841c1b0f19Sopenharmony_ci    int64_t timeStamp;
2851c1b0f19Sopenharmony_ci    if (!buffer->FindInt64(TIME_STAMP_US, timeStamp)) {
2861c1b0f19Sopenharmony_ci        DHLOGD("SendSofbusStream find %{public}s failed.", TIME_STAMP_US.c_str());
2871c1b0f19Sopenharmony_ci    }
2881c1b0f19Sopenharmony_ci    int32_t frameType;
2891c1b0f19Sopenharmony_ci    if (!buffer->FindInt32(FRAME_TYPE, frameType)) {
2901c1b0f19Sopenharmony_ci        DHLOGD("SendSofbusStream find %{public}s failed.", FRAME_TYPE.c_str());
2911c1b0f19Sopenharmony_ci    }
2921c1b0f19Sopenharmony_ci    int32_t index;
2931c1b0f19Sopenharmony_ci    if (!buffer->FindInt32(INDEX, index)) {
2941c1b0f19Sopenharmony_ci        DHLOGD("SendSofbusStream find %{public}s failed.", INDEX.c_str());
2951c1b0f19Sopenharmony_ci    }
2961c1b0f19Sopenharmony_ci    int64_t startEncodeT;
2971c1b0f19Sopenharmony_ci    if (!buffer->FindInt64(START_ENCODE_TIME_US, startEncodeT)) {
2981c1b0f19Sopenharmony_ci        DHLOGD("SendSofbusStream find %{public}s failed.", START_ENCODE_TIME_US.c_str());
2991c1b0f19Sopenharmony_ci    }
3001c1b0f19Sopenharmony_ci    int64_t finishEncodeT;
3011c1b0f19Sopenharmony_ci    if (!buffer->FindInt64(FINISH_ENCODE_TIME_US, finishEncodeT)) {
3021c1b0f19Sopenharmony_ci        DHLOGD("SendSofbusStream find %{public}s failed.", FINISH_ENCODE_TIME_US.c_str());
3031c1b0f19Sopenharmony_ci    }
3041c1b0f19Sopenharmony_ci    std::string jsonStr = "";
3051c1b0f19Sopenharmony_ci    DCameraSinkFrameInfo sinkFrameInfo;
3061c1b0f19Sopenharmony_ci    sinkFrameInfo.pts_ = timeStamp;
3071c1b0f19Sopenharmony_ci    sinkFrameInfo.type_ = frameType;
3081c1b0f19Sopenharmony_ci    sinkFrameInfo.index_ = index;
3091c1b0f19Sopenharmony_ci    sinkFrameInfo.startEncodeT_ = startEncodeT;
3101c1b0f19Sopenharmony_ci    sinkFrameInfo.finishEncodeT_ = finishEncodeT;
3111c1b0f19Sopenharmony_ci    sinkFrameInfo.sendT_ = GetNowTimeStampUs();
3121c1b0f19Sopenharmony_ci    sinkFrameInfo.Marshal(jsonStr);
3131c1b0f19Sopenharmony_ci    StreamData ext = { const_cast<char *>(jsonStr.c_str()), jsonStr.length() };
3141c1b0f19Sopenharmony_ci    StreamFrameInfo param = { 0 };
3151c1b0f19Sopenharmony_ci    int32_t ret = SendStream(socket, &streamData, &ext, &param);
3161c1b0f19Sopenharmony_ci    if (ret != SOFTBUS_OK) {
3171c1b0f19Sopenharmony_ci        DHLOGD("SendSofbusStream failed, ret is %{public}d", ret);
3181c1b0f19Sopenharmony_ci        return DCAMERA_BAD_VALUE;
3191c1b0f19Sopenharmony_ci    }
3201c1b0f19Sopenharmony_ci    return DCAMERA_OK;
3211c1b0f19Sopenharmony_ci}
3221c1b0f19Sopenharmony_ci
3231c1b0f19Sopenharmony_ciint32_t DCameraSoftbusAdapter::DCameraSoftbusSourceGetSession(int32_t socket,
3241c1b0f19Sopenharmony_ci    std::shared_ptr<DCameraSoftbusSession>& session)
3251c1b0f19Sopenharmony_ci{
3261c1b0f19Sopenharmony_ci    std::lock_guard<std::mutex> autoLock(sourceSocketLock_);
3271c1b0f19Sopenharmony_ci    auto iter =  sourceSocketSessionMap_.find(socket);
3281c1b0f19Sopenharmony_ci    if (iter == sourceSocketSessionMap_.end()) {
3291c1b0f19Sopenharmony_ci        DHLOGE("source can not find current socket %{public}d", socket);
3301c1b0f19Sopenharmony_ci        return DCAMERA_NOT_FOUND;
3311c1b0f19Sopenharmony_ci    }
3321c1b0f19Sopenharmony_ci    session = iter->second;
3331c1b0f19Sopenharmony_ci    return DCAMERA_OK;
3341c1b0f19Sopenharmony_ci}
3351c1b0f19Sopenharmony_ci
3361c1b0f19Sopenharmony_ci// source
3371c1b0f19Sopenharmony_ciint32_t DCameraSoftbusAdapter::SourceOnBind(int32_t socket, PeerSocketInfo info)
3381c1b0f19Sopenharmony_ci{
3391c1b0f19Sopenharmony_ci    DHLOGI("source bind socket begin, socket: %{public}d", socket);
3401c1b0f19Sopenharmony_ci    std::shared_ptr<DCameraSoftbusSession> session = nullptr;
3411c1b0f19Sopenharmony_ci    int32_t ret = DCameraSoftbusSourceGetSession(socket, session);
3421c1b0f19Sopenharmony_ci    if (ret != DCAMERA_OK || session == nullptr) {
3431c1b0f19Sopenharmony_ci        DHLOGE("source bind socket can not find socket %{public}d", socket);
3441c1b0f19Sopenharmony_ci        return DCAMERA_NOT_FOUND;
3451c1b0f19Sopenharmony_ci    }
3461c1b0f19Sopenharmony_ci    ret = session->OnSessionOpened(socket, info.networkId);
3471c1b0f19Sopenharmony_ci    if (ret != DCAMERA_OK) {
3481c1b0f19Sopenharmony_ci        DHLOGE("source bind socket failed, ret: %{public}d socket: %{public}d", ret, socket);
3491c1b0f19Sopenharmony_ci    }
3501c1b0f19Sopenharmony_ci    DHLOGI("source bind socket end, socket: %{public}d end", socket);
3511c1b0f19Sopenharmony_ci    return ret;
3521c1b0f19Sopenharmony_ci}
3531c1b0f19Sopenharmony_ci
3541c1b0f19Sopenharmony_civoid DCameraSoftbusAdapter::SourceOnShutDown(int32_t socket, ShutdownReason reason)
3551c1b0f19Sopenharmony_ci{
3561c1b0f19Sopenharmony_ci    DHLOGI("source on shutdown socket start, socket: %{public}d", socket);
3571c1b0f19Sopenharmony_ci    std::shared_ptr<DCameraSoftbusSession> session = nullptr;
3581c1b0f19Sopenharmony_ci    int32_t ret = DCameraSoftbusSourceGetSession(socket, session);
3591c1b0f19Sopenharmony_ci    if (ret != DCAMERA_OK || session == nullptr) {
3601c1b0f19Sopenharmony_ci        DHLOGE("SourceOnShutDown can not find socket %{public}d", socket);
3611c1b0f19Sopenharmony_ci        return;
3621c1b0f19Sopenharmony_ci    }
3631c1b0f19Sopenharmony_ci    session->OnSessionClose(socket);
3641c1b0f19Sopenharmony_ci    DHLOGI("source on shutdown socket end socket: %{public}d end", socket);
3651c1b0f19Sopenharmony_ci    return;
3661c1b0f19Sopenharmony_ci}
3671c1b0f19Sopenharmony_ci
3681c1b0f19Sopenharmony_civoid DCameraSoftbusAdapter::SourceOnBytes(int32_t socket, const void *data, uint32_t dataLen)
3691c1b0f19Sopenharmony_ci{
3701c1b0f19Sopenharmony_ci    if (dataLen == 0 || dataLen > DCAMERA_MAX_RECV_DATA_LEN || data == nullptr) {
3711c1b0f19Sopenharmony_ci        DHLOGE("source callback send bytes error, dataLen: %{public}d, socket: %{public}d", dataLen, socket);
3721c1b0f19Sopenharmony_ci        return;
3731c1b0f19Sopenharmony_ci    }
3741c1b0f19Sopenharmony_ci    DHLOGI("source callback send bytes start, socket: %{public}d", socket);
3751c1b0f19Sopenharmony_ci    std::shared_ptr<DCameraSoftbusSession> session = nullptr;
3761c1b0f19Sopenharmony_ci    int32_t ret = DCameraSoftbusSourceGetSession(socket, session);
3771c1b0f19Sopenharmony_ci    if (ret != DCAMERA_OK || session == nullptr) {
3781c1b0f19Sopenharmony_ci        DHLOGE("source callback send bytes not find session %{public}d", socket);
3791c1b0f19Sopenharmony_ci        return;
3801c1b0f19Sopenharmony_ci    }
3811c1b0f19Sopenharmony_ci
3821c1b0f19Sopenharmony_ci    std::shared_ptr<DataBuffer> buffer = std::make_shared<DataBuffer>(dataLen);
3831c1b0f19Sopenharmony_ci    ret = memcpy_s(buffer->Data(), buffer->Capacity(), data, dataLen);
3841c1b0f19Sopenharmony_ci    if (ret != EOK) {
3851c1b0f19Sopenharmony_ci        DHLOGE("source callback send bytes memcpy_s failed ret: %{public}d", ret);
3861c1b0f19Sopenharmony_ci        return;
3871c1b0f19Sopenharmony_ci    }
3881c1b0f19Sopenharmony_ci    session->OnDataReceived(buffer);
3891c1b0f19Sopenharmony_ci    DHLOGI("source callback send bytes end, socket: %{public}d", socket);
3901c1b0f19Sopenharmony_ci    return;
3911c1b0f19Sopenharmony_ci}
3921c1b0f19Sopenharmony_ci
3931c1b0f19Sopenharmony_civoid DCameraSoftbusAdapter::SourceOnMessage(int32_t socket, const void *data, uint32_t dataLen)
3941c1b0f19Sopenharmony_ci{
3951c1b0f19Sopenharmony_ci    (void)socket;
3961c1b0f19Sopenharmony_ci    (void)data;
3971c1b0f19Sopenharmony_ci    (void)dataLen;
3981c1b0f19Sopenharmony_ci    return;
3991c1b0f19Sopenharmony_ci}
4001c1b0f19Sopenharmony_ci
4011c1b0f19Sopenharmony_civoid DCameraSoftbusAdapter::SourceOnStream(int32_t socket, const StreamData *data, const StreamData *ext,
4021c1b0f19Sopenharmony_ci    const StreamFrameInfo *param)
4031c1b0f19Sopenharmony_ci{
4041c1b0f19Sopenharmony_ci    int64_t recvT = GetNowTimeStampUs();
4051c1b0f19Sopenharmony_ci    if (data == nullptr) {
4061c1b0f19Sopenharmony_ci        DHLOGE("SourceOnStream Error, data is null, socket: %{public}d.", socket);
4071c1b0f19Sopenharmony_ci        return;
4081c1b0f19Sopenharmony_ci    }
4091c1b0f19Sopenharmony_ci    int32_t dataLen = data->bufLen;
4101c1b0f19Sopenharmony_ci    if (dataLen <= 0 || dataLen > static_cast<int32_t>(DCAMERA_MAX_RECV_DATA_LEN)) {
4111c1b0f19Sopenharmony_ci        DHLOGE("SourceOnStream Error, dataLen: %{public}d, socket: %{public}d", dataLen, socket);
4121c1b0f19Sopenharmony_ci        return;
4131c1b0f19Sopenharmony_ci    }
4141c1b0f19Sopenharmony_ci    std::shared_ptr<DCameraSoftbusSession> session = nullptr;
4151c1b0f19Sopenharmony_ci    int32_t ret = DCameraSoftbusSourceGetSession(socket, session);
4161c1b0f19Sopenharmony_ci    if (ret != DCAMERA_OK || session == nullptr) {
4171c1b0f19Sopenharmony_ci        DHLOGE("SourceOnStream not find socket %{public}d", socket);
4181c1b0f19Sopenharmony_ci        return;
4191c1b0f19Sopenharmony_ci    }
4201c1b0f19Sopenharmony_ci
4211c1b0f19Sopenharmony_ci    std::shared_ptr<DataBuffer> buffer = std::make_shared<DataBuffer>(data->bufLen);
4221c1b0f19Sopenharmony_ci    buffer->SetInt64(RECV_TIME_US, recvT);
4231c1b0f19Sopenharmony_ci    ret = memcpy_s(buffer->Data(), buffer->Capacity(), reinterpret_cast<uint8_t *>(data->buf), data->bufLen);
4241c1b0f19Sopenharmony_ci    if (ret != EOK) {
4251c1b0f19Sopenharmony_ci        DHLOGE("SourceOnStream memcpy_s failed ret: %{public}d", ret);
4261c1b0f19Sopenharmony_ci        return;
4271c1b0f19Sopenharmony_ci    }
4281c1b0f19Sopenharmony_ci    ret = HandleSourceStreamExt(buffer, ext);
4291c1b0f19Sopenharmony_ci    if (ret != DCAMERA_OK) {
4301c1b0f19Sopenharmony_ci        DHLOGE("Handle source stream ext failed, ret is: %{public}d", ret);
4311c1b0f19Sopenharmony_ci    }
4321c1b0f19Sopenharmony_ci    session->OnDataReceived(buffer);
4331c1b0f19Sopenharmony_ci}
4341c1b0f19Sopenharmony_ci
4351c1b0f19Sopenharmony_ciint32_t DCameraSoftbusAdapter::HandleSourceStreamExt(std::shared_ptr<DataBuffer>& buffer, const StreamData *ext)
4361c1b0f19Sopenharmony_ci{
4371c1b0f19Sopenharmony_ci    if (ext == nullptr) {
4381c1b0f19Sopenharmony_ci        DHLOGE("Source stream ext is null.");
4391c1b0f19Sopenharmony_ci        return DCAMERA_BAD_VALUE;
4401c1b0f19Sopenharmony_ci    }
4411c1b0f19Sopenharmony_ci    int32_t extLen = ext->bufLen;
4421c1b0f19Sopenharmony_ci    if (extLen <= 0 || extLen > DCAMERA_MAX_RECV_EXT_LEN) {
4431c1b0f19Sopenharmony_ci        DHLOGD("ExtLen is: %{public}d.", extLen);
4441c1b0f19Sopenharmony_ci        return DCAMERA_BAD_VALUE;
4451c1b0f19Sopenharmony_ci    }
4461c1b0f19Sopenharmony_ci
4471c1b0f19Sopenharmony_ci    std::string jsonStr(reinterpret_cast<const char*>(ext->buf), ext->bufLen);
4481c1b0f19Sopenharmony_ci    DCameraSinkFrameInfo sinkFrameInfo;
4491c1b0f19Sopenharmony_ci    int32_t ret = sinkFrameInfo.Unmarshal(jsonStr);
4501c1b0f19Sopenharmony_ci    if (ret != DCAMERA_OK) {
4511c1b0f19Sopenharmony_ci        DHLOGE("Unmarshal sinkFrameInfo failed.");
4521c1b0f19Sopenharmony_ci        return DCAMERA_BAD_VALUE;
4531c1b0f19Sopenharmony_ci    }
4541c1b0f19Sopenharmony_ci    int64_t recvT;
4551c1b0f19Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(buffer == nullptr, DCAMERA_BAD_VALUE, "Data buffer is null");
4561c1b0f19Sopenharmony_ci    if (!buffer->FindInt64(RECV_TIME_US, recvT)) {
4571c1b0f19Sopenharmony_ci        DHLOGD("HandleSourceStreamExt find %{public}s failed.", RECV_TIME_US.c_str());
4581c1b0f19Sopenharmony_ci    }
4591c1b0f19Sopenharmony_ci    DCameraFrameInfo frameInfo;
4601c1b0f19Sopenharmony_ci    frameInfo.type = sinkFrameInfo.type_;
4611c1b0f19Sopenharmony_ci    frameInfo.pts = sinkFrameInfo.pts_;
4621c1b0f19Sopenharmony_ci    frameInfo.index = sinkFrameInfo.index_;
4631c1b0f19Sopenharmony_ci    frameInfo.ver = sinkFrameInfo.ver_;
4641c1b0f19Sopenharmony_ci    frameInfo.timePonit.startEncode = sinkFrameInfo.startEncodeT_;
4651c1b0f19Sopenharmony_ci    frameInfo.timePonit.finishEncode = sinkFrameInfo.finishEncodeT_;
4661c1b0f19Sopenharmony_ci    frameInfo.timePonit.send = sinkFrameInfo.sendT_;
4671c1b0f19Sopenharmony_ci    frameInfo.timePonit.recv = recvT;
4681c1b0f19Sopenharmony_ci    buffer->frameInfo_ = frameInfo;
4691c1b0f19Sopenharmony_ci    return DCAMERA_OK;
4701c1b0f19Sopenharmony_ci}
4711c1b0f19Sopenharmony_ci
4721c1b0f19Sopenharmony_civoid DCameraSoftbusAdapter::RecordSourceSocketSession(int32_t socket, std::shared_ptr<DCameraSoftbusSession> session)
4731c1b0f19Sopenharmony_ci{
4741c1b0f19Sopenharmony_ci    if (session == nullptr) {
4751c1b0f19Sopenharmony_ci        DHLOGE("RecordSourceSocketSession error, session is null");
4761c1b0f19Sopenharmony_ci        return;
4771c1b0f19Sopenharmony_ci    }
4781c1b0f19Sopenharmony_ci    {
4791c1b0f19Sopenharmony_ci        std::lock_guard<std::mutex> autoLock(sourceSocketLock_);
4801c1b0f19Sopenharmony_ci        sourceSocketSessionMap_[socket] = session;
4811c1b0f19Sopenharmony_ci    }
4821c1b0f19Sopenharmony_ci}
4831c1b0f19Sopenharmony_ci
4841c1b0f19Sopenharmony_ciint32_t DCameraSoftbusAdapter::DCameraSoftbusSinkGetSession(int32_t socket,
4851c1b0f19Sopenharmony_ci    std::shared_ptr<DCameraSoftbusSession>& session)
4861c1b0f19Sopenharmony_ci{
4871c1b0f19Sopenharmony_ci    DHLOGI("sink find session start, socket: %{public}d", socket);
4881c1b0f19Sopenharmony_ci    {
4891c1b0f19Sopenharmony_ci        std::lock_guard<std::mutex> autoLock(sinkSocketLock_);
4901c1b0f19Sopenharmony_ci        auto iter = sinkSocketSessionMap_.find(socket);
4911c1b0f19Sopenharmony_ci        if (iter == sinkSocketSessionMap_.end()) {
4921c1b0f19Sopenharmony_ci            DHLOGE("sink can not find socket %{public}d", socket);
4931c1b0f19Sopenharmony_ci            return DCAMERA_NOT_FOUND;
4941c1b0f19Sopenharmony_ci        }
4951c1b0f19Sopenharmony_ci        session = iter->second;
4961c1b0f19Sopenharmony_ci        CHECK_AND_RETURN_RET_LOG(session == nullptr, DCAMERA_BAD_VALUE, "Softbus session is null");
4971c1b0f19Sopenharmony_ci        if (session->GetSessionId() < 0) {
4981c1b0f19Sopenharmony_ci            DHLOGE("sink find session error, current sessionId is invalid");
4991c1b0f19Sopenharmony_ci            return DCAMERA_BAD_VALUE;
5001c1b0f19Sopenharmony_ci        }
5011c1b0f19Sopenharmony_ci    }
5021c1b0f19Sopenharmony_ci    DHLOGI("sink find session end, socket: %{public}d", socket);
5031c1b0f19Sopenharmony_ci    return DCAMERA_OK;
5041c1b0f19Sopenharmony_ci}
5051c1b0f19Sopenharmony_ci
5061c1b0f19Sopenharmony_cistd::string DCameraSoftbusAdapter::FindSessNameByPeerSessName(const std::string peerSessionName)
5071c1b0f19Sopenharmony_ci{
5081c1b0f19Sopenharmony_ci    auto foundItem = std::find_if(std::begin(LOCAL_TO_PEER_SESS_NAME_MAP), std::end(LOCAL_TO_PEER_SESS_NAME_MAP),
5091c1b0f19Sopenharmony_ci        [&](const auto& item) { return item.first == peerSessionName; });
5101c1b0f19Sopenharmony_ci    if (foundItem != std::end(LOCAL_TO_PEER_SESS_NAME_MAP)) {
5111c1b0f19Sopenharmony_ci        return foundItem->second;
5121c1b0f19Sopenharmony_ci    }
5131c1b0f19Sopenharmony_ci    return "";
5141c1b0f19Sopenharmony_ci}
5151c1b0f19Sopenharmony_ci
5161c1b0f19Sopenharmony_ciint32_t DCameraSoftbusAdapter::DCameraSoftBusGetSessionByPeerSocket(int32_t socket,
5171c1b0f19Sopenharmony_ci    std::shared_ptr<DCameraSoftbusSession> &session, PeerSocketInfo info)
5181c1b0f19Sopenharmony_ci{
5191c1b0f19Sopenharmony_ci    DHLOGI("find session by peer socket start, socket %{public}d", socket);
5201c1b0f19Sopenharmony_ci    std::string mySessionName = "";
5211c1b0f19Sopenharmony_ci    if (!ManageSelectChannel::GetInstance().GetSinkConnect()) {
5221c1b0f19Sopenharmony_ci        std::lock_guard<std::mutex> autoLock(mySessionNamePeerDevIdLock_);
5231c1b0f19Sopenharmony_ci        auto sessionNameIter = peerDevIdMySessionNameMap_.find(info.name);
5241c1b0f19Sopenharmony_ci        if (sessionNameIter == peerDevIdMySessionNameMap_.end()) {
5251c1b0f19Sopenharmony_ci            DHLOGE("find session by peer socket error, socket %{public}d", socket);
5261c1b0f19Sopenharmony_ci            return DCAMERA_NOT_FOUND;
5271c1b0f19Sopenharmony_ci        }
5281c1b0f19Sopenharmony_ci        mySessionName = sessionNameIter->second;
5291c1b0f19Sopenharmony_ci    } else {
5301c1b0f19Sopenharmony_ci        mySessionName = FindSessNameByPeerSessName(info.name);
5311c1b0f19Sopenharmony_ci    }
5321c1b0f19Sopenharmony_ci    if (mySessionName.empty()) {
5331c1b0f19Sopenharmony_ci        DHLOGE("find mySessionName is empty");
5341c1b0f19Sopenharmony_ci        return DCAMERA_BAD_VALUE;
5351c1b0f19Sopenharmony_ci    }
5361c1b0f19Sopenharmony_ci    auto iter = sinkSessions_.find(std::string(mySessionName));
5371c1b0f19Sopenharmony_ci    if (iter == sinkSessions_.end()) {
5381c1b0f19Sopenharmony_ci        DHLOGE("find session by peer socket error, mySessionName %{public}s",
5391c1b0f19Sopenharmony_ci            GetAnonyString(mySessionName).c_str());
5401c1b0f19Sopenharmony_ci        return DCAMERA_NOT_FOUND;
5411c1b0f19Sopenharmony_ci    }
5421c1b0f19Sopenharmony_ci    session = iter->second;
5431c1b0f19Sopenharmony_ci    {
5441c1b0f19Sopenharmony_ci        std::lock_guard<std::mutex> autoLock(sinkSocketLock_);
5451c1b0f19Sopenharmony_ci        sinkSocketSessionMap_[socket] = session;
5461c1b0f19Sopenharmony_ci    }
5471c1b0f19Sopenharmony_ci    DHLOGI("find session by peer socket end, socket %{public}d", socket);
5481c1b0f19Sopenharmony_ci    return DCAMERA_OK;
5491c1b0f19Sopenharmony_ci}
5501c1b0f19Sopenharmony_ci
5511c1b0f19Sopenharmony_ci// sink
5521c1b0f19Sopenharmony_ciint32_t DCameraSoftbusAdapter::SinkOnBind(int32_t socket, PeerSocketInfo info)
5531c1b0f19Sopenharmony_ci{
5541c1b0f19Sopenharmony_ci    DHLOGI("sink bind socket start, socket: %{public}d", socket);
5551c1b0f19Sopenharmony_ci    std::shared_ptr<DCameraSoftbusSession> session = nullptr;
5561c1b0f19Sopenharmony_ci    int32_t ret = DCameraSoftBusGetSessionByPeerSocket(socket, session, info);
5571c1b0f19Sopenharmony_ci    if (ret != DCAMERA_OK || session == nullptr) {
5581c1b0f19Sopenharmony_ci        DHLOGE("sink bind socket error, can not find socket %{public}d", socket);
5591c1b0f19Sopenharmony_ci        return DCAMERA_NOT_FOUND;
5601c1b0f19Sopenharmony_ci    }
5611c1b0f19Sopenharmony_ci    ret = session->OnSessionOpened(socket, info.networkId);
5621c1b0f19Sopenharmony_ci    if (ret != DCAMERA_OK) {
5631c1b0f19Sopenharmony_ci        DHLOGE("sink bind socket error, not find socket %{public}d", socket);
5641c1b0f19Sopenharmony_ci    }
5651c1b0f19Sopenharmony_ci    DHLOGI("sink bind socket end, socket: %{public}d", socket);
5661c1b0f19Sopenharmony_ci    return ret;
5671c1b0f19Sopenharmony_ci}
5681c1b0f19Sopenharmony_ci
5691c1b0f19Sopenharmony_civoid DCameraSoftbusAdapter::SinkOnShutDown(int32_t socket, ShutdownReason reason)
5701c1b0f19Sopenharmony_ci{
5711c1b0f19Sopenharmony_ci    DHLOGI("sink on shutdown socket start, socket: %{public}d", socket);
5721c1b0f19Sopenharmony_ci    std::shared_ptr<DCameraSoftbusSession> session = nullptr;
5731c1b0f19Sopenharmony_ci    int32_t ret = DCameraSoftbusSinkGetSession(socket, session);
5741c1b0f19Sopenharmony_ci    if (ret != DCAMERA_OK || session == nullptr) {
5751c1b0f19Sopenharmony_ci        DHLOGE("sink on shutdown socket can not find socket %{public}d", socket);
5761c1b0f19Sopenharmony_ci        return;
5771c1b0f19Sopenharmony_ci    }
5781c1b0f19Sopenharmony_ci    session->OnSessionClose(socket);
5791c1b0f19Sopenharmony_ci    DHLOGI("sink on shutdown socket end, socket: %{public}d", socket);
5801c1b0f19Sopenharmony_ci    return;
5811c1b0f19Sopenharmony_ci}
5821c1b0f19Sopenharmony_ci
5831c1b0f19Sopenharmony_civoid DCameraSoftbusAdapter::SinkOnBytes(int32_t socket, const void *data, uint32_t dataLen)
5841c1b0f19Sopenharmony_ci{
5851c1b0f19Sopenharmony_ci    if (dataLen == 0 || dataLen > DCAMERA_MAX_RECV_DATA_LEN || data == nullptr) {
5861c1b0f19Sopenharmony_ci        DHLOGE("sink on bytes error, dataLen: %{public}d, socket: %{public}d", dataLen, socket);
5871c1b0f19Sopenharmony_ci        return;
5881c1b0f19Sopenharmony_ci    }
5891c1b0f19Sopenharmony_ci    DHLOGI("sink on bytes start, socket: %{public}d", socket);
5901c1b0f19Sopenharmony_ci    std::shared_ptr<DCameraSoftbusSession> session = nullptr;
5911c1b0f19Sopenharmony_ci    int32_t ret = DCameraSoftbusSinkGetSession(socket, session);
5921c1b0f19Sopenharmony_ci    if (ret != DCAMERA_OK || session == nullptr) {
5931c1b0f19Sopenharmony_ci        DHLOGE("sink on bytes error, can not find session %{public}d", socket);
5941c1b0f19Sopenharmony_ci        return;
5951c1b0f19Sopenharmony_ci    }
5961c1b0f19Sopenharmony_ci    std::shared_ptr<DataBuffer> buffer = std::make_shared<DataBuffer>(dataLen);
5971c1b0f19Sopenharmony_ci    ret = memcpy_s(buffer->Data(), buffer->Capacity(), data, dataLen);
5981c1b0f19Sopenharmony_ci    if (ret != EOK) {
5991c1b0f19Sopenharmony_ci        DHLOGE("sink on bytes memcpy_s failed ret: %{public}d", ret);
6001c1b0f19Sopenharmony_ci        return;
6011c1b0f19Sopenharmony_ci    }
6021c1b0f19Sopenharmony_ci    session->OnDataReceived(buffer);
6031c1b0f19Sopenharmony_ci    DHLOGI("sink on bytes end, socket: %{public}d", socket);
6041c1b0f19Sopenharmony_ci    return;
6051c1b0f19Sopenharmony_ci}
6061c1b0f19Sopenharmony_ci
6071c1b0f19Sopenharmony_civoid DCameraSoftbusAdapter::SinkOnMessage(int32_t socket, const void *data, uint32_t dataLen)
6081c1b0f19Sopenharmony_ci{
6091c1b0f19Sopenharmony_ci    return;
6101c1b0f19Sopenharmony_ci}
6111c1b0f19Sopenharmony_ci
6121c1b0f19Sopenharmony_civoid DCameraSoftbusAdapter::SinkOnStream(int32_t socket, const StreamData *data, const StreamData *ext,
6131c1b0f19Sopenharmony_ci    const StreamFrameInfo *param)
6141c1b0f19Sopenharmony_ci{
6151c1b0f19Sopenharmony_ci    if (data == nullptr) {
6161c1b0f19Sopenharmony_ci        DHLOGE("SinkOnStream error, data is null, socket: %{public}d.", socket);
6171c1b0f19Sopenharmony_ci        return;
6181c1b0f19Sopenharmony_ci    }
6191c1b0f19Sopenharmony_ci    int32_t dataLen = data->bufLen;
6201c1b0f19Sopenharmony_ci    if (dataLen <= 0 || dataLen > static_cast<int32_t>(DCAMERA_MAX_RECV_DATA_LEN)) {
6211c1b0f19Sopenharmony_ci        DHLOGE("SinkOnStream error, dataLen: %{public}d socket: %{public}d", dataLen, socket);
6221c1b0f19Sopenharmony_ci        return;
6231c1b0f19Sopenharmony_ci    }
6241c1b0f19Sopenharmony_ci    std::shared_ptr<DCameraSoftbusSession> session = nullptr;
6251c1b0f19Sopenharmony_ci    int32_t ret = DCameraSoftbusSinkGetSession(socket, session);
6261c1b0f19Sopenharmony_ci    if (ret != DCAMERA_OK || session == nullptr) {
6271c1b0f19Sopenharmony_ci        DHLOGE("SinkOnStream error, can not find socket %{public}d", socket);
6281c1b0f19Sopenharmony_ci        return;
6291c1b0f19Sopenharmony_ci    }
6301c1b0f19Sopenharmony_ci
6311c1b0f19Sopenharmony_ci    std::shared_ptr<DataBuffer> buffer = std::make_shared<DataBuffer>(data->bufLen);
6321c1b0f19Sopenharmony_ci    ret = memcpy_s(buffer->Data(), buffer->Capacity(), reinterpret_cast<uint8_t *>(data->buf), data->bufLen);
6331c1b0f19Sopenharmony_ci    if (ret != EOK) {
6341c1b0f19Sopenharmony_ci        DHLOGE("SinkOnStream error, memcpy_s failed ret: %{public}d", ret);
6351c1b0f19Sopenharmony_ci        return;
6361c1b0f19Sopenharmony_ci    }
6371c1b0f19Sopenharmony_ci    session->OnDataReceived(buffer);
6381c1b0f19Sopenharmony_ci    return;
6391c1b0f19Sopenharmony_ci}
6401c1b0f19Sopenharmony_ci
6411c1b0f19Sopenharmony_ciint32_t DCameraSoftbusAdapter::GetLocalNetworkId(std::string& myDevId)
6421c1b0f19Sopenharmony_ci{
6431c1b0f19Sopenharmony_ci    NodeBasicInfo basicInfo = { { 0 } };
6441c1b0f19Sopenharmony_ci    int32_t ret = GetLocalNodeDeviceInfo(PKG_NAME.c_str(), &basicInfo);
6451c1b0f19Sopenharmony_ci    if (ret != DCAMERA_OK) {
6461c1b0f19Sopenharmony_ci        DHLOGE("GetLocalNodeDeviceInfo failed ret: %{public}d", ret);
6471c1b0f19Sopenharmony_ci        return ret;
6481c1b0f19Sopenharmony_ci    }
6491c1b0f19Sopenharmony_ci
6501c1b0f19Sopenharmony_ci    myDevId = std::string(basicInfo.networkId);
6511c1b0f19Sopenharmony_ci    return DCAMERA_OK;
6521c1b0f19Sopenharmony_ci}
6531c1b0f19Sopenharmony_ci} // namespace DistributedHardware
6541c1b0f19Sopenharmony_ci} // namespace OHOS
655