ffmpeg写rtmp推流中的坑:sps/pps和aac header的首包发送

1、需要在编码中使能CODEC_FLAG_GLOBAL_HEADER

AVCodecContext的flags |= CODEC_FLAG_GLOBAL_HEADER

设置这个标志位后,才能在AVCodecContext中的extradata和extradata_size得到对应的音视频头信息。

视频: AVCDecoderConfigurationRecord+ sps + pps

音频: ASC FLAG

2、设置rtmp发送中的头信息

    AVCodecContext* dest = pStream->codec;

    if (_pSendCtx->oformat->flags & AVFMT_GLOBALHEADER) {
        dest->flags |= CODEC_FLAG_GLOBAL_HEADER;
        HostLogInfo("video oformat->flags=0x%08x, extradata_size=%d", _pSendCtx->oformat->flags, _pVCodeCtx->extradata_size); 
        // for flv, we should avoid invoking ff_avc_parse_nal_units_buf() in flvenc.c
        if (_pVCodeCtx->extradata_size > 0) {
            int extra_size = (uint64_t)_pVCodeCtx->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE;
            dest->extradata = (uint8_t*)av_mallocz(extra_size);
            memcpy(dest->extradata, _pVCodeCtx->extradata, _pVCodeCtx->extradata_size);
            dest->extradata_size = _pVCodeCtx->extradata_size;
        }
    }
关键点:

1) 设置dest中的flag信息

2) 设置dest中的extradata, extradata_size信息

这样在avformat_write_header函数中才会头几帧写入sps/pps asc

发布了21 篇原创文章 · 获赞 18 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/sweibd/article/details/79175541
今日推荐