rtc_plugins/util/RTCContext.h

123 lines
4.7 KiB
C++

// RTCContext.h
#pragma once
//#include "numpyConfig.h"
#include "numpyStub.h"
#include "IMRTCEngine.hpp"
#include "MRTCEngineDefine.hpp"
#include "IMRTCEngineFactory.hpp"
#include <mutex>
#include <cstring>
#include <filesystem> // C++17
#include <iostream>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <Python.h>
#include <boost/python.hpp>
#include <boost/python/detail/wrap_python.hpp>
#include <boost/python/numpy.hpp>
#include <boost/python/detail/prefix.hpp>
#include <boost/python/module.hpp>
#include <boost/python/def.hpp>
#include <numpy/ndarrayobject.h>
#include <numpy/arrayobject.h>
// 必须声明外部变量(关键!)
//#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
namespace fs = std::filesystem;
#define ENV_PRODUCT
//#define SEND_MODE
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;
}
static void** numpy_api() {
static void** api = [](){
// 强制初始化NumPy
if (_import_array() < 0) {
PyErr_Print();
throw std::runtime_error("NumPy initialization failed");
}
void** ptr = reinterpret_cast<void**>(RTC_PLUGINS_ARRAY_API);
std::cout << "ptr:" << ptr << std::endl;
if (!ptr || !ptr[93]) {
std::cerr << "NumPy API corrupt! Expected at 93: "
<< (ptr ? (void*)ptr[93] : nullptr) << std::endl;
abort();
}
return ptr;
}();
return api;
}
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, uint8_t channelNum);
void* getpData() const;
void setpData(void* pData);
void setPyCallback(boost::python::object callback);
void setNumpyApi(void** numpyApi);
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()
{
}
mutable std::mutex mutex_;
mrtc::IMRTCEngine * rtcEngine_ = nullptr;
void* pData_ = nullptr;
bool isOnRoom_ = false;
bool isRecv_ = false;
bool isJoinMultiRoom_ = false;
bool isMultiRoom_ = false;
boost::python::object pyCallback_;
void ** numpyApi_;
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);
};