一. 简介

 SRS是一个简单高效的实时视频服务器,支持RTMP/WebRTC/HLS/HTTP-FLV/SRT。

你可以用它实现视频推流,并且支持http回调事件(HTTPCallback),还可以保存视频流文件。支持本地化部署,操作简单。

 二. SRS-HTTP服务部署实例

 SRS内嵌了http服务器,支持分发hls流和文件。

以分发HLS为例,使用SRS分发RTMP和HLS流,不依赖于外部服务器。

假设服务器的IP是:192.168.1.152

第一步,获取SRS。

git clone https://github.com/ossrs/srs
cd srs/trunk

第二步,编译SRS。

./configure && make

 第三步,启动服务器。

./objs/srs -c conf/srs.conf

检查SRS是否成功启动,可以打开 http://localhost:8080/ ,或者执行命令:

# 查看SRS的状态
./etc/init.d/srs status

第三步,编写SRS配置文件。

贴出我的配置文件

# main config for srs.
# @see full.conf for detail config.

listen              1935;
max_connections     1000;
srs_log_tank        console;
srs_log_file        ./objs/srs.log;
daemon              off;
stats {
    network	0;
}
http_api {
    enabled         on;
    listen          1985;
    crossdomain	    on;
	
    raw_api {
        enabled		    on;
        allow_reload	on;
        allow_query	    on;
        allow_update	on;
    }
}
http_server {
    enabled         on;
    listen          8080;
    dir             ./objs/nginx/html;
}

vhost 192.168.1.152 {
    # http回调
    http_hooks{
	    enabled        on;
        on_connect     http://xxx.com/api/live/srs_callback;
    	on_close       http://xxx.com/api/live/srs_callback;
    	on_publish     http://xxx.com/api/live/srs_callback;
    	on_unpublish   http://xxx.com/api/live/srs_callback;
    	on_play        http://xxx.com/api/live/srs_callback;
    	on_stop        http://xxx.com/api/live/srs_callback;
    	on_dvr         http://xxx.com/api/live/srs_callback;

    }
    hls {
        enabled         on;
	    hls_path        ./objs/nginx/html;
        hls_fragment    3;
	    hls_cleanup	    on;
	    hls_dispose	    3;
        hls_window    	12;
    }
    dvr {
        enabled             on;
        dvr_path            ./objs/nginx/html/[app]/[stream].[timestamp].mp4;
        dvr_plan            session;
        dvr_duration        30;
        dvr_wait_keyframe   on;
        time_jitter         full;
    }
    http_remux {
        enabled     on;
        mount       [vhost]/[app]/[stream].flv;
    }
}

三.HttpCallback回调

SRS总是开启HttpCallback

SRS的回调事件包括:

事件数据说明
on_connect{
"action": "on_connect",
"client_id": 1985,
"ip": "192.168.1.10",
"vhost": "video.test.com",
"app": "live",
"tcUrl": "rtmp://x/x?key=xxx",
"pageUrl": "http://x/x.html"
}
当客户端连接到指定的vhost时
on_close{
"action": "on_close",
"client_id": 1985,
"ip": "192.168.1.10",
"vhost": "video.test.com",
"app": "live",
"send_bytes": 10240,
"recv_bytes": 10240
}
当客户端关闭连接,或者SRS主动关闭连接时
on_publish{
"action": "on_publish",
"client_id": 1985,
"ip": "192.168.1.10",
"vhost": "video.test.com",
"app": "live",
"stream": "livestream"
}
当客户端发布流时,譬如flash/FMLE方式推流到服务器
on_unpublish{
"action": "on_unpublish",
"client_id": 1985,
"ip": "192.168.1.10",
"vhost": "video.test.com",
"app": "live",
"stream": "livestream"
}
当客户端停止发布流时
on_play{
"action": "on_play",
"client_id": 1985,
"ip": "192.168.1.10",
"vhost": "video.test.com",
"app": "live",
"stream": "livestream",
"pageUrl": "http://a.com/i.html",
"param":"?k=v"
}
当客户端开始播放流时
on_stop{
"action": "on_stop",
"client_id": 1985,
"ip": "192.168.1.10",
"vhost": "video.test.com",
"app": "live",
"stream": "livestream"
}
当客户端停止播放时。备注:停止播放可能不会关闭连接,还能再继续播放。
on_dvr{
"action": "on_dvr",
"client_id": 1985,
"ip": "192.168.1.10",
"vhost": "video.test.com",
"app": "live",
"stream": "livestream",
"cwd": "/opt",
"file": "./l.xxx.flv"
}
当DVR录制关闭一个flv文件时

其中,

  • 事件:发生该事件时,即回调指定的HTTP地址。
  • HTTP地址:可以支持多个,以空格分隔,SRS会依次回调这些接口。
  • 数据:SRS将数据POST到HTTP接口。
  • 返回值:SRS要求HTTP服务器返回HTTP200并且response内容为整数错误码(0表示成功),其他错误码会断开客户端连接。
//返回示例
return json(["code" => 0, "msg" => "success", "state" => "OK"]);

当http接口获取到ssr回调数据时,就可以在PHP或者JAVA端展开你的工作了。

以下示例代码对应SRS配置的http回调接口地址(api/live/srs_callback)

//thinkphp代码示例

$param = $this->request->param();

$action = $param["action"];

if ($action == "on_connect") {

} elseif ($action == "on_publish") {
           
} elseif ($action == "on_dvr") {
          
} elseif ($action == "on_unpublish") {
           
} else {
           
}

Logo

致力于链接即构和开发者,提供实时互动和元宇宙领域的前沿洞察、技术分享和丰富的开发者活动,共建实时互动世界。

更多推荐