Skip to content

Commit 5db2007

Browse files
committed
imporove reconnection
1 parent 46c3d33 commit 5db2007

File tree

3 files changed

+49
-43
lines changed

3 files changed

+49
-43
lines changed

webrtc-android-framework/src/main/java/io/antmedia/webrtcandroidframework/core/WebRTCClient.java

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -277,17 +277,17 @@ public void createReconnectorRunnables() {
277277
&& pc.iceConnectionState() != PeerConnection.IceConnectionState.COMPLETED)) {
278278

279279

280-
if (peerInfo.mode.equals(Mode.PUBLISH)) {
281-
if (pc != null) {
282-
pc.close();
283-
/*
284-
This is a FIX of a reconnection bug.
285-
If dispose is used instead of close, in one of consequent reconnection attempts segmentation fault occurs.
286-
pc.dispose();
287-
*/
288-
}
280+
if (pc != null) {
281+
pc.close();
282+
/*
283+
This is a FIX of a reconnection bug.
284+
If dispose is used instead of close, in one of consequent reconnection attempts segmentation fault occurs.
285+
pc.dispose();
286+
*/
287+
}
289288

290-
config.webRTCListener.onReconnectionAttempt(peerInfo.id,peerInfo.mode);
289+
config.webRTCListener.onReconnectionAttempt(peerInfo.id,peerInfo.mode);
290+
if (peerInfo.mode.equals(Mode.PUBLISH)) {
291291

292292
Log.d(TAG, "Reconnect attempt for publish");
293293
wsHandler.stop(peerInfo.id);
@@ -316,20 +316,19 @@ public void createReconnectorRunnables() {
316316
&& pc.iceConnectionState() != PeerConnection.IceConnectionState.CONNECTED
317317
&& pc.iceConnectionState() != PeerConnection.IceConnectionState.COMPLETED)) {
318318

319-
if (peerInfo.mode.equals(Mode.PLAY)) {
320-
321-
releaseRemoteRenderers();
322319

323-
if (pc != null) {
324-
pc.close();
325-
/*
326-
This is a FIX of a reconnection bug.
327-
If dispose is used instead of close, in one of consequent reconnection attempts segmentation fault occurs.
328-
pc.dispose();
329-
*/
330-
}
320+
releaseRemoteRenderers();
321+
if (pc != null) {
322+
pc.close();
323+
/*
324+
This is a FIX of a reconnection bug.
325+
If dispose is used instead of close, in one of consequent reconnection attempts segmentation fault occurs.
326+
pc.dispose();
327+
*/
328+
}
331329

332-
config.webRTCListener.onReconnectionAttempt(peerInfo.id,peerInfo.mode);
330+
config.webRTCListener.onReconnectionAttempt(peerInfo.id,peerInfo.mode);
331+
if (peerInfo.mode.equals(Mode.PLAY)) {
333332
Log.d(TAG, "Reconnect attempt for play");
334333

335334
play(peerInfo.id,
@@ -348,6 +347,10 @@ public void createReconnectorRunnables() {
348347
if(released || streamStoppedByUser){
349348
return;
350349
}
350+
if(wsHandler.pingPongExecutor == null){
351+
wsHandler.startPingPongTimer();
352+
}
353+
351354
peerReconnectionHandler.postDelayed(peerReconnectorRunnable, PEER_RECONNECTION_RETRY_DELAY_MS);
352355

353356
for (PeerInfo peerInfo : peers.values()) {
@@ -674,7 +677,7 @@ public void onCreateSuccess(final SessionDescription desc) {
674677
PeerConnection pc = peerInfo.peerConnection;
675678
if (pc != null) {
676679
Log.d(TAG, "Set local SDP from " + desc.type);
677-
pc.setLocalDescription(this, newDesc);
680+
pc.setLocalDescription(this, newDesc);
678681
}
679682
});
680683
}
@@ -1379,11 +1382,6 @@ public void rePublishPlay() {
13791382
publishReconnectionHandler.postDelayed(publishReconnectorRunnable, PEER_RECONNECTION_DELAY_MS);
13801383
Log.d(TAG, "------------------------------------- Publish Reconnection --------------------------------------");
13811384
}
1382-
if (!isPlayConnected() && !playReconnectionInProgress) {
1383-
playReconnectionInProgress = true;
1384-
playReconnectionHandler.postDelayed(playReconnectorRunnable, PEER_RECONNECTION_DELAY_MS);
1385-
Log.d(TAG, "------------------------------------- Play Reconnection --------------------------------------");
1386-
}
13871385
} else {
13881386
Log.i(TAG, "Peer was connected before. Will try to republish/replay in " + PEER_RECONNECTION_DELAY_MS + " ms.");
13891387
publishReconnectionInProgress = true;
@@ -1490,6 +1488,12 @@ public void onConnected(String streamId) {
14901488
Log.i(TAG, "Connected for streamId:" + streamId);
14911489

14921490
if(isConference() && config.reconnectionEnabled){
1491+
if (!isPlayConnected() && !playReconnectionInProgress) {
1492+
playReconnectionInProgress = true;
1493+
playReconnectionHandler.postDelayed(playReconnectorRunnable, PEER_RECONNECTION_DELAY_MS);
1494+
Log.d(TAG, "------------------------------------- Play Reconnection --------------------------------------");
1495+
}
1496+
14931497
if(isPublishConnected()){
14941498
Log.i(TAG,"Publish reconnected");
14951499
publishReconnectionHandler.removeCallbacksAndMessages(null);

webrtc-android-framework/src/main/java/io/antmedia/webrtcandroidframework/websocket/WebSocketHandler.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -69,19 +69,19 @@ public WebSocketHandler(AntMediaSignallingEvents signallingListener, Handler han
6969
}
7070

7171
public WebSocket connectWebSocket(String wsServerUrl){
72-
synchronized (this) {
73-
if (isWsOpen) {
74-
disconnect(true);
75-
}
76-
Request request = new Request.Builder().url(wsServerUrl).build();
77-
return client.newWebSocket(request, this);
72+
if (isWsOpen) {
73+
disconnect(true);
7874
}
75+
Request request = new Request.Builder().url(wsServerUrl).build();
76+
return client.newWebSocket(request, this);
7977
}
8078
public void connect(final String wsUrl) {
81-
if(wsUrl == null || wsUrl.isBlank())
82-
return;
83-
wsServerUrl = wsUrl;
84-
ws = connectWebSocket(wsServerUrl);
79+
synchronized (this) {
80+
if (wsUrl == null || wsUrl.isBlank())
81+
return;
82+
wsServerUrl = wsUrl;
83+
ws = connectWebSocket(wsServerUrl);
84+
}
8585
}
8686

8787
public void sendTextMessage(String message) {
@@ -136,11 +136,11 @@ public void onOpen(@NonNull WebSocket webSocket, @NonNull Response response) {
136136
@Override
137137
public void onClosed(@NonNull WebSocket webSocket, int code, @NonNull String reason) {
138138
synchronized (this) {
139+
isWsOpen = false;
139140
stopPingPongTimer();
140141
handler.post(() -> {
141142
Log.d(TAG, "WebSocket connection closed.");
142143
signallingListener.onWebSocketDisconnected();
143-
isWsOpen = false;
144144
synchronized (closeEventLock) {
145145
closeEvent = true;
146146
closeEventLock.notify();

webrtc-android-framework/src/main/java/org/webrtc/audio/WebRtcAudioTrack.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -538,10 +538,12 @@ public static void setSpeakerMute(boolean mute) {
538538

539539
// Releases the native AudioTrack resources.
540540
private void releaseAudioResources() {
541-
Logging.d(TAG, "releaseAudioResources");
542-
if (audioTrack != null) {
543-
audioTrack.release();
544-
audioTrack = null;
541+
synchronized (this) {
542+
Logging.d(TAG, "releaseAudioResources");
543+
if (audioTrack != null) {
544+
audioTrack.release();
545+
audioTrack = null;
546+
}
545547
}
546548
}
547549

0 commit comments

Comments
 (0)