import { clContext as nodenCLContext } from 'nodencl' ; import { ClJobs } from '../clJobQueue' ; import { LoadParams } from '../chanLayer' ; import { VideoFormat } from '../config' ; import { FFmpegProducerFactory } from './ffmpegProducer' ; import { MacadamProducerFactory } from './macadamProducer' ; import { RouteProducerFactory } from './routeProducer' ; import { Mixer } from './mixer' ; import { RouteSource } from '../routeSource' ; export type ProducerConfig = { ffmpeg : { [ key ] : unknown ; } ; } ; export interface Producer extends RouteSource { initialise ( ) : void ; getMixer ( ) : Mixer ; setPaused ( pause ) : void ; release ( ) : void ; } export interface ProducerFactory < T extends Producer > { createProducer ( id , params , clJobs , consumerFormat , config ) : T ; } export class InvalidProducerError extends Error { constructor ( message ? ) { super ( message ) ; Object . setPrototypeOf ( this , new . target . prototype ) ; this . name = InvalidProducerError . name ; } } export class ProducerRegistry { private readonly producerFactories : ProducerFactory < Producer > [ ] ; private readonly config : ProducerConfig ; private producerID = 0 ; constructor ( clContext , config ) { this . producerFactories = [ ] ; this . producerFactories . push ( new MacadamProducerFactory ( clContext ) ) ; this . producerFactories . push ( new RouteProducerFactory ( clContext ) ) ; this . producerFactories . push ( new FFmpegProducerFactory ( clContext ) ) ; this . config = config ; } async createSource ( params , consumerFormat , clJobs ) { let producerErr = '' ; for ( const f of this . producerFactories ) { try { const producer = f . createProducer ( this . producerID ++ , params , clJobs , consumerFormat , this . config ) ; await producer . initialise ( ) ; return producer ; } catch ( err ) { if ( err instanceof InvalidProducerError ) producerErr = err . message ; else throw err ; } } if ( producerErr !== '' ) console . log ( producerErr ) ; console . log ( `Failed to find producer for params: '${ params }'` ) ; return undefined ; } }