This commit is contained in:
wangjiyu 2025-04-10 09:07:06 +08:00
parent 71ef584b5f
commit 74b131ad8d
3 changed files with 26 additions and 3 deletions

View File

@ -33,8 +33,24 @@ int initSend(const char* destRoomId, const int16_t destChannelIndex) {
} }
int sendCustomAudioData(const int16_t destChannelIndex, py::object pyData, int32_t sampleRate, uint64_t channelNum, int sendCustomAudioData(const int16_t destChannelIndex, py::object pyData, int32_t sampleRate, uint64_t channelNum,
uint64_t dataLen) { uint64_t dataLen) {
PyObject* pyObj = pyData.ptr(); // 检查是否为 numpy 数组
void* dataPtr = PyArray_DATA((PyArrayObject*)pyObj); if (!PyArray_Check(pyData.ptr())) {
PyErr_SetString(PyExc_TypeError, "Expected a numpy array");
throw py::error_already_set();
}
// 检查数据类型是否为 int16
PyArrayObject* npArray = (PyArrayObject*)pyData.ptr();
if (PyArray_TYPE(npArray) != NPY_INT16) {
PyErr_SetString(PyExc_TypeError, "Array must be of type int16 (np.int16)");
throw py::error_already_set();
}
void* dataPtr = PyArray_DATA(npArray);
if (dataPtr == nullptr) {
PyErr_SetString(PyExc_ValueError, "Invalid data pointer");
throw py::error_already_set();
}
return RTCContext::instance().sendCustomAudioData(destChannelIndex, dataPtr, sampleRate, channelNum, dataLen); return RTCContext::instance().sendCustomAudioData(destChannelIndex, dataPtr, sampleRate, channelNum, dataLen);
} }

View File

@ -1,5 +1,6 @@
import rtc_plugins import rtc_plugins
import time import time
import numpy as np
srcUserId = "srcUser1" srcUserId = "srcUser1"
destUserId = "destUser1" destUserId = "destUser1"
@ -20,8 +21,9 @@ ret = rtc_plugins.initSend(destRoomId, destChannelIndex)
if ret != 0: if ret != 0:
print(f"initSend fail, ret:{ret}") print(f"initSend fail, ret:{ret}")
exit(1) exit(1)
audioData = np.zeros(16, dtype=np.int16)
while True: while True:
ret = rtc_plugins.sendCustomAudioData(destChannelIndex, "aaaaa", 48000, 1, 10) ret = rtc_plugins.sendCustomAudioData(destChannelIndex, audioData, 48000, 1, len(audioData))
if ret != 0: if ret != 0:
print(f"send fail, ret:{ret}") print(f"send fail, ret:{ret}")
time.sleep(3) time.sleep(3)

View File

@ -183,6 +183,11 @@ int16_t RTCContext::sendCustomAudioData(const int16_t channelIndex, void* custom
sleep(3); sleep(3);
} }
std::lock_guard<std::mutex> lock(mutex_); std::lock_guard<std::mutex> lock(mutex_);
if (customData == nullptr) {
std::cout << "customData is null" << std::endl;
return -1;
}
std::cout << "customData addr is:" << customData << std::endl;
return rtcEngine_->sendCustomAudioData(channelIndex, customData, sampleRate, channelNum, dataLen); return rtcEngine_->sendCustomAudioData(channelIndex, customData, sampleRate, channelNum, dataLen);
} }
mrtc::IMRTCEngine* RTCContext::getRtcEngine() const mrtc::IMRTCEngine* RTCContext::getRtcEngine() const