• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 void
do_step(GstElement * bin)33 do_step (GstElement * bin)
34 {
35   gdouble rate;
36 
37   rate = sin (period);
38 
39   period += G_PI / 150;
40 
41   rate += 1.2;
42 
43   gst_element_send_event (bin,
44       gst_event_new_step (GST_FORMAT_TIME, 40 * GST_MSECOND, rate, FALSE,
45           FALSE));
46 }
47 
48 static void
handle_sync_message(GstBus * bus,GstMessage * message,gpointer data)49 handle_sync_message (GstBus * bus, GstMessage * message, gpointer data)
50 {
51   GstElement *bin;
52 
53   bin = GST_ELEMENT_CAST (data);
54 
55   switch (message->type) {
56     case GST_MESSAGE_STEP_DONE:
57       do_step (bin);
58       break;
59     default:
60       break;
61   }
62 }
63 
64 static void
handle_message(GstBus * bus,GstMessage * message,gpointer data)65 handle_message (GstBus * bus, GstMessage * message, gpointer data)
66 {
67   switch (message->type) {
68     case GST_MESSAGE_EOS:
69       g_message ("got EOS");
70       g_main_loop_quit (loop);
71       break;
72     case GST_MESSAGE_WARNING:
73     case GST_MESSAGE_ERROR:
74     {
75       GError *gerror;
76       gchar *debug;
77 
78       if (message->type == GST_MESSAGE_ERROR)
79         gst_message_parse_error (message, &gerror, &debug);
80       else
81         gst_message_parse_warning (message, &gerror, &debug);
82 
83       gst_object_default_error (GST_MESSAGE_SRC (message), gerror, debug);
84       g_error_free (gerror);
85       g_free (debug);
86       if (message->type == GST_MESSAGE_ERROR)
87         g_main_loop_quit (loop);
88       break;
89     }
90     default:
91       break;
92   }
93 }
94 
95 int
main(int argc,char * argv[])96 main (int argc, char *argv[])
97 {
98   GstElement *bin;
99   GstBus *bus;
100 
101   gst_init (&argc, &argv);
102 
103   if (argc < 2) {
104     g_print ("usage: %s <uri>\n", argv[0]);
105     return -1;
106   }
107 
108   /* create a new bin to hold the elements */
109   bin = gst_element_factory_make ("playbin", "bin");
110   g_assert (bin);
111   g_object_set (bin, "uri", argv[1], NULL);
112 
113   bus = gst_pipeline_get_bus (GST_PIPELINE (bin));
114   gst_bus_add_signal_watch (bus);
115   gst_bus_enable_sync_message_emission (bus);
116 
117   g_signal_connect (bus, "message", (GCallback) handle_message, bin);
118   g_signal_connect (bus, "sync-message", (GCallback) handle_sync_message, bin);
119 
120   /* go to the PAUSED state and wait for preroll */
121   g_message ("prerolling first frame");
122   gst_element_set_state (bin, GST_STATE_PAUSED);
123   gst_element_get_state (bin, NULL, NULL, -1);
124 
125   /* queue step */
126   do_step (bin);
127 
128   gst_element_set_state (bin, GST_STATE_PLAYING);
129 
130   loop = g_main_loop_new (NULL, TRUE);
131   g_main_loop_run (loop);
132 
133   g_message ("finished");
134 
135   /* stop the bin */
136   gst_element_set_state (bin, GST_STATE_NULL);
137 
138   g_main_loop_unref (loop);
139   gst_object_unref (bus);
140 
141   exit (0);
142 }
143