newInvite method

Call newInvite(
  1. String callerName,
  2. String callerNumber,
  3. String destinationNumber,
  4. String clientState, {
  5. Map<String, String> customHeaders = const {},
  6. List<AudioCodec>? preferredCodecs,
  7. AudioConstraints? audioConstraints,
  8. bool debug = false,
  9. bool mutedMicOnStart = false,
})

Creates and sends a new call invitation.

This method initiates an outgoing call to a destinationNumber (which can be a SIP URI or a phone number).

  • callerName: The name to be displayed to the callee.

  • callerNumber: The phone number or SIP URI of the caller.

  • destinationNumber: The phone number or SIP URI to call.

  • clientState: A custom string that can be used to store and retrieve state between applications. It is passed to the remote party.

  • customHeaders: Optional custom SIP headers to add to the INVITE message.

    Note that if you are calling a Telnyx AI Assistant, Headers with the X- prefix will be mapped to dynamic variables in the AI assistant (e.g., X-Account-Number becomes {{account_number}}). Note: Hyphens in header names are converted to underscores in variable names.

  • preferredCodecs: Optional list of preferred audio codecs in order of preference. If any codec in the list is not supported by the platform or remote party, the system will automatically fall back to a supported codec.

  • debug: Enables detailed logging for this specific call if set to true.

  • mutedMicOnStart: When true, starts the call with the microphone muted. Defaults to false.

Returns a Call object representing the new outgoing call.

Implementation

Call newInvite(
  String callerName,
  String callerNumber,
  String destinationNumber,
  String clientState, {
  Map<String, String> customHeaders = const {},
  List<AudioCodec>? preferredCodecs,
  AudioConstraints? audioConstraints,
  bool debug = false,
  bool mutedMicOnStart = false,
}) {
  final Call inviteCall = _createCall()
    ..sessionCallerName = callerName
    ..sessionCallerNumber = callerNumber
    ..sessionDestinationNumber = destinationNumber
    ..sessionClientState = clientState;
  customHeaders = customHeaders;
  inviteCall.callId = const Uuid().v4();
  final base64State = base64.encode(utf8.encode(clientState));
  updateCall(inviteCall);

  // Create the peer connection with debug enabled if requested
  inviteCall.peerConnection = Peer(
    inviteCall.txSocket,
    debug || _debug,
    this,
    getForceRelayCandidate(),
    audioConstraints,
    _serverConfiguration.turn,
    _serverConfiguration.stun,
    mutedMicOnStart,
  );
  // Convert AudioCodec objects to Map format for the peer connection
  List<Map<String, dynamic>>? codecMaps;
  if (preferredCodecs != null && preferredCodecs.isNotEmpty) {
    codecMaps = preferredCodecs.map((codec) => codec.toJson()).toList();
  }

  inviteCall.peerConnection?.invite(
    callerName,
    callerNumber,
    destinationNumber,
    base64State,
    inviteCall.callId!,
    inviteCall.sessid,
    customHeaders,
    preferredCodecs: codecMaps,
  );

  if (debug) {
    inviteCall.initCallMetrics();
  }

  //play ringback tone
  inviteCall.playAudio(_ringBackpath);
  inviteCall.callHandler.changeState(CallState.newCall);
  return inviteCall;
}