import { Tube } from '../component' ; import { MessageType , Message } from '../message' ; import { VideoMedia } from '../../utils/protocols/sdp' ; import { jpegDepayFactory } from './parser' ; import { Transform } from 'stream' ; import { payloadType , timestamp , marker } from '../../utils/protocols/rtp' ; export class JPEGDepay extends Tube { constructor ( ) { let jpegPayloadType ; let packets = [ ] ; let jpegDepay ; const incoming = new Transform ( { objectMode : true , transform : function ( msg , encoding , callback ) { if ( msg . type === MessageType . SDP ) { const jpegMedia = msg . sdp . media . find ( ( media ) : media is VideoMedia => { return ( media . type === 'video' && media . rtpmap !== undefined && media . rtpmap . encodingName === 'JPEG' ) ; } ) ; if ( jpegMedia !== undefined && jpegMedia . rtpmap !== undefined ) { jpegPayloadType = Number ( jpegMedia . rtpmap . payloadType ) ; const framesize = jpegMedia . framesize ; if ( framesize !== undefined ) { const [ width , height ] = framesize ; jpegDepay = jpegDepayFactory ( width , height ) ; } else { jpegDepay = jpegDepayFactory ( ) ; } } callback ( undefined , msg ) ; } else if ( msg . type === MessageType . RTP && payloadType ( msg . data ) === jpegPayloadType ) { packets . push ( msg . data ) ; if ( marker ( msg . data ) && packets . length > 0 ) { const jpegFrame = jpegDepay ( packets ) ; this . push ( { timestamp : timestamp ( msg . data ) , ntpTimestamp : msg . ntpTimestamp , payloadType : payloadType ( msg . data ) , data : jpegFrame . data , framesize : jpegFrame . size , type : MessageType . JPEG , } ) ; packets = [ ] ; } callback ( ) ; } else { callback ( undefined , msg ) ; } } , } ) ; super ( incoming ) ; } }