88import lombok .Getter ;
99import lombok .extern .slf4j .Slf4j ;
1010import org .ice4j .Transport ;
11+ import org .ice4j .TransportAddress ;
1112import org .ice4j .ice .*;
1213import org .ice4j .ice .harvest .StunCandidateHarvester ;
1314import org .ice4j .ice .harvest .TurnCandidateHarvester ;
1415import org .ice4j .security .LongTermCredential ;
1516
16- import javax .swing .text .html .Option ;
1717import java .io .IOException ;
1818import java .net .DatagramPacket ;
19- import java .util .*;
19+ import java .util .ArrayList ;
20+ import java .util .Comparator ;
21+ import java .util .List ;
22+ import java .util .Optional ;
2023import java .util .concurrent .CancellationException ;
2124import java .util .concurrent .CompletableFuture ;
2225import java .util .concurrent .CompletionException ;
@@ -136,11 +139,10 @@ private void gatherCandidates() {
136139
137140 int previousConnectivityAttempts = getConnectivityAttempsInThePast (FORCE_SRFLX_RELAY_INTERVAL );
138141 CandidatesMessage localCandidatesMessage = CandidateUtil .packCandidates (IceAdapter .id , peer .getRemoteId (), agent , component , previousConnectivityAttempts < FORCE_SRFLX_COUNT && IceAdapter .ALLOW_HOST , previousConnectivityAttempts < FORCE_RELAY_COUNT && IceAdapter .ALLOW_REFLEXIVE , IceAdapter .ALLOW_RELAY );
139- log .debug (getLogPrefix () + "Sending own candidates to {}" , peer .getRemoteId ());
142+ log .debug (getLogPrefix () + "Sending own candidates to {}, offered candidates: {} " , peer .getRemoteId (), localCandidatesMessage . getCandidates (). stream (). map ( it -> it . getType (). toString () + "(" + it . getProtocol () + ")" ). collect ( Collectors . joining ( ", " ) ));
140143 setState (AWAITING_CANDIDATES );
141144 RPCService .onIceMsg (localCandidatesMessage );
142145
143- //TODO: is this a good fix for awaiting candidates loop????
144146 //Make sure to abort the connection process and reinitiate when we haven't received an answer to our offer in 6 seconds, candidate packet was probably lost
145147 final int currentacei = ++awaitingCandidatesEventId ;
146148 Executor .executeDelayed (6000 , () -> {
@@ -172,6 +174,7 @@ private List<IceServer> getViableIceServers() {
172174 .filter (IceServer ::hasAcceptableLatency )
173175 .collect (Collectors .toList ());
174176 if (!viableIceServers .isEmpty ()) {
177+ log .info ("Using official ice servers: {}" , viableIceServers .stream ().map (it -> "[" + it .getTurnAddresses ().stream ().map (TransportAddress ::toString ).collect (Collectors .joining (", " )) + "]" ).collect (Collectors .joining (", " )));
175178 return viableIceServers ;
176179 }
177180
@@ -180,6 +183,7 @@ private List<IceServer> getViableIceServers() {
180183 .filter (IceServer ::hasAcceptableLatency )
181184 .collect (Collectors .toList ());
182185 if (!viableIceServers .isEmpty ()) {
186+ log .info ("Using all viable ice servers: {}" , viableIceServers .stream ().map (it -> "[" + it .getTurnAddresses ().stream ().map (TransportAddress ::toString ).collect (Collectors .joining (", " )) + "]" ).collect (Collectors .joining (", " )));
183187 return viableIceServers ;
184188 }
185189
@@ -188,12 +192,15 @@ private List<IceServer> getViableIceServers() {
188192 .filter (server -> server .getRoundTripTime ().join ().isPresent ())
189193 .min (Comparator .comparing (server -> server .getRoundTripTime ().join ().getAsDouble ()));
190194 if (closestIceServer .isPresent ()) {
195+ log .info ("Using closest ice server: {}" , closestIceServer .get ().getTurnAddresses ().stream ().map (TransportAddress ::toString ).collect (Collectors .joining (", " )));
191196 viableIceServers .add (closestIceServer .get ());
192197 }
193198 if (!viableIceServers .isEmpty ()) {
199+ log .info ("Using all reachable ice servers: {}" , viableIceServers .stream ().map (it -> "[" + it .getTurnAddresses ().stream ().map (TransportAddress ::toString ).collect (Collectors .joining (", " )) + "]" ).collect (Collectors .joining (", " )));
194200 return viableIceServers ;
195201 }
196202
203+ log .info ("Using all ice servers: {}" , viableIceServers .stream ().map (it -> "[" + it .getTurnAddresses ().stream ().map (TransportAddress ::toString ).collect (Collectors .joining (", " )) + "]" ).collect (Collectors .joining (", " )));
197204 return allIceServers ;
198205 }
199206
@@ -209,7 +216,7 @@ public synchronized void onIceMessageReceived(CandidatesMessage remoteCandidates
209216
210217 //Start ICE async as it's blocking and this is the RPC thread
211218 new Thread (() -> {
212- log .debug (getLogPrefix () + "Got IceMsg for peer" );
219+ log .debug (getLogPrefix () + "Got IceMsg for peer, offered candidates: {}" , remoteCandidatesMessage . getCandidates (). stream (). map ( it -> it . getType (). toString () + "(" + it . getProtocol () + ")" ). collect ( Collectors . joining ( ", " )) );
213220
214221 if (peer .isLocalOffer ()) {
215222 if (iceState != AWAITING_CANDIDATES ) {
@@ -269,7 +276,7 @@ private void startIce() {
269276 }
270277 }
271278
272- log .debug (getLogPrefix () + "ICE terminated" );
279+ log .debug (getLogPrefix () + "ICE terminated, connected, selected candidate pair: " + component . getSelectedPair (). getLocalCandidate (). getType (). toString () + " <-> " + component . getSelectedPair (). getRemoteCandidate (). getType (). toString () );
273280
274281 //We are connected
275282 connected = true ;
0 commit comments