import debug from "debug" ; import Event from "rx.mini" ; import * as uuid from "uuid" ; import { RtpHeader , RtpPacket } from "../../../rtp/src" ; import { RTCDtlsTransport } from "../transport/dtls" ; import { Kind } from "../typings/domain" ; import { RTCRtpCodecParameters , RTCRtpHeaderExtensionParameters , RTCRtpParameters , } from "./parameters" ; import { RTCRtpReceiver } from "./rtpReceiver" ; import { RTCRtpSender } from "./rtpSender" ; import { RtpTrack } from "./track" ; const log = debug ( "werift:webrtc:rtpTransceiver" ) ; export class RTCRtpTransceiver { readonly uuid = uuid . v4 ( ) ; readonly onTrack = new Event < [ RtpTrack ] > ( ) ; mid ? : string ; mLineIndex ? : number ; dtlsTransport ? : RTCDtlsTransport ; codecs : RTCRtpCodecParameters [ ] = [ ] ; headerExtensions : RTCRtpHeaderExtensionParameters [ ] = [ ] ; senderParams ? : RTCRtpParameters ; options : Partial < TransceiverOptions > = { } ; inactive = false ; constructor ( public readonly kind , public readonly receiver , public readonly sender , public direction ) { } get msid ( ) { return `${ this . sender . streamId } ${ this . sender . trackId }` ; } addTrack ( track ) { const exist = this . receiver . tracks . find ( ( t ) => { if ( t . rid ) return t . rid === track . rid ; if ( t . ssrc ) return t . ssrc === track . ssrc ; } ) ; if ( ! exist ) { this . receiver . tracks . push ( track ) ; this . onTrack . execute ( track ) ; } } replaceRtp ( header ) { this . sender . replaceRTP ( header ) ; } sendRtp = ( rtp ) => { if ( this . direction === "inactive" ) { log ( "sendRtp" , this . uuid , "direction inactive" ) ; return ; } if ( ! this . senderParams ) { log ( "sendRtp" , this . uuid , "senderParams null" ) ; return ; } this . sender . sendRtp ( rtp , this . senderParams ) ; } ; } export const Directions = [ "sendonly" , "sendrecv" , "recvonly" , "inactive" , ] as const ; export type Direction = typeof Directions [ number ] ; type SimulcastDirection = "send" | "recv" ; export type TransceiverOptions = { simulcast : { direction : SimulcastDirection ; rid : string ; } [ ] ; } ;