/
star_rtc_video.min.js
1 lines (1 loc) · 30.3 KB
/
star_rtc_video.min.js
1
var sdpUtils=new SdpUtils;function extractVersion(e,t,i){var a=e.match(t);return a&&a.length>=i&&parseInt(a[i],10)}function detectBrowser(e){var t=e.navigator,i={browser:null,version:null};if(void 0===e||!e.navigator)return i.browser="Not a browser.",i;if(t.mozGetUserMedia)i.browser="firefox",i.version=extractVersion(t.userAgent,/Firefox\/(\d+)\./,1);else if(t.webkitGetUserMedia||!1===e.isSecureContext&&e.webkitRTCPeerConnection&&!e.RTCIceGatherer)i.browser="chrome",i.version=extractVersion(t.userAgent,/Chrom(e|ium)\/(\d+)\./,2);else if(t.mediaDevices&&t.userAgent.match(/Edge\/(\d+).(\d+)$/))i.browser="edge",i.version=extractVersion(t.userAgent,/Edge\/(\d+).(\d+)$/,2);else{if(!e.RTCPeerConnection||!t.userAgent.match(/AppleWebKit\/(\d+)\./))return i.browser="Not a supported browser.",i;i.browser="safari",i.version=extractVersion(t.userAgent,/AppleWebKit\/(\d+)\./,1),i.supportsUnifiedPlan=e.RTCRtpTransceiver&&"currentDirection"in e.RTCRtpTransceiver.prototype}return i}function ICEInfo(){this.iceUfrag="",this.icePwd="",this.iceOptions="",this.iceLite=!1}function FingerprintInfo(){this.algorithm="",this.value=""}function CodecInfo(){this.payload=0,this.codecName="",this.codecInterval="",this.rtcpFB=[],this.fmtp=[]}function SSRCInfo(){this.ssrc=0,this.cname="",this.mslabel="",this.label="",this.msid=""}function MediaDesc(){this.type="",this.protocol="UDP/TLS/RTP/SAVPF",this.iceInfo=new ICEInfo,this.fingerprintInfo=new FingerprintInfo,this.setup="",this.mid="",this.msid="",this.extmap={},this.commAbility="sendrecv",this.rtcpMux=!0,this.rtcpResize=!1,this.codecInfoMap={},this.codecInfoArray=[],this.ssrcInfoMap={},this.ssrcInfoArray=[],this.ssrcGroup=[],this.candidate=[]}function SdpDesc(){this.version=0,this.username="-",this.sessionID=0,this.sessionVersion=0,this.sessionName="-",this.sessionStartTime=0,this.sessionEndTime=0,this.iceInfo=new ICEInfo,this.fingerprintInfo=new FingerprintInfo,this.group=[],this.wms=[],this.mediaDesc=[]}function SdpUtils(){SdpUtils.prototype.clone=function e(t){if(null==t||"object"!=typeof t)return t;if(t instanceof Date)return(i=new Date).setTime(t.getTime()),i;if(t instanceof Array){for(var i=[],a=t.length,r=0;r<a;++r)i[r]=e(t[r]);return i}if(t instanceof Object){i={};for(var n in t)t.hasOwnProperty(n)&&(i[n]=e(t[n]));return i}throw new Error("Unable to copy obj! Its type isn't supported.")},SdpUtils.prototype.parseSdp=function(e){for(var t=new SdpDesc,i=e.split("\r\n"),a=!1,r=null,n=i.length,s=0;s<n;++s){switch(i[s].charAt(0)){case"v":t.version=parseInt(i[s].substring(2));break;case"o":var o=i[s].substring(2).split(" ");t.username=o[0],t.sessionID=o[1],t.sessionVersion=o[2];break;case"s":t.sessionName=i[s].substring(2);break;case"t":o=i[s].substring(2).split(" ");t.sessionStartTime=parseInt(o[0]),t.sessionEndTime=parseInt(o[1]);break;case"m":null!=r&&(t.mediaDesc.push(r),r=null);o=i[s].substring(2).split(" ");(r=new MediaDesc).type=o[0],r.protocol=o[2];for(var c=3;c<o.length;++c){(l=new CodecInfo).payload=parseInt(o[c]),r.codecInfoMap[l.payload]=l,r.codecInfoArray.push(l)}a=!0;break;case"a":if(a){if(-1!=i[s].indexOf("a=ice-ufrag:"))r.iceInfo.iceUfrag=i[s].substring(12);else if(-1!=i[s].indexOf("a=ice-pwd:"))r.iceInfo.icePwd=i[s].substring(10);else if(-1!=i[s].indexOf("a=ice-options:"))r.iceInfo.iceOptions=i[s].substring(14);else if(-1!=i[s].indexOf("a=ice-lite"))r.iceInfo.iceLite=!0;else if(-1!=i[s].indexOf("a=fingerprint:")){o=i[s].substring(14).split(" ");r.fingerprintInfo.algorithm=o[0],r.fingerprintInfo.value=o[1]}else if(-1!=i[s].indexOf("a=setup:"))r.setup=i[s].substring(8);else if(-1!=i[s].indexOf("a=mid:"))r.mid=i[s].substring(6);else if(-1!=i[s].indexOf("a=extmap:")){o=i[s].substring(9).split(" ");r.extmap[o[0]]=o[1]}else if(-1!=i[s].indexOf("a=rtcp-mux"))r.rtcpMux=!0;else if(-1!=i[s].indexOf("a=rtcp-rsize"))r.rtcpResize=!0;else if(-1!=i[s].indexOf("a=sendrecv")||-1!=i[s].indexOf("a=sendonly")||-1!=i[s].indexOf("a=recvonly")||-1!=i[s].indexOf("a=inactive"))r.commAbility=i[s].substring(2);else if(-1!=i[s].indexOf("a=rtpmap:")){o=i[s].substring(9).split(" ");var l=r.codecInfoMap[o[0]],d=o[1].indexOf("/");o[1].split("/");l.codecName=o[1].substring(0,d),l.codecInterval=o[1].substring(d+1)}else if(-1!=i[s].indexOf("a=rtcp-fb:")){d=i[s].indexOf(" ");var f=i[s].substring(10,d);o=i[s].substring(d+1);(l=r.codecInfoMap[f]).rtcpFB.push(o)}else if(-1!=i[s].indexOf("a=fmtp:")){o=i[s].substring(7).split(" ");(l=r.codecInfoMap[o[0]]).fmtp.push(o[1])}else if(-1!=i[s].indexOf("a=msid:")){var p=i[s].substring(7);r.msid=p}else if(-1!=i[s].indexOf("a=ssrc:")){d=i[s].indexOf(" ");var m=i[s].substring(7,d),u=(o=i[s].substring(d+1).split(":"),r.ssrcInfoMap[m]);u||((u=new SSRCInfo).ssrc=m,r.ssrcInfoArray.push(u)),"cname"==o[0]?u.cname=o[1]:"msid"==o[0]?u.msid=o[1]:"mslabel"==o[0]?u.mslabel=o[1]:"label"==o[0]&&(u.label=o[1]),r.ssrcInfoMap[m]=u}else if(-1!=i[s].indexOf("a=ssrc-group:")){var h=(o=i[s].substring(13).split(" ")).length;for(c=0;c<h;++c)r.ssrcGroup.push(o[c])}else if(-1!=i[s].indexOf("a=candidate:")){o=i[s].substring(12);r.candidate.push(o)}}else if(-1!=i[s].indexOf("group:BUNDLE"))for(var o=i[s].split(" "),c=1;c<o.length;++c)t.group.push(o[c]);else if(-1!=i[s].indexOf("msid-semantic:")){var o=i[s].split(" ");t.wms.push(o[o.length-1])}else if(-1!=i[s].indexOf("a=ice-ufrag:"))t.iceInfo.iceUfrag=i[s].substring(12);else if(-1!=i[s].indexOf("a=ice-pwd:"))t.iceInfo.icePwd=i[s].substring(10);else if(-1!=i[s].indexOf("a=ice-options:"))t.iceInfo.iceOptions=i[s].substring(14);else if(-1!=i[s].indexOf("a=ice-lite"))t.iceInfo.iceLite=!0;else if(-1!=i[s].indexOf("a=fingerprint:")){var o=i[s].substring(14).split(" ");t.fingerprintInfo.algorithm=o[0],t.fingerprintInfo.value=o[1]}}}return null!=r&&(t.mediaDesc.push(r),r=null),t},SdpUtils.prototype.genIceInfoSdp=function(e){var t="";return""!=e.iceUfrag&&(t+="a=ice-ufrag:"+e.iceUfrag+"\r\n"),""!=e.icePwd&&(t+="a=ice-pwd:"+e.icePwd+"\r\n"),e.iceLite?t+="a=ice-lite\r\n":""!=e.iceOptions&&(t+="a=ice-options:"+e.iceOptions+"\r\n"),t},SdpUtils.prototype.genFingerprintSdp=function(e){var t="";return""!=e.algorithm&&(t+="a=fingerprint:"+e.algorithm+" "+e.value+"\r\n"),t},SdpUtils.prototype.genCodecSdp=function(e){var t="";t+="a=rtpmap:"+e.payload+" "+e.codecName+"/"+e.codecInterval+"\r\n";for(var i=e.rtcpFB.length,a=0;a<i;++a)t+="a=rtcp-fb:"+e.payload+" "+e.rtcpFB[a]+"\r\n";var r=e.fmtp.length;for(a=0;a<r;++a)t+="a=fmtp:"+e.payload+" "+e.fmtp[a]+"\r\n";return t},SdpUtils.prototype.genSSRCSdp=function(e){var t="";return e.cname&&(t+="a=ssrc:"+e.ssrc+" cname:"+e.cname+"\r\n"),e.msid&&(t+="a=ssrc:"+e.ssrc+" msid:"+e.msid+"\r\n"),e.mslabel&&(t+="a=ssrc:"+e.ssrc+" mslabel:"+e.mslabel+"\r\n"),e.label&&(t+="a=ssrc:"+e.ssrc+" label:"+e.label+"\r\n"),t},SdpUtils.prototype.genMediaSdp=function(e){var t="";t+="m="+e.type+" 9 "+e.protocol;for(var i="",a=e.codecInfoArray.length,r=0;r<a;++r){var n=e.codecInfoArray[r];t+=" "+n.payload,i+=this.genCodecSdp(n)}t+="\r\n",t+="c=IN IP4 0.0.0.0\r\n";var s=e.candidate.length;for(r=0;r<s;++r)t+="a=candidate:"+e.candidate[r]+"\r\n";for(var o in 0<e.candidate.length&&(t+="a=end-of-candidates\r\n"),t+="a="+e.commAbility+"\r\n",t+="a=setup:"+e.setup+"\r\n",t+="a=mid:"+e.mid+"\r\n",e.msid&&(t+="a=msid:"+e.msid+"\r\n"),t+=this.genIceInfoSdp(e.iceInfo),t+=this.genFingerprintSdp(e.fingerprintInfo),1==e.rtcpMux&&(t+="a=rtcp-mux\r\n"),1==e.rtcpResize&&(t+="a=rtcp-rsize\r\n"),e.extmap)t+="a=extmap:"+o+" "+e.extmap[o]+"\r\n";t+=i,e.ssrcGroup.length&&(t+="a=ssrc-group:FID "+e.ssrcGroup.join(" ")+"\r\n");var c=e.ssrcInfoArray.length;for(r=0;r<c;++r)t+=this.genSSRCSdp(e.ssrcInfoArray[r]);return t},SdpUtils.prototype.genSdp=function(e){var t="";t+="v="+e.version+"\r\n",t+="o="+e.username+" "+e.sessionID+" "+e.sessionVersion+" IN IP4 127.0.0.1\r\n",t+="s="+e.sessionName+"\r\n",t+="t="+e.sessionStartTime+" "+e.sessionEndTime+"\r\n";for(var i="",a="",r=e.mediaDesc.length,n=0;n<r;++n)a+=" "+e.mediaDesc[n].mid,i+=this.genMediaSdp(e.mediaDesc[n]);return""!=a&&(t+="a=group:BUNDLE"+a+"\r\n"),0!=e.wms.length&&(t+="a=msid-semantic:WMS *\r\n"),t+=this.genIceInfoSdp(e.iceInfo),t+=this.genFingerprintSdp(e.fingerprintInfo),t+=i}}var StarWebRTC=function(e){var s=RTCPeerConnection,r=(navigator.mediaDevices.getUserMedia,RTCIceCandidate,RTCSessionDescription),o=detectBrowser(window),u=e||{iceServers:[],iceTransportPolicy:"all",bundlePolicy:"balanced",rtcpMuxPolicy:"require",iceCandidatePoolSize:0};null==u.sdpSemantics&&(u.sdpSemantics="unified-plan","chrome"==o.browser?o.version<72&&(u.sdpSemantics="plan-b"):"safari"==o.browser&&(o.supportsUnifiedPlan||(u.sdpSemantics="plan-b"))),null==u.iceControlRole&&(u.iceControlRole="controlled","firefox"==o.browser&&(u.iceControlRole="controlling"));function t(){this.events={}}function i(){this.localMediaStream=null,this.room="",this.fileData={},this.socket=null,this.me=null,this.peerConnections={},this.connections=[],this.numStreams=0,this.initializedStreams=0,this.dataChannels={},this.fileChannels={},this.receiveFiles={},this.localSmallMediaStream=null,this.localSmallMediaTrack=null,this.bigStreamId="",this.smallStreamId="",this.audioStreamId="",this.answerSDP="",this.offerSDP=null,this.starPeerConnection=null,this.tempPC=null,this.callback=null,this.streamAllSetCallback=null,this.serverIp="0.0.0.0",this.serverPort=80,this.streamInfos=[]}function a(){this.videoId="",this.streamId="",this.bigVideoSSRC=0,this.smallVideoSSRC=0,this.audioSSRC=0,this.streamObj=null,this.switchFlag=!1}t.prototype.on=function(e,t){this.events[e]=this.events[e]||[],this.events[e].push(t)},t.prototype.emit=function(e,t){var i,a,r=this.events[e],n=Array.prototype.slice.call(arguments,1);if(r)for(i=0,a=r.length;i<a;i++)r[i].apply(null,n)},(i.prototype=new t).destroy=function(){this.emit("_remove_peer")},i.prototype.resetStreamInfos=function(e){this.streamInfos=[];for(var t=0;t<7;++t){var i=new a;i.bigVideoSSRC=2312312300+t,i.smallVideoSSRC=2312311300+t,i.audioSSRC=2312310300+t,i.streamId=this.createRandomString(g,36),i.cname=this.createRandomString(g,16),this.streamInfos.push(i)}},i.prototype.init=function(){this.resetStreamInfos();var m=this;this.on("_peers",function(e){m.connections=e.connections,m.me=e.you,m.emit("get_peers",m.connections)}),this.on("_remove_peer",function(e){m.starPeerConnection&&(m.closePeerConnection(m.starPeerConnection),delete m.starPeerConnection,m.starPeerConnection=null),m.tempPC&&(m.closePeerConnection(m.tempPC),delete m.tempPC,m.tempPC=null),null!=m.localMediaStream&&(m.localMediaStream.getTracks().forEach(function(e){e.stop()}),m.localMediaStream=null),null!=m.localSmallMediaTrack&&(m.localSmallMediaTrack.stop(),m.localSmallMediaTrack=null),null!=m.localSmallMediaStream&&(m.localSmallMediaStream.getTracks().forEach(function(e){e.stop()}),m.localSmallMediaStream=null),m.bigStreamId="",m.smallStreamId="",m.audioStreamId="",m.answerSDP="",m.offerSDP=null,m.callback=null,m.serverIp="0.0.0.0",m.serverPort=80,m.resetStreamInfos()}),this.on("_offer",function(e){m.receiveOffer(e.socketId,e.sdp),m.emit("get_offer",e)}),this.on("_answer",function(e){var t=e.sessionDesc;t.type="answer",t.sdp=t.sdp.replace(/actpass/g,"active"),m.receiveAnswer(e.socketId,t),m.emit("get_answer",e)}),this.on("_webrtc_apply_ok",function(e,t,i,a){if(null==m.offerSDP)m.offerSDP=m.createDefaultSdpObj(i,a);else{for(var r=!1,n=!1,s=m.offerSDP.mediaDesc.length,o=0;o<s;++o){var c=m.offerSDP.mediaDesc[o];"audio"==c.type?n=!0:"video"==c.type&&(r=!0)}n||m.offerSDP.mediaDesc.push(m.createDefaultAudioMediaSdpObj(a)),r||m.offerSDP.mediaDesc.push(m.createDefaultVideoMediaSdpObj(i))}m.streamAllSetCallback=t;var l="504457478 1 udp 2013266431 "+m.serverIp+" "+m.serverPort+" typ host generation 0 ufrag Nud3 network-id 1",d=sdpUtils.clone(m.offerSDP);"controlled"==u.iceControlRole?"plan-b"==u.sdpSemantics?m.createAnswerFromOfferPlanB(d,"Nud3","SKuOCnwS3ScdasO5hD2aheqb",e,m.streamInfos,[l]):m.createAnswerFromOfferUnifyPlan(d,"Nud3","SKuOCnwS3ScdasO5hD2aheqb",e,m.streamInfos,[l]):"plan-b"==u.sdpSemantics?m.createAnswerFromOfferPlanB(d,"Nud3","SKuOCnwS3ScdasO5hD2aheqb",e,m.streamInfos,[l]):m.createAnswerFromOfferUnifyPlan2(d,"Nud3","SKuOCnwS3ScdasO5hD2aheqb",e,m.streamInfos,[l]);var f=sdpUtils.genSdp(d),p={};"controlled"==u.iceControlRole?p.type="offer":p.type="answer",p.sdp=f,m.starPeerConnection.setRemoteDescription(p).then(function(){"controlled"==u.iceControlRole?m.starPeerConnection.createAnswer(function(e){var t=sdpUtils.parseSdp(e.sdp);m.filterSdpObj(t),m.replaceData(t);var i=sdpUtils.genSdp(t);e.sdp=i,m.starPeerConnection.setLocalDescription(e).then(function(){m.callback({type:"applyAnswer",status:"success"})},function(e){console.error(e),m.callback({type:"applyAnswer",status:"failed"})})},function(e){console.error(e),m.callback({type:"applyAnswer",status:"failed"})}):m.callback({type:"applyAnswer",status:"success"})},function(e){console.error(e),m.callback({type:"applyAnswer",status:"failed"})})}),this.on("ready",function(e,t){var i=t||!1;m.callback=e,m.createPeerConnections(),m.addStreams(i),m.sendOffers()})},i.prototype.createScreenCaptureStream=function(e,t,i){var a=this;(function(e){return navigator.getDisplayMedia?navigator.getDisplayMedia({video:!0,audio:e.audio}):navigator.mediaDevices.getDisplayMedia?navigator.mediaDevices.getDisplayMedia({video:!0,audio:e.audio}):navigator.mediaDevices.getUserMedia({video:{mediaSource:"screen"},audio:e.audio})})(e).then(function(e){a.localMediaStream=e,a.localMediaStream&&(a.localMediaStream.addEventListener("inactive",function(e){console.warn("Capture stream inactive"),i(e)}),a.initializedStreams++),t("success",e)}).catch(function(e){t("failed",e)})},i.prototype.publishStream=function(t){this.starPeerConnection&&this.starPeerConnection.getSenders().forEach(function(e){e.track&&("video"==e.track.kind&&null!=t.video?e.track.enabled=t.video:"audio"==e.track.kind&&null!=t.audio&&(e.track.enabled=t.audio))})},i.prototype.createStream=function(n,t,i){var a=this;n.video||n.audio?navigator.mediaDevices.getUserMedia?(this.numStreams++,navigator.mediaDevices.enumerateDevices().then(function(e){for(var t=!1,i=!1,a=0;a!==e.length;++a){var r=e[a];"audioinput"===r.kind?t=!0:"audiooutput"===r.kind||("videoinput"===r.kind?i=!0:console.info("Some other kind of source/device: ",r))}return i||(n.video=!1),t||(n.audio=!1),n.video||n.audio?navigator.mediaDevices.getUserMedia(n):Promise.resolve(null)}).then(function(e){a.localMediaStream=e,a.localMediaStream&&(a.localMediaStream.addEventListener("inactive",function(e){console.warn("Capture stream inactive"),i(e)}),a.initializedStreams++),t("success",e)}).catch(function(e){t("failed",e)})):t("failed",new Error("WebRTC is not yet supported in this browser.")):t("success",null)},i.prototype.addStreams=function(i){if(null!=this.localMediaStream){var a=this,r=!1,n=null;window.RTCPeerConnection.prototype.addTrack&&65<=o.version&&(r=!0),r||i||(n=this.localMediaStream.clone()).getTracks().forEach(function(e){e.stop(),n.removeTrack(e)}),this.localMediaStream.getTracks().forEach(function(e){if(r&&(a.starPeerConnection.addTrack(e,a.localMediaStream),"controlled"==u.iceControlRole&&a.tempPC.addTrack(e,a.localMediaStream)),"video"==e.kind){if(a.bigStreamId=e.id,!i){var t=e.clone();a.localSmallMediaTrack=t,a.smallStreamId=t.id,MediaStreamTrack.prototype.applyConstraints&&("safari"!=o.browser?t.applyConstraints({width:{ideal:120},height:{ideal:90},facingMode:{ideal:["user"]}}):t.applyConstraints({width:{ideal:640},height:{ideal:480},facingMode:{ideal:["user"]}})),r?(a.starPeerConnection.addTrack(t,a.localMediaStream),"controlled"==u.iceControlRole&&a.tempPC.addTrack(t,a.localMediaStream)):n.addTrack(t)}}else"audio"==e.kind&&(a.audioStreamId=e.id)}),r||(a.starPeerConnection.addStream(a.localMediaStream),"controlled"==u.iceControlRole&&a.tempPC.addStream(a.localMediaStream),null!=n&&(a.starPeerConnection.addStream(n),"controlled"==u.iceControlRole&&a.tempPC.addStream(n))),a.localSmallMediaStream=n}},i.prototype.attachStream=function(e,t){document.getElementById(t).srcObject=e},i.prototype.switchStream=function(t){var i=[];t.getVideoTracks().forEach(function(e){i.push(e),t.removeTrack(e)});for(var e=i.length-1;0<=e;e--)t.addTrack(i[e])},i.prototype.switchStreamInfo=function(e){e.switchFlag=!e.switchFlag,this.switchStream(e.streamObj)},i.prototype.resetStreamInfo=function(e){e.switchFlag&&this.switchStreamInfo(e)},i.prototype.switchStreams=function(){for(var e=this.streamInfos.length,t=0;t<e;++t)this.switchStreamInfo(this.streamInfos[t])},i.prototype.getStreamByIndex=function(e){return this.streamInfos[e]},i.prototype.getStreamInfos=function(e){return this.streamInfos},i.prototype.onAddStream=function(e){for(var t=!0,i=this.streamInfos.length,a=0;a<i;++a){var r=this.streamInfos[a];r.streamId==e.id&&(r.streamObj=e),null==r.streamObj&&(t=!1)}t&&null!=this.streamAllSetCallback&&"connected"==this.starPeerConnection.iceConnectionState&&(this.streamAllSetCallback(),this.streamAllSetCallback=null)},i.prototype.setServerInfo=function(e){this.serverIp=e.serverIp,this.serverPort=e.serverPort};var g=["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","0","1","2","3","4","5","6","7","8","9"],c=["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","0","1","2","3","4","5","6","7","8","9","+","/"];return i.prototype.createRandomString=function(e,t){for(var i="",a=0;a<t;++a)i+=e[Math.floor(Math.random()*e.length)];return i},i.prototype.uuid=function(){function e(){return(65536*(1+Math.random())|0).toString(16).substring(1)}return e()+e()+"-"+e()+"-"+e()+"-"+e()+"-"+e()+e()+e()},i.prototype.replaceData=function(e){""==e.iceInfo.iceUfrag&&""!=this.sdpData.iceUfrag&&(e.iceInfo.iceUfrag=this.sdpData.iceUfrag,e.iceInfo.icePwd=this.sdpData.icePwd);for(var t=e.mediaDesc.length,i=0;i<t;++i){var a=e.mediaDesc[i];if(""!=this.sdpData.iceUfrag&&(a.iceInfo.iceUfrag=this.sdpData.iceUfrag,a.iceInfo.icePwd=this.sdpData.icePwd),"audio"==a.type){if(a.msid&&""!=a.msid){if(""!=this.audioStreamId&&-1!=a.msid.indexOf(this.audioStreamId)){s=a.ssrcInfoArray[0];delete a.ssrcInfoMap[s.ssrc],s.ssrc=this.sdpData.audioSSRC,a.ssrcInfoMap[s.ssrc]=s}}else if(""!=this.audioStreamId)for(var r=a.ssrcInfoArray.length,n=0;n<r;++n){-1!=(s=a.ssrcInfoArray[n]).msid.indexOf(this.audioStreamId)&&(delete a.ssrcInfoMap[s.ssrc],s.ssrc=this.sdpData.audioSSRC,a.ssrcInfoMap[s.ssrc]=s)}}else if("video"==a.type)if(a.msid&&""!=a.msid){if(""!=this.smallStreamId&&-1!=a.msid.indexOf(this.smallStreamId)){s=a.ssrcInfoArray[0];delete a.ssrcInfoMap[s.ssrc],s.ssrc=this.sdpData.smallVideoSSRC,a.ssrcInfoMap[s.ssrc]=s}else if(""!=this.bigStreamId&&-1!=a.msid.indexOf(this.bigStreamId)){s=a.ssrcInfoArray[0];delete a.ssrcInfoMap[s.ssrc],s.ssrc=this.sdpData.bigVideoSSRC,a.ssrcInfoMap[s.ssrc]=s}}else for(r=a.ssrcInfoArray.length,n=0;n<r;++n){var s=a.ssrcInfoArray[n];""!=this.smallStreamId&&-1!=s.msid.indexOf(this.smallStreamId)?(delete a.ssrcInfoMap[s.ssrc],s.ssrc=this.sdpData.smallVideoSSRC,a.ssrcInfoMap[s.ssrc]=s):""!=this.bigStreamId&&-1!=s.msid.indexOf(this.bigStreamId)&&(delete a.ssrcInfoMap[s.ssrc],s.ssrc=this.sdpData.bigVideoSSRC,a.ssrcInfoMap[s.ssrc]=s)}}},i.prototype.filterSdpObj=function(e){for(var t=e.mediaDesc.length,i=0;i<t;++i){var a=e.mediaDesc[i];a.extmap={},a.ssrcGroup=[],a.codecInfoMap={};for(var r=[],n=a.codecInfoArray.length,s=0;s<n;++s){var o=a.codecInfoArray[s];"audio"==a.type?"opus"==o.codecName&&(r.push(o),a.codecInfoMap[o.payload]=o):"video"==a.type&&"H264"==o.codecName&&0==r.length&&(r.push(o),a.codecInfoMap[o.payload]=o)}a.codecInfoArray=r;var c={},l=[],d={},f=a.ssrcInfoArray.length;for(s=0;s<f;++s){var p=a.ssrcInfoArray[s];null==d[p.label]&&(d[p.label]=1,c[p.ssrc]=p,l.push(p))}a.ssrcInfoArray=l,a.ssrcInfoMap=c}},i.prototype.getSdpData=function(e,t){var i={audioSSRC:"",smallVideoSSRC:"",bigVideoSSRC:"",audioCodec:"0",videoCodec:"0",iceUfrag:"",icePwd:""};i.iceUfrag=e.iceInfo.iceUfrag,i.icePwd=e.iceInfo.icePwd;for(var a=e.mediaDesc.length,r=0;r<a;++r){var n=e.mediaDesc[r];if(""==i.iceUfrag&&(i.iceUfrag=n.iceInfo.iceUfrag,i.icePwd=n.iceInfo.icePwd),"recvonly"!=n.commAbility)if("audio"==n.type){for(var s in n.ssrcInfoMap){var o=n.ssrcInfoMap[s];i.audioSSRC=o.ssrc}i.audioCodec=n.codecInfoArray[0].payload}else if("video"==n.type){for(var s in n.ssrcInfoMap){o=n.ssrcInfoMap[s];""!=t.bigStreamId&&(0==o.label.length?-1!=n.msid.indexOf(t.bigStreamId)&&(i.bigVideoSSRC=o.ssrc):-1!=o.label.indexOf(t.bigStreamId)&&(i.bigVideoSSRC=o.ssrc)),""!=t.smallStreamId&&(0==o.label.length?-1!=n.msid.indexOf(t.smallStreamId)&&(i.smallVideoSSRC=o.ssrc):-1!=o.label.indexOf(t.smallStreamId)&&(i.smallVideoSSRC=o.ssrc))}""==i.bigVideoSSRC&&""!=t.bigStreamId?0!=n.ssrcInfoArray.length&&(i.bigVideoSSRC=n.ssrcInfoArray[0].ssrc):""==i.smallVideoSSRC&&""!=t.smallStreamId&&0!=n.ssrcInfoArray.length&&(i.smallVideoSSRC=n.ssrcInfoArray[0].ssrc),i.videoCodec=n.codecInfoArray[0].payload}}return i},i.prototype.createDefaultVideoMediaSdpObj=function(e){e=e||102;var t=new MediaDesc;t.mid="video",t.type="video",t.rtcpResize=!0,t.fingerprintInfo.algorithm="sha-256",t.iceInfo.iceOptions="trickle";var i=new CodecInfo;return i.payload=e,i.codecName="H264",i.codecInterval="90000",i.fmtp=["level-asymmetry-allowed=1;packetization-mode=1;profile-level-id=42001f"],i.rtcpFB=["goog-remb","transport-cc","ccm fir","nack","nack pli"],t.codecInfoArray=[i],t.codecInfoMap[i.payload]=i,t},i.prototype.createDefaultAudioMediaSdpObj=function(e){e=e||111;var t=new MediaDesc;t.mid="audio",t.type="audio",t.fingerprintInfo.algorithm="sha-256",t.iceInfo.iceOptions="trickle";var i=new CodecInfo;return i.payload=e,i.codecName="opus",i.codecInterval="48000/2",i.fmtp=["minptime=10;useinbandfec=1"],i.rtcpFB=["transport-cc"],t.codecInfoArray=[i],t.codecInfoMap[i.payload]=i,t},i.prototype.createDefaultSdpObj=function(e,t){var i=new SdpDesc;return i.mediaDesc=[this.createDefaultAudioMediaSdpObj(t),this.createDefaultVideoMediaSdpObj(e)],i},i.prototype.createAnswerFromOfferUnifyPlan=function(e,t,i,a,r,n){""!=e.iceInfo.iceUfrag&&(e.iceInfo.iceUfrag=t),""!=e.iceInfo.icePwd&&(e.iceInfo.icePwd=i),e.iceInfo.iceLite=!0,""!=e.fingerprintInfo.algorithm&&(e.fingerprintInfo.value=a);for(var s=!1,o=!1,c=[],l=e.mediaDesc.length,d=0;d<l;++d){var f=e.mediaDesc[d];if(f.setup="actpass",f.iceInfo.iceUfrag=t,f.iceInfo.icePwd=i,f.iceInfo.iceLite=!0,""!=f.fingerprintInfo.algorithm&&(f.fingerprintInfo.value=a),f.candidate=n,f.ssrcInfoMap={},f.ssrcInfoArray=[],f.ssrcGroup=[],f.msid="","audio"!=f.type||s){if("video"==f.type&&!o){o=!0;for(u=r.length,h=0;h<u;++h){var p,m;if(0!=r[h].smallVideoSSRC)(p=sdpUtils.clone(f)).mid=f.type+"_s"+d+"_"+h,(m={}).ssrc=r[h].smallVideoSSRC,m.cname=r[h].cname,m.mslabel=r[h].streamId,m.label=this.uuid(),m.msid=m.mslabel+" "+m.label,p.ssrcInfoMap[m.ssrc]=m,p.ssrcInfoArray.push(m),p.msid=m.mslabel+" "+m.label,c.push(p);if(0!=r[h].bigVideoSSRC)(p=sdpUtils.clone(f)).mid=f.type+"_b"+d+"_"+h,(m={}).ssrc=r[h].bigVideoSSRC,m.cname=r[h].cname,m.mslabel=r[h].streamId,m.label=this.uuid(),m.msid=m.mslabel+" "+m.label,p.ssrcInfoMap[m.ssrc]=m,p.msid=m.mslabel+" "+m.label,p.ssrcInfoArray.push(m),c.push(p)}}}else{s=!0;for(var u=r.length,h=0;h<u;++h){var S=sdpUtils.clone(f);S.mid=f.type+"_"+d+"_"+h;var g={};g.ssrc=r[h].audioSSRC,g.cname=r[h].cname,g.mslabel=r[h].streamId,g.label=this.uuid(),g.msid=g.mslabel+" "+g.label,S.ssrcInfoMap[g.ssrc]=g,S.ssrcInfoArray.push(g),S.msid=g.mslabel+" "+g.label,c.push(S)}}}e.mediaDesc=c},i.prototype.createAnswerFromOfferUnifyPlan2=function(e,t,i,a,r,n){""!=e.iceInfo.iceUfrag&&(e.iceInfo.iceUfrag=t),""!=e.iceInfo.icePwd&&(e.iceInfo.icePwd=i),e.iceInfo.iceLite=!0,""!=e.fingerprintInfo.algorithm&&(e.fingerprintInfo.value=a);for(var s=0,o=0,c=0,l=e.mediaDesc.length,d=0;d<l;++d){var f=e.mediaDesc[d];if("actpass"==f.setup?f.setup="passive":f.setup="actpass",f.iceInfo.iceUfrag=t,f.iceInfo.icePwd=i,f.iceInfo.iceLite=!0,""!=f.fingerprintInfo.algorithm&&(f.fingerprintInfo.value=a),f.candidate=n,f.ssrcInfoMap={},f.ssrcInfoArray=[],f.ssrcGroup=[],f.msid="","recvonly"==f.commAbility){var p,m={};"audio"==f.type?(p=s,m.ssrc=r[p].audioSSRC,s++):"video"==f.type&&(o<c?(p=o,m.ssrc=r[p].bigVideoSSRC,o++):(p=c,m.ssrc=r[p].smallVideoSSRC,c++)),m.cname=r[p].cname,m.mslabel=r[p].streamId,m.label=this.uuid(),m.msid=m.mslabel+" "+m.label,f.ssrcInfoMap[m.ssrc]=m,f.ssrcInfoArray.push(m),f.msid=m.mslabel+" "+m.label,f.commAbility="sendonly"}}},i.prototype.createAnswerFromOfferPlanB=function(e,t,i,a,r,n){var s=this.createRandomString(g,16);""!=e.iceInfo.iceUfrag&&(e.iceInfo.iceUfrag=t),""!=e.iceInfo.icePwd&&(e.iceInfo.icePwd=i),e.iceInfo.iceLite=!0,""!=e.fingerprintInfo.algorithm&&(e.fingerprintInfo.value=a);for(var o=[],c=!1,l=!1,d=e.mediaDesc.length,f=0;f<d;++f){var p=e.mediaDesc[f];if(p.setup="actpass",p.iceInfo.iceUfrag=t,p.iceInfo.icePwd=i,p.iceInfo.iceLite=!0,""!=p.fingerprintInfo.algorithm&&(p.fingerprintInfo.value=a),p.candidate=n,p.ssrcInfoMap={},p.ssrcInfoArray=[],p.ssrcGroup=[],p.msid="","audio"!=p.type||c){if("video"==p.type&&!l){l=!0;for(u=r.length,h=0;h<u;++h){var m;if(0!=r[h].smallVideoSSRC)(m={}).ssrc=r[h].smallVideoSSRC,m.cname=s,m.mslabel=r[h].streamId,m.label=this.uuid(),m.msid=m.mslabel+" "+m.label,p.ssrcInfoMap[m.ssrc]=m,p.ssrcInfoArray.push(m);if(0!=r[h].bigVideoSSRC)(m={}).ssrc=r[h].bigVideoSSRC,m.cname=s,m.mslabel=r[h].streamId,m.label=this.uuid(),m.msid=m.mslabel+" "+m.label,p.ssrcInfoMap[m.ssrc]=m,p.ssrcInfoArray.push(m)}o.push(p)}}else{c=!0;for(var u=r.length,h=0;h<u;++h){var S={};S.ssrc=r[h].audioSSRC,S.cname=s,S.mslabel=r[h].streamId,S.label=this.uuid(),S.msid=S.mslabel+" "+S.label,p.ssrcInfoMap[S.ssrc]=S,p.ssrcInfoArray.push(S)}o.push(p)}}e.mediaDesc=o},i.prototype.addTransceiverForOffer=function(e,t){for(var i=t.length,a=0;a<i;++a)e.addTransceiver("audio",{direction:"recvonly"}),e.addTransceiver("video",{direction:"recvonly"}),e.addTransceiver("video",{direction:"recvonly"})},i.prototype.sendOffers=function(){if(null==this.localMediaStream&&"controlled"==u.iceControlRole)return this.sdpData={audioSSRC:"",smallVideoSSRC:"",bigVideoSSRC:"",audioCodec:"0",videoCodec:"0",iceUfrag:this.createRandomString(c,4),icePwd:this.createRandomString(c,24)},void this.callback({type:"createOffer",status:"success",audioSSRC:this.sdpData.audioSSRC,smallVideoSSRC:this.sdpData.smallVideoSSRC,bigVideoSSRC:this.sdpData.bigVideoSSRC,videoCodec:this.sdpData.videoCodec,audioCodec:this.sdpData.audioCodec,iceUfrag:this.sdpData.iceUfrag,icePwd:this.sdpData.icePwd});var e,r,n,s=this;"controlled"==u.iceControlRole?e=this.tempPC:(e=this.starPeerConnection,"unified-plan"==u.sdpSemantics&&this.addTransceiverForOffer(e,this.streamInfos)),e.createOffer((r=e,n=this.callback,function(e){var t=sdpUtils.parseSdp(e.sdp);s.filterSdpObj(t);var i=sdpUtils.genSdp(t);s.offerSDP=t,e.sdp=i;var a=s.getSdpData(t,{bigStreamId:s.bigStreamId,smallStreamId:s.smallStreamId});s.sdpData=a,"controlled"==u.iceControlRole?(s.tempPC.close(),delete s.tempPC,s.tempPC=null,n({type:"createOffer",status:"success",audioSSRC:a.audioSSRC,smallVideoSSRC:a.smallVideoSSRC,bigVideoSSRC:a.bigVideoSSRC,videoCodec:a.videoCodec,audioCodec:a.audioCodec,iceUfrag:a.iceUfrag,icePwd:a.icePwd})):r.setLocalDescription(e).then(function(){n({type:"createOffer",status:"success",audioSSRC:a.audioSSRC,smallVideoSSRC:a.smallVideoSSRC,bigVideoSSRC:a.bigVideoSSRC,videoCodec:a.videoCodec,audioCodec:a.audioCodec,iceUfrag:a.iceUfrag,icePwd:a.icePwd})},function(e){n({type:"createOffer",status:"failed",audioSSRC:a.audioSSRC,smallVideoSSRC:a.smallVideoSSRC,bigVideoSSRC:a.bigVideoSSRC,videoCodec:a.videoCodec,audioCodec:a.audioCodec,iceUfrag:a.iceUfrag,icePwd:a.icePwd})})}),function(e){console.error(e),s.callback({type:"createOffer",status:"failed"})})},i.prototype.receiveOffer=function(e,t){this.starPeerConnection;this.sendAnswer(e,t)},i.prototype.sendAnswer=function(t,e){var i=this.starPeerConnection,a=this;i.setRemoteDescription(new r(e)),i.createAnswer(function(e){i.setLocalDescription(e),a.socket.send(JSON.stringify({eventName:"__answer",data:{socketId:t,sdp:e}}))},function(e){console.error(e)})},i.prototype.receiveAnswer=function(e,t){var i=this.starPeerConnection,a=this;i.setRemoteDescription(new r(t)).then(function(){a.callback({type:"applyAnswer",status:"success"})},function(e){a.callback({type:"applyAnswer",status:"failed"})})},i.prototype.createPeerConnections=function(){this.createPeerConnection("xuaisi")},i.prototype.createPeerConnection=function(e){var r=this,n=new s(u);return this.starPeerConnection&&(this.closePeerConnection(this.starPeerConnection),delete this.starPeerConnection,this.starPeerConnection=null),this.tempPC&&(this.closePeerConnection(this.tempPC),delete this.tempPC,this.tempPC=null),this.starPeerConnection=n,null!=this.localMediaStream&&"controlled"==u.iceControlRole&&(this.tempPC=new s(u)),n.onicecandidate=function(e){},n.oniceconnectionstatechange=function(e){if(console.log("oniceconnectionstatechange:"+n.iceConnectionState),"connected"===n.iceConnectionState&&null!=r.streamAllSetCallback){for(var t=!0,i=r.streamInfos.length,a=0;a<i;++a){null==r.streamInfos[a].streamObj&&(t=!1)}t&&(r.streamAllSetCallback(),r.streamAllSetCallback=null)}},n.onconnectionstatechange=function(e){if(console.log("onconnectionstatechange:"+n.connectionState),"connected"===n.connectionState&&null!=r.streamAllSetCallback){for(var t=!0,i=r.streamInfos.length,a=0;a<i;++a){null==r.streamInfos[a].streamObj&&(t=!1)}t&&(r.streamAllSetCallback(),r.streamAllSetCallback=null)}},n.onopen=function(){console.log("pc onopen")},n.onaddstream=function(e){r.onAddStream(e.stream)},n.ondatachannel=function(e){},n},i.prototype.closePeerConnection=function(e){e&&e.close()},i.prototype.broadcast=function(e){var t;for(t in this.dataChannels)this.sendMessage(e,t)},i.prototype.sendMessage=function(e,t){"open"===this.dataChannels[t].readyState.toLowerCase()&&this.dataChannels[t].send(JSON.stringify({type:"__msg",data:e}))},i.prototype.addDataChannels=function(){var e;for(e in this.peerConnections)this.createDataChannel(e)},i.prototype.createDataChannel=function(t,e){var i,a;i=this.peerConnections[t],t||this.emit("data_channel_create_error",t,new Error("attempt to create data channel without socket id")),i instanceof s||this.emit("data_channel_create_error",t,new Error("attempt to create data channel without peerConnection"));try{a=i.createDataChannel(e)}catch(e){this.emit("data_channel_create_error",t,e)}return this.addDataChannel(t,a)},i.prototype.addDataChannel=function(i,a){var r=this;return a.onopen=function(){r.emit("data_channel_opened",a,i)},a.onclose=function(e){delete r.dataChannels[i],r.emit("data_channel_closed",a,i)},a.onmessage=function(e){var t;"__file"===(t=JSON.parse(e.data)).type?r.parseFilePacket(t,i):r.emit("data_channel_message",a,i,t.data)},a.onerror=function(e){r.emit("data_channel_error",a,i,e)},this.dataChannels[i]=a},new i};