gst-indent
[platform/upstream/gst-plugins-good.git] / examples / gstplay / player.c
1 /* GStreamer
2  * Copyright (C) 2003 Julien Moutte <julien@moutte.net>
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
20 #include <gst/play/play.h>
21
22 static GMainLoop *loop = NULL;
23 static gint64 length = 0;
24
25 static void
26 print_tag (const GstTagList * list, const gchar * tag, gpointer unused)
27 {
28   gint i, count;
29
30   count = gst_tag_list_get_tag_size (list, tag);
31
32   for (i = 0; i < count; i++) {
33     gchar *str;
34
35     if (gst_tag_get_type (tag) == G_TYPE_STRING) {
36       g_assert (gst_tag_list_get_string_index (list, tag, i, &str));
37     } else {
38       str =
39           g_strdup_value_contents (gst_tag_list_get_value_index (list, tag, i));
40     }
41
42     if (i == 0) {
43       g_print ("%15s: %s\n", gst_tag_get_nick (tag), str);
44     } else {
45       g_print ("               : %s\n", str);
46     }
47
48     g_free (str);
49   }
50 }
51
52 static void
53 got_found_tag (GstPlay * play, GstElement * source, GstTagList * tag_list)
54 {
55   gst_tag_list_foreach (tag_list, print_tag, NULL);
56 }
57
58 static void
59 got_time_tick (GstPlay * play, gint64 time_nanos)
60 {
61   g_print ("time tick %f\n", time_nanos / (float) GST_SECOND);
62 }
63
64 static void
65 got_stream_length (GstPlay * play, gint64 length_nanos)
66 {
67   g_print ("got length %llu\n", length_nanos);
68   length = length_nanos;
69 }
70
71 static void
72 got_video_size (GstPlay * play, gint width, gint height)
73 {
74   g_print ("got video size %d, %d\n", width, height);
75 }
76
77 static void
78 got_eos (GstPlay * play)
79 {
80   g_print ("End Of Stream\n");
81   g_main_loop_quit (loop);
82 }
83
84 static gboolean
85 seek_timer (GstPlay * play)
86 {
87   gst_play_seek_to_time (play, length / 2);
88   return FALSE;
89 }
90
91 static gboolean
92 idle_iterate (GstPlay * play)
93 {
94   gst_bin_iterate (GST_BIN (play));
95   return (GST_STATE (GST_ELEMENT (play)) == GST_STATE_PLAYING);
96 }
97
98 int
99 main (int argc, char *argv[])
100 {
101   GstPlay *play;
102   GstElement *data_src, *video_sink, *audio_sink, *vis_element;
103   GError *error = NULL;
104
105   /* Initing GStreamer library */
106   gst_init (&argc, &argv);
107
108   if (argc != 2) {
109     g_print ("usage: %s <video filename>\n", argv[0]);
110     exit (-1);
111   }
112
113   loop = g_main_loop_new (NULL, FALSE);
114
115   /* Creating the GstPlay object */
116   play = gst_play_new (&error);
117   if (error) {
118     g_print ("Error: could not create play object:\n%s\n", error->message);
119     g_error_free (error);
120     return 1;
121   }
122
123   /* Getting default audio and video plugins from GConf */
124   audio_sink = gst_element_factory_make ("osssink", "audio_sink");
125   video_sink = gst_element_factory_make ("ximagesink", "video_sink");
126   vis_element = gst_element_factory_make ("goom", "vis_element");
127   data_src = gst_element_factory_make ("gnomevfssrc", "source");
128
129   /* Let's send them to GstPlay object */
130   gst_play_set_audio_sink (play, audio_sink);
131   gst_play_set_video_sink (play, video_sink);
132   gst_play_set_data_src (play, data_src);
133   gst_play_set_visualization (play, vis_element);
134
135   /* Setting location we want to play */
136   gst_play_set_location (play, argv[1]);
137
138   /* Uncomment that line to get an XML dump of the pipeline */
139   /* gst_xml_write_file (GST_ELEMENT (play), stdout); */
140
141   g_signal_connect (G_OBJECT (play), "time_tick",
142       G_CALLBACK (got_time_tick), NULL);
143   g_signal_connect (G_OBJECT (play), "stream_length",
144       G_CALLBACK (got_stream_length), NULL);
145   g_signal_connect (G_OBJECT (play), "have_video_size",
146       G_CALLBACK (got_video_size), NULL);
147   g_signal_connect (G_OBJECT (play), "found_tag",
148       G_CALLBACK (got_found_tag), NULL);
149   g_signal_connect (G_OBJECT (play), "error",
150       G_CALLBACK (gst_element_default_error), NULL);
151   g_signal_connect (G_OBJECT (play), "eos", G_CALLBACK (got_eos), NULL);
152
153   /* Change state to PLAYING */
154   gst_element_set_state (GST_ELEMENT (play), GST_STATE_PLAYING);
155
156   g_idle_add ((GSourceFunc) idle_iterate, play);
157   g_timeout_add (20000, (GSourceFunc) seek_timer, play);
158
159   g_main_loop_run (loop);
160
161   g_print ("setting pipeline to ready\n");
162
163   gst_element_set_state (GST_ELEMENT (play), GST_STATE_READY);
164
165   /* unref 
166      gst_object_unref (GST_OBJECT (play)); */
167
168   exit (0);
169 }