@testrtc/watchrtc-sdk
Advanced tools
Comparing version 1.27.11 to 1.28.2
@@ -1,1 +0,2 @@ | ||
!function(e,n){"object"==typeof exports&&"object"==typeof module?module.exports=n():"function"==typeof define&&define.amd?define([],n):"object"==typeof exports?exports.watchRTC=n():e.watchRTC=n()}(self,(function(){return(()=>{"use strict";var e={607:(e,n,t)=>{n.IO=n.S1=void 0,t(699);var i=t(231);n.S1=function(e){i.initSDK(e,["","webkit","moz"])},n.IO=function(e,n){i.setUserRating(e,n)},n.default={init:n.S1,setUserRating:n.IO}},231:function(e,n,t){var i=this&&this.__assign||function(){return(i=Object.assign||function(e){for(var n,t=1,i=arguments.length;t<i;t++)for(var o in n=arguments[t])Object.prototype.hasOwnProperty.call(n,o)&&(e[o]=n[o]);return e}).apply(this,arguments)},o=this&&this.__spreadArrays||function(){for(var e=0,n=0,t=arguments.length;n<t;n++)e+=arguments[n].length;var i=Array(e),o=0;for(n=0;n<t;n++)for(var r=arguments[n],a=0,c=r.length;a<c;a++,o++)i[o]=r[a];return i};Object.defineProperty(n,"__esModule",{value:!0}),n.setUserRating=n.initSDK=void 0;var r=t(85),a=t(412),c=t(593),s=function(e,n){n=JSON.parse(JSON.stringify(n)),Object.keys(n).forEach((function(t){var i=n[t];delete i.id,e[t]&&Object.keys(i).forEach((function(o){i[o]===e[t][o]&&delete n[t][o],(0===Object.keys(i).length||1===Object.keys(i).length&&i.timestamp)&&delete n[t]}))}));var t=-1/0;return Object.keys(n).forEach((function(e){var i=n[e];i.timestamp>t&&(t=i.timestamp)})),Object.keys(n).forEach((function(e){var i=n[e];i.timestamp===t&&(i.timestamp=0)})),n.timestamp=t,n},l=function(e){return{id:e.id,tracks:e.getTracks().map((function(e){return{id:e.id,kind:e.kind,label:e.label,enabled:e.enabled,muted:e.muted,readyState:e.readyState}}))}};n.initSDK=function(e,n){if(!window.watchRTCInitialized){window.watchRTCInitialized=!0;var t=0,d=!!window.mozRTCPeerConnection,f=!!window.RTCIceGatherer,u=(!d&&window.RTCPeerConnection&&window.navigator.webkitGetUserMedia,new r.default),p=u.trace;if(n.forEach((function(n){if(window[n+"RTCPeerConnection"]&&("webkit"!==n||!f)){var r=window[n+"RTCPeerConnection"],l=function(n,l){var g,v=new r(n,l),h="PC_"+t++;v.__rtcStatsId=h;var y,m,_,w,b=e||(null==n?void 0:n.watchrtc);if(function(e){return e?e.rtcApiKey?e.rtcRoomId?!!e.rtcPeerId||(console.info.apply(console,o(c.logPrefix("error"),["config.rtcPeerId is required."])),!1):(console.info.apply(console,o(c.logPrefix("error"),["config.rtcRoomId is required."])),!1):(console.info.apply(console,o(c.logPrefix("error"),["config.rtcApiKey is required."])),!1):(console.info.apply(console,o(c.logPrefix("error"),["config is required."])),!1)}(b)){b.rtcTags&&!Array.isArray(b.rtcTags)&&(console.info.apply(console,o(c.logPrefix("info"),["config.rtcTags must be an array."])),b.rtcTags=void 0);var S=(y=b.rtcApiKey,m=b.proxyUrl||b.wsUrl,_="wss://watchrtc.testrtc.com",w=y.split(":"),-1!==y.indexOf("local")?{url:m||"ws://localhost:9101",apiKey:w[1]}:-1!==y.indexOf("staging")?{url:m||"wss://watchrtc-staging2.testrtc.com",apiKey:w[1]}:-1!==y.indexOf("production")?{url:m||_,apiKey:w[1]}:{url:m||_,apiKey:w[0]});u.connect(h,S.url+"?apiKey="+S.apiKey,(function(e){console.info.apply(console,o(c.logPrefix("info"),["Connection established. watchRTCConnectionId: "+e.connectionId+" sdkVersion:"+a.default])),p("watchrtc",h,i(i(i({},b),e),{sdkVersion:a.default})),f||(window.clearInterval(g),g=window.setInterval((function(){if("closed"===v.signalingState)return window.clearInterval(g),void setTimeout((function(){(0===u.clients.length||1===u.clients.length&&u.clients.find((function(e){return e===h})))&&(u.close(),console.info.apply(console,o(c.logPrefix("info"),["Last connection closed. watchRTCConnectionId: "+e.connectionId+" sdkVersion: "+a.default])))}),1e3);k()}),e.interval))}))}n||(n={nullConfig:!0}),((n=JSON.parse(JSON.stringify(n)))&&n.iceServers||[]).forEach((function(e){delete e.credential})),(null==n?void 0:n.watchrtc)&&delete n.watchrtc,n.browserType=d?"moz":f?"edge":"webkit",p("create",h,n),l&&p("constraints",h,l),v.addEventListener("icecandidate",(function(e){p("onicecandidate",h,e.candidate)})),v.addEventListener("addstream",(function(e){p("onaddstream",h,e.stream.id+" "+e.stream.getTracks().map((function(e){return e.kind+":"+e.id})))})),v.addEventListener("track",(function(e){p("ontrack",h,e.track.kind+":"+e.track.id+" "+e.streams.map((function(e){return"stream:"+e.id})))})),v.addEventListener("removestream",(function(e){p("onremovestream",h,e.stream.id+" "+e.stream.getTracks().map((function(e){return e.kind+":"+e.id})))})),v.addEventListener("signalingstatechange",(function(){p("onsignalingstatechange",h,v.signalingState)})),v.addEventListener("iceconnectionstatechange",(function(){p("oniceconnectionstatechange",h,v.iceConnectionState)})),v.addEventListener("icegatheringstatechange",(function(){p("onicegatheringstatechange",h,v.iceGatheringState)})),v.addEventListener("connectionstatechange",(function(){p("onconnectionstatechange",h,v.connectionState)})),v.addEventListener("negotiationneeded",(function(){p("onnegotiationneeded",h,void 0)})),v.addEventListener("datachannel",(function(e){p("ondatachannel",h,[e.channel.id,e.channel.label])}));var O={},k=function(){v.getStats(null).then((function(e){var n=function(e){if(!e.entries)return e;var n={};return e.forEach((function(e,t){n[t]=e})),n}(e),t=JSON.parse(JSON.stringify(n)),i=s(O,n);null!==(null==i?void 0:i.timestamp)&&(null==i?void 0:i.timestamp)!==-1/0&&p("getstats",h,s(O,n)),O=t}))};return f||v.addEventListener("iceconnectionstatechange",(function(){"connected"===v.iceConnectionState&&k()})),v};["createDataChannel","close"].forEach((function(e){var n=r.prototype[e];n&&(r.prototype[e]=function(){return p(e,this.__rtcStatsId,arguments),n.apply(this,arguments)})})),["addStream","removeStream"].forEach((function(e){var n=r.prototype[e];n&&(r.prototype[e]=function(){var t=arguments[0],i=t.getTracks().map((function(e){return e.kind+":"+e.id})).join(",");return p(e,this.__rtcStatsId,t.id+" "+i),n.apply(this,arguments)})})),["addTrack"].forEach((function(e){var n=r.prototype[e];n&&(r.prototype[e]=function(){var t=arguments[0],i=[].slice.call(arguments,1);return p(e,this.__rtcStatsId,t.kind+":"+t.id+" "+(i.map((function(e){return"stream:"+e.id})).join(";")||"-")),n.apply(this,arguments)})})),["removeTrack"].forEach((function(e){var n=r.prototype[e];n&&(r.prototype[e]=function(){var t=arguments[0].track;return p(e,this.__rtcStatsId,t?t.kind+":"+t.id:"null"),n.apply(this,arguments)})})),["createOffer","createAnswer"].forEach((function(e){var n=r.prototype[e];n&&(r.prototype[e]=function(){var t,i=this.__rtcStatsId,o=arguments;return 1===arguments.length&&"object"==typeof arguments[0]?t=arguments[0]:3===arguments.length&&"object"==typeof arguments[2]&&(t=arguments[2]),p(e,this.__rtcStatsId,t),n.apply(this,t?[t]:void 0).then((function(n){if(p(e+"OnSuccess",i,n),!(o.length>0&&"function"==typeof o[0]))return n;o[0].apply(null,[n])}),(function(n){if(p(e+"OnFailure",i,n.toString()),!(o.length>1&&"function"==typeof o[1]))throw n;o[1].apply(null,[n])}))})})),["setLocalDescription","setRemoteDescription","addIceCandidate"].forEach((function(e){var n=r.prototype[e];n&&(r.prototype[e]=function(){var t=this.__rtcStatsId,i=arguments;return p(e,this.__rtcStatsId,i[0]),n.apply(this,[i[0]]).then((function(){p(e+"OnSuccess",t,void 0),i.length>=2&&"function"==typeof i[1]&&i[1].apply(null,[])}),(function(n){if(p(e+"OnFailure",t,n.toString()),!(i.length>=3&&"function"==typeof i[2]))throw n;i[2].apply(null,[n])}))})})),r.generateCertificate&&Object.defineProperty(l,"generateCertificate",{get:function(){return arguments.length?r.generateCertificate.apply(null,arguments):r.generateCertificate}}),window[n+"RTCPeerConnection"]=l,window[n+"RTCPeerConnection"].prototype=r.prototype}})),n.forEach((function(e){var n=e+(e.length?"GetUserMedia":"getUserMedia");if(navigator[n]){var t=navigator[n].bind(navigator);navigator[n]=function(){p("getUserMedia",null,arguments[0]);var e=arguments[1],n=arguments[2];t(arguments[0],(function(n){p("getUserMediaOnSuccess",null,l(n)),e&&e(n)}),(function(e){p("getUserMediaOnFailure",null,e.name),n&&n(e)}))}.bind(navigator)}})),navigator.mediaDevices&&navigator.mediaDevices.getUserMedia){var g=navigator.mediaDevices.getUserMedia.bind(navigator.mediaDevices);navigator.mediaDevices.getUserMedia=function(){return p("navigator.mediaDevices.getUserMedia",null,arguments[0]),g.apply(navigator.mediaDevices,arguments).then((function(e){return p("navigator.mediaDevices.getUserMediaOnSuccess",null,l(e)),e}),(function(e){return p("navigator.mediaDevices.getUserMediaOnFailure",null,e.name),Promise.reject(e)}))}.bind(navigator.mediaDevices)}if(navigator.mediaDevices&&navigator.mediaDevices.getDisplayMedia){var v=navigator.mediaDevices.getDisplayMedia.bind(navigator.mediaDevices);navigator.mediaDevices.getDisplayMedia=function(){return p("navigator.mediaDevices.getDisplayMedia",null,arguments[0]),v.apply(navigator.mediaDevices,arguments).then((function(e){return p("navigator.mediaDevices.getDisplayMediaOnSuccess",null,l(e)),e}),(function(e){return p("navigator.mediaDevices.getDisplayMediaOnFailure",null,e.name),Promise.reject(e)}))}.bind(navigator.mediaDevices)}}},n.setUserRating=function(e,n){var t;e&&(r.default._instance&&(null===(t=r.default._instance.connection)||void 0===t?void 0:t.readyState)===WebSocket.OPEN?r.default._instance.trace("userRating","",{rate:e,rateReason:n}):console.info.apply(console,o(c.logPrefix("error"),["Please set user rating before websocket connection is closed."])))}},699:(e,n)=>{Object.defineProperty(n,"__esModule",{value:!0})},593:(e,n)=>{Object.defineProperty(n,"__esModule",{value:!0}),n.logPrefix=void 0,n.logPrefix=function(e){return void 0===e&&(e="info"),"error"===e?["%cwatchRTC %cERROR","background: gold; color: black; padding: 2px 0.5em; border-radius: 0.5em;","background: red; color: white; padding: 2px 0.5em; border-radius: 0.5em;"]:["%cwatchRTC","background: gold; color: black; padding: 2px 0.5em; border-radius: 0.5em;"]}},412:(e,n)=>{Object.defineProperty(n,"__esModule",{value:!0}),n.default="1.27.11"},85:function(e,n,t){var i=this&&this.__spreadArrays||function(){for(var e=0,n=0,t=arguments.length;n<t;n++)e+=arguments[n].length;var i=Array(e),o=0;for(n=0;n<t;n++)for(var r=arguments[n],a=0,c=r.length;a<c;a++,o++)i[o]=r[a];return i};Object.defineProperty(n,"__esModule",{value:!0});var o=t(593),r=function(){function e(){this.connection=null,this.clients=[],this.buffer=[],this.onClose=function(){},e._instance?console.info.apply(console,i(o.logPrefix("info"),["WatchRTCSocket instance already created"])):e._instance=this}return e.prototype.connect=function(n,t,r){e._instance.connection&&e._instance.connection.close(),e._instance.connection=new WebSocket(t,"2.0");var a=e._instance;e._instance.connection.onopen=function(e){a.clients.push(n)},e._instance.connection.onmessage=function(e){var n;try{var t=JSON.parse(e.data);if(t.error)return console.info.apply(console,i(o.logPrefix("error"),["\n"+t.error])),void(null===(n=null==a?void 0:a.connection)||void 0===n||n.close());r&&r(t)}catch(n){console.info.apply(console,i(o.logPrefix("error"),[e]))}},e._instance.connection.onclose=function(e){a.clients=a.clients.filter((function(e){return e!==n}))},e._instance.connection.onerror=function(e){console.info.apply(console,i(o.logPrefix("error"),["\n",e]))}},e.prototype.trace=function(){for(var n=[],t=0;t<arguments.length;t++)n[t]=arguments[t];var i=Array.prototype.slice.call(n);if(i.push(Date.now()),i[1]instanceof RTCPeerConnection&&(i[1]=i[1].__rtcStatsId),e._instance.connection)if(e._instance.connection.readyState===WebSocket.OPEN){for(;e._instance.buffer.length;){var o=JSON.stringify(e._instance.buffer.shift());e._instance.connection.send(o)}e._instance.connection.send(JSON.stringify(i))}else e._instance.connection.readyState>=WebSocket.CLOSING||e._instance.buffer.push(i);else{if(e._instance.buffer.length>1e3)return;e._instance.buffer.push(i)}},e.prototype.close=function(){e._instance.buffer=[],e._instance.connection&&(e._instance.connection.close(),e._instance.onClose())},e}();n.default=r}},n={};return function t(i){if(n[i])return n[i].exports;var o=n[i]={exports:{}};return e[i].call(o.exports,o,o.exports,t),o.exports}(607)})().default})); | ||
!function(e,n){"object"==typeof exports&&"object"==typeof module?module.exports=n():"function"==typeof define&&define.amd?define([],n):"object"==typeof exports?exports.watchRTC=n():e.watchRTC=n()}(self,(function(){return(()=>{"use strict";var e={607:(e,n,t)=>{n.IO=n.S1=void 0,t(699);var i=t(231);n.S1=function(e){i.initSDK(e,["","webkit","moz"])},n.IO=function(e,n){i.setUserRating(e,n)},n.default={init:n.S1,setUserRating:n.IO}},231:function(e,n,t){var i=this&&this.__assign||function(){return(i=Object.assign||function(e){for(var n,t=1,i=arguments.length;t<i;t++)for(var o in n=arguments[t])Object.prototype.hasOwnProperty.call(n,o)&&(e[o]=n[o]);return e}).apply(this,arguments)},o=this&&this.__spreadArrays||function(){for(var e=0,n=0,t=arguments.length;n<t;n++)e+=arguments[n].length;var i=Array(e),o=0;for(n=0;n<t;n++)for(var r=arguments[n],a=0,c=r.length;a<c;a++,o++)i[o]=r[a];return i};Object.defineProperty(n,"__esModule",{value:!0}),n.setUserRating=n.initSDK=void 0;var r=t(85),a=t(412),c=t(593),s=function(e,n){e=e||{},n=n||{},n=JSON.parse(JSON.stringify(n)),Object.keys(n).forEach((function(t){var i=n[t];delete i.id,e[t]&&Object.keys(i).forEach((function(o){i[o]===e[t][o]&&delete n[t][o],(0===Object.keys(i).length||1===Object.keys(i).length&&i.timestamp)&&delete n[t]}))}));var t=-1/0;return Object.keys(n).forEach((function(e){var i=n[e];i.timestamp>t&&(t=i.timestamp)})),Object.keys(n).forEach((function(e){var i=n[e];i.timestamp===t&&(i.timestamp=0)})),n.timestamp=t,n},l=function(e){return{id:e.id,tracks:e.getTracks().map((function(e){return{id:e.id,kind:e.kind,label:e.label,enabled:e.enabled,muted:e.muted,readyState:e.readyState}}))}};n.initSDK=function(e,n){if(!window.watchRTCInitialized){window.watchRTCInitialized=!0;var t,d=0,f=null,u=0,p=!1,g=new r.default,v=g.trace,h={},y=!!window.mozRTCPeerConnection,m=!!window.RTCIceGatherer,_=(!y&&window.RTCPeerConnection&&window.navigator.webkitGetUserMedia,function(){return Object.keys(h).filter((function(e){return h[e].validConnection})).length}),w=function(){var e,n,r,s,l,d=_();if(d>0&&!p){p=!0;var y=(l=f)?l.rtcApiKey?l.rtcRoomId?!!l.rtcPeerId||(console.info.apply(console,o(c.logPrefix("error"),["config.rtcPeerId is required."])),!1):(console.info.apply(console,o(c.logPrefix("error"),["config.rtcRoomId is required."])),!1):(console.info.apply(console,o(c.logPrefix("error"),["config.rtcApiKey is required."])),!1):(console.info.apply(console,o(c.logPrefix("error"),["config is required."])),!1),w=Object.keys(h)[d-1];if(y){f.rtcTags&&!Array.isArray(f.rtcTags)&&(console.info.apply(console,o(c.logPrefix("info"),["config.rtcTags must be an array."])),f.rtcTags=void 0);var S=(e=f.rtcApiKey,n=f.proxyUrl||f.wsUrl,r="wss://watchrtc.testrtc.com",s=e.split(":"),-1!==e.indexOf("local")?{url:n||"ws://localhost:9101",apiKey:s[1]}:-1!==e.indexOf("staging")?{url:n||"wss://watchrtc-staging2.testrtc.com",apiKey:s[1]}:-1!==e.indexOf("production")?{url:n||r,apiKey:s[1]}:{url:n||r,apiKey:s[0]});u=Date.now(),g.connect(S.url+"?apiKey="+S.apiKey+"×tamp="+Date.now(),(function(e){console.info.apply(console,o(c.logPrefix("info"),["Connection established. watchRTCConnectionId: "+e.connectionId+" sdkVersion:"+a.default])),v("watchrtc",w,i(i(i({},f),e),{sdkVersion:a.default})),m||(window.clearInterval(t),t=window.setInterval((function(){0===_()?u&&u+2e4<Date.now()&&(p=!1,window.clearInterval(t),g.close(),console.info.apply(console,o(c.logPrefix("info"),["Last connection closed. watchRTCConnectionId: "+e.connectionId+" sdkVersion: "+a.default]))):(u=Date.now(),Object.values(h).forEach((function(e){"closed"!==e.pc.signalingState&&b(e)})))}),e.interval))}),(function(){p=!1,u=0}))}}},b=function(e){var n=e.id,t=e.pc,i=e.prev;t.getStats(null).then((function(t){var o=function(e){if(!e.entries)return e;var n={};return e.forEach((function(e,t){n[t]=e})),n}(t),r=JSON.parse(JSON.stringify(o)),a=s(i,o);null!==(null==a?void 0:a.timestamp)&&(null==a?void 0:a.timestamp)!==-1/0&&v("getstats",n,s(e.prev,o)),e.prev=r}))};if(n.forEach((function(n){if(window[n+"RTCPeerConnection"]&&("webkit"!==n||!m)){var t=window[n+"RTCPeerConnection"],i=function(n,i){var o=new t(n,i),r="PC_"+d++;return o.__rtcStatsId=r,h[r]={id:r,pc:o,validConnection:!1},f=e||(null==n?void 0:n.watchrtc),n||(n={nullConfig:!0}),((n=JSON.parse(JSON.stringify(n)))&&n.iceServers||[]).forEach((function(e){delete e.credential})),(null==n?void 0:n.watchrtc)&&delete n.watchrtc,n.browserType=y?"moz":m?"edge":"webkit",v("create",r,n),i&&v("constraints",r,i),o.addEventListener("icecandidate",(function(e){v("onicecandidate",r,e.candidate)})),o.addEventListener("addstream",(function(e){v("onaddstream",r,e.stream.id+" "+e.stream.getTracks().map((function(e){return e.kind+":"+e.id})))})),o.addEventListener("track",(function(e){v("ontrack",r,e.track.kind+":"+e.track.id+" "+e.streams.map((function(e){return"stream:"+e.id})))})),o.addEventListener("removestream",(function(e){v("onremovestream",r,e.stream.id+" "+e.stream.getTracks().map((function(e){return e.kind+":"+e.id})))})),o.addEventListener("signalingstatechange",(function(){h[r]&&!h[r].validConnection&&(h[r].validConnection=!0,setTimeout(w,5e3)),v("onsignalingstatechange",r,o.signalingState)})),o.addEventListener("iceconnectionstatechange",(function(){v("oniceconnectionstatechange",r,o.iceConnectionState)})),o.addEventListener("icegatheringstatechange",(function(){v("onicegatheringstatechange",r,o.iceGatheringState)})),o.addEventListener("connectionstatechange",(function(){v("onconnectionstatechange",r,o.connectionState)})),o.addEventListener("negotiationneeded",(function(){v("onnegotiationneeded",r,void 0)})),o.addEventListener("datachannel",(function(e){v("ondatachannel",r,[e.channel.id,e.channel.label])})),m||o.addEventListener("iceconnectionstatechange",(function(){"connected"===o.iceConnectionState&&b(h[r])})),o};["createDataChannel","close"].forEach((function(e){var n=t.prototype[e];n&&(t.prototype[e]=function(){return v(e,this.__rtcStatsId,arguments),delete h[this.__rtcStatsId],n.apply(this,arguments)})})),["addStream","removeStream"].forEach((function(e){var n=t.prototype[e];n&&(t.prototype[e]=function(){var t=arguments[0],i=t.getTracks().map((function(e){return e.kind+":"+e.id})).join(",");return v(e,this.__rtcStatsId,t.id+" "+i),n.apply(this,arguments)})})),["addTrack"].forEach((function(e){var n=t.prototype[e];n&&(t.prototype[e]=function(){var t=arguments[0],i=[].slice.call(arguments,1);return v(e,this.__rtcStatsId,t.kind+":"+t.id+" "+(i.map((function(e){return"stream:"+e.id})).join(";")||"-")),n.apply(this,arguments)})})),["removeTrack"].forEach((function(e){var n=t.prototype[e];n&&(t.prototype[e]=function(){var t=arguments[0].track;return v(e,this.__rtcStatsId,t?t.kind+":"+t.id:"null"),n.apply(this,arguments)})})),["createOffer","createAnswer"].forEach((function(e){var n=t.prototype[e];n&&(t.prototype[e]=function(){var t,i=this.__rtcStatsId,o=arguments;return 1===arguments.length&&"object"==typeof arguments[0]?t=arguments[0]:3===arguments.length&&"object"==typeof arguments[2]&&(t=arguments[2]),v(e,this.__rtcStatsId,t),n.apply(this,t?[t]:void 0).then((function(n){if(v(e+"OnSuccess",i,n),!(o.length>0&&"function"==typeof o[0]))return n;o[0].apply(null,[n])}),(function(n){if(v(e+"OnFailure",i,n.toString()),!(o.length>1&&"function"==typeof o[1]))throw n;o[1].apply(null,[n])}))})})),["setLocalDescription","setRemoteDescription","addIceCandidate"].forEach((function(e){var n=t.prototype[e];n&&(t.prototype[e]=function(){var t=this.__rtcStatsId,i=arguments;return v(e,this.__rtcStatsId,i[0]),n.apply(this,[i[0]]).then((function(){v(e+"OnSuccess",t,void 0),i.length>=2&&"function"==typeof i[1]&&i[1].apply(null,[])}),(function(n){if(v(e+"OnFailure",t,n.toString()),!(i.length>=3&&"function"==typeof i[2]))throw n;i[2].apply(null,[n])}))})})),t.generateCertificate&&Object.defineProperty(i,"generateCertificate",{get:function(){return arguments.length?t.generateCertificate.apply(null,arguments):t.generateCertificate}}),window[n+"RTCPeerConnection"]=i,window[n+"RTCPeerConnection"].prototype=t.prototype}})),n.forEach((function(e){var n=e+(e.length?"GetUserMedia":"getUserMedia");if(navigator[n]){var t=navigator[n].bind(navigator);navigator[n]=function(){v("getUserMedia",null,arguments[0]);var e=arguments[1],n=arguments[2];t(arguments[0],(function(n){v("getUserMediaOnSuccess",null,l(n)),e&&e(n)}),(function(e){v("getUserMediaOnFailure",null,e.name),n&&n(e)}))}.bind(navigator)}})),navigator.mediaDevices&&navigator.mediaDevices.getUserMedia){var S=navigator.mediaDevices.getUserMedia.bind(navigator.mediaDevices);navigator.mediaDevices.getUserMedia=function(){return v("navigator.mediaDevices.getUserMedia",null,arguments[0]),S.apply(navigator.mediaDevices,arguments).then((function(e){return v("navigator.mediaDevices.getUserMediaOnSuccess",null,l(e)),e}),(function(e){return v("navigator.mediaDevices.getUserMediaOnFailure",null,e.name),Promise.reject(e)}))}.bind(navigator.mediaDevices)}if(navigator.mediaDevices&&navigator.mediaDevices.getDisplayMedia){var k=navigator.mediaDevices.getDisplayMedia.bind(navigator.mediaDevices);navigator.mediaDevices.getDisplayMedia=function(){return v("navigator.mediaDevices.getDisplayMedia",null,arguments[0]),k.apply(navigator.mediaDevices,arguments).then((function(e){return v("navigator.mediaDevices.getDisplayMediaOnSuccess",null,l(e)),e}),(function(e){return v("navigator.mediaDevices.getDisplayMediaOnFailure",null,e.name),Promise.reject(e)}))}.bind(navigator.mediaDevices)}}},n.setUserRating=function(e,n){var t;e&&(r.default._instance&&(null===(t=r.default._instance.connection)||void 0===t?void 0:t.readyState)===WebSocket.OPEN?r.default._instance.trace("userRating","",{rate:e,rateReason:n}):console.info.apply(console,o(c.logPrefix("error"),["Please set user rating before websocket connection is closed."])))}},699:(e,n)=>{Object.defineProperty(n,"__esModule",{value:!0})},593:(e,n)=>{Object.defineProperty(n,"__esModule",{value:!0}),n.logPrefix=void 0,n.logPrefix=function(e){return void 0===e&&(e="info"),"error"===e?["%cwatchRTC %cERROR","background: gold; color: black; padding: 2px 0.5em; border-radius: 0.5em;","background: red; color: white; padding: 2px 0.5em; border-radius: 0.5em;"]:["%cwatchRTC","background: gold; color: black; padding: 2px 0.5em; border-radius: 0.5em;"]}},412:(e,n)=>{Object.defineProperty(n,"__esModule",{value:!0}),n.default="1.28.2"},85:function(e,n,t){var i=this&&this.__spreadArrays||function(){for(var e=0,n=0,t=arguments.length;n<t;n++)e+=arguments[n].length;var i=Array(e),o=0;for(n=0;n<t;n++)for(var r=arguments[n],a=0,c=r.length;a<c;a++,o++)i[o]=r[a];return i};Object.defineProperty(n,"__esModule",{value:!0});var o=t(593),r=function(){function e(){this.connection=null,this.buffer=[],this.sendInterval=1,this.onClose=function(){},e._instance?console.info.apply(console,i(o.logPrefix("info"),["WatchRTCSocket instance already created"])):e._instance=this}return e.prototype.connect=function(n,t,r){e._instance.connection&&e._instance.connection.close(),e._instance.connection=new WebSocket(n,"2.0"),e._instance.connection.onopen=function(e){},e._instance.connection.onclose=function(e){};var a=e._instance;e._instance.connection.onmessage=function(n){var c;try{var s=JSON.parse(n.data);s.error?(null===(c=null==a?void 0:a.connection)||void 0===c||c.close(),a.connection=null,console.info.apply(console,i(o.logPrefix("error"),["\n"+s.error])),r(s.error)):(s.sendInterval&&(e._instance.sendInterval=s.sendInterval),t(s))}catch(e){console.info.apply(console,i(o.logPrefix("error"),[{err:e.stack}])),r(e.message)}},e._instance.connection.onerror=function(e){console.info.apply(console,i(o.logPrefix("error"),["\n",e])),r(e)}},e.prototype.trace=function(){for(var n=[],t=0;t<arguments.length;t++)n[t]=arguments[t];var i=Array.prototype.slice.call(n);if(i.push(Date.now()),i[1]instanceof RTCPeerConnection&&(i[1]=i[1].__rtcStatsId),e._instance.connection){if(e._instance.connection.readyState===WebSocket.OPEN&&(e._instance.buffer.push(i),e._instance.buffer.length>=e._instance.sendInterval)){var o=JSON.stringify(e._instance.buffer);e._instance.buffer=[],e._instance.connection.send(o)}}else{if(e._instance.buffer.length>1e3)return;e._instance.buffer.push(i)}},e.prototype.close=function(){e._instance.buffer=[],e._instance.connection&&(e._instance.connection.close(),e._instance.onClose(),e._instance.connection=null)},e}();n.default=r}},n={},t=function t(i){if(n[i])return n[i].exports;var o=n[i]={exports:{}};return e[i].call(o.exports,o,o.exports,t),o.exports}(607);return t.default})()})); | ||
//# sourceMappingURL=index.js.map |
151
lib/sdk.js
@@ -41,2 +41,4 @@ "use strict"; | ||
var deltaCompression = function (oldStats, newStats) { | ||
oldStats = oldStats || {}; | ||
newStats = newStats || {}; | ||
newStats = JSON.parse(JSON.stringify(newStats)); | ||
@@ -171,23 +173,23 @@ Object.keys(newStats).forEach(function (id) { | ||
var peerconnectioncounter = 0; | ||
var watchrtcConfig = null; | ||
var lastConnectionOpen = 0; // so we know when was the last active connection seen | ||
var getStatsInterval; | ||
var tryingToConnectSocket = false; | ||
var socket = new watchrtcsocket_1.default(); | ||
var trace = socket.trace; | ||
// Data structure for RTCPeerConnection related stuff we need | ||
var openChannels = {}; | ||
var isFirefox = !!window.mozRTCPeerConnection; | ||
var isEdge = !!window.RTCIceGatherer; | ||
var isSafari = !isFirefox && window.RTCPeerConnection && !window.navigator.webkitGetUserMedia; | ||
var socket = new watchrtcsocket_1.default(); | ||
var trace = socket.trace; | ||
prefixesToWrap.forEach(function (prefix) { | ||
if (!window[prefix + "RTCPeerConnection"]) { | ||
return; | ||
} | ||
if (prefix === "webkit" && isEdge) { | ||
// dont wrap webkitRTCPeerconnection in Edge. | ||
return; | ||
} | ||
var origPeerConnection = window[prefix + "RTCPeerConnection"]; | ||
var peerconnection = function (config, constraints) { | ||
var interval; | ||
var pc = new origPeerConnection(config, constraints); | ||
var id = "PC_" + peerconnectioncounter++; | ||
pc.__rtcStatsId = id; | ||
var watchrtcConfig = watchrtc || (config === null || config === void 0 ? void 0 : config.watchrtc); | ||
var countOfValidConnections = function () { | ||
return Object.keys(openChannels).filter(function (id) { return openChannels[id].validConnection; }).length; | ||
}; | ||
var maybeOpenWebsocketConnection = function () { | ||
var connectionCount = countOfValidConnections(); | ||
if (connectionCount > 0 && !tryingToConnectSocket) { | ||
tryingToConnectSocket = true; | ||
var canConnect = validateConfig(watchrtcConfig); | ||
// not very critical, but for consistency with trace | ||
var id_1 = Object.keys(openChannels)[connectionCount - 1]; | ||
if (canConnect) { | ||
@@ -199,24 +201,79 @@ if (watchrtcConfig.rtcTags && !Array.isArray(watchrtcConfig.rtcTags)) { | ||
var wsConnectionData = getWSConnectionData(watchrtcConfig.rtcApiKey, watchrtcConfig.proxyUrl || watchrtcConfig.wsUrl); | ||
socket.connect(id, wsConnectionData.url + "?apiKey=" + wsConnectionData.apiKey, function (data) { | ||
lastConnectionOpen = Date.now(); | ||
socket.connect(wsConnectionData.url + "?apiKey=" + wsConnectionData.apiKey + "×tamp=" + Date.now(), function (data) { | ||
console.info.apply(console, __spreadArrays(utils_1.logPrefix("info"), ["Connection established. watchRTCConnectionId: " + data.connectionId + " sdkVersion:" + version_1.default])); | ||
trace("watchrtc", id, __assign(__assign(__assign({}, watchrtcConfig), data), { sdkVersion: version_1.default })); | ||
trace("watchrtc", id_1, __assign(__assign(__assign({}, watchrtcConfig), data), { sdkVersion: version_1.default })); | ||
if (!isEdge) { | ||
window.clearInterval(interval); | ||
interval = window.setInterval(function () { | ||
if (pc.signalingState === "closed") { | ||
window.clearInterval(interval); | ||
setTimeout(function () { | ||
if (socket.clients.length === 0 || | ||
(socket.clients.length === 1 && socket.clients.find(function (x) { return x === id; }))) { | ||
socket.close(); | ||
console.info.apply(console, __spreadArrays(utils_1.logPrefix("info"), ["Last connection closed. watchRTCConnectionId: " + data.connectionId + " sdkVersion: " + version_1.default])); | ||
window.clearInterval(getStatsInterval); | ||
getStatsInterval = window.setInterval(function () { | ||
if (countOfValidConnections() === 0) { | ||
// if we don't have any connection for 20 sec we can close the socket | ||
if (lastConnectionOpen && lastConnectionOpen + 20000 < Date.now()) { | ||
tryingToConnectSocket = false; | ||
window.clearInterval(getStatsInterval); | ||
socket.close(); | ||
console.info.apply(console, __spreadArrays(utils_1.logPrefix("info"), ["Last connection closed. watchRTCConnectionId: " + data.connectionId + " sdkVersion: " + version_1.default])); | ||
} | ||
} | ||
else { | ||
lastConnectionOpen = Date.now(); | ||
Object.values(openChannels).forEach(function (pcInfo) { | ||
if (pcInfo.pc.signalingState !== "closed") { | ||
getStats(pcInfo); | ||
} | ||
}, 1000); | ||
return; | ||
}); | ||
} | ||
getStats(); | ||
}, data.interval); | ||
} | ||
}, function () { | ||
tryingToConnectSocket = false; | ||
lastConnectionOpen = 0; | ||
}); | ||
} | ||
} | ||
}; | ||
var getStats = function (pcInfo) { | ||
var id = pcInfo.id, pc = pcInfo.pc, prev = pcInfo.prev; | ||
if (standardGetstats || isFirefox || isSafari) { | ||
pc.getStats(null).then(function (res) { | ||
var now = map2obj(res); | ||
var base = JSON.parse(JSON.stringify(now)); // our new prev | ||
var data = deltaCompression(prev, now); | ||
if ((data === null || data === void 0 ? void 0 : data.timestamp) !== null && (data === null || data === void 0 ? void 0 : data.timestamp) !== -Infinity) { | ||
trace("getstats", id, deltaCompression(pcInfo.prev, now)); | ||
} | ||
pcInfo.prev = base; | ||
}); | ||
} | ||
else { | ||
pc.getStats(function (res) { | ||
var now = mangleChromeStats(pc, res); | ||
var base = JSON.parse(JSON.stringify(now)); // our new prev | ||
var data = deltaCompression(prev, now); | ||
if ((data === null || data === void 0 ? void 0 : data.timestamp) !== null && (data === null || data === void 0 ? void 0 : data.timestamp) !== -Infinity) { | ||
trace("getstats", id, deltaCompression(prev, now)); | ||
} | ||
pcInfo.prev = base; | ||
}); | ||
} | ||
}; | ||
prefixesToWrap.forEach(function (prefix) { | ||
if (!window[prefix + "RTCPeerConnection"]) { | ||
return; | ||
} | ||
if (prefix === "webkit" && isEdge) { | ||
// dont wrap webkitRTCPeerconnection in Edge. | ||
return; | ||
} | ||
var origPeerConnection = window[prefix + "RTCPeerConnection"]; | ||
var peerconnection = function (config, constraints) { | ||
var pc = new origPeerConnection(config, constraints); | ||
var id = "PC_" + peerconnectioncounter++; | ||
pc.__rtcStatsId = id; | ||
openChannels[id] = { | ||
id: id, | ||
pc: pc, | ||
validConnection: false, | ||
}; | ||
watchrtcConfig = watchrtc || (config === null || config === void 0 ? void 0 : config.watchrtc); | ||
if (!config) { | ||
@@ -275,2 +332,6 @@ config = { nullConfig: true }; | ||
pc.addEventListener("signalingstatechange", function () { | ||
if (openChannels[id] && !openChannels[id].validConnection) { | ||
openChannels[id].validConnection = true; | ||
setTimeout(maybeOpenWebsocketConnection, 5000); | ||
} | ||
trace("onsignalingstatechange", id, pc.signalingState); | ||
@@ -293,31 +354,6 @@ }); | ||
}); | ||
var prev = {}; | ||
var getStats = function () { | ||
if (standardGetstats || isFirefox || isSafari) { | ||
pc.getStats(null).then(function (res) { | ||
var now = map2obj(res); | ||
var base = JSON.parse(JSON.stringify(now)); // our new prev | ||
var data = deltaCompression(prev, now); | ||
if ((data === null || data === void 0 ? void 0 : data.timestamp) !== null && (data === null || data === void 0 ? void 0 : data.timestamp) !== -Infinity) { | ||
trace("getstats", id, deltaCompression(prev, now)); | ||
} | ||
prev = base; | ||
}); | ||
} | ||
else { | ||
pc.getStats(function (res) { | ||
var now = mangleChromeStats(pc, res); | ||
var base = JSON.parse(JSON.stringify(now)); // our new prev | ||
var data = deltaCompression(prev, now); | ||
if ((data === null || data === void 0 ? void 0 : data.timestamp) !== null && (data === null || data === void 0 ? void 0 : data.timestamp) !== -Infinity) { | ||
trace("getstats", id, deltaCompression(prev, now)); | ||
} | ||
prev = base; | ||
}); | ||
} | ||
}; | ||
if (!isEdge) { | ||
pc.addEventListener("iceconnectionstatechange", function () { | ||
if (pc.iceConnectionState === "connected") { | ||
getStats(); | ||
getStats(openChannels[id]); | ||
} | ||
@@ -333,2 +369,3 @@ }); | ||
trace(method, this.__rtcStatsId, arguments); | ||
delete openChannels[this.__rtcStatsId]; | ||
return nativeMethod.apply(this, arguments); | ||
@@ -335,0 +372,0 @@ }; |
@@ -1,2 +0,2 @@ | ||
declare const _default: "1.27.11"; | ||
declare const _default: "1.28.2"; | ||
export default _default; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.default = "1.27.10"; | ||
exports.default = "1.28.2"; |
export default class WatchRTCSocket { | ||
static _instance: WatchRTCSocket; | ||
connection: WebSocket | null; | ||
clients: string[]; | ||
private buffer; | ||
private sendInterval; | ||
private onClose; | ||
constructor(); | ||
connect(id: string, url: string, onMessage: any): void; | ||
connect(url: string, onData: (data: any) => void, onError: (error: any) => void): void; | ||
trace(...data: any[]): void; | ||
close(): void; | ||
} |
@@ -15,4 +15,4 @@ "use strict"; | ||
this.connection = null; | ||
this.clients = []; | ||
this.buffer = []; | ||
this.sendInterval = 1; | ||
this.onClose = function () { }; | ||
@@ -26,3 +26,3 @@ if (WatchRTCSocket._instance) { | ||
} | ||
WatchRTCSocket.prototype.connect = function (id, url, onMessage) { | ||
WatchRTCSocket.prototype.connect = function (url, onData, onError) { | ||
if (WatchRTCSocket._instance.connection) { | ||
@@ -32,6 +32,5 @@ WatchRTCSocket._instance.connection.close(); | ||
WatchRTCSocket._instance.connection = new WebSocket(url, PROTOCOL_VERSION); | ||
WatchRTCSocket._instance.connection.onopen = function (_e) { }; | ||
WatchRTCSocket._instance.connection.onclose = function (_e) { }; | ||
var _this = WatchRTCSocket._instance; | ||
WatchRTCSocket._instance.connection.onopen = function (_e) { | ||
_this.clients.push(id); | ||
}; | ||
WatchRTCSocket._instance.connection.onmessage = function (e) { | ||
@@ -42,21 +41,22 @@ var _a; | ||
if (data.error) { | ||
(_a = _this === null || _this === void 0 ? void 0 : _this.connection) === null || _a === void 0 ? void 0 : _a.close(); | ||
_this.connection = null; | ||
console.info.apply(console, __spreadArrays(utils_1.logPrefix("error"), ["\n" + data.error])); | ||
(_a = _this === null || _this === void 0 ? void 0 : _this.connection) === null || _a === void 0 ? void 0 : _a.close(); | ||
return; | ||
onError(data.error); | ||
} | ||
else { | ||
if (onMessage) { | ||
onMessage(data); | ||
if (data.sendInterval) { | ||
WatchRTCSocket._instance.sendInterval = data.sendInterval; | ||
} | ||
onData(data); | ||
} | ||
} | ||
catch (err) { | ||
console.info.apply(console, __spreadArrays(utils_1.logPrefix("error"), [e])); | ||
console.info.apply(console, __spreadArrays(utils_1.logPrefix("error"), [{ err: err.stack }])); | ||
onError(err.message); | ||
} | ||
}; | ||
WatchRTCSocket._instance.connection.onclose = function (_e) { | ||
_this.clients = _this.clients.filter(function (x) { return x !== id; }); | ||
}; | ||
WatchRTCSocket._instance.connection.onerror = function (e) { | ||
console.info.apply(console, __spreadArrays(utils_1.logPrefix("error"), ["\n", e])); | ||
onError(e); | ||
}; | ||
@@ -82,13 +82,9 @@ }; | ||
if (WatchRTCSocket._instance.connection.readyState === WebSocket.OPEN) { | ||
while (WatchRTCSocket._instance.buffer.length) { | ||
var line = JSON.stringify(WatchRTCSocket._instance.buffer.shift()); | ||
WatchRTCSocket._instance.connection.send(line); | ||
WatchRTCSocket._instance.buffer.push(args); | ||
if (WatchRTCSocket._instance.buffer.length >= WatchRTCSocket._instance.sendInterval) { | ||
var lines = JSON.stringify(WatchRTCSocket._instance.buffer); | ||
WatchRTCSocket._instance.buffer = []; | ||
WatchRTCSocket._instance.connection.send(lines); | ||
} | ||
WatchRTCSocket._instance.connection.send(JSON.stringify(args)); | ||
} | ||
else if (WatchRTCSocket._instance.connection.readyState >= WebSocket.CLOSING) { | ||
} | ||
else { | ||
WatchRTCSocket._instance.buffer.push(args); | ||
} | ||
}; | ||
@@ -100,2 +96,3 @@ WatchRTCSocket.prototype.close = function () { | ||
WatchRTCSocket._instance.onClose(); | ||
WatchRTCSocket._instance.connection = null; | ||
} | ||
@@ -102,0 +99,0 @@ }; |
{ | ||
"name": "@testrtc/watchrtc-sdk", | ||
"version": "1.27.11", | ||
"version": "1.28.2", | ||
"description": "gather WebRTC API traces and statistics", | ||
@@ -16,4 +16,3 @@ "main": "lib/index.js", | ||
"prepare": "yarn run build", | ||
"prepublishOnly": "yarn run lint", | ||
"npm:publish": "npm publish --access public" | ||
"prepublishOnly": "yarn run lint" | ||
}, | ||
@@ -20,0 +19,0 @@ "dependencies": {}, |
96151
15
769