// RTCContext.h #pragma once #include "IMRTCEngine.hpp" #include "MRTCEngineDefine.hpp" #include "IMRTCEngineFactory.hpp" #include #include #include // C++17 #include #include #include #include #include #include #include // pybind11 头文件 #include #include #include namespace py = pybind11; //#include #define ENV_PRODUCT //#define SEND_MODE // 音频数据帧 struct RetAudioFrame { std::unique_ptr data; int dataCount = 0; int sampleRate = 48000; int numChannels = 1; int channelIndex = 0; }; class RTCContext : public RTCENGINE_NAMESPACE::IMRTCRoomCallBack, public RTCENGINE_NAMESPACE::IMRTCConsumerCallBack, public RTCENGINE_NAMESPACE::IMRTCVideoRenderCallBack, public RTCENGINE_NAMESPACE::IMRTCMessageCallBack, public RTCENGINE_NAMESPACE::IMRTCReceivedAudioInfoCallBack, public RTCENGINE_NAMESPACE::IMRTCProduerCallBack { public: #ifdef ENV_PRODUCT const char* appid = "1357389261426393088"; const char* appSecrectKey = "gytr3qrrznsgac2lbdchfg0l4ltqx4x3nf4my0yt1dbx7riz1gnnsnhblrlc2j4o1lx4vdu495lifv8s7jyrmfxtq9lif9rusxnur5smdn5cjopstxp5dim7p52xrezp"; const char* domain = "rtc.migu.cn"; const unsigned short port = 34443; #else const char* appid = "1357413779192676352"; const char* appSecrectKey = "q04ex8bivag3mn8ncf504t5lw4asldbc1k2rfbhoy54u7lsqqkmkzp33x0h4d9ww9z3f1deiel34m191lj4k5dc7utidom9s1izg88jhouv1bwy62q372ly9dmd63g4u"; const char* domain = "dorytest.migu.cn"; const unsigned short port = 34443; #endif static RTCContext& instance() { static RTCContext instance; return instance; } RTCContext(const RTCContext&) = delete; RTCContext& operator=(const RTCContext&) = delete; mrtc::IMRTCEngine* getRtcEngine() const; bool init(const char* selfUserId, const char* selfDisplayName, const char* selfRoomId); bool initRecv(const char* destRoomId, const char* srcUserId, const int16_t destChannelIndex); bool initSend(const char* srcRoomId, const char* destRoomId, const int16_t destChannelIndex, const uint8_t channelNum); int16_t getSize(); void setData(const mrtc::MRTCAudioFrame& frame); RetAudioFrame getData(); py::array_t getNumpyData(); py::list getListData(); int16_t getDataCount(); void* getpData() const; void setpData(void* pData); void setPyCallback(py::object callback); int16_t sendAudioData(uint8_t channelIndex = 0, const void* pData = nullptr, int32_t nSampleRate = 48000, uint64_t nNumberOfChannels = 2, uint64_t dataLength = 0); int16_t sendCustomAudioData(const int16_t channelIndex, void* customData, int32_t sampleRate, uint64_t channelNum, uint64_t dataLen); void destorySend(const int16_t selfChannelIndex); private: RTCContext() { data_.resize(totalSize_); } mutable std::mutex mutex_; mrtc::IMRTCEngine * rtcEngine_ = nullptr; void* pData_ = nullptr; bool isOnRoom_ = false; bool isOnConsumer_ = false; bool isJoinMultiRoom_ = false; bool isMultiRoom_ = false; py::object pyCallback_; std::vector data_; mutable std::mutex dataMutex_; const int16_t totalSize_ = 100; int16_t dataSize_ = 0; int16_t bottom_ = 0; int16_t head_= 0; void onRoom(uint32_t typeId, RTCENGINE_NAMESPACE::MRTCRoomInfo& roomInfo); void onConsumer(uint32_t msgId, const char* roomId, const char* peerId, RTCENGINE_NAMESPACE::MRTCConsumerInfo& consumerInfo); void onRender(const char* roomId, const char* peerId, RTCENGINE_NAMESPACE::MRTCVideoSourceType sourceType, const RTCENGINE_NAMESPACE::MRTCVideoFrame& videoFrame); void onCallBackMessage(uint32_t msgId, const char* msg); void onCallBackCustomData(RTCENGINE_NAMESPACE::MRTCCustomDataObject object); void onSoundLevelUpdate(const char* roomId, const char* peerId, uint16_t audioSourceType,uint8_t channelIndex, uint16_t volume, int32_t vad); void onAudioProcess(const char* roomId, const char* peerId, mrtc::MRTCAudioFrame& audioFrame, mrtc::MRTCAudioSourceType audioSourceType); void onProducer(uint32_t msgId, mrtc::MRTCProducerInfo& info); };