Skip to content

Commit

Permalink
Merge pull request #42 from paullouisageneau/ice-state
Browse files Browse the repository at this point in the history
Add ICE state to API
  • Loading branch information
paullouisageneau authored Aug 12, 2023
2 parents bb1da1b + 18cece4 commit 17bc47a
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 3 deletions.
22 changes: 19 additions & 3 deletions wasm/include/rtc/peerconnection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,16 @@ class PeerConnection final {
Closed = 5
};

enum class IceState : int {
New = 0,
Checking = 1,
Connected = 2,
Completed = 3,
Failed = 4,
Disconnected = 5,
Closed = 6
};

enum class GatheringState : int { New = 0, InProgress = 1, Complete = 2 };

enum class SignalingState : int {
Expand All @@ -66,6 +76,7 @@ class PeerConnection final {
~PeerConnection();

State state() const;
IceState iceState() const;
GatheringState gatheringState() const;
SignalingState signalingState() const;
optional<Description> localDescription() const;
Expand All @@ -80,6 +91,7 @@ class PeerConnection final {
void onLocalDescription(std::function<void(const Description &description)> callback);
void onLocalCandidate(std::function<void(const Candidate &candidate)> callback);
void onStateChange(std::function<void(State state)> callback);
void onIceStateChange(std::function<void(IceState state)> callback);
void onGatheringStateChange(std::function<void(GatheringState state)> callback);
void onSignalingStateChange(std::function<void(SignalingState state)> callback);

Expand All @@ -88,26 +100,30 @@ class PeerConnection final {
void triggerLocalDescription(const Description &description);
void triggerLocalCandidate(const Candidate &candidate);
void triggerStateChange(State state);
void triggerIceStateChange(IceState state);
void triggerGatheringStateChange(GatheringState state);
void triggerSignalingStateChange(SignalingState state);

std::function<void(shared_ptr<DataChannel>)> mDataChannelCallback;
std::function<void(const Description &description)> mLocalDescriptionCallback;
std::function<void(const Candidate &candidate)> mLocalCandidateCallback;
std::function<void(State candidate)> mStateChangeCallback;
std::function<void(GatheringState candidate)> mGatheringStateChangeCallback;
std::function<void(SignalingState candidate)> mSignalingStateChangeCallback;
std::function<void(State state)> mStateChangeCallback;
std::function<void(IceState state)> mIceStateChangeCallback;
std::function<void(GatheringState state)> mGatheringStateChangeCallback;
std::function<void(SignalingState state)> mSignalingStateChangeCallback;

private:
int mId;
State mState = State::New;
IceState mIceState = IceState::New;
GatheringState mGatheringState = GatheringState::New;
SignalingState mSignalingState = SignalingState::Stable;

static void DataChannelCallback(int dc, void *ptr);
static void DescriptionCallback(const char *sdp, const char *type, void *ptr);
static void CandidateCallback(const char *candidate, const char *mid, void *ptr);
static void StateChangeCallback(int state, void *ptr);
static void IceStateChangeCallback(int state, void *ptr);
static void GatheringStateChangeCallback(int state, void *ptr);
static void SignalingStateChangeCallback(int state, void *ptr);
};
Expand Down
28 changes: 28 additions & 0 deletions wasm/js/webrtc.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@
peerConnection.onconnectionstatechange = function() {
WEBRTC.handleConnectionStateChange(peerConnection, peerConnection.connectionState)
};
peerConnection.oniceconnectionstatechange = function() {
WEBRTC.handleIceStateChange(peerConnection, peerConnection.iceConnectionState)
};
peerConnection.onicegatheringstatechange = function() {
WEBRTC.handleGatheringStateChange(peerConnection, peerConnection.iceGatheringState)
};
Expand Down Expand Up @@ -115,6 +118,25 @@
}
},

handleIceStateChange: function(peerConnection, iceConnectionState) {
if(peerConnection.rtcUserDeleted) return;
if(!peerConnection.rtcIceStateChangeCallback) return;
var map = {
'new': 0,
'checking': 1,
'connected': 2,
'completed': 3,
'failed': 4,
'disconnected': 5,
'closed': 6,
};
if(iceConnectionState in map) {
var iceStateChangeCallback = peerConnection.rtcIceStateChangeCallback;
var userPointer = peerConnection.rtcUserPointer || 0;
{{{ makeDynCall('vii', 'iceStateChangeCallback') }}} (map[iceConnectionState], userPointer);
}
},

handleGatheringStateChange: function(peerConnection, iceGatheringState) {
if(peerConnection.rtcUserDeleted) return;
if(!peerConnection.rtcGatheringStateChangeCallback) return;
Expand Down Expand Up @@ -278,6 +300,12 @@
peerConnection.rtcStateChangeCallback = stateChangeCallback;
},

rtcSetIceStateChangeCallback: function(pc, iceStateChangeCallback) {
if(!pc) return;
var peerConnection = WEBRTC.peerConnectionsMap[pc];
peerConnection.rtcIceStateChangeCallback = iceStateChangeCallback;
},

rtcSetGatheringStateChangeCallback: function(pc, gatheringStateChangeCallback) {
if(!pc) return;
var peerConnection = WEBRTC.peerConnectionsMap[pc];
Expand Down
20 changes: 20 additions & 0 deletions wasm/src/peerconnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ extern void rtcSetLocalDescriptionCallback(int pc,
extern void rtcSetLocalCandidateCallback(int pc, void (*candidateCallback)(const char *,
const char *, void *));
extern void rtcSetStateChangeCallback(int pc, void (*stateChangeCallback)(int, void *));
extern void rtcSetIceStateChangeCallback(int pc, void (*iceStateChangeCallback)(int, void *));
extern void rtcSetGatheringStateChangeCallback(int pc,
void (*gatheringStateChangeCallback)(int, void *));
extern void rtcSetSignalingStateChangeCallback(int pc,
Expand Down Expand Up @@ -83,6 +84,12 @@ void PeerConnection::StateChangeCallback(int state, void *ptr) {
p->triggerStateChange(static_cast<State>(state));
}

void PeerConnection::IceStateChangeCallback(int state, void *ptr) {
PeerConnection *p = static_cast<PeerConnection *>(ptr);
if (p)
p->triggerIceStateChange(static_cast<IceState>(state));
}

void PeerConnection::GatheringStateChangeCallback(int state, void *ptr) {
PeerConnection *p = static_cast<PeerConnection *>(ptr);
if (p)
Expand Down Expand Up @@ -142,6 +149,7 @@ PeerConnection::PeerConnection(const Configuration &config) {
rtcSetLocalDescriptionCallback(mId, DescriptionCallback);
rtcSetLocalCandidateCallback(mId, CandidateCallback);
rtcSetStateChangeCallback(mId, StateChangeCallback);
rtcSetIceStateChangeCallback(mId, IceStateChangeCallback);
rtcSetGatheringStateChangeCallback(mId, GatheringStateChangeCallback);
rtcSetSignalingStateChangeCallback(mId, SignalingStateChangeCallback);
}
Expand All @@ -150,6 +158,8 @@ PeerConnection::~PeerConnection() { rtcDeletePeerConnection(mId); }

PeerConnection::State PeerConnection::state() const { return mState; }

PeerConnection::IceState PeerConnection::iceState() const { return mIceState; }

PeerConnection::GatheringState PeerConnection::gatheringState() const { return mGatheringState; }

PeerConnection::SignalingState PeerConnection::signalingState() const { return mSignalingState; }
Expand Down Expand Up @@ -219,6 +229,10 @@ void PeerConnection::onStateChange(function<void(State state)> callback) {
mStateChangeCallback = callback;
}

void PeerConnection::onIceStateChange(function<void(IceState state)> callback) {
mIceStateChangeCallback = callback;
}

void PeerConnection::onGatheringStateChange(function<void(GatheringState state)> callback) {
mGatheringStateChangeCallback = callback;
}
Expand Down Expand Up @@ -248,6 +262,12 @@ void PeerConnection::triggerStateChange(State state) {
mStateChangeCallback(state);
}

void PeerConnection::triggerIceStateChange(IceState state) {
mIceState = state;
if (mIceStateChangeCallback)
mIceStateChangeCallback(state);
}

void PeerConnection::triggerGatheringStateChange(GatheringState state) {
mGatheringState = state;
if (mGatheringStateChangeCallback)
Expand Down

0 comments on commit 17bc47a

Please sign in to comment.