API: Avoid exposing pkg/ice

OnICEConnectionStateChange now return a ICEConnectionState instead of
ice.ConnectionState.
Resolves #422
This commit is contained in:
backkem
2019-02-20 19:57:00 +01:00
committed by Michiel De Backker
parent ad647c28d7
commit bf422e0c0a
18 changed files with 61 additions and 51 deletions

View File

@@ -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())
})

View File

@@ -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())
})

View File

@@ -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())
})

View File

@@ -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())
})

View File

@@ -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())
})

View File

@@ -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())
})

View File

@@ -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())
})

View File

@@ -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())
})

View File

@@ -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())
})

View File

@@ -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())
})

View File

@@ -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())
})

View File

@@ -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())
})

View File

@@ -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())
})

2
go.mod
View File

@@ -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

4
go.sum
View File

@@ -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=

View File

@@ -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)
@@ -135,8 +134,7 @@ func (api *API) NewPeerConnection(configuration Configuration) (*PeerConnection,
lastOffer: "",
lastAnswer: "",
SignalingState: SignalingStateStable,
// ICEConnectionState: ICEConnectionStateNew, // FIXME SWAP-FOR-THIS
ICEConnectionState: ice.ConnectionStateNew, // FIXME REMOVE
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)

View File

@@ -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)

View File

@@ -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
})