UNPKG

infobip-rtc

Version:

Infobip RTC JavaScript SDK - Infobip WebRTC API Implementation

87 lines 3.29 kB
import { createFrameProvider } from './frame-provider/FrameProvider'; import { FpsCounter } from "./util/FpsCounter"; import { CallsApiEvent } from "../../../event/CallsApiEvents"; import { ApplicationErrorCode } from "../../../ApplicationErrorCode"; export const START_VIDEO_FILTER_ERROR_DESCRIPTION = 'Error while starting video filter.'; export class DefaultVideoFilterManager { constructor(videoFilter, logger, callId) { this.videoFilter = videoFilter; this.logger = logger; this.callId = callId; this.fpsCounter = new FpsCounter(); } async stop() { if (!this.originalTrack) { return; } await this.videoFilter.stop(); this.frameProvider?.destroy(); this.setActiveTrack(this.originalTrack); delete this.originalTrack; delete this.originalStream; delete this.frameProvider; } getCurrentFramerate() { return this.fpsCounter.fps(); } getVideoFilter() { return this.videoFilter; } configureFramerate(filterFps) { this.frameProvider.configureFramerate(filterFps); } async start(stream, trackIndex, apiEventEmitter) { try { const track = stream.getTracks()[trackIndex]; if (!track) { return null; } if (this.originalTrack) { this.originalTrack.stop(); } this.originalTrack = track; this.originalStream = stream; const width = track.getSettings().width; const height = track.getSettings().height; const sourceFps = track.getSettings().frameRate; this.apiEventEmitter = apiEventEmitter; this.frameProvider = createFrameProvider(track, sourceFps, (frame) => this.onFrameProvided(frame)); this.frameProvider.configureFramerate(sourceFps); this.filteredTrack = await this.videoFilter.start(width, height, sourceFps, this); this.setActiveTrack(this.filteredTrack); this.frameProvider.start(); return this.filteredTrack; } catch (error) { this.logger.error(`Error occurred while attempting to start video filter: ${error}`, this.callId); throw { id: '10103', name: 'MEDIA_ERROR', description: START_VIDEO_FILTER_ERROR_DESCRIPTION }; } } setActiveTrack(track) { if (track !== this.originalTrack) { this.originalStream.removeTrack(this.originalTrack); } else if (track !== this.filteredTrack && !!this.filteredTrack) { this.originalStream.removeTrack(this.filteredTrack); } this.originalStream.addTrack(track); } async onFrameProvided(frame) { try { await this.videoFilter.applyFilter(frame); } catch (error) { this.apiEventEmitter.emit(CallsApiEvent.ERROR, { id: ApplicationErrorCode.MEDIA_ERROR.id, name: ApplicationErrorCode.MEDIA_ERROR.name, description: error?.message || error, }); } this.fpsCounter.tick(); } } //# sourceMappingURL=DefaultVideoFilterManager.js.map