gst_plugins_base/gst/playback/decodetest.c
changeset 0 0e761a78d257
child 8 4a7fac7dd34a
equal deleted inserted replaced
-1:000000000000 0:0e761a78d257
       
     1 /* GStreamer
       
     2  * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
       
     3  *
       
     4  * This library is free software; you can redistribute it and/or
       
     5  * modify it under the terms of the GNU Library General Public
       
     6  * License as published by the Free Software Foundation; either
       
     7  * version 2 of the License, or (at your option) any later version.
       
     8  *
       
     9  * This library is distributed in the hope that it will be useful,
       
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
       
    12  * Library General Public License for more details.
       
    13  *
       
    14  * You should have received a copy of the GNU Library General Public
       
    15  * License along with this library; if not, write to the
       
    16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
       
    17  * Boston, MA 02111-1307, USA.
       
    18  */
       
    19 #include <gst/gst.h>
       
    20 #include <string.h>
       
    21 
       
    22 static void
       
    23 warning_cb (GstBus * bus, GstMessage * msg, gpointer foo)
       
    24 {
       
    25   GError *err = NULL;
       
    26   gchar *dbg = NULL;
       
    27 
       
    28   gst_message_parse_warning (msg, &err, &dbg);
       
    29 
       
    30   g_printerr ("WARNING: %s (%s)\n", err->message, (dbg) ? dbg : "no details");
       
    31 
       
    32   g_error_free (err);
       
    33   g_free (dbg);
       
    34 }
       
    35 #ifdef __SYMBIAN32__
       
    36 EXPORT_C
       
    37 #endif
       
    38 
       
    39 
       
    40 static void
       
    41 error_cb (GstBus * bus, GstMessage * msg, GMainLoop * main_loop)
       
    42 {
       
    43   GError *err = NULL;
       
    44   gchar *dbg = NULL;
       
    45 
       
    46   gst_message_parse_error (msg, &err, &dbg);
       
    47 
       
    48   g_printerr ("ERROR: %s (%s)\n", err->message, (dbg) ? dbg : "no details");
       
    49 
       
    50   g_main_loop_quit (main_loop);
       
    51 
       
    52   g_error_free (err);
       
    53   g_free (dbg);
       
    54 }
       
    55 
       
    56 static void
       
    57 eos_cb (GstBus * bus, GstMessage * msg, GMainLoop * main_loop)
       
    58 {
       
    59   g_print ("EOS\n");
       
    60   g_main_loop_quit (main_loop);
       
    61 }
       
    62 
       
    63 static void
       
    64 state_cb (GstBus * bus, GstMessage * msg, GstElement * pipeline)
       
    65 {
       
    66   if (msg->src == GST_OBJECT (pipeline)) {
       
    67     GstState old_state, new_state, pending_state;
       
    68 
       
    69     gst_message_parse_state_changed (msg, &old_state, &new_state,
       
    70         &pending_state);
       
    71     if (new_state == GST_STATE_PLAYING) {
       
    72       g_print ("Decoding ...\n");
       
    73     }
       
    74   }
       
    75 }
       
    76 
       
    77 static void
       
    78 new_decoded_pad_cb (GstElement * decodebin, GstPad * pad, gboolean last,
       
    79     GstElement * pipeline)
       
    80 {
       
    81   GstPadLinkReturn ret;
       
    82   GstElement *fakesink;
       
    83   GstPad *fakesink_pad;
       
    84 
       
    85   fakesink = gst_element_factory_make ("fakesink", NULL);
       
    86   fakesink_pad = gst_element_get_pad (fakesink, "sink");
       
    87 
       
    88   gst_bin_add (GST_BIN (pipeline), fakesink);
       
    89 
       
    90   /* this doesn't really seem right, but it makes things work for me */
       
    91   gst_element_set_state (fakesink, GST_STATE_PLAYING);
       
    92 
       
    93   ret = gst_pad_link (pad, fakesink_pad);
       
    94   if (!GST_PAD_LINK_SUCCESSFUL (ret)) {
       
    95     g_printerr ("Failed to link %s:%s to %s:%s (ret = %d)\n",
       
    96         GST_DEBUG_PAD_NAME (pad), GST_DEBUG_PAD_NAME (fakesink_pad), ret);
       
    97   } else {
       
    98     g_printerr ("Linked %s:%s to %s:%s\n", GST_DEBUG_PAD_NAME (pad),
       
    99         GST_DEBUG_PAD_NAME (fakesink_pad));
       
   100   }
       
   101 
       
   102   gst_object_unref (fakesink_pad);
       
   103 }
       
   104 
       
   105 gint
       
   106 main (gint argc, gchar * argv[])
       
   107 {
       
   108   GstElement *decoder;
       
   109   GstElement *source;
       
   110   GstElement *pipeline;
       
   111   GstStateChangeReturn res;
       
   112   GMainLoop *loop;
       
   113   GstBus *bus;
       
   114 
       
   115   gst_init (&argc, &argv);
       
   116 
       
   117   if (argc != 2) {
       
   118     g_printerr ("Decode file from start to end.\n");
       
   119     g_printerr ("Usage: %s URI\n\n", argv[0]);
       
   120     return 1;
       
   121   }
       
   122 
       
   123   loop = g_main_loop_new (NULL, TRUE);
       
   124 
       
   125   pipeline = gst_pipeline_new ("pipeline");
       
   126 
       
   127   bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
       
   128   gst_bus_add_signal_watch (bus);
       
   129 
       
   130   g_signal_connect (bus, "message::eos", G_CALLBACK (eos_cb), loop);
       
   131   g_signal_connect (bus, "message::error", G_CALLBACK (error_cb), loop);
       
   132   g_signal_connect (bus, "message::warning", G_CALLBACK (warning_cb), NULL);
       
   133   g_signal_connect (bus, "message::state-changed", G_CALLBACK (state_cb),
       
   134       pipeline);
       
   135 
       
   136   source = gst_element_factory_make ("gnomevfssrc", "source");
       
   137   g_assert (source);
       
   138 
       
   139   if (argv[1] && strstr (argv[1], "://") != NULL) {
       
   140     g_object_set (G_OBJECT (source), "location", argv[1], NULL);
       
   141   } else if (argv[1]) {
       
   142     gchar *uri = g_strdup_printf ("file://%s", argv[1]);
       
   143 
       
   144     g_object_set (G_OBJECT (source), "location", uri, NULL);
       
   145     g_free (uri);
       
   146   }
       
   147 
       
   148   decoder = gst_element_factory_make ("decodebin", "decoder");
       
   149   g_assert (decoder);
       
   150 
       
   151   gst_bin_add (GST_BIN (pipeline), source);
       
   152   gst_bin_add (GST_BIN (pipeline), decoder);
       
   153 
       
   154   gst_element_link_pads (source, "src", decoder, "sink");
       
   155 
       
   156   g_signal_connect (decoder, "new-decoded-pad",
       
   157       G_CALLBACK (new_decoded_pad_cb), pipeline);
       
   158 
       
   159   res = gst_element_set_state (pipeline, GST_STATE_PLAYING);
       
   160   if (res == GST_STATE_CHANGE_FAILURE) {
       
   161     g_print ("could not play\n");
       
   162     return -1;
       
   163   }
       
   164 
       
   165   g_main_loop_run (loop);
       
   166 
       
   167   /* tidy up */
       
   168   gst_element_set_state (pipeline, GST_STATE_NULL);
       
   169   gst_object_unref (pipeline);
       
   170   gst_object_unref (bus);
       
   171 
       
   172   return 0;
       
   173 }