应该是内存覆盖了,函数被调用三次,初始化了三次,现在我想使用你上面提到的方案方案1:为每个摄像头创建独立 Decoder 实例,std::unique_ptr<MppDecoder> mppDecoderPerStream = createDecoderForStream(stream1);告诉我具体怎么做?void *open_media(void *argv){
__LOG_PRINT(ANDROID_LOG_DEBUG, "rtsp", "open_media xxxxxxxxxx");
ThreadData *data = static_cast<ThreadData *>(argv);
const char *rurl = data->url;
std::string cstr = std::string(data->cameraId);
int cameraId = std::stoi(cstr);
std::string istr = std::string(data->indexStr);
__LOG_PRINT(ANDROID_LOG_DEBUG, "rtsp", "open_media xxxxxxxxxx %s", istr.c_str());
int index = std::stoi(istr);
__LOG_PRINT(ANDROID_LOG_DEBUG, "rtsp", "open_media %d %s %i", cameraId, rurl, index);
int retries = 0;
// 初始化 MPP 解码器
auto mppDecoder = std::make_shared<RKMpp>();
if (mppDecoder->initCodec("video/avc", false) != 0) {
__LOG_PRINT(ANDROID_LOG_ERROR, "rtsp", "MPP 初始化失败");
delete data;
return nullptr;
}
__LOG_PRINT(ANDROID_LOG_DEBUG, "rtsp", "MPP 初始化成功!");
mppDecoder->createInputBuffers();
while (retries < MAX_RETRIES) {
cv::VideoCapture stream1(rurl, cv::CAP_FFMPEG);
if (!stream1.isOpened()) {
__LOG_PRINT(ANDROID_LOG_DEBUG, "rtsp", "Cannot open RTSP stream!");
std::cerr << "无法打开 RTSP 流,尝试第 " << retries + 1 << " 次重连..." << std::endl;
retries++;
std::this_thread::sleep_for(std::chrono::seconds(RETRY_INTERVAL));
continue;
}
__LOG_PRINT(ANDROID_LOG_DEBUG, "rtsp", "oooooooooooo!");
cv::Mat encodedPacket; // 假设存储编码数据(如 H.264 包)
while (true) {
// 从流中读取编码数据包(需验证 OpenCV 是否支持直接读包)
if (!stream1.grab()) {
// 处理断流重连
break;
}
__LOG_PRINT(ANDROID_LOG_DEBUG, "rtsp", "kkkkkkkkkk!");
// 获取 MPP 输入缓冲区
int inputIndex = mppDecoder->dequeueInputBuffer(500000);
if (inputIndex < 0) continue;
__LOG_PRINT(ANDROID_LOG_DEBUG, "rtsp", "ppppppppppp!");
// 读取编码数据到缓冲区
if (stream1.retrieve(encodedPacket)) {
__LOG_PRINT(ANDROID_LOG_DEBUG, "rtsp", "iiiiiiiiii!");
char *inputBuf = mppDecoder->getInputBuffer(inputIndex);
__LOG_PRINT(ANDROID_LOG_DEBUG, "rtsp", "uuuuuuuuuuu!");
size_t dataSize = encodedPacket.total() * encodedPacket.elemSize();
__LOG_PRINT(ANDROID_LOG_DEBUG, "rtsp", "ffffffffff!");
memcpy(inputBuf, encodedPacket.data, dataSize);
__LOG_PRINT(ANDROID_LOG_DEBUG, "rtsp", "aaaaaaaaa!");
std::lock_guard<std::mutex> lock(decoderMutex);
mppDecoder->queueInputBuffer(inputIndex, 0, dataSize, 0);
__LOG_PRINT(ANDROID_LOG_DEBUG, "rtsp", "sssssssssss!");
}
__LOG_PRINT(ANDROID_LOG_DEBUG, "rtsp", "jjjjjjjjjjjj!");
// 获取解码后的帧
int outputIndex = mppDecoder->dequeueOutputBuffer(100000);
// if (outputIndex >= 0) {
// char *decodedFrame = mppDecoder.getFrame();
// screenShot(num, cameraId, index, decodedFrame); // 修改后的截图函数
// }
}
}
}