Linphone SDK使用(二)登录

一、SIP账号登录

_account_creator = linphone_account_creator_new(
        LC, [LinphoneManager.instance lpConfigStringForKey:@"xmlrpc_url" inSection:@"assistant" withDefault:@""]
                .UTF8String);
NSString *ip = @"SIP地址ip";
NSInteger port = @"SIP地址端口";
NSString *username = @"SIP登录账号";
NSString *displayName = @"SIP登录账号用户名";
NSString *pwd = @"SIP登录密码";
NSString *domain = [NSString stringWithFormat:@"%@:%ld", ip, port];
LinphoneAccountParams *accountParams =  linphone_core_create_account_params(LC);
LinphoneAddress *addr = linphone_address_new(NULL);

linphone_address_set_username(addr, username.UTF8String);
linphone_address_set_port(addr, port);
linphone_address_set_domain(addr, ip.UTF8String);
if (displayName && ![displayName isEqualToString:@""]) {
    linphone_address_set_display_name(addr, displayName.UTF8String);
}

linphone_account_params_set_identity_address(accountParams, addr);
// set transport
// 使用UDP方式登录
NSString *type = @"UDP";
LinphoneAddress *transportAddr = linphone_address_new([NSString stringWithFormat:@"sip:%s;transport=%s", domain.UTF8String, type.lowercaseString.UTF8String].UTF8String);
linphone_account_params_set_routes_addresses(accountParams, bctbx_list_new(transportAddr));
linphone_account_params_set_server_addr(accountParams, [NSString stringWithFormat:@"%s;transport=%s", domain.UTF8String, type.lowercaseString.UTF8String].UTF8String);

linphone_address_unref(transportAddr);


linphone_account_params_set_publish_enabled(accountParams, FALSE);
linphone_account_params_set_register_enabled(accountParams, TRUE);

// 设置ice关闭,不然通话保持后,不能再次接回
LinphoneNatPolicy *policy = linphone_core_create_nat_policy(LC);
linphone_nat_policy_enable_stun(policy, FALSE); // We always use STUN with ICE
linphone_nat_policy_enable_ice(policy, FALSE);
linphone_account_params_set_nat_policy(accountParams, policy);

LinphoneAuthInfo *info =
    linphone_auth_info_new(linphone_address_get_username(addr), // username
                           NULL,                                // user id
                           pwd.UTF8String,                        // passwd
                           NULL,                                // ha1
                           linphone_address_get_domain(addr),   // realm - assumed to be domain
                           linphone_address_get_domain(addr)    // domain
                           );
linphone_core_add_auth_info(LC, info);
linphone_address_unref(addr);

LinphoneAccount *account = linphone_core_create_account(LC, accountParams);
linphone_account_params_unref(accountParams);
if (account) {
    if (linphone_core_add_account(LC, account) != -1) {
        linphone_core_set_default_account(LC, account);
        // reload address book to prepend proxy config domain to contacts' phone number
        // todo: STOP doing that!
        // 添加成功
    } else {
      
    }
} else {
  
}

通知监听账号登录成功

[NSNotificationCenter.defaultCenter addObserver:self
                                           selector:@selector(registrationUpdate:)
                                               name:kLinphoneRegistrationUpdate
                                             object:nil];
...
- (void)registrationUpdate:(NSNotification *)notif
{
    LinphoneAccount *account = linphone_core_get_default_account(LC);
    LinphoneRegistrationState state = linphone_account_get_state(account);
    switch (state) {
        case LinphoneRegistrationOk:
        {
            [self successToSipRegister];
        }
            break;
        case LinphoneRegistrationNone:
        case LinphoneRegistrationCleared:
           
            break;
        case LinphoneRegistrationFailed:
        {
            [self failedToSipRegister];
        }
            break;
        case LinphoneRegistrationProgress:
            
            break;
        default:
            break;
    }
}

二、退出登录

[[LinphoneManager instance] removeAllAccounts];
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容