当前位置:网站首页>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
边栏推荐
- Custom component -- pure data field & component life cycle
- Network - network layer
- Array to linked list
- As a result, fill in the birthday candles
- Asynchronous programming promise
- Network - data link layer
- Icc2 analysis timing artifact analyze_ design_ violations
- 小甲鱼C(第六章数组1、2)
- Result fill in the blank (dfs*c language)
- JS array method Encyclopedia
猜你喜欢
随机推荐
MOOC Weng Kai C language week 8: pointer and string: 1. Pointer 2. Character type 3. String 4. String calculation
Results fill in the blanks carelessly (violent solution)
Clock tree analysis example
Bert的实现方法
Applet custom components - data, methods, and properties
[learning notes] linked list operation
Joern的代码使用-devign
DNS domain name resolution service
NAT和PAT的原理及配置
小甲鱼C(第六章数组1、2)
shell脚本——编程条件语句(条件测试、if语句、case分支语句、echo用法、for循环、while循环)
DOM -- page rendering, style attribute operation, preloading and lazy loading, anti shake and throttling
MOOC翁恺C语言第八周:指针与字符串:1.指针2.字符类型3.字符串4.字符串计算
Results fill in the blank shopping list (teach you to solve it with Excel)
DNS正向解析实验
Icc2 analysis timing artifact analyze_ design_ violations
Shell script - regular expression
MOOC翁恺 C语言 第三周:判断与循环:2.循环
VSphere esxi 7.0 update 3 release notes
Esxi community nvme driver update v1.1










