Skip to content

Commit

Permalink
hotfix: add locks to make sure candidates are not send to a closed ca…
Browse files Browse the repository at this point in the history
…ndidate channel
  • Loading branch information
cedricve committed Nov 18, 2023
1 parent dd7fcb3 commit ca84664
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
9 changes: 2 additions & 7 deletions machinery/src/routers/mqtt/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
}
Expand Down
18 changes: 18 additions & 0 deletions machinery/src/webrtc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit ca84664

Please sign in to comment.