UNPKG

fcr-core

Version:

Core APIs for building online scenes

268 lines (267 loc) 16.3 kB
"use strict"; require("core-js/modules/es.array.push.js"); require("core-js/modules/esnext.function.metadata.js"); require("core-js/modules/esnext.map.delete-all.js"); require("core-js/modules/esnext.map.emplace.js"); require("core-js/modules/esnext.map.every.js"); require("core-js/modules/esnext.map.filter.js"); require("core-js/modules/esnext.map.find.js"); require("core-js/modules/esnext.map.find-key.js"); require("core-js/modules/esnext.map.includes.js"); require("core-js/modules/esnext.map.key-of.js"); require("core-js/modules/esnext.map.map-keys.js"); require("core-js/modules/esnext.map.map-values.js"); require("core-js/modules/esnext.map.merge.js"); require("core-js/modules/esnext.map.reduce.js"); require("core-js/modules/esnext.map.some.js"); require("core-js/modules/esnext.map.update.js"); require("core-js/modules/esnext.symbol.metadata.js"); Object.defineProperty(exports, "__esModule", { value: true }); exports.FcrSharingControlImpl = void 0; require("core-js/modules/esnext.iterator.constructor.js"); require("core-js/modules/esnext.iterator.filter.js"); require("core-js/modules/esnext.iterator.for-each.js"); var _annotationControl = require("../whiteboard-control-v2/annotation-control"); var _type = require("./type"); var _imports = require("../../imports"); var _ = require("../.."); var _enums = require("../whiteboard-control/enums"); var _error = require("../../utilities/error"); var _logger = require("../../utilities/logger"); let _initProto; function _applyDecs(e, t, r, n, o, a) { function i(e, t, r) { return function (n, o) { return r && r(n), e[t].call(n, o); }; } function c(e, t) { for (var r = 0; r < e.length; r++) e[r].call(t); return t; } function s(e, t, r, n) { if ("function" != typeof e && (n || void 0 !== e)) throw new TypeError(t + " must " + (r || "be") + " a function" + (n ? "" : " or undefined")); return e; } function applyDec(e, t, r, n, o, a, c, u, l, f, p, d, h) { function m(e) { if (!h(e)) throw new TypeError("Attempted to access private element on non-instance"); } var y, v = t[0], g = t[3], b = !u; if (!b) { r || Array.isArray(v) || (v = [v]); var w = {}, S = [], A = 3 === o ? "get" : 4 === o || d ? "set" : "value"; f ? (p || d ? w = { get: _setFunctionName(function () { return g(this); }, n, "get"), set: function (e) { t[4](this, e); } } : w[A] = g, p || _setFunctionName(w[A], n, 2 === o ? "" : A)) : p || (w = Object.getOwnPropertyDescriptor(e, n)); } for (var P = e, j = v.length - 1; j >= 0; j -= r ? 2 : 1) { var D = v[j], E = r ? v[j - 1] : void 0, I = {}, O = { kind: ["field", "accessor", "method", "getter", "setter", "class"][o], name: n, metadata: a, addInitializer: function (e, t) { if (e.v) throw Error("attempted to call addInitializer after decoration was finished"); s(t, "An initializer", "be", !0), c.push(t); }.bind(null, I) }; try { if (b) (y = s(D.call(E, P, O), "class decorators", "return")) && (P = y);else { var k, F; O.static = l, O.private = f, f ? 2 === o ? k = function (e) { return m(e), w.value; } : (o < 4 && (k = i(w, "get", m)), 3 !== o && (F = i(w, "set", m))) : (k = function (e) { return e[n]; }, (o < 2 || 4 === o) && (F = function (e, t) { e[n] = t; })); var N = O.access = { has: f ? h.bind() : function (e) { return n in e; } }; if (k && (N.get = k), F && (N.set = F), P = D.call(E, d ? { get: w.get, set: w.set } : w[A], O), d) { if ("object" == typeof P && P) (y = s(P.get, "accessor.get")) && (w.get = y), (y = s(P.set, "accessor.set")) && (w.set = y), (y = s(P.init, "accessor.init")) && S.push(y);else if (void 0 !== P) throw new TypeError("accessor decorators must return an object with get, set, or init properties or void 0"); } else s(P, (p ? "field" : "method") + " decorators", "return") && (p ? S.push(P) : w[A] = P); } } finally { I.v = !0; } } return (p || d) && u.push(function (e, t) { for (var r = S.length - 1; r >= 0; r--) t = S[r].call(e, t); return t; }), p || b || (f ? d ? u.push(i(w, "get"), i(w, "set")) : u.push(2 === o ? w[A] : i.call.bind(w[A])) : Object.defineProperty(e, n, w)), P; } function u(e, t) { return Object.defineProperty(e, Symbol.metadata || Symbol.for("Symbol.metadata"), { configurable: !0, enumerable: !0, value: t }); } if (arguments.length >= 6) var l = a[Symbol.metadata || Symbol.for("Symbol.metadata")]; var f = Object.create(null == l ? null : l), p = function (e, t, r, n) { var o, a, i = [], s = function (t) { return _checkInRHS(t) === e; }, u = new Map(); function l(e) { e && i.push(c.bind(null, e)); } for (var f = 0; f < t.length; f++) { var p = t[f]; if (Array.isArray(p)) { var d = p[1], h = p[2], m = p.length > 3, y = 16 & d, v = !!(8 & d), g = 0 == (d &= 7), b = h + "/" + v; if (!g && !m) { var w = u.get(b); if (!0 === w || 3 === w && 4 !== d || 4 === w && 3 !== d) throw Error("Attempted to decorate a public method/accessor that has the same name as a previously decorated public method/accessor. This is not currently supported by the decorators plugin. Property name was: " + h); u.set(b, !(d > 2) || d); } applyDec(v ? e : e.prototype, p, y, m ? "#" + h : _toPropertyKey(h), d, n, v ? a = a || [] : o = o || [], i, v, m, g, 1 === d, v && m ? s : r); } } return l(o), l(a), i; }(e, t, o, f); return r.length || u(e, f), { e: p, get c() { var t = []; return r.length && [u(applyDec(e, [r], n, e.name, 5, f, t), f), c.bind(null, t, e)]; } }; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; } function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } function _setFunctionName(e, t, n) { "symbol" == typeof t && (t = (t = t.description) ? "[" + t + "]" : ""); try { Object.defineProperty(e, "name", { configurable: !0, value: n ? n + " " + t : t }); } catch (e) {} return e; } function _checkInRHS(e) { if (Object(e) !== e) throw TypeError("right-hand side of 'in' should be an object, got " + (null !== e ? typeof e : "null")); return e; } class FcrSharingControlImpl { static #_ = (() => [_initProto] = _applyDecs(this, [[_imports.trace, 2, "startScreenSharing"], [_imports.trace, 2, "startWhiteboard"], [_imports.trace, 2, "updateScreenSharing"], [_imports.trace, 2, "stop"], [_imports.trace, 2, "getScreenSharingState"]], []).e)(); logger = (() => (_initProto(this), (0, _logger.createLogger)({ prefix: 'FcrSharingControlImpl' })))(); _observable = (() => new _imports.AgoraObservable())(); _shareOwnerId = ''; _shareOwnerStream = null; _streamObserver = { onStreamsAdded: this._handleOnStreamsAdded.bind(this), onStreamsRemoved: this._handleOnStreamsRemoved.bind(this) }; _sceneObserver = { onScenePropertiesUpdated: this._handleOnScenePropertiesUpdated.bind(this) }; _whiteboardObserver = { onActive: this._handleOnWhiteboardActive.bind(this), onInactive: this._handleOnWhiteboardInActive.bind(this) }; get ownerId() { return this._shareOwnerId; } get ownerStream() { return this._shareOwnerStream; } constructor(_scene, _api, _engine, _privilegeControl, _whiteboardControl, _streamControl, _sharedCache, _userControl) { this._scene = _scene; this._api = _api; this._engine = _engine; this._privilegeControl = _privilegeControl; this._whiteboardControl = _whiteboardControl; this._streamControl = _streamControl; this._sharedCache = _sharedCache; this._userControl = _userControl; this._streamControl.addObserver(this._streamObserver); this._whiteboardControl.addObserver(this._whiteboardObserver); this._scene.addObserver(this._sceneObserver); } async startScreenSharing(config, size) { try { const streamId = await this._streamControl.addLocalScreenStream(config, size); return streamId; } catch (error) { throw error; } } startWhiteboard() { return this._whiteboardControl.active(); } updateScreenSharing(enableAnnotation) { const currentShareState = this.getScreenSharingState(); if (currentShareState === _type.FcrScreenSharingState.START_ONLY_SCREEN) { if (enableAnnotation) { return this._api.toggleAnnotationActivityState(this._scene.sceneId, _enums.FcrSharePermissionState.ON); } else { return this._api.toggleAnnotationActivityState(this._scene.sceneId, _enums.FcrSharePermissionState.OFF); } } return Promise.resolve(); } stop() { const currentScreenSharingState = this.getScreenSharingState(); if (currentScreenSharingState !== _type.FcrScreenSharingState.END) { return this._streamControl.removeScreenStream(); } const currentWhiteboardState = this.getWhiteboardState(); if (currentWhiteboardState === _type.FcrWhiteboardState.START) { return this._whiteboardControl.inactive(); } return Promise.resolve(); } getScreenSharingState() { const streams = this._streamControl.getStreamList(); const screenSharingInfos = streams.filter(streamInfo => this._isStreamToHandleScreenSharing(streamInfo)); if (screenSharingInfos.length === 0) { return _type.FcrScreenSharingState.END; } else { const annotationState = this._getPureSceneAnnotationState(); return annotationState === 0 ? _type.FcrScreenSharingState.START_ONLY_SCREEN : _type.FcrScreenSharingState.START_WHIT_ANNOTATION; } } getWhiteboardState() { const isActive = this._whiteboardControl.getActivity(); return isActive ? _type.FcrWhiteboardState.START : _type.FcrWhiteboardState.END; } getAnnotationControl() { if (!this._annotationControl) { this._annotationControl = new _annotationControl.FcrAnnotationControlImpl(this._scene, this._api, this._engine, this._privilegeControl, this._sharedCache, this._streamControl, this._userControl); } return this._annotationControl; } getSharingWindowSize() { const width = this._scene.getScenePropertiesByKeyPath('widgets.annotation.size.width'); const height = this._scene.getScenePropertiesByKeyPath('widgets.annotation.size.height'); return width && height ? { width, height } : undefined; } getBoardControl() { if (!this._whiteboardControl) { throw (0, _error.generateFcrCoreClientError)(_error.FcrErrorModuleCode.ROOM, _error.FcrErrorCode.NOT_JOINED_ROOM, 'whiteboard control not initialized, please join room first', new Error('whiteboard control not initialized, please join room first')); } return this._whiteboardControl; } addObserver(observer) { this._observable.addObserver(observer); } removeObserver(observer) { this._observable.removeObserver(observer); } release() { this._scene.removeObserver(this._sceneObserver); this._streamControl.removeObserver(this._streamObserver); this._whiteboardControl.removeObserver(this._whiteboardObserver); } _getPureSceneAnnotationState() { return this._scene.getScenePropertiesByKeyPath('widgets.annotation.state'); } _isStreamToHandleScreenSharing(stream) { return stream.videoSourceType === _.FcrVideoSourceType.SCREEN && (stream.streamType === _imports.AgoraRteMediaStreamType.BOTH || stream.streamType === _imports.AgoraRteMediaStreamType.VIDEO); } _handleOnStreamsAdded(roomId, events) { events.forEach(event => { const { modifiedStream } = event; if (this._isStreamToHandleScreenSharing(modifiedStream)) { this._shareOwnerId = modifiedStream.owner.userId; this._shareOwnerStream = modifiedStream; // const annotationState = this._getPureSceneAnnotationState(); // if (annotationState === 0) { this._observable.notifyObservers('onScreenSharingUpdated', modifiedStream, _type.FcrScreenSharingState.START_ONLY_SCREEN); // } // else { // this._observable.notifyObservers( // 'onScreenSharingUpdated', // modifiedStream.owner.userId, // FcrScreenSharingState.START_WHIT_ANNOTATION, // ); // } } }); } _handleOnStreamsRemoved(roomId, events) { events.forEach(event => { const { modifiedStream } = event; const ownerId = modifiedStream.owner.userId; if (this._isStreamToHandleScreenSharing(modifiedStream)) { this._observable.notifyObservers('onScreenSharingUpdated', modifiedStream, _type.FcrScreenSharingState.END); if (this.getScreenSharingState() === _type.FcrScreenSharingState.END) { const isMeSharing = ownerId === this._userControl.getLocalUser().userId; if (isMeSharing) { this.logger.info('[sharing]: on stream removed, clean before terminal Annotation app'); this._annotationControl?.getMainWindow()?.clean(); // @ts-ignore // this._annotationControl?.getMainWindow()?._terminalApp('Annotation'); // this.logger.info(`[sharing]: on stream removed, terminated Annotation app`); } } } }); } _handleOnScenePropertiesUpdated(sceneId, event) { const { cause } = event; const causeData = cause?.data; const currentScreenSharingState = this.getScreenSharingState(); const causeCmd = cause?.cmd; if (currentScreenSharingState === _type.FcrScreenSharingState.END) { if (causeData && causeCmd === 10 && causeData.widgetUuid === 'annotation') { if (causeData.widgetCause?.cmd === 1) { this._observable.notifyObservers('onScreenSharingUpdated', this.ownerStream, _type.FcrScreenSharingState.END); const isMeSharing = this.ownerId === this._userControl.getLocalUser().userId; if (isMeSharing) { this.logger.info('[sharing]: on scene properties updated, clean before terminal Annotation app'); this._annotationControl?.getMainWindow()?.clean(); // @ts-ignore // this._annotationControl?.getMainWindow()?._terminalApp('Annotation'); // this.logger.info(`[sharing]: on scene properties updated, terminated Annotation app`); } } } } else { // 当屏幕共享开启时 if (causeData && causeCmd === 10 && causeData.widgetUuid === 'annotation') { if (causeData.widgetCause?.cmd === 1) { const screenStream = this._getScreenShareStreamByUuid(); this._observable.notifyObservers('onScreenSharingUpdated', screenStream ?? this.ownerStream, _type.FcrScreenSharingState.START_WHIT_ANNOTATION); // const owner = this._userControl.getUser(this.ownerId); // const isElectron = // owner && // [FcrPlatform.WEB_DESKTOP, FcrPlatform.MACOS, FcrPlatform.WINDOWS].includes( // owner.platform, // ); // const mainWindow = this._annotationControl?.getMainWindow(); // const isAndroid = owner && owner.platform === FcrPlatform.ANDROID; // if (isElectron && mainWindow) { // mainWindow.setAutoCancelDraw(false); // } // if (isAndroid && mainWindow) { // const isNotMeSharing = this.ownerId !== this._userControl.getLocalUser().userId; // mainWindow.setAutoCancelDraw(isNotMeSharing); // } } else { if (causeData.reason === 1) { this._observable.notifyObservers('onScreenSharingUpdated', this.ownerStream, _type.FcrScreenSharingState.END); } } } } } _handleOnWhiteboardActive(ownerId, operatorUser) { this._observable.notifyObservers('onWhiteboardStarted', ownerId, operatorUser); } _handleOnWhiteboardInActive(reason, operatorUser) { this._observable.notifyObservers('onWhiteboardEnded', reason, operatorUser); } _getScreenShareStreamByUuid() { const screenStreamUuid = this._scene.getScenePropertiesByKeyPath('widgets.annotation.extra.screenStreamUuid'); this.logger.info('[sharing]: scene properties screenStreamUuid: ', screenStreamUuid); const stream = this._streamControl.getStreamByStreamId(screenStreamUuid); return typeof stream === 'undefined' ? null : stream; } } exports.FcrSharingControlImpl = FcrSharingControlImpl;