UNPKG

@class100/webrtc

Version:

TODO

608 lines 21.2 kB
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, autorun, computed, reaction, when } from 'mobx'; import { EventEmitter } from 'events'; import lodash from 'lodash'; import Enums, { RtcTypes } from './common/Enums'; import * as utils from './utils'; import RtcLocalView from './RtcLocalView'; import RtcRemoteView from './RtcRemoteView'; import { AgoraWeb } from './libs/agora_web'; import { TRTCWeb } from './libs/TRTC_web'; class WebRTCSDk { /** * rtc 类型 * @param rtcType */ constructor(type, options) { /** * event事件监听对像 */ this.ee = undefined; /** * 当前RTC类型 */ this.rtcType = undefined; /** * 当前RTC实体 */ this.client = undefined; /** * RTC配置 */ this.config = undefined; /** * 本地流 */ this.localStream = undefined; /** * 屏幕共享流 */ this.screenStream = undefined; /** * 本地禁推视频流 */ this.localStreamVideoIsDisable = false; /** * 本地禁推音频流 */ this.localStreamAudioDisable = false; /** * 是否加入房间 */ this.joined = false; /** * 是否推送 */ this.published = false; /** * 远程用户流 */ this.remoteUsers = undefined; try { if (!RtcTypes[type]) { throw `请选择相应webRTC平台(${type})`; } makeAutoObservable(this); this.ee = new EventEmitter(); this.rtcType = type; this.config = options; this.init(options); } catch (err) { throw err; } } setClient(client) { this.client = client; } setJoined(isJoined) { this.joined = isJoined; } setLocalStream(localStream) { this.localStream = localStream; } setScreenStream(screenStream) { this.screenStream = screenStream; } setLocalStreamVideoDisable(stop) { this.localStreamVideoIsDisable = !!stop; } setLocalStreamAudioDisable(stop) { this.localStreamAudioDisable = !!stop; } setPublished(isPublished) { this.published = isPublished; } setRemoteUsers(remoteUsers) { this.remoteUsers = remoteUsers; } /** * 初始化客户端对象。 */ init(options) { return __awaiter(this, void 0, void 0, function* () { this.config = options; let client; switch (this.rtcType) { case RtcTypes.AgoraWeb: client = new AgoraWeb(); // this.client = AgoraWeb.createClient(); break; case RtcTypes.TRTCWeb: client = new TRTCWeb(this.config); // this.client = TRTCWeb.createClient(parseInt(options.appid), `${options.uid}`, options.token); break; default: break; } if (client) { this.setClient(client); autorun(() => { var _a, _b, _c, _d; try { switch (this.rtcType) { case RtcTypes.AgoraWeb: this.setRemoteUsers((_b = (_a = this.client) === null || _a === void 0 ? void 0 : _a.remoteUsers) === null || _b === void 0 ? void 0 : _b.map((item) => { return { uid: item.uid, remoteStream: item, }; })); break; case RtcTypes.TRTCWeb: this.setRemoteUsers((_d = (_c = this.client) === null || _c === void 0 ? void 0 : _c.remoteUsers) === null || _d === void 0 ? void 0 : _d.map((item) => { return { uid: item.getUserId(), remoteStream: item, }; })); break; default: break; } } catch (err) { } }); } else { throw new Error('未成功初始化'); } return this.client; }); } join() { return __awaiter(this, void 0, void 0, function* () { const conf = this.config; let isJoin = false; switch (this.rtcType) { case RtcTypes.AgoraWeb: isJoin = yield this.client.join(conf.appid, conf.channel, conf.token, parseInt(`${conf.uid}`)); break; case RtcTypes.TRTCWeb: // @ts-ignore isJoin = yield this.client.join(conf.channel); break; default: break; } this.setJoined(!!isJoin); return this.joined; }); } createLocalStream(videoDeviceId, audioDeviceId) { return __awaiter(this, void 0, void 0, function* () { const conf = this.config; let localStream; switch (this.rtcType) { case RtcTypes.AgoraWeb: localStream = { uid: conf.uid, localStream: yield this.client.createLocalStream(videoDeviceId, audioDeviceId), }; break; case RtcTypes.TRTCWeb: localStream = { uid: conf.uid, localStream: yield this.client.createLocalStream(videoDeviceId, audioDeviceId), }; break; default: break; } this.setLocalStream(localStream); console.log('createRTCStream', localStream, this.localStream); return this.localStream; }); } startScreenSharing(config, callback) { return __awaiter(this, void 0, void 0, function* () { let shareStream; switch (this.rtcType) { case RtcTypes.AgoraWeb: break; case RtcTypes.TRTCWeb: // @ts-ignore shareStream = { uid: config.uid, // @ts-ignore localStream: this.client.startScreenSharing(config, callback) }; break; default: break; } return shareStream; }); } stopScreenSharing() { return __awaiter(this, void 0, void 0, function* () { switch (this.rtcType) { case RtcTypes.AgoraWeb: break; case RtcTypes.TRTCWeb: // @ts-ignore yield this.client.stopScreenSharing(); break; default: break; } }); } changeLocalStream(videoeDviceId, audioDeviceId) { return __awaiter(this, void 0, void 0, function* () { const conf = this.config; let localStream; let isPublish = false; if (!this.localStream) { throw new Error('请先调用createRTCStream'); } if (this.published) { // isPublish = true; // await this.unpublish(); } switch (this.rtcType) { case RtcTypes.AgoraWeb: localStream = { uid: conf.uid, localStream: yield this.client.changeLocalStream(videoeDviceId, audioDeviceId), }; break; case RtcTypes.TRTCWeb: localStream = { uid: conf.uid, localStream: yield this.client.changeLocalStream(videoeDviceId, audioDeviceId), }; break; default: break; } this.setLocalStream(localStream); console.log('createRTCStream', localStream, this.localStream); if (isPublish) { // await this.publish(); } return this.localStream; }); } publish() { return __awaiter(this, void 0, void 0, function* () { if (!(this.joined && this.localStream && !this.published)) { throw new Error('请确认是否加入房间,是否创建本地流,是否已推送'); } switch (this.rtcType) { case RtcTypes.AgoraWeb: yield this.client.publish(); break; case RtcTypes.TRTCWeb: yield this.client.publish(); break; default: break; } this.setPublished(true); }); } unpublish() { return __awaiter(this, void 0, void 0, function* () { if (!(this.joined && this.localStream && this.published)) { throw new Error('请确认是否加入房间,是否创建本地流,是否已推送'); } switch (this.rtcType) { case RtcTypes.AgoraWeb: yield this.client.unpublish(); break; case RtcTypes.TRTCWeb: yield this.client.unpublish(); break; default: break; } this.setPublished(false); }); } leave() { return __awaiter(this, void 0, void 0, function* () { const conf = this.config; if (this.published) { yield this.unpublish(); } switch (this.rtcType) { case RtcTypes.AgoraWeb: yield this.client.leave(); break; case RtcTypes.TRTCWeb: yield this.client.leave(); break; default: break; } this.setJoined(false); return this.joined; }); } clearLocalRTCStream() { return __awaiter(this, void 0, void 0, function* () { if (!this.localStream) { return; } switch (this.rtcType) { case RtcTypes.AgoraWeb: yield this.client.clearLocalRTCStream(); break; case RtcTypes.TRTCWeb: yield this.client.clearLocalRTCStream(); break; default: break; } this.localStream = undefined; }); } destroy() { return __awaiter(this, void 0, void 0, function* () { if (this.joined) { yield this.leave(); } if (this.localStream) { yield this.clearLocalRTCStream(); } this.client = null; this.config = null; this.client = null; this.joined = null; this.rtcType = null; }); } disableVideo(isDisable) { return __awaiter(this, void 0, void 0, function* () { this.setLocalStreamVideoDisable(isDisable); this.client.disableVideo(isDisable); }); } disableAudio(isDisable) { return __awaiter(this, void 0, void 0, function* () { this.setLocalStreamAudioDisable(isDisable); this.client.disableAudio(isDisable); }); } getDevices() { return __awaiter(this, void 0, void 0, function* () { let list = []; // switch (this.rtcType) { // case RtcTypes.AgoraWeb: // list = await this.getDevices(); // break; // case RtcTypes.TRTCWeb: // list = await this.getDevices(); // break; // default: // break; // } let audioinput = []; let videoinput = []; let audiooutput = []; const AgoraList = (yield AgoraWeb.getDevices()).map((item) => { const info = { type: 'agoraWeb', deviceId: item.deviceId, groupId: item.groupId, kind: item.kind, label: item.label, }; if (item.kind === 'audioinput') { audioinput.push(info); } if (item.kind === 'videoinput') { videoinput.push(info); } if (item.kind === 'audiooutput') { audiooutput.push(info); } return info; }); const TRTCList = (yield TRTCWeb.getDevices()).map((item) => { const info = { type: 'trtcWeb', deviceId: item.deviceId, groupId: item.groupId, kind: item.kind, label: item.label, }; if (item.kind === 'audioinput') { audioinput.push(info); } if (item.kind === 'videoinput') { videoinput.push(info); } if (item.kind === 'audiooutput') { audiooutput.push(info); } return info; }); console.log(AgoraList, TRTCList); audioinput = lodash.unionBy(audioinput, 'deviceId'); videoinput = lodash.unionBy(videoinput, 'deviceId'); audiooutput = lodash.unionBy(audiooutput, 'deviceId'); return [...audioinput, ...videoinput, ...audiooutput]; }); } /** * 检测本地webrtc数据 * @returns */ static doctor() { return __awaiter(this, void 0, void 0, function* () { let err = { msg: '', }; try { const getRequest = () => __awaiter(this, void 0, void 0, function* () { // @ts-ignore const microphoneState = yield navigator.permissions.query({ name: 'microphone' }); // @ts-ignore const cameraState = yield navigator.permissions.query({ name: 'camera' }); console.log('getRequest', microphoneState, cameraState); let isSuccess = (microphoneState === null || microphoneState === void 0 ? void 0 : microphoneState.state) === 'granted' && (cameraState === null || cameraState === void 0 ? void 0 : cameraState.state) === 'granted'; if (microphoneState.state === 'denied' || cameraState.state === 'denied') { err.msg = '音/视频权限被禁'; } return isSuccess; }); const tryOpenVideo = () => __awaiter(this, void 0, void 0, function* () { try { const constraints = { audio: true, video: { width: 1280, height: 720 }, }; if (!navigator.mediaDevices) { console.log('无法捕捉网络摄像头。'); err.msg = '无法捕捉网络摄像头。'; return false; } else { const st = yield navigator.mediaDevices.getUserMedia(constraints); st.getTracks().forEach(function (track) { track.stop(); }); return true; } } catch (err) { console.log(err); err.msg = `${err}`; return false; } }); let isRequest = yield getRequest(); if (!isRequest) { yield tryOpenVideo(); } isRequest = yield getRequest(); return isRequest === true ? true : err; } catch (error) { return err.msg = `${error}`; } return err; }); } } __decorate([ observable ], WebRTCSDk.prototype, "ee", void 0); __decorate([ observable ], WebRTCSDk.prototype, "rtcType", void 0); __decorate([ observable ], WebRTCSDk.prototype, "client", void 0); __decorate([ observable ], WebRTCSDk.prototype, "config", void 0); __decorate([ observable ], WebRTCSDk.prototype, "localStream", void 0); __decorate([ observable ], WebRTCSDk.prototype, "screenStream", void 0); __decorate([ observable ], WebRTCSDk.prototype, "localStreamVideoIsDisable", void 0); __decorate([ observable ], WebRTCSDk.prototype, "localStreamAudioDisable", void 0); __decorate([ observable ], WebRTCSDk.prototype, "joined", void 0); __decorate([ observable ], WebRTCSDk.prototype, "published", void 0); __decorate([ observable ], WebRTCSDk.prototype, "remoteUsers", void 0); __decorate([ action ], WebRTCSDk.prototype, "setClient", null); __decorate([ action ], WebRTCSDk.prototype, "setJoined", null); __decorate([ action ], WebRTCSDk.prototype, "setLocalStream", null); __decorate([ action ], WebRTCSDk.prototype, "setScreenStream", null); __decorate([ action ], WebRTCSDk.prototype, "setLocalStreamVideoDisable", null); __decorate([ action ], WebRTCSDk.prototype, "setLocalStreamAudioDisable", null); __decorate([ action ], WebRTCSDk.prototype, "setPublished", null); __decorate([ action ], WebRTCSDk.prototype, "setRemoteUsers", null); __decorate([ computed ], WebRTCSDk.prototype, "init", null); __decorate([ computed ], WebRTCSDk.prototype, "join", null); __decorate([ computed ], WebRTCSDk.prototype, "createLocalStream", null); __decorate([ computed ], WebRTCSDk.prototype, "startScreenSharing", null); __decorate([ computed ], WebRTCSDk.prototype, "stopScreenSharing", null); __decorate([ computed ], WebRTCSDk.prototype, "changeLocalStream", null); __decorate([ computed ], WebRTCSDk.prototype, "publish", null); __decorate([ computed ], WebRTCSDk.prototype, "unpublish", null); __decorate([ computed ], WebRTCSDk.prototype, "leave", null); __decorate([ computed ], WebRTCSDk.prototype, "clearLocalRTCStream", null); __decorate([ computed ], WebRTCSDk.prototype, "destroy", null); __decorate([ computed ], WebRTCSDk.prototype, "disableVideo", null); __decorate([ computed ], WebRTCSDk.prototype, "disableAudio", null); __decorate([ computed ], WebRTCSDk.prototype, "getDevices", null); // @ts-ignore window.WebRTCSDk = WebRTCSDk; export default WebRTCSDk; export { RtcLocalView, RtcRemoteView, utils, autorun, reaction, when, Enums }; //# sourceMappingURL=index.js.map