UNPKG

enc-webrtc

Version:

An utility which helps using web rtc.

1 lines 12.4 kB
!function(e,t){for(var n in t)e[n]=t[n]}(exports,function(e){var t={};function n(r){if(t[r])return t[r].exports;var i=t[r]={i:r,l:!1,exports:{}};return e[r].call(i.exports,i,i.exports,n),i.l=!0,i.exports}return n.m=e,n.c=t,n.d=function(e,t,r){n.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:r})},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 r=Object.create(null);if(n.r(r),Object.defineProperty(r,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var i in e)n.d(r,i,function(t){return e[t]}.bind(null,i));return r},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=1)}([function(e,t,n){"use strict";Object.defineProperty(t,"__esModule",{value:!0});var r=function(){return function(){var e=this;this.headers=[],this.get=function(t){return e.request("GET",t)},this.put=function(t,n){return e.request("PUT",t,n)},this.post=function(t,n){return e.request("POST",t,n)},this.delete=function(t){return e.request("DELETE",t)},this.request=function(t,n,r){return new Promise(function(i,o){var c=new XMLHttpRequest;if(c.onreadystatechange=function(){4==c.readyState&&(200==c.status?i(c.responseText):o())},c.open(t,n,!0),e.headers)for(var s=0,a=e.headers;s<a.length;s++){var u=a[s];c.setRequestHeader(u.name,u.value)}r?c.send(r):c.send()})}}}();t.Http=r},function(e,t,n){"use strict";Object.defineProperty(t,"__esModule",{value:!0}),function(e){for(var n in e)t.hasOwnProperty(n)||(t[n]=e[n])}(n(2))},function(e,t,n){"use strict";var r=this&&this.__awaiter||function(e,t,n,r){return new(n||(n=Promise))(function(i,o){function c(e){try{a(r.next(e))}catch(e){o(e)}}function s(e){try{a(r.throw(e))}catch(e){o(e)}}function a(e){e.done?i(e.value):new n(function(t){t(e.value)}).then(c,s)}a((r=r.apply(e,t||[])).next())})},i=this&&this.__generator||function(e,t){var n,r,i,o,c={label:0,sent:function(){if(1&i[0])throw i[1];return i[1]},trys:[],ops:[]};return o={next:s(0),throw:s(1),return:s(2)},"function"==typeof Symbol&&(o[Symbol.iterator]=function(){return this}),o;function s(o){return function(s){return function(o){if(n)throw new TypeError("Generator is already executing.");for(;c;)try{if(n=1,r&&(i=2&o[0]?r.return:o[0]?r.throw||((i=r.return)&&i.call(r),0):r.next)&&!(i=i.call(r,o[1])).done)return i;switch(r=0,i&&(o=[2&o[0],i.value]),o[0]){case 0:case 1:i=o;break;case 4:return c.label++,{value:o[1],done:!1};case 5:c.label++,r=o[1],o=[0];continue;case 7:o=c.ops.pop(),c.trys.pop();continue;default:if(!(i=(i=c.trys).length>0&&i[i.length-1])&&(6===o[0]||2===o[0])){c=0;continue}if(3===o[0]&&(!i||o[1]>i[0]&&o[1]<i[3])){c.label=o[1];break}if(6===o[0]&&c.label<i[1]){c.label=i[1],i=o;break}if(i&&c.label<i[2]){c.label=i[2],c.ops.push(o);break}i[2]&&c.ops.pop(),c.trys.pop();continue}o=t.call(e,c)}catch(e){o=[6,e],r=0}finally{n=i=0}if(5&o[0])throw o[1];return{value:o[0]?o[1]:void 0,done:!0}}([o,s])}}};Object.defineProperty(t,"__esModule",{value:!0});var o=n(3),c=n(0),s=n(4),a=function(){return function(e){var t=this;this.sigServeUrl=e,this.receivedNegotiatedConnection=new o.EEventT,this.retryIntervalSeconds=2,this.isStarted=!1,this.createListener=function(){return new Promise(function(e,n){return r(t,void 0,void 0,function(){var t,n,r;return i(this,function(i){switch(i.label){case 0:return this.serverId?[3,2]:(r=(n=JSON).parse,[4,this.http.post(this.sigServeUrl+"/api/v1/create")]);case 1:t=r.apply(n,[i.sent()]),this.serverId=t.id,i.label=2;case 2:return e(this.serverId),[2]}})})})},this.startListener=function(){console.log("started"),t.isStarted=!0,t.checkConnectStart()},this.stopListener=function(){console.log("stopping..."),t.isStarted=!1},this.connect=function(e){return new Promise(function(n,o){return r(t,void 0,void 0,function(){var t,r,o,c;return i(this,function(i){switch(i.label){case 0:return o=(r=JSON).parse,[4,this.http.post(this.sigServeUrl+"/api/v1/connectstart/"+e)];case 1:return t=o.apply(r,[i.sent()]),c=new s.WebRtcClientWorkflow(this.sigServeUrl,e,t.id),n(c.RtcPeerConnection),[2]}})})})},this.checkConnectStart=function(){t.serverId&&t.isStarted?t.http.get(t.sigServeUrl+"/api/v1/connectstart/"+t.serverId).then(function(e){for(var n=0,r=JSON.parse(e);n<r.length;n++){var i=r[n];if(t.serverId){var o=new s.WebRtcClientWorkflow(t.sigServeUrl,t.serverId,i.id);t.receivedNegotiatedConnection.dispatchEvent(o.RtcPeerConnection)}}setTimeout(function(){t.checkConnectStart()},1e3*t.retryIntervalSeconds)}):console.log("stopped")},this.http=new c.Http}}();t.WebRtcConnector=a},function(e,t,n){"use strict";Object.defineProperty(t,"__esModule",{value:!0});var r=function(){return function(){var e=this;this.listeners=[],this.addEventListener=function(t){if(!t||e.listeners.indexOf(t)>=0)throw"listener already added";e.listeners.push(t)},this.dispatchEvent=function(t){for(var n=0;n<e.listeners.length;n++)(0,e.listeners[n])(t)},this.removeEventListener=function(t){if(!t||e.listeners.indexOf(t)<0)throw"listener not found";e.listeners.splice(e.listeners.indexOf(t),1)}}}();t.EEventT=r},function(e,t,n){"use strict";var r=this&&this.__awaiter||function(e,t,n,r){return new(n||(n=Promise))(function(i,o){function c(e){try{a(r.next(e))}catch(e){o(e)}}function s(e){try{a(r.throw(e))}catch(e){o(e)}}function a(e){e.done?i(e.value):new n(function(t){t(e.value)}).then(c,s)}a((r=r.apply(e,t||[])).next())})},i=this&&this.__generator||function(e,t){var n,r,i,o,c={label:0,sent:function(){if(1&i[0])throw i[1];return i[1]},trys:[],ops:[]};return o={next:s(0),throw:s(1),return:s(2)},"function"==typeof Symbol&&(o[Symbol.iterator]=function(){return this}),o;function s(o){return function(s){return function(o){if(n)throw new TypeError("Generator is already executing.");for(;c;)try{if(n=1,r&&(i=2&o[0]?r.return:o[0]?r.throw||((i=r.return)&&i.call(r),0):r.next)&&!(i=i.call(r,o[1])).done)return i;switch(r=0,i&&(o=[2&o[0],i.value]),o[0]){case 0:case 1:i=o;break;case 4:return c.label++,{value:o[1],done:!1};case 5:c.label++,r=o[1],o=[0];continue;case 7:o=c.ops.pop(),c.trys.pop();continue;default:if(!(i=(i=c.trys).length>0&&i[i.length-1])&&(6===o[0]||2===o[0])){c=0;continue}if(3===o[0]&&(!i||o[1]>i[0]&&o[1]<i[3])){c.label=o[1];break}if(6===o[0]&&c.label<i[1]){c.label=i[1],i=o;break}if(i&&c.label<i[2]){c.label=i[2],c.ops.push(o);break}i[2]&&c.ops.pop(),c.trys.pop();continue}o=t.call(e,c)}catch(e){o=[6,e],r=0}finally{n=i=0}if(5&o[0])throw o[1];return{value:o[0]?o[1]:void 0,done:!0}}([o,s])}}};Object.defineProperty(t,"__esModule",{value:!0});var o=n(0),c=function(){function e(e,t,n){var c=this;this.sigServeUrl=e,this.serverId=t,this.connectId=n,this.retries=5,this.retryIntervalSeconds=2,this.retryDelaySeconds=2,this.listenForNegotiation=!0,this.isConnecting=!1,this.negotiationNeeded=function(e){return r(c,void 0,void 0,function(){return i(this,function(e){switch(e.label){case 0:return console.log("rtcPeerConnection.onnegotiationneeded"),this.isConnecting?[3,2]:(this.isConnecting=!0,[4,this.client1Workflow()]);case 1:return e.sent(),this.isConnecting=!1,[3,3];case 2:console.log("This log should not appear. But if you see it, it doesn't mean there is an error. It's just for documentation purposes."),e.label=3;case 3:return[2]}})})},this.client1Workflow=function(){return r(c,void 0,void 0,function(){var e,t,n=this;return i(this,function(o){switch(o.label){case 0:return this.listenForNegotiation=!1,this.client="client1",[4,this.rtcPeerConnection.createOffer()];case 1:return e=o.sent(),[4,this.rtcPeerConnection.setLocalDescription(e)];case 2:return o.sent(),console.log("PUT connectoffer"),[4,this.http.put(this.sigServeUrl+"/api/v1/connectoffer/"+this.serverId+"/"+this.connectId,JSON.stringify(e))];case 3:return o.sent(),[4,this.retry(this.retries,this.retryDelaySeconds,function(){return r(n,void 0,void 0,function(){var e=this;return i(this,function(t){return[2,new Promise(function(t,n){return r(e,void 0,void 0,function(){var e,r;return i(this,function(i){switch(i.label){case 0:return i.trys.push([0,2,,3]),console.log("GET connectanswer"),[4,this.http.get(this.sigServeUrl+"/api/v1/connectanswer/"+this.serverId+"/"+this.connectId)];case 1:return e=i.sent(),r=JSON.parse(e),t(r),[3,3];case 2:return i.sent(),n(),[3,3];case 3:return[2]}})})})]})})})];case 4:return t=o.sent(),[4,this.rtcPeerConnection.setRemoteDescription(t)];case 5:return o.sent(),this.checkIceCandidates("client2icecandidates",this.retries,this.retryDelaySeconds),[4,this.validateRtcConnectionState(this.retries*this.retryDelaySeconds)];case 6:return o.sent(),[2]}})})},this.client2Workflow=function(e){return r(c,void 0,void 0,function(){var t;return i(this,function(n){switch(n.label){case 0:return this.listenForNegotiation=!1,this.client="client2",[4,this.rtcPeerConnection.setRemoteDescription(e)];case 1:return n.sent(),[4,this.rtcPeerConnection.createAnswer()];case 2:return t=n.sent(),[4,this.rtcPeerConnection.setLocalDescription(t)];case 3:return n.sent(),console.log("PUT connectanswer"),[4,this.http.put(this.sigServeUrl+"/api/v1/connectanswer/"+this.serverId+"/"+this.connectId,JSON.stringify(t))];case 4:return n.sent(),this.checkIceCandidates("client1icecandidates",this.retries,this.retryDelaySeconds),[4,this.validateRtcConnectionState(this.retries*this.retryDelaySeconds)];case 5:return n.sent(),[2]}})})},this.validateRtcConnectionState=function(e){return new Promise(function(t,n){var r=setTimeout(function(){c.checkIceCandidates=function(){},console.log("not connected :("),console.log(c.rtcPeerConnection.connectionState),console.log(c.rtcPeerConnection),n()},1e3*e);c.rtcPeerConnection.addEventListener("connectionstatechange",function(e){console.log(c.rtcPeerConnection.connectionState),console.log(e),"connected"==c.rtcPeerConnection.connectionState&&(c.checkIceCandidates=function(){},console.log("connected :)"),t(),clearTimeout(r))}),"connected"==c.rtcPeerConnection.connectionState&&(c.checkIceCandidates=function(){},console.log("connected :)"),clearTimeout(r),t())})},this.checkIceCandidates=function(e,t,n){console.log("GET icecandidate"),c.http.get(c.sigServeUrl+"/api/v1/"+e+"/"+c.serverId+"/"+c.connectId).then(function(r){for(var i=0,o=JSON.parse(r);i<o.length;i++){var s=o[i];console.log("GOT icecandidate"),console.log(s),c.rtcPeerConnection.addIceCandidate(s)}"connected"!=c.rtcPeerConnection.connectionState&&setTimeout(function(){c.checkIceCandidates(e,t--,n)},1e3*n)})},this.addIceCandidate=function(e){console.log("ICE candidate:"),e.candidate&&(console.log(e.candidate),c.http.put(c.sigServeUrl+"/api/v1/"+c.client+"icecandidates/"+c.serverId+"/"+c.connectId,JSON.stringify(e.candidate)))},this.checkConnectStart=function(){return r(c,void 0,void 0,function(){var e,t,n=this;return i(this,function(r){switch(r.label){case 0:if(!this.listenForNegotiation)return[3,5];console.log("listening for negotiation"),r.label=1;case 1:return r.trys.push([1,4,,5]),[4,this.http.get(this.sigServeUrl+"/api/v1/connectoffer/"+this.serverId+"/"+this.connectId)];case 2:return e=r.sent(),t=JSON.parse(e),[4,this.client2Workflow(t)];case 3:return r.sent(),this.listenForNegotiation=!1,[3,5];case 4:return r.sent(),setTimeout(function(){n.checkConnectStart()},1e3*this.retryIntervalSeconds),[3,5];case 5:return[2]}})})},this.retry=function(e,t,n){return new Promise(function(o,s){return r(c,void 0,void 0,function(){var c=this;return i(this,function(a){return n().then(function(e){o(e)}).catch(function(){console.log("catched"),--e>0?setTimeout(function(){return r(c,void 0,void 0,function(){var r;return i(this,function(i){switch(i.label){case 0:return[4,this.retry(e,t,n)];case 1:return r=i.sent(),o(r),[2]}})})},1e3*t):s()}),[2]})})})},this.http=new o.Http;this.rtcPeerConnection=new RTCPeerConnection({iceServers:[{urls:"stun:stun1.l.google.com:19302"}]}),this.rtcPeerConnection.addEventListener("icecandidate",this.addIceCandidate),this.rtcPeerConnection.addEventListener("negotiationneeded",this.negotiationNeeded),this.checkConnectStart()}return Object.defineProperty(e.prototype,"RtcPeerConnection",{get:function(){return this.rtcPeerConnection},enumerable:!0,configurable:!0}),e}();t.WebRtcClientWorkflow=c}]));