Skip to content

Commit

Permalink
Safely mutate local description
Browse files Browse the repository at this point in the history
adding candidates races with negotiation check
  • Loading branch information
tarrencev committed Nov 3, 2020
1 parent 0582c68 commit 78f7c97
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
12 changes: 7 additions & 5 deletions peerconnection.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,10 +320,11 @@ func (pc *PeerConnection) checkNegotiationNeeded() bool { //nolint:gocognit
// To check if negotiation is needed for connection, perform the following checks:
// Skip 1, 2 steps
// Step 3
pc.mu.RLock()
pc.mu.Lock()
defer pc.mu.Unlock()

localDesc := pc.currentLocalDescription
remoteDesc := pc.currentRemoteDescription
pc.mu.RUnlock()

if localDesc == nil {
return true
Expand All @@ -337,9 +338,6 @@ func (pc *PeerConnection) checkNegotiationNeeded() bool { //nolint:gocognit
return true
}

pc.mu.Lock()
defer pc.mu.Unlock()

for _, t := range pc.rtpTransceivers {
// https://www.w3.org/TR/webrtc/#dfn-update-the-negotiation-needed-flag
// Step 5.1
Expand Down Expand Up @@ -1843,6 +1841,8 @@ func (pc *PeerConnection) newRTPTransceiver(
// into the stable state plus any local candidates that have been generated
// by the ICEAgent since the offer or answer was created.
func (pc *PeerConnection) CurrentLocalDescription() *SessionDescription {
pc.mu.Lock()
defer pc.mu.Unlock()
return populateLocalCandidates(pc.currentLocalDescription, pc.iceGatherer, pc.ICEGatheringState())
}

Expand All @@ -1851,6 +1851,8 @@ func (pc *PeerConnection) CurrentLocalDescription() *SessionDescription {
// generated by the ICEAgent since the offer or answer was created. If the
// PeerConnection is in the stable state, the value is null.
func (pc *PeerConnection) PendingLocalDescription() *SessionDescription {
pc.mu.Lock()
defer pc.mu.Unlock()
return populateLocalCandidates(pc.pendingLocalDescription, pc.iceGatherer, pc.ICEGatheringState())
}

Expand Down
3 changes: 2 additions & 1 deletion sdp.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,8 @@ func populateLocalCandidates(sessionDescription *SessionDescription, i *ICEGathe
}

parsed := sessionDescription.parsed
for _, m := range parsed.MediaDescriptions {
if len(parsed.MediaDescriptions) > 0 {
m := parsed.MediaDescriptions[0]
if err = addCandidatesToMediaDescriptions(candidates, m, iceGatheringState); err != nil {
return sessionDescription
}
Expand Down

0 comments on commit 78f7c97

Please sign in to comment.