当前位置:网站首页>media-搭建直播服务器
media-搭建直播服务器
2022-07-28 05:18:00 【SheepOnTheCloud】
1,安装
npm install node-media-server --save2,config/config.default.js配置
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,在根目录创建app.js文件,配置
const NodeMediaServer = require('node-media-server');// 引入 class AppBootHook { constructor(app) { this.app = app; } configWillLoad() { // 此时 config 文件已经被读取并合并,但是还并未生效 // 这是应用层修改配置的最后时机 // 注意:此函数只支持同步调用 // 例如:参数中的密码是加密的,在此处进行解密 } async didLoad() { // 所有的配置已经加载完毕 // 可以用来加载应用自定义的文件,启动自定义的服务 if(!this.app.nms){ this.app.nms = new NodeMediaServer(this.app.config.mediaServer)//获取config配置 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() { // 所有的插件都已启动完毕,但是应用整体还未 ready // 可以做一些数据初始化等操作,这些操作成功才会启动应用 // 例如:从数据库加载数据到内存缓存 // this.app.cacheData = await this.app.model.query(QUERY_CACHE_SQL); } async didReady() { // 应用已经启动完毕 // const ctx = await this.app.createAnonymousContext(); // await ctx.service.Biz.request(); } async serverDidReady() { // http / https server 已启动,开始接受外部请求 // 此时可以从 app.server 拿到 server 的实例 // this.app.server.on('timeout', socket => { // // handle socket timeout // }); } } module.exports = AppBootHook;4,启动服务,出现关键词就说明启动成功了
2,生成推流码
rtmp://127.0.0.1:23480/${app名称}/${房间号}?sign=${防止链接被盗用的签名加密}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);//生成key let signData = sign(key); // 生成签名 console.log(key, signData);配置推拉流地址 rtmp://127.0.0.1:23480/${app名称}/${房间号}?sign=${防止链接被盗用的签名加密} 拉流(客户) http://127.0.0.1:23481/${app名称}/${房间号}.flv?sign=${防止链接被盗用的签名加密}通过OBS与VlC调试
3,总结
边栏推荐
猜你喜欢

链表中关于快慢指针的oj题

顺序表oj题目

Delete specific elements in order table OJ

顺序表oj之删除特定的元素

The Monte Carlo method solves the PI and draws points with turtle, and completes the progress bar problem

冶金物理化学复习 --- 液 - 液相反应动力学

顺序表oj之合并两个有序数组

冶金物理化学复习 -- 金属电沉积过程中的阴极极化,超电势以及阳极和阳极过程

repackag failed: Unable to find main class

shell运行原理
随机推荐
Mabtis(一)框架的基本使用
低照度图像数据集
五子棋优化版
Oracle view lock table statement and unlocking method
How to compare long and integer and why to report errors
Idea configures the service (run dashboard) service, and multiple modules are started at the same time
Merge two ordered arrays of order table OJ
C语言通讯录系统
Operation and use of collection framework
Arrangement of main drawings of the latest 54 papers of eccv22
Oracle create table, delete table, modify table (add field, modify field, delete field) statement summary
openjudge:大小写字母互换
Framework step by step easy-to-use process
[singleton mode] thread safety of lazy mode
When SQL queries the list, the data is inconsistent twice, and limit is automatically added
Openjudge: stone scissors cloth
Export excel, generate multiple sheet pages, and name them
You must configure either the server or JDBC driver (via the ‘serverTimezone)
Mabtis (I) basic use of framework
visio如何精确控制图形的大小和位置及角度
