- improved quitting of baresip application

- using temporary fix in ua_stop_all() api function
- baresip.c cleanups
This commit is contained in:
Juha Heinanen
2018-09-20 17:04:50 +03:00
parent c4687ae6ba
commit bed8b9dfe3
3 changed files with 77 additions and 74 deletions
+38 -37
View File
@@ -161,11 +161,11 @@ static void ua_event_handler(struct ua *ua, enum ua_event ev,
if (res != JNI_OK) { if (res != JNI_OK) {
res = (*javaVM)->AttachCurrentThread(javaVM, &env, NULL); res = (*javaVM)->AttachCurrentThread(javaVM, &env, NULL);
if (JNI_OK != res) { if (JNI_OK != res) {
LOGE("Failed to AttachCurrentThread, ErrorCode = %d", res); LOGE("failed to AttachCurrentThread, ErrorCode = %d", res);
return; return;
} }
} }
jmethodID statusId = (*env)->GetMethodID(env, pctx->mainActivityClz, jmethodID methodId = (*env)->GetMethodID(env, pctx->mainActivityClz,
"uaEvent", "uaEvent",
"(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V"); "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V");
sprintf(ua_buf, "%lu", (unsigned long)ua); sprintf(ua_buf, "%lu", (unsigned long)ua);
@@ -174,7 +174,7 @@ static void ua_event_handler(struct ua *ua, enum ua_event ev,
jstring javaCall = (*env)->NewStringUTF(env, call_buf); jstring javaCall = (*env)->NewStringUTF(env, call_buf);
jstring javaEvent = (*env)->NewStringUTF(env, event); jstring javaEvent = (*env)->NewStringUTF(env, event);
LOGD("sending ua/call %s/%s event %s\n", ua_buf, call_buf, event); LOGD("sending ua/call %s/%s event %s\n", ua_buf, call_buf, event);
(*env)->CallVoidMethod(env, pctx->mainActivityObj, statusId, javaEvent, javaUA, javaCall); (*env)->CallVoidMethod(env, pctx->mainActivityObj, methodId, javaEvent, javaUA, javaCall);
(*env)->DeleteLocalRef(env, javaUA); (*env)->DeleteLocalRef(env, javaUA);
(*env)->DeleteLocalRef(env, javaCall); (*env)->DeleteLocalRef(env, javaCall);
(*env)->DeleteLocalRef(env, javaEvent); (*env)->DeleteLocalRef(env, javaEvent);
@@ -314,6 +314,7 @@ JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* vm, void* reserved) {
g_ctx.jniHelperObj = (*env)->NewGlobalRef(env, handler); g_ctx.jniHelperObj = (*env)->NewGlobalRef(env, handler);
g_ctx.mainActivityObj = NULL; g_ctx.mainActivityObj = NULL;
return JNI_VERSION_1_6; return JNI_VERSION_1_6;
} }
@@ -324,21 +325,17 @@ Java_com_tutpro_baresip_BaresipService_baresipStart(JNIEnv *env, jobject instanc
JavaVM *javaVM = pctx->javaVM; JavaVM *javaVM = pctx->javaVM;
jint res = (*javaVM)->GetEnv(javaVM, (void **) &env, JNI_VERSION_1_6); jint res = (*javaVM)->GetEnv(javaVM, (void **) &env, JNI_VERSION_1_6);
if (res != JNI_OK) { if (res != JNI_OK) {
res = (*javaVM)->AttachCurrentThread(javaVM, &env, NULL); LOGE("failed to GetEnv, ErrorCode = %d", res);
if (JNI_OK != res) { goto stopped;
LOGE("Failed to AttachCurrentThread, ErrorCode = %d", res);
return;
}
} }
jclass clz = (*env)->GetObjectClass(env, instance);
g_ctx.mainActivityClz = (*env)->NewGlobalRef(env, clz);
g_ctx.mainActivityObj = (*env)->NewGlobalRef(env, instance);
int err; int err;
const char *path = (*env)->GetStringUTFChars(env, javaPath, 0); const char *path = (*env)->GetStringUTFChars(env, javaPath, 0);
struct le *le; struct le *le;
jclass clz = (*env)->GetObjectClass(env, instance);
g_ctx.mainActivityClz = (*env)->NewGlobalRef(env, clz);
g_ctx.mainActivityObj = (*env)->NewGlobalRef(env, instance);
runLoggingThread(); runLoggingThread();
err = libre_init(); err = libre_init();
@@ -390,7 +387,7 @@ Java_com_tutpro_baresip_BaresipService_baresipStart(JNIEnv *env, jobject instanc
goto out; goto out;
} }
LOGD("Adding %u accounts", list_count(uag_list())); LOGD("adding %u accounts", list_count(uag_list()));
char ua_buf[256]; char ua_buf[256];
struct ua *ua; struct ua *ua;
for (le = list_head(uag_list()); le != NULL; le = le->next) { for (le = list_head(uag_list()); le != NULL; le = le->next) {
@@ -398,49 +395,53 @@ Java_com_tutpro_baresip_BaresipService_baresipStart(JNIEnv *env, jobject instanc
sprintf(ua_buf, "%lu", (unsigned long) ua); sprintf(ua_buf, "%lu", (unsigned long) ua);
jstring javaUA = (*env)->NewStringUTF(env, ua_buf); jstring javaUA = (*env)->NewStringUTF(env, ua_buf);
LOGD("adding UA for AoR %s/%s\n", ua_aor(ua), ua_buf); LOGD("adding UA for AoR %s/%s\n", ua_aor(ua), ua_buf);
jmethodID accountId = (*env)->GetMethodID(env, pctx->mainActivityClz, "uaAdd", jmethodID uaAddId = (*env)->GetMethodID(env, pctx->mainActivityClz, "uaAdd",
"(Ljava/lang/String;)V"); "(Ljava/lang/String;)V");
(*env)->CallVoidMethod(env, pctx->mainActivityObj, accountId, javaUA); (*env)->CallVoidMethod(env, pctx->mainActivityObj, uaAddId, javaUA);
(*env)->DeleteLocalRef(env, javaUA); (*env)->DeleteLocalRef(env, javaUA);
} }
LOGI("Running main loop\n"); LOGI("running main loop ...\n");
err = re_main(signal_handler); err = re_main(signal_handler);
out: out:
if (err) { if (err) {
LOGE("Closing upon re_main error: (%d)\n", err); LOGE("stopping UAs due to error: (%d)\n", err);
ua_stop_all(true); ua_stop_all(true);
play = mem_deref(play);
message = mem_deref(message);
//ua_close();
conf_close();
baresip_close();
mod_close();
//libre_close();
} }
play = mem_deref(play);
message = mem_deref(message);
LOGD("closing ...");
ua_close();
conf_close();
baresip_close();
uag_event_unregister(ua_event_handler);
LOGD("unloading modules ...");
mod_close();
libre_close();
// tmr_debug(); // tmr_debug();
// mem_debug(); // mem_debug();
stopped:
LOGD("tell app about baresip stop");
jmethodID stoppedId = (*env)->GetMethodID(env, pctx->mainActivityClz, "stopped", "()V");
(*env)->CallVoidMethod(env, pctx->mainActivityObj, stoppedId);
return; return;
} }
JNIEXPORT void JNICALL JNIEXPORT void JNICALL
Java_com_tutpro_baresip_BaresipService_baresipStop(JNIEnv *env, jobject thiz) { Java_com_tutpro_baresip_BaresipService_baresipStop(JNIEnv *env, jobject thiz, jboolean force) {
LOGD("ua_stop_all upon baresipStop");
LOGD("Closing upon stop"); ua_stop_all(force);
ua_stop_all(false);
play = mem_deref(play);
// ua_close();
conf_close();
baresip_close();
mod_close();
// libre_close();
// tmr_debug();
// mem_debug();
return; return;
} }
@@ -182,7 +182,6 @@ class BaresipService: Service() {
BaresipService.IS_SERVICE_RUNNING = true BaresipService.IS_SERVICE_RUNNING = true
registerReceiver(nr, IntentFilter("android.net.conn.CONNECTIVITY_CHANGE")) registerReceiver(nr, IntentFilter("android.net.conn.CONNECTIVITY_CHANGE"))
showStatusNotification() showStatusNotification()
if (!RUN_FOREGROUNG) super.onStartCommand(intent, flags, startId)
} }
"UpdateNotification" -> { "UpdateNotification" -> {
@@ -190,13 +189,12 @@ class BaresipService: Service() {
} }
"Stop" -> { "Stop" -> {
cleanStop() stop()
if (RUN_FOREGROUNG) stopForeground(true)
stopSelf()
} }
"Kill" -> { "Kill" -> {
stopSelf() RESTARTING = true
stop()
} }
} }
@@ -208,14 +206,11 @@ class BaresipService: Service() {
} }
override fun onDestroy() { override fun onDestroy() {
Log.i(LOG_TAG, "In onDestroy") Log.d(LOG_TAG, "In onDestroy")
super.onDestroy() super.onDestroy()
if (IS_SERVICE_RUNNING) { Log.i(LOG_TAG, "Restart baresip killed by Android")
Log.i(LOG_TAG, "Restart baresip killed by Android") RESTARTING = true
cleanStop() stop()
val broadcastIntent = Intent("com.tutpro.baresip.Restart")
sendBroadcast(broadcastIntent)
}
} }
private fun createNotificationChannels() { private fun createNotificationChannels() {
@@ -240,10 +235,7 @@ class BaresipService: Service() {
.setContentIntent(npi) .setContentIntent(npi)
.setOngoing(true) .setOngoing(true)
.setContent(RemoteViews(packageName, R.layout.status_notification)) .setContent(RemoteViews(packageName, R.layout.status_notification))
if (RUN_FOREGROUNG) startForeground(STATUS_NOTIFICATION_ID, snb.build())
startForeground(STATUS_NOTIFICATION_ID, snb.build())
else
nm.notify(STATUS_NOTIFICATION_ID, snb.build())
} }
@Keep @Keep
@@ -270,13 +262,8 @@ class BaresipService: Service() {
fun uaEvent(event: String, uap: String, callp: String) { fun uaEvent(event: String, uap: String, callp: String) {
Log.d(LOG_TAG, "updateStatus got event $event/$uap/$callp") Log.d(LOG_TAG, "updateStatus got event $event/$uap/$callp")
if (!IS_SERVICE_RUNNING) return if (!IS_SERVICE_RUNNING) return
if (event == "exit") { if (event == "exit")
val intent = Intent("service event")
intent.putExtra("event", event)
intent.putExtra("params", arrayListOf<String>())
LocalBroadcastManager.getInstance(this).sendBroadcast(intent)
return return
}
val ua = UserAgent.find(MainActivity.uas, uap) val ua = UserAgent.find(MainActivity.uas, uap)
if (ua == null) { if (ua == null) {
Log.w(LOG_TAG, "updateStatus did not find ua $uap") Log.w(LOG_TAG, "updateStatus did not find ua $uap")
@@ -431,6 +418,19 @@ class BaresipService: Service() {
LocalBroadcastManager.getInstance(this).sendBroadcast(intent) LocalBroadcastManager.getInstance(this).sendBroadcast(intent)
} }
@Keep
fun stopped() {
Log.d(LOG_TAG, "got event 'stopped'")
IS_SERVICE_RUNNING = false
val intent = Intent("service event")
intent.putExtra("event", "stopped")
intent.putExtra("params", arrayListOf<String>())
LocalBroadcastManager.getInstance(this).sendBroadcast(intent)
stopForeground(true)
stopSelf()
if (RESTARTING) restart()
}
private fun updateStatusNotification() { private fun updateStatusNotification() {
val contentView = RemoteViews(getPackageName(), R.layout.status_notification) val contentView = RemoteViews(getPackageName(), R.layout.status_notification)
for (i: Int in 0 .. 5) { for (i: Int in 0 .. 5) {
@@ -450,34 +450,42 @@ class BaresipService: Service() {
nm.notify(STATUS_NOTIFICATION_ID, snb.build()) nm.notify(STATUS_NOTIFICATION_ID, snb.build())
} }
private fun cleanStop() { private fun stop() {
CallsActivity.saveHistory() CallsActivity.saveHistory()
MessagesActivity.saveMessages() MessagesActivity.saveMessages()
MainActivity.uas.clear() MainActivity.uas.clear()
MainActivity.images.clear() MainActivity.images.clear()
MainActivity.history.clear() MainActivity.history.clear()
MainActivity.messages.clear() MainActivity.messages.clear()
baresipStop()
BaresipService.IS_SERVICE_RUNNING = false
unregisterReceiver(nr) unregisterReceiver(nr)
nm.cancelAll() nm.cancelAll()
if (wl.isHeld) wl.release() if (wl.isHeld) wl.release()
if (fl.isHeld) fl.release() if (fl.isHeld) fl.release()
if (IS_SERVICE_RUNNING)
baresipStop(false)
else if (RESTARTING)
restart()
}
private fun restart() {
RESTARTING = false
val broadcastIntent = Intent("com.tutpro.baresip.Restart")
sendBroadcast(broadcastIntent)
} }
external fun baresipStart(path: String) external fun baresipStart(path: String)
external fun baresipStop() external fun baresipStop(force: Boolean)
companion object { companion object {
var IS_SERVICE_RUNNING = false var IS_SERVICE_RUNNING = false
var RESTARTING = false
val STATUS_NOTIFICATION_ID = 101 val STATUS_NOTIFICATION_ID = 101
val CALL_NOTIFICATION_ID = 102 val CALL_NOTIFICATION_ID = 102
val MESSAGE_NOTIFICATION_ID = 103 val MESSAGE_NOTIFICATION_ID = 103
val DEFAULT_CHANNEL_ID = "com.tutpro.baresip.default" val DEFAULT_CHANNEL_ID = "com.tutpro.baresip.default"
val HIGH_CHANNEL_ID = "com.tutpro.baresip.high" val HIGH_CHANNEL_ID = "com.tutpro.baresip.high"
var disconnected = false var disconnected = false
val RUN_FOREGROUNG = true
} }
@@ -428,15 +428,10 @@ class MainActivity : AppCompatActivity() {
} }
private fun handleServiceEvent(event: String, params: ArrayList<String>) { private fun handleServiceEvent(event: String, params: ArrayList<String>) {
if (event == "exit") { if (event == "stopped") {
if (BaresipService.IS_SERVICE_RUNNING) { Log.d("Baresip", "Handling service event 'stopped'")
baresipService.setAction("Stop"); finishAndRemoveTask()
startService(baresipService) System.exit(0)
}
Toast.makeText(applicationContext,
"Baresip has stopped! Check your network connectivity.",
Toast.LENGTH_SHORT).show()
finish()
return return
} }
val uap = params[0] val uap = params[0]
@@ -868,7 +863,6 @@ class MainActivity : AppCompatActivity() {
baresipService.setAction("Stop"); baresipService.setAction("Stop");
startService(baresipService) startService(baresipService)
} }
finish()
return true return true
} }
else -> return super.onOptionsItemSelected(item) else -> return super.onOptionsItemSelected(item)