This commit is contained in:
cxh 2025-11-24 16:37:53 +08:00
parent ac51d125dd
commit 843dbd09c6

View File

@ -6,6 +6,9 @@
#include <algorithm>
#include <thread>
#include <chrono>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/videodev2.h>
// 静态变量定义
GMainLoop *RTSPManager::loop = nullptr;
@ -19,6 +22,34 @@ std::mutex RTSPManager::mounted_factories_mutex;
std::unordered_map<std::string, std::vector<GstRTSPMedia *>> RTSPManager::media_map;
std::mutex RTSPManager::media_map_mutex;
bool set_v4l2_format(const std::string &dev, int width, int height)
{
int fd = open(dev.c_str(), O_RDWR);
if (fd < 0)
{
LOG_ERROR("Failed to open " + dev);
return false;
}
struct v4l2_format fmt;
memset(&fmt, 0, sizeof(fmt));
fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
fmt.fmt.pix_mp.width = width;
fmt.fmt.pix_mp.height = height;
fmt.fmt.pix_mp.pixelformat = V4L2_PIX_FMT_NV12;
fmt.fmt.pix_mp.num_planes = 1;
if (ioctl(fd, VIDIOC_S_FMT, &fmt) < 0)
{
LOG_ERROR("VIDIOC_S_FMT failed for " + dev);
close(fd);
return false;
}
close(fd);
return true;
}
void RTSPManager::init()
{
gst_init(nullptr, nullptr);
@ -27,6 +58,11 @@ void RTSPManager::init()
GstRTSPMediaFactory *RTSPManager::create_media_factory(const Camera &cam)
{
if (!set_v4l2_format(cam.device, cam.width, cam.height))
{
LOG_ERROR("[RTSP] Failed to set V4L2 format for " + cam.name);
}
int out_width = cam.width;
int out_height = cam.height;