Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,4 @@
# DEBUG_INCOMING_API_REQUEST=TRUE
# DEBUG_PRINT_ANSWER=TRUE
# DEBUG_PRINT_OFFER=TRUE
# DEBUG_SESSION_MANAGER=TRUE
1 change: 1 addition & 0 deletions .env.development
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
# DEBUG_PRINT_ANSWER=TRUE
# DEBUG_PRINT_OFFER=TRUE
# DEBUG_PRINT_SSE_MESSAGES=TRUE
# DEBUG_SESSION_MANAGER=TRUE

# ################
# LOGGING
Expand Down
1 change: 1 addition & 0 deletions .env.production
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
# DEBUG_INCOMING_API_REQUEST=TRUE
# DEBUG_PRINT_ANSWER=TRUE
# DEBUG_PRINT_OFFER=TRUE
# DEBUG_SESSION_MANAGER=TRUE

# ################
# CHAT
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions internal/environment/variables.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
18 changes: 18 additions & 0 deletions internal/server/helpers/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...)...)
}
17 changes: 9 additions & 8 deletions internal/webrtc/sessions/manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{

Expand All @@ -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()
Expand All @@ -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)
}

Expand All @@ -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()
Expand All @@ -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)
Expand Down Expand Up @@ -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()
Expand Down
17 changes: 9 additions & 8 deletions internal/webrtc/sessions/session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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,
Expand All @@ -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()
Expand Down Expand Up @@ -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
}

Expand All @@ -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
}
Expand All @@ -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()
Expand Down
13 changes: 7 additions & 6 deletions internal/webrtc/sessions/whep/whep.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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,
Expand All @@ -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()
Expand Down Expand Up @@ -109,15 +110,15 @@ 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()
}

// 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)
Expand Down