SRS4.0 RTC模块增加Gop cache

简介

随着对直播延迟要求的不断提高,原来的RTMP直播已经不能满足要求,需要使用基于RTC的低延迟直播。SRS中提供了RTC推拉流的能力,并且提供了RTMP和RTC互相转换的能力。为什么需要在SRS的RTC模块中增加Gop缓存,目的是为了降低起播等待时长。

对于超大规模的低延迟直播来说,不能每加入一个用户向服务器发送PLI/SLI/FIR请求,服务器就编码一个I帧或向推流端请求I帧。

对于从RTMP转RTC,由于RTMP存在gop,没有gop cache时最长需要等待一个gop才能起播。

我在原始的RTMP直播服务上,使用SRS做了一层RTC代理,实现和腾讯云LEB或阿里云RTS的功能,通过在SRS RTC模块中增加Gop Cache使其可以具备像RTMP直播一样的秒开能力。这样可以复用原有的直播转码、直播录制等服务。

 本文福利, C++音视频学习资料包、技术视频,内容包括(音视频开发,面试题,FFmpeg webRTC rtmp hls rtsp ffplay srs↓↓↓↓↓↓见下面↓↓文章底部点击领取↓↓ 

实现方式

配置解析

在SrsConfig类中增加对RTC Gop Cache相应对解析。对应对文件是trunk/src/app/srs_app_config.h/trunk/src/app/srs_app_config.cpp.增加两个参数:

rtc_gop_cache:RTC gop的开关选项,默认是关闭。

rtc_gop_cache_max_packet_number:RTC Gop最多缓存的packet数量,是为了避免推流端两个I帧间隔时间过长时,增加对服务器内存资源对消耗,默认是0个。

bool SrsConfig::get_rtc_gop_cache_enabled(std::string vhost)
{
    static bool DEFAULT = false;

    SrsConfDirective* conf = get_rtc(vhost);

    if (!conf) {
        return DEFAULT;
    }

    conf = conf->get("rtc_gop_cache");
    if (!conf || conf->arg0().empty()) {
        return DEFAULT;
    }

    return SRS_CONF_PERFER_FALSE(conf->arg0());
}
int SrsConfig::get_rtc_gop_cache_max_packets(std::string vhost)
{
    static int DEFAULT = 0;

    SrsConfDirective* conf = get_rtc(vhost);

    if (!conf) {
        return DEFAULT;
    }

    conf = conf->get("rtc_gop_cache_max_packet_number");
    if (!conf || conf->arg0().empty()) {
        return DEFAULT;
    }

    return ::atoi(conf->arg0().c_str());
}

增加SrsRtcGopCache类 


// RTC cache a gop of video/audio data,
// delivery at the connect of rtc player,
// To enable it to fast startup.
class SrsRtcGopCache
{
private:
    // if disabled the gop cache,
    // The client will wait for the next keyframe for h264,
    // and will be black-screen.
    bool enable_rtc_gop_cache;
    //when meet keyframe, then start cache packet
    bool meet_keyframe;
    // The cached rtp packet count
    int cached_rtp_packet_count;
    // avoid cache too much packet when gop size too large.
    int max_cached_count;
    // cached gop.
    std::vector<SrsRtpPacket*> rtc_gop_cache_vec;
public:
    SrsRtcGopCache();
    virtual ~SrsRtcGopCache();
public:
    // cleanup when system quit.
    virtual void dispose();
    // To enable or disable the gop cache.
    virtual void set(bool v, int max_size);
    // only for h264 codec
    // 1. cache the gop when got h264 keyframe.
    // @param pkt, directly ptr, copy it if need to save it.
    virtual srs_error_t cache(SrsRtpPacket* pkt);
    // clear the rtc gop cache.
    virtual void clear();
    // dump the rtc cached gop to consumer.
    virtual srs_error_t dump(SrsRtcConsumer* consumer);
};

  1. 函数set:设置gop cache开启或关闭。
  2. 函数cache:缓存需要packet数据。
  3. 函数dump:将缓存的rtp packet放入发送队列给播放端。
  4. 函数clear:清理缓存数据。
  5. 函数dispose:系统退出时调用。

缓存数据

扫描二维码关注公众号,回复: 14431234 查看本文章
rtc_gop_cache_->cache(pkt->copy());

SrsRtcSource::on_rtp在接收到推流端发送的rtp数据时,加入到rtc_gop_cache中。

总结

通过在SRS的rtc.conf中进行以下配置,可以开启RTC Gop Cache功能。使用RTMP推流,并且使用RTC播放时可以实现视频秒开。

rtc {
	    enabled                         on;
        rtc_gop_cache                   on;
        rtc_gop_cache_max_packet_number 2048;
    }

 本文福利, C++音视频学习资料包、技术视频,内容包括(音视频开发,面试题,FFmpeg webRTC rtmp hls rtsp ffplay srs↓↓↓↓↓↓见下面↓↓文章底部点击领取↓↓

猜你喜欢

转载自blog.csdn.net/m0_60259116/article/details/126126682