1 /*
2 * position-formats.c
3 *
4 * we mostly use GST_FORMAT_TIME in queries and seeks. Test the other ones to
5 * know what works and what not.
6 */
7
8 #include <gst/gst.h>
9
10 #include <stdio.h>
11
12 static gboolean
bus_message(GstBus * bus,GstMessage * message,gpointer user_data)13 bus_message (GstBus * bus, GstMessage * message, gpointer user_data)
14 {
15 GMainLoop *loop = (GMainLoop *) user_data;
16
17 switch (GST_MESSAGE_TYPE (message)) {
18 case GST_MESSAGE_ERROR:
19 {
20 GError *gerror;
21 gchar *debug;
22
23 gst_message_parse_error (message, &gerror, &debug);
24 gst_object_default_error (GST_MESSAGE_SRC (message), gerror, debug);
25 g_error_free (gerror);
26 g_free (debug);
27
28 g_main_loop_quit (loop);
29 break;
30 }
31 case GST_MESSAGE_WARNING:
32 {
33 GError *gerror;
34 gchar *debug;
35
36 gst_message_parse_warning (message, &gerror, &debug);
37 gst_object_default_error (GST_MESSAGE_SRC (message), gerror, debug);
38 g_error_free (gerror);
39 g_free (debug);
40
41 g_main_loop_quit (loop);
42 break;
43 }
44 case GST_MESSAGE_EOS:
45 g_main_loop_quit (loop);
46 break;
47 default:
48 break;
49 }
50 return TRUE;
51 }
52
53 static void
print_value(gboolean res,GstFormat fmt,gint64 val)54 print_value (gboolean res, GstFormat fmt, gint64 val)
55 {
56 if (res) {
57 switch (fmt) {
58 case GST_FORMAT_TIME:
59 printf ("%" GST_TIME_FORMAT, GST_TIME_ARGS (val));
60 break;
61 case GST_FORMAT_PERCENT:
62 printf ("%8.4lf%%", (gdouble) val / GST_FORMAT_PERCENT_SCALE);
63 break;
64 case GST_FORMAT_DEFAULT:
65 case GST_FORMAT_BYTES:
66 case GST_FORMAT_BUFFERS:
67 default:
68 printf ("%" G_GINT64_FORMAT, val);
69 break;
70 }
71 } else {
72 printf ("-");
73 }
74 }
75
76 static gboolean
run_queries(gpointer user_data)77 run_queries (gpointer user_data)
78 {
79 GstElement *bin = (GstElement *) user_data;
80 GstFormat i, fmt;
81 gint64 pos, dur;
82 gboolean pres, dres;
83
84 for (i = GST_FORMAT_DEFAULT; i <= GST_FORMAT_PERCENT; i++) {
85 fmt = i;
86 pres = gst_element_query_position (bin, fmt, &pos);
87 fmt = i;
88 dres = gst_element_query_duration (bin, fmt, &dur);
89 printf ("%-8s : ", gst_format_get_name (i));
90 print_value (pres, fmt, pos);
91 printf (" / ");
92 print_value (dres, fmt, dur);
93 printf ("\n");
94 }
95 printf ("\n");
96
97 return TRUE;
98 }
99
100 gint
main(gint argc,gchar ** argv)101 main (gint argc, gchar ** argv)
102 {
103 gint res = 1;
104 GstElement *bin;
105 GstBus *bus;
106 GMainLoop *loop;
107 const gchar *uri;
108
109 gst_init (&argc, &argv);
110
111 if (argc < 2) {
112 printf ("Usage: %s <uri>\n", argv[0]);
113 goto Error;
114 }
115 uri = argv[1];
116
117 /* build pipeline */
118 bin = gst_element_factory_make ("playbin", NULL);
119 if (!bin) {
120 GST_WARNING ("need playbin from gst-plugins-base");
121 goto Error;
122 }
123
124 g_object_set (bin, "uri", uri, NULL);
125
126 loop = g_main_loop_new (NULL, TRUE);
127
128 /* add watch for messages */
129 bus = gst_pipeline_get_bus (GST_PIPELINE (bin));
130 gst_bus_add_watch (bus, (GstBusFunc) bus_message, (gpointer) loop);
131 gst_object_unref (bus);
132
133 /* add timeout for queries */
134 g_timeout_add_seconds (1, (GSourceFunc) run_queries, (gpointer) bin);
135
136 /* run the show */
137 if (gst_element_set_state (bin,
138 GST_STATE_PLAYING) != GST_STATE_CHANGE_FAILURE) {
139 g_main_loop_run (loop);
140 gst_element_set_state (bin, GST_STATE_NULL);
141 }
142
143 /* cleanup */
144 g_main_loop_unref (loop);
145 gst_object_unref (G_OBJECT (bin));
146 res = 0;
147 Error:
148 return (res);
149 }
150