1 /* GStreamer interactive audioresample test
2  * Copyright (C) 2016 Wim Taymans <wim.taymans@gmail.com>
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., 51 Franklin St, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19 
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23 
24 #include <stdlib.h>
25 
26 #include <gst/gst.h>
27 
28 static GstElement *
make_pipeline(gint type)29 make_pipeline (gint type)
30 {
31   GstElement *result;
32   gchar *pstr;
33 
34   switch (type) {
35     case 0:
36       pstr = g_strdup_printf ("audiotestsrc ! audio/x-raw,rate=44100 ! "
37           " audioresample ! capsfilter name=filter ! capssetter caps="
38           "audio/x-raw,rate=44100 ! wavenc ! filesink location=test.wav");
39       break;
40     default:
41       return NULL;
42   }
43 
44   result = gst_parse_launch_full (pstr, NULL, GST_PARSE_FLAG_NONE, NULL);
45   g_print ("created test %d: \"%s\"\n", type, pstr);
46   g_free (pstr);
47 
48   return result;
49 }
50 
51 typedef struct
52 {
53   gint rate;
54   GstElement *filter;
55 } Data;
56 
57 static GstPadProbeReturn
have_probe(GstPad * pad,GstPadProbeInfo * info,gpointer user_data)58 have_probe (GstPad * pad, GstPadProbeInfo * info, gpointer user_data)
59 {
60   Data *data = user_data;
61   gchar *capsstr;
62   GstCaps *caps;
63 
64   g_print ("resample to %d   \r", data->rate);
65 
66   capsstr = g_strdup_printf ("audio/x-raw, rate=(int)%d", data->rate);
67   caps = gst_caps_from_string (capsstr);
68   g_free (capsstr);
69   g_object_set (data->filter, "caps", caps, NULL);
70   gst_caps_unref (caps);
71 
72   data->rate += 100;
73 
74   if (data->rate > 128000)
75     gst_element_post_message (data->filter,
76         gst_message_new_application (GST_OBJECT (data->filter),
77             gst_structure_new_empty ("my-message")));
78 
79   return GST_PAD_PROBE_OK;
80 }
81 
82 int
main(int argc,char ** argv)83 main (int argc, char **argv)
84 {
85   GstElement *pipe;
86   GstMessage *message;
87   GstPad *srcpad;
88   Data data;
89 
90   gst_init (&argc, &argv);
91 
92   pipe = make_pipeline (0);
93   if (pipe == NULL)
94     return -1;
95 
96   data.rate = 1000;
97   data.filter = gst_bin_get_by_name (GST_BIN (pipe), "filter");
98   g_assert (data.filter);
99 
100   srcpad = gst_element_get_static_pad (data.filter, "src");
101   gst_pad_add_probe (srcpad, GST_PAD_PROBE_TYPE_DATA_DOWNSTREAM, have_probe,
102       &data, NULL);
103   gst_object_unref (srcpad);
104 
105   gst_element_set_state (pipe, GST_STATE_PLAYING);
106 
107   while (TRUE) {
108     message =
109         gst_bus_poll (GST_ELEMENT_BUS (pipe),
110         GST_MESSAGE_ERROR | GST_MESSAGE_APPLICATION, 50 * GST_MSECOND);
111     if (message) {
112       g_print ("got error           \n");
113 
114       gst_message_unref (message);
115       break;
116     }
117   }
118   gst_object_unref (data.filter);
119   gst_element_set_state (pipe, GST_STATE_NULL);
120   gst_object_unref (pipe);
121 
122   return 0;
123 }
124