int m_rtpSocket; std::thread m_thread; bool m_running = false; std::string m_url; FrameCallback m_callback; ; extern "C" #include <libavcodec/avcodec.h> #include <libavformat/avformat.h> #include <libswscale/swscale.h>
public: bool open(const char* rtspUrl) avformat_network_init(); AVDictionary* opts = nullptr; av_dict_set(&opts, "rtsp_transport", "tcp", 0); // TCP for reliability av_dict_set(&opts, "max_delay", "500000", 0); rtspvideoplugin
extern "C" __declspec(dllexport) void* CreateRTSPPlayer() return new FFmpegRTSPDecoder(); __declspec(dllexport) bool OpenStream(void* player, const char* url) return ((FFmpegRTSPDecoder*)player)->open(url); bool m_running = false
if (avformat_open_input(&m_fmtCtx, rtspUrl, nullptr, &opts) < 0) return false; if (avformat_find_stream_info(m_fmtCtx, nullptr) < 0) return false; // Find video stream int videoStream = -1; for (int i = 0; i < m_fmtCtx->nb_streams; i++) if (m_fmtCtx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) videoStream = i; break; AVCodec* codec = avcodec_find_decoder(m_fmtCtx->streams[videoStream]->codecpar->codec_id); m_codecCtx = avcodec_alloc_context3(codec); avcodec_parameters_to_context(m_codecCtx, m_fmtCtx->streams[videoStream]->codecpar); avcodec_open2(m_codecCtx, codec, nullptr); m_frame = av_frame_alloc(); return true; extern "C" #include <
def start(self): self.cap = cv2.VideoCapture(self.url, cv2.CAP_FFMPEG) # Set options self.cap.set(cv2.CAP_PROP_BUFFERSIZE, 1) self.cap.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter_fourcc('H','2','6','4')) self.running = True Thread(target=self._update, daemon=True).start() def _update(self): while self.running: ret, self.frame = self.cap.read() if not ret: self.cap.release() self.cap = cv2.VideoCapture(self.url) # Reconnect def get_frame(self): return self.frame