Call pjlib from unknown / outer thread. You must "" register external threads with pj_thread_register ()

I have included pjsua2 in my android app. When sending an SMS message, the application crashes. It does not fall every time, it happens randomly. (every 10 posts).

MyCode:

public void sendInstantMessage(String number, String msgBody) { String buddy_uri = "<sip:" + number + "@" + mPref.getString(PREF_SIPSERVER, "") + ">"; Log.e(TAG, "sendInstantMessage ==== "+buddy_uri); BuddyConfig bCfg = new BuddyConfig(); bCfg.setUri(buddy_uri); bCfg.setSubscribe(false); MyBuddy im = new MyBuddy(bCfg); SendInstantMessageParam prm = new SendInstantMessageParam(); prm.setContent(msgBody); prm.setContentType("text/plain; charset=utf-8"); try { im.create(account, bCfg); boolean valid1 = im.isValid(); Log.e(TAG, "valid1 ======= "+valid1); im.sendInstantMessage(prm); } catch (Exception e) { Log.e(TAG, "sendInstantMessage ==== "+e); e.printStackTrace(); return; } } 

According to logcat, I have to call pj_thread_register (). But I have a libRegisterThread () method at the endpoint, so I used it as shown below

 MyApp.ep.libRegisterThread("SipApi"); 

Here is logcat:

 ../src/pj/os_core_unix.c:692: pj_thread_this: assertion "!"Calling pjlib from unknown/external thread. You must " "register external threads with pj_thread_register() " "before calling any pjlib functions."" failed 
+6
source share
3 answers

Here is the correct answer.

 /**Send message to this number * @param String number * @param String msgBody*/ public void sendInstantMessage(String number, String msgBody) { String sipServer = "aaa.ggg.net"; String buddy_uri = "<sip:" + number + "@" + sipServer + ">"; BuddyConfig bCfg = new BuddyConfig(); bCfg.setUri(buddy_uri); bCfg.setSubscribe(false); MyBuddy myBuddy = new MyBuddy(bCfg); SendInstantMessageParam prm = new SendInstantMessageParam(); prm.setContent(msgBody); try { myBuddy.create(account, bCfg); myBuddy.sendInstantMessage(prm); myBuddy.delete(); } catch (Exception e) { e.printStackTrace(); return; } } 
+3
source

(For those who wonder why he solved the problem)

To clarify Gangadhar's solution, the problem was caused by the garbage collector. By PJSIP Docs :

There are two problems with the Java garbage collector (gc):

  • it delays the destruction of Java objects (including pjsua2 objects), as a result of which the code in the object's destructor will be executed out of order.
  • Gc operation can be performed in different threads, but not previously registered in PJLIB

The decision was

an application should destroy pjsua2 objects immediately, using the delete () object instead of relying on gc to clean the object.

as you can see in your solution, called the MyBuddy removal method:

 try { myBuddy.create(account, bCfg); myBuddy.sendInstantMessage(prm); myBuddy.delete(); } catch (Exception e) { e.printStackTrace(); return; } 
+3
source

To avoid this error, you need to register an external thread (for example, a Java thread) immediately after calling the libCreate () method; otherwise, the same error will be shown. PJSUA2 first registers the current thread as the main thread inside libCreate (). So, to register another thread, you need to do one of the following:

  • Call libCreate () from the thread that you want to register as the main thread.
  • Call libRegisterThread (the name of the thread you need) after calling libCreate (). The new stream will be registered as a secondary stream.
0
source

Source: https://habr.com/ru/post/989346/


All Articles