当前位置:网站首页>Media set up live broadcast server
Media set up live broadcast server
2022-07-28 07:05:00 【SheepOnTheCloud】
1, install
npm install node-media-server --save2,config/config.default.js To configure
config.mediaServer = { rtmp: { port: 23480, chunk_size: 60000, gop_cache: true, ping: 30, ping_timeout: 60, }, http: { port: 23481, allow_origin: "*", }, auth: { play: true, publish: true, secret: "nodemedia2017privatekey", }, };3, Created in the root directory app.js file , To configure
const NodeMediaServer = require('node-media-server');// introduce class AppBootHook { constructor(app) { this.app = app; } configWillLoad() { // here config The file has been read and merged , But it hasn't come into effect yet // This is the final time for the application layer to modify the configuration // Be careful : This function only supports synchronous calls // for example : The password in the parameter is encrypted , Decrypt here } async didLoad() { // All configurations have been loaded // Can be used to load application customization files , Start a custom service if(!this.app.nms){ this.app.nms = new NodeMediaServer(this.app.config.mediaServer)// obtain config To configure this.app.nms.run(); this.app.nms.on('preConnect', (id, args) => { console.log('[NodeEvent on preConnect]', `id=${id} args=${JSON.stringify(args)}`); // let session = nms.getSession(id); // session.reject(); }); this.app.nms.on('postConnect', (id, args) => { console.log('[NodeEvent on postConnect]', `id=${id} args=${JSON.stringify(args)}`); }); this.app.nms.on('doneConnect', (id, args) => { console.log('[NodeEvent on doneConnect]', `id=${id} args=${JSON.stringify(args)}`); }); this.app.nms.on('prePublish', (id, StreamPath, args) => { console.log('[NodeEvent on prePublish]', `id=${id} StreamPath=${StreamPath} args=${JSON.stringify(args)}`); // let session = nms.getSession(id); // session.reject(); }); this.app.nms.on('postPublish', (id, StreamPath, args) => { console.log('[NodeEvent on postPublish]', `id=${id} StreamPath=${StreamPath} args=${JSON.stringify(args)}`); }); this.app.nms.on('donePublish', (id, StreamPath, args) => { console.log('[NodeEvent on donePublish]', `id=${id} StreamPath=${StreamPath} args=${JSON.stringify(args)}`); }); this.app.nms.on('prePlay', (id, StreamPath, args) => { console.log('[NodeEvent on prePlay]', `id=${id} StreamPath=${StreamPath} args=${JSON.stringify(args)}`); // let session = nms.getSession(id); // session.reject(); }); this.app.nms.on('postPlay', (id, StreamPath, args) => { console.log('[NodeEvent on postPlay]', `id=${id} StreamPath=${StreamPath} args=${JSON.stringify(args)}`); }); this.app.nms.on('donePlay', (id, StreamPath, args) => { console.log('[NodeEvent on donePlay]', `id=${id} StreamPath=${StreamPath} args=${JSON.stringify(args)}`); }); } } async willReady() { // All plug-ins have been started , But the application as a whole has not ready // Can do some data initialization and other operations , These operations are successful before the application starts // for example : Load data from database to memory cache // this.app.cacheData = await this.app.model.query(QUERY_CACHE_SQL); } async didReady() { // The app has started // const ctx = await this.app.createAnonymousContext(); // await ctx.service.Biz.request(); } async serverDidReady() { // http / https server Started , Start accepting external requests // At this time, you can go from app.server Get server Example // this.app.server.on('timeout', socket => { // // handle socket timeout // }); } } module.exports = AppBootHook;4, Start the service , The keyword indicates that the startup is successful
2, Generate streaming code
rtmp://127.0.0.1:23480/${app name }/${ Your room number, }?sign=${ Signature encryption to prevent link theft }function randomString(length) { const chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; var result = ""; for (var i = length; i > 0; --i) result += chars[Math.floor(Math.random() * chars.length)]; return result; } let { ctx, app } = this; function sign(key) { const secret = app.config.mediaServer.auth.secret; let expire = parseInt((Date.now() + 100000000) / 1000); let hashValue = md5(`/live/${key}-${expire}-${secret}`); return `${expire}-${hashValue}`; } let key = randomString(20);// Generate key let signData = sign(key); // Generate signature console.log(key, signData);Configure the push-pull stream address rtmp://127.0.0.1:23480/${app name }/${ Your room number, }?sign=${ Signature encryption to prevent link theft } Pull flow ( Customer ) http://127.0.0.1:23481/${app name }/${ Your room number, }.flv?sign=${ Signature encryption to prevent link theft }adopt OBS And VlC debugging
3, summary
边栏推荐
- Tcp/ip five layer model
- YUM仓库的搭建
- Hdu-5805-nanoape loves sequence (thinking questions)
- Custom component -- data listener
- Esxi community network card driver
- VSphere esxi 7.0 update 3 release notes
- Technology sharing | send requests using postman
- Wechat applet custom compilation mode
- SSH服务配置
- Animation animation realizes the crossing (click) pause
猜你喜欢

Custom component -- data listener

修复故障扇区

Technology sharing | sending requests using curl

raid磁盘阵列

Technology sharing | send requests using postman

Technology sharing | common proxy tools for interface testing

PXE无人值守安装管理

Archery database audit platform deployment

Results fill in the blank. How many days of national day are Sundays (solved by pure Excel)

Network - transport layer (detailed version)
随机推荐
修复故障扇区
ES6 add -- > object
Technology sharing | how to do Assertion Verification in interface automated testing?
MOOC翁恺C语言 第六周:数组与函数:1.数组2.函数的定义与使用3.函数的参数和变量4.二维数组
shell脚本——正则表达式
Test life | second tier cities with an annual salary of more than 40W? How did you achieve 100% salary increase under the epidemic?
Three cache technologies -- localstorage, sessionstorage, cookies
NFS shared storage service
SSH服务配置
Si Han talks about the professional development of testers
DOM - Events
Method of designing test cases
MySQL installation and use
Insertion and deletion of nodes in linked list
MOOC翁恺C语言 第四周:进一步的判断与循环:1.逻辑类型与运算2.级联和嵌套的判断
JSON notes
Forward and backward slash notes
MOOC Weng Kai C language week 7: array operation: 1. array operation 2. Search 3. preliminary sorting
Blue bridge code error ticket
DNS正向解析实验
