ioscmultithreadingpjsipswift2

register a swift thread as pj_thread


I'm currently working on an iOS app using pjSIP and swift.

I got a method on in a .c-file for making a call, let it be

void makeCall(const char *destUri){
    ...
    status = pjsua_call_make_call(...
}

and I got a swift method that is called from the main thread which is calling the makeCall-function from the C-file.

If I do so, the app crashes and by saying that I need to register the thread to pjSIP before calling anymore pjLib functions.

To register a thread to pjSIP I need to call the function pj_thread_register

I tried to add the thread as UnsafeMutablePointer.

my calls look like this right now:

void makeCall(const char *destUri, long threadDesc, pj_thread_t **thread){
    ...
    pj_thread_register(NULL, &threadDesc, thread);
    status = pjsua_call_make_call(...
}


internal static func makeSIPCall(numberToCall: String){
    ...
    let destination = ... //my call destination
    let thread : NSThread = NSThread.currentThread()
    let observer = UnsafeMutablePointer<CopaquePointer>(Unmanaged.passUnretained(thread.self).toOpue())

    makeCall(destination, Int(thread.description)! as Clong, observer)

}

Thread 1: EXC_Breakpoint and the fatal error "found nil while unwrapping an Optional value"

So how can I pass the needed properties of the thread from swift to C?


Solution

  • Try not to send an reference from swift but use this directly within your .c-file:

    void makeCall(const char *destUri, long threadDesc, pj_thread_t **thread){
        ...
        pj_thread_desc a_thread_desc;
        pj_thread_t *a_thread;
    
        if (!pj_thread_is_registered()) {
            pj_thread_register(NULL, a_thread_desc, &a_thread);
        }
        status = pjsua_call_make_call(...
    }
    

    Source: ipjsua (demo project you get after building pjSIP)