gst/mpegstream/: Ref events before sending them to multiple pads, after all gst_pad_s...
[platform/upstream/gstreamer.git] / examples / level / plot.c
1 /* GStreamer
2  * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3  *
4  * plot.c: output data points to be graphed with gnuplot
5  * Copyright (C) 2003
6  *           Thomas Vander Stichele <thomas at apestaart dot org>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the
20  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #include "config.h"
26 #endif
27
28 #include <gst/gst.h>
29 #include <gtk/gtk.h>
30
31 gboolean got_channel[2] = { FALSE, FALSE };     /* to see if we got the signal for this one yet */
32 gint channels = 0;              /* guess at how many channels there are */
33 gdouble last_time = 0.0;        /* time of last signal */
34 gdouble values[2][3];           /* array of levels from which to print */
35
36 static void
37 level_callback (GstElement * element, gdouble time, gint channel,
38     gdouble rms, gdouble peak, gdouble decay)
39 {
40   int i = 0, j = 0;
41   gboolean got_all = FALSE;
42
43   if (channel + 1 > channels)
44     channels = channel + 1;
45
46   /* reset got_channel if this is a new time point */
47   if (time > last_time) {
48     for (i = 0; i < channels; ++i)
49       got_channel[i] = FALSE;
50     last_time = time;
51   }
52
53   /* store values */
54   got_channel[channel] = TRUE;
55   values[channel][0] = rms;
56   values[channel][1] = peak;
57   values[channel][2] = decay;
58
59   /* check if we have all channels, and output if we do */
60   /* FIXME: this fails on the first, no ? */
61   got_all = TRUE;
62   for (i = 0; i < channels; ++i)
63     if (!got_channel[i])
64       got_all = FALSE;
65   if (got_all) {
66     g_print ("%f ", time);
67     for (i = 0; i < channels; ++i)
68       for (j = 0; j < 3; ++j)
69         g_print ("%f ", values[i][j]);
70     g_print ("\n");
71   }
72 }
73
74 static gboolean
75 idler (gpointer data)
76 {
77   GstElement *pipeline = GST_ELEMENT (data);
78
79   if (gst_bin_iterate (GST_BIN (pipeline)))
80     return TRUE;
81
82   gtk_main_quit ();
83   return FALSE;
84 }
85
86 int
87 main (int argc, char *argv[])
88 {
89
90   GstElement *pipeline = NULL;
91   GError *error = NULL;
92   GstElement *level;
93
94   gst_init (&argc, &argv);
95   gtk_init (&argc, &argv);
96
97   pipeline = gst_parse_launchv ((const gchar **) &argv[1], &error);
98   if (error) {
99     g_print ("pipeline could not be constructed: %s\n", error->message);
100     g_print ("Please give a complete pipeline  with a 'level' element.\n");
101     g_print ("Example: sinesrc ! level ! %s\n", DEFAULT_AUDIOSINK);
102     g_error_free (error);
103     return 1;
104   }
105
106   level = gst_bin_get_by_name (GST_BIN (pipeline), "level0");
107   if (level == NULL) {
108     g_print ("Please give a pipeline with a 'level' element in it\n");
109     return 1;
110   }
111
112   g_object_set (level, "signal", TRUE, NULL);
113   g_signal_connect (level, "level", G_CALLBACK (level_callback), NULL);
114
115
116   /* go to main loop */
117   gst_element_set_state (pipeline, GST_STATE_PLAYING);
118   g_idle_add (idler, pipeline);
119
120   gtk_main ();
121
122   return 0;
123 }