Try Live Demo

Imagine effortlessly streaming high-quality media with just a few lines of code. That’s the power of combining Python, GStreamer, and WebRTC with Ant Media ! If you’re looking to dive into the world of real-time media streaming and need a scalable, low-latency solution, then you’ve come to the right place. In this blog post, I’ll walk you through the process of publishing a media stream to Ant Media Server using Python. It’s easy, it’s fun, and best of all — it’s all in your control. Let’s get started!

Prerequisites

Before diving into the code, make sure you have the following:

Ant Media Server

You can install Ant Media Server as told in this document.

Python Environment

  • Ensure Python 3.8+ is installed on your system.
  • Install the required Python modules:
pip install PyGObject pycairo websocket-client

Install GStreamer

Ubuntu:

Run the following command:

apt-get install libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev libgstreamer-plugins-bad1.0-dev gstreamer1.0-plugins-base gstreamer1.0-plugins-good gstreamer1.0-plugins-bad gstreamer1.0-plugins-ugly gstreamer1.0-libav gstreamer1.0-tools gstreamer1.0-x gstreamer1.0-alsa gstreamer1.0-gl gstreamer1.0-gtk3 gstreamer1.0-qt5 libjson-glib-dev gstreamer1.0-nice gstreamer1.0-pulseaudio

Windows:

There are two installer binaries gstreamer-1.0 and gstreamer-devel. Make sure you install both of them

Download the GStreamer Windows installer

Mac:

There are two installer binaries gstreamer-1.0 and gstreamer-devel make sure you install both of them.

Download the GStreamer Mac installer and export the path to Gstreamer:

export  PKG_CONFIG_PATH="/Library/Frameworks/GStreamer.framework/Versions/Current/lib/pkgconfig${PKG_CONFIG_PATH:+:$PKG_CONFIG_PATH}"

Step 1. Download WebRTC Python SDK

Download webrtc.py and publish_play.py  and place it in same directory.

wget https://raw.githubusercontent.com/USAMAWIZARD/webrtc-ant-media/refs/heads/master/webrtc.py
wget https://raw.githubusercontent.com/USAMAWIZARD/webrtc-ant-media/refs/heads/master/publish_play.py

Step 2. Publish WebRTC Stream

1. Run the program with your server URL:

python3 publish_play.py wss://test.antmedia.io/live/websocket stream123 publish

2. Open Ant Media Server’s dashboard and verify the stream under your application’s live streams section you can play the stream by clicking on playing icon next to the stream.

Step 3. Play WebRTC Stream

  1. Keep the publish running and open a new terminal to play our published stream.
python3 publish_play.py wss://test.antmedia.io/live/websocket stream123 play
playing

Troubleshooting

1. WebSocket Connection Issues:

  • Ensure the WebSocket URL is correct and accessible.Specify the ports number and domain/ip correctly use wss or ws accordingly.
  • wss://ip:5443/appname/websocket or ws://ip:5080/appname/webscoket

2. GStreamer Plugin Errors:

  • Run gst-inspect-1.0 to list available plugins and ensure required ones are installed.
gst-inspect-1.0 opus vpx nice webrtc dtls srtp rtp rtpmanager videotestsrc audiotestsrc
if there is no error that means every thing is installed correctly.

3. Debugging Logs:

export GST_DEBUG=3
  • Set the above environment variable and run the script, this will show some debug logs for the script to identify issues.

Code Walkthrough for Developers

Here is the Python script to connect to Ant Media Server to publish and play a WebRTC stream.You can access full source code here

from webrtc import WebRTCAdapter, init_gstreamer

appname = "live"
WEBSOCKET_URL = 'wss://test.antmedia.io/' + appname + '/websocket'

init_gstreamer()
webrtc_adapter = WebRTCAdapter(WEBSOCKET_URL)
webrtc_adapter.connect()
# replace publish call with play to play a stream
webrtc_adapter.publish("test1")
GStreamer Initialization:
init_gstreamer()
  •  initializes GStreamer checks if required plugins are installed.

WebSocket URL:

  • If you want to publish to your own Server, replace WEBSOCKET_URL with the appropriate WebSocket endpoint for your Ant Media Server application.

WebRTCAdapter:

webrtc_adapter = WebRTCAdapter(WEBSOCKET_URL)
  • This class abstracts the WebRTC connection process. It handles connecting to the WebSocket server, WebRTC signalling, Gstreamer pipelines for publishing and playing the streams.
webrtc_adapter.connect()

connects to the WebSocket endpoint specified while creating WebRTCAdapter.

webrtc_adapter.publish("test1")
  • The method starts streaming the media under the ID test1. This ID is unique and used to identify the stream on Ant Media Server.
webrtc_adapter.play("test1")
  • If you want to play a stream from Ant Media Server just replace the publish call with play and it will play a stream.

Conclusion

In this post, we demonstrated how to use Python, to publish and play stream to and from Ant Media Server. By leveraging these technologies, you can create powerful media streaming applications.

Feel free to explore additional features like subscribing to streams or integrating custom GStreamer pipelines for advanced media processing.

Happy streaming!

Categories: Tutorial

Usama Tahseen

Usama is a Software Engineer at Ant Media. His technical stack includes WebRTC, Gstreamer , FFmpeg , Python, Javascript, Node.js