wiki:PJSUA_Initialization

Threading issue with PJSUA initialization and shutdown

Moved to https://docs.pjsip.org/en/latest/specific-guides/develop/init_shutdown_thread.html

To use PJSIP, it is recommended to call pj_init() and pj_shutdown() from the main thread. After pj_init() is completed, application can continue with the initialization or create a secondary/worker thread and register the thread by calling pj_thread_register() (Creating a secondary thread is especially recommended, sometimes necessary, for apps that require main thread to be responsive, such as GUI apps, or apps that use video, or real-time apps). As described in PJSUA-API Basic API documentation, app needs to call pjsua_create(), pjsua_pool_create(), pjsua_init() to perform the initialization. Then app must call pjsua_start() to start PJSUA and finally after everything is done, call pjsua_destroy() to shut it down. Sample code:

int main()
{
    pj_init();
    // Continue with PJSUA initialization here or create a secondary thread
    ....
    // After pjsua_destroy() is called
    pj_shutdown();
}

int worker_thread()
{
    // Register the thread, after pj_init() is called
    pj_thread_register();

    // Create pjsua and pool
    pjsua_create();
    pjsua_pool_create();

    // Init pjsua
    pjsua_init();

    // Start pjsua
    pjsua_start();

    .........

    // Destroy pjsua
    pjsua_destroy();
}

When restarting the library, after pjsua_destroy() is completed, application needs to call pj_shutdown() and pj_init() in the main thread. Application also needs to make sure that the number of calls to pj_shutdown() matches with the calls to pj_init()

Last modified 2 years ago Last modified on Jan 25, 2023 8:56:26 AM