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