在 libav 中读取 dumepd RTP 流

Read dumepd RTP stream in libav

嗨,我需要一点 help/guidance 因为我在研究中遇到了困难。

问题:

如何在 API(通过编程)或控制台版本中使用 gstreamer 或 avlib (ffmpeg) 转换 RTP 数据。

数据

我有来自 RTP/RTCP 通过 TCP 的 RTP 转储,因此我可以准确地开始和停止文件中的每个 RTP 数据包。这是一个 H264 视频流转储。 数据采用这种方式是因为我需要通过 libcurl(我目前正在这样做)获取 RTCP/RTP 交错流

状态

我尝试使用 ffmpeg 来使用纯 RTP 数据包,但似乎通过控制台或编程使用 rtp 涉及 ffmpeg 中的 "starting" 整个 rtsp/rtp 会话业务。我已经停在那里,暂时我没有更深入地追求这条大道。我想这对于情人级别的 RTP API 是可能的,比如 ff_rtp_parse_packet() 我对这个库太陌生了,无法直接完成。

然后是 gstreamer 它具有更多的功能,无需编程即可完成,但目前我无法弄清楚如何将我拥有的 RTP 转储传递给它。

我还尝试了一些小技巧,通过 socat/nc 将转储流式传输到 udp 端口​​,并通过带有 sdp 文件的 ffplay 监听它作为输入,rtp 似乎有一些进展至少得到了认可,但是对于 socat 有大量的数据包丢失(数据发送太快了?),最后数据没有可视化。当我使用 nc 时,视频严重变形但至少没有那么多接收错误。

数据以某种方式未正确可视化。

我知道我可以对数据进行解包 "by hand" 但我的想法是通过某种库来完成,因为最后还会有第二个流与音频一起必须与视频。

对于解决此问题的任何帮助,我将不胜感激。 谢谢

经过一段时间终于有时间重新坐下来解决这个问题,终于得到了令我满意的解决方案。我继续使用 RTP 交错流(RTP 通过单个 TCP 连接与 RTCP 交错)。
所以我有一个交错的 RTCP/RTP 流,需要将其分解为音频(PCM A-Law)和视频(h.264 约束基线)RTP 数据包。
此处描述了包含 RTP 数据的 RTSP 流的分解 rfc2326
此处描述了 H264 的解包 rfc6184,对于 PCM A-Law,帧在 RTP 中是原始音频,因此不需要解包。

下一步是为每个流计算适当的 PTS(或呈现时间戳),这有点麻烦,但最终 Live555 代码来帮忙 (参见 )。
最后一个任务是将它混合到一个支持 PCM alaw 的容器中,我使用了 ffmpeg 的 avlibraries。
Internet 上有很多示例,但其中很多已经过时(ffmpeg 在 API 更改区域中非常 'dynamic')所以我发布了(最重要的部分)最终对我有用的内容:

设置部分:

#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include "libavutil/intreadwrite.h"
#include "libavutil/mathematics.h"

AVFormatContext   *formatContext;
AVOutputFormat    *outputFormat;
AVStream          *video_st;
AVStream          *audio_st;
AVCodec           *av_encode_codec = NULL;
AVCodec           *av_audio_encode_codec = NULL;
AVCodecContext    *av_video_encode_codec_ctx = NULL;
AVCodecContext    *av_audio_encode_codec_ctx = NULL;


av_register_all();
av_log_set_level(AV_LOG_TRACE);
outputFormat = av_guess_format(NULL, pu8outFileName, NULL);
outputFormat->video_codec = AV_CODEC_ID_H264;

av_encode_codec = avcodec_find_encoder(AV_CODEC_ID_H264);
av_audio_encode_codec = avcodec_find_encoder(AV_CODEC_ID_PCM_ALAW);
avformat_alloc_output_context2(&formatContext, NULL, NULL, pu8outFileName);
formatContext->oformat = outputFormat;
strcpy(formatContext->filename, pu8outFileName);
outputFormat->audio_codec  = AV_CODEC_ID_PCM_ALAW;

av_video_encode_codec_ctx = avcodec_alloc_context3(av_encode_codec);
av_audio_encode_codec_ctx = avcodec_alloc_context3(av_audio_encode_codec);

av_video_encode_codec_ctx->codec_id = outputFormat->video_codec;
av_video_encode_codec_ctx->codec_type = AVMEDIA_TYPE_VIDEO;
av_video_encode_codec_ctx->bit_rate = 4000;
av_video_encode_codec_ctx->width  = u32width;
av_video_encode_codec_ctx->height = u32height;
av_video_encode_codec_ctx->time_base = (AVRational){ 1, u8fps };
av_video_encode_codec_ctx->max_b_frames = 0;
av_video_encode_codec_ctx->pix_fmt = AV_PIX_FMT_YUV420P;

av_audio_encode_codec_ctx->sample_fmt = AV_SAMPLE_FMT_S16;
av_audio_encode_codec_ctx->codec_id = AV_CODEC_ID_PCM_ALAW; 
av_audio_encode_codec_ctx->codec_type = AVMEDIA_TYPE_AUDIO;
av_audio_encode_codec_ctx->sample_rate = 8000;
av_audio_encode_codec_ctx->channels = 1;
av_audio_encode_codec_ctx->time_base = (AVRational){ 1, u8fps };
av_audio_encode_codec_ctx->channel_layout = AV_CH_LAYOUT_MONO;

video_st = avformat_new_stream(formatContext, av_encode_codec);
audio_st = avformat_new_stream(formatContext, av_audio_encode_codec);
audio_st->index = 1;
video_st->avg_frame_rate = (AVRational){ 90000, 90000 / u8fps };
av_stream_set_r_frame_rate(video_st, (AVRational){ 90000, 90000 / u8fps });

视频的数据包是这样写的:

uint8_t  *pu8framePtr = video_frame;
AVPacket pkt = { 0 };
av_init_packet(&pkt);
if (0x65 == pu8framePtr[4] || 0x67 == pu8framePtr[4] || 0x68 == pu8framePtr[4]) 
{
    pkt.flags = AV_PKT_FLAG_KEY;
}

pkt.data = (uint8_t *)pu8framePtr;
pkt.size = u32LastFrameSize;

pkt.pts  = av_rescale_q(s_video_sync.fSyncTime.tv_sec * 1000000 + s_video_sync.fSyncTime.tv_usec, (AVRational){ 1, 1000000 }, video_st->time_base);
pkt.dts  = pkt.pts;
pkt.stream_index = video_st->index;
av_interleaved_write_frame(formatContext, &pkt);
av_packet_unref(&pkt);

对于这样的音频:

AVPacket pkt = { 0 };
av_init_packet(&pkt);
pkt.flags = AV_PKT_FLAG_KEY;
pkt.data = (uint8_t *)pu8framePtr;
pkt.size = u32AudioDataLen;

pkt.pts  = av_rescale_q(s_audio_sync.fSyncTime.tv_sec * 1000000 + s_audio_sync.fSyncTime.tv_usec, (AVRational){ 1, 1000000 }, audio_st->time_base);
pkt.dts  = pkt.pts;
pkt.stream_index = audio_st->index;
if (u8FirstIFrameFound) {av_interleaved_write_frame(formatContext, &pkt);}
av_packet_unref(&pkt)

最后是一些 deinits:

av_write_trailer(formatContext);
av_dump_format(formatContext, 0, pu8outFileName, 1);
avcodec_free_context(&av_video_encode_codec_ctx);
avcodec_free_context(&av_audio_encode_codec_ctx);
avio_closep(&formatContext->pb);
avformat_free_context(formatContext);