first commit

This commit is contained in:
2024-03-05 19:20:15 +05:00
commit 9167785656
17 changed files with 2182 additions and 0 deletions
+16
View File
@@ -0,0 +1,16 @@
import { connect } from "mongoose";
async function connectDB() {
try {
await connect(process.env.MONGO_URI!, { dbName: "pixel_streaming2" });
console.log("MongoDB connected...");
} catch (error) {
if (error instanceof Error) {
console.error(error.message);
}
process.exit(1);
}
}
export default connectDB;
+209
View File
@@ -0,0 +1,209 @@
import "dotenv/config";
import connectDB from "./config/db";
import express, { json } from "express";
import cors from "cors";
import os from "os";
import util from "util";
import { exec, execFile } from "child_process";
import treeKill from "tree-kill";
import SessionServer from "./models/SessionServer";
import ActiveSession from "./models/ActiveSession";
connectDB();
const app = express();
const port = process.env.PORT;
app.use(json());
app.use(cors());
const serverLocation = process.env.SERVER_LOCATION;
const serverName = process.env.SERVER_NAME;
const serverType = process.env.SERVER_TYPE;
const serverHostname = os.hostname();
async function updateSessionServerData(data: any) {
try {
await SessionServer.findOneAndUpdate({ hostname: serverHostname }, data);
} catch (error) {
if (error instanceof Error) {
console.log("Error: ", error.message);
}
}
}
const execAsync = util.promisify(exec);
async function getGpuMemoryFree() {
try {
const { stdout } = await execAsync(
"nvidia-smi --query-gpu=memory.free --format=csv,noheader,nounits",
{ windowsHide: true }
);
const gpuMemoryFree = stdout.trimEnd();
await updateSessionServerData({ gpuMemoryFree });
} catch (error) {
if (error instanceof Error) {
console.log("Error: ", error.message);
}
}
setTimeout(async () => {
await getGpuMemoryFree();
}, 1000);
}
async function getAvailablePorts() {
const cirrusPorts = [14000, 14001, 14002];
for (const cirrusPort of cirrusPorts) {
console.log(cirrusPort);
const activeSession = await ActiveSession.exists({
location: serverLocation,
name: serverName,
cirrusPort,
});
if (activeSession) continue;
const uePort = cirrusPort + 10;
return { cirrusPort, uePort };
}
}
async function startSession(buildName: string) {
const filePath = `C:/PixelStreaming/builds/${buildName}/${buildName}.exe`;
const availablePorts = await getAvailablePorts();
if (!availablePorts) {
console.log("No available ports");
return;
}
const { cirrusPort, uePort } = availablePorts;
const cirrusProcess = execFile("node", [
`C:/PixelStreaming/signalling-server/cirrus.js`,
`--StreamerPort`,
`${uePort}`,
`--HttpPort`,
`${cirrusPort}`,
]);
const cirrusProcessId = cirrusProcess.pid;
if (!cirrusProcessId) {
console.log("Cirrus server was not started");
return;
}
const ueProcess = execFile(filePath, [
"-PixelStreamingIP=127.0.0.1",
`-PixelStreamingPort=${uePort}`,
"-RenderOffScreen",
"-ForceRes",
"-ResX=1920",
"-ResY=1080",
"-Unattended",
"-PixelStreamingWebRTCMinBitrate=5000000",
"-PixelStreamingWebRTCMaxBitrate=20000000",
"-PixelStreamingH264Profile=HIGH",
"-PixelStreamingWebRTCDisableReceiveAudio=true",
"-PixelStreamingEncoderRateControl=VBR",
// "-PixelStreamingHudStats=true",
// `-SessionID=${session.id}`,
]);
const ueProcessId = ueProcess.pid;
if (!ueProcessId) {
console.log("UE application was not started");
return;
}
const activeSession = await ActiveSession.create({
location: serverLocation,
name: serverName,
buildName,
cirrusPort,
uePort,
cirrusProcessId,
ueProcessId,
});
const activeSessionId = activeSession.id;
return activeSessionId;
}
async function endSession(activeSessionId: string) {
const activeSession = await ActiveSession.findByIdAndDelete(activeSessionId);
if (!activeSession) {
console.log("Session with this ID not found");
return;
}
treeKill(activeSession.cirrusProcessId as number);
treeKill(activeSession.ueProcessId as number);
console.log("Kill session");
}
async function init() {
try {
await SessionServer.findOneAndUpdate(
{ hostname: serverHostname },
{
location: serverLocation,
name: serverName,
type: serverType,
hostname: serverHostname,
},
{ upsert: true, new: true }
);
getGpuMemoryFree();
} catch (error) {
if (error instanceof Error) {
console.log("Error: ", error.message);
}
}
}
app.post("/start", async (req, res) => {
console.log(req.query);
const buildName: string = req.body.buildName;
if (!buildName) {
return res.json({ error: 1 });
}
const result = await startSession(buildName);
console.log("Result: ", result);
res.json({ ok: 1 });
});
app.post("/end", async (req, res) => {
const activeSessionId: string = req.body.activeSessionId;
if (!activeSessionId) {
return res.json({ error: 1 });
}
await endSession(activeSessionId);
res.json({ ok: 1 });
});
app.listen(port, () => {
console.log(`Server is listening on port ${port}`);
init();
});
+36
View File
@@ -0,0 +1,36 @@
import { model, Schema } from "mongoose";
const activeSessionSchema = new Schema(
{
location: {
type: String,
},
name: {
type: String,
},
buildName: {
type: String,
},
uePort: {
type: Number,
},
cirrusPort: {
type: Number,
},
ueProcessId: {
type: Number,
},
cirrusProcessId: {
type: Number,
},
},
{
timestamps: true,
toJSON: { virtuals: true },
toObject: { virtuals: true },
}
);
const ActiveSession = model("Active_Session", activeSessionSchema);
export default ActiveSession;
+31
View File
@@ -0,0 +1,31 @@
import { model, Schema } from "mongoose";
const sessionServerSchema = new Schema(
{
location: {
type: String,
},
name: {
type: String,
},
type: {
type: String,
},
hostname: {
type: String,
unique: true,
},
gpuMemoryFree: {
type: Number,
},
},
{
timestamps: true,
toJSON: { virtuals: true },
toObject: { virtuals: true },
}
);
const SessionServer = model("Session_Server", sessionServerSchema);
export default SessionServer;
+11
View File
@@ -0,0 +1,11 @@
import { Router } from "express";
const router = Router();
router.get("/", async (req, res) => {
res.json({ ok: 1 });
});
const testRouter = router;
export default testRouter;