diff --git a/rtc_plugins.cpp b/rtc_plugins.cpp index 1469588..8673ec6 100644 --- a/rtc_plugins.cpp +++ b/rtc_plugins.cpp @@ -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, uint64_t dataLen) { - PyObject* pyObj = pyData.ptr(); - void* dataPtr = PyArray_DATA((PyArrayObject*)pyObj); + // 检查是否为 numpy 数组 + 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); } diff --git a/test_send.py b/test_send.py index c147ff7..c86e54f 100644 --- a/test_send.py +++ b/test_send.py @@ -1,5 +1,6 @@ import rtc_plugins import time +import numpy as np srcUserId = "srcUser1" destUserId = "destUser1" @@ -20,8 +21,9 @@ ret = rtc_plugins.initSend(destRoomId, destChannelIndex) if ret != 0: print(f"initSend fail, ret:{ret}") exit(1) +audioData = np.zeros(16, dtype=np.int16) while True: - ret = rtc_plugins.sendCustomAudioData(destChannelIndex, "aaaaa", 48000, 1, 10) + ret = rtc_plugins.sendCustomAudioData(destChannelIndex, audioData, 48000, 1, len(audioData)) if ret != 0: print(f"send fail, ret:{ret}") time.sleep(3) diff --git a/util/RTCContext.cpp b/util/RTCContext.cpp index 4098315..2cc090f 100644 --- a/util/RTCContext.cpp +++ b/util/RTCContext.cpp @@ -183,6 +183,11 @@ int16_t RTCContext::sendCustomAudioData(const int16_t channelIndex, void* custom sleep(3); } std::lock_guard 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); } mrtc::IMRTCEngine* RTCContext::getRtcEngine() const