added some conditions for triggering reconnect and fixed some crashes
parent
eb6ee47e4e
commit
d7591684d7
|
@ -181,7 +181,7 @@ func (c *RoomController) Start(ctx *gin.Context) {
|
||||||
println(err.Error())
|
println(err.Error())
|
||||||
time.Sleep(4 * time.Second)
|
time.Sleep(4 * time.Second)
|
||||||
}
|
}
|
||||||
if res.StatusCode > 204 {
|
if res != nil && res.StatusCode > 204 {
|
||||||
resbody, _ := io.ReadAll(res.Body)
|
resbody, _ := io.ReadAll(res.Body)
|
||||||
println("get /join "+res.Status, string(resbody))
|
println("get /join "+res.Status, string(resbody))
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,11 +20,14 @@ type Track struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type Peer struct {
|
type Peer struct {
|
||||||
ID uint64
|
ID uint64
|
||||||
Conn *webrtc.PeerConnection
|
Conn *webrtc.PeerConnection
|
||||||
CanPublish bool
|
CanPublish bool
|
||||||
IsCaller bool
|
IsCaller bool
|
||||||
HandshakeLock *sync.Mutex
|
HandshakeLock *sync.Mutex
|
||||||
|
gotFirstVideoTrack bool
|
||||||
|
gotFirstAudioTrack bool
|
||||||
|
triggeredReconnectOnce bool
|
||||||
}
|
}
|
||||||
|
|
||||||
type Room struct {
|
type Room struct {
|
||||||
|
@ -126,9 +129,25 @@ func (r *RoomRepository) CreatePeer(roomId string, id uint64, canPublish bool, i
|
||||||
r.onPeerICECandidate(roomId, id, ic)
|
r.onPeerICECandidate(roomId, id, ic)
|
||||||
})
|
})
|
||||||
peerConn.OnConnectionStateChange(func(state webrtc.PeerConnectionState) {
|
peerConn.OnConnectionStateChange(func(state webrtc.PeerConnectionState) {
|
||||||
r.onPeerConnectionStateChange(roomId, id, state)
|
r.Lock()
|
||||||
if state == webrtc.PeerConnectionStateClosed && isCaller {
|
if !r.doesRoomExists(roomId) {
|
||||||
go r.onCallerDisconnected(roomId)
|
r.Unlock()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
room := r.Rooms[roomId]
|
||||||
|
r.Unlock()
|
||||||
|
room.Lock()
|
||||||
|
defer room.Unlock()
|
||||||
|
peer, stillThere := room.Peers[id]
|
||||||
|
|
||||||
|
r.onPeerConnectionStateChange(room, peer, state)
|
||||||
|
{
|
||||||
|
if state == webrtc.PeerConnectionStateClosed && isCaller {
|
||||||
|
if stillThere && peer.triggeredReconnectOnce {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
go r.onCallerDisconnected(roomId)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
peerConn.OnTrack(func(remote *webrtc.TrackRemote, receiver *webrtc.RTPReceiver) {
|
peerConn.OnTrack(func(remote *webrtc.TrackRemote, receiver *webrtc.RTPReceiver) {
|
||||||
|
@ -203,28 +222,20 @@ func (r *RoomRepository) onPeerICECandidate(roomId string, id uint64, ic *webrtc
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *RoomRepository) onPeerConnectionStateChange(roomId string, id uint64, newState webrtc.PeerConnectionState) {
|
func (r *RoomRepository) onPeerConnectionStateChange(room *Room, peer *Peer, newState webrtc.PeerConnectionState) {
|
||||||
r.Lock()
|
if peer == nil {
|
||||||
|
|
||||||
if !r.doesRoomExists(roomId) {
|
|
||||||
r.Unlock()
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
room := r.Rooms[roomId]
|
println("[PC] con_stat", newState.String(), peer.ID)
|
||||||
r.Unlock()
|
|
||||||
room.Lock()
|
|
||||||
defer room.Unlock()
|
|
||||||
|
|
||||||
println("[PC] con_stat", newState.String(), id)
|
|
||||||
switch newState {
|
switch newState {
|
||||||
case webrtc.PeerConnectionStateDisconnected:
|
case webrtc.PeerConnectionStateDisconnected:
|
||||||
fallthrough
|
fallthrough
|
||||||
case webrtc.PeerConnectionStateFailed:
|
case webrtc.PeerConnectionStateFailed:
|
||||||
if err := room.Peers[id].Conn.Close(); err != nil {
|
if err := peer.Conn.Close(); err != nil {
|
||||||
println(err.Error())
|
println(err.Error())
|
||||||
}
|
}
|
||||||
case webrtc.PeerConnectionStateClosed:
|
case webrtc.PeerConnectionStateClosed:
|
||||||
delete(room.Peers, id)
|
delete(room.Peers, peer.ID)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -248,6 +259,19 @@ func (r *RoomRepository) onPeerTrack(roomId string, id uint64, remote *webrtc.Tr
|
||||||
TrackLocal: trackLocal,
|
TrackLocal: trackLocal,
|
||||||
}
|
}
|
||||||
room.trackLock.Unlock()
|
room.trackLock.Unlock()
|
||||||
|
firstVideo := false
|
||||||
|
firstAudio := false
|
||||||
|
room.Lock()
|
||||||
|
peer := room.Peers[id]
|
||||||
|
room.Unlock()
|
||||||
|
if remote.Kind() == webrtc.RTPCodecTypeVideo && !peer.gotFirstVideoTrack {
|
||||||
|
peer.gotFirstVideoTrack = true
|
||||||
|
firstVideo = true
|
||||||
|
}
|
||||||
|
if remote.Kind() == webrtc.RTPCodecTypeAudio && !peer.gotFirstAudioTrack {
|
||||||
|
peer.gotFirstAudioTrack = true
|
||||||
|
firstAudio = true
|
||||||
|
}
|
||||||
|
|
||||||
defer func(trackId string) {
|
defer func(trackId string) {
|
||||||
room.trackLock.Lock()
|
room.trackLock.Lock()
|
||||||
|
@ -268,9 +292,14 @@ func (r *RoomRepository) onPeerTrack(roomId string, id uint64, remote *webrtc.Tr
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (firstVideo || firstAudio) && peer.IsCaller && !peer.triggeredReconnectOnce {
|
||||||
|
go r.onCallerDisconnected(roomId)
|
||||||
|
peer.triggeredReconnectOnce = true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *RoomRepository) updatePCTracks(roomId string) {
|
func (r *RoomRepository) updatePCTracks(roomId string) {
|
||||||
|
println("[] updatePCTracks start")
|
||||||
r.Lock()
|
r.Lock()
|
||||||
if !r.doesRoomExists(roomId) {
|
if !r.doesRoomExists(roomId) {
|
||||||
r.Unlock()
|
r.Unlock()
|
||||||
|
@ -343,6 +372,7 @@ func (r *RoomRepository) updatePCTracks(roomId string) {
|
||||||
}(peer, roomId)
|
}(peer, roomId)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
println("[] updatePCTracks end")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *RoomRepository) AddPeerIceCandidate(roomId string, id uint64, ic webrtc.ICECandidateInit) error {
|
func (r *RoomRepository) AddPeerIceCandidate(roomId string, id uint64, ic webrtc.ICECandidateInit) error {
|
||||||
|
|
|
@ -12,6 +12,7 @@ type Router struct {
|
||||||
func (r *Router) RegisterRoutes(rCtrl *controllers.RoomController) error {
|
func (r *Router) RegisterRoutes(rCtrl *controllers.RoomController) error {
|
||||||
gin.SetMode(gin.ReleaseMode)
|
gin.SetMode(gin.ReleaseMode)
|
||||||
r.router = gin.Default()
|
r.router = gin.Default()
|
||||||
|
r.router.Use(gin.Recovery())
|
||||||
registerRoomRoutes(r.router.Group("/room"), rCtrl)
|
registerRoomRoutes(r.router.Group("/room"), rCtrl)
|
||||||
r.router.GET("/healthcheck", rCtrl.HealthCheck)
|
r.router.GET("/healthcheck", rCtrl.HealthCheck)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue