rtc_plugins/util/RTCContext.h

134 lines
5.0 KiB
C
Raw Normal View History

2025-04-10 16:45:57 +08:00
// RTCContext.h
#pragma once
2025-04-10 16:20:22 +08:00
2025-04-09 16:18:50 +08:00
#include "IMRTCEngine.hpp"
#include "MRTCEngineDefine.hpp"
#include "IMRTCEngineFactory.hpp"
#include <mutex>
#include <cstring>
#include <filesystem> // C++17
#include <iostream>
2025-04-10 16:20:22 +08:00
#include <stdio.h>
2025-04-09 16:18:50 +08:00
#include <unistd.h>
2025-04-11 08:44:47 +08:00
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>
2025-04-10 16:20:22 +08:00
#include <Python.h>
2025-05-03 21:51:02 +08:00
// pybind11 头文件
#include <pybind11/pybind11.h>
#include <pybind11/numpy.h>
#include <pybind11/stl.h>
namespace py = pybind11;
2025-04-09 18:13:36 +08:00
#include <numpy/arrayobject.h>
2025-04-10 16:45:57 +08:00
2025-04-09 16:18:50 +08:00
#define ENV_PRODUCT
//#define SEND_MODE
2025-04-16 15:30:55 +08:00
// 音频数据帧
2025-05-03 21:51:02 +08:00
struct RetAudioFrame
2025-04-16 15:30:55 +08:00
{
std::unique_ptr<int16_t[]> data;
int dataCount = 0;
int sampleRate = 48000;
int numChannels = 1;
int channelIndex = 0;
};
2025-04-09 16:18:50 +08:00
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;
}
2025-04-10 17:47:00 +08:00
static void** numpy_api() {
2025-04-10 18:31:30 +08:00
static void** api = [](){
// 强制初始化NumPy
if (_import_array() < 0) {
PyErr_Print();
throw std::runtime_error("NumPy initialization failed");
}
2025-04-10 18:22:50 +08:00
2025-04-10 18:31:30 +08:00
void** ptr = reinterpret_cast<void**>(RTC_PLUGINS_ARRAY_API);
2025-04-10 18:35:30 +08:00
std::cout << "ptr:" << ptr << std::endl;
2025-04-10 18:31:30 +08:00
if (!ptr || !ptr[93]) {
std::cerr << "NumPy API corrupt! Expected at 93: "
<< (ptr ? (void*)ptr[93] : nullptr) << std::endl;
abort();
}
return ptr;
}();
return api;
2025-04-10 17:47:00 +08:00
}
2025-04-09 16:18:50 +08:00
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);
2025-04-15 23:21:48 +08:00
bool initSend(const char* srcRoomId, const char* destRoomId, const int16_t destChannelIndex, const uint8_t channelNum);
2025-04-16 15:30:55 +08:00
int16_t getSize();
2025-04-16 15:59:45 +08:00
void setData(const mrtc::MRTCAudioFrame& frame);
RetAudioFrame getData();
2025-05-03 21:51:02 +08:00
py::array_t<int16_t> getNumpyData();
py::list getListData();
2025-04-16 16:55:10 +08:00
int16_t getDataCount();
2025-04-09 16:18:50 +08:00
void* getpData() const;
void setpData(void* pData);
2025-05-03 21:51:02 +08:00
void setPyCallback(py::object callback);
2025-04-09 16:18:50 +08:00
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()
{
2025-04-16 15:30:55 +08:00
data_.resize(totalSize_);
2025-04-09 16:18:50 +08:00
}
mutable std::mutex mutex_;
mrtc::IMRTCEngine * rtcEngine_ = nullptr;
void* pData_ = nullptr;
bool isOnRoom_ = false;
bool isOnConsumer_ = false;
bool isJoinMultiRoom_ = false;
2025-04-14 21:11:08 +08:00
bool isMultiRoom_ = false;
2025-05-03 21:51:02 +08:00
py::object pyCallback_;
2025-04-16 15:30:55 +08:00
std::vector<RetAudioFrame> data_;
mutable std::mutex dataMutex_;
const int16_t totalSize_ = 100;
int16_t dataSize_ = 0;
int16_t bottom_ = 0;
int16_t head_= 0;
2025-04-09 16:18:50 +08:00
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);
};