Skip to content

Commit 1ce9a92

Browse files
committed
Introduce function SetRemoteGatherDone that can be called at the end of gathering on remote side
1 parent b94354f commit 1ce9a92

14 files changed

+81
-11
lines changed

include/rtc/peerconnection.hpp

+1
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ class RTC_CPP_EXPORT PeerConnection final : CheshireCat<impl::PeerConnection> {
9393
void setLocalDescription(Description::Type type = Description::Type::Unspec);
9494
void setRemoteDescription(Description description);
9595
void addRemoteCandidate(Candidate candidate);
96+
void setRemoteGatherDone();
9697

9798
void setMediaHandler(shared_ptr<MediaHandler> handler);
9899
shared_ptr<MediaHandler> getMediaHandler();

include/rtc/rtc.h

+1
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ RTC_C_EXPORT int rtcSetSignalingStateChangeCallback(int pc, rtcSignalingStateCal
213213
RTC_C_EXPORT int rtcSetLocalDescription(int pc, const char *type);
214214
RTC_C_EXPORT int rtcSetRemoteDescription(int pc, const char *sdp, const char *type);
215215
RTC_C_EXPORT int rtcAddRemoteCandidate(int pc, const char *cand, const char *mid);
216+
RTC_C_EXPORT int rtcSetRemoteGatherDone(int pc);
216217

217218
RTC_C_EXPORT int rtcGetLocalDescription(int pc, char *buffer, int size);
218219
RTC_C_EXPORT int rtcGetRemoteDescription(int pc, char *buffer, int size);

src/capi.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,14 @@ int rtcAddRemoteCandidate(int pc, const char *cand, const char *mid) {
584584
});
585585
}
586586

587+
int rtcSetRemoteGatherDone(int pc) {
588+
return wrap([&] {
589+
auto peerConnection = getPeerConnection(pc);
590+
peerConnection->setRemoteGatherDone();
591+
return RTC_ERR_SUCCESS;
592+
});
593+
}
594+
587595
int rtcGetLocalDescription(int pc, char *buffer, int size) {
588596
return wrap([&] {
589597
auto peerConnection = getPeerConnection(pc);

src/impl/icetransport.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,8 @@ void IceTransport::setRemoteDescription(const Description &description) {
202202
throw std::invalid_argument("Invalid ICE settings from remote SDP");
203203
}
204204

205+
void IceTransport::setRemoteGatherDone() { juice_set_remote_gathering_done(mAgent.get()); }
206+
205207
bool IceTransport::addRemoteCandidate(const Candidate &candidate) {
206208
// Don't try to pass unresolved candidates for more safety
207209
if (!candidate.isResolved())
@@ -664,6 +666,8 @@ void IceTransport::setRemoteDescription(const Description &description) {
664666
throw std::invalid_argument("Invalid ICE settings from remote SDP");
665667
}
666668

669+
void IceTransport::setRemoteGatherDone() {}
670+
667671
bool IceTransport::addRemoteCandidate(const Candidate &candidate) {
668672
// Don't try to pass unresolved candidates to libnice for more safety
669673
if (!candidate.isResolved())

src/impl/icetransport.hpp

+1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ class IceTransport : public Transport {
5050
Description getLocalDescription(Description::Type type) const;
5151
void setRemoteDescription(const Description &description);
5252
bool addRemoteCandidate(const Candidate &candidate);
53+
void setRemoteGatherDone();
5354
void gatherLocalCandidates(string mid);
5455

5556
optional<string> getLocalAddress() const;

src/impl/peerconnection.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -1127,6 +1127,13 @@ void PeerConnection::processRemoteCandidate(Candidate candidate) {
11271127
}
11281128
}
11291129

1130+
void PeerConnection::setRemoteGatherDone() {
1131+
auto iceTransport = std::atomic_load(&mIceTransport);
1132+
if (!iceTransport)
1133+
throw std::logic_error("setting remote gather done without ICE transport");
1134+
iceTransport->setRemoteGatherDone();
1135+
}
1136+
11301137
string PeerConnection::localBundleMid() const {
11311138
std::lock_guard lock(mLocalDescriptionMutex);
11321139
return mLocalDescription ? mLocalDescription->bundleMid() : "0";

src/impl/peerconnection.hpp

+1
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ struct PeerConnection : std::enable_shared_from_this<PeerConnection> {
7878
void processLocalCandidate(Candidate candidate);
7979
void processRemoteDescription(Description description);
8080
void processRemoteCandidate(Candidate candidate);
81+
void setRemoteGatherDone();
8182
string localBundleMid() const;
8283

8384
void setMediaHandler(shared_ptr<MediaHandler> handler);

src/peerconnection.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,12 @@ void PeerConnection::addRemoteCandidate(Candidate candidate) {
249249
impl()->processRemoteCandidate(std::move(candidate));
250250
}
251251

252+
void PeerConnection::setRemoteGatherDone() {
253+
std::unique_lock signalingLock(impl()->signalingMutex);
254+
PLOG_VERBOSE << "Remote gather is done";
255+
impl()->setRemoteGatherDone();
256+
}
257+
252258
void PeerConnection::setMediaHandler(shared_ptr<MediaHandler> handler) {
253259
impl()->setMediaHandler(std::move(handler));
254260
};

test/benchmark.cpp

+8-2
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,11 @@ size_t benchmark(milliseconds duration) {
5151
});
5252

5353
pc1.onStateChange([](PeerConnection::State state) { cout << "State 1: " << state << endl; });
54-
pc1.onGatheringStateChange([](PeerConnection::GatheringState state) {
54+
pc1.onGatheringStateChange([&pc2](PeerConnection::GatheringState state) {
5555
cout << "Gathering state 1: " << state << endl;
56+
if (state == PeerConnection::GatheringState::Complete) {
57+
pc2.setRemoteGatherDone();
58+
}
5659
});
5760

5861
pc2.onLocalDescription([&pc1](Description sdp) {
@@ -66,8 +69,11 @@ size_t benchmark(milliseconds duration) {
6669
});
6770

6871
pc2.onStateChange([](PeerConnection::State state) { cout << "State 2: " << state << endl; });
69-
pc2.onGatheringStateChange([](PeerConnection::GatheringState state) {
72+
pc2.onGatheringStateChange([&pc1](PeerConnection::GatheringState state) {
7073
cout << "Gathering state 2: " << state << endl;
74+
if (state == PeerConnection::GatheringState::Complete) {
75+
pc1.setRemoteGatherDone();
76+
}
7177
});
7278

7379
const size_t messageSize = 65535;

test/capi_connectivity.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ static void RTC_API gatheringStateCallback(int pc, rtcGatheringState state, void
6464
Peer *peer = (Peer *)ptr;
6565
peer->gatheringState = state;
6666
printf("Gathering state %d: %d\n", peer == peer1 ? 1 : 2, (int)state);
67+
if (state == RTC_GATHERING_COMPLETE) {
68+
Peer *other = peer == peer1 ? peer2 : peer1;
69+
rtcSetRemoteGatherDone(other->pc);
70+
}
6771
}
6872

6973
static void RTC_API signalingStateCallback(int pc, rtcSignalingState state, void *ptr) {

test/capi_track.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ static void RTC_API gatheringStateCallback(int pc, rtcGatheringState state, void
5858
Peer *peer = (Peer *)ptr;
5959
peer->gatheringState = state;
6060
printf("Gathering state %d: %d\n", peer == peer1 ? 1 : 2, (int)state);
61+
if (state == RTC_GATHERING_COMPLETE) {
62+
Peer *other = peer == peer1 ? peer2 : peer1;
63+
rtcSetRemoteGatherDone(other->pc);
64+
}
6165
}
6266

6367
static void RTC_API openCallback(int id, void *ptr) {

test/connectivity.cpp

+8-2
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,11 @@ void test_connectivity(bool signal_wrong_fingerprint) {
7171
cout << "ICE state 1: " << state << endl;
7272
});
7373

74-
pc1.onGatheringStateChange([](PeerConnection::GatheringState state) {
74+
pc1.onGatheringStateChange([&pc2](PeerConnection::GatheringState state) {
7575
cout << "Gathering state 1: " << state << endl;
76+
if (state == PeerConnection::GatheringState::Complete) {
77+
pc2.setRemoteGatherDone();
78+
}
7679
});
7780

7881
pc1.onSignalingStateChange([](PeerConnection::SignalingState state) {
@@ -95,8 +98,11 @@ void test_connectivity(bool signal_wrong_fingerprint) {
9598
cout << "ICE state 2: " << state << endl;
9699
});
97100

98-
pc2.onGatheringStateChange([](PeerConnection::GatheringState state) {
101+
pc2.onGatheringStateChange([&pc1](PeerConnection::GatheringState state) {
99102
cout << "Gathering state 2: " << state << endl;
103+
if (state == PeerConnection::GatheringState::Complete) {
104+
pc1.setRemoteGatherDone();
105+
}
100106
});
101107

102108
pc2.onSignalingStateChange([](PeerConnection::SignalingState state) {

test/track.cpp

+8-2
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,11 @@ void test_track() {
4949

5050
pc1.onStateChange([](PeerConnection::State state) { cout << "State 1: " << state << endl; });
5151

52-
pc1.onGatheringStateChange([](PeerConnection::GatheringState state) {
52+
pc1.onGatheringStateChange([&pc2](PeerConnection::GatheringState state) {
5353
cout << "Gathering state 1: " << state << endl;
54+
if (state == PeerConnection::GatheringState::Complete) {
55+
pc2.setRemoteGatherDone();
56+
}
5457
});
5558

5659
pc2.onLocalDescription([&pc1](Description sdp) {
@@ -65,8 +68,11 @@ void test_track() {
6568

6669
pc2.onStateChange([](PeerConnection::State state) { cout << "State 2: " << state << endl; });
6770

68-
pc2.onGatheringStateChange([](PeerConnection::GatheringState state) {
71+
pc2.onGatheringStateChange([&pc1](PeerConnection::GatheringState state) {
6972
cout << "Gathering state 2: " << state << endl;
73+
if (state == PeerConnection::GatheringState::Complete) {
74+
pc1.setRemoteGatherDone();
75+
}
7076
});
7177

7278
shared_ptr<Track> t2;

test/turn_connectivity.cpp

+20-5
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,22 @@ void test_turn_connectivity() {
4646
pc1.onGatheringStateChange([&pc1, &pc2](PeerConnection::GatheringState state) {
4747
cout << "Gathering state 1: " << state << endl;
4848
if (state == PeerConnection::GatheringState::Complete) {
49-
auto sdp = pc1.localDescription().value();
49+
pc2.setRemoteGatherDone();
50+
}
51+
});
52+
53+
pc1.onLocalDescription([&pc2](Description sdp) {
5054
cout << "Description 1: " << sdp << endl;
5155
pc2.setRemoteDescription(string(sdp));
56+
});
57+
58+
pc1.onLocalCandidate([&pc2](Candidate candidate) {
59+
if (candidate.type() == rtc::Candidate::Type::Host) {
60+
cout << "Candidate 1 ignored: " << candidate << endl;
61+
return;
5262
}
63+
cout << "Candidate 1: " << candidate << endl;
64+
pc2.addRemoteCandidate(string(candidate));
5365
});
5466

5567
pc1.onSignalingStateChange([](PeerConnection::SignalingState state) {
@@ -62,10 +74,10 @@ void test_turn_connectivity() {
6274
});
6375

6476
pc2.onLocalCandidate([&pc1](Candidate candidate) {
65-
// Filter server reflexive candidates
66-
if (candidate.type() != rtc::Candidate::Type::ServerReflexive)
77+
if (candidate.type() == rtc::Candidate::Type::Host) {
78+
cout << "Candidate 2 ignored: " << candidate << endl;
6779
return;
68-
80+
}
6981
cout << "Candidate 2: " << candidate << endl;
7082
pc1.addRemoteCandidate(string(candidate));
7183
});
@@ -75,8 +87,11 @@ void test_turn_connectivity() {
7587
pc2.onIceStateChange(
7688
[](PeerConnection::IceState state) { cout << "ICE state 2: " << state << endl; });
7789

78-
pc2.onGatheringStateChange([](PeerConnection::GatheringState state) {
90+
pc2.onGatheringStateChange([&pc1](PeerConnection::GatheringState state) {
7991
cout << "Gathering state 2: " << state << endl;
92+
if (state == PeerConnection::GatheringState::Complete) {
93+
pc1.setRemoteGatherDone();
94+
}
8095
});
8196

8297
pc2.onSignalingStateChange([](PeerConnection::SignalingState state) {

0 commit comments

Comments
 (0)