dart-sip-ua

repository·main·Indexed 18 days ago

https://github.com/flutter-webrtc/dart-sip-ua

A Dart-based SIP User Agent stack ported from JsSIP for Flutter applications. It enables SIP signaling over WebSockets and TCP, and WebRTC media over UDP, supporting audio/video calls, instant messaging, and DTMF. It is compatible with standard servers such as OpenSIPS, Kamailio, Asterisk, 3CX, and FreeSWITCH.

Tokens
5.1K
Snippets
22
Records
27
Agent score
59%

What's inside dart-sip-ua

  1. Overview of dart-sip-ua

    main

    dart-sip-ua is a Dart implementation of the SIP User Agent (UA) stack, ported from JsSIP. It allows for SIP over WebSocket and TCP, enabling real SIP functionality in Flutter mobile, desktop, and web applications.

    Key features include:

    • Audio/Video Calls: Integrated with flutter-webrtc.
    • Instant Messaging: Support for SIP-based messaging.
    • Standard Server Support: Works with OpenSIPS, Kamailio, Asterisk, 3CX, and FreeSWITCH.
    • DTMF Support: Supports RFC2833 or INFO for sending DTMF tones.
  2. Understand the connection model (Signalling vs Media)

    main

    In dart-sip-ua, the connection is split into two layers:

    1. Signalling: Uses WebSocket (WS) or TCP to initiate and terminate sessions (SIP messages).
    2. Media: Once the session is established, WebRTC transmits the actual audio/video data over UDP.

    Note that the package does not support UDP for the signalling process itself.

  3. Get started with the dart_sip_ua_example project

    main

    To run the example project, you must ensure your Flutter SDK is using the dev channel. Follow these steps to set up and run the project:

    1. Switch to the dev channel: flutter channel dev
    2. Run the project creation script: ./scripts/project_tools.sh create
    3. Run the application: flutter run
    flutter channel dev
    ./scripts/project_tools.sh create
    flutter run
  4. Customize iOS launch screen assets

    main

    To change the launch image for the iOS version of your application, you can either replace the image files directly in the directory or use Xcode.

    Method 1: Direct replacement Replace the existing image files in example/ios/Runner/Assets.xcassets/LaunchImage.imageset/ with your own assets.

    Method 2: Using Xcode

    1. Open the iOS project in Xcode by running open ios/Runner.xcworkspace from your terminal.
    2. In the Xcode Project Navigator, navigate to Runner/Assets.xcassets.
    3. Drag and drop your desired images into the asset catalog to replace the existing launch images.
    open ios/Runner.xcworkspace
  5. Generate grammar_parser.dart using Docker

    main

    If you prefer not to manage legacy Dart 1.x versions locally, you can use Docker to generate the grammar parser.

    Docker Generation Steps

    1. Install Docker on your system.
    2. Clone the repository and run make commands:
      git clone https://github.com/flutter-webrtc/dart-sip-ua
      cd dart-sip-ua
      make peg && make grammar
    git clone https://github.com/flutter-webrtc/dart-sip-ua
    cd dart-sip-ua
    make peg && make grammar
  6. Configure Android Proguard rules for dart-sip-ua

    main

    When building for Android, you must add specific Proguard rules to prevent the obfuscation of Flutter and WebRTC classes, which is necessary for the plugin to function correctly.

    -keep class io.flutter.app.** { *; }
    -keep class io.flutter.plugin.**  { *; }
    -keep class io.flutter.util.**  { *; }
    -keep class io.flutter.view.**  { *; }
    -keep class io.flutter.**  { *; }
    -keep class io.flutter.plugins.**  { *; }
    
    -keep class com.cloudwebrtc.webrtc.** {*;}
    -keep class org.webrtc.** {*;}
  7. Generate grammar_parser.dart using the peg tool

    main

    The grammar_parser.dart file is generated from grammar.peg using the peg tool. Because the peg tool is incompatible with Dart 2.x, you must use Dart 1.24.3 to perform the generation manually.

    Manual Generation Steps

    1. Install Dart 1.24.3:

      brew install dart@1
      export PATH="/usr/local/opt/dart@1/bin:$PATH"
    2. Setup the peg tool:

      cd tools
      git clone https://github.com/cloudwebrtc/peg
      cd peg
      pub get
    3. Run the generation script:

      ./tool/generate_grammar.sh
    # Manual generation sequence
    brew install dart@1
    export PATH="/usr/local/opt/dart@1/bin:$PATH"
    cd tools
    git clone https://github.com/cloudwebrtc/peg
    cd peg
    pub get
    ./tool/generate_grammar.sh
  8. Define and validate socket configurations

    main

    The sockets parameter in Settings is mandatory and must be a non-empty list of SIPUASocketInterface. The configuration logic supports several ways to define sockets:

    • Single Socket: sockets: socket (Note: The validation logic expects a List, so ensure it is wrapped in []).
    • List of Sockets: sockets: [socket1, socket2]
    • List of Objects with weights: sockets: [{socket: socket1, weight: 1}, {socket: socket2, weight: 0}]
    • Mixed List: sockets: [{socket: socket1}, socket2]

    If the sockets list is empty or not a list, a ConfigurationError will be thrown.

    // Example of providing a list of sockets
    settings.sockets = [
      myWebSocketSocket,
      myOtherSocket,
    ];
  9. Troubleshoot SIP/2.0 488 Not acceptable here

    main

    The error SIP/2.0 488 Not acceptable here typically indicates a codec mismatch between your PBX server and WebRTC.

    WebRTC uses the following codecs:

    • opus (payload type 111, 48kHz, 2 channels)
    • red (payload type 63, 48kHz, 2 channels)
    • G722 (payload type 9, 8kHz, 1 channel)
    • ILBC (payload type 102, 8kHz, 1 channel)
    • PCMU (payload type 0, 8kHz, 1 channel)
    • PCMA (payload type 8, 8kHz, 1 channel)
    • CN (payload type 13, 8kHz, 1 channel)
    • telephone-event (payload type 110, 48kHz, 1 channel for wideband, 8000Hz, 1 channel for narrowband)
  10. Troubleshoot WEBRTC_SET_REMOTE_DESCRIPTION_ERROR

    main

    If you encounter the error WEBRTC_SET_REMOTE_DESCRIPTION_ERROR: Failed to set remote offer sdp: Called with SDP without DTLS fingerprint, it means your SIP server is not sending a DTLS fingerprint inside the SDP during the invite.

    Because WebRTC requires encryption by default (using DTLS and SRTP), your PBX must be explicitly configured to use DTLS/SRTP when communicating with the sip_ua client.

  11. Configure the Settings class for SIP UA

    main

    The Settings class is used to configure the SIP User Agent (UA) stack. It contains parameters for authentication, SIP account details, session management, registration, and connection behavior.

    Key Configuration Groups

    SIP Authentication

    • authorization_user: The username for SIP authentication.
    • password: The password for SIP authentication.
    • realm: The authentication realm.
    • ha1: The HA1 value.

    SIP Account

    • display_name: The display name for the account.
    • uri: The SIP URI of the account.
    • contact_uri: The contact URI.
    • user_agent: The User-Agent string (defaults to DartSIP_C.USER_AGENT).
    • instance_id: The SIP instance ID (GRUU). If provided with a uuid: prefix, the prefix is stripped.

    Registration

    • register: Whether to register automatically (defaults to true).
    • register_expires: Registration expiration time in seconds (defaults to 600).
    • registrar_server: The registrar server URI.
    • register_extra_headers: A list of extra headers to include in registration.
    • register_extra_contact_uri_params: A map of extra parameters for the contact URI.

    Session & DTMF

    • session_timers: Enables/disables session timers (defaults to true).
    • session_timers_refresh_method: The method used to refresh timers. Supported values are SipMethod.INVITE or SipMethod.UPDATE (defaults to SipMethod.UPDATE).
    • no_answer_timeout: Timeout in seconds for unanswered calls (defaults to 60).
    • dtmf_mode: The DTMF mode (e.g., DtmfMode.INFO).

    Connection & Transport

    • transportType: The type of transport to use.
    • sockets: A list of SIPUASocketInterface objects used for connectivity.
    • connection_recovery_max_interval: Maximum interval for connection recovery (defaults to 30).
    • connection_recovery_min_interval: Minimum interval for connection recovery (defaults to 2).
    • ice_gathering_timeout: ICE gathering timeout in milliseconds (defaults to 500).
    • sip_message_delay: Delay for SIP messages in milliseconds (defaults to 0).
    Settings settings = Settings()
      ..uri = URI('sip:user@domain.com')
      ..authorization_user = 'my_user'
      ..password = 'my_password'
      ..sockets = [mySocket]
      ..transportType = TransportType.webSocket;