Skip to content

Commit

Permalink
expose Go IceConnectionState enums which correspond to native enums u…
Browse files Browse the repository at this point in the history
…sing CGO,

and also implement IceConnection() on PeerConnection
  • Loading branch information
keroserene committed Jan 9, 2016
1 parent 7b2ac94 commit 6970c4c
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 9 deletions.
20 changes: 20 additions & 0 deletions ctestenums.cc
Expand Up @@ -3,6 +3,11 @@

using namespace webrtc;

/*
In order to match native enums with Go enum values, it is necessary to
expose the values to CGO identifiers which Go can access.
*/

const int CGO_IceTransportPolicyNone =
PeerConnectionInterface::IceTransportsType::kNone;
const int CGO_IceTransportPolicyRelay =
Expand Down Expand Up @@ -38,6 +43,21 @@ const int CGO_SignalingStateHaveRemotePrAnswer =
const int CGO_SignalingStateClosed =
PeerConnectionInterface::SignalingState::kClosed;

const int CGO_IceConnectionStateNew =
PeerConnectionInterface::IceConnectionState::kIceConnectionNew;
const int CGO_IceConnectionStateChecking =
PeerConnectionInterface::IceConnectionState::kIceConnectionChecking;
const int CGO_IceConnectionStateConnected =
PeerConnectionInterface::IceConnectionState::kIceConnectionConnected;
const int CGO_IceConnectionStateCompleted =
PeerConnectionInterface::IceConnectionState::kIceConnectionCompleted;
const int CGO_IceConnectionStateFailed =
PeerConnectionInterface::IceConnectionState::kIceConnectionFailed;
const int CGO_IceConnectionStateDisconnected =
PeerConnectionInterface::IceConnectionState::kIceConnectionDisconnected;
const int CGO_IceConnectionStateClosed =
PeerConnectionInterface::IceConnectionState::kIceConnectionClosed;

const int CGO_IceGatheringStateNew =
PeerConnectionInterface::IceGatheringState::kIceGatheringNew;
const int CGO_IceGatheringStateGathering =
Expand Down
8 changes: 8 additions & 0 deletions ctestenums.h
Expand Up @@ -29,6 +29,14 @@ extern "C" {
extern const int CGO_SignalingStateHaveRemotePrAnswer;
extern const int CGO_SignalingStateClosed;

extern const int CGO_IceConnectionStateNew;
extern const int CGO_IceConnectionStateChecking;
extern const int CGO_IceConnectionStateConnected;
extern const int CGO_IceConnectionStateCompleted;
extern const int CGO_IceConnectionStateFailed;
extern const int CGO_IceConnectionStateDisconnected;
extern const int CGO_IceConnectionStateClosed;

extern const int CGO_IceGatheringStateNew;
extern const int CGO_IceGatheringStateGathering;
extern const int CGO_IceGatheringStateComplete;
Expand Down
39 changes: 30 additions & 9 deletions peerconnection.go
Expand Up @@ -53,6 +53,7 @@ func init() {
type (
PeerConnectionState int
IceGatheringState int
IceConnectionState int
)

const (
Expand All @@ -66,6 +67,20 @@ const (
var PeerConnectionStateString = []string{
"New", "Connecting", "Connected", "Disconnected", "Failed"}

const (
IceConnectionStateNew IceConnectionState = iota
IceConnectionStateChecking
IceConnectionStateConnected
IceConnectionStateCompleted
IceConnectionStateFailed
IceConnectionStateDisconnected
IceConnectionStateClosed
)

var IceConnectionStateString = []string{
"New", "Checking", "Connected", "Completed",
"Failed", "Disconnected", "Closed"}

const (
IceGatheringStateNew IceGatheringState = iota
IceGatheringStateGathering
Expand All @@ -83,15 +98,8 @@ negotiation to setting up Data Channels.
See: https://w3c.github.io/webrtc-pc/#idl-def-RTCPeerConnection
*/
type PeerConnection struct {
localDescription *SessionDescription
// currentLocalDescription
// pendingLocalDescription

remoteDescription *SessionDescription
// currentRemoteDescription
// pendingRemoteDescription

// iceConnectionState IceConnectionState
localDescription *SessionDescription
remoteDescription *SessionDescription
canTrickleIceCandidates bool

// Event handlers
Expand Down Expand Up @@ -243,6 +251,11 @@ func (pc *PeerConnection) IceGatheringState() IceGatheringState {
return (IceGatheringState)(C.CGO_IceGatheringState(pc.cgoPeer))
}

// readonly iceconnectionState
func (pc *PeerConnection) IceConnectionState() IceConnectionState {
return (IceConnectionState)(C.CGO_IceConnectionState(pc.cgoPeer))
}

func (pc *PeerConnection) AddIceCandidate(ic IceCandidate) error {
sdpMid := C.CString(ic.SdpMid)
defer C.free(unsafe.Pointer(sdpMid))
Expand Down Expand Up @@ -392,6 +405,14 @@ func cgoOnDataChannel(p unsafe.Pointer, cDC C.CGO_Channel) {

// Test helpers
//
var _cgoIceConnectionStateNew = int(C.CGO_IceConnectionStateNew)
var _cgoIceConnectionStateChecking = int(C.CGO_IceConnectionStateChecking)
var _cgoIceConnectionStateConnected = int(C.CGO_IceConnectionStateConnected)
var _cgoIceConnectionStateCompleted = int(C.CGO_IceConnectionStateCompleted)
var _cgoIceConnectionStateFailed = int(C.CGO_IceConnectionStateFailed)
var _cgoIceConnectionStateDisconnected = int(C.CGO_IceConnectionStateDisconnected)
var _cgoIceConnectionStateClosed = int(C.CGO_IceConnectionStateClosed)

var _cgoIceGatheringStateNew = int(C.CGO_IceGatheringStateNew)
var _cgoIceGatheringStateGathering = int(C.CGO_IceGatheringStateGathering)
var _cgoIceGatheringStateComplete = int(C.CGO_IceGatheringStateComplete)
Expand Down
17 changes: 17 additions & 0 deletions webrtc_test.go
Expand Up @@ -17,6 +17,22 @@ C++ webrtc::PeerConnectionInterface values`, t, func() {
})
}

func TestIceConnectionStateEnums(t *testing.T) {
Convey(`Enum: IceConnectionState values should match
C++ webrtc::PeerConnectionInterface values`, t, func() {
So(IceConnectionStateNew, ShouldEqual, _cgoIceConnectionStateNew)
So(IceConnectionStateChecking, ShouldEqual, _cgoIceConnectionStateChecking)
So(IceConnectionStateConnected, ShouldEqual,
_cgoIceConnectionStateConnected)
So(IceConnectionStateCompleted, ShouldEqual,
_cgoIceConnectionStateCompleted)
So(IceConnectionStateFailed, ShouldEqual, _cgoIceConnectionStateFailed)
So(IceConnectionStateDisconnected, ShouldEqual,
_cgoIceConnectionStateDisconnected)
So(IceConnectionStateClosed, ShouldEqual, _cgoIceConnectionStateClosed)
})
}

func TestPeerConnection(t *testing.T) {
SetLoggingVerbosity(0)

Expand All @@ -37,6 +53,7 @@ func TestPeerConnection(t *testing.T) {
So(err, ShouldBeNil)
So(pc.ConnectionState(), ShouldEqual, PeerConnectionStateNew)
So(pc.IceGatheringState(), ShouldEqual, IceGatheringStateNew)
So(pc.IceConnectionState(), ShouldEqual, IceConnectionStateNew)

Convey("Set and Get Configuration", func() {
config := NewConfiguration(
Expand Down

0 comments on commit 6970c4c

Please sign in to comment.