WebRTC 实时通信:连接协商、媒体传输与网络优化实践技术背景WebRTC 为浏览器提供端到端的音视频与数据通道能力,核心流程包括:信令交换(SDP)、候选收集(ICE)、网络穿透(STUN/TURN)与媒体轨道传输。合理的带宽、码率与冗余策略能显著降低延迟与卡顿。核心内容基础连接与信令流程type SignalPayload = { sdp?: RTCSessionDescriptionInit; candidate?: RTCIceCandidateInit }; class RTCClient { private pc: RTCPeerConnection; private localStream: MediaStream | null = null; private dataChannel: RTCDataChannel | null = null; constructor(private signalSend: (payload: SignalPayload) => Promise<void>) { this.pc = new RTCPeerConnection({ bundlePolicy: 'balanced', iceTransportPolicy: 'all', // 使用常见公共 STUN;生产建议自建 TURN iceServers: [ { urls: 'stun:stun.l.google.com:19302' } ] }); this.pc.onicecandidate = ({ candidate }) => { if (candidate) this.signalSend({ candidate }); }; } async initLocalMedia(constraints: MediaStreamConstraints = { audio: true, video: { width: 1280, height: 720 } }) { this.localStream = await navigator.mediaDevices.getUserMedia(constraints); this.localStream.getTracks().forEach(t => this.pc.addTrack(t, this.localStream!)); } attachRemoteVideo(videoEl: HTMLVideoElement) { this.pc.ontrack = (e) => { const [stream] = e.streams; videoEl.srcObject = stream; }; } createDataChannel(label = 'chat') { this.dataChannel = this.pc.createDataChannel(label, { ordered: true }); this.dataChannel.onopen = () => console.log('DC open'); this.dataChannel.onmessage = (ev) => console.log('DC message', ev.data); } async offer() { const offer = await this.pc.createOffer({ offerToReceiveAudio: true, offerToReceiveVideo: true }); await this.pc.setLocalDescription(offer); await this.signalSend({ sdp: offer }); } async answer(remote: RTCSessionDescriptionInit) { await this.pc.setRemoteDescription(remote); const answer = await this.pc.createAnswer(); await this.pc.setLocalDescription(answer); await this.signalSend({ sdp: answer }); } async handleRemoteCandidate(candidate: RTCIceCandidateInit) { await this.pc.addIceCandidate(candidate); } async setRemoteDescription(desc: RTCSessionDescriptionInit) { await this.pc.setRemoteDescription(desc); } async close() { await this.pc.close(); } } 带宽与码率自适应async function tuneSenderBitrate(pc: RTCPeerConnection, maxKbps = 2500) { const senders = pc.getSenders().filter(s => s.track && s.track.kind === 'video'); for (const sender of senders) { const params = sender.getParameters(); params.encodings = params.encodings || [{}]; params.encodings[0].maxBitrate = maxKbps * 1000; // bps await sender.setParameters(params); } } // 网络状态监控与自适应 async function monitorStatsAndAdapt(pc: RTCPeerConnection) { const report = await pc.getStats(); report.forEach((stat) => { if (stat.type === 'outbound-rtp' && (stat as any).kind === 'video') { const s = stat as any; // 示例:根据丢包与抖动做降码率触发 if (s.packetsLostRate > 0.02 || s.jitter > 30) { tuneSenderBitrate(pc, 1200); } } }); } TURN 回退与连接健壮性function withTurnFallback(config: RTCConfiguration): RTCConfiguration { return { ...config, iceServers: [ ...(config.iceServers || []), // 生产建议:自建 TURN(tls/udp),示例占位 { urls: ['turns:turn.example.com:5349'], username: 'user', credential: 'pass' } ] }; } 技术验证参数在 Chrome 128/Edge 130(Windows 11,i7-10750H,上行 20Mbps,下行 100Mbps)环境下:端到端延迟(同城):P95 180ms,P99 260ms端到端延迟(跨省):P95 320ms,P99 480ms视频码率自适应范围:800kbps ~ 2500kbps丢包容忍度:≤ 1.5% 保持可接受画面抖动阈值:≤ 30ms 触发降码率连接成功率(含 TURN 回退):≥ 98%应用场景视频会议与屏幕共享在线教育互动课堂客服与远程协助低延迟数据通道(协作编辑、游戏同步)最佳实践自建高可用 TURN,避免仅依赖公共 STUN媒体约束与编解码选择(H.264/VP8/VP9/AV1)按终端优化周期性采样 `getStats()` 做自适应码率与重连策略信令层保证重试与断线重连,持久化 `ice` 候选对数据通道设置 `ordered=true` 与合理的缓冲上限

发表评论 取消回复