What are the best practises while using pjsip in a multi-threaded environment in Java ?

847 views
Skip to first unread message

anand prajapati

unread,
May 9, 2017, 7:57:08 AM5/9/17
to PJSIP experts
I am using pjsip on server side to integrate with SIP protocol. However when I try to update a call from a different thread, I get a fatal error saying 

(!"Calling pjlib from unknown/external thread. You must " "register external threads with pj_thread_register() " "before calling any pjlib functions."), function pj_thread_this, file ../src/pj/os_core_unix.c, line 692.

Since the error seems explanatory, I try to register the thread before updating call in my threads. I use the following snipped for registering :-


public void registerIfNeeded(String name) {
if (mEndpoint.libIsThreadRegistered()) return;
try {
Logger.debug("----------------- Registering thread. ------------");
if (TextUtils.isEmpty(name)) name = this.getClass().getName();
mEndpoint.libRegisterThread(name);
} catch (Exception e) {
Logger.error("Unable to register thread ", e);
}
}

Now What I don't understand is that sometimes it works but most of the times I get the same error again. There must
be something that I am doing wrong. Please help me guys.

vinee...@gmail.com

unread,
May 9, 2017, 9:38:26 AM5/9/17
to PJSIP experts

Before calling any PJ API in the same function register thread by giving call to

            status = pj_thread_register("ABCD", calling_thread_desc,

                  &pj_thread);

 

  This usually works in C, C++ environment.

anand prajapati

unread,
May 10, 2017, 4:19:41 AM5/10/17
to PJSIP experts
Thanks for your reply. I am calling the equivalent Java Api indeed. Still it works sometimes and fails sometimes.

Vitold S

unread,
Mar 6, 2018, 12:52:05 PM3/6/18
to PJSIP experts
Java have another one thread model it model provide not 1:1 os thread mapping.
You may read https://www.developer.com/java/data/understanding-the-java-thread-model.html article for more understand thread model in Java
I suggest write microservice on pure C and connect with your java application or you may write dll or .so library with thread safe call it means you create thread safe queue from Java calls and another pthread with pjsip stack.

Reply all
Reply to author
Forward
0 new messages