123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369 |
- import Event from "../utils/event";
- import User from "../model/user";
- import Stream from "../model/stream";
- import { EVENT } from "../common/constants";
- const TAG_NAME = 'UserController';
- class UserController {
- constructor(componentContext) {
-
- this.userMap = new Map();
- this.userList = [];
- this.streamList = [];
- this._emitter = new Event();
- this.componentContext = componentContext;
- this.isNewVersion = componentContext.isNewVersion;
- }
- userEventHandler(event) {
- const code = event.detail.code;
- let data;
- if (event.detail.message && typeof event.detail.message === 'string') {
- try {
- data = JSON.parse(event.detail.message);
- } catch (exception) {
- console.warn(TAG_NAME, 'userEventHandler 数据格式错误', exception);
- return false;
- }
- } else {
- console.warn(TAG_NAME, 'userEventHandler 数据格式错误');
- return false;
- }
- switch (code) {
- case 1020:
-
- if (!this.isNewVersion) {
- }
- break;
- case 1031:
-
-
-
-
-
-
-
-
-
- this.addUser(data);
- break;
- case 1032:
-
-
- this.removeUser(data);
- break;
- case 1033:
-
-
-
-
-
-
-
-
-
-
-
-
- this.updateUserVideo(data);
- break;
- case 1034:
-
-
-
-
-
-
-
-
-
-
-
- this.updateUserAudio(data);
- break;
- }
- }
-
- addUser(data) {
-
- const incomingUserList = data.userlist;
- const userMap = this.userMap;
- if (Array.isArray(incomingUserList) && incomingUserList.length > 0) {
- incomingUserList.forEach(item => {
- const userID = item.userid;
- let user = this.getUser(userID);
- if (!user) {
-
- user = new User({
- userID: userID
- });
- this.userList.push({
- userID: userID
- });
- }
- userMap.set(userID, user);
- this._emitter.emit(EVENT.REMOTE_USER_JOIN, {
- userID: userID,
- userList: this.userList
- });
- });
- }
- }
-
- removeUser(data) {
-
- const incomingUserList = data.userlist;
- if (Array.isArray(incomingUserList) && incomingUserList.length > 0) {
- incomingUserList.forEach(item => {
- const userID = item.userid;
- let user = this.getUser(userID);
- this._removeUserAndStream(userID);
- user.streams['main'] && user.streams['main'].reset();
- user.streams['aux'] && user.streams['aux'].reset();
-
- this._emitter.emit(EVENT.REMOTE_USER_LEAVE, {
- userID: userID,
- userList: this.userList,
- streamList: this.streamList
- });
- user = undefined;
- this.userMap.delete(userID);
- });
- }
- }
-
- updateUserVideo(data) {
-
- const incomingUserList = data.userlist;
- if (Array.isArray(incomingUserList) && incomingUserList.length > 0) {
- incomingUserList.forEach(item => {
-
- const userID = item.userid;
- const streamType = item.streamtype;
- const hasVideo = item.hasvideo;
- const src = item.playurl;
- const user = this.getUser(userID);
- if (user) {
- let stream = user.streams[streamType];
-
- if (!stream) {
- user.streams[streamType] = stream = new Stream({
- streamType: streamType
- });
- this.streamList.push(stream);
- }
- if (hasVideo && streamType === 'aux') {
- stream.objectFit = 'contain';
- }
- if (!hasVideo && streamType === 'aux') {
-
- this._removeStream(stream);
- } else {
- stream.userID = userID;
- stream.streamID = userID + '_' + streamType;
- stream.hasVideo = hasVideo;
- stream.src = src;
- }
- this.userList.find(item => {
- if (item.userID === userID) {
- item[`has${streamType.replace(/^\S/, s => s.toUpperCase())}Video`] = hasVideo;
- return true;
- }
- });
- const eventName = hasVideo ? EVENT.REMOTE_VIDEO_ADD : EVENT.REMOTE_VIDEO_REMOVE;
- this._emitter.emit(eventName, {
- stream: stream,
- streamList: this.streamList,
- userList: this.userList
- });
- }
- });
- }
- }
-
- updateUserAudio(data) {
-
- const incomingUserList = data.userlist;
- if (Array.isArray(incomingUserList) && incomingUserList.length > 0) {
- incomingUserList.forEach(item => {
- const userID = item.userid;
- const streamType = 'main';
- const hasAudio = item.hasaudio;
- const src = item.playurl;
- const user = this.getUser(userID);
- if (user) {
- let stream = user.streams[streamType];
- if (!stream) {
- user.streams[streamType] = stream = new Stream({
- streamType: streamType
- });
- this.streamList.push(stream);
- }
- stream.userID = userID;
- stream.streamID = userID + '_' + streamType;
- stream.hasAudio = hasAudio;
- stream.src = src;
- this.userList.find(item => {
- if (item.userID === userID) {
- item[`has${streamType.replace(/^\S/, s => s.toUpperCase())}Audio`] = hasAudio;
- return true;
- }
- });
- const eventName = hasAudio ? EVENT.REMOTE_AUDIO_ADD : EVENT.REMOTE_AUDIO_REMOVE;
- this._emitter.emit(eventName, {
- stream: stream,
- streamList: this.streamList,
- userList: this.userList
- });
- }
- });
- }
- }
-
- getUser(userID) {
- return this.userMap.get(userID);
- }
- getStream({
- userID,
- streamType
- }) {
- const user = this.userMap.get(userID);
- if (user) {
- return user.streams[streamType];
- }
- return undefined;
- }
- getUserList() {
- return this.userList;
- }
- getStreamList() {
- return this.streamList;
- }
-
- reset() {
- this.streamList.forEach(item => {
- item.reset();
- });
- this.streamList = [];
- this.userList = [];
- this.userMap.clear();
- return {
- userList: this.userList,
- streamList: this.streamList
- };
- }
- on(eventCode, handler, context) {
- this._emitter.on(eventCode, handler, context);
- }
- off(eventCode, handler) {
- this._emitter.off(eventCode, handler);
- }
-
- _removeUserAndStream(userID) {
- this.streamList = this.streamList.filter(item => {
- return item.userID !== userID && item.userID !== '';
- });
- this.userList = this.userList.filter(item => {
- return item.userID !== userID;
- });
- }
- _removeStream(stream) {
- this.streamList = this.streamList.filter(item => {
- if (item.userID === stream.userID && item.streamType === stream.streamType) {
- return false;
- }
- return true;
- });
- }
- }
- export default UserController;
|