!!better!! — Rtspvideoplugin
import cv2 import numpy as np from threading import Thread class RTSPVideoPlugin: def init (self, rtsp_url): self.url = rtsp_url self.cap = None self.running = False self.frame = None
Client (Plugin) Server (Camera) |--- OPTIONS ---------------->| |<--- 200 OK -----------------| |--- DESCRIBE --------------->| |<--- SDP (Session Description)| |--- SETUP (client_port)----->| |<--- 200 OK (server_port)----| |--- PLAY ------------------->| |<--- 200 OK -----------------| |<--- RTP/UDP Stream ---------| 3.1 Minimal RTSP Client Core // RTSPClient.h #include <string> #include <functional> #include <thread> #include <cstring> #include <sys/socket.h> #include <arpa/inet.h> #include <unistd.h> class RTSPVideoPlugin public: using FrameCallback = std::function<void(uint8_t* data, int width, int height, int stride)>; rtspvideoplugin
void stop() m_running = false; if (m_thread.joinable()) m_thread.join(); import cv2 import numpy as np from threading
bool readFrame(uint8_t** outRGB, int* width, int* height) AVPacket pkt; if (av_read_frame(m_fmtCtx, &pkt) < 0) return false; if (avcodec_send_packet(m_codecCtx, &pkt) == 0) while (avcodec_receive_frame(m_codecCtx, m_frame) == 0) // Convert YUV to RGB if (!m_swsCtx) m_swsCtx = sws_getContext(m_frame->width, m_frame->height, m_frame->format, m_frame->width, m_frame->height, AV_PIX_FMT_RGB24, SWS_BILINEAR, nullptr, nullptr, nullptr); *width = m_frame->width; *height = m_frame->height; sws_scale(m_swsCtx, m_frame->data, m_frame->linesize, 0, m_frame->height, outRGB, &m_frame->linesize[0]); av_packet_unref(&pkt); return true; av_packet_unref(&pkt); return false; --- 200 OK -----------------| |<
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

