More work on capsnego, mostly proxying
[platform/upstream/gstreamer.git] / tests / old / testsuite / capsnego / converter2.c
1
2 #include <gst/gst.h>
3
4 GstPad *srcpad, *sinkpad;
5 GstPad *srcconvpad, *sinkconvpad;
6 GstPad *srcpadtempl, *sinkpadtempl;
7 GstPad *srcconvtempl, *sinkconvtempl;
8
9 gint converter_in = -1, converter_out = -1;
10 gint target_rate = 2000;
11
12 static GstPadFactory src_factory = {
13   "src",
14   GST_PAD_FACTORY_SRC,
15   GST_PAD_FACTORY_ALWAYS,
16   GST_PAD_FACTORY_CAPS(
17   "test_src",
18     "audio/raw",
19     "rate",    GST_PROPS_INT_RANGE (16, 20000)
20   ),
21   NULL,
22 };
23
24 static GstPadFactory src_conv_factory = {
25   "src",
26   GST_PAD_FACTORY_SRC,
27   GST_PAD_FACTORY_ALWAYS,
28   GST_PAD_FACTORY_CAPS(
29   "test_src",
30     "audio/raw",
31     "rate",    GST_PROPS_INT_RANGE (16, 20000)
32   ),
33   NULL,
34 };
35
36 static GstPadFactory sink_conv_factory = {
37   "src",
38   GST_PAD_FACTORY_SINK,
39   GST_PAD_FACTORY_ALWAYS,
40   GST_PAD_FACTORY_CAPS(
41   "test_src",
42     "audio/raw",
43     "rate",    GST_PROPS_INT_RANGE (16, 20000)
44   ),
45   NULL,
46 };
47
48 static GstPadFactory sink_factory = {
49   "sink",
50   GST_PAD_FACTORY_SINK,
51   GST_PAD_FACTORY_ALWAYS,
52   GST_PAD_FACTORY_CAPS(
53   "test_sink",
54     "audio/raw",
55     "rate",    GST_PROPS_INT_RANGE (16, 20000)
56   ),
57   NULL,
58 };
59
60 static GstCapsFactory sink_caps = {
61   "sink_caps",
62   "audio/raw",
63   "rate",     GST_PROPS_INT (6000),
64   NULL
65 };
66
67 static GstCapsFactory src_caps = {
68   "src_caps",
69   "audio/raw",
70   "rate",     GST_PROPS_INT (3000),
71   NULL
72 };
73
74 static GstPadTemplate *srctempl, *sinktempl;
75 static GstCaps *srccaps, *sinkcaps;
76
77 static GstPadNegotiateReturn
78 converter_negotiate_src (GstPad *pad, GstCaps **caps, gint counter)
79 {
80   g_print (">");
81
82   if (counter == 0) {
83     *caps = NULL;
84     return GST_PAD_NEGOTIATE_TRY;
85   }
86   if (*caps) {
87     converter_out = gst_caps_get_int (*caps, "rate");
88     return GST_PAD_NEGOTIATE_AGREE;
89   }
90
91   return GST_PAD_NEGOTIATE_FAIL;
92 }
93
94 static GstPadNegotiateReturn
95 converter_negotiate_sink (GstPad *pad, GstCaps **caps, gint counter)
96 {
97   g_print ("<");
98   if (counter == 0) {
99     *caps = GST_PAD_CAPS (srcconvpad);
100     return GST_PAD_NEGOTIATE_TRY;
101   }
102   if (*caps) {
103     converter_in = gst_caps_get_int (*caps, "rate");
104
105     if (counter == 1) {
106       converter_out = gst_caps_get_int (*caps, "rate");
107       return gst_pad_negotiate_proxy (srcconvpad, caps, counter);
108     }
109     return GST_PAD_NEGOTIATE_AGREE;
110   }
111
112   return GST_PAD_NEGOTIATE_FAIL;
113 }
114
115 static GstPadNegotiateReturn
116 target_negotiate_sink (GstPad *pad, GstCaps **caps, gint counter)
117 {
118   g_print ("{");
119   if (counter == 0) {
120     *caps = gst_caps_new_with_props (
121                     "target_caps",
122                     "audio/raw",
123                     gst_props_new (
124                             "rate", GST_PROPS_INT (target_rate),
125                             NULL)
126                     );
127     return GST_PAD_NEGOTIATE_TRY;
128   }
129   if (*caps) {
130     target_rate = gst_caps_get_int (*caps, "rate");
131     g_print ("target set %d\n", target_rate);
132     return GST_PAD_NEGOTIATE_AGREE;
133   }
134
135   return GST_PAD_NEGOTIATE_FAIL;
136 }
137
138 int 
139 main (int argc, char *argv[])
140 {
141   gboolean overall = TRUE;
142   gboolean result;
143   
144   gst_init (&argc, &argv);
145
146   srctempl = gst_padtemplate_new (&src_factory);
147   sinktempl = gst_padtemplate_new (&sink_factory);
148   srcpad = gst_pad_new_from_template (srctempl, "src");
149   sinkpad = gst_pad_new_from_template (sinktempl, "sink");
150
151   srcconvtempl = gst_padtemplate_new (&src_conv_factory);
152   sinkconvtempl = gst_padtemplate_new (&sink_conv_factory);
153   srcconvpad = gst_pad_new_from_template (srcconvtempl, "csrc");
154   sinkconvpad = gst_pad_new_from_template (sinkconvtempl, "csink");
155
156   gst_pad_set_negotiate_function (srcconvpad, converter_negotiate_src);
157   gst_pad_set_negotiate_function (sinkconvpad, converter_negotiate_sink);
158   gst_pad_set_negotiate_function (sinkpad, target_negotiate_sink);
159
160   sinkcaps  = gst_caps_register (&sink_caps);
161   srccaps  = gst_caps_register (&src_caps);
162
163   g_print ("-------)      (-----------)       (-----   \n");
164   g_print ("       !      ! converter !       !        \n");
165   g_print ("      src -- csink       csrc -- sink      \n");
166   g_print ("-------)      (-----------)       (-----   \n\n");
167   g_print ("The convertor first tries to proxy the caps received\n");
168   g_print ("on its csink pad to its csrc pad, when that fails, it\n");
169   g_print ("sets up the conversion.\n\n");
170   
171
172   g_print ("sink pad set caps (rate=%d), converter status: %d %d\n", target_rate, 
173                   converter_in, converter_out);
174   gst_caps_set (sinkcaps, "rate", GST_PROPS_INT (target_rate));
175   result = gst_pad_set_caps (sinkpad, sinkcaps);
176   g_print ("result: %d, converter status: %d %d, target: %d\n\n", result, 
177                   converter_in, converter_out, target_rate);
178
179   result = gst_pad_connect (srcpad, sinkconvpad);
180   g_print ("pad connect 1: %d\n", result);
181   overall &= (result == TRUE);
182   result = gst_pad_connect (srcconvpad, sinkpad);
183   g_print ("pad connect 2: %d\n", result);
184   overall &= (result == TRUE);
185
186   g_print ("after connect, converter status: %d %d, target %d\n\n", converter_in, converter_out, target_rate);
187
188   g_print ("src pad set caps (rate=%d), converter status: %d %d, target %d \n", gst_caps_get_int (srccaps, "rate"),
189                   converter_in, converter_out, target_rate);
190   result = gst_pad_set_caps (srcpad, srccaps);
191   g_print ("result %d, converter status: %d %d, target %d\n\n", result, 
192                   converter_in, converter_out, target_rate);
193
194   g_print ("sink pad set caps (rate=2000), converter status: %d %d, target %d \n",
195                   converter_in, converter_out, target_rate);
196   target_rate = 2000;
197   gst_caps_set (sinkcaps, "rate", GST_PROPS_INT (target_rate));
198   result = gst_pad_set_caps (sinkpad, sinkcaps);
199   g_print ("result %d, converter status: %d %d, target: %d\n\n", result, 
200                   converter_in, converter_out, target_rate);
201
202   gst_caps_set (srccaps, "rate", GST_PROPS_INT (4000));
203   result = gst_pad_renegotiate (srcpad);
204   g_print ("sink pad renegotiate caps %d, converter status: %d %d, target: %d\n", result, 
205                   converter_in, converter_out, target_rate);
206
207   gst_caps_set (srccaps, "rate", GST_PROPS_INT (40000));
208   result = gst_pad_set_caps (srcpad, srccaps);
209   g_print ("sink pad set caps %d, converter status: %d %d, target: %d\n", result, 
210                   converter_in, converter_out, target_rate);
211
212   gst_caps_set (sinkcaps, "rate", GST_PROPS_INT (40000));
213   result = gst_pad_set_caps (sinkpad, sinkcaps);
214   g_print ("sink pad set caps %d, converter status: %d %d, target: %d\n", result, 
215                   converter_in, converter_out, target_rate);
216
217   target_rate = 9000;
218   gst_caps_set (sinkcaps, "rate", GST_PROPS_INT (target_rate));
219   result = gst_pad_set_caps (sinkpad, sinkcaps);
220   g_print ("sink pad set caps %d, converter status: %d %d, target: %d\n", result, 
221                   converter_in, converter_out, target_rate);
222
223   exit (!overall);
224 }