Skip to content

Instantly share code, notes, and snippets.

@NimaMX
Last active August 15, 2024 08:59
Show Gist options
  • Save NimaMX/1dc00f25bf73cd14b9adb855c9d649be to your computer and use it in GitHub Desktop.
Save NimaMX/1dc00f25bf73cd14b9adb855c9d649be to your computer and use it in GitHub Desktop.
[Gstreamer] a cpp implementation to receive two audio from udp sink

Tested with gstreamer GStreamer 1.14.5 (on aarch64 and amd64)

To stream system mic with udp, on Windows:

.\gst-launch-1.0.exe -v directsoundsrc ! audioresample ! audioconvert ! rtpL24pay ! udpsink host=192.168.1.3 port=4343

To stream system mic with udp, on Linux:

gst-launch-1.0 -v alsasrc ! queue ! audioresample ! audioconvert ! rtpL24pay ! udpsink host=192.168.1.3 port=4343

cmake_minimum_required(VERSION 3.10.2)
project(RTSP-Test)
find_package(PkgConfig REQUIRED)
#pkg_check_modules(gtk3 REQUIRED IMPORTED_TARGET gtk+-3.0)
pkg_search_module(gstreamer REQUIRED IMPORTED_TARGET gstreamer-1.0)
pkg_search_module(gstreamer-sdp REQUIRED IMPORTED_TARGET gstreamer-sdp-1.0)
pkg_search_module(gstreamer-app REQUIRED IMPORTED_TARGET gstreamer-app-1.0)
pkg_search_module(gstreamer-audio REQUIRED IMPORTED_TARGET gstreamer-audio-1.0)
add_executable(rtps-test udpsrc_filesink.cpp)
target_link_libraries(rtps-test
PkgConfig::gstreamer
PkgConfig::gstreamer-sdp
PkgConfig::gstreamer-app
PkgConfig::gstreamer-audio
)
#include <gst/gst.h>
static GMainLoop *loop;
int main (int argc, char *argv[]) {
GstElement *pipeline, *udpsrc, *rtpL2depay, *audioconvert, *wavenc, *filesink;
gst_init (&argc, &argv);
loop = g_main_loop_new (NULL, FALSE);
pipeline = gst_pipeline_new ("pipeline");
udpsrc = gst_element_factory_make ("udpsrc", NULL);
rtpL2depay = gst_element_factory_make ("rtpL24depay", NULL);
audioconvert = gst_element_factory_make ("audioconvert", NULL);
wavenc = gst_element_factory_make("wavenc", NULL);
filesink = gst_element_factory_make ("filesink", NULL);
g_object_set(G_OBJECT(udpsrc),
"address", "192.168.1.3", /* UDP IP Address */
"port", 4343, /* UDP port */
NULL);
g_object_set (G_OBJECT (udpsrc), "caps",
gst_caps_new_simple ("application/x-rtp",
"channels", G_TYPE_INT, 2,
"format", G_TYPE_STRING, "S16LE",
"media", G_TYPE_STRING, "audio",
"payload", G_TYPE_INT, 96,
"clock-rate", G_TYPE_INT, 44100,
"encoding-name", G_TYPE_STRING, "L24",
NULL), NULL);
g_object_set(G_OBJECT(filesink), "location", "capture.wave", NULL);
gst_bin_add_many (GST_BIN (pipeline), udpsrc, rtpL2depay, audioconvert, wavenc, filesink, NULL);
gst_element_link_many (udpsrc, rtpL2depay, audioconvert, wavenc, filesink, NULL);
gst_element_set_state (pipeline, GST_STATE_PLAYING);
g_main_loop_run (loop);
gst_element_set_state (pipeline, GST_STATE_NULL);
gst_object_unref (GST_OBJECT (pipeline));
g_main_loop_unref (loop);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment