fix: webrtc batch bug

This commit is contained in:
langhuihui
2025-06-12 14:21:59 +08:00
parent 94be02cd79
commit e478a1972e
2 changed files with 25 additions and 26 deletions

View File

@@ -2,6 +2,7 @@ package plugin_webrtc
import (
"encoding/json"
"errors"
"net"
"net/http"
"strings"
@@ -92,6 +93,30 @@ func (wsh *WebSocketHandler) Go() (err error) {
}); err != nil {
return
}
wsh.OnICECandidate(func(ice *ICECandidate) {
if ice != nil {
wsh.Info(ice.ToJSON().Candidate)
}
})
// 监听ICE连接状态变化
wsh.OnICEConnectionStateChange(func(state ICEConnectionState) {
wsh.Debug("ICE connection state changed", "state", state.String())
if state == ICEConnectionStateFailed {
wsh.Error("ICE connection failed")
}
})
wsh.OnConnectionStateChange(func(state PeerConnectionState) {
wsh.Info("Connection State has changed:" + state.String())
switch state {
case PeerConnectionStateConnected:
case PeerConnectionStateDisconnected, PeerConnectionStateFailed, PeerConnectionStateClosed:
wsh.Stop(errors.New("connection state:" + state.String()))
}
})
// 创建并发送应答
if answer, err := wsh.GetAnswer(); err == nil {
wsh.sendAnswer(answer.SDP)

View File

@@ -452,32 +452,6 @@ type SingleConnection struct {
Connection
}
func (c *SingleConnection) Start() (err error) {
c.OnICECandidate(func(ice *ICECandidate) {
if ice != nil {
c.Info(ice.ToJSON().Candidate)
}
})
// 监听ICE连接状态变化
c.OnICEConnectionStateChange(func(state ICEConnectionState) {
c.Debug("ICE connection state changed", "state", state.String())
if state == ICEConnectionStateFailed {
c.Error("ICE connection failed")
}
})
c.OnConnectionStateChange(func(state PeerConnectionState) {
c.Info("Connection State has changed:" + state.String())
switch state {
case PeerConnectionStateConnected:
case PeerConnectionStateDisconnected, PeerConnectionStateFailed, PeerConnectionStateClosed:
c.Stop(errors.New("connection state:" + state.String()))
}
})
return
}
func (c *SingleConnection) Receive() {
c.OnTrack(func(track *TrackRemote, receiver *RTPReceiver) {
c.Info("OnTrack", "kind", track.Kind().String(), "payloadType", uint8(track.Codec().PayloadType))