Skip to content

Commit 358ce98

Browse files
[WIP] discuss: folder move
1 parent 886f009 commit 358ce98

File tree

3 files changed

+20
-11
lines changed

3 files changed

+20
-11
lines changed

src/client.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,9 @@ export class SfuClient extends EventTarget {
261261
return stats;
262262
}
263263
async startRecording() {
264+
if (this.state !== SfuClientState.CONNECTED) {
265+
throw new Error("InvalidState");
266+
}
264267
return this._bus?.request(
265268
{
266269
name: CLIENT_REQUEST.START_RECORDING,
@@ -270,6 +273,9 @@ export class SfuClient extends EventTarget {
270273
}
271274

272275
async stopRecording() {
276+
if (this.state !== SfuClientState.CONNECTED) {
277+
throw new Error("InvalidState");
278+
}
273279
return this._bus?.request(
274280
{
275281
name: CLIENT_REQUEST.STOP_RECORDING,

src/models/recorder.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { EventEmitter } from "node:events";
22
import type { Channel } from "./channel";
33
import { getFolder } from "#src/services/resources.ts";
4+
import type { Folder } from "#src/services/resources.ts";
45
import { Logger } from "#src/utils/utils.ts";
56

67
export enum RECORDER_STATE {
@@ -12,8 +13,8 @@ const logger = new Logger("RECORDER");
1213
export class Recorder extends EventEmitter {
1314
channel: Channel;
1415
state: RECORDER_STATE = RECORDER_STATE.STOPPED;
16+
folder: Folder | undefined;
1517
ffmpeg = null;
16-
destPath: string | undefined;
1718
/** Path to which the final recording will be uploaded to */
1819
recordingAddress: string;
1920

@@ -25,14 +26,10 @@ export class Recorder extends EventEmitter {
2526

2627
async start() {
2728
if (this.state === RECORDER_STATE.STOPPED) {
28-
const folder = getFolder();
29-
this.destPath = folder.path;
30-
this.once("stopped", ({ name }) => {
31-
folder.seal(name);
32-
});
29+
this.folder = getFolder();
3330
this.state = RECORDER_STATE.STARTED;
3431
logger.trace("TO IMPLEMENT");
35-
// TODO ffmpeg instance creation for recording to destPath with proper name, start, build timestamps object
32+
// TODO ffmpeg instance creation for recording to folder.path with proper name, start, build timestamps object
3633
}
3734
this._record();
3835
return { state: this.state };
@@ -41,7 +38,8 @@ export class Recorder extends EventEmitter {
4138
async stop() {
4239
if (this.state === RECORDER_STATE.STARTED) {
4340
logger.trace("TO IMPLEMENT");
44-
this.emit("stopped", { name: "test" });
41+
await this.folder!.seal("test-name");
42+
this.folder = undefined;
4543
// TODO ffmpeg instance stop, cleanup,
4644
// only resolve promise and switch state when completely ready to start a new recording.
4745
this.state = RECORDER_STATE.STOPPED;

src/services/resources.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import * as config from "#src/config.ts";
55
import { Logger } from "#src/utils/utils.ts";
66
import { PortLimitReachedError } from "#src/utils/errors.ts";
77
import os from "node:os";
8+
import fs from "node:fs/promises";
9+
import path from "node:path";
810

911
const availablePorts: Set<number> = new Set();
1012
let unique = 1;
@@ -89,15 +91,18 @@ export async function getWorker(): Promise<mediasoup.types.Worker> {
8991
return leastUsedWorker;
9092
}
9193

92-
class Folder {
94+
export class Folder {
9395
path: string;
9496

9597
constructor(path: string) {
9698
this.path = path;
9799
}
98100

99-
seal(name: string) {
100-
console.trace(`TO IMPLEMENT, MOVING TO ${config.recording.directory}/${name}`);
101+
async seal(name: string) {
102+
const destinationPath = path.join(config.recording.directory, name);
103+
await fs.rename(this.path, destinationPath);
104+
this.path = destinationPath;
105+
logger.verbose(`Moved folder from ${this.path} to ${destinationPath}`);
101106
}
102107
}
103108

0 commit comments

Comments
 (0)