Skip to content

Commit

Permalink
Merge pull request #81 from boostcampwm-2024/dev-be
Browse files Browse the repository at this point in the history
[MERGE] 프론트, 백엔드 통합 준비
  • Loading branch information
hoeeeeeh authored Nov 7, 2024
2 parents 61d958d + 7b8d5f8 commit 45f5d1b
Show file tree
Hide file tree
Showing 47 changed files with 6,398 additions and 3,547 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
!.yarn/sdks
!.yarn/versions

.DS_Store
.idea

# Swap the comments on the following lines if you wish to use zero-installs
Expand Down
1 change: 1 addition & 0 deletions .yarn/sdks/integrations.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@

integrations:
- vscode

5 changes: 5 additions & 0 deletions backend/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
OBJECT_STORAGE_ACCESS_KEY_ID: ncp_iam_BPAMKR1RRjFz97yoco59
OBJECT_STORAGE_SECRET_ACCESS_KEY: ncp_iam_BPKMKRWpU9HKDIyLUCrSzk0ZZTQiKTTYay
OBJECT_STORAGE_REGION: kr-standard
OBJECT_STORAGE_ENDPOINT: https://kr.object.ncloudstorage.com
OBJECT_STORAGE_BUCKET_NAME: web22
5 changes: 5 additions & 0 deletions backend/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
node_modules
ffmpeg
nodeMediaServer/media/*

.env
1 change: 1 addition & 0 deletions backend/nodeMediaServer/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# nodeMediaServer
36 changes: 36 additions & 0 deletions backend/nodeMediaServer/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"name": "nodeMediaServer",
"version": "2.7.0",
"description": "A Node.js implementation of RTMP Server",
"bin": "bin/app.js",
"main": "src/node_media_server.js",
"types": "types/index.d.ts",
"packageManager": "[email protected]",
"scripts": {
"start": ""
},
"keywords": [
"rtmp",
"flv",
"server"
],
"bugs": {
"url": "https://github.com/illuspas/Node-Media-Server/issues"
},
"homepage": "https://github.com/illuspas/Node-Media-Server#readme",
"dependencies": {
"@aws-sdk/client-s3": "^3.685.0",
"aws-sdk": "^2.1691.0",
"basic-auth-connect": "^1.0.0",
"body-parser": "^1.20.3",
"chalk": "^4.1.0",
"dateformat": "^4.6.3",
"dotenv": "^16.4.5",
"express": "^4.19.2",
"http2-express-bridge": "^1.0.7",
"lodash": "^4.17.21",
"minimist": "^1.2.8",
"mkdirp": "^2.1.5",
"ws": "^8.13.0"
}
}
175 changes: 175 additions & 0 deletions backend/nodeMediaServer/src/api/controllers/relay.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
//
// Created by Mingliang Chen on 19/4/11.
// illuspas[a]gmail.com
// Copyright (c) 2019 Nodemedia. All rights reserved.
//
const { get, set } = require('lodash');
const Express = require('express');
const { once } = require('events');

/**
* get all relay tasks
* @param {Express.Request} req
* @param {Express.Response} res
* @param {*} next
*/
function getStreams(req, res, next) {
let stats = {};
this.sessions.forEach(function (session, id) {
if (session.constructor.name !== 'NodeRelaySession') {
return;
}

let { app, name } = session.conf;

if (!get(stats, [app, name])) {
set(stats, [app, name], {
relays: [],
});
}

stats[app][name]['relays'].push({
app: app,
name: name,
path: session.conf.inPath,
url: session.conf.ouPath,
mode: session.conf.mode,
ts: session.ts,
id: id,
});
});
res.json(stats);
}

/**
* get relay task by id
* @param {Express.Request} req
* @param {Express.Response} res
* @param {*} next
*/
function getStreamByID(req, res, next) {
const relaySession = Array.from(this.sessions.values()).filter(
(session) =>
session.constructor.name === 'NodeRelaySession' &&
req.params.id === session.id
);
const relays = relaySession.map((item) => ({
app: item.conf.app,
name: item.conf.name,
path: item.conf.inPath,
url: item.conf.ouPath,
mode: item.conf.mode,
ts: item.ts,
id: item.id,
}));
res.json(relays);
}

/**
* get relay task by name
* @param {Express.Request} req
* @param {Express.Response} res
* @param {*} next
*/
function getStreamByName(req, res, next) {
const relaySession = Array.from(this.sessions.values()).filter(
(session) =>
session.constructor.name === 'NodeRelaySession' &&
req.params.app === session.conf.app &&
req.params.name === session.conf.name
);
const relays = relaySession.map((item) => ({
app: item.conf.app,
name: item.conf.name,
url: item.conf.ouPath,
mode: item.conf.mode,
ts: item.ts,
id: item.id,
}));
res.json(relays);
}

/**
* create relay url to url task
* @param {Express.Request} req
* @param {Express.Response} res
* @param {*} next
*/
async function relayStream(req, res, next) {
let path = req.body.path;
let url = req.body.url;
if (path && url) {
process.nextTick(() => this.nodeEvent.emit('relayTask', path, url));
let ret = await once(this.nodeEvent, 'relayTaskDone');
res.send(ret[0]);
} else {
res.sendStatus(400);
}
}


/**
* create relay pull task
* @param {Express.Request} req
* @param {Express.Response} res
* @param {*} next
*/
async function pullStream(req, res, next) {
let url = req.body.url;
let app = req.body.app;
let name = req.body.name;
let rtsp_transport = req.body.rtsp_transport ? req.body.rtsp_transport : null;
if (url && app && name) {
process.nextTick(() => this.nodeEvent.emit('relayPull', url, app, name, rtsp_transport));
let ret = await once(this.nodeEvent, 'relayPullDone');
res.send(ret[0]);

} else {
res.sendStatus(400);
}
}

/**
* create relay push task
* @param {Express.Request} req
* @param {Express.Response} res
* @param {*} next
*/
async function pushStream(req, res, next) {
let url = req.body.url;
let app = req.body.app;
let name = req.body.name;
if (url && app && name) {
process.nextTick(() => this.nodeEvent.emit('relayPush', url, app, name));
let ret = await once(this.nodeEvent, 'relayPushDone');
res.send(ret[0]);
} else {
res.sendStatus(400);
}
}

/**
* delete relay task
* @param {Express.Request} req
* @param {Express.Response} res
* @param {*} next
*/
function delStream(req, res, next) {
let relaySession = this.sessions.get(req.params.id);
if (relaySession) {
relaySession.end();
res.sendStatus(200);
} else {
res.sendStatus(404);
}
}

module.exports = {
getStreams,
getStreamByID,
getStreamByName,
relayStream,
pullStream,
pushStream,
delStream,
};
116 changes: 116 additions & 0 deletions backend/nodeMediaServer/src/api/controllers/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
//
// Created by Mingliang Chen on 17/12/24. Merry Christmas
// illuspas[a]gmail.com
// Copyright (c) 2018 Nodemedia. All rights reserved.
//

const OS = require('os');
const Package = require('../../../package.json');
function cpuAverage() {

//Initialise sum of idle and time of cores and fetch CPU info
let totalIdle = 0, totalTick = 0;
let cpus = OS.cpus();

//Loop through CPU cores
for (let i = 0, len = cpus.length; i < len; i++) {

//Select CPU core
let cpu = cpus[i];

//Total up the time in the cores tick
for (type in cpu.times) {
totalTick += cpu.times[type];
}

//Total up the idle time of the core
totalIdle += cpu.times.idle;
}

//Return the average Idle and Tick times
return { idle: totalIdle / cpus.length, total: totalTick / cpus.length };
}

function percentageCPU() {
return new Promise(function (resolve, reject) {
let startMeasure = cpuAverage();
setTimeout(() => {
let endMeasure = cpuAverage();
//Calculate the difference in idle and total time between the measures
let idleDifference = endMeasure.idle - startMeasure.idle;
let totalDifference = endMeasure.total - startMeasure.total;

//Calculate the average percentage CPU usage
let percentageCPU = 100 - ~~(100 * idleDifference / totalDifference);
resolve(percentageCPU);
}, 100);
});
}

function getSessionsInfo(sessions) {
let info = {
inbytes: 0,
outbytes: 0,
rtmp: 0,
http: 0,
ws: 0,
};

for (let session of sessions.values()) {
if (session.TAG === 'relay') continue;
let socket = session.TAG === 'rtmp' ? session.socket : session.req.socket;
info.inbytes += socket.bytesRead;
info.outbytes += socket.bytesWritten;
info.rtmp += session.TAG === 'rtmp' ? 1 : 0;
info.http += session.TAG === 'http-flv' ? 1 : 0;
info.ws += session.TAG === 'websocket-flv' ? 1 : 0;
}

return info;
}


function getInfo(req, res, next) {
let s = this.sessions;
percentageCPU().then((cpuload) => {
let sinfo = getSessionsInfo(s);
let info = {
os: {
arch: OS.arch(),
platform: OS.platform(),
release: OS.release(),
},
cpu: {
num: OS.cpus().length,
load: cpuload,
model: OS.cpus()[0].model,
speed: OS.cpus()[0].speed,
},
mem: {
totle: OS.totalmem(),
free: OS.freemem()
},
net: {
inbytes: this.stat.inbytes + sinfo.inbytes,
outbytes: this.stat.outbytes + sinfo.outbytes,
},
nodejs: {
uptime: Math.floor(process.uptime()),
version: process.version,
mem: process.memoryUsage()
},
clients: {
accepted: this.stat.accepted,
active: this.sessions.size - this.idlePlayers.size,
idle: this.idlePlayers.size,
rtmp: sinfo.rtmp,
http: sinfo.http,
ws: sinfo.ws
},
version: Package.version
};
res.json(info);
});
}

exports.getInfo = getInfo;
Loading

0 comments on commit 45f5d1b

Please sign in to comment.