-
Notifications
You must be signed in to change notification settings - Fork 1
/
bundle.js
1 lines (1 loc) · 5.02 KB
/
bundle.js
1
!function(e){var t={};function n(i){if(t[i])return t[i].exports;var a=t[i]={i:i,l:!1,exports:{}};return e[i].call(a.exports,a,a.exports,n),a.l=!0,a.exports}n.m=e,n.c=t,n.d=function(e,t,i){n.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:i})},n.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},n.t=function(e,t){if(1&t&&(e=n(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var i=Object.create(null);if(n.r(i),Object.defineProperty(i,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var a in e)n.d(i,a,function(t){return e[t]}.bind(null,a));return i},n.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return n.d(t,"a",t),t},n.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},n.p="",n(n.s=0)}([function(e,t,n){"use strict";n.r(t),n.d(t,"getMedia",function(){return s}),n.d(t,"Bond",function(){return d});let i={optional:[{minWidth:320},{minWidth:640},{minWidth:1024},{minWidth:1280}]};var a={iceServers:[{urls:["stun:stun.l.google.com:19302","stun:stun1.l.google.com:19302","stun:stun2.l.google.com:19302"]}]};const s=(e=i,t=!0)=>{let n={video:e,audio:t};return new Promise((e,t)=>{navigator.mediaDevices.getUserMedia(n).then(t=>{e(t)}).catch(e=>{t(e)})})};class d{constructor(e,t,n,i={},s=[250,2250],d=a){console.log("=== CONSTRUCTOR ==="),this.localStream=e,this.remoteStream,this.id=t,this.callbacks=i,this.sendMessage=n,this.servers=d,this.bandwidth=s,this.pc=new RTCPeerConnection(this.servers),this.handleIceCandidate=this.handleIceCandidate.bind(this),this.handleRemoteStreamAdded=this.handleRemoteStreamAdded.bind(this),this.handleRemoteStreamRemoved=this.handleRemoteStreamRemoved.bind(this),this.onDataChannelOpen=this.onDataChannelOpen.bind(this),this.onDataChannelReceive=this.onDataChannelReceive.bind(this),this.getBandwidth=this.getBandwidth.bind(this),this.dataChannel=this.pc.createDataChannel("data",{negotiated:!0,id:7}),this.dataChannel.onopen=this.onDataChannelOpen,this.dataChannel.onmessage=this.onDataChannelReceive,this.pc.onicecandidate=this.handleIceCandidate,this.pc.onaddstream=this.handleRemoteStreamAdded,this.pc.onremovestream=this.handleRemoteStreamRemoved,this.pc.addStream(this.localStream)}createAndSendOffer(){console.log("=== CREATE AND SEND OFFER ==="),this.pc.createOffer().then(e=>{this.pc.setLocalDescription(e),e.sdp=this.setSessionDescriptionBandwidth(e.sdp),this.sendMessage(e)}).catch(e=>{console.error("Failed to create session description: "+e.toString())})}receivedOffer(e){console.log("=== RECEIVED OFFER ==="),this.pc.setRemoteDescription(new RTCSessionDescription(e));let t=this.getBandwidth(e.sdp);this.bandwidth[0]+this.bandwidth[0]>t[0]+t[1]&&(this.bandwidth=t),this.createAndSendAnswer()}createAndSendAnswer(){console.log("=== CREATE AND SEND ANSWER ==="),this.pc.createAnswer().then(e=>{this.pc.setLocalDescription(e),e.sdp=this.setSessionDescriptionBandwidth(e.sdp),this.sendMessage(e,this.id)}).catch(e=>{console.error("Failed to create session description: "+e.toString())})}receivedAnswer(e){console.log("=== RECEIVED ANSWER ==="),this.pc.setRemoteDescription(new RTCSessionDescription(e))}receivedIceCandidate(e){let t=new RTCIceCandidate({sdpMLineIndex:e.label,candidate:e.candidate});this.pc.addIceCandidate(t)}handleIceCandidate(e){e.target;e.candidate&&(console.log("icecandidate event: ",e),e.candidate?this.sendMessage({type:"candidate",label:e.candidate.sdpMLineIndex,id:e.candidate.sdpMid,candidate:e.candidate.candidate},this.id):console.log("End of candidates."))}handleRemoteStreamAdded(e){this.remoteStream=e.stream,this.callbacks.remoteStreamAdded&&this.callbacks.remoteStreamAdded(this.remoteStream,this.id)}handleRemoteStreamRemoved(e){this.remoteStream=null,this.callbacks.remoteStreamRemoved&&this.callbacks.remoteStreamRemoved(this.id)}hangup(){this.pc.close(),this.pc=null,this.sendMessage("bye",this.id)}handleRemoteHangup(){this.pc.close(),this.pc=null}onDataChannelOpen(e){console.log("=== DATA CHANNEL OPEN ===")}onDataChannelReceive(e){console.log("received data",e),this.callbacks.onDataReceive(e)}sendData(e){this.dataChannel.send(e)}getIceConnectionState(){return!!this.pc&&this.pc.iceConnectionState}getLocalStream(){return this.localStream}getRemoteStream(){return this.remoteStream}getCallbacks(){return this.callbacks}getSendMessage(){return this.sendMessage}getConnection(){return this.pc}getServers(){return this.servers}setBandwidth(e=[250,2250]){this.bandwidth=e,this.createAndSendOffer()}getBandwidth(e){for(var t=e.split("\n"),n=[],i=0;i<t.length;i++)t[i].indexOf("b=AS:")>-1&&n.push(parseInt(t[i].split(":")[1]));return n}setSessionDescriptionBandwidth(e){for(var t=e.split("\n"),n=0,i=0,a=0;a<t.length;a++){let e=t[a];e.indexOf("m=audio")>-1&&(t[a+2].indexOf("b=AS")>-1?t[a+2]="b=AS:"+this.bandwidth[0]:n=a+2),e.indexOf("m=video")>-1&&(t[a+2].indexOf("b=AS")>-1?t[a+2]="b=AS:"+this.bandwidth[1]:i=a+2)}return n>0&&t.splice(n,0,"b=AS:"+this.bandwidth[0]),i&&t.splice(n>0?i+1:i,0,"b=AS:"+this.bandwidth[1]),t.join("\n")}}}]);