agora-edu-core
Version:
Core APIs for building an online classroom
893 lines (862 loc) • 35.5 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.SceneType = exports.LeaveReason = exports.ConnectionStore = void 0;
var _agoraRteSdk = require("agora-rte-sdk");
var _awaitToJs = _interopRequireDefault(require("await-to-js"));
var _mobx = require("mobx");
var _configs = require("../../../configs");
var _eventCenter = require("../../../event-center");
var _type = require("../../../type");
var _utils = require("../../../utils");
var _error = require("../../../utils/error");
var _base = require("./base");
var _error2 = require("../../../services/error");
var _utils2 = require("agora-rte-sdk/lib/core/utils/utils");
var _dec, _dec2, _dec3, _dec4, _dec5, _dec6, _dec7, _dec8, _dec9, _class, _class2, _descriptor, _descriptor2, _descriptor3, _descriptor4, _descriptor5, _descriptor6, _descriptor7, _descriptor8, _descriptor9, _descriptor0;
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = 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 _initializerDefineProperty(e, i, r, l) { r && Object.defineProperty(e, i, { enumerable: r.enumerable, configurable: r.configurable, writable: r.writable, value: r.initializer ? r.initializer.call(l) : void 0 }); }
function _applyDecoratedDescriptor(i, e, r, n, l) { var a = {}; return Object.keys(n).forEach(function (i) { a[i] = n[i]; }), a.enumerable = !!a.enumerable, a.configurable = !!a.configurable, ("value" in a || a.initializer) && (a.writable = !0), a = r.slice().reverse().reduce(function (r, n) { return n(i, e, r) || r; }, a), l && void 0 !== a.initializer && (a.value = a.initializer ? a.initializer.call(l) : void 0, a.initializer = void 0), void 0 === a.initializer ? (Object.defineProperty(i, e, a), null) : a; }
function _initializerWarningHelper(r, e) { throw Error("Decorating class property failed. Please ensure that transform-class-properties is enabled and runs after the decorators transform."); }
let LeaveReason = exports.LeaveReason = /*#__PURE__*/function (LeaveReason) {
LeaveReason[LeaveReason["leave"] = 0] = "leave";
LeaveReason[LeaveReason["kickOut"] = 1] = "kickOut";
return LeaveReason;
}({});
let SceneType = exports.SceneType = /*#__PURE__*/function (SceneType) {
SceneType[SceneType["Main"] = 0] = "Main";
SceneType[SceneType["Sub"] = 1] = "Sub";
return SceneType;
}({});
const classroomStateMap = {
[_type.ClassroomState.Connected]: 'connected',
[_type.ClassroomState.Connecting]: 'connecting',
[_type.ClassroomState.Error]: 'error',
[_type.ClassroomState.Idle]: 'idle',
[_type.ClassroomState.Reconnecting]: 'reconnecting'
};
const rtcStateMap = {
[_agoraRteSdk.AGRtcState.Idle]: 'idle',
[_agoraRteSdk.AGRtcState.Connected]: 'connected',
[_agoraRteSdk.AGRtcState.Connecting]: 'connecting',
[_agoraRteSdk.AGRtcState.Reconnecting]: 'reconnecting'
};
const connTypeMap = {
[_agoraRteSdk.AGRtcConnectionType.main]: 'main',
[_agoraRteSdk.AGRtcConnectionType.sub]: 'sub'
};
/**
* `ConnectionStore` 类用于管理连接状态。
*/
/** @en
* The `ConnectionStore` class is used for managing connection states.
*/
let ConnectionStore = exports.ConnectionStore = (_dec = _agoraRteSdk.Log.attach({
proxyMethods: false
}), _dec2 = _mobx.action.bound, _dec3 = _mobx.action.bound, _dec4 = _mobx.action.bound, _dec5 = _mobx.action.bound, _dec6 = _mobx.action.bound, _dec7 = _mobx.action.bound, _dec8 = _mobx.action.bound, _dec9 = _mobx.action.bound, _dec(_class = (_class2 = class ConnectionStore extends _base.EduStoreBase {
constructor() {
super(...arguments);
this._cancelJoin = void 0;
this._disposers = [];
this._rteRole = void 0;
/**
* 房间连接状态
*/
/** @en
* Room connection state
*/
_initializerDefineProperty(this, "classroomState", _descriptor, this);
/**
* @internal
*/
/** @en
* @internal
*/
_initializerDefineProperty(this, "subRoomState", _descriptor2, this);
/**
* @internal
*/
/** @en
* @internal
*/
_initializerDefineProperty(this, "classroomStateErrorReason", _descriptor3, this);
/**
* RTC 连接状态
*/
/** @en
* RTC connection state
*/
_initializerDefineProperty(this, "rtcState", _descriptor4, this);
/**
* @internal
*/
/** @en
* @internal
*/
_initializerDefineProperty(this, "rtcSubState", _descriptor5, this);
/**
* @internal
*/
/** @en
* @internal
*/
_initializerDefineProperty(this, "_mainRoomScene", _descriptor6, this);
/**
* @internal
*/
/** @en
* @internal
*/
_initializerDefineProperty(this, "_subRoomScene", _descriptor7, this);
/**
* RTE 引擎
*/
/** @en
* RTE engine
*/
_initializerDefineProperty(this, "engine", _descriptor8, this);
/**
* @internal
*/
/** @en
* @internal
*/
_initializerDefineProperty(this, "_mainRoomCheckInData", _descriptor9, this);
/**
* @internal
*/
/** @en
* @internal
*/
_initializerDefineProperty(this, "_subRoomCheckInData", _descriptor0, this);
/**
* @internal
*/
this._entryData = void 0;
}
/**
* 当前场景对象
**/
/** @en
* current scene object
*/
get scene() {
if (this._subRoomScene) {
return this._subRoomScene;
}
return this._mainRoomScene;
}
/**
* 当前场景 ID
**/
/** @en
* Current scene id
*/
get sceneId() {
return this.scene.sceneId;
}
/**
* @internal
*
* 大房间 scene
**/
/** @en
* @internal
*
* Scene of the main room
*/
get mainRoomScene() {
return this._mainRoomScene;
}
/**
* @internal
*
* 子房间 scene
**/
/** @en
* @internal
*
* Scene of the sub room
*/
get subRoomScene() {
return this._subRoomScene;
}
/**
* @internal
*
* 大房间接入房间数据
**/
/** @en
* @internal
*
* CheckIn data of the main room
*/
get mainRoomCheckInData() {
return this._mainRoomCheckInData;
}
/**
* @internal
*
* 子房间加入数据
**/
/** @en
* @internal
*
* CheckIn data of the sub room
*/
get subRoomCheckInData() {
return this._subRoomCheckInData;
}
/**
* 设置房间状态
* @param state 房间状态
* @param reason 原因
**/
/** @en
* Sets the room state
* @param state The room state
* @param reason The reason
*/
_setClassroomState(state, reason) {
if (this.classroomState !== state) {
this.logger.info(`classroom state changed to: ${classroomStateMap[state]} ${reason ? ', reason: ' + reason : ''}`);
if (state === _type.ClassroomState.Error) {
this.classroomStateErrorReason = reason;
}
this.classroomState = state;
}
}
/**
* 设置子房间状态
* @param state 子房间状态
* @param reason 原因
*
**/
/** @en
* Sets the sub-room state
* @param state The classroom state
* @param reason The reason
*/
_setSubRoomState(state, reason) {
if (this.subRoomState !== state) {
this.logger.info(`sub room state changed to: ${classroomStateMap[state]} ${reason ? ', reason: ' + reason : ''}`);
this.subRoomState = state;
}
}
/**
* 设置 RTC 状态
* @param state RTC 状态
* @param connectionType 大小房间
**/
/** @en
* Sets the RTC state
* @param state The RTC state
* @param connectionType Whether in the main room or in the sub-room
*
*/
_setRtcState(state, connectionType) {
const connType = connectionType ? connectionType : _agoraRteSdk.AGRtcConnectionType.main;
this.logger.info(`[${connTypeMap[connType]}] rtc state changed to: ${rtcStateMap[state]}`);
connType === _agoraRteSdk.AGRtcConnectionType.main ? this.rtcState = state : this.rtcSubState = state;
}
/**
* 设置 scene
* @param sceneType 大小房间类型
* @param scene
*
**/
/** @en
* Sets the current scene
* @param sceneType SceneType
* @param scene RteScene
*/
_setScene(sceneType, scene) {
if (sceneType === SceneType.Main) {
this._mainRoomScene = scene;
} else {
this._subRoomScene = scene;
}
}
/**
* 设置接入房间数据
* @param sceneType 房间类型
* @param checkInData 接入数据
*
**/
/** @en
* Sets the check-in data
* @param sceneType SceceType
* @param checkInData The check-in data
*/
_setCheckInData(sceneType, checkInData) {
if (sceneType === SceneType.Main) {
this.logger.info('set check-in data for classroom:', (0, _utils2.jsonstring)(checkInData));
this._mainRoomCheckInData = checkInData;
} else {
this.logger.info('set check-in data for subroom:', (0, _utils2.jsonstring)(checkInData));
this._subRoomCheckInData = checkInData;
}
}
/**
* 初始化 RTE engine
**/
/** @en
* Initializes RteEngine
*/
initialize() {
this.logger.info('start initializing connection store');
const ignoreUrlRegionPrefix = _configs.EduClassroomConfig.shared.rteEngineConfig.ignoreUrlRegionPrefix;
this.classroomStore.api.pathPrefix = `${ignoreUrlRegionPrefix ? '' : '/' + _configs.EduClassroomConfig.shared.rteEngineConfig.region.toLowerCase()}/edu/apps/${_configs.EduClassroomConfig.shared.appId}`;
this.logger.info('set edu api path prefix:', this.classroomStore.api.pathPrefix);
this.classroomStore.api.headers = _configs.EduClassroomConfig.shared.headers;
this.logger.info('set edu api headers:', (0, _utils2.jsonstring)(this.classroomStore.api.headers));
this._createEngine();
this.logger.info('done initializing connection store');
}
_createEngine() {
this.logger.info('create engine with config', (0, _utils2.jsonstring)(_configs.EduClassroomConfig.shared.rteEngineConfig));
return this.engine = _agoraRteSdk.AgoraRteEngine.createWithConfig(_configs.EduClassroomConfig.shared.rteEngineConfig);
}
/**
* 获取当前 RteEngine,调用此方法获取实例前需要先调用 connectionStore.initialize, 否则将抛出异常
* @returns RTE engine
**/
/** @en
* Gets instance of RTE Engine, it will throw if this method gets called before calling connectionStore.initialize
* @returns RTE engine
*/
getEngine() {
if (!this.engine) {
return _error.EduErrorCenter.shared.handleThrowableError(_error.AGEduErrorCode.EDU_ERR_RTE_ENGINE_NOT_READY, new Error(`engine not initialized, call initialize first`));
}
return this.engine;
}
/**
* 加入房间
*
**/
/** @en
* Joins the room
*/
async joinClassroom(options) {
var _this$_rteRole;
if (this.classroomState !== _type.ClassroomState.Idle) {
return _error.EduErrorCenter.shared.handleThrowableError(_error.AGEduErrorCode.EDU_ERR_INVALID_JOIN_CLASSROOM_STATE, new Error(`invalid join classroom state: ${this.classroomState}`));
}
this.logger.info('start joining classroom');
this._setClassroomState(_type.ClassroomState.Connecting);
const {
sessionInfo
} = _configs.EduClassroomConfig.shared;
this.logger.info('session info: ', (0, _utils2.jsonstring)(sessionInfo));
let [error] = ((options === null || options === void 0 ? void 0 : options.mode) || 'check-in') === 'entry' ? await (0, _awaitToJs.default)(this._entry(sessionInfo, SceneType.Main)) : await (0, _awaitToJs.default)(this.checkIn(sessionInfo, SceneType.Main));
if (error) {
this._setClassroomState(_type.ClassroomState.Idle);
this._throwEduJoinRoomError(error);
}
this.logger.info('done check-in');
// Because of the limitation of electron rtc engine creation, the rtc manager and the media control need to re-create to use a new electron rtc engine
if (_agoraRteSdk.AgoraRteEngineConfig.platform === _agoraRteSdk.AgoraRteRuntimePlatform.Electron) {
var _this$engine;
this.logger.info('start destroying electrong rte engine as region has retrived from edu server');
(_this$engine = this.engine) === null || _this$engine === void 0 ? void 0 : _this$engine.destroy();
this.logger.info('start create rte engine with configured region');
this._createEngine();
this.logger.info('done creating rte engine');
}
const engine = this.getEngine();
// try login rtm
[error] = await (0, _awaitToJs.default)((0, _agoraRteSdk.retryAttempt)(async () => {
this.logger.info('start engine login');
await engine.login(sessionInfo.token, sessionInfo.userUuid);
}, [], {
retriesMax: 10
}).fail(async _ref => {
let {
error,
timeFn,
currentRetry
} = _ref;
this.logger.error('join classroom engine-login failed, error:', error.message, ', current retry: ', currentRetry);
await timeFn();
return true;
}).exec());
if (error) {
this._setClassroomState(_type.ClassroomState.Idle);
this._throwEduJoinRoomError(error);
}
this.logger.info('done engine login-in');
const scene = engine.createAgoraRteScene(sessionInfo.roomUuid);
//listen to rte state change
scene.on(_agoraRteSdk.AgoraRteEventType.RteConnectionStateChanged, (state, reason) => {
this._setClassroomState(this._getClassroomState(state), reason);
});
//listen to rtc state change
scene.on(_agoraRteSdk.AgoraRteEventType.RtcConnectionStateChanged, (state, connectionType) => {
this._setRtcState(state, connectionType);
});
const userRole = (_this$_rteRole = this._rteRole) !== null && _this$_rteRole !== void 0 ? _this$_rteRole : (0, _utils.EduRole2RteRole)(sessionInfo.roomType, sessionInfo.role);
this._setScene(SceneType.Main, scene);
this.logger.info('start joining scene');
// streamId defaults to 0 means server allocate streamId for you
if (this._entryData) {
[error] = await (0, _awaitToJs.default)(scene.joinSceneWithInitialSnapshot(this._entryData));
} else {
[error] = await (0, _awaitToJs.default)(scene.joinScene({
userRole,
userName: sessionInfo.userName,
streamId: '0'
}));
}
if (error) {
this._setClassroomState(_type.ClassroomState.Idle);
this._throwEduJoinRoomError(error);
}
this.logger.info('done join-scene');
this._setClassroomState(_type.ClassroomState.Connected);
}
_throwEduJoinRoomError(error) {
_eventCenter.EduEventCenter.shared.emitClasroomEvents(_type.AgoraEduClassroomEvent.FailedToJoin, error);
if (error instanceof _agoraRteSdk.AGError) {
if (error.servCode === _error2.AGServiceErrorCode.SERV_ROOM_IS_END) {
_error.EduErrorCenter.shared.handleThrowableError(_error.AGEduErrorCode.EDU_ERR_JOIN_CLASSROOM_FAIL_ROOM_IS_END, error);
}
if (error.servCode === _error2.AGServiceErrorCode.SERV_ROOM_NOT_EXIST) {
_error.EduErrorCenter.shared.handleThrowableError(_error.AGEduErrorCode.EDU_ERR_JOIN_CLASSROOM_FAIL_ROOM_NOT_EXIST, error);
}
if (error.servCode === _error2.AGServiceErrorCode.SERV_ROLE_LIMIT_EXCEEDED) {
_error.EduErrorCenter.shared.handleThrowableError(_error.AGEduErrorCode.EDU_ERR_JOIN_CLASSROOM_FAIL_ROLE_LIMIT_EXCEEDED, error);
}
if (error.servCode === _error2.AGServiceErrorCode.SERV_PERSONS_LIMIT_EXCEEDED) {
_error.EduErrorCenter.shared.handleThrowableError(_error.AGEduErrorCode.EDU_ERR_JOIN_CLASSROOM_FAIL_PERSONS_LIMIT_EXCEEDED, error);
}
if (error.servCode === _error2.AGServiceErrorCode.SERV_CANNOT_JOIN_ROOM) {
_error.EduErrorCenter.shared.handleThrowableError(_error.AGEduErrorCode.EDU_ERR_JOIN_CLASSROOM_FAIL_USER_IS_BANNED, error);
}
if (error.original.name === 'RtmUnauthenticatedError' || error.codeList.includes(_agoraRteSdk.AGRteErrorCode.RTE_ERR_RESTFUL_UNAUTHORIZED_ERR)) {
_error.EduErrorCenter.shared.handleThrowableError(_error.AGEduErrorCode.EDU_ERR_JOIN_CLASSROOM_FAIL_RTM_UNAUTHENTICATED_ERROR, error);
}
}
_error.EduErrorCenter.shared.handleThrowableError(_error.AGEduErrorCode.EDU_ERR_JOIN_CLASSROOM_FAIL, error);
}
_throwEduJoinSubRoomError(error) {
_error.EduErrorCenter.shared.handleThrowableError(_error.AGEduErrorCode.EDU_ERR_GROUP_JOIN_SUBROOM_FAIL, error);
}
async entry(sessionInfo, sceneType) {
this._entry(sessionInfo, sceneType);
}
/**
* 进入房间预检,进入房间前请先调用此方法
* @param sessionInfo 当前房间数据
* @param sceneType scene 类型
*
**/
/** @en
* Precheck before joining the room, call this method before joining room.
* @param sessionInfo session info
* @param sceneType sceneType
*/
async checkIn(sessionInfo, sceneType) {
let mode = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'check-in';
let data = {};
let ts = 0;
if (sceneType === SceneType.Main) {
if (mode === 'entry') {
({
data,
ts
} = await this.classroomStore.api.entry(sessionInfo));
} else {
({
data,
ts
} = await this.classroomStore.api.checkIn(sessionInfo));
}
} else {
({
data,
ts
} = await this.classroomStore.api.checkIn({
roomUuid: sessionInfo.roomUuid,
roomName: sessionInfo.roomName,
roomType: sessionInfo.roomType,
role: sessionInfo.role,
userUuid: sessionInfo.userUuid,
userName: sessionInfo.userName
}));
}
const {
state = 0,
startTime,
duration,
closeDelay = 0,
rtcRegion,
rtmRegion,
vid,
rteRole
} = data;
this._rteRole = rteRole;
_configs.EduClassroomConfig.shared.rteEngineConfig.setRtcRegion(rtcRegion);
this.logger.info('set rtc region: ', rtcRegion);
_configs.EduClassroomConfig.shared.rteEngineConfig.setRtmRegion(rtmRegion);
this.logger.info('set rtm region: ', rtmRegion);
this._setCheckInData(sceneType, {
vid,
clientServerTime: ts,
classRoomSchedule: {
state,
startTime,
duration,
closeDelay
},
rtcRegion,
rtmRegion
});
}
async _entry(sessionInfo, sceneType) {
var _this$engine2, _this$engine3;
const {
data,
ts
} = await this.classroomStore.api.entry(sessionInfo, {
videoSourceState: (_this$engine2 = this.engine) === null || _this$engine2 === void 0 ? void 0 : _this$engine2.getAgoraMediaControl().createCameraVideoTrack().state,
audioSourceState: (_this$engine3 = this.engine) === null || _this$engine3 === void 0 ? void 0 : _this$engine3.getAgoraMediaControl().createMicrophoneAudioTrack().state
});
this._entryData = data;
const {
localUser,
room,
sysConfig
} = data;
const rtcRegion = room.roomProperties.region.rtc;
const rtmRegion = room.roomProperties.region.rtm;
this._rteRole = localUser.role;
_configs.EduClassroomConfig.shared.rteEngineConfig.setRtcRegion(rtcRegion);
this.logger.info('set rtc region: ', rtcRegion);
_configs.EduClassroomConfig.shared.rteEngineConfig.setRtmRegion(rtmRegion);
this.logger.info('set rtm region: ', rtmRegion);
this._setCheckInData(sceneType, {
vid: sysConfig.vid,
clientServerTime: ts,
classRoomSchedule: {
state: room.roomProperties.state,
startTime: room.roomProperties.schedule.startTime,
duration: room.roomProperties.schedule.duration,
closeDelay: room.roomProperties.schedule.closeDelay
},
rtcRegion,
rtmRegion
});
}
/**
* 加入子房间
* @param roomUuid 子房间 ID
*
**/
/** @en
* Joins a sub-room
* @param roomUuid The sub-room ID
*/
async joinSubRoom(roomUuid) {
if (this.classroomState !== _type.ClassroomState.Connected) {
_error.EduErrorCenter.shared.handleThrowableError(_error.AGEduErrorCode.EDU_ERR_GROUP_CANNOT_JOIN, new Error(`cannot join group while classroom not connected`));
}
this.logger.info('start joining subroom');
const {
sessionInfo: baseSessionInfo
} = _configs.EduClassroomConfig.shared;
const sessionInfo = _objectSpread(_objectSpread({}, baseSessionInfo), {}, {
roomUuid,
roomType: _type.EduRoomTypeEnum.RoomGroup
});
this.logger.info('subroom session info:', (0, _utils2.jsonstring)(baseSessionInfo));
let [error] = await (0, _awaitToJs.default)((0, _agoraRteSdk.retryAttempt)(async () => {
this._setSubRoomState(_type.ClassroomState.Connecting);
await this.checkIn(sessionInfo, SceneType.Sub);
}, []).fail(async _ref2 => {
let {
error,
timeFn,
currentRetry
} = _ref2;
this.logger.error('join sub room check-in failed, error:', error.message, ', current retry: ', currentRetry);
await timeFn();
return true;
}).exec());
if (error) {
this._setSubRoomState(_type.ClassroomState.Idle);
this._throwEduJoinSubRoomError(error);
}
this.logger.info('done subroom check-in');
[error] = await (0, _awaitToJs.default)((0, _agoraRteSdk.retryAttempt)(async () => {
var _this$_rteRole2;
const engine = this.getEngine();
const scene = engine.createAgoraRteScene(sessionInfo.roomUuid);
//listen to rte state change
scene.on(_agoraRteSdk.AgoraRteEventType.RteConnectionStateChanged, (state, reason) => {
this._setSubRoomState(this._getClassroomState(state), reason);
});
//listen to rtc state change
scene.on(_agoraRteSdk.AgoraRteEventType.RtcConnectionStateChanged, (state, connectionType) => {
this._setRtcState(state, connectionType);
});
const userRole = (_this$_rteRole2 = this._rteRole) !== null && _this$_rteRole2 !== void 0 ? _this$_rteRole2 : (0, _utils.EduRole2RteRole)(sessionInfo.roomType, sessionInfo.role);
this._setScene(SceneType.Sub, scene);
this.logger.info('start joining subroom scene');
// streamId defaults to 0 means server allocate streamId for you
await scene.joinScene({
userRole,
userName: sessionInfo.userName,
streamId: '0'
});
}, []).fail(async _ref3 => {
let {
error,
timeFn,
currentRetry
} = _ref3;
this._setScene(SceneType.Sub, undefined);
this.logger.error(error.message);
this.logger.error('join sub room join-scene failed, error:', error.message, ', current retry: ', currentRetry);
await timeFn();
return true;
}).abort(() => {
this._setScene(SceneType.Sub, undefined);
}).exec());
if (error) {
this._setSubRoomState(_type.ClassroomState.Idle);
this._throwEduJoinSubRoomError(error);
}
this.logger.info('done subroom join-scene');
this._setSubRoomState(_type.ClassroomState.Connected);
}
/**
* 离开子房间
**/
/** @en
* Leaves the sub-room
*/
async leaveSubRoom() {
this.logger.info('start leaving subroom');
if (this.scene) {
if (this.rtcSubState !== _agoraRteSdk.AGRtcState.Idle) {
this.classroomStore.mediaStore.stopScreenShareCapture();
this.logger.info(`[${connTypeMap[_agoraRteSdk.AGRtcConnectionType.sub]}]leave rtc`);
await this.scene.leaveRTC(_agoraRteSdk.AGRtcConnectionType.sub);
this.logger.info(`[${connTypeMap[_agoraRteSdk.AGRtcConnectionType.sub]}]done leave rtc`);
}
this.logger.info(`[${connTypeMap[_agoraRteSdk.AGRtcConnectionType.main]}]leave rtc`);
await this.scene.leaveRTC();
this.logger.info(`[${connTypeMap[_agoraRteSdk.AGRtcConnectionType.main]}]done leave rtc`);
this.logger.info(`leave subroom scene`);
await this.scene.leaveScene();
this.logger.info('done leave subroom scene');
this._setScene(SceneType.Sub, undefined);
this._setSubRoomState(_type.ClassroomState.Idle);
}
}
/**
* 离开房间
* @param reason 原因
*
**/
/** @en
* Leaves the room
* @param reason The reason
*/
async leaveClassroom(reason) {
let promise = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : Promise.resolve();
this.logger.info('start leaving classroom');
await this._clean();
this.logger.info('done leaving classroom');
promise.then(() => _eventCenter.EduEventCenter.shared.emitClasroomEvents(_type.AgoraEduClassroomEvent.Destroyed, reason));
}
async _clean() {
var _this$scene;
let [err] = await (0, _awaitToJs.default)(this.leaveRTC());
err && _error.EduErrorCenter.shared.handleNonThrowableError(_error.AGEduErrorCode.EDU_ERR_LEAVE_CLASSROOM_FAIL, err);
[err] = await (0, _awaitToJs.default)(((_this$scene = this.scene) === null || _this$scene === void 0 ? void 0 : _this$scene.leaveScene()) || Promise.resolve());
err && _error.EduErrorCenter.shared.handleNonThrowableError(_error.AGEduErrorCode.EDU_ERR_LEAVE_CLASSROOM_FAIL, err);
this._setClassroomState(_type.ClassroomState.Idle);
this._setScene(SceneType.Main, undefined);
if (!this.engine) {
return;
}
this.logger.info('destorying rte engine');
this.engine.destroy();
this.logger.info('done destorying rte engine');
this.classroomStore.logReporter.uploadLog();
}
/**
* 加入 RTC 频道
* @param options RTC 配置
*
**/
/** @en
* Joins an RTC channel
* @param options The RTC options
*/
async joinRTC(options) {
var _this$scene2;
//join rtc
const [err] = await (0, _awaitToJs.default)(((_this$scene2 = this.scene) === null || _this$scene2 === void 0 ? void 0 : _this$scene2.joinRTC(options)) || Promise.resolve());
err && _error.EduErrorCenter.shared.handleThrowableError(_error.AGEduErrorCode.EDU_ERR_JOIN_RTC_FAIL, err);
_eventCenter.EduEventCenter.shared.emitClasroomEvents(_type.AgoraEduClassroomEvent.Ready);
}
/**
* 离开 RTC 频道
* @param connectionType 大小 scene
*
**/
/** @en
* Leaves the RTC channel
* @param connectionType connection type
*/
async leaveRTC(connectionType) {
var _this$scene3;
//leave rtc
const [err] = await (0, _awaitToJs.default)(((_this$scene3 = this.scene) === null || _this$scene3 === void 0 ? void 0 : _this$scene3.leaveRTC(connectionType)) || Promise.resolve());
err && _error.EduErrorCenter.shared.handleNonThrowableError(_error.AGEduErrorCode.EDU_ERR_LEAVE_RTC_FAIL, err);
}
/**
* 获取房间 RTC 状态
* @param connectionType
* @returns
*
**/
/** @en
* Gets the RTC state
* @param connectionType
* @returns
*/
getRtcState(connectionType) {
return connectionType === _agoraRteSdk.AGRtcConnectionType.main ? this.rtcState : this.rtcSubState;
}
/**
* 添加一个云代理观察者,joinClassroom 成功调用后才能调用此方法
* @param observer
*/
/** @en
* Adds an observer for cloud proxy, this method can only be called after joinClassroom succeeds
* @param observer
*/
addCloudProxyObserver(observer) {
var _this$mainRoomScene;
(_this$mainRoomScene = this.mainRoomScene) === null || _this$mainRoomScene === void 0 ? void 0 : _this$mainRoomScene.addCloudProxyObserver(observer);
}
/**
* 移除一个云代理观察者,joinClassroom 成功调用后才能调用此方法
* @param observer
*/
/** @en
* Removes an observer for cloud proxy, this method can only be called after joinClassroom succeeds
* @param observer
*/
removeCloudProxyObserver(observer) {
var _this$mainRoomScene2;
(_this$mainRoomScene2 = this.mainRoomScene) === null || _this$mainRoomScene2 === void 0 ? void 0 : _this$mainRoomScene2.removeCloudProxyObserver(observer);
}
_getClassroomState(state) {
switch (state) {
case _agoraRteSdk.AgoraRteConnectionState.Idle:
return _type.ClassroomState.Idle;
case _agoraRteSdk.AgoraRteConnectionState.Connecting:
return _type.ClassroomState.Connecting;
case _agoraRteSdk.AgoraRteConnectionState.Connected:
return _type.ClassroomState.Connected;
case _agoraRteSdk.AgoraRteConnectionState.Reconnecting:
return _type.ClassroomState.Reconnecting;
case _agoraRteSdk.AgoraRteConnectionState.Error:
return _type.ClassroomState.Error;
}
}
/**
* @internal
*/
/**
* @internal
*/
onInstall() {
this._disposers.push((0, _mobx.reaction)(() => this.rtcState, rtcState => {
_eventCenter.EduEventCenter.shared.emitClasroomEvents(_type.AgoraEduClassroomEvent.RTCStateChanged, rtcState);
}));
}
/**
* @internal
*/
/**
* @internal
*/
onDestroy() {
if (this._cancelJoin) {
this._cancelJoin();
}
this._clean();
this._disposers.forEach(d => d());
this._disposers = [];
}
}, _descriptor = _applyDecoratedDescriptor(_class2.prototype, "classroomState", [_mobx.observable], {
configurable: true,
enumerable: true,
writable: true,
initializer: function () {
return _type.ClassroomState.Idle;
}
}), _descriptor2 = _applyDecoratedDescriptor(_class2.prototype, "subRoomState", [_mobx.observable], {
configurable: true,
enumerable: true,
writable: true,
initializer: function () {
return _type.ClassroomState.Idle;
}
}), _descriptor3 = _applyDecoratedDescriptor(_class2.prototype, "classroomStateErrorReason", [_mobx.observable], {
configurable: true,
enumerable: true,
writable: true,
initializer: null
}), _descriptor4 = _applyDecoratedDescriptor(_class2.prototype, "rtcState", [_mobx.observable], {
configurable: true,
enumerable: true,
writable: true,
initializer: function () {
return _agoraRteSdk.AGRtcState.Idle;
}
}), _descriptor5 = _applyDecoratedDescriptor(_class2.prototype, "rtcSubState", [_mobx.observable], {
configurable: true,
enumerable: true,
writable: true,
initializer: function () {
return _agoraRteSdk.AGRtcState.Idle;
}
}), _descriptor6 = _applyDecoratedDescriptor(_class2.prototype, "_mainRoomScene", [_mobx.observable], {
configurable: true,
enumerable: true,
writable: true,
initializer: null
}), _descriptor7 = _applyDecoratedDescriptor(_class2.prototype, "_subRoomScene", [_mobx.observable], {
configurable: true,
enumerable: true,
writable: true,
initializer: null
}), _descriptor8 = _applyDecoratedDescriptor(_class2.prototype, "engine", [_mobx.observable], {
configurable: true,
enumerable: true,
writable: true,
initializer: null
}), _descriptor9 = _applyDecoratedDescriptor(_class2.prototype, "_mainRoomCheckInData", [_mobx.observable], {
configurable: true,
enumerable: true,
writable: true,
initializer: null
}), _descriptor0 = _applyDecoratedDescriptor(_class2.prototype, "_subRoomCheckInData", [_mobx.observable], {
configurable: true,
enumerable: true,
writable: true,
initializer: null
}), _applyDecoratedDescriptor(_class2.prototype, "scene", [_mobx.computed], Object.getOwnPropertyDescriptor(_class2.prototype, "scene"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "mainRoomScene", [_mobx.computed], Object.getOwnPropertyDescriptor(_class2.prototype, "mainRoomScene"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "subRoomScene", [_mobx.computed], Object.getOwnPropertyDescriptor(_class2.prototype, "subRoomScene"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "mainRoomCheckInData", [_mobx.computed], Object.getOwnPropertyDescriptor(_class2.prototype, "mainRoomCheckInData"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "subRoomCheckInData", [_mobx.computed], Object.getOwnPropertyDescriptor(_class2.prototype, "subRoomCheckInData"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "_setClassroomState", [_dec2], Object.getOwnPropertyDescriptor(_class2.prototype, "_setClassroomState"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "_setSubRoomState", [_dec3], Object.getOwnPropertyDescriptor(_class2.prototype, "_setSubRoomState"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "_setRtcState", [_dec4], Object.getOwnPropertyDescriptor(_class2.prototype, "_setRtcState"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "_setScene", [_mobx.action], Object.getOwnPropertyDescriptor(_class2.prototype, "_setScene"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "_setCheckInData", [_mobx.action], Object.getOwnPropertyDescriptor(_class2.prototype, "_setCheckInData"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "initialize", [_dec5], Object.getOwnPropertyDescriptor(_class2.prototype, "initialize"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "_createEngine", [_dec6], Object.getOwnPropertyDescriptor(_class2.prototype, "_createEngine"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "joinClassroom", [_dec7], Object.getOwnPropertyDescriptor(_class2.prototype, "joinClassroom"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "joinSubRoom", [_dec8], Object.getOwnPropertyDescriptor(_class2.prototype, "joinSubRoom"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "leaveSubRoom", [_dec9], Object.getOwnPropertyDescriptor(_class2.prototype, "leaveSubRoom"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "leaveClassroom", [_agoraRteSdk.bound], Object.getOwnPropertyDescriptor(_class2.prototype, "leaveClassroom"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "joinRTC", [_agoraRteSdk.bound], Object.getOwnPropertyDescriptor(_class2.prototype, "joinRTC"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "leaveRTC", [_agoraRteSdk.bound], Object.getOwnPropertyDescriptor(_class2.prototype, "leaveRTC"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "addCloudProxyObserver", [_agoraRteSdk.bound], Object.getOwnPropertyDescriptor(_class2.prototype, "addCloudProxyObserver"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "removeCloudProxyObserver", [_agoraRteSdk.bound], Object.getOwnPropertyDescriptor(_class2.prototype, "removeCloudProxyObserver"), _class2.prototype), _class2)) || _class);