/* * OggAudio.c * Test program to practice gstreamer */ #include #include 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 *err; gst_message_parse_error (msg, &err, &debug); g_free (debug); g_print ("Error: %s\n", err->message); g_error_free (err); g_main_loop_quit (loop); break; } default: break; } return TRUE; } static void new_pad(GstElement *element, GstPad *pad, gpointer data) { GstPad *sinkpad; gchar *cstr; const gchar* name; GstElement *audioq, *videoq; GstElement *pipeline = (GstElement*) data; GstCaps *caps = gst_pad_get_caps(pad); GstStructure *stru = gst_caps_get_structure(caps, 0); cstr = gst_caps_to_string(caps); name = gst_structure_get_name(stru); videoq = gst_bin_get_by_name (GST_BIN (pipeline), "videoq"); audioq = gst_bin_get_by_name(GST_BIN(pipeline), "audioq"); g_print ("caps are %s\n 0th is:{%s}\nname is:%s", cstr, gst_structure_to_string(stru), name); if (strcmp(name, "video/mpeg") == 0) sinkpad = gst_element_get_pad(videoq, "sink"); else if (strcmp(name, "audio/mpeg") == 0) sinkpad = gst_element_get_pad(audioq, "sink"); if (!gst_pad_is_linked(pad)) { gchar* srcname = gst_pad_get_name(pad); gchar* sinkname = gst_pad_get_name(sinkpad); GstPadLinkReturn stat; g_print("can_link %s to %s returned %s\n", srcname, sinkname, gst_pad_can_link(pad, sinkpad) ? "YES!": "NO!!"); gst_element_set_state(pipeline, GST_STATE_PAUSED); stat = gst_pad_link (pad, sinkpad); gst_element_set_state (pipeline, GST_STATE_PLAYING); g_print("Link status:%d\tReset to Playing\n", stat); } gst_caps_unref(caps); // gst_object_unref (sinkpad); } int mpegVideo(int argc, char* argv[]) { GMainLoop *loop; GstBus *bus; GstElement *pipeline, *source, *demux, *vdecoder, *vsink, *vconv; GstElement *adecoder, *aconv, *asink; GstElement *audioq, *videoq; loop = g_main_loop_new(NULL, FALSE); /* create the elements */ pipeline = gst_pipeline_new ("mpeg-player"); source = gst_element_factory_make ("filesrc", "file-source"); demux = gst_element_factory_make ("mpegdemux", "ffdemux-mpegts"); audioq = gst_element_factory_make("queue", "audioq"); videoq = gst_element_factory_make("queue", "videoq"); vdecoder = gst_element_factory_make ("ffdec_mpegvideo", "mpeg2-vdecoder"); vconv = gst_element_factory_make("ffmpegcolorspace", "colorconv"); adecoder = gst_element_factory_make("ffdec_mp3", "mpeg2-adecoder"); aconv = gst_element_factory_make ("audioconvert", "converter"); asink = gst_element_factory_make ("directsoundsink", "audio-output"); vsink = gst_element_factory_make("directdrawsink", "video-output"); if (!pipeline || !source || !demux || !audioq || !videoq || !vdecoder || !vconv || !adecoder || !aconv || !asink || !vsink) { g_print ("One element could not be created\n"); return -1; } /* set filename property on the file source. Also add a message * handler. */ g_object_set (G_OBJECT (source), "location", argv[1], NULL); bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline)); gst_bus_add_watch (bus, bus_call, loop); gst_object_unref (bus); /* put all elements in a bin */ gst_bin_add_many (GST_BIN (pipeline), source, demux, vdecoder, audioq, videoq, adecoder, aconv, asink, vsink, NULL); /* link together - note that we cannot link the demux and * decoder yet, becuse the demux uses dynamic pads. For that, * we set a pad-added signal handler. */ gst_element_link (source, demux); gst_element_link_many (audioq, adecoder, aconv, asink, NULL); gst_element_link_many (videoq, vdecoder, vconv, vsink, NULL); g_signal_connect (demux, "pad-added", G_CALLBACK (new_pad), pipeline); /* Now set to playing and iterate. */ g_print ("Setting to PLAYING\n"); gst_element_set_state (pipeline, GST_STATE_PLAYING); g_print ("Running\n"); g_main_loop_run (loop); /* clean up nicely */ g_print ("Returned, stopping playback\n"); gst_element_set_state (pipeline, GST_STATE_NULL); g_print ("Deleting pipeline\n"); gst_object_unref (GST_OBJECT (pipeline)); return 0; } int main(int argc, char* argv[]) { if (argc != 2) { g_print("Usage: %s \n", argv[0]); return -1; } /* Initialize the gstreamer */ gst_init(&argc, &argv); mpegVideo(argc, argv); }