UNPKG

ortc-adapter

Version:
63 lines (49 loc) 28.2 kB
/*! ortc-adapter.js 0.1.7 The following license applies to all parts of this software except as documented below. Copyright (c) 2015, Twilio, inc. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. Neither the name of Twilio nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. This software includes sdp-transform under the following license: (The MIT License) Copyright (c) 2013 Eirik Albrigtsen Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ !function(e,t){var n=t();if("function"==typeof define&&define.amd)define([],n);else if("object"==typeof module&&module.exports)module.exports=n;else for(var r in n)e[r]=n[r]}(this,function(){var e=function r(e,t,n){function i(s,o){if(!t[s]){if(!e[s]){var c="function"==typeof require&&require;if(!o&&c)return c(s,!0);if(a)return a(s,!0);var d=new Error("Cannot find module '"+s+"'");throw d.code="MODULE_NOT_FOUND",d}var u=t[s]={exports:{}};e[s][0].call(u.exports,function(t){var n=e[s][1][t];return i(n?n:t)},u,u.exports,r,e,t,n)}return t[s].exports}for(var a="function"==typeof require&&require,s=0;s<n.length;s++)i(n[s]);return i}({1:[function(e,t,n){"use strict";t.exports.RTCIceCandidate=e("./rtcicecandidate"),t.exports.RTCPeerConnection=e("./rtcpeerconnection"),t.exports.RTCSessionDescription=e("./rtcsessiondescription")},{"./rtcicecandidate":4,"./rtcpeerconnection":5,"./rtcsessiondescription":7}],2:[function(e,t,n){"use strict";function r(e,t,n,i,a,s,o,c,d,u){if(!(this instanceof r))return new r(e,t,n,i,a,s,o,c,d,u);var p=!1;e=e||"0.0.0.0",o="number"==typeof o?o:9,c="boolean"!=typeof c||c,d=d||null,u=u||null,Object.defineProperties(this,{_address:{get:function(){return e},set:function(t){e=t}},_candidates:{value:[]},_port:{get:function(){return o},set:function(e){o=e}},_rejected:{get:function(){return p},set:function(e){p=e}},_streamId:{get:function(){return d},set:function(e){d=e}},_track:{get:function(){return u},set:function(e){u=e}},_triples:{value:new Set},candidates:{enumerable:!0,get:function(){return this._candidates.slice()}},capabilities:{enumerable:!0,value:n},defaultCandidate:{enumerable:!0,get:function(){return this._candidates.length?this._candidates[0]:null}},direction:{enumerable:!0,value:i},kind:{enumerable:!0,value:a},port:{enumerable:!0,get:function(){return o}},rtcpMux:{enumerable:!0,value:c},streamId:{enumerable:!0,get:function(){return d}},track:{enumerable:!0,get:function(){return u}}}),t&&t.forEach(this.addCandidate,this)}r.prototype.addCandidate=function(e){var t=[e.ip,e.port,e.protocol].join(" ");return!this._triples.has(t)&&(this._triples.add(t),this._candidates.push(e),!0)},r.prototype.copy=function(e,t,n,i,a,s,o){return new r(this.address,t,n||this.capabilities,i||this.direction,this.kind,this.mid,a,this.rtcpMux,s,o)},r.prototype.copyAndReject=function(){var e=new r(null,this.candidates,this.capabilities,this.direction,this.kind,this.mid,null,this.rtcpMux);return e.reject()},r.prototype.reject=function(){return this.setPort(0),this},r.prototype.setAddress=function(e){return this._address=e,this},r.prototype.setPort=function(e){return this._port=e,this},t.exports=r},{}],3:[function(e,t,n){"use strict";function r(e,t){return this instanceof r?(Event.call(this,e,t),void Object.defineProperties(this,{stream:{enumerable:!0,value:t.stream}})):new r(e,t)}t.exports=r},{}],4:[function(e,t,n){"use strict";function r(e){return this instanceof r?void Object.defineProperties(this,{candidate:{enumerable:!0,value:e.candidate},sdpMLineIndex:{enumerable:!0,value:e.sdpMLineIndex}}):new r(e)}t.exports=r},{}],5:[function(e,t,n){"use strict";function r(e){if(!(this instanceof r))return new r(e);var t=s(e),n=new RTCIceGatherer(t),i=!1;n.onlocalcandidate=this._onlocalcandidate.bind(this);var a=null,o=!1,c=0,d=new RTCIceTransport,u=null;d.onicestatechange=this._onicestatechange.bind(this);var p=new RTCDtlsTransport(d);p.ondtlsstatechange=this._ondtlsstatechange.bind(this);var l="stable",f=null,m=null,h=null,g=null;Object.defineProperties(this,{_dtlsTransport:{value:p},_dtmfSenders:{value:new Map},_gatherOptions:{value:t},_iceCandidatesAdded:{get:function(){return c},set:function(e){c=e}},_iceGatherer:{value:n},_iceGatheringCompleted:{get:function(){return i},set:function(e){i=e}},_iceTransport:{value:d},_localCandidates:{value:new Set},_localDescription:{get:function(){return m},set:function(e){m=e}},_localStreams:{value:[]},_midCounters:{value:{audio:0,video:0}},_remoteCandidates:{value:new Set},_remoteDescription:{get:function(){return h},set:function(e){h=e}},_remoteStreams:{value:[]},_rtpReceivers:{value:new Map},_rtpSenders:{value:new Map},_signalingState:{get:function(){return l},set:function(e){l=e,this.onsignalingstatechange&&this.onsignalingstatechange()}},_streamIds:{value:new Map},iceConnectionState:{enumerable:!0,get:function(){return d.state}},iceGatheringState:{enumerable:!0,get:function(){return i?"gathering":"complete"}},localDescription:{enumerable:!0,get:function(){return m}},onaddstream:{enumerable:!0,get:function(){return g},set:function(e){g=e}},onicecandidate:{enumerable:!0,get:function(){return a},set:function(e){if(a=e,!o)try{n.getLocalCandidates().forEach(n.onlocalcandidate)}catch(t){}o=!0}},oniceconnectionstatechange:{enumerable:!0,get:function(){return u},set:function(e){u=e}},onsignalingstatechange:{enumerable:!0,get:function(){return f},set:function(e){f=e}},remoteDescription:{enumerable:!0,get:function(){return h}},signalingState:{enumerable:!0,get:function(){return l}}})}function i(e){return new Error("Invalid signaling state: "+e)}function a(e){return!Object.keys(e).length}function s(e){var t=(e.iceServers||[]).filter(function(e){return!e.urls.match(/^stun:/)});return{gatherPolicy:e.gatherPolicy||"all",iceServers:t}}function o(e){return new f("addstream",{stream:e})}function c(e){return new h("icecandidate",{candidate:e})}function d(e){if(!e)return{};var t=e.candidate.indexOf("candidate:"),n=e.candidate.slice(t+10).replace(/ +/g," ").split(" "),r={foundation:n[0],protocol:n[2],priority:parseInt(n[3]),ip:n[4],port:parseInt(n[5]),type:n[7],relatedAddress:null,relatedPort:0,tcpType:"active"};return"host"!==r.type&&(r.relatedAddress=n[9],r.relatedPort=parseInt(n[11])),r}function u(e){if(a(e))return null;var t=["a=candidate",e.foundation,1,e.protocol,e.priority,e.ip,e.port,e.type];return e.relatedAddress&&(t=t.concat(["raddr",e.relatedAddress,"rport",e.relatedPort])),t.push("generation 0"),new m({candidate:t.join(" "),sdpMLineIndex:0})}function p(e,t){var n=[];return e.forEach(function(e){t.forEach(function(t){e.name===t.name&&e.clockRate===t.clockRate&&e.numChannels===t.numChannels&&n.push(t)})}),n}var l=e("./mediasection"),f=e("./mediastreamevent"),m=e("./rtcicecandidate"),h=e("./rtcpeerconnectioniceevent"),g=e("./rtcsessiondescription"),v=e("sdp-transform"),y=e("./sdp-utils");r.prototype._makeMid=function(e){return e+ ++this._midCounters[e]},r.prototype._ondtlsstatechange=function(e){},r.prototype._onicestatechange=function(e){this.oniceconnectionstatechange&&this.oniceconnectionstatechange(e)},r.prototype._onlocalcandidate=function(e){if(a(e.candidate)&&(this._iceGatheringCompleted=!0),this._localCandidates.add(e.candidate),this.onicecandidate){var t=u(e.candidate);this.onicecandidate(c(t))}},r.prototype._sendRtp=function(e){var t=e.kind;return this._rtpSenders.forEach(function(n){n.track.kind===t&&n.send(e.capabilities)},this),this},r.prototype._sendAndReceiveRtp=function(e){return e.forEach(function(e){"sendrecv"!==e.direction&&"sendonly"!==e.direction||this._sendRtp(e),"sendrecv"!==e.direction&&"recvonly"!==e.direction||this._receiveRtp(e)},this),this},r.prototype._receiveRtp=function(e){var t=e.capabilities.type,n=new RTCRtpReceiver(this._dtlsTransport,t);n.receive(e.capabilities);var r=n.track;this._rtpReceivers.set(r,n);var i=new MediaStream;return i.addTrack(r),this._remoteStreams.push(i),this.onaddstream&&this.onaddstream(o(i)),this},r.prototype._startDtlsTransport=function(e){return this._dtlsTransport.start(e),this},r.prototype._startIceTransport=function(e){var t="have-local-offer"===this.signalingState?"controlling":"controlled";return this._iceTransport.start(this._iceGatherer,e,t),this},r.prototype.addIceCandidate=function(e,t,n){if(!t)return new Promise(this.addIceCandidate.bind(this,e));this._iceCandidatesAdded++;var r=d(e),i=[r.ip,r.port,r.transport].join(" ");this._remoteCandidates.has(i)||(this._remoteCandidates.add(i),this._iceTransport.addRemoteCandidate(r)),t&&t()},r.prototype.addStream=function(e){this._localStreams.push(e),e.getTracks().forEach(function(t){var n=new RTCRtpSender(t,this._dtlsTransport);this._rtpSenders.set(t,n),this._streamIds.set(t,e.id)},this)},r.prototype.close=function(){this._signalingState="closed",this._rtpReceivers.forEach(function(e){e.stop()}),this._dtlsTransport.stop(),this._iceTransport.stop()},r.prototype.createAnswer=function(e,t){if("function"!=typeof e)return new Promise(this.createAnswer.bind(this));if("have-remote-offer"!==this.signalingState)return void t(i(this.signalingState));var n=y.parseDescription(this.remoteDescription),r=this.getLocalStreams(),a={audio:[],video:[]};r.forEach(function(e){a.audio=a.audio.concat(e.getAudioTracks()),a.video=a.video.concat(e.getVideoTracks())});var s=n.mediaSections.map(function(e){var t,n,r=e.kind,i=e.direction,s=e.capabilities,o=RTCRtpSender.getCapabilities(r),c=p(s.codecs,o.codecs),d={codecs:c},u=d;if(!c.length)return e.copyAndReject();"inactive"===i||"recvonly"===i&&!a[r].length?t="inactive":"recvonly"===i?(n=a[r].shift(),t="sendonly"):"sendrecv"===i?(n=a[r].shift(),t=n?"sendrecv":"recvonly"):t="recvonly";var l=this._streamIds.get(n),f=e.copy(null,null,u,t,null,l,n);return f},this);s.forEach(function(e){this._localCandidates.forEach(e.addCandidate,e)},this);var o=y.makeInitialSDPBlob();y.addMediaSectionsToSDPBlob(o,s),y.addIceParametersToSDPBlob(o,this._iceGatherer.getLocalParameters()),y.addDtlsParametersToSDPBlob(o,this._dtlsTransport.getLocalParameters());var c=new g({sdp:v.write(o),type:"answer"});e(c)},r.prototype.createDTMFSender=function(e){if(!this._dtmfSenders.has(e)){var t=this._rtpSenders.get(e),n=new RTCDtmfSender(t);this._dtmfSenders.set(e,n)}return this._dtmfSenders.get(e)},r.prototype.createOffer=function(e,t,n){if("function"!=typeof e)return new Promise(function(t,n){this.createOffer(t,n,e)}.bind(this));var r={audio:null,video:null},i={audio:0,video:0};n=n||{},["optional","mandatory"].forEach(function(e){e in n&&("OfferToReceiveAudio"in n[e]&&(r.audio=Number(n[e].OfferToReceiveAudio)),"OfferToReceiveVideo"in n[e]&&(r.video=Number(n[e].OfferToReceiveVideo)))});var a=[],s={audio:r.audio,video:r.video},o=this.getLocalStreams();o.forEach(function(e){var t=e.getAudioTracks(),n=e.getVideoTracks();i.audio+=t.length,i.video+=n.length;var r=t.concat(n);r.forEach(function(t){var n,r=t.kind,i=RTCRtpSender.getCapabilities(r),o=this._makeMid(r);null===s.audio?n="sendrecv":s[r]?(s[r]--,n="sendrecv"):n="sendonly";var c=new l(null,null,i,n,r,o,null,null,e.id,t);a.push(c)},this)},this),["audio","video"].forEach(function(e){var t=Math.max(r[e]-i[e],0);if(t)for(var n,s,o=RTCRtpSender.getCapabilities(e),c="recvonly";t--;)n=this._makeMid(e),s=new l(null,null,o,c,e,n),a.push(s)},this),a.forEach(function(e){this._localCandidates.forEach(e.addCandidate,e)},this);var c=y.makeInitialSDPBlob();y.addMediaSectionsToSDPBlob(c,a),y.addIceParametersToSDPBlob(c,this._iceGatherer.getLocalParameters()),y.addDtlsParametersToSDPBlob(c,this._dtlsTransport.getLocalParameters());var d=new g({sdp:v.write(c),type:"offer"});e(d)},r.prototype.getLocalStreams=function(){return this._localStreams.slice()},r.prototype.getRemoteStreams=function(){return this._remoteStreams.slice()},r.prototype.setLocalDescription=function(e,t,n){if(!t)return new Promise(this.setLocalDescription.bind(this,e));var r;switch(this.signalingState){case"stable":r="have-local-offer";break;case"have-remote-offer":r="stable";break;default:return void n(i(this.signalingState))}var a=y.parseDescription(e);"have-remote-offer"===this.signalingState&&this._sendAndReceiveRtp(a.mediaSections),this._localDescription=e,this._signalingState=r,t()},r.prototype.setRemoteDescription=function(e,t,n){if(!t)return new Promise(this.setRemoteDescription.bind(this,e));var r;switch(this.signalingState){case"stable":r="have-remote-offer";break;case"have-local-offer":r="stable";break;default:return void n(i(this.signalingState))}var a=y.parseDescription(e);a.mediaSections.forEach(function(e){e.candidates.forEach(this._iceTransport.addRemoteCandidate,this._iceTransport)},this),this._startIceTransport(a.iceParameters[0]),this._startDtlsTransport(a.dtlsParameters[0]),"have-local-offer"===this.signalingState&&this._sendAndReceiveRtp(a.mediaSections),this._remoteDescription=e,this._signalingState=r,t()},t.exports=r},{"./mediasection":2,"./mediastreamevent":3,"./rtcicecandidate":4,"./rtcpeerconnectioniceevent":6,"./rtcsessiondescription":7,"./sdp-utils":8,"sdp-transform":10}],6:[function(e,t,n){"use strict";function r(e,t){return this instanceof r?(Event.call(this,e,t),void Object.defineProperties(this,{candidate:{enumerable:!0,value:t.candidate}})):new r(e,t)}t.exports=r},{}],7:[function(e,t,n){"use strict";function r(e){return this instanceof r?void Object.defineProperties(this,{sdp:{enumerable:!0,value:e.sdp},type:{enumerable:!0,value:e.type}}):new r(e)}t.exports=r},{}],8:[function(e,t,n){"use strict";function r(e,t,n){return e=e||{},e.candidates=e.candidates||[],t?(t.forEach(function(t){if(!t.foundation)return void(e.endOfCandidates="end-of-candidates");var r={foundation:t.foundation,transport:t.protocol,priority:t.priority,ip:t.ip,port:t.port,type:t.type,generation:0};if(t.relatedAddress&&(r.raddr=t.relatedAddress,r.rport=t.relatedPort),"number"==typeof n)return r.component=n,void e.candidates.push(r);r.component=1,e.candidates.push(r);var i={};for(var a in r)i[a]=r[a];i.component=2,e.candidates.push(i)}),e):e}function i(e,t,n){return e=e||{},e.media?(e.media.forEach(function(e){r(e,t,n)}),e):e}function a(e,t,n){return e=e||{},i(e,t,n),e}function s(e,t){return e=e||{},c(e,t),e}function o(e,t){e=e||{};var n=t.fingerprints;return n.length&&(e.fingerprint={type:n[0].algorithm,hash:n[0].value}),e}function c(e,t){return e=e||{},e.media?(e.media.forEach(function(e){o(e,t)}),e):e}function d(e,t){return e=e||{},u(e,t),e}function u(e,t){return e=e||{},e.media?(e.media.forEach(function(e){p(e,t)}),e):e}function p(e,t){return e=e||{},e.iceUfrag=t.usernameFragment,e.icePwd=t.password,e}function l(e,t){var n=t.streamId;n&&(e.msidSemantic=e.msidSemantic||{semantic:"WMS",token:[]},e.msidSemantic.token.push(n));var i=t.mid;if(i){e.groups=e.groups||[];var a=!1;e.groups.forEach(function(e){"BUNDLE"===e.type&&(e.mids.push(i),a=!0)}),a||e.groups.push({type:"BUNDLE",mids:[i]})}var s=[],o=[],c=[];t.capabilities.codecs.forEach(function(e){var t=e.preferredPayloadType;s.push(t);var n={payload:t,codec:e.name,rate:e.clockRate};switch(e.numChannels>1&&(n.encoding=e.numChannels),o.push(n),e.name){case"telephone-event":e.parameters&&e.parameters.events&&c.push({payload:t,config:e.parameters.events})}});var d=[];if(n&&t.track){var u=Math.floor(4294967296*Math.random()),p=g(),l=t.track.id;d=d.concat([{id:u,attribute:"cname",value:p},{id:u,attribute:"msid",value:t.streamId+" "+l},{id:u,attribute:"mslabel",value:l},{id:u,attribute:"label",value:l}])}var f=t.defaultCandidate,m={rtp:o,fmtp:c,type:t.kind,port:f?f.port:9,payloads:s.join(" "),protocol:"RTP/SAVPF",direction:t.direction,connection:{version:4,ip:f?f.ip:"0.0.0.0"},rtcp:{port:f?f.port:9,netType:"IN",ipVer:4,address:f?f.ip:"0.0.0.0"},ssrcs:d};return i&&(m.mid=i),t.rtcpMux&&(m.rtcpMux="rtcp-mux"),r(m,t.candidates),e.media.push(m),e}function f(e,t){return t.forEach(l.bind(null,e)),e}function m(e){return e=e||Math.floor(4294967296*Math.random()),{version:0,origin:{username:"-",sessionId:e,sessionVersion:0,netType:"IN",ipVer:4,address:"127.0.0.1"},name:"-",timing:{start:0,stop:0},connection:{version:4,ip:"0.0.0.0"},media:[]}}function h(e){var t=y.parse(e.sdp),n=[],r=[],i=[],a=[],s=[t];return t.media&&(s=s.concat(t.media)),s.forEach(function(e){if(e.iceUfrag&&e.icePwd&&e.fingerprint&&(n.push({usernameFragment:e.iceUfrag,password:e.icePwd}),r.push({fingerprints:[{algorithm:e.fingerprint.type,value:e.fingerprint.hash}]})),e.rtp){if("video"===e.type)return;var t,s,o,c=e.connection?e.connection.ip:null,d=e.direction,u=e.type,p=e.mid,l=e.port||null,f="rtcp-mux"===e.rtcpMux;(e.ssrcs||[]).forEach(function(e){switch(e.attribute){case"cname":s=e.id,t=e.value;break;case"label":case"mslabel":s=e.id;break;case"msid":s=e.id,o=e.value.split(" ")[0]}});var m={type:u,muxId:p,codecs:e.rtp.map(function(e){var t={name:e.codec,payloadType:parseInt(e.payload),clockRate:parseInt(e.rate),numChannels:e.encoding||1,rtcpFeedback:[],parameters:{}};switch(e.codec){case"telephone-event":t.parameters.events="0-16"}return t}),headerExtensions:[],encodings:e.rtp.map(function(e){return{ssrc:s,codecPayloadType:parseInt(e.payload),active:!0}}),rtcp:{ssrc:s,cname:t,mux:f}},h=new v(c,i,m,d,u,p,l,f,o);(e.candidates||[]).forEach(function(e){var t={foundation:String(e.foundation),protocol:e.transport,priority:e.priority,ip:e.ip,port:e.port,type:e.type,relatedAddress:e.raddr,relatedPort:e.rport};i.push(t),h.addCandidate(t)}),"end-of-candidates"===e.endOfCandidates&&h.addCandidate({}),a.push(h)}}),{iceParameters:n,dtlsParameters:r,mediaSections:a}}function g(){for(var e="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+/".split(""),t=16,n="";t--;)n+=e[Math.floor(Math.random()*e.length)];return n}var v=e("./mediasection"),y=e("sdp-transform");t.exports.addCandidatesToSDPBlob=a,t.exports.addDtlsParametersToSDPBlob=s,t.exports.addIceParametersToSDPBlob=d,t.exports.addMediaSectionsToSDPBlob=f,t.exports.makeInitialSDPBlob=m,t.exports.parseDescription=h},{"./mediasection":2,"sdp-transform":10}],9:[function(e,t,n){var r=t.exports={v:[{name:"version",reg:/^(\d*)$/}],o:[{name:"origin",reg:/^(\S*) (\d*) (\d*) (\S*) IP(\d) (\S*)/,names:["username","sessionId","sessionVersion","netType","ipVer","address"],format:"%s %s %d %s IP%d %s"}],s:[{name:"name"}],i:[{name:"description"}],u:[{name:"uri"}],e:[{name:"email"}],p:[{name:"phone"}],z:[{name:"timezones"}],r:[{name:"repeats"}],t:[{name:"timing",reg:/^(\d*) (\d*)/,names:["start","stop"],format:"%d %d"}],c:[{name:"connection",reg:/^IN IP(\d) (\S*)/,names:["version","ip"],format:"IN IP%d %s"}],b:[{push:"bandwidth",reg:/^(TIAS|AS|CT|RR|RS):(\d*)/,names:["type","limit"],format:"%s:%s"}],m:[{reg:/^(\w*) (\d*) ([\w\/]*)(?: (.*))?/,names:["type","port","protocol","payloads"],format:"%s %d %s %s"}],a:[{push:"rtp",reg:/^rtpmap:(\d*) ([\w\-\.]*)(?:\s*\/(\d*)(?:\s*\/(\S*))?)?/,names:["payload","codec","rate","encoding"],format:function(e){return e.encoding?"rtpmap:%d %s/%s/%s":e.rate?"rtpmap:%d %s/%s":"rtpmap:%d %s"}},{push:"fmtp",reg:/^fmtp:(\d*) ([\S| ]*)/,names:["payload","config"],format:"fmtp:%d %s"},{name:"control",reg:/^control:(.*)/,format:"control:%s"},{name:"rtcp",reg:/^rtcp:(\d*)(?: (\S*) IP(\d) (\S*))?/,names:["port","netType","ipVer","address"],format:function(e){return null!=e.address?"rtcp:%d %s IP%d %s":"rtcp:%d"}},{push:"rtcpFbTrrInt",reg:/^rtcp-fb:(\*|\d*) trr-int (\d*)/,names:["payload","value"],format:"rtcp-fb:%d trr-int %d"},{push:"rtcpFb",reg:/^rtcp-fb:(\*|\d*) ([\w-_]*)(?: ([\w-_]*))?/,names:["payload","type","subtype"],format:function(e){return null!=e.subtype?"rtcp-fb:%s %s %s":"rtcp-fb:%s %s"}},{push:"ext",reg:/^extmap:([\w_\/]*) (\S*)(?: (\S*))?/,names:["value","uri","config"],format:function(e){return null!=e.config?"extmap:%s %s %s":"extmap:%s %s"}},{push:"crypto",reg:/^crypto:(\d*) ([\w_]*) (\S*)(?: (\S*))?/,names:["id","suite","config","sessionConfig"],format:function(e){return null!=e.sessionConfig?"crypto:%d %s %s %s":"crypto:%d %s %s"}},{name:"setup",reg:/^setup:(\w*)/,format:"setup:%s"},{name:"mid",reg:/^mid:([^\s]*)/,format:"mid:%s"},{name:"msid",reg:/^msid:(.*)/,format:"msid:%s"},{name:"ptime",reg:/^ptime:(\d*)/,format:"ptime:%d"},{name:"maxptime",reg:/^maxptime:(\d*)/,format:"maxptime:%d"},{name:"direction",reg:/^(sendrecv|recvonly|sendonly|inactive)/},{name:"icelite",reg:/^(ice-lite)/},{name:"iceUfrag",reg:/^ice-ufrag:(\S*)/,format:"ice-ufrag:%s"},{name:"icePwd",reg:/^ice-pwd:(\S*)/,format:"ice-pwd:%s"},{name:"fingerprint",reg:/^fingerprint:(\S*) (\S*)/,names:["type","hash"],format:"fingerprint:%s %s"},{push:"candidates",reg:/^candidate:(\S*) (\d*) (\S*) (\d*) (\S*) (\d*) typ (\S*)(?: raddr (\S*) rport (\d*))?(?: tcptype (\S*))?(?: generation (\d*))?(?: network-id (\d*))?(?: network-cost (\d*))?/,names:["foundation","component","transport","priority","ip","port","type","raddr","rport","tcptype","generation","network-id","network-cost"],format:function(e){var t="candidate:%s %d %s %d %s %d typ %s";return t+=null!=e.raddr?" raddr %s rport %d":"%v%v",t+=null!=e.tcptype?" tcptype %s":"%v",null!=e.generation&&(t+=" generation %d"),t+=null!=e["network-id"]?" network-id %d":"%v",t+=null!=e["network-cost"]?" network-cost %d":"%v"}},{name:"endOfCandidates",reg:/^(end-of-candidates)/},{name:"remoteCandidates",reg:/^remote-candidates:(.*)/,format:"remote-candidates:%s"},{name:"iceOptions",reg:/^ice-options:(\S*)/,format:"ice-options:%s"},{push:"ssrcs",reg:/^ssrc:(\d*) ([\w_]*)(?::(.*))?/,names:["id","attribute","value"],format:function(e){var t="ssrc:%d";return null!=e.attribute&&(t+=" %s",null!=e.value&&(t+=":%s")),t}},{push:"ssrcGroups",reg:/^ssrc-group:(\w*) (.*)/,names:["semantics","ssrcs"],format:"ssrc-group:%s %s"},{name:"msidSemantic",reg:/^msid-semantic:\s?(\w*) (\S*)/,names:["semantic","token"],format:"msid-semantic: %s %s"},{push:"groups",reg:/^group:(\w*) (.*)/,names:["type","mids"],format:"group:%s %s"},{name:"rtcpMux",reg:/^(rtcp-mux)/},{name:"rtcpRsize",reg:/^(rtcp-rsize)/},{name:"sctpmap",reg:/^sctpmap:([\w_\/]*) (\S*)(?: (\S*))?/,names:["sctpmapNumber","app","maxMessageSize"],format:function(e){return null!=e.maxMessageSize?"sctpmap:%s %s %s":"sctpmap:%s %s"}},{push:"invalid",names:["value"]}]};Object.keys(r).forEach(function(e){var t=r[e];t.forEach(function(e){e.reg||(e.reg=/(.*)/),e.format||(e.format="%s")})})},{}],10:[function(e,t,n){var r=e("./parser"),i=e("./writer");n.write=i,n.parse=r.parse,n.parseFmtpConfig=r.parseFmtpConfig,n.parsePayloads=r.parsePayloads,n.parseRemoteCandidates=r.parseRemoteCandidates},{"./parser":11,"./writer":12}],11:[function(e,t,n){var r=function(e){return String(Number(e))===e?Number(e):e},i=function(e,t,n,i){if(i&&!n)t[i]=r(e[1]);else for(var a=0;a<n.length;a+=1)null!=e[a+1]&&(t[n[a]]=r(e[a+1]))},a=function(e,t,n){var r=e.name&&e.names;e.push&&!t[e.push]?t[e.push]=[]:r&&!t[e.name]&&(t[e.name]={});var a=e.push?{}:r?t[e.name]:t;i(n.match(e.reg),a,e.names,e.name),e.push&&t[e.push].push(a)},s=e("./grammar"),o=RegExp.prototype.test.bind(/^([a-z])=(.*)/);n.parse=function(e){var t={},n=[],r=t;return e.split(/(\r\n|\r|\n)/).filter(o).forEach(function(e){var t=e[0],i=e.slice(2);"m"===t&&(n.push({rtp:[],fmtp:[]}),r=n[n.length-1]);for(var o=0;o<(s[t]||[]).length;o+=1){var c=s[t][o];if(c.reg.test(i))return a(c,r,i)}}),t.media=n,t};var c=function(e,t){var n=t.split(/=(.+)/,2);return 2===n.length&&(e[n[0]]=r(n[1])),e};n.parseFmtpConfig=function(e){return e.split(/\;\s?/).reduce(c,{})},n.parsePayloads=function(e){return e.split(" ").map(Number)},n.parseRemoteCandidates=function(e){for(var t=[],n=e.split(" ").map(r),i=0;i<n.length;i+=3)t.push({component:n[i],ip:n[i+1],port:n[i+2]});return t}},{"./grammar":9}],12:[function(e,t,n){var r=e("./grammar"),i=/%[sdv%]/g,a=function(e){var t=1,n=arguments,r=n.length;return e.replace(i,function(e){if(t>=r)return e;var i=n[t];switch(t+=1,e){case"%%":return"%";case"%s":return String(i);case"%d":return Number(i);case"%v":return""}})},s=function(e,t,n){var r=t.format instanceof Function?t.format(t.push?n:n[t.name]):t.format,i=[e+"="+r];if(t.names)for(var s=0;s<t.names.length;s+=1){var o=t.names[s];t.name?i.push(n[t.name][o]):i.push(n[t.names[s]])}else i.push(n[t.name]);return a.apply(null,i)},o=["v","o","s","i","u","e","p","c","b","t","r","z","a"],c=["i","c","b","a"];t.exports=function(e,t){t=t||{},null==e.version&&(e.version=0),null==e.name&&(e.name=" "),e.media.forEach(function(e){null==e.payloads&&(e.payloads="")});var n=t.outerOrder||o,i=t.innerOrder||c,a=[];return n.forEach(function(t){r[t].forEach(function(n){n.name in e&&null!=e[n.name]?a.push(s(t,n,e)):n.push in e&&null!=e[n.push]&&e[n.push].forEach(function(e){a.push(s(t,n,e))})})}),e.media.forEach(function(e){a.push(s("m",r.m[0],e)),i.forEach(function(t){r[t].forEach(function(n){n.name in e&&null!=e[n.name]?a.push(s(t,n,e)):n.push in e&&null!=e[n.push]&&e[n.push].forEach(function(e){a.push(s(t,n,e))})})})}),a.join("\r\n")+"\r\n"}},{"./grammar":9}]},{},[1]),t=e(1),n={};return"undefined"!=typeof window&&window.navigator?navigator.mozGetUserMedia&&window.mozRTCPeerConnection?(console.log("This appears to be Firefox; using moz-prefixed WebRTC APIs."),n.RTCIceCandidate=window.RTCIceCandidate||mozRTCIceCandidate,n.RTCPeerConnection=mozRTCPeerConnection,n.RTCSessionDescription=window.RTCSessionDescription||mozRTCSessionDescription):navigator.webkitGetUserMedia&&window.chrome?(console.log("This appears to be Chrome; using webkit-prefixed WebRTC APIs."),n.RTCIceCandidate=RTCIceCandidate,n.RTCPeerConnection=webkitRTCPeerConnection,n.RTCSessionDescription=RTCSessionDescription):"undefined"!=typeof RTCRtpSender&&(console.log("This appears to be an ORTC-compatible browser; using ORTC to WebRTC adapter."),n.RTCIceCandidate=t.RTCIceCandidate,n.RTCPeerConnection=t.RTCPeerConnection,n.RTCSessionDescription=t.RTCSessionDescription):console.log("This does not appear to be a browser; WebRTC APIs are unavailable."),n});