diago

repository·main·Indexed 18 days ago

https://github.com/emiago/diago

A high-level Go library for building VOIP applications, providing simplified APIs for SIP signaling and media handling (RTP/SDP) on top of the sipgo library. It includes a media package for handling RTP/RTCP streams using io.Reader and io.Writer interfaces, support for PCM encoding/decoding, WAV file processing, and DTMF encoding/decoding via RFC4733. The library supports building media servers for playback, recording, and bridging between SIP endpoints.

Tokens
15.7K
Snippets
76
Records
90
Agent score
64%

What's inside diago

  1. Overview of the diago media package

    main

    The media package provides an optimized framework for handling RTP/RTCP media streams. It is designed around the Go standard library's io.Reader and io.Writer interfaces, allowing developers to chain readers and writers to build interceptors, encoders, and decoders without significant memory allocation overhead or contention.

    Key features include:

    • SDP building for alaw, ulaw, and dtmf formats.
    • RTP/RTCP receiving and logging.
    • Extendable MediaSession handling for hardware like microphones and speakers.
    • DTMF encoding/decoding via RFC4733.
    • RTP/RTCP session and monitoring capabilities.
  2. What is Diago?

    main
    Diago is a high-level library for building VOIP (Voice over IP) solutions in Go. It is built on top of the sipgo library and provides high-level APIs to handle calls, registrations, and media sessions, while still allowing low-level packet control if necessary. It is designed to make telephony development easier and more testable.
  3. Important media API change: Using returned media objects

    main
    In recent API updates (specifically in the webrtc-pion branch), Answer and Invite methods now return a media stack object. Instead of accessing media through the dialog session, you should capture and use the returned media object for all media operations. This change is intended to facilitate the integration of the Pion media stack into Diago.
  4. Understand the media IO flow

    main

    The package uses a layered approach to media processing by chaining standard library interfaces.

    For receiving media (Reader flow): AudioDecoder $\leftrightarrow$ RTPPacketReader $\leftrightarrow$ RTPSession $\leftrightarrow$ MediaSession

    For sending media (Writer flow): AudioEncoder $\leftrightarrow$ RTPPacketWriter $\leftrightarrow$ RTPSession $\leftrightarrow$ MediaSession

    By following this pattern, you can insert custom logic (like interceptors or transformations) anywhere in the chain by implementing the appropriate io.Reader or io.Writer.

  5. Core concepts in diago media

    main

    To work with media in this package, you need to understand the following abstractions and how they compose:

    • Media Session: Represents the mapping between an SDP (Session Description Protocol) media description and a session created based on local and remote addresses.
    • RTP Session: Manages the RTP/RTCP session. It uses a MediaSession underneath to provide the networking layer.
    • RTP Packet Reader: Depacketizes RTP packets and exposes the payload as an io.Reader. This is typically chained to an RTP Session.
    • RTP Packet Writer: Packetizes a payload into RTP packets as an io.Writer. This is typically chained to an RTP Session.
  6. Enable SIP and RTP tracing for debugging

    main

    When debugging VOIP issues, you can enable tracing for SIP, RTCP, and RTP packets. Note that enabling media.RTPDebug will dump every single RTP packet, which can be very verbose.

    sip.SIPDebug = true // Enables SIP tracing
    media.RTCPDebug = true // Enables RTCP tracing
    media.RTPDebug = true // Enables RTP tracing. NOTE: It will dump every RTP Packet
  7. Install the opus C library for audio encoding/decoding

    main

    The diago audio package requires the opus C library for various encoding and decoding tasks. Depending on your Linux distribution, use the following commands to install the necessary dependencies:

    Ubuntu

    Install libopus0 using apt.

    Fedora

    Install opus-devel and opusfile-devel using dnf.

    # Ubuntu
    sudo apt install libopus0
    
    # Fedora
    sudo dnf install opus-devel
    sudo dnf install opusfile-devel
  8. Manage inbound calls with DialogServerSession

    main

    The DialogServerSession struct is the primary interface for handling inbound SIP calls (UAS - User Agent Server). It embeds sipgo.DialogServerSession and extends it with media handling capabilities via DialogMedia.

    Key responsibilities include:

    • Responding to SIP signaling (Ringing, Trying, Progress).
    • Managing media sessions (SDP negotiation, early media, answering calls).
    • Handling call control (Hold, Unhold, Re-Invite, Refer, Hangup).
    • Managing media updates and re-invites.
    var session *diago.DialogServerSession
    // session is typically provided by the diago server upon receiving an INVITE
  9. Handle incoming REFER requests

    main

    When your application receives a REFER request, you can use dialogHandleRefer or dialogHandleReferTransaction to process it.

    dialogHandleRefer is used when you want to provide a callback (OnReferDialogFunc) that receives the new dialog created by the REFER operation. This is useful for managing the lifecycle of the referred call.

    dialogHandleReferTransaction provides a more granular approach using ReferTransaction, allowing you to manually Accept the transaction and control the invitation process via Accept(ctx, opts).