import BrowserBehavior from '../browserbehavior/BrowserBehavior' ; import Logger from '../logger/Logger' ; import DefaultTransceiverController from './DefaultTransceiverController' ; export default class SimulcastTransceiverController extends DefaultTransceiverController { static readonly LOW_LEVEL_NAME : string = 'low' ; static readonly MID_LEVEL_NAME : string = 'mid' ; static readonly HIGH_LEVEL_NAME : string = 'hi' ; static readonly NAME_ARR_ASCENDING = [ 'low' , 'mid' , 'hi' ] ; static readonly BITRATE_ARR_ASCENDING = [ 200 , 400 , 1100 ] ; private videoQualityControlParameterMap : Map < string , RTCRtpEncodingParameters > = new Map < string , RTCRtpEncodingParameters > ( ) ; constructor ( logger , browserBehavior ) { super ( logger , browserBehavior ) ; let scale = 4 ; for ( let i = 0 ; i < SimulcastTransceiverController . NAME_ARR_ASCENDING . length ; i ++ ) { const ridName = SimulcastTransceiverController . NAME_ARR_ASCENDING [ i ] ; this . videoQualityControlParameterMap . set ( ridName , { rid : ridName , scaleResolutionDownBy : scale , maxBitrate : SimulcastTransceiverController . BITRATE_ARR_ASCENDING [ i ] * 1000 , } ) ; scale = scale / 2 ; } } async setEncodingParameters ( encodingParamMap ) { if ( ! this . _localCameraTransceiver || this . _localCameraTransceiver . direction !== 'sendrecv' ) { return ; } const sender = this . _localCameraTransceiver . sender ; const newEncodingParams = Array . from ( encodingParamMap . values ( ) ) ; if ( newEncodingParams . length <= 0 ) { return ; } const oldParam = sender . getParameters ( ) ; if ( ! oldParam . encodings ) { oldParam . encodings = newEncodingParams ; } else { for ( let i = 0 ; i < oldParam . encodings . length ; i ++ ) { if ( oldParam . encodings [ i ] . rid === SimulcastTransceiverController . LOW_LEVEL_NAME ) { oldParam . encodings [ i ] . maxBitrate = encodingParamMap . get ( SimulcastTransceiverController . LOW_LEVEL_NAME ) . maxBitrate ; oldParam . encodings [ i ] . active = encodingParamMap . get ( SimulcastTransceiverController . LOW_LEVEL_NAME ) . active ; oldParam . encodings [ i ] . scaleResolutionDownBy = encodingParamMap . get ( SimulcastTransceiverController . LOW_LEVEL_NAME ) . scaleResolutionDownBy ; } if ( oldParam . encodings [ i ] . rid === SimulcastTransceiverController . MID_LEVEL_NAME ) { oldParam . encodings [ i ] . maxBitrate = encodingParamMap . get ( SimulcastTransceiverController . MID_LEVEL_NAME ) . maxBitrate ; oldParam . encodings [ i ] . active = encodingParamMap . get ( SimulcastTransceiverController . MID_LEVEL_NAME ) . active ; oldParam . encodings [ i ] . scaleResolutionDownBy = encodingParamMap . get ( SimulcastTransceiverController . MID_LEVEL_NAME ) . scaleResolutionDownBy ; } if ( oldParam . encodings [ i ] . rid === SimulcastTransceiverController . HIGH_LEVEL_NAME ) { oldParam . encodings [ i ] . maxBitrate = encodingParamMap . get ( SimulcastTransceiverController . HIGH_LEVEL_NAME ) . maxBitrate ; oldParam . encodings [ i ] . active = encodingParamMap . get ( SimulcastTransceiverController . HIGH_LEVEL_NAME ) . active ; oldParam . encodings [ i ] . scaleResolutionDownBy = encodingParamMap . get ( SimulcastTransceiverController . HIGH_LEVEL_NAME ) . scaleResolutionDownBy ; } } } await sender . setParameters ( oldParam ) ; this . logVideoTransceiverParameters ( ) ; } static async replaceAudioTrackForSender ( sender , track ) { if ( ! sender ) { return false ; } await sender . replaceTrack ( track ) ; return true ; } async setVideoSendingBitrateKbps ( _bitrateKbps ) { return ; } setupLocalTransceivers ( ) { if ( ! this . useTransceivers ( ) ) { return ; } if ( ! this . defaultMediaStream && typeof MediaStream !== 'undefined' ) { this . defaultMediaStream = new MediaStream ( ) ; } if ( ! this . _localAudioTransceiver ) { this . _localAudioTransceiver = this . peer . addTransceiver ( 'audio' , { direction : 'inactive' , streams : [ this . defaultMediaStream ] , } ) ; } if ( ! this . _localCameraTransceiver ) { const encodingParams = Array . from ( this . videoQualityControlParameterMap . values ( ) ) ; this . _localCameraTransceiver = this . peer . addTransceiver ( 'video' , { direction : 'inactive' , streams : [ this . defaultMediaStream ] , sendEncodings : encodingParams , } ) ; } } private logVideoTransceiverParameters ( ) { const params = this . _localCameraTransceiver . sender . getParameters ( ) ; const encodings = params . encodings ; let msg = 'simulcast: current encoding parameters \n' ; for ( const encodingParam of encodings ) { msg += `rid=${ encodingParam . rid } maxBitrate=${ encodingParam . maxBitrate } active=${ encodingParam . active } scaleDownBy=${ encodingParam . scaleResolutionDownBy } \n` ; } this . logger . info ( msg ) ; } }