WebRTC进阶流媒体服务器开发(二)Mediasoup环境配置

一:Mediasoup环境安装

(一)安装NodeJs环境

见:https://www.cnblogs.com/ssyfj/p/14781982.html

(二)下载Mediasoup-demo源码

git clone https://github.com/versatica/mediasoup-demo.git

WebRTC进阶流媒体服务器开发(二)Mediasoup环境配置

app:客户端代码,当mediasoup环境搭建完成后,浏览器去app目录中获取客户端代码(通过npm去按照package.json文件获取)

WebRTC进阶流媒体服务器开发(二)Mediasoup环境配置

package.json文件如下:

{
  "name": "mediasoup-demo-app",
  "version": "3.0.0",
  "private": true,
  "description": "mediasoup demo app",
  "author": "Iñaki Baz Castillo <ibc@aliax.net>",
  "license": "All Rights Reserved",
  "main": "lib/index.jsx",
  "scripts": {
    "lint": "eslint -c .eslintrc.js --ext .js,.jsx gulpfile.js lib",
    "start": "gulp live"
  },
  "dependencies": {
    "@babel/runtime": "^7.8.4",
    "bowser": "^2.11.0",
    "classnames": "^2.2.6",
    "clipboard-copy": "^3.1.0",
    "debug": "^4.3.1",
    "domready": "^1.0.8",
    "face-api.js": "^0.21.0",
    "hark": "^1.2.3",
    "js-cookie": "^2.2.1",
    "mediasoup-client": "github:versatica/mediasoup-client#v3",  //客户端与流媒体服务器通讯,使用的是mediasoup-client库。将文件下载到流媒体服务器,之后客户端浏览器去app文件下获取即可
    "pokemon": "^2.0.2",
    "prop-types": "^15.7.2",
    "protoo-client": "^4.0.5",
    "random-string": "^0.2.0",
    "react": "^16.12.0",
    "react-dom": "^16.12.0",
    "react-draggable": "^3.3.2",
    "react-dropzone": "^10.2.1",
    "react-redux": "^7.2.0",
    "react-spinner": "^0.2.7",
    "react-tooltip": "^3.11.1",
    "react-transition-group": "^4.3.0",
    "redux": "^4.0.5",
    "redux-logger": "^3.0.6",
    "redux-thunk": "^2.3.0",
    "riek": "^1.1.0",
    "url-parse": "^1.4.7"
  },
  "devDependencies": {
    "@babel/core": "^7.8.4",
    "@babel/plugin-proposal-object-rest-spread": "^7.8.3",
    "@babel/plugin-transform-runtime": "^7.8.3",
    "@babel/preset-env": "^7.8.4",
    "@babel/preset-react": "^7.8.3",
    "babel-plugin-jsx-control-statements": "^4.0.0",
    "babelify": "^10.0.0",
    "browser-sync": "^2.26.7",
    "browserify": "^16.5.0",
    "del": "^5.1.0",
    "envify": "^4.1.0",
    "eslint": "^6.8.0",
    "eslint-plugin-import": "^2.20.1",
    "eslint-plugin-jsx-control-statements": "^2.2.1",
    "eslint-plugin-react": "^7.18.3",
    "gulp": "^4.0.2",
    "gulp-css-base64": "^1.3.4",
    "gulp-eslint": "^6.0.0",
    "gulp-header": "^2.0.9",
    "gulp-if": "^3.0.0",
    "gulp-plumber": "^1.2.1",
    "gulp-rename": "^1.4.0",
    "gulp-stylus": "^2.7.0",
    "gulp-touch-cmd": "0.0.1",
    "gulp-uglify-es": "^2.0.0",
    "gulp-util": "^3.0.8",
    "mkdirp": "^0.5.1",
    "ncp": "^2.0.0",
    "nib": "^1.1.2",
    "supports-color": "^7.1.0",
    "vinyl-buffer": "^1.0.1",
    "vinyl-source-stream": "^2.0.0",
    "watchify": "^3.11.1"
  }
}

broadcasters:用于推流的模块

server:信令服务和媒体流服务,两者通过管道通信。下面目录中只有js代码,C++代码是nodejs的依赖库,通过package.json去下载运行

WebRTC进阶流媒体服务器开发(二)Mediasoup环境配置

package.json文件:

{
  "name": "mediasoup-demo-server",
  "version": "3.0.0",
  "private": true,
  "description": "mediasoup demo server",
  "author": "Iñaki Baz Castillo <ibc@aliax.net>",
  "license": "All Rights Reserved",
  "main": "lib/index.js",
  "scripts": {
    "lint": "eslint -c .eslintrc.js server.js lib connect.js",
    "start": "DEBUG=${DEBUG:='*mediasoup* *INFO* *WARN* *ERROR*'} INTERACTIVE=${INTERACTIVE:='true'} node server.js",
    "connect": "node connect.js"
  },
  "dependencies": {
    "@sitespeed.io/throttle": "^0.5.4",
    "awaitqueue": "^2.3.3",
    "body-parser": "^1.19.0",
    "colors": "^1.4.0",
    "debug": "^4.3.1",
    "express": "^4.17.1",
    "heapdump": "^0.3.15",
    "mediasoup": "github:versatica/mediasoup#v3",
    "pidusage": "^2.0.21",
    "protoo-server": "^4.0.5"
  },
  "devDependencies": {
    "eslint": "^6.8.0",
    "gulp": "^4.0.2",
    "gulp-eslint": "^6.0.0",
    "gulp-plumber": "^1.2.1"
  }
}

(三)配置服务端

1.进入服务端目录

cd server

WebRTC进阶流媒体服务器开发(二)Mediasoup环境配置

2.安装server端所需要的所有第三方库,包括前面提及的C++代码

npm install

WebRTC进阶流媒体服务器开发(二)Mediasoup环境配置

其中多了一个node_modules目录就是安装的依赖库

3.修改配置文件

cp config.example.js config.js

WebRTC进阶流媒体服务器开发(二)Mediasoup环境配置

const os = require('os');

module.exports =
{
    domain : process.env.DOMAIN || 'localhost',
    https  :  //https配置
    {
        listenIp   : '0.0.0.0',
        listenPort : process.env.PROTOO_LISTEN_PORT || 4443,
        tls        :  //安全机制,配置密钥
        {
            cert : process.env.HTTPS_CERT_FULLCHAIN || `${__dirname}/certs/fullchain.pem`,  
            key  : process.env.HTTPS_CERT_PRIVKEY || `${__dirname}/certs/privkey.pem`
        }
    },
    // mediasoup settings.
    mediasoup :
    {
        numWorkers     : Object.keys(os.cpus()).length,  //配置进程数量,默认按照CPU数量
        workerSettings :
        {
            logLevel : 'warn',  //日志级别
            logTags  :
            [
                'info',
                'ice',
                'dtls',
                'rtp',
                'srtp',
                'rtcp',
                'rtx',
                'bwe',
                'score',
                'simulcast',
                'svc',
                'sctp'
            ],
            rtcMinPort : process.env.MEDIASOUP_MIN_PORT || 40000,  //端口信息,流媒体服务器服务很多个客户端,所以需要一个端口池(最小,最大),每个客户端对应一个端口
            rtcMaxPort : process.env.MEDIASOUP_MAX_PORT || 49999
        },
        routerOptions :  //room
        {
            mediaCodecs :
            [
                {
                    kind      : 'audio',
                    mimeType  : 'audio/opus',
                    clockRate : 48000,
                    channels  : 2
                },
                {
                    kind       : 'video',
                    mimeType   : 'video/VP8',
                    clockRate  : 90000,
                    parameters :
                    {
                        'x-google-start-bitrate' : 1000
                    }
                },
                {
                    kind       : 'video',
                    mimeType   : 'video/VP9',
                    clockRate  : 90000,
                    parameters :
                    {
                        'profile-id'             : 2,
                        'x-google-start-bitrate' : 1000
                    }
                },
                {
                    kind       : 'video',
                    mimeType   : 'video/h264',
                    clockRate  : 90000,
                    parameters :
                    {
                        'packetization-mode'      : 1,
                        'profile-level-id'        : '4d0032',
                        'level-asymmetry-allowed' : 1,
                        'x-google-start-bitrate'  : 1000
                    }
                },
                {
                    kind       : 'video',
                    mimeType   : 'video/h264',
                    clockRate  : 90000,
                    parameters :
                    {
                        'packetization-mode'      : 1,
                        'profile-level-id'        : '42e01f',
                        'level-asymmetry-allowed' : 1,
                        'x-google-start-bitrate'  : 1000
                    }
                }
            ]
        },
        webRtcTransportOptions :  //webrtc传输选项---浏览器传输协议
        {
            listenIps :
            [
                {
                    ip          : process.env.MEDIASOUP_LISTEN_IP || '127.0.0.1',  //写为本机iP(服务器中可以写为0.0.0.0或者服务器IP任何一个网卡都可以接受推流)
                    announcedIp : process.env.MEDIASOUP_ANNOUNCED_IP
                }
            ],
            initialAvailableOutgoingBitrate : 1000000,
            minimumAvailableOutgoingBitrate : 600000,
            maxSctpMessageSize              : 262144,
            maxIncomingBitrate              : 1500000
        },
        plainTransportOptions :  //非浏览器---普通RTP传输协议
        {
            listenIp :
            {
                ip          : process.env.MEDIASOUP_LISTEN_IP || '127.0.0.1',    //同上
                announcedIp : process.env.MEDIASOUP_ANNOUNCED_IP
            },
            maxSctpMessageSize : 262144
        }
    }
};

4.配置密钥,现在安装部署基本完成,由于V3版本必须要密钥,现生成密钥:

在server目录下创建certs目录

mkdir certs

WebRTC进阶流媒体服务器开发(二)Mediasoup环境配置

在server/certs下执行:

openssl genrsa > privkey.pem
openssl req -new -x509 -key privkey.pem > fullchain.pem

WebRTC进阶流媒体服务器开发(二)Mediasoup环境配置 

(四)配置客户端

1.进入APP应用目录:

cd app

WebRTC进阶流媒体服务器开发(二)Mediasoup环境配置

2.安装app端所需要的所有第三方库

npm install

3.全局安装 gulp-cli 客户端工具:

sudo npm install -g gulp-cli

(五)启动测试

1.启动服务端程序(server目录)

sudo npm start  或者 sudo node server.js

WebRTC进阶流媒体服务器开发(二)Mediasoup环境配置

WebRTC进阶流媒体服务器开发(二)Mediasoup环境配置

2.启动客户端程序(app目录)

sudo gulp live 

WebRTC进阶流媒体服务器开发(二)Mediasoup环境配置

3.结果显示

WebRTC进阶流媒体服务器开发(二)Mediasoup环境配置

 

上一篇:day21


下一篇:Gulp自动化构建案例---图片和字体文件转换