fcr-core
Version:
Core APIs for building online scenes
460 lines (431 loc) • 22.2 kB
JavaScript
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, _renewUserTokenDecs, _createMainRoomControlDecs, _createInfinityRoomControlDecs, _createWaitingRoomControlDecs, _createRoomRouterDecs, _createRoomControlAndJoinDecs, _sendPeerMessageDecs, _setParametersDecs;
import "core-js/modules/es.array.includes.js";
import "core-js/modules/es.json.stringify.js";
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 { trace } from 'agora-foundation/lib/decorator';
import { retryAttempt } from 'agora-foundation/lib/utilities/async-retry';
import { getPlatform } from 'agora-foundation/lib/utilities/env';
import { ErrorModuleCode } from 'agora-foundation/lib/utilities/error/error-code';
import { AgoraObservable } from 'agora-foundation/lib/utilities/observable';
import { Mutex } from 'agora-foundation/lib/worker/mutex';
import { AgoraRestfulClientImpl, AgoraRteEngine, convertStreamTypeToPublishState } from 'agora-rte-sdk';
import { AgoraRtcRegion } from 'agora-rte-sdk/lib/core/rtc/type';
import { AgoraRtmRegion } from 'agora-rte-sdk/lib/core/rtm/type';
import { getSharedDomainHolder, resetSharedDomainHolder } from 'agora-rte-sdk/lib/core/services/domain-holder';
import to from 'await-to-js';
import { FcrConnectionState, FcrCoreEngineConfig } from '..';
import { FcrDesktopMediaControlImpl } from '../media-control/desktop';
import { FcrMobileMediaControlImpl } from '../media-control/mobile';
import { FcrMonitorControlImpl } from '../monitor-control';
import { FcrPeerSessionControlImpl } from '../peer-session';
import { FcrChatRoomControlImpl } from '../plugins/chat/chatroom';
import { FcrChatConnectorImpl } from '../plugins/chat/connector';
import { FcrMainRoomControlImpl } from '../room-control/mainroom-control';
import { FcrSharedCache } from '../room-control/shared-cache';
import { FcrRoomType } from '../room-control/type';
import { FcrWaitingRoomControlImpl } from '../room-control/waitingroom-control';
import { FcrRoomRouterImpl } from '../room-router';
import { booleanSchema, fcrRoomJoinOptionsSchema, stringKeyUnknownValueSchema, stringSchema } from '../schema';
import { FcrCoreServiceApi } from '../service/api';
import { FcrReturnCode, FcrUserRoleToStringMap } from '../type';
import { CMD_PEER_MESSAGE } from '../utilities/cmd';
import { ERROR_CODES_NOT_RETRYABLE_WHEN_JOINING_ROOM, FcrError, handleRequestError } from '../utilities/error';
import { FcrCoreLoggerManagerHolder, generateLogObserver } from '../utilities/logger';
import { getDependenciesInfo, getVersion } from '../utilities/package-info';
import { getCoreIpList, getCoreLogFileSize, getEasemobChatIpList, getEasemobRestIpList, isEndpointRegionDisabled } from '../utilities/parameters';
import validateParams from '../utilities/validate-params';
import { FcrInfinityRoomControlImpl } from '../room-control/infinity-room-control';
import { FcrRemoteControlImpl } from '../remote-control';
import { convertServerRoomTypeToFcrRoomType } from '../utilities/convertRoomType';
export class FcrCoreEngineImpl {
static {
[_initProto] = _applyDecs(this, [[trace, 2, "release"], [trace, 2, "login"], [trace, 2, "logout"], [_renewUserTokenDecs, 2, "renewUserToken"], [trace, 2, "getVersion"], [_createMainRoomControlDecs, 2, "createMainRoomControl"], [_createInfinityRoomControlDecs, 2, "createInfinityRoomControl"], [_createWaitingRoomControlDecs, 2, "createWaitingRoomControl"], [_createRoomRouterDecs, 2, "createRoomRouter"], [_createRoomControlAndJoinDecs, 2, "createRoomControlAndJoin"], [trace, 2, "getDesktopMediaControl"], [trace, 2, "getMobileMediaControl"], [trace, 2, "getMonitorControl"], [trace, 2, "getPeerSessionControl"], [trace, 2, "getRemoteControl"], [_sendPeerMessageDecs, 2, "sendPeerMessage"], [_setParametersDecs, 2, "setParameters"]], []).e;
}
// @internal
[(_renewUserTokenDecs = trace(['token'], true), _createMainRoomControlDecs = [trace(['roomId']), validateParams(stringSchema)], _createInfinityRoomControlDecs = [trace(['roomId']), validateParams(stringSchema)], _createWaitingRoomControlDecs = [trace(['roomId']), validateParams(stringSchema)], _createRoomRouterDecs = [trace(['roomId']), validateParams(stringSchema)], _createRoomControlAndJoinDecs = [trace(['roomId', 'options']), validateParams(stringSchema, fcrRoomJoinOptionsSchema)], _sendPeerMessageDecs = [trace(['payload', 'guaranteedDelivery', 'receiverId'], true), validateParams(stringKeyUnknownValueSchema, booleanSchema, stringSchema)], _setParametersDecs = [trace(['parameters'], true), validateParams(stringKeyUnknownValueSchema)], "logger")] = void _initProto(this);
// @internal
// @internal
// @internal
// @internal
// @internal
// @internal
// @internal
// @internal
// @internal
// @internal
_isLoggingIn = false;
// @internal
_sharedCache = new FcrSharedCache();
// @internal
_lock = new Mutex();
// @internal
_observable = new AgoraObservable();
// @internal
constructor(config) {
// 确保config是一个正确的FcrCoreEngineConfig实例,以
// 便即使调用者传递的是一个普通对象,也能始终应用_prependInternalParameters(rtc/rtm预设参数)。
if (!(config instanceof FcrCoreEngineConfig)) {
config = new FcrCoreEngineConfig(config);
}
this._config = config;
resetSharedDomainHolder();
const rteLogFileSize = getCoreLogFileSize(config.parameters);
FcrCoreLoggerManagerHolder.initialize({
maxSize: rteLogFileSize
});
this.logger = FcrCoreLoggerManagerHolder.createLogger({
prefix: 'FcrCoreEngine'
});
this.logger.info('Fcr core engine is created, version: ', this.getVersion());
this._rteEngine = new AgoraRteEngine({
appId: config.appId,
userId: config.userId,
userToken: config.token,
rteRegion: config.region,
rtcRegion: AgoraRtcRegion.AREA_GLOBAL,
rtmRegion: AgoraRtmRegion.AREA_GLOBAL,
audioConfig: {
profile: 'default',
scenario: 'meeting'
},
dualCameraVideoStreamConfig: config.dualCameraVideoStreamConfig,
dualScreenVideoStreamConfig: config.dualScreenVideoStreamConfig,
parameters: config.parameters
});
const domainHolder = getSharedDomainHolder(config.region);
this._restfulClient = new AgoraRestfulClientImpl(this._rteEngine.getHttpAuthHeadersProvider(), domainHolder, this._rteEngine.getRequestScheduler());
this._apiService = new FcrCoreServiceApi(this._restfulClient, config.appId, config.region, !isEndpointRegionDisabled(config.parameters));
// cannot be lazy loaded, because all the observers should be added before the engine is started
this._peerSessionControl = new FcrPeerSessionControlImpl(this._rteEngine, this._apiService, this._config.userId);
this._remoteControl = new FcrRemoteControlImpl(this._rteEngine, this._apiService, this._config.userId);
this._chatConnection = this._createChatConnection(config.parameters);
this._monitorControl = new FcrMonitorControlImpl(this._rteEngine, this._config);
this._desktopMediaControl = new FcrDesktopMediaControlImpl(this._rteEngine.getMediaControl());
this._mobileMediaControl = new FcrMobileMediaControlImpl(this._rteEngine.getMediaControl());
this._addLogObserver();
this._addRteEngineObserver();
this._setupCoreIpList(config.parameters);
this._setupChatConnection(config.parameters);
}
/**
* Releases the engine and all its resources.
*/
release() {
this._rteEngine.release();
return FcrReturnCode.SUCCESS;
}
/**
* Logins to the RTE service.
*/
async login() {
await this._lock.dispatch(async () => {
if (this._isLoggingIn) {
return FcrReturnCode.UNDEFINED;
}
try {
this._isLoggingIn = true;
this._chatConnection.login();
return await this._rteEngine.login();
} finally {
this._isLoggingIn = false;
if (this._monitorControl.needLogUpload) {
this._rteEngine.getMonitor().uploadLog({
userUuid: this._config.userId
});
} else {
this.logger.info('log upload is disabled');
}
}
});
}
/**
* Logouts from the RTE service.
*/
async logout() {
await this._lock.dispatch(async () => {
this._isLoggingIn = false;
this._chatConnection.logout();
return await this._rteEngine.logout();
});
}
/**
* Renews the user token.
* @param token
*/
renewUserToken(token) {
return this._rteEngine.renewUserToken(token);
}
/**
* Gets the version of the SDK.
* @returns The version of the SDK.
*/
getVersion() {
return getVersion();
}
getDependencyVersions() {
return {
...this._rteEngine.getDependencyVersions(),
rte: this._rteEngine.getVersion(),
whiteboard: getDependenciesInfo('@netless/forge-whiteboard'),
easemob: getDependenciesInfo('easemob-websdk')
};
}
createMainRoomControl(roomId) {
const sceneConfig = {
sceneId: roomId
};
const scene = this._rteEngine.createScene(sceneConfig);
return new FcrMainRoomControlImpl(this._rteEngine, scene, this._apiService, this._config, this._sharedCache, this._chatConnection, new FcrChatRoomControlImpl(scene, this._chatConnection, this._sharedCache, false), this._monitorControl);
}
/**
* Creates a room control.
* @param roomId
* @returns The room control.
*/
createInfinityRoomControl(roomId) {
const sceneConfig = {
sceneId: roomId
};
const scene = this._rteEngine.createScene(sceneConfig);
return new FcrInfinityRoomControlImpl(this._rteEngine, scene, this._apiService, this._config, this._sharedCache, this._chatConnection, new FcrChatRoomControlImpl(scene, this._chatConnection, this._sharedCache, false), this._monitorControl);
}
/**
* Creates a waiting room control.
* @param roomId
* @returns The room control.
*/
createWaitingRoomControl(roomId) {
const sceneConfig = {
sceneId: roomId
};
const scene = this._rteEngine.createScene(sceneConfig);
return new FcrWaitingRoomControlImpl(this._rteEngine, scene, this._apiService, this._config, this._sharedCache, this._chatConnection, new FcrChatRoomControlImpl(scene, this._chatConnection, this._sharedCache, false));
}
/**
* Creates room router.
* @param roomId
* @returns The room router.
*/
createRoomRouter(roomId) {
const roomRouter = new FcrRoomRouterImpl(this._rteEngine, this._apiService, this._config, this._chatConnection, this._sharedCache, roomId, this._monitorControl);
return roomRouter;
}
/**
* Creates a main room control or a waiting room control.
* @param roomId
* @returns The room control.
*/
async createRoomControlAndJoin(roomId, options) {
const {
userId
} = this._rteEngine.getConfig();
let roomControl = null;
let [error] = await to(retryAttempt(async () => {
const res = await handleRequestError(() => this._apiService.checkIn({
userName: options.userName,
userId,
userRole: FcrUserRoleToStringMap[options.userRole],
userProperties: options.userProperties,
roomId,
platform: getPlatform(),
streams: options.createStreamConfigs?.map(s => ({
videoSourceUuid: s.videoSourceId,
audioSourceUuid: s.audioSourceId,
streamName: s.streamName,
...convertStreamTypeToPublishState(s.streamType),
videoSourceType: s.videoSourceType,
audioSourceType: s.audioSourceType,
audioSourceState: s.audioSourceState,
videoSourceState: s.videoSourceState
})),
version: getVersion(),
password: options.password,
avatar: options.avatar
}), ErrorModuleCode.FCR_ENGINE);
const {
data,
ts
} = res;
const fcrRoomType = convertServerRoomTypeToFcrRoomType(data.room.roomProperties.roomType);
switch (fcrRoomType) {
case FcrRoomType.Waitingroom:
roomControl = this.createWaitingRoomControl(data.room.roomInfo.roomUuid);
break;
case FcrRoomType.Infinityroom:
roomControl = this.createInfinityRoomControl(roomId);
break;
case FcrRoomType.Mainroom:
roomControl = this.createMainRoomControl(roomId);
break;
default:
throw new Error(`Unknown room type: ${fcrRoomType}`);
}
await roomControl.join({
...options,
snapshot: data,
timestamp: ts,
createStreamConfigs: fcrRoomType === FcrRoomType.Waitingroom ? [] : options.createStreamConfigs
});
}, [], {
retriesMax: 10
}).fail(async ({
error,
timeFn,
currentRetry
}) => {
if (error instanceof FcrError && ERROR_CODES_NOT_RETRYABLE_WHEN_JOINING_ROOM.includes(error.code)) {
throw error;
}
this.logger.error(`retry to join room, ${error.message}, retry ${currentRetry} times`);
await timeFn();
return true;
}).exec());
if (error) {
this.logger.error(`join room failed, ${error.message}-${JSON.stringify(error)}`);
throw error;
}
return roomControl;
}
/**
* Gets the media control.
* @returns The media control.
*/
getDesktopMediaControl() {
return this._desktopMediaControl;
}
/**
* Gets the mobile media control.
* @returns The mobile media control.
*/
getMobileMediaControl() {
return this._mobileMediaControl;
}
/**
* Gets the monitor control.
* @returns The monitor control.
*/
getMonitorControl() {
return this._monitorControl;
}
/**
* Gets the peer session control.
* @returns The peer session control.
*/
getPeerSessionControl() {
return this._peerSessionControl;
}
/**
* Gets the remote control.
* @returns The remote control.
*/
getRemoteControl() {
return this._remoteControl;
}
/**
* Sends a peer message.
* @param payload
* @param guaranteedDelivery
* @param receiverId
*/
async sendPeerMessage(payload, guaranteedDelivery, receiverId) {
return await this._rteEngine.sendPeerMessage(payload, `${CMD_PEER_MESSAGE}`, guaranteedDelivery, receiverId);
}
/**
* Sets the parameters.
* @param parameters
*/
setParameters(parameters) {
try {
this._setupCoreIpList(parameters);
this._setupChatConnection(parameters);
const {
rte,
rtc,
rtm
} = parameters;
return this._rteEngine.setParameters({
rtc,
rtm,
rte
});
} catch (e) {
this.logger.warn('setParameters failed', e);
return FcrReturnCode.UNDEFINED;
}
}
/**
* Adds an observer.
* @param observer
*/
addObserver(observer) {
this._rteEngine.addObserver(observer);
}
/**
* Removes an observer.
* @param observer
*/
removeObserver(observer) {
this._rteEngine.removeObserver(observer);
}
_addRteEngineObserver() {
this._rteEngine.addObserver({
onConnectionStateUpdated: state => {
if (state === FcrConnectionState.ABORTED) {
this.logout();
}
},
onUserTokenWillExpire: () => {
this._observable.notifyObservers('onUserTokenWillExpire');
}
});
}
_addLogObserver() {
this.addObserver(generateLogObserver(this.logger, [['onConnectionStateUpdated', ['state']], ['onPeerMessageReceived', ['message']], 'onUserTokenWillExpire']));
}
_setupCoreIpList(parameters) {
const coreIpList = getCoreIpList(parameters);
if (coreIpList) {
this.logger.info(`setup fixed service domain list: ${JSON.stringify(coreIpList)}`);
resetSharedDomainHolder();
getSharedDomainHolder(this._config.region).setFixedDomainList(coreIpList);
this._restfulClient = new AgoraRestfulClientImpl(this._rteEngine.getHttpAuthHeadersProvider(), getSharedDomainHolder(this._config.region), this._rteEngine.getRequestScheduler());
this._apiService.setRestfulClient(this._restfulClient);
}
}
_setupChatConnection(parameters) {
const chatIpList = getEasemobChatIpList(parameters);
const restIpList = getEasemobRestIpList(parameters);
if (chatIpList && restIpList) {
this.logger.info(`setup chat ip list: ${JSON.stringify(chatIpList)}, rest ip list: ${JSON.stringify(restIpList)}`);
this._chatConnection = this._createChatConnection(parameters);
}
}
_createChatConnection(parameters) {
const chatIpList = getEasemobChatIpList(parameters);
const restIpList = getEasemobRestIpList(parameters);
return new FcrChatConnectorImpl(this._config.userId, this._apiService, chatIpList, restIpList);
}
}