• 欢迎光临~

【SRS】http回调配置

开发技术 开发技术 2022-12-29 次浏览

SRS作为一个高效的实时视频服务器,行业内很出名。

目前我这里通过推流rtmp到SRS,然后在Web端请求视频进行播放。

官网介绍地址:Introduction | SRS (ossrs.net)

添加回调配置

官方回调接口文档:HTTP回调 | SRS (ossrs.net)

这里以docker.conf为例,需要在vhost配置中添加http_hooks项。

我这里只使用了on_playon_stop两个回调接口。(完整的配置项,可以查看官方文档,或者full.conf文件)

vhost __defaultVhost__ {
    http_hooks {
        # 开启http回调
        enabled         on;
        on_play         http://192.168.0.159:9982/api/v1/sessions;
        on_stop         http://192.168.0.159:9982/api/v1/sessions;
    }
}

同一个接口:可以设置多个回调地址,以空格 分割,以逗号;结尾,比如:

on_play  http://192.168.0.159:9982/api/v1/sessions http://192.168.0.159:9983/api/v1/sessions;

on_play

官方文档描述:

    # when client start to play vhost/app/stream, call the hook,
    # the request in the POST data string is a object encode by json:
    #       {
    #           "action": "on_play",
    #           "client_id": 1985,
    #           "ip": "192.168.1.10", "vhost": "video.test.com", "app": "live",
    #           "stream": "livestream", "param":"?token=xxx&salt=yyy",
    #           "pageUrl": "http://www.test.com/live.html"
    #       }
    # if valid, the hook must return HTTP code 200(Status OK) and response
    # an int value specifies the error code(0 corresponding to success):
    #       0
    # support multiple api hooks, format:
    #       on_play http://xxx/api0 http://xxx/api1 http://xxx/apiN
    on_play		http://127.0.0.1:8085/api/v1/sessions http://localhost:8085/api/v1/sessions;

当客户端开始播放视频时,会触发此回调接口。如果允许播放,则需要返回HTTP数据,内容为0。

on_stop

官方文档描述:

	# when client stop to play vhost/app/stream, call the hook,
    # the request in the POST data string is a object encode by json:
    #       {
    #           "action": "on_stop",
    #           "client_id": 1985,
    #           "ip": "192.168.1.10", "vhost": "video.test.com", "app": "live",
    #           "stream": "livestream", "param":"?token=xxx&salt=yyy"
    #       }
    # if valid, the hook must return HTTP code 200(Status OK) and response
    # an int value specifies the error code(0 corresponding to success):
    #       0
    # support multiple api hooks, format:
    #       on_stop http://xxx/api0 http://xxx/api1 http://xxx/apiN
    on_stop		http://127.0.0.1:8085/api/v1/sessions http://localhost:8085/api/v1/sessions;

当客户端停止播放视频(比如关闭浏览器,或者推流异常)时,会触发此回调接口。当我们处理完后,应该返回HTTP数据,内容为0。

Docker启动

注:最新版SRS4已经发布了,我这里使用的是SRS3版本。通过-v参数,将conf目录映射到本地目录。

注:SRS在Docker中的默认目录是/usr/local/srs

sudo docker run --rm -it -p 1935:1935 -p 1985:1985 -p 8080:8080 -v /home/sjt/work/srs3/conf:/usr/local/srs/conf	
    registry.cn-hangzhou.aliyuncs.com/ossrs/srs:3 ./objs/srs -c conf/docker.conf

关于回调数据中client_id参数的注意点

使用SRS4时,发现回调返回的json数据中,client_id字段的类型是string,而在SRS3中,类型是number

需要判断应用程序中对client_id的处理,是否会因为类型与期望不一致,而导致处理异常(我的程序类型不一样就会报错)。

程序员灯塔
转载请注明原文链接:【SRS】http回调配置
喜欢 (0)