开发者

SIP deregistration using PJSIP

开发者 https://www.devze.com 2023-04-06 16:09 出处:网络
In our app, we need to de-reg a user when he pushes the app to the background. We are using PJSIP. My applicationDidEnterBackground:

In our app, we need to de-reg a user when he pushes the app to the background. We are using PJSIP. My applicationDidEnterBackground:

- (void)applicationDidEnterBackground:(UIApplication *)application {
    NSLog(@"did enter background");


     __block UIBackgroundTaskIdentifier bgTask;

     bgTask = [application beginBackgroundTaskWithExpirationHandler:^{
        [application endBackgroundTask:bgTask];
        bgTask = UIBackgroundTaskInvalid;
    }];


    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        [self deregis];        
        [application endBackgroundTask: bgTask]; //End the task so the system knows that you are done with what you need to perform
        bgTask = UIBackgroundTaskInvalid; //Invalidate the background_task
        NSLog(@"\n\nRunning in the background!\n\n");

    });
     }

The deregis method is as below:

- (void)deregis {
    if (!pj_thread_is_registered())
    {
        pj_thread_register("ipjsua", a_thread_desc, &a_thread);
   }    
    dereg();

}

And the de-reg method is开发者_StackOverflow中文版 as below:

void dereg()
{
    int i;
    for (i=0; i<(int)pjsua_acc_get_count(); ++i) {
        if (!pjsua_acc_is_valid(i))
             pjsua_buddy_del(i);

        pjsua_acc_set_registration(i, PJ_FALSE);
    }
}

When we push the app to the background, the dereg gets called. But when the server sends back a 401 challenge, the stack isn't sending back the auth details in SIP call until I bring the application back to foreground. Does anyone have any idea why this is happening?

Thanks, Hetal


You don't want to end the background task in your background thread:

e.g.

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    [self deregis];        
    // don't do below...
    // [application endBackgroundTask: bgTask]; //End the task so the system knows that you are done with what you need to perform
    // bgTask = UIBackgroundTaskInvalid; //Invalidate the background_task
    NSLog(@"\n\nRunning in the background!\n\n");

});

You want to end the background task when the registration is updated. So you need to hook into the pjsua on_reg_state callback.

e.g. this example may only assume one unregister, for multiple accounts you have to wait until all are unregistered

-(void) regStateChanged: (bool)unregistered {
    if (unregistered && bgTask != UIBackgroundTaskInvalid) {
        [application endBackgroundTask: bgTask]; //End the task so the system knows that you are done with what you need to perform
        bgTask = UIBackgroundTaskInvalid; //Invalidate the background_task
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消