Created
May 14, 2025 19:31
-
-
Save arthurafarias/e76a7926f7fc754f3ee81fe224ccafbd to your computer and use it in GitHub Desktop.
TCP RAW Straming Service for Gstreamer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cmake_minimum_required(VERSION 3.15.3) | |
project(gstreamer-appsrc-example) | |
set(CMAKE_CXX_STANDARD 20) | |
find_package(PkgConfig REQUIRED) | |
pkg_check_modules(gtk3 REQUIRED IMPORTED_TARGET gtk+-3.0) | |
pkg_search_module(gstreamer REQUIRED IMPORTED_TARGET gstreamer-1.0>=1.4) | |
pkg_search_module(gstreamer-sdp REQUIRED IMPORTED_TARGET gstreamer-sdp-1.0>=1.4) | |
pkg_search_module(gstreamer-app REQUIRED IMPORTED_TARGET gstreamer-app-1.0>=1.4) | |
pkg_search_module(gstreamer-video REQUIRED IMPORTED_TARGET gstreamer-video-1.0>=1.4) | |
add_executable(gstreamer-appsrc-example main.cpp) | |
target_link_libraries(gstreamer-appsrc-example | |
PkgConfig::gtk3 | |
PkgConfig::gstreamer | |
PkgConfig::gstreamer-sdp | |
PkgConfig::gstreamer-app | |
PkgConfig::gstreamer-video | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// example appsrc for gstreamer 1.0 with own mainloop & external buffers. based | |
// on example from gstreamer docs. public domain, 2015 by Florian Echtler | |
// <[email protected]>. compile with: gcc --std=c99 -Wall $(pkg-config | |
// --cflags gstreamer-1.0) -o gst-appsrc gst-appsrc.c $(pkg-config --libs | |
// gstreamer-1.0) -lgstapp-1.0 | |
#include <glib.h> | |
#include <gst/app/gstappsrc.h> | |
#include <gst/gst.h> | |
#include <gst/gstbin.h> | |
#include <gst/gstparse.h> | |
#include <stdint.h> | |
int want = 1; | |
#define BUFFER_LENGTH (288 * 384) | |
#define FRAMES (1000) | |
uint16_t data[BUFFER_LENGTH * FRAMES]; | |
static void cb_need_data_push_buffer(GstElement *appsrc, guint unused_size, | |
gpointer user_data) { | |
static gboolean white = FALSE; | |
static GstClockTime timestamp = 0; | |
GstBuffer *buffer; | |
guint size; | |
GstFlowReturn ret; | |
GMainLoop *main_loop = (GMainLoop *)user_data; | |
size = BUFFER_LENGTH * sizeof(uint16_t); | |
buffer = gst_buffer_new_wrapped_full((GstMemoryFlags)0, (gpointer)data, size, | |
0, size, NULL, NULL); | |
white = !white; | |
GST_BUFFER_PTS(buffer) = timestamp; | |
GST_BUFFER_DURATION(buffer) = gst_util_uint64_scale_int(1, GST_SECOND, 1000); | |
timestamp += GST_BUFFER_DURATION(buffer); | |
ret = gst_app_src_push_buffer(GST_APP_SRC(appsrc), buffer); | |
if (ret != GST_FLOW_OK) { | |
/* something wrong, stop pushing */ | |
g_main_loop_quit(main_loop); | |
} | |
} | |
static void cb_need_data(GstElement *appsrc, guint unused_size, | |
gpointer user_data) { | |
cb_need_data_push_buffer(appsrc, unused_size, user_data); | |
} | |
gint main(gint argc, gchar *argv[]) { | |
gst_init(&argc, &argv); | |
auto main_loop = g_main_loop_new(NULL, FALSE); | |
auto pipeline = gst_parse_launch( | |
"appsrc name=appsrc emit-signals=true format=GST_FORMAT_TIME block=true " | |
"! video/x-raw, width=384, height=288, format=GRAY16_LE, " | |
"framerate=1000/1 ! rawvideoparse use-sink-caps=true ! tcpserversink host=0.0.0.0 port=5000", | |
NULL); | |
auto appsrc = gst_bin_get_by_name(GST_BIN(pipeline), "appsrc"); | |
g_signal_connect(appsrc, "need-data", G_CALLBACK(cb_need_data), main_loop); | |
gst_element_set_state(pipeline, GST_STATE_PLAYING); | |
g_main_loop_run(main_loop); | |
gst_element_set_state(pipeline, GST_STATE_NULL); | |
gst_object_unref(GST_OBJECT(pipeline)); | |
gst_object_unref(GST_OBJECT(appsrc)); | |
g_main_loop_unref(main_loop); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment