Tizen 2.0 Release
[framework/multimedia/gst-plugins-good0.10.git] / tests / check / elements / avisubtitle.c
1 /* GStreamer
2  *
3  * unit test for avisubtitle
4  *
5  * Copyright (C) <2007> Thijs Vermeir <thijsvermeir@gmail.com>
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 /* Element-Checklist-Version: 5 */
23
24 #include <unistd.h>
25
26 #include <gst/gst.h>
27 #include <gst/check/gstcheck.h>
28
29 GstPad *mysinkpad;
30 GstPad *mysrcpad;
31
32 guint8 avisub_utf_8_with_bom[] = {
33   0x47, 0x41, 0x42, 0x32, 0x00, 0x02, 0x00, 0x10,
34   0x00, 0x00, 0x00, 0x45, 0x00, 0x6e, 0x00, 0x67,
35   0x00, 0x6c, 0x00, 0x69, 0x00, 0x73, 0x00, 0x68,
36   0x00, 0x00, 0x00, 0x04, 0x00, 0x8e, 0x00, 0x00,
37   0x00, 0xef, 0xbb, 0xbf, 0x31, 0x0d, 0x0a, 0x30,
38   0x30, 0x3a, 0x30, 0x30, 0x3a, 0x30, 0x30, 0x2c,
39   0x31, 0x30, 0x30, 0x20, 0x2d, 0x2d, 0x3e, 0x20,
40   0x30, 0x30, 0x3a, 0x30, 0x30, 0x3a, 0x30, 0x32,
41   0x2c, 0x30, 0x30, 0x30, 0x0d, 0x0a, 0x3c, 0x62,
42   0x3e, 0x41, 0x6e, 0x20, 0x55, 0x54, 0x46, 0x38,
43   0x20, 0x53, 0x75, 0x62, 0x74, 0x69, 0x74, 0x6c,
44   0x65, 0x20, 0x77, 0x69, 0x74, 0x68, 0x20, 0x42,
45   0x4f, 0x4d, 0x3c, 0x2f, 0x62, 0x3e, 0x0d, 0x0a,
46   0x0d, 0x0a, 0x32, 0x0d, 0x0a, 0x30, 0x30, 0x3a,
47   0x30, 0x30, 0x3a, 0x30, 0x32, 0x2c, 0x31, 0x30,
48   0x30, 0x20, 0x2d, 0x2d, 0x3e, 0x20, 0x30, 0x30,
49   0x3a, 0x30, 0x30, 0x3a, 0x30, 0x34, 0x2c, 0x30,
50   0x30, 0x30, 0x0d, 0x0a, 0x53, 0x6f, 0x6d, 0x65,
51   0x74, 0x68, 0x69, 0x6e, 0x67, 0x20, 0x6e, 0x6f,
52   0x6e, 0x41, 0x53, 0x43, 0x49, 0x49, 0x20, 0x2d,
53   0x20, 0xc2, 0xb5, 0xc3, 0xb6, 0xc3, 0xa4, 0xc3,
54   0xbc, 0xc3, 0x9f, 0x0d, 0x0a, 0x0d, 0x0a
55 };
56
57 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
58     GST_PAD_SINK,
59     GST_PAD_ALWAYS,
60     GST_STATIC_CAPS ("application/x-subtitle")
61     );
62
63 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
64     GST_PAD_SRC,
65     GST_PAD_ALWAYS,
66     GST_STATIC_CAPS ("application/x-subtitle-avi")
67     );
68
69 static GstElement *
70 setup_avisubtitle (void)
71 {
72   GstElement *avisubtitle;
73   GstCaps *caps;
74
75   GST_DEBUG ("setup_avisubtitle");
76   avisubtitle = gst_check_setup_element ("avisubtitle");
77   caps = gst_caps_new_simple ("application/x-subtitle", NULL);
78   mysinkpad = gst_check_setup_sink_pad (avisubtitle, &sink_template, caps);
79   gst_caps_unref (caps);
80   caps = gst_caps_new_simple ("application/x-subtitle-avi", NULL);
81   mysrcpad = gst_check_setup_src_pad (avisubtitle, &src_template, caps);
82   gst_caps_unref (caps);
83   gst_pad_set_active (mysinkpad, TRUE);
84   gst_pad_set_active (mysrcpad, TRUE);
85   return avisubtitle;
86 }
87
88 static void
89 cleanup_avisubtitle (GstElement * avisubtitle)
90 {
91   gst_pad_set_active (mysinkpad, FALSE);
92   gst_pad_set_active (mysrcpad, FALSE);
93   gst_check_teardown_sink_pad (avisubtitle);
94   gst_check_teardown_src_pad (avisubtitle);
95   gst_check_teardown_element (avisubtitle);
96 }
97
98 static void
99 check_wrong_buffer (guint8 * data, guint length)
100 {
101   GstBuffer *buffer = gst_buffer_new ();
102   GstElement *avisubtitle = setup_avisubtitle ();
103
104   gst_buffer_set_data (buffer, data, length);
105   fail_unless (gst_element_set_state (avisubtitle,
106           GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
107       "could not set to playing");
108   gst_buffer_ref (buffer);
109   ASSERT_BUFFER_REFCOUNT (buffer, "inbuffer", 2);
110   /* push the broken buffer */
111   fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_ERROR,
112       "accepted a broken buffer");
113   /* check if we have unreffed this buffer on failure */
114   ASSERT_BUFFER_REFCOUNT (buffer, "inbuffer", 1);
115   gst_buffer_unref (buffer);
116   fail_unless (gst_element_set_state (avisubtitle,
117           GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS, "could not set to null");
118   cleanup_avisubtitle (avisubtitle);
119 }
120
121 static void
122 check_correct_buffer (guint8 * src_data, guint src_size, guint8 * dst_data,
123     guint dst_size)
124 {
125   GstBuffer *buffer = gst_buffer_new ();
126   GstBuffer *newBuffer;
127   GstElement *avisubtitle = setup_avisubtitle ();
128   GstEvent *event;
129
130   fail_unless (g_list_length (buffers) == 0, "Buffers list needs to be empty");
131   gst_buffer_set_data (buffer, src_data, src_size);
132   fail_unless (gst_element_set_state (avisubtitle,
133           GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
134       "could not set to playing");
135   ASSERT_BUFFER_REFCOUNT (buffer, "inbuffer", 1);
136   event = gst_event_new_seek (1.0, GST_FORMAT_TIME, GST_SEEK_FLAG_FLUSH,
137       GST_SEEK_TYPE_SET, 2 * GST_SECOND, GST_SEEK_TYPE_SET, 5 * GST_SECOND);
138   fail_unless (gst_element_send_event (avisubtitle, event) == FALSE,
139       "Seeking is not possible when there is no buffer yet");
140   fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_OK,
141       "not accepted a correct buffer");
142   /* we gave away our reference to the buffer, don't assume anything */
143   buffer = NULL;
144   /* a new buffer is created in the list */
145   fail_unless (g_list_length (buffers) == 1,
146       "No new buffer in the buffers list");
147   event = gst_event_new_seek (1.0, GST_FORMAT_TIME, GST_SEEK_FLAG_FLUSH,
148       GST_SEEK_TYPE_SET, 2 * GST_SECOND, GST_SEEK_TYPE_SET, 5 * GST_SECOND);
149   fail_unless (gst_element_send_event (avisubtitle, event) == TRUE,
150       "seeking should be working now");
151   fail_unless (g_list_length (buffers) == 2,
152       "After seeking we need another buffer in the buffers");
153   newBuffer = GST_BUFFER (buffers->data);
154   buffers = g_list_remove (buffers, newBuffer);
155   fail_unless (g_list_length (buffers) == 1, "Buffers list needs to be empty");
156   fail_unless (GST_BUFFER_SIZE (newBuffer) == dst_size,
157       "size of the new buffer is wrong ( %d != %d)",
158       GST_BUFFER_SIZE (newBuffer), dst_size);
159   fail_unless (memcmp (GST_BUFFER_DATA (newBuffer), dst_data, dst_size) == 0,
160       "data of the buffer is not correct");
161   gst_buffer_unref (newBuffer);
162   /* free the buffer from seeking */
163   gst_buffer_unref (GST_BUFFER (buffers->data));
164   buffers = g_list_remove (buffers, buffers->data);
165   fail_unless (gst_element_set_state (avisubtitle,
166           GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS, "could not set to null");
167   cleanup_avisubtitle (avisubtitle);
168 }
169
170
171 GST_START_TEST (test_avisubtitle_negative)
172 {
173   guint8 wrong_magic[] =
174       { 0x47, 0x41, 0x41, 0x32, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
175     0x00, 0x00
176   };
177   guint8 wrong_fixed_word_2[] = {
178     0x47, 0x41, 0x42, 0x32, 0x00, 0x02, 0x01, 0x10,
179     0x00, 0x00, 0x00, 0x45, 0x00, 0x6e, 0x00, 0x67,
180     0x00, 0x6c, 0x00, 0x69, 0x00, 0x73, 0x00, 0x68
181   };
182   guint8 wrong_length_after_name[] = {
183     0x47, 0x41, 0x42, 0x32, 0x00, 0x02, 0x00, 0x10,
184     0x00, 0x00, 0x00, 0x45, 0x00, 0x6e, 0x00, 0x67,
185     0x00, 0x6c, 0x00, 0x69, 0x00, 0x73, 0x00, 0x68
186   };
187   guint8 wrong_fixed_word_4[] = {
188     0x47, 0x41, 0x42, 0x32, 0x00, 0x02, 0x00, 0x10,
189     0x00, 0x00, 0x00, 0x45, 0x00, 0x6e, 0x00, 0x67,
190     0x00, 0x6c, 0x00, 0x69, 0x00, 0x73, 0x00, 0x68,
191     0x00, 0x00, 0x00, 0x04, 0x01, 0x8e, 0x00, 0x00,
192     0x00, 0xef, 0xbb, 0xbf, 0x31, 0x0d, 0x0a, 0x30
193   };
194   guint8 wrong_total_length[] = {
195     0x47, 0x41, 0x42, 0x32, 0x00, 0x02, 0x00, 0x10,
196     0x00, 0x00, 0x00, 0x45, 0x00, 0x6e, 0x00, 0x67,
197     0x00, 0x6c, 0x00, 0x69, 0x00, 0x73, 0x00, 0x68,
198     0x00, 0x00, 0x00, 0x04, 0x00, 0x8e, 0x00, 0x00,
199     0x00, 0xef, 0xbb, 0xbf, 0x31, 0x0d, 0x0a, 0x30
200   };
201   /* size of the buffer must be larger than 11 */
202   check_wrong_buffer (avisub_utf_8_with_bom, 11);
203   /* buffer must start with 'GAB2\0' */
204   check_wrong_buffer (wrong_magic, 14);
205   /* next word must be 2 */
206   check_wrong_buffer (wrong_fixed_word_2, 24);
207   /* length must be larger than the length of the name + 17 */
208   check_wrong_buffer (wrong_length_after_name, 24);
209   /* next word must be 4 */
210   check_wrong_buffer (wrong_fixed_word_4, 36);
211   /* check wrong total length */
212   check_wrong_buffer (wrong_total_length, 36);
213 }
214
215 GST_END_TEST;
216
217 GST_START_TEST (test_avisubtitle_positive)
218 {
219   guint8 avisub_utf_8_without_bom[] = {
220     0x47, 0x41, 0x42, 0x32, 0x00, 0x02, 0x00, 0x10,
221     0x00, 0x00, 0x00, 0x45, 0x00, 0x6e, 0x00, 0x67,
222     0x00, 0x6c, 0x00, 0x69, 0x00, 0x73, 0x00, 0x68,
223     0x00, 0x00, 0x00, 0x04, 0x00, 0x8b, 0x00, 0x00,
224     0x00, 0x31, 0x0d, 0x0a, 0x30,
225     0x30, 0x3a, 0x30, 0x30, 0x3a, 0x30, 0x30, 0x2c,
226     0x31, 0x30, 0x30, 0x20, 0x2d, 0x2d, 0x3e, 0x20,
227     0x30, 0x30, 0x3a, 0x30, 0x30, 0x3a, 0x30, 0x32,
228     0x2c, 0x30, 0x30, 0x30, 0x0d, 0x0a, 0x3c, 0x62,
229     0x3e, 0x41, 0x6e, 0x20, 0x55, 0x54, 0x46, 0x38,
230     0x20, 0x53, 0x75, 0x62, 0x74, 0x69, 0x74, 0x6c,
231     0x65, 0x20, 0x77, 0x69, 0x74, 0x68, 0x20, 0x42,
232     0x4f, 0x4d, 0x3c, 0x2f, 0x62, 0x3e, 0x0d, 0x0a,
233     0x0d, 0x0a, 0x32, 0x0d, 0x0a, 0x30, 0x30, 0x3a,
234     0x30, 0x30, 0x3a, 0x30, 0x32, 0x2c, 0x31, 0x30,
235     0x30, 0x20, 0x2d, 0x2d, 0x3e, 0x20, 0x30, 0x30,
236     0x3a, 0x30, 0x30, 0x3a, 0x30, 0x34, 0x2c, 0x30,
237     0x30, 0x30, 0x0d, 0x0a, 0x53, 0x6f, 0x6d, 0x65,
238     0x74, 0x68, 0x69, 0x6e, 0x67, 0x20, 0x6e, 0x6f,
239     0x6e, 0x41, 0x53, 0x43, 0x49, 0x49, 0x20, 0x2d,
240     0x20, 0xc2, 0xb5, 0xc3, 0xb6, 0xc3, 0xa4, 0xc3,
241     0xbc, 0xc3, 0x9f, 0x0d, 0x0a, 0x0d, 0x0a
242   };
243   check_correct_buffer (avisub_utf_8_with_bom, 175, avisub_utf_8_with_bom + 36,
244       139);
245   check_correct_buffer (avisub_utf_8_without_bom, 172,
246       avisub_utf_8_without_bom + 33, 139);
247 }
248
249 GST_END_TEST;
250
251 static Suite *
252 avisubtitle_suite (void)
253 {
254   Suite *s = suite_create ("avisubtitle");
255   TCase *tc_chain = tcase_create ("general");
256
257   suite_add_tcase (s, tc_chain);
258   tcase_add_test (tc_chain, test_avisubtitle_negative);
259   tcase_add_test (tc_chain, test_avisubtitle_positive);
260
261   return s;
262 }
263
264 GST_CHECK_MAIN (avisubtitle);