registerAccount static method

Future<String> registerAccount({
  1. required String domain,
  2. required String username,
  3. required String password,
  4. String? realm,
  5. String? callerId,
  6. String? displayName,
  7. int port = 5060,
  8. bool useWebSocket = false,
  9. String? wsUrl,
  10. String? accountId,
})

Register a new SIP account Returns the account ID

Implementation

static Future<String> registerAccount({
  required String domain,
  required String username,
  required String password,
  String? realm,
  String? callerId,
  String? displayName,
  int port = 5060,
  bool useWebSocket = false,
  String? wsUrl,
  String? accountId, // Optional custom account ID
}) async {
  try {
    // Generate account ID if not provided
    final accId = accountId ?? '$username@$domain';

    debugPrint('═══════════════════════════════════════');
    debugPrint('📝 MULTI-SIP: REGISTERING ACCOUNT');
    debugPrint('═══════════════════════════════════════');
    debugPrint('Account ID: $accId');
    debugPrint('Domain: $domain');
    debugPrint('Username: $username');
    debugPrint('═══════════════════════════════════════');

    // Create account
    final account = SipAccount(
      accountId: accId,
      domain: domain,
      username: username,
      realm: realm,
      callerId: callerId,
      displayName: displayName,
    );

    // Create SIPUAHelper for this account
    final helper = SIPUAHelper();
    final listener = _SipAccountListener(accId);
    helper.addSipUaHelperListener(listener);

    // Store references
    _accounts[accId] = account;
    _sipHelpers[accId] = helper;
    _listeners[accId] = listener;
    _currentCalls[accId] = null;

    // Store helper reference in listener for answer call
    listener._helper = helper;

    // Build SIP URI
    final sipUri = 'sip:$username@$domain';
    final finalDisplayName = displayName ?? callerId ?? username;

    // Create UaSettings
    final uaSettings = UaSettings()
      ..uri = sipUri
      ..password = password
      ..displayName = finalDisplayName
      ..userAgent = 'VoIP Test Flutter'
      ..register = true
      ..transportType = useWebSocket ? TransportType.WS : TransportType.TCP
      ..iceServers = [
        {'urls': 'stun:stun.l.google.com:19302'},
      ];

    // Set realm if provided
    if (realm != null && realm.isNotEmpty) {
      uaSettings.realm = realm;
    }

    if (username.isNotEmpty) {
      uaSettings.authorizationUser = username;
    }

    if (useWebSocket) {
      if (wsUrl != null && wsUrl.isNotEmpty) {
        uaSettings.webSocketUrl = wsUrl;
      } else if (realm != null && realm.startsWith('ws')) {
        uaSettings.webSocketUrl = realm;
      } else {
        uaSettings.webSocketUrl = 'wss://$domain';
      }
    } else {
      uaSettings.registrarServer = 'sip:$domain:$port';
      uaSettings.port = port.toString();
    }

    // Start SIP UA (this registers)
    await helper.start(uaSettings);

    debugPrint('✅ Account registration initiated: $accId');
    return accId;
  } catch (e, stackTrace) {
    _printException('Register Account', e, stackTrace);
    rethrow;
  }
}