11package session
22
33import (
4- "context"
54 "fmt"
65 "log"
76
@@ -23,6 +22,10 @@ func (s *Session) UpdateStreamStatus(profile authorization.PublicProfile) {
2322 s .StatusLock .Unlock ()
2423}
2524
25+ func (session * Session ) SetOnClose (onClose func ()) {
26+ session .onClose = onClose
27+ }
28+
2629// Add WHEP session to existing WHIP session
2730func (s * Session ) AddWHEP (whepSessionID string , peerConnection * webrtc.PeerConnection , audioTrack * codecs.TrackMultiCodec , videoTrack * codecs.TrackMultiCodec , videoRTCPSender * webrtc.RTPSender ) (err error ) {
2831 log .Println ("WHIPSessionManager.WHIPSession.AddWHEPSession" )
@@ -47,8 +50,7 @@ func (s *Session) AddWHEP(whepSessionID string, peerConnection *webrtc.PeerConne
4750 s .WHEPSessions [whepSessionID ] = whepSession
4851 s .WHEPSessionsLock .Unlock ()
4952 s .updateHostWHEPSessionsSnapshot ()
50-
51- go s .handleWHEPConnection (whepSession )
53+ whepSession .SetOnClose (s .handleWHEPClose )
5254 go s .handleWHEPVideoRTCPSender (whepSession , videoRTCPSender )
5355
5456 return nil
@@ -64,7 +66,7 @@ func (s *Session) AddHost(peerConnection *webrtc.PeerConnection) (err error) {
6466 break
6567 }
6668
67- if host .PeerConnection .ConnectionState () != webrtc .PeerConnectionStateClosed || s . ActiveContext . Err () == nil {
69+ if host .PeerConnection .ConnectionState () != webrtc .PeerConnectionStateClosed {
6870 return fmt .Errorf ("session already has a host" )
6971 }
7072
@@ -73,20 +75,14 @@ func (s *Session) AddHost(peerConnection *webrtc.PeerConnection) (err error) {
7375 }
7476 }
7577
76- activeContext , activeContextCancel := context .WithCancel (context .Background ())
77-
7878 host := & whip.WHIPSession {
7979 ID : uuid .New ().String (),
8080 AudioTracks : make (map [string ]* whip.AudioTrack ),
8181 VideoTracks : make (map [string ]* whip.VideoTrack ),
82-
83- ActiveContext : activeContext ,
84- ActiveContextCancel : activeContextCancel ,
8582 }
8683
8784 host .AddPeerConnection (peerConnection , s .StreamKey )
8885 if ! s .Host .CompareAndSwap (nil , host ) {
89- host .ActiveContextCancel ()
9086 host .RemovePeerConnection ()
9187 host .RemoveTracks ()
9288 return fmt .Errorf ("session already has a host" )
@@ -110,25 +106,24 @@ func (s *Session) RemoveHost() {
110106 log .Println ("Session.RemoveHost" , s .StreamKey )
111107
112108 host .WHEPSessionsSnapshot .Store (make (map [string ]* whep.WHEPSession ))
113- host .ActiveContextCancel ()
114109 host .RemovePeerConnection ()
115110 host .RemoveTracks ()
116111}
117112
118- // Remove WHEP session from WHIP session
119- // In case the WHIP session does not have a host, and no more whep sessions, it will
120- // be remove from the manager.
121- func (s * Session ) removeWHEP (whepSessionID string ) {
122- log .Println ("Session.RemoveWHEPSession:" , s .StreamKey , " - " , whepSessionID )
113+ func (s * Session ) handleWHEPClose (whepSessionID string ) {
114+ log .Println ("Session.HandleWHEPClose:" , s .StreamKey , " - " , whepSessionID )
123115
124116 s .WHEPSessionsLock .Lock ()
125- if whepSession , ok := s .WHEPSessions [whepSessionID ]; ok {
126- whepSession . Close ()
117+ _ , ok := s .WHEPSessions [whepSessionID ]
118+ if ok {
127119 delete (s .WHEPSessions , whepSessionID )
128- } else {
129- log .Println ("Session.RemoveWHEPSession.InvalidSession:" , s .StreamKey , " - " , whepSessionID )
130120 }
131121 s .WHEPSessionsLock .Unlock ()
122+
123+ if ! ok {
124+ return
125+ }
126+
132127 s .updateHostWHEPSessionsSnapshot ()
133128
134129 if s .isEmpty () {
@@ -138,22 +133,26 @@ func (s *Session) removeWHEP(whepSessionID string) {
138133
139134// Remove all Hosts and clients before closing down session
140135func (s * Session ) close () {
141- s .WHEPSessionsLock .Lock ()
142- whepSessions := make ([]* whep.WHEPSession , 0 , len (s .WHEPSessions ))
143- for _ , whepSession := range s .WHEPSessions {
144- whepSessions = append (whepSessions , whepSession )
145- }
146- s .WHEPSessions = make (map [string ]* whep.WHEPSession )
147- s .WHEPSessionsLock .Unlock ()
136+ s .closeOnce .Do (func () {
137+ s .WHEPSessionsLock .Lock ()
138+ whepSessions := make ([]* whep.WHEPSession , 0 , len (s .WHEPSessions ))
139+ for _ , whepSession := range s .WHEPSessions {
140+ whepSessions = append (whepSessions , whepSession )
141+ }
142+ s .WHEPSessions = make (map [string ]* whep.WHEPSession )
143+ s .WHEPSessionsLock .Unlock ()
148144
149- for _ , whepSession := range whepSessions {
150- whepSession .Close ()
151- }
152- s .updateHostWHEPSessionsSnapshot ()
145+ for _ , whepSession := range whepSessions {
146+ whepSession .Close ()
147+ }
148+ s .updateHostWHEPSessionsSnapshot ()
153149
154- s .RemoveHost ()
150+ s .RemoveHost ()
155151
156- s .ActiveContextCancel ()
152+ if s .onClose != nil {
153+ s .onClose ()
154+ }
155+ })
157156}
158157
159158func (s * Session ) Close () {
0 commit comments