@@ -45,8 +45,8 @@ type PeerConnection struct {
45
45
currentRemoteDescription * SessionDescription
46
46
pendingRemoteDescription * SessionDescription
47
47
signalingState SignalingState
48
- iceConnectionState ICEConnectionState
49
- connectionState PeerConnectionState
48
+ iceConnectionState atomic. Value // ICEConnectionState
49
+ connectionState atomic. Value // PeerConnectionState
50
50
51
51
idpLoginURL * string
52
52
@@ -66,8 +66,8 @@ type PeerConnection struct {
66
66
rtpTransceivers []* RTPTransceiver
67
67
68
68
onSignalingStateChangeHandler func (SignalingState )
69
- onICEConnectionStateChangeHandler func (ICEConnectionState )
70
- onConnectionStateChangeHandler func (PeerConnectionState )
69
+ onICEConnectionStateChangeHandler atomic. Value // func(ICEConnectionState)
70
+ onConnectionStateChangeHandler atomic. Value // func(PeerConnectionState)
71
71
onTrackHandler func (* TrackRemote , * RTPReceiver )
72
72
onDataChannelHandler func (* DataChannel )
73
73
onNegotiationNeededHandler atomic.Value // func()
@@ -128,12 +128,12 @@ func (api *API) NewPeerConnection(configuration Configuration) (*PeerConnection,
128
128
lastAnswer : "" ,
129
129
greaterMid : - 1 ,
130
130
signalingState : SignalingStateStable ,
131
- iceConnectionState : ICEConnectionStateNew ,
132
- connectionState : PeerConnectionStateNew ,
133
131
134
132
api : api ,
135
133
log : api .settingEngine .LoggerFactory .NewLogger ("pc" ),
136
134
}
135
+ pc .iceConnectionState .Store (ICEConnectionStateNew )
136
+ pc .connectionState .Store (PeerConnectionStateNew )
137
137
138
138
if ! api .settingEngine .disableMediaEngineCopy {
139
139
pc .api = & API {
@@ -458,29 +458,21 @@ func (pc *PeerConnection) onTrack(t *TrackRemote, r *RTPReceiver) {
458
458
// OnICEConnectionStateChange sets an event handler which is called
459
459
// when an ICE connection state is changed.
460
460
func (pc * PeerConnection ) OnICEConnectionStateChange (f func (ICEConnectionState )) {
461
- pc .mu .Lock ()
462
- defer pc .mu .Unlock ()
463
- pc .onICEConnectionStateChangeHandler = f
461
+ pc .onICEConnectionStateChangeHandler .Store (f )
464
462
}
465
463
466
464
func (pc * PeerConnection ) onICEConnectionStateChange (cs ICEConnectionState ) {
467
- pc .mu .Lock ()
468
- pc .iceConnectionState = cs
469
- handler := pc .onICEConnectionStateChangeHandler
470
- pc .mu .Unlock ()
471
-
465
+ pc .iceConnectionState .Store (cs )
472
466
pc .log .Infof ("ICE connection state changed: %s" , cs )
473
- if handler != nil {
474
- go handler (cs )
467
+ if handler := pc . onICEConnectionStateChangeHandler . Load (); handler != nil {
468
+ handler .( func ( ICEConnectionState )) (cs )
475
469
}
476
470
}
477
471
478
472
// OnConnectionStateChange sets an event handler which is called
479
473
// when the PeerConnectionState has changed
480
474
func (pc * PeerConnection ) OnConnectionStateChange (f func (PeerConnectionState )) {
481
- pc .mu .Lock ()
482
- defer pc .mu .Unlock ()
483
- pc .onConnectionStateChangeHandler = f
475
+ pc .onConnectionStateChangeHandler .Store (f )
484
476
}
485
477
486
478
// SetConfiguration updates the configuration of this PeerConnection object.
@@ -714,9 +706,6 @@ func (pc *PeerConnection) createICEGatherer() (*ICEGatherer, error) {
714
706
// Update the PeerConnectionState given the state of relevant transports
715
707
// https://www.w3.org/TR/webrtc/#rtcpeerconnectionstate-enum
716
708
func (pc * PeerConnection ) updateConnectionState (iceConnectionState ICEConnectionState , dtlsTransportState DTLSTransportState ) {
717
- pc .mu .Lock ()
718
- defer pc .mu .Unlock ()
719
-
720
709
connectionState := PeerConnectionStateNew
721
710
switch {
722
711
// The RTCPeerConnection object's [[IsClosed]] slot is true.
@@ -743,15 +732,14 @@ func (pc *PeerConnection) updateConnectionState(iceConnectionState ICEConnection
743
732
connectionState = PeerConnectionStateConnecting
744
733
}
745
734
746
- if pc .connectionState == connectionState {
735
+ if pc .connectionState . Load () == connectionState {
747
736
return
748
737
}
749
738
750
739
pc .log .Infof ("peer connection state changed: %s" , connectionState )
751
- pc .connectionState = connectionState
752
- handler := pc .onConnectionStateChangeHandler
753
- if handler != nil {
754
- go handler (connectionState )
740
+ pc .connectionState .Store (connectionState )
741
+ if handler := pc .onConnectionStateChangeHandler .Load (); handler != nil {
742
+ go handler .(func (PeerConnectionState ))(connectionState )
755
743
}
756
744
}
757
745
@@ -1534,10 +1522,7 @@ func (pc *PeerConnection) AddICECandidate(candidate ICECandidateInit) error {
1534
1522
// ICEConnectionState returns the ICE connection state of the
1535
1523
// PeerConnection instance.
1536
1524
func (pc * PeerConnection ) ICEConnectionState () ICEConnectionState {
1537
- pc .mu .RLock ()
1538
- defer pc .mu .RUnlock ()
1539
-
1540
- return pc .iceConnectionState
1525
+ return pc .iceConnectionState .Load ().(ICEConnectionState )
1541
1526
}
1542
1527
1543
1528
// GetSenders returns the RTPSender that are currently attached to this PeerConnection
@@ -1959,10 +1944,7 @@ func (pc *PeerConnection) ICEGatheringState() ICEGatheringState {
1959
1944
// ConnectionState attribute returns the connection state of the
1960
1945
// PeerConnection instance.
1961
1946
func (pc * PeerConnection ) ConnectionState () PeerConnectionState {
1962
- pc .mu .Lock ()
1963
- defer pc .mu .Unlock ()
1964
-
1965
- return pc .connectionState
1947
+ return pc .connectionState .Load ().(PeerConnectionState )
1966
1948
}
1967
1949
1968
1950
// GetStats return data providing statistics about the overall connection
0 commit comments