From 6e07514c4460c378b8909c25616e3694e33c1905 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Sun, 3 May 2020 20:57:52 +0200 Subject: [PATCH] Better wait for new tracks --- relay.go | 37 ++++++++++++++++++++++++------------- 1 file changed, 24 insertions(+), 13 deletions(-) diff --git a/relay.go b/relay.go index 7457b69..1816fd7 100644 --- a/relay.go +++ b/relay.go @@ -8,6 +8,7 @@ import ( "encoding/json" "io" "log" + "strings" "sync" "time" @@ -192,7 +193,16 @@ func processCloseRequests() { /// Start new call func newCall(mainOffer receivedSignal, r *activeRelay) { - log.Printf("Starting new call: %s / id: %d", r.callHash, r.id) + // I am not sure this is a strong way to determine whether we are having + // a video call or not, but I have not found any better way yet... + isVideoCall := strings.Contains(mainOffer.sdp.SDP, "m=video") || strings.Contains(mainOffer.sdp.SDP, "mid:video") + print(mainOffer.sdp.SDP) + numberTracks := 1 + if isVideoCall { + numberTracks = 2 + } + + log.Printf("Starting new call: %s / id: %d / number of tracks: %d", r.callHash, r.id, numberTracks) // Ice candidates mutex var candidatesMux sync.Mutex @@ -279,7 +289,7 @@ func newCall(mainOffer receivedSignal, r *activeRelay) { return } - localTrackChan := make(chan *webrtc.Track) + localTrackChan := make(chan *webrtc.Track, numberTracks) // Set a handler for when a new remote track starts, this just distributes all our packets // to connected peers @@ -400,17 +410,6 @@ func newCall(mainOffer receivedSignal, r *activeRelay) { // Enter messags loop for { - // Receive new channels - stopCheck := len(localTracks) >= 2 // Stop check if we got all the channel - for !stopCheck { - select { - case t := <-localTrackChan: - localTracks = append(localTracks, t) - case <-time.After(time.Millisecond * 100): - stopCheck = true - } - } - newMessage, ok := <-r.channel if !ok { @@ -426,6 +425,18 @@ func newCall(mainOffer receivedSignal, r *activeRelay) { val.Close() } + // First, we must make sure that we got all the tracks we need, before + // creating an offer (in order not to miss a track while building the offer) + stopCheck := len(localTracks) >= numberTracks // Stop check if we got all the channel + for !stopCheck { + select { + case t := <-localTrackChan: + localTracks = append(localTracks, t) + case <-time.After(time.Millisecond * 1000): + stopCheck = true + } + } + // Ice candidates mutex var newCandidatesMux sync.Mutex newPendingCandidates := make([]*webrtc.ICECandidate, 0)