xcamera_record.h
cpp
#pragma once
#include <string>
#include "xtools.h"
class XCameraRecord :public XThread
{
public:
void set_rtsp_url(std::string url) { rtsp_url_ = url; }
void set_save_path(std::string s) { save_path_ = s; }
private:
void Main();//124
std::string rtsp_url_;
std::string save_path_;//存储的根目录
};
xcamera_record.cpp
cpp
#include "xcamera_record.h"
#include "xdemux_task.h"
#include "xmux_task.h"
void XCameraRecord::Main()//124
{
XDemuxTask demux;
XMuxTask mux;
if (rtsp_url_.empty())
{
LOGERROR("open rtsp url failed!");
return;
}
//自定重连
while (!is_exit_)
{
if (demux.Open(rtsp_url_)) //最坏情况阻塞1秒
{
break;
}
MSleep(10);
continue;
}
//音视频参数
auto vpara = demux.CopyVideoPara();
if (!vpara)
{
LOGERROR("demux.CopyVideoPara failed!");
//需要考虑demux 资源释放的问题
demux.Stop();
return;
}
//启动解封装线程,提前启动,防止超时
demux.Start();
auto apara = demux.CopyAudioPara();
AVCodecParameters* para = nullptr; //音频参数
AVRational* timebase = nullptr; //音频时间基数
if (apara)
{
para = apara->para;
timebase = apara->time_base;
}
if (!mux.Open(save_path_.c_str(),
vpara->para, vpara->time_base,//视频参数
para, timebase)) //音频参数
{
LOGERROR("mux.Open rtsp_url_ failed!");
demux.Stop();
mux.Stop();
return;
}
demux.set_next(&mux);
mux.Start();
MSleep(3000);
mux.Stop();
demux.Stop();
}
主函数:
cpp
int main(int argc, char *argv[])
{
XCameraRecord xr;
xr.set_rtsp_url(
"v1080.mp4");
xr.set_save_path("./testrecord.mp4");
xr.Start();
}
运行结果:
录制了两个视频在当前目录下