Skip to content

Commit

Permalink
Merge branch 'feature/resume-audio-after-network-switch-VIALA-786' in…
Browse files Browse the repository at this point in the history
…to develop
  • Loading branch information
jeremy.norman committed Mar 13, 2018
2 parents 063636e + 12118c6 commit 1588249
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.voipgrid.vialer.logging.sip;

import android.content.Intent;

import com.voipgrid.vialer.VialerApplication;

/**
* Performs various actions based on the result of the pjsip logs.
*/
public class SipLogHandler {

private static final String NETWORK_UNAVAILABLE = "Error sending RTP: Network is unreachable";

public static final String NETWORK_UNAVAILABLE_BROADCAST = "com.voipgrid.vialer.logging.sip.NETWORK_UNAVAILABLE_BROADCAST";

/**
* Perform various tasks based on pjsip logs.
*
* @param log
*/
public void handle(String log) {
if(log == null) return;

if(log.contains(NETWORK_UNAVAILABLE)) {
performNetworkSwitch();
}
}

/**
* When pjsip is reporting that the network is unreachable, we will send out a broadcast so that
* the IP can be updated and RTP can be resumed.
*
*/
private void performNetworkSwitch() {
VialerApplication.get().sendBroadcast(new Intent(SipLogHandler.NETWORK_UNAVAILABLE_BROADCAST));
}

}
21 changes: 12 additions & 9 deletions app/src/main/java/com/voipgrid/vialer/sip/SipConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.voipgrid.vialer.api.models.PhoneAccount;
import com.voipgrid.vialer.fcm.FcmMessagingService;
import com.voipgrid.vialer.logging.RemoteLogger;
import com.voipgrid.vialer.logging.sip.SipLogHandler;
import com.voipgrid.vialer.util.ConnectivityHelper;

import org.pjsip.pjsua2.Account;
Expand Down Expand Up @@ -112,29 +113,28 @@ public void initLibrary() throws LibraryInitFailedException {
startNetworkingListener();
}

private static final int NETWORK_SWITCH_DELAY_MS = 500;

private BroadcastReceiver mNetworkStateReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
mRemoteLogger.d("Received a network change.");
if(isChangingNetwork) return;

mRemoteLogger.d("Received a network change: " + intent.getAction());

if(isInitialStickyBroadcast()) {
mRemoteLogger.i("Ignoring network change as broadcast is old (sticky).");
return;
}

if (isChangingNetwork) {
mRemoteLogger.i("There is already a network change in progress.");
return;
}
isChangingNetwork = true;

final Handler handler = new Handler();
handler.postDelayed(() -> {
mRemoteLogger.d("Wait 1 sec before doing the network switch");
mRemoteLogger.d("Wait " + NETWORK_SWITCH_DELAY_MS + "ms before doing the network switch");
doIpSwitch();
isChangingNetwork = false;
}, 1000);

}, NETWORK_SWITCH_DELAY_MS);
}
};

Expand Down Expand Up @@ -169,9 +169,12 @@ private void doIpSwitch() {
}

private void startNetworkingListener() {
IntentFilter filter = new IntentFilter();
filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
filter.addAction(SipLogHandler.NETWORK_UNAVAILABLE_BROADCAST);
mSipService.registerReceiver(
mNetworkStateReceiver,
new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION)
filter
);
}

Expand Down
5 changes: 5 additions & 0 deletions app/src/main/java/com/voipgrid/vialer/sip/SipLogWriter.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package com.voipgrid.vialer.sip;

import com.voipgrid.vialer.logging.RemoteLogger;
import com.voipgrid.vialer.logging.sip.SipLogHandler;

import org.pjsip.pjsua2.LogEntry;
import org.pjsip.pjsua2.LogWriter;

public class SipLogWriter extends LogWriter {
private RemoteLogger mRemoteLogger;

private SipLogHandler mSipLogHandler = new SipLogHandler();

void enabledRemoteLogging(RemoteLogger logger) {
mRemoteLogger = logger;
}
Expand All @@ -22,6 +25,8 @@ public void write(LogEntry entry) {
Integer pjsipLogLevel = entry.getLevel();
String logString = entry.getMsg().substring(13);

mSipLogHandler.handle(logString);

if (mRemoteLogger == null) {
return;
}
Expand Down

0 comments on commit 1588249

Please sign in to comment.