diff --git a/machinery/src/routers/mqtt/main.go b/machinery/src/routers/mqtt/main.go index ebb27ab..f124f42 100644 --- a/machinery/src/routers/mqtt/main.go +++ b/machinery/src/routers/mqtt/main.go @@ -476,14 +476,9 @@ func HandleReceiveHDCandidates(mqttClient mqtt.Client, hubKey string, payload mo if receiveHDCandidatesPayload.Timestamp != 0 { if communication.CameraConnected { + // Register candidate channel key := configuration.Config.Key + "/" + receiveHDCandidatesPayload.SessionID - channel := webrtc.CandidateArrays[key] - if channel == nil { - channel = make(chan string) - webrtc.CandidateArrays[key] = channel - } - log.Log.Info("HandleReceiveHDCandidates: " + receiveHDCandidatesPayload.Candidate) - channel <- receiveHDCandidatesPayload.Candidate + webrtc.RegisterCandidates(key, receiveHDCandidatesPayload) } else { log.Log.Info("HandleReceiveHDCandidates: received candidate, but camera is not connected.") } diff --git a/machinery/src/webrtc/main.go b/machinery/src/webrtc/main.go index 059fdbd..59d9ba1 100644 --- a/machinery/src/webrtc/main.go +++ b/machinery/src/webrtc/main.go @@ -87,6 +87,21 @@ func (w WebRTC) CreateOffer(sd []byte) pionWebRTC.SessionDescription { return offer } +func RegisterCandidates(key string, candidate models.ReceiveHDCandidatesPayload) { + + // Set lock + CandidatesMutex.Lock() + defer CandidatesMutex.Unlock() + + channel := CandidateArrays[key] + if channel == nil { + channel = make(chan string) + CandidateArrays[key] = channel + } + log.Log.Info("HandleReceiveHDCandidates: " + candidate.Candidate) + channel <- candidate.Candidate +} + func InitializeWebRTCConnection(configuration *models.Configuration, communication *models.Communication, mqttClient mqtt.Client, videoTrack *pionWebRTC.TrackLocalStaticSample, audioTrack *pionWebRTC.TrackLocalStaticSample, handshake models.RequestHDStreamPayload, candidates chan string) { config := configuration.Config @@ -145,6 +160,9 @@ func InitializeWebRTCConnection(configuration *models.Configuration, communicati peerConnection.OnICEConnectionStateChange(func(connectionState pionWebRTC.ICEConnectionState) { if connectionState == pionWebRTC.ICEConnectionStateDisconnected { + CandidatesMutex.Lock() + defer CandidatesMutex.Unlock() + atomic.AddInt64(&peerConnectionCount, -1) peerConnections[handshake.SessionID] = nil close(candidates)