dd81c6b5c76555b08e1c14e8f1f0a84451e0ed7f
[mafwsubrenderer] / gst-plugins-base-subtitles0.10 / tests / icles / playback / decodetest.c
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
36 static void
37 error_cb (GstBus * bus, GstMessage * msg, GMainLoop * main_loop)
38 {
39   GError *err = NULL;
40   gchar *dbg = NULL;
41
42   gst_message_parse_error (msg, &err, &dbg);
43
44   g_printerr ("ERROR: %s (%s)\n", err->message, (dbg) ? dbg : "no details");
45
46   g_main_loop_quit (main_loop);
47
48   g_error_free (err);
49   g_free (dbg);
50 }
51
52 static void
53 eos_cb (GstBus * bus, GstMessage * msg, GMainLoop * main_loop)
54 {
55   g_print ("EOS\n");
56   g_main_loop_quit (main_loop);
57 }
58
59 static void
60 state_cb (GstBus * bus, GstMessage * msg, GstElement * pipeline)
61 {
62   if (msg->src == GST_OBJECT (pipeline)) {
63     GstState old_state, new_state, pending_state;
64
65     gst_message_parse_state_changed (msg, &old_state, &new_state,
66         &pending_state);
67     if (new_state == GST_STATE_PLAYING) {
68       g_print ("Decoding ...\n");
69     }
70   }
71 }
72
73 static void
74 new_decoded_pad_cb (GstElement * decodebin, GstPad * pad, gboolean last,
75     GstElement * pipeline)
76 {
77   GstPadLinkReturn ret;
78   GstElement *fakesink;
79   GstPad *fakesink_pad;
80
81   fakesink = gst_element_factory_make ("fakesink", NULL);
82   fakesink_pad = gst_element_get_static_pad (fakesink, "sink");
83
84   gst_bin_add (GST_BIN (pipeline), fakesink);
85
86   /* this doesn't really seem right, but it makes things work for me */
87   gst_element_set_state (fakesink, GST_STATE_PLAYING);
88
89   ret = gst_pad_link (pad, fakesink_pad);
90   if (!GST_PAD_LINK_SUCCESSFUL (ret)) {
91     g_printerr ("Failed to link %s:%s to %s:%s (ret = %d)\n",
92         GST_DEBUG_PAD_NAME (pad), GST_DEBUG_PAD_NAME (fakesink_pad), ret);
93   } else {
94     g_printerr ("Linked %s:%s to %s:%s\n", GST_DEBUG_PAD_NAME (pad),
95         GST_DEBUG_PAD_NAME (fakesink_pad));
96   }
97
98   gst_object_unref (fakesink_pad);
99 }
100
101 gint
102 main (gint argc, gchar * argv[])
103 {
104   GstElement *decoder;
105   GstElement *source;
106   GstElement *pipeline;
107   GstStateChangeReturn res;
108   GMainLoop *loop;
109   GstBus *bus;
110
111   gst_init (&argc, &argv);
112
113   if (argc != 2) {
114     g_printerr ("Decode file from start to end.\n");
115     g_printerr ("Usage: %s URI\n\n", argv[0]);
116     return 1;
117   }
118
119   loop = g_main_loop_new (NULL, TRUE);
120
121   pipeline = gst_pipeline_new ("pipeline");
122
123   bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
124   gst_bus_add_signal_watch (bus);
125
126   g_signal_connect (bus, "message::eos", G_CALLBACK (eos_cb), loop);
127   g_signal_connect (bus, "message::error", G_CALLBACK (error_cb), loop);
128   g_signal_connect (bus, "message::warning", G_CALLBACK (warning_cb), NULL);
129   g_signal_connect (bus, "message::state-changed", G_CALLBACK (state_cb),
130       pipeline);
131
132   source = gst_element_factory_make ("gnomevfssrc", "source");
133   g_assert (source);
134
135   if (argv[1] && strstr (argv[1], "://") != NULL) {
136     g_object_set (G_OBJECT (source), "location", argv[1], NULL);
137   } else if (argv[1]) {
138     gchar *uri = g_strdup_printf ("file://%s", argv[1]);
139
140     g_object_set (G_OBJECT (source), "location", uri, NULL);
141     g_free (uri);
142   }
143
144   decoder = gst_element_factory_make ("decodebin", "decoder");
145   g_assert (decoder);
146
147   gst_bin_add (GST_BIN (pipeline), source);
148   gst_bin_add (GST_BIN (pipeline), decoder);
149
150   gst_element_link_pads (source, "src", decoder, "sink");
151
152   g_signal_connect (decoder, "new-decoded-pad",
153       G_CALLBACK (new_decoded_pad_cb), pipeline);
154
155   res = gst_element_set_state (pipeline, GST_STATE_PLAYING);
156   if (res == GST_STATE_CHANGE_FAILURE) {
157     g_print ("could not play\n");
158     return -1;
159   }
160
161   g_main_loop_run (loop);
162
163   /* tidy up */
164   gst_element_set_state (pipeline, GST_STATE_NULL);
165   gst_object_unref (pipeline);
166   gst_object_unref (bus);
167
168   return 0;
169 }