【m】测试,便于回退
This commit is contained in:
@@ -19,7 +19,8 @@ class CallStateManager {
|
||||
status: 'idle' // 初始状态为空闲
|
||||
},
|
||||
localStream: null, // MediaStream 对象
|
||||
remoteStream: null // MediaStream 对象
|
||||
remoteStream: null, // 单路远端流(兼容旧逻辑,participant端使用)
|
||||
remoteStreams: {} // 多路远端流 Map: { connectionId: MediaStream }(host端使用)
|
||||
};
|
||||
|
||||
// 监听器数组
|
||||
@@ -380,12 +381,16 @@ this.renderstreaming.onNewPeer = (connectionId) => {
|
||||
this.updateRemoteUserStatus('offline');
|
||||
this.updateRemoteUserNetworkQuality('no_signal');
|
||||
showNotification('对方已离开通话', 'warning');
|
||||
// 清理远端流,重置Peer连接为新participant加入做准备
|
||||
// 清理该 participant 的远端流
|
||||
if (this.state.remoteStreams[connectionId]) {
|
||||
this.state.remoteStreams[connectionId].getTracks().forEach(track => track.stop());
|
||||
delete this.state.remoteStreams[connectionId];
|
||||
}
|
||||
// 同时清理单路远端流(兼容)
|
||||
if (this.state.remoteStream) {
|
||||
this.state.remoteStream.getTracks().forEach(track => track.stop());
|
||||
this.state.remoteStream = null;
|
||||
}
|
||||
this.notify({ type: 'REMOTE_STREAM_OBTAINED', stream: null });
|
||||
// 通知UI更新
|
||||
this.notify({ type: 'PARTICIPANT_LEFT', connectionId: connectionId });
|
||||
};
|
||||
@@ -394,25 +399,48 @@ this.renderstreaming.onNewPeer = (connectionId) => {
|
||||
this.renderstreaming.onTrackEvent = (data) => {
|
||||
const direction = data.transceiver.direction;
|
||||
if (direction == "sendrecv" || direction == "recvonly") {
|
||||
if (this.state.remoteStream == null) {
|
||||
this.state.remoteStream = new MediaStream();
|
||||
// 获取当前连接的远端流
|
||||
const trackConnectionId = this.connectionId;
|
||||
// Host端: 每个participant有独立的远端流
|
||||
// Participant端: 只有一个host的远端流
|
||||
const isHost = this.role === 'host';
|
||||
|
||||
// 获取或创建对应的远端流
|
||||
let targetStream = null;
|
||||
if (isHost) {
|
||||
// Host端: 按 connectionId 管理多路远端流
|
||||
if (!this.state.remoteStreams[trackConnectionId]) {
|
||||
this.state.remoteStreams[trackConnectionId] = new MediaStream();
|
||||
}
|
||||
targetStream = this.state.remoteStreams[trackConnectionId];
|
||||
} else {
|
||||
// Participant端: 使用单一远端流
|
||||
if (this.state.remoteStream == null) {
|
||||
this.state.remoteStream = new MediaStream();
|
||||
}
|
||||
targetStream = this.state.remoteStream;
|
||||
}
|
||||
|
||||
// 检查是否已经有相同类型的轨道
|
||||
const existingTracks = this.state.remoteStream.getTracks().filter(track => track.kind === data.track.kind);
|
||||
const existingTracks = targetStream.getTracks().filter(track => track.kind === data.track.kind);
|
||||
|
||||
// 移除旧的轨道
|
||||
existingTracks.forEach(track => {
|
||||
this.state.remoteStream.removeTrack(track);
|
||||
targetStream.removeTrack(track);
|
||||
console.log('Removed old track:', track.kind);
|
||||
});
|
||||
|
||||
// 添加新的轨道
|
||||
this.state.remoteStream.addTrack(data.track);
|
||||
console.log('Added new track:', data.track.kind);
|
||||
targetStream.addTrack(data.track);
|
||||
console.log('Added new track:', data.track.kind, 'to stream:', trackConnectionId);
|
||||
|
||||
// 通知UI远程流已更新
|
||||
this.notify({ type: 'REMOTE_STREAM_OBTAINED', stream: this.state.remoteStream });
|
||||
this.notify({
|
||||
type: 'REMOTE_STREAM_OBTAINED',
|
||||
stream: targetStream,
|
||||
connectionId: trackConnectionId,
|
||||
isHost: isHost
|
||||
});
|
||||
console.log('Notified UI about remote stream update');
|
||||
// 只有当收到远程流时才更新远程用户状态为在线
|
||||
if (this.state.session.remoteUser.status !== 'online') {
|
||||
|
||||
Reference in New Issue
Block a user