Freeswitch的mod_av模块优化

最近在解决一个Freeswitch录制视频后出现视频文件播放花屏问题,大概梳理mod_av模块的录制流程,并做了一些流程上的优化。

mod_av的录制流程如下:

收到的RTP音视频包-> 解码 -> 然后写文件(首先是编码-> 然后调用ffmpeg的音视频混合接口输出为mp4文件)

首先做的流程优化,去掉画中横线的两个流程,直接将接收到的编码帧写文件:

收到的RTP音视频包-> 解码 -> 然后写文件(首先是编码-> 调用ffmpeg的音视频混合接口输出为mp4文件)


然后把录制出来的文件播放,发现还是花屏,多次尝试后发现,是在调用av_packet_rescale_ts后,才调用av_interleaved_write_frame,而av_packet_rescale_ts函数则是把AVCodecContext 的time_base转换为AVStream的time_base,先看看time_base的理解:ffmpeg存在多个时间基准(time_base),对应不同的阶段(结构体),每个time_base具体的值不一样,ffmpeg提供函数在各个time_base中进行切换。

AVStream->time_base比AVCodecContext->time_base精度要高(数值要小),比如AVStream->time_base为1/90000,而AVCodecContext->time_base为1/30(假设frame_rate为30);同样的pts和dts,以AVStream->time_base为单位,数值要比以AVCodecContext->time_base为单位要大。

编码后,pkt.pts、pkt.dts使用AVCodecContext->time_base为单位,后通过调用"av_packet_rescale_ts"转换为AVStream->time_base为单位。

首先修改time_base的初始化赋值:

原来的值:

        mst->st->time_base.den = 1000;
        mst->st->time_base.num = 1;
        c->time_base.den = 1000;
        c->time_base.num = 1;


修改为:

        mst->st->time_base.den = 90000;
        mst->st->time_base.num = 1;
        c->time_base.den = 25;
        c->time_base.num = 1;


从网络接收到的rtp包是编码后的包,首先需要把多个rtp包组成一个完整的frame的包,然后pts需要重设,代码:


                   if (frame->m) {
                        AVPacket *pkt = (AVPacket *)malloc(sizeof(AVPacket));
                        uint32_t size = switch_buffer_inuse(context_temp->nalu_buffer);
                            if (size > 0){
                                uint8_t *temp_data;         
                                static uint8_t ff_input_buffer_padding[FF_INPUT_BUFFER_PADDING_SIZE] = { 0};
 
                                switch_buffer_write(context_temp->nalu_buffer, ff_input_buffer_padding, sizeof(ff_input_buffer_padding));  //lyz delete
 
                                switch_buffer_peek_zerocopy(context_temp->nalu_buffer, (const void **)&temp_data);
 
                                av_init_packet(pkt);
                                av_new_packet(pkt, size);
                                pkt->size = size;
 
                                if (switch_test_flag(frame, SFF_WAIT_KEY_FRAME)){
                                        pkt->flags        |= AV_PKT_FLAG_KEY;
                                }
 
                                pkt->pts = context_temp->pts++;//context_temp->pts + 40;//((frame->timestamp - context_temp->last_received_timestamp)/90);
                                pkt->dts = pkt->pts; 
                                //pkt->duration = 40;
 
                                //context_temp->pts = pkt->pts;
                                
                                memcpy((unsigned char *)pkt->data, (unsigned char *)temp_data, size);
                                /*
                                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "write video stream,pkt:%08x, data:%08x, pkt->pts:%d, data[0]:%02x, data[1]:%02x, data[2]:%02x, data[3]:%02x, data[4]:%02x\n", 
                                    pkt, 
                                    pkt->data, 
                                    pkt->pts, 
                                    pkt->data[0],
                                    pkt->data[1],
                                    pkt->data[2],
                                    pkt->data[3],
                                    pkt->data[4]);
                                    */
 
                                context_temp->last_received_timestamp = frame->timestamp;
                                context_temp->last_received_complete_picture = frame->m ? SWITCH_TRUE : SWITCH_FALSE;
 
                                switch_queue_push(context->eh.video_queue, pkt);
                            }
                        
                        switch_buffer_zero(context_temp->nalu_buffer);
                   }


注释掉mod_av原来设置pts的代码,


if (switch_queue_pop(eh->video_queue, &pop) == SWITCH_STATUS_SUCCESS) {
                     switch_img_free(&img);
 
            if (!pop) {
                goto endfor;
            }
 
                    pkt = (AVPacket *)pop;
 
 
 
            ret = write_frame(eh->fc, &eh->video_st->st->codec->time_base, eh->video_st->st, pkt);

后来发现,不修改录制流程,只修改编码时pts的值也能解决这个问题,只需要把eh->video_st->frame->pts每次累加即可。

--------------------- 
作者:twoconk 
来源:CSDN 
原文:https://blog.csdn.net/twoconk/article/details/54575134 
版权声明:本文为博主原创文章,转载请附上博文链接!

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值