<<ffmpeg 中 rtsp相关源码解析(四)>>
简单介绍了rtp包是通过ff_rtsp_fetch_packet来获取的
我们更深入地问一个问题,那么对于vp8 、h264等音视频编解码标准,这些packet又是如何处理的呢?
这边就涉及一个非常重要的结构体
RTPDynamicProtocolHandler
位于rtpdec.h文件中定义如下
struct RTPDynamicProtocolHandler {
const char *enc_name;
enum AVMediaType codec_type;
enum AVCodecID codec_id;
enum AVStreamParseType need_parsing;
int static_payload_id; /* 0 means no payload id is set. 0 is a valid
* payload ID (PCMU), too, but that format doesn't
* require any custom depacketization code. */
int priv_data_size;
/** Initialize dynamic protocol handler, called after the full rtpmap line is parsed, may be null */
int (*init)(AVFormatContext *s, int st_index, PayloadContext *priv_data);
/** Parse the a= line from the sdp field */
int (*parse_sdp_a_line)(AVFormatContext *s, int st_index,
PayloadContext *priv_data, const char *line);
/** Free any data needed by the rtp parsing for this dynamic data.
* Don't free the protocol_data pointer itself, that is freed by the
* caller. This is called even if the init method failed. */
void (*close)(PayloadContext *protocol_data);
/** Parse handler for this dynamic packet */
DynamicPayloadPacketHandlerProc parse_packet;
int (*need_keyframe)(PayloadContext *context);
struct RTPDynamicProtocolHandler *next;
};
其中DynamicPayloadPacketHandlerProc parse_packet; 就是我们关注的解析包的方法
以h264协议为例,ffmpeg 在 rtpdec_h264.c中定义了ff_h264_dynamic_handler,并且在rtpdec.c 中
通过ff_register_dynamic_payload_handler(&ff_h264_dynamic_handler);注册了这个handler
RTPDynamicProtocolHandler ff_h264_dynamic_handler = {
.enc_name = "H264",
.codec_type = AVMEDIA_TYPE_VIDEO,
.codec_id = AV_CODEC_ID_H264,
.need_parsing = AVSTREAM_PARSE_FULL,
.priv_data_size = sizeof(PayloadContext),
.parse_sdp_a_line = parse_h264_sdp_line,
.close = h264_close_context,
.parse_packet = h264_handle_packet,
};
而它的调用方式如下
如果有buffer
ff_rtsp_fetch_packet ---> ff_rtp_parse_packet ----> rtp_parse_one_packet ---> rtp_parse_packet_internal ---> parse_packet
如果没有buffer
ff_rtsp_fetch_packet ---> ff_rtp_parse_packet ----> rtp_parse_one_packet ---> parse_packet