1 /* 2 * Copyright (c) 2022-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_ADAPTER_H 17 #define OHOS_SOFTBUS_ADAPTER_H 18 19 #include <condition_variable> 20 #include <unordered_map> 21 #include <mutex> 22 #include <memory> 23 #include <queue> 24 #include <set> 25 #include <thread> 26 27 #include "single_instance.h" 28 29 #include "audio_data.h" 30 #include "daudio_constants.h" 31 #include "daudio_log.h" 32 #include "daudio_errorcode.h" 33 #include "daudio_util.h" 34 #include "isoftbus_listener.h" 35 36 namespace OHOS { 37 namespace DistributedHardware { 38 class SoftbusAdapter { 39 public: 40 DECLARE_SINGLE_INSTANCE_BASE(SoftbusAdapter); 41 public: 42 int32_t CreateSoftbusSessionServer(const std::string &pkgName, const std::string &sessionName, 43 const std::string &peerDevId); 44 int32_t RemoveSoftbusSessionServer(const std::string &pkgname, const std::string &sessionName, 45 const std::string &peerDevId); 46 int32_t OpenSoftbusSession(const std::string &mySessionName, const std::string &peerSessionName, 47 const std::string &peerDevId); 48 int32_t CloseSoftbusSession(int32_t sessionId); 49 int32_t SendSoftbusBytes(int32_t sessionId, const void *data, int32_t dataLen); 50 int32_t SendSoftbusStream(int32_t sessionId, const std::shared_ptr<AudioData> &audioData); 51 int32_t RegisterSoftbusListener(const std::shared_ptr<ISoftbusListener> &listener, const std::string &sessionName, 52 const std::string &peerDevId); 53 int32_t UnRegisterSoftbusListener(const std::string &sessionName, const std::string &peerDevId); 54 55 int32_t OnSoftbusSessionOpened(int32_t sessionId, int32_t result); 56 void OnSoftbusSessionClosed(int32_t sessionId); 57 void OnBytesReceived(int32_t sessionId, const void *data, uint32_t dataLen); 58 void OnStreamReceived(int32_t sessionId, const StreamData *data, const StreamData *ext, 59 const StreamFrameInfo *streamFrameInfo); 60 void OnMessageReceived(int sessionId, const void *data, unsigned int dataLen); 61 void OnQosEvent(int sessionId, int eventId, int tvCount, const QosTv *tvList); 62 63 private: 64 SoftbusAdapter(); 65 ~SoftbusAdapter(); 66 std::shared_ptr<ISoftbusListener> &GetSoftbusListenerByName(int32_t sessionId); 67 std::shared_ptr<ISoftbusListener> &GetSoftbusListenerById(int32_t sessionId); 68 void SendAudioData(); 69 void StopSendDataThread(); 70 71 struct SoftbusStreamData { 72 SoftbusStreamData() = default; SoftbusStreamDataSoftbusStreamData73 SoftbusStreamData(const std::shared_ptr<AudioData> &data, const int32_t id) : data_(data), sessionId_(id) {}; 74 ~SoftbusStreamData() = default; 75 std::shared_ptr<AudioData> data_; 76 int32_t sessionId_; 77 }; 78 79 private: 80 static constexpr size_t DATA_QUEUE_MAX_SIZE = 500; 81 static constexpr const char* SENDDATA_THREAD = "sendDataThread"; 82 std::mutex listenerMtx_; 83 std::mutex sessSetMtx_; 84 std::mutex dataQueueMtx_; 85 std::condition_variable sendDataCond_; 86 std::thread sendDataThread_; 87 88 std::atomic<bool> isSessionOpened_ = false; 89 ISessionListener sessListener_; 90 std::shared_ptr<ISoftbusListener> nullListener_; 91 std::unordered_map<std::string, std::set<std::string>> mapSessionSet_; 92 std::unordered_map<std::string, std::shared_ptr<ISoftbusListener>> mapListenersN_; 93 std::unordered_map<int32_t, std::shared_ptr<ISoftbusListener>> mapListenersI_; 94 std::queue<std::shared_ptr<SoftbusStreamData>> audioDataQueue_; 95 }; 96 } // namespace DistributedHardware 97 } // namespace OHOS 98 #endif // OHOS_SOFTBUS_ADAPTER_H 99