From bf422e0c0a89eecfd08af6dbfe9aadfaea277d83 Mon Sep 17 00:00:00 2001 From: backkem Date: Wed, 20 Feb 2019 19:57:00 +0100 Subject: [PATCH] API: Avoid exposing pkg/ice OnICEConnectionStateChange now return a ICEConnectionState instead of ice.ConnectionState. Resolves #422 --- examples/data-channels-close/main.go | 3 +- examples/data-channels-create/main.go | 3 +- examples/data-channels-detach-create/main.go | 3 +- examples/data-channels-detach/main.go | 3 +- examples/data-channels/main.go | 3 +- examples/gstreamer-receive/main.go | 3 +- examples/gstreamer-send-offer/main.go | 3 +- examples/gstreamer-send/main.go | 3 +- examples/janus-gateway/streaming/main.go | 3 +- examples/janus-gateway/video-room/main.go | 3 +- examples/pion-to-pion/answer/main.go | 3 +- examples/pion-to-pion/offer/main.go | 3 +- examples/save-to-disk/main.go | 3 +- go.mod | 2 +- go.sum | 4 +- peerconnection.go | 60 ++++++++++++++------ peerconnection_media_test.go | 5 +- peerconnection_test.go | 2 +- 18 files changed, 61 insertions(+), 51 deletions(-) diff --git a/examples/data-channels-close/main.go b/examples/data-channels-close/main.go index 32045aaabb5..cc7fa6e6774 100644 --- a/examples/data-channels-close/main.go +++ b/examples/data-channels-close/main.go @@ -8,7 +8,6 @@ import ( "github.com/pions/webrtc" "github.com/pions/webrtc/examples/util" "github.com/pions/webrtc/pkg/datachannel" - "github.com/pions/webrtc/pkg/ice" ) func main() { @@ -32,7 +31,7 @@ func main() { // Set the handler for ICE connection state // This will notify you when the peer has connected/disconnected - peerConnection.OnICEConnectionStateChange(func(connectionState ice.ConnectionState) { + peerConnection.OnICEConnectionStateChange(func(connectionState webrtc.ICEConnectionState) { fmt.Printf("ICE Connection State has changed: %s\n", connectionState.String()) }) diff --git a/examples/data-channels-create/main.go b/examples/data-channels-create/main.go index ca80fbb0e41..fbbee2c1408 100644 --- a/examples/data-channels-create/main.go +++ b/examples/data-channels-create/main.go @@ -7,7 +7,6 @@ import ( "github.com/pions/webrtc" "github.com/pions/webrtc/examples/util" "github.com/pions/webrtc/pkg/datachannel" - "github.com/pions/webrtc/pkg/ice" ) func main() { @@ -32,7 +31,7 @@ func main() { // Set the handler for ICE connection state // This will notify you when the peer has connected/disconnected - peerConnection.OnICEConnectionStateChange(func(connectionState ice.ConnectionState) { + peerConnection.OnICEConnectionStateChange(func(connectionState webrtc.ICEConnectionState) { fmt.Printf("ICE Connection State has changed: %s\n", connectionState.String()) }) diff --git a/examples/data-channels-detach-create/main.go b/examples/data-channels-detach-create/main.go index 7f760f67e7e..3796c9975d4 100644 --- a/examples/data-channels-detach-create/main.go +++ b/examples/data-channels-detach-create/main.go @@ -7,7 +7,6 @@ import ( "github.com/pions/datachannel" "github.com/pions/webrtc" "github.com/pions/webrtc/examples/util" - "github.com/pions/webrtc/pkg/ice" ) const messageSize = 15 @@ -39,7 +38,7 @@ func main() { // Set the handler for ICE connection state // This will notify you when the peer has connected/disconnected - peerConnection.OnICEConnectionStateChange(func(connectionState ice.ConnectionState) { + peerConnection.OnICEConnectionStateChange(func(connectionState webrtc.ICEConnectionState) { fmt.Printf("ICE Connection State has changed: %s\n", connectionState.String()) }) diff --git a/examples/data-channels-detach/main.go b/examples/data-channels-detach/main.go index 4964f253957..7f69565035f 100644 --- a/examples/data-channels-detach/main.go +++ b/examples/data-channels-detach/main.go @@ -7,7 +7,6 @@ import ( "github.com/pions/datachannel" "github.com/pions/webrtc" "github.com/pions/webrtc/examples/util" - "github.com/pions/webrtc/pkg/ice" ) const messageSize = 15 @@ -35,7 +34,7 @@ func main() { // Set the handler for ICE connection state // This will notify you when the peer has connected/disconnected - peerConnection.OnICEConnectionStateChange(func(connectionState ice.ConnectionState) { + peerConnection.OnICEConnectionStateChange(func(connectionState webrtc.ICEConnectionState) { fmt.Printf("ICE Connection State has changed: %s\n", connectionState.String()) }) diff --git a/examples/data-channels/main.go b/examples/data-channels/main.go index 9585ffda780..32dd6aac0ba 100644 --- a/examples/data-channels/main.go +++ b/examples/data-channels/main.go @@ -7,7 +7,6 @@ import ( "github.com/pions/webrtc" "github.com/pions/webrtc/examples/util" "github.com/pions/webrtc/pkg/datachannel" - "github.com/pions/webrtc/pkg/ice" ) func main() { @@ -28,7 +27,7 @@ func main() { // Set the handler for ICE connection state // This will notify you when the peer has connected/disconnected - peerConnection.OnICEConnectionStateChange(func(connectionState ice.ConnectionState) { + peerConnection.OnICEConnectionStateChange(func(connectionState webrtc.ICEConnectionState) { fmt.Printf("ICE Connection State has changed: %s\n", connectionState.String()) }) diff --git a/examples/gstreamer-receive/main.go b/examples/gstreamer-receive/main.go index 5ad61bbcb8c..9d9457df2c8 100644 --- a/examples/gstreamer-receive/main.go +++ b/examples/gstreamer-receive/main.go @@ -9,7 +9,6 @@ import ( "github.com/pions/webrtc" "github.com/pions/webrtc/examples/util" gst "github.com/pions/webrtc/examples/util/gstreamer-sink" - "github.com/pions/webrtc/pkg/ice" ) // gstreamerReceiveMain is launched in a goroutine because the main thread is needed @@ -61,7 +60,7 @@ func gstreamerReceiveMain() { // Set the handler for ICE connection state // This will notify you when the peer has connected/disconnected - peerConnection.OnICEConnectionStateChange(func(connectionState ice.ConnectionState) { + peerConnection.OnICEConnectionStateChange(func(connectionState webrtc.ICEConnectionState) { fmt.Printf("Connection State has changed %s \n", connectionState.String()) }) diff --git a/examples/gstreamer-send-offer/main.go b/examples/gstreamer-send-offer/main.go index 71ff4a204eb..56f9802db3b 100644 --- a/examples/gstreamer-send-offer/main.go +++ b/examples/gstreamer-send-offer/main.go @@ -6,7 +6,6 @@ import ( "github.com/pions/webrtc" "github.com/pions/webrtc/examples/util" gst "github.com/pions/webrtc/examples/util/gstreamer-src" - "github.com/pions/webrtc/pkg/ice" ) func main() { @@ -31,7 +30,7 @@ func main() { // Set the handler for ICE connection state // This will notify you when the peer has connected/disconnected - peerConnection.OnICEConnectionStateChange(func(connectionState ice.ConnectionState) { + peerConnection.OnICEConnectionStateChange(func(connectionState webrtc.ICEConnectionState) { fmt.Printf("Connection State has changed %s \n", connectionState.String()) }) diff --git a/examples/gstreamer-send/main.go b/examples/gstreamer-send/main.go index 18791cf690a..9058deff121 100644 --- a/examples/gstreamer-send/main.go +++ b/examples/gstreamer-send/main.go @@ -7,7 +7,6 @@ import ( "github.com/pions/webrtc" "github.com/pions/webrtc/examples/util" gst "github.com/pions/webrtc/examples/util/gstreamer-src" - "github.com/pions/webrtc/pkg/ice" ) func main() { @@ -36,7 +35,7 @@ func main() { // Set the handler for ICE connection state // This will notify you when the peer has connected/disconnected - peerConnection.OnICEConnectionStateChange(func(connectionState ice.ConnectionState) { + peerConnection.OnICEConnectionStateChange(func(connectionState webrtc.ICEConnectionState) { fmt.Printf("Connection State has changed %s \n", connectionState.String()) }) diff --git a/examples/janus-gateway/streaming/main.go b/examples/janus-gateway/streaming/main.go index 3a835f7bf55..6ac0cecc1e2 100644 --- a/examples/janus-gateway/streaming/main.go +++ b/examples/janus-gateway/streaming/main.go @@ -7,7 +7,6 @@ import ( janus "github.com/notedit/janus-go" "github.com/pions/webrtc" "github.com/pions/webrtc/examples/util" - "github.com/pions/webrtc/pkg/ice" "github.com/pions/webrtc/pkg/media/ivfwriter" ) @@ -52,7 +51,7 @@ func main() { peerConnection, err := webrtc.NewPeerConnection(config) util.Check(err) - peerConnection.OnICEConnectionStateChange(func(connectionState ice.ConnectionState) { + peerConnection.OnICEConnectionStateChange(func(connectionState webrtc.ICEConnectionState) { fmt.Printf("Connection State has changed %s \n", connectionState.String()) }) diff --git a/examples/janus-gateway/video-room/main.go b/examples/janus-gateway/video-room/main.go index 057559103ad..e05bac743d5 100644 --- a/examples/janus-gateway/video-room/main.go +++ b/examples/janus-gateway/video-room/main.go @@ -8,7 +8,6 @@ import ( "github.com/pions/webrtc" "github.com/pions/webrtc/examples/util" gst "github.com/pions/webrtc/examples/util/gstreamer-src" - "github.com/pions/webrtc/pkg/ice" ) func watchHandle(handle *janus.Handle) { @@ -52,7 +51,7 @@ func main() { peerConnection, err := webrtc.NewPeerConnection(config) util.Check(err) - peerConnection.OnICEConnectionStateChange(func(connectionState ice.ConnectionState) { + peerConnection.OnICEConnectionStateChange(func(connectionState webrtc.ICEConnectionState) { fmt.Printf("Connection State has changed %s \n", connectionState.String()) }) diff --git a/examples/pion-to-pion/answer/main.go b/examples/pion-to-pion/answer/main.go index 710d301578f..eb0d4db5b36 100644 --- a/examples/pion-to-pion/answer/main.go +++ b/examples/pion-to-pion/answer/main.go @@ -10,7 +10,6 @@ import ( "github.com/pions/webrtc" "github.com/pions/webrtc/examples/util" "github.com/pions/webrtc/pkg/datachannel" - "github.com/pions/webrtc/pkg/ice" ) func main() { @@ -34,7 +33,7 @@ func main() { // Set the handler for ICE connection state // This will notify you when the peer has connected/disconnected - peerConnection.OnICEConnectionStateChange(func(connectionState ice.ConnectionState) { + peerConnection.OnICEConnectionStateChange(func(connectionState webrtc.ICEConnectionState) { fmt.Printf("ICE Connection State has changed: %s\n", connectionState.String()) }) diff --git a/examples/pion-to-pion/offer/main.go b/examples/pion-to-pion/offer/main.go index 371f5a29c2c..bcf43514d82 100644 --- a/examples/pion-to-pion/offer/main.go +++ b/examples/pion-to-pion/offer/main.go @@ -11,7 +11,6 @@ import ( "github.com/pions/webrtc" "github.com/pions/webrtc/examples/util" "github.com/pions/webrtc/pkg/datachannel" - "github.com/pions/webrtc/pkg/ice" ) func main() { @@ -39,7 +38,7 @@ func main() { // Set the handler for ICE connection state // This will notify you when the peer has connected/disconnected - peerConnection.OnICEConnectionStateChange(func(connectionState ice.ConnectionState) { + peerConnection.OnICEConnectionStateChange(func(connectionState webrtc.ICEConnectionState) { fmt.Printf("ICE Connection State has changed: %s\n", connectionState.String()) }) diff --git a/examples/save-to-disk/main.go b/examples/save-to-disk/main.go index 628870b24d0..e0ee163b522 100644 --- a/examples/save-to-disk/main.go +++ b/examples/save-to-disk/main.go @@ -7,7 +7,6 @@ import ( "github.com/pions/rtcp" "github.com/pions/webrtc" "github.com/pions/webrtc/examples/util" - "github.com/pions/webrtc/pkg/ice" "github.com/pions/webrtc/pkg/media/ivfwriter" ) @@ -61,7 +60,7 @@ func main() { // Set the handler for ICE connection state // This will notify you when the peer has connected/disconnected - peerConnection.OnICEConnectionStateChange(func(connectionState ice.ConnectionState) { + peerConnection.OnICEConnectionStateChange(func(connectionState webrtc.ICEConnectionState) { fmt.Printf("Connection State has changed %s \n", connectionState.String()) }) diff --git a/go.mod b/go.mod index a50757e1eee..59bf2144f1d 100644 --- a/go.mod +++ b/go.mod @@ -5,8 +5,8 @@ require ( github.com/pions/dtls v1.2.1 github.com/pions/rtcp v1.0.0 github.com/pions/rtp v1.0.0 - github.com/pions/sdp/v2 v2.0.0 github.com/pions/sctp v1.4.0 + github.com/pions/sdp/v2 v2.0.0 github.com/pions/srtp v1.0.3 github.com/pions/stun v0.2.0 github.com/pions/transport v0.2.0 diff --git a/go.sum b/go.sum index 73a8804eaa3..6f8d752c6b3 100644 --- a/go.sum +++ b/go.sum @@ -29,11 +29,11 @@ github.com/pions/rtcp v1.0.0 h1:kYGe6RegZ63yVDkqXaru1+kHZAqHEufP3zfRAGKPycI= github.com/pions/rtcp v1.0.0/go.mod h1:Q5twXlqiz775Yn37X0cl4lAsfSk8EiHgeNkte59jBY4= github.com/pions/rtp v1.0.0 h1:H/TUg7bhgBT/mQsUx0adW3cmgwqPmygoYbbRTc3Y7Ek= github.com/pions/rtp v1.0.0/go.mod h1:GDIt4UYlSz7za4vfaLqihGJJ+yLvgPshnqrF/lm3vcM= -github.com/pions/sdp/v2 v2.0.0 h1:tWh8ehKPtXTaFYF12sBAHqYmhV36Q7YTmm3O6ycNa6M= -github.com/pions/sdp/v2 v2.0.0/go.mod h1:KGRBcHfpkgJXjrzKJz2wj/Jf1KWnsHdoIiqtayQ5QmE= github.com/pions/sctp v1.3.0/go.mod h1:GZTG/xApE7wdUFEQq2Rmzgxl/+YaB/L1k8xUl1D5bmo= github.com/pions/sctp v1.4.0 h1:U8SAVk+nWa2248SZGHCYsNyOqe/DM5+TKOAlh9Qdw8c= github.com/pions/sctp v1.4.0/go.mod h1:dAna+Ct/aIIFiGW45yhGzuQjULWD7ni1vjoKHa9DsyU= +github.com/pions/sdp/v2 v2.0.0 h1:tWh8ehKPtXTaFYF12sBAHqYmhV36Q7YTmm3O6ycNa6M= +github.com/pions/sdp/v2 v2.0.0/go.mod h1:KGRBcHfpkgJXjrzKJz2wj/Jf1KWnsHdoIiqtayQ5QmE= github.com/pions/srtp v1.0.3 h1:0rlg7yUHQblFA1e451mhx50IkA7+e48ja5K8mljyMYY= github.com/pions/srtp v1.0.3/go.mod h1:egXe0STDyQDXLm7hjOMzuk7rkAhJ1SHOx+tTgtw/cQs= github.com/pions/stun v0.2.0 h1:spIzpfkEg6HV+2iIo6qeOsAjtadZKzbXbrd2e9ZCCcs= diff --git a/peerconnection.go b/peerconnection.go index d7b0a2da013..8ea327cb077 100644 --- a/peerconnection.go +++ b/peerconnection.go @@ -75,8 +75,7 @@ type PeerConnection struct { // ICEConnectionState attribute returns the ICE connection state of the // PeerConnection instance. - // ICEConnectionState ICEConnectionState // FIXME SWAP-FOR-THIS - ICEConnectionState ice.ConnectionState // FIXME REMOVE + iceConnectionState ICEConnectionState // ConnectionState attribute returns the connection state of the // PeerConnection instance. @@ -103,7 +102,7 @@ type PeerConnection struct { // OnConnectionStateChange func() // FIXME NOT-USED onSignalingStateChangeHandler func(SignalingState) - onICEConnectionStateChangeHandler func(ice.ConnectionState) + onICEConnectionStateChangeHandler func(ICEConnectionState) onTrackHandler func(*Track) onDataChannelHandler func(*DataChannel) @@ -130,13 +129,12 @@ func (api *API) NewPeerConnection(configuration Configuration) (*PeerConnection, Certificates: []Certificate{}, ICECandidatePoolSize: 0, }, - isClosed: false, - negotiationNeeded: false, - lastOffer: "", - lastAnswer: "", - SignalingState: SignalingStateStable, - // ICEConnectionState: ICEConnectionStateNew, // FIXME SWAP-FOR-THIS - ICEConnectionState: ice.ConnectionStateNew, // FIXME REMOVE + isClosed: false, + negotiationNeeded: false, + lastOffer: "", + lastAnswer: "", + SignalingState: SignalingStateStable, + iceConnectionState: ICEConnectionStateNew, ICEGatheringState: ICEGatheringStateNew, ConnectionState: PeerConnectionStateNew, dataChannels: make(map[uint16]*DataChannel), @@ -300,13 +298,13 @@ func (pc *PeerConnection) onTrack(t *Track) (done chan struct{}) { // OnICEConnectionStateChange sets an event handler which is called // when an ICE connection state is changed. -func (pc *PeerConnection) OnICEConnectionStateChange(f func(ice.ConnectionState)) { +func (pc *PeerConnection) OnICEConnectionStateChange(f func(ICEConnectionState)) { pc.mu.Lock() defer pc.mu.Unlock() pc.onICEConnectionStateChangeHandler = f } -func (pc *PeerConnection) onICEConnectionStateChange(cs ice.ConnectionState) (done chan struct{}) { +func (pc *PeerConnection) onICEConnectionStateChange(cs ICEConnectionState) (done chan struct{}) { pc.mu.RLock() hdlr := pc.onICEConnectionStateChangeHandler pc.mu.RUnlock() @@ -481,10 +479,27 @@ func (pc *PeerConnection) createICETransport() *ICETransport { t := pc.api.NewICETransport(pc.iceGatherer) t.OnConnectionStateChange(func(state ICETransportState) { - // We convert the state back to the ICE state to not brake the - // existing public API at this point. - iceState := state.toICE() - pc.iceStateChange(iceState) + cs := ICEConnectionStateNew + switch state { + case ICETransportStateNew: + cs = ICEConnectionStateNew + case ICETransportStateChecking: + cs = ICEConnectionStateChecking + case ICETransportStateConnected: + cs = ICEConnectionStateConnected + case ICETransportStateCompleted: + cs = ICEConnectionStateCompleted + case ICETransportStateFailed: + cs = ICEConnectionStateFailed + case ICETransportStateDisconnected: + cs = ICEConnectionStateDisconnected + case ICETransportStateClosed: + cs = ICEConnectionStateClosed + default: + pcLog.Warnf("OnConnectionStateChange: unhandled ICE state: %s", state) + return + } + pc.iceStateChange(cs) }) return t @@ -1037,6 +1052,15 @@ func (pc *PeerConnection) AddICECandidate(s string) error { return pc.iceTransport.AddRemoteCandidate(candidate) } +// ICEConnectionState returns the ICE connection state of the +// PeerConnection instance. +func (pc *PeerConnection) ICEConnectionState() ICEConnectionState { + pc.mu.RLock() + defer pc.mu.RUnlock() + + return pc.iceConnectionState +} + // ------------------------------------------------------------------------ // --- FIXME - BELOW CODE NEEDS RE-ORGANIZATION - https://w3c.github.io/webrtc-pc/#rtp-media-api // ------------------------------------------------------------------------ @@ -1333,9 +1357,9 @@ func flattenErrs(errs []error) error { return fmt.Errorf(strings.Join(errstrings, "\n")) } -func (pc *PeerConnection) iceStateChange(newState ice.ConnectionState) { +func (pc *PeerConnection) iceStateChange(newState ICEConnectionState) { pc.mu.Lock() - pc.ICEConnectionState = newState + pc.iceConnectionState = newState pc.mu.Unlock() pc.onICEConnectionStateChange(newState) diff --git a/peerconnection_media_test.go b/peerconnection_media_test.go index 7fd07874eb7..dc25577cd8d 100644 --- a/peerconnection_media_test.go +++ b/peerconnection_media_test.go @@ -8,7 +8,6 @@ import ( "github.com/pions/rtcp" "github.com/pions/transport/test" - "github.com/pions/webrtc/pkg/ice" "github.com/pions/webrtc/pkg/media" ) @@ -196,8 +195,8 @@ func TestPeerConnection_Media_Shutdown(t *testing.T) { onTrackFired = true }) - pcAnswer.OnICEConnectionStateChange(func(iceState ice.ConnectionState) { - if iceState == ice.ConnectionStateConnected { + pcAnswer.OnICEConnectionStateChange(func(iceState ICEConnectionState) { + if iceState == ICEConnectionStateConnected { go func() { time.Sleep(3 * time.Second) // TODO PeerConnection.Close() doesn't block for all subsystems close(iceComplete) diff --git a/peerconnection_test.go b/peerconnection_test.go index cccbb86185d..932bb23121d 100644 --- a/peerconnection_test.go +++ b/peerconnection_test.go @@ -497,7 +497,7 @@ func TestPeerConnection_EventHandlers(t *testing.T) { onTrackCalled <- true }) - pc.OnICEConnectionStateChange(func(cs ice.ConnectionState) { + pc.OnICEConnectionStateChange(func(cs ICEConnectionState) { onICEConnectionStateChangeCalled <- true })