Skip to content

Instantly share code, notes, and snippets.

@pulsar256
Last active January 29, 2024 01:19
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pulsar256/fa1b2958ba7fe9731a10a501593c5207 to your computer and use it in GitHub Desktop.
Save pulsar256/fa1b2958ba7fe9731a10a501593c5207 to your computer and use it in GitHub Desktop.
Opus p2p Audio Streaming

Opus p2p streaming

Low latency streaming of ogg compressed audio data between two peers using RTP and gstreamer-1.0. Expected latency over the network is below 1 second.

Producer

The producer will provide a Jack sink which needs to be connected to a audio producer.

TARGET_PORT=5100
TARGET_IP=PEER_IP

gst-launch-1.0 \
  jackaudiosrc connect=1 \
  ! audio/x-raw, channels=2 \
  ! audioconvert \
  ! opusenc bitrate-type=vbr bitrate=128000 \
  ! rtpopuspay pt=101 \
  ! udpsink host=$TARGET_IP port=$TARGET_PORT

Consumer

Gstreamer Based

Default / auto audio sink

RECEIVER_PORT=5100

gst-launch-1.0 \
  udpsrc port=$RECEIVER_PORT \
  ! application/x-rtp,media=audio,payload=101,encoding-name=OPUS \
  ! rtpjitterbuffer \
  ! rtpopusdepay \
  ! opusdec \
  ! audioconvert \
  ! autoaudiosink

Jack Audio Sink

RECEIVER_PORT=5100

gst-launch-1.0 \
  udpsrc port=$RECEIVER_PORT \
  ! application/x-rtp,media=audio,payload=101,encoding-name=OPUS \
  ! rtpjitterbuffer \
  ! rtpopusdepay \
  ! opusdec \
  ! audioconvert \
  ! audioresample \
  ! jackaudiosink

VLC Based

VLC requires a SDP File specifying the RTP payload.

stream.sdp:

m=audio 5100 RTP/AVP 101
c=IN IP4 127.0.0.1
a=rtpmap:101 opus/48000/2

run with

vlc ./stream.sdp

Please notice the hardcoded UDP port in the SDP file set to 5100, depending on your network configuration / forwarding rules this might need to change.

See https://tools.ietf.org/html/rfc7587 for more details.

Notes

this solution provides a peer to peer path only. If multiple clients shall connect to the same audio source, a RTSP server would be the next logical building block. See https://github.com/GStreamer/gst-rtsp-server for examples how to implement one. test-sdp and test-launch in the 1.16 branch did not yeld any working servers during my tests.

rtpjitterbuffer for the client might be optional if your network is stable and transport is udp (which it is in the examples above).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment