rtsp服務默認使用udp協議,容易丟包,報這個錯誤。改成tcp,則解決。tcp
//設置參數 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); // ......
//初始化輸出流上下文。 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); }