|
@@ -1,7 +1,9 @@
|
|
|
import Event from '../utils/event.js'
|
|
|
import User from '../model/user.js'
|
|
|
import Stream from '../model/stream.js'
|
|
|
-import { EVENT } from '../common/constants.js'
|
|
|
+import {
|
|
|
+ EVENT
|
|
|
+} from '../common/constants.js'
|
|
|
|
|
|
const TAG_NAME = 'UserController'
|
|
|
/**
|
|
@@ -103,13 +105,18 @@ class UserController {
|
|
|
let user = this.getUser(userID)
|
|
|
if (!user) {
|
|
|
// 新增用户
|
|
|
- user = new User({ userID: userID })
|
|
|
+ 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 })
|
|
|
+ this._emitter.emit(EVENT.REMOTE_USER_JOIN, {
|
|
|
+ userID: userID,
|
|
|
+ userList: this.userList
|
|
|
+ })
|
|
|
// console.log(TAG_NAME, 'addUser', item, userMap.get(userID), this.userMap)
|
|
|
})
|
|
|
}
|
|
@@ -136,7 +143,11 @@ class UserController {
|
|
|
user.streams['aux'] && user.streams['aux'].reset()
|
|
|
// 用户退出,释放引用,外部调用该 user 所有stream 的 playerContext.stop() 方法停止播放
|
|
|
// TODO 触发时机提前了,方便外部用户做出处理,时机仍需进一步验证
|
|
|
- this._emitter.emit(EVENT.REMOTE_USER_LEAVE, { userID: userID, userList: this.userList, streamList: this.streamList })
|
|
|
+ this._emitter.emit(EVENT.REMOTE_USER_LEAVE, {
|
|
|
+ userID: userID,
|
|
|
+ userList: this.userList,
|
|
|
+ streamList: this.streamList
|
|
|
+ })
|
|
|
user = undefined
|
|
|
this.userMap.delete(userID)
|
|
|
// console.log(TAG_NAME, 'removeUser', this.userMap)
|
|
@@ -152,60 +163,75 @@ class UserController {
|
|
|
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) {
|
|
|
- // 查找对应的 stream
|
|
|
- let stream = user.streams[streamType]
|
|
|
- console.log(TAG_NAME, 'updateUserVideo start', user, streamType, stream)
|
|
|
- // 常规逻辑
|
|
|
- // 新来的stream,新增到 user.steams 和 streamList,streamList 包含所有用户(有音频或视频)的 stream
|
|
|
- if (!stream) {
|
|
|
- // 不在 user streams 里,需要新建
|
|
|
- user.streams[streamType] = stream = new Stream({ userID, streamID, hasVideo, src, streamType })
|
|
|
- this._addStream(stream)
|
|
|
- } else {
|
|
|
- // 更新 stream 属性
|
|
|
- stream.setProperty({ hasVideo })
|
|
|
- if (!hasVideo && !stream.hasAudio) {
|
|
|
- this._removeStream(stream)
|
|
|
- }
|
|
|
- // or
|
|
|
- // if (hasVideo) {
|
|
|
- // stream.setProperty({ hasVideo })
|
|
|
- // } else if (!stream.hasAudio) {
|
|
|
- // // hasVideo == false && hasAudio == false
|
|
|
- // this._removeStream(stream)
|
|
|
- // }
|
|
|
- }
|
|
|
- // 特殊逻辑
|
|
|
- if (streamType === 'aux') {
|
|
|
- if (hasVideo) {
|
|
|
- // 辅流需要修改填充模式
|
|
|
- stream.objectFit = 'contain'
|
|
|
+ console.log(item, "5555555555555555555555555")
|
|
|
+ // if (item.userid == "0" || item.userid == "share-userId") {
|
|
|
+ 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) {
|
|
|
+ // 查找对应的 stream
|
|
|
+ let stream = user.streams[streamType]
|
|
|
+ console.log(TAG_NAME, 'updateUserVideo start', user, streamType, stream)
|
|
|
+ // 常规逻辑
|
|
|
+ // 新来的stream,新增到 user.steams 和 streamList,streamList 包含所有用户(有音频或视频)的 stream
|
|
|
+ if (!stream) {
|
|
|
+ // 不在 user streams 里,需要新建
|
|
|
+ user.streams[streamType] = stream = new Stream({
|
|
|
+ userID,
|
|
|
+ streamID,
|
|
|
+ hasVideo,
|
|
|
+ src,
|
|
|
+ streamType
|
|
|
+ })
|
|
|
this._addStream(stream)
|
|
|
} else {
|
|
|
- // 如果是辅流要移除该 stream,否则需要移除 player
|
|
|
- this._removeStream(stream)
|
|
|
+ // 更新 stream 属性
|
|
|
+ stream.setProperty({
|
|
|
+ hasVideo
|
|
|
+ })
|
|
|
+ if (!hasVideo && !stream.hasAudio) {
|
|
|
+ this._removeStream(stream)
|
|
|
+ }
|
|
|
+ // or
|
|
|
+ // if (hasVideo) {
|
|
|
+ // stream.setProperty({ hasVideo })
|
|
|
+ // } else if (!stream.hasAudio) {
|
|
|
+ // // hasVideo == false && hasAudio == false
|
|
|
+ // this._removeStream(stream)
|
|
|
+ // }
|
|
|
}
|
|
|
- }
|
|
|
- // 更新所属user 的 hasXxx 值
|
|
|
- this.userList.find((item)=>{
|
|
|
- if (item.userID === userID) {
|
|
|
- item[`has${streamType.replace(/^\S/, (s) => s.toUpperCase())}Video`] = hasVideo
|
|
|
- return true
|
|
|
+ // 特殊逻辑
|
|
|
+ if (streamType === 'aux') {
|
|
|
+ if (hasVideo) {
|
|
|
+ // 辅流需要修改填充模式
|
|
|
+ stream.objectFit = 'contain'
|
|
|
+ this._addStream(stream)
|
|
|
+ } else {
|
|
|
+ // 如果是辅流要移除该 stream,否则需要移除 player
|
|
|
+ this._removeStream(stream)
|
|
|
+ }
|
|
|
}
|
|
|
- })
|
|
|
- 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 })
|
|
|
- // console.log(TAG_NAME, 'updateUserVideo', user, stream, this.userMap)
|
|
|
- }
|
|
|
+ // 更新所属user 的 hasXxx 值
|
|
|
+ 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
|
|
|
+ })
|
|
|
+ // console.log(TAG_NAME, 'updateUserVideo', user, stream, this.userMap)
|
|
|
+ }
|
|
|
+ // }
|
|
|
})
|
|
|
}
|
|
|
}
|
|
@@ -218,56 +244,69 @@ class UserController {
|
|
|
const incomingUserList = data.userlist
|
|
|
if (Array.isArray(incomingUserList) && incomingUserList.length > 0) {
|
|
|
incomingUserList.forEach((item) => {
|
|
|
- const userID = item.userid
|
|
|
- // 音频只跟着 stream main ,这里只修改 main
|
|
|
- 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({ streamType: streamType })
|
|
|
- // this._addStream(stream)
|
|
|
- // }
|
|
|
-
|
|
|
- // 常规逻辑
|
|
|
- // 新来的stream,新增到 user.steams 和 streamList,streamList 包含所有用户的 stream
|
|
|
- if (!stream) {
|
|
|
- // 不在 user streams 里,需要新建
|
|
|
- user.streams[streamType] = stream = new Stream({ userID, streamID, hasAudio, src, streamType })
|
|
|
- this._addStream(stream)
|
|
|
- } else {
|
|
|
- // 更新 stream 属性
|
|
|
- stream.setProperty({ hasAudio })
|
|
|
- if (!hasAudio && !stream.hasVideo) {
|
|
|
- this._removeStream(stream)
|
|
|
- }
|
|
|
- // or
|
|
|
- // if (hasAudio) {
|
|
|
- // stream.setProperty({ hasAudio })
|
|
|
- // } else if (!stream.hasVideo) {
|
|
|
- // // hasVideo == false && hasAudio == false
|
|
|
- // this._removeStream(stream)
|
|
|
+ // if (item.userid == "0" || item.userid == "share-userId") {
|
|
|
+ const userID = item.userid
|
|
|
+ // 音频只跟着 stream main ,这里只修改 main
|
|
|
+ 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({ streamType: streamType })
|
|
|
+ // this._addStream(stream)
|
|
|
// }
|
|
|
- }
|
|
|
|
|
|
- // stream.userID = userID
|
|
|
- // stream.streamID = userID + '_' + streamType
|
|
|
- // stream.hasAudio = hasAudio
|
|
|
- // stream.src = src
|
|
|
- // 更新所属 user 的 hasXxx 值
|
|
|
- this.userList.find((item)=>{
|
|
|
- if (item.userID === userID) {
|
|
|
- item[`has${streamType.replace(/^\S/, (s) => s.toUpperCase())}Audio`] = hasAudio
|
|
|
- return true
|
|
|
+ // 常规逻辑
|
|
|
+ // 新来的stream,新增到 user.steams 和 streamList,streamList 包含所有用户的 stream
|
|
|
+ if (!stream) {
|
|
|
+ // 不在 user streams 里,需要新建
|
|
|
+ user.streams[streamType] = stream = new Stream({
|
|
|
+ userID,
|
|
|
+ streamID,
|
|
|
+ hasAudio,
|
|
|
+ src,
|
|
|
+ streamType
|
|
|
+ })
|
|
|
+ this._addStream(stream)
|
|
|
+ } else {
|
|
|
+ // 更新 stream 属性
|
|
|
+ stream.setProperty({
|
|
|
+ hasAudio
|
|
|
+ })
|
|
|
+ if (!hasAudio && !stream.hasVideo) {
|
|
|
+ this._removeStream(stream)
|
|
|
+ }
|
|
|
+ // or
|
|
|
+ // if (hasAudio) {
|
|
|
+ // stream.setProperty({ hasAudio })
|
|
|
+ // } else if (!stream.hasVideo) {
|
|
|
+ // // hasVideo == false && hasAudio == false
|
|
|
+ // this._removeStream(stream)
|
|
|
+ // }
|
|
|
}
|
|
|
- })
|
|
|
- const eventName = hasAudio ? EVENT.REMOTE_AUDIO_ADD : EVENT.REMOTE_AUDIO_REMOVE
|
|
|
- this._emitter.emit(eventName, { stream: stream, streamList: this.streamList, userList: this.userList })
|
|
|
- // console.log(TAG_NAME, 'updateUserAudio', user, stream, this.userMap)
|
|
|
- }
|
|
|
+ // stream.userID = userID
|
|
|
+ // stream.streamID = userID + '_' + streamType
|
|
|
+ // stream.hasAudio = hasAudio
|
|
|
+ // stream.src = src
|
|
|
+ // 更新所属 user 的 hasXxx 值
|
|
|
+ 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
|
|
|
+ })
|
|
|
+ // console.log(TAG_NAME, 'updateUserAudio', user, stream, this.userMap)
|
|
|
+ }
|
|
|
+ // }
|
|
|
})
|
|
|
}
|
|
|
}
|
|
@@ -279,7 +318,10 @@ class UserController {
|
|
|
getUser(userID) {
|
|
|
return this.userMap.get(userID)
|
|
|
}
|
|
|
- getStream({ userID, streamType }) {
|
|
|
+ getStream({
|
|
|
+ userID,
|
|
|
+ streamType
|
|
|
+ }) {
|
|
|
const user = this.userMap.get(userID)
|
|
|
if (user) {
|
|
|
return user.streams[streamType]
|
|
@@ -297,7 +339,7 @@ class UserController {
|
|
|
* @returns {Object}
|
|
|
*/
|
|
|
reset() {
|
|
|
- this.streamList.forEach((item)=>{
|
|
|
+ this.streamList.forEach((item) => {
|
|
|
item.reset()
|
|
|
})
|
|
|
this.streamList = []
|
|
@@ -319,20 +361,24 @@ class UserController {
|
|
|
* @param {String} userID 用户ID
|
|
|
*/
|
|
|
_removeUserAndStream(userID) {
|
|
|
- this.streamList = this.streamList.filter((item)=>{
|
|
|
+ this.streamList = this.streamList.filter((item) => {
|
|
|
return item.userID !== userID && item.userID !== ''
|
|
|
})
|
|
|
- this.userList = this.userList.filter((item)=>{
|
|
|
+ this.userList = this.userList.filter((item) => {
|
|
|
return item.userID !== userID
|
|
|
})
|
|
|
}
|
|
|
_addStream(stream) {
|
|
|
- if (!this.streamList.includes(stream)) {
|
|
|
- this.streamList.push(stream)
|
|
|
- }
|
|
|
+ console.log(stream, "ioioioooioioioo")
|
|
|
+ // if (stream.userID == "0" || stream.userID == "share-userId") {
|
|
|
+ if (!this.streamList.includes(stream)) {
|
|
|
+ this.streamList.push(stream)
|
|
|
+ console.log(this.streamList, "我是过滤后的推送列表")
|
|
|
+ }
|
|
|
+ // }
|
|
|
}
|
|
|
_removeStream(stream) {
|
|
|
- this.streamList = this.streamList.filter((item)=>{
|
|
|
+ this.streamList = this.streamList.filter((item) => {
|
|
|
if (item.userID === stream.userID && item.streamType === stream.streamType) {
|
|
|
return false
|
|
|
}
|
|
@@ -342,5 +388,4 @@ class UserController {
|
|
|
user.streams[stream.streamType] = undefined
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
-export default UserController
|
|
|
+export default UserController
|