register static method

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

Register SIP extension

Implementation

static Future<bool> register({
  required String username,
  required String password,
  String? domain,
  String? realm,
  String? displayName,
  String? callerId,
  String? sendAs,
  String? sipServer,
  int port = 5060,
  bool useWebSocket = false,
  String? wsUrl,
}) async {
  try {
    if (_sipHelper == null) {
      await initialize();
    }

    debugPrint('═══════════════════════════════════════');
    debugPrint('📝 SIP REGISTRATION');
    debugPrint('═══════════════════════════════════════');
    debugPrint('Username: $username');
    debugPrint('Domain: ${domain ?? sipServer}');
    debugPrint('Realm: $realm');
    debugPrint('Display Name: ${displayName ?? callerId ?? sendAs ?? username}');
    debugPrint('Caller ID: $callerId');
    debugPrint('WebSocket: $useWebSocket');
    debugPrint('═══════════════════════════════════════');

    // Use domain if provided, otherwise use sipServer
    final serverDomain = domain ?? sipServer ?? '';
    if (serverDomain.isEmpty) {
      throw Exception('Domain or SIP server is required');
    }

    // Build SIP URI - use domain if provided
    final sipUri = 'sip:$username@$serverDomain';

    // Determine display name (prefer callerId/sendAs)
    final finalDisplayName = displayName ?? callerId ?? sendAs ?? 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 (for authentication)
    if (realm != null && realm.isNotEmpty) {
      uaSettings.realm = realm;
    }

    // Set authorization user if different from username
    if (username.isNotEmpty) {
      uaSettings.authorizationUser = username;
    }

    if (useWebSocket) {
      // Use provided WebSocket URL or construct from realm/server
      if (wsUrl != null && wsUrl.isNotEmpty) {
        uaSettings.webSocketUrl = wsUrl;
      } else if (realm != null && realm.startsWith('ws')) {
        // If realm is a WebSocket URL, use it
        uaSettings.webSocketUrl = realm;
      } else {
        uaSettings.webSocketUrl = 'wss://$serverDomain';
      }
    } else {
      uaSettings.registrarServer = 'sip:$serverDomain:$port';
      uaSettings.port = port.toString();
    }

    // Store settings for reconnection
    _lastUaSettings = uaSettings;

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

    debugPrint('SIP: Registration request sent');
    return true;
  } catch (e, stackTrace) {
    _printException('Register', e, stackTrace);
    return false;
  }
}