connect method

void connect(
  1. dynamic target, [
  2. Map<String, dynamic>? options,
  3. InitSuccessCallback? initCallback
])

Implementation

void connect(dynamic target,
    [Map<String, dynamic>? options,
    InitSuccessCallback? initCallback]) async {
  logger.debug('connect()');

  options = options ?? <String, dynamic>{};
  dynamic originalTarget = target;
  EventManager eventHandlers = options['eventHandlers'] ?? EventManager();
  List<dynamic> extraHeaders = utils.cloneArray(options['extraHeaders']);
  Map<String, dynamic> mediaConstraints = options['mediaConstraints'] ??
      <String, dynamic>{'audio': true, 'video': true};
  MediaStream? mediaStream = options['mediaStream'];
  Map<String, dynamic> pcConfig =
      options['pcConfig'] ?? <String, dynamic>{'iceServers': <dynamic>[]};
  Map<String, dynamic> rtcConstraints =
      options['rtcConstraints'] ?? <String, dynamic>{};
  Map<String, dynamic> rtcOfferConstraints =
      options['rtcOfferConstraints'] ?? <String, dynamic>{};
  _rtcOfferConstraints = rtcOfferConstraints;
  _rtcAnswerConstraints =
      options['rtcAnswerConstraints'] ?? <String, dynamic>{};
  data = options['data'] ?? data;

  // Check target.
  if (target == null) {
    throw exceptions.TypeError('Not enough arguments');
  }

  // Check Session Status.
  if (_status != C.statusNull) {
    throw exceptions.InvalidStateError(_status);
  }

  // Check WebRTC support.
  // TODO(cloudwebrtc): change support for flutter-webrtc
  //if (RTCPeerConnection == null)
  //{
  //  throw exceptions.NotSupportedError('WebRTC not supported');
  //}

  // Check target validity.
  target = _ua!.normalizeTarget(target);
  if (target == null) {
    throw exceptions.TypeError('Invalid target: $originalTarget');
  }

  // Session Timers.
  if (_sessionTimers.enabled) {
    if (utils.isDecimal(options['sessionTimersExpires'])) {
      if (options['sessionTimersExpires'] >= dart_sip_c.MIN_SESSION_EXPIRES) {
        _sessionTimers.defaultExpires = options['sessionTimersExpires'];
      } else {
        _sessionTimers.defaultExpires = dart_sip_c.SESSION_EXPIRES;
      }
    }
  }

  // Set event handlers.
  addAllEventHandlers(eventHandlers);

  // Session parameter initialization.
  _from_tag = utils.newTag();

  // Set anonymous property.
  bool anonymous = options['anonymous'] ?? false;
  Map<String, dynamic> requestParams = <String, dynamic>{
    'from_tag': _from_tag
  };
  _ua!.contact!.anonymous = anonymous;
  _ua!.contact!.outbound = true;
  _contact = _ua!.contact.toString();

  if (anonymous) {
    requestParams['from_display_name'] = 'Anonymous';
    requestParams['from_uri'] = URI('sip', 'anonymous', 'anonymous.invalid');
    extraHeaders
        .add('P-Preferred-Identity: ${_ua!.configuration!.uri.toString()}');
    extraHeaders.add('Privacy: id');
  }

  extraHeaders.add('Contact: $_contact');
  extraHeaders.add('Content-Type: application/sdp');
  if (_sessionTimers.enabled) {
    extraHeaders.add('Session-Expires: ${_sessionTimers.defaultExpires}');
  }

  _request =
      InitialOutgoingInviteRequest(target, _ua, requestParams, extraHeaders);

  _id = _request.call_id + _from_tag;

  // Create a RTCPeerConnection instance.
  await _createRTCConnection(pcConfig, rtcConstraints);

  // Set internal properties.
  _direction = 'outgoing';
  _local_identity = _request.from;
  _remote_identity = _request.to;

  // User explicitly provided a newRTCSession callback for this session.
  if (initCallback != null) {
    initCallback(this);
  }

  _newRTCSession('local', _request);
  await _sendInitialRequest(
      pcConfig, mediaConstraints, rtcOfferConstraints, mediaStream);
}