From 8f946b828ba4b0128c9775458d3143de99e6580b Mon Sep 17 00:00:00 2001 From: Chen Mingliang Date: Fri, 6 Dec 2024 15:48:15 +0800 Subject: [PATCH] Add the RTMPS implementation --- README.md | 2 +- bin/app.js | 8 ++++++++ bin/config.json | 6 ++++++ src/server/rtmp_server.js | 14 ++++++++++++-- 4 files changed, 27 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 72d92a30..320ec982 100644 --- a/README.md +++ b/README.md @@ -23,7 +23,7 @@ npx node-media-server ## Features * HTTP/HTTP2-flv Push/Play -* RTMP Push/Play +* RTMP/RTMPS Push/Play * GOP cache ## Roadmap diff --git a/bin/app.js b/bin/app.js index 7caa6071..ceb60200 100755 --- a/bin/app.js +++ b/bin/app.js @@ -5,6 +5,14 @@ const path = require("path"); const config = require("./config.json"); const NodeMediaServer = require(".."); +if (!fs.existsSync(config.rtmps.key)) { + config.rtmps.key = path.join(__dirname, config.rtmps.key); + +} +if (!fs.existsSync(config.rtmps.cert)) { + config.rtmps.cert = path.join(__dirname, config.rtmps.cert); +} + if (!fs.existsSync(config.https.key)) { config.https.key = path.join(__dirname, config.https.key); diff --git a/bin/config.json b/bin/config.json index 0493c12d..502038df 100644 --- a/bin/config.json +++ b/bin/config.json @@ -3,6 +3,12 @@ "bind": "0.0.0.0", "port": 1935 }, + "rtmps": { + "bind": "0.0.0.0", + "port": 1936, + "key": "./key.pem", + "cert": "./cert.pem" + }, "http": { "bind": "0.0.0.0", "port": 8000 diff --git a/src/server/rtmp_server.js b/src/server/rtmp_server.js index c757b6a9..d61e27ad 100644 --- a/src/server/rtmp_server.js +++ b/src/server/rtmp_server.js @@ -5,9 +5,11 @@ // Copyright (c) 2023 Nodemedia. All rights reserved. // -const Context = require("../core/context.js"); +const fs = require("fs"); const net = require("net"); +const tls = require('tls'); const logger = require("../core/logger.js"); +const Context = require("../core/context.js"); const RtmpSession = require("../session/rtmp_session.js"); class NodeRtmpServer { @@ -16,14 +18,22 @@ class NodeRtmpServer { */ constructor(ctx) { this.ctx = ctx; + const opt = { + key: fs.readFileSync(ctx.config.rtmps.key), + cert: fs.readFileSync(ctx.config.rtmps.cert), + }; + this.tcpServer = net.createServer(this.handleRequest); + this.tlsServer = tls.createServer(opt, this.handleRequest); } run = () => { this.tcpServer.listen(this.ctx.config.rtmp.port, this.ctx.config.rtmp.bind, () => { logger.log(`Rtmp Server listening on port ${this.ctx.config.rtmp.bind}:${this.ctx.config.rtmp.port}`); }); - + this.tlsServer.listen(this.ctx.config.rtmps.port, this.ctx.config.rtmps.bind, () => { + logger.log(`Rtmps Server listening on port ${this.ctx.config.rtmps.bind}:${this.ctx.config.rtmps.port}`); + }); }; /**