123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346 |
- import Event from '../utils/event.js'
- import User from '../model/user.js'
- import Stream from '../model/stream.js'
- import { EVENT } from '../common/constants.js'
- 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)
-
- if (!user || !user.streams) {
- return
- }
-
- 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) {
- console.log(TAG_NAME, '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 streamID = userID + '_' + streamType
- const hasVideo = item.hasvideo
- const src = item.playurl
- const user = this.getUser(userID)
-
- if (user) {
-
- let stream = user.streams[streamType]
- console.log(TAG_NAME, 'updateUserVideo start', user, streamType, stream)
-
-
- if (!stream) {
-
- user.streams[streamType] = stream = new Stream({ userID, streamID, hasVideo, src, streamType })
- this._addStream(stream)
- } else {
-
- stream.setProperty({ hasVideo })
- if (!hasVideo && !stream.hasAudio) {
- this._removeStream(stream)
- }
-
-
-
-
-
-
-
- }
-
- if (streamType === 'aux') {
- if (hasVideo) {
-
- stream.objectFit = 'contain'
- this._addStream(stream)
- } else {
-
- this._removeStream(stream)
- }
- }
-
- this.userList.find((item)=>{
- if (item.userID === userID) {
- item[`has${streamType.replace(/^\S/, (s) => s.toUpperCase())}Video`] = hasVideo
- return true
- }
- })
- console.log(TAG_NAME, 'updateUserVideo end', user, streamType, stream)
- 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 streamID = userID + '_' + streamType
- 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({ userID, streamID, hasAudio, src, streamType })
- this._addStream(stream)
- } else {
-
- stream.setProperty({ hasAudio })
- if (!hasAudio && !stream.hasVideo) {
- this._removeStream(stream)
- }
-
-
-
-
-
-
-
- }
-
-
-
-
-
- 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
- })
- }
- _addStream(stream) {
- if (!this.streamList.includes(stream)) {
- this.streamList.push(stream)
- }
- }
- _removeStream(stream) {
- this.streamList = this.streamList.filter((item)=>{
- if (item.userID === stream.userID && item.streamType === stream.streamType) {
- return false
- }
- return true
- })
- const user = this.getUser(stream.userID)
- user.streams[stream.streamType] = undefined
- }
- }
- export default UserController
|