Better wait for new tracks

This commit is contained in:
Pierre HUBERT 2020-05-03 20:57:52 +02:00
parent 698132b070
commit 6e07514c44

View File

@ -8,6 +8,7 @@ import (
"encoding/json" "encoding/json"
"io" "io"
"log" "log"
"strings"
"sync" "sync"
"time" "time"
@ -192,7 +193,16 @@ func processCloseRequests() {
/// Start new call /// Start new call
func newCall(mainOffer receivedSignal, r *activeRelay) { 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 // Ice candidates mutex
var candidatesMux sync.Mutex var candidatesMux sync.Mutex
@ -279,7 +289,7 @@ func newCall(mainOffer receivedSignal, r *activeRelay) {
return 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 // Set a handler for when a new remote track starts, this just distributes all our packets
// to connected peers // to connected peers
@ -400,17 +410,6 @@ func newCall(mainOffer receivedSignal, r *activeRelay) {
// Enter messags loop // Enter messags loop
for { 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 newMessage, ok := <-r.channel
if !ok { if !ok {
@ -426,6 +425,18 @@ func newCall(mainOffer receivedSignal, r *activeRelay) {
val.Close() 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 // Ice candidates mutex
var newCandidatesMux sync.Mutex var newCandidatesMux sync.Mutex
newPendingCandidates := make([]*webrtc.ICECandidate, 0) newPendingCandidates := make([]*webrtc.ICECandidate, 0)