#include #include #include static gboolean cintr = FALSE; static gboolean seek_flag = TRUE; static GMainLoop *loop; static GstElement *pipeline, *source, *demux, *queue, *parser, *decoder, *sink; static gint timeout_id; static void pad_added_handler (GstElement *demux, GstPad *new_pad, gpointer *data); static guint overlay_x_pos; static guint overlay_y_pos; static guint overlay_width; static guint overlay_height; 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; } case GST_MESSAGE_APPLICATION: { const GstStructure *s; s = gst_message_get_structure (msg); if (gst_structure_has_name (s, "NvGstAppInterrupt")) { g_print ("Terminating the pipeline ...\n"); g_main_loop_quit (loop); } } break; default: break; } return TRUE; } static gboolean check_for_interrupt (gpointer data) { if (cintr) { cintr = FALSE; gst_element_post_message (GST_ELEMENT (pipeline), gst_message_new_application (GST_OBJECT (pipeline), gst_structure_new ("NvGstAppInterrupt", "message", G_TYPE_STRING, "Pipeline interrupted", NULL))); return FALSE; } return TRUE; } static void _intr_handler (int signum) { struct sigaction action; g_print ("User Interrupted.. \n"); memset (&action, 0, sizeof (action)); action.sa_handler = SIG_DFL; sigaction (SIGINT, &action, NULL); cintr = TRUE; } static void _intr_setup (void) { struct sigaction action; memset (&action, 0, sizeof (action)); action.sa_handler = _intr_handler; sigaction (SIGINT, &action, NULL); } static gboolean change_overlay_pos (gpointer data) { GstElement *sink = (GstElement *) data; if (seek_flag) { overlay_x_pos += 10; overlay_y_pos += 10; overlay_width += 20; overlay_height += 20; g_print (":------------> Now Overlay Pos : x = %d y = %d w = %d h = %d\n",overlay_x_pos,overlay_y_pos,overlay_width,overlay_height); g_object_set (sink, "overlay-x", overlay_x_pos, NULL); g_object_set (sink, "overlay-y", overlay_y_pos, NULL); g_object_set (sink, "overlay-w", overlay_width, NULL); g_object_set (sink, "overlay-h", overlay_height, NULL); //seek_flag = FALSE; seek_flag = TRUE; } return TRUE; } /* This function will be called by the pad-added signal */ static void pad_added_handler (GstElement *demux, GstPad *new_pad, gpointer *data) { GstElement *queue = (GstElement *) data; GstPad *sink_pad_video = gst_element_get_static_pad (queue, "sink"); GstPadLinkReturn ret; GstCaps *new_pad_caps = NULL; GstStructure *new_pad_struct = NULL; new_pad_caps = gst_pad_query_caps (new_pad, NULL); new_pad_struct = gst_caps_get_structure (new_pad_caps, 0); const gchar *name = gst_structure_get_name (new_pad_struct); if (!strncmp (name, "video", 5)) { ret = gst_pad_link (new_pad, sink_pad_video); if (GST_PAD_LINK_FAILED (ret)) { g_print (" Type video link failed.\n"); } } if (new_pad_caps != NULL) gst_caps_unref (new_pad_caps); gst_object_unref (sink_pad_video); } int main (int argc, char *argv[]) { GstBus *bus; guint bus_watch_id; guint overlay_index = 1; guint display_index = 1; overlay_x_pos = 0; overlay_y_pos = 0; overlay_width = 1280; overlay_height = 720; /* Initialisation */ gst_init (&argc, &argv); _intr_setup (); g_timeout_add (400, check_for_interrupt, NULL); loop = g_main_loop_new (NULL, FALSE); /* gst-launch-1.0 filesrc location=filename.mp4 ! qtdemux ! queue ! h264parse ! omxh264dec ! nvoverlaysink display-id=1 overlay-x=100 overlay-y=100 overlay-w=640 overlay-h=480 -v -e */ /* Create gstreamer elements */ pipeline = gst_pipeline_new ("overlay-pos-test-app"); source = gst_element_factory_make ("filesrc", "my_filesrc"); demux = gst_element_factory_make ("qtdemux", "my_demux"); queue = gst_element_factory_make ("queue", "my_queue"); parser = gst_element_factory_make ("h264parse", "my_parser"); decoder = gst_element_factory_make ("omxh264dec", "my_dec_h264"); sink = gst_element_factory_make ("nvoverlaysink", "my_sink"); if (!pipeline || !source || !demux || !parser || !queue || !decoder || !sink) { g_printerr ("One element could not be created. Exiting.\n"); return -1; } timeout_id = g_timeout_add (5000, change_overlay_pos, sink); g_object_set (sink, "display-id", display_index, NULL); g_object_set (sink, "overlay", overlay_index, NULL); g_object_set (sink, "overlay-x", overlay_x_pos, NULL); g_object_set (sink, "overlay-y", overlay_y_pos, NULL); g_object_set (sink, "overlay-w", overlay_width, NULL); g_object_set (sink, "overlay-h", overlay_height, NULL); /* Set up the pipeline */ /* we add a message handler */ bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline)); bus_watch_id = gst_bus_add_watch (bus, bus_call, loop); gst_object_unref (bus); g_object_set (G_OBJECT (source), "location", argv[1], NULL); /* we add all elements into the pipeline */ gst_bin_add_many (GST_BIN (pipeline), source, demux, queue, parser, decoder, sink, NULL); /* we link the elements together */ gst_element_link (source, demux); if (!gst_element_link_many (queue, parser, decoder, sink, NULL)) { g_print ("Failed to link one or more elements!\n"); return -1; } g_signal_connect (demux, "pad-added", G_CALLBACK (pad_added_handler), queue); /* Set the pipeline to "playing" state*/ g_print ("Now playing:\n"); gst_element_set_state (pipeline, GST_STATE_PLAYING); /* Iterate */ g_print ("Running...\n"); g_main_loop_run (loop); /* Out of the main loop, clean up nicely */ g_print ("Returned, stopping playback\n"); if (GST_STATE_CHANGE_FAILURE == gst_element_set_state (pipeline, GST_STATE_NULL)) { g_print ("can't set camera pipeline to null\n"); } g_print ("Deleting pipeline\n"); gst_object_unref (GST_OBJECT (pipeline)); g_source_remove (bus_watch_id); g_main_loop_unref (loop); return 0; }