tizen 2.3 release
[framework/multimedia/gst-plugins-base0.10.git] / tests / examples / audio / audiomix.c
1 /* GStreamer
2  *
3  * audiomix.c: sample audio mixing application
4  *
5  * Copyright (C) 2011 Stefan Sauer <ensonic@users.sf.net>
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., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26
27 #include <string.h>
28 #include <gst/gst.h>
29 #include <gtk/gtk.h>
30
31 /* global items for the interaction */
32 static GtkWidget *scale;
33 static GObject *volumes[2];
34 static gint num_vol = 0;
35
36
37 static void
38 value_changed_callback (GtkWidget * widget, gpointer * user_data)
39 {
40   gdouble value = gtk_range_get_value (GTK_RANGE (widget));
41   g_object_set (volumes[0], "volume", 1.0 - value, NULL);
42   g_object_set (volumes[1], "volume", value, NULL);
43 }
44
45 static void
46 setup_gui (GstElement * volume, gchar * file_name1, gchar * file_name2)
47 {
48   GtkWidget *window, *layout, *label;
49   gchar *name, *ext;
50
51   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
52   gtk_window_set_title (GTK_WINDOW (window), "audiomix");
53   g_signal_connect (window, "destroy", gtk_main_quit, NULL);
54
55   layout = gtk_table_new (2, 3, FALSE);
56   gtk_table_set_col_spacings (GTK_TABLE (layout), 6);
57   gtk_container_add (GTK_CONTAINER (window), layout);
58
59   /* channel labels */
60   name = g_path_get_basename (file_name1);
61   if ((ext = strrchr (name, '.')))
62     *ext = '\0';
63   label = gtk_label_new (name);
64   g_free (name);
65   gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
66   gtk_table_attach_defaults (GTK_TABLE (layout), label, 0, 1, 0, 1);
67
68   gtk_table_attach_defaults (GTK_TABLE (layout), gtk_label_new ("|"), 1, 2, 0,
69       1);
70
71   name = g_path_get_basename (file_name2);
72   if ((ext = strrchr (name, '.')))
73     *ext = '\0';
74   label = gtk_label_new (name);
75   g_free (name);
76   gtk_misc_set_alignment (GTK_MISC (label), 1.0, 0.5);
77   gtk_table_attach_defaults (GTK_TABLE (layout), label, 2, 3, 0, 1);
78
79   /* mix slider */
80   scale = gtk_hscale_new_with_range (0.0, 1.0, 1.0 / 200.0);
81   gtk_range_set_value (GTK_RANGE (scale), 0.0);
82   gtk_widget_set_size_request (scale, 200, -1);
83   gtk_table_attach_defaults (GTK_TABLE (layout), scale, 0, 3, 1, 2);
84   g_signal_connect (scale, "value-changed",
85       G_CALLBACK (value_changed_callback), volume);
86
87   gtk_widget_show_all (GTK_WIDGET (window));
88 }
89
90 static void
91 message_received (GstBus * bus, GstMessage * message, GstPipeline * pipeline)
92 {
93   const GstStructure *s;
94
95   s = gst_message_get_structure (message);
96   g_print ("message from \"%s\" (%s): ",
97       GST_STR_NULL (GST_ELEMENT_NAME (GST_MESSAGE_SRC (message))),
98       gst_message_type_get_name (GST_MESSAGE_TYPE (message)));
99   if (s) {
100     gchar *sstr;
101
102     sstr = gst_structure_to_string (s);
103     g_print ("%s\n", sstr);
104     g_free (sstr);
105   } else {
106     g_print ("no message details\n");
107   }
108 }
109
110 static void
111 eos_message_received (GstBus * bus, GstMessage * message,
112     GstPipeline * pipeline)
113 {
114   message_received (bus, message, pipeline);
115   gtk_main_quit ();
116 }
117
118 static void
119 dynamic_link (GstPadTemplate * templ, GstPad * newpad, gpointer user_data)
120 {
121   GstPad *target = GST_PAD (user_data);
122
123   gst_pad_link (newpad, target);
124   gst_object_unref (target);
125 }
126
127 static void
128 make_mixer_channel (GstElement * pipeline, GstElement * mix, gchar * file_name)
129 {
130   GstElement *filesrc, *decodebin, *volume, *convert, *format;
131   GstCaps *caps;
132
133   /* prepare mixer channel */
134   filesrc = gst_element_factory_make ("filesrc", NULL);
135   decodebin = gst_element_factory_make ("decodebin2", NULL);
136   volume = gst_element_factory_make ("volume", NULL);
137   convert = gst_element_factory_make ("audioconvert", NULL);
138   format = gst_element_factory_make ("capsfilter", NULL);
139   gst_bin_add_many (GST_BIN (pipeline), filesrc, decodebin, volume, convert,
140       format, NULL);
141   gst_element_link (filesrc, decodebin);
142   gst_element_link_many (volume, convert, format, mix, NULL);
143
144   /* configure elements */
145   g_object_set (filesrc, "location", file_name, NULL);
146   g_object_set (volume, "volume", (num_vol == 0) ? 1.0 : 0.0, NULL);
147
148   caps = gst_caps_from_string ("audio/x-raw-int, "
149       "channels = (int) 2, "
150       "endianness = (int) BYTE_ORDER, "
151       "width = (int) 16, " "depth = (int) 16, " "signed = (boolean) true");
152   g_object_set (format, "caps", caps, NULL);
153   gst_caps_unref (caps);
154
155   /* remember volume element */
156   volumes[num_vol++] = (GObject *) volume;
157
158   /* handle dynamic pads */
159   g_signal_connect (G_OBJECT (decodebin), "pad-added",
160       G_CALLBACK (dynamic_link), gst_element_get_static_pad (volume, "sink"));
161 }
162
163 int
164 main (int argc, char *argv[])
165 {
166   GstElement *pipeline = NULL;
167   GstElement *mix, *convert, *sink;
168   GstBus *bus;
169
170   if (argc < 3) {
171     g_print ("Usage: audiomix <file1> <file2>\n");
172     return 1;
173   }
174
175   gst_init (&argc, &argv);
176   gtk_init (&argc, &argv);
177
178   /* prepare tail of pipeline */
179   pipeline = gst_pipeline_new ("audiomix");
180   mix = gst_element_factory_make ("adder", NULL);
181   convert = gst_element_factory_make ("audioconvert", NULL);
182   sink = gst_element_factory_make ("autoaudiosink", NULL);
183   gst_bin_add_many (GST_BIN (pipeline), mix, convert, sink, NULL);
184   gst_element_link_many (mix, convert, sink, NULL);
185
186   /* prepare mixer channel strips */
187   make_mixer_channel (pipeline, mix, argv[1]);
188   make_mixer_channel (pipeline, mix, argv[2]);
189
190   /* setup message handling */
191   bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
192   gst_bus_add_signal_watch_full (bus, G_PRIORITY_HIGH);
193   g_signal_connect (bus, "message::error", (GCallback) message_received,
194       pipeline);
195   g_signal_connect (bus, "message::warning", (GCallback) message_received,
196       pipeline);
197   g_signal_connect (bus, "message::eos", (GCallback) eos_message_received,
198       pipeline);
199
200   /* setup GUI */
201   setup_gui (pipeline, argv[1], argv[2]);
202
203   /* go to main loop */
204   gst_element_set_state (pipeline, GST_STATE_PLAYING);
205   gtk_main ();
206   gst_element_set_state (pipeline, GST_STATE_NULL);
207   gst_object_unref (pipeline);
208
209   return 0;
210 }