b9abcb5b76113c65c1f5855c6daa3eeb633dd8dc
[platform/upstream/gstreamer.git] / tests / icles / playback / test7.c
1 /* GStreamer
2  * Copyright (C) <2007> Wim Taymans <wim.taymans@gmail.com>
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 #ifdef HAVE_CONFIG_H
20 #include "config.h"
21 #endif
22 #ifdef HAVE_STDLIB_H
23 #include <stdlib.h>             /* exit() */
24 #endif
25 #include <gst/gst.h>
26
27 #define UPDATE_INTERVAL 500
28
29 static int arg_count;
30 static int max_count;
31
32 static gboolean
33 update_scale (GstElement * element)
34 {
35   gint64 duration = -1;
36   gint64 position = -1;
37   gchar dur_str[32], pos_str[32];
38
39   if (gst_element_query_position (element, GST_FORMAT_TIME, &position) &&
40       position != -1) {
41     g_snprintf (pos_str, 32, "%" GST_TIME_FORMAT, GST_TIME_ARGS (position));
42   } else {
43     g_snprintf (pos_str, 32, "-:--:--.---------");
44   }
45
46   if (gst_element_query_duration (element, GST_FORMAT_TIME, &duration) &&
47       duration != -1) {
48     g_snprintf (dur_str, 32, "%" GST_TIME_FORMAT, GST_TIME_ARGS (duration));
49   } else {
50     g_snprintf (dur_str, 32, "-:--:--.---------");
51   }
52
53   g_print ("%s / %s\n", pos_str, dur_str);
54
55   return TRUE;
56 }
57
58 static void
59 warning_cb (GstBus * bus, GstMessage * msg, gpointer foo)
60 {
61   GError *err = NULL;
62   gchar *dbg = NULL;
63
64   gst_message_parse_warning (msg, &err, &dbg);
65
66   g_printerr ("WARNING: %s (%s)\n", err->message, (dbg) ? dbg : "no details");
67
68   g_error_free (err);
69   g_free (dbg);
70 }
71
72 static void
73 error_cb (GstBus * bus, GstMessage * msg, GMainLoop * main_loop)
74 {
75   GError *err = NULL;
76   gchar *dbg = NULL;
77
78   gst_message_parse_error (msg, &err, &dbg);
79
80   g_printerr ("ERROR: %s (%s)\n", err->message, (dbg) ? dbg : "no details");
81
82   g_main_loop_quit (main_loop);
83
84   g_error_free (err);
85   g_free (dbg);
86 }
87
88 static void
89 eos_cb (GstBus * bus, GstMessage * msg, GMainLoop * main_loop)
90 {
91   g_print ("EOS\n");
92   g_main_loop_quit (main_loop);
93 }
94
95 static void
96 new_clock_cb (GstBus * bus, GstMessage * msg, gpointer nothing)
97 {
98   GstClock *clock;
99
100   gst_message_parse_new_clock (msg, &clock);
101   g_print ("NEW CLOCK: %s\n", GST_OBJECT_NAME (clock));
102 }
103
104 static void
105 clock_lost_cb (GstBus * bus, GstMessage * msg, GstElement * playbin)
106 {
107   GstClock *clock;
108
109   gst_message_parse_clock_lost (msg, &clock);
110   g_print ("CLOCK LOST: %s\n", GST_OBJECT_NAME (clock));
111
112   gst_element_set_state (playbin, GST_STATE_PAUSED);
113   gst_element_set_state (playbin, GST_STATE_PLAYING);
114 }
115
116 static void
117 about_to_finish_cb (GstElement * element, gchar * uri[])
118 {
119   if (arg_count < max_count) {
120     g_object_set (G_OBJECT (element), "uri", uri[arg_count], NULL);
121     arg_count++;
122   }
123 }
124
125 gint
126 main (gint argc, gchar * argv[])
127 {
128   GstStateChangeReturn res;
129   GstElement *player;
130   GMainLoop *loop;
131   GstBus *bus;
132
133   gst_init (&argc, &argv);
134
135   loop = g_main_loop_new (NULL, TRUE);
136
137   if (argc < 2) {
138     g_print ("usage: %s <uri> [<uri> ... ]\n", argv[0]);
139     exit (-1);
140   }
141
142   player = gst_element_factory_make ("playbin", "player");
143   g_assert (player);
144
145   bus = gst_pipeline_get_bus (GST_PIPELINE (player));
146   gst_bus_add_signal_watch (bus);
147
148   g_signal_connect (bus, "message::eos", G_CALLBACK (eos_cb), loop);
149   g_signal_connect (bus, "message::error", G_CALLBACK (error_cb), loop);
150   g_signal_connect (bus, "message::warning", G_CALLBACK (warning_cb), NULL);
151   g_signal_connect (bus, "message::new-clock", G_CALLBACK (new_clock_cb), NULL);
152   g_signal_connect (bus, "message::clock-lost", G_CALLBACK (clock_lost_cb),
153       player);
154
155   g_object_set (G_OBJECT (player), "uri", argv[1], NULL);
156
157   arg_count = 2;
158   max_count = argc;
159   g_signal_connect (player, "about-to-finish", G_CALLBACK (about_to_finish_cb),
160       argv);
161
162   res = gst_element_set_state (player, GST_STATE_PLAYING);
163   if (res == GST_STATE_CHANGE_FAILURE) {
164     g_print ("could not play\n");
165     return -1;
166   }
167
168   g_timeout_add (UPDATE_INTERVAL, (GSourceFunc) update_scale, player);
169
170   g_main_loop_run (loop);
171
172   /* tidy up */
173   gst_element_set_state (player, GST_STATE_NULL);
174   gst_object_unref (player);
175   gst_object_unref (bus);
176
177   return 0;
178 }