srs的webrtc功能由三个组件共同完成:srs流媒体服务(默认端口1985)、3rdparty/signaling下的信令服务(默认端口1989)以及3rdparty/httpx-static的http/https代理服务组成。httpx-static监听http(80)及https(443)端口,并将/sig请求转发给信令服务,将/rtc请求转发给srs流媒体服务。
信令连接类型为websocket,连接请求的url类似下面的样子
ws://xx.com/sig/v1/rtc?room=live&display=234e488
room为房间号,display为用户名。默认情况下,这个请求首先到达httpx-static服务,再由httpx-static服务转请求转发到signaling服务进行处理。
{"tid":"81515ac","msg":{"action":"join","room":"10098","display":"85e63c6"}}
- {
- "tid": "81515ac",
- "msg": {
- "action": "join",
- "room": "live",
- "self": {
- "display": "6e1e3d8",
- "publishing": false
- },
- "participants": [{
- "display": "6e1e3d8",
- "publishing": false
- }]
- }
- }
当房间有其他用户时,participants字段里将有其他用户的信息,其返回如下:
- {
- "tid": "81515ac",
- "msg": {
- "action": "join",
- "room": "live",
- "self": {
- "display": "12789",
- "publishing": false
- },
- "participants": [{
- "display": "6e1e3d8",
- "publishing": true
- }, {
- "display": "12789",
- "publishing": false
- }]
- }
- }
{"tid":"2f65662","msg":{"action":"publish","room":"live","display":"337ecd3"}}
{"tid":"2f65662","msg":null}
当房间有其他人join的时候,会收到如下notify:
- {
- "msg": {
- "action": "notify",
- "event": "join",
- "room": "live",
- "self": {
- "display": "337ecd3",
- "publishing": true
- },
- "peer": {
- "display": "6f22ee1",
- "publishing": false
- },
- "participants": [{
- "display": "337ecd3",
- "publishing": true
- }, {
- "display": "6f22ee1",
- "publishing": false
- }]
- }
- }
当房间有其他人publish流的时候,会收到如下notify:
- {
- "msg": {
- "action": "notify",
- "event": "publish",
- "room": "live",
- "self": {
- "display": "337ecd3",
- "publishing": true
- },
- "peer": {
- "display": "6f22ee1",
- "publishing": true
- },
- "participants": [{
- "display": "337ecd3",
- "publishing": true
- }, {
- "display": "6f22ee1",
- "publishing": true
- }]
- }
- }
推流信令由srs流媒体服务进行处理,默认端口为1985。
通过http post发送
- method: POST
- url: /rtc/v1/publish/
- ContentType: application/json
一个可能的content如下:
- {
- "api": "http://xxx.com:1985/rtc/v1/publish/",
- "tid": "905b653",
- "streamurl": "webrtc://xxxx.com/live/337ecd3null",
- "clientip": null,
- "sdp": "v=0\r\no=- 3779124144089994960 ...\r\n"
- }
上面sdp就是webrtc的sdp,这些进行了省略。
srs.sdk.js中提供的示例代码片段如下:
- var data = {
- api: conf.apiUrl, tid: conf.tid, streamurl: conf.streamUrl,
- clientip: null, sdp: offer.sdp
- };
- console.log("Generated offer: ", data);
-
- $.ajax({
- type: "POST", url: conf.apiUrl, data: JSON.stringify(data),
- contentType: 'application/json', dataType: 'json'
- })
下面是返回示例:
- {
- "code": 0,
- "server": "vid-a8mj4jz",
- "sdp": "v=0\r\no=SRS/4.0.265(Leo) 94612519010016 2 IN IP4 0.0.0.0\r\ns=SRSPublishSession\r\n...",
- "sessionid": "t95zu036:l4Ai",
- "simulator": "http://xxxx.com:1985/rtc/v1/nack/"
- }
方法是OPTIONS???
- method: POST
- url: /rtc/v1/play/
- ContentType: application/json
content:
- {
- "api": "http://xxxx.com:1985/rtc/v1/play/",
- "tid": "4b84522",
- "streamurl": "webrtc://xxxx.com/live/6f22ee1null",
- "clientip": null,
- "sdp": "v=0\r\no=- 2143530685089536534 2 IN IP4 127.0.0.1\r\ns=-\r\nt=0 0\r\na=group:BUNDLE 0 1...\r\n"
- }
返回示例如下:
- {
- "code": 0,
- "server": "vid-a8mj4jz",
- "sdp": "v=0\r\no=SRS/4.0.265(Leo) 94612519489568 2 IN IP4 0.0.0.0\r\ns=SRSPlaySession\r\nt=0 0\r\na=ice-lite...\r\n",
- "sessionid": "637a69d0:6L8Z",
- "simulator": "http://xxxx.com:1985/rtc/v1/nack/"
- }