ffmpeg-設定推流,拉流使用的協議型別(TCP/UDP)
阿新 • • 發佈:2019-01-02
如有錯誤,請指正,謝謝。
拉流(設定TCP/UDP)
//設定引數
AVDictionary *format_opts = NULL;
av_dict_set(&format_opts, "stimeout", std::to_string( 2* 1000000).c_str(), 0); //設定連結超時時間(us)
av_dict_set(&format_opts, "rtsp_transport", "tcp", 0); //設定推流的方式,預設udp。
//初始化輸入上下文
AVFormatContext * m_InputContext = avformat_alloc_context();
//開啟輸入流。
avformat_open_input(&m_InputContext, "rtsp://127.0.0.1:554/", NULL, &format_opts);
// ......
推流(設定TCP/UDP)
//初始化輸出流上下文。
AVFormatContext * output_format_context_ = NULL;
avformat_alloc_output_context2(&output_format_context_, NULL, "rtsp", "rtsp://127.0.0.1:554/");
/*
新增流資訊
//TODO
*/
//設定引數,設定為TCP推流, 預設UDP
AVDictionary *format_opts = NULL;
av_dict_set(&format_opts, "stimeout", std::to_string(2 * 1000000).c_str(), 0);
av_dict_set(&format_opts, "rtsp_transport", "tcp", 0);
//寫入輸出頭(建立rtsp連線)
avformat_write_header(output_format_context_, &format_opts);
while(1)
{
AVPakcet media_pkt;
/*
初始化PKT,
讀取資料,
填充資料,
*/
//傳送資料, 多個流時需要使用 av_interleaved_write_frame, 另附ret部分錯誤碼,見其他文章。
int ret = av_interleaved_write_frame(output_format_context_, &media_pkt);
//釋放資料
av_packet_unref(&media_pkt);
av_free_packet(&media_pkt);
}