Skip to content

Commit bf422e0

Browse files
committed
API: Avoid exposing pkg/ice
OnICEConnectionStateChange now return a ICEConnectionState instead of ice.ConnectionState. Resolves #422
1 parent ad647c2 commit bf422e0

File tree

18 files changed

+61
-51
lines changed

18 files changed

+61
-51
lines changed

examples/data-channels-close/main.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"github.com/pions/webrtc"
99
"github.com/pions/webrtc/examples/util"
1010
"github.com/pions/webrtc/pkg/datachannel"
11-
"github.com/pions/webrtc/pkg/ice"
1211
)
1312

1413
func main() {
@@ -32,7 +31,7 @@ func main() {
3231

3332
// Set the handler for ICE connection state
3433
// This will notify you when the peer has connected/disconnected
35-
peerConnection.OnICEConnectionStateChange(func(connectionState ice.ConnectionState) {
34+
peerConnection.OnICEConnectionStateChange(func(connectionState webrtc.ICEConnectionState) {
3635
fmt.Printf("ICE Connection State has changed: %s\n", connectionState.String())
3736
})
3837

examples/data-channels-create/main.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"github.com/pions/webrtc"
88
"github.com/pions/webrtc/examples/util"
99
"github.com/pions/webrtc/pkg/datachannel"
10-
"github.com/pions/webrtc/pkg/ice"
1110
)
1211

1312
func main() {
@@ -32,7 +31,7 @@ func main() {
3231

3332
// Set the handler for ICE connection state
3433
// This will notify you when the peer has connected/disconnected
35-
peerConnection.OnICEConnectionStateChange(func(connectionState ice.ConnectionState) {
34+
peerConnection.OnICEConnectionStateChange(func(connectionState webrtc.ICEConnectionState) {
3635
fmt.Printf("ICE Connection State has changed: %s\n", connectionState.String())
3736
})
3837

examples/data-channels-detach-create/main.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"github.com/pions/datachannel"
88
"github.com/pions/webrtc"
99
"github.com/pions/webrtc/examples/util"
10-
"github.com/pions/webrtc/pkg/ice"
1110
)
1211

1312
const messageSize = 15
@@ -39,7 +38,7 @@ func main() {
3938

4039
// Set the handler for ICE connection state
4140
// This will notify you when the peer has connected/disconnected
42-
peerConnection.OnICEConnectionStateChange(func(connectionState ice.ConnectionState) {
41+
peerConnection.OnICEConnectionStateChange(func(connectionState webrtc.ICEConnectionState) {
4342
fmt.Printf("ICE Connection State has changed: %s\n", connectionState.String())
4443
})
4544

examples/data-channels-detach/main.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"github.com/pions/datachannel"
88
"github.com/pions/webrtc"
99
"github.com/pions/webrtc/examples/util"
10-
"github.com/pions/webrtc/pkg/ice"
1110
)
1211

1312
const messageSize = 15
@@ -35,7 +34,7 @@ func main() {
3534

3635
// Set the handler for ICE connection state
3736
// This will notify you when the peer has connected/disconnected
38-
peerConnection.OnICEConnectionStateChange(func(connectionState ice.ConnectionState) {
37+
peerConnection.OnICEConnectionStateChange(func(connectionState webrtc.ICEConnectionState) {
3938
fmt.Printf("ICE Connection State has changed: %s\n", connectionState.String())
4039
})
4140

examples/data-channels/main.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"github.com/pions/webrtc"
88
"github.com/pions/webrtc/examples/util"
99
"github.com/pions/webrtc/pkg/datachannel"
10-
"github.com/pions/webrtc/pkg/ice"
1110
)
1211

1312
func main() {
@@ -28,7 +27,7 @@ func main() {
2827

2928
// Set the handler for ICE connection state
3029
// This will notify you when the peer has connected/disconnected
31-
peerConnection.OnICEConnectionStateChange(func(connectionState ice.ConnectionState) {
30+
peerConnection.OnICEConnectionStateChange(func(connectionState webrtc.ICEConnectionState) {
3231
fmt.Printf("ICE Connection State has changed: %s\n", connectionState.String())
3332
})
3433

examples/gstreamer-receive/main.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"github.com/pions/webrtc"
1010
"github.com/pions/webrtc/examples/util"
1111
gst "github.com/pions/webrtc/examples/util/gstreamer-sink"
12-
"github.com/pions/webrtc/pkg/ice"
1312
)
1413

1514
// gstreamerReceiveMain is launched in a goroutine because the main thread is needed
@@ -61,7 +60,7 @@ func gstreamerReceiveMain() {
6160

6261
// Set the handler for ICE connection state
6362
// This will notify you when the peer has connected/disconnected
64-
peerConnection.OnICEConnectionStateChange(func(connectionState ice.ConnectionState) {
63+
peerConnection.OnICEConnectionStateChange(func(connectionState webrtc.ICEConnectionState) {
6564
fmt.Printf("Connection State has changed %s \n", connectionState.String())
6665
})
6766

examples/gstreamer-send-offer/main.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"github.com/pions/webrtc"
77
"github.com/pions/webrtc/examples/util"
88
gst "github.com/pions/webrtc/examples/util/gstreamer-src"
9-
"github.com/pions/webrtc/pkg/ice"
109
)
1110

1211
func main() {
@@ -31,7 +30,7 @@ func main() {
3130

3231
// Set the handler for ICE connection state
3332
// This will notify you when the peer has connected/disconnected
34-
peerConnection.OnICEConnectionStateChange(func(connectionState ice.ConnectionState) {
33+
peerConnection.OnICEConnectionStateChange(func(connectionState webrtc.ICEConnectionState) {
3534
fmt.Printf("Connection State has changed %s \n", connectionState.String())
3635
})
3736

examples/gstreamer-send/main.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"github.com/pions/webrtc"
88
"github.com/pions/webrtc/examples/util"
99
gst "github.com/pions/webrtc/examples/util/gstreamer-src"
10-
"github.com/pions/webrtc/pkg/ice"
1110
)
1211

1312
func main() {
@@ -36,7 +35,7 @@ func main() {
3635

3736
// Set the handler for ICE connection state
3837
// This will notify you when the peer has connected/disconnected
39-
peerConnection.OnICEConnectionStateChange(func(connectionState ice.ConnectionState) {
38+
peerConnection.OnICEConnectionStateChange(func(connectionState webrtc.ICEConnectionState) {
4039
fmt.Printf("Connection State has changed %s \n", connectionState.String())
4140
})
4241

examples/janus-gateway/streaming/main.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
janus "github.com/notedit/janus-go"
88
"github.com/pions/webrtc"
99
"github.com/pions/webrtc/examples/util"
10-
"github.com/pions/webrtc/pkg/ice"
1110
"github.com/pions/webrtc/pkg/media/ivfwriter"
1211
)
1312

@@ -52,7 +51,7 @@ func main() {
5251
peerConnection, err := webrtc.NewPeerConnection(config)
5352
util.Check(err)
5453

55-
peerConnection.OnICEConnectionStateChange(func(connectionState ice.ConnectionState) {
54+
peerConnection.OnICEConnectionStateChange(func(connectionState webrtc.ICEConnectionState) {
5655
fmt.Printf("Connection State has changed %s \n", connectionState.String())
5756
})
5857

examples/janus-gateway/video-room/main.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"github.com/pions/webrtc"
99
"github.com/pions/webrtc/examples/util"
1010
gst "github.com/pions/webrtc/examples/util/gstreamer-src"
11-
"github.com/pions/webrtc/pkg/ice"
1211
)
1312

1413
func watchHandle(handle *janus.Handle) {
@@ -52,7 +51,7 @@ func main() {
5251
peerConnection, err := webrtc.NewPeerConnection(config)
5352
util.Check(err)
5453

55-
peerConnection.OnICEConnectionStateChange(func(connectionState ice.ConnectionState) {
54+
peerConnection.OnICEConnectionStateChange(func(connectionState webrtc.ICEConnectionState) {
5655
fmt.Printf("Connection State has changed %s \n", connectionState.String())
5756
})
5857

examples/pion-to-pion/answer/main.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"github.com/pions/webrtc"
1111
"github.com/pions/webrtc/examples/util"
1212
"github.com/pions/webrtc/pkg/datachannel"
13-
"github.com/pions/webrtc/pkg/ice"
1413
)
1514

1615
func main() {
@@ -34,7 +33,7 @@ func main() {
3433

3534
// Set the handler for ICE connection state
3635
// This will notify you when the peer has connected/disconnected
37-
peerConnection.OnICEConnectionStateChange(func(connectionState ice.ConnectionState) {
36+
peerConnection.OnICEConnectionStateChange(func(connectionState webrtc.ICEConnectionState) {
3837
fmt.Printf("ICE Connection State has changed: %s\n", connectionState.String())
3938
})
4039

examples/pion-to-pion/offer/main.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"github.com/pions/webrtc"
1212
"github.com/pions/webrtc/examples/util"
1313
"github.com/pions/webrtc/pkg/datachannel"
14-
"github.com/pions/webrtc/pkg/ice"
1514
)
1615

1716
func main() {
@@ -39,7 +38,7 @@ func main() {
3938

4039
// Set the handler for ICE connection state
4140
// This will notify you when the peer has connected/disconnected
42-
peerConnection.OnICEConnectionStateChange(func(connectionState ice.ConnectionState) {
41+
peerConnection.OnICEConnectionStateChange(func(connectionState webrtc.ICEConnectionState) {
4342
fmt.Printf("ICE Connection State has changed: %s\n", connectionState.String())
4443
})
4544

examples/save-to-disk/main.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"github.com/pions/rtcp"
88
"github.com/pions/webrtc"
99
"github.com/pions/webrtc/examples/util"
10-
"github.com/pions/webrtc/pkg/ice"
1110
"github.com/pions/webrtc/pkg/media/ivfwriter"
1211
)
1312

@@ -61,7 +60,7 @@ func main() {
6160

6261
// Set the handler for ICE connection state
6362
// This will notify you when the peer has connected/disconnected
64-
peerConnection.OnICEConnectionStateChange(func(connectionState ice.ConnectionState) {
63+
peerConnection.OnICEConnectionStateChange(func(connectionState webrtc.ICEConnectionState) {
6564
fmt.Printf("Connection State has changed %s \n", connectionState.String())
6665
})
6766

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ require (
55
github.com/pions/dtls v1.2.1
66
github.com/pions/rtcp v1.0.0
77
github.com/pions/rtp v1.0.0
8-
github.com/pions/sdp/v2 v2.0.0
98
github.com/pions/sctp v1.4.0
9+
github.com/pions/sdp/v2 v2.0.0
1010
github.com/pions/srtp v1.0.3
1111
github.com/pions/stun v0.2.0
1212
github.com/pions/transport v0.2.0

go.sum

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ github.com/pions/rtcp v1.0.0 h1:kYGe6RegZ63yVDkqXaru1+kHZAqHEufP3zfRAGKPycI=
2929
github.com/pions/rtcp v1.0.0/go.mod h1:Q5twXlqiz775Yn37X0cl4lAsfSk8EiHgeNkte59jBY4=
3030
github.com/pions/rtp v1.0.0 h1:H/TUg7bhgBT/mQsUx0adW3cmgwqPmygoYbbRTc3Y7Ek=
3131
github.com/pions/rtp v1.0.0/go.mod h1:GDIt4UYlSz7za4vfaLqihGJJ+yLvgPshnqrF/lm3vcM=
32-
github.com/pions/sdp/v2 v2.0.0 h1:tWh8ehKPtXTaFYF12sBAHqYmhV36Q7YTmm3O6ycNa6M=
33-
github.com/pions/sdp/v2 v2.0.0/go.mod h1:KGRBcHfpkgJXjrzKJz2wj/Jf1KWnsHdoIiqtayQ5QmE=
3432
github.com/pions/sctp v1.3.0/go.mod h1:GZTG/xApE7wdUFEQq2Rmzgxl/+YaB/L1k8xUl1D5bmo=
3533
github.com/pions/sctp v1.4.0 h1:U8SAVk+nWa2248SZGHCYsNyOqe/DM5+TKOAlh9Qdw8c=
3634
github.com/pions/sctp v1.4.0/go.mod h1:dAna+Ct/aIIFiGW45yhGzuQjULWD7ni1vjoKHa9DsyU=
35+
github.com/pions/sdp/v2 v2.0.0 h1:tWh8ehKPtXTaFYF12sBAHqYmhV36Q7YTmm3O6ycNa6M=
36+
github.com/pions/sdp/v2 v2.0.0/go.mod h1:KGRBcHfpkgJXjrzKJz2wj/Jf1KWnsHdoIiqtayQ5QmE=
3737
github.com/pions/srtp v1.0.3 h1:0rlg7yUHQblFA1e451mhx50IkA7+e48ja5K8mljyMYY=
3838
github.com/pions/srtp v1.0.3/go.mod h1:egXe0STDyQDXLm7hjOMzuk7rkAhJ1SHOx+tTgtw/cQs=
3939
github.com/pions/stun v0.2.0 h1:spIzpfkEg6HV+2iIo6qeOsAjtadZKzbXbrd2e9ZCCcs=

peerconnection.go

+42-18
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,7 @@ type PeerConnection struct {
7575

7676
// ICEConnectionState attribute returns the ICE connection state of the
7777
// PeerConnection instance.
78-
// ICEConnectionState ICEConnectionState // FIXME SWAP-FOR-THIS
79-
ICEConnectionState ice.ConnectionState // FIXME REMOVE
78+
iceConnectionState ICEConnectionState
8079

8180
// ConnectionState attribute returns the connection state of the
8281
// PeerConnection instance.
@@ -103,7 +102,7 @@ type PeerConnection struct {
103102
// OnConnectionStateChange func() // FIXME NOT-USED
104103

105104
onSignalingStateChangeHandler func(SignalingState)
106-
onICEConnectionStateChangeHandler func(ice.ConnectionState)
105+
onICEConnectionStateChangeHandler func(ICEConnectionState)
107106
onTrackHandler func(*Track)
108107
onDataChannelHandler func(*DataChannel)
109108

@@ -130,13 +129,12 @@ func (api *API) NewPeerConnection(configuration Configuration) (*PeerConnection,
130129
Certificates: []Certificate{},
131130
ICECandidatePoolSize: 0,
132131
},
133-
isClosed: false,
134-
negotiationNeeded: false,
135-
lastOffer: "",
136-
lastAnswer: "",
137-
SignalingState: SignalingStateStable,
138-
// ICEConnectionState: ICEConnectionStateNew, // FIXME SWAP-FOR-THIS
139-
ICEConnectionState: ice.ConnectionStateNew, // FIXME REMOVE
132+
isClosed: false,
133+
negotiationNeeded: false,
134+
lastOffer: "",
135+
lastAnswer: "",
136+
SignalingState: SignalingStateStable,
137+
iceConnectionState: ICEConnectionStateNew,
140138
ICEGatheringState: ICEGatheringStateNew,
141139
ConnectionState: PeerConnectionStateNew,
142140
dataChannels: make(map[uint16]*DataChannel),
@@ -300,13 +298,13 @@ func (pc *PeerConnection) onTrack(t *Track) (done chan struct{}) {
300298

301299
// OnICEConnectionStateChange sets an event handler which is called
302300
// when an ICE connection state is changed.
303-
func (pc *PeerConnection) OnICEConnectionStateChange(f func(ice.ConnectionState)) {
301+
func (pc *PeerConnection) OnICEConnectionStateChange(f func(ICEConnectionState)) {
304302
pc.mu.Lock()
305303
defer pc.mu.Unlock()
306304
pc.onICEConnectionStateChangeHandler = f
307305
}
308306

309-
func (pc *PeerConnection) onICEConnectionStateChange(cs ice.ConnectionState) (done chan struct{}) {
307+
func (pc *PeerConnection) onICEConnectionStateChange(cs ICEConnectionState) (done chan struct{}) {
310308
pc.mu.RLock()
311309
hdlr := pc.onICEConnectionStateChangeHandler
312310
pc.mu.RUnlock()
@@ -481,10 +479,27 @@ func (pc *PeerConnection) createICETransport() *ICETransport {
481479
t := pc.api.NewICETransport(pc.iceGatherer)
482480

483481
t.OnConnectionStateChange(func(state ICETransportState) {
484-
// We convert the state back to the ICE state to not brake the
485-
// existing public API at this point.
486-
iceState := state.toICE()
487-
pc.iceStateChange(iceState)
482+
cs := ICEConnectionStateNew
483+
switch state {
484+
case ICETransportStateNew:
485+
cs = ICEConnectionStateNew
486+
case ICETransportStateChecking:
487+
cs = ICEConnectionStateChecking
488+
case ICETransportStateConnected:
489+
cs = ICEConnectionStateConnected
490+
case ICETransportStateCompleted:
491+
cs = ICEConnectionStateCompleted
492+
case ICETransportStateFailed:
493+
cs = ICEConnectionStateFailed
494+
case ICETransportStateDisconnected:
495+
cs = ICEConnectionStateDisconnected
496+
case ICETransportStateClosed:
497+
cs = ICEConnectionStateClosed
498+
default:
499+
pcLog.Warnf("OnConnectionStateChange: unhandled ICE state: %s", state)
500+
return
501+
}
502+
pc.iceStateChange(cs)
488503
})
489504

490505
return t
@@ -1037,6 +1052,15 @@ func (pc *PeerConnection) AddICECandidate(s string) error {
10371052
return pc.iceTransport.AddRemoteCandidate(candidate)
10381053
}
10391054

1055+
// ICEConnectionState returns the ICE connection state of the
1056+
// PeerConnection instance.
1057+
func (pc *PeerConnection) ICEConnectionState() ICEConnectionState {
1058+
pc.mu.RLock()
1059+
defer pc.mu.RUnlock()
1060+
1061+
return pc.iceConnectionState
1062+
}
1063+
10401064
// ------------------------------------------------------------------------
10411065
// --- FIXME - BELOW CODE NEEDS RE-ORGANIZATION - https://w3c.github.io/webrtc-pc/#rtp-media-api
10421066
// ------------------------------------------------------------------------
@@ -1333,9 +1357,9 @@ func flattenErrs(errs []error) error {
13331357
return fmt.Errorf(strings.Join(errstrings, "\n"))
13341358
}
13351359

1336-
func (pc *PeerConnection) iceStateChange(newState ice.ConnectionState) {
1360+
func (pc *PeerConnection) iceStateChange(newState ICEConnectionState) {
13371361
pc.mu.Lock()
1338-
pc.ICEConnectionState = newState
1362+
pc.iceConnectionState = newState
13391363
pc.mu.Unlock()
13401364

13411365
pc.onICEConnectionStateChange(newState)

peerconnection_media_test.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88

99
"github.com/pions/rtcp"
1010
"github.com/pions/transport/test"
11-
"github.com/pions/webrtc/pkg/ice"
1211
"github.com/pions/webrtc/pkg/media"
1312
)
1413

@@ -196,8 +195,8 @@ func TestPeerConnection_Media_Shutdown(t *testing.T) {
196195
onTrackFired = true
197196
})
198197

199-
pcAnswer.OnICEConnectionStateChange(func(iceState ice.ConnectionState) {
200-
if iceState == ice.ConnectionStateConnected {
198+
pcAnswer.OnICEConnectionStateChange(func(iceState ICEConnectionState) {
199+
if iceState == ICEConnectionStateConnected {
201200
go func() {
202201
time.Sleep(3 * time.Second) // TODO PeerConnection.Close() doesn't block for all subsystems
203202
close(iceComplete)

peerconnection_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,7 @@ func TestPeerConnection_EventHandlers(t *testing.T) {
497497
onTrackCalled <- true
498498
})
499499

500-
pc.OnICEConnectionStateChange(func(cs ice.ConnectionState) {
500+
pc.OnICEConnectionStateChange(func(cs ICEConnectionState) {
501501
onICEConnectionStateChangeCalled <- true
502502
})
503503

0 commit comments

Comments
 (0)