diff --git a/examples/data-channels-close/main.go b/examples/data-channels-close/main.go index 32045aaa..cc7fa6e6 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 ca80fbb0..fbbee2c1 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 7f760f67..3796c997 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 4964f253..7f695650 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 9585ffda..32dd6aac 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 5ad61bbc..9d9457df 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 71ff4a20..56f9802d 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 18791cf6..9058deff 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 3a835f7b..6ac0cecc 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 05755910..e05bac74 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 710d3015..eb0d4db5 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 371f5a29..bcf43514 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 628870b2..e0ee163b 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 a50757e1..59bf2144 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 73a8804e..6f8d752c 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 d7b0a2da..8ea327cb 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 7fd07874..dc25577c 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 cccbb861..932bb231 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 })