• srs webrtc服务p2p案例信令分析


    srs版本为v4.0-r3

    概述

    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服务进行处理。

    加入房间请求(join)

    {"tid":"81515ac","msg":{"action":"join","room":"10098","display":"85e63c6"}}

    加入房间返回

    1. {
    2. "tid": "81515ac",
    3. "msg": {
    4. "action": "join",
    5. "room": "live",
    6. "self": {
    7. "display": "6e1e3d8",
    8. "publishing": false
    9. },
    10. "participants": [{
    11. "display": "6e1e3d8",
    12. "publishing": false
    13. }]
    14. }
    15. }

    当房间有其他用户时,participants字段里将有其他用户的信息,其返回如下:

    1. {
    2. "tid": "81515ac",
    3. "msg": {
    4. "action": "join",
    5. "room": "live",
    6. "self": {
    7. "display": "12789",
    8. "publishing": false
    9. },
    10. "participants": [{
    11. "display": "6e1e3d8",
    12. "publishing": true
    13. }, {
    14. "display": "12789",
    15. "publishing": false
    16. }]
    17. }
    18. }

    发布流请求

    {"tid":"2f65662","msg":{"action":"publish","room":"live","display":"337ecd3"}}

    发布流返回

    {"tid":"2f65662","msg":null}

    Notify通知

    当房间有其他人join的时候,会收到如下notify:

    1. {
    2. "msg": {
    3. "action": "notify",
    4. "event": "join",
    5. "room": "live",
    6. "self": {
    7. "display": "337ecd3",
    8. "publishing": true
    9. },
    10. "peer": {
    11. "display": "6f22ee1",
    12. "publishing": false
    13. },
    14. "participants": [{
    15. "display": "337ecd3",
    16. "publishing": true
    17. }, {
    18. "display": "6f22ee1",
    19. "publishing": false
    20. }]
    21. }
    22. }

    当房间有其他人publish流的时候,会收到如下notify:

    1. {
    2. "msg": {
    3. "action": "notify",
    4. "event": "publish",
    5. "room": "live",
    6. "self": {
    7. "display": "337ecd3",
    8. "publishing": true
    9. },
    10. "peer": {
    11. "display": "6f22ee1",
    12. "publishing": true
    13. },
    14. "participants": [{
    15. "display": "337ecd3",
    16. "publishing": true
    17. }, {
    18. "display": "6f22ee1",
    19. "publishing": true
    20. }]
    21. }
    22. }

    推拉流信令部分

    推流信令由srs流媒体服务进行处理,默认端口为1985。

    推流信令

    通过http post发送

    1. method: POST
    2. url: /rtc/v1/publish/
    3. ContentType: application/json

    一个可能的content如下:

    1. {
    2. "api": "http://xxx.com:1985/rtc/v1/publish/",
    3. "tid": "905b653",
    4. "streamurl": "webrtc://xxxx.com/live/337ecd3null",
    5. "clientip": null,
    6. "sdp": "v=0\r\no=- 3779124144089994960 ...\r\n"
    7. }

    上面sdp就是webrtc的sdp,这些进行了省略。

    srs.sdk.js中提供的示例代码片段如下:

    1. var data = {
    2. api: conf.apiUrl, tid: conf.tid, streamurl: conf.streamUrl,
    3. clientip: null, sdp: offer.sdp
    4. };
    5. console.log("Generated offer: ", data);
    6. $.ajax({
    7. type: "POST", url: conf.apiUrl, data: JSON.stringify(data),
    8. contentType: 'application/json', dataType: 'json'
    9. })

    下面是返回示例:

    1. {
    2. "code": 0,
    3. "server": "vid-a8mj4jz",
    4. "sdp": "v=0\r\no=SRS/4.0.265(Leo) 94612519010016 2 IN IP4 0.0.0.0\r\ns=SRSPublishSession\r\n...",
    5. "sessionid": "t95zu036:l4Ai",
    6. "simulator": "http://xxxx.com:1985/rtc/v1/nack/"
    7. }

    拉流信令

    方法是OPTIONS???

    1. method: POST
    2. url: /rtc/v1/play/
    3. ContentType: application/json

    content:

    1. {
    2. "api": "http://xxxx.com:1985/rtc/v1/play/",
    3. "tid": "4b84522",
    4. "streamurl": "webrtc://xxxx.com/live/6f22ee1null",
    5. "clientip": null,
    6. "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"
    7. }

    返回示例如下:

    1. {
    2. "code": 0,
    3. "server": "vid-a8mj4jz",
    4. "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",
    5. "sessionid": "637a69d0:6L8Z",
    6. "simulator": "http://xxxx.com:1985/rtc/v1/nack/"
    7. }

  • 相关阅读:
    [11]重绘与回流
    .NET关于 跳过SSL中遇到的问题
    【编解码格式】Sorenson系列
    QT国际化
    详解性能测试(2023最新版)
    5G移动通信网的定位技术发展趋势
    企业使用有线和5G主备双链路上网配置案例
    一种可信万兆加密分流认证装置研究
    Keepalived源码安装-高可用(主备)场景
    螺丝扭断力试验机SJ-12
  • 原文地址:https://blog.csdn.net/u010155023/article/details/127819758