forked from feross/simple-peer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
simplepeer.min.js
1 lines (1 loc) · 15.3 KB
/
simplepeer.min.js
1
(function(a){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=a();else if("function"==typeof define&&define.amd)define([],a);else{var b;b="undefined"==typeof window?"undefined"==typeof global?"undefined"==typeof self?this:self:global:window,b.SimplePeer=a()}})(function(){return function(){function b(d,e,g){function a(j,i){if(!e[j]){if(!d[j]){var f="function"==typeof require&&require;if(!i&&f)return f(j,!0);if(h)return h(j,!0);var c=new Error("Cannot find module '"+j+"'");throw c.code="MODULE_NOT_FOUND",c}var k=e[j]={exports:{}};d[j][0].call(k.exports,function(b){var c=d[j][1][b];return a(c||b)},k,k.exports,b,d,e,g)}return e[j].exports}for(var h="function"==typeof require&&require,c=0;c<g.length;c++)a(g[c]);return a}return b}()({"/":[function(a,b){function c(a){for(var b="",c=0;c<a.length;++c)b+=a[c].toString(16).padStart(2,"0");return b}function d(a){var b=this;return b instanceof d?void(g.call(b),b.channelName=a.initiator?a.channelName||c(i(20)):null,b._isChromium="undefined"!=typeof window&&!!window.webkitRTCPeerConnection,b.initiator=a.initiator||!1,b.channelConfig=a.channelConfig||d.channelConfig,b.config=a.config||d.config,b.constraints=b._transformConstraints(a.constraints||d.constraints),b.offerConstraints=b._transformConstraints(a.offerConstraints||{}),b.answerConstraints=b._transformConstraints(a.answerConstraints||{}),b.sdpTransform=a.sdpTransform||function(a){return a},b.streams=a.streams||(a.stream?[a.stream]:[]),b.trickle=void 0===a.trickle||a.trickle,b.destroyed=!1,b.connected=!1,b.remoteAddress=void 0,b.remoteFamily=void 0,b.remotePort=void 0,b.localAddress=void 0,b.localPort=void 0,b._wrtc=a.wrtc&&"object"==typeof a.wrtc?a.wrtc:window,b._pcReady=!1,b._channelReady=!1,b._iceComplete=!1,b._channel=null,b._pendingCandidates=[],b._isNegotiating=!1,b._batchedNegotiation=!1,b._queuedNegotiation=!1,b._sendersAwaitingStable=[],b._senderMap=new WeakMap,b._remoteTracks=[],b._remoteStreams=[],b._chunk=null,b._cb=null,b._interval=null,b._pc=new b._wrtc.RTCPeerConnection(b.config,b.constraints),b._isReactNativeWebrtc="number"==typeof b._pc._peerConnectionId,b._pc.oniceconnectionstatechange=function(){b._onIceStateChange()},b._pc.onicegatheringstatechange=function(){b._onIceStateChange()},b._pc.onsignalingstatechange=function(){b._onSignalingStateChange()},b._pc.onicecandidate=function(a){b._onIceCandidate(a)},b.initiator?b._setupData({channel:b._pc.createDataChannel(b.channelName,b.channelConfig)}):b._pc.ondatachannel=function(a){b._setupData(a)},"addTrack"in b._pc&&(b.streams&&b.streams.forEach(function(a){b.addStream(a)}),b._pc.ontrack=function(a){b._onTrack(a)}),b.initiator&&b._needsNegotiation()):new d(a)}function e(a,b){var c=new Error(a);return c.code=b,c}function f(){}b.exports=d;var g=a("eventemitter3"),h=a("inherits"),i=a("./lib/randombytes"),j=65536;h(d,g),d.config={iceServers:[{urls:"stun:stun.l.google.com:19302"},{urls:"stun:global.stun.twilio.com:3478?transport=udp"}]},d.constraints={},d.channelConfig={},Object.defineProperty(d.prototype,"bufferSize",{get:function(){var a=this;return a._channel&&a._channel.bufferedAmount||0}}),d.prototype.address=function(){var a=this;return{port:a.localPort,family:"IPv4",address:a.localAddress}},d.prototype.signal=function(a){var b=this;if(b.destroyed)throw e("cannot signal after peer is destroyed","ERR_SIGNALING");if("string"==typeof a)try{a=JSON.parse(a)}catch(b){a={}}a.renegotiate&&b._needsNegotiation(),a.candidate&&(b._pc.remoteDescription&&b._pc.remoteDescription.type?b._addIceCandidate(a.candidate):b._pendingCandidates.push(a.candidate)),a.sdp&&b._pc.setRemoteDescription(new b._wrtc.RTCSessionDescription(a),function(){b.destroyed||(b._pendingCandidates.forEach(function(a){b._addIceCandidate(a)}),b._pendingCandidates=[],"offer"===b._pc.remoteDescription.type&&b._createAnswer())},function(a){b.destroy(e(a,"ERR_SET_REMOTE_DESCRIPTION"))}),a.sdp||a.candidate||a.renegotiate||b.destroy(e("signal() called with invalid signal data","ERR_SIGNALING"))},d.prototype._addIceCandidate=function(a){var b=this;try{b._pc.addIceCandidate(new b._wrtc.RTCIceCandidate(a),f,function(a){b.destroy(e(a,"ERR_ADD_ICE_CANDIDATE"))})}catch(a){b.destroy(e("error adding candidate: "+a.message,"ERR_ADD_ICE_CANDIDATE"))}},d.prototype.send=function(a){var b=this;b._channel.send(a)},d.prototype.addStream=function(a){var b=this;a.getTracks().forEach(function(c){b.addTrack(c,a)})},d.prototype.addTrack=function(a,b){var c=this,d=c._pc.addTrack(a,b),e=c._senderMap.get(a)||new WeakMap;e.set(b,d),c._senderMap.set(a,e),c._needsNegotiation()},d.prototype.removeTrack=function(a,b){var c=this,d=c._senderMap.get(a),e=d?d.get(b):null;e||c.destroy(new Error("Cannot remove track that was never added."));try{c._pc.removeTrack(e)}catch(a){"NS_ERROR_UNEXPECTED"===a.name?c._sendersAwaitingStable.push(e):c.destroy(a)}},d.prototype.removeStream=function(a){var b=this;a.getTracks().forEach(function(c){b.removeTrack(c,a)})},d.prototype._needsNegotiation=function(){var a=this;a._batchedNegotiation||(a._batchedNegotiation=!0,setTimeout(function(){a._batchedNegotiation=!1,a.negotiate()},0))},d.prototype.negotiate=function(){var a=this;a.initiator?a._isNegotiating?a._queuedNegotiation=!0:a._createOffer():a.emit("signal",{renegotiate:!0}),a._isNegotiating=!0},d.prototype.destroy=function(a){var b=this;if(!b.destroyed){if(b.destroyed=!0,b.connected=!1,b._pcReady=!1,b._channelReady=!1,b._remoteTracks=null,b._remoteStreams=null,b._senderMap=null,clearInterval(b._interval),b._interval=null,b._chunk=null,b._cb=null,b._channel){try{b._channel.close()}catch(a){}b._channel.onmessage=null,b._channel.onopen=null,b._channel.onclose=null,b._channel.onerror=null}if(b._pc){try{b._pc.close()}catch(a){}b._pc.oniceconnectionstatechange=null,b._pc.onicegatheringstatechange=null,b._pc.onsignalingstatechange=null,b._pc.onicecandidate=null,"addTrack"in b._pc&&(b._pc.ontrack=null),b._pc.ondatachannel=null}b._pc=null,b._channel=null,a&&b.emit("error",a),b.emit("close")}},d.prototype._setupData=function(a){var b=this;return a.channel?void(b._channel=a.channel,b._channel.binaryType="arraybuffer","number"==typeof b._channel.bufferedAmountLowThreshold&&(b._channel.bufferedAmountLowThreshold=j),b.channelName=b._channel.label,b._channel.onmessage=function(a){b._onChannelMessage(a)},b._channel.onbufferedamountlow=function(){b._onChannelBufferedAmountLow()},b._channel.onopen=function(){b._onChannelOpen()},b._channel.onclose=function(){b._onChannelClose()},b._channel.onerror=function(a){b.destroy(e(a,"ERR_DATA_CHANNEL"))}):b.destroy(e("Data channel event is missing `channel` property","ERR_DATA_CHANNEL"))},d.prototype._createOffer=function(){var a=this;a.destroyed||a._pc.createOffer(function(b){function c(){var c=a._pc.localDescription||b;a.emit("signal",{type:c.type,sdp:c.sdp})}a.destroyed||(b.sdp=a.sdpTransform(b.sdp),a._pc.setLocalDescription(b,function(){a.destroyed||(a.trickle||a._iceComplete?c():a.once("_iceComplete",c))},function(b){a.destroy(e(b,"ERR_SET_LOCAL_DESCRIPTION"))}))},function(b){a.destroy(e(b,"ERR_CREATE_OFFER"))},a.offerConstraints)},d.prototype._createAnswer=function(){var a=this;a.destroyed||a._pc.createAnswer(function(b){function c(){var c=a._pc.localDescription||b;a.emit("signal",{type:c.type,sdp:c.sdp})}a.destroyed||(b.sdp=a.sdpTransform(b.sdp),a._pc.setLocalDescription(b,function(){a.destroyed||(a.trickle||a._iceComplete?c():a.once("_iceComplete",c))},function(b){a.destroy(e(b,"ERR_SET_LOCAL_DESCRIPTION"))}))},function(b){a.destroy(e(b,"ERR_CREATE_ANSWER"))},a.answerConstraints)},d.prototype._onIceStateChange=function(){var a=this;if(!a.destroyed){var b=a._pc.iceConnectionState,c=a._pc.iceGatheringState;a.emit("iceStateChange",b,c),("connected"===b||"completed"===b)&&(a._pcReady=!0,a._maybeReady()),"failed"===b&&a.destroy(e("Ice connection failed.","ERR_ICE_CONNECTION_FAILURE")),"closed"===b&&a.destroy(new Error("Ice connection closed."))}},d.prototype.getStats=function(a){var b=this;0===b._pc.getStats.length?b._pc.getStats().then(function(b){var c=[];b.forEach(function(a){c.push(a)}),a(null,c)},function(b){a(b)}):b._isReactNativeWebrtc?b._pc.getStats(null,function(b){var c=[];b.forEach(function(a){c.push(a)}),a(null,c)},function(b){a(b)}):0<b._pc.getStats.length?b._pc.getStats(function(c){if(!b.destroyed){var d=[];c.result().forEach(function(a){var b={};a.names().forEach(function(c){b[c]=a.stat(c)}),b.id=a.id,b.type=a.type,b.timestamp=a.timestamp,d.push(b)}),a(null,d)}},function(b){a(b)}):a(null,[])},d.prototype._maybeReady=function(){function a(){b.destroyed||b.getStats(function(c,d){function f(a){j=!0;var c=h[a.localCandidateId];c&&c.ip?(b.localAddress=c.ip,b.localPort=+c.port):c&&c.ipAddress?(b.localAddress=c.ipAddress,b.localPort=+c.portNumber):"string"==typeof a.googLocalAddress&&(c=a.googLocalAddress.split(":"),b.localAddress=c[0],b.localPort=+c[1]);var d=g[a.remoteCandidateId];d&&d.ip?(b.remoteAddress=d.ip,b.remotePort=+d.port):d&&d.ipAddress?(b.remoteAddress=d.ipAddress,b.remotePort=+d.portNumber):"string"==typeof a.googRemoteAddress&&(d=a.googRemoteAddress.split(":"),b.remoteAddress=d[0],b.remotePort=+d[1]),b.remoteFamily="IPv4"}if(!b.destroyed){c&&(d=[]);var g={},h={},i={},j=!1;if(d.forEach(function(a){("remotecandidate"===a.type||"remote-candidate"===a.type)&&(g[a.id]=a),("localcandidate"===a.type||"local-candidate"===a.type)&&(h[a.id]=a),("candidatepair"===a.type||"candidate-pair"===a.type)&&(i[a.id]=a)}),d.forEach(function(a){"transport"===a.type&&a.selectedCandidatePairId&&f(i[a.selectedCandidatePairId]),("googCandidatePair"===a.type&&"true"===a.googActiveConnection||("candidatepair"===a.type||"candidate-pair"===a.type)&&a.selected)&&f(a)}),!j&&(!Object.keys(i).length||Object.keys(h).length))return void setTimeout(a,100);if(b._connecting=!1,b.connected=!0,b._chunk){try{b.send(b._chunk)}catch(a){return b.destroy(e(a,"ERR_DATA_CHANNEL"))}b._chunk=null;var k=b._cb;b._cb=null,k(null)}"number"!=typeof b._channel.bufferedAmountLowThreshold&&(b._interval=setInterval(function(){b._onInterval()},150),b._interval.unref&&b._interval.unref()),b.emit("connect")}})}var b=this;b.connected||b._connecting||!b._pcReady||!b._channelReady||(b._connecting=!0,a())},d.prototype._onInterval=function(){var a=this;a._cb&&a._channel&&!(a._channel.bufferedAmount>j)&&a._onChannelBufferedAmountLow()},d.prototype._onSignalingStateChange=function(){var a=this;a.destroyed||("stable"===a._pc.signalingState&&(a._isNegotiating=!1,a._sendersAwaitingStable.forEach(function(b){a.removeTrack(b),a._queuedNegotiation=!0}),a._sendersAwaitingStable=[],a._queuedNegotiation&&(a._queuedNegotiation=!1,a._needsNegotiation()),a.emit("negotiate")),a.emit("signalingStateChange",a._pc.signalingState))},d.prototype._onIceCandidate=function(a){var b=this;b.destroyed||(a.candidate&&b.trickle?b.emit("signal",{candidate:{candidate:a.candidate.candidate,sdpMLineIndex:a.candidate.sdpMLineIndex,sdpMid:a.candidate.sdpMid}}):!a.candidate&&(b._iceComplete=!0,b.emit("_iceComplete")))},d.prototype._onChannelMessage=function(a){var b=this;if(!b.destroyed){var c=a.data;c instanceof ArrayBuffer&&(c=new Uint8Array(c)),b.emit("data",c)}},d.prototype._onChannelBufferedAmountLow=function(){var a=this;if(!a.destroyed&&a._cb){var b=a._cb;a._cb=null,b(null)}},d.prototype._onChannelOpen=function(){var a=this;a.connected||a.destroyed||(a._channelReady=!0,a._maybeReady())},d.prototype._onChannelClose=function(){var a=this;a.destroyed||a.destroy()},d.prototype._onTrack=function(a){var b=this;b.destroyed||a.streams.forEach(function(c){b.emit("track",a.track,c),b._remoteTracks.push({track:a.track,stream:c}),b._remoteStreams.some(function(a){return a.id===c.id})||(b._remoteStreams.push(c),setTimeout(function(){b.emit("stream",c)},0))})},d.prototype._transformConstraints=function(a){var b=this;if(0===Object.keys(a).length)return a;if((a.mandatory||a.optional)&&!b._isChromium){var c=Object.assign({},a.optional,a.mandatory);return void 0!==c.OfferToReceiveVideo&&(c.offerToReceiveVideo=c.OfferToReceiveVideo,delete c.OfferToReceiveVideo),void 0!==c.OfferToReceiveAudio&&(c.offerToReceiveAudio=c.OfferToReceiveAudio,delete c.OfferToReceiveAudio),c}return a.mandatory||a.optional||!b._isChromium?a:(void 0!==a.offerToReceiveVideo&&(a.OfferToReceiveVideo=a.offerToReceiveVideo,delete a.offerToReceiveVideo),void 0!==a.offerToReceiveAudio&&(a.OfferToReceiveAudio=a.offerToReceiveAudio,delete a.offerToReceiveAudio),{mandatory:a})}},{"./lib/randombytes":1,eventemitter3:2,inherits:3}],1:[function(a,b){b.exports=function(a){var b=new Uint8Array(a);return window.crypto.getRandomValues(b),b}},{}],2:[function(a,b){"use strict";function c(){}function d(a,b,c){this.fn=a,this.context=b,this.once=c||!1}function e(a,b,c,e,f){if("function"!=typeof c)throw new TypeError("The listener must be a function");var g=new d(c,e||a,f),h=k?k+b:b;return a._events[h]?a._events[h].fn?a._events[h]=[a._events[h],g]:a._events[h].push(g):(a._events[h]=g,a._eventsCount++),a}function f(a,b){0==--a._eventsCount?a._events=new c:delete a._events[b]}function g(){this._events=new c,this._eventsCount=0}var h=Object.prototype.hasOwnProperty,k="~";Object.create&&(c.prototype=Object.create(null),!new c().__proto__&&(k=!1)),g.prototype.eventNames=function(){var a,b,c=[];if(0===this._eventsCount)return c;for(b in a=this._events)h.call(a,b)&&c.push(k?b.slice(1):b);return Object.getOwnPropertySymbols?c.concat(Object.getOwnPropertySymbols(a)):c},g.prototype.listeners=function(a){var b=k?k+a:a,c=this._events[b];if(!c)return[];if(c.fn)return[c.fn];for(var d=0,e=c.length,f=Array(e);d<e;d++)f[d]=c[d].fn;return f},g.prototype.listenerCount=function(a){var b=k?k+a:a,c=this._events[b];return c?c.fn?1:c.length:0},g.prototype.emit=function(a,b,c,d,e,f){var g=k?k+a:a;if(!this._events[g])return!1;var h,l,m=this._events[g],n=arguments.length;if(m.fn){switch(m.once&&this.removeListener(a,m.fn,void 0,!0),n){case 1:return m.fn.call(m.context),!0;case 2:return m.fn.call(m.context,b),!0;case 3:return m.fn.call(m.context,b,c),!0;case 4:return m.fn.call(m.context,b,c,d),!0;case 5:return m.fn.call(m.context,b,c,d,e),!0;case 6:return m.fn.call(m.context,b,c,d,e,f),!0;}for(l=1,h=Array(n-1);l<n;l++)h[l-1]=arguments[l];m.fn.apply(m.context,h)}else{var o,p=m.length;for(l=0;l<p;l++)switch(m[l].once&&this.removeListener(a,m[l].fn,void 0,!0),n){case 1:m[l].fn.call(m[l].context);break;case 2:m[l].fn.call(m[l].context,b);break;case 3:m[l].fn.call(m[l].context,b,c);break;case 4:m[l].fn.call(m[l].context,b,c,d);break;default:if(!h)for(o=1,h=Array(n-1);o<n;o++)h[o-1]=arguments[o];m[l].fn.apply(m[l].context,h);}}return!0},g.prototype.on=function(a,b,c){return e(this,a,b,c,!1)},g.prototype.once=function(a,b,c){return e(this,a,b,c,!0)},g.prototype.removeListener=function(a,b,c,d){var e=k?k+a:a;if(!this._events[e])return this;if(!b)return f(this,e),this;var g=this._events[e];if(g.fn)g.fn!==b||d&&!g.once||c&&g.context!==c||f(this,e);else{for(var h=0,j=[],l=g.length;h<l;h++)(g[h].fn!==b||d&&!g[h].once||c&&g[h].context!==c)&&j.push(g[h]);j.length?this._events[e]=1===j.length?j[0]:j:f(this,e)}return this},g.prototype.removeAllListeners=function(a){var b;return a?(b=k?k+a:a,this._events[b]&&f(this,b)):(this._events=new c,this._eventsCount=0),this},g.prototype.off=g.prototype.removeListener,g.prototype.addListener=g.prototype.on,g.prefixed=k,g.EventEmitter=g,"undefined"!=typeof b&&(b.exports=g)},{}],3:[function(a,b){b.exports="function"==typeof Object.create?function(a,b){a.super_=b,a.prototype=Object.create(b.prototype,{constructor:{value:a,enumerable:!1,writable:!0,configurable:!0}})}:function(a,b){a.super_=b;var c=function(){};c.prototype=b.prototype,a.prototype=new c,a.prototype.constructor=a}},{}]},{},[])("/")});