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., 51 Franklin St, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19 #include <gst/gst.h>
20 
21 #define UPDATE_INTERVAL 500
22 
23 static gboolean
update_scale(GstElement * element)24 update_scale (GstElement * element)
25 {
26   gint64 duration = -1;
27   gint64 position = -1;
28   gchar dur_str[32], pos_str[32];
29 
30   if (gst_element_query_position (element, GST_FORMAT_TIME, &position) &&
31       position != -1) {
32     g_snprintf (pos_str, 32, "%" GST_TIME_FORMAT, GST_TIME_ARGS (position));
33   } else {
34     g_snprintf (pos_str, 32, "-:--:--.---------");
35   }
36 
37   if (gst_element_query_duration (element, GST_FORMAT_TIME, &duration) &&
38       duration != -1) {
39     g_snprintf (dur_str, 32, "%" GST_TIME_FORMAT, GST_TIME_ARGS (duration));
40   } else {
41     g_snprintf (dur_str, 32, "-:--:--.---------");
42   }
43 
44   g_print ("%s / %s\n", pos_str, dur_str);
45 
46   return TRUE;
47 }
48 
49 static void
warning_cb(GstBus * bus,GstMessage * msg,gpointer foo)50 warning_cb (GstBus * bus, GstMessage * msg, gpointer foo)
51 {
52   GError *err = NULL;
53   gchar *dbg = NULL;
54 
55   gst_message_parse_warning (msg, &err, &dbg);
56 
57   g_printerr ("WARNING: %s (%s)\n", err->message, (dbg) ? dbg : "no details");
58 
59   g_error_free (err);
60   g_free (dbg);
61 }
62 
63 static void
error_cb(GstBus * bus,GstMessage * msg,GMainLoop * main_loop)64 error_cb (GstBus * bus, GstMessage * msg, GMainLoop * main_loop)
65 {
66   GError *err = NULL;
67   gchar *dbg = NULL;
68 
69   gst_message_parse_error (msg, &err, &dbg);
70 
71   g_printerr ("ERROR: %s (%s)\n", err->message, (dbg) ? dbg : "no details");
72 
73   g_main_loop_quit (main_loop);
74 
75   g_error_free (err);
76   g_free (dbg);
77 }
78 
79 static void
eos_cb(GstBus * bus,GstMessage * msg,GMainLoop * main_loop)80 eos_cb (GstBus * bus, GstMessage * msg, GMainLoop * main_loop)
81 {
82   g_print ("EOS\n");
83   g_main_loop_quit (main_loop);
84 }
85 
86 
87 gint
main(gint argc,gchar * argv[])88 main (gint argc, gchar * argv[])
89 {
90   GstStateChangeReturn res;
91   GstElement *player;
92   GMainLoop *loop;
93   GstBus *bus;
94 
95   gst_init (&argc, &argv);
96 
97   loop = g_main_loop_new (NULL, TRUE);
98 
99   player = gst_element_factory_make ("playbin", "player");
100   g_assert (player);
101 
102   bus = gst_pipeline_get_bus (GST_PIPELINE (player));
103   gst_bus_add_signal_watch (bus);
104 
105   g_signal_connect (bus, "message::eos", G_CALLBACK (eos_cb), loop);
106   g_signal_connect (bus, "message::error", G_CALLBACK (error_cb), loop);
107   g_signal_connect (bus, "message::warning", G_CALLBACK (warning_cb), NULL);
108 
109   g_object_set (G_OBJECT (player), "uri", argv[1], NULL);
110 
111   res = gst_element_set_state (player, GST_STATE_PLAYING);
112   if (res == GST_STATE_CHANGE_FAILURE) {
113     g_print ("could not play\n");
114     return -1;
115   }
116 
117   g_timeout_add (UPDATE_INTERVAL, (GSourceFunc) update_scale, player);
118 
119   g_main_loop_run (loop);
120 
121   /* tidy up */
122   gst_element_set_state (player, GST_STATE_NULL);
123   gst_object_unref (player);
124   gst_object_unref (bus);
125 
126   return 0;
127 }
128