#include #include static gboolean timeoutcall (GMainLoop *loop) { g_main_loop_quit(loop); return FALSE; } 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; } GstElement *m_pipeline; GstElement *m_audioGraph; GstElement *m_videoGraph; GstElement *m_datasource; GstElement *m_decodebin; GstElement *m_audioPipe; GstElement *m_videoPipe; GstElement *m_audioTee; GstElement *m_videoTee; GstElement *m_fakeAudioSink; GstElement *m_fakeVideoSink; static void on_pad_added (GstElement *element, GstPad *pad, gboolean last, gpointer data) { GstPad *sinkpad; GstElement *decoder = (GstElement *) data; /* We can now link this pad with the vorbis-decoder sink pad */ g_print ("Dynamic pad created, linking demuxer/decoder\n"); sinkpad = gst_element_get_static_pad (m_videoGraph, "sink"); GstPadLinkReturn x = gst_pad_link (pad, sinkpad); g_print("LINKED, return %d\n", x); if (x) { sinkpad = gst_element_get_static_pad (m_audioGraph, "sink"); GstPadLinkReturn x = gst_pad_link (pad, sinkpad); g_print("LINKED TRY2 to videograph, return %d\n", x); } gst_object_unref (sinkpad); } bool connectToFakeSink(GstElement *tee, GstElement *sink, GstElement *bin) { bool success = true; GstPad *sinkPad = gst_element_get_pad (sink, "sink"); if (GST_PAD_IS_LINKED (sinkPad)) { //This fakesink is already connected gst_object_unref (sinkPad); return true; } GstPad *srcPad = gst_element_get_request_pad (tee, "src%d"); gst_bin_add(GST_BIN(bin), sink); if (success) success = (gst_pad_link (srcPad, sinkPad) == GST_PAD_LINK_OK); if (success) success = (gst_element_set_state(sink, GST_STATE(bin)) != GST_STATE_CHANGE_FAILURE); gst_object_unref (srcPad); gst_object_unref (sinkPad); return success; } GstElement *conv, *sink; int main (int argc, char *argv[]) { GMainLoop *loop; // GstElement *pipeline, *source, *demuxer, *decoder, *conv, *sink; GstBus *bus; /* Initialisation */ gst_init (&argc, &argv); loop = g_main_loop_new (NULL, FALSE); /* Check input arguments */ if (argc != 2) { g_printerr ("Usage: %s \n", argv[0]); return -1; } m_pipeline = gst_pipeline_new (NULL); gst_object_ref (GST_OBJECT (m_pipeline)); gst_object_sink (GST_OBJECT (m_pipeline)); m_decodebin = gst_element_factory_make ("decodebin2", "DECODEBIN"); gst_bin_add(GST_BIN(m_pipeline), m_decodebin); m_audioGraph = gst_bin_new("AUDIOGRAPH"); gst_object_ref (GST_OBJECT (m_audioGraph)); gst_object_sink (GST_OBJECT (m_audioGraph)); m_audioPipe = gst_element_factory_make("queue", "AUDIOPIPE"); // g_object_set(G_OBJECT(m_audioPipe), "max-size-time", MAX_QUEUE_TIME, (const char*)NULL); gst_bin_add(GST_BIN(m_audioGraph), m_audioPipe); GstPad *audiopad = gst_element_get_pad (m_audioPipe, "sink"); gst_element_add_pad (m_audioGraph, gst_ghost_pad_new ("sink", audiopad)); gst_object_unref (audiopad); gst_bin_add(GST_BIN(m_pipeline), m_audioGraph); // Set up video graph m_videoGraph = gst_bin_new("VIDEOGRAPH"); gst_object_ref (GST_OBJECT (m_videoGraph)); gst_object_sink (GST_OBJECT (m_videoGraph)); m_videoPipe = gst_bin_new("VIDEOPIPE"); gst_bin_add(GST_BIN(m_videoGraph), m_videoPipe); GstElement *subtitles = gst_element_factory_make("subtitleoverlay", "SUBIOVERLAY"); GstElement *videoQueue = gst_element_factory_make("queue", "VIDEOQUEUE"); gst_bin_add_many(GST_BIN(m_videoPipe), subtitles, videoQueue, NULL); gst_element_link_pads(videoQueue, "src", subtitles, "video_sink"); // Link subtitle overlay and queue to container bin GstPad *videoPad = gst_element_get_pad(videoQueue, "sink"); gst_element_add_pad(m_videoPipe, gst_ghost_pad_new("sink", videoPad)); gst_object_unref(videoPad); GstPad *subtitlePad = gst_element_get_pad(subtitles, "subtitle_sink"); gst_element_add_pad(m_videoPipe, gst_ghost_pad_new("subtitle_sink", subtitlePad)); gst_object_unref(subtitlePad); GstPad *videoOutPad = gst_element_get_pad(subtitles, "src"); gst_element_add_pad(m_videoPipe, gst_ghost_pad_new("src", videoOutPad)); gst_object_unref(videoOutPad); // Link container bin to video graph bin videoPad = gst_element_get_pad(m_videoPipe, "sink"); gst_element_add_pad(m_videoGraph, gst_ghost_pad_new("sink", videoPad)); gst_object_unref(videoPad); subtitlePad = gst_element_get_pad(m_videoPipe, "subtitle_sink"); gst_element_add_pad(m_videoGraph, gst_ghost_pad_new("subtitle_sink", subtitlePad)); gst_object_unref(subtitlePad); gst_bin_add(GST_BIN(m_pipeline), m_videoGraph); // g_object_set(G_OBJECT(videoQueue), "max-size-time", MAX_QUEUE_TIME, (const char*)NULL); // Set some sensible default for the subs. 11pt is too tiny! g_object_set(G_OBJECT(subtitles), "font-desc", "sans-serif 22px", NULL); conv = gst_element_factory_make ("audioconvert", "converter"); sink = gst_element_factory_make ("autoaudiosink", "audio-output"); gst_element_link_many (conv, sink, NULL); g_signal_connect (m_decodebin, "new-decoded-pad", G_CALLBACK (on_pad_added), m_audioGraph); // g_signal_connect (m_decodebin, "pad-added", G_CALLBACK (on_pad_added), m_audioGraph); m_datasource = gst_element_make_from_uri(GST_URI_SRC, argv[1], (const char*)NULL); if (!m_datasource) return false; gst_bin_add(GST_BIN(m_pipeline), m_datasource); gst_element_link(m_datasource, m_decodebin); m_audioTee = gst_element_factory_make("tee", NULL); gst_object_ref (GST_OBJECT (m_audioTee)); gst_object_sink (GST_OBJECT (m_audioTee)); gst_bin_add(GST_BIN(m_audioGraph), m_audioTee); gst_element_link_pads(m_audioPipe, "src", m_audioTee, "sink"); // Fake audio sink to swallow unconnected audio pads m_fakeAudioSink = gst_element_factory_make("fakesink", NULL); g_object_set (G_OBJECT (m_fakeAudioSink), "sync", TRUE, (const char*)NULL); gst_object_ref (GST_OBJECT (m_fakeAudioSink)); gst_object_sink (GST_OBJECT (m_fakeAudioSink)); m_videoTee = gst_element_factory_make("tee", NULL); gst_object_ref (GST_OBJECT (m_videoTee)); gst_object_sink (GST_OBJECT (m_videoTee)); gst_bin_add(GST_BIN(m_videoGraph), m_videoTee); gst_element_link_pads(m_videoPipe, "src", m_videoTee, "sink"); // Fake video sink to swallow unconnected video pads m_fakeVideoSink = gst_element_factory_make("fakesink", NULL); g_object_set (G_OBJECT (m_fakeVideoSink), "sync", TRUE, (const char*)NULL); gst_object_ref (GST_OBJECT (m_fakeVideoSink)); gst_object_sink (GST_OBJECT (m_fakeVideoSink)); g_print("state %d\n", connectToFakeSink(m_audioTee, m_fakeAudioSink, m_audioGraph)); g_print("state %d\n", connectToFakeSink(m_videoTee, m_fakeVideoSink, m_videoGraph)); bus = gst_pipeline_get_bus (GST_PIPELINE (m_pipeline)); gst_bus_add_watch (bus, bus_call, loop); gst_object_unref (bus); /* Set the pipeline to "playing" state*/ g_print ("Now playing: %s\n", argv[1]); gst_element_set_state (m_pipeline, GST_STATE_PAUSED); g_print ("Running...\n"); g_timeout_add(1000, (GSourceFunc)timeoutcall, loop); g_main_loop_run (loop); gst_element_set_state (m_pipeline, GST_STATE_READY); g_timeout_add(5000, (GSourceFunc)timeoutcall, loop); g_main_loop_run (loop); gst_element_set_state (m_pipeline, GST_STATE_PAUSED); g_timeout_add(5000, (GSourceFunc)timeoutcall, loop); g_main_loop_run(loop); GstState statee, pending; GstStateChangeReturn y = gst_element_get_state(m_pipeline, &statee, &pending, 1000000000); return 0; _gst_debug_bin_to_dot_file (GST_BIN (m_pipeline), GST_DEBUG_GRAPH_SHOW_ALL, "ilmeeni.warning"); /* Out of the main loop, clean up nicely */ g_print ("Returned, stopping playback\n"); gst_element_set_state (m_pipeline, GST_STATE_NULL); g_print ("Deleting pipeline\n"); gst_object_unref (GST_OBJECT (m_pipeline)); return 0; }