Skip to content

Commit

Permalink
API: Avoid exposing pkg/ice
Browse files Browse the repository at this point in the history
OnICEConnectionStateChange now return a ICEConnectionState instead of
ice.ConnectionState.
Resolves #422
  • Loading branch information
backkem committed Feb 20, 2019
1 parent ad647c2 commit bf422e0
Show file tree
Hide file tree
Showing 18 changed files with 61 additions and 51 deletions.
3 changes: 1 addition & 2 deletions examples/data-channels-close/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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())
})

Expand Down
3 changes: 1 addition & 2 deletions examples/data-channels-create/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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())
})

Expand Down
3 changes: 1 addition & 2 deletions examples/data-channels-detach-create/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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())
})

Expand Down
3 changes: 1 addition & 2 deletions examples/data-channels-detach/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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())
})

Expand Down
3 changes: 1 addition & 2 deletions examples/data-channels/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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())
})

Expand Down
3 changes: 1 addition & 2 deletions examples/gstreamer-receive/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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())
})

Expand Down
3 changes: 1 addition & 2 deletions examples/gstreamer-send-offer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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())
})

Expand Down
3 changes: 1 addition & 2 deletions examples/gstreamer-send/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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())
})

Expand Down
3 changes: 1 addition & 2 deletions examples/janus-gateway/streaming/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

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

Expand Down
3 changes: 1 addition & 2 deletions examples/janus-gateway/video-room/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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())
})

Expand Down
3 changes: 1 addition & 2 deletions examples/pion-to-pion/answer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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())
})

Expand Down
3 changes: 1 addition & 2 deletions examples/pion-to-pion/offer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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())
})

Expand Down
3 changes: 1 addition & 2 deletions examples/save-to-disk/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

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

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
60 changes: 42 additions & 18 deletions peerconnection.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)

Expand All @@ -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),
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
// ------------------------------------------------------------------------
Expand Down Expand Up @@ -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)
Expand Down
5 changes: 2 additions & 3 deletions peerconnection_media_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion peerconnection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
})

Expand Down

0 comments on commit bf422e0

Please sign in to comment.