将 swift 线程注册为 pj_thread

Umu*_*man 4 c multithreading pjsip ios swift2

我目前正在使用 pjSIP 和 swift 开发 iOS 应用程序。

我在 .c 文件中找到了一个用于拨打电话的方法,就这样吧

void makeCall(const char *destUri){
    ...
    status = pjsua_call_make_call(...
}
Run Code Online (Sandbox Code Playgroud)

我得到了一个从主线程调用的快速方法,该方法从 C 文件调用 makeCall 函数。

如果我这样做,应用程序会崩溃,并提示我需要在调用 pjLib 函数之前将线程注册到 pjSIP。

要将线程注册到 pjSIP,我需要调用函数pj_thread_register

我尝试将线程添加为 UnsafeMutablePointer。

我的电话现在看起来像这样:

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)

}
Run Code Online (Sandbox Code Playgroud)

线程 1:EXC_Breakpoint 和致命错误“在展开可选值时发现 nil”

那么如何将线程所需的属性从 swift 传递到 C 呢?

Ner*_*har 5

尝试不要从 swift 发送引用,而是直接在您的 .c 文件中使用它:

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(...
}
Run Code Online (Sandbox Code Playgroud)

来源:ipjsua(构建pjSIP后获得的演示项目)