SRS关闭无人观看的流

这里需要使用到SRS自身自带的钩子回调功能,配置文件中有标注:

Hook函数:

分为on_publish、on_play、on_stop、on_unpublish、on_dvr等类别;

其中主要介绍on_play、on_stop

on_play:

主要用于用户在对srs拉流进行播放的时候触发;

on_stop:

主要用于用户在停止播放的时候触发;

一、如何获取SRS发出的回调信息

要做到关闭无人观看的SRS流的话,需要一个http_server端来接收SRS所发出的http请求,并及时回复code:200, 否则SRS无法进行下一步的操作(放流或断流), 回复了SRS以后我们对信息进行解析即可获取到是用户断开了播放还是开始播放。

消息体如下:使用on_play举例

Body:
{
  "server_id": "vid-0xk989d",
  "action": "on_play",
  "client_id": "341w361a",
  "ip": "127.0.0.1",
  "vhost": "__defaultVhost__",
  "app": "live",
  "tcUrl": "rtmp://127.0.0.1:1935/live?vhost=__defaultVhost__",
  "stream": "livestream",
  "param": "",
  "stream_url": "video.test.com/live/livestream",
  "stream_id": "vid-124q9y3"
}

可以从上述json中的action字段看到是on_play,此时可以知道用户在拉流进行播放操作,如果用户断开播放或停止播放后,SRS会向http_server发出on_stop的信息告知用户已经停止播放。 

 二、如何对SRS进行配置,以达到SRS向指定的IP和端口发送http信息

vhost your_vhost {
    http_hooks {
        enabled         on;
        on_publish      http://127.0.0.1:8085/api/v1/on_publish;
        on_unpublish    http://127.0.0.1:8085/api/v1/on_unpublish;
        on_play         http://127.0.0.1:8085/api/v1/on_play;
        on_stop         http://127.0.0.1:8085/api/v1/on_stop;
        on_dvr          http://127.0.0.1:8085/api/v1/dvrs;
        on_hls          http://127.0.0.1:8085/api/v1/hls;
        on_hls_notify   http://127.0.0.1:8085/api/v1/hls/[app]/[stream]/[ts_url][param];
    }
}

 在SRS的配置文件中可以这样进行配置,IP和端口根据自己需求进行替换即可,如果需要https,那么直接将http替换为https即可。不需要的配置选项可以通过#符号注释掉

三、在http_server端关闭无人观看的音视频流

对SRS返回的http信息进行解析后,我们可以设置每路流的基础观看人数为0,若收到on_play信息则计数+1,收到on_stop那么计数-1即可。根据自己需求进行控制. 

四、注意事项

其中需要注意的有:

1、hls流要进行统计的话,需要打开一个配置文件参数叫hls_ctx的配置;

2、hls流在通过nginx代理过后,SRS则无法进行统计,因为hls比较特殊,是通过不断请求ts切片进行播放,所以会建立很多连接并断开,SRS内部为hls做了虚拟连接,但是这个虚拟连接会导致nginx的代理无效。以下是SRS的配置原话

        # Whether enable hls_ctx for HLS streaming, for which we create a "fake" connection for HTTP API and callback.
        # For each HLS streaming session, we use a child m3u8 with a session identified by query "hls_ctx", it simply
        # work as the session id.
        # Once the HLS streaming session is created, we will cleanup it when timeout in 2*hls_window seconds. So it
        # takes a long time period to identify the timeout.
        # Now we got a HLS stremaing session, just like RTMP/WebRTC/HTTP-FLV streaming, we're able to stat the session
        # as a "fake" connection, do HTTP callback when start playing the HLS streaming. You're able to do querying and
        # authentication.
        # Note that it will make NGINX edge cache always missed, so never enable HLS streaming if use NGINX edges.
        # Overwrite by env SRS_VHOST_HLS_HLS_CTX for all vhosts.
        # Default: on
        hls_ctx on;

  • 4
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

美丽的欣情

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值