@webex/internal-media-core 2.4.0 → 2.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/index.js +2 -2
- package/dist/esm/index.js +2 -2
- package/package.json +2 -2
package/dist/cjs/index.js
CHANGED
|
@@ -2898,7 +2898,7 @@ for(i=listeners.length-1;i>=0;i--){this.removeListener(type,listeners[i]);}}retu
|
|
|
2898
2898
|
// EventEmitters, we do not listen for `error` events here.
|
|
2899
2899
|
emitter.addEventListener(name,function wrapListener(arg){// IE does not have builtin `{ once: true }` support so we
|
|
2900
2900
|
// have to do it manually.
|
|
2901
|
-
if(flags.once){emitter.removeEventListener(name,wrapListener);}listener(arg);});}else {throw new TypeError('The "emitter" argument must be of type EventEmitter. Received type '+typeof emitter);}}class EventEmitter$2 extends events$1.exports.EventEmitter{}class IngressSdpMunger{constructor(){this.ssrc=generateSsrc();}getReceiverId(){return Object.assign({ssrc:this.ssrc},this.rtxSsrc?{rtxSsrc:this.rtxSsrc}:{});}mungeLocalDescription(mediaDescription,options){retainCodecs(mediaDescription,['h264','opus','rtx']);if(mediaDescription.codecs.size===0){logErrorAndThrow(exports.WcmeErrorType.SDP_MUNGE_MISSING_CODECS,"No codecs present in m-line with MID ".concat(mediaDescription.mid," after filtering."));}removeMidRidExtensions(mediaDescription);if(options.twccDisabled){disableTwcc(mediaDescription);}}mungeRemoteDescription(mediaDescription){if(!mediaDescription.ssrcs.length){mediaDescription.addLine(new SsrcLine$1(this.ssrc,'cname',"".concat(this.ssrc,"-cname")));mediaDescription.addLine(new SsrcLine$1(this.ssrc,'msid','-',"".concat(mediaDescription.mid)));if(hasCodec('rtx',mediaDescription)){if(!this.rtxSsrc){this.rtxSsrc=generateSsrc();}mediaDescription.addLine(new SsrcLine$1(this.rtxSsrc,'cname',"".concat(this.ssrc,"-cname")));mediaDescription.addLine(new SsrcLine$1(this.rtxSsrc,'msid','-',"".concat(mediaDescription.mid)));mediaDescription.addLine(new SsrcGroupLine$1('FID',[this.ssrc,this.rtxSsrc]));}}if(retainCandidates(mediaDescription,['udp','tcp'])){logger.log("Some unsupported remote candidates have been removed from mid ".concat(mediaDescription.mid));}[...mediaDescription.codecs.values()].forEach(ci=>{ci.fmtParams.set('x-google-start-bitrate','60000');});}reset(){this.ssrc=generateSsrc();}}class MidPredictor{constructor(){this.currentMid=0;this.midMap=new Map();}getNextMid(mediaType){var mid=this.currentMid++;var mids=this.midMap.get(mediaType)||[];mids.push("".concat(mid));this.midMap.set(mediaType,mids);return "".concat(mid);}allocateMidForDatachannel(){this.currentMid+=1;}reset(){this.midMap=new Map();this.currentMid=0;}getMidMap(){return this.midMap;}}var OveruseState;(function(OveruseState){OveruseState[OveruseState["NOT_OVERUSED"]=0]="NOT_OVERUSED";OveruseState[OveruseState["OVERUSED"]=1]="OVERUSED";})(OveruseState||(OveruseState={}));class OveruseStateManager{constructor(callback){this.monitors=[];this.lastOverallOveruseState=OveruseState.NOT_OVERUSED;this.isRunning=false;this.overuseUpdateCallback=callback;}addMonitor(monitor){this.monitors.push(monitor);if(this.isRunning){monitor.startMonitoring(()=>this.onMonitorOveruseUpdate());}}start(){this.isRunning=true;this.monitors.forEach(monitor=>monitor.startMonitoring(()=>this.onMonitorOveruseUpdate()));}stop(){this.isRunning=false;this.monitors.forEach(monitor=>monitor.stopMonitoring());}onMonitorOveruseUpdate(){var overuseStates=this.monitors.map(monitor=>monitor.getLastOveruseState());var overallOveruseState=overuseStates.some(overuseState=>overuseState===OveruseState.OVERUSED)?OveruseState.OVERUSED:OveruseState.NOT_OVERUSED;if(overallOveruseState!==this.lastOverallOveruseState){this.lastOverallOveruseState=overallOveruseState;this.overuseUpdateCallback(overallOveruseState);}}}exports.ReceiveSlotEvents = void 0;(function(ReceiveSlotEvents){ReceiveSlotEvents["MediaStarted"]="media-started";ReceiveSlotEvents["MediaStopped"]="media-stopped";ReceiveSlotEvents["MediaEnded"]="media-ended";ReceiveSlotEvents["SourceUpdate"]="source-update";})(exports.ReceiveSlotEvents||(exports.ReceiveSlotEvents={}));class ReceiveSlot extends EventEmitter$2{constructor(idGetter,track){super();this._idGetter=idGetter;this.handleStreamMediaStateChange=this.handleStreamMediaStateChange.bind(this);this.handleStreamEnded=this.handleStreamEnded.bind(this);this._stream=new RemoteStream(new MediaStream([track]));this._sourceState='no source';this._stream.on(exports.RemoteStreamEventNames.MediaStateChange,this.handleStreamMediaStateChange);this._stream.on(exports.StreamEventNames.Ended,this.handleStreamEnded);}handleStreamMediaStateChange(state){if(state===RemoteMediaState.Stopped){this.emit(exports.ReceiveSlotEvents.MediaStopped);}else {this.emit(exports.ReceiveSlotEvents.MediaStarted);}}handleStreamEnded(){this.emit(exports.ReceiveSlotEvents.MediaEnded);}_replaceTrack(newTrack){this._stream.replaceTrack(newTrack);}_updateSource(state,csi){if(state!==this._sourceState||csi!==this._currentRxCsi){this._sourceState=state;this._currentRxCsi=csi;this.emit(exports.ReceiveSlotEvents.SourceUpdate,state,csi);}}close(){this._stream.off(exports.RemoteStreamEventNames.MediaStateChange,this.handleStreamMediaStateChange);this._stream.off(exports.StreamEventNames.Ended,this.handleStreamEnded);}get id(){return this._idGetter();}get stream(){return this._stream.outputStream;}get currentRxCsi(){return this._currentRxCsi;}}ReceiveSlot.Events=exports.ReceiveSlotEvents;class Transceiver{constructor(rtcRtpTransceiver,mid){this.twccDisabled=false;this._rtcRtpTransceiver=rtcRtpTransceiver;this.mid=mid;}replaceTransceiver(newRtcRtpTransceiver){this._rtcRtpTransceiver=newRtcRtpTransceiver;}get receiver(){return this._rtcRtpTransceiver.receiver;}get sender(){return this._rtcRtpTransceiver.sender;}close(){this._rtcRtpTransceiver.stop();}}class ReceiveOnlyTransceiver extends Transceiver{constructor(rtcRtpTransceiver,mid,munger){super(rtcRtpTransceiver,mid);this.munger=munger;this._receiveSlot=new ReceiveSlot(()=>{if(!this._rtcRtpTransceiver.mid){return null;}return this.munger.getReceiverId();},this._rtcRtpTransceiver.receiver.track);}replaceTransceiver(newRtcRtpTransceiver){super.replaceTransceiver(newRtcRtpTransceiver);this._receiveSlot._replaceTrack(newRtcRtpTransceiver.receiver.track);}close(){super.close();this._receiveSlot.close();}get receiveSlot(){return this._receiveSlot;}getStats(){return this.receiver.getStats();}mungeLocalDescription(mediaDescription){this.munger.mungeLocalDescription(mediaDescription,{twccDisabled:this.twccDisabled});}mungeRemoteDescription(mediaDescription){this.munger.mungeRemoteDescription(mediaDescription);}get receiverId(){return this.munger.getReceiverId();}resetSdpMunger(){this.munger.reset();}}ReceiveOnlyTransceiver.rid='1';var events={exports:{}};var R=typeof Reflect==='object'?Reflect:null;var ReflectApply=R&&typeof R.apply==='function'?R.apply:function ReflectApply(target,receiver,args){return Function.prototype.apply.call(target,receiver,args);};var ReflectOwnKeys;if(R&&typeof R.ownKeys==='function'){ReflectOwnKeys=R.ownKeys;}else if(Object.getOwnPropertySymbols){ReflectOwnKeys=function ReflectOwnKeys(target){return Object.getOwnPropertyNames(target).concat(Object.getOwnPropertySymbols(target));};}else {ReflectOwnKeys=function ReflectOwnKeys(target){return Object.getOwnPropertyNames(target);};}function ProcessEmitWarning(warning){if(console&&console.warn)console.warn(warning);}var NumberIsNaN=Number.isNaN||function NumberIsNaN(value){return value!==value;};function EventEmitter$1(){EventEmitter$1.init.call(this);}events.exports=EventEmitter$1;events.exports.once=once$1;// Backwards-compat with node 0.10.x
|
|
2901
|
+
if(flags.once){emitter.removeEventListener(name,wrapListener);}listener(arg);});}else {throw new TypeError('The "emitter" argument must be of type EventEmitter. Received type '+typeof emitter);}}class EventEmitter$2 extends events$1.exports.EventEmitter{}class IngressSdpMunger{constructor(){this.ssrc=generateSsrc();}getReceiverId(){return Object.assign({ssrc:this.ssrc},this.rtxSsrc?{rtxSsrc:this.rtxSsrc}:{});}mungeLocalDescription(mediaDescription,options){retainCodecs(mediaDescription,['h264','opus','rtx']);if(mediaDescription.codecs.size===0){logErrorAndThrow(exports.WcmeErrorType.SDP_MUNGE_MISSING_CODECS,"No codecs present in m-line with MID ".concat(mediaDescription.mid," after filtering."));}removeMidRidExtensions(mediaDescription);if(options.twccDisabled){disableTwcc(mediaDescription);}}mungeRemoteDescription(mediaDescription){if(!mediaDescription.ssrcs.length){mediaDescription.addLine(new SsrcLine$1(this.ssrc,'cname',"".concat(this.ssrc,"-cname")));mediaDescription.addLine(new SsrcLine$1(this.ssrc,'msid','-',"".concat(mediaDescription.mid)));if(hasCodec('rtx',mediaDescription)){if(!this.rtxSsrc){this.rtxSsrc=generateSsrc();}mediaDescription.addLine(new SsrcLine$1(this.rtxSsrc,'cname',"".concat(this.ssrc,"-cname")));mediaDescription.addLine(new SsrcLine$1(this.rtxSsrc,'msid','-',"".concat(mediaDescription.mid)));mediaDescription.addLine(new SsrcGroupLine$1('FID',[this.ssrc,this.rtxSsrc]));}}if(retainCandidates(mediaDescription,['udp','tcp'])){logger.log("Some unsupported remote candidates have been removed from mid ".concat(mediaDescription.mid));}[...mediaDescription.codecs.values()].forEach(ci=>{ci.fmtParams.set('x-google-start-bitrate','60000');});}reset(){this.ssrc=generateSsrc();}}class MidPredictor{constructor(){this.currentMid=0;this.midMap=new Map();}getNextMid(mediaType){var mid=this.currentMid++;var mids=this.midMap.get(mediaType)||[];mids.push("".concat(mid));this.midMap.set(mediaType,mids);return "".concat(mid);}allocateMidForDatachannel(){this.currentMid+=1;}reset(){this.midMap=new Map();this.currentMid=0;}getMidMap(){return this.midMap;}}var OveruseState;(function(OveruseState){OveruseState[OveruseState["NOT_OVERUSED"]=0]="NOT_OVERUSED";OveruseState[OveruseState["OVERUSED"]=1]="OVERUSED";})(OveruseState||(OveruseState={}));class OveruseStateManager{constructor(callback){this.monitors=[];this.lastOverallOveruseState=OveruseState.NOT_OVERUSED;this.isRunning=false;this.overuseUpdateCallback=callback;}addMonitor(monitor){this.monitors.push(monitor);if(this.isRunning){monitor.startMonitoring(()=>this.onMonitorOveruseUpdate());}}start(){this.isRunning=true;this.monitors.forEach(monitor=>monitor.startMonitoring(()=>this.onMonitorOveruseUpdate()));}stop(){this.isRunning=false;this.monitors.forEach(monitor=>monitor.stopMonitoring());}onMonitorOveruseUpdate(){var overuseStates=this.monitors.map(monitor=>monitor.getLastOveruseState());var overallOveruseState=overuseStates.some(overuseState=>overuseState===OveruseState.OVERUSED)?OveruseState.OVERUSED:OveruseState.NOT_OVERUSED;if(overallOveruseState!==this.lastOverallOveruseState){this.lastOverallOveruseState=overallOveruseState;this.overuseUpdateCallback(overallOveruseState);}}}exports.ReceiveSlotEvents = void 0;(function(ReceiveSlotEvents){ReceiveSlotEvents["MediaStarted"]="media-started";ReceiveSlotEvents["MediaStopped"]="media-stopped";ReceiveSlotEvents["MediaEnded"]="media-ended";ReceiveSlotEvents["SourceUpdate"]="source-update";})(exports.ReceiveSlotEvents||(exports.ReceiveSlotEvents={}));class ReceiveSlot extends EventEmitter$2{constructor(idGetter,track){super();this._idGetter=idGetter;this.handleStreamMediaStateChange=this.handleStreamMediaStateChange.bind(this);this.handleStreamEnded=this.handleStreamEnded.bind(this);this._stream=new RemoteStream(new MediaStream([track]));this._sourceState='no source';this._stream.on(exports.RemoteStreamEventNames.MediaStateChange,this.handleStreamMediaStateChange);this._stream.on(exports.StreamEventNames.Ended,this.handleStreamEnded);}handleStreamMediaStateChange(state){if(state===RemoteMediaState.Stopped){this.emit(exports.ReceiveSlotEvents.MediaStopped);}else {this.emit(exports.ReceiveSlotEvents.MediaStarted);}}handleStreamEnded(){this.emit(exports.ReceiveSlotEvents.MediaEnded);}_replaceTrack(newTrack){this._stream.replaceTrack(newTrack);}_updateSource(state,csi){if(state!==this._sourceState||csi!==this._currentRxCsi){this._sourceState=state;this._currentRxCsi=csi;this.emit(exports.ReceiveSlotEvents.SourceUpdate,state,csi);}}close(){this._stream.off(exports.RemoteStreamEventNames.MediaStateChange,this.handleStreamMediaStateChange);this._stream.off(exports.StreamEventNames.Ended,this.handleStreamEnded);}get id(){return this._idGetter();}get stream(){return this._stream.outputStream;}get currentRxCsi(){return this._currentRxCsi;}}ReceiveSlot.Events=exports.ReceiveSlotEvents;class Transceiver{constructor(rtcRtpTransceiver,mid){this.twccDisabled=false;this._rtcRtpTransceiver=rtcRtpTransceiver;this.mid=mid;}replaceTransceiver(newRtcRtpTransceiver){this._rtcRtpTransceiver=newRtcRtpTransceiver;}get receiver(){return this._rtcRtpTransceiver.receiver;}get sender(){return this._rtcRtpTransceiver.sender;}close(){this._rtcRtpTransceiver.stop();}}class ReceiveOnlyTransceiver extends Transceiver{constructor(rtcRtpTransceiver,mid,munger){super(rtcRtpTransceiver,mid);this.metadata={};this.munger=munger;this._receiveSlot=new ReceiveSlot(()=>{if(!this._rtcRtpTransceiver.mid){return null;}return this.munger.getReceiverId();},this._rtcRtpTransceiver.receiver.track);}replaceTransceiver(newRtcRtpTransceiver){super.replaceTransceiver(newRtcRtpTransceiver);this._receiveSlot._replaceTrack(newRtcRtpTransceiver.receiver.track);}close(){super.close();this._receiveSlot.close();}get receiveSlot(){return this._receiveSlot;}getStats(){return __awaiter$1(this,void 0,void 0,function*(){var statsMap=new Map();var statsReport=yield this.receiver.getStats();statsReport.forEach((stats,key)=>{if(stats.type==='inbound-rtp'){stats.mid=this.mid;stats.csi=this.receiveSlot.currentRxCsi;stats.calliopeMediaType=this.metadata.mediaType;stats.requestedBitrate=this.metadata.requestedBitrate;stats.requestedFrameSize=this.metadata.requestedFrameSize;Object.assign(stats,this.receiverId);}statsMap.set(key,stats);});return statsMap;});}mungeLocalDescription(mediaDescription){this.munger.mungeLocalDescription(mediaDescription,{twccDisabled:this.twccDisabled});}mungeRemoteDescription(mediaDescription){this.munger.mungeRemoteDescription(mediaDescription);}get receiverId(){return this.munger.getReceiverId();}resetSdpMunger(){this.munger.reset();}handleRequested(streamRequest){var _a,_b;this.metadata.requestedBitrate=streamRequest.maxPayloadBitsPerSecond;this.metadata.requestedFrameSize=(_b=(_a=streamRequest.codecInfos[0])===null||_a===void 0?void 0:_a.h264)===null||_b===void 0?void 0:_b.maxFs;}handleUnrequested(){this.metadata.requestedBitrate=undefined;this.metadata.requestedFrameSize=undefined;this.receiveSlot._updateSource('no source',undefined);}}ReceiveOnlyTransceiver.rid='1';var events={exports:{}};var R=typeof Reflect==='object'?Reflect:null;var ReflectApply=R&&typeof R.apply==='function'?R.apply:function ReflectApply(target,receiver,args){return Function.prototype.apply.call(target,receiver,args);};var ReflectOwnKeys;if(R&&typeof R.ownKeys==='function'){ReflectOwnKeys=R.ownKeys;}else if(Object.getOwnPropertySymbols){ReflectOwnKeys=function ReflectOwnKeys(target){return Object.getOwnPropertyNames(target).concat(Object.getOwnPropertySymbols(target));};}else {ReflectOwnKeys=function ReflectOwnKeys(target){return Object.getOwnPropertyNames(target);};}function ProcessEmitWarning(warning){if(console&&console.warn)console.warn(warning);}var NumberIsNaN=Number.isNaN||function NumberIsNaN(value){return value!==value;};function EventEmitter$1(){EventEmitter$1.init.call(this);}events.exports=EventEmitter$1;events.exports.once=once$1;// Backwards-compat with node 0.10.x
|
|
2902
2902
|
EventEmitter$1.EventEmitter=EventEmitter$1;EventEmitter$1.prototype._events=undefined;EventEmitter$1.prototype._eventsCount=0;EventEmitter$1.prototype._maxListeners=undefined;// By default EventEmitters will print a warning if more than 10 listeners are
|
|
2903
2903
|
// added to it. This is a useful default which helps finding memory leaks.
|
|
2904
2904
|
var defaultMaxListeners=10;function checkListener(listener){if(typeof listener!=='function'){throw new TypeError('The "listener" argument must be of type Function. Received type '+typeof listener);}}Object.defineProperty(EventEmitter$1,'defaultMaxListeners',{enumerable:true,get:function get(){return defaultMaxListeners;},set:function set(arg){if(typeof arg!=='number'||arg<0||NumberIsNaN(arg)){throw new RangeError('The value of "defaultMaxListeners" is out of range. It must be a non-negative number. Received '+arg+'.');}defaultMaxListeners=arg;}});EventEmitter$1.init=function(){if(this._events===undefined||this._events===Object.getPrototypeOf(this)._events){this._events=Object.create(null);this._eventsCount=0;}this._maxListeners=this._maxListeners||undefined;};// Obviously not all Emitters should be limited to 10. This function allows
|
|
@@ -4892,7 +4892,7 @@ var{hasOwnProperty}=Object.prototype;for(var i=0;i<mapResults.length;i++){if(map
|
|
|
4892
4892
|
* // arg1 now equals 'three'
|
|
4893
4893
|
* callback(null, 'done');
|
|
4894
4894
|
* }
|
|
4895
|
-
*/function waterfall(tasks,callback){callback=once(callback);if(!Array.isArray(tasks))return callback(new Error('First argument to waterfall must be an array of functions'));if(!tasks.length)return callback();var taskIndex=0;function nextTask(args){var task=wrapAsync(tasks[taskIndex++]);task(...args,onlyOnce(next));}function next(err){if(err===false)return;for(var _len22=arguments.length,args=new Array(_len22>1?_len22-1:0),_key22=1;_key22<_len22;_key22++){args[_key22-1]=arguments[_key22];}if(err||taskIndex===tasks.length){return callback(err,...args);}nextTask(args);}nextTask([]);}awaitify(waterfall);function processTasks(task,finishedCallback){return __awaiter$1(this,void 0,void 0,function*(){try{yield task();finishedCallback();}catch(e){finishedCallback(e);}});}class AsyncQueue{constructor(){this.queue=queue$1(processTasks,1);}push(task){return this.queue.pushAsync(task);}empty(){return this.queue.empty();}}var OfferAnswerType;(function(OfferAnswerType){OfferAnswerType[OfferAnswerType["LocalOnly"]=0]="LocalOnly";OfferAnswerType[OfferAnswerType["Remote"]=1]="Remote";})(OfferAnswerType||(OfferAnswerType={}));class SendOnlyTransceiver extends Transceiver{constructor(rtcRtpTransceiver,mid,csi,munger,mediaType){super(rtcRtpTransceiver,mid);this.rtxEnabled=false;this.streamMuteStateChange=new TypedEvent();this.streamPublishStateChange=new TypedEvent();this.negotiationNeeded=new TypedEvent();this.namedMediaGroupsChange=new TypedEvent();this.requestedIdEncodingParamsMap=new Map();this.updateSendParametersQueue=new AsyncQueue();this.csi=csi;this.direction='sendrecv';this.handleTrackChange=this.handleTrackChange.bind(this);this.handleStreamConstraintsChange=this.handleStreamConstraintsChange.bind(this);this.handleStreamMuteStateChange=this.handleStreamMuteStateChange.bind(this);this.munger=munger;this.mediaType=mediaType;}replaceSenderSource(stream){var _a,_b;return __awaiter$1(this,void 0,void 0,function*(){var trackOrNull=(_a=stream===null||stream===void 0?void 0:stream.outputStream.getTracks()[0])!==null&&_a!==void 0?_a:null;if(((_b=this.sender.track)===null||_b===void 0?void 0:_b.id)!==(trackOrNull===null||trackOrNull===void 0?void 0:trackOrNull.id)){yield this.sender.replaceTrack(trackOrNull);if(trackOrNull){logger.log("Sender source for ".concat(this.mediaType," replaced with track ID ").concat(trackOrNull.id));}else {logger.log("Sender source for ".concat(this.mediaType," set to null, sender stopped"));}}});}handleTrackChange(){return __awaiter$1(this,void 0,void 0,function*(){if(this.requested){yield this.replaceSenderSource(this.publishedStream);}});}handleStreamConstraintsChange(){return __awaiter$1(this,void 0,void 0,function*(){yield this.updateSendParameters(this.requestedIdEncodingParamsMap);});}handleStreamMuteStateChange(){this.streamMuteStateChange.emit();}get requested(){return this.requestedIdEncodingParamsMap.size>0;}replaceTransceiver(newRtcRtpTransceiver){var _super=Object.create(null,{replaceTransceiver:{get:()=>super.replaceTransceiver}});return __awaiter$1(this,void 0,void 0,function*(){_super.replaceTransceiver.call(this,newRtcRtpTransceiver);newRtcRtpTransceiver.direction=this.direction;if(this.requested){yield this.replaceSenderSource(this.publishedStream);}});}replacePublishedStream(newStream){return __awaiter$1(this,void 0,void 0,function*(){var oldStream=this.publishedStream;oldStream===null||oldStream===void 0?void 0:oldStream.off(exports.LocalStreamEventNames.OutputTrackChange,this.handleTrackChange);oldStream===null||oldStream===void 0?void 0:oldStream.off(exports.LocalStreamEventNames.ConstraintsChange,this.handleStreamConstraintsChange);oldStream===null||oldStream===void 0?void 0:oldStream.off(exports.LocalStreamEventNames.UserMuteStateChange,this.handleStreamMuteStateChange);oldStream===null||oldStream===void 0?void 0:oldStream.off(exports.LocalStreamEventNames.SystemMuteStateChange,this.handleStreamMuteStateChange);if(this.requested){yield this.replaceSenderSource(newStream);}this.publishedStream=newStream;newStream===null||newStream===void 0?void 0:newStream.on(exports.LocalStreamEventNames.OutputTrackChange,this.handleTrackChange);newStream===null||newStream===void 0?void 0:newStream.on(exports.LocalStreamEventNames.ConstraintsChange,this.handleStreamConstraintsChange);newStream===null||newStream===void 0?void 0:newStream.on(exports.LocalStreamEventNames.UserMuteStateChange,this.handleStreamMuteStateChange);newStream===null||newStream===void 0?void 0:newStream.on(exports.LocalStreamEventNames.SystemMuteStateChange,this.handleStreamMuteStateChange);if(!oldStream&&newStream&&!newStream.muted||oldStream&&!newStream&&!oldStream.muted){this.streamPublishStateChange.emit();}else if((oldStream===null||oldStream===void 0?void 0:oldStream.muted)!==(newStream===null||newStream===void 0?void 0:newStream.muted)){this.streamMuteStateChange.emit();}});}setNamedMediaGroups(namedMediaGroups){if(this.mediaType!==exports.MediaType.AudioMain){logErrorAndThrow(exports.WcmeErrorType.SET_NMG_FAILED,"Named media groups can only be set for audio.");}this.namedMediaGroups=namedMediaGroups;this.namedMediaGroupsChange.emit();}publishStream(stream){return this.replacePublishedStream(stream);}unpublishStream(){return this.replacePublishedStream();}get active(){return this._rtcRtpTransceiver.direction==='sendrecv';}set active(enabled){this.direction=enabled?'sendrecv':'inactive';this._rtcRtpTransceiver.direction=this.direction;if(this._rtcRtpTransceiver.direction!==this._rtcRtpTransceiver.currentDirection){this.negotiationNeeded.emit(OfferAnswerType.Remote);}}getStats(){return this.sender.getStats();}updateSendParameters(requestedIdEncodingParamsMap){return __awaiter$1(this,void 0,void 0,function*(){return this.updateSendParametersQueue.push(()=>__awaiter$1(this,void 0,void 0,function*(){var sendParameters=this.sender.getParameters();sendParameters.encodings.forEach((encoding,index)=>{var _a,_b;var encodingParams=requestedIdEncodingParamsMap.get(index);encoding.active=!!encodingParams;if(encodingParams){var{maxPayloadBitsPerSecond,maxFs,maxWidth,maxHeight}=encodingParams;var scaleDownRatio=getScaleDownRatio((_a=this.publishedStream)===null||_a===void 0?void 0:_a.getSettings().width,(_b=this.publishedStream)===null||_b===void 0?void 0:_b.getSettings().height,maxFs,maxWidth,maxHeight);if(maxPayloadBitsPerSecond!==undefined&&maxPayloadBitsPerSecond>=0){encoding.maxBitrate=maxPayloadBitsPerSecond;}if(scaleDownRatio!==undefined&&scaleDownRatio>=1.0){encoding.scaleResolutionDownBy=scaleDownRatio;}}});yield this.sender.setParameters(sendParameters);var oldRequested=this.requested;var newRequested=requestedIdEncodingParamsMap.size>0;this.requestedIdEncodingParamsMap=requestedIdEncodingParamsMap;if(oldRequested!==newRequested){yield this.replaceSenderSource(newRequested?this.publishedStream:null);}}));});}getRequestedBitrate(ssrc){var index=this.senderIds.findIndex(streamId=>streamId.ssrc===ssrc);if(this.requestedIdEncodingParamsMap.has(index)){var encodingParams=this.requestedIdEncodingParamsMap.get(index);return encodingParams===null||encodingParams===void 0?void 0:encodingParams.maxPayloadBitsPerSecond;}return undefined;}getRequestedFrameSize(ssrc){var index=this.senderIds.findIndex(streamId=>streamId.ssrc===ssrc);if(this.requestedIdEncodingParamsMap.has(index)){var encodingParams=this.requestedIdEncodingParamsMap.get(index);return encodingParams===null||encodingParams===void 0?void 0:encodingParams.maxFs;}return undefined;}isSimulcastEnabled(){var params=this.sender.getParameters();return params.encodings.length>1;}mungeLocalDescription(mediaDescription){this.munger.mungeLocalDescription(mediaDescription,{simulcastEnabled:this.isSimulcastEnabled(),rtxEnabled:this.rtxEnabled,twccDisabled:this.twccDisabled});}mungeLocalDescriptionForRemoteServer(mediaDescription){this.munger.mungeLocalDescriptionForRemoteServer(mediaDescription,getMediaContent(this.mediaType),this.csi);}mungeRemoteDescription(mediaDescription){this.munger.mungeRemoteDescription(mediaDescription);}get senderIds(){return this.munger.getSenderIds();}get numActiveSimulcastLayers(){var _a;if(getMediaFamily(this.mediaType)===exports.MediaFamily.Video){return (_a=this.publishedStream)===null||_a===void 0?void 0:_a.getNumActiveSimulcastLayers();}return this.publishedStream?0:undefined;}getEncodingIndexForStreamId(id){return this.munger.getEncodingIndexForStreamId(id);}resetSdpMunger(){this.munger.reset();}setCodecParameters(parameters){this.munger.setCodecParameters(parameters);this.negotiationNeeded.emit(OfferAnswerType.LocalOnly);}deleteCodecParameters(parameters){this.munger.deleteCodecParameters(parameters);this.negotiationNeeded.emit(OfferAnswerType.LocalOnly);}}class SendSlot{constructor(sendTransceiver){this.sendTransceiver=sendTransceiver;}publishStream(stream){return __awaiter$1(this,void 0,void 0,function*(){if(stream===this.sendTransceiver.publishedStream){return Promise.resolve();}return this.sendTransceiver.publishStream(stream);});}unpublishStream(){return __awaiter$1(this,void 0,void 0,function*(){if(!this.sendTransceiver.publishedStream){return Promise.resolve();}return this.sendTransceiver.unpublishStream();});}setNamedMediaGroups(namedMediaGroups){this.sendTransceiver.setNamedMediaGroups(namedMediaGroups);}clearNamedMediaGroups(){this.setNamedMediaGroups([]);}get active(){return this.sendTransceiver.active;}set active(active){this.sendTransceiver.active=active;}setCodecParameters(parameters){return __awaiter$1(this,void 0,void 0,function*(){this.sendTransceiver.setCodecParameters(parameters);});}deleteCodecParameters(parameters){return __awaiter$1(this,void 0,void 0,function*(){this.sendTransceiver.deleteCodecParameters(parameters);});}}class StatsManager{constructor(statsGetter){var statsPreprocessor=arguments.length>1&&arguments[1]!==undefined?arguments[1]:()=>__awaiter$1(this,void 0,void 0,function*(){});this.statsGetter=statsGetter;this.statsPreProcessor=statsPreprocessor;}getStats(){return __awaiter$1(this,void 0,void 0,function*(){var statsReport=yield this.statsGetter();var statsMap=new Map();statsReport.forEach((stats,key)=>statsMap.set(key,stats));yield this.statsPreProcessor(statsMap);return statsMap;});}}var organizeTransceiverStats=(sendTransceivers,recvTransceivers)=>__awaiter$1(void 0,void 0,void 0,function*(){var result={audio:{senders:[],receivers:[]},video:{senders:[],receivers:[]},screenShareAudio:{senders:[],receivers:[]},screenShareVideo:{senders:[],receivers:[]}};yield Promise.all([...sendTransceivers.entries()].map(_ref6=>{var[mediaType,transceiver]=_ref6;return __awaiter$1(void 0,void 0,void 0,function*(){var _a;var report=[];(yield transceiver.getStats()).forEach(senderStats=>{if(senderStats.type==='outbound-rtp'){report.push(Object.assign(Object.assign({},senderStats),{requestedBitrate:transceiver.getRequestedBitrate(senderStats.ssrc),requestedFrameSize:transceiver.getRequestedFrameSize(senderStats.ssrc)}));}});var item={report,mid:transceiver.mid,csi:transceiver.csi,currentDirection:'sendonly',localTrackLabel:(_a=transceiver.publishedStream)===null||_a===void 0?void 0:_a.label};if(mediaType===exports.MediaType.AudioMain){result.audio.senders.push(item);}if(mediaType===exports.MediaType.VideoMain){result.video.senders.push(item);}if(mediaType===exports.MediaType.AudioSlides){result.screenShareAudio.senders.push(item);}if(mediaType===exports.MediaType.VideoSlides){result.screenShareVideo.senders.push(item);}});}));yield Promise.all([...recvTransceivers.entries()].map(_ref7=>{var[mediaType,transceivers]=_ref7;return __awaiter$1(void 0,void 0,void 0,function*(){return Promise.all(transceivers.map(t=>__awaiter$1(void 0,void 0,void 0,function*(){var _b,_c;var report=[];(yield t.getStats()).forEach(receiverStats=>{if(receiverStats.type==='inbound-rtp'){report.push(Object.assign(Object.assign({},receiverStats),{requestedBitrate:t.requestedBitrate,requestedFrameSize:t.requestedFrameSize}));}});var item={report,mid:(_b=t.receiveSlot.id)===null||_b===void 0?void 0:_b.mid,csi:t.receiveSlot.currentRxCsi,currentDirection:'recvonly',localTrackLabel:(_c=t.receiveSlot.stream.getTracks()[0])===null||_c===void 0?void 0:_c.label};if(mediaType===exports.MediaType.AudioMain){result.audio.receivers.push(item);}if(mediaType===exports.MediaType.VideoMain){result.video.receivers.push(item);}if(mediaType===exports.MediaType.AudioSlides){result.screenShareAudio.receivers.push(item);}if(mediaType===exports.MediaType.VideoSlides){result.screenShareVideo.receivers.push(item);}})));});}));return result;});function toMediaStreamTrackKind(mediaType){return [exports.MediaType.VideoMain,exports.MediaType.VideoSlides].includes(mediaType)?exports.MediaStreamTrackKind.Video:exports.MediaStreamTrackKind.Audio;}function webRtcVideoContentHintToJmpVideoContentHint(hint){if(hint==='motion'){return 'motion';}if(hint==='detail'){return 'sharpness';}return undefined;}var MultistreamConnectionEventNames;(function(MultistreamConnectionEventNames){MultistreamConnectionEventNames["VideoSourceCountUpdate"]="video-source-count-update";MultistreamConnectionEventNames["AudioSourceCountUpdate"]="audio-source-count-update";MultistreamConnectionEventNames["ActiveSpeakerNotification"]="active-speaker-notification";MultistreamConnectionEventNames["ConnectionStateUpdate"]="connection-state-update";MultistreamConnectionEventNames["IceGatheringStateUpdate"]="ice-gathering-state-update";MultistreamConnectionEventNames["NegotiationNeeded"]="negotiation-needed";MultistreamConnectionEventNames["CreateOfferOnSuccess"]="createofferonsuccess";MultistreamConnectionEventNames["CreateAnswerOnSuccess"]="createansweronsuccess";MultistreamConnectionEventNames["SetLocalDescriptionOnSuccess"]="setlocaldescriptiononsuccess";MultistreamConnectionEventNames["SetRemoteDescriptionOnSuccess"]="setremotedescriptiononsuccess";})(MultistreamConnectionEventNames||(MultistreamConnectionEventNames={}));var defaultMultistreamConnectionOptions={disableSimulcast:BrowserInfo.isFirefox(),bundlePolicy:'max-compat',iceServers:undefined,disableContentSimulcast:true,disableAudioTwcc:true};class MultistreamConnection extends EventEmitter$2{constructor(){var userOptions=arguments.length>0&&arguments[0]!==undefined?arguments[0]:{};super();this.sendTransceivers=new Map();this.recvTransceivers=new Map();this.jmpSessions=new Map();this.pendingJmpTasks=[];this.metricsCallback=()=>{};this.overuseUpdateCallback=()=>{};this.midPredictor=new MidPredictor();this.offerAnswerQueue=new AsyncQueue();this.currentCreateOfferId=0;this.options=Object.assign(Object.assign({},defaultMultistreamConnectionOptions),userOptions);logger.info("Creating multistream connection with options ".concat(JSON.stringify(this.options)));this.initializePeerConnection();this.overuseStateManager=new OveruseStateManager(overuseState=>this.overuseUpdateCallback(overuseState));this.overuseStateManager.start();this.statsManager=new StatsManager(()=>this.pc.getStats(),stats=>this.preProcessStats(stats));var mainSceneId=generateSceneId();var slidesSceneId=generateSceneId();var videoMainEncodingOptions=this.getVideoEncodingOptions(exports.MediaContent.Main);var videoSlidesEncodingOptions=this.getVideoEncodingOptions(exports.MediaContent.Slides);this.createSendTransceiver(exports.MediaType.VideoMain,mainSceneId,videoMainEncodingOptions);this.createSendTransceiver(exports.MediaType.AudioMain,mainSceneId);this.createSendTransceiver(exports.MediaType.VideoSlides,slidesSceneId,videoSlidesEncodingOptions);this.createSendTransceiver(exports.MediaType.AudioSlides,slidesSceneId);}initializePeerConnection(){var _a;(_a=this.pc)===null||_a===void 0?void 0:_a.close();this.pc=new PeerConnection({iceServers:this.options.iceServers,bundlePolicy:this.options.bundlePolicy});this.propagatePeerConnectionEvents();this.attachMetricsObserver();this.createDataChannel();}propagatePeerConnectionEvents(){this.pc.on(PeerConnection.Events.ConnectionStateChange,state=>{this.emit(MultistreamConnectionEventNames.ConnectionStateUpdate,state);});this.pc.on(PeerConnection.Events.CreateOfferOnSuccess,description=>{this.emit(MultistreamConnectionEventNames.CreateOfferOnSuccess,description);});this.pc.on(PeerConnection.Events.CreateAnswerOnSuccess,description=>{this.emit(MultistreamConnectionEventNames.CreateAnswerOnSuccess,description);});this.pc.on(PeerConnection.Events.SetLocalDescriptionOnSuccess,description=>{this.emit(MultistreamConnectionEventNames.SetLocalDescriptionOnSuccess,description);});this.pc.on(PeerConnection.Events.SetRemoteDescriptionOnSuccess,description=>{this.emit(MultistreamConnectionEventNames.SetRemoteDescriptionOnSuccess,description);});this.pc.on(PeerConnection.Events.IceGatheringStateChange,()=>{this.emit(MultistreamConnectionEventNames.IceGatheringStateUpdate,this.getIceGatheringState());});}getConnectionState(){return this.pc.getConnectionState();}getIceGatheringState(){return this.pc.iceGatheringState;}getVideoEncodingOptions(content){var enabledSimulcast=content===exports.MediaContent.Main?!this.options.disableSimulcast:!this.options.disableContentSimulcast;return enabledSimulcast?[{scaleResolutionDownBy:4,active:false},{scaleResolutionDownBy:2,active:false},{active:false}]:[{active:false}];}createSendTransceiver(mediaType,sceneId,sendEncodingsOptions){var rtcTransceiver;try{rtcTransceiver=this.pc.addTransceiver(toMediaStreamTrackKind(mediaType),{direction:'sendrecv',sendEncodings:sendEncodingsOptions});}catch(e){logger.error("addTransceiver failed due to : ".concat(e));throw e;}var mid=this.midPredictor.getNextMid(mediaType);var csi=generateCsi(getMediaFamily(mediaType),sceneId);var munger=new EgressSdpMunger();var transceiver=new SendOnlyTransceiver(rtcTransceiver,mid,csi,munger,mediaType);if(getMediaFamily(mediaType)===exports.MediaFamily.Video){transceiver.rtxEnabled=true;transceiver.setCodecParameters({'max-mbps':"".concat(defaultMaxVideoEncodeMbps),'max-fs':"".concat(defaultMaxVideoEncodeFrameSize)});}transceiver.twccDisabled=getMediaFamily(mediaType)===exports.MediaFamily.Audio?this.options.disableAudioTwcc:false;transceiver.active=false;transceiver.streamMuteStateChange.on(()=>{this.sendSourceAdvertisement(mediaType);this.sendMediaRequestStatus(mediaType);});transceiver.streamPublishStateChange.on(()=>{this.sendSourceAdvertisement(mediaType);this.sendMediaRequestStatus(mediaType);});transceiver.negotiationNeeded.on(offerAnswerType=>{if(offerAnswerType===OfferAnswerType.Remote){this.emit(MultistreamConnectionEventNames.NegotiationNeeded);}else if(this.pc.getRemoteDescription()){this.queueLocalOfferAnswer();}});transceiver.namedMediaGroupsChange.on(()=>{this.sendSourceAdvertisement(mediaType);});this.sendTransceivers.set(mediaType,transceiver);this.createJmpSession(mediaType);}createSendSlot(mediaType){var active=arguments.length>1&&arguments[1]!==undefined?arguments[1]:true;var transceiver=this.getSendTransceiverOrThrow(mediaType);transceiver.active=active;return new SendSlot(transceiver);}createJmpSession(mediaType){var jmpSession=new JmpSession(getMediaFamily(mediaType),getMediaContent(mediaType));jmpSession.setTxCallback(msg=>{var _a;if(((_a=this.dataChannel)===null||_a===void 0?void 0:_a.readyState)!=='open'){logger.error("DataChannel not created or not connected. Unable to send JMP message.");return;}try{logger.info("Sending JMP message (size: ".concat(msg.length,"): ").concat(msg));this.dataChannel.send(msg);}catch(err){var{bufferedAmount,readyState}=this.dataChannel;var{sctp}=this.pc.getUnderlyingRTCPeerConnection();var{maxMessageSize,state}=sctp||{};logErrorAndThrow(exports.WcmeErrorType.DATA_CHANNEL_SEND_FAILED,"Sending JMP message failed with error: ".concat(err,",\nMessage size: ").concat(msg.length,",\nData Channel State: ").concat(readyState,",\nData Channel Buffered amount: ").concat(bufferedAmount,",\nSCTP State: ").concat(state,",\nSCTP Max Message Size: ").concat(maxMessageSize));}});var prevNumTotalSources=0;var prevNumLiveSources=0;jmpSession.on(JmpSessionEvents.SourceAdvertisementReceived,data=>{logger.log("SourceAdvertisement received: ".concat(JSON.stringify(data)));if(data.numTotalSources!==prevNumTotalSources||data.numLiveSources!==prevNumLiveSources){prevNumTotalSources=data.numTotalSources;prevNumLiveSources=data.numLiveSources;var eventName=getMediaFamily(mediaType)===exports.MediaFamily.Video?MultistreamConnectionEventNames.VideoSourceCountUpdate:MultistreamConnectionEventNames.AudioSourceCountUpdate;this.emit(eventName,data.numTotalSources,data.numLiveSources,getMediaContent(mediaType));}else {logger.log('Number of sources was unchanged, ignoring message');}});jmpSession.on(JmpSessionEvents.MediaRequestStatusReceived,data=>{logger.log("MediaRequestStatus received: ".concat(JSON.stringify(data)));data.streamStates.forEach(s=>{var receiveSlot=this.getReceiveSlotById(s.id);if(!receiveSlot){logger.warn("Got MediaRequestStatus for unknown receive slot: ".concat(JSON.stringify(s.id)));return;}receiveSlot._updateSource(s.state,s.csi);});});jmpSession.on(JmpSessionEvents.MediaRequestReceived,data=>{logger.log("MediaRequest received: ".concat(JSON.stringify(data)));this.updateRequestedStreams(mediaType,data.requests).then(()=>{if(getMediaFamily(mediaType)===exports.MediaFamily.Video){this.sendMediaRequestStatus(mediaType);}});});jmpSession.on(JmpSessionEvents.ActiveSpeaker,data=>{this.emit(MultistreamConnectionEventNames.ActiveSpeakerNotification,data.csis);});this.jmpSessions.set(mediaType,jmpSession);}updateRequestedStreams(mediaType,requests){var sendTransceiver=this.getSendTransceiverOrThrow(mediaType);var mediaFamily=getMediaFamily(mediaType);var requestedIdEncodingParamsMap=new Map();var rsRequests=requests.filter(r=>isValidReceiverSelectedInfo(r.policySpecificInfo));if(rsRequests.length!==requests.length){logger.warn('Ignoring non-receiver-selected requests');}rsRequests.forEach(_ref8=>{var{ids,policySpecificInfo,codecInfos,maxPayloadBitsPerSecond}=_ref8;var _a,_b,_c;if(ids.length>1){logErrorAndThrow(exports.WcmeErrorType.INVALID_STREAM_REQUEST,"Stream request cannot have more than one ID.");}if(ids.length===0){return;}if(sendTransceiver.csi!==policySpecificInfo.csi){logger.warn('csi in the StreamRequest does not match');return;}var id=ids[0];var codecInfo=codecInfos[0];var streamIdsMatched=sendTransceiver.senderIds.some(validId=>compareStreamIds(id,validId));if(streamIdsMatched){var encodingIndex=sendTransceiver.getEncodingIndexForStreamId(id);if(encodingIndex!==-1){var encodingParams={maxPayloadBitsPerSecond};if(mediaFamily===exports.MediaFamily.Video){encodingParams.maxFs=(_a=codecInfo===null||codecInfo===void 0?void 0:codecInfo.h264)===null||_a===void 0?void 0:_a.maxFs;encodingParams.maxWidth=(_b=codecInfo===null||codecInfo===void 0?void 0:codecInfo.h264)===null||_b===void 0?void 0:_b.maxWidth;encodingParams.maxHeight=(_c=codecInfo===null||codecInfo===void 0?void 0:codecInfo.h264)===null||_c===void 0?void 0:_c.maxHeight;}requestedIdEncodingParamsMap.set(encodingIndex,encodingParams);}else {logger.warn("".concat(mediaType,": Unable to get encoding index for stream ID: ").concat(JSON.stringify(id)));}}else {logger.warn("".concat(mediaType,": Unable to find matching stream ID for requested ID: ").concat(JSON.stringify(id)));}});return sendTransceiver.updateSendParameters(requestedIdEncodingParamsMap);}createDataChannel(){var dataChannel=this.pc.createDataChannel('datachannel',{ordered:false,maxRetransmits:0});dataChannel.onopen=event=>{logger.info('DataChannel opened:',JSON.stringify(event));[...this.sendTransceivers.keys()].forEach(mediaType=>{this.sendSourceAdvertisement(mediaType);});logger.info("Flushing pending JMP task queue");this.pendingJmpTasks.forEach(t=>t());this.pendingJmpTasks=[];};dataChannel.onmessage=e=>{var parsed;try{parsed=JSON.parse(e.data);}catch(err){logger.error("Error parsing datachannel JSON: ".concat(err));return;}logger.debug('DataChannel got msg:',e.data);var homerMsg=HomerMsg.fromJson(parsed);if(!homerMsg){logger.error("Received invalid datachannel message: ".concat(e));return;}var jmpMsg=homerMsg.payload;if(!isValidJmpMsg(jmpMsg)){logger.error("Received invalid JMP msg: ".concat(JSON.stringify(jmpMsg)));return;}var mediaType=getMediaType(jmpMsg.mediaFamily,jmpMsg.mediaContent);var jmpSession=this.jmpSessions.get(mediaType);if(!jmpSession){logger.error("Unable to find JMP session for media type ".concat(mediaType,"."));return;}jmpSession.receive(jmpMsg);};dataChannel.onclose=event=>{logger.info('DataChannel closed:',JSON.stringify(event));};dataChannel.onerror=event=>{logger.info('DataChannel error:',JSON.stringify(event));};this.dataChannel=dataChannel;}close(){this.sendTransceivers.forEach(t=>t.close());this.recvTransceivers.forEach(recvTransceivers=>{recvTransceivers.forEach(t=>t.close());});this.jmpSessions.forEach(jmpSession=>jmpSession.close());this.pc.close();}sendMediaRequestStatus(mediaType){var _a;if(getMediaFamily(mediaType)!==exports.MediaFamily.Video){return;}if(!this.getSendTransceiverOrThrow(mediaType).requested){return;}var streamStates=this.getVideoStreamStates(mediaType);var task=()=>{var _a;(_a=this.jmpSessions.get(mediaType))===null||_a===void 0?void 0:_a.sendMediaRequestStatus(streamStates);};if(((_a=this.dataChannel)===null||_a===void 0?void 0:_a.readyState)==='open'){task();}else {this.pendingJmpTasks.push(task);}}sendSourceAdvertisement(mediaType){var _a,_b;var transceiver=this.getSendTransceiverOrThrow(mediaType);var numLiveSources=((_a=transceiver.publishedStream)===null||_a===void 0?void 0:_a.muted)===false?1:0;var task;if(getMediaFamily(mediaType)===exports.MediaFamily.Video){var sources=this.getVideoStreamStates(mediaType);if(sources===null){return;}var contentHint;if(transceiver.publishedStream&&mediaType===exports.MediaType.VideoSlides){contentHint=transceiver.publishedStream.contentHint;}task=()=>{var _a;(_a=this.jmpSessions.get(mediaType))===null||_a===void 0?void 0:_a.sendSourceAdvertisement(1,numLiveSources,[],webRtcVideoContentHintToJmpVideoContentHint(contentHint));};}else {task=()=>{var _a;return (_a=this.jmpSessions.get(mediaType))===null||_a===void 0?void 0:_a.sendSourceAdvertisement(1,numLiveSources,mediaType===exports.MediaType.AudioMain?transceiver.namedMediaGroups:[]);};}if(((_b=this.dataChannel)===null||_b===void 0?void 0:_b.readyState)==='open'){task();}else {this.pendingJmpTasks.push(task);}}getVideoStreamStates(mediaType){var _a;var sendTransceiver=this.getSendTransceiverOrThrow(mediaType);var published=!!sendTransceiver.publishedStream;var muted=(_a=sendTransceiver.publishedStream)===null||_a===void 0?void 0:_a.muted;return sendTransceiver.senderIds.map(id=>{var state;if(!published){state='no source';}else if(muted){state='avatar';}else {state='live';}return {id,state,csi:sendTransceiver.csi};});}createReceiveTransceiver(mediaType){var rtcRtpTransceiver=this.pc.addTransceiver(toMediaStreamTrackKind(mediaType),{direction:'recvonly'});var transceiverMid=this.midPredictor.getNextMid(mediaType);var munger=new IngressSdpMunger();var recvOnlyTransceiver=new ReceiveOnlyTransceiver(rtcRtpTransceiver,transceiverMid,munger);recvOnlyTransceiver.twccDisabled=getMediaFamily(mediaType)===exports.MediaFamily.Audio?this.options.disableAudioTwcc:false;this.recvTransceivers.set(mediaType,[...(this.recvTransceivers.get(mediaType)||[]),recvOnlyTransceiver]);return recvOnlyTransceiver;}createReceiveSlot(mediaType){return __awaiter$1(this,void 0,void 0,function*(){return (yield this.createReceiveSlots(mediaType,1))[0];});}createReceiveSlots(mediaType){var count=arguments.length>1&&arguments[1]!==undefined?arguments[1]:1;return __awaiter$1(this,void 0,void 0,function*(){return new Promise(createReceiveSlotsResolve=>{this.offerAnswerQueue.push(()=>__awaiter$1(this,void 0,void 0,function*(){var createdReceiveOnlyTransceivers=[];for(var i=0;i<count;i++){var recvOnlyTransceiver=this.createReceiveTransceiver(mediaType);createdReceiveOnlyTransceivers.push(recvOnlyTransceiver);}if(this.pc.getRemoteDescription()){yield this.doLocalOfferAnswer();}var receiveSlots=createdReceiveOnlyTransceivers.map(transceiver=>transceiver.receiveSlot);createReceiveSlotsResolve(receiveSlots);}));});});}getIngressPayloadType(mediaType,mimeType){var _a,_b,_c;var requestedMediaCodecType=mimeType.split('/')[1];var requestedMid=(_a=this.sendTransceivers.get(mediaType))===null||_a===void 0?void 0:_a.mid;var parsedOffer=parse$1((_b=this.pc.getLocalDescription())===null||_b===void 0?void 0:_b.sdp);var parsedAnswer=parse$1((_c=this.pc.getRemoteDescription())===null||_c===void 0?void 0:_c.sdp);var senderCodecs=parsedAnswer.avMedia.filter(media=>requestedMid===media.mid).map(media=>[...media.codecs.values()]).flat().filter(ci=>ci.name===requestedMediaCodecType);var receiverCodecs=parsedOffer.avMedia.filter(media=>requestedMid===media.mid).map(media=>[...media.codecs.values()]).flat().filter(ci=>ci.name===requestedMediaCodecType);if(!senderCodecs||!receiverCodecs||senderCodecs.length===0||receiverCodecs.length===0){logErrorAndThrow(exports.WcmeErrorType.GET_PAYLOAD_TYPE_FAILED,"Sender codecs or receiver codecs is undefined or empty.");}var senderCodec=senderCodecs[0];var targetCodec=receiverCodecs.find(receiverCodec=>{return areCodecsCompatible(senderCodec,receiverCodec);});if(!targetCodec||!targetCodec.pt){logErrorAndThrow(exports.WcmeErrorType.GET_PAYLOAD_TYPE_FAILED,"Cannot find matching receiver codec for the given mediaType/mimeType.");}return targetCodec.pt;}createOffer(){return __awaiter$1(this,void 0,void 0,function*(){if(!this.pc.getLocalDescription()){this.midPredictor.allocateMidForDatachannel();}if(this.setAnswerResolve){logger.info('Canceling previous offer since setAnswer was never called for it');this.setAnswerResolve();this.setAnswerResolve=undefined;}var createOfferId=++this.currentCreateOfferId;return new Promise((createOfferResolve,createOfferReject)=>{this.offerAnswerQueue.push(()=>__awaiter$1(this,void 0,void 0,function*(){var _a;try{var offer=yield this.pc.createOffer();if(!offer.sdp){logErrorAndThrow(exports.WcmeErrorType.CREATE_OFFER_FAILED,'SDP not found in offer.');}offer.sdp=this.preProcessLocalOffer(offer.sdp);yield this.pc.setLocalDescription(offer).then(()=>__awaiter$1(this,void 0,void 0,function*(){logger.info('this.pc.setLocalDescription() resolved');})).catch(error=>{var _a;logErrorAndThrow(exports.WcmeErrorType.CREATE_OFFER_FAILED,"Error: ".concat(error,". SDP: ").concat(maskIp((_a=offer.sdp)!==null&&_a!==void 0?_a:'')));});var sdpToSend=this.prepareLocalOfferForRemoteServer((_a=this.pc.getLocalDescription())===null||_a===void 0?void 0:_a.sdp);createOfferResolve({type:'offer',sdp:sdpToSend});if(this.currentCreateOfferId>createOfferId){logger.log('Canceling previous offer since createOffer was called while it was being created');}else {yield new Promise(setAnswerResolve=>{this.setAnswerResolve=setAnswerResolve;});}}catch(error){createOfferReject(error);}}));});});}setAnswer(answer){return __awaiter$1(this,void 0,void 0,function*(){var sdp=this.preProcessRemoteAnswer(answer);if(!this.setAnswerResolve){logErrorAndThrow(exports.WcmeErrorType.SET_ANSWER_FAILED,"Call to setAnswer without having previously called createOffer.");}logger.info('calling this.pc.setRemoteDescription()');return this.pc.setRemoteDescription({type:'answer',sdp}).then(()=>__awaiter$1(this,void 0,void 0,function*(){logger.info('this.pc.setRemoteDescription() resolved');if(this.setAnswerResolve){this.setAnswerResolve();this.setAnswerResolve=undefined;}else {logger.debug("setAnswerResolve function was cleared between setAnswer and result of setRemoteDescription");}})).catch(error=>{logErrorAndThrow(exports.WcmeErrorType.SET_ANSWER_FAILED,"Error: ".concat(error,". SDP: ").concat(maskIp(answer)));});});}doLocalOfferAnswer(){var _a;return __awaiter$1(this,void 0,void 0,function*(){var offer=yield this.pc.createOffer();if(!offer.sdp){logErrorAndThrow(exports.WcmeErrorType.CREATE_OFFER_FAILED,'SDP not found in offer.');}offer.sdp=this.preProcessLocalOffer(offer.sdp);yield this.pc.setLocalDescription(offer).then(()=>__awaiter$1(this,void 0,void 0,function*(){logger.info('this.pc.setLocalDescription() resolved');})).catch(error=>{var _a;logErrorAndThrow(exports.WcmeErrorType.CREATE_OFFER_FAILED,"Error: ".concat(error,". SDP: ").concat(maskIp((_a=offer.sdp)!==null&&_a!==void 0?_a:'')));});var answer=this.preProcessRemoteAnswer((_a=this.pc.getRemoteDescription())===null||_a===void 0?void 0:_a.sdp);return this.pc.setRemoteDescription({type:'answer',sdp:answer}).then(()=>__awaiter$1(this,void 0,void 0,function*(){logger.info('this.pc.setRemoteDescription() resolved');})).catch(error=>{logErrorAndThrow(exports.WcmeErrorType.CREATE_OFFER_FAILED,"Error: ".concat(error,". SDP: ").concat(maskIp(answer)));});});}queueLocalOfferAnswer(){return __awaiter$1(this,void 0,void 0,function*(){return this.offerAnswerQueue.push(()=>__awaiter$1(this,void 0,void 0,function*(){yield this.doLocalOfferAnswer();}));});}preProcessLocalOffer(offer){var parsedOffer=parse$1(offer);parsedOffer.avMedia.filter(av=>av.direction==='recvonly').forEach(av=>{var recvTransceiver=this.getRecvTransceiverByMidOrThrow(av.mid);recvTransceiver.mungeLocalDescription(av);});parsedOffer.avMedia.filter(av=>av.direction==='sendrecv'||av.direction==='inactive').forEach(av=>{var sendTransceiver=this.getSendTransceiverByMidOrThrow(av.mid);sendTransceiver.mungeLocalDescription(av);});if(BrowserInfo.isFirefox()){setupBundle(parsedOffer,this.options.bundlePolicy,this.midPredictor.getMidMap());}return parsedOffer.toString();}prepareLocalOfferForRemoteServer(offer){var parsedOffer=parse$1(offer);parsedOffer.avMedia.filter(av=>av.direction==='sendrecv'||av.direction==='inactive').forEach(av=>{var sendTransceiver=this.getSendTransceiverByMidOrThrow(av.mid);sendTransceiver.mungeLocalDescriptionForRemoteServer(av);});parsedOffer.media.filter(media=>media instanceof ApplicationMediaDescription$1).forEach(media=>{injectDummyCandidates(media);});if(BrowserInfo.isFirefox()){setupBundle(parsedOffer,this.options.bundlePolicy,this.midPredictor.getMidMap());if(this.options.bundlePolicy==='max-bundle'){parsedOffer.media.forEach((media,index)=>{if(index>0){media.port=parsedOffer.media[0].port;}});}}filterRecvOnlyMediaDescriptions(parsedOffer);return parsedOffer.toString();}preProcessRemoteAnswer(answer){var _a;var parsedAnswer=parse$1(answer);var parsedOffer=parse$1((_a=this.pc.getLocalDescription())===null||_a===void 0?void 0:_a.sdp);matchMediaDescriptionsInAnswer(parsedOffer,parsedAnswer);parsedAnswer.avMedia.filter(av=>av.direction==='sendonly').forEach(av=>{var recvTransceiver=this.getRecvTransceiverByMidOrThrow(av.mid);recvTransceiver.mungeRemoteDescription(av);});parsedAnswer.avMedia.filter(av=>av.direction==='sendrecv'||av.direction==='recvonly').forEach(av=>{var sendTransceiver=this.getSendTransceiverByMidOrThrow(av.mid);sendTransceiver.mungeRemoteDescription(av);});parsedAnswer.media.filter(media=>media instanceof ApplicationMediaDescription$1).forEach(media=>{if(retainCandidates(media,['udp','tcp'])){logger.log("Some unsupported remote candidates have been removed from mid ".concat(media.mid));}});if(BrowserInfo.isFirefox()){setupBundle(parsedAnswer,this.options.bundlePolicy,this.midPredictor.getMidMap());}return parsedAnswer.toString();}getSendTransceiverOrThrow(mediaType){var sendTransceiver=this.sendTransceivers.get(mediaType);if(!sendTransceiver){logErrorAndThrow(exports.WcmeErrorType.GET_TRANSCEIVER_FAILED,"Unable to find send transceiver for media type ".concat(mediaType,"."));}return sendTransceiver;}getSendTransceiverByMidOrThrow(mid){var transceiver=[...this.sendTransceivers.values()].find(t=>t.mid===mid);if(!transceiver){logErrorAndThrow(exports.WcmeErrorType.GET_TRANSCEIVER_FAILED,"Unable to find send transceiver with MID ".concat(mid,"."));}return transceiver;}getRecvTransceiverByMidOrThrow(mid){var transceiver=[...this.recvTransceivers.values()].flat().find(t=>t.mid===mid);if(!transceiver){logErrorAndThrow(exports.WcmeErrorType.GET_TRANSCEIVER_FAILED,"Unable to find recv transceiver with MID ".concat(mid,"."));}return transceiver;}requestMedia(mediaType,streamRequests){var _a;var task=()=>{var _a;var jmpSession=this.jmpSessions.get(mediaType);if(!jmpSession){logger.error("Unable to find jmp session for ".concat(mediaType));return;}var requestedReceiveSlotIds=[];streamRequests.forEach(sr=>{sr.receiveSlots.forEach(rs=>{if(!rs.id){logger.error("Running stream request task, but ReceiveSlot ID is missing!");return;}requestedReceiveSlotIds.push(rs.id);});});(_a=this.recvTransceivers.get(mediaType))===null||_a===void 0?void 0:_a.forEach(transceiver=>{if(!requestedReceiveSlotIds.some(id=>compareStreamIds(id,transceiver.receiveSlot.id))){transceiver.receiveSlot._updateSource('no source',undefined);}});jmpSession.sendRequests(streamRequests.map(sr=>sr._toJmpStreamRequest()));streamRequests.forEach(sr=>{var _a;var isAffectedInStreamRequest=t=>sr.receiveSlots.some(rs=>rs.id&&compareStreamIds(rs.id,t.receiverId));(_a=this.recvTransceivers.get(mediaType))===null||_a===void 0?void 0:_a.forEach(t=>{var _a,_b;if(isAffectedInStreamRequest(t)){t.requestedBitrate=sr.maxPayloadBitsPerSecond;t.requestedFrameSize=(_b=(_a=sr.codecInfos[0])===null||_a===void 0?void 0:_a.h264)===null||_b===void 0?void 0:_b.maxFs;}else {t.requestedBitrate=undefined;t.requestedFrameSize=undefined;}});});};if(((_a=this.dataChannel)===null||_a===void 0?void 0:_a.readyState)==='open'){task();}else {this.pendingJmpTasks.push(task);}}renewPeerConnection(userOptions){if(userOptions){this.options=Object.assign(Object.assign({},this.options),userOptions);}logger.info("Renewing multistream connection with options ".concat(JSON.stringify(this.options)));this.midPredictor.reset();this.initializePeerConnection();var mainSceneId=generateSceneId();var slidesSceneId=generateSceneId();this.sendTransceivers.forEach((transceiver,mediaType)=>{var _a;var mediaContent=getMediaContent(mediaType);var sceneId=mediaContent===exports.MediaContent.Main?mainSceneId:slidesSceneId;var mid=this.midPredictor.getNextMid(mediaType);transceiver.replaceTransceiver(this.pc.addTransceiver(toMediaStreamTrackKind(mediaType),{direction:'sendrecv',sendEncodings:getMediaFamily(mediaType)===exports.MediaFamily.Video?this.getVideoEncodingOptions(mediaContent):undefined}));transceiver.mid=mid;transceiver.csi=generateCsi(getMediaFamily(mediaType),sceneId);transceiver.resetSdpMunger();(_a=this.jmpSessions.get(mediaType))===null||_a===void 0?void 0:_a.close();this.createJmpSession(mediaType);});this.recvTransceivers.forEach((transceivers,mediaType)=>{transceivers.forEach(t=>{var mid=this.midPredictor.getNextMid(mediaType);t.replaceTransceiver(this.pc.addTransceiver(toMediaStreamTrackKind(mediaType),{direction:'recvonly'}));t.mid=mid;});});}getReceiveSlotById(id){return [...this.recvTransceivers.values()].flat().map(transceiver=>transceiver.receiveSlot).find(receiveSlot=>{var receiveSlotId=receiveSlot.id||{};return Object.keys(receiveSlotId).length===Object.keys(id).length&&Object.keys(receiveSlotId).every(key=>Object.prototype.hasOwnProperty.call(id,key)&&receiveSlotId[key]===id[key]);});}getStats(){return this.statsManager.getStats();}getTransceiverStats(){return __awaiter$1(this,void 0,void 0,function*(){return organizeTransceiverStats(this.sendTransceivers,this.recvTransceivers);});}preProcessStats(stats){return __awaiter$1(this,void 0,void 0,function*(){yield Promise.all([...this.sendTransceivers.entries()].map(_ref9=>{var[mediaType,transceiver]=_ref9;return __awaiter$1(this,void 0,void 0,function*(){(yield transceiver.getStats()).forEach(senderStats=>{var _a;if(senderStats.type==='outbound-rtp'){var statsToModify=stats.get(senderStats.id);statsToModify.mid=transceiver.mid;statsToModify.csi=transceiver.csi;statsToModify.calliopeMediaType=mediaType;statsToModify.requestedBitrate=transceiver.getRequestedBitrate(senderStats.ssrc);statsToModify.requestedFrameSize=transceiver.getRequestedFrameSize(senderStats.ssrc);var trackSettings=(_a=transceiver.publishedStream)===null||_a===void 0?void 0:_a.getSettings();if(trackSettings===null||trackSettings===void 0?void 0:trackSettings.frameRate){statsToModify.targetFrameRate=trackSettings===null||trackSettings===void 0?void 0:trackSettings.frameRate;}stats.set(senderStats.id,statsToModify);}else if(senderStats.type==='media-source'){var _statsToModify=stats.get(senderStats.id);_statsToModify.calliopeMediaType=mediaType;stats.set(senderStats.id,_statsToModify);}});});}));yield Promise.all([...this.recvTransceivers.entries()].map(_ref10=>{var[mediaType,transceivers]=_ref10;return __awaiter$1(this,void 0,void 0,function*(){yield Promise.all(transceivers.map(transceiver=>__awaiter$1(this,void 0,void 0,function*(){(yield transceiver.getStats()).forEach(receiverStats=>{var _a;if(receiverStats.type==='inbound-rtp'){var statsToModify=stats.get(receiverStats.id);statsToModify.mid=(_a=transceiver.receiveSlot.id)===null||_a===void 0?void 0:_a.mid;statsToModify.csi=transceiver.receiveSlot.currentRxCsi;statsToModify.calliopeMediaType=mediaType;statsToModify.requestedBitrate=transceiver.requestedBitrate;statsToModify.requestedFrameSize=transceiver.requestedFrameSize;Object.assign(statsToModify,transceiver.receiverId);stats.set(receiverStats.id,statsToModify);}});})));});}));});}attachMetricsObserver(){this.forceStatsReport=rtcStats_1(this.pc.getUnderlyingRTCPeerConnection(),data=>this.metricsCallback(data),5000,stats=>this.preProcessStats(stats)).forceStatsReport;}forceRtcMetricsCallback(){var _a;return (_a=this.forceStatsReport)===null||_a===void 0?void 0:_a.call(this);}setMetricsCallback(callback){this.metricsCallback=callback;}setOveruseUpdateCallback(callback){this.overuseUpdateCallback=callback;}getCsiByMediaType(mediaType){var _a;return (_a=this.sendTransceivers.get(mediaType))===null||_a===void 0?void 0:_a.csi;}getAllCsis(){return {audioMain:this.getCsiByMediaType(exports.MediaType.AudioMain),audioSlides:this.getCsiByMediaType(exports.MediaType.AudioSlides),videoMain:this.getCsiByMediaType(exports.MediaType.VideoMain),videoSlides:this.getCsiByMediaType(exports.MediaType.VideoSlides)};}}class StreamRequest{constructor(policy,policySpecificInfo,receiveSlots,maxPayloadBitsPerSecond){var codecInfos=arguments.length>4&&arguments[4]!==undefined?arguments[4]:[];this.policy=policy;this.policySpecificInfo=policySpecificInfo;this.receiveSlots=receiveSlots;this.maxPayloadBitsPerSecond=maxPayloadBitsPerSecond;this.codecInfos=codecInfos;}_toJmpStreamRequest(){return new StreamRequest$1(this.policy,this.policySpecificInfo,this.receiveSlots.map(rs=>rs.id),this.maxPayloadBitsPerSecond,this.codecInfos);}}
|
|
4895
|
+
*/function waterfall(tasks,callback){callback=once(callback);if(!Array.isArray(tasks))return callback(new Error('First argument to waterfall must be an array of functions'));if(!tasks.length)return callback();var taskIndex=0;function nextTask(args){var task=wrapAsync(tasks[taskIndex++]);task(...args,onlyOnce(next));}function next(err){if(err===false)return;for(var _len22=arguments.length,args=new Array(_len22>1?_len22-1:0),_key22=1;_key22<_len22;_key22++){args[_key22-1]=arguments[_key22];}if(err||taskIndex===tasks.length){return callback(err,...args);}nextTask(args);}nextTask([]);}awaitify(waterfall);function processTasks(task,finishedCallback){return __awaiter$1(this,void 0,void 0,function*(){try{yield task();finishedCallback();}catch(e){finishedCallback(e);}});}class AsyncQueue{constructor(){this.queue=queue$1(processTasks,1);}push(task){return this.queue.pushAsync(task);}empty(){return this.queue.empty();}}var OfferAnswerType;(function(OfferAnswerType){OfferAnswerType[OfferAnswerType["LocalOnly"]=0]="LocalOnly";OfferAnswerType[OfferAnswerType["Remote"]=1]="Remote";})(OfferAnswerType||(OfferAnswerType={}));class SendOnlyTransceiver extends Transceiver{constructor(rtcRtpTransceiver,mid,csi,munger,mediaType){super(rtcRtpTransceiver,mid);this.rtxEnabled=false;this.streamMuteStateChange=new TypedEvent();this.streamPublishStateChange=new TypedEvent();this.negotiationNeeded=new TypedEvent();this.namedMediaGroupsChange=new TypedEvent();this.requestedIdEncodingParamsMap=new Map();this.updateSendParametersQueue=new AsyncQueue();this.csi=csi;this.direction='sendrecv';this.handleTrackChange=this.handleTrackChange.bind(this);this.handleStreamConstraintsChange=this.handleStreamConstraintsChange.bind(this);this.handleStreamMuteStateChange=this.handleStreamMuteStateChange.bind(this);this.munger=munger;this.mediaType=mediaType;}replaceSenderSource(stream){var _a,_b;return __awaiter$1(this,void 0,void 0,function*(){var trackOrNull=(_a=stream===null||stream===void 0?void 0:stream.outputStream.getTracks()[0])!==null&&_a!==void 0?_a:null;if(((_b=this.sender.track)===null||_b===void 0?void 0:_b.id)!==(trackOrNull===null||trackOrNull===void 0?void 0:trackOrNull.id)){yield this.sender.replaceTrack(trackOrNull);if(trackOrNull){logger.log("Sender source for ".concat(this.mediaType," replaced with track ID ").concat(trackOrNull.id));}else {logger.log("Sender source for ".concat(this.mediaType," set to null, sender stopped"));}}});}handleTrackChange(){return __awaiter$1(this,void 0,void 0,function*(){if(this.requested){yield this.replaceSenderSource(this.publishedStream);}});}handleStreamConstraintsChange(){return __awaiter$1(this,void 0,void 0,function*(){yield this.updateSendParameters(this.requestedIdEncodingParamsMap);});}handleStreamMuteStateChange(){this.streamMuteStateChange.emit();}get requested(){return this.requestedIdEncodingParamsMap.size>0;}replaceTransceiver(newRtcRtpTransceiver){var _super=Object.create(null,{replaceTransceiver:{get:()=>super.replaceTransceiver}});return __awaiter$1(this,void 0,void 0,function*(){_super.replaceTransceiver.call(this,newRtcRtpTransceiver);newRtcRtpTransceiver.direction=this.direction;if(this.requested){yield this.replaceSenderSource(this.publishedStream);}});}replacePublishedStream(newStream){return __awaiter$1(this,void 0,void 0,function*(){var oldStream=this.publishedStream;oldStream===null||oldStream===void 0?void 0:oldStream.off(exports.LocalStreamEventNames.OutputTrackChange,this.handleTrackChange);oldStream===null||oldStream===void 0?void 0:oldStream.off(exports.LocalStreamEventNames.ConstraintsChange,this.handleStreamConstraintsChange);oldStream===null||oldStream===void 0?void 0:oldStream.off(exports.LocalStreamEventNames.UserMuteStateChange,this.handleStreamMuteStateChange);oldStream===null||oldStream===void 0?void 0:oldStream.off(exports.LocalStreamEventNames.SystemMuteStateChange,this.handleStreamMuteStateChange);if(this.requested){yield this.replaceSenderSource(newStream);}this.publishedStream=newStream;newStream===null||newStream===void 0?void 0:newStream.on(exports.LocalStreamEventNames.OutputTrackChange,this.handleTrackChange);newStream===null||newStream===void 0?void 0:newStream.on(exports.LocalStreamEventNames.ConstraintsChange,this.handleStreamConstraintsChange);newStream===null||newStream===void 0?void 0:newStream.on(exports.LocalStreamEventNames.UserMuteStateChange,this.handleStreamMuteStateChange);newStream===null||newStream===void 0?void 0:newStream.on(exports.LocalStreamEventNames.SystemMuteStateChange,this.handleStreamMuteStateChange);if(!oldStream&&newStream&&!newStream.muted||oldStream&&!newStream&&!oldStream.muted){this.streamPublishStateChange.emit();}else if((oldStream===null||oldStream===void 0?void 0:oldStream.muted)!==(newStream===null||newStream===void 0?void 0:newStream.muted)){this.streamMuteStateChange.emit();}});}setNamedMediaGroups(namedMediaGroups){if(this.mediaType!==exports.MediaType.AudioMain){logErrorAndThrow(exports.WcmeErrorType.SET_NMG_FAILED,"Named media groups can only be set for audio.");}this.namedMediaGroups=namedMediaGroups;this.namedMediaGroupsChange.emit();}publishStream(stream){return this.replacePublishedStream(stream);}unpublishStream(){return this.replacePublishedStream();}get active(){return this._rtcRtpTransceiver.direction==='sendrecv';}set active(enabled){this.direction=enabled?'sendrecv':'inactive';this._rtcRtpTransceiver.direction=this.direction;if(this._rtcRtpTransceiver.direction!==this._rtcRtpTransceiver.currentDirection){this.negotiationNeeded.emit(OfferAnswerType.Remote);}}getStats(){return __awaiter$1(this,void 0,void 0,function*(){var statsMap=new Map();var statsReport=yield this.sender.getStats();statsReport.forEach((stats,key)=>{var _a;if(stats.type==='outbound-rtp'){stats.mid=this.mid;stats.csi=this.csi;stats.calliopeMediaType=this.mediaType;stats.requestedBitrate=this.getRequestedBitrate(stats.ssrc);stats.requestedFrameSize=this.getRequestedFrameSize(stats.ssrc);var trackSettings=(_a=this.publishedStream)===null||_a===void 0?void 0:_a.getSettings();if(trackSettings===null||trackSettings===void 0?void 0:trackSettings.frameRate){stats.targetFrameRate=trackSettings===null||trackSettings===void 0?void 0:trackSettings.frameRate;}}else if(stats.type==='media-source'){stats.calliopeMediaType=this.mediaType;}statsMap.set(key,stats);});return statsMap;});}updateSendParameters(requestedIdEncodingParamsMap){return __awaiter$1(this,void 0,void 0,function*(){return this.updateSendParametersQueue.push(()=>__awaiter$1(this,void 0,void 0,function*(){var sendParameters=this.sender.getParameters();sendParameters.encodings.forEach((encoding,index)=>{var _a,_b;var encodingParams=requestedIdEncodingParamsMap.get(index);encoding.active=!!encodingParams;if(encodingParams){var{maxPayloadBitsPerSecond,maxFs,maxWidth,maxHeight}=encodingParams;var scaleDownRatio=getScaleDownRatio((_a=this.publishedStream)===null||_a===void 0?void 0:_a.getSettings().width,(_b=this.publishedStream)===null||_b===void 0?void 0:_b.getSettings().height,maxFs,maxWidth,maxHeight);if(maxPayloadBitsPerSecond!==undefined&&maxPayloadBitsPerSecond>=0){encoding.maxBitrate=maxPayloadBitsPerSecond;}if(scaleDownRatio!==undefined&&scaleDownRatio>=1.0){encoding.scaleResolutionDownBy=scaleDownRatio;}}});yield this.sender.setParameters(sendParameters);var oldRequested=this.requested;var newRequested=requestedIdEncodingParamsMap.size>0;this.requestedIdEncodingParamsMap=requestedIdEncodingParamsMap;if(oldRequested!==newRequested){yield this.replaceSenderSource(newRequested?this.publishedStream:null);}}));});}getRequestedBitrate(ssrc){var index=this.senderIds.findIndex(streamId=>streamId.ssrc===ssrc);if(this.requestedIdEncodingParamsMap.has(index)){var encodingParams=this.requestedIdEncodingParamsMap.get(index);return encodingParams===null||encodingParams===void 0?void 0:encodingParams.maxPayloadBitsPerSecond;}return undefined;}getRequestedFrameSize(ssrc){var index=this.senderIds.findIndex(streamId=>streamId.ssrc===ssrc);if(this.requestedIdEncodingParamsMap.has(index)){var encodingParams=this.requestedIdEncodingParamsMap.get(index);return encodingParams===null||encodingParams===void 0?void 0:encodingParams.maxFs;}return undefined;}isSimulcastEnabled(){var params=this.sender.getParameters();return params.encodings.length>1;}mungeLocalDescription(mediaDescription){this.munger.mungeLocalDescription(mediaDescription,{simulcastEnabled:this.isSimulcastEnabled(),rtxEnabled:this.rtxEnabled,twccDisabled:this.twccDisabled});}mungeLocalDescriptionForRemoteServer(mediaDescription){this.munger.mungeLocalDescriptionForRemoteServer(mediaDescription,getMediaContent(this.mediaType),this.csi);}mungeRemoteDescription(mediaDescription){this.munger.mungeRemoteDescription(mediaDescription);}get senderIds(){return this.munger.getSenderIds();}get numActiveSimulcastLayers(){var _a;if(getMediaFamily(this.mediaType)===exports.MediaFamily.Video){return (_a=this.publishedStream)===null||_a===void 0?void 0:_a.getNumActiveSimulcastLayers();}return this.publishedStream?0:undefined;}getEncodingIndexForStreamId(id){return this.munger.getEncodingIndexForStreamId(id);}resetSdpMunger(){this.munger.reset();}setCodecParameters(parameters){this.munger.setCodecParameters(parameters);this.negotiationNeeded.emit(OfferAnswerType.LocalOnly);}deleteCodecParameters(parameters){this.munger.deleteCodecParameters(parameters);this.negotiationNeeded.emit(OfferAnswerType.LocalOnly);}}class SendSlot{constructor(sendTransceiver){this.sendTransceiver=sendTransceiver;}publishStream(stream){return __awaiter$1(this,void 0,void 0,function*(){if(stream===this.sendTransceiver.publishedStream){return Promise.resolve();}return this.sendTransceiver.publishStream(stream);});}unpublishStream(){return __awaiter$1(this,void 0,void 0,function*(){if(!this.sendTransceiver.publishedStream){return Promise.resolve();}return this.sendTransceiver.unpublishStream();});}setNamedMediaGroups(namedMediaGroups){this.sendTransceiver.setNamedMediaGroups(namedMediaGroups);}clearNamedMediaGroups(){this.setNamedMediaGroups([]);}get active(){return this.sendTransceiver.active;}set active(active){this.sendTransceiver.active=active;}setCodecParameters(parameters){return __awaiter$1(this,void 0,void 0,function*(){this.sendTransceiver.setCodecParameters(parameters);});}deleteCodecParameters(parameters){return __awaiter$1(this,void 0,void 0,function*(){this.sendTransceiver.deleteCodecParameters(parameters);});}}class StatsManager{constructor(statsGetter){var statsPreprocessor=arguments.length>1&&arguments[1]!==undefined?arguments[1]:()=>__awaiter$1(this,void 0,void 0,function*(){});this.statsGetter=statsGetter;this.statsPreProcessor=statsPreprocessor;}getStats(){return __awaiter$1(this,void 0,void 0,function*(){var statsReport=yield this.statsGetter();var statsMap=new Map();statsReport.forEach((stats,key)=>statsMap.set(key,stats));yield this.statsPreProcessor(statsMap);return statsMap;});}}var organizeTransceiverStats=(sendTransceivers,recvTransceivers)=>__awaiter$1(void 0,void 0,void 0,function*(){var result={audio:{senders:[],receivers:[]},video:{senders:[],receivers:[]},screenShareAudio:{senders:[],receivers:[]},screenShareVideo:{senders:[],receivers:[]}};yield Promise.all([...sendTransceivers.entries()].map(_ref6=>{var[mediaType,transceiver]=_ref6;return __awaiter$1(void 0,void 0,void 0,function*(){var _a;var item={report:yield transceiver.getStats(),mid:transceiver.mid,csi:transceiver.csi,currentDirection:'sendonly',localTrackLabel:(_a=transceiver.publishedStream)===null||_a===void 0?void 0:_a.label};if(mediaType===exports.MediaType.AudioMain){result.audio.senders.push(item);}if(mediaType===exports.MediaType.VideoMain){result.video.senders.push(item);}if(mediaType===exports.MediaType.AudioSlides){result.screenShareAudio.senders.push(item);}if(mediaType===exports.MediaType.VideoSlides){result.screenShareVideo.senders.push(item);}});}));yield Promise.all([...recvTransceivers.entries()].map(_ref7=>{var[mediaType,transceivers]=_ref7;return __awaiter$1(void 0,void 0,void 0,function*(){return Promise.all(transceivers.map(t=>__awaiter$1(void 0,void 0,void 0,function*(){var _b,_c;var item={report:yield t.getStats(),mid:(_b=t.receiveSlot.id)===null||_b===void 0?void 0:_b.mid,csi:t.receiveSlot.currentRxCsi,currentDirection:'recvonly',localTrackLabel:(_c=t.receiveSlot.stream.getTracks()[0])===null||_c===void 0?void 0:_c.label};if(mediaType===exports.MediaType.AudioMain){result.audio.receivers.push(item);}if(mediaType===exports.MediaType.VideoMain){result.video.receivers.push(item);}if(mediaType===exports.MediaType.AudioSlides){result.screenShareAudio.receivers.push(item);}if(mediaType===exports.MediaType.VideoSlides){result.screenShareVideo.receivers.push(item);}})));});}));return result;});function toMediaStreamTrackKind(mediaType){return [exports.MediaType.VideoMain,exports.MediaType.VideoSlides].includes(mediaType)?exports.MediaStreamTrackKind.Video:exports.MediaStreamTrackKind.Audio;}function webRtcVideoContentHintToJmpVideoContentHint(hint){if(hint==='motion'){return 'motion';}if(hint==='detail'){return 'sharpness';}return undefined;}var MultistreamConnectionEventNames;(function(MultistreamConnectionEventNames){MultistreamConnectionEventNames["VideoSourceCountUpdate"]="video-source-count-update";MultistreamConnectionEventNames["AudioSourceCountUpdate"]="audio-source-count-update";MultistreamConnectionEventNames["ActiveSpeakerNotification"]="active-speaker-notification";MultistreamConnectionEventNames["ConnectionStateUpdate"]="connection-state-update";MultistreamConnectionEventNames["IceGatheringStateUpdate"]="ice-gathering-state-update";MultistreamConnectionEventNames["NegotiationNeeded"]="negotiation-needed";MultistreamConnectionEventNames["CreateOfferOnSuccess"]="createofferonsuccess";MultistreamConnectionEventNames["CreateAnswerOnSuccess"]="createansweronsuccess";MultistreamConnectionEventNames["SetLocalDescriptionOnSuccess"]="setlocaldescriptiononsuccess";MultistreamConnectionEventNames["SetRemoteDescriptionOnSuccess"]="setremotedescriptiononsuccess";})(MultistreamConnectionEventNames||(MultistreamConnectionEventNames={}));var defaultMultistreamConnectionOptions={disableSimulcast:BrowserInfo.isFirefox(),bundlePolicy:'max-compat',iceServers:undefined,disableContentSimulcast:true,disableAudioTwcc:true};class MultistreamConnection extends EventEmitter$2{constructor(){var userOptions=arguments.length>0&&arguments[0]!==undefined?arguments[0]:{};super();this.sendTransceivers=new Map();this.recvTransceivers=new Map();this.jmpSessions=new Map();this.pendingJmpTasks=[];this.metricsCallback=()=>{};this.overuseUpdateCallback=()=>{};this.midPredictor=new MidPredictor();this.offerAnswerQueue=new AsyncQueue();this.currentCreateOfferId=0;this.options=Object.assign(Object.assign({},defaultMultistreamConnectionOptions),userOptions);logger.info("Creating multistream connection with options ".concat(JSON.stringify(this.options)));this.initializePeerConnection();this.overuseStateManager=new OveruseStateManager(overuseState=>this.overuseUpdateCallback(overuseState));this.overuseStateManager.start();this.statsManager=new StatsManager(()=>this.pc.getStats(),stats=>this.preProcessStats(stats));var mainSceneId=generateSceneId();var slidesSceneId=generateSceneId();var videoMainEncodingOptions=this.getVideoEncodingOptions(exports.MediaContent.Main);var videoSlidesEncodingOptions=this.getVideoEncodingOptions(exports.MediaContent.Slides);this.createSendTransceiver(exports.MediaType.VideoMain,mainSceneId,videoMainEncodingOptions);this.createSendTransceiver(exports.MediaType.AudioMain,mainSceneId);this.createSendTransceiver(exports.MediaType.VideoSlides,slidesSceneId,videoSlidesEncodingOptions);this.createSendTransceiver(exports.MediaType.AudioSlides,slidesSceneId);}initializePeerConnection(){var _a;(_a=this.pc)===null||_a===void 0?void 0:_a.close();this.pc=new PeerConnection({iceServers:this.options.iceServers,bundlePolicy:this.options.bundlePolicy});this.propagatePeerConnectionEvents();this.attachMetricsObserver();this.createDataChannel();}propagatePeerConnectionEvents(){this.pc.on(PeerConnection.Events.ConnectionStateChange,state=>{this.emit(MultistreamConnectionEventNames.ConnectionStateUpdate,state);});this.pc.on(PeerConnection.Events.CreateOfferOnSuccess,description=>{this.emit(MultistreamConnectionEventNames.CreateOfferOnSuccess,description);});this.pc.on(PeerConnection.Events.CreateAnswerOnSuccess,description=>{this.emit(MultistreamConnectionEventNames.CreateAnswerOnSuccess,description);});this.pc.on(PeerConnection.Events.SetLocalDescriptionOnSuccess,description=>{this.emit(MultistreamConnectionEventNames.SetLocalDescriptionOnSuccess,description);});this.pc.on(PeerConnection.Events.SetRemoteDescriptionOnSuccess,description=>{this.emit(MultistreamConnectionEventNames.SetRemoteDescriptionOnSuccess,description);});this.pc.on(PeerConnection.Events.IceGatheringStateChange,()=>{this.emit(MultistreamConnectionEventNames.IceGatheringStateUpdate,this.getIceGatheringState());});}getConnectionState(){return this.pc.getConnectionState();}getIceGatheringState(){return this.pc.iceGatheringState;}getVideoEncodingOptions(content){var enabledSimulcast=content===exports.MediaContent.Main?!this.options.disableSimulcast:!this.options.disableContentSimulcast;return enabledSimulcast?[{scaleResolutionDownBy:4,active:false},{scaleResolutionDownBy:2,active:false},{active:false}]:[{active:false}];}createSendTransceiver(mediaType,sceneId,sendEncodingsOptions){var rtcTransceiver;try{rtcTransceiver=this.pc.addTransceiver(toMediaStreamTrackKind(mediaType),{direction:'sendrecv',sendEncodings:sendEncodingsOptions});}catch(e){logger.error("addTransceiver failed due to : ".concat(e));throw e;}var mid=this.midPredictor.getNextMid(mediaType);var csi=generateCsi(getMediaFamily(mediaType),sceneId);var munger=new EgressSdpMunger();var transceiver=new SendOnlyTransceiver(rtcTransceiver,mid,csi,munger,mediaType);if(getMediaFamily(mediaType)===exports.MediaFamily.Video){transceiver.rtxEnabled=true;transceiver.setCodecParameters({'max-mbps':"".concat(defaultMaxVideoEncodeMbps),'max-fs':"".concat(defaultMaxVideoEncodeFrameSize)});}transceiver.twccDisabled=getMediaFamily(mediaType)===exports.MediaFamily.Audio?this.options.disableAudioTwcc:false;transceiver.active=false;transceiver.streamMuteStateChange.on(()=>{this.sendSourceAdvertisement(mediaType);this.sendMediaRequestStatus(mediaType);});transceiver.streamPublishStateChange.on(()=>{this.sendSourceAdvertisement(mediaType);this.sendMediaRequestStatus(mediaType);});transceiver.negotiationNeeded.on(offerAnswerType=>{if(offerAnswerType===OfferAnswerType.Remote){this.emit(MultistreamConnectionEventNames.NegotiationNeeded);}else if(this.pc.getRemoteDescription()){this.queueLocalOfferAnswer();}});transceiver.namedMediaGroupsChange.on(()=>{this.sendSourceAdvertisement(mediaType);});this.sendTransceivers.set(mediaType,transceiver);this.createJmpSession(mediaType);}createSendSlot(mediaType){var active=arguments.length>1&&arguments[1]!==undefined?arguments[1]:true;var transceiver=this.getSendTransceiverOrThrow(mediaType);transceiver.active=active;return new SendSlot(transceiver);}createJmpSession(mediaType){var jmpSession=new JmpSession(getMediaFamily(mediaType),getMediaContent(mediaType));jmpSession.setTxCallback(msg=>{var _a;if(((_a=this.dataChannel)===null||_a===void 0?void 0:_a.readyState)!=='open'){logger.error("DataChannel not created or not connected. Unable to send JMP message.");return;}try{logger.info("Sending JMP message (size: ".concat(msg.length,"): ").concat(msg));this.dataChannel.send(msg);}catch(err){var{bufferedAmount,readyState}=this.dataChannel;var{sctp}=this.pc.getUnderlyingRTCPeerConnection();var{maxMessageSize,state}=sctp||{};logErrorAndThrow(exports.WcmeErrorType.DATA_CHANNEL_SEND_FAILED,"Sending JMP message failed with error: ".concat(err,",\nMessage size: ").concat(msg.length,",\nData Channel State: ").concat(readyState,",\nData Channel Buffered amount: ").concat(bufferedAmount,",\nSCTP State: ").concat(state,",\nSCTP Max Message Size: ").concat(maxMessageSize));}});var prevNumTotalSources=0;var prevNumLiveSources=0;jmpSession.on(JmpSessionEvents.SourceAdvertisementReceived,data=>{logger.log("SourceAdvertisement received: ".concat(JSON.stringify(data)));if(data.numTotalSources!==prevNumTotalSources||data.numLiveSources!==prevNumLiveSources){prevNumTotalSources=data.numTotalSources;prevNumLiveSources=data.numLiveSources;var eventName=getMediaFamily(mediaType)===exports.MediaFamily.Video?MultistreamConnectionEventNames.VideoSourceCountUpdate:MultistreamConnectionEventNames.AudioSourceCountUpdate;this.emit(eventName,data.numTotalSources,data.numLiveSources,getMediaContent(mediaType));}else {logger.log('Number of sources was unchanged, ignoring message');}});jmpSession.on(JmpSessionEvents.MediaRequestStatusReceived,data=>{logger.log("MediaRequestStatus received: ".concat(JSON.stringify(data)));data.streamStates.forEach(s=>{var receiveSlot=this.getReceiveSlotById(s.id);if(!receiveSlot){logger.warn("Got MediaRequestStatus for unknown receive slot: ".concat(JSON.stringify(s.id)));return;}receiveSlot._updateSource(s.state,s.csi);});});jmpSession.on(JmpSessionEvents.MediaRequestReceived,data=>{logger.log("MediaRequest received: ".concat(JSON.stringify(data)));this.updateRequestedStreams(mediaType,data.requests).then(()=>{if(getMediaFamily(mediaType)===exports.MediaFamily.Video){this.sendMediaRequestStatus(mediaType);}});});jmpSession.on(JmpSessionEvents.ActiveSpeaker,data=>{this.emit(MultistreamConnectionEventNames.ActiveSpeakerNotification,data.csis);});this.jmpSessions.set(mediaType,jmpSession);}updateRequestedStreams(mediaType,requests){var sendTransceiver=this.getSendTransceiverOrThrow(mediaType);var mediaFamily=getMediaFamily(mediaType);var requestedIdEncodingParamsMap=new Map();var rsRequests=requests.filter(r=>isValidReceiverSelectedInfo(r.policySpecificInfo));if(rsRequests.length!==requests.length){logger.warn('Ignoring non-receiver-selected requests');}rsRequests.forEach(_ref8=>{var{ids,policySpecificInfo,codecInfos,maxPayloadBitsPerSecond}=_ref8;var _a,_b,_c;if(ids.length>1){logErrorAndThrow(exports.WcmeErrorType.INVALID_STREAM_REQUEST,"Stream request cannot have more than one ID.");}if(ids.length===0){return;}if(sendTransceiver.csi!==policySpecificInfo.csi){logger.warn('csi in the StreamRequest does not match');return;}var id=ids[0];var codecInfo=codecInfos[0];var streamIdsMatched=sendTransceiver.senderIds.some(validId=>compareStreamIds(id,validId));if(streamIdsMatched){var encodingIndex=sendTransceiver.getEncodingIndexForStreamId(id);if(encodingIndex!==-1){var encodingParams={maxPayloadBitsPerSecond};if(mediaFamily===exports.MediaFamily.Video){encodingParams.maxFs=(_a=codecInfo===null||codecInfo===void 0?void 0:codecInfo.h264)===null||_a===void 0?void 0:_a.maxFs;encodingParams.maxWidth=(_b=codecInfo===null||codecInfo===void 0?void 0:codecInfo.h264)===null||_b===void 0?void 0:_b.maxWidth;encodingParams.maxHeight=(_c=codecInfo===null||codecInfo===void 0?void 0:codecInfo.h264)===null||_c===void 0?void 0:_c.maxHeight;}requestedIdEncodingParamsMap.set(encodingIndex,encodingParams);}else {logger.warn("".concat(mediaType,": Unable to get encoding index for stream ID: ").concat(JSON.stringify(id)));}}else {logger.warn("".concat(mediaType,": Unable to find matching stream ID for requested ID: ").concat(JSON.stringify(id)));}});return sendTransceiver.updateSendParameters(requestedIdEncodingParamsMap);}createDataChannel(){var dataChannel=this.pc.createDataChannel('datachannel',{ordered:false,maxRetransmits:0});dataChannel.onopen=event=>{logger.info('DataChannel opened:',JSON.stringify(event));[...this.sendTransceivers.keys()].forEach(mediaType=>{this.sendSourceAdvertisement(mediaType);});logger.info("Flushing pending JMP task queue");this.pendingJmpTasks.forEach(t=>t());this.pendingJmpTasks=[];};dataChannel.onmessage=e=>{var parsed;try{parsed=JSON.parse(e.data);}catch(err){logger.error("Error parsing datachannel JSON: ".concat(err));return;}logger.debug('DataChannel got msg:',e.data);var homerMsg=HomerMsg.fromJson(parsed);if(!homerMsg){logger.error("Received invalid datachannel message: ".concat(e));return;}var jmpMsg=homerMsg.payload;if(!isValidJmpMsg(jmpMsg)){logger.error("Received invalid JMP msg: ".concat(JSON.stringify(jmpMsg)));return;}var mediaType=getMediaType(jmpMsg.mediaFamily,jmpMsg.mediaContent);var jmpSession=this.jmpSessions.get(mediaType);if(!jmpSession){logger.error("Unable to find JMP session for media type ".concat(mediaType,"."));return;}jmpSession.receive(jmpMsg);};dataChannel.onclose=event=>{logger.info('DataChannel closed:',JSON.stringify(event));};dataChannel.onerror=event=>{logger.info('DataChannel error:',JSON.stringify(event));};this.dataChannel=dataChannel;}close(){this.sendTransceivers.forEach(t=>t.close());this.recvTransceivers.forEach(recvTransceivers=>{recvTransceivers.forEach(t=>t.close());});this.jmpSessions.forEach(jmpSession=>jmpSession.close());this.pc.close();}sendMediaRequestStatus(mediaType){var _a;if(getMediaFamily(mediaType)!==exports.MediaFamily.Video){return;}if(!this.getSendTransceiverOrThrow(mediaType).requested){return;}var streamStates=this.getVideoStreamStates(mediaType);var task=()=>{var _a;(_a=this.jmpSessions.get(mediaType))===null||_a===void 0?void 0:_a.sendMediaRequestStatus(streamStates);};if(((_a=this.dataChannel)===null||_a===void 0?void 0:_a.readyState)==='open'){task();}else {this.pendingJmpTasks.push(task);}}sendSourceAdvertisement(mediaType){var _a,_b;var transceiver=this.getSendTransceiverOrThrow(mediaType);var numLiveSources=((_a=transceiver.publishedStream)===null||_a===void 0?void 0:_a.muted)===false?1:0;var task;if(getMediaFamily(mediaType)===exports.MediaFamily.Video){var sources=this.getVideoStreamStates(mediaType);if(sources===null){return;}var contentHint;if(transceiver.publishedStream&&mediaType===exports.MediaType.VideoSlides){contentHint=transceiver.publishedStream.contentHint;}task=()=>{var _a;(_a=this.jmpSessions.get(mediaType))===null||_a===void 0?void 0:_a.sendSourceAdvertisement(1,numLiveSources,[],webRtcVideoContentHintToJmpVideoContentHint(contentHint));};}else {task=()=>{var _a;return (_a=this.jmpSessions.get(mediaType))===null||_a===void 0?void 0:_a.sendSourceAdvertisement(1,numLiveSources,mediaType===exports.MediaType.AudioMain?transceiver.namedMediaGroups:[]);};}if(((_b=this.dataChannel)===null||_b===void 0?void 0:_b.readyState)==='open'){task();}else {this.pendingJmpTasks.push(task);}}getVideoStreamStates(mediaType){var _a;var sendTransceiver=this.getSendTransceiverOrThrow(mediaType);var published=!!sendTransceiver.publishedStream;var muted=(_a=sendTransceiver.publishedStream)===null||_a===void 0?void 0:_a.muted;return sendTransceiver.senderIds.map(id=>{var state;if(!published){state='no source';}else if(muted){state='avatar';}else {state='live';}return {id,state,csi:sendTransceiver.csi};});}createReceiveTransceiver(mediaType){var rtcRtpTransceiver=this.pc.addTransceiver(toMediaStreamTrackKind(mediaType),{direction:'recvonly'});var transceiverMid=this.midPredictor.getNextMid(mediaType);var munger=new IngressSdpMunger();var recvOnlyTransceiver=new ReceiveOnlyTransceiver(rtcRtpTransceiver,transceiverMid,munger);recvOnlyTransceiver.metadata.mediaType=mediaType;recvOnlyTransceiver.twccDisabled=getMediaFamily(mediaType)===exports.MediaFamily.Audio?this.options.disableAudioTwcc:false;this.recvTransceivers.set(mediaType,[...(this.recvTransceivers.get(mediaType)||[]),recvOnlyTransceiver]);return recvOnlyTransceiver;}createReceiveSlot(mediaType){return __awaiter$1(this,void 0,void 0,function*(){return (yield this.createReceiveSlots(mediaType,1))[0];});}createReceiveSlots(mediaType){var count=arguments.length>1&&arguments[1]!==undefined?arguments[1]:1;return __awaiter$1(this,void 0,void 0,function*(){return new Promise(createReceiveSlotsResolve=>{this.offerAnswerQueue.push(()=>__awaiter$1(this,void 0,void 0,function*(){var createdReceiveOnlyTransceivers=[];for(var i=0;i<count;i++){var recvOnlyTransceiver=this.createReceiveTransceiver(mediaType);createdReceiveOnlyTransceivers.push(recvOnlyTransceiver);}if(this.pc.getRemoteDescription()){yield this.doLocalOfferAnswer();}var receiveSlots=createdReceiveOnlyTransceivers.map(transceiver=>transceiver.receiveSlot);createReceiveSlotsResolve(receiveSlots);}));});});}getIngressPayloadType(mediaType,mimeType){var _a,_b,_c;var requestedMediaCodecType=mimeType.split('/')[1];var requestedMid=(_a=this.sendTransceivers.get(mediaType))===null||_a===void 0?void 0:_a.mid;var parsedOffer=parse$1((_b=this.pc.getLocalDescription())===null||_b===void 0?void 0:_b.sdp);var parsedAnswer=parse$1((_c=this.pc.getRemoteDescription())===null||_c===void 0?void 0:_c.sdp);var senderCodecs=parsedAnswer.avMedia.filter(media=>requestedMid===media.mid).map(media=>[...media.codecs.values()]).flat().filter(ci=>ci.name===requestedMediaCodecType);var receiverCodecs=parsedOffer.avMedia.filter(media=>requestedMid===media.mid).map(media=>[...media.codecs.values()]).flat().filter(ci=>ci.name===requestedMediaCodecType);if(!senderCodecs||!receiverCodecs||senderCodecs.length===0||receiverCodecs.length===0){logErrorAndThrow(exports.WcmeErrorType.GET_PAYLOAD_TYPE_FAILED,"Sender codecs or receiver codecs is undefined or empty.");}var senderCodec=senderCodecs[0];var targetCodec=receiverCodecs.find(receiverCodec=>{return areCodecsCompatible(senderCodec,receiverCodec);});if(!targetCodec||!targetCodec.pt){logErrorAndThrow(exports.WcmeErrorType.GET_PAYLOAD_TYPE_FAILED,"Cannot find matching receiver codec for the given mediaType/mimeType.");}return targetCodec.pt;}createOffer(){return __awaiter$1(this,void 0,void 0,function*(){if(!this.pc.getLocalDescription()){this.midPredictor.allocateMidForDatachannel();}if(this.setAnswerResolve){logger.info('Canceling previous offer since setAnswer was never called for it');this.setAnswerResolve();this.setAnswerResolve=undefined;}var createOfferId=++this.currentCreateOfferId;return new Promise((createOfferResolve,createOfferReject)=>{this.offerAnswerQueue.push(()=>__awaiter$1(this,void 0,void 0,function*(){var _a;try{var offer=yield this.pc.createOffer();if(!offer.sdp){logErrorAndThrow(exports.WcmeErrorType.CREATE_OFFER_FAILED,'SDP not found in offer.');}offer.sdp=this.preProcessLocalOffer(offer.sdp);yield this.pc.setLocalDescription(offer).then(()=>__awaiter$1(this,void 0,void 0,function*(){logger.info('this.pc.setLocalDescription() resolved');})).catch(error=>{var _a;logErrorAndThrow(exports.WcmeErrorType.CREATE_OFFER_FAILED,"Error: ".concat(error,". SDP: ").concat(maskIp((_a=offer.sdp)!==null&&_a!==void 0?_a:'')));});var sdpToSend=this.prepareLocalOfferForRemoteServer((_a=this.pc.getLocalDescription())===null||_a===void 0?void 0:_a.sdp);createOfferResolve({type:'offer',sdp:sdpToSend});if(this.currentCreateOfferId>createOfferId){logger.log('Canceling previous offer since createOffer was called while it was being created');}else {yield new Promise(setAnswerResolve=>{this.setAnswerResolve=setAnswerResolve;});}}catch(error){createOfferReject(error);}}));});});}setAnswer(answer){return __awaiter$1(this,void 0,void 0,function*(){var sdp=this.preProcessRemoteAnswer(answer);if(!this.setAnswerResolve){logErrorAndThrow(exports.WcmeErrorType.SET_ANSWER_FAILED,"Call to setAnswer without having previously called createOffer.");}logger.info('calling this.pc.setRemoteDescription()');return this.pc.setRemoteDescription({type:'answer',sdp}).then(()=>__awaiter$1(this,void 0,void 0,function*(){logger.info('this.pc.setRemoteDescription() resolved');if(this.setAnswerResolve){this.setAnswerResolve();this.setAnswerResolve=undefined;}else {logger.debug("setAnswerResolve function was cleared between setAnswer and result of setRemoteDescription");}})).catch(error=>{logErrorAndThrow(exports.WcmeErrorType.SET_ANSWER_FAILED,"Error: ".concat(error,". SDP: ").concat(maskIp(answer)));});});}doLocalOfferAnswer(){var _a;return __awaiter$1(this,void 0,void 0,function*(){var offer=yield this.pc.createOffer();if(!offer.sdp){logErrorAndThrow(exports.WcmeErrorType.CREATE_OFFER_FAILED,'SDP not found in offer.');}offer.sdp=this.preProcessLocalOffer(offer.sdp);yield this.pc.setLocalDescription(offer).then(()=>__awaiter$1(this,void 0,void 0,function*(){logger.info('this.pc.setLocalDescription() resolved');})).catch(error=>{var _a;logErrorAndThrow(exports.WcmeErrorType.CREATE_OFFER_FAILED,"Error: ".concat(error,". SDP: ").concat(maskIp((_a=offer.sdp)!==null&&_a!==void 0?_a:'')));});var answer=this.preProcessRemoteAnswer((_a=this.pc.getRemoteDescription())===null||_a===void 0?void 0:_a.sdp);return this.pc.setRemoteDescription({type:'answer',sdp:answer}).then(()=>__awaiter$1(this,void 0,void 0,function*(){logger.info('this.pc.setRemoteDescription() resolved');})).catch(error=>{logErrorAndThrow(exports.WcmeErrorType.CREATE_OFFER_FAILED,"Error: ".concat(error,". SDP: ").concat(maskIp(answer)));});});}queueLocalOfferAnswer(){return __awaiter$1(this,void 0,void 0,function*(){return this.offerAnswerQueue.push(()=>__awaiter$1(this,void 0,void 0,function*(){yield this.doLocalOfferAnswer();}));});}preProcessLocalOffer(offer){var parsedOffer=parse$1(offer);parsedOffer.avMedia.filter(av=>av.direction==='recvonly').forEach(av=>{var recvTransceiver=this.getRecvTransceiverByMidOrThrow(av.mid);recvTransceiver.mungeLocalDescription(av);});parsedOffer.avMedia.filter(av=>av.direction==='sendrecv'||av.direction==='inactive').forEach(av=>{var sendTransceiver=this.getSendTransceiverByMidOrThrow(av.mid);sendTransceiver.mungeLocalDescription(av);});if(BrowserInfo.isFirefox()){setupBundle(parsedOffer,this.options.bundlePolicy,this.midPredictor.getMidMap());}return parsedOffer.toString();}prepareLocalOfferForRemoteServer(offer){var parsedOffer=parse$1(offer);parsedOffer.avMedia.filter(av=>av.direction==='sendrecv'||av.direction==='inactive').forEach(av=>{var sendTransceiver=this.getSendTransceiverByMidOrThrow(av.mid);sendTransceiver.mungeLocalDescriptionForRemoteServer(av);});parsedOffer.media.filter(media=>media instanceof ApplicationMediaDescription$1).forEach(media=>{injectDummyCandidates(media);});if(BrowserInfo.isFirefox()){setupBundle(parsedOffer,this.options.bundlePolicy,this.midPredictor.getMidMap());if(this.options.bundlePolicy==='max-bundle'){parsedOffer.media.forEach((media,index)=>{if(index>0){media.port=parsedOffer.media[0].port;}});}}filterRecvOnlyMediaDescriptions(parsedOffer);return parsedOffer.toString();}preProcessRemoteAnswer(answer){var _a;var parsedAnswer=parse$1(answer);var parsedOffer=parse$1((_a=this.pc.getLocalDescription())===null||_a===void 0?void 0:_a.sdp);matchMediaDescriptionsInAnswer(parsedOffer,parsedAnswer);parsedAnswer.avMedia.filter(av=>av.direction==='sendonly').forEach(av=>{var recvTransceiver=this.getRecvTransceiverByMidOrThrow(av.mid);recvTransceiver.mungeRemoteDescription(av);});parsedAnswer.avMedia.filter(av=>av.direction==='sendrecv'||av.direction==='recvonly').forEach(av=>{var sendTransceiver=this.getSendTransceiverByMidOrThrow(av.mid);sendTransceiver.mungeRemoteDescription(av);});parsedAnswer.media.filter(media=>media instanceof ApplicationMediaDescription$1).forEach(media=>{if(retainCandidates(media,['udp','tcp'])){logger.log("Some unsupported remote candidates have been removed from mid ".concat(media.mid));}});if(BrowserInfo.isFirefox()){setupBundle(parsedAnswer,this.options.bundlePolicy,this.midPredictor.getMidMap());}return parsedAnswer.toString();}getSendTransceiverOrThrow(mediaType){var sendTransceiver=this.sendTransceivers.get(mediaType);if(!sendTransceiver){logErrorAndThrow(exports.WcmeErrorType.GET_TRANSCEIVER_FAILED,"Unable to find send transceiver for media type ".concat(mediaType,"."));}return sendTransceiver;}getSendTransceiverByMidOrThrow(mid){var transceiver=[...this.sendTransceivers.values()].find(t=>t.mid===mid);if(!transceiver){logErrorAndThrow(exports.WcmeErrorType.GET_TRANSCEIVER_FAILED,"Unable to find send transceiver with MID ".concat(mid,"."));}return transceiver;}getRecvTransceiverByMidOrThrow(mid){var transceiver=[...this.recvTransceivers.values()].flat().find(t=>t.mid===mid);if(!transceiver){logErrorAndThrow(exports.WcmeErrorType.GET_TRANSCEIVER_FAILED,"Unable to find recv transceiver with MID ".concat(mid,"."));}return transceiver;}requestMedia(mediaType,streamRequests){var _a;var task=()=>{var _a;var jmpSession=this.jmpSessions.get(mediaType);if(!jmpSession){logger.error("Unable to find jmp session for ".concat(mediaType));return;}var requestedReceiveSlotIds=[];streamRequests.forEach(request=>{request.receiveSlots.forEach(slot=>{if(!slot.id){logger.error("Running stream request task, but ReceiveSlot ID is missing!");return;}requestedReceiveSlotIds.push(slot.id);});});jmpSession.sendRequests(streamRequests.map(sr=>sr._toJmpStreamRequest()));(_a=this.recvTransceivers.get(mediaType))===null||_a===void 0?void 0:_a.forEach(transceiver=>{if(requestedReceiveSlotIds.some(id=>compareStreamIds(id,transceiver.receiveSlot.id))){var relevantRequest=streamRequests.find(request=>request.receiveSlots.some(slot=>compareStreamIds(slot.id,transceiver.receiveSlot.id)));transceiver.handleRequested(relevantRequest);}else {transceiver.handleUnrequested();}});};if(((_a=this.dataChannel)===null||_a===void 0?void 0:_a.readyState)==='open'){task();}else {this.pendingJmpTasks.push(task);}}renewPeerConnection(userOptions){if(userOptions){this.options=Object.assign(Object.assign({},this.options),userOptions);}logger.info("Renewing multistream connection with options ".concat(JSON.stringify(this.options)));this.midPredictor.reset();this.initializePeerConnection();var mainSceneId=generateSceneId();var slidesSceneId=generateSceneId();this.sendTransceivers.forEach((transceiver,mediaType)=>{var _a;var mediaContent=getMediaContent(mediaType);var sceneId=mediaContent===exports.MediaContent.Main?mainSceneId:slidesSceneId;var mid=this.midPredictor.getNextMid(mediaType);transceiver.replaceTransceiver(this.pc.addTransceiver(toMediaStreamTrackKind(mediaType),{direction:'sendrecv',sendEncodings:getMediaFamily(mediaType)===exports.MediaFamily.Video?this.getVideoEncodingOptions(mediaContent):undefined}));transceiver.mid=mid;transceiver.csi=generateCsi(getMediaFamily(mediaType),sceneId);transceiver.resetSdpMunger();(_a=this.jmpSessions.get(mediaType))===null||_a===void 0?void 0:_a.close();this.createJmpSession(mediaType);});this.recvTransceivers.forEach((transceivers,mediaType)=>{transceivers.forEach(t=>{var mid=this.midPredictor.getNextMid(mediaType);t.replaceTransceiver(this.pc.addTransceiver(toMediaStreamTrackKind(mediaType),{direction:'recvonly'}));t.mid=mid;});});}getReceiveSlotById(id){return [...this.recvTransceivers.values()].flat().map(transceiver=>transceiver.receiveSlot).find(receiveSlot=>{var receiveSlotId=receiveSlot.id||{};return Object.keys(receiveSlotId).length===Object.keys(id).length&&Object.keys(receiveSlotId).every(key=>Object.prototype.hasOwnProperty.call(id,key)&&receiveSlotId[key]===id[key]);});}getStats(){return this.statsManager.getStats();}getTransceiverStats(){return __awaiter$1(this,void 0,void 0,function*(){return organizeTransceiverStats(this.sendTransceivers,this.recvTransceivers);});}preProcessStats(stats){return __awaiter$1(this,void 0,void 0,function*(){yield Promise.all([...this.sendTransceivers.values()].map(transceiver=>__awaiter$1(this,void 0,void 0,function*(){(yield transceiver.getStats()).forEach(senderStats=>{if(senderStats.type==='outbound-rtp'||senderStats.type==='media-source'){stats.set(senderStats.id,senderStats);}});})));yield Promise.all([...this.recvTransceivers.values()].map(transceivers=>__awaiter$1(this,void 0,void 0,function*(){yield Promise.all(transceivers.map(transceiver=>__awaiter$1(this,void 0,void 0,function*(){(yield transceiver.getStats()).forEach(receiverStats=>{if(receiverStats.type==='inbound-rtp'){stats.set(receiverStats.id,receiverStats);}});})));})));});}attachMetricsObserver(){this.forceStatsReport=rtcStats_1(this.pc.getUnderlyingRTCPeerConnection(),data=>this.metricsCallback(data),5000,stats=>this.preProcessStats(stats)).forceStatsReport;}forceRtcMetricsCallback(){var _a;return (_a=this.forceStatsReport)===null||_a===void 0?void 0:_a.call(this);}setMetricsCallback(callback){this.metricsCallback=callback;}setOveruseUpdateCallback(callback){this.overuseUpdateCallback=callback;}getCsiByMediaType(mediaType){var _a;return (_a=this.sendTransceivers.get(mediaType))===null||_a===void 0?void 0:_a.csi;}getAllCsis(){return {audioMain:this.getCsiByMediaType(exports.MediaType.AudioMain),audioSlides:this.getCsiByMediaType(exports.MediaType.AudioSlides),videoMain:this.getCsiByMediaType(exports.MediaType.VideoMain),videoSlides:this.getCsiByMediaType(exports.MediaType.VideoSlides)};}}class StreamRequest{constructor(policy,policySpecificInfo,receiveSlots,maxPayloadBitsPerSecond){var codecInfos=arguments.length>4&&arguments[4]!==undefined?arguments[4]:[];this.policy=policy;this.policySpecificInfo=policySpecificInfo;this.receiveSlots=receiveSlots;this.maxPayloadBitsPerSecond=maxPayloadBitsPerSecond;this.codecInfos=codecInfos;}_toJmpStreamRequest(){return new StreamRequest$1(this.policy,this.policySpecificInfo,this.receiveSlots.map(rs=>rs.id),this.maxPayloadBitsPerSecond,this.codecInfos);}}
|
|
4896
4896
|
|
|
4897
4897
|
var defaultLogger = {
|
|
4898
4898
|
info: function info() {
|
package/dist/esm/index.js
CHANGED
|
@@ -2887,7 +2887,7 @@ for(i=listeners.length-1;i>=0;i--){this.removeListener(type,listeners[i]);}}retu
|
|
|
2887
2887
|
// EventEmitters, we do not listen for `error` events here.
|
|
2888
2888
|
emitter.addEventListener(name,function wrapListener(arg){// IE does not have builtin `{ once: true }` support so we
|
|
2889
2889
|
// have to do it manually.
|
|
2890
|
-
if(flags.once){emitter.removeEventListener(name,wrapListener);}listener(arg);});}else {throw new TypeError('The "emitter" argument must be of type EventEmitter. Received type '+typeof emitter);}}class EventEmitter$2 extends events$1.exports.EventEmitter{}class IngressSdpMunger{constructor(){this.ssrc=generateSsrc();}getReceiverId(){return Object.assign({ssrc:this.ssrc},this.rtxSsrc?{rtxSsrc:this.rtxSsrc}:{});}mungeLocalDescription(mediaDescription,options){retainCodecs(mediaDescription,['h264','opus','rtx']);if(mediaDescription.codecs.size===0){logErrorAndThrow(WcmeErrorType.SDP_MUNGE_MISSING_CODECS,"No codecs present in m-line with MID ".concat(mediaDescription.mid," after filtering."));}removeMidRidExtensions(mediaDescription);if(options.twccDisabled){disableTwcc(mediaDescription);}}mungeRemoteDescription(mediaDescription){if(!mediaDescription.ssrcs.length){mediaDescription.addLine(new SsrcLine$1(this.ssrc,'cname',"".concat(this.ssrc,"-cname")));mediaDescription.addLine(new SsrcLine$1(this.ssrc,'msid','-',"".concat(mediaDescription.mid)));if(hasCodec('rtx',mediaDescription)){if(!this.rtxSsrc){this.rtxSsrc=generateSsrc();}mediaDescription.addLine(new SsrcLine$1(this.rtxSsrc,'cname',"".concat(this.ssrc,"-cname")));mediaDescription.addLine(new SsrcLine$1(this.rtxSsrc,'msid','-',"".concat(mediaDescription.mid)));mediaDescription.addLine(new SsrcGroupLine$1('FID',[this.ssrc,this.rtxSsrc]));}}if(retainCandidates(mediaDescription,['udp','tcp'])){logger.log("Some unsupported remote candidates have been removed from mid ".concat(mediaDescription.mid));}[...mediaDescription.codecs.values()].forEach(ci=>{ci.fmtParams.set('x-google-start-bitrate','60000');});}reset(){this.ssrc=generateSsrc();}}class MidPredictor{constructor(){this.currentMid=0;this.midMap=new Map();}getNextMid(mediaType){var mid=this.currentMid++;var mids=this.midMap.get(mediaType)||[];mids.push("".concat(mid));this.midMap.set(mediaType,mids);return "".concat(mid);}allocateMidForDatachannel(){this.currentMid+=1;}reset(){this.midMap=new Map();this.currentMid=0;}getMidMap(){return this.midMap;}}var OveruseState;(function(OveruseState){OveruseState[OveruseState["NOT_OVERUSED"]=0]="NOT_OVERUSED";OveruseState[OveruseState["OVERUSED"]=1]="OVERUSED";})(OveruseState||(OveruseState={}));class OveruseStateManager{constructor(callback){this.monitors=[];this.lastOverallOveruseState=OveruseState.NOT_OVERUSED;this.isRunning=false;this.overuseUpdateCallback=callback;}addMonitor(monitor){this.monitors.push(monitor);if(this.isRunning){monitor.startMonitoring(()=>this.onMonitorOveruseUpdate());}}start(){this.isRunning=true;this.monitors.forEach(monitor=>monitor.startMonitoring(()=>this.onMonitorOveruseUpdate()));}stop(){this.isRunning=false;this.monitors.forEach(monitor=>monitor.stopMonitoring());}onMonitorOveruseUpdate(){var overuseStates=this.monitors.map(monitor=>monitor.getLastOveruseState());var overallOveruseState=overuseStates.some(overuseState=>overuseState===OveruseState.OVERUSED)?OveruseState.OVERUSED:OveruseState.NOT_OVERUSED;if(overallOveruseState!==this.lastOverallOveruseState){this.lastOverallOveruseState=overallOveruseState;this.overuseUpdateCallback(overallOveruseState);}}}var ReceiveSlotEvents;(function(ReceiveSlotEvents){ReceiveSlotEvents["MediaStarted"]="media-started";ReceiveSlotEvents["MediaStopped"]="media-stopped";ReceiveSlotEvents["MediaEnded"]="media-ended";ReceiveSlotEvents["SourceUpdate"]="source-update";})(ReceiveSlotEvents||(ReceiveSlotEvents={}));class ReceiveSlot extends EventEmitter$2{constructor(idGetter,track){super();this._idGetter=idGetter;this.handleStreamMediaStateChange=this.handleStreamMediaStateChange.bind(this);this.handleStreamEnded=this.handleStreamEnded.bind(this);this._stream=new RemoteStream(new MediaStream([track]));this._sourceState='no source';this._stream.on(RemoteStreamEventNames.MediaStateChange,this.handleStreamMediaStateChange);this._stream.on(StreamEventNames.Ended,this.handleStreamEnded);}handleStreamMediaStateChange(state){if(state===RemoteMediaState.Stopped){this.emit(ReceiveSlotEvents.MediaStopped);}else {this.emit(ReceiveSlotEvents.MediaStarted);}}handleStreamEnded(){this.emit(ReceiveSlotEvents.MediaEnded);}_replaceTrack(newTrack){this._stream.replaceTrack(newTrack);}_updateSource(state,csi){if(state!==this._sourceState||csi!==this._currentRxCsi){this._sourceState=state;this._currentRxCsi=csi;this.emit(ReceiveSlotEvents.SourceUpdate,state,csi);}}close(){this._stream.off(RemoteStreamEventNames.MediaStateChange,this.handleStreamMediaStateChange);this._stream.off(StreamEventNames.Ended,this.handleStreamEnded);}get id(){return this._idGetter();}get stream(){return this._stream.outputStream;}get currentRxCsi(){return this._currentRxCsi;}}ReceiveSlot.Events=ReceiveSlotEvents;class Transceiver{constructor(rtcRtpTransceiver,mid){this.twccDisabled=false;this._rtcRtpTransceiver=rtcRtpTransceiver;this.mid=mid;}replaceTransceiver(newRtcRtpTransceiver){this._rtcRtpTransceiver=newRtcRtpTransceiver;}get receiver(){return this._rtcRtpTransceiver.receiver;}get sender(){return this._rtcRtpTransceiver.sender;}close(){this._rtcRtpTransceiver.stop();}}class ReceiveOnlyTransceiver extends Transceiver{constructor(rtcRtpTransceiver,mid,munger){super(rtcRtpTransceiver,mid);this.munger=munger;this._receiveSlot=new ReceiveSlot(()=>{if(!this._rtcRtpTransceiver.mid){return null;}return this.munger.getReceiverId();},this._rtcRtpTransceiver.receiver.track);}replaceTransceiver(newRtcRtpTransceiver){super.replaceTransceiver(newRtcRtpTransceiver);this._receiveSlot._replaceTrack(newRtcRtpTransceiver.receiver.track);}close(){super.close();this._receiveSlot.close();}get receiveSlot(){return this._receiveSlot;}getStats(){return this.receiver.getStats();}mungeLocalDescription(mediaDescription){this.munger.mungeLocalDescription(mediaDescription,{twccDisabled:this.twccDisabled});}mungeRemoteDescription(mediaDescription){this.munger.mungeRemoteDescription(mediaDescription);}get receiverId(){return this.munger.getReceiverId();}resetSdpMunger(){this.munger.reset();}}ReceiveOnlyTransceiver.rid='1';var events={exports:{}};var R=typeof Reflect==='object'?Reflect:null;var ReflectApply=R&&typeof R.apply==='function'?R.apply:function ReflectApply(target,receiver,args){return Function.prototype.apply.call(target,receiver,args);};var ReflectOwnKeys;if(R&&typeof R.ownKeys==='function'){ReflectOwnKeys=R.ownKeys;}else if(Object.getOwnPropertySymbols){ReflectOwnKeys=function ReflectOwnKeys(target){return Object.getOwnPropertyNames(target).concat(Object.getOwnPropertySymbols(target));};}else {ReflectOwnKeys=function ReflectOwnKeys(target){return Object.getOwnPropertyNames(target);};}function ProcessEmitWarning(warning){if(console&&console.warn)console.warn(warning);}var NumberIsNaN=Number.isNaN||function NumberIsNaN(value){return value!==value;};function EventEmitter$1(){EventEmitter$1.init.call(this);}events.exports=EventEmitter$1;events.exports.once=once$1;// Backwards-compat with node 0.10.x
|
|
2890
|
+
if(flags.once){emitter.removeEventListener(name,wrapListener);}listener(arg);});}else {throw new TypeError('The "emitter" argument must be of type EventEmitter. Received type '+typeof emitter);}}class EventEmitter$2 extends events$1.exports.EventEmitter{}class IngressSdpMunger{constructor(){this.ssrc=generateSsrc();}getReceiverId(){return Object.assign({ssrc:this.ssrc},this.rtxSsrc?{rtxSsrc:this.rtxSsrc}:{});}mungeLocalDescription(mediaDescription,options){retainCodecs(mediaDescription,['h264','opus','rtx']);if(mediaDescription.codecs.size===0){logErrorAndThrow(WcmeErrorType.SDP_MUNGE_MISSING_CODECS,"No codecs present in m-line with MID ".concat(mediaDescription.mid," after filtering."));}removeMidRidExtensions(mediaDescription);if(options.twccDisabled){disableTwcc(mediaDescription);}}mungeRemoteDescription(mediaDescription){if(!mediaDescription.ssrcs.length){mediaDescription.addLine(new SsrcLine$1(this.ssrc,'cname',"".concat(this.ssrc,"-cname")));mediaDescription.addLine(new SsrcLine$1(this.ssrc,'msid','-',"".concat(mediaDescription.mid)));if(hasCodec('rtx',mediaDescription)){if(!this.rtxSsrc){this.rtxSsrc=generateSsrc();}mediaDescription.addLine(new SsrcLine$1(this.rtxSsrc,'cname',"".concat(this.ssrc,"-cname")));mediaDescription.addLine(new SsrcLine$1(this.rtxSsrc,'msid','-',"".concat(mediaDescription.mid)));mediaDescription.addLine(new SsrcGroupLine$1('FID',[this.ssrc,this.rtxSsrc]));}}if(retainCandidates(mediaDescription,['udp','tcp'])){logger.log("Some unsupported remote candidates have been removed from mid ".concat(mediaDescription.mid));}[...mediaDescription.codecs.values()].forEach(ci=>{ci.fmtParams.set('x-google-start-bitrate','60000');});}reset(){this.ssrc=generateSsrc();}}class MidPredictor{constructor(){this.currentMid=0;this.midMap=new Map();}getNextMid(mediaType){var mid=this.currentMid++;var mids=this.midMap.get(mediaType)||[];mids.push("".concat(mid));this.midMap.set(mediaType,mids);return "".concat(mid);}allocateMidForDatachannel(){this.currentMid+=1;}reset(){this.midMap=new Map();this.currentMid=0;}getMidMap(){return this.midMap;}}var OveruseState;(function(OveruseState){OveruseState[OveruseState["NOT_OVERUSED"]=0]="NOT_OVERUSED";OveruseState[OveruseState["OVERUSED"]=1]="OVERUSED";})(OveruseState||(OveruseState={}));class OveruseStateManager{constructor(callback){this.monitors=[];this.lastOverallOveruseState=OveruseState.NOT_OVERUSED;this.isRunning=false;this.overuseUpdateCallback=callback;}addMonitor(monitor){this.monitors.push(monitor);if(this.isRunning){monitor.startMonitoring(()=>this.onMonitorOveruseUpdate());}}start(){this.isRunning=true;this.monitors.forEach(monitor=>monitor.startMonitoring(()=>this.onMonitorOveruseUpdate()));}stop(){this.isRunning=false;this.monitors.forEach(monitor=>monitor.stopMonitoring());}onMonitorOveruseUpdate(){var overuseStates=this.monitors.map(monitor=>monitor.getLastOveruseState());var overallOveruseState=overuseStates.some(overuseState=>overuseState===OveruseState.OVERUSED)?OveruseState.OVERUSED:OveruseState.NOT_OVERUSED;if(overallOveruseState!==this.lastOverallOveruseState){this.lastOverallOveruseState=overallOveruseState;this.overuseUpdateCallback(overallOveruseState);}}}var ReceiveSlotEvents;(function(ReceiveSlotEvents){ReceiveSlotEvents["MediaStarted"]="media-started";ReceiveSlotEvents["MediaStopped"]="media-stopped";ReceiveSlotEvents["MediaEnded"]="media-ended";ReceiveSlotEvents["SourceUpdate"]="source-update";})(ReceiveSlotEvents||(ReceiveSlotEvents={}));class ReceiveSlot extends EventEmitter$2{constructor(idGetter,track){super();this._idGetter=idGetter;this.handleStreamMediaStateChange=this.handleStreamMediaStateChange.bind(this);this.handleStreamEnded=this.handleStreamEnded.bind(this);this._stream=new RemoteStream(new MediaStream([track]));this._sourceState='no source';this._stream.on(RemoteStreamEventNames.MediaStateChange,this.handleStreamMediaStateChange);this._stream.on(StreamEventNames.Ended,this.handleStreamEnded);}handleStreamMediaStateChange(state){if(state===RemoteMediaState.Stopped){this.emit(ReceiveSlotEvents.MediaStopped);}else {this.emit(ReceiveSlotEvents.MediaStarted);}}handleStreamEnded(){this.emit(ReceiveSlotEvents.MediaEnded);}_replaceTrack(newTrack){this._stream.replaceTrack(newTrack);}_updateSource(state,csi){if(state!==this._sourceState||csi!==this._currentRxCsi){this._sourceState=state;this._currentRxCsi=csi;this.emit(ReceiveSlotEvents.SourceUpdate,state,csi);}}close(){this._stream.off(RemoteStreamEventNames.MediaStateChange,this.handleStreamMediaStateChange);this._stream.off(StreamEventNames.Ended,this.handleStreamEnded);}get id(){return this._idGetter();}get stream(){return this._stream.outputStream;}get currentRxCsi(){return this._currentRxCsi;}}ReceiveSlot.Events=ReceiveSlotEvents;class Transceiver{constructor(rtcRtpTransceiver,mid){this.twccDisabled=false;this._rtcRtpTransceiver=rtcRtpTransceiver;this.mid=mid;}replaceTransceiver(newRtcRtpTransceiver){this._rtcRtpTransceiver=newRtcRtpTransceiver;}get receiver(){return this._rtcRtpTransceiver.receiver;}get sender(){return this._rtcRtpTransceiver.sender;}close(){this._rtcRtpTransceiver.stop();}}class ReceiveOnlyTransceiver extends Transceiver{constructor(rtcRtpTransceiver,mid,munger){super(rtcRtpTransceiver,mid);this.metadata={};this.munger=munger;this._receiveSlot=new ReceiveSlot(()=>{if(!this._rtcRtpTransceiver.mid){return null;}return this.munger.getReceiverId();},this._rtcRtpTransceiver.receiver.track);}replaceTransceiver(newRtcRtpTransceiver){super.replaceTransceiver(newRtcRtpTransceiver);this._receiveSlot._replaceTrack(newRtcRtpTransceiver.receiver.track);}close(){super.close();this._receiveSlot.close();}get receiveSlot(){return this._receiveSlot;}getStats(){return __awaiter$1(this,void 0,void 0,function*(){var statsMap=new Map();var statsReport=yield this.receiver.getStats();statsReport.forEach((stats,key)=>{if(stats.type==='inbound-rtp'){stats.mid=this.mid;stats.csi=this.receiveSlot.currentRxCsi;stats.calliopeMediaType=this.metadata.mediaType;stats.requestedBitrate=this.metadata.requestedBitrate;stats.requestedFrameSize=this.metadata.requestedFrameSize;Object.assign(stats,this.receiverId);}statsMap.set(key,stats);});return statsMap;});}mungeLocalDescription(mediaDescription){this.munger.mungeLocalDescription(mediaDescription,{twccDisabled:this.twccDisabled});}mungeRemoteDescription(mediaDescription){this.munger.mungeRemoteDescription(mediaDescription);}get receiverId(){return this.munger.getReceiverId();}resetSdpMunger(){this.munger.reset();}handleRequested(streamRequest){var _a,_b;this.metadata.requestedBitrate=streamRequest.maxPayloadBitsPerSecond;this.metadata.requestedFrameSize=(_b=(_a=streamRequest.codecInfos[0])===null||_a===void 0?void 0:_a.h264)===null||_b===void 0?void 0:_b.maxFs;}handleUnrequested(){this.metadata.requestedBitrate=undefined;this.metadata.requestedFrameSize=undefined;this.receiveSlot._updateSource('no source',undefined);}}ReceiveOnlyTransceiver.rid='1';var events={exports:{}};var R=typeof Reflect==='object'?Reflect:null;var ReflectApply=R&&typeof R.apply==='function'?R.apply:function ReflectApply(target,receiver,args){return Function.prototype.apply.call(target,receiver,args);};var ReflectOwnKeys;if(R&&typeof R.ownKeys==='function'){ReflectOwnKeys=R.ownKeys;}else if(Object.getOwnPropertySymbols){ReflectOwnKeys=function ReflectOwnKeys(target){return Object.getOwnPropertyNames(target).concat(Object.getOwnPropertySymbols(target));};}else {ReflectOwnKeys=function ReflectOwnKeys(target){return Object.getOwnPropertyNames(target);};}function ProcessEmitWarning(warning){if(console&&console.warn)console.warn(warning);}var NumberIsNaN=Number.isNaN||function NumberIsNaN(value){return value!==value;};function EventEmitter$1(){EventEmitter$1.init.call(this);}events.exports=EventEmitter$1;events.exports.once=once$1;// Backwards-compat with node 0.10.x
|
|
2891
2891
|
EventEmitter$1.EventEmitter=EventEmitter$1;EventEmitter$1.prototype._events=undefined;EventEmitter$1.prototype._eventsCount=0;EventEmitter$1.prototype._maxListeners=undefined;// By default EventEmitters will print a warning if more than 10 listeners are
|
|
2892
2892
|
// added to it. This is a useful default which helps finding memory leaks.
|
|
2893
2893
|
var defaultMaxListeners=10;function checkListener(listener){if(typeof listener!=='function'){throw new TypeError('The "listener" argument must be of type Function. Received type '+typeof listener);}}Object.defineProperty(EventEmitter$1,'defaultMaxListeners',{enumerable:true,get:function get(){return defaultMaxListeners;},set:function set(arg){if(typeof arg!=='number'||arg<0||NumberIsNaN(arg)){throw new RangeError('The value of "defaultMaxListeners" is out of range. It must be a non-negative number. Received '+arg+'.');}defaultMaxListeners=arg;}});EventEmitter$1.init=function(){if(this._events===undefined||this._events===Object.getPrototypeOf(this)._events){this._events=Object.create(null);this._eventsCount=0;}this._maxListeners=this._maxListeners||undefined;};// Obviously not all Emitters should be limited to 10. This function allows
|
|
@@ -4881,7 +4881,7 @@ var{hasOwnProperty}=Object.prototype;for(var i=0;i<mapResults.length;i++){if(map
|
|
|
4881
4881
|
* // arg1 now equals 'three'
|
|
4882
4882
|
* callback(null, 'done');
|
|
4883
4883
|
* }
|
|
4884
|
-
*/function waterfall(tasks,callback){callback=once(callback);if(!Array.isArray(tasks))return callback(new Error('First argument to waterfall must be an array of functions'));if(!tasks.length)return callback();var taskIndex=0;function nextTask(args){var task=wrapAsync(tasks[taskIndex++]);task(...args,onlyOnce(next));}function next(err){if(err===false)return;for(var _len22=arguments.length,args=new Array(_len22>1?_len22-1:0),_key22=1;_key22<_len22;_key22++){args[_key22-1]=arguments[_key22];}if(err||taskIndex===tasks.length){return callback(err,...args);}nextTask(args);}nextTask([]);}awaitify(waterfall);function processTasks(task,finishedCallback){return __awaiter$1(this,void 0,void 0,function*(){try{yield task();finishedCallback();}catch(e){finishedCallback(e);}});}class AsyncQueue{constructor(){this.queue=queue$1(processTasks,1);}push(task){return this.queue.pushAsync(task);}empty(){return this.queue.empty();}}var OfferAnswerType;(function(OfferAnswerType){OfferAnswerType[OfferAnswerType["LocalOnly"]=0]="LocalOnly";OfferAnswerType[OfferAnswerType["Remote"]=1]="Remote";})(OfferAnswerType||(OfferAnswerType={}));class SendOnlyTransceiver extends Transceiver{constructor(rtcRtpTransceiver,mid,csi,munger,mediaType){super(rtcRtpTransceiver,mid);this.rtxEnabled=false;this.streamMuteStateChange=new TypedEvent();this.streamPublishStateChange=new TypedEvent();this.negotiationNeeded=new TypedEvent();this.namedMediaGroupsChange=new TypedEvent();this.requestedIdEncodingParamsMap=new Map();this.updateSendParametersQueue=new AsyncQueue();this.csi=csi;this.direction='sendrecv';this.handleTrackChange=this.handleTrackChange.bind(this);this.handleStreamConstraintsChange=this.handleStreamConstraintsChange.bind(this);this.handleStreamMuteStateChange=this.handleStreamMuteStateChange.bind(this);this.munger=munger;this.mediaType=mediaType;}replaceSenderSource(stream){var _a,_b;return __awaiter$1(this,void 0,void 0,function*(){var trackOrNull=(_a=stream===null||stream===void 0?void 0:stream.outputStream.getTracks()[0])!==null&&_a!==void 0?_a:null;if(((_b=this.sender.track)===null||_b===void 0?void 0:_b.id)!==(trackOrNull===null||trackOrNull===void 0?void 0:trackOrNull.id)){yield this.sender.replaceTrack(trackOrNull);if(trackOrNull){logger.log("Sender source for ".concat(this.mediaType," replaced with track ID ").concat(trackOrNull.id));}else {logger.log("Sender source for ".concat(this.mediaType," set to null, sender stopped"));}}});}handleTrackChange(){return __awaiter$1(this,void 0,void 0,function*(){if(this.requested){yield this.replaceSenderSource(this.publishedStream);}});}handleStreamConstraintsChange(){return __awaiter$1(this,void 0,void 0,function*(){yield this.updateSendParameters(this.requestedIdEncodingParamsMap);});}handleStreamMuteStateChange(){this.streamMuteStateChange.emit();}get requested(){return this.requestedIdEncodingParamsMap.size>0;}replaceTransceiver(newRtcRtpTransceiver){var _super=Object.create(null,{replaceTransceiver:{get:()=>super.replaceTransceiver}});return __awaiter$1(this,void 0,void 0,function*(){_super.replaceTransceiver.call(this,newRtcRtpTransceiver);newRtcRtpTransceiver.direction=this.direction;if(this.requested){yield this.replaceSenderSource(this.publishedStream);}});}replacePublishedStream(newStream){return __awaiter$1(this,void 0,void 0,function*(){var oldStream=this.publishedStream;oldStream===null||oldStream===void 0?void 0:oldStream.off(LocalStreamEventNames.OutputTrackChange,this.handleTrackChange);oldStream===null||oldStream===void 0?void 0:oldStream.off(LocalStreamEventNames.ConstraintsChange,this.handleStreamConstraintsChange);oldStream===null||oldStream===void 0?void 0:oldStream.off(LocalStreamEventNames.UserMuteStateChange,this.handleStreamMuteStateChange);oldStream===null||oldStream===void 0?void 0:oldStream.off(LocalStreamEventNames.SystemMuteStateChange,this.handleStreamMuteStateChange);if(this.requested){yield this.replaceSenderSource(newStream);}this.publishedStream=newStream;newStream===null||newStream===void 0?void 0:newStream.on(LocalStreamEventNames.OutputTrackChange,this.handleTrackChange);newStream===null||newStream===void 0?void 0:newStream.on(LocalStreamEventNames.ConstraintsChange,this.handleStreamConstraintsChange);newStream===null||newStream===void 0?void 0:newStream.on(LocalStreamEventNames.UserMuteStateChange,this.handleStreamMuteStateChange);newStream===null||newStream===void 0?void 0:newStream.on(LocalStreamEventNames.SystemMuteStateChange,this.handleStreamMuteStateChange);if(!oldStream&&newStream&&!newStream.muted||oldStream&&!newStream&&!oldStream.muted){this.streamPublishStateChange.emit();}else if((oldStream===null||oldStream===void 0?void 0:oldStream.muted)!==(newStream===null||newStream===void 0?void 0:newStream.muted)){this.streamMuteStateChange.emit();}});}setNamedMediaGroups(namedMediaGroups){if(this.mediaType!==MediaType.AudioMain){logErrorAndThrow(WcmeErrorType.SET_NMG_FAILED,"Named media groups can only be set for audio.");}this.namedMediaGroups=namedMediaGroups;this.namedMediaGroupsChange.emit();}publishStream(stream){return this.replacePublishedStream(stream);}unpublishStream(){return this.replacePublishedStream();}get active(){return this._rtcRtpTransceiver.direction==='sendrecv';}set active(enabled){this.direction=enabled?'sendrecv':'inactive';this._rtcRtpTransceiver.direction=this.direction;if(this._rtcRtpTransceiver.direction!==this._rtcRtpTransceiver.currentDirection){this.negotiationNeeded.emit(OfferAnswerType.Remote);}}getStats(){return this.sender.getStats();}updateSendParameters(requestedIdEncodingParamsMap){return __awaiter$1(this,void 0,void 0,function*(){return this.updateSendParametersQueue.push(()=>__awaiter$1(this,void 0,void 0,function*(){var sendParameters=this.sender.getParameters();sendParameters.encodings.forEach((encoding,index)=>{var _a,_b;var encodingParams=requestedIdEncodingParamsMap.get(index);encoding.active=!!encodingParams;if(encodingParams){var{maxPayloadBitsPerSecond,maxFs,maxWidth,maxHeight}=encodingParams;var scaleDownRatio=getScaleDownRatio((_a=this.publishedStream)===null||_a===void 0?void 0:_a.getSettings().width,(_b=this.publishedStream)===null||_b===void 0?void 0:_b.getSettings().height,maxFs,maxWidth,maxHeight);if(maxPayloadBitsPerSecond!==undefined&&maxPayloadBitsPerSecond>=0){encoding.maxBitrate=maxPayloadBitsPerSecond;}if(scaleDownRatio!==undefined&&scaleDownRatio>=1.0){encoding.scaleResolutionDownBy=scaleDownRatio;}}});yield this.sender.setParameters(sendParameters);var oldRequested=this.requested;var newRequested=requestedIdEncodingParamsMap.size>0;this.requestedIdEncodingParamsMap=requestedIdEncodingParamsMap;if(oldRequested!==newRequested){yield this.replaceSenderSource(newRequested?this.publishedStream:null);}}));});}getRequestedBitrate(ssrc){var index=this.senderIds.findIndex(streamId=>streamId.ssrc===ssrc);if(this.requestedIdEncodingParamsMap.has(index)){var encodingParams=this.requestedIdEncodingParamsMap.get(index);return encodingParams===null||encodingParams===void 0?void 0:encodingParams.maxPayloadBitsPerSecond;}return undefined;}getRequestedFrameSize(ssrc){var index=this.senderIds.findIndex(streamId=>streamId.ssrc===ssrc);if(this.requestedIdEncodingParamsMap.has(index)){var encodingParams=this.requestedIdEncodingParamsMap.get(index);return encodingParams===null||encodingParams===void 0?void 0:encodingParams.maxFs;}return undefined;}isSimulcastEnabled(){var params=this.sender.getParameters();return params.encodings.length>1;}mungeLocalDescription(mediaDescription){this.munger.mungeLocalDescription(mediaDescription,{simulcastEnabled:this.isSimulcastEnabled(),rtxEnabled:this.rtxEnabled,twccDisabled:this.twccDisabled});}mungeLocalDescriptionForRemoteServer(mediaDescription){this.munger.mungeLocalDescriptionForRemoteServer(mediaDescription,getMediaContent(this.mediaType),this.csi);}mungeRemoteDescription(mediaDescription){this.munger.mungeRemoteDescription(mediaDescription);}get senderIds(){return this.munger.getSenderIds();}get numActiveSimulcastLayers(){var _a;if(getMediaFamily(this.mediaType)===MediaFamily.Video){return (_a=this.publishedStream)===null||_a===void 0?void 0:_a.getNumActiveSimulcastLayers();}return this.publishedStream?0:undefined;}getEncodingIndexForStreamId(id){return this.munger.getEncodingIndexForStreamId(id);}resetSdpMunger(){this.munger.reset();}setCodecParameters(parameters){this.munger.setCodecParameters(parameters);this.negotiationNeeded.emit(OfferAnswerType.LocalOnly);}deleteCodecParameters(parameters){this.munger.deleteCodecParameters(parameters);this.negotiationNeeded.emit(OfferAnswerType.LocalOnly);}}class SendSlot{constructor(sendTransceiver){this.sendTransceiver=sendTransceiver;}publishStream(stream){return __awaiter$1(this,void 0,void 0,function*(){if(stream===this.sendTransceiver.publishedStream){return Promise.resolve();}return this.sendTransceiver.publishStream(stream);});}unpublishStream(){return __awaiter$1(this,void 0,void 0,function*(){if(!this.sendTransceiver.publishedStream){return Promise.resolve();}return this.sendTransceiver.unpublishStream();});}setNamedMediaGroups(namedMediaGroups){this.sendTransceiver.setNamedMediaGroups(namedMediaGroups);}clearNamedMediaGroups(){this.setNamedMediaGroups([]);}get active(){return this.sendTransceiver.active;}set active(active){this.sendTransceiver.active=active;}setCodecParameters(parameters){return __awaiter$1(this,void 0,void 0,function*(){this.sendTransceiver.setCodecParameters(parameters);});}deleteCodecParameters(parameters){return __awaiter$1(this,void 0,void 0,function*(){this.sendTransceiver.deleteCodecParameters(parameters);});}}class StatsManager{constructor(statsGetter){var statsPreprocessor=arguments.length>1&&arguments[1]!==undefined?arguments[1]:()=>__awaiter$1(this,void 0,void 0,function*(){});this.statsGetter=statsGetter;this.statsPreProcessor=statsPreprocessor;}getStats(){return __awaiter$1(this,void 0,void 0,function*(){var statsReport=yield this.statsGetter();var statsMap=new Map();statsReport.forEach((stats,key)=>statsMap.set(key,stats));yield this.statsPreProcessor(statsMap);return statsMap;});}}var organizeTransceiverStats=(sendTransceivers,recvTransceivers)=>__awaiter$1(void 0,void 0,void 0,function*(){var result={audio:{senders:[],receivers:[]},video:{senders:[],receivers:[]},screenShareAudio:{senders:[],receivers:[]},screenShareVideo:{senders:[],receivers:[]}};yield Promise.all([...sendTransceivers.entries()].map(_ref6=>{var[mediaType,transceiver]=_ref6;return __awaiter$1(void 0,void 0,void 0,function*(){var _a;var report=[];(yield transceiver.getStats()).forEach(senderStats=>{if(senderStats.type==='outbound-rtp'){report.push(Object.assign(Object.assign({},senderStats),{requestedBitrate:transceiver.getRequestedBitrate(senderStats.ssrc),requestedFrameSize:transceiver.getRequestedFrameSize(senderStats.ssrc)}));}});var item={report,mid:transceiver.mid,csi:transceiver.csi,currentDirection:'sendonly',localTrackLabel:(_a=transceiver.publishedStream)===null||_a===void 0?void 0:_a.label};if(mediaType===MediaType.AudioMain){result.audio.senders.push(item);}if(mediaType===MediaType.VideoMain){result.video.senders.push(item);}if(mediaType===MediaType.AudioSlides){result.screenShareAudio.senders.push(item);}if(mediaType===MediaType.VideoSlides){result.screenShareVideo.senders.push(item);}});}));yield Promise.all([...recvTransceivers.entries()].map(_ref7=>{var[mediaType,transceivers]=_ref7;return __awaiter$1(void 0,void 0,void 0,function*(){return Promise.all(transceivers.map(t=>__awaiter$1(void 0,void 0,void 0,function*(){var _b,_c;var report=[];(yield t.getStats()).forEach(receiverStats=>{if(receiverStats.type==='inbound-rtp'){report.push(Object.assign(Object.assign({},receiverStats),{requestedBitrate:t.requestedBitrate,requestedFrameSize:t.requestedFrameSize}));}});var item={report,mid:(_b=t.receiveSlot.id)===null||_b===void 0?void 0:_b.mid,csi:t.receiveSlot.currentRxCsi,currentDirection:'recvonly',localTrackLabel:(_c=t.receiveSlot.stream.getTracks()[0])===null||_c===void 0?void 0:_c.label};if(mediaType===MediaType.AudioMain){result.audio.receivers.push(item);}if(mediaType===MediaType.VideoMain){result.video.receivers.push(item);}if(mediaType===MediaType.AudioSlides){result.screenShareAudio.receivers.push(item);}if(mediaType===MediaType.VideoSlides){result.screenShareVideo.receivers.push(item);}})));});}));return result;});function toMediaStreamTrackKind(mediaType){return [MediaType.VideoMain,MediaType.VideoSlides].includes(mediaType)?MediaStreamTrackKind.Video:MediaStreamTrackKind.Audio;}function webRtcVideoContentHintToJmpVideoContentHint(hint){if(hint==='motion'){return 'motion';}if(hint==='detail'){return 'sharpness';}return undefined;}var MultistreamConnectionEventNames;(function(MultistreamConnectionEventNames){MultistreamConnectionEventNames["VideoSourceCountUpdate"]="video-source-count-update";MultistreamConnectionEventNames["AudioSourceCountUpdate"]="audio-source-count-update";MultistreamConnectionEventNames["ActiveSpeakerNotification"]="active-speaker-notification";MultistreamConnectionEventNames["ConnectionStateUpdate"]="connection-state-update";MultistreamConnectionEventNames["IceGatheringStateUpdate"]="ice-gathering-state-update";MultistreamConnectionEventNames["NegotiationNeeded"]="negotiation-needed";MultistreamConnectionEventNames["CreateOfferOnSuccess"]="createofferonsuccess";MultistreamConnectionEventNames["CreateAnswerOnSuccess"]="createansweronsuccess";MultistreamConnectionEventNames["SetLocalDescriptionOnSuccess"]="setlocaldescriptiononsuccess";MultistreamConnectionEventNames["SetRemoteDescriptionOnSuccess"]="setremotedescriptiononsuccess";})(MultistreamConnectionEventNames||(MultistreamConnectionEventNames={}));var defaultMultistreamConnectionOptions={disableSimulcast:BrowserInfo.isFirefox(),bundlePolicy:'max-compat',iceServers:undefined,disableContentSimulcast:true,disableAudioTwcc:true};class MultistreamConnection extends EventEmitter$2{constructor(){var userOptions=arguments.length>0&&arguments[0]!==undefined?arguments[0]:{};super();this.sendTransceivers=new Map();this.recvTransceivers=new Map();this.jmpSessions=new Map();this.pendingJmpTasks=[];this.metricsCallback=()=>{};this.overuseUpdateCallback=()=>{};this.midPredictor=new MidPredictor();this.offerAnswerQueue=new AsyncQueue();this.currentCreateOfferId=0;this.options=Object.assign(Object.assign({},defaultMultistreamConnectionOptions),userOptions);logger.info("Creating multistream connection with options ".concat(JSON.stringify(this.options)));this.initializePeerConnection();this.overuseStateManager=new OveruseStateManager(overuseState=>this.overuseUpdateCallback(overuseState));this.overuseStateManager.start();this.statsManager=new StatsManager(()=>this.pc.getStats(),stats=>this.preProcessStats(stats));var mainSceneId=generateSceneId();var slidesSceneId=generateSceneId();var videoMainEncodingOptions=this.getVideoEncodingOptions(MediaContent.Main);var videoSlidesEncodingOptions=this.getVideoEncodingOptions(MediaContent.Slides);this.createSendTransceiver(MediaType.VideoMain,mainSceneId,videoMainEncodingOptions);this.createSendTransceiver(MediaType.AudioMain,mainSceneId);this.createSendTransceiver(MediaType.VideoSlides,slidesSceneId,videoSlidesEncodingOptions);this.createSendTransceiver(MediaType.AudioSlides,slidesSceneId);}initializePeerConnection(){var _a;(_a=this.pc)===null||_a===void 0?void 0:_a.close();this.pc=new PeerConnection({iceServers:this.options.iceServers,bundlePolicy:this.options.bundlePolicy});this.propagatePeerConnectionEvents();this.attachMetricsObserver();this.createDataChannel();}propagatePeerConnectionEvents(){this.pc.on(PeerConnection.Events.ConnectionStateChange,state=>{this.emit(MultistreamConnectionEventNames.ConnectionStateUpdate,state);});this.pc.on(PeerConnection.Events.CreateOfferOnSuccess,description=>{this.emit(MultistreamConnectionEventNames.CreateOfferOnSuccess,description);});this.pc.on(PeerConnection.Events.CreateAnswerOnSuccess,description=>{this.emit(MultistreamConnectionEventNames.CreateAnswerOnSuccess,description);});this.pc.on(PeerConnection.Events.SetLocalDescriptionOnSuccess,description=>{this.emit(MultistreamConnectionEventNames.SetLocalDescriptionOnSuccess,description);});this.pc.on(PeerConnection.Events.SetRemoteDescriptionOnSuccess,description=>{this.emit(MultistreamConnectionEventNames.SetRemoteDescriptionOnSuccess,description);});this.pc.on(PeerConnection.Events.IceGatheringStateChange,()=>{this.emit(MultistreamConnectionEventNames.IceGatheringStateUpdate,this.getIceGatheringState());});}getConnectionState(){return this.pc.getConnectionState();}getIceGatheringState(){return this.pc.iceGatheringState;}getVideoEncodingOptions(content){var enabledSimulcast=content===MediaContent.Main?!this.options.disableSimulcast:!this.options.disableContentSimulcast;return enabledSimulcast?[{scaleResolutionDownBy:4,active:false},{scaleResolutionDownBy:2,active:false},{active:false}]:[{active:false}];}createSendTransceiver(mediaType,sceneId,sendEncodingsOptions){var rtcTransceiver;try{rtcTransceiver=this.pc.addTransceiver(toMediaStreamTrackKind(mediaType),{direction:'sendrecv',sendEncodings:sendEncodingsOptions});}catch(e){logger.error("addTransceiver failed due to : ".concat(e));throw e;}var mid=this.midPredictor.getNextMid(mediaType);var csi=generateCsi(getMediaFamily(mediaType),sceneId);var munger=new EgressSdpMunger();var transceiver=new SendOnlyTransceiver(rtcTransceiver,mid,csi,munger,mediaType);if(getMediaFamily(mediaType)===MediaFamily.Video){transceiver.rtxEnabled=true;transceiver.setCodecParameters({'max-mbps':"".concat(defaultMaxVideoEncodeMbps),'max-fs':"".concat(defaultMaxVideoEncodeFrameSize)});}transceiver.twccDisabled=getMediaFamily(mediaType)===MediaFamily.Audio?this.options.disableAudioTwcc:false;transceiver.active=false;transceiver.streamMuteStateChange.on(()=>{this.sendSourceAdvertisement(mediaType);this.sendMediaRequestStatus(mediaType);});transceiver.streamPublishStateChange.on(()=>{this.sendSourceAdvertisement(mediaType);this.sendMediaRequestStatus(mediaType);});transceiver.negotiationNeeded.on(offerAnswerType=>{if(offerAnswerType===OfferAnswerType.Remote){this.emit(MultistreamConnectionEventNames.NegotiationNeeded);}else if(this.pc.getRemoteDescription()){this.queueLocalOfferAnswer();}});transceiver.namedMediaGroupsChange.on(()=>{this.sendSourceAdvertisement(mediaType);});this.sendTransceivers.set(mediaType,transceiver);this.createJmpSession(mediaType);}createSendSlot(mediaType){var active=arguments.length>1&&arguments[1]!==undefined?arguments[1]:true;var transceiver=this.getSendTransceiverOrThrow(mediaType);transceiver.active=active;return new SendSlot(transceiver);}createJmpSession(mediaType){var jmpSession=new JmpSession(getMediaFamily(mediaType),getMediaContent(mediaType));jmpSession.setTxCallback(msg=>{var _a;if(((_a=this.dataChannel)===null||_a===void 0?void 0:_a.readyState)!=='open'){logger.error("DataChannel not created or not connected. Unable to send JMP message.");return;}try{logger.info("Sending JMP message (size: ".concat(msg.length,"): ").concat(msg));this.dataChannel.send(msg);}catch(err){var{bufferedAmount,readyState}=this.dataChannel;var{sctp}=this.pc.getUnderlyingRTCPeerConnection();var{maxMessageSize,state}=sctp||{};logErrorAndThrow(WcmeErrorType.DATA_CHANNEL_SEND_FAILED,"Sending JMP message failed with error: ".concat(err,",\nMessage size: ").concat(msg.length,",\nData Channel State: ").concat(readyState,",\nData Channel Buffered amount: ").concat(bufferedAmount,",\nSCTP State: ").concat(state,",\nSCTP Max Message Size: ").concat(maxMessageSize));}});var prevNumTotalSources=0;var prevNumLiveSources=0;jmpSession.on(JmpSessionEvents.SourceAdvertisementReceived,data=>{logger.log("SourceAdvertisement received: ".concat(JSON.stringify(data)));if(data.numTotalSources!==prevNumTotalSources||data.numLiveSources!==prevNumLiveSources){prevNumTotalSources=data.numTotalSources;prevNumLiveSources=data.numLiveSources;var eventName=getMediaFamily(mediaType)===MediaFamily.Video?MultistreamConnectionEventNames.VideoSourceCountUpdate:MultistreamConnectionEventNames.AudioSourceCountUpdate;this.emit(eventName,data.numTotalSources,data.numLiveSources,getMediaContent(mediaType));}else {logger.log('Number of sources was unchanged, ignoring message');}});jmpSession.on(JmpSessionEvents.MediaRequestStatusReceived,data=>{logger.log("MediaRequestStatus received: ".concat(JSON.stringify(data)));data.streamStates.forEach(s=>{var receiveSlot=this.getReceiveSlotById(s.id);if(!receiveSlot){logger.warn("Got MediaRequestStatus for unknown receive slot: ".concat(JSON.stringify(s.id)));return;}receiveSlot._updateSource(s.state,s.csi);});});jmpSession.on(JmpSessionEvents.MediaRequestReceived,data=>{logger.log("MediaRequest received: ".concat(JSON.stringify(data)));this.updateRequestedStreams(mediaType,data.requests).then(()=>{if(getMediaFamily(mediaType)===MediaFamily.Video){this.sendMediaRequestStatus(mediaType);}});});jmpSession.on(JmpSessionEvents.ActiveSpeaker,data=>{this.emit(MultistreamConnectionEventNames.ActiveSpeakerNotification,data.csis);});this.jmpSessions.set(mediaType,jmpSession);}updateRequestedStreams(mediaType,requests){var sendTransceiver=this.getSendTransceiverOrThrow(mediaType);var mediaFamily=getMediaFamily(mediaType);var requestedIdEncodingParamsMap=new Map();var rsRequests=requests.filter(r=>isValidReceiverSelectedInfo(r.policySpecificInfo));if(rsRequests.length!==requests.length){logger.warn('Ignoring non-receiver-selected requests');}rsRequests.forEach(_ref8=>{var{ids,policySpecificInfo,codecInfos,maxPayloadBitsPerSecond}=_ref8;var _a,_b,_c;if(ids.length>1){logErrorAndThrow(WcmeErrorType.INVALID_STREAM_REQUEST,"Stream request cannot have more than one ID.");}if(ids.length===0){return;}if(sendTransceiver.csi!==policySpecificInfo.csi){logger.warn('csi in the StreamRequest does not match');return;}var id=ids[0];var codecInfo=codecInfos[0];var streamIdsMatched=sendTransceiver.senderIds.some(validId=>compareStreamIds(id,validId));if(streamIdsMatched){var encodingIndex=sendTransceiver.getEncodingIndexForStreamId(id);if(encodingIndex!==-1){var encodingParams={maxPayloadBitsPerSecond};if(mediaFamily===MediaFamily.Video){encodingParams.maxFs=(_a=codecInfo===null||codecInfo===void 0?void 0:codecInfo.h264)===null||_a===void 0?void 0:_a.maxFs;encodingParams.maxWidth=(_b=codecInfo===null||codecInfo===void 0?void 0:codecInfo.h264)===null||_b===void 0?void 0:_b.maxWidth;encodingParams.maxHeight=(_c=codecInfo===null||codecInfo===void 0?void 0:codecInfo.h264)===null||_c===void 0?void 0:_c.maxHeight;}requestedIdEncodingParamsMap.set(encodingIndex,encodingParams);}else {logger.warn("".concat(mediaType,": Unable to get encoding index for stream ID: ").concat(JSON.stringify(id)));}}else {logger.warn("".concat(mediaType,": Unable to find matching stream ID for requested ID: ").concat(JSON.stringify(id)));}});return sendTransceiver.updateSendParameters(requestedIdEncodingParamsMap);}createDataChannel(){var dataChannel=this.pc.createDataChannel('datachannel',{ordered:false,maxRetransmits:0});dataChannel.onopen=event=>{logger.info('DataChannel opened:',JSON.stringify(event));[...this.sendTransceivers.keys()].forEach(mediaType=>{this.sendSourceAdvertisement(mediaType);});logger.info("Flushing pending JMP task queue");this.pendingJmpTasks.forEach(t=>t());this.pendingJmpTasks=[];};dataChannel.onmessage=e=>{var parsed;try{parsed=JSON.parse(e.data);}catch(err){logger.error("Error parsing datachannel JSON: ".concat(err));return;}logger.debug('DataChannel got msg:',e.data);var homerMsg=HomerMsg.fromJson(parsed);if(!homerMsg){logger.error("Received invalid datachannel message: ".concat(e));return;}var jmpMsg=homerMsg.payload;if(!isValidJmpMsg(jmpMsg)){logger.error("Received invalid JMP msg: ".concat(JSON.stringify(jmpMsg)));return;}var mediaType=getMediaType(jmpMsg.mediaFamily,jmpMsg.mediaContent);var jmpSession=this.jmpSessions.get(mediaType);if(!jmpSession){logger.error("Unable to find JMP session for media type ".concat(mediaType,"."));return;}jmpSession.receive(jmpMsg);};dataChannel.onclose=event=>{logger.info('DataChannel closed:',JSON.stringify(event));};dataChannel.onerror=event=>{logger.info('DataChannel error:',JSON.stringify(event));};this.dataChannel=dataChannel;}close(){this.sendTransceivers.forEach(t=>t.close());this.recvTransceivers.forEach(recvTransceivers=>{recvTransceivers.forEach(t=>t.close());});this.jmpSessions.forEach(jmpSession=>jmpSession.close());this.pc.close();}sendMediaRequestStatus(mediaType){var _a;if(getMediaFamily(mediaType)!==MediaFamily.Video){return;}if(!this.getSendTransceiverOrThrow(mediaType).requested){return;}var streamStates=this.getVideoStreamStates(mediaType);var task=()=>{var _a;(_a=this.jmpSessions.get(mediaType))===null||_a===void 0?void 0:_a.sendMediaRequestStatus(streamStates);};if(((_a=this.dataChannel)===null||_a===void 0?void 0:_a.readyState)==='open'){task();}else {this.pendingJmpTasks.push(task);}}sendSourceAdvertisement(mediaType){var _a,_b;var transceiver=this.getSendTransceiverOrThrow(mediaType);var numLiveSources=((_a=transceiver.publishedStream)===null||_a===void 0?void 0:_a.muted)===false?1:0;var task;if(getMediaFamily(mediaType)===MediaFamily.Video){var sources=this.getVideoStreamStates(mediaType);if(sources===null){return;}var contentHint;if(transceiver.publishedStream&&mediaType===MediaType.VideoSlides){contentHint=transceiver.publishedStream.contentHint;}task=()=>{var _a;(_a=this.jmpSessions.get(mediaType))===null||_a===void 0?void 0:_a.sendSourceAdvertisement(1,numLiveSources,[],webRtcVideoContentHintToJmpVideoContentHint(contentHint));};}else {task=()=>{var _a;return (_a=this.jmpSessions.get(mediaType))===null||_a===void 0?void 0:_a.sendSourceAdvertisement(1,numLiveSources,mediaType===MediaType.AudioMain?transceiver.namedMediaGroups:[]);};}if(((_b=this.dataChannel)===null||_b===void 0?void 0:_b.readyState)==='open'){task();}else {this.pendingJmpTasks.push(task);}}getVideoStreamStates(mediaType){var _a;var sendTransceiver=this.getSendTransceiverOrThrow(mediaType);var published=!!sendTransceiver.publishedStream;var muted=(_a=sendTransceiver.publishedStream)===null||_a===void 0?void 0:_a.muted;return sendTransceiver.senderIds.map(id=>{var state;if(!published){state='no source';}else if(muted){state='avatar';}else {state='live';}return {id,state,csi:sendTransceiver.csi};});}createReceiveTransceiver(mediaType){var rtcRtpTransceiver=this.pc.addTransceiver(toMediaStreamTrackKind(mediaType),{direction:'recvonly'});var transceiverMid=this.midPredictor.getNextMid(mediaType);var munger=new IngressSdpMunger();var recvOnlyTransceiver=new ReceiveOnlyTransceiver(rtcRtpTransceiver,transceiverMid,munger);recvOnlyTransceiver.twccDisabled=getMediaFamily(mediaType)===MediaFamily.Audio?this.options.disableAudioTwcc:false;this.recvTransceivers.set(mediaType,[...(this.recvTransceivers.get(mediaType)||[]),recvOnlyTransceiver]);return recvOnlyTransceiver;}createReceiveSlot(mediaType){return __awaiter$1(this,void 0,void 0,function*(){return (yield this.createReceiveSlots(mediaType,1))[0];});}createReceiveSlots(mediaType){var count=arguments.length>1&&arguments[1]!==undefined?arguments[1]:1;return __awaiter$1(this,void 0,void 0,function*(){return new Promise(createReceiveSlotsResolve=>{this.offerAnswerQueue.push(()=>__awaiter$1(this,void 0,void 0,function*(){var createdReceiveOnlyTransceivers=[];for(var i=0;i<count;i++){var recvOnlyTransceiver=this.createReceiveTransceiver(mediaType);createdReceiveOnlyTransceivers.push(recvOnlyTransceiver);}if(this.pc.getRemoteDescription()){yield this.doLocalOfferAnswer();}var receiveSlots=createdReceiveOnlyTransceivers.map(transceiver=>transceiver.receiveSlot);createReceiveSlotsResolve(receiveSlots);}));});});}getIngressPayloadType(mediaType,mimeType){var _a,_b,_c;var requestedMediaCodecType=mimeType.split('/')[1];var requestedMid=(_a=this.sendTransceivers.get(mediaType))===null||_a===void 0?void 0:_a.mid;var parsedOffer=parse$1((_b=this.pc.getLocalDescription())===null||_b===void 0?void 0:_b.sdp);var parsedAnswer=parse$1((_c=this.pc.getRemoteDescription())===null||_c===void 0?void 0:_c.sdp);var senderCodecs=parsedAnswer.avMedia.filter(media=>requestedMid===media.mid).map(media=>[...media.codecs.values()]).flat().filter(ci=>ci.name===requestedMediaCodecType);var receiverCodecs=parsedOffer.avMedia.filter(media=>requestedMid===media.mid).map(media=>[...media.codecs.values()]).flat().filter(ci=>ci.name===requestedMediaCodecType);if(!senderCodecs||!receiverCodecs||senderCodecs.length===0||receiverCodecs.length===0){logErrorAndThrow(WcmeErrorType.GET_PAYLOAD_TYPE_FAILED,"Sender codecs or receiver codecs is undefined or empty.");}var senderCodec=senderCodecs[0];var targetCodec=receiverCodecs.find(receiverCodec=>{return areCodecsCompatible(senderCodec,receiverCodec);});if(!targetCodec||!targetCodec.pt){logErrorAndThrow(WcmeErrorType.GET_PAYLOAD_TYPE_FAILED,"Cannot find matching receiver codec for the given mediaType/mimeType.");}return targetCodec.pt;}createOffer(){return __awaiter$1(this,void 0,void 0,function*(){if(!this.pc.getLocalDescription()){this.midPredictor.allocateMidForDatachannel();}if(this.setAnswerResolve){logger.info('Canceling previous offer since setAnswer was never called for it');this.setAnswerResolve();this.setAnswerResolve=undefined;}var createOfferId=++this.currentCreateOfferId;return new Promise((createOfferResolve,createOfferReject)=>{this.offerAnswerQueue.push(()=>__awaiter$1(this,void 0,void 0,function*(){var _a;try{var offer=yield this.pc.createOffer();if(!offer.sdp){logErrorAndThrow(WcmeErrorType.CREATE_OFFER_FAILED,'SDP not found in offer.');}offer.sdp=this.preProcessLocalOffer(offer.sdp);yield this.pc.setLocalDescription(offer).then(()=>__awaiter$1(this,void 0,void 0,function*(){logger.info('this.pc.setLocalDescription() resolved');})).catch(error=>{var _a;logErrorAndThrow(WcmeErrorType.CREATE_OFFER_FAILED,"Error: ".concat(error,". SDP: ").concat(maskIp((_a=offer.sdp)!==null&&_a!==void 0?_a:'')));});var sdpToSend=this.prepareLocalOfferForRemoteServer((_a=this.pc.getLocalDescription())===null||_a===void 0?void 0:_a.sdp);createOfferResolve({type:'offer',sdp:sdpToSend});if(this.currentCreateOfferId>createOfferId){logger.log('Canceling previous offer since createOffer was called while it was being created');}else {yield new Promise(setAnswerResolve=>{this.setAnswerResolve=setAnswerResolve;});}}catch(error){createOfferReject(error);}}));});});}setAnswer(answer){return __awaiter$1(this,void 0,void 0,function*(){var sdp=this.preProcessRemoteAnswer(answer);if(!this.setAnswerResolve){logErrorAndThrow(WcmeErrorType.SET_ANSWER_FAILED,"Call to setAnswer without having previously called createOffer.");}logger.info('calling this.pc.setRemoteDescription()');return this.pc.setRemoteDescription({type:'answer',sdp}).then(()=>__awaiter$1(this,void 0,void 0,function*(){logger.info('this.pc.setRemoteDescription() resolved');if(this.setAnswerResolve){this.setAnswerResolve();this.setAnswerResolve=undefined;}else {logger.debug("setAnswerResolve function was cleared between setAnswer and result of setRemoteDescription");}})).catch(error=>{logErrorAndThrow(WcmeErrorType.SET_ANSWER_FAILED,"Error: ".concat(error,". SDP: ").concat(maskIp(answer)));});});}doLocalOfferAnswer(){var _a;return __awaiter$1(this,void 0,void 0,function*(){var offer=yield this.pc.createOffer();if(!offer.sdp){logErrorAndThrow(WcmeErrorType.CREATE_OFFER_FAILED,'SDP not found in offer.');}offer.sdp=this.preProcessLocalOffer(offer.sdp);yield this.pc.setLocalDescription(offer).then(()=>__awaiter$1(this,void 0,void 0,function*(){logger.info('this.pc.setLocalDescription() resolved');})).catch(error=>{var _a;logErrorAndThrow(WcmeErrorType.CREATE_OFFER_FAILED,"Error: ".concat(error,". SDP: ").concat(maskIp((_a=offer.sdp)!==null&&_a!==void 0?_a:'')));});var answer=this.preProcessRemoteAnswer((_a=this.pc.getRemoteDescription())===null||_a===void 0?void 0:_a.sdp);return this.pc.setRemoteDescription({type:'answer',sdp:answer}).then(()=>__awaiter$1(this,void 0,void 0,function*(){logger.info('this.pc.setRemoteDescription() resolved');})).catch(error=>{logErrorAndThrow(WcmeErrorType.CREATE_OFFER_FAILED,"Error: ".concat(error,". SDP: ").concat(maskIp(answer)));});});}queueLocalOfferAnswer(){return __awaiter$1(this,void 0,void 0,function*(){return this.offerAnswerQueue.push(()=>__awaiter$1(this,void 0,void 0,function*(){yield this.doLocalOfferAnswer();}));});}preProcessLocalOffer(offer){var parsedOffer=parse$1(offer);parsedOffer.avMedia.filter(av=>av.direction==='recvonly').forEach(av=>{var recvTransceiver=this.getRecvTransceiverByMidOrThrow(av.mid);recvTransceiver.mungeLocalDescription(av);});parsedOffer.avMedia.filter(av=>av.direction==='sendrecv'||av.direction==='inactive').forEach(av=>{var sendTransceiver=this.getSendTransceiverByMidOrThrow(av.mid);sendTransceiver.mungeLocalDescription(av);});if(BrowserInfo.isFirefox()){setupBundle(parsedOffer,this.options.bundlePolicy,this.midPredictor.getMidMap());}return parsedOffer.toString();}prepareLocalOfferForRemoteServer(offer){var parsedOffer=parse$1(offer);parsedOffer.avMedia.filter(av=>av.direction==='sendrecv'||av.direction==='inactive').forEach(av=>{var sendTransceiver=this.getSendTransceiverByMidOrThrow(av.mid);sendTransceiver.mungeLocalDescriptionForRemoteServer(av);});parsedOffer.media.filter(media=>media instanceof ApplicationMediaDescription$1).forEach(media=>{injectDummyCandidates(media);});if(BrowserInfo.isFirefox()){setupBundle(parsedOffer,this.options.bundlePolicy,this.midPredictor.getMidMap());if(this.options.bundlePolicy==='max-bundle'){parsedOffer.media.forEach((media,index)=>{if(index>0){media.port=parsedOffer.media[0].port;}});}}filterRecvOnlyMediaDescriptions(parsedOffer);return parsedOffer.toString();}preProcessRemoteAnswer(answer){var _a;var parsedAnswer=parse$1(answer);var parsedOffer=parse$1((_a=this.pc.getLocalDescription())===null||_a===void 0?void 0:_a.sdp);matchMediaDescriptionsInAnswer(parsedOffer,parsedAnswer);parsedAnswer.avMedia.filter(av=>av.direction==='sendonly').forEach(av=>{var recvTransceiver=this.getRecvTransceiverByMidOrThrow(av.mid);recvTransceiver.mungeRemoteDescription(av);});parsedAnswer.avMedia.filter(av=>av.direction==='sendrecv'||av.direction==='recvonly').forEach(av=>{var sendTransceiver=this.getSendTransceiverByMidOrThrow(av.mid);sendTransceiver.mungeRemoteDescription(av);});parsedAnswer.media.filter(media=>media instanceof ApplicationMediaDescription$1).forEach(media=>{if(retainCandidates(media,['udp','tcp'])){logger.log("Some unsupported remote candidates have been removed from mid ".concat(media.mid));}});if(BrowserInfo.isFirefox()){setupBundle(parsedAnswer,this.options.bundlePolicy,this.midPredictor.getMidMap());}return parsedAnswer.toString();}getSendTransceiverOrThrow(mediaType){var sendTransceiver=this.sendTransceivers.get(mediaType);if(!sendTransceiver){logErrorAndThrow(WcmeErrorType.GET_TRANSCEIVER_FAILED,"Unable to find send transceiver for media type ".concat(mediaType,"."));}return sendTransceiver;}getSendTransceiverByMidOrThrow(mid){var transceiver=[...this.sendTransceivers.values()].find(t=>t.mid===mid);if(!transceiver){logErrorAndThrow(WcmeErrorType.GET_TRANSCEIVER_FAILED,"Unable to find send transceiver with MID ".concat(mid,"."));}return transceiver;}getRecvTransceiverByMidOrThrow(mid){var transceiver=[...this.recvTransceivers.values()].flat().find(t=>t.mid===mid);if(!transceiver){logErrorAndThrow(WcmeErrorType.GET_TRANSCEIVER_FAILED,"Unable to find recv transceiver with MID ".concat(mid,"."));}return transceiver;}requestMedia(mediaType,streamRequests){var _a;var task=()=>{var _a;var jmpSession=this.jmpSessions.get(mediaType);if(!jmpSession){logger.error("Unable to find jmp session for ".concat(mediaType));return;}var requestedReceiveSlotIds=[];streamRequests.forEach(sr=>{sr.receiveSlots.forEach(rs=>{if(!rs.id){logger.error("Running stream request task, but ReceiveSlot ID is missing!");return;}requestedReceiveSlotIds.push(rs.id);});});(_a=this.recvTransceivers.get(mediaType))===null||_a===void 0?void 0:_a.forEach(transceiver=>{if(!requestedReceiveSlotIds.some(id=>compareStreamIds(id,transceiver.receiveSlot.id))){transceiver.receiveSlot._updateSource('no source',undefined);}});jmpSession.sendRequests(streamRequests.map(sr=>sr._toJmpStreamRequest()));streamRequests.forEach(sr=>{var _a;var isAffectedInStreamRequest=t=>sr.receiveSlots.some(rs=>rs.id&&compareStreamIds(rs.id,t.receiverId));(_a=this.recvTransceivers.get(mediaType))===null||_a===void 0?void 0:_a.forEach(t=>{var _a,_b;if(isAffectedInStreamRequest(t)){t.requestedBitrate=sr.maxPayloadBitsPerSecond;t.requestedFrameSize=(_b=(_a=sr.codecInfos[0])===null||_a===void 0?void 0:_a.h264)===null||_b===void 0?void 0:_b.maxFs;}else {t.requestedBitrate=undefined;t.requestedFrameSize=undefined;}});});};if(((_a=this.dataChannel)===null||_a===void 0?void 0:_a.readyState)==='open'){task();}else {this.pendingJmpTasks.push(task);}}renewPeerConnection(userOptions){if(userOptions){this.options=Object.assign(Object.assign({},this.options),userOptions);}logger.info("Renewing multistream connection with options ".concat(JSON.stringify(this.options)));this.midPredictor.reset();this.initializePeerConnection();var mainSceneId=generateSceneId();var slidesSceneId=generateSceneId();this.sendTransceivers.forEach((transceiver,mediaType)=>{var _a;var mediaContent=getMediaContent(mediaType);var sceneId=mediaContent===MediaContent.Main?mainSceneId:slidesSceneId;var mid=this.midPredictor.getNextMid(mediaType);transceiver.replaceTransceiver(this.pc.addTransceiver(toMediaStreamTrackKind(mediaType),{direction:'sendrecv',sendEncodings:getMediaFamily(mediaType)===MediaFamily.Video?this.getVideoEncodingOptions(mediaContent):undefined}));transceiver.mid=mid;transceiver.csi=generateCsi(getMediaFamily(mediaType),sceneId);transceiver.resetSdpMunger();(_a=this.jmpSessions.get(mediaType))===null||_a===void 0?void 0:_a.close();this.createJmpSession(mediaType);});this.recvTransceivers.forEach((transceivers,mediaType)=>{transceivers.forEach(t=>{var mid=this.midPredictor.getNextMid(mediaType);t.replaceTransceiver(this.pc.addTransceiver(toMediaStreamTrackKind(mediaType),{direction:'recvonly'}));t.mid=mid;});});}getReceiveSlotById(id){return [...this.recvTransceivers.values()].flat().map(transceiver=>transceiver.receiveSlot).find(receiveSlot=>{var receiveSlotId=receiveSlot.id||{};return Object.keys(receiveSlotId).length===Object.keys(id).length&&Object.keys(receiveSlotId).every(key=>Object.prototype.hasOwnProperty.call(id,key)&&receiveSlotId[key]===id[key]);});}getStats(){return this.statsManager.getStats();}getTransceiverStats(){return __awaiter$1(this,void 0,void 0,function*(){return organizeTransceiverStats(this.sendTransceivers,this.recvTransceivers);});}preProcessStats(stats){return __awaiter$1(this,void 0,void 0,function*(){yield Promise.all([...this.sendTransceivers.entries()].map(_ref9=>{var[mediaType,transceiver]=_ref9;return __awaiter$1(this,void 0,void 0,function*(){(yield transceiver.getStats()).forEach(senderStats=>{var _a;if(senderStats.type==='outbound-rtp'){var statsToModify=stats.get(senderStats.id);statsToModify.mid=transceiver.mid;statsToModify.csi=transceiver.csi;statsToModify.calliopeMediaType=mediaType;statsToModify.requestedBitrate=transceiver.getRequestedBitrate(senderStats.ssrc);statsToModify.requestedFrameSize=transceiver.getRequestedFrameSize(senderStats.ssrc);var trackSettings=(_a=transceiver.publishedStream)===null||_a===void 0?void 0:_a.getSettings();if(trackSettings===null||trackSettings===void 0?void 0:trackSettings.frameRate){statsToModify.targetFrameRate=trackSettings===null||trackSettings===void 0?void 0:trackSettings.frameRate;}stats.set(senderStats.id,statsToModify);}else if(senderStats.type==='media-source'){var _statsToModify=stats.get(senderStats.id);_statsToModify.calliopeMediaType=mediaType;stats.set(senderStats.id,_statsToModify);}});});}));yield Promise.all([...this.recvTransceivers.entries()].map(_ref10=>{var[mediaType,transceivers]=_ref10;return __awaiter$1(this,void 0,void 0,function*(){yield Promise.all(transceivers.map(transceiver=>__awaiter$1(this,void 0,void 0,function*(){(yield transceiver.getStats()).forEach(receiverStats=>{var _a;if(receiverStats.type==='inbound-rtp'){var statsToModify=stats.get(receiverStats.id);statsToModify.mid=(_a=transceiver.receiveSlot.id)===null||_a===void 0?void 0:_a.mid;statsToModify.csi=transceiver.receiveSlot.currentRxCsi;statsToModify.calliopeMediaType=mediaType;statsToModify.requestedBitrate=transceiver.requestedBitrate;statsToModify.requestedFrameSize=transceiver.requestedFrameSize;Object.assign(statsToModify,transceiver.receiverId);stats.set(receiverStats.id,statsToModify);}});})));});}));});}attachMetricsObserver(){this.forceStatsReport=rtcStats_1(this.pc.getUnderlyingRTCPeerConnection(),data=>this.metricsCallback(data),5000,stats=>this.preProcessStats(stats)).forceStatsReport;}forceRtcMetricsCallback(){var _a;return (_a=this.forceStatsReport)===null||_a===void 0?void 0:_a.call(this);}setMetricsCallback(callback){this.metricsCallback=callback;}setOveruseUpdateCallback(callback){this.overuseUpdateCallback=callback;}getCsiByMediaType(mediaType){var _a;return (_a=this.sendTransceivers.get(mediaType))===null||_a===void 0?void 0:_a.csi;}getAllCsis(){return {audioMain:this.getCsiByMediaType(MediaType.AudioMain),audioSlides:this.getCsiByMediaType(MediaType.AudioSlides),videoMain:this.getCsiByMediaType(MediaType.VideoMain),videoSlides:this.getCsiByMediaType(MediaType.VideoSlides)};}}class StreamRequest{constructor(policy,policySpecificInfo,receiveSlots,maxPayloadBitsPerSecond){var codecInfos=arguments.length>4&&arguments[4]!==undefined?arguments[4]:[];this.policy=policy;this.policySpecificInfo=policySpecificInfo;this.receiveSlots=receiveSlots;this.maxPayloadBitsPerSecond=maxPayloadBitsPerSecond;this.codecInfos=codecInfos;}_toJmpStreamRequest(){return new StreamRequest$1(this.policy,this.policySpecificInfo,this.receiveSlots.map(rs=>rs.id),this.maxPayloadBitsPerSecond,this.codecInfos);}}
|
|
4884
|
+
*/function waterfall(tasks,callback){callback=once(callback);if(!Array.isArray(tasks))return callback(new Error('First argument to waterfall must be an array of functions'));if(!tasks.length)return callback();var taskIndex=0;function nextTask(args){var task=wrapAsync(tasks[taskIndex++]);task(...args,onlyOnce(next));}function next(err){if(err===false)return;for(var _len22=arguments.length,args=new Array(_len22>1?_len22-1:0),_key22=1;_key22<_len22;_key22++){args[_key22-1]=arguments[_key22];}if(err||taskIndex===tasks.length){return callback(err,...args);}nextTask(args);}nextTask([]);}awaitify(waterfall);function processTasks(task,finishedCallback){return __awaiter$1(this,void 0,void 0,function*(){try{yield task();finishedCallback();}catch(e){finishedCallback(e);}});}class AsyncQueue{constructor(){this.queue=queue$1(processTasks,1);}push(task){return this.queue.pushAsync(task);}empty(){return this.queue.empty();}}var OfferAnswerType;(function(OfferAnswerType){OfferAnswerType[OfferAnswerType["LocalOnly"]=0]="LocalOnly";OfferAnswerType[OfferAnswerType["Remote"]=1]="Remote";})(OfferAnswerType||(OfferAnswerType={}));class SendOnlyTransceiver extends Transceiver{constructor(rtcRtpTransceiver,mid,csi,munger,mediaType){super(rtcRtpTransceiver,mid);this.rtxEnabled=false;this.streamMuteStateChange=new TypedEvent();this.streamPublishStateChange=new TypedEvent();this.negotiationNeeded=new TypedEvent();this.namedMediaGroupsChange=new TypedEvent();this.requestedIdEncodingParamsMap=new Map();this.updateSendParametersQueue=new AsyncQueue();this.csi=csi;this.direction='sendrecv';this.handleTrackChange=this.handleTrackChange.bind(this);this.handleStreamConstraintsChange=this.handleStreamConstraintsChange.bind(this);this.handleStreamMuteStateChange=this.handleStreamMuteStateChange.bind(this);this.munger=munger;this.mediaType=mediaType;}replaceSenderSource(stream){var _a,_b;return __awaiter$1(this,void 0,void 0,function*(){var trackOrNull=(_a=stream===null||stream===void 0?void 0:stream.outputStream.getTracks()[0])!==null&&_a!==void 0?_a:null;if(((_b=this.sender.track)===null||_b===void 0?void 0:_b.id)!==(trackOrNull===null||trackOrNull===void 0?void 0:trackOrNull.id)){yield this.sender.replaceTrack(trackOrNull);if(trackOrNull){logger.log("Sender source for ".concat(this.mediaType," replaced with track ID ").concat(trackOrNull.id));}else {logger.log("Sender source for ".concat(this.mediaType," set to null, sender stopped"));}}});}handleTrackChange(){return __awaiter$1(this,void 0,void 0,function*(){if(this.requested){yield this.replaceSenderSource(this.publishedStream);}});}handleStreamConstraintsChange(){return __awaiter$1(this,void 0,void 0,function*(){yield this.updateSendParameters(this.requestedIdEncodingParamsMap);});}handleStreamMuteStateChange(){this.streamMuteStateChange.emit();}get requested(){return this.requestedIdEncodingParamsMap.size>0;}replaceTransceiver(newRtcRtpTransceiver){var _super=Object.create(null,{replaceTransceiver:{get:()=>super.replaceTransceiver}});return __awaiter$1(this,void 0,void 0,function*(){_super.replaceTransceiver.call(this,newRtcRtpTransceiver);newRtcRtpTransceiver.direction=this.direction;if(this.requested){yield this.replaceSenderSource(this.publishedStream);}});}replacePublishedStream(newStream){return __awaiter$1(this,void 0,void 0,function*(){var oldStream=this.publishedStream;oldStream===null||oldStream===void 0?void 0:oldStream.off(LocalStreamEventNames.OutputTrackChange,this.handleTrackChange);oldStream===null||oldStream===void 0?void 0:oldStream.off(LocalStreamEventNames.ConstraintsChange,this.handleStreamConstraintsChange);oldStream===null||oldStream===void 0?void 0:oldStream.off(LocalStreamEventNames.UserMuteStateChange,this.handleStreamMuteStateChange);oldStream===null||oldStream===void 0?void 0:oldStream.off(LocalStreamEventNames.SystemMuteStateChange,this.handleStreamMuteStateChange);if(this.requested){yield this.replaceSenderSource(newStream);}this.publishedStream=newStream;newStream===null||newStream===void 0?void 0:newStream.on(LocalStreamEventNames.OutputTrackChange,this.handleTrackChange);newStream===null||newStream===void 0?void 0:newStream.on(LocalStreamEventNames.ConstraintsChange,this.handleStreamConstraintsChange);newStream===null||newStream===void 0?void 0:newStream.on(LocalStreamEventNames.UserMuteStateChange,this.handleStreamMuteStateChange);newStream===null||newStream===void 0?void 0:newStream.on(LocalStreamEventNames.SystemMuteStateChange,this.handleStreamMuteStateChange);if(!oldStream&&newStream&&!newStream.muted||oldStream&&!newStream&&!oldStream.muted){this.streamPublishStateChange.emit();}else if((oldStream===null||oldStream===void 0?void 0:oldStream.muted)!==(newStream===null||newStream===void 0?void 0:newStream.muted)){this.streamMuteStateChange.emit();}});}setNamedMediaGroups(namedMediaGroups){if(this.mediaType!==MediaType.AudioMain){logErrorAndThrow(WcmeErrorType.SET_NMG_FAILED,"Named media groups can only be set for audio.");}this.namedMediaGroups=namedMediaGroups;this.namedMediaGroupsChange.emit();}publishStream(stream){return this.replacePublishedStream(stream);}unpublishStream(){return this.replacePublishedStream();}get active(){return this._rtcRtpTransceiver.direction==='sendrecv';}set active(enabled){this.direction=enabled?'sendrecv':'inactive';this._rtcRtpTransceiver.direction=this.direction;if(this._rtcRtpTransceiver.direction!==this._rtcRtpTransceiver.currentDirection){this.negotiationNeeded.emit(OfferAnswerType.Remote);}}getStats(){return __awaiter$1(this,void 0,void 0,function*(){var statsMap=new Map();var statsReport=yield this.sender.getStats();statsReport.forEach((stats,key)=>{var _a;if(stats.type==='outbound-rtp'){stats.mid=this.mid;stats.csi=this.csi;stats.calliopeMediaType=this.mediaType;stats.requestedBitrate=this.getRequestedBitrate(stats.ssrc);stats.requestedFrameSize=this.getRequestedFrameSize(stats.ssrc);var trackSettings=(_a=this.publishedStream)===null||_a===void 0?void 0:_a.getSettings();if(trackSettings===null||trackSettings===void 0?void 0:trackSettings.frameRate){stats.targetFrameRate=trackSettings===null||trackSettings===void 0?void 0:trackSettings.frameRate;}}else if(stats.type==='media-source'){stats.calliopeMediaType=this.mediaType;}statsMap.set(key,stats);});return statsMap;});}updateSendParameters(requestedIdEncodingParamsMap){return __awaiter$1(this,void 0,void 0,function*(){return this.updateSendParametersQueue.push(()=>__awaiter$1(this,void 0,void 0,function*(){var sendParameters=this.sender.getParameters();sendParameters.encodings.forEach((encoding,index)=>{var _a,_b;var encodingParams=requestedIdEncodingParamsMap.get(index);encoding.active=!!encodingParams;if(encodingParams){var{maxPayloadBitsPerSecond,maxFs,maxWidth,maxHeight}=encodingParams;var scaleDownRatio=getScaleDownRatio((_a=this.publishedStream)===null||_a===void 0?void 0:_a.getSettings().width,(_b=this.publishedStream)===null||_b===void 0?void 0:_b.getSettings().height,maxFs,maxWidth,maxHeight);if(maxPayloadBitsPerSecond!==undefined&&maxPayloadBitsPerSecond>=0){encoding.maxBitrate=maxPayloadBitsPerSecond;}if(scaleDownRatio!==undefined&&scaleDownRatio>=1.0){encoding.scaleResolutionDownBy=scaleDownRatio;}}});yield this.sender.setParameters(sendParameters);var oldRequested=this.requested;var newRequested=requestedIdEncodingParamsMap.size>0;this.requestedIdEncodingParamsMap=requestedIdEncodingParamsMap;if(oldRequested!==newRequested){yield this.replaceSenderSource(newRequested?this.publishedStream:null);}}));});}getRequestedBitrate(ssrc){var index=this.senderIds.findIndex(streamId=>streamId.ssrc===ssrc);if(this.requestedIdEncodingParamsMap.has(index)){var encodingParams=this.requestedIdEncodingParamsMap.get(index);return encodingParams===null||encodingParams===void 0?void 0:encodingParams.maxPayloadBitsPerSecond;}return undefined;}getRequestedFrameSize(ssrc){var index=this.senderIds.findIndex(streamId=>streamId.ssrc===ssrc);if(this.requestedIdEncodingParamsMap.has(index)){var encodingParams=this.requestedIdEncodingParamsMap.get(index);return encodingParams===null||encodingParams===void 0?void 0:encodingParams.maxFs;}return undefined;}isSimulcastEnabled(){var params=this.sender.getParameters();return params.encodings.length>1;}mungeLocalDescription(mediaDescription){this.munger.mungeLocalDescription(mediaDescription,{simulcastEnabled:this.isSimulcastEnabled(),rtxEnabled:this.rtxEnabled,twccDisabled:this.twccDisabled});}mungeLocalDescriptionForRemoteServer(mediaDescription){this.munger.mungeLocalDescriptionForRemoteServer(mediaDescription,getMediaContent(this.mediaType),this.csi);}mungeRemoteDescription(mediaDescription){this.munger.mungeRemoteDescription(mediaDescription);}get senderIds(){return this.munger.getSenderIds();}get numActiveSimulcastLayers(){var _a;if(getMediaFamily(this.mediaType)===MediaFamily.Video){return (_a=this.publishedStream)===null||_a===void 0?void 0:_a.getNumActiveSimulcastLayers();}return this.publishedStream?0:undefined;}getEncodingIndexForStreamId(id){return this.munger.getEncodingIndexForStreamId(id);}resetSdpMunger(){this.munger.reset();}setCodecParameters(parameters){this.munger.setCodecParameters(parameters);this.negotiationNeeded.emit(OfferAnswerType.LocalOnly);}deleteCodecParameters(parameters){this.munger.deleteCodecParameters(parameters);this.negotiationNeeded.emit(OfferAnswerType.LocalOnly);}}class SendSlot{constructor(sendTransceiver){this.sendTransceiver=sendTransceiver;}publishStream(stream){return __awaiter$1(this,void 0,void 0,function*(){if(stream===this.sendTransceiver.publishedStream){return Promise.resolve();}return this.sendTransceiver.publishStream(stream);});}unpublishStream(){return __awaiter$1(this,void 0,void 0,function*(){if(!this.sendTransceiver.publishedStream){return Promise.resolve();}return this.sendTransceiver.unpublishStream();});}setNamedMediaGroups(namedMediaGroups){this.sendTransceiver.setNamedMediaGroups(namedMediaGroups);}clearNamedMediaGroups(){this.setNamedMediaGroups([]);}get active(){return this.sendTransceiver.active;}set active(active){this.sendTransceiver.active=active;}setCodecParameters(parameters){return __awaiter$1(this,void 0,void 0,function*(){this.sendTransceiver.setCodecParameters(parameters);});}deleteCodecParameters(parameters){return __awaiter$1(this,void 0,void 0,function*(){this.sendTransceiver.deleteCodecParameters(parameters);});}}class StatsManager{constructor(statsGetter){var statsPreprocessor=arguments.length>1&&arguments[1]!==undefined?arguments[1]:()=>__awaiter$1(this,void 0,void 0,function*(){});this.statsGetter=statsGetter;this.statsPreProcessor=statsPreprocessor;}getStats(){return __awaiter$1(this,void 0,void 0,function*(){var statsReport=yield this.statsGetter();var statsMap=new Map();statsReport.forEach((stats,key)=>statsMap.set(key,stats));yield this.statsPreProcessor(statsMap);return statsMap;});}}var organizeTransceiverStats=(sendTransceivers,recvTransceivers)=>__awaiter$1(void 0,void 0,void 0,function*(){var result={audio:{senders:[],receivers:[]},video:{senders:[],receivers:[]},screenShareAudio:{senders:[],receivers:[]},screenShareVideo:{senders:[],receivers:[]}};yield Promise.all([...sendTransceivers.entries()].map(_ref6=>{var[mediaType,transceiver]=_ref6;return __awaiter$1(void 0,void 0,void 0,function*(){var _a;var item={report:yield transceiver.getStats(),mid:transceiver.mid,csi:transceiver.csi,currentDirection:'sendonly',localTrackLabel:(_a=transceiver.publishedStream)===null||_a===void 0?void 0:_a.label};if(mediaType===MediaType.AudioMain){result.audio.senders.push(item);}if(mediaType===MediaType.VideoMain){result.video.senders.push(item);}if(mediaType===MediaType.AudioSlides){result.screenShareAudio.senders.push(item);}if(mediaType===MediaType.VideoSlides){result.screenShareVideo.senders.push(item);}});}));yield Promise.all([...recvTransceivers.entries()].map(_ref7=>{var[mediaType,transceivers]=_ref7;return __awaiter$1(void 0,void 0,void 0,function*(){return Promise.all(transceivers.map(t=>__awaiter$1(void 0,void 0,void 0,function*(){var _b,_c;var item={report:yield t.getStats(),mid:(_b=t.receiveSlot.id)===null||_b===void 0?void 0:_b.mid,csi:t.receiveSlot.currentRxCsi,currentDirection:'recvonly',localTrackLabel:(_c=t.receiveSlot.stream.getTracks()[0])===null||_c===void 0?void 0:_c.label};if(mediaType===MediaType.AudioMain){result.audio.receivers.push(item);}if(mediaType===MediaType.VideoMain){result.video.receivers.push(item);}if(mediaType===MediaType.AudioSlides){result.screenShareAudio.receivers.push(item);}if(mediaType===MediaType.VideoSlides){result.screenShareVideo.receivers.push(item);}})));});}));return result;});function toMediaStreamTrackKind(mediaType){return [MediaType.VideoMain,MediaType.VideoSlides].includes(mediaType)?MediaStreamTrackKind.Video:MediaStreamTrackKind.Audio;}function webRtcVideoContentHintToJmpVideoContentHint(hint){if(hint==='motion'){return 'motion';}if(hint==='detail'){return 'sharpness';}return undefined;}var MultistreamConnectionEventNames;(function(MultistreamConnectionEventNames){MultistreamConnectionEventNames["VideoSourceCountUpdate"]="video-source-count-update";MultistreamConnectionEventNames["AudioSourceCountUpdate"]="audio-source-count-update";MultistreamConnectionEventNames["ActiveSpeakerNotification"]="active-speaker-notification";MultistreamConnectionEventNames["ConnectionStateUpdate"]="connection-state-update";MultistreamConnectionEventNames["IceGatheringStateUpdate"]="ice-gathering-state-update";MultistreamConnectionEventNames["NegotiationNeeded"]="negotiation-needed";MultistreamConnectionEventNames["CreateOfferOnSuccess"]="createofferonsuccess";MultistreamConnectionEventNames["CreateAnswerOnSuccess"]="createansweronsuccess";MultistreamConnectionEventNames["SetLocalDescriptionOnSuccess"]="setlocaldescriptiononsuccess";MultistreamConnectionEventNames["SetRemoteDescriptionOnSuccess"]="setremotedescriptiononsuccess";})(MultistreamConnectionEventNames||(MultistreamConnectionEventNames={}));var defaultMultistreamConnectionOptions={disableSimulcast:BrowserInfo.isFirefox(),bundlePolicy:'max-compat',iceServers:undefined,disableContentSimulcast:true,disableAudioTwcc:true};class MultistreamConnection extends EventEmitter$2{constructor(){var userOptions=arguments.length>0&&arguments[0]!==undefined?arguments[0]:{};super();this.sendTransceivers=new Map();this.recvTransceivers=new Map();this.jmpSessions=new Map();this.pendingJmpTasks=[];this.metricsCallback=()=>{};this.overuseUpdateCallback=()=>{};this.midPredictor=new MidPredictor();this.offerAnswerQueue=new AsyncQueue();this.currentCreateOfferId=0;this.options=Object.assign(Object.assign({},defaultMultistreamConnectionOptions),userOptions);logger.info("Creating multistream connection with options ".concat(JSON.stringify(this.options)));this.initializePeerConnection();this.overuseStateManager=new OveruseStateManager(overuseState=>this.overuseUpdateCallback(overuseState));this.overuseStateManager.start();this.statsManager=new StatsManager(()=>this.pc.getStats(),stats=>this.preProcessStats(stats));var mainSceneId=generateSceneId();var slidesSceneId=generateSceneId();var videoMainEncodingOptions=this.getVideoEncodingOptions(MediaContent.Main);var videoSlidesEncodingOptions=this.getVideoEncodingOptions(MediaContent.Slides);this.createSendTransceiver(MediaType.VideoMain,mainSceneId,videoMainEncodingOptions);this.createSendTransceiver(MediaType.AudioMain,mainSceneId);this.createSendTransceiver(MediaType.VideoSlides,slidesSceneId,videoSlidesEncodingOptions);this.createSendTransceiver(MediaType.AudioSlides,slidesSceneId);}initializePeerConnection(){var _a;(_a=this.pc)===null||_a===void 0?void 0:_a.close();this.pc=new PeerConnection({iceServers:this.options.iceServers,bundlePolicy:this.options.bundlePolicy});this.propagatePeerConnectionEvents();this.attachMetricsObserver();this.createDataChannel();}propagatePeerConnectionEvents(){this.pc.on(PeerConnection.Events.ConnectionStateChange,state=>{this.emit(MultistreamConnectionEventNames.ConnectionStateUpdate,state);});this.pc.on(PeerConnection.Events.CreateOfferOnSuccess,description=>{this.emit(MultistreamConnectionEventNames.CreateOfferOnSuccess,description);});this.pc.on(PeerConnection.Events.CreateAnswerOnSuccess,description=>{this.emit(MultistreamConnectionEventNames.CreateAnswerOnSuccess,description);});this.pc.on(PeerConnection.Events.SetLocalDescriptionOnSuccess,description=>{this.emit(MultistreamConnectionEventNames.SetLocalDescriptionOnSuccess,description);});this.pc.on(PeerConnection.Events.SetRemoteDescriptionOnSuccess,description=>{this.emit(MultistreamConnectionEventNames.SetRemoteDescriptionOnSuccess,description);});this.pc.on(PeerConnection.Events.IceGatheringStateChange,()=>{this.emit(MultistreamConnectionEventNames.IceGatheringStateUpdate,this.getIceGatheringState());});}getConnectionState(){return this.pc.getConnectionState();}getIceGatheringState(){return this.pc.iceGatheringState;}getVideoEncodingOptions(content){var enabledSimulcast=content===MediaContent.Main?!this.options.disableSimulcast:!this.options.disableContentSimulcast;return enabledSimulcast?[{scaleResolutionDownBy:4,active:false},{scaleResolutionDownBy:2,active:false},{active:false}]:[{active:false}];}createSendTransceiver(mediaType,sceneId,sendEncodingsOptions){var rtcTransceiver;try{rtcTransceiver=this.pc.addTransceiver(toMediaStreamTrackKind(mediaType),{direction:'sendrecv',sendEncodings:sendEncodingsOptions});}catch(e){logger.error("addTransceiver failed due to : ".concat(e));throw e;}var mid=this.midPredictor.getNextMid(mediaType);var csi=generateCsi(getMediaFamily(mediaType),sceneId);var munger=new EgressSdpMunger();var transceiver=new SendOnlyTransceiver(rtcTransceiver,mid,csi,munger,mediaType);if(getMediaFamily(mediaType)===MediaFamily.Video){transceiver.rtxEnabled=true;transceiver.setCodecParameters({'max-mbps':"".concat(defaultMaxVideoEncodeMbps),'max-fs':"".concat(defaultMaxVideoEncodeFrameSize)});}transceiver.twccDisabled=getMediaFamily(mediaType)===MediaFamily.Audio?this.options.disableAudioTwcc:false;transceiver.active=false;transceiver.streamMuteStateChange.on(()=>{this.sendSourceAdvertisement(mediaType);this.sendMediaRequestStatus(mediaType);});transceiver.streamPublishStateChange.on(()=>{this.sendSourceAdvertisement(mediaType);this.sendMediaRequestStatus(mediaType);});transceiver.negotiationNeeded.on(offerAnswerType=>{if(offerAnswerType===OfferAnswerType.Remote){this.emit(MultistreamConnectionEventNames.NegotiationNeeded);}else if(this.pc.getRemoteDescription()){this.queueLocalOfferAnswer();}});transceiver.namedMediaGroupsChange.on(()=>{this.sendSourceAdvertisement(mediaType);});this.sendTransceivers.set(mediaType,transceiver);this.createJmpSession(mediaType);}createSendSlot(mediaType){var active=arguments.length>1&&arguments[1]!==undefined?arguments[1]:true;var transceiver=this.getSendTransceiverOrThrow(mediaType);transceiver.active=active;return new SendSlot(transceiver);}createJmpSession(mediaType){var jmpSession=new JmpSession(getMediaFamily(mediaType),getMediaContent(mediaType));jmpSession.setTxCallback(msg=>{var _a;if(((_a=this.dataChannel)===null||_a===void 0?void 0:_a.readyState)!=='open'){logger.error("DataChannel not created or not connected. Unable to send JMP message.");return;}try{logger.info("Sending JMP message (size: ".concat(msg.length,"): ").concat(msg));this.dataChannel.send(msg);}catch(err){var{bufferedAmount,readyState}=this.dataChannel;var{sctp}=this.pc.getUnderlyingRTCPeerConnection();var{maxMessageSize,state}=sctp||{};logErrorAndThrow(WcmeErrorType.DATA_CHANNEL_SEND_FAILED,"Sending JMP message failed with error: ".concat(err,",\nMessage size: ").concat(msg.length,",\nData Channel State: ").concat(readyState,",\nData Channel Buffered amount: ").concat(bufferedAmount,",\nSCTP State: ").concat(state,",\nSCTP Max Message Size: ").concat(maxMessageSize));}});var prevNumTotalSources=0;var prevNumLiveSources=0;jmpSession.on(JmpSessionEvents.SourceAdvertisementReceived,data=>{logger.log("SourceAdvertisement received: ".concat(JSON.stringify(data)));if(data.numTotalSources!==prevNumTotalSources||data.numLiveSources!==prevNumLiveSources){prevNumTotalSources=data.numTotalSources;prevNumLiveSources=data.numLiveSources;var eventName=getMediaFamily(mediaType)===MediaFamily.Video?MultistreamConnectionEventNames.VideoSourceCountUpdate:MultistreamConnectionEventNames.AudioSourceCountUpdate;this.emit(eventName,data.numTotalSources,data.numLiveSources,getMediaContent(mediaType));}else {logger.log('Number of sources was unchanged, ignoring message');}});jmpSession.on(JmpSessionEvents.MediaRequestStatusReceived,data=>{logger.log("MediaRequestStatus received: ".concat(JSON.stringify(data)));data.streamStates.forEach(s=>{var receiveSlot=this.getReceiveSlotById(s.id);if(!receiveSlot){logger.warn("Got MediaRequestStatus for unknown receive slot: ".concat(JSON.stringify(s.id)));return;}receiveSlot._updateSource(s.state,s.csi);});});jmpSession.on(JmpSessionEvents.MediaRequestReceived,data=>{logger.log("MediaRequest received: ".concat(JSON.stringify(data)));this.updateRequestedStreams(mediaType,data.requests).then(()=>{if(getMediaFamily(mediaType)===MediaFamily.Video){this.sendMediaRequestStatus(mediaType);}});});jmpSession.on(JmpSessionEvents.ActiveSpeaker,data=>{this.emit(MultistreamConnectionEventNames.ActiveSpeakerNotification,data.csis);});this.jmpSessions.set(mediaType,jmpSession);}updateRequestedStreams(mediaType,requests){var sendTransceiver=this.getSendTransceiverOrThrow(mediaType);var mediaFamily=getMediaFamily(mediaType);var requestedIdEncodingParamsMap=new Map();var rsRequests=requests.filter(r=>isValidReceiverSelectedInfo(r.policySpecificInfo));if(rsRequests.length!==requests.length){logger.warn('Ignoring non-receiver-selected requests');}rsRequests.forEach(_ref8=>{var{ids,policySpecificInfo,codecInfos,maxPayloadBitsPerSecond}=_ref8;var _a,_b,_c;if(ids.length>1){logErrorAndThrow(WcmeErrorType.INVALID_STREAM_REQUEST,"Stream request cannot have more than one ID.");}if(ids.length===0){return;}if(sendTransceiver.csi!==policySpecificInfo.csi){logger.warn('csi in the StreamRequest does not match');return;}var id=ids[0];var codecInfo=codecInfos[0];var streamIdsMatched=sendTransceiver.senderIds.some(validId=>compareStreamIds(id,validId));if(streamIdsMatched){var encodingIndex=sendTransceiver.getEncodingIndexForStreamId(id);if(encodingIndex!==-1){var encodingParams={maxPayloadBitsPerSecond};if(mediaFamily===MediaFamily.Video){encodingParams.maxFs=(_a=codecInfo===null||codecInfo===void 0?void 0:codecInfo.h264)===null||_a===void 0?void 0:_a.maxFs;encodingParams.maxWidth=(_b=codecInfo===null||codecInfo===void 0?void 0:codecInfo.h264)===null||_b===void 0?void 0:_b.maxWidth;encodingParams.maxHeight=(_c=codecInfo===null||codecInfo===void 0?void 0:codecInfo.h264)===null||_c===void 0?void 0:_c.maxHeight;}requestedIdEncodingParamsMap.set(encodingIndex,encodingParams);}else {logger.warn("".concat(mediaType,": Unable to get encoding index for stream ID: ").concat(JSON.stringify(id)));}}else {logger.warn("".concat(mediaType,": Unable to find matching stream ID for requested ID: ").concat(JSON.stringify(id)));}});return sendTransceiver.updateSendParameters(requestedIdEncodingParamsMap);}createDataChannel(){var dataChannel=this.pc.createDataChannel('datachannel',{ordered:false,maxRetransmits:0});dataChannel.onopen=event=>{logger.info('DataChannel opened:',JSON.stringify(event));[...this.sendTransceivers.keys()].forEach(mediaType=>{this.sendSourceAdvertisement(mediaType);});logger.info("Flushing pending JMP task queue");this.pendingJmpTasks.forEach(t=>t());this.pendingJmpTasks=[];};dataChannel.onmessage=e=>{var parsed;try{parsed=JSON.parse(e.data);}catch(err){logger.error("Error parsing datachannel JSON: ".concat(err));return;}logger.debug('DataChannel got msg:',e.data);var homerMsg=HomerMsg.fromJson(parsed);if(!homerMsg){logger.error("Received invalid datachannel message: ".concat(e));return;}var jmpMsg=homerMsg.payload;if(!isValidJmpMsg(jmpMsg)){logger.error("Received invalid JMP msg: ".concat(JSON.stringify(jmpMsg)));return;}var mediaType=getMediaType(jmpMsg.mediaFamily,jmpMsg.mediaContent);var jmpSession=this.jmpSessions.get(mediaType);if(!jmpSession){logger.error("Unable to find JMP session for media type ".concat(mediaType,"."));return;}jmpSession.receive(jmpMsg);};dataChannel.onclose=event=>{logger.info('DataChannel closed:',JSON.stringify(event));};dataChannel.onerror=event=>{logger.info('DataChannel error:',JSON.stringify(event));};this.dataChannel=dataChannel;}close(){this.sendTransceivers.forEach(t=>t.close());this.recvTransceivers.forEach(recvTransceivers=>{recvTransceivers.forEach(t=>t.close());});this.jmpSessions.forEach(jmpSession=>jmpSession.close());this.pc.close();}sendMediaRequestStatus(mediaType){var _a;if(getMediaFamily(mediaType)!==MediaFamily.Video){return;}if(!this.getSendTransceiverOrThrow(mediaType).requested){return;}var streamStates=this.getVideoStreamStates(mediaType);var task=()=>{var _a;(_a=this.jmpSessions.get(mediaType))===null||_a===void 0?void 0:_a.sendMediaRequestStatus(streamStates);};if(((_a=this.dataChannel)===null||_a===void 0?void 0:_a.readyState)==='open'){task();}else {this.pendingJmpTasks.push(task);}}sendSourceAdvertisement(mediaType){var _a,_b;var transceiver=this.getSendTransceiverOrThrow(mediaType);var numLiveSources=((_a=transceiver.publishedStream)===null||_a===void 0?void 0:_a.muted)===false?1:0;var task;if(getMediaFamily(mediaType)===MediaFamily.Video){var sources=this.getVideoStreamStates(mediaType);if(sources===null){return;}var contentHint;if(transceiver.publishedStream&&mediaType===MediaType.VideoSlides){contentHint=transceiver.publishedStream.contentHint;}task=()=>{var _a;(_a=this.jmpSessions.get(mediaType))===null||_a===void 0?void 0:_a.sendSourceAdvertisement(1,numLiveSources,[],webRtcVideoContentHintToJmpVideoContentHint(contentHint));};}else {task=()=>{var _a;return (_a=this.jmpSessions.get(mediaType))===null||_a===void 0?void 0:_a.sendSourceAdvertisement(1,numLiveSources,mediaType===MediaType.AudioMain?transceiver.namedMediaGroups:[]);};}if(((_b=this.dataChannel)===null||_b===void 0?void 0:_b.readyState)==='open'){task();}else {this.pendingJmpTasks.push(task);}}getVideoStreamStates(mediaType){var _a;var sendTransceiver=this.getSendTransceiverOrThrow(mediaType);var published=!!sendTransceiver.publishedStream;var muted=(_a=sendTransceiver.publishedStream)===null||_a===void 0?void 0:_a.muted;return sendTransceiver.senderIds.map(id=>{var state;if(!published){state='no source';}else if(muted){state='avatar';}else {state='live';}return {id,state,csi:sendTransceiver.csi};});}createReceiveTransceiver(mediaType){var rtcRtpTransceiver=this.pc.addTransceiver(toMediaStreamTrackKind(mediaType),{direction:'recvonly'});var transceiverMid=this.midPredictor.getNextMid(mediaType);var munger=new IngressSdpMunger();var recvOnlyTransceiver=new ReceiveOnlyTransceiver(rtcRtpTransceiver,transceiverMid,munger);recvOnlyTransceiver.metadata.mediaType=mediaType;recvOnlyTransceiver.twccDisabled=getMediaFamily(mediaType)===MediaFamily.Audio?this.options.disableAudioTwcc:false;this.recvTransceivers.set(mediaType,[...(this.recvTransceivers.get(mediaType)||[]),recvOnlyTransceiver]);return recvOnlyTransceiver;}createReceiveSlot(mediaType){return __awaiter$1(this,void 0,void 0,function*(){return (yield this.createReceiveSlots(mediaType,1))[0];});}createReceiveSlots(mediaType){var count=arguments.length>1&&arguments[1]!==undefined?arguments[1]:1;return __awaiter$1(this,void 0,void 0,function*(){return new Promise(createReceiveSlotsResolve=>{this.offerAnswerQueue.push(()=>__awaiter$1(this,void 0,void 0,function*(){var createdReceiveOnlyTransceivers=[];for(var i=0;i<count;i++){var recvOnlyTransceiver=this.createReceiveTransceiver(mediaType);createdReceiveOnlyTransceivers.push(recvOnlyTransceiver);}if(this.pc.getRemoteDescription()){yield this.doLocalOfferAnswer();}var receiveSlots=createdReceiveOnlyTransceivers.map(transceiver=>transceiver.receiveSlot);createReceiveSlotsResolve(receiveSlots);}));});});}getIngressPayloadType(mediaType,mimeType){var _a,_b,_c;var requestedMediaCodecType=mimeType.split('/')[1];var requestedMid=(_a=this.sendTransceivers.get(mediaType))===null||_a===void 0?void 0:_a.mid;var parsedOffer=parse$1((_b=this.pc.getLocalDescription())===null||_b===void 0?void 0:_b.sdp);var parsedAnswer=parse$1((_c=this.pc.getRemoteDescription())===null||_c===void 0?void 0:_c.sdp);var senderCodecs=parsedAnswer.avMedia.filter(media=>requestedMid===media.mid).map(media=>[...media.codecs.values()]).flat().filter(ci=>ci.name===requestedMediaCodecType);var receiverCodecs=parsedOffer.avMedia.filter(media=>requestedMid===media.mid).map(media=>[...media.codecs.values()]).flat().filter(ci=>ci.name===requestedMediaCodecType);if(!senderCodecs||!receiverCodecs||senderCodecs.length===0||receiverCodecs.length===0){logErrorAndThrow(WcmeErrorType.GET_PAYLOAD_TYPE_FAILED,"Sender codecs or receiver codecs is undefined or empty.");}var senderCodec=senderCodecs[0];var targetCodec=receiverCodecs.find(receiverCodec=>{return areCodecsCompatible(senderCodec,receiverCodec);});if(!targetCodec||!targetCodec.pt){logErrorAndThrow(WcmeErrorType.GET_PAYLOAD_TYPE_FAILED,"Cannot find matching receiver codec for the given mediaType/mimeType.");}return targetCodec.pt;}createOffer(){return __awaiter$1(this,void 0,void 0,function*(){if(!this.pc.getLocalDescription()){this.midPredictor.allocateMidForDatachannel();}if(this.setAnswerResolve){logger.info('Canceling previous offer since setAnswer was never called for it');this.setAnswerResolve();this.setAnswerResolve=undefined;}var createOfferId=++this.currentCreateOfferId;return new Promise((createOfferResolve,createOfferReject)=>{this.offerAnswerQueue.push(()=>__awaiter$1(this,void 0,void 0,function*(){var _a;try{var offer=yield this.pc.createOffer();if(!offer.sdp){logErrorAndThrow(WcmeErrorType.CREATE_OFFER_FAILED,'SDP not found in offer.');}offer.sdp=this.preProcessLocalOffer(offer.sdp);yield this.pc.setLocalDescription(offer).then(()=>__awaiter$1(this,void 0,void 0,function*(){logger.info('this.pc.setLocalDescription() resolved');})).catch(error=>{var _a;logErrorAndThrow(WcmeErrorType.CREATE_OFFER_FAILED,"Error: ".concat(error,". SDP: ").concat(maskIp((_a=offer.sdp)!==null&&_a!==void 0?_a:'')));});var sdpToSend=this.prepareLocalOfferForRemoteServer((_a=this.pc.getLocalDescription())===null||_a===void 0?void 0:_a.sdp);createOfferResolve({type:'offer',sdp:sdpToSend});if(this.currentCreateOfferId>createOfferId){logger.log('Canceling previous offer since createOffer was called while it was being created');}else {yield new Promise(setAnswerResolve=>{this.setAnswerResolve=setAnswerResolve;});}}catch(error){createOfferReject(error);}}));});});}setAnswer(answer){return __awaiter$1(this,void 0,void 0,function*(){var sdp=this.preProcessRemoteAnswer(answer);if(!this.setAnswerResolve){logErrorAndThrow(WcmeErrorType.SET_ANSWER_FAILED,"Call to setAnswer without having previously called createOffer.");}logger.info('calling this.pc.setRemoteDescription()');return this.pc.setRemoteDescription({type:'answer',sdp}).then(()=>__awaiter$1(this,void 0,void 0,function*(){logger.info('this.pc.setRemoteDescription() resolved');if(this.setAnswerResolve){this.setAnswerResolve();this.setAnswerResolve=undefined;}else {logger.debug("setAnswerResolve function was cleared between setAnswer and result of setRemoteDescription");}})).catch(error=>{logErrorAndThrow(WcmeErrorType.SET_ANSWER_FAILED,"Error: ".concat(error,". SDP: ").concat(maskIp(answer)));});});}doLocalOfferAnswer(){var _a;return __awaiter$1(this,void 0,void 0,function*(){var offer=yield this.pc.createOffer();if(!offer.sdp){logErrorAndThrow(WcmeErrorType.CREATE_OFFER_FAILED,'SDP not found in offer.');}offer.sdp=this.preProcessLocalOffer(offer.sdp);yield this.pc.setLocalDescription(offer).then(()=>__awaiter$1(this,void 0,void 0,function*(){logger.info('this.pc.setLocalDescription() resolved');})).catch(error=>{var _a;logErrorAndThrow(WcmeErrorType.CREATE_OFFER_FAILED,"Error: ".concat(error,". SDP: ").concat(maskIp((_a=offer.sdp)!==null&&_a!==void 0?_a:'')));});var answer=this.preProcessRemoteAnswer((_a=this.pc.getRemoteDescription())===null||_a===void 0?void 0:_a.sdp);return this.pc.setRemoteDescription({type:'answer',sdp:answer}).then(()=>__awaiter$1(this,void 0,void 0,function*(){logger.info('this.pc.setRemoteDescription() resolved');})).catch(error=>{logErrorAndThrow(WcmeErrorType.CREATE_OFFER_FAILED,"Error: ".concat(error,". SDP: ").concat(maskIp(answer)));});});}queueLocalOfferAnswer(){return __awaiter$1(this,void 0,void 0,function*(){return this.offerAnswerQueue.push(()=>__awaiter$1(this,void 0,void 0,function*(){yield this.doLocalOfferAnswer();}));});}preProcessLocalOffer(offer){var parsedOffer=parse$1(offer);parsedOffer.avMedia.filter(av=>av.direction==='recvonly').forEach(av=>{var recvTransceiver=this.getRecvTransceiverByMidOrThrow(av.mid);recvTransceiver.mungeLocalDescription(av);});parsedOffer.avMedia.filter(av=>av.direction==='sendrecv'||av.direction==='inactive').forEach(av=>{var sendTransceiver=this.getSendTransceiverByMidOrThrow(av.mid);sendTransceiver.mungeLocalDescription(av);});if(BrowserInfo.isFirefox()){setupBundle(parsedOffer,this.options.bundlePolicy,this.midPredictor.getMidMap());}return parsedOffer.toString();}prepareLocalOfferForRemoteServer(offer){var parsedOffer=parse$1(offer);parsedOffer.avMedia.filter(av=>av.direction==='sendrecv'||av.direction==='inactive').forEach(av=>{var sendTransceiver=this.getSendTransceiverByMidOrThrow(av.mid);sendTransceiver.mungeLocalDescriptionForRemoteServer(av);});parsedOffer.media.filter(media=>media instanceof ApplicationMediaDescription$1).forEach(media=>{injectDummyCandidates(media);});if(BrowserInfo.isFirefox()){setupBundle(parsedOffer,this.options.bundlePolicy,this.midPredictor.getMidMap());if(this.options.bundlePolicy==='max-bundle'){parsedOffer.media.forEach((media,index)=>{if(index>0){media.port=parsedOffer.media[0].port;}});}}filterRecvOnlyMediaDescriptions(parsedOffer);return parsedOffer.toString();}preProcessRemoteAnswer(answer){var _a;var parsedAnswer=parse$1(answer);var parsedOffer=parse$1((_a=this.pc.getLocalDescription())===null||_a===void 0?void 0:_a.sdp);matchMediaDescriptionsInAnswer(parsedOffer,parsedAnswer);parsedAnswer.avMedia.filter(av=>av.direction==='sendonly').forEach(av=>{var recvTransceiver=this.getRecvTransceiverByMidOrThrow(av.mid);recvTransceiver.mungeRemoteDescription(av);});parsedAnswer.avMedia.filter(av=>av.direction==='sendrecv'||av.direction==='recvonly').forEach(av=>{var sendTransceiver=this.getSendTransceiverByMidOrThrow(av.mid);sendTransceiver.mungeRemoteDescription(av);});parsedAnswer.media.filter(media=>media instanceof ApplicationMediaDescription$1).forEach(media=>{if(retainCandidates(media,['udp','tcp'])){logger.log("Some unsupported remote candidates have been removed from mid ".concat(media.mid));}});if(BrowserInfo.isFirefox()){setupBundle(parsedAnswer,this.options.bundlePolicy,this.midPredictor.getMidMap());}return parsedAnswer.toString();}getSendTransceiverOrThrow(mediaType){var sendTransceiver=this.sendTransceivers.get(mediaType);if(!sendTransceiver){logErrorAndThrow(WcmeErrorType.GET_TRANSCEIVER_FAILED,"Unable to find send transceiver for media type ".concat(mediaType,"."));}return sendTransceiver;}getSendTransceiverByMidOrThrow(mid){var transceiver=[...this.sendTransceivers.values()].find(t=>t.mid===mid);if(!transceiver){logErrorAndThrow(WcmeErrorType.GET_TRANSCEIVER_FAILED,"Unable to find send transceiver with MID ".concat(mid,"."));}return transceiver;}getRecvTransceiverByMidOrThrow(mid){var transceiver=[...this.recvTransceivers.values()].flat().find(t=>t.mid===mid);if(!transceiver){logErrorAndThrow(WcmeErrorType.GET_TRANSCEIVER_FAILED,"Unable to find recv transceiver with MID ".concat(mid,"."));}return transceiver;}requestMedia(mediaType,streamRequests){var _a;var task=()=>{var _a;var jmpSession=this.jmpSessions.get(mediaType);if(!jmpSession){logger.error("Unable to find jmp session for ".concat(mediaType));return;}var requestedReceiveSlotIds=[];streamRequests.forEach(request=>{request.receiveSlots.forEach(slot=>{if(!slot.id){logger.error("Running stream request task, but ReceiveSlot ID is missing!");return;}requestedReceiveSlotIds.push(slot.id);});});jmpSession.sendRequests(streamRequests.map(sr=>sr._toJmpStreamRequest()));(_a=this.recvTransceivers.get(mediaType))===null||_a===void 0?void 0:_a.forEach(transceiver=>{if(requestedReceiveSlotIds.some(id=>compareStreamIds(id,transceiver.receiveSlot.id))){var relevantRequest=streamRequests.find(request=>request.receiveSlots.some(slot=>compareStreamIds(slot.id,transceiver.receiveSlot.id)));transceiver.handleRequested(relevantRequest);}else {transceiver.handleUnrequested();}});};if(((_a=this.dataChannel)===null||_a===void 0?void 0:_a.readyState)==='open'){task();}else {this.pendingJmpTasks.push(task);}}renewPeerConnection(userOptions){if(userOptions){this.options=Object.assign(Object.assign({},this.options),userOptions);}logger.info("Renewing multistream connection with options ".concat(JSON.stringify(this.options)));this.midPredictor.reset();this.initializePeerConnection();var mainSceneId=generateSceneId();var slidesSceneId=generateSceneId();this.sendTransceivers.forEach((transceiver,mediaType)=>{var _a;var mediaContent=getMediaContent(mediaType);var sceneId=mediaContent===MediaContent.Main?mainSceneId:slidesSceneId;var mid=this.midPredictor.getNextMid(mediaType);transceiver.replaceTransceiver(this.pc.addTransceiver(toMediaStreamTrackKind(mediaType),{direction:'sendrecv',sendEncodings:getMediaFamily(mediaType)===MediaFamily.Video?this.getVideoEncodingOptions(mediaContent):undefined}));transceiver.mid=mid;transceiver.csi=generateCsi(getMediaFamily(mediaType),sceneId);transceiver.resetSdpMunger();(_a=this.jmpSessions.get(mediaType))===null||_a===void 0?void 0:_a.close();this.createJmpSession(mediaType);});this.recvTransceivers.forEach((transceivers,mediaType)=>{transceivers.forEach(t=>{var mid=this.midPredictor.getNextMid(mediaType);t.replaceTransceiver(this.pc.addTransceiver(toMediaStreamTrackKind(mediaType),{direction:'recvonly'}));t.mid=mid;});});}getReceiveSlotById(id){return [...this.recvTransceivers.values()].flat().map(transceiver=>transceiver.receiveSlot).find(receiveSlot=>{var receiveSlotId=receiveSlot.id||{};return Object.keys(receiveSlotId).length===Object.keys(id).length&&Object.keys(receiveSlotId).every(key=>Object.prototype.hasOwnProperty.call(id,key)&&receiveSlotId[key]===id[key]);});}getStats(){return this.statsManager.getStats();}getTransceiverStats(){return __awaiter$1(this,void 0,void 0,function*(){return organizeTransceiverStats(this.sendTransceivers,this.recvTransceivers);});}preProcessStats(stats){return __awaiter$1(this,void 0,void 0,function*(){yield Promise.all([...this.sendTransceivers.values()].map(transceiver=>__awaiter$1(this,void 0,void 0,function*(){(yield transceiver.getStats()).forEach(senderStats=>{if(senderStats.type==='outbound-rtp'||senderStats.type==='media-source'){stats.set(senderStats.id,senderStats);}});})));yield Promise.all([...this.recvTransceivers.values()].map(transceivers=>__awaiter$1(this,void 0,void 0,function*(){yield Promise.all(transceivers.map(transceiver=>__awaiter$1(this,void 0,void 0,function*(){(yield transceiver.getStats()).forEach(receiverStats=>{if(receiverStats.type==='inbound-rtp'){stats.set(receiverStats.id,receiverStats);}});})));})));});}attachMetricsObserver(){this.forceStatsReport=rtcStats_1(this.pc.getUnderlyingRTCPeerConnection(),data=>this.metricsCallback(data),5000,stats=>this.preProcessStats(stats)).forceStatsReport;}forceRtcMetricsCallback(){var _a;return (_a=this.forceStatsReport)===null||_a===void 0?void 0:_a.call(this);}setMetricsCallback(callback){this.metricsCallback=callback;}setOveruseUpdateCallback(callback){this.overuseUpdateCallback=callback;}getCsiByMediaType(mediaType){var _a;return (_a=this.sendTransceivers.get(mediaType))===null||_a===void 0?void 0:_a.csi;}getAllCsis(){return {audioMain:this.getCsiByMediaType(MediaType.AudioMain),audioSlides:this.getCsiByMediaType(MediaType.AudioSlides),videoMain:this.getCsiByMediaType(MediaType.VideoMain),videoSlides:this.getCsiByMediaType(MediaType.VideoSlides)};}}class StreamRequest{constructor(policy,policySpecificInfo,receiveSlots,maxPayloadBitsPerSecond){var codecInfos=arguments.length>4&&arguments[4]!==undefined?arguments[4]:[];this.policy=policy;this.policySpecificInfo=policySpecificInfo;this.receiveSlots=receiveSlots;this.maxPayloadBitsPerSecond=maxPayloadBitsPerSecond;this.codecInfos=codecInfos;}_toJmpStreamRequest(){return new StreamRequest$1(this.policy,this.policySpecificInfo,this.receiveSlots.map(rs=>rs.id),this.maxPayloadBitsPerSecond,this.codecInfos);}}
|
|
4885
4885
|
|
|
4886
4886
|
var defaultLogger = {
|
|
4887
4887
|
info: function info() {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@webex/internal-media-core",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.5.0",
|
|
4
4
|
"files": [
|
|
5
5
|
"dist/cjs",
|
|
6
6
|
"dist/esm",
|
|
@@ -46,7 +46,7 @@
|
|
|
46
46
|
"dependencies": {
|
|
47
47
|
"@babel/runtime": "^7.18.9",
|
|
48
48
|
"@webex/ts-sdp": "1.6.0",
|
|
49
|
-
"@webex/web-client-media-engine": "3.
|
|
49
|
+
"@webex/web-client-media-engine": "3.17.0",
|
|
50
50
|
"events": "^3.3.0",
|
|
51
51
|
"typed-emitter": "^2.1.0",
|
|
52
52
|
"uuid": "^8.3.2",
|