diff --git a/.env b/.env index 1c25d1dc..aeebbfb6 100644 --- a/.env +++ b/.env @@ -42,3 +42,4 @@ # DEBUG_INCOMING_API_REQUEST=TRUE # DEBUG_PRINT_ANSWER=TRUE # DEBUG_PRINT_OFFER=TRUE +# DEBUG_SESSION_MANAGER=TRUE diff --git a/.env.development b/.env.development index f55e6d7a..8bea57fe 100644 --- a/.env.development +++ b/.env.development @@ -49,6 +49,7 @@ # DEBUG_PRINT_ANSWER=TRUE # DEBUG_PRINT_OFFER=TRUE # DEBUG_PRINT_SSE_MESSAGES=TRUE +# DEBUG_SESSION_MANAGER=TRUE # ################ # LOGGING diff --git a/.env.production b/.env.production index 5de46980..b277979e 100644 --- a/.env.production +++ b/.env.production @@ -42,6 +42,7 @@ # DEBUG_INCOMING_API_REQUEST=TRUE # DEBUG_PRINT_ANSWER=TRUE # DEBUG_PRINT_OFFER=TRUE +# DEBUG_SESSION_MANAGER=TRUE # ################ # CHAT diff --git a/README.md b/README.md index f6ba9a88..81290129 100644 --- a/README.md +++ b/README.md @@ -299,6 +299,7 @@ These values are parsed by the Go backend and applied to WHIP/WHEP `PeerConnecti | `DEBUG_PRINT_ANSWER` | Prints WebRTC answers sent to clients. | | `DEBUG_INCOMING_API_REQUEST` | Logs incoming API request paths. | | `DEBUG_PRINT_SSE_MESSAGES` | Logs Server-Sent Events messages. | +| `DEBUG_SESSION_MANAGER` | Logs SessionManager messages. | ### Logging diff --git a/internal/environment/variables.go b/internal/environment/variables.go index 7555bcd5..5439c7b3 100644 --- a/internal/environment/variables.go +++ b/internal/environment/variables.go @@ -53,6 +53,7 @@ const ( DebugPrintAnswer = "DEBUG_PRINT_ANSWER" DebugPrintOffer = "DEBUG_PRINT_OFFER" DebugPrintSSEMessages = "DEBUG_PRINT_SSE_MESSAGES" + DebugSessionManager = "DEBUG_SESSION_MANAGER" // LOGGING loggingEnabled = "LOGGING_ENABLED" diff --git a/internal/server/helpers/log.go b/internal/server/helpers/log.go index aa7c38f1..afd53bc6 100644 --- a/internal/server/helpers/log.go +++ b/internal/server/helpers/log.go @@ -3,9 +3,27 @@ package helpers import ( "log" "net/http" + "os" + "strings" + + "github.com/glimesh/broadcast-box/internal/environment" +) + +var debugSessionManager = strings.EqualFold( + os.Getenv(environment.DebugSessionManager), + "true", ) func LogHTTPError(responseWriter http.ResponseWriter, error string, code int) { log.Println("LogHTTPError", error) http.Error(responseWriter, error, code) } + +// Print Session Manager debug logs +func DebugSessionLog(args ...any) { + if !debugSessionManager { + return + } + + log.Println(append([]any{"[DEBUG]"}, args...)...) +} diff --git a/internal/webrtc/sessions/manager/manager.go b/internal/webrtc/sessions/manager/manager.go index 2e76ac66..5c620c99 100644 --- a/internal/webrtc/sessions/manager/manager.go +++ b/internal/webrtc/sessions/manager/manager.go @@ -6,20 +6,21 @@ import ( "time" "github.com/glimesh/broadcast-box/internal/server/authorization" + "github.com/glimesh/broadcast-box/internal/server/helpers" "github.com/glimesh/broadcast-box/internal/webrtc/sessions/session" "github.com/glimesh/broadcast-box/internal/webrtc/sessions/whep" ) // Prepare the WHIP Session Manager func (m *SessionManager) Setup() { - log.Println("WHIPSessionManager.Setup") + helpers.DebugSessionLog("WHIPSessionManager.Setup") m.sessions = make(map[string]*session.Session) } // Add new session func (m *SessionManager) addSession(profile authorization.PublicProfile) (s *session.Session, err error) { - log.Println("SessionManager.AddWHIPSession") + helpers.DebugSessionLog("SessionManager.AddWHIPSession") s = &session.Session{ @@ -32,7 +33,7 @@ func (m *SessionManager) addSession(profile authorization.PublicProfile) (s *ses ChatManager: m.ChatManager, } s.SetOnClose(func() { - log.Println("SessionManager.Session.Done") + helpers.DebugSessionLog("SessionManager.Session.Done") m.sessionsLock.Lock() delete(m.sessions, profile.StreamKey) m.sessionsLock.Unlock() @@ -50,10 +51,10 @@ func (m *SessionManager) GetOrAddSession(profile authorization.PublicProfile, is session, ok := m.GetSessionByID(profile.StreamKey) if !ok { - log.Println("SessionManager.GetOrAddStream: Adding", profile.StreamKey) + helpers.DebugSessionLog("SessionManager.GetOrAddStream: Adding", profile.StreamKey) session, err = m.addSession(profile) } else if isWHIP { - log.Println("SessionManager.GetOrAddStream: Updating", profile.StreamKey) + helpers.DebugSessionLog("SessionManager.GetOrAddStream: Updating", profile.StreamKey) session.UpdateStreamStatus(profile) } @@ -62,7 +63,7 @@ func (m *SessionManager) GetOrAddSession(profile authorization.PublicProfile, is // Get Session by id func (m *SessionManager) GetSessionByID(streamKey string) (session *session.Session, foundSession bool) { - log.Println("SessionManager.GetSessionByID", streamKey) + helpers.DebugSessionLog("SessionManager.GetSessionByID", streamKey) m.sessionsLock.RLock() defer m.sessionsLock.RUnlock() @@ -73,7 +74,7 @@ func (m *SessionManager) GetSessionByID(streamKey string) (session *session.Sess // Gets the current state of all sessions func (m *SessionManager) GetSessionStates(includePrivateStreams bool) (result []session.StreamSessionState) { - log.Println("SessionManager.GetSessionStates: IsAdmin", includePrivateStreams) + helpers.DebugSessionLog("SessionManager.GetSessionStates: IsAdmin", includePrivateStreams) m.sessionsLock.RLock() copiedSessions := make(map[string]*session.Session) maps.Copy(copiedSessions, m.sessions) @@ -149,7 +150,7 @@ func (m *SessionManager) GetSessionStates(includePrivateStreams bool) (result [] // Update the provided session information func (m *SessionManager) UpdateProfile(profile *authorization.PersonalProfile) { - log.Println("WHIPSessionManager.UpdateProfile") + helpers.DebugSessionLog("WHIPSessionManager.UpdateProfile") m.sessionsLock.RLock() whipSession, ok := m.sessions[profile.StreamKey] m.sessionsLock.RUnlock() diff --git a/internal/webrtc/sessions/session/session.go b/internal/webrtc/sessions/session/session.go index aceb25f6..10acefb1 100644 --- a/internal/webrtc/sessions/session/session.go +++ b/internal/webrtc/sessions/session/session.go @@ -6,6 +6,7 @@ import ( "github.com/glimesh/broadcast-box/internal/server/authorization" "github.com/glimesh/broadcast-box/internal/webrtc/codecs" + "github.com/glimesh/broadcast-box/internal/server/helpers" "github.com/glimesh/broadcast-box/internal/webrtc/sessions/whep" "github.com/glimesh/broadcast-box/internal/webrtc/sessions/whip" "github.com/google/uuid" @@ -27,7 +28,7 @@ func (session *Session) SetOnClose(onClose func()) { // Add WHEP viewer session func (s *Session) AddWHEP(whepSessionID string, peerConnection *webrtc.PeerConnection, audioTrack *codecs.TrackMultiCodec, videoTrack *codecs.TrackMultiCodec, videoRTCPSender *webrtc.RTPSender, pliSender func()) (err error) { - log.Println("WHIPSessionManager.WHIPSession.AddWHEPSession") + helpers.DebugSessionLog("WHIPSessionManager.WHIPSession.AddWHEPSession") whepSession := whep.CreateNewWHEP( whepSessionID, @@ -53,7 +54,7 @@ func (s *Session) AddWHEP(whepSessionID string, peerConnection *webrtc.PeerConne // Add host func (s *Session) AddHost(peerConnection *webrtc.PeerConnection) (err error) { - log.Println("Session.AddHost") + helpers.DebugSessionLog("Session.AddHost") for { host := s.Host.Load() @@ -169,16 +170,16 @@ func (s *Session) Close() { // Returns true is no WHIP tracks are present, and no WHEP sessions are waiting for incoming streams func (s *Session) isEmpty() bool { if s.hasWHEPSessions() { - log.Println("Session.IsEmpty.HasWHEPSessions (false):", s.StreamKey) + helpers.DebugSessionLog("Session.IsEmpty.HasWHEPSessions (false):", s.StreamKey) return false } if s.isStreaming() { - log.Println("Session.IsEmpty.IsActive (false):", s.StreamKey) + helpers.DebugSessionLog("Session.IsEmpty.IsActive (false):", s.StreamKey) return false } - log.Println("Session.IsEmpty (true):", s.StreamKey) + helpers.DebugSessionLog("Session.IsEmpty (true):", s.StreamKey) return true } @@ -193,12 +194,12 @@ func (s *Session) isStreaming() bool { host.TracksLock.RLock() if len(host.AudioTracks) != 0 { - log.Println("Session.IsActive.AudioTracks", len(host.AudioTracks)) + helpers.DebugSessionLog("Session.IsActive.AudioTracks", len(host.AudioTracks)) host.TracksLock.RUnlock() return true } if len(host.VideoTracks) != 0 { - log.Println("Session.IsActive.VideoTracks", len(host.VideoTracks)) + helpers.DebugSessionLog("Session.IsActive.VideoTracks", len(host.VideoTracks)) host.TracksLock.RUnlock() return true } @@ -209,7 +210,7 @@ func (s *Session) isStreaming() bool { func (s *Session) hasWHEPSessions() bool { s.WHEPSessionsLock.RLock() - log.Println("Session.HasWHEPSessions:", len(s.WHEPSessions)) + helpers.DebugSessionLog("Session.HasWHEPSessions:", len(s.WHEPSessions)) if len(s.WHEPSessions) == 0 { s.WHEPSessionsLock.RUnlock() diff --git a/internal/webrtc/sessions/whep/whep.go b/internal/webrtc/sessions/whep/whep.go index c54858cb..39cb38a6 100644 --- a/internal/webrtc/sessions/whep/whep.go +++ b/internal/webrtc/sessions/whep/whep.go @@ -6,6 +6,7 @@ import ( "github.com/glimesh/broadcast-box/internal/chat" "github.com/glimesh/broadcast-box/internal/webrtc/codecs" + "github.com/glimesh/broadcast-box/internal/server/helpers" "github.com/pion/webrtc/v4" ) @@ -19,7 +20,7 @@ func CreateNewWHEP( pliSender func(), chatManager *chat.Manager, ) (w *WHEPSession) { - log.Println("WHEPSession.CreateNewWHEP", whepSessionID) + helpers.DebugSessionLog("WHEPSession.CreateNewWHEP", whepSessionID) w = &WHEPSession{ SessionID: whepSessionID, @@ -45,16 +46,16 @@ func CreateNewWHEP( func (w *WHEPSession) Close() { // Close WHEP channels w.SessionClose.Do(func() { - log.Println("WHEPSession.Close") + helpers.DebugSessionLog("WHEPSession.Close") w.IsSessionClosed.Store(true) // Close PeerConnection - log.Println("WHEPSession.Close.PeerConnection.GracefulClose") + helpers.DebugSessionLog("WHEPSession.Close.PeerConnection.GracefulClose") err := w.PeerConnection.Close() if err != nil { log.Println("WHEPSession.Close.PeerConnection.Error", err) } - log.Println("WHEPSession.Close.PeerConnection.GracefulClose.Completed") + helpers.DebugSessionLog("WHEPSession.Close.PeerConnection.GracefulClose.Completed") // Empty tracks w.AudioLock.Lock() @@ -109,7 +110,7 @@ func (w *WHEPSession) GetWHEPSessionStatus() (state SessionState) { // Sets the requested audio layer for this WHEP session. func (w *WHEPSession) SetAudioLayer(encodingID string) { - log.Println("Setting Audio Layer") + helpers.DebugSessionLog("Setting Audio Layer") w.AudioLayerCurrent.Store(encodingID) w.IsWaitingForKeyframe.Store(true) w.SendPLI() @@ -117,7 +118,7 @@ func (w *WHEPSession) SetAudioLayer(encodingID string) { // Sets the requested video layer for this WHEP session. func (w *WHEPSession) SetVideoLayer(encodingID string) { - log.Println("Setting Video Layer") + helpers.DebugSessionLog("Setting Video Layer") w.VideoLock.Lock() w.VideoLayerCurrent.Store(encodingID)