1 /* GStreamer
2 *
3 * stepping.c: stepping sample application
4 *
5 * Copyright (C) 2009 Wim Taymans <wim.taymans@gmail.com>
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
16 *
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
21 */
22
23 #include <stdlib.h>
24 #include <math.h>
25
26 #include <gst/gst.h>
27
28 static GMainLoop *loop;
29
30 static gdouble period = 0.0;
31
32 static gboolean
do_step(GstElement * bin)33 do_step (GstElement * bin)
34 {
35 gdouble length;
36
37 length = sin (period);
38
39 period += G_PI / 40;
40
41 length += 1.1;
42 length *= 100 * GST_MSECOND;
43
44 gst_element_send_event (bin,
45 gst_event_new_step (GST_FORMAT_TIME, length, 1.0, TRUE, FALSE));
46
47 return FALSE;
48 }
49
50 static gboolean
handle_message(GstBus * bus,GstMessage * message,gpointer data)51 handle_message (GstBus * bus, GstMessage * message, gpointer data)
52 {
53 GstElement *bin = GST_ELEMENT_CAST (data);
54
55 switch (message->type) {
56 case GST_MESSAGE_EOS:
57 g_message ("got EOS");
58 g_main_loop_quit (loop);
59 break;
60 case GST_MESSAGE_WARNING:
61 case GST_MESSAGE_ERROR:
62 {
63 GError *gerror;
64 gchar *debug;
65
66 if (message->type == GST_MESSAGE_ERROR)
67 gst_message_parse_error (message, &gerror, &debug);
68 else
69 gst_message_parse_warning (message, &gerror, &debug);
70
71 gst_object_default_error (GST_MESSAGE_SRC (message), gerror, debug);
72 g_error_free (gerror);
73 g_free (debug);
74 g_main_loop_quit (loop);
75 break;
76 }
77 case GST_MESSAGE_ASYNC_DONE:
78 g_timeout_add (40, (GSourceFunc) do_step, bin);
79 break;
80 default:
81 break;
82 }
83 return TRUE;
84 }
85
86 int
main(int argc,char * argv[])87 main (int argc, char *argv[])
88 {
89 GstElement *bin;
90 GstBus *bus;
91
92 gst_init (&argc, &argv);
93
94 if (argc < 2) {
95 g_print ("usage: %s <uri>\n", argv[0]);
96 return -1;
97 }
98
99 /* create a new bin to hold the elements */
100 bin = gst_element_factory_make ("playbin", "bin");
101 g_assert (bin);
102 g_object_set (bin, "uri", argv[1], NULL);
103
104 bus = gst_pipeline_get_bus (GST_PIPELINE (bin));
105 gst_bus_add_watch (bus, handle_message, bin);
106
107 /* go to the PAUSED state and wait for preroll */
108 g_message ("prerolling first frame");
109 gst_element_set_state (bin, GST_STATE_PAUSED);
110 gst_element_get_state (bin, NULL, NULL, -1);
111
112 loop = g_main_loop_new (NULL, TRUE);
113 g_main_loop_run (loop);
114
115 g_message ("finished");
116
117 /* stop the bin */
118 gst_element_set_state (bin, GST_STATE_NULL);
119
120 g_main_loop_unref (loop);
121 gst_object_unref (bus);
122
123 exit (0);
124 }
125