agora-edu-core
Version:
Core APIs for building an online classroom
156 lines (154 loc) • 7.39 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.Platform = exports.EduClassroomConfig = void 0;
var _agoraRteSdk = require("agora-rte-sdk");
var _merge = _interopRequireDefault(require("lodash/merge"));
var _error = require("../utils/error");
var _EduClassroomConfig;
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); }
let Platform = exports.Platform = /*#__PURE__*/function (Platform) {
Platform["PC"] = "PC";
Platform["H5"] = "H5";
return Platform;
}({});
class EduClassroomConfig {
static get shared() {
if (!this._classroomConfig) {
return _error.EduErrorCenter.shared.handleThrowableError(_error.AGEduErrorCode.EDU_ERR_CLASSROOM_CONFIG_NOT_READY, new Error(`classroom config not ready`));
}
return this._classroomConfig;
}
static setConfig(config) {
this._classroomConfig = config;
}
get headers() {
var _this$sessionInfo, _this$sessionInfo2;
return {
'Content-Type': 'application/json',
'x-agora-token': ((_this$sessionInfo = this.sessionInfo) === null || _this$sessionInfo === void 0 ? void 0 : _this$sessionInfo.token) || '',
'x-agora-uid': ((_this$sessionInfo2 = this.sessionInfo) === null || _this$sessionInfo2 === void 0 ? void 0 : _this$sessionInfo2.userUuid) || '',
Authorization: `agora token="${this.sessionInfo.token}"`
};
}
constructor(appId, sessionInfo, recordUrl, rteOpts) {
var _options$openCameraDe, _options$openRecordin, _options$recordRetryT;
let platform = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : Platform.PC;
let options = arguments.length > 5 && arguments[5] !== undefined ? arguments[5] : {};
this.appId = void 0;
this.recordUrl = void 0;
this.platform = void 0;
this._rteEngineConfig = void 0;
this._sessionInfo = void 0;
this._currentAPIVersion = 'v3';
this._compatibleVersions = [];
this._openCameraDeviceAfterLaunch = void 0;
this._openRecordingDeviceAfterLaunch = void 0;
this._recordRetryTimeout = void 0;
this.boardDefaults = {
scale: 1.2
};
this.appId = appId;
this.platform = platform;
this._sessionInfo = sessionInfo;
this.recordUrl = recordUrl;
this._openCameraDeviceAfterLaunch = (_options$openCameraDe = options.openCameraDeviceAfterLaunch) !== null && _options$openCameraDe !== void 0 ? _options$openCameraDe : true;
this._openRecordingDeviceAfterLaunch = (_options$openRecordin = options.openRecordingDeviceAfterLaunch) !== null && _options$openRecordin !== void 0 ? _options$openRecordin : true;
this._recordRetryTimeout = (_options$recordRetryT = options.recordRetryTimeout) !== null && _options$recordRetryT !== void 0 ? _options$recordRetryT : 60;
const rtcConfigs = (0, _merge.default)({
defaultCameraEncoderConfigurations: EduClassroomConfig.defaultMediaOptions(sessionInfo.roomType, sessionInfo.role)
}, rteOpts === null || rteOpts === void 0 ? void 0 : rteOpts.rtcConfigs);
const rteOptions = _objectSpread(_objectSpread({}, rteOpts), {}, {
rtcConfigs,
rtcSDKParameters: [{
'rtc.report_app_scenario': {
appScenario: sessionInfo.roomType,
serviceType: 0,
appVersion: EDU_SDK_VERSION
}
}].concat((rteOpts === null || rteOpts === void 0 ? void 0 : rteOpts.rtcSDKParameters) || [])
});
this._rteEngineConfig = new _agoraRteSdk.AgoraRteEngineConfig(appId, rteOptions);
if (this.sessionInfo) {
this._rteEngineConfig.token = this.sessionInfo.token;
}
_agoraRteSdk.AgoraRteEngineConfig.setConfig(this._rteEngineConfig);
_agoraRteSdk.Logger.info(`[EduClassroomConfig] core configurations initialized, rte: v${_agoraRteSdk.AgoraRteEngine.getVersion()}, core: ${EduClassroomConfig.getVersion()}`);
//@ts-ignore
window.EduClassroomConfig = this;
}
get sessionInfo() {
if (!this._sessionInfo) {
return _error.EduErrorCenter.shared.handleThrowableError(_error.AGEduErrorCode.EDU_ERR_SESSION_INFO_NOT_READY, new Error(`session info is undefined, not logged in?`));
}
// handleThrowableError will throw an error so it's not possible to return undefined here
return this._sessionInfo;
}
setCompatibleVersions(compatibleVersions) {
this._compatibleVersions = compatibleVersions;
}
get host() {
return this._rteEngineConfig.service.host;
}
set host(host) {
this._rteEngineConfig.service.host = host;
_agoraRteSdk.Logger.info('[EduClassroomConfig] set host to:', host);
}
get ignoreUrlRegionPrefix() {
return this._rteEngineConfig.ignoreUrlRegionPrefix;
}
set ignoreUrlRegionPrefix(ignoreUrlRegionPrefix) {
this._rteEngineConfig.ignoreUrlRegionPrefix = ignoreUrlRegionPrefix;
this._rteEngineConfig.domainPoolDisabled = ignoreUrlRegionPrefix;
_agoraRteSdk.Logger.info(`[EduClassroomConfig] set ignoreUrlRegionPrefix to:`, ignoreUrlRegionPrefix);
_agoraRteSdk.Logger.info(`[EduClassroomConfig] set domainPoolDisabled to:`, ignoreUrlRegionPrefix);
}
get isLowAPIVersionCompatibleRequired() {
return this._compatibleVersions.some(v => {
return v < this._currentAPIVersion;
});
}
get rteEngineConfig() {
return this._rteEngineConfig;
}
get openCameraDeviceAfterLaunch() {
return this._openCameraDeviceAfterLaunch;
}
get openRecordingDeviceAfterLaunch() {
return this._openRecordingDeviceAfterLaunch;
}
get recordRetryTimeout() {
return this._recordRetryTimeout;
}
static getVersion() {
return EDU_SDK_VERSION;
}
static getRtcVersion() {
return _agoraRteSdk.AgoraRteEngine.getRtcVersion();
}
static defaultMediaOptions(roomType, userRole) {
return {
width: 640,
height: 480,
frameRate: 15,
bitrate: 800
};
}
static defaultLowStreamParameter() {
return {
width: 320,
height: 240,
frameRate: 15,
bitrate: 200
};
}
}
exports.EduClassroomConfig = EduClassroomConfig;
_EduClassroomConfig = EduClassroomConfig;
EduClassroomConfig._classroomConfig = void 0;