hii,
i want to run a cpp program with gstreamer elements as follows
|—> queue1---->videoconvert—>appsink
v4l2src—> tee —>|
|—>queue2—>encoder—>muxer—>filesink
following is my code
#include <gst/gst.h>
#include <gst/app/gstappsink.h>
static GstFlowReturn new_sample_callback(GstElement *sink, gpointer user_data) {
GstSample *sample;
GstBuffer *buffer;
guint size;
// Retrieve the sample from the appsink
sample = gst_app_sink_pull_sample(GST_APP_SINK(sink));
buffer = gst_sample_get_buffer(sample);
// Get the size of the frame
size = gst_buffer_get_size(buffer);
// Print the size
g_print("Frame size: %u bytes\n", size);
// Release the resources
gst_sample_unref(sample);
return GST_FLOW_OK;
}
static void pad_added_handler(GstElement *src, GstPad *new_pad, GstElement *sink) {
GstPad *sink_pad = gst_element_get_static_pad(sink, “sink”);
GstPadLinkReturn ret;
GstCaps *new_pad_caps = NULL;
GstStructure *new_pad_struct = NULL;
const gchar *new_pad_type = NULL;
g_print("Received new pad '%s' from '%s':\n", GST_PAD_NAME(new_pad), GST_ELEMENT_NAME(src));
// If our converter is already linked, we have nothing to do here
if (gst_pad_is_linked(sink_pad)) {
g_print("We are already linked. Ignoring.\n");
goto exit;
}
// Check the new pad's type
new_pad_caps = gst_pad_get_current_caps(new_pad);
new_pad_struct = gst_caps_get_structure(new_pad_caps, 0);
new_pad_type = gst_structure_get_name(new_pad_struct);
if (!g_str_has_prefix(new_pad_type, "video/x-raw")) {
g_print("It has type '%s' which is not raw video. Ignoring.\n", new_pad_type);
goto exit;
}
// Attempt the link
ret = gst_pad_link(new_pad, sink_pad);
if (GST_PAD_LINK_FAILED(ret)) {
g_print("Type is '%s' but link failed.\n", new_pad_type);
} else {
g_print("Link succeeded (type '%s').\n", new_pad_type);
}
exit:
if (new_pad_caps != NULL)
gst_caps_unref(new_pad_caps);
gst_object_unref(sink_pad);
}
int main(int argc, char *argv[]) {
gst_init(&argc, &argv);
GstElement *pipeline, *source, *convert, *filter, *capsfilter, *tee, *queue1, *scale, *videobalance, *appsink;
GstElement *queue2, *encoder, *muxer, *filesink;
GstBus *bus;
GstMessage *msg;
GstStateChangeReturn ret;
pipeline = gst_pipeline_new("webcam-pipeline");
source = gst_element_factory_make("v4l2src", "source");
convert = gst_element_factory_make("videoconvert", "convert");
// filter = gst_element_factory_make("capsfilter", "filter");
// capsfilter = gst_element_factory_make("capsfilter", "capsfilter");
tee = gst_element_factory_make("tee", "tee");
queue1 = gst_element_factory_make("queue", "queue1");
scale = gst_element_factory_make("videoscale", "scale");
videobalance = gst_element_factory_make("videobalance", "videobalance");
appsink = gst_element_factory_make("appsink", "appsink");
queue2 = gst_element_factory_make("queue", "queue2");
encoder = gst_element_factory_make("x264enc", "encoder");
muxer = gst_element_factory_make("mp4mux", "muxer");
filesink = gst_element_factory_make("filesink", "filesink");
if (!pipeline || !source || !convert || !tee || !queue1 || !scale || !videobalance || !appsink || !queue2 || !encoder || !muxer || !filesink) {
g_error("Failed to create elements.");
return -1;
}
g_object_set(filesink, "location", "output.mp4", NULL);
g_object_set(appsink, "emit-signals", TRUE, "sync", FALSE, NULL);
g_signal_connect(appsink, "new-sample", G_CALLBACK(new_sample_callback), NULL);
g_signal_connect(source, "pad-added", G_CALLBACK(pad_added_handler), convert);
gst_bin_add_many(GST_BIN(pipeline), source, convert, tee, queue1, scale, videobalance, appsink, queue2, encoder, muxer, filesink, NULL);
if (!gst_element_link_many(source, convert, NULL)) {
g_error("Failed to link source and convert elements.");
gst_object_unref(pipeline);
return -1;
}
if (!gst_element_link_many(convert, tee, NULL)) {
g_error("Failed to link convert and tee elements.");
gst_object_unref(pipeline);
return -1;
}
if (!gst_element_link_many(tee, queue1, NULL, NULL)) {
g_error("Failed to link tee to queue1.");
gst_object_unref(pipeline);
return -1;
}
if (!gst_element_link_many(queue1, scale, videobalance, appsink, NULL)) {
g_error("Failed to link elements inside queue1.");
gst_object_unref(pipeline);
return -1;
}
// Manually link tee to queue2
if (!gst_element_link_many(tee, queue2, NULL, NULL)) {
g_error("Failed to link tee to queue2.");
gst_object_unref(pipeline);
return -1;
}
// Connect recording elements to queue2
if (!gst_element_link_many(queue2, encoder, muxer, filesink, NULL)) {
g_error("Failed to link recording elements to queue2.");
gst_object_unref(pipeline);
return -1;
}
gst_element_set_state(pipeline, GST_STATE_PLAYING);
g_print("Pipeline state set to PLAYING\n");
// Create a main loop to keep the pipeline running
GMainLoop *main_loop = g_main_loop_new(NULL, FALSE);
g_main_loop_run(main_loop);
bus = gst_element_get_bus(pipeline);
msg = gst_bus_timed_pop_filtered(bus, GST_CLOCK_TIME_NONE,
(GstMessageType)(GST_MESSAGE_ERROR | GST_MESSAGE_EOS));
if (msg != NULL) {
GError *err;
gchar *debug_info;
switch (GST_MESSAGE_TYPE(msg)) {
case GST_MESSAGE_ERROR:
gst_message_parse_error(msg, &err, &debug_info);
g_printerr("Error received from element %s: %s\n", GST_OBJECT_NAME(msg->src), err->message);
g_printerr("Debugging information: %s\n", debug_info ? debug_info : "none");
g_clear_error(&err);
g_free(debug_info);
break;
case GST_MESSAGE_EOS:
g_print("End-Of-Stream reached.\n");
break;
default:
// We should not reach here
g_printerr("Unexpected message received.\n");
break;
}
gst_message_unref(msg);
}
// Clean up
gst_element_set_state(pipeline, GST_STATE_NULL);
gst_object_unref(pipeline);
g_main_loop_unref(main_loop);
return 0;
}
but i am getting ollowing error
0:00:00.358494657 4159 0x55cdbc2a40 WARN v4l2bufferpool gstv4l2bufferpool.c:814:gst_v4l2_buffer_pool_start:source:pool0:src Uncertain or not enough buffers, enabling copy threshold
0:00:01.023612456 4159 0x55cdbc2a40 WARN basesrc gstbasesrc.c:3127:gst_base_src_loop: error: Internal data stream error.
0:00:01.023830920 4159 0x55cdbc2a40 WARN basesrc gstbasesrc.c:3127:gst_base_src_loop: error: streaming stopped, reason not-linked (-1)
Error received from element source: Internal data stream error.
Debugging information: …/libs/gst/base/gstbasesrc.c(3127): gst_base_src_loop (): /GstPipeline:webcam-pipeline/GstV4l2Src:source:
also i tried running gstreamer pipeline from gstreamer-rockchip guide
gst-launch-1.0 v4l2src ! ‘video/x-raw,format=NV12’ ! tee name=tv ! queue !
mpph264enc ! ‘video/x-h264’ ! h264parse ! ‘video/x-h264’ ! filesink
location=/data/out.h264 tv. ! queue ! autovideosink
but it is also not working
could you help me out to achieve my desired piepline
Thanks in advance