Initialize Tizen 2.3
[framework/multimedia/gstreamer0.10.git] / mobile / tests / check / libs / test_transform.c
1
2 #include <gst/base/gstbasetransform.h>
3
4 typedef struct
5 {
6   GstPad *srcpad;
7   GstPad *sinkpad;
8   GList *events;
9   GList *buffers;
10   GstElement *trans;
11   GstBaseTransformClass *klass;
12
13   GstPadBufferAllocFunction buffer_alloc;
14 } TestTransData;
15
16 static GstStaticPadTemplate gst_test_trans_src_template =
17 GST_STATIC_PAD_TEMPLATE ("src",
18     GST_PAD_SRC,
19     GST_PAD_ALWAYS,
20     GST_STATIC_CAPS ("foo/x-bar")
21     );
22
23 static GstStaticPadTemplate gst_test_trans_sink_template =
24 GST_STATIC_PAD_TEMPLATE ("sink",
25     GST_PAD_SINK,
26     GST_PAD_ALWAYS,
27     GST_STATIC_CAPS ("foo/x-bar")
28     );
29
30 typedef struct _GstTestTrans GstTestTrans;
31 typedef struct _GstTestTransClass GstTestTransClass;
32
33 #define GST_TYPE_TEST_TRANS \
34   (gst_test_trans_get_type())
35 #define GST_TEST_TRANS(obj) \
36   (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_TEST_TRANS,GstTestTrans))
37 #define GST_TEST_TRANS_CLASS(klass) \
38   (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_TEST_TRANS,GstTestTransClass))
39 #define GST_TEST_TRANS_GET_CLASS(obj) \
40   (G_TYPE_INSTANCE_GET_CLASS((obj), GST_TYPE_TEST_TRANS, GstTestTransClass))
41 #define GST_IS_TEST_TRANS(obj) \
42   (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_TEST_TRANS))
43 #define GST_IS_TEST_TRANS_CLASS(klass) \
44   (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_TEST_TRANS))
45
46 struct _GstTestTrans
47 {
48   GstBaseTransform element;
49
50   TestTransData *data;
51 };
52
53 struct _GstTestTransClass
54 {
55   GstBaseTransformClass parent_class;
56 };
57
58 GType gst_test_trans_get_type (void);
59
60 GST_BOILERPLATE (GstTestTrans, gst_test_trans, GstBaseTransform,
61     GST_TYPE_BASE_TRANSFORM);
62
63 static GstFlowReturn (*klass_transform) (GstBaseTransform * trans,
64     GstBuffer * inbuf, GstBuffer * outbuf) = NULL;
65 static GstFlowReturn (*klass_transform_ip) (GstBaseTransform * trans,
66     GstBuffer * buf) = NULL;
67 static gboolean (*klass_set_caps) (GstBaseTransform * trans, GstCaps * incaps,
68     GstCaps * outcaps) = NULL;
69 static GstCaps *(*klass_transform_caps) (GstBaseTransform * trans,
70     GstPadDirection direction, GstCaps * caps) = NULL;
71 static gboolean (*klass_transform_size) (GstBaseTransform * trans,
72     GstPadDirection direction, GstCaps * caps, guint size, GstCaps * othercaps,
73     guint * othersize) = NULL;
74 static gboolean klass_passthrough_on_same_caps = FALSE;
75
76 static GstStaticPadTemplate *sink_template = &gst_test_trans_sink_template;
77 static GstStaticPadTemplate *src_template = &gst_test_trans_src_template;
78
79 static void
80 gst_test_trans_base_init (gpointer g_class)
81 {
82   GstElementClass *element_class;
83
84   element_class = GST_ELEMENT_CLASS (g_class);
85
86   gst_element_class_set_details_simple (element_class, "TestTrans",
87       "Filter/Test", "Test transform", "Wim Taymans <wim.taymans@gmail.com>");
88
89   gst_element_class_add_pad_template (element_class,
90       gst_static_pad_template_get (sink_template));
91   gst_element_class_add_pad_template (element_class,
92       gst_static_pad_template_get (src_template));
93 }
94
95 static void
96 gst_test_trans_class_init (GstTestTransClass * klass)
97 {
98   GstBaseTransformClass *trans_class;
99
100   trans_class = (GstBaseTransformClass *) klass;
101
102   trans_class->passthrough_on_same_caps = klass_passthrough_on_same_caps;
103   trans_class->transform_ip = klass_transform_ip;
104   trans_class->transform = klass_transform;
105   trans_class->transform_caps = klass_transform_caps;
106   trans_class->transform_size = klass_transform_size;
107   trans_class->set_caps = klass_set_caps;
108 }
109
110 static void
111 gst_test_trans_init (GstTestTrans * this, GstTestTransClass * g_class)
112 {
113 }
114
115 static void
116 gst_test_trans_set_data (GstTestTrans * this, TestTransData * data)
117 {
118   this->data = data;
119 }
120
121 static GstFlowReturn
122 result_sink_chain (GstPad * pad, GstBuffer * buffer)
123 {
124   TestTransData *data;
125
126   data = gst_pad_get_element_private (pad);
127
128   data->buffers = g_list_append (data->buffers, buffer);
129
130   return GST_FLOW_OK;
131 }
132
133 static GstFlowReturn
134 result_buffer_alloc (GstPad * pad, guint64 offset, guint size, GstCaps * caps,
135     GstBuffer ** buf)
136 {
137   GstFlowReturn res;
138   TestTransData *data;
139
140   data = gst_pad_get_element_private (pad);
141
142   if (data->buffer_alloc) {
143     res = data->buffer_alloc (pad, offset, size, caps, buf);
144   } else {
145     *buf = gst_buffer_new_and_alloc (size);
146     gst_buffer_set_caps (*buf, caps);
147     res = GST_FLOW_OK;
148   }
149
150   return res;
151 }
152
153 static TestTransData *
154 gst_test_trans_new (void)
155 {
156   TestTransData *res;
157   GstPad *tmp;
158   GstPadTemplate *templ;
159
160   res = g_new0 (TestTransData, 1);
161   res->trans = g_object_new (GST_TYPE_TEST_TRANS, NULL);
162
163   templ = gst_static_pad_template_get (sink_template);
164   templ->direction = GST_PAD_SRC;
165   res->srcpad = gst_pad_new_from_template (templ, "src");
166   gst_object_unref (templ);
167
168   templ = gst_static_pad_template_get (src_template);
169   templ->direction = GST_PAD_SINK;
170   res->sinkpad = gst_pad_new_from_template (templ, "sink");
171   gst_object_unref (templ);
172
173   res->klass = GST_BASE_TRANSFORM_GET_CLASS (res->trans);
174
175   gst_test_trans_set_data (GST_TEST_TRANS (res->trans), res);
176   gst_pad_set_element_private (res->sinkpad, res);
177
178   gst_pad_set_bufferalloc_function (res->sinkpad, result_buffer_alloc);
179   gst_pad_set_chain_function (res->sinkpad, result_sink_chain);
180
181   tmp = gst_element_get_static_pad (res->trans, "sink");
182   gst_pad_link (res->srcpad, tmp);
183   gst_object_unref (tmp);
184
185   tmp = gst_element_get_static_pad (res->trans, "src");
186   gst_pad_link (tmp, res->sinkpad);
187   gst_object_unref (tmp);
188
189   gst_pad_set_active (res->sinkpad, TRUE);
190   gst_element_set_state (res->trans, GST_STATE_PAUSED);
191   gst_pad_set_active (res->srcpad, TRUE);
192
193   return res;
194 }
195
196 static void
197 gst_test_trans_free (TestTransData * data)
198 {
199   GstPad *tmp;
200
201   gst_pad_set_active (data->sinkpad, FALSE);
202   gst_element_set_state (data->trans, GST_STATE_NULL);
203   gst_pad_set_active (data->srcpad, FALSE);
204
205   tmp = gst_element_get_static_pad (data->trans, "src");
206   gst_pad_unlink (tmp, data->sinkpad);
207   gst_object_unref (tmp);
208
209   tmp = gst_element_get_static_pad (data->trans, "sink");
210   gst_pad_link (data->srcpad, tmp);
211   gst_object_unref (tmp);
212
213   gst_object_unref (data->srcpad);
214   gst_object_unref (data->sinkpad);
215   gst_object_unref (data->trans);
216
217   g_free (data);
218 }
219
220 static GstFlowReturn
221 gst_test_trans_push (TestTransData * data, GstBuffer * buffer)
222 {
223   GstFlowReturn ret;
224
225   ret = gst_pad_push (data->srcpad, buffer);
226
227   return ret;
228 }
229
230 static GstBuffer *
231 gst_test_trans_pop (TestTransData * data)
232 {
233   GstBuffer *ret;
234
235   if (data->buffers) {
236     ret = data->buffers->data;
237     data->buffers = g_list_delete_link (data->buffers, data->buffers);
238   } else {
239     ret = NULL;
240   }
241   return ret;
242 }