Skip to content

Commit

Permalink
Adds a check on whether the configuration is enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
illuspas committed Dec 6, 2024
1 parent 84e5999 commit ff4f69f
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 38 deletions.
8 changes: 4 additions & 4 deletions bin/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@ const path = require("path");
const config = require("./config.json");
const NodeMediaServer = require("..");

if (!fs.existsSync(config.rtmps.key)) {
if (config.rtmps?.key && !fs.existsSync(config.rtmps.key)) {
config.rtmps.key = path.join(__dirname, config.rtmps.key);

}
if (!fs.existsSync(config.rtmps.cert)) {
if (config.rtmps?.cert && !fs.existsSync(config.rtmps.cert)) {
config.rtmps.cert = path.join(__dirname, config.rtmps.cert);
}

if (!fs.existsSync(config.https.key)) {
if (config.https?.key && !fs.existsSync(config.https.key)) {
config.https.key = path.join(__dirname, config.https.key);

}
if (!fs.existsSync(config.https.cert)) {
if (config.https?.cert && !fs.existsSync(config.https.cert)) {
config.https.cert = path.join(__dirname, config.https.cert);
}

Expand Down
9 changes: 6 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
// Copyright (c) 2024 NodeMedia. All rights reserved.
//

const NodeHttpServer = require("./server/http_server.js");
const NodeRtmpServer = require("./server/rtmp_server.js");
const logger = require("./core/logger.js");
const Context = require("./core/context.js");
const Package = require("../package.json");
const Context = require("./core/context.js");
const NodeHttpServer = require("./server/http_server.js");
const NodeRtmpServer = require("./server/rtmp_server.js");

class NodeMediaServer {
constructor(config) {
Expand All @@ -18,14 +18,17 @@ class NodeMediaServer {
logger.info(`Homepage: ${Package.homepage}`);
logger.info(`License: ${Package.license}`);
logger.info(`Author: ${Package.author}`);

this.ctx = new Context(config);
this.httpServer = new NodeHttpServer(this.ctx);
this.rtmpServer = new NodeRtmpServer(this.ctx);
}

run() {

this.httpServer.run();
this.rtmpServer.run();

}
}

Expand Down
1 change: 0 additions & 1 deletion src/protocol/amf.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

const logger = require("../core/logger.js");


const amf0dRules = {
0x00: amf0decNumber,
0x01: amf0decBool,
Expand Down
2 changes: 1 addition & 1 deletion src/protocol/flv.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
// Copyright (c) 2023 Nodemedia. All rights reserved.
//

const AMF = require("./amf.js");
const logger = require("../core/logger.js");
const AVPacket = require("../core/avpacket.js");
const AMF = require("./amf.js");

const FLV_MEDIA_TYPE_AUDIO = 8;
const FLV_MEDIA_TYPE_VIDEO = 9;
Expand Down
4 changes: 2 additions & 2 deletions src/protocol/rtmp.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
// Copyright (c) 2024 Nodemedia. All rights reserved.
//

const AMF = require("./amf.js");
const Flv = require("./flv.js");
const crypto = require("node:crypto");
const logger = require("../core/logger.js");
const AVPacket = require("../core/avpacket.js");
const Flv = require("./flv.js");
const AMF = require("./amf.js");

const N_CHUNK_STREAM = 8;
const RTMP_VERSION = 3;
Expand Down
2 changes: 1 addition & 1 deletion src/server/broadcast_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
// Copyright (c) 2023 NodeMedia. All rights reserved.
//

const AVPacket = require("../core/avpacket.js");
const Flv = require("../protocol/flv.js");
const Rtmp = require("../protocol/rtmp.js");
const AVPacket = require("../core/avpacket.js");
const BaseSession = require("../session/base_session.js");

class BroadcastServer {
Expand Down
27 changes: 16 additions & 11 deletions src/server/http_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ const fs = require("fs");
const http = require("http");
const http2 = require("http2");
const express = require("express");
const http2Express = require("http2-express-bridge");
const FlvSession = require("../session/flv_session.js");
const logger = require("../core/logger.js");
const Context = require("../core/context.js");
const http2Express = require("http2-express-bridge");
const FlvSession = require("../session/flv_session.js");

class NodeHttpServer {
/**
Expand All @@ -21,11 +21,6 @@ class NodeHttpServer {
constructor(ctx) {
this.ctx = ctx;
const app = http2Express(express);
const opt = {
key: fs.readFileSync(ctx.config.https.key),
cert: fs.readFileSync(ctx.config.https.cert),
allowHTTP1: true
};

app.all("*", (req, res, next) => {
res.header("Access-Control-Allow-Origin", "*");
Expand All @@ -34,15 +29,25 @@ class NodeHttpServer {

app.all("/:app/:name.flv", this.handleFlv);

this.server1 = http.createServer(app);
this.server2 = http2.createSecureServer(opt, app);
if (ctx.config.http?.port) {
this.httpServer = http.createServer(app);
}
if (ctx.config.https?.port) {
const opt = {
key: fs.readFileSync(ctx.config.https.key),
cert: fs.readFileSync(ctx.config.https.cert),
allowHTTP1: true
};
this.httpsServer = http2.createSecureServer(opt, app);
}

}

run = () => {
this.server1.listen(this.ctx.config.http.port, this.ctx.config.http.bind, () => {
this.httpServer?.listen(this.ctx.config.http.port, this.ctx.config.http.bind, () => {
logger.info(`HTTP server listening on port ${this.ctx.config.http.bind}:${this.ctx.config.http.port}`);
});
this.server2.listen(this.ctx.config.https.port, this.ctx.config.https.bind, () => {
this.httpsServer?.listen(this.ctx.config.https.port, this.ctx.config.https.bind, () => {
logger.info(`HTTPS server listening on port ${this.ctx.config.https.bind}:${this.ctx.config.https.port}`);
});
};
Expand Down
21 changes: 12 additions & 9 deletions src/server/rtmp_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,23 @@ 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);
if (ctx.config.rtmp?.port) {
this.tcpServer = net.createServer(this.handleRequest);
}
if (ctx.config.rtmps?.port) {
const opt = {
key: fs.readFileSync(ctx.config.rtmps.key),
cert: fs.readFileSync(ctx.config.rtmps.cert),
};
this.tlsServer = tls.createServer(opt, this.handleRequest);
}
}

run = () => {
this.tcpServer.listen(this.ctx.config.rtmp.port, this.ctx.config.rtmp.bind, () => {
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, () => {
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}`);
});
};
Expand Down
4 changes: 2 additions & 2 deletions src/session/flv_session.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
//

const express = require( "express");
const BaseSession = require( "./base_session.js");
const BroadcastServer = require( "../server/broadcast_server.js");
const Flv = require( "../protocol/flv.js");
const logger = require( "../core/logger.js");
const Context = require( "../core/context.js");
const AVPacket = require( "../core/avpacket.js");
const BaseSession = require( "./base_session.js");
const BroadcastServer = require( "../server/broadcast_server.js");

/**
* @class
Expand Down
8 changes: 4 additions & 4 deletions src/session/rtmp_session.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
// Copyright (c) 2024 Nodemedia. All rights reserved.
//

const net = require( "node:net");
const Context = require( "../core/context.js");
const BaseSession = require( "./base_session.js");
const BroadcastServer = require( "../server/broadcast_server.js");
const net = require( "net");
const Rtmp = require( "../protocol/rtmp.js");
const logger = require( "../core/logger.js");
const Context = require( "../core/context.js");
const AVPacket = require( "../core/avpacket.js");
const BaseSession = require( "./base_session.js");
const BroadcastServer = require( "../server/broadcast_server.js");

/**
* @class
Expand Down

0 comments on commit ff4f69f

Please sign in to comment.