srs开源流媒体服务器中gop_cash是如何实现的?

要解决几个问题:

    (1)srs服务器gop_cache如何打开和关闭?

    (2)gop_cache缓存时间如何设置?

    (3)服务器如何知道某个msg是gop_cache的关键帧,并且清理前面的帧。

    (4)audio和video的gop_cache如何对应起来(同步)?

    (5)客户端连入的时候如何把gop_cache发给客户端。

    (6)gop_cache有何用?

(1)服务器gop_cache如何打开和关闭?

    其实很简单,如下conf配置上加 gop_cache on/off; 即可

# the listen ports, split by space.
listen              1935;
vhost __defaultVhost__ {
    # whether cache the last gop.
    # if on, cache the last gop and dispatch to client,
    #   to enable fast startup for client, client play immediately.
    # if off, send the latest media data to client,
    #   client need to wait for the next Iframe to decode and show the video.
    # set to off if requires min delay;
    # set to on if requires client fast startup.
    # default: on
    gop_cache       on;
}

     参考这里:

     https://github.com/ossrs/srs/wiki/v2_CN_LowLatency#gop-cache

(2)gop_cache缓存时间如何设置?

     gop_cache的时间间隔是推流的客户端决定的,跟服务器没关系。

     比如:

     A.基于ffmpeg实现的客户端设置可以参考如下代码,如下代码把gop_size设置为帧率大小,换算成时间就是1S。很多人设置为50,如果帧率是24fps,那么就是2s。</

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
首先,需要在pom.xml文件添加以下依赖: ``` <dependency> <groupId>org.bytedeco</groupId> <artifactId>javacv</artifactId> <version>1.4.4</version> </dependency> ``` 接下来,可以使用以下代码将视频推送到SRS服务器: ```java import org.bytedeco.ffmpeg.global.avcodec; import org.bytedeco.javacv.FFmpegFrameGrabber; import org.bytedeco.javacv.FFmpegFrameRecorder; import java.io.IOException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class PushStreamUtil { private static final String PUSH_STREAM_URL = "rtmp://localhost/live/test"; // SRS服务器地址 private static final int FRAME_RATE = 25; // 帧率 private static final int GOP_LENGTH_IN_FRAMES = 60; // GOP长度 private static final int AUDIO_CHANNELS = 2; private static final int AUDIO_SAMPLE_RATE = 44100; private static final ExecutorService executorService = Executors.newSingleThreadExecutor(); private static FFmpegFrameGrabber grabber; private static FFmpegFrameRecorder recorder; public static void startPushStream() { executorService.execute(() -> { try { grabber = new FFmpegFrameGrabber("video.mp4"); // 视频源文件 grabber.start(); recorder = new FFmpegFrameRecorder(PUSH_STREAM_URL, grabber.getImageWidth(), grabber.getImageHeight(), AUDIO_CHANNELS); recorder.setInterleaved(true); recorder.setVideoOption("crf", "28"); recorder.setVideoCodec(avcodec.AV_CODEC_ID_H264); recorder.setFormat("flv"); recorder.setFrameRate(FRAME_RATE); recorder.setGopSize(GOP_LENGTH_IN_FRAMES); recorder.setAudioOption("crf", "0"); recorder.setAudioQuality(0); recorder.setAudioCodec(avcodec.AV_CODEC_ID_AAC); recorder.setAudioChannels(AUDIO_CHANNELS); recorder.setSampleRate(AUDIO_SAMPLE_RATE); recorder.start(); while (true) { recorder.record(grabber.grab()); } } catch (IOException | InterruptedException e) { e.printStackTrace(); } finally { stopPushStream(); } }); } public static void stopPushStream() { try { if (recorder != null) { recorder.stop(); recorder.release(); } if (grabber != null) { grabber.stop(); grabber.release(); } } catch (Exception e) { e.printStackTrace(); } } } ``` 其,`FFmpegFrameGrabber`用于从视频源文件读取视频帧,`FFmpegFrameRecorder`用于将视频推送到SRS服务器。在`startPushStream`方法启动推任务,在`stopPushStream`方法停止推任务。 需要注意的是,在使用`FFmpegFrameGrabber`和`FFmpegFrameRecorder`时,需要保证本地机器上已经安装了FFmpeg组件。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值