UNPKG

fcr-core

Version:

Core APIs for building online scenes

431 lines (428 loc) 21.1 kB
import "core-js/modules/es.array.push.js"; import "core-js/modules/esnext.function.metadata.js"; import "core-js/modules/esnext.map.delete-all.js"; import "core-js/modules/esnext.map.emplace.js"; import "core-js/modules/esnext.map.every.js"; import "core-js/modules/esnext.map.filter.js"; import "core-js/modules/esnext.map.find.js"; import "core-js/modules/esnext.map.find-key.js"; import "core-js/modules/esnext.map.includes.js"; import "core-js/modules/esnext.map.key-of.js"; import "core-js/modules/esnext.map.map-keys.js"; import "core-js/modules/esnext.map.map-values.js"; import "core-js/modules/esnext.map.merge.js"; import "core-js/modules/esnext.map.reduce.js"; import "core-js/modules/esnext.map.some.js"; import "core-js/modules/esnext.map.update.js"; import "core-js/modules/esnext.symbol.metadata.js"; let _initProto, _openDecs, _setBackgroundColorDecs, _setOperationPrivilegeDecs, _setBoardWritableDecs; import "core-js/modules/esnext.iterator.constructor.js"; import "core-js/modules/esnext.iterator.map.js"; 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; } import SlideApp from '@netless/app-slide'; import { ApplianceMultiPlugin } from '@netless/appliance-plugin'; import * as netlessVideoPlugin from '@netless/video-js-plugin'; import { WindowManager } from '@netless/window-manager'; import { bound } from 'agora-foundation/lib/decorator'; import { ErrorModuleCode } from '../../imports'; import { AgoraObservable } from 'agora-foundation/lib/utilities/observable'; import { sleep } from 'agora-rte-sdk/lib/imports'; import { DeviceType, LoggerReportMode, RoomPhase, ViewMode, WhiteWebSdk, createPlugins } from 'white-web-sdk'; import { FcrConnectionState } from '../..'; import { trace } from '../../imports'; import { FcrReturnCode } from '../../type'; import { handleRequestError } from '../../utilities/error'; import { createLogger, generateLogObserver } from '../../utilities/logger'; import { convertRteUserToFcrUser } from '../../utilities/user'; import { FcrSharePermissionState } from '../sharing-control/type'; import { FcrBoardPropertiesState } from '../whiteboard-control/type'; import { FcrBoardMainWindowImpl } from './board-window'; import { convertToFcrBoardToolShape, hexColorToWhiteboardColor, textColors } from './utils'; /** * @internal */ export class FcrWhiteboardControlImpl { static { [_initProto] = _applyDecs(this, [[[bound, trace], 2, "active"], [[bound, trace], 2, "inactive"], [_openDecs, 2, "open"], [[bound, trace], 2, "close"], [[bound, trace], 2, "release"], [_setBackgroundColorDecs, 2, "setBackgroundColor"], [[bound, trace], 2, "getActivity"], [[bound, trace], 2, "getOwnerId"], [[bound, trace], 2, "getBackgroundColor"], [[bound, trace], 2, "getConnectionState"], [[bound, trace], 2, "getMainWindow"], [[bound, trace], 2, "addObserver"], [[bound, trace], 2, "removeObserver"], [[bound, trace], 2, "getBoardActiveInfo"], [[bound, trace], 2, "hasOperationPrivilege"], [_setOperationPrivilegeDecs, 2, "setOperationPrivilege"], [_setBoardWritableDecs, 2, "_setBoardWritable"], [bound, 2, "_getToken"], [bound, 2, "_handleRoomStateUpdated"], [bound, 2, "_updateConnnectionState"], [bound, 2, "_handleConnectionStateUpdated"], [trace, 2, "_connect"], [trace, 2, "_clean"]], []).e; } [(_openDecs = [bound, trace(['params'])], _setBackgroundColorDecs = [bound, trace(['backgroundColor'])], _setOperationPrivilegeDecs = [bound, trace(['hasPrivilege'])], _setBoardWritableDecs = trace(['granted']), "logger")] = (_initProto(this), createLogger({ prefix: 'FcrWhiteboardControlImpl' })); _client = {}; _joined = false; _connectState = FcrConnectionState.DISCONNECTED; _observable = new AgoraObservable(); _options = { debug: false }; _hasOperationPrivilege = false; constructor(_scene, _api, _sharedCache, hasOperationPrivilege) { this._scene = _scene; this._api = _api; this._sharedCache = _sharedCache; this._hasOperationPrivilege = hasOperationPrivilege; this._roomCache = this._sharedCache.getRoomCache(this._scene.sceneId); this._addLogObserver(); _scene.addObserver({ onScenePropertiesUpdated: (_, event) => { const operatorUser = convertRteUserToFcrUser(event.operatorUser, this._roomCache); if (event.cause?.cmd === 10) { const { state, extra: { backgroundColor: bgColor } } = _scene.getScenePropertiesByKeyPath('widgets.netlessBoard') ?? { state: 0, extra: { backgroundColor: '#fff' } }; if (state === 1) { this._observable.notifyObservers('onActive', _scene.getScenePropertiesByKeyPath('widgets.netlessBoard').ownerUserUuid, operatorUser); } if (state === 0) { this._observable.notifyObservers('onInactive', event.cause.data.widgetCause?.data?.reason, operatorUser); } if (event.cause.data.widgetCause?.cmd === 100101) { this._observable.notifyObservers('onBackgroundColorUpdated', bgColor, operatorUser); } } } }); WindowManager.register({ kind: 'Slide', src: SlideApp }); } async active() { return handleRequestError(() => this._api.toggleWhiteboardActivityState(this._scene.sceneId, FcrSharePermissionState.ON), ErrorModuleCode.FCR_ROOM_WHITEBOARD, 'active failed'); } async inactive() { return handleRequestError(() => this._api.toggleWhiteboardActivityState(this._scene.sceneId, FcrSharePermissionState.OFF), ErrorModuleCode.FCR_ROOM_WHITEBOARD, 'inactive failed'); } async open(params = {}) { this._whiteboardPrivateParameters = params; // 白板权限禁用状态下,主持人和联席主持人仍然可以发起共享,所以,不能根据是否禁用状态直接拒绝 this._joined = true; if (this._connectState !== FcrConnectionState.DISCONNECTED) { return this._boardView; } const boardView = await this._connect(); return boardView; } async close() { this._joined = false; await this._clean(); return FcrReturnCode.SUCCESS; } release() { this._observable.removeAllObservers(); return this.close(); } async setBackgroundColor(backgroundColor) { await this._api.setBackgroundColor({ roomId: this._scene.sceneId, backgroundColor }); return FcrReturnCode.SUCCESS; } getActivity() { return this._scene.getScenePropertiesByKeyPath('widgets.netlessBoard').state === 1; } getOwnerId() { return this._scene.getScenePropertiesByKeyPath('widgets.netlessBoard.ownerUserUuid'); } getBackgroundColor() { return this._scene.getScenePropertiesByKeyPath('widgets.netlessBoard.extra.backgroundColor'); } getConnectionState() { return this._connectState; } getMainWindow() { return this._boardView; } addObserver(observer) { this._observable.addObserver(observer); return FcrReturnCode.SUCCESS; } removeObserver(observer) { this._observable.removeObserver(observer); return FcrReturnCode.SUCCESS; } getBoardActiveInfo() { const { state, ownerUserUuid: ownerUserId } = this._scene.getScenePropertiesByKeyPath('widgets.netlessBoard') || { isActive: false, ownerUserUuid: null }; if (state === FcrBoardPropertiesState.ACTIVE) { return { isActive: true, ownerUserId }; } else { return { isActive: false, ownerUserId: null }; } } hasOperationPrivilege() { return this._hasOperationPrivilege; } setOperationPrivilege(hasPrivilege) { if (hasPrivilege !== this._hasOperationPrivilege) { this._hasOperationPrivilege = hasPrivilege; this._setBoardWritable(hasPrivilege); } return FcrReturnCode.SUCCESS; } async _setBoardWritable(granted) { if (this._room) { const room = this._room; if (granted) { this.logger.info('set whiteboard writable => true, start'); await room.setWritable(true); this.logger.info('set whiteboard writable => true, success'); room.disableDeviceInputs = false; room.disableSerialization = false; const mainWindow = this._boardView; if (mainWindow) { // @ts-ignore mainWindow._observable.notifyObservers('onWritable', true); } } else { room.disableDeviceInputs = true; room.disableSerialization = true; this.logger.info('set whiteboard writable => false, start'); await room.setWritable(false); this.logger.info('set whiteboard writable => false, success'); const mainWindow = this._boardView; if (mainWindow) { // @ts-ignore mainWindow._observable.notifyObservers('onWritable', false); } } } return FcrReturnCode.SUCCESS; } _createPlugins() { const plugins = createPlugins({ [netlessVideoPlugin.PluginId]: netlessVideoPlugin.videoJsPlugin() }); plugins.setPluginContext(netlessVideoPlugin.PluginId, { enable: true, verbose: true }); return plugins; } async _getToken(roomId, userId) { return await handleRequestError(() => this._api.getWhiteboardToken({ roomId, userId }), ErrorModuleCode.FCR_ROOM_WHITEBOARD, 'get whiteboard token failed'); } _handleRoomStateUpdated(state) { const { memberState } = state; if (memberState) { const { strokeColor, strokeWidth, currentApplianceName, textSize, shapeType } = memberState; const localState = {}; const [tool, shape] = convertToFcrBoardToolShape(currentApplianceName, shapeType); localState.tool = tool; localState.shape = shape; if (typeof strokeColor !== 'undefined') { const [r, g, b] = strokeColor; localState.strokeColor = { r, g, b }; } if (typeof strokeWidth !== 'undefined') { localState.strokeWidth = strokeWidth; } if (typeof textSize !== 'undefined') { localState.textSize = textSize; } } } _updateConnnectionState(state) { if (this._connectState !== state) { if (state === FcrConnectionState.CONNECTED) { // 首次加入白板,白板回调connected时,boardView还为创建 if (this._boardView) { this._connectState = state; this._observable.notifyObservers('onConnectionStateUpdated', state); } } else if (state === FcrConnectionState.DISCONNECTED) { if (this._joined) { this.logger.info('whiteboard is disconnected, about to reconnect..'); sleep(5000).then(() => { if (this._joined) { this._connect(); } }); } else { this._connectState = state; this._observable.notifyObservers('onConnectionStateUpdated', state); } } else { this._connectState = state; this._observable.notifyObservers('onConnectionStateUpdated', state); } } } _handleConnectionStateUpdated(phase) { if (phase === RoomPhase.Connecting) { this._updateConnnectionState(FcrConnectionState.CONNECTING); } else if (phase === RoomPhase.Connected) { this._updateConnnectionState(FcrConnectionState.CONNECTED); } else if (phase === RoomPhase.Reconnecting) { this._updateConnnectionState(FcrConnectionState.RECONNECTING); } else if (phase === RoomPhase.Disconnected) { this._updateConnnectionState(FcrConnectionState.DISCONNECTED); } else if (phase === RoomPhase.Disconnecting) { // this._updateConnnectionState(FcrConnectionState.DISCONNECTING); } } async _connect() { try { this._updateConnnectionState(FcrConnectionState.CONNECTING); const roomId = this._scene.sceneId; const userId = this._scene.localUser.getLocalUserId(); const cursorName = this._scene.getUser(userId)?.userName; const syncMode = this._whiteboardPrivateParameters?.syncMode ?? false; const { data } = await this._getToken(roomId, userId); const { boardAppId, boardId, boardRegion, boardToken } = data || {}; const plugins = this._createPlugins(); this._client = new WhiteWebSdk({ appIdentifier: boardAppId, useMobXState: true, pptParams: { useServerWrap: true }, deviceType: DeviceType.Surface, plugins, preloadDynamicPPT: true, loggerOptions: { reportQualityMode: LoggerReportMode.AlwaysReport, reportDebugLogMode: LoggerReportMode.AlwaysReport, reportLevelMask: this._options.debug ? 'debug' : 'info', printLevelMask: this._options.debug ? 'debug' : 'info' }, ...this._whiteboardPrivateParameters }); const hasPermission = this._hasOperationPrivilege; const joinParams = { region: boardRegion, uuid: boardId, uid: userId, roomToken: boardToken, isWritable: hasPermission, disableDeviceInputs: false, disableCameraTransform: true, disableNewPencil: false, disableEraseImage: false, wrappedComponents: [], invisiblePlugins: [WindowManager, ApplianceMultiPlugin], useMultiViews: true, disableMagixEventDispatchLimit: true, userPayload: { userId, avatar: '', cursorName, disappearCursor: true }, floatBar: this._whiteboardPrivateParameters?.floatBar ?? { colors: textColors.map(color => hexColorToWhiteboardColor(color)) } }; const room = await this._client.joinRoom(joinParams, { onPhaseChanged: this._handleConnectionStateUpdated, onRoomStateChanged: this._handleRoomStateUpdated }); this._room = room; if (hasPermission) { room.setViewMode(ViewMode.Broadcaster); } this.logger.info(`set whiteboard sync mode: ${syncMode}`); room.syncMode = syncMode; const mountOptions = {}; const privateParams = this._whiteboardPrivateParameters; if (!this._boardView) { const mainWindow = this._createBoardMainWindow(room); this._boardView = mainWindow; // @ts-ignore await this._boardView._mount(mountOptions, privateParams); } else { this.logger.info('reusing main window for whiteboard'); //@ts-ignore this._boardView._setRoom(room, mountOptions, privateParams); } if (this._joined) { this._updateConnnectionState(FcrConnectionState.CONNECTED); } else { // 如果没有加入白板,则需要清理掉之前的状态 this.logger.info('whiteboard is not joined, cleaning up the state'); this._clean(); } } catch (e) { this.logger.error('error when connecting to whiteboard', e); this._updateConnnectionState(FcrConnectionState.DISCONNECTED); } return this._boardView; } _createBoardMainWindow(room) { this.logger.info('creating main window for whiteboard'); const mainWindow = new FcrBoardMainWindowImpl(room); return mainWindow; } async _clean() { if (this._boardView) { // @ts-ignore this._boardView.destroy(); this._boardView = undefined; } if (this._room) { try { // Workaround for whiteboard sdk issue: frameGenerator being null when disconnecting will cause error and trigger 'did-finish-loading' event in electron. // Set writable to false to prevent this error from throwing. // this.logger.info('change writable to false before disconnecting'); // await this._room.setWritable(false); await this._room.disconnect(); } catch (e) { this.logger.error('error when disconnecting whiteboard room', e); } finally { this._room.callbacks.off('onPhaseChanged', this._handleConnectionStateUpdated); this._room.callbacks.off('onRoomStateChanged', this._handleRoomStateUpdated); } this._room = undefined; } return FcrReturnCode.SUCCESS; } _addLogObserver() { this.addObserver(generateLogObserver(this.logger, [['onActive', ['ownerId', 'operatorUser']], ['onInactive', ['reason', 'operatorUser']], ['onBackgroundColorUpdated', ['color', 'operatorUser']], ['onConnectionStateUpdated', ['state']]])); } }