UNPKG

@salutejs/jazz-sdk-electron-plugins

Version:

Jazz SDK electron plugins

2 lines (1 loc) 16.1 kB
import{token as e}from"ditox";import{createEventBus as t,createKeyQueue as r,isNotEmpty as n,filterByType as o,DeniedBySystemError as a,createModule as i}from"@salutejs/jazz-sdk-web";import{createScope as c}from"rx-effects";import{filter as u,switchMap as s,Observable as f,map as p,firstValueFrom as l}from"rxjs";var h=e({key:"JAZZ_SDK_CONTEXT_TOKEN"}),d=e({key:"JAZZ_SDK_DESKTOP_CAPTURER_TOKEN"}),y=2e3;function v(e,t,r,n,o,a,i){try{var c=e[a](i),u=c.value}catch(e){return void r(e)}c.done?t(u):Promise.resolve(u).then(n,o)}function m(e){return function(){var t=this,r=arguments;return new Promise((function(n,o){var a=e.apply(t,r);function i(e){v(a,n,o,i,c,"next",e)}function c(e){v(a,n,o,i,c,"throw",e)}i(void 0)}))}}function g(e){return g="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},g(e)}var w,x=Object.prototype,b=x.hasOwnProperty,S="function"==typeof Symbol?Symbol:{},k=S.iterator||"@@iterator",E=S.asyncIterator||"@@asyncIterator",L=S.toStringTag||"@@toStringTag";function A(e,t,r,n){var o=t&&t.prototype instanceof $?t:$,a=Object.create(o.prototype),i=new U(n||[]);return a._invoke=function(e,t,r){var n=M;return function(o,a){if(n===j)throw new Error("Generator is already running");if(n===D){if("throw"===o)throw a;return X()}for(r.method=o,r.arg=a;;){var i=r.delegate;if(i){var c=I(i,r);if(c){if(c===O)continue;return c}}if("next"===r.method)r.sent=r._sent=r.arg;else if("throw"===r.method){if(n===M)throw n=D,r.arg;r.dispatchException(r.arg)}else"return"===r.method&&r.abrupt("return",r.arg);n=j;var u=P(e,t,r);if("normal"===u.type){if(n=r.done?D:_,u.arg===O)continue;return{value:u.arg,done:r.done}}"throw"===u.type&&(n=D,r.method="throw",r.arg=u.arg)}}}(e,r,i),a}function P(e,t,r){try{return{type:"normal",arg:e.call(t,r)}}catch(e){return{type:"throw",arg:e}}}var M="suspendedStart",_="suspendedYield",j="executing",D="completed",O={};function $(){}function C(){}function T(){}var N={};N[k]=function(){return this};var z=Object.getPrototypeOf,F=z&&z(z(Y([])));F&&F!==x&&b.call(F,k)&&(N=F);var q=T.prototype=$.prototype=Object.create(N);function G(e){["next","throw","return"].forEach((function(t){e[t]=function(e){return this._invoke(t,e)}}))}function K(e){var t="function"==typeof e&&e.constructor;return!!t&&(t===C||"GeneratorFunction"===(t.displayName||t.name))}function R(e,t){function r(n,o,a,i){var c=P(e[n],e,o);if("throw"!==c.type){var u=c.arg,s=u.value;return s&&"object"===g(s)&&b.call(s,"__await")?t.resolve(s.__await).then((function(e){r("next",e,a,i)}),(function(e){r("throw",e,a,i)})):t.resolve(s).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 I(e,t){var r=e.iterator[t.method];if(r===w){if(t.delegate=null,"throw"===t.method){if(e.iterator.return&&(t.method="return",t.arg=w,I(e,t),"throw"===t.method))return O;t.method="throw",t.arg=new TypeError("The iterator does not provide a 'throw' method")}return O}var n=P(r,e.iterator,t.arg);if("throw"===n.type)return t.method="throw",t.arg=n.arg,t.delegate=null,O;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=w),t.delegate=null,O):o:(t.method="throw",t.arg=new TypeError("iterator result is not an object"),t.delegate=null,O)}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 Z(e){var t=e.completion||{};t.type="normal",delete t.arg,e.completion=t}function U(e){this.tryEntries=[{tryLoc:"root"}],e.forEach(J,this),this.reset(!0)}function Y(e){if(e){var t=e[k];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(b.call(e,r))return t.value=e[r],t.done=!1,t;return t.value=w,t.done=!0,t};return n.next=n}}return{next:X}}function X(){return{value:w,done:!0}}C.prototype=q.constructor=T,T.constructor=C,T[L]=C.displayName="GeneratorFunction",G(R.prototype),R.prototype[E]=function(){return this},G(q),q[L]="Generator",q[k]=function(){return this},q.toString=function(){return"[object Generator]"},U.prototype={constructor:U,reset:function(e){if(this.prev=0,this.next=0,this.sent=this._sent=w,this.done=!1,this.delegate=null,this.method="next",this.arg=w,this.tryEntries.forEach(Z),!e)for(var t in this)"t"===t.charAt(0)&&b.call(this,t)&&!isNaN(+t.slice(1))&&(this[t]=w)},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=w),!!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=b.call(o,"catchLoc"),c=b.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&&b.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,O):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),O},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),Z(r),O}},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;Z(r)}return o}}throw new Error("illegal catch attempt")},delegateYield:function(e,t,r){return this.delegate={iterator:Y(e),resultName:t,nextLoc:r},"next"===this.method&&(this.arg=w),O}};var B={wrap:A,isGeneratorFunction:K,AsyncIterator:R,mark:function(e){return Object.setPrototypeOf?Object.setPrototypeOf(e,T):(e.__proto__=T,L in e||(e[L]="GeneratorFunction")),e.prototype=Object.create(q),e},awrap:function(e){return{__await:e}},async:function(e,t,r,n,o){void 0===o&&(o=Promise);var a=new R(A(e,t,r,n),o);return K(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:Y};function H(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 Q(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)?Q(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 Q(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 V(e){switch(e){case"audio":return"microphone";case"video":return"camera";case"displayScreen":return"screen";default:return}}function W(){return ee.apply(this,arguments)}function ee(){return(ee=m(B.mark((function e(){return B.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.abrupt("return",i({name:"DesktopCapturerPlugin",imports:{sdkContext:h},factory:te,exports:{desktopCapturer:d}}));case 1:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function te(e){var i=e.deps.sdkContext,h=i.logger,d=i.localDevices,y=h.getLogger(e.name),v=c(),g=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,w=v.createController((function(){return function(e){var t=e.ipcMain,r=e.logger,n=c();function o(e){return i.apply(this,arguments)}function i(){return(i=m(B.mark((function e(n){var o,a;return B.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r.debug("Request media access status for",n),e.next=3,t.getMediaAccessStatus(n);case 3:return o=e.sent,a=o.status,r.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=m(B.mark((function e(n){return B.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r.debug("Request openSystemSettings",n),e.next=3,t.openSystemSettings(n);case 3:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function s(e){return f.apply(this,arguments)}function f(){return(f=m(B.mark((function e(r){var n;return B.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,t.askForMediaAccess(r);case 2:return n=e.sent,e.abrupt("return",n);case 4:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var p=function(){var e=m(B.mark((function e(t){var r,n,o;return B.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:r=H(t),e.prev=1,r.s();case 3:if((n=r.n()).done){e.next=12;break}return o=n.value,e.next=7,l(o);case 7:if("denied"!==e.sent){e.next=10;break}throw new a("audio"===o,"video"===o);case 10:e.next=3;break;case 12:e.next=17;break;case 14:e.prev=14,e.t0=e.catch(1),r.e(e.t0);case 17:return e.prev=17,r.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)}}(),l=function(){var e=m(B.mark((function e(t){var n,a,i;return B.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=V(t),e.next=3,o(n);case 3:if(a=e.sent,r.debug("Received desktop media access status",a),"not-determined"===a){e.next=7;break}return e.abrupt("return",a);case 7:if("screen"!==n){e.next=9;break}return e.abrupt("return",a);case 9:return e.next=11,s(n);case 11:return e.next=13,o(n);case 13:return i=e.sent,e.abrupt("return",i);case 15:case"end":return e.stop()}}),e)})));return function(t){return e.apply(this,arguments)}}();return{askForMediaAccess:s,getMediaAccessStatus:o,requestDesktopMediaPermissions:p,requestDesktopMediaAccess:l,openSystemSettings:function(e){return u.apply(this,arguments)},destroy:function(){n.destroy()}}}({ipcMain:g,logger:y})})),x=v.createController((function(){return function(e){var a=e.ipcMain,i=e.logger,h=e.mediaAccess,d=c(),y=t(),v=d.createStore(void 0),g=r(),w=d.createStore("finished"),x=d.createStore([]),b=function(e){i.debug("received sources",e.map((function(e){return e.id}))),e.sort((function(e,t){return e.name<t.name?1:-1})),x.set(e)};a.capturerSourcesFinishProcess(),window.JazzScreenObtainer={openDesktopPicker:function(e,t){i.debug("Request to open desktop capture picker"),w.set("pending"),a.capturerSourcesStartProcess().then((function(e){v.set({onSourceChoose:t}),"failure"!==e.type?(w.set("started"),b(e.value.sources),y({type:"sources",payload:{sources:e.value.sources}})):E()}))}},d.add((function(){window.JazzScreenObtainer=void 0,a.capturerSourcesFinishProcess()}));var S=function(){var e=m(B.mark((function e(){return B.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return v.set(void 0),x.set([]),e.next=4,a.capturerSourcesFinishProcess();case 4:w.set("finished");case 5:case"end":return e.stop()}}),e)})));return function(){return e.apply(this,arguments)}}(),k=function(){var e=m(B.mark((function e(){var t;return B.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if("finished"!==w.get()){e.next=2;break}return e.abrupt("return");case 2:return e.next=4,l(w.value$.pipe(u((function(e){return"pending"!==e}))));case 4:if("finished"!==e.sent){e.next=7;break}return e.abrupt("return");case 7:return null===(t=v.get())||void 0===t||t.onSourceChoose(),y({type:"cancel",payload:{action:"cancel"}}),e.next=11,S();case 11:case"end":return e.stop()}}),e)})));return function(){return e.apply(this,arguments)}}(),E=function(){var e=m(B.mark((function e(){return B.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,g.waitKey("selectSource");case 2:return e.next=4,g.add({key:"cancel",cb:k});case 4:case"end":return e.stop()}}),e)})));return function(){return e.apply(this,arguments)}}(),L=function(){var e=m(B.mark((function e(t){var r,n,o=arguments;return B.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(r=o.length>1&&void 0!==o[1]&&o[1],"finished"!==w.get()){e.next=3;break}return e.abrupt("return");case 3:return e.next=5,l(w.value$.pipe(u((function(e){return"pending"!==e}))));case 5:if("finished"!==e.sent){e.next=8;break}return e.abrupt("return");case 8:if(n=v.get()){e.next=11;break}return e.abrupt("return");case 11:return n.onSourceChoose(t.id),y({type:"selectDesktopSource",payload:{source:t,isShareDisplayAudio:r}}),e.next=15,S();case 15:case"end":return e.stop()}}),e)})));return function(t){return e.apply(this,arguments)}}(),A=function(){var e=m(B.mark((function e(t){var r,n=arguments;return B.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,g.waitKey("cancel");case 3:return e.next=5,g.add({key:"selectSource",cb:function(){return L(t,r)}});case 5:case"end":return e.stop()}}),e)})));return function(t){return e.apply(this,arguments)}}(),P=d.createStore(!1),M=d.createStore(void 0);return d.handle(x.value$.pipe(u(n)),m(B.mark((function e(){var t;return B.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(!P.get()){e.next=2;break}return e.abrupt("return");case 2:return P.set(!0),e.next=5,h.getMediaAccessStatus("screen");case 5:t=e.sent,M.set(t);case 7:case"end":return e.stop()}}),e)})))),d.handle(y.event$.pipe(o("selectDesktopSource"),s((function(e){var t=e.payload;return new f((function(e){a.once("askDisplayMediaStream",(function(){e.next("askDisplayMediaStream")}))})).pipe(p((function(){return t})))}))),(function(e){var t=e.source,r=e.isShareDisplayAudio;a.replyDisplayMediaStream(t,r)})),d.add(a.subscribe("updateSources",(function(e){b(e.sources),y({type:"updateSources",payload:e})}))),d.add(a.subscribe("startProcess",(function(e){y({type:"startProcess",payload:e})}))),d.add(a.subscribe("finishProcess",(function(e){y({type:"finishProcess",payload:e})}))),{$accessStatus:M,$sources:x,$status:w,event$:y.event$,cancel:E,selectSource:A,destroy:function(){d.destroy()}}}({ipcMain:g,logger:y,mediaAccess:w})})),b=x.$status,S=x.$accessStatus,k=x.$sources,E=x.cancel,L=x.event$,A=x.selectSource,P=d.hooks.requestMediaPermissions.tap(function(){var e=m(B.mark((function e(t){return B.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,w.requestDesktopMediaPermissions(t);case 2:case"end":return e.stop()}}),e)})));return function(t){return e.apply(this,arguments)}}());return v.add((function(){return P()})),{desktopCapturer:{sources:k,accessStatus:S,status:b,getMediaAccessStatus:w.getMediaAccessStatus,requestDesktopMediaAccess:w.requestDesktopMediaAccess,selectSource:A,openSystemSettings:w.openSystemSettings,cancel:E,event$:L},destroy:function(){v.destroy()}}}function re(e){return e.container.resolve(d)}export{y as INTERVAL_CHECK_ACTUAL_SOURCE,h as JAZZ_SDK_CONTEXT_TOKEN,d as JAZZ_SDK_DESKTOP_CAPTURER_TOKEN,W as desktopCapturerPlugin,re as getDesktopCapturer};