1 /* 2 * Copyright (c) 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 #ifndef OHOS_SOFTBUS_CHANNEL_ADAPTER 17 #define OHOS_SOFTBUS_CHANNEL_ADAPTER 18 19 #include <map> 20 #include <mutex> 21 #include <set> 22 23 #include "av_trans_types.h" 24 #include "session.h" 25 #include "single_instance.h" 26 #include "softbus_bus_center.h" 27 #include "softbus_common.h" 28 29 namespace OHOS { 30 namespace DistributedHardware { 31 class ISoftbusChannelListener { 32 public: 33 virtual ~ISoftbusChannelListener() = default; 34 virtual void OnChannelEvent(const AVTransEvent &event) = 0; 35 virtual void OnStreamReceived(const StreamData *data, const StreamData *ext) = 0; 36 }; 37 38 class SoftbusChannelAdapter { 39 DECLARE_SINGLE_INSTANCE_BASE(SoftbusChannelAdapter); 40 public: 41 int32_t CreateChannelServer(const std::string &pkgName, const std::string &sessName); 42 int32_t RemoveChannelServer(const std::string &pkgName, const std::string &sessName); 43 44 int32_t OpenSoftbusChannel(const std::string &mySessName, const std::string &peerSessName, 45 const std::string &peerDevId); 46 int32_t CloseSoftbusChannel(const std::string &mySessName, const std::string &peerDevId); 47 48 int32_t SendBytesData(const std::string &sessName, const std::string &peerDevId, const std::string &data); 49 int32_t SendStreamData(const std::string &sessName, const std::string &peerDevId, const StreamData *data, 50 const StreamData *ext); 51 52 int32_t RegisterChannelListener(const std::string &sessName, const std::string &peerDevId, 53 ISoftbusChannelListener *listener); 54 int32_t UnRegisterChannelListener(const std::string &sessName, const std::string &peerDevId); 55 56 int32_t StartDeviceTimeSync(const std::string &pkgName, const std::string &sessName, 57 const std::string &peerDevId); 58 int32_t StopDeviceTimeSync(const std::string &pkgName, const std::string &sessName, 59 const std::string &peerDevId); 60 61 void SendChannelEvent(ISoftbusChannelListener *listener, const AVTransEvent &event); 62 63 int32_t OnSoftbusChannelOpened(int32_t sessionId, int32_t result); 64 void OnSoftbusChannelClosed(int32_t sessionId); 65 void OnSoftbusBytesReceived(int32_t sessionId, const void *data, uint32_t dataLen); 66 void OnSoftbusTimeSyncResult(const TimeSyncResultInfo *info, int32_t result); 67 void OnSoftbusStreamReceived(int32_t sessionId, const StreamData *data, const StreamData *ext, 68 const StreamFrameInfo *frameInfo); 69 70 private: 71 SoftbusChannelAdapter(); 72 ~SoftbusChannelAdapter(); 73 74 std::string GetSessionNameById(int32_t sessionId); 75 int32_t GetSessIdBySessName(const std::string &sessName, const std::string &peerDevId); 76 std::string GetPeerDevIdBySessId(int32_t sessionId); 77 std::string GetOwnerFromSessName(const std::string &sessName); 78 79 private: 80 std::mutex name2IdMtx_; 81 std::mutex timeSyncMtx_; 82 std::mutex idMapMutex_; 83 std::mutex listenerMtx_; 84 85 ISessionListener sessListener_; 86 std::set<std::string> sessionNameSet_; 87 std::set<std::string> timeSyncSessNames_; 88 std::map<std::string, int32_t> devId2SessIdMap_; 89 std::map<std::string, ISoftbusChannelListener *> listenerMap_; 90 }; 91 } // namespace DistributedHardware 92 } // namespace OHOS 93 #endif // OHOS_SOFTBUS_CHANNEL_ADAPTER 94