| 127 | * responses using a simple Promise-based API. |
| 128 | */ |
| 129 | export class FFmpegBridge { |
| 130 | private process: FFmpegExporterServer | null = null; |
| 131 | |
| 132 | public constructor( |
| 133 | private readonly ws: WebSocketServer, |
| 134 | private readonly config: ExporterPluginConfig, |
| 135 | ) { |
| 136 | ws.on('revideo:ffmpeg-exporter', this.handleMessage); |
| 137 | } |
| 138 | |
| 139 | private handleMessage = async ({method, data}: BrowserRequest) => { |
| 140 | if (method === 'start') { |
| 141 | try { |
| 142 | this.process = new FFmpegExporterServer({ |
| 143 | ...(data as FFmpegExporterSettings), |
| 144 | ...this.config, |
| 145 | }); |
| 146 | this.respondSuccess(method, await this.process.start()); |
| 147 | } catch (e: any) { |
| 148 | this.respondError(method, e?.message); |
| 149 | } |
| 150 | return; |
| 151 | } |
| 152 | |
| 153 | if (!this.process) { |
| 154 | this.respondError(method, 'The exporting process has not been started.'); |
| 155 | return; |
| 156 | } |
| 157 | |
| 158 | if (!(method in this.process)) { |
| 159 | this.respondError(method, `Unknown method: "${method}".`); |
| 160 | return; |
| 161 | } |
| 162 | |
| 163 | try { |
| 164 | this.respondSuccess(method, await (this.process as any)[method](data)); |
| 165 | } catch (e: any) { |
| 166 | this.respondError(method, e?.message); |
| 167 | } |
| 168 | |
| 169 | if (method === 'kill') { |
| 170 | this.process = null; |
| 171 | return; |
| 172 | } |
| 173 | }; |
| 174 | |
| 175 | private respondSuccess(method: string, data: any = {}) { |
| 176 | this.ws.send('revideo:ffmpeg-exporter-ack', { |
| 177 | status: 'success', |
| 178 | method, |
| 179 | data, |
| 180 | }); |
| 181 | } |
| 182 | |
| 183 | private respondError(method: string, message = 'Unknown error.') { |
| 184 | this.ws.send('revideo:ffmpeg-exporter-ack', { |
| 185 | status: 'error', |
| 186 | method, |
nothing calls this directly
no test coverage detected