@class100/webrtc
Version:
TODO
396 lines • 14.9 kB
JavaScript
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
import { makeAutoObservable, observable, action, computed } from 'mobx';
import TRTC from 'trtc-js-sdk';
export class TRTCWeb {
constructor(options) {
this.client = undefined;
this.localStream = undefined;
this.remoteUsers = undefined;
this.uid = undefined;
this.conf = undefined;
this.shareStream = undefined;
this.shareClient = undefined;
makeAutoObservable(this);
this.conf = options;
this.createClient(parseInt(options.appid), `${options.uid}`, options.token);
}
createClient(appId, uid, token) {
console.log(token, appId, uid);
this.client = TRTC.createClient({
mode: 'rtc',
sdkAppId: appId,
userId: `${uid}`,
userSig: token,
useStringRoomId: true,
});
console.log(this.client);
this.uid = uid;
this.client.on('stream-added', event => {
const remoteStream = event.stream;
console.log('远端流增加: ' + remoteStream.getUserId());
this.handleUserPublished(remoteStream);
//订阅远端流
this.client.subscribe(remoteStream);
});
this.client.on('stream-removed', event => {
const remoteStream = event.stream;
this.handleUserUnpublished(remoteStream);
});
this.client.on('stream-updated', event => {
const remoteStream = event.stream;
console.log('远端流更新: ' + remoteStream.getUserId());
this.handleUserPublished(remoteStream);
//订阅远端流
this.client.subscribe(remoteStream);
});
this.client.on('stream-subscribed', event => {
const remoteStream = event.stream;
console.log('远端流订阅成功:' + remoteStream.getUserId());
// 播放远端流
// remoteStream.play('remote_stream-' + remoteStream.getUserId());
});
return this.client;
}
handleUserPublished(user) {
var _a, _b, _c, _d;
return __awaiter(this, void 0, void 0, function* () {
try {
yield ((_a = this.client) === null || _a === void 0 ? void 0 : _a.subscribe(user));
console.log(this);
const _listObj = {};
(_b = this.remoteUsers) === null || _b === void 0 ? void 0 : _b.forEach((item) => {
_listObj[`${item.getUserId()}`] = item;
});
_listObj[`${user.getUserId()}`] = user;
const list = [];
for (const key in _listObj) {
const element = _listObj[key];
list.push(element);
}
// fix 临时解决 音频播放 后面有时间改
(_c = user) === null || _c === void 0 ? void 0 : _c.muteAudio();
(_d = user) === null || _d === void 0 ? void 0 : _d.muteVideo();
this.remoteUsers = list;
}
catch (err) {
console.error('handleUserPublished err');
console.error(err);
}
});
}
handleUserUnpublished(user) {
var _a;
return __awaiter(this, void 0, void 0, function* () {
const _listObj = {};
(_a = this.remoteUsers) === null || _a === void 0 ? void 0 : _a.forEach((item) => {
_listObj[`${item.getUserId()}`] = item;
});
delete _listObj[`${user.getUserId()}`];
const list = [];
for (const key in _listObj) {
const element = _listObj[key];
list.push(element);
}
this.remoteUsers = list;
});
}
join(roomId) {
return __awaiter(this, void 0, void 0, function* () {
let code = false;
try {
console.info(this.client, roomId);
yield this.client.join({
roomId: roomId,
});
code = true;
}
catch (err) {
console.error(err);
code = false;
}
return code;
});
}
leave() {
return __awaiter(this, void 0, void 0, function* () {
let code = false;
try {
// await this.clearLocalRTCStream();
yield this.client.leave();
code = true;
}
catch (err) {
code = false;
}
return code;
});
}
createLocalStream(cameraId, microphoneId) {
return __awaiter(this, void 0, void 0, function* () {
let code = false;
let localStream = this.localStream;
try {
if (!localStream) {
localStream = TRTC.createStream({ userId: this.uid, audio: true, video: true, cameraId, microphoneId });
yield localStream
.initialize();
this.localStream = localStream;
}
else {
localStream.switchDevice('video', cameraId).then(() => {
console.log('switch camera success');
});
localStream.switchDevice('audio', microphoneId).then(() => {
console.log('switch camera success');
});
}
code = true;
}
catch (err) {
code = false;
}
return localStream;
});
}
startScreenSharing(options, callback) {
return __awaiter(this, void 0, void 0, function* () {
const shareClient = TRTC.createClient({ mode: 'rtc',
sdkAppId: parseInt(options.appid),
userId: `${options.uid}`,
userSig: options.token,
useStringRoomId: true,
});
this.shareClient = shareClient;
const shareStream = TRTC.createStream({
audio: false,
video: false,
screen: true,
userId: `${options.uid}`,
});
this.shareStream = shareStream;
try {
yield shareStream.initialize();
// 屏幕分享流监听屏幕分享停止事件
shareStream.on('screen-sharing-stopped', (event) => __awaiter(this, void 0, void 0, function* () {
this.stopScreenSharing();
callback('屏幕分享流监听屏幕分享停止事件');
}));
}
catch (e) {
console.error('屏幕分享流', e);
// 当屏幕分享流初始化失败时, 提醒用户并停止后续进房发布流程
switch (e.name) {
case 'NotReadableError':
this.stopScreenSharing();
// 提醒用户确保系统允许当前浏览器获取屏幕内容
callback('请确保系统允许当前浏览器获取屏幕内容');
return;
case 'NotAllowedError':
this.stopScreenSharing();
if (e.message === 'Permission denied by system') {
callback('请确保系统允许当前浏览器获取屏幕内容');
// 提醒用户确保系统允许当前浏览器获取屏幕内容
}
else {
// 用户拒绝/取消屏幕分享
callback('用户拒绝/取消屏幕分享');
}
return;
default:
this.stopScreenSharing();
// 初始化屏幕分享流时遇到了未知错误,提醒用户重试
callback('初始化屏幕分享流时遇到了未知错误');
return;
}
}
try {
yield shareClient.join({ roomId: options.channel });
// ShareClient join room success
}
catch (e) {
// ShareClient join room failed
callback('加入房间失败', e);
console.error('加入房间失败', e);
}
debugger;
try {
yield shareClient.publish(shareStream);
}
catch (e) {
console.error('推送流', e);
this.stopScreenSharing();
// ShareClient failed to publish local stream
}
return shareStream;
});
}
stopScreenSharing() {
return __awaiter(this, void 0, void 0, function* () {
if (this.shareStream && this.shareClient) {
try {
// 屏幕分享客户端停止推流
yield this.shareClient.unpublish(this.shareStream);
// 关闭屏幕分享流
this.shareStream.close();
// 屏幕分享客户端退房
yield this.shareClient.leave();
this.shareStream = undefined;
this.shareClient = undefined;
}
catch (err) {
}
}
});
}
changeLocalStream(cameraId, microphoneId) {
return __awaiter(this, void 0, void 0, function* () {
let code = false;
let localStream = this.localStream;
try {
if (!localStream) {
localStream = TRTC.createStream({ userId: this.uid, audio: true, video: true, cameraId, microphoneId });
yield localStream
.initialize();
this.localStream = localStream;
}
else {
localStream.switchDevice('video', cameraId).then(() => {
console.log('switch camera success');
});
localStream.switchDevice('audio', microphoneId).then(() => {
console.log('switch camera success');
});
}
this.localStream = localStream;
code = true;
}
catch (err) {
code = false;
}
return localStream;
});
}
clearLocalRTCStream() {
var _a;
return __awaiter(this, void 0, void 0, function* () {
if (this.localStream) {
// @ts-ignore
(_a = this.localStream) === null || _a === void 0 ? void 0 : _a.close();
}
this.localStream = undefined;
});
}
publish() {
return __awaiter(this, void 0, void 0, function* () {
let code = false;
try {
yield this.client.publish(this.localStream);
code = true;
}
catch (err) {
code = false;
console.error(err);
}
return code;
});
}
unpublish() {
return __awaiter(this, void 0, void 0, function* () {
let code = false;
try {
yield this.client.unpublish(this.localStream);
code = true;
}
catch (err) {
code = false;
}
return code;
});
}
static getDevices() {
return __awaiter(this, void 0, void 0, function* () {
return yield TRTC.getDevices();
});
}
disableVideo(disable) {
return __awaiter(this, void 0, void 0, function* () {
if (this.localStream) {
if (disable) {
yield this.localStream.muteVideo();
}
else {
yield this.localStream.unmuteVideo();
}
}
});
}
disableAudio(disable) {
return __awaiter(this, void 0, void 0, function* () {
if (this.localStream) {
if (disable) {
yield this.localStream.muteAudio();
}
else {
yield this.localStream.unmuteAudio();
}
}
});
}
}
__decorate([
observable
], TRTCWeb.prototype, "client", void 0);
__decorate([
observable
], TRTCWeb.prototype, "localStream", void 0);
__decorate([
observable
], TRTCWeb.prototype, "remoteUsers", void 0);
__decorate([
observable
], TRTCWeb.prototype, "uid", void 0);
__decorate([
observable
], TRTCWeb.prototype, "conf", void 0);
__decorate([
observable
], TRTCWeb.prototype, "shareStream", void 0);
__decorate([
observable
], TRTCWeb.prototype, "shareClient", void 0);
__decorate([
action
], TRTCWeb.prototype, "handleUserPublished", null);
__decorate([
action
], TRTCWeb.prototype, "handleUserUnpublished", null);
__decorate([
computed
], TRTCWeb.prototype, "createLocalStream", null);
__decorate([
computed
], TRTCWeb.prototype, "startScreenSharing", null);
__decorate([
computed
], TRTCWeb.prototype, "stopScreenSharing", null);
__decorate([
computed
], TRTCWeb.prototype, "changeLocalStream", null);
__decorate([
computed
], TRTCWeb.prototype, "clearLocalRTCStream", null);
//# sourceMappingURL=TRTC_web.js.map