Gstreamer cant read frames from rtsp

27 Views Asked by At

i try read frames from rtsp stream using gstreamer. i create rtsp stream using vlc

vlc -I dummy -vvv 480.mov --loop --sout '#rtp{sdp=rtsp://localhost:8080/test}'

if i try read it with gst-lauch, its play streamed video

/gst-launch-1.0 rtspsrc location=rtsp://localhost:8080/test ! decodebin ! autovideosink

so why my program doesnt work

#include <gst/gst.h>
#include <stdio.h>

// Define your RTSP URI here
#define RTSP_URI "rtsp://localhost:8080/test"

//vlc -I dummy -vvv 480.mov --loop --sout '#rtp{sdp=rtsp://localhost:8080/test}'

//gst-launch-1.0 rtspsrc location=rtsp://ip/url ! rtph264depay ! h264parse ! mp4mux ! filesink location=file.mp4
//gst-launch-1.0 -e rtspsrc location=rtsp://admin:[email protected]/rtsph2641080p protocols=tcp ! rtph264depay ! h264parse ! mp4mux ! filesink location=~/camera.mp4
//gst-launch-1.0 rtspsrc location=rtsp://localhost:8080/test ! decodebin !  autovideosink
//g++ -o rtsp_reader gsttest.cpp $(pkg-config --cflags --libs gstreamer-1.0 gstreamer-video-1.0)

static gboolean bus_call(GstBus *bus, GstMessage *msg, gpointer data) {

    GMainLoop *loop = (GMainLoop *) data;


    switch (GST_MESSAGE_TYPE(msg)) {
        case GST_MESSAGE_EOS:
            g_print("End of stream\n");
            g_main_loop_quit(loop);
            break;

        case GST_MESSAGE_ERROR: {
            gchar *debug;
            GError *error;

            gst_message_parse_error(msg, &error, &debug);
            g_free(debug);

            g_printerr("Error: %s\n", error->message);
            g_error_free(error);

            g_main_loop_quit(loop);
            break;
        }

        default:
            break;
    }

    return TRUE;
}

static GstFlowReturn new_frame(GstElement *sink, gpointer data) {
    GstSample *sample = NULL;
    GstBuffer *buffer = NULL;
    printf("frame \n");
    g_signal_emit_by_name(sink, "pull-sample", &sample);
    
    if (sample) {
        buffer = gst_sample_get_buffer(sample);
        if (buffer) {
            GstCaps *caps = gst_sample_get_caps(sample);
            gint width, height;
            GstStructure * strct = gst_caps_get_structure(caps, 0);
            gst_structure_get_int(strct, "width", &width);
            gst_structure_get_int(strct, "height", &height);
            g_print("Received frame - Width: %d, Height: %d\n", width, height);

        }
        gst_sample_unref(sample);
    }
    return GST_FLOW_OK;
}

int main(int argc, char *argv[]) {
    GstElement *pipeline, *source, *decoder, *sink;
    GstBus *bus;
    GMainLoop *loop;

    /* Initialize GStreamer */
    gst_init(&argc, &argv);
    loop = g_main_loop_new(NULL, FALSE);

    /* Create elements */
    pipeline = gst_pipeline_new("rtsp-pipeline");
    source = gst_element_factory_make("rtspsrc", "source");
    decoder = gst_element_factory_make("decodebin", "decoder");
    sink = gst_element_factory_make("appsink", "sink");

    if (!pipeline || !source || !decoder || !sink) {
        g_printerr("Not all elements could be created.\n");
        return -1;
    }

    /* Set the URI to play */
    g_object_set(source, "location", RTSP_URI, NULL);

    /* Build the pipeline */
    gst_bin_add_many(GST_BIN(pipeline), source, decoder, sink, NULL);
    gst_element_link(source, decoder);
    gst_element_link(decoder, sink);


    /* Set the bus */
    bus = gst_pipeline_get_bus(GST_PIPELINE(pipeline));
    gst_bus_add_watch(bus, bus_call, loop);
    gst_object_unref(bus);

    /* Set the new sample callback on the sink */
    g_signal_connect(sink, "new-sample", G_CALLBACK(new_frame), NULL);

    /* Start playing */
    g_print("Playing: %s\n", RTSP_URI);
    gst_element_set_state(pipeline, GST_STATE_PLAYING);

    /* Iterate */
    g_main_loop_run(loop);

    /* Out of the main loop, clean up nicely */
    g_print("Returned, stopping playback\n");
    gst_element_set_state(pipeline, GST_STATE_NULL);

    gst_object_unref(GST_OBJECT(pipeline));
    g_main_loop_unref(loop);

    return 0;
}


compiled by command g++ -o rtsp_reader gsttest.cpp $(pkg-config --cflags --libs gstreamer-1.0 gstreamer-video-1.0)

then run and ends with

/rtsp_reader
Playing: rtsp://localhost:8080/test
Error: Internal data stream error.
Returned, stopping playback
0

There are 0 best solutions below