UNPKG

@salutejs/jazz-sdk-electron-plugins

Version:

Jazz SDK electron plugins

2 lines (1 loc) 16.2 kB
"use strict";var e=require("ditox"),t=require("@salutejs/jazz-sdk-web"),r=require("rx-effects"),n=require("rxjs"),o=e.token({key:"JAZZ_SDK_CONTEXT_TOKEN"}),a=e.token({key:"JAZZ_SDK_DESKTOP_CAPTURER_TOKEN"});function i(e,t,r,n,o,a,i){try{var c=e[a](i),s=c.value}catch(e){return void r(e)}c.done?t(s):Promise.resolve(s).then(n,o)}function c(e){return function(){var t=this,r=arguments;return new Promise((function(n,o){var a=e.apply(t,r);function c(e){i(a,n,o,c,s,"next",e)}function s(e){i(a,n,o,c,s,"throw",e)}c(void 0)}))}}function s(e){return s="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},s(e)}var u,f=Object.prototype,p=f.hasOwnProperty,l="function"==typeof Symbol?Symbol:{},h=l.iterator||"@@iterator",d=l.asyncIterator||"@@asyncIterator",y=l.toStringTag||"@@toStringTag";function v(e,t,r,n){var o=t&&t.prototype instanceof k?t:k,a=Object.create(o.prototype),i=new $(n||[]);return a._invoke=function(e,t,r){var n=m;return function(o,a){if(n===x)throw new Error("Generator is already running");if(n===b){if("throw"===o)throw a;return q()}for(r.method=o,r.arg=a;;){var i=r.delegate;if(i){var c=T(i,r);if(c){if(c===S)continue;return c}}if("next"===r.method)r.sent=r._sent=r.arg;else if("throw"===r.method){if(n===m)throw n=b,r.arg;r.dispatchException(r.arg)}else"return"===r.method&&r.abrupt("return",r.arg);n=x;var s=g(e,t,r);if("normal"===s.type){if(n=r.done?b:w,s.arg===S)continue;return{value:s.arg,done:r.done}}"throw"===s.type&&(n=b,r.method="throw",r.arg=s.arg)}}}(e,r,i),a}function g(e,t,r){try{return{type:"normal",arg:e.call(t,r)}}catch(e){return{type:"throw",arg:e}}}var m="suspendedStart",w="suspendedYield",x="executing",b="completed",S={};function k(){}function E(){}function _(){}var A={};A[h]=function(){return this};var L=Object.getPrototypeOf,P=L&&L(L(K([])));P&&P!==f&&p.call(P,h)&&(A=P);var M=_.prototype=k.prototype=Object.create(A);function O(e){["next","throw","return"].forEach((function(t){e[t]=function(e){return this._invoke(t,e)}}))}function D(e){var t="function"==typeof e&&e.constructor;return!!t&&(t===E||"GeneratorFunction"===(t.displayName||t.name))}function C(e,t){function r(n,o,a,i){var c=g(e[n],e,o);if("throw"!==c.type){var u=c.arg,f=u.value;return f&&"object"===s(f)&&p.call(f,"__await")?t.resolve(f.__await).then((function(e){r("next",e,a,i)}),(function(e){r("throw",e,a,i)})):t.resolve(f).then((function(e){u.value=e,a(u)}),(function(e){return r("throw",e,a,i)}))}i(c.arg)}var n;this._invoke=function(e,o){function a(){return new t((function(t,n){r(e,o,t,n)}))}return n=n?n.then(a,a):a()}}function T(e,t){var r=e.iterator[t.method];if(r===u){if(t.delegate=null,"throw"===t.method){if(e.iterator.return&&(t.method="return",t.arg=u,T(e,t),"throw"===t.method))return S;t.method="throw",t.arg=new TypeError("The iterator does not provide a 'throw' method")}return S}var n=g(r,e.iterator,t.arg);if("throw"===n.type)return t.method="throw",t.arg=n.arg,t.delegate=null,S;var o=n.arg;return o?o.done?(t[e.resultName]=o.value,t.next=e.nextLoc,"return"!==t.method&&(t.method="next",t.arg=u),t.delegate=null,S):o:(t.method="throw",t.arg=new TypeError("iterator result is not an object"),t.delegate=null,S)}function j(e){var t={tryLoc:e[0]};1 in e&&(t.catchLoc=e[1]),2 in e&&(t.finallyLoc=e[2],t.afterLoc=e[3]),this.tryEntries.push(t)}function N(e){var t=e.completion||{};t.type="normal",delete t.arg,e.completion=t}function $(e){this.tryEntries=[{tryLoc:"root"}],e.forEach(j,this),this.reset(!0)}function K(e){if(e){var t=e[h];if(t)return t.call(e);if("function"==typeof e.next)return e;if(!isNaN(e.length)){var r=-1,n=function t(){for(;++r<e.length;)if(p.call(e,r))return t.value=e[r],t.done=!1,t;return t.value=u,t.done=!0,t};return n.next=n}}return{next:q}}function q(){return{value:u,done:!0}}E.prototype=M.constructor=_,_.constructor=E,_[y]=E.displayName="GeneratorFunction",O(C.prototype),C.prototype[d]=function(){return this},O(M),M[y]="Generator",M[h]=function(){return this},M.toString=function(){return"[object Generator]"},$.prototype={constructor:$,reset:function(e){if(this.prev=0,this.next=0,this.sent=this._sent=u,this.done=!1,this.delegate=null,this.method="next",this.arg=u,this.tryEntries.forEach(N),!e)for(var t in this)"t"===t.charAt(0)&&p.call(this,t)&&!isNaN(+t.slice(1))&&(this[t]=u)},stop:function(){this.done=!0;var e=this.tryEntries[0].completion;if("throw"===e.type)throw e.arg;return this.rval},dispatchException:function(e){if(this.done)throw e;var t=this;function r(r,n){return a.type="throw",a.arg=e,t.next=r,n&&(t.method="next",t.arg=u),!!n}for(var n=this.tryEntries.length-1;n>=0;--n){var o=this.tryEntries[n],a=o.completion;if("root"===o.tryLoc)return r("end");if(o.tryLoc<=this.prev){var i=p.call(o,"catchLoc"),c=p.call(o,"finallyLoc");if(i&&c){if(this.prev<o.catchLoc)return r(o.catchLoc,!0);if(this.prev<o.finallyLoc)return r(o.finallyLoc)}else if(i){if(this.prev<o.catchLoc)return r(o.catchLoc,!0)}else{if(!c)throw new Error("try statement without catch or finally");if(this.prev<o.finallyLoc)return r(o.finallyLoc)}}}},abrupt:function(e,t){for(var r=this.tryEntries.length-1;r>=0;--r){var n=this.tryEntries[r];if(n.tryLoc<=this.prev&&p.call(n,"finallyLoc")&&this.prev<n.finallyLoc){var o=n;break}}o&&("break"===e||"continue"===e)&&o.tryLoc<=t&&t<=o.finallyLoc&&(o=null);var a=o?o.completion:{};return a.type=e,a.arg=t,o?(this.method="next",this.next=o.finallyLoc,S):this.complete(a)},complete:function(e,t){if("throw"===e.type)throw e.arg;return"break"===e.type||"continue"===e.type?this.next=e.arg:"return"===e.type?(this.rval=this.arg=e.arg,this.method="return",this.next="end"):"normal"===e.type&&t&&(this.next=t),S},finish:function(e){for(var t=this.tryEntries.length-1;t>=0;--t){var r=this.tryEntries[t];if(r.finallyLoc===e)return this.complete(r.completion,r.afterLoc),N(r),S}},catch:function(e){for(var t=this.tryEntries.length-1;t>=0;--t){var r=this.tryEntries[t];if(r.tryLoc===e){var n=r.completion;if("throw"===n.type){var o=n.arg;N(r)}return o}}throw new Error("illegal catch attempt")},delegateYield:function(e,t,r){return this.delegate={iterator:K(e),resultName:t,nextLoc:r},"next"===this.method&&(this.arg=u),S}};var F={wrap:v,isGeneratorFunction:D,AsyncIterator:C,mark:function(e){return Object.setPrototypeOf?Object.setPrototypeOf(e,_):(e.__proto__=_,y in e||(e[y]="GeneratorFunction")),e.prototype=Object.create(M),e},awrap:function(e){return{__await:e}},async:function(e,t,r,n,o){void 0===o&&(o=Promise);var a=new C(v(e,t,r,n),o);return D(t)?a:a.next().then((function(e){return e.done?e.value:a.next()}))},keys:function(e){var t=[];for(var r in e)t.push(r);return t.reverse(),function r(){for(;t.length;){var n=t.pop();if(n in e)return r.value=n,r.done=!1,r}return r.done=!0,r}},values:K};function R(e,t){var r="undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(!r){if(Array.isArray(e)||(r=function(e,t){if(e){if("string"==typeof e)return z(e,t);var r={}.toString.call(e).slice(8,-1);return"Object"===r&&e.constructor&&(r=e.constructor.name),"Map"===r||"Set"===r?Array.from(e):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?z(e,t):void 0}}(e))||t){r&&(e=r);var n=0,o=function(){};return{s:o,n:function(){return n>=e.length?{done:!0}:{done:!1,value:e[n++]}},e:function(e){throw e},f:o}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var a,i=!0,c=!1;return{s:function(){r=r.call(e)},n:function(){var e=r.next();return i=e.done,e},e:function(e){c=!0,a=e},f:function(){try{i||null==r.return||r.return()}finally{if(c)throw a}}}}function z(e,t){(null==t||t>e.length)&&(t=e.length);for(var r=0,n=Array(t);r<t;r++)n[r]=e[r];return n}function Z(e){switch(e){case"audio":return"microphone";case"video":return"camera";case"displayScreen":return"screen";default:return}}function G(){return(G=c(F.mark((function e(){return F.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.abrupt("return",t.createModule({name:"DesktopCapturerPlugin",imports:{sdkContext:o},factory:I,exports:{desktopCapturer:a}}));case 1:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function I(e){var o=e.deps.sdkContext,a=o.logger,i=o.localDevices,s=a.getLogger(e.name),u=r.createScope(),f=function(){var e=window["jazz-sdk-desktop-capturer-bridge"];if(void 0===e)throw new Error("Failed connect to the preload layer. Please setup @salutejs/jazz-sdk-electron-plugins/preload/desktopCapturerPlugin to preload layer of electron");return e}().ipcMain,p=u.createController((function(){return function(e){var n=e.ipcMain,o=e.logger,a=r.createScope();function i(e){return s.apply(this,arguments)}function s(){return(s=c(F.mark((function e(t){var r,a;return F.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return o.debug("Request media access status for",t),e.next=3,n.getMediaAccessStatus(t);case 3:return r=e.sent,a=r.status,o.debug("Response media access status is",a),e.abrupt("return",a);case 7:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function u(){return(u=c(F.mark((function e(t){return F.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return o.debug("Request openSystemSettings",t),e.next=3,n.openSystemSettings(t);case 3:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function f(e){return p.apply(this,arguments)}function p(){return(p=c(F.mark((function e(t){var r;return F.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,n.askForMediaAccess(t);case 2:return r=e.sent,e.abrupt("return",r);case 4:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var l=function(){var e=c(F.mark((function e(r){var n,o,a;return F.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:n=R(r),e.prev=1,n.s();case 3:if((o=n.n()).done){e.next=12;break}return a=o.value,e.next=7,h(a);case 7:if("denied"!==e.sent){e.next=10;break}throw new t.DeniedBySystemError("audio"===a,"video"===a);case 10:e.next=3;break;case 12:e.next=17;break;case 14:e.prev=14,e.t0=e.catch(1),n.e(e.t0);case 17:return e.prev=17,n.f(),e.finish(17);case 20:case"end":return e.stop()}}),e,null,[[1,14,17,20]])})));return function(t){return e.apply(this,arguments)}}(),h=function(){var e=c(F.mark((function e(t){var r,n,a;return F.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=Z(t),e.next=3,i(r);case 3:if(n=e.sent,o.debug("Received desktop media access status",n),"not-determined"===n){e.next=7;break}return e.abrupt("return",n);case 7:if("screen"!==r){e.next=9;break}return e.abrupt("return",n);case 9:return e.next=11,f(r);case 11:return e.next=13,i(r);case 13:return a=e.sent,e.abrupt("return",a);case 15:case"end":return e.stop()}}),e)})));return function(t){return e.apply(this,arguments)}}();return{askForMediaAccess:f,getMediaAccessStatus:i,requestDesktopMediaPermissions:l,requestDesktopMediaAccess:h,openSystemSettings:function(e){return u.apply(this,arguments)},destroy:function(){a.destroy()}}}({ipcMain:f,logger:s})})),l=u.createController((function(){return function(e){var o=e.ipcMain,a=e.logger,i=e.mediaAccess,s=r.createScope(),u=t.createEventBus(),f=s.createStore(void 0),p=t.createKeyQueue(),l=s.createStore("finished"),h=s.createStore([]),d=function(e){a.debug("received sources",e.map((function(e){return e.id}))),e.sort((function(e,t){return e.name<t.name?1:-1})),h.set(e)};o.capturerSourcesFinishProcess(),window.JazzScreenObtainer={openDesktopPicker:function(e,t){a.debug("Request to open desktop capture picker"),l.set("pending"),o.capturerSourcesStartProcess().then((function(e){f.set({onSourceChoose:t}),"failure"!==e.type?(l.set("started"),d(e.value.sources),u({type:"sources",payload:{sources:e.value.sources}})):g()}))}},s.add((function(){window.JazzScreenObtainer=void 0,o.capturerSourcesFinishProcess()}));var y=function(){var e=c(F.mark((function e(){return F.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return f.set(void 0),h.set([]),e.next=4,o.capturerSourcesFinishProcess();case 4:l.set("finished");case 5:case"end":return e.stop()}}),e)})));return function(){return e.apply(this,arguments)}}(),v=function(){var e=c(F.mark((function e(){var t;return F.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if("finished"!==l.get()){e.next=2;break}return e.abrupt("return");case 2:return e.next=4,n.firstValueFrom(l.value$.pipe(n.filter((function(e){return"pending"!==e}))));case 4:if("finished"!==e.sent){e.next=7;break}return e.abrupt("return");case 7:return null===(t=f.get())||void 0===t||t.onSourceChoose(),u({type:"cancel",payload:{action:"cancel"}}),e.next=11,y();case 11:case"end":return e.stop()}}),e)})));return function(){return e.apply(this,arguments)}}(),g=function(){var e=c(F.mark((function e(){return F.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,p.waitKey("selectSource");case 2:return e.next=4,p.add({key:"cancel",cb:v});case 4:case"end":return e.stop()}}),e)})));return function(){return e.apply(this,arguments)}}(),m=function(){var e=c(F.mark((function e(t){var r,o,a=arguments;return F.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(r=a.length>1&&void 0!==a[1]&&a[1],"finished"!==l.get()){e.next=3;break}return e.abrupt("return");case 3:return e.next=5,n.firstValueFrom(l.value$.pipe(n.filter((function(e){return"pending"!==e}))));case 5:if("finished"!==e.sent){e.next=8;break}return e.abrupt("return");case 8:if(o=f.get()){e.next=11;break}return e.abrupt("return");case 11:return o.onSourceChoose(t.id),u({type:"selectDesktopSource",payload:{source:t,isShareDisplayAudio:r}}),e.next=15,y();case 15:case"end":return e.stop()}}),e)})));return function(t){return e.apply(this,arguments)}}(),w=function(){var e=c(F.mark((function e(t){var r,n=arguments;return F.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.length>1&&void 0!==n[1]&&n[1],e.next=3,p.waitKey("cancel");case 3:return e.next=5,p.add({key:"selectSource",cb:function(){return m(t,r)}});case 5:case"end":return e.stop()}}),e)})));return function(t){return e.apply(this,arguments)}}(),x=s.createStore(!1),b=s.createStore(void 0);return s.handle(h.value$.pipe(n.filter(t.isNotEmpty)),c(F.mark((function e(){var t;return F.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(!x.get()){e.next=2;break}return e.abrupt("return");case 2:return x.set(!0),e.next=5,i.getMediaAccessStatus("screen");case 5:t=e.sent,b.set(t);case 7:case"end":return e.stop()}}),e)})))),s.handle(u.event$.pipe(t.filterByType("selectDesktopSource"),n.switchMap((function(e){var t=e.payload;return new n.Observable((function(e){o.once("askDisplayMediaStream",(function(){e.next("askDisplayMediaStream")}))})).pipe(n.map((function(){return t})))}))),(function(e){var t=e.source,r=e.isShareDisplayAudio;o.replyDisplayMediaStream(t,r)})),s.add(o.subscribe("updateSources",(function(e){d(e.sources),u({type:"updateSources",payload:e})}))),s.add(o.subscribe("startProcess",(function(e){u({type:"startProcess",payload:e})}))),s.add(o.subscribe("finishProcess",(function(e){u({type:"finishProcess",payload:e})}))),{$accessStatus:b,$sources:h,$status:l,event$:u.event$,cancel:g,selectSource:w,destroy:function(){s.destroy()}}}({ipcMain:f,logger:s,mediaAccess:p})})),h=l.$status,d=l.$accessStatus,y=l.$sources,v=l.cancel,g=l.event$,m=l.selectSource,w=i.hooks.requestMediaPermissions.tap(function(){var e=c(F.mark((function e(t){return F.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,p.requestDesktopMediaPermissions(t);case 2:case"end":return e.stop()}}),e)})));return function(t){return e.apply(this,arguments)}}());return u.add((function(){return w()})),{desktopCapturer:{sources:y,accessStatus:d,status:h,getMediaAccessStatus:p.getMediaAccessStatus,requestDesktopMediaAccess:p.requestDesktopMediaAccess,selectSource:m,openSystemSettings:p.openSystemSettings,cancel:v,event$:g},destroy:function(){u.destroy()}}}exports.INTERVAL_CHECK_ACTUAL_SOURCE=2e3,exports.JAZZ_SDK_CONTEXT_TOKEN=o,exports.JAZZ_SDK_DESKTOP_CAPTURER_TOKEN=a,exports.desktopCapturerPlugin=function(){return G.apply(this,arguments)},exports.getDesktopCapturer=function(e){return e.container.resolve(a)};