【m】状态同步

This commit is contained in:
zhangzheng
2026-03-12 17:53:34 +08:00
parent 7b92f69d6a
commit 4ce99ae140
5 changed files with 93 additions and 23 deletions

View File

@@ -47,11 +47,6 @@ class CallStateManager {
// 初始化
async init() {
// 启动通话时长计时器
this.durationInterval = setInterval(() => {
this.state.session.duration++;
this.notify({ type: 'DURATION_UPDATE', duration: this.state.session.duration });
}, 1000);
// 初始化配置
await this.setupConfig();
// 获取本地摄像头视频流
@@ -356,7 +351,17 @@ class CallStateManager {
// 通知UI远程流已更新
this.notify({ type: 'REMOTE_STREAM_OBTAINED', stream: this.state.remoteStream });
console.log('Notified UI about remote stream update');
// 只有当收到远程流时才更新远程用户状态为在线
if (this.state.session.remoteUser.status !== 'online') {
this.updateRemoteUserStatus('online');
// 更新远程用户网络质量为好
this.updateRemoteUserNetworkQuality('good');
// 启动通话时长计时器
this.durationInterval = setInterval(() => {
this.state.session.duration++;
this.notify({ type: 'DURATION_UPDATE', duration: this.state.session.duration });
}, 1000);
}
// 如果是音频轨道,启动远程音频活动检测
if (data.track.kind === 'audio') {
this.startRemoteActivityDetection();
@@ -389,6 +394,7 @@ class CallStateManager {
await this.renderstreaming.start();
await this.renderstreaming.createConnection(connectionId);
// 启动网络质量检测
this.startNetworkQualityDetection();
@@ -408,6 +414,11 @@ class CallStateManager {
async hangUp() {
this.clearStatsMessage(); // 清除统计信息
this.stopNetworkQualityDetection(); // 停止网络质量检测
// 停止通话时长计时器
if (this.durationInterval) {
clearInterval(this.durationInterval);
this.durationInterval = null;
}
console.log(`Disconnect peer on ${this.connectionId}.`);
// 删除连接并停止WebRTC
@@ -421,6 +432,9 @@ class CallStateManager {
this.renderstreaming = null;
}
// 更新远程用户状态为离线
this.updateRemoteUserStatus('offline');
this.updateRemoteUserNetworkQuality('no_signal');
this.connectionId = null;
this.state.session.status = 'ended';
this.notify({ type: 'CALL_ENDED' });
@@ -466,9 +480,22 @@ class CallStateManager {
// 通知UI更新用户列表
this.notify({ type: 'USER_LIST_UPDATE', localUser: this.state.session.localUser, remoteUser: this.state.session.remoteUser });
}
// 更新远端用户状态
updateRemoteUserStatus(status) {
this.state.session.remoteUser.status = status;
this.notify({ type: 'REMOTE_MEDIA_CHANGE', localUser: this.state.session.localUser, remoteUser: this.state.session.remoteUser });
}
updateRemoteUserNetworkQuality(networkQuality) {
this.state.session.remoteUser.networkQuality = networkQuality;
this.notify({ type: 'REMOTE_MEDIA_CHANGE', localUser: this.state.session.localUser, remoteUser: this.state.session.remoteUser });
}
// 结束通话
endCall() {
clearInterval(this.durationInterval);
if (this.durationInterval) {
clearInterval(this.durationInterval);
this.durationInterval = null;
}
this.state.session.status = 'ended';
this.notify({ type: 'CALL_ENDED' });
@@ -512,7 +539,7 @@ class CallStateManager {
simulateNetworkChange() {
// 模拟网络质量变化
const qualities = ['good', 'fair', 'excellent', 'poor'];
const qualities = ['good', 'fair', 'excellent', 'poor', 'no_signal'];
setInterval(() => {
if (Math.random() > 0.8) {
const quality = qualities[Math.floor(Math.random() * qualities.length)];
@@ -585,6 +612,9 @@ class CallStateManager {
} else {
quality = 'excellent';
}
} else {
// 没有收到任何RTP包设置为无信号状态
quality = 'no_signal';
}
// 更新网络质量状态