# v4l2_ipc_live555 **Repository Path**: GarySC30/v4l2_ipc_live555 ## Basic Information - **Project Name**: v4l2_ipc_live555 - **Description**: 基于live555+ffmpeg+x264开发网络摄像头(IPC) - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 8 - **Created**: 2024-07-24 - **Last Updated**: 2024-07-24 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README 更多音视频知识分享 腾讯课堂 零声教育 # 查看主持的分辨率格式 v4l2-ctl -d /dev/video0 --list-formats-ext # 注意 V4L2 目前已经设置为MJPEG输出格式,对于使用到的FFmpeg解码也固定为MJPEG格式 需要需要修改为YUV,请自行修改程序,比如取消解码器过程,直接使用YUV数据进行编码 # 设置分辨率和摄像头设备 在include/config.h 修改分辨率格式和摄像头 ``` #define CAMERA_DEV_NAME "/dev/video0" #define CAMERA_WIDTH 640 #define CAMERA_HEIGHT 480 ``` # 第三方库文件编译 1. 需要编译live555 ``` # 官方下载较新稳定的live555 git clone https://github.com/rgaufman/live555.git # 进入live555目录 cd live555 # 产生配置文件 ./genMakefiles linux # 编译 make # 安装 sudo make install ``` 本项目已经把该https://github.com/rgaufman/live555.git 拷贝到live555目录,可以直接 ``` cd live555 # 编译 make # 安装 sudo make install ``` live555被安装到的路径: - 头文件安装: /usr/local/include - 库文件安装: /usr/local/lib - 执行文件: /usr/local/bin 2. 编译FFmpeg,可以参考音视频VIP课程 3. 编译x264,在音视频VIP课程编译FFmpeg时已经包含了x264编译 # 本项目编译和执行 在本项目目录直接make ``` make ``` 生成v4l2_ipc_live555执行文件 运行 ``` sudo ./v4l2_ipc_live555 ``` 运行该程序后终端打印出来url地址,客户端使用该url地址访问。 比如:ffplay rtsp://192.168.1.21:8554/video0 # 代码逻辑流程 重点在于 - 何时打开摄像头 - 何时初始化编码器 - 何时读取摄像头数据 通过gdb下断点重点debug的函数 - Camera::Camera - Camera::initDev - Camera::startStream - Camera::readFrame - H264Encoder::x264Init ## 添加源 ``` #0 Camera::Camera (this=0x682e30, bufferCount=3) at /home/lqf/media/v4l2_ipc_live555/src/camera.cpp:3 #1 0x0000000000402870 in CameraFramedSource::CameraFramedSource (this=0x682d30, env=...) at /home/lqf/media/v4l2_ipc_live555/src/CameraFramed.cpp:35 #2 0x0000000000403ed4 in main () at /home/lqf/media/v4l2_ipc_live555/src/main.cpp:23 ``` sms->addSubsession(new H264OnDemandServerMediaSubsession(*env, new CameraFramedSource(*env))); 添加摄像头源 ``` #0 Camera::initDev (this=0x682e30, devName=0x4419b8 "/dev/video0", width=1920, height=1920) at /home/lqf/media/v4l2_ipc_live555/src/camera.cpp:13 #1 0x00000000004028bd in CameraFramedSource::CameraFramedSource (this=0x682d30, env=...) at /home/lqf/media/v4l2_ipc_live555/src/CameraFramed.cpp:37 #2 0x0000000000403ed4 in main () at /home/lqf/media/v4l2_ipc_live555/src/main.cpp:23 ``` CameraFramedSource里创建Camera和初始化Camera ``` #0 Camera::startStream (this=0x682e30) at /home/lqf/media/v4l2_ipc_live555/src/camera.cpp:143 #1 0x0000000000402914 in CameraFramedSource::CameraFramedSource (this=0x682d30, env=...) at /home/lqf/media/v4l2_ipc_live555/src/CameraFramed.cpp:42 #2 0x0000000000403ed4 in main () at /home/lqf/media/v4l2_ipc_live555/src/main.cpp:23 ``` 启动源-启动摄像头 ``` #0 H264Encoder::x264Init (this=0x682e80, picture=0x68afc0, width=1920, height=1080) at /home/lqf/media/v4l2_ipc_live555/src/H264Encoder.cpp:26 #1 0x000000000040295e in CameraFramedSource::CameraFramedSource (this=0x682d30, env=...) at /home/lqf/media/v4l2_ipc_live555/src/CameraFramed.cpp:43 #2 0x0000000000403ed4 in main () at /home/lqf/media/v4l2_ipc_live555/src/main.cpp:23 ``` 初始化h264编码器 ## 有客户端拉流-响应 有客户端访问触发读取流 ``` #0 Camera::readFrame (this=0x682e30, picDest=0x68afc0, picFmt=AV_PIX_FMT_YUV420P, picWidth=1920, picHeight=1080) at /home/lqf/media/v4l2_ipc_live555/src/camera.cpp:188 #1 0x0000000000402a2f in CameraFramedSource::doGetNextFrame (this=0x682d30) at /home/lqf/media/v4l2_ipc_live555/src/CameraFramed.cpp:56 #2 0x0000000000436ca1 in StreamParser::ensureValidBytes1(unsigned int) () #3 0x00000000004229dd in H264or5VideoStreamParser::parse() () #4 0x0000000000420341 in MPEGVideoStreamFramer::continueReadProcessing() () #5 0x000000000042c493 in H264or5Fragmenter::doGetNextFrame() () #6 0x0000000000406ee7 in MultiFramedRTPSink::packFrame() () #7 0x000000000040702e in MultiFramedRTPSink::continuePlaying() () #8 0x000000000041b0f0 in StreamState::startPlaying(Destinations*, unsigned int, void (*)(void*), void*, void (*)(void*, unsigned char), void*) () #9 0x000000000041b288 in OnDemandServerMediaSubsession::startStream(unsigned int, void*, void (*)(void*), void*, unsigned short&, unsigned int&, void (*)(void*, unsigned char), void*) () #10 0x000000000040b767 in RTSPServer::RTSPClientSession::handleCmd_PLAY(RTSPServer::RTSPClientConnection*, ServerMediaSubsession*, char const*) () #11 0x000000000040cdcd in RTSPServer::RTSPClientSession::handleCmd_withinSession(RTSPServer::RTSPClientConnection*, char const*, char const*, char const*, char const*) () ---Type to continue, or q to quit--- #12 0x000000000040a686 in RTSPServer::RTSPClientConnection::handleRequestBytes(int) () #13 0x0000000000408867 in GenericMediaServer::ClientConnection::incomingRequestHandler() () #14 0x000000000043b5cd in BasicTaskScheduler::SingleStep(unsigned int) () #15 0x000000000043cacc in BasicTaskScheduler0::doEventLoop(char volatile*) () #16 0x0000000000403f57 in main () at /home/lqf/media/v4l2_ipc_live555/src/main.cpp:26 ``` # 版权声明 本项目基于https://github.com/lam2003/v4l2_webstream_server.git进行修改 主要改动: - 替换过时的avcodec_decode_video2 - 替换过时的AVPicture - 替换原有的先保存mpjeg图片再进行编码 - 原有的swscale有内存泄漏问题 - 修改头文件包含的编译错误 - 增加项目文档 # 进一步需要考虑的问题 - 统一代码风格,大家自行调整 - url、分辨率、设备名可以通过命令行输入,大家自行调整 - 部分返回值没有做判断,大家自行完善