前面2篇已經講解了如何搭建直播服務器 ffmpeg + nginx配置智播推流服務器和ffmpeg + crtmpserver實現直播推流,並且通過ffmpeg命令實現了推流,下面講解下如何用代碼實現推流,並且通過設置dts來實現推流的速度。
之前也寫過,將攝像頭的數據保存到本地爲mp4:ffmpeg 獲取電腦攝像頭數據,並且保存爲MP4視頻 ,這裏其實差不多,只是將輸出設備從本地文件mp4 改爲了 rtmp://192.168.32.129/live(自己搭建的rtmp服務器)
這裏主要是介紹下,解封裝後,如何創建輸出流,並且控制推流速度,讓播放器端正常播放。
創建輸出流
- 創建輸出上下文
//創建輸出流上下文
AVFormatContext *octx = NULL;
re = avformat_alloc_output_context2(&octx, 0, "flv", outUrl);
if (!octx)
{
return XError(re);
}
cout << "octx create success!" << endl;
- 創建輸出流
for (int i = 0; i < ictx->nb_streams; i++)
{
//創建輸出流
AVCodec *codec = avcodec_find_decoder(ictx->streams[i]->codecpar->codec_id);
AVStream *out = avformat_new_stream(octx, 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);
這裏因爲avStream結構體中codec已經棄用,但用新版本推送mp4視頻的時候會出現不支持,但ffmpeg還是會封裝推送,在服務器端支持,並且解析出來,但爲了不出現這個錯誤有2種方案:
1、將推流的mp4文件用 ffmpeg轉封裝爲flv,然後用新版本來實現推流
ffmpeg -i 1080.mp4 test.flv
2、切換到舊版本
這裏我用的新版本,推送flv視頻
推流
推流主要是要通過pts 或者dts來進行控制速度。
原理: 在推流開始時候幾下時間start(1970至今的時間,單位爲微妙 微秒:百萬分之一秒),然後解封轉後每一幀,得到pts 和 dts,然後將packet 的pts 和 dts轉化爲outAvformatContext 的,並且如果dts 轉化爲與start一樣時間單位時候,如果大於 now - start ,就休眠 dts-(now - start)
這裏用到了2個ffmpeg的函數:
- av_strerror 輸出錯誤信息
int XError(int errNum)
{
char buf[1024] = { 0 };
av_strerror(errNum, buf, sizeof(buf));
cout << buf << endl;
getchar();
return -1;
}
- av_gettime:1970至今的時間,單位爲微妙 微秒:百萬分之一秒
- av_rescale_q_rnd 將輸入的pts格式轉化爲輸出上下文的格式
int64_t av_rescale_rnd(int64_t a, int64_t b, int64_t c, enum AVRounding rnd)
最後一個參數轉換的方式:
AV_ROUND_ZERO = 0, // Round toward zero. 趨近於0
AV_ROUND_INF = 1, // Round away from zero. 趨遠於0
AV_ROUND_DOWN = 2, // Round toward -infinity. 趨於更小的整數
AV_ROUND_UP = 3, // Round toward +infinity. 趨於更大的整數
AV_ROUND_NEAR_INF = 5, // Round to nearest and halfway cases away from zero.
// 四捨五入,小於0.5取值趨向0,大於0.5取值趨遠於0
完整的代碼如下:(沒有考慮控件釋放,和 優化)
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));
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[])
{
const char *inUrl = "test.flv";
const char *outUrl = "rtmp://192.168.32.129/live";
//初始化所有封裝和解封裝 flv mp4 mov mp3
av_register_all();
//初始化網絡庫
avformat_network_init();
//////////////////////////////////////////////////////////////////////////
//輸入流 1 打開文件,解封裝
//輸入封裝上下文
AVFormatContext *ictx = NULL;
//打開文件,解封文件頭
int re = avformat_open_input(&ictx, inUrl, 0, 0);
if (re != 0)
{
return XError(re);
}
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);
}
cout << "octx create success!" << endl;
//配置輸出流
//遍歷輸入的AVStream
for (int i = 0; i < ictx->nb_streams; i++)
{
//創建輸出流
AVCodec *codec = avcodec_find_decoder(ictx->streams[i]->codecpar->codec_id);
AVStream *out = avformat_new_stream(octx, codec);
//AVStream *out = avformat_new_stream(octx, );
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_write_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_NEAR_INF));
pkt.dts = av_rescale_q_rnd(pkt.pts, itime, otime, (AVRounding)(AV_ROUND_NEAR_INF | AV_ROUND_NEAR_INF));
pkt.duration = av_rescale_q_rnd(pkt.duration, itime, otime, (AVRounding)(AV_ROUND_NEAR_INF | AV_ROUND_NEAR_INF));
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));
if (dts > now)
av_usleep(dts - now);
}
re = av_interleaved_write_frame(octx, &pkt);
if (re < 0)
{
return XError(re);
}
}
cout << "file to rtmp test" << endl;
getchar();
return 0;
}