本章将带你了解一个真正的多方通信是如何搭建起来的。

13-1 Mediasoup的运行环境

图片.png
图片.png
图片.png
Mac安装
图片.png
https://www.yuque.com/caokunchao/pelvyg/kv389s

13-2 Mediasoup Demo的布署

1)搭建环境

  1. git clone https://github.com/versatica/mediasoup-demo.git
  2. cd mediasoup-demo/
  3. ubuntu@VM-8-4-ubuntu:~/lichao_audio_video/server_webrtc/mediasoup-demo$ git branch -a
  4. * v3
  5. remotes/origin/HEAD -> origin/v3
  6. remotes/origin/pipe
  7. remotes/origin/test-directtransport-rtp
  8. remotes/origin/v1
  9. remotes/origin/v2
  10. remotes/origin/v3
  11. ubuntu@VM-8-4-ubuntu:~/lichao_audio_video/server_webrtc/mediasoup-demo$ git checkout v3
  12. Already on 'v3'
  13. Your branch is up to date with 'origin/v3'.
  14. ubuntu@VM-8-4-ubuntu:~/lichao_audio_video/server_webrtc/mediasoup-demo$ ls
  15. aiortc app broadcasters CHANGELOG.md README.md server

app 客户端代码
broadcasters 用户推流的一个模块,如通过FFmpeg进行一个广播推流到Mediasoup服务器。
server 服务端代码,信令服务+媒体服务

2)配置服务端

  1. cd server
  2. npm install
  3. cp config.example.js config.js

npm install
1.
77 verbose Linux 5.11.0-40-generic
78 verbose argv “/home/charles/Softwares/node-v16.3.0-linux-x64/bin/node” “/home/charles/Softwares/node-v16.3.0-linux-x64/bin/npm” “install”
79 verbose node v16.3.0
80 verbose npm v7.15.1
81 error code EINVALIDTAGNAME
82 error Invalid tag name “>=^16.0.0”: Tags may not have any characters that encodeURIComponent encodes.
83 verbose exit 1

resolve:

  1. npm install -g npm

2.
npm WARN deprecated urix@0.1.0: Please see https://github.com/lydell/urix#deprecated
npm WARN deprecated resolve-url@0.2.1: https://github.com/lydell/resolve-url#deprecated
npm WARN deprecated chokidar@2.1.8: Chokidar 2 will break on node v14+. Upgrade to chokidar 3 with 15x less dependencies.
npm ERR! code 1
npm ERR! path /home/charles/AudioVideoDev/mediasoup-learn/mediasoup-demo/server/node_modules/mediasoup
npm ERR! command failed
npm ERR! command sh -c node npm-scripts.js postinstall
npm ERR! npm-scripts.js [INFO] running task “postinstall”
npm ERR! npm-scripts.js [INFO] executing command: node npm-scripts.js worker:build
npm ERR! npm-scripts.js [INFO] running task “worker:build”
npm ERR! npm-scripts.js [INFO] executing command: make -C worker
npm ERR! make: Entering directory ‘/home/charles/AudioVideoDev/mediasoup-learn/mediasoup-demo/server/node_modules/mediasoup/worker’
npm ERR! # Updated pip and setuptools are needed for meson
npm ERR! # --system is not present everywhere and is only needed as workaround for
npm ERR! # Debian-specific issue (copied from
npm ERR! # https://github.com/gluster/gstatus/pull/33),,) fallback to command without
npm ERR! # --system if the first one fails.
npm ERR! python3 -m pip install —system —target=/home/charles/AudioVideoDev/mediasoup-learn/mediasoup-demo/server/node_modules/mediasoup/worker/out/pip pip setuptools || \
npm ERR! python3 -m pip install —target=/home/charles/AudioVideoDev/mediasoup-learn/mediasoup-demo/server/node_modules/mediasoup/worker/out/pip pip setuptools || \
npm ERR! echo “Installation failed, likely because PIP is unavailable, if you are on Debian/Ubuntu or derivative please install the python3-pip package”
npm ERR! Installation failed, likely because PIP is unavailable, if you are on Debian/Ubuntu or derivative please install the python3-pip package
npm ERR! # Install meson and ninja using pip into custom location, so we don’t
npm ERR! # depend on system-wide installation.
npm ERR! python3 -m pip install —upgrade —target=/home/charles/AudioVideoDev/mediasoup-learn/mediasoup-demo/server/node_modules/mediasoup/worker/out/pip meson ninja
npm ERR! make: Leaving directory ‘/home/charles/AudioVideoDev/mediasoup-learn/mediasoup-demo/server/node_modules/mediasoup/worker’
npm ERR! /usr/bin/python3: No module named pip
npm ERR! /usr/bin/python3: No module named pip
npm ERR! /usr/bin/python3: No module named pip
npm ERR! make: * [Makefile:56: meson-ninja] Error 1

npm ERR! A complete log of this run can be found in:
npm ERR! /home/charles/.npm/_logs/2021-11-28T13_52_29_376Z-debug.log

reslove:

  1. sudo apt install python3-pip

4、
charles@charles-VirtualBox:~/codes/mediasoup/mediasoup-demo/server$ npm install
npm WARN deprecated source-map-url@0.4.1: See https://github.com/lydell/source-map-url#deprecated
npm WARN deprecated source-map-resolve@0.5.3: See https://github.com/lydell/source-map-resolve#deprecated
npm WARN deprecated chokidar@2.1.8: Chokidar 2 does not receive security updates since 2019. Upgrade to chokidar 3 with 15x fewer dependencies
npm WARN deprecated urix@0.1.0: Please see https://github.com/lydell/urix#deprecated
npm WARN deprecated resolve-url@0.2.1: https://github.com/lydell/resolve-url#deprecated
npm ERR! code ECONNREFUSED
npm ERR! syscall connect
npm ERR! errno ECONNREFUSED
npm ERR! FetchError: request to https://codeload.github.com/versatica/mediasoup/tar.gz/a9f366151926479fbbc87530ada9d57bff707821 failed, reason: connect ECONNREFUSED 127.0.0.1:443
npm ERR! at ClientRequest. (/home/charles/toolsoftwares/node-v16.15.0-linux-x64/lib/node_modules/npm/node_modules/minipass-fetch/lib/index.js:130:14)
npm ERR! at ClientRequest.emit (node:events:527:28)
npm ERR! at TLSSocket.socketErrorListener (node:_http_client:454:9)
npm ERR! at TLSSocket.emit (node:events:539:35)
npm ERR! at emitErrorNT (node:internal/streams/destroy:157:8)
npm ERR! at emitErrorCloseNT (node:internal/streams/destroy:122:3)
npm ERR! at processTicksAndRejections (node:internal/process/task_queues:83:21)
npm ERR! FetchError: request to https://codeload.github.com/versatica/mediasoup/tar.gz/a9f366151926479fbbc87530ada9d57bff707821 failed, reason: connect ECONNREFUSED 127.0.0.1:443
npm ERR! at ClientRequest. (/home/charles/toolsoftwares/node-v16.15.0-linux-x64/lib/node_modules/npm/node_modules/minipass-fetch/lib/index.js:130:14)
npm ERR! at ClientRequest.emit (node:events:527:28)
npm ERR! at TLSSocket.socketErrorListener (node:_http_client:454:9)
npm ERR! at TLSSocket.emit (node:events:539:35)
npm ERR! at emitErrorNT (node:internal/streams/destroy:157:8)
npm ERR! at emitErrorCloseNT (node:internal/streams/destroy:122:3)
npm ERR! at processTicksAndRejections (node:internal/process/task_queues:83:21) {
npm ERR! code: ‘ECONNREFUSED’,
npm ERR! errno: ‘ECONNREFUSED’,
npm ERR! syscall: ‘connect’,
npm ERR! address: ‘127.0.0.1’,
npm ERR! port: 443,
npm ERR! type: ‘system’
npm ERR! }
npm ERR!
npm ERR! If you are behind a proxy, please make sure that the
npm ERR! ‘proxy’ config is set properly. See: ‘npm help config’

npm ERR! A complete log of this run can be found in:
npm ERR! /home/charles/.npm/_logs/2022-05-27T15_58_14_858Z-debug-0.log
charles@charles-VirtualBox:~/codes/mediasoup/mediasoup-demo/server$
关掉科学上网看看。

修改config.js

  1. /**
  2. * IMPORTANT (PLEASE READ THIS):
  3. *
  4. * This is not the "configuration file" of mediasoup. This is the configuration
  5. * file of the mediasoup-demo app. mediasoup itself is a server-side library, it
  6. * does not read any "configuration file". Instead it exposes an API. This demo
  7. * application just reads settings from this file (once copied to config.js) and
  8. * calls the mediasoup API with those settings when appropriate.
  9. */
  10. const os = require('os');
  11. module.exports =
  12. {
  13. // Listening hostname (just for `gulp live` task).
  14. //修改地方1:如果是想在app目录运行gulp live,则外部使用公网地址打开显示,则需要修改为公网地址
  15. // 例如domain : '1.14.148.33',
  16. domain : 'localhost',
  17. // Signaling settings (protoo WebSocket server and HTTP API server).
  18. https :
  19. {
  20. listenIp : '0.0.0.0',
  21. // NOTE: Don't change listenPort (client app assumes 4443).
  22. listenPort : process.env.PROTOO_LISTEN_PORT || 4443,
  23. // NOTE: Set your own valid certificate files.
  24. tls :
  25. {
  26. //修改地方:2:需要生成一个证书秘钥,这个用命令自行生成,并放置相对应的目录
  27. //${__dirname} 为当前目录,可以放在当前目录下
  28. //openssl genrsa -out key.pem 2048
  29. //openssl req ‐new ‐x509 ‐key key.pem ‐out cert.pem ‐days 1095
  30. cert : process.env.HTTPS_CERT_FULLCHAIN || `${__dirname}/certs/cert.pem`,
  31. key : process.env.HTTPS_CERT_PRIVKEY || `${__dirname}/certs/key.pem`
  32. }
  33. },
  34. // mediasoup settings.
  35. mediasoup :
  36. {
  37. // Number of mediasoup workers to launch.
  38. numWorkers : Object.keys(os.cpus()).length,
  39. // mediasoup WorkerSettings.
  40. // See https://mediasoup.org/documentation/v3/mediasoup/api/#WorkerSettings
  41. workerSettings :
  42. {
  43. logLevel : 'warn',
  44. logTags :
  45. [
  46. 'info',
  47. 'ice',
  48. 'dtls',
  49. 'rtp',
  50. 'srtp',
  51. 'rtcp',
  52. 'rtx',
  53. 'bwe',
  54. 'score',
  55. 'simulcast',
  56. 'svc',
  57. 'sctp'
  58. ],
  59. rtcMinPort : process.env.MEDIASOUP_MIN_PORT || 40000,
  60. rtcMaxPort : process.env.MEDIASOUP_MAX_PORT || 49999
  61. },
  62. // mediasoup Router options.
  63. // See https://mediasoup.org/documentation/v3/mediasoup/api/#RouterOptions
  64. routerOptions :
  65. {
  66. mediaCodecs :
  67. [
  68. {
  69. kind : 'audio',
  70. mimeType : 'audio/opus',
  71. clockRate : 48000,
  72. channels : 2
  73. },
  74. {
  75. kind : 'video',
  76. mimeType : 'video/VP8',
  77. clockRate : 90000,
  78. parameters :
  79. {
  80. 'x-google-start-bitrate' : 1000
  81. }
  82. },
  83. {
  84. kind : 'video',
  85. mimeType : 'video/VP9',
  86. clockRate : 90000,
  87. parameters :
  88. {
  89. 'profile-id' : 2,
  90. 'x-google-start-bitrate' : 1000
  91. }
  92. },
  93. {
  94. kind : 'video',
  95. mimeType : 'video/h264',
  96. clockRate : 90000,
  97. parameters :
  98. {
  99. 'packetization-mode' : 1,
  100. 'profile-level-id' : '4d0032',
  101. 'level-asymmetry-allowed' : 1,
  102. 'x-google-start-bitrate' : 1000
  103. }
  104. },
  105. {
  106. kind : 'video',
  107. mimeType : 'video/h264',
  108. clockRate : 90000,
  109. parameters :
  110. {
  111. 'packetization-mode' : 1,
  112. 'profile-level-id' : '42e01f',
  113. 'level-asymmetry-allowed' : 1,
  114. 'x-google-start-bitrate' : 1000
  115. }
  116. }
  117. ]
  118. },
  119. // mediasoup WebRtcTransport options for WebRTC endpoints (mediasoup-client,
  120. // libmediasoupclient).
  121. // See https://mediasoup.org/documentation/v3/mediasoup/api/#WebRtcTransportOptions
  122. webRtcTransportOptions :
  123. {
  124. listenIps :
  125. [
  126. {
  127. //修改地方3
  128. ip : '0.0.0.0', //内网ip
  129. announcedIp : '1.14.148.xx' //修改为公网ip地址
  130. }
  131. ],
  132. initialAvailableOutgoingBitrate : 1000000,
  133. minimumAvailableOutgoingBitrate : 600000,
  134. maxSctpMessageSize : 262144,
  135. // Additional options that are not part of WebRtcTransportOptions.
  136. maxIncomingBitrate : 1500000
  137. },
  138. // mediasoup PlainTransport options for legacy RTP endpoints (FFmpeg,
  139. // GStreamer).
  140. // See https://mediasoup.org/documentation/v3/mediasoup/api/#PlainTransportOptions
  141. plainTransportOptions :
  142. {
  143. listenIp :
  144. {
  145. //修改地方4
  146. ip : '0.0.0.0', //内网ip
  147. announcedIp : '1.14.148.xx' //修改为公网ip地址
  148. },
  149. maxSctpMessageSize : 262144
  150. }
  151. }
  152. };

记得防火墙开放端口
40000-49999 TCP和UDP
还有 4443 TCP

3)测试

  1. # 安装cnpm
  2. npm install -g cnpm --registry=https://registry.npm.taobao.org
  3. cnpm -v
  4. # cd到server服务端目录下运行
  5. cnpm start
  6. # cd到app客户端目录下运行
  7. cnpm install -g gulp
  8. cnpm install
  9. gulp live

输入gulp live后图片.png此时,使用谷歌浏览器访问
https://1.14.148.67:3000/?info=true
注意,需要在云服务器那边开放3000 TCP端口。

新建位图图像.png
拷贝当前带着房间号的链接,然后新建标签页打开链接后,会在当前房间添加随机名称的用户。
新建位图图像.png

13-3 通过Nodejs实现HTTP服务

13-4 HTTPS基本知识

https://www.yuque.com/caokunchao/pelvyg/kv389s

13-5 通过WWW服务发布mediasoup客户端代码

https://www.gulpjs.com.cn/

  1. npm install gulp-cli -g
  2. npm install gulp -D

mediasoup-demo里面

  1. cd app
  2. gulp dist

图片.png
运行之后,就会在上一级的server目录中生成一个public目录。
图片.png
里面的是mediasoup-demo的网页端代码。

可以使用nodejs写一个服务端程序server.js来调用

  1. 'use strict'
  2. var http = require('http');
  3. var https = require('https');
  4. var fs = require('fs');
  5. // 将文件夹中文件列表显示到浏览器中。
  6. var serveIndex = require('serve-index');
  7. var express = require('express');
  8. var app = express();
  9. // 下面2行代码,顺序不能换
  10. // 浏览静态目录(浏览目录中所有文件)
  11. app.use(serveIndex('./public'));
  12. // 发布静态目录
  13. app.use(express.static('./public'));
  14. var options = {
  15. key : fs.readFileSync('./cert/key.pem'),
  16. cert : fs.readFileSync('./cert/cert.pem')
  17. }
  18. var https_server = https.createServer(options, app);
  19. https_server.listen(443, '0.0.0.0');
  20. var http_server = http.createServer(app);
  21. http_server.listen(80, '0.0.0.0');

将客户端代码所在的整个public目录,放在server.js的当前目录下。
然后后面执行sudo node server.js。
图片.png
运行index.html,则会出现客户端界面。
图片.pngwebserver.zip

13-6 作业-客户端是如何与信令服务建立连接的

通过wss协议,在mediasoup-demo-app.js里面有代码。

  1. exports.getProtooUrl = getProtooUrl;
  2. var protooPort = 4443;
  3. if (window.location.hostname === 'test.mediasoup.org') protooPort = 4444;
  4. function getProtooUrl(_ref) {
  5. var roomId = _ref.roomId,
  6. peerId = _ref.peerId;
  7. var hostname = window.location.hostname;
  8. return "wss://".concat(hostname, ":").concat(protooPort, "/?roomId=").concat(roomId, "&peerId=").concat(peerId);
  9. }

13-7 参考资料

Mediasoup(webrtc) Demo搭建及测试
https://blog.csdn.net/m0_37263637/article/details/89921861

【流媒体服务器Mediasoup】环境部署与demo搭建
https://blog.csdn.net/gjy_it/article/details/104423353