Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // testpipeline.c
- // create a dummy pipeline to view the camera feed
- //
- #include <gst/gst.h>
- GMainLoop *main_loop;
- static gboolean my_bus_callback (GstBus *bus, GstMessage *msg, gpointer data) {
- switch (GST_MESSAGE_TYPE (msg)) {
- case GST_MESSAGE_ERROR: {
- GError *err;
- gchar *debug;
- gst_message_parse_error (msg, &err, &debug);
- g_print ("Error: %s\n", err->message);
- g_error_free (err);
- g_free (debug);
- g_main_loop_quit (main_loop);
- break;
- }
- }
- return TRUE;
- }
- GstElement *__video_tee;
- gboolean build_video_source (GstElement *pipeline) {
- // build the pipeline that goes from source to tee
- g_debug ("Building video source ...");
- GstElement *bin = gst_bin_new ("video-source");
- GstElement *src = gst_element_factory_make ("v4l2src", NULL);
- GstElement *caps = gst_element_factory_make ("capsfilter", NULL);
- GstElement *col = gst_element_factory_make ("ffmpegcolorspace", NULL);
- if (!src || !caps || !col) {
- g_warning ("Failed to create one of the elements for basic video source.");
- gst_object_unref (bin);
- gst_object_unref (src);
- gst_object_unref (caps);
- gst_object_unref (col);
- return FALSE;
- }
- GstCaps *c = gst_caps_new_simple ("video/x-raw-yuv",
- "width", G_TYPE_INT, 640,
- "height", G_TYPE_INT, 480,
- "framerate", GST_TYPE_FRACTION, 30, 1,
- NULL);
- g_object_set (G_OBJECT (caps), "caps", c, NULL);
- gst_caps_unref (c);
- g_debug ("Attaching source elements ...");
- gst_bin_add_many (GST_BIN(bin), src, caps, col, NULL);
- if (!gst_element_link_many (src, caps, col, NULL)) {
- g_warning ("Failed to link video source elements.");
- gst_object_unref (bin);
- return FALSE;
- }
- GstPad *colspace_src = gst_element_get_static_pad (col, "src");
- g_assert (colspace_src);
- // create our ghost pad for the outside world
- GstPad *gp = gst_ghost_pad_new ("src", colspace_src);
- if (!gp) {
- g_warning ("Failed to create ghost pad for video source.");
- gst_object_unref (colspace_src);
- gst_object_unref (bin);
- }
- gst_object_unref (colspace_src);
- // Now create our tee
- __video_tee = gst_element_factory_make ("tee", NULL);
- if (!__video_tee) {
- g_warning ("Failed to create splitter tee.");
- gst_object_unref (bin);
- return FALSE;
- }
- GstPad *tee_sink = gst_element_get_static_pad (__video_tee, "sink");
- if (!tee_sink) {
- g_warning ("Failed to query video splitter tee for sink pad.");
- gst_object_unref (bin);
- return FALSE;
- }
- gst_bin_add_many (GST_BIN(pipeline), bin, __video_tee, NULL);
- if (GST_PAD_LINK_FAILED (gst_pad_link (gp, tee_sink))) {
- g_warning ("Failed to attach video source to splitter tee.");
- gst_bin_remove_many (GST_BIN (pipeline), bin, __video_tee, NULL);
- gst_object_unref (bin);
- gst_object_unref (__video_tee);
- return FALSE;
- }
- return TRUE;
- }
- gboolean attach_video_view (GstElement *pipeline) {
- g_debug ("Attaching video view ...");
- GstElement *view_bin = gst_bin_new ("view-bin");
- GstElement *sink = gst_element_factory_make ("xvimagesink", NULL);
- GstElement *col = gst_element_factory_make ("ffmpegcolorspace", NULL);
- GstElement *queue = gst_element_factory_make ("queue", NULL);
- if (!sink || !col || !queue) {
- g_warning ("Failed to create basic viewer elements.");
- gst_object_unref (sink);
- gst_object_unref (col);
- gst_object_unref (queue);
- return FALSE;
- }
- gst_bin_add_many (GST_BIN (view_bin), queue, col, sink, NULL);
- if (!gst_element_link_many (queue, col, sink, NULL)) {
- g_warning ("Failed to link viewer elements.");
- gst_object_unref (view_bin);
- return FALSE;
- }
- gst_bin_add_many (GST_BIN (pipeline), view_bin, NULL);
- GstPad *queue_sink = gst_element_get_static_pad (queue, "sink");
- g_assert (queue_sink != NULL);
- GstPad *gp = gst_ghost_pad_new ("src", queue_sink);
- gst_object_unref (queue_sink);
- // Now request a new pad from the tee
- GstPad *tee_src = gst_element_get_request_pad (__video_tee, "src%d");
- g_assert (tee_src);
- if (GST_PAD_LINK_FAILED (gst_pad_link (tee_src, gp))) {
- g_warning ("Failed to link tee source to viewer pipeline");
- return FALSE;
- }
- // otherwise we're good!
- }
- int main (int argc, char *argv[]) {
- // make a dummy pipeline and run it
- //
- g_debug ("Starting up ...");
- main_loop = g_main_loop_new (NULL, FALSE);
- gst_init (&argc, &argv);
- g_debug ("Building pipeline ...");
- GstElement *pipeline = gst_pipeline_new ("mypipeline");
- if (!build_video_source (pipeline)) {
- g_warning ("Failed to build video source.");
- gst_object_unref (pipeline);
- return -1;
- }
- if (!attach_video_view (pipeline)) {
- g_warning ("Failed to build video viewer.");
- gst_object_unref (pipeline);
- return -1;
- }
- g_debug ("Attaching hooks to pipeline ...");
- GstBus *bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
- gst_bus_add_watch (bus, my_bus_callback, NULL);
- gst_object_unref (bus);
- g_debug ("Done building pipeline. Starting up ...");
- gst_element_set_state (pipeline, GST_STATE_PLAYING);
- g_debug ("Starting main loop ..");
- g_main_loop_run (main_loop);
- gst_element_set_state (pipeline, GST_STATE_NULL);
- gst_object_unref (pipeline);
- g_main_loop_unref (main_loop);
- return 0;
- }
Add Comment
Please, Sign In to add comment