tizen 2.3 release
[framework/multimedia/gst-plugins-base0.10.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., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, 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   GstFormat format = GST_FORMAT_TIME;
38   gchar dur_str[32], pos_str[32];
39
40   if (gst_element_query_position (element, &format, &position) &&
41       position != -1) {
42     g_snprintf (pos_str, 32, "%" GST_TIME_FORMAT, GST_TIME_ARGS (position));
43   } else {
44     g_snprintf (pos_str, 32, "-:--:--.---------");
45   }
46
47   if (gst_element_query_duration (element, &format, &duration) &&
48       duration != -1) {
49     g_snprintf (dur_str, 32, "%" GST_TIME_FORMAT, GST_TIME_ARGS (duration));
50   } else {
51     g_snprintf (dur_str, 32, "-:--:--.---------");
52   }
53
54   g_print ("%s / %s\n", pos_str, dur_str);
55
56   return TRUE;
57 }
58
59 static void
60 warning_cb (GstBus * bus, GstMessage * msg, gpointer foo)
61 {
62   GError *err = NULL;
63   gchar *dbg = NULL;
64
65   gst_message_parse_warning (msg, &err, &dbg);
66
67   g_printerr ("WARNING: %s (%s)\n", err->message, (dbg) ? dbg : "no details");
68
69   g_error_free (err);
70   g_free (dbg);
71 }
72
73 static void
74 error_cb (GstBus * bus, GstMessage * msg, GMainLoop * main_loop)
75 {
76   GError *err = NULL;
77   gchar *dbg = NULL;
78
79   gst_message_parse_error (msg, &err, &dbg);
80
81   g_printerr ("ERROR: %s (%s)\n", err->message, (dbg) ? dbg : "no details");
82
83   g_main_loop_quit (main_loop);
84
85   g_error_free (err);
86   g_free (dbg);
87 }
88
89 static void
90 eos_cb (GstBus * bus, GstMessage * msg, GMainLoop * main_loop)
91 {
92   g_print ("EOS\n");
93   g_main_loop_quit (main_loop);
94 }
95
96 static void
97 new_clock_cb (GstBus * bus, GstMessage * msg, gpointer nothing)
98 {
99   GstClock *clock;
100
101   gst_message_parse_new_clock (msg, &clock);
102   g_print ("NEW CLOCK: %s\n", GST_OBJECT_NAME (clock));
103 }
104
105 static void
106 clock_lost_cb (GstBus * bus, GstMessage * msg, GstElement * playbin)
107 {
108   GstClock *clock;
109
110   gst_message_parse_clock_lost (msg, &clock);
111   g_print ("CLOCK LOST: %s\n", GST_OBJECT_NAME (clock));
112
113   gst_element_set_state (playbin, GST_STATE_PAUSED);
114   gst_element_set_state (playbin, GST_STATE_PLAYING);
115 }
116
117 static void
118 about_to_finish_cb (GstElement * element, gchar * uri[])
119 {
120   if (arg_count < max_count) {
121     g_object_set (G_OBJECT (element), "uri", uri[arg_count], NULL);
122     arg_count++;
123   }
124 }
125
126 gint
127 main (gint argc, gchar * argv[])
128 {
129   GstStateChangeReturn res;
130   GstElement *player;
131   GMainLoop *loop;
132   GstBus *bus;
133
134   gst_init (&argc, &argv);
135
136   loop = g_main_loop_new (NULL, TRUE);
137
138   if (argc < 2) {
139     g_print ("usage: %s <uri> [<uri> ... ]\n", argv[0]);
140     exit (-1);
141   }
142
143   player = gst_element_factory_make ("playbin2", "player");
144   g_assert (player);
145
146   bus = gst_pipeline_get_bus (GST_PIPELINE (player));
147   gst_bus_add_signal_watch (bus);
148
149   g_signal_connect (bus, "message::eos", G_CALLBACK (eos_cb), loop);
150   g_signal_connect (bus, "message::error", G_CALLBACK (error_cb), loop);
151   g_signal_connect (bus, "message::warning", G_CALLBACK (warning_cb), NULL);
152   g_signal_connect (bus, "message::new-clock", G_CALLBACK (new_clock_cb), NULL);
153   g_signal_connect (bus, "message::clock-lost", G_CALLBACK (clock_lost_cb),
154       player);
155
156   g_object_set (G_OBJECT (player), "uri", argv[1], NULL);
157
158   arg_count = 2;
159   max_count = argc;
160   g_signal_connect (player, "about-to-finish", G_CALLBACK (about_to_finish_cb),
161       argv);
162
163   res = gst_element_set_state (player, GST_STATE_PLAYING);
164   if (res == GST_STATE_CHANGE_FAILURE) {
165     g_print ("could not play\n");
166     return -1;
167   }
168
169   g_timeout_add (UPDATE_INTERVAL, (GSourceFunc) update_scale, player);
170
171   g_main_loop_run (loop);
172
173   /* tidy up */
174   gst_element_set_state (player, GST_STATE_NULL);
175   gst_object_unref (player);
176   gst_object_unref (bus);
177
178   return 0;
179 }