- 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

View File

@ -161,11 +161,11 @@ static void ua_event_handler(struct ua *ua, enum ua_event ev,
if (res != JNI_OK) {
res = (*javaVM)->AttachCurrentThread(javaVM, &env, NULL);
if (JNI_OK != res) {
LOGE("Failed to AttachCurrentThread, ErrorCode = %d", res);
LOGE("failed to AttachCurrentThread, ErrorCode = %d", res);
return;
}
}
jmethodID statusId = (*env)->GetMethodID(env, pctx->mainActivityClz,
jmethodID methodId = (*env)->GetMethodID(env, pctx->mainActivityClz,
"uaEvent",
"(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V");
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 javaEvent = (*env)->NewStringUTF(env, 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, javaCall);
(*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.mainActivityObj = NULL;
return JNI_VERSION_1_6;
}
@ -324,21 +325,17 @@ Java_com_tutpro_baresip_BaresipService_baresipStart(JNIEnv *env, jobject instanc
JavaVM *javaVM = pctx->javaVM;
jint res = (*javaVM)->GetEnv(javaVM, (void **) &env, JNI_VERSION_1_6);
if (res != JNI_OK) {
res = (*javaVM)->AttachCurrentThread(javaVM, &env, NULL);
if (JNI_OK != res) {
LOGE("Failed to AttachCurrentThread, ErrorCode = %d", res);
return;
}
LOGE("failed to GetEnv, ErrorCode = %d", res);
goto stopped;
}
jclass clz = (*env)->GetObjectClass(env, instance);
g_ctx.mainActivityClz = (*env)->NewGlobalRef(env, clz);
g_ctx.mainActivityObj = (*env)->NewGlobalRef(env, instance);
int err;
const char *path = (*env)->GetStringUTFChars(env, javaPath, 0);
struct le *le;
jclass clz = (*env)->GetObjectClass(env, instance);
g_ctx.mainActivityClz = (*env)->NewGlobalRef(env, clz);
g_ctx.mainActivityObj = (*env)->NewGlobalRef(env, instance);
runLoggingThread();
err = libre_init();
@ -390,7 +387,7 @@ Java_com_tutpro_baresip_BaresipService_baresipStart(JNIEnv *env, jobject instanc
goto out;
}
LOGD("Adding %u accounts", list_count(uag_list()));
LOGD("adding %u accounts", list_count(uag_list()));
char ua_buf[256];
struct ua *ua;
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);
jstring javaUA = (*env)->NewStringUTF(env, 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");
(*env)->CallVoidMethod(env, pctx->mainActivityObj, accountId, javaUA);
(*env)->CallVoidMethod(env, pctx->mainActivityObj, uaAddId, javaUA);
(*env)->DeleteLocalRef(env, javaUA);
}
LOGI("Running main loop\n");
LOGI("running main loop ...\n");
err = re_main(signal_handler);
out:
if (err) {
LOGE("Closing upon re_main error: (%d)\n", err);
LOGE("stopping UAs due to error: (%d)\n", err);
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();
// 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;
}
JNIEXPORT void JNICALL
Java_com_tutpro_baresip_BaresipService_baresipStop(JNIEnv *env, jobject thiz) {
LOGD("Closing upon stop");
ua_stop_all(false);
play = mem_deref(play);
// ua_close();
conf_close();
baresip_close();
mod_close();
// libre_close();
// tmr_debug();
// mem_debug();
Java_com_tutpro_baresip_BaresipService_baresipStop(JNIEnv *env, jobject thiz, jboolean force) {
LOGD("ua_stop_all upon baresipStop");
ua_stop_all(force);
return;
}

View File

@ -182,7 +182,6 @@ class BaresipService: Service() {
BaresipService.IS_SERVICE_RUNNING = true
registerReceiver(nr, IntentFilter("android.net.conn.CONNECTIVITY_CHANGE"))
showStatusNotification()
if (!RUN_FOREGROUNG) super.onStartCommand(intent, flags, startId)
}
"UpdateNotification" -> {
@ -190,13 +189,12 @@ class BaresipService: Service() {
}
"Stop" -> {
cleanStop()
if (RUN_FOREGROUNG) stopForeground(true)
stopSelf()
stop()
}
"Kill" -> {
stopSelf()
RESTARTING = true
stop()
}
}
@ -208,14 +206,11 @@ class BaresipService: Service() {
}
override fun onDestroy() {
Log.i(LOG_TAG, "In onDestroy")
Log.d(LOG_TAG, "In onDestroy")
super.onDestroy()
if (IS_SERVICE_RUNNING) {
Log.i(LOG_TAG, "Restart baresip killed by Android")
cleanStop()
val broadcastIntent = Intent("com.tutpro.baresip.Restart")
sendBroadcast(broadcastIntent)
}
Log.i(LOG_TAG, "Restart baresip killed by Android")
RESTARTING = true
stop()
}
private fun createNotificationChannels() {
@ -240,10 +235,7 @@ class BaresipService: Service() {
.setContentIntent(npi)
.setOngoing(true)
.setContent(RemoteViews(packageName, R.layout.status_notification))
if (RUN_FOREGROUNG)
startForeground(STATUS_NOTIFICATION_ID, snb.build())
else
nm.notify(STATUS_NOTIFICATION_ID, snb.build())
startForeground(STATUS_NOTIFICATION_ID, snb.build())
}
@Keep
@ -270,13 +262,8 @@ class BaresipService: Service() {
fun uaEvent(event: String, uap: String, callp: String) {
Log.d(LOG_TAG, "updateStatus got event $event/$uap/$callp")
if (!IS_SERVICE_RUNNING) return
if (event == "exit") {
val intent = Intent("service event")
intent.putExtra("event", event)
intent.putExtra("params", arrayListOf<String>())
LocalBroadcastManager.getInstance(this).sendBroadcast(intent)
if (event == "exit")
return
}
val ua = UserAgent.find(MainActivity.uas, uap)
if (ua == null) {
Log.w(LOG_TAG, "updateStatus did not find ua $uap")
@ -431,6 +418,19 @@ class BaresipService: Service() {
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() {
val contentView = RemoteViews(getPackageName(), R.layout.status_notification)
for (i: Int in 0 .. 5) {
@ -450,34 +450,42 @@ class BaresipService: Service() {
nm.notify(STATUS_NOTIFICATION_ID, snb.build())
}
private fun cleanStop() {
private fun stop() {
CallsActivity.saveHistory()
MessagesActivity.saveMessages()
MainActivity.uas.clear()
MainActivity.images.clear()
MainActivity.history.clear()
MainActivity.messages.clear()
baresipStop()
BaresipService.IS_SERVICE_RUNNING = false
unregisterReceiver(nr)
nm.cancelAll()
if (wl.isHeld) wl.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 baresipStop()
external fun baresipStop(force: Boolean)
companion object {
var IS_SERVICE_RUNNING = false
var RESTARTING = false
val STATUS_NOTIFICATION_ID = 101
val CALL_NOTIFICATION_ID = 102
val MESSAGE_NOTIFICATION_ID = 103
val DEFAULT_CHANNEL_ID = "com.tutpro.baresip.default"
val HIGH_CHANNEL_ID = "com.tutpro.baresip.high"
var disconnected = false
val RUN_FOREGROUNG = true
}

View File

@ -428,15 +428,10 @@ class MainActivity : AppCompatActivity() {
}
private fun handleServiceEvent(event: String, params: ArrayList<String>) {
if (event == "exit") {
if (BaresipService.IS_SERVICE_RUNNING) {
baresipService.setAction("Stop");
startService(baresipService)
}
Toast.makeText(applicationContext,
"Baresip has stopped! Check your network connectivity.",
Toast.LENGTH_SHORT).show()
finish()
if (event == "stopped") {
Log.d("Baresip", "Handling service event 'stopped'")
finishAndRemoveTask()
System.exit(0)
return
}
val uap = params[0]
@ -868,7 +863,6 @@ class MainActivity : AppCompatActivity() {
baresipService.setAction("Stop");
startService(baresipService)
}
finish()
return true
}
else -> return super.onOptionsItemSelected(item)