This repository documents our journey of learning the GLib family of libraries (GLib, GObject, GIO), GStreamer, and the Meson build system.
This project is structured as a series of hands-on tutorials, each exploring a different aspect of the GLib/GStreamer ecosystem.
The repository is organized into a main application and several tutorial modules:
.
├── main.c # Main application entry point that dispatches tutorials
├── meson.build # The main build configuration for Meson
├── tutorials/
│ ├── common/ # Common utility functions
│ ├── gio-example/ # GIO examples (D-Bus communication)
│ ├── gobject-example/ # GObject system tutorial
│ ├── gstreamer-example/ # GStreamer-related portal examples
│ └── timeout-example/ # GLib main loop and timeout example
└── ...
All tutorials are compiled into a single executable (glib-tutorials). A specific tutorial is run by providing its name as a command-line argument.
- Command:
timeout - File:
tutorials/timeout-example/timeout.c - Concept: Demonstrates the basics of the
GMainLoop. - Implementation: Uses
g_timeout_add()to schedule a function to be called every second for five seconds. This is a fundamental concept for any event-driven application using GLib.
- Command:
gobject-get-set - Files:
tutorials/gobject-example/example-person.c - Concept: Introduces the GObject type and object system.
- Implementation: Defines a new
ExamplePersonclass that inherits fromGObject. The example covers:- Defining a class structure with public and private members.
- Creating properties (
GParamSpec) for getter/setter access. - Registering and using signals.
- Instantiating the object and interacting with its properties.
- Command:
dbus-notification - File:
tutorials/gio-example/notification-sender.c - Concept: Shows how to communicate with other services over the D-Bus session bus using GIO.
- Implementation: Sends a desktop notification by calling the
Notifymethod on theorg.freedesktop.Notificationsservice. It demonstrates creating aGDBusConnectionand using it to call a remote method with parameters (GVariant).
- Command:
screencast - File:
tutorials/gstreamer-example/screencast.c - Concept: An advanced example that interacts with the Freedesktop Portals API to set up a screen sharing session and record it with GStreamer. This is a necessary step for sandboxed applications to access resources like the screen.
- Implementation: This tutorial walks through the entire portal screencasting flow:
- Create a Session: Initiates a
CreateSessionrequest via D-Bus. - Select Sources: Opens the desktop environment's dialog for the user to select which screen/window to share.
- Start Cast: Starts the screencast, receives a PipeWire stream node, and constructs a GStreamer pipeline to record it.
- It makes heavy use of asynchronous D-Bus calls and signal subscriptions within the
GMainLoop.
- Create a Session: Initiates a
- Options:
--output <FILE>or-o <FILE>: Specifies the output file path for the screen recording. Defaults tocapture.mkvin the current working directory.
DISCLAIMER: The screencast tutorial is designed to work exclusively with NVIDIA graphics cards that support CUDA for hardware-accelerated video encoding. This tutorial requires the nvidia, nvidia-utils, cuda, and related proprietary packages to be installed and fully functional on your system. It does not support AMD, Intel, or other integrated graphics solutions due to its reliance on NVIDIA's specific encoding capabilities.
You will need a C compiler, Meson, Ninja, and the development files for GLib and GStreamer.
- GLib & Build Tools:
glib2.0,meson,ninja - GStreamer:
gstreamer-1.0and the following plugin packages:gstreamer-plugins-basegstreamer-plugins-goodgstreamer-plugins-badgstreamer-plugins-uglylibgstreamer-plugins-base1.0-dev(for development headers)
- Portal Implementation: For the
screencasttutorial, you also need a Freedesktop portal implementation installed (e.g.,xdg-desktop-portal-gtk).
Debian / Ubuntu
sudo apt-get install build-essential libglib2.0-dev meson ninja-build xdg-desktop-portal-gtk gstreamer1.0-plugins-base gstreamer1.0-plugins-good gstreamer1.0-plugins-bad gstreamer1.0-plugins-ugly gstreamer1.0-tools libgstreamer-plugins-base1.0-devFedora
sudo dnf install gcc glib2-devel meson ninja-build xdg-desktop-portal-gtk gstreamer1.0-plugins-base gstreamer1.0-plugins-good gstreamer1.0-plugins-bad gstreamer1.0-plugins-ugly gstreamer1.0-develArch Linux
sudo pacman -S base-devel glib2 meson ninja xdg-desktop-portal-gtk gst-plugins-base gst-plugins-good gst-plugins-bad gst-plugins-ugly gstreamer-
Set up the build directory:
meson setup build
-
Compile the project:
ninja -C build
The final executable is created in the build directory. Run it without arguments to see the list of available tutorials.
# List available tutorials
./build/glib-tutorials
# Usage: ./build/glib-tutorials <command>
# Available commands:
# - timeout
# - gobject-get-set
# - dbus-notification
# - screencastTo run a specific tutorial, provide its name as an argument. For example:
# Run the screencast portal example and save to a custom file
./build/glib-tutorials screencast --output my_recording.mkv