1 Star 0 Fork 199

a75929 / starrtc-web

forked from starRTC / starrtc-web 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
star_rtc_video.min.js 16.55 KB
一键复制 编辑 原始数据 按行查看 历史
elesos.com 提交于 2018-09-14 19:17 . init
var sdpUtils=new SdpUtils;function SdpUtils(){function e(){this.iceUfrag="",this.icePwd="",this.iceOptions=""}function t(){this.algorithm="",this.value=""}function p(){this.playload=0,this.codecName="",this.codecInterval="",this.rtcpFB=[],this.fmtp=[]}function u(){this.ssrc=0,this.cname="",this.mslabel="",this.label=""}function h(){this.type="",this.protocol="",this.iceInfo=new e,this.fingerprintInfo=new t,this.setup="",this.mid="",this.extmap={},this.commAbility="",this.rtcpMux=!0,this.rtcpResize=!1,this.codecInfoMap={},this.codecInfoArray=[],this.ssrcInfoMap={},this.ssrcInfoArray=[],this.ssrcGroup=[],this.candidate=[]}function m(){this.version=0,this.username="-",this.sessionID=0,this.sessionVersion=0,this.sessionName="-",this.sessionStartTime=0,this.sessionEndTime=0,this.iceInfo=new e,this.fingerprintInfo=new t,this.group=[],this.wms=[],this.mediaDesc=[]}SdpUtils.prototype.clone=function e(t){if(null==t||"object"!=typeof t)return t;if(t instanceof Date)return(n=new Date).setTime(t.getTime()),n;if(t instanceof Array){for(var n=[],i=t.length,s=0;s<i;++s)n[s]=e(t[s]);return n}if(t instanceof Object){n={};for(var r in t)t.hasOwnProperty(r)&&(n[r]=e(t[r]));return n}throw new Error("Unable to copy obj! Its type isn't supported.")},SdpUtils.prototype.parseSdp=function(e){var t=new m,n=e.split("\r\n"),i=!1,s=null;for(var r in n){switch(n[r].charAt(0)){case"v":t.version=parseInt(n[r].substring(2));break;case"o":var a=n[r].substring(2).split(" ");t.username=a[0],t.sessionID=a[1],t.sessionVersion=a[2];break;case"s":t.sessionName=n[r].substring(2);break;case"t":a=n[r].substring(2).split(" ");t.sessionStartTime=parseInt(a[0]),t.sessionEndTime=parseInt(a[1]);break;case"m":null!=s&&(t.mediaDesc.push(s),s=null);a=n[r].substring(2).split(" ");(s=new h).type=a[0],s.protocol=a[2];for(r=3;r<a.length;++r){(o=new p).playload=parseInt(a[r]),s.codecInfoMap[o.playload]=o,s.codecInfoArray.push(o)}i=!0;break;case"a":if(i){if(-1!=n[r].indexOf("a=ice-ufrag:"))s.iceInfo.iceUfrag=n[r].substring(12);else if(-1!=n[r].indexOf("a=ice-pwd:"))s.iceInfo.icePwd=n[r].substring(10);else if(-1!=n[r].indexOf("a=ice-options:"))s.iceInfo.iceOptions=n[r].substring(14);else if(-1!=n[r].indexOf("a=fingerprint:")){a=n[r].substring(14).split(" ");s.fingerprintInfo.algorithm=a[0],s.fingerprintInfo.value=a[1]}else if(-1!=n[r].indexOf("a=setup:"))s.setup=n[r].substring(8);else if(-1!=n[r].indexOf("a=mid:"))s.mid=n[r].substring(6);else if(-1!=n[r].indexOf("a=extmap:")){a=n[r].substring(9).split(" ");s.extmap[a[0]]=a[1]}else if(-1!=n[r].indexOf("a=rtcp-mux"))s.rtcpMux=!0;else if(-1!=n[r].indexOf("a=rtcp-rsize"))s.rtcpResize=!0;else if(-1!=n[r].indexOf("a=sendrecv")||-1!=n[r].indexOf("a=sendonly")||-1!=n[r].indexOf("a=recvonly")||-1!=n[r].indexOf("a=inactive"))s.commAbility=n[r].substring(2);else if(-1!=n[r].indexOf("a=rtpmap:")){a=n[r].substring(9).split(" ");var o=s.codecInfoMap[a[0]],c=a[1].indexOf("/");a[1].split("/");o.codecName=a[1].substring(0,c),o.codecInterval=a[1].substring(c+1)}else if(-1!=n[r].indexOf("a=rtcp-fb:")){c=n[r].indexOf(" ");var l=n[r].substring(10,c);a=n[r].substring(c+1);(o=s.codecInfoMap[l]).rtcpFB.push(a)}else if(-1!=n[r].indexOf("a=fmtp:")){a=n[r].substring(7).split(" ");(o=s.codecInfoMap[a[0]]).fmtp.push(a[1])}else if(-1!=n[r].indexOf("a=ssrc:")){c=n[r].indexOf(" ");var d=n[r].substring(7,c),f=(a=n[r].substring(c+1).split(":"),s.ssrcInfoMap[d]);f||((f=new u).ssrc=d,s.ssrcInfoArray.push(f)),"cname"==a[0]?f.cname=a[1]:"msid"==a[0]?f.msid=a[1]:"mslabel"==a[0]?f.mslabel=a[1]:"label"==a[0]&&(f.label=a[1]),s.ssrcInfoMap[d]=f}else if(-1!=n[r].indexOf("a=ssrc-group:")){a=n[r].substring(13).split(" ");for(var r in a)s.ssrcGroup.push(a[r])}else if(-1!=n[r].indexOf("a=candidate:")){a=n[r].substring(12);s.candidate.push(a)}}else if(-1!=n[r].indexOf("group:BUNDLE")){var a=n[r].split(" ");for(r=1;r<a.length;++r)t.group.push(a[r])}else if(-1!=n[r].indexOf("msid-semantic:")){var a=n[r].split(" ");t.wms.push(a[a.length-1])}else if(-1!=n[r].indexOf("a=ice-ufrag:"))t.iceInfo.iceUfrag=n[r].substring(12);else if(-1!=n[r].indexOf("a=ice-pwd:"))t.iceInfo.icePwd=n[r].substring(10);else if(-1!=n[r].indexOf("a=ice-options:"))t.iceInfo.iceOptions=n[r].substring(14);else if(-1!=n[r].indexOf("a=fingerprint:")){var a=n[r].substring(14).split(" ");t.fingerprintInfo.algorithm=a[0],t.fingerprintInfo.value=a[1]}}}return null!=s&&(t.mediaDesc.push(s),s=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.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="";for(var n in t+="a=rtpmap:"+e.playload+" "+e.codecName+"/"+e.codecInterval+"\r\n",e.rtcpFB)t+="a=rtcp-fb:"+e.playload+" "+e.rtcpFB[n]+"\r\n";for(var n in e.fmtp)t+="a=fmtp:"+e.playload+" "+e.fmtp[n]+"\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;var n="";for(var i in e.codecInfoArray){var s=e.codecInfoArray[i];t+=" "+s.playload,n+=this.genCodecSdp(s)}for(var i in t+="\r\n",t+="c=IN IP4 0.0.0.0\r\n",e.candidate)t+="a=candidate:"+e.candidate[i]+"\r\n";for(var r 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",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:"+r+" "+e.extmap[r]+"\r\n";for(var i in t+=n,e.ssrcGroup.length&&(t+="a=ssrc-group:FID "+e.ssrcGroup.join(" ")+"\r\n"),e.ssrcInfoArray)t+=this.genSSRCSdp(e.ssrcInfoArray[i]);return t},SdpUtils.prototype.genSdp=function(e){var t="";for(var n in 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",0!=e.group.length&&(t+="a=group:BUNDLE "+e.group.join(" ")+"\r\n"),0!=e.wms.length&&(t+="a=msid-semantic:WMS "+e.wms.join(" ")+"\r\n"),t+=this.genIceInfoSdp(e.iceInfo),t+=this.genFingerprintSdp(e.fingerprintInfo),e.mediaDesc)t+=this.genMediaSdp(e.mediaDesc[n]);return t}}var StarWebRTC=function(){var s=RTCPeerConnection,n=(navigator.mediaDevices.getUserMedia,RTCIceCandidate),r=RTCSessionDescription,i=(navigator.mozGetUserMedia,{});function e(){this.events={}}function t(){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.bigStreamId="",this.smallStreamId="",this.bigSSRC="",this.smallSSRC="",this.audioSSRC="",this.answerSDP="",this.offerSDP={},this.starPeerConnection=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}return e.prototype.on=function(e,t){this.events[e]=this.events[e]||[],this.events[e].push(t)},e.prototype.emit=function(e,t){var n,i,s=this.events[e],r=Array.prototype.slice.call(arguments,1);if(s)for(n=0,i=s.length;n<i;n++)s[n].apply(null,r)},(t.prototype=new e).destroy=function(){this.emit("_remove_peer")},t.prototype.resetStreamInfos=function(e){this.streamInfos=[];for(var t=0;t<7;++t){var n=new a;n.bigVideoSSRC=2312312300+t,n.smallVideoSSRC=2312311300+t,n.audioSSRC=2312310300+t,n.streamId=this.createRandomString(36),this.streamInfos.push(n)}},t.prototype.init=function(){this.resetStreamInfos();var r=this;this.on("_peers",function(e){r.connections=e.connections,r.me=e.you,r.emit("get_peers",r.connections),r.emit("connected",socket)}),this.on("_ice_candidate",function(e){var t=new n({label:1,candidate:"candidate:504457478 1 udp 2122260223 123.123.3.116 8989 typ host generation 0 ufrag Nud3 network-id 1",socketId:"xuaisi"});r.starPeerConnection.addIceCandidate(t),r.emit("get_ice_candidate",t)}),this.on("_remove_peer",function(e){r.closePeerConnection(r.starPeerConnection),delete starPeerConnection,r.bigStreamId="",r.smallStreamId="",r.bigSSRC="",r.smallSSRC="",r.audioSSRC="",r.answerSDP="",r.offerSDP={},r.starPeerConnection=null,r.callback=null,r.serverIp="0.0.0.0",r.serverPort=80,r.resetStreamInfos()}),this.on("_offer",function(e){r.receiveOffer(e.socketId,e.sdp),r.emit("get_offer",e)}),this.on("_answer",function(e){var t=e.sessionDesc;t.type="answer",t.sdp=t.sdp.replace(/actpass/g,"active"),r.receiveAnswer(e.socketId,t),r.emit("get_answer",e)}),this.on("_webrtc_apply_ok",function(e,t){var n="candidate:504457478 1 udp 2122260223 "+r.serverIp+" "+r.serverPort+" typ host generation 0 ufrag Nud3 network-id 1",i=sdpUtils.clone(r.offerSDP);r.createAnswerFromOffer2(i,"Nud3","SKuOCnwS3ScdasO5hD2aheqb",e,r.streamInfos,[n]);var s={type:"answer"};s.sdp=sdpUtils.genSdp(i),r.streamAllsetCallback=t,r.receiveAnswer("xuaisi",s),r.emit("get_answer",s)}),this.on("send_file_error",function(e,t,n,i){r.cleanSendFile(n,t)}),this.on("receive_file_error",function(e,t){r.cleanReceiveFile(t)}),this.on("ready",function(e,t){var n=t||!1;r.callback=e,r.createPeerConnections(),r.addStreams(n),r.sendOffers()})},t.prototype.createStream=function(e,t){var n=this;navigator.mediaDevices.getUserMedia?(this.numStreams++,navigator.mediaDevices.getUserMedia(e).then(function(e){n.localMediaStream=e,n.initializedStreams++,t("success",e)}).catch(function(e){t("failed",e)})):t("failed",new Error("WebRTC is not yet supported in this browser."))},t.prototype.addStreams=function(n){var i=this;this.localMediaStream.getTracks().forEach(function(e){if(i.starPeerConnection.addTrack(e,i.localMediaStream),"video"==e.kind&&(i.bigStreamId=e.id,!n)){var t=e.clone();i.smallStreamId=t.id,t.applyConstraints({width:{ideal:120},height:{ideal:90},frameRate:{ideal:15,max:15}}),i.starPeerConnection.addTrack(t,i.localMediaStream)}})},t.prototype.attachStream=function(e,t){document.getElementById(t).srcObject=e},t.prototype.switchStream=function(t){var n=[];t.getVideoTracks().forEach(function(e){n.push(e),t.removeTrack(e)});for(var e=n.length-1;0<=e;e--)t.addTrack(n[e])},t.prototype.switchStreamInfo=function(e){e.switchFlag=!e.switchFlag,this.switchStream(e.streamObj)},t.prototype.resetStreamInfo=function(e){e.switchFlag&&this.switchStreamInfo(e)},t.prototype.switchStreams=function(){for(var e in this.streamInfos)this.switchStreamInfo(this.streamInfos[e])},t.prototype.getStreamByIndex=function(e){return this.streamInfos[e]},t.prototype.getStreamInfos=function(e){return this.streamInfos},t.prototype.onAddStream=function(e){var t=!0;for(var n in this.streamInfos){var i=this.streamInfos[n];i.streamId==e.id&&(i.streamObj=e),null==i.streamObj&&(t=!1)}t&&null!=this.streamAllsetCallback&&(this.streamAllsetCallback(),this.streamAllsetCallback=null)},t.prototype.setServerInfo=function(e){this.serverIp=e.serverIp,this.serverPort=e.serverPort},t.prototype.createRandomString=function(e){for(var t=["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"],n="",i=0;i<e;++i)n+=t[Math.floor(Math.random()*t.length)];return n},t.prototype.uuid=function(){function e(){return(65536*(1+Math.random())|0).toString(16).substring(1)}return e()+e()+"-"+e()+"-"+e()+"-"+e()+"-"+e()+e()+e()},t.prototype.filterSdpObj=function(e){for(var t in e.mediaDesc){var n=e.mediaDesc[t];n.extmap={},n.ssrcGroup=[],n.codecInfoMap={};var i=[];for(var t in n.codecInfoArray){var s=n.codecInfoArray[t];"audio"==n.type?"opus"==s.codecName&&(i.push(s),n.codecInfoMap[s.playload]=s):"video"==n.type&&"H264"==s.codecName&&0==i.length&&(i.push(s),n.codecInfoMap[s.playload]=s)}n.codecInfoArray=i;var r={},a=[],o={};for(var t in n.ssrcInfoArray){var c=n.ssrcInfoArray[t];null==o[c.label]&&(o[c.label]=1,r[c.ssrc]=c,a.push(c))}n.ssrcInfoArray=a,n.ssrcInfoMap=r}},t.prototype.getSSRCData=function(e,t){var n={audioSSRC:"",smallVideoSSRC:"",bigVideoSSRC:""};for(var i in e.mediaDesc){var s=e.mediaDesc[i];if("audio"==s.type)for(var r in s.ssrcInfoMap){var a=s.ssrcInfoMap[r];n.audioSSRC=a.ssrc}else if("video"==s.type)for(var r in s.ssrcInfoMap){a=s.ssrcInfoMap[r];null!=t.bigStreamId&&-1!=a.label.indexOf(t.bigStreamId)&&(n.bigVideoSSRC=a.ssrc),null!=t.smallStreamId&&-1!=a.label.indexOf(t.smallStreamId)&&(n.smallVideoSSRC=a.ssrc)}}return n},t.prototype.createAnswerFromOffer2=function(e,t,n,i,s,r){var a=this.createRandomString(16);for(var o in""!=e.iceInfo.iceUfrag&&(e.iceInfo.iceUfrag=t),""!=e.iceInfo.icePwd&&(e.iceInfo.icePwd=n),""!=e.fingerprintInfo.algorithm&&(e.fingerprintInfo.value=i),e.mediaDesc){var c=e.mediaDesc[o];if(c.setup="passive",""!=c.iceInfo.iceUfrag&&(c.iceInfo.iceUfrag=t),""!=c.iceInfo.icePwd&&(c.iceInfo.icePwd=n),""!=c.fingerprintInfo.algorithm&&(c.fingerprintInfo.value=i),c.candidate=r,c.ssrcInfoMap={},c.ssrcInfoArray=[],c.ssrcGroup=[],"audio"==c.type)for(var o in s){var l={};l.ssrc=s[o].audioSSRC,l.cname=a,l.mslabel=s[o].streamId,l.label=this.uuid(),l.msid=l.mslabel+" "+l.label,c.ssrcInfoMap[l.ssrc]=l,c.ssrcInfoArray.push(l)}else if("video"==c.type)for(var o in s){var d;if(0!=s[o].smallVideoSSRC)(d={}).ssrc=s[o].smallVideoSSRC,d.cname=a,d.mslabel=s[o].streamId,d.label=this.uuid(),d.msid=d.mslabel+" "+d.label,c.ssrcInfoMap[d.ssrc]=d,c.ssrcInfoArray.push(d);if(0!=s[o].bigVideoSSRC)(d={}).ssrc=s[o].bigVideoSSRC,d.cname=a,d.mslabel=s[o].streamId,d.label=this.uuid(),d.msid=d.mslabel+" "+d.label,c.ssrcInfoMap[d.ssrc]=d,c.ssrcInfoArray.push(d)}}},t.prototype.sendOffers=function(){var e,s,r,a=this;(e=this.starPeerConnection).createOffer((s=e,r=this.callback,function(e){var t=sdpUtils.parseSdp(e.sdp);a.filterSdpObj(t);var n=sdpUtils.genSdp(t);a.offerSDP=t,e.sdp=n;var i=a.getSSRCData(t,{bigStreamId:a.bigStreamId,smallStreamId:a.smallStreamId});s.setLocalDescription(e).then(function(){r({type:"createOffer",status:"success",audioSSRC:i.audioSSRC,smallVideoSSRC:i.smallVideoSSRC,bigVideoSSRC:i.bigVideoSSRC})},function(){r({type:"createOffer",status:"failed",audioSSRC:i.audioSSRC,smallVideoSSRC:i.smallVideoSSRC,bigVideoSSRC:i.bigVideoSSRC})})}),function(e){console.log(e)})},t.prototype.receiveOffer=function(e,t){this.starPeerConnection;this.sendAnswer(e,t)},t.prototype.sendAnswer=function(t,e){var n=this.starPeerConnection,i=this;n.setRemoteDescription(new r(e)),n.createAnswer(function(e){n.setLocalDescription(e),i.socket.send(JSON.stringify({eventName:"__answer",data:{socketId:t,sdp:e}}))},function(e){console.log(e)})},t.prototype.receiveAnswer=function(e,t){var n=this.starPeerConnection,i=this;n.setRemoteDescription(new r(t)).then(function(){i.callback({type:"applyAnswer",status:"success"})},function(e){i.callback({type:"applyAnswer",status:"failed"})})},t.prototype.createPeerConnections=function(){this.createPeerConnection("xuaisi")},t.prototype.createPeerConnection=function(e){var t=this,n=new s(i);return(this.starPeerConnection=n).onicecandidate=function(e){},n.onopen=function(){t.emit("pc_opened",e,n)},n.onaddstream=function(e){t.onAddStream(e.stream)},n.ondatachannel=function(e){},n},t.prototype.closePeerConnection=function(e){e&&e.close()},t.prototype.broadcast=function(e){var t;for(t in this.dataChannels)this.sendMessage(e,t)},t.prototype.sendMessage=function(e,t){"open"===this.dataChannels[t].readyState.toLowerCase()&&this.dataChannels[t].send(JSON.stringify({type:"__msg",data:e}))},t.prototype.addDataChannels=function(){var e;for(e in this.peerConnections)this.createDataChannel(e)},t.prototype.createDataChannel=function(t,e){var n,i;n=this.peerConnections[t],t||this.emit("data_channel_create_error",t,new Error("attempt to create data channel without socket id")),n instanceof s||this.emit("data_channel_create_error",t,new Error("attempt to create data channel without peerConnection"));try{i=n.createDataChannel(e)}catch(e){this.emit("data_channel_create_error",t,e)}return this.addDataChannel(t,i)},t.prototype.addDataChannel=function(n,i){var s=this;return i.onopen=function(){s.emit("data_channel_opened",i,n)},i.onclose=function(e){delete s.dataChannels[n],s.emit("data_channel_closed",i,n)},i.onmessage=function(e){var t;"__file"===(t=JSON.parse(e.data)).type?s.parseFilePacket(t,n):s.emit("data_channel_message",i,n,t.data)},i.onerror=function(e){s.emit("data_channel_error",i,n,e)},this.dataChannels[n]=i},new t};
JavaScript
1
https://gitee.com/a75929/starrtc-web.git
git@gitee.com:a75929/starrtc-web.git
a75929
starrtc-web
starrtc-web
master

搜索帮助