user-controller.js
13.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
import { TRTC_EVENT } from "../common/constants.js";
import Stream from "../model/stream.js";
import User from "../model/user.js";
import Event from "../utils/event.js";
const TAG_NAME = "UserController";
/**
* 通讯成员管理
*/
class UserController {
constructor(componentContext) {
// userMap 用于存储完整的数据结构
this.userMap = new Map();
// userList 用于存储简化的用户数据 Object,包括 {userID hasMainAudio hasMainVideo hasAuxAudio hasAuxVideo}
this.userList = [];
// streamList 存储steam 对象列表,用于 trtc-room 渲染 player
this.streamList = [];
this._emitter = new Event();
this.componentContext = componentContext;
}
_addStream(stream) {
if (!this.streamList.includes(stream)) {
this.streamList.push(stream);
}
}
_removeStream(stream) {
console.warn("==========", 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;
}
/**
* 删除用户和所有的 stream
* @param {string} userID 用户ID
*/
_removeUserAndStream(userID) {
this.streamList = this.streamList.filter((item)=>{
return item.userID !== userID && item.userID !== "";
});
this.userList = this.userList.filter((item)=>{
return item.userID !== userID;
});
}
/**
* 处理用户进房事件
* @param {object} data pusher 下发的数据
*/
addUser(data) {
// console.log(TAG_NAME, 'addUser', data)
const incomingUserList = data.userlist;
const userMap = this.userMap;
if (Array.isArray(incomingUserList) && incomingUserList.length > 0) {
incomingUserList.forEach((item) => {
const userID = item.userid;
// 已经在 map 中的用户
let user = this.getUser(userID);
if (!user) {
// 新增用户
user = new User({ userID });
this.userList.push({
userID,
});
}
userMap.set(userID, user);
this._emitter.emit(TRTC_EVENT.REMOTE_USER_JOIN, { userID, userList: this.userList });
// console.log(TAG_NAME, 'addUser', item, userMap.get(userID), this.userMap)
});
}
}
getStream({ userID, streamType }) {
const user = this.userMap.get(userID);
if (user) {
return user.streams[streamType];
}
return undefined;
}
getStreamList() {
return this.streamList;
}
/**
* Get user object by userID
* @param {string} userID 用户ID
* @returns {object} User object
*/
getUser(userID) {
return this.userMap.get(userID);
}
getUserList() {
return this.userList;
}
off(eventCode, handler) {
this._emitter.off(eventCode, handler);
}
on(eventCode, handler, context) {
this._emitter.on(eventCode, handler, context);
}
/**
* 处理用户退房事件
* @param {object} data pusher 下发的数据 {userlist}
*/
removeUser(data) {
// console.log(TAG_NAME, '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);
// 偶现SDK触发退房事件前没有触发进房事件
if (!user || !user.streams) {
return;
}
// 从userList 里删除指定的用户和 stream
this._removeUserAndStream(userID);
// 重置
user.streams.main && user.streams.main.reset();
user.streams.aux && user.streams.aux.reset();
// 用户退出,释放引用,外部调用该 user 所有stream 的 playerContext.stop() 方法停止播放
// TODO 触发时机提前了,方便外部用户做出处理,时机仍需进一步验证
this._emitter.emit(TRTC_EVENT.REMOTE_USER_LEAVE, { userID, userList: this.userList, streamList: this.streamList });
user = undefined;
this.userMap.delete(userID);
// console.log(TAG_NAME, 'removeUser', this.userMap)
});
}
}
/**
* 重置所有user 和 steam
* @returns {object} Object containing reset userList and streamList
*/
reset() {
this.streamList.forEach((item)=>{
item.reset();
});
this.streamList = [];
this.userList = [];
this.userMap.clear();
return {
userList: this.userList,
streamList: this.streamList,
};
}
/**
* 处理用户音频通知事件
* @param {object} data pusher 下发的数据 {userlist}
*/
updateUserAudio(data) {
// console.log(TAG_NAME, 'updateUserAudio', data)
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) {
// 更新 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)
// }
} else {
// 不在 user streams 里,需要新建
user.streams[streamType] = stream = new Stream({ userID, streamID, hasAudio, src, streamType });
this._addStream(stream);
}
// stream.userID = userID
// stream.streamID = userID + '_' + streamType
// stream.hasAudio = hasAudio
// stream.src = src
// 更新所属 user 的 hasXxx 值
this.userList.forEach((item) => {
if (item.userID === userID) {
item[`has${streamType.replace(/^\S/, (s) => s.toUpperCase())}Audio`] = hasAudio;
}
});
const eventName = hasAudio ? TRTC_EVENT.REMOTE_AUDIO_ADD : TRTC_EVENT.REMOTE_AUDIO_REMOVE;
this._emitter.emit(eventName, { stream, streamList: this.streamList, userList: this.userList });
// console.log(TAG_NAME, 'updateUserAudio', user, stream, this.userMap)
}
});
}
}
/**
* 处理用户视频通知事件
* @param {object} data pusher 下发的数据 {userlist}
*/
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) {
// 查找对应的 stream
let stream = user.streams[streamType];
console.log(TAG_NAME, "updateUserVideo start", user, streamType, stream);
// 常规逻辑
// 新来的stream,新增到 user.steams 和 streamList,streamList 包含所有用户(有音频或视频)的 stream
if (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)
// }
} else {
// 不在 user streams 里,需要新建
user.streams[streamType] = stream = new Stream({ userID, streamID, hasVideo, src, streamType });
this._addStream(stream);
}
// 更新所属user 的 hasXxx 值
this.userList.forEach((item)=>{
if (item.userID === userID) {
item[`has${streamType.replace(/^\S/, (s) => s.toUpperCase())}Video`] = hasVideo;
}
});
console.log(TAG_NAME, "updateUserVideo end", user, streamType, stream);
const eventName = hasVideo ? TRTC_EVENT.REMOTE_VIDEO_ADD : TRTC_EVENT.REMOTE_VIDEO_REMOVE;
this._emitter.emit(eventName, { stream, streamList: this.streamList, userList: this.userList });
// console.log(TAG_NAME, 'updateUserVideo', user, stream, this.userMap)
}
});
}
}
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 (error) {
console.warn(TAG_NAME, "userEventHandler 数据格式错误", error);
return false;
}
} else {
console.warn(TAG_NAME, "userEventHandler 数据格式错误");
return false;
}
switch (code) {
case 1031: {
// console.log(TAG_NAME, '远端用户进房通知:', code)
// 1031 有新用户
// {
// "userlist":[
// {
// "userid":"webrtc11"
// }
// ]
// }
this.addUser(data);
break;
}
case 1032: {
// console.log(TAG_NAME, '远端用户退房通知:', code)
// 1032 有用户退出
this.removeUser(data);
break;
}
case 1033: {
// console.log(TAG_NAME, '远端用户视频状态位变化通知:', code)
// 1033 用户视频状态变化,新增stream或者更新stream 状态
// {
// "userlist":[
// {
// "userid":"webrtc11",
// "playurl":" room://rtc.tencent.com?userid=xxx&streamtype=main",
// "streamtype":"main",
// "hasvideo":true
// }
// ]
// }
this.updateUserVideo(data);
break;
}
case 1034: {
// console.log(TAG_NAME, '远端用户音频状态位变化通知:', code)
// 1034 用户音频状态变化
// {
// "userlist":[
// {
// "userid":"webrtc11",
// "playurl":" room://rtc.tencent.com?userid=xxx&streamtype=main",
// "hasaudio":false
// }
// ]
// }
this.updateUserAudio(data);
break;
}
}
}
}
export default UserController;