I used gstreamer to create a code to obtain the rtsp stream, and then used appsink to obtain it frame by frame.
After I run it, it prompts that there is a problem with TLS. I don’t understand this problem.
#include <gst/gst.h>
#include <gst/app/gstappsink.h>
#include <string.h>
#include <cstdio>
#include <gtk-3.0/gtk/gtk.h>
#define RTSP_PATH "rtsp://admin:[email protected]:554"
#define CAPS "video/x-raw,format=RGB,pixel-aspect-ratio=1/1"
GST_DEBUG_CATEGORY_STATIC (my_category);
int i;
typedef struct _CustomData {
GstElement *pipeline;
GstElement *source;
GstElement *depayloader;
GstElement *avdec;
GstElement *convert1, *convert2;
GstElement *scale;
GstElement *tee, *sink, *appsink;
GstElement *capture_queue, *video_queue;
GstMapInfo map_info;
int Width = 640, Height = 480;
bool capture_flag = true;
GMainLoop *main_loop;
} CustomData;
/* 这个是appsink的信号new-sample的回调函数,表示已经接收到一帧数据 */
static GstFlowReturn new_sample (GstElement *sink, CustomData *data){
/* 我这里控制输出1分钟,如果不需要可以自行修改代码 */
if(i >= 30*60){
g_main_loop_quit (data->main_loop);
return GST_FLOW_OK;
}
i++;
GstSample *sample;
GstBuffer *buffer;
GstCaps *caps;
GstStructure *s;
gint width, height;
/* Retrieve the buffer */
g_signal_emit_by_name (sink, "pull-sample", &sample);
if (sample){
caps = gst_sample_get_caps (sample);
if (!caps) {
g_print ("gst_sample_get_caps failn");
gst_sample_unref (sample);
return GST_FLOW_ERROR;
}
s = gst_caps_get_structure (caps, 0);
gboolean res;
res = gst_structure_get_int (s, "width", &width);
res |= gst_structure_get_int (s, "height", &height);
if (!res) {
g_print ("gst_structure_get_int failn");
gst_sample_unref (sample);
return GST_FLOW_ERROR;
}
buffer = gst_sample_get_buffer (sample);
if(!buffer){
g_print ("gst_sample_get_buffer failn");
gst_sample_unref (sample);
return GST_FLOW_ERROR;
}
GstMapInfo map;
char name[32] = {0};
sprintf(name,"frame%d.png",i);
if (gst_buffer_map (buffer, &map, GST_MAP_READ)){
/* save the buffer as a png file */
GdkPixbuf * pixbuf = gdk_pixbuf_new_from_data (map.data,GDK_COLORSPACE_RGB, FALSE, 8, width, height,GST_ROUND_UP_4 (width * 3), NULL, NULL);
gdk_pixbuf_save (pixbuf, name, "png", NULL, NULL);
g_object_unref(pixbuf);
gst_buffer_unmap (buffer, &map);
}
gst_sample_unref (sample);
GST_DEBUG_BIN_TO_DOT_FILE(GST_BIN(data->pipeline), GST_DEBUG_GRAPH_SHOW_ALL, "capture");
return GST_FLOW_OK;
}
return GST_FLOW_OK ;
}
/* 这个是pad-added信号的回调函数 */
static void pad_added_handler (GstElement *src, GstPad *new_pad, CustomData *data){
g_print ("Received new pad '%s' from '%s':n", GST_PAD_NAME (new_pad), GST_ELEMENT_NAME (src));
GstPad *sink_pad = gst_element_get_static_pad (data->depayloader, "sink");
/* 如果depayloader已经连接好了,那就忽略 */
if (gst_pad_is_linked (sink_pad)) {
g_print ("We are already linked. Ignoring.n");
goto exit;
}
GstCaps *new_pad_caps;
GstStructure *new_pad_struct;
const gchar *new_pad_type;
/* 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, "application/x-rtp")) {
g_print ("It has type '%s' which is not application/x-rtp. Ignoring.n", new_pad_type);
goto exit;
}
GstPadLinkReturn ret;
/* 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:
/* Unreference the new pad's caps, if we got them */
if (new_pad_caps != NULL)
gst_caps_unref (new_pad_caps);
/* Unreference the sink pad */
gst_object_unref (GST_OBJECT(sink_pad));
}
/* This function is called when an error message is posted on the bus */
static void error_cb (GstBus *bus, GstMessage *msg, CustomData *data) {
GError *err;
gchar *debug_info;
/* Print error details on the screen */
gst_message_parse_error (msg, &err, &debug_info);
g_printerr ("Error received from element %s: %sn", GST_OBJECT_NAME (msg->src), err->message);
g_printerr ("Debugging information: %sn", debug_info ? debug_info : "none");
g_clear_error (&err);
g_free (debug_info);
g_main_loop_quit (data->main_loop);
}
int main(int argc, char *argv[]){
g_setenv("GST_DEBUG_DUMP_DOT_DIR", "/home/cht/test", TRUE);
GST_DEBUG_CATEGORY_INIT (my_category, "my category", 0, "This is my very own");
i = 0;
CustomData data;
memset (&data, 0, sizeof (data));
/* Initialize GStreamer */
gst_init (&argc, &argv);
GstPad *tee_capture_pad, *tee_video_pad;
GstPad *queue_capture_pad, *queue_video_pad;
data.source = gst_element_factory_make ("rtspsrc", "source");
data.depayloader = gst_element_factory_make ("rtph264depay", "depayloader");
data.avdec = gst_element_factory_make ("avdec_h264", "avdec");
data.convert1 = gst_element_factory_make ("videoconvert", "convert1");
data.convert2 = gst_element_factory_make ("videoconvert", "convert2");
data.scale = gst_element_factory_make ("videoscale", "scale");
data.tee = gst_element_factory_make ("tee", "tee");
data.video_queue = gst_element_factory_make ("queue", "video_queue");
data.capture_queue = gst_element_factory_make ("queue", "capture_queue");
data.appsink = gst_element_factory_make ("appsink", "sink");
data.sink = gst_element_factory_make ("ximagesink", "videosink");
data.pipeline = gst_pipeline_new ("test-pipeline");
if (!data.pipeline || !data.source || !data.depayloader || !data.avdec || !data.convert1 || !data.scale || !data.sink) {
g_printerr("Not all elements could be created.n");
return -1;
}
g_object_set (data.source, "location", RTSP_PATH, NULL);
GstCaps *video_caps;
gchar *video_caps_text;
video_caps_text = g_strdup_printf (CAPS);
video_caps = gst_caps_from_string (video_caps_text);
if(!video_caps){
g_printerr("gst_caps_from_string failn");
return -1;
}
g_object_set (data.appsink, "emit-signals", TRUE, "caps", video_caps, NULL);
g_signal_connect (data.appsink, "new-sample", G_CALLBACK (new_sample), &data);
gst_caps_unref (video_caps);
g_free (video_caps_text);
g_signal_connect (data.source, "pad-added", G_CALLBACK (pad_added_handler), &data);
gst_bin_add_many (GST_BIN (data.pipeline), data.source,data.depayloader,data.avdec,data.tee,data.capture_queue,data.video_queue,data.convert1,data.convert2,data.scale,data.sink,data.appsink, NULL);
if (gst_element_link_many (data.depayloader, data.avdec, data.tee, NULL) != TRUE ||
gst_element_link_many (data.capture_queue, data.convert1, data.appsink, NULL) != TRUE ||
gst_element_link_many (data.video_queue, data.convert2,data.scale, data.sink, NULL) != TRUE) {
g_printerr ("Elements could not be linked.n");
gst_object_unref (data.pipeline);
return -1;
}
tee_capture_pad = gst_element_get_request_pad (data.tee, "src_%u");
g_print ("Obtained request pad %s for audio branch.n", gst_pad_get_name (tee_capture_pad));
queue_capture_pad = gst_element_get_static_pad (data.capture_queue, "sink");
tee_video_pad = gst_element_get_request_pad(data.tee, "src_%u");
g_print ("Obtained request pad %s for video branch.n", gst_pad_get_name (tee_video_pad));
queue_video_pad = gst_element_get_static_pad (data.video_queue, "sink");
if (gst_pad_link (tee_capture_pad, queue_capture_pad) != GST_PAD_LINK_OK ||
gst_pad_link (tee_video_pad, queue_video_pad) != GST_PAD_LINK_OK) {
g_printerr ("Tee could not be linked.n");
gst_object_unref (data.pipeline);
return -1;
}
gst_object_unref (queue_capture_pad);
gst_object_unref (queue_video_pad);
GstBus *bus;
bus = gst_element_get_bus (data.pipeline);
gst_bus_add_signal_watch (bus);
g_signal_connect (G_OBJECT (bus), "message::error", (GCallback)error_cb, &data);
gst_element_set_state (data.pipeline, GST_STATE_PLAYING);
GST_DEBUG_BIN_TO_DOT_FILE(GST_BIN(data.pipeline), GST_DEBUG_GRAPH_SHOW_ALL, "pipeline");
data.main_loop = g_main_loop_new (NULL, FALSE);
g_main_loop_run (data.main_loop);
gst_element_set_state (data.pipeline, GST_STATE_NULL);
gst_object_unref (GST_OBJECT(data.pipeline));
return 0;
}
I compiled this code with gcc. After the compilation was successful, an error was reported:(Gstream_test:1695898): GStreamer-WARNING **: 14:24:50.660: Failed to load plugin ‘/usr/lib/aarch64-linux-gnu/gstreamer-1.0/libgstlibav.so’: /lib/aarch64-linux-gnu/libgomp.so.1: cannot allocate memory in static TLS block
Not all elements could be created.
What should I do to eliminate this error so that the code can run and get the rtsp stream?
jack is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.