summaryrefslogtreecommitdiffstats
path: root/tests/check/elements/avisubtitle.c
blob: 52f8586a5433306e9005fe8d43de22cdcf26129b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/* GStreamer
 *
 * unit test for avisubtitle
 *
 * Copyright (C) <2007> Thijs Vermeir <thijsvermeir@gmail.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */
/* Element-Checklist-Version: 5 */

#include <unistd.h>

#include <gst/gst.h>
#include <gst/check/gstcheck.h>

GstPad *mysinkpad;
GstPad *mysrcpad;

guint8 avisub_utf_8_with_bom[] = {
  0x47, 0x41, 0x42, 0x32, 0x00, 0x02, 0x00, 0x10,
  0x00, 0x00, 0x00, 0x45, 0x00, 0x6e, 0x00, 0x67,
  0x00, 0x6c, 0x00, 0x69, 0x00, 0x73, 0x00, 0x68,
  0x00, 0x00, 0x00, 0x04, 0x00, 0x8e, 0x00, 0x00,
  0x00, 0xef, 0xbb, 0xbf, 0x31, 0x0d, 0x0a, 0x30,
  0x30, 0x3a, 0x30, 0x30, 0x3a, 0x30, 0x30, 0x2c,
  0x31, 0x30, 0x30, 0x20, 0x2d, 0x2d, 0x3e, 0x20,
  0x30, 0x30, 0x3a, 0x30, 0x30, 0x3a, 0x30, 0x32,
  0x2c, 0x30, 0x30, 0x30, 0x0d, 0x0a, 0x3c, 0x62,
  0x3e, 0x41, 0x6e, 0x20, 0x55, 0x54, 0x46, 0x38,
  0x20, 0x53, 0x75, 0x62, 0x74, 0x69, 0x74, 0x6c,
  0x65, 0x20, 0x77, 0x69, 0x74, 0x68, 0x20, 0x42,
  0x4f, 0x4d, 0x3c, 0x2f, 0x62, 0x3e, 0x0d, 0x0a,
  0x0d, 0x0a, 0x32, 0x0d, 0x0a, 0x30, 0x30, 0x3a,
  0x30, 0x30, 0x3a, 0x30, 0x32, 0x2c, 0x31, 0x30,
  0x30, 0x20, 0x2d, 0x2d, 0x3e, 0x20, 0x30, 0x30,
  0x3a, 0x30, 0x30, 0x3a, 0x30, 0x34, 0x2c, 0x30,
  0x30, 0x30, 0x0d, 0x0a, 0x53, 0x6f, 0x6d, 0x65,
  0x74, 0x68, 0x69, 0x6e, 0x67, 0x20, 0x6e, 0x6f,
  0x6e, 0x41, 0x53, 0x43, 0x49, 0x49, 0x20, 0x2d,
  0x20, 0xc2, 0xb5, 0xc3, 0xb6, 0xc3, 0xa4, 0xc3,
  0xbc, 0xc3, 0x9f, 0x0d, 0x0a, 0x0d, 0x0a
};

static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
    GST_PAD_SINK,
    GST_PAD_ALWAYS,
    GST_STATIC_CAPS ("application/x-subtitle")
    );

static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
    GST_PAD_SRC,
    GST_PAD_ALWAYS,
    GST_STATIC_CAPS ("application/x-subtitle-avi")
    );

GstElement *
setup_avisubtitle ()
{
  GstElement *avisubtitle;
  GstCaps *caps;

  GST_DEBUG ("setup_avisubtitle");
  avisubtitle = gst_check_setup_element ("avisubtitle");
  caps = gst_caps_new_simple ("application/x-subtitle", NULL);
  mysinkpad = gst_check_setup_sink_pad (avisubtitle, &sink_template, caps);
  gst_caps_unref (caps);
  caps = gst_caps_new_simple ("application/x-subtitle-avi", NULL);
  mysrcpad = gst_check_setup_src_pad (avisubtitle, &src_template, caps);
  gst_caps_unref (caps);
  gst_pad_set_active (mysinkpad, TRUE);
  gst_pad_set_active (mysrcpad, TRUE);
  return avisubtitle;
}

void
cleanup_avisubtitle (GstElement * avisubtitle)
{
  gst_pad_set_active (mysinkpad, FALSE);
  gst_pad_set_active (mysrcpad, FALSE);
  gst_check_teardown_sink_pad (avisubtitle);
  gst_check_teardown_src_pad (avisubtitle);
  gst_check_teardown_element (avisubtitle);
}

void
check_wrong_buffer (guint8 * data, guint length)
{
  GstBuffer *buffer = gst_buffer_new ();
  GstElement *avisubtitle = setup_avisubtitle ();

  gst_buffer_set_data (buffer, data, length);
  fail_unless (gst_element_set_state (avisubtitle,
          GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
      "could not set to playing");
  gst_buffer_ref (buffer);
  ASSERT_BUFFER_REFCOUNT (buffer, "inbuffer", 2);
  /* push the broken buffer */
  fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_ERROR,
      "accepted a broken buffer");
  /* check if we have unreffed this buffer on failure */
  ASSERT_BUFFER_REFCOUNT (buffer, "inbuffer", 1);
  gst_buffer_unref (buffer);
  fail_unless (gst_element_set_state (avisubtitle,
          GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS, "could not set to null");
  cleanup_avisubtitle (avisubtitle);
}

void
check_correct_buffer (guint8 * src_data, guint src_size, guint8 * dst_data,
    guint dst_size)
{
  GstBuffer *buffer = gst_buffer_new ();
  GstBuffer *newBuffer;
  GstElement *avisubtitle = setup_avisubtitle ();
  GstEvent *event;

  fail_unless (g_list_length (buffers) == 0, "Buffers list needs to be empty");
  gst_buffer_set_data (buffer, src_data, src_size);
  fail_unless (gst_element_set_state (avisubtitle,
          GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
      "could not set to playing");
  ASSERT_BUFFER_REFCOUNT (buffer, "inbuffer", 1);
  event = gst_event_new_seek (1.0, GST_FORMAT_TIME, GST_SEEK_FLAG_FLUSH,
      GST_SEEK_TYPE_SET, 2 * GST_SECOND, GST_SEEK_TYPE_SET, 5 * GST_SECOND);
  fail_unless (gst_element_send_event (avisubtitle, event) == FALSE,
      "Seeking is not possible when there is no buffer yet");
  fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_OK,
      "not accepted a correct buffer");
  /* we gave away our reference to the buffer, don't assume anything */
  buffer = NULL;
  /* a new buffer is created in the list */
  fail_unless (g_list_length (buffers) == 1,
      "No new buffer in the buffers list");
  event = gst_event_new_seek (1.0, GST_FORMAT_TIME, GST_SEEK_FLAG_FLUSH,
      GST_SEEK_TYPE_SET, 2 * GST_SECOND, GST_SEEK_TYPE_SET, 5 * GST_SECOND);
  fail_unless (gst_element_send_event (avisubtitle, event) == TRUE,
      "seeking should be working now");
  fail_unless (g_list_length (buffers) == 2,
      "After seeking we need another buffer in the buffers");
  newBuffer = GST_BUFFER (buffers->data);
  buffers = g_list_remove (buffers, newBuffer);
  fail_unless (g_list_length (buffers) == 1, "Buffers list needs to be empty");
  fail_unless (GST_BUFFER_SIZE (newBuffer) == dst_size,
      "size of the new buffer is wrong ( %d != %d)",
      GST_BUFFER_SIZE (newBuffer), dst_size);
  fail_unless (memcmp (GST_BUFFER_DATA (newBuffer), dst_data, dst_size) == 0,
      "data of the buffer is not correct");
  gst_buffer_unref (newBuffer);
  /* free the buffer from seeking */
  gst_buffer_unref (GST_BUFFER (buffers->data));
  buffers = g_list_remove (buffers, buffers->data);
  fail_unless (gst_element_set_state (avisubtitle,
          GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS, "could not set to null");
  cleanup_avisubtitle (avisubtitle);
}


GST_START_TEST (test_avisubtitle_negative)
{
  guint8 wrong_magic[] =
      { 0x47, 0x41, 0x41, 0x32, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00
  };
  guint8 wrong_fixed_word_2[] = {
    0x47, 0x41, 0x42, 0x32, 0x00, 0x02, 0x01, 0x10,
    0x00, 0x00, 0x00, 0x45, 0x00, 0x6e, 0x00, 0x67,
    0x00, 0x6c, 0x00, 0x69, 0x00, 0x73, 0x00, 0x68
  };
  guint8 wrong_length_after_name[] = {
    0x47, 0x41, 0x42, 0x32, 0x00, 0x02, 0x00, 0x10,
    0x00, 0x00, 0x00, 0x45, 0x00, 0x6e, 0x00, 0x67,
    0x00, 0x6c, 0x00, 0x69, 0x00, 0x73, 0x00, 0x68
  };
  guint8 wrong_fixed_word_4[] = {
    0x47, 0x41, 0x42, 0x32, 0x00, 0x02, 0x00, 0x10,
    0x00, 0x00, 0x00, 0x45, 0x00, 0x6e, 0x00, 0x67,
    0x00, 0x6c, 0x00, 0x69, 0x00, 0x73, 0x00, 0x68,
    0x00, 0x00, 0x00, 0x04, 0x01, 0x8e, 0x00, 0x00,
    0x00, 0xef, 0xbb, 0xbf, 0x31, 0x0d, 0x0a, 0x30
  };
  guint8 wrong_total_length[] = {
    0x47, 0x41, 0x42, 0x32, 0x00, 0x02, 0x00, 0x10,
    0x00, 0x00, 0x00, 0x45, 0x00, 0x6e, 0x00, 0x67,
    0x00, 0x6c, 0x00, 0x69, 0x00, 0x73, 0x00, 0x68,
    0x00, 0x00, 0x00, 0x04, 0x00, 0x8e, 0x00, 0x00,
    0x00, 0xef, 0xbb, 0xbf, 0x31, 0x0d, 0x0a, 0x30
  };
  /* size of the buffer must be larger than 11 */
  check_wrong_buffer (avisub_utf_8_with_bom, 11);
  /* buffer must start with 'GAB2\0' */
  check_wrong_buffer (wrong_magic, 14);
  /* next word must be 2 */
  check_wrong_buffer (wrong_fixed_word_2, 24);
  /* length must be larger than the length of the name + 17 */
  check_wrong_buffer (wrong_length_after_name, 24);
  /* next word must be 4 */
  check_wrong_buffer (wrong_fixed_word_4, 36);
  /* check wrong total length */
  check_wrong_buffer (wrong_total_length, 36);
}

GST_END_TEST;

GST_START_TEST (test_avisubtitle_positive)
{
  guint8 avisub_utf_8_without_bom[] = {
    0x47, 0x41, 0x42, 0x32, 0x00, 0x02, 0x00, 0x10,
    0x00, 0x00, 0x00, 0x45, 0x00, 0x6e, 0x00, 0x67,
    0x00, 0x6c, 0x00, 0x69, 0x00, 0x73, 0x00, 0x68,
    0x00, 0x00, 0x00, 0x04, 0x00, 0x8b, 0x00, 0x00,
    0x00, 0x31, 0x0d, 0x0a, 0x30,
    0x30, 0x3a, 0x30, 0x30, 0x3a, 0x30, 0x30, 0x2c,
    0x31, 0x30, 0x30, 0x20, 0x2d, 0x2d, 0x3e, 0x20,
    0x30, 0x30, 0x3a, 0x30, 0x30, 0x3a, 0x30, 0x32,
    0x2c, 0x30, 0x30, 0x30, 0x0d, 0x0a, 0x3c, 0x62,
    0x3e, 0x41, 0x6e, 0x20, 0x55, 0x54, 0x46, 0x38,
    0x20, 0x53, 0x75, 0x62, 0x74, 0x69, 0x74, 0x6c,
    0x65, 0x20, 0x77, 0x69, 0x74, 0x68, 0x20, 0x42,
    0x4f, 0x4d, 0x3c, 0x2f, 0x62, 0x3e, 0x0d, 0x0a,
    0x0d, 0x0a, 0x32, 0x0d, 0x0a, 0x30, 0x30, 0x3a,
    0x30, 0x30, 0x3a, 0x30, 0x32, 0x2c, 0x31, 0x30,
    0x30, 0x20, 0x2d, 0x2d, 0x3e, 0x20, 0x30, 0x30,
    0x3a, 0x30, 0x30, 0x3a, 0x30, 0x34, 0x2c, 0x30,
    0x30, 0x30, 0x0d, 0x0a, 0x53, 0x6f, 0x6d, 0x65,
    0x74, 0x68, 0x69, 0x6e, 0x67, 0x20, 0x6e, 0x6f,
    0x6e, 0x41, 0x53, 0x43, 0x49, 0x49, 0x20, 0x2d,
    0x20, 0xc2, 0xb5, 0xc3, 0xb6, 0xc3, 0xa4, 0xc3,
    0xbc, 0xc3, 0x9f, 0x0d, 0x0a, 0x0d, 0x0a
  };
  check_correct_buffer (avisub_utf_8_with_bom, 175, avisub_utf_8_with_bom + 36,
      139);
  check_correct_buffer (avisub_utf_8_without_bom, 172,
      avisub_utf_8_without_bom + 33, 139);
}

GST_END_TEST;

Suite *
avisubtitle_suite (void)
{
  Suite *s = suite_create ("avisubtitle");
  TCase *tc_chain = tcase_create ("general");

  suite_add_tcase (s, tc_chain);
  tcase_add_test (tc_chain, test_avisubtitle_negative);
  tcase_add_test (tc_chain, test_avisubtitle_positive);

  return s;
}

GST_CHECK_MAIN (avisubtitle);