上节分享了打印AVPacket中的时间戳信息,这节准备分享一下利用rtmp协议将视频和音频数据推流到nginx服务器上并利用vlc来进行拉流播放视频,达到直播的效果。
由于AVPacket包数据中的解码时间戳dts、显示时间戳pts是使用输入流来进行配置的,上节分享的是利用av_read_frame()函数读取数据,由于第一个参数是输入流的帧,所以配置的dts和pts就是输入流的配置,要想用输出流的pts、dts配置,可以利用ffmpeg中的int64_t av_rescale_q_rnd(int64_t a, int64_t b, int64_t c, enum AVRounding rnd); 函数来进行转化,该函数的原理是:
可以假设原来的dts为x,利用c来作为度量的值为y,这样就可以列出两个方程式
x/b=a ,x/c=y ,所以a*b=c*y,及最后的y=a*b/c;转化的代码如下,
AVRational itime = ictx->streams[pkt.stream_index]->time_base;
AVRational otime = octx->streams[pkt.stream_index]->time_base;
pkt.pts=av_rescale_q_rnd(pkt.pts,itime,otime,(AVRounding)(AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX));
pkt.dts = av_rescale_q_rnd(pkt.dts, itime, otime, (AVRounding)(AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX));
pkt.duration = av_rescale_q_rnd(pkt.duration, itime, otime, (AVRounding)(AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX));
pkt.pos = -1;
最后再利用ffmpeg的av_usleep()函数来进行延时即可实现推流。
但是利用固定延时这种方式来进行推流的话,播放出来的画面会有卡顿的现象,可以通过改变视频解码的时间和发送的时间一致来解决,这里题主修改了多次av_usleep()函数的参数,发现只要解码时间和发送时间不同步就会导致视频卡顿,具体代码如下。
//视频帧推送速度
if (ictx->streams[pkt.stream_index]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
{
//时间基数
AVRational tb = ictx->streams[pkt.stream_index]->time_base;
//已经过去的时间
long long now = av_gettime() - startTime;
long long dts = 0;
dts = pkt.dts * (1000 * 1000 * r2d(tb));
cout << dts << " " << now <<flush<<endl;
if(dts>now)
av_usleep(dts-now);
}
最后利用vlc播放器就可以实现直播推流了。设置方式如下
最后的效果
通过读取本地的视频推流实现直播。下面是源码分享。
extern "C"
{
#include "libavformat/avformat.h"
#include "libavutil/time.h"
}
#include <iostream>
using namespace std;
#pragma comment(lib,"avformat.lib")
#pragma comment(lib,"avutil.lib")
#pragma comment(lib,"avcodec.lib")
int XError(int errNum)
{
char buf[1024] = { 0 };
av_strerror(errNum, buf, sizeof(buf));
std::cout << buf << endl;
getchar();
return -1;
}
static double r2d(AVRational r)
{
return r.num == 0 || r.den == 0 ? 0. : (double)r.num / (double)r.den;
}
int main(int argc, char *argv[])
{
char *inUrl = "test.flv";
char *outUrl = "rtmp://192.168.198.128/live";
//初始化所有封装和解封装 flv mp4 mov mp3
av_register_all();
//初始化网络库
avformat_network_init();
//1 打开文件,解封装
//输入封装上下文
AVFormatContext *ictx = NULL;
//打开文件,解封文件头
int re=avformat_open_input(&ictx,inUrl,NULL,NULL);
if (re != 0)
{
return XError(re);
}
std::cout << "open file" << inUrl << "Success" << endl;
//获取音视频流信息,h264 flv
re=avformat_find_stream_info(ictx,0);
if (re != 0)
{
return XError(re);
}
av_dump_format(ictx,0,inUrl,0);
/
//输出流
//创建输出流上下文
AVFormatContext *octx = NULL;
re=avformat_alloc_output_context2(&octx,0,"flv",outUrl);
if (!octx)
{
return XError(re);
}
std::cout << "octx create success!" << endl;
//配置输出流
//遍历输入的AVStream
for (int i = 0;i < ictx->nb_streams;i++)
{
//创建输出流
AVStream *out= avformat_new_stream(octx,ictx->streams[i]->codec->codec);
if (!out)
{
return XError(0);
}
//复制配置信息,用于MP4
//re=avcodec_copy_context(out->codec, ictx->streams[i]->codec);
re = avcodec_parameters_copy(out->codecpar, ictx->streams[i]->codecpar);
out->codec->codec_tag = 0;
}
av_dump_format(octx, 0, outUrl, 1);
//
//rtmp推流
//打开io
re=avio_open(&octx->pb,outUrl,AVIO_FLAG_WRITE);
if (!octx->pb)
{
return XError(re);
}
//写入头信息
re=avformat_write_header(octx,0);
if (re < 0)
{
return XError(re);
}
cout << "avformat_writr_header" << re << endl;
//推流每一帧数据
AVPacket pkt;
long long startTime = av_gettime();
for (;;)
{
re=av_read_frame(ictx,&pkt);
if (re != 0)
{
break;
}
//cout << pkt.pts <<" "<<flush;
//计算转化pts dts
AVRational itime = ictx->streams[pkt.stream_index]->time_base;
AVRational otime = octx->streams[pkt.stream_index]->time_base;
pkt.pts=av_rescale_q_rnd(pkt.pts,itime,otime,(AVRounding)(AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX));
pkt.dts = av_rescale_q_rnd(pkt.dts, itime, otime, (AVRounding)(AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX));
pkt.duration = av_rescale_q_rnd(pkt.duration, itime, otime, (AVRounding)(AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX));
pkt.pos = -1;
//视频帧推送速度
if (ictx->streams[pkt.stream_index]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
{
//时间基数
AVRational tb = ictx->streams[pkt.stream_index]->time_base;
//已经过去的时间
long long now = av_gettime() - startTime;
long long dts = 0;
dts = pkt.dts * (1000 * 1000 * r2d(tb));
cout << dts << " " << now <<flush<<endl;
if(dts>now)
av_usleep(dts-now);
}
re=av_interleaved_write_frame(octx,&pkt);
//av_usleep(10*1000);
if (re < 0)
{
return XError(re);
}
//清除空间
//av_packet_unref(&pkt);
}
std::cout << "file to rtmp test" << endl;
getchar();
return 0;
}