【发布时间】:2015-03-27 15:19:51
【问题描述】:
我一直在关注许多关于将图像推送到 Gstreamer 管道的示例,但我仍然无法使我的代码正常工作。
任何建议(除了告诉我尝试使用 Gstreamer1.0 而不是 0.10)将不胜感激。我想了解以下脚本中有什么问题,该脚本为appsrc 元素提供 jpeg 图像。稍后我将使用相同的代码来提供从相机获取的 openCv 图像,但首先我想通过使这个简单的示例工作来了解基础知识。
#include <gst/gst.h>
#include <string.h>
#include <cv.h>
#include <highgui.h>
#include <stdio.h>
#define VIDEO_CAPS "video/x-raw-rgb,bpp=8,depth=8,width=640,height=360,framerate=5/1,red_mask=224,green_mask=28,blue_mask=3,endianness=1234;"
/* Structure to contain all our information, so we can pass it to callbacks */
guint64 imagecounter=1;
typedef struct _CustomData {
GstElement *pipeline, *app_source;
GstElement *video_convert, *video_sink;
GstElement *image_manage;
guint64 num_samples; /* Number of samples generated so far (for timestamp generation) */
guint sourceid; /* To control the GSource */
GMainLoop *main_loop; /* GLib's Main Loop */
} CustomData;
/* This method is called by the idle GSource in the mainloop, to feed CHUNK_SIZE bytes into appsrc.
* The idle handler is added to the mainloop when appsrc requests us to start sending data (need-data signal)
* and is removed when appsrc has enough data (enough-data signal).
*/
static gboolean push_data (CustomData *data) {
GstBuffer *buffer;
GstFlowReturn ret;
int i;
gint8 *raw;
gint num_samples;
gfloat freq;
int size,depth,height,width,step,channels;
guchar *data1;
IplImage* img;
g_print("push_data is beginning\n");
img=cvLoadImage("frame1.jpg",CV_LOAD_IMAGE_COLOR);
height = img->height;
width = img->width;
step = img->widthStep;
channels = img->nChannels;
depth = img->depth;
data1 = (guchar *)img->imageData;
num_samples = height*width*channels;
g_print("height %d\n",height);
g_print("width %d\n",width);
g_print("widthStep %d\n",step);
g_print("nChannels %d\n",channels);
g_print("depth %d\n",depth);
g_print("image_number %" G_GUINT64_FORMAT "\n", imagecounter);
g_print("sizeof guchar: %lu\n",sizeof(guchar));
/* Create a new empty buffer */
buffer = gst_buffer_new_and_alloc (num_samples); // guint size :the size in bytes of the new buffer's data.
size = GST_BUFFER_SIZE(buffer);
g_print("nSize %d\n",size);
g_print("buffer initialized\n");
/* Set its timestamp and duration */
GST_BUFFER_TIMESTAMP (buffer) = gst_util_uint64_scale (imagecounter, GST_SECOND, 5 );
imagecounter += 1;
GST_BUFFER_DURATION (buffer) = gst_util_uint64_scale (num_samples, GST_SECOND,5);
memcpy( (guchar *)GST_BUFFER_DATA( buffer ), data1, GST_BUFFER_SIZE( buffer ) );
g_print("image data copied into buffer\n");
data->num_samples += num_samples;
/* Push the buffer into the appsrc */
g_signal_emit_by_name (data->app_source, "push-buffer", buffer, &ret);
g_print("buffer pushed\n");
/* Free the buffer now that we are done with it */
gst_buffer_unref (buffer);
g_print("buffer unreferenced\n");
if (ret != GST_FLOW_OK) {
g_printerr("something wrong in sending data");
/* We got some error, stop sending data */
return FALSE;
}
g_print("push_data is ending\n");
return TRUE;
}
/* This signal callback triggers when appsrc needs data. Here, we add an idle handler
* to the mainloop to start pushing data into the appsrc */
static void start_feed (GstElement *source, guint size, CustomData *data) {
if (data->sourceid == 0) {
g_print ("Start feeding\n");
data->sourceid = g_idle_add ((GSourceFunc) push_data, data);
g_print ("push_data started\n");
}
}
/* This callback triggers when appsrc has enough data and we can stop sending.
* We remove the idle handler from the mainloop */
static void stop_feed (GstElement *source, CustomData *data) {
if (data->sourceid != 0) {
g_print ("Stop feeding\n");
g_source_remove (data->sourceid);
data->sourceid = 0;
}
}
/* 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: %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);
g_main_loop_quit (data->main_loop);
}
int main(int argc, char *argv[]) {
CustomData data;
gchar *video_caps_text;
GstCaps *video_caps;
GstBus *bus;
/* Initialize cumstom data structure */
memset (&data, 0, sizeof (data));
/* Initialize GStreamer */
gst_init (&argc, &argv);
/* Create the elements */
data.app_source = gst_element_factory_make ("appsrc", "video_source");
data.video_convert = gst_element_factory_make ("ffmpegcolorspace", "csp");
data.video_sink = gst_element_factory_make ("v4l2sink", "video_sink");
data.image_manage= gst_element_factory_make("imagefreeze","image_manage");
/* Create the empty pipeline */
data.pipeline = gst_pipeline_new ("test-pipeline");
if (!data.pipeline || !data.app_source || !data.video_convert || !data.video_sink || !data.image_manage) {
g_printerr ("Not all elements could be created.\n");
return -1;
}
/* Configure appsrc */
video_caps_text = g_strdup_printf (VIDEO_CAPS);
video_caps = gst_caps_from_string (video_caps_text);
if( !GST_IS_CAPS(video_caps) ) {
g_printerr("Error creating Caps for OpenCV-Source, exiting...");
exit( 1 );
}
g_object_set (data.app_source, "caps", video_caps, NULL);
g_signal_connect (data.app_source, "need-data", G_CALLBACK (start_feed), &data);
g_signal_connect (data.app_source, "enough-data", G_CALLBACK (stop_feed), &data);
/* Configure v4l2loopback videosink*/
//g_object_set (data.video_sink, "device", "/dev/video0", NULL);
/* Link all elements that can be automatically linked because they have "Always" pads */
gst_bin_add_many (GST_BIN (data.pipeline), data.app_source, data.image_manage, data.video_convert, data.video_sink, NULL);
if ( gst_element_link_many ( data.app_source, data.video_convert,data.video_sink, NULL) != TRUE) {
g_printerr ("Elements could not be linked.\n");
gst_object_unref (data.pipeline);
return -1;}
/*
/* Instruct the bus to emit signals for each received message, and connect to the interesting signals */
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_object_unref (bus);
/* Start playing the pipeline */
gst_element_set_state (data.pipeline, GST_STATE_PLAYING);
/* Create a GLib Main Loop and set it to run */
data.main_loop = g_main_loop_new (NULL, FALSE);
g_main_loop_run (data.main_loop);
/* Free resources */
gst_element_set_state (data.pipeline, GST_STATE_NULL);
gst_object_unref (data.pipeline);
return 0;
}
发送几个缓冲区后,我收到以下输出:
Error received from element video_source: Internal data flow error.
Debugging information: gstbasesrc.c(2625): gst_base_src_loop (): /GstPipeline:test-pipeline/GstAppSrc:video_source:
streaming task paused, reason not-negotiated (-4)
【问题讨论】:
-
您是否尝试将
appsrc大写更改为image/jpeg, height=360, width=640, framerate=5/1,然后使用jpegparse和jpegdec解析和解码jpeg,并将其链接到ffmpegcolorspace。另外,如果你想显示,只需使用autovideosink而不是v4l2sink。 -
感谢本杰明!添加 jpegparse 和 jpegdec 元素后,我不再收到该错误,并且缓冲区被毫无问题地推送。但即使我使用 autovideosink,我仍然无法想象该图像!当我运行程序时,没有窗口出现!有什么进一步的建议吗?顺便说一句,我使用 v4l2sink 是因为它对于我的最终目的是必要的,但我可以稍后再处理它。
-
我尝试在调用 gst_element_set_state (data.pipeline, GST_STATE_PLAYING) 的返回值上添加一个控件;该值与预期的 GST_STATE_CHANGE_SUCCESS 不同。所以我想我的问题是管道无法设置为播放状态。但我不知道为什么。如果所有这些问题都很愚蠢,我很抱歉,但我对 Gstreamer 一点也不熟悉..
-
下一步就是开启gstreamer调试。运行时将
GST_DEBUG=*:5放在可执行文件的字体中,以从所有元素中获取调试输出,从而帮助您缩小范围。 -
除了尝试使用调试器的建议之外,有没有人可以链接到一些有用的示例,我可以从中学习?我也可以切换到 Gstreamer1.0,因为我发现 Gstreamer 开发人员通过了从 0.10 到 1.0 的所有教程代码。谢谢