Update openvidu-recording-basic-node

This commit is contained in:
pabloFuente 2025-02-11 13:33:30 +01:00
parent 5e2a891250
commit 48e1fb79f7
6 changed files with 1703 additions and 1555 deletions

File diff suppressed because it is too large Load Diff

View File

@ -8,10 +8,10 @@
"start": "node src/index.js"
},
"dependencies": {
"@aws-sdk/client-s3": "3.654.0",
"@aws-sdk/client-s3": "3.744.0",
"cors": "2.8.5",
"dotenv": "16.4.5",
"express": "4.21.0",
"livekit-server-sdk": "^2.7.2"
"dotenv": "16.4.7",
"express": "5.0.1",
"livekit-server-sdk": "^2.9.7"
}
}

View File

@ -7,171 +7,190 @@ const LivekitClient = window.LivekitClient;
var room;
function configureLiveKitUrl() {
// If LIVEKIT_URL is not configured, use default value from OpenVidu Local deployment
if (!LIVEKIT_URL) {
if (window.location.hostname === "localhost") {
LIVEKIT_URL = "ws://localhost:7880/";
} else {
LIVEKIT_URL = "wss://" + window.location.hostname + ":7443/";
}
// If LIVEKIT_URL is not configured, use default value from OpenVidu Local deployment
if (!LIVEKIT_URL) {
if (window.location.hostname === "localhost") {
LIVEKIT_URL = "ws://localhost:7880/";
} else {
LIVEKIT_URL = "wss://" + window.location.hostname + ":7443/";
}
}
}
async function joinRoom() {
// Disable 'Join' button
document.getElementById("join-button").disabled = true;
document.getElementById("join-button").innerText = "Joining...";
// Disable 'Join' button
document.getElementById("join-button").disabled = true;
document.getElementById("join-button").innerText = "Joining...";
// Initialize a new Room object
room = new LivekitClient.Room();
// Initialize a new Room object
room = new LivekitClient.Room();
// Specify the actions when events take place in the room
// On every new Track received...
room.on(LivekitClient.RoomEvent.TrackSubscribed, (track, _publication, participant) => {
addTrack(track, participant.identity);
});
// On every new Track destroyed...
room.on(LivekitClient.RoomEvent.TrackUnsubscribed, (track, _publication, participant) => {
track.detach();
document.getElementById(track.sid)?.remove();
if (track.kind === "video") {
removeVideoContainer(participant.identity);
}
});
// When recording status changes...
room.on(LivekitClient.RoomEvent.RecordingStatusChanged, async (isRecording) => {
await updateRecordingInfo(isRecording);
});
try {
// Get the room name and participant name from the form
const roomName = document.getElementById("room-name").value;
const userName = document.getElementById("participant-name").value;
// Get a token from your application server with the room name and participant name
const token = await getToken(roomName, userName);
// Connect to the room with the LiveKit URL and the token
await room.connect(LIVEKIT_URL, token);
// Hide the 'Join room' page and show the 'Room' page
document.getElementById("room-title").innerText = roomName;
document.getElementById("join").hidden = true;
document.getElementById("room").hidden = false;
// Publish your camera and microphone
await room.localParticipant.enableCameraAndMicrophone();
const localVideoTrack = this.room.localParticipant.videoTrackPublications.values().next().value.track;
addTrack(localVideoTrack, userName, true);
// Update recording info
await updateRecordingInfo(room.isRecording);
} catch (error) {
console.log("There was an error connecting to the room:", error.message);
await leaveRoom();
// Specify the actions when events take place in the room
// On every new Track received...
room.on(
LivekitClient.RoomEvent.TrackSubscribed,
(track, _publication, participant) => {
addTrack(track, participant.identity);
}
);
// On every new Track destroyed...
room.on(
LivekitClient.RoomEvent.TrackUnsubscribed,
(track, _publication, participant) => {
track.detach();
document.getElementById(track.sid)?.remove();
if (track.kind === "video") {
removeVideoContainer(participant.identity);
}
}
);
// When recording status changes...
room.on(
LivekitClient.RoomEvent.RecordingStatusChanged,
async (isRecording) => {
await updateRecordingInfo(isRecording);
}
);
try {
// Get the room name and participant name from the form
const roomName = document.getElementById("room-name").value;
const userName = document.getElementById("participant-name").value;
// Get a token from your application server with the room name and participant name
const token = await getToken(roomName, userName);
// Connect to the room with the LiveKit URL and the token
await room.connect(LIVEKIT_URL, token);
// Hide the 'Join room' page and show the 'Room' page
document.getElementById("room-title").innerText = roomName;
document.getElementById("join").hidden = true;
document.getElementById("room").hidden = false;
// Publish your camera and microphone
await room.localParticipant.enableCameraAndMicrophone();
const localVideoTrack = this.room.localParticipant.videoTrackPublications
.values()
.next().value.track;
addTrack(localVideoTrack, userName, true);
// Update recording info
await updateRecordingInfo(room.isRecording);
} catch (error) {
console.log("There was an error connecting to the room:", error.message);
await leaveRoom();
}
}
function addTrack(track, participantIdentity, local = false) {
const element = track.attach();
element.id = track.sid;
const element = track.attach();
element.id = track.sid;
/* If the track is a video track, we create a container and append the video element to it
/* If the track is a video track, we create a container and append the video element to it
with the participant's identity */
if (track.kind === "video") {
const videoContainer = createVideoContainer(participantIdentity, local);
videoContainer.append(element);
appendParticipantData(videoContainer, participantIdentity + (local ? " (You)" : ""));
} else {
document.getElementById("layout-container").append(element);
}
if (track.kind === "video") {
const videoContainer = createVideoContainer(participantIdentity, local);
videoContainer.append(element);
appendParticipantData(
videoContainer,
participantIdentity + (local ? " (You)" : "")
);
} else {
document.getElementById("layout-container").append(element);
}
}
async function leaveRoom() {
// Leave the room by calling 'disconnect' method over the Room object
await room.disconnect();
// Leave the room by calling 'disconnect' method over the Room object
await room.disconnect();
// Remove all HTML elements inside the layout container
removeAllLayoutElements();
// Remove all HTML elements inside the layout container
removeAllLayoutElements();
// Remove all recordings from the list
removeAllRecordings();
// Remove all recordings from the list
removeAllRecordings();
// Reset recording state
document.getElementById("recording-button").disabled = false;
document.getElementById("recording-button").innerText = "Start Recording";
document.getElementById("recording-button").className = "btn btn-primary";
document.getElementById("recording-text").hidden = true;
// Reset recording state
document.getElementById("recording-button").disabled = false;
document.getElementById("recording-button").innerText = "Start Recording";
document.getElementById("recording-button").className = "btn btn-primary";
document.getElementById("recording-text").hidden = true;
// Back to 'Join room' page
document.getElementById("join").hidden = false;
document.getElementById("room").hidden = true;
// Back to 'Join room' page
document.getElementById("join").hidden = false;
document.getElementById("room").hidden = true;
// Enable 'Join' button
document.getElementById("join-button").disabled = false;
document.getElementById("join-button").innerText = "Join!";
// Enable 'Join' button
document.getElementById("join-button").disabled = false;
document.getElementById("join-button").innerText = "Join!";
}
window.onbeforeunload = () => {
room?.disconnect();
room?.disconnect();
};
document.addEventListener("DOMContentLoaded", async function () {
var currentPage = window.location.pathname;
var currentPage = window.location.pathname;
if (currentPage === "/recordings.html") {
await listRecordings();
} else {
generateFormValues();
}
if (currentPage === "/recordings.html") {
await listRecordings();
} else {
generateFormValues();
}
// Remove recording video when the dialog is closed
document.getElementById("recording-video-dialog").addEventListener("close", () => {
const recordingVideo = document.getElementById("recording-video");
recordingVideo.src = "";
// Remove recording video when the dialog is closed
document
.getElementById("recording-video-dialog")
.addEventListener("close", () => {
const recordingVideo = document.getElementById("recording-video");
recordingVideo.src = "";
});
});
function generateFormValues() {
document.getElementById("room-name").value = "Test Room";
document.getElementById("participant-name").value = "Participant" + Math.floor(Math.random() * 100);
document.getElementById("room-name").value = "Test Room";
document.getElementById("participant-name").value =
"Participant" + Math.floor(Math.random() * 100);
}
function createVideoContainer(participantIdentity, local = false) {
const videoContainer = document.createElement("div");
videoContainer.id = `camera-${participantIdentity}`;
videoContainer.className = "video-container";
const layoutContainer = document.getElementById("layout-container");
const videoContainer = document.createElement("div");
videoContainer.id = `camera-${participantIdentity}`;
videoContainer.className = "video-container";
const layoutContainer = document.getElementById("layout-container");
if (local) {
layoutContainer.prepend(videoContainer);
} else {
layoutContainer.append(videoContainer);
}
if (local) {
layoutContainer.prepend(videoContainer);
} else {
layoutContainer.append(videoContainer);
}
return videoContainer;
return videoContainer;
}
function appendParticipantData(videoContainer, participantIdentity) {
const dataElement = document.createElement("div");
dataElement.className = "participant-data";
dataElement.innerHTML = `<p>${participantIdentity}</p>`;
videoContainer.prepend(dataElement);
const dataElement = document.createElement("div");
dataElement.className = "participant-data";
dataElement.innerHTML = `<p>${participantIdentity}</p>`;
videoContainer.prepend(dataElement);
}
function removeVideoContainer(participantIdentity) {
const videoContainer = document.getElementById(`camera-${participantIdentity}`);
videoContainer?.remove();
const videoContainer = document.getElementById(
`camera-${participantIdentity}`
);
videoContainer?.remove();
}
function removeAllLayoutElements() {
const layoutElements = document.getElementById("layout-container").children;
Array.from(layoutElements).forEach((element) => {
element.remove();
});
const layoutElements = document.getElementById("layout-container").children;
Array.from(layoutElements).forEach((element) => {
element.remove();
});
}
/**
@ -188,156 +207,153 @@ function removeAllLayoutElements() {
* access to the endpoints.
*/
async function getToken(roomName, participantName) {
const [error, body] = await httpRequest("POST", "/token", {
roomName,
participantName
});
const [error, body] = await httpRequest("POST", "/token", {
roomName,
participantName,
});
if (error) {
throw new Error(`Failed to get token: ${error.message}`);
}
if (error) {
throw new Error(`Failed to get token: ${error.message}`);
}
return body.token;
return body.token;
}
async function updateRecordingInfo(isRecording) {
const recordingButton = document.getElementById("recording-button");
const recordingText = document.getElementById("recording-text");
const recordingButton = document.getElementById("recording-button");
const recordingText = document.getElementById("recording-text");
if (isRecording) {
recordingButton.disabled = false;
recordingButton.innerText = "Stop Recording";
recordingButton.className = "btn btn-danger";
recordingText.hidden = false;
} else {
recordingButton.disabled = false;
recordingButton.innerText = "Start Recording";
recordingButton.className = "btn btn-primary";
recordingText.hidden = true;
}
if (isRecording) {
recordingButton.disabled = false;
recordingButton.innerText = "Stop Recording";
recordingButton.className = "btn btn-danger";
recordingText.hidden = false;
} else {
recordingButton.disabled = false;
recordingButton.innerText = "Start Recording";
recordingButton.className = "btn btn-primary";
recordingText.hidden = true;
}
const roomId = await room.getSid();
await listRecordings(room.name, roomId);
const roomId = await room.getSid();
await listRecordings(roomId);
}
async function manageRecording() {
const recordingButton = document.getElementById("recording-button");
const recordingButton = document.getElementById("recording-button");
if (recordingButton.innerText === "Start Recording") {
recordingButton.disabled = true;
recordingButton.innerText = "Starting...";
if (recordingButton.innerText === "Start Recording") {
recordingButton.disabled = true;
recordingButton.innerText = "Starting...";
const [error, _] = await startRecording();
const [error, _] = await startRecording();
if (error && error.status !== 409) {
recordingButton.disabled = false;
recordingButton.innerText = "Start Recording";
}
} else {
recordingButton.disabled = true;
recordingButton.innerText = "Stopping...";
const [error, _] = await stopRecording();
if (error && error.status !== 409) {
recordingButton.disabled = false;
recordingButton.innerText = "Stop Recording";
}
if (error && error.status !== 409) {
recordingButton.disabled = false;
recordingButton.innerText = "Start Recording";
}
} else {
recordingButton.disabled = true;
recordingButton.innerText = "Stopping...";
const [error, _] = await stopRecording();
if (error && error.status !== 409) {
recordingButton.disabled = false;
recordingButton.innerText = "Stop Recording";
}
}
}
async function startRecording() {
return httpRequest("POST", "/recordings/start", {
roomName: room.name
});
return httpRequest("POST", "/recordings/start", {
roomName: room.name,
});
}
async function stopRecording() {
return httpRequest("POST", "/recordings/stop", {
roomName: room.name
});
return httpRequest("POST", "/recordings/stop", {
roomName: room.name,
});
}
async function deleteRecording(recordingName) {
const [error, _] = await httpRequest("DELETE", `/recordings/${recordingName}`);
const [error, _] = await httpRequest(
"DELETE",
`/recordings/${recordingName}`
);
if (!error || error.status === 404) {
const roomId = await room?.getSid();
await listRecordings(room?.name, roomId);
}
if (!error || error.status === 404) {
const roomId = await room?.getSid();
await listRecordings(roomId);
}
}
async function listRecordings(roomName, roomId) {
const url = "/recordings" + (roomName ? `?roomName=${roomName}` + (roomId ? `&roomId=${roomId}` : "") : "");
const [error, body] = await httpRequest("GET", url);
async function listRecordings(roomId) {
let url = "/recordings";
if (roomId) {
url += `?roomId=${roomId}`;
}
const [error, body] = await httpRequest("GET", url);
if (!error) {
const recordings = body.recordings;
showRecordingList(recordings);
}
}
async function listRecordingsByRoom() {
const roomName = document.getElementById("room-name").value;
await listRecordings(roomName);
if (!error) {
const recordings = body.recordings;
showRecordingList(recordings);
}
}
async function httpRequest(method, url, body) {
try {
const response = await fetch(url, {
method,
headers: {
"Content-Type": "application/json"
},
body: method !== "GET" ? JSON.stringify(body) : undefined
});
try {
const response = await fetch(url, {
method,
headers: {
"Content-Type": "application/json",
},
body: method !== "GET" ? JSON.stringify(body) : undefined,
});
const responseBody = await response.json();
const responseBody = await response.json();
if (!response.ok) {
console.error(responseBody.errorMessage);
const error = {
status: response.status,
message: responseBody.errorMessage
};
return [error, undefined];
}
return [undefined, responseBody];
} catch (error) {
console.error(error.message);
const errorObj = {
status: 0,
message: error.message
};
return [errorObj, undefined];
if (!response.ok) {
console.error(responseBody.errorMessage);
const error = {
status: response.status,
message: responseBody.errorMessage,
};
return [error, undefined];
}
return [undefined, responseBody];
} catch (error) {
console.error(error.message);
const errorObj = {
status: 0,
message: error.message,
};
return [errorObj, undefined];
}
}
function showRecordingList(recordings) {
const recordingsList = document.getElementById("recording-list");
const recordingsList = document.getElementById("recording-list");
if (recordings.length === 0) {
recordingsList.innerHTML = "<span>There are no recordings available</span>";
} else {
recordingsList.innerHTML = "";
}
if (recordings.length === 0) {
recordingsList.innerHTML = "<span>There are no recordings available</span>";
} else {
recordingsList.innerHTML = "";
}
recordings.forEach((recording) => {
const recordingName = recording.name;
const recordingSize = formatBytes(recording.size ?? 0);
const recordingDate = new Date(recording.startedAt).toLocaleString();
recordings.forEach((recording) => {
const recordingName = recording.name;
const recordingContainer = document.createElement("div");
recordingContainer.className = "recording-container";
recordingContainer.id = recordingName;
const recordingContainer = document.createElement("div");
recordingContainer.className = "recording-container";
recordingContainer.id = recordingName;
recordingContainer.innerHTML = `
recordingContainer.innerHTML = `
<i class="fa-solid fa-file-video"></i>
<div class="recording-info">
<p class="recording-name">${recordingName}</p>
<p class="recording-size">${recordingSize}</p>
<p class="recording-date">${recordingDate}</p>
</div>
<div class="recording-actions">
<button title="Play" class="icon-button" onclick="displayRecording('${recordingName}')">
@ -349,38 +365,29 @@ function showRecordingList(recordings) {
</div>
`;
recordingsList.append(recordingContainer);
});
recordingsList.append(recordingContainer);
});
}
function displayRecording(recordingName) {
const recordingVideoDialog = document.getElementById("recording-video-dialog");
recordingVideoDialog.showModal();
const recordingVideo = document.getElementById("recording-video");
recordingVideo.src = `/recordings/${recordingName}`;
const recordingVideoDialog = document.getElementById(
"recording-video-dialog"
);
recordingVideoDialog.showModal();
const recordingVideo = document.getElementById("recording-video");
recordingVideo.src = `/recordings/${recordingName}`;
}
function closeRecording() {
const recordingVideoDialog = document.getElementById("recording-video-dialog");
recordingVideoDialog.close();
const recordingVideoDialog = document.getElementById(
"recording-video-dialog"
);
recordingVideoDialog.close();
}
function removeAllRecordings() {
const recordingList = document.getElementById("recording-list").children;
Array.from(recordingList).forEach((recording) => {
recording.remove();
});
}
function formatBytes(bytes) {
if (bytes === 0) {
return "0Bytes";
}
const k = 1024;
const sizes = ["Bytes", "KB", "MB", "GB"];
const i = Math.floor(Math.log(bytes) / Math.log(k));
const decimals = i < 2 ? 0 : 1;
return (bytes / Math.pow(k, i)).toFixed(decimals) + sizes[i];
const recordingList = document.getElementById("recording-list").children;
Array.from(recordingList).forEach((recording) => {
recording.remove();
});
}

View File

@ -56,18 +56,6 @@
<main>
<div id="recordings-all">
<div id="actions">
<button id="refresh-button" title="Refresh" class="icon-button" onclick="listRecordings()">
<i class="fas fa-sync-alt"></i>
</button>
<form onsubmit="listRecordingsByRoom(); return false">
<input id="room-name" type="text" placeholder="Room name" />
<button title="Search" type="submit">
<i class="fas fa-search"></i>
</button>
</form>
</div>
<h2>Recordings</h2>
<div id="recording-list"></div>
<dialog id="recording-video-dialog">

View File

@ -3,19 +3,35 @@ import express from "express";
import cors from "cors";
import path from "path";
import { fileURLToPath } from "url";
import { AccessToken, EgressClient, EncodedFileOutput, EncodedFileType, WebhookReceiver } from "livekit-server-sdk";
import {
AccessToken,
EgressClient,
EncodedFileOutput,
EncodedFileType,
WebhookReceiver,
} from "livekit-server-sdk";
import { S3Service } from "./s3.service.js";
// Configuration
const SERVER_PORT = process.env.SERVER_PORT || 6080;
// LiveKit configuration
const LIVEKIT_URL = process.env.LIVEKIT_URL || "http://localhost:7880";
const LIVEKIT_API_KEY = process.env.LIVEKIT_API_KEY || "devkey";
const LIVEKIT_API_SECRET = process.env.LIVEKIT_API_SECRET || "secret";
const RECORDINGS_PATH = process.env.RECORDINGS_PATH ?? "recordings/";
const RECORDING_FILE_PORTION_SIZE = 5 * 1024 * 1024; // 5MB
// Initialize services
const egressClient = new EgressClient(
LIVEKIT_URL,
LIVEKIT_API_KEY,
LIVEKIT_API_SECRET
);
const webhookReceiver = new WebhookReceiver(
LIVEKIT_API_KEY,
LIVEKIT_API_SECRET
);
const s3Service = new S3Service();
const app = express();
app.use(cors());
@ -27,234 +43,220 @@ const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
app.use(express.static(path.join(__dirname, "../public")));
// Generate access tokens for participants to join a room
app.post("/token", async (req, res) => {
const roomName = req.body.roomName;
const participantName = req.body.participantName;
const roomName = req.body.roomName;
const participantName = req.body.participantName;
if (!roomName || !participantName) {
res.status(400).json({ errorMessage: "roomName and participantName are required" });
return;
}
if (!roomName || !participantName) {
return res
.status(400)
.json({ errorMessage: "roomName and participantName are required" });
}
const at = new AccessToken(LIVEKIT_API_KEY, LIVEKIT_API_SECRET, {
identity: participantName
});
at.addGrant({ roomJoin: true, room: roomName, roomRecord: true });
const token = await at.toJwt();
res.json({ token });
const at = new AccessToken(LIVEKIT_API_KEY, LIVEKIT_API_SECRET, {
identity: participantName,
});
at.addGrant({ roomJoin: true, room: roomName, roomRecord: true });
const token = await at.toJwt();
return res.json({ token });
});
const webhookReceiver = new WebhookReceiver(LIVEKIT_API_KEY, LIVEKIT_API_SECRET);
// Receive webhooks from LiveKit Server
app.post("/livekit/webhook", async (req, res) => {
try {
const event = await webhookReceiver.receive(req.body, req.get("Authorization"));
console.log(event);
} catch (error) {
console.error("Error validating webhook event.", error);
}
res.status(200).send();
try {
const event = await webhookReceiver.receive(
req.body,
req.get("Authorization")
);
console.log(event);
} catch (error) {
console.error("Error validating webhook event.", error);
}
return res.status(200).send();
});
const egressClient = new EgressClient(LIVEKIT_URL, LIVEKIT_API_KEY, LIVEKIT_API_SECRET);
const s3Service = new S3Service();
// Start a recording
app.post("/recordings/start", async (req, res) => {
const { roomName } = req.body;
const { roomName } = req.body;
if (!roomName) {
res.status(400).json({ errorMessage: "roomName is required" });
return;
}
if (!roomName) {
return res.status(400).json({ errorMessage: "roomName is required" });
}
const activeRecording = await getActiveRecordingByRoom(roomName);
const activeRecording = await getActiveRecordingByRoom(roomName);
// Check if there is already an active recording for this room
if (activeRecording) {
res.status(409).json({ errorMessage: "Recording already started for this room" });
return;
}
// Check if there is already an active recording for this room
if (activeRecording) {
return res
.status(409)
.json({ errorMessage: "Recording already started for this room" });
}
// Use the EncodedFileOutput to save the recording to an MP4 file
const fileOutput = new EncodedFileOutput({
fileType: EncodedFileType.MP4,
filepath: `${RECORDINGS_PATH}{room_name}-{room_id}-{time}`
// Use the EncodedFileOutput to save the recording to an MP4 file
// The room name, time and room ID in the file path help to organize the recordings
const fileOutput = new EncodedFileOutput({
fileType: EncodedFileType.MP4,
filepath: `${RECORDINGS_PATH}/{room_name}-{time}-{room_id}`,
disableManifest: true,
});
try {
// Start a RoomCompositeEgress to record all participants in the room
const egressInfo = await egressClient.startRoomCompositeEgress(roomName, {
file: fileOutput,
});
try {
// Start a RoomCompositeEgress to record all participants in the room
const egressInfo = await egressClient.startRoomCompositeEgress(roomName, { file: fileOutput });
const recording = {
name: egressInfo.fileResults[0].filename.split("/").pop(),
startedAt: Number(egressInfo.startedAt) / 1_000_000
};
res.json({ message: "Recording started", recording });
} catch (error) {
console.error("Error starting recording.", error);
res.status(500).json({ errorMessage: "Error starting recording" });
}
const recording = {
name: egressInfo.fileResults[0].filename.split("/").pop(),
startedAt: Number(egressInfo.startedAt) / 1_000_000,
};
res.json({ message: "Recording started", recording });
} catch (error) {
console.error("Error starting recording.", error);
res.status(500).json({ errorMessage: "Error starting recording" });
}
});
// Stop a recording
app.post("/recordings/stop", async (req, res) => {
const { roomName } = req.body;
const { roomName } = req.body;
if (!roomName) {
res.status(400).json({ errorMessage: "roomName is required" });
return;
}
if (!roomName) {
return res.status(400).json({ errorMessage: "roomName is required" });
}
const activeRecording = await getActiveRecordingByRoom(roomName);
const activeRecording = await getActiveRecordingByRoom(roomName);
// Check if there is an active recording for this room
if (!activeRecording) {
res.status(409).json({ errorMessage: "Recording not started for this room" });
return;
}
// Check if there is an active recording for this room
if (!activeRecording) {
return res
.status(409)
.json({ errorMessage: "Recording not started for this room" });
}
try {
// Stop the egress to finish the recording
const egressInfo = await egressClient.stopEgress(activeRecording);
const file = egressInfo.fileResults[0];
const recording = {
name: file.filename.split("/").pop(),
startedAt: Number(egressInfo.startedAt) / 1_000_000,
size: Number(file.size)
};
res.json({ message: "Recording stopped", recording });
} catch (error) {
console.error("Error stopping recording.", error);
res.status(500).json({ errorMessage: "Error stopping recording" });
}
try {
// Stop the egress to finish the recording
const egressInfo = await egressClient.stopEgress(activeRecording);
const file = egressInfo.fileResults[0];
const recording = {
name: file.filename.split("/").pop(),
};
return res.json({ message: "Recording stopped", recording });
} catch (error) {
console.error("Error stopping recording.", error);
return res.status(500).json({ errorMessage: "Error stopping recording" });
}
});
// List recordings
app.get("/recordings", async (req, res) => {
const roomId = req.query.roomId?.toString();
try {
const awsResponse = await s3Service.listObjects(RECORDINGS_PATH);
let recordings = [];
if (awsResponse.Contents) {
recordings = awsResponse.Contents.map((recording) => {
return {
name: recording.Key.split("/").pop(),
};
});
}
// Filter recordings by room ID
recordings = recordings.filter((recording) =>
roomId ? recording.name.includes(roomId) : true
);
return res.json({ recordings });
} catch (error) {
console.error("Error listing recordings.", error);
return res.status(500).json({ errorMessage: "Error listing recordings" });
}
});
// Play a recording
app.get("/recordings/:recordingName", async (req, res) => {
const { recordingName } = req.params;
const { range } = req.headers;
const key = RECORDINGS_PATH + recordingName;
const exists = await s3Service.exists(key);
if (!exists) {
return res.status(404).json({ errorMessage: "Recording not found" });
}
try {
// Get the recording file from S3
const { stream, size, start, end } = await getRecordingStream(
recordingName,
range
);
// Set response headers
res.status(206);
res.setHeader("Cache-Control", "no-cache");
res.setHeader("Content-Type", "video/mp4");
res.setHeader("Accept-Ranges", "bytes");
res.setHeader("Content-Range", `bytes ${start}-${end}/${size}`);
res.setHeader("Content-Length", end - start + 1);
// Pipe the recording file to the response
stream.pipe(res).on("finish", () => res.end());
} catch (error) {
console.error("Error getting recording.", error);
return res.status(500).json({ errorMessage: "Error getting recording" });
}
});
// Delete a recording
app.delete("/recordings/:recordingName", async (req, res) => {
const { recordingName } = req.params;
const key = RECORDINGS_PATH + recordingName;
const exists = await s3Service.exists(key);
if (!exists) {
return res.status(404).json({ errorMessage: "Recording not found" });
}
try {
// Delete the recording file and metadata file from S3
await Promise.all([s3Service.deleteObject(key)]);
res.json({ message: "Recording deleted" });
} catch (error) {
console.error("Error deleting recording.", error);
res.status(500).json({ errorMessage: "Error deleting recording" });
}
});
// Start the server
app.listen(SERVER_PORT, () => {
console.log("Server started on port:", SERVER_PORT);
});
// Helper functions
const getActiveRecordingByRoom = async (roomName) => {
try {
// List all active egresses for the room
const egresses = await egressClient.listEgress({ roomName, active: true });
return egresses.length > 0 ? egresses[0].egressId : null;
} catch (error) {
console.error("Error listing egresses.", error);
return null;
}
try {
// List all active egresses for the room
const egresses = await egressClient.listEgress({ roomName, active: true });
return egresses.length > 0 ? egresses[0].egressId : null;
} catch (error) {
console.error("Error listing egresses.", error);
return null;
}
};
app.get("/recordings", async (req, res) => {
const roomName = req.query.roomName?.toString();
const roomId = req.query.roomId?.toString();
try {
const keyStart = RECORDINGS_PATH + (roomName ? `${roomName}-` + (roomId ? roomId : "") : "");
const keyEnd = ".mp4.json";
const regex = new RegExp(`^${keyStart}.*${keyEnd}$`);
// List all egress metadata files in the recordings path that match the regex
const payloadKeys = await s3Service.listObjects(RECORDINGS_PATH, regex);
const recordings = await Promise.all(payloadKeys.map((payloadKey) => getRecordingInfo(payloadKey)));
const sortedRecordings = filterAndSortRecordings(recordings, roomName, roomId);
res.json({ recordings: sortedRecordings });
} catch (error) {
console.error("Error listing recordings.", error);
res.status(500).json({ errorMessage: "Error listing recordings" });
}
});
const getRecordingInfo = async (payloadKey) => {
// Get the egress metadata file as JSON
const data = await s3Service.getObjectAsJson(payloadKey);
// Get the recording file size
const recordingKey = payloadKey.replace(".json", "");
const size = await s3Service.getObjectSize(recordingKey);
const recordingName = recordingKey.split("/").pop();
const recording = {
id: data.egress_id,
name: recordingName,
roomName: data.room_name,
roomId: data.room_id,
startedAt: Number(data.started_at) / 1000000,
size: size
};
return recording;
};
const filterAndSortRecordings = (recordings, roomName, roomId) => {
let filteredRecordings = recordings;
if (roomName || roomId) {
filteredRecordings = recordings.filter((recording) => {
return (!roomName || recording.roomName === roomName) && (!roomId || recording.roomId === roomId);
});
}
return filteredRecordings.sort((a, b) => b.startedAt - a.startedAt);
};
app.get("/recordings/:recordingName", async (req, res) => {
const { recordingName } = req.params;
const { range } = req.headers;
const key = RECORDINGS_PATH + recordingName;
const exists = await s3Service.exists(key);
if (!exists) {
res.status(404).json({ errorMessage: "Recording not found" });
return;
}
try {
// Get the recording file from S3
const { stream, size, start, end } = await getRecordingStream(recordingName, range);
// Set response headers
res.status(206);
res.setHeader("Cache-Control", "no-cache");
res.setHeader("Content-Type", "video/mp4");
res.setHeader("Accept-Ranges", "bytes");
res.setHeader("Content-Range", `bytes ${start}-${end}/${size}`);
res.setHeader("Content-Length", end - start + 1);
// Pipe the recording file to the response
stream.pipe(res).on("finish", () => res.end());
} catch (error) {
console.error("Error getting recording.", error);
res.status(500).json({ errorMessage: "Error getting recording" });
}
});
const getRecordingStream = async (recordingName, range) => {
const key = RECORDINGS_PATH + recordingName;
const size = await s3Service.getObjectSize(key);
const key = RECORDINGS_PATH + recordingName;
const size = await s3Service.getObjectSize(key);
// Get the requested range
const parts = range?.replace(/bytes=/, "").split("-");
const start = range ? parseInt(parts[0], 10) : 0;
const endRange = parts[1] ? parseInt(parts[1], 10) : start + RECORDING_FILE_PORTION_SIZE;
const end = Math.min(endRange, size - 1);
// Get the requested range
const parts = range?.replace(/bytes=/, "").split("-");
const start = range ? parseInt(parts[0], 10) : 0;
const endRange = parts[1]
? parseInt(parts[1], 10)
: start + RECORDING_FILE_PORTION_SIZE;
const end = Math.min(endRange, size - 1);
const stream = await s3Service.getObject(key, { start, end });
return { stream, size, start, end };
const stream = await s3Service.getObject(key, { start, end });
return { stream, size, start, end };
};
app.delete("/recordings/:recordingName", async (req, res) => {
const { recordingName } = req.params;
const key = RECORDINGS_PATH + recordingName;
const exists = await s3Service.exists(key);
if (!exists) {
res.status(404).json({ errorMessage: "Recording not found" });
return;
}
try {
// Delete the recording file and metadata file from S3
await Promise.all([s3Service.deleteObject(key), s3Service.deleteObject(`${key}.json`)]);
res.json({ message: "Recording deleted" });
} catch (error) {
console.error("Error deleting recording.", error);
res.status(500).json({ errorMessage: "Error deleting recording" });
}
});
app.listen(SERVER_PORT, () => {
console.log("Server started on port:", SERVER_PORT);
});

View File

@ -1,9 +1,9 @@
import {
S3Client,
GetObjectCommand,
DeleteObjectCommand,
ListObjectsV2Command,
HeadObjectCommand
S3Client,
GetObjectCommand,
DeleteObjectCommand,
ListObjectsV2Command,
HeadObjectCommand,
} from "@aws-sdk/client-s3";
// S3 configuration
@ -14,89 +14,80 @@ const AWS_REGION = process.env.AWS_REGION || "us-east-1";
const S3_BUCKET = process.env.S3_BUCKET || "openvidu";
export class S3Service {
static instance;
static instance;
constructor() {
if (S3Service.instance) {
return S3Service.instance;
}
this.s3Client = new S3Client({
endpoint: S3_ENDPOINT,
credentials: {
accessKeyId: S3_ACCESS_KEY,
secretAccessKey: S3_SECRET_KEY
},
region: AWS_REGION,
forcePathStyle: true
});
S3Service.instance = this;
return this;
constructor() {
if (S3Service.instance) {
return S3Service.instance;
}
async exists(key) {
try {
await this.headObject(key);
return true;
} catch (error) {
return false;
}
}
this.s3Client = new S3Client({
endpoint: S3_ENDPOINT,
credentials: {
accessKeyId: S3_ACCESS_KEY,
secretAccessKey: S3_SECRET_KEY,
},
region: AWS_REGION,
forcePathStyle: true,
});
async headObject(key) {
const params = {
Bucket: S3_BUCKET,
Key: key
};
const command = new HeadObjectCommand(params);
return this.run(command);
}
S3Service.instance = this;
return this;
}
async getObjectSize(key) {
const { ContentLength: size } = await this.headObject(key);
return size;
async exists(key) {
try {
await this.headObject(key);
return true;
} catch (error) {
return false;
}
}
async getObject(key, range) {
const params = {
Bucket: S3_BUCKET,
Key: key,
Range: range ? `bytes=${range.start}-${range.end}` : undefined
};
const command = new GetObjectCommand(params);
const { Body: body } = await this.run(command);
return body;
}
async headObject(key) {
const params = {
Bucket: S3_BUCKET,
Key: key,
};
const command = new HeadObjectCommand(params);
return this.run(command);
}
async getObjectAsJson(key) {
const body = await this.getObject(key);
const stringifiedData = await body.transformToString();
return JSON.parse(stringifiedData);
}
async getObjectSize(key) {
const { ContentLength: size } = await this.headObject(key);
return size;
}
async listObjects(prefix, regex) {
const params = {
Bucket: S3_BUCKET,
Prefix: prefix
};
const command = new ListObjectsV2Command(params);
const { Contents: objects } = await this.run(command);
async getObject(key, range) {
const params = {
Bucket: S3_BUCKET,
Key: key,
Range: range ? `bytes=${range.start}-${range.end}` : undefined,
};
const command = new GetObjectCommand(params);
const { Body: body } = await this.run(command);
return body;
}
// Filter objects by regex and return the keys
return objects?.filter((object) => regex.test(object.Key)).map((payload) => payload.Key) ?? [];
}
async listObjects(prefix) {
const params = {
Bucket: S3_BUCKET,
Prefix: prefix,
};
const command = new ListObjectsV2Command(params);
return await this.run(command);
}
async deleteObject(key) {
const params = {
Bucket: S3_BUCKET,
Key: key
};
const command = new DeleteObjectCommand(params);
return this.run(command);
}
async deleteObject(key) {
const params = {
Bucket: S3_BUCKET,
Key: key,
};
const command = new DeleteObjectCommand(params);
return this.run(command);
}
async run(command) {
return this.s3Client.send(command);
}
async run(command) {
return this.s3Client.send(command);
}
}