11import { EventEmitter } from "node:events" ;
2- import type { Channel } from "./channel" ;
3- import { getFolder } from "#src/services/resources.ts" ;
4- import type { Folder } from "#src/services/resources.ts" ;
2+ import { getFolder , type Folder } from "#src/services/resources.ts" ;
53import { Logger } from "#src/utils/utils.ts" ;
64
5+ import type { Channel } from "./channel" ;
6+
77export enum RECORDER_STATE {
88 STARTED = "started" ,
9- STOPPED = "stopped" ,
9+ STOPPED = "stopped"
1010}
1111const logger = new Logger ( "RECORDER" ) ;
1212
1313export class Recorder extends EventEmitter {
1414 channel : Channel ;
15- state : RECORDER_STATE = RECORDER_STATE . STOPPED ;
1615 folder : Folder | undefined ;
1716 ffmpeg = null ;
1817 /** Path to which the final recording will be uploaded to */
1918 recordingAddress : string ;
19+ private _state : RECORDER_STATE = RECORDER_STATE . STOPPED ;
2020
2121 constructor ( channel : Channel , recordingAddress : string ) {
2222 super ( ) ;
@@ -26,10 +26,10 @@ export class Recorder extends EventEmitter {
2626
2727 async start ( ) {
2828 if ( this . state === RECORDER_STATE . STOPPED ) {
29- this . folder = getFolder ( ) ;
29+ this . folder = getFolder ( ) ;
3030 this . state = RECORDER_STATE . STARTED ;
31- logger . trace ( "TO IMPLEMENT" ) ;
32- // TODO ffmpeg instance creation for recording to folder.path with proper name, start, build timestamps object
31+ logger . trace ( "TO IMPLEMENT" ) ;
32+ // TODO ffmpeg instance creation for recording to folder.path with proper name, start, build timestamps object
3333 }
3434 this . _record ( ) ;
3535 return { state : this . state } ;
@@ -38,7 +38,11 @@ export class Recorder extends EventEmitter {
3838 async stop ( ) {
3939 if ( this . state === RECORDER_STATE . STARTED ) {
4040 logger . trace ( "TO IMPLEMENT" ) ;
41- await this . folder ! . seal ( "test-name" ) ;
41+ try {
42+ await this . folder ! . seal ( "test-name" ) ;
43+ } catch {
44+ logger . verbose ( "failed to save the recording" ) ; // TODO maybe warn and give more info
45+ }
4246 this . folder = undefined ;
4347 // TODO ffmpeg instance stop, cleanup,
4448 // only resolve promise and switch state when completely ready to start a new recording.
@@ -47,11 +51,24 @@ export class Recorder extends EventEmitter {
4751 return { state : this . state } ;
4852 }
4953
54+ get isRecording ( ) : boolean {
55+ return this . state === RECORDER_STATE . STARTED ;
56+ }
57+
58+ get state ( ) : RECORDER_STATE {
59+ return this . _state ;
60+ }
61+
62+ set state ( state : RECORDER_STATE ) {
63+ this . _state = state ;
64+ this . emit ( "stateChange" , state ) ;
65+ }
66+
5067 /**
5168 * @param video whether we want to record videos or not (will always record audio)
5269 */
5370 _record ( video : boolean = false ) {
54- console . trace ( `TO IMPLEMENT: recording channel ${ this . channel . name } , video: ${ video } ` ) ;
71+ logger . trace ( `TO IMPLEMENT: recording channel ${ this . channel . name } , video: ${ video } ` ) ;
5572 // iterate all producers on all sessions of the channel, create a ffmpeg for each,
5673 // save them on a map by session id+type.
5774 // check if recording for that session id+type is already in progress
0 commit comments