End of Life for Twilio Programmable Video - Upgrade to VideoSDKLearn More

How to Build GStreamer WebRTC App with C?

Learn how to integrate GStreamer with WebRTC for real-time media streaming. This guide covers setup, implementation steps, code examples, and best practices.

Introduction to GStreamer WebRTC Technology

What is GStreamer WebRTC?

GStreamer is a powerful and versatile pipeline-based multimedia framework designed for constructing graphs of media-handling components. It allows developers to build a variety of media applications, from simple audio playback to complex video streaming systems. GStreamer supports a wide range of formats, making it an ideal choice for multimedia application development.
WebRTC (Web Real-Time Communication) is a technology that enables peer-to-peer communication between browsers and mobile applications. It allows for real-time audio, video, and data sharing without the need for plugins or external software. WebRTC is widely used in applications such as video conferencing, online gaming, and live streaming.
Combining GStreamer with WebRTC leverages the strengths of both technologies, providing a robust solution for real-time media streaming and processing. GStreamer handles the multimedia processing, while WebRTC facilitates seamless real-time communication. For more information and the latest updates, you can visit the

GStreamer GitHub repository


Getting Started with the Code

Create a New GStreamer WebRTC App

To start building a GStreamer WebRTC application, you first need to set up your development environment. Ensure you have the necessary tools and libraries installed on your system.

Prerequisites and Dependencies

  • GStreamer: Make sure you have GStreamer installed on your system. You can download it from the official

    GStreamer website

  • WebRTC Libraries: Download and install the WebRTC libraries from



Install GStreamer and WebRTC

[a] Installing GStreamer

For Linux:


1sudo apt-get install gstreamer1.0-tools gstreamer1.0-plugins-base gstreamer1.0-plugins-good gstreamer1.0-plugins-bad gstreamer1.0-plugins-ugly
For macOS:


1brew install gstreamer gst-plugins-base gst-plugins-good gst-plugins-bad gst-plugins-ugly
For Windows, download the installer from the

GStreamer website


[b] Installing WebRTC

Follow the instructions on


to download and build the WebRTC libraries for your platform.

Structure of the Project

Organizing your project directory is crucial for managing code effectively. A typical GStreamer WebRTC project structure might look like this:
23├── src/
4│   ├── main.c
5│   ├── webrtc.c
6│   └── webrtc.h
7├── build/
8│   └── Makefile
9└── README.md

Important Files and Folders

  • src/: Contains the source code for your application.
  • build/: Contains build scripts and makefiles.
  • README.md: Provides documentation and instructions for your project.

App Architecture

Understanding the architecture of your GStreamer WebRTC application is key to successful implementation. The application typically consists of several components:
  • Media Source: Captures audio and video data.
  • Media Pipeline: Processes and encodes media data.
  • WebRTC Bin: Manages WebRTC sessions and connections.
  • Controls: Allows user interaction with the media stream.
The architecture ensures that media data flows seamlessly from the source through the pipeline and is transmitted via WebRTC to the end users.
With this setup, you're ready to start coding your GStreamer WebRTC application. The next sections will guide you through the step-by-step implementation, beginning with the main application file and setting up the necessary components.

Step 1: Get Started with main.c

Creating the main.c File

To kick off your GStreamer WebRTC application, you need to set up the main entry point of your program. This is where you'll initialize GStreamer, create the media pipeline, and set up WebRTC elements.

Setting Up the main Function

Start by creating a new file named main.c in the src directory. In this file, you'll define the main function and initialize GStreamer.


1#include <gst/gst.h>
3int main(int argc, char *argv[]) {
4    gst_init(&argc, &argv);
6    GMainLoop *main_loop = g_main_loop_new(NULL, FALSE);
8    // Create and set up the pipeline here
10    g_main_loop_run(main_loop);
12    gst_deinit();
13    return 0;
In this code snippet, you initialize GStreamer with gst_init(), create a GMainLoop to keep the application running, and call g_main_loop_run() to start the main loop.

Pipeline Creation

The next step is to create a simple GStreamer pipeline that will serve as the backbone of your application. This pipeline will handle the media flow and processing.

Code Snippet for Creating a Simple GStreamer Pipeline


1#include <gst/gst.h>
3int main(int argc, char *argv[]) {
4    gst_init(&argc, &argv);
6    GMainLoop *main_loop = g_main_loop_new(NULL, FALSE);
8    GstElement *pipeline = gst_pipeline_new("webrtc-pipeline");
9    GstElement *source = gst_element_factory_make("videotestsrc", "source");
10    GstElement *sink = gst_element_factory_make("autovideosink", "sink");
12    if (!pipeline || !source || !sink) {
13        g_printerr("Failed to create elements\n");
14        return -1;
15    }
17    gst_bin_add_many(GST_BIN(pipeline), source, sink, NULL);
18    if (gst_element_link(source, sink) != TRUE) {
19        g_printerr("Failed to link elements\n");
20        gst_object_unref(pipeline);
21        return -1;
22    }
24    gst_element_set_state(pipeline, GST_STATE_PLAYING);
26    g_main_loop_run(main_loop);
28    gst_element_set_state(pipeline, GST_STATE_NULL);
29    gst_object_unref(pipeline);
30    g_main_loop_unref(main_loop);
31    gst_deinit();
33    return 0;
In this snippet, you create a pipeline named webrtc-pipeline, add a video source (videotestsrc), and a video sink (autovideosink). The elements are linked together to form a complete pipeline. The pipeline is set to the PLAYING state, and the main loop runs to keep the application active.
This basic setup lays the groundwork for adding WebRTC components and further developing your application in the next steps.

Step 2: Wireframe All the Components

Setting Up the Components

In this step, you'll extend the basic pipeline created in main.c to include WebRTC elements. This will enable the application to handle real-time media streaming.

Adding and Linking WebRTC Elements to the Pipeline

To start, you need to add the necessary WebRTC elements to your pipeline. This typically includes elements for encoding and decoding video and audio, as well as the WebRTC bin that manages the WebRTC connections.

Code Snippets for Components

Adding WebRTC Bin


1   GstElement *webrtcbin = gst_element_factory_make("webrtcbin", "webrtcbin");
2   if (!webrtcbin) {
3       g_printerr("Failed to create webrtcbin\n");
4       return -1;
5   }

Adding Video Encoder


1   GstElement *videoconvert = gst_element_factory_make("videoconvert", "videoconvert");
2   GstElement *vp8enc = gst_element_factory_make("vp8enc", "vp8enc");
3   if (!videoconvert || !vp8enc) {
4       g_printerr("Failed to create video elements\n");
5       return -1;
6   }

Adding Audio Encoder


1   GstElement *audioconvert = gst_element_factory_make("audioconvert", "audioconvert");
2   GstElement *opusenc = gst_element_factory_make("opusenc", "opusenc");
3   if (!audioconvert || !opusenc) {
4       g_printerr("Failed to create audio elements\n");
5       return -1;
6   }

Linking the Components

Now, link these components into the pipeline. Ensure the data flows from the source, through the encoders, and into the WebRTC bin.


1gst_bin_add_many(GST_BIN(pipeline), source, videoconvert, vp8enc, webrtcbin, NULL);
2if (!gst_element_link_many(source, videoconvert, vp8enc, webrtcbin, NULL)) {
3    g_printerr("Failed to link video elements\n");
4    gst_object_unref(pipeline);
5    return -1;
8gst_bin_add_many(GST_BIN(pipeline), audioconvert, opusenc, webrtcbin, NULL);
9if (!gst_element_link_many(audioconvert, opusenc, webrtcbin, NULL)) {
10    g_printerr("Failed to link audio elements\n");
11    gst_object_unref(pipeline);
12    return -1;

Explanation of the Role of Each Component

  • webrtcbin: Manages WebRTC connections, including signaling and media streaming.
  • videoconvert: Converts video formats to ensure compatibility with the encoder.
  • vp8enc: Encodes video streams using the VP8 codec.
  • audioconvert: Converts audio formats for compatibility with the encoder.
  • opusenc: Encodes audio streams using the Opus codec.
By adding these components and linking them, you've prepared the pipeline to handle WebRTC streams. The next step involves designing the join screen, which provides a user interface for connecting to WebRTC sessions.

Step 3: Implement Join Screen

Designing the Join Screen

The join screen is the user interface where users can enter connection details and initiate the WebRTC session. It typically includes fields for entering a room ID or URL and buttons to join or leave the session.

Basic Layout and Elements Required

  1. Text Input Field:
    • Allows users to input the room ID or connection URL.
  2. Join Button:
    • Initiates the WebRTC connection using the provided details.
  3. Leave Button:
    • Ends the WebRTC session and disconnects the user.

Code for Join Screen

Creating the Join Screen UI

For simplicity, we'll use GTK, a multi-platform toolkit for creating graphical user interfaces, to design the join screen.

Code Snippet for Creating the Join Screen


1#include <gtk/gtk.h>
3static void on_join_button_clicked(GtkWidget *widget, gpointer data) {
4    const gchar *room_id = gtk_entry_get_text(GTK_ENTRY(data));
5    g_print("Joining room: %s\n", room_id);
6    // Implement WebRTC connection logic here
9static void on_leave_button_clicked(GtkWidget *widget, gpointer data) {
10    g_print("Leaving room\n");
11    // Implement WebRTC disconnection logic here
14int main(int argc, char *argv[]) {
15    gtk_init(&argc, &argv);
17    GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
18    gtk_window_set_title(GTK_WINDOW(window), "GStreamer WebRTC Join Screen");
19    gtk_window_set_default_size(GTK_WINDOW(window), 300, 200);
21    GtkWidget *vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 5);
22    gtk_container_add(GTK_CONTAINER(window), vbox);
24    GtkWidget *entry = gtk_entry_new();
25    gtk_box_pack_start(GTK_BOX(vbox), entry, TRUE, TRUE, 0);
27    GtkWidget *join_button = gtk_button_new_with_label("Join");
28    g_signal_connect(join_button, "clicked", G_CALLBACK(on_join_button_clicked), entry);
29    gtk_box_pack_start(GTK_BOX(vbox), join_button, TRUE, TRUE, 0);
31    GtkWidget *leave_button = gtk_button_new_with_label("Leave");
32    g_signal_connect(leave_button, "clicked", G_CALLBACK(on_leave_button_clicked), NULL);
33    gtk_box_pack_start(GTK_BOX(vbox), leave_button, TRUE, TRUE, 0);
35    g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
37    gtk_widget_show_all(window);
38    gtk_main();
40    return 0;

Handling User Input and Events

  • on_join_button_clicked: Captures the room ID from the input field and initiates the WebRTC connection logic.
  • on_leave_button_clicked: Ends the WebRTC session when the leave button is clicked.
By implementing this join screen, you provide a simple and interactive interface for users to connect to WebRTC sessions, making the application more user-friendly. The next step will focus on implementing the control functionalities.

Step 4: Implement Controls

Setting Up Controls

In this step, you'll add basic control functionalities to your GStreamer WebRTC application, such as play, pause, and stop. These controls will allow users to manage the media stream during the WebRTC session.

Implementing Basic Controls

  1. Play Button:
    • Starts or resumes the media stream.
  2. Pause Button:
    • Pauses the media stream.
  3. Stop Button:
    • Stops the media stream and resets the pipeline.

Code Snippets for Controls

Adding Control Buttons to the UI

Expand your existing GTK-based join screen to include buttons for play, pause, and stop.


1#include <gtk/gtk.h>
2#include <gst/gst.h>
4GstElement *pipeline;
6static void on_play_button_clicked(GtkWidget *widget, gpointer data) {
7    gst_element_set_state(pipeline, GST_STATE_PLAYING);
8    g_print("Stream started\n");
11static void on_pause_button_clicked(GtkWidget *widget, gpointer data) {
12    gst_element_set_state(pipeline, GST_STATE_PAUSED);
13    g_print("Stream paused\n");
16static void on_stop_button_clicked(GtkWidget *widget, gpointer data) {
17    gst_element_set_state(pipeline, GST_STATE_READY);
18    g_print("Stream stopped\n");
21int main(int argc, char *argv[]) {
22    gst_init(&argc, &argv);
23    gtk_init(&argc, &argv);
25    pipeline = gst_pipeline_new("webrtc-pipeline");
27    GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
28    gtk_window_set_title(GTK_WINDOW(window), "GStreamer WebRTC Controls");
29    gtk_window_set_default_size(GTK_WINDOW(window), 300, 200);
31    GtkWidget *vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 5);
32    gtk_container_add(GTK_CONTAINER(window), vbox);
34    GtkWidget *entry = gtk_entry_new();
35    gtk_box_pack_start(GTK_BOX(vbox), entry, TRUE, TRUE, 0);
37    GtkWidget *join_button = gtk_button_new_with_label("Join");
38    g_signal_connect(join_button, "clicked", G_CALLBACK(on_join_button_clicked), entry);
39    gtk_box_pack_start(GTK_BOX(vbox), join_button, TRUE, TRUE, 0);
41    GtkWidget *leave_button = gtk_button_new_with_label("Leave");
42    g_signal_connect(leave_button, "clicked", G_CALLBACK(on_leave_button_clicked), NULL);
43    gtk_box_pack_start(GTK_BOX(vbox), leave_button, TRUE, TRUE, 0);
45    GtkWidget *play_button = gtk_button_new_with_label("Play");
46    g_signal_connect(play_button, "clicked", G_CALLBACK(on_play_button_clicked), NULL);
47    gtk_box_pack_start(GTK_BOX(vbox), play_button, TRUE, TRUE, 0);
49    GtkWidget *pause_button = gtk_button_new_with_label("Pause");
50    g_signal_connect(pause_button, "clicked", G_CALLBACK(on_pause_button_clicked), NULL);
51    gtk_box_pack_start(GTK_BOX(vbox), pause_button, TRUE, TRUE, 0);
53    GtkWidget *stop_button = gtk_button_new_with_label("Stop");
54    g_signal_connect(stop_button, "clicked", G_CALLBACK(on_stop_button_clicked), NULL);
55    gtk_box_pack_start(GTK_BOX(vbox), stop_button, TRUE, TRUE, 0);
57    g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
59    gtk_widget_show_all(window);
60    gtk_main();
62    gst_object_unref(pipeline);
63    return 0;

Integrating Controls with the GStreamer Pipeline

  • on_play_button_clicked: Sets the pipeline state to PLAYING to start or resume the stream.
  • on_pause_button_clicked: Sets the pipeline state to PAUSED to pause the stream.
  • on_stop_button_clicked: Sets the pipeline state to READY to stop the stream and reset the pipeline.
These controls provide users with basic functionality to manage the media stream during the WebRTC session, enhancing the user experience and making the application more interactive. The next step involves creating the participant view to display active participants in the session.

Get Free 10,000 Minutes Every Months

No credit card required to start.

Step 5: Implement Participant View

Creating the Participant View

The participant view is crucial for displaying the streams of all active participants in a WebRTC session. This allows users to see and interact with each other in real-time.

Layout Design for Displaying Participants

To implement the participant view, you need a layout that can dynamically display multiple video streams. A grid layout is often a suitable choice for this purpose.

Code for Participant View

Expanding the GTK-based UI

Enhance the existing GTK interface to include a drawing area or a grid where video streams can be displayed.


1#include <gtk/gtk.h>
2#include <gst/gst.h>
4GstElement *pipeline;
6static void on_join_button_clicked(GtkWidget *widget, gpointer data) {
7    const gchar *room_id = gtk_entry_get_text(GTK_ENTRY(data));
8    g_print("Joining room: %s\n", room_id);
9    // Implement WebRTC connection logic here
12static void on_leave_button_clicked(GtkWidget *widget, gpointer data) {
13    g_print("Leaving room\n");
14    // Implement WebRTC disconnection logic here
17static void on_play_button_clicked(GtkWidget *widget, gpointer data) {
18    gst_element_set_state(pipeline, GST_STATE_PLAYING);
19    g_print("Stream started\n");
22static void on_pause_button_clicked(GtkWidget *widget, gpointer data) {
23    gst_element_set_state(pipeline, GST_STATE_PAUSED);
24    g_print("Stream paused\n");
27static void on_stop_button_clicked(GtkWidget *widget, gpointer data) {
28    gst_element_set_state(pipeline, GST_STATE_READY);
29    g_print("Stream stopped\n");
32int main(int argc, char *argv[]) {
33    gst_init(&argc, &argv);
34    gtk_init(&argc, &argv);
36    pipeline = gst_pipeline_new("webrtc-pipeline");
38    GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
39    gtk_window_set_title(GTK_WINDOW(window), "GStreamer WebRTC Participant View");
40    gtk_window_set_default_size(GTK_WINDOW(window), 600, 400);
42    GtkWidget *vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 5);
43    gtk_container_add(GTK_CONTAINER(window), vbox);
45    GtkWidget *entry = gtk_entry_new();
46    gtk_box_pack_start(GTK_BOX(vbox), entry, FALSE, FALSE, 0);
48    GtkWidget *join_button = gtk_button_new_with_label("Join");
49    g_signal_connect(join_button, "clicked", G_CALLBACK(on_join_button_clicked), entry);
50    gtk_box_pack_start(GTK_BOX(vbox), join_button, FALSE, FALSE, 0);
52    GtkWidget *leave_button = gtk_button_new_with_label("Leave");
53    g_signal_connect(leave_button, "clicked", G_CALLBACK(on_leave_button_clicked), NULL);
54    gtk_box_pack_start(GTK_BOX(vbox), leave_button, FALSE, FALSE, 0);
56    GtkWidget *play_button = gtk_button_new_with_label("Play");
57    g_signal_connect(play_button, "clicked", G_CALLBACK(on_play_button_clicked), NULL);
58    gtk_box_pack_start(GTK_BOX(vbox), play_button, FALSE, FALSE, 0);
60    GtkWidget *pause_button = gtk_button_new_with_label("Pause");
61    g_signal_connect(pause_button, "clicked", G_CALLBACK(on_pause_button_clicked), NULL);
62    gtk_box_pack_start(GTK_BOX(vbox), pause_button, FALSE, FALSE, 0);
64    GtkWidget *stop_button = gtk_button_new_with_label("Stop");
65    g_signal_connect(stop_button, "clicked", G_CALLBACK(on_stop_button_clicked), NULL);
66    gtk_box_pack_start(GTK_BOX(vbox), stop_button, FALSE, FALSE, 0);
68    GtkWidget *grid = gtk_grid_new();
69    gtk_box_pack_start(GTK_BOX(vbox), grid, TRUE, TRUE, 0);
71    // Function to add video streams to the grid will be implemented here
73    g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
75    gtk_widget_show_all(window);
76    gtk_main();
78    gst_object_unref(pipeline);
79    return 0;

Managing Multiple Streams

Implement a function to add video streams to the grid dynamically. This function will be called whenever a new participant joins the session.


1void add_video_stream(GtkWidget *grid, GstElement *video_sink, int participant_id) {
2    GtkWidget *drawing_area = gtk_drawing_area_new();
3    gtk_widget_set_size_request(drawing_area, 160, 120);
5    gtk_grid_attach(GTK_GRID(grid), drawing_area, participant_id % 4, participant_id / 4, 1, 1);
6    gtk_widget_show(drawing_area);
8    // Connect the video_sink to the drawing_area for rendering the stream
9    gst_video_overlay_set_window_handle(GST_VIDEO_OVERLAY(video_sink), GDK_WINDOW_XID(gtk_widget_get_window(drawing_area)));
add_video_stream: This function adds a drawing area for each video stream to the grid and connects the video sink to the drawing area for rendering the video.
By implementing the participant view, your application can display multiple video streams, allowing users to see all active participants in the WebRTC session. This step enhances the interactivity and usability of your GStreamer WebRTC application.

Step 6: Run Your Code Now

Compiling and Running the Application

Now that you have implemented the main components of your GStreamer WebRTC application, it’s time to compile and run the code. Follow these steps to ensure your application is ready for use.

Steps to Compile the Code

[a] Create a Makefile

Create a Makefile in the build directory to simplify the compilation process.
1   CC=gcc
2   CFLAGS=`pkg-config --cflags gstreamer-1.0 gtk+-3.0`
3   LIBS=`pkg-config --libs gstreamer-1.0 gtk+-3.0`
4   SOURCES=../src/main.c
5   TARGET=gstreamer-webrtc
8       $(CC) -o $(TARGET) $(SOURCES) $(CFLAGS) $(LIBS)

[b] Compile the Code

Navigate to the build directory and run the make command.


1   cd build
2   make

[c] Running the Application

Once the code is compiled, you can run the application:


This command starts your GStreamer WebRTC application, launching the UI with the join screen and controls.

Troubleshooting Common Issues

  • Missing Dependencies: Ensure all GStreamer and GTK dependencies are installed.
  • Pipeline Errors: Check for errors in the pipeline setup and linking of elements.
By following these steps, you can successfully compile and run your GStreamer WebRTC application, allowing you to test and use the functionalities implemented so far.


In this article, we explored the integration of GStreamer with WebRTC, providing a robust solution for real-time media streaming and processing. Starting with setting up the development environment, we progressed through creating the main application, adding essential components, implementing the join screen and controls, and finally running the application. This comprehensive guide, complete with practical code snippets and detailed explanations, aims to equip you with the knowledge needed to develop your own GStreamer WebRTC applications.

Want to level-up your learning? Subscribe now

Subscribe to our newsletter for more tech based insights