Skip to content

Commit

Permalink
Add DEBUG_PRINT_OFFER and DEBUG_PRINT_ANSWER
Browse files Browse the repository at this point in the history
Print Offer/Answer to terminal for easier debugging
  • Loading branch information
Sean-Der committed Sep 22, 2024
1 parent 98abef0 commit 5d538c2
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 3 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,9 @@ The backend can be configured with the following environment variables.

- `APPEND_CANDIDATE` - Append candidates to Offer that ICE Agent did not generate. Worse version of `NAT_1_TO_1_IP`

- `DEBUG_PRINT_OFFER` - Print WebRTC Offers from client to Broadcast Box. Debug things like accepted codecs.
- `DEBUG_PRINT_ANSWER` - Print WebRTC Answers from Broadcast Box to Browser. Debug things like IP/Ports returned to client.

## Network Test on Start

When running in Docker Broadcast Box runs a network tests on startup. This tests that WebRTC traffic can be established
Expand Down
14 changes: 13 additions & 1 deletion internal/webrtc/webrtc.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ func newPeerConnection(api *webrtc.API) (*webrtc.PeerConnection, error) {
return api.NewPeerConnection(cfg)
}

func appendOffer(in string) string {
func appendAnswer(in string) string {
if extraCandidate := os.Getenv("APPEND_CANDIDATE"); extraCandidate != "" {
index := strings.Index(in, "a=end-of-candidates")
in = in[:index] + extraCandidate + in[index:]
Expand All @@ -359,6 +359,18 @@ func appendOffer(in string) string {
return in
}

func maybePrintOfferAnswer(sdp string, isOffer bool) string {
if os.Getenv("DEBUG_PRINT_OFFER") != "" && isOffer {
fmt.Println(sdp)
}

if os.Getenv("DEBUG_PRINT_ANSWER") != "" && !isOffer {
fmt.Println(sdp)
}

return sdp
}

func Configure() {
streamMap = map[string]*stream{}

Expand Down
4 changes: 3 additions & 1 deletion internal/webrtc/whep.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ func WHEPChangeLayer(whepSessionId, layer string) error {
}

func WHEP(offer, streamKey string) (string, string, error) {
maybePrintOfferAnswer(offer, true)

streamMapLock.Lock()
defer streamMapLock.Unlock()
stream, err := getStream(streamKey, false)
Expand Down Expand Up @@ -155,7 +157,7 @@ func WHEP(offer, streamKey string) (string, string, error) {
stream.whepSessions[whepSessionId].currentLayer.Store("")
stream.whepSessions[whepSessionId].waitingForKeyframe.Store(false)

return appendOffer(peerConnection.LocalDescription().SDP), whepSessionId, nil
return maybePrintOfferAnswer(appendAnswer(peerConnection.LocalDescription().SDP), false), whepSessionId, nil
}

func (w *whepSession) sendVideoPacket(rtpPkt *rtp.Packet, layer string, timeDiff int64, sequenceDiff int, codec videoTrackCodec, isKeyframe bool) {
Expand Down
4 changes: 3 additions & 1 deletion internal/webrtc/whip.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ func videoWriter(remoteTrack *webrtc.TrackRemote, stream *stream, peerConnection
}

func WHIP(offer, streamKey string) (string, error) {
maybePrintOfferAnswer(offer, true)

peerConnection, err := newPeerConnection(apiWhip)
if err != nil {
return "", err
Expand Down Expand Up @@ -187,5 +189,5 @@ func WHIP(offer, streamKey string) (string, error) {
}

<-gatherComplete
return appendOffer(peerConnection.LocalDescription().SDP), nil
return maybePrintOfferAnswer(appendAnswer(peerConnection.LocalDescription().SDP), false), nil
}

0 comments on commit 5d538c2

Please sign in to comment.