~ubuntu-branches/ubuntu/precise/gst-plugins-bad0.10/precise-proposed

« back to all changes in this revision

Viewing changes to tests/check/elements/assrender.c

Tags: upstream-0.10.17.2
Import upstream version 0.10.17.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* GStreamer
 
2
 * Copyright (C) 2010 Sebastian Dröge <sebastian.droege@collabora.co.uk>
 
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., 59 Temple Place - Suite 330,
 
17
 * Boston, MA 02111-1307, USA.
 
18
 */
 
19
 
 
20
#include <string.h>
 
21
 
 
22
#include <gst/check/gstcheck.h>
 
23
#include <gst/video/video.h>
 
24
#include <gst/app/gstappsrc.h>
 
25
 
 
26
static gboolean
 
27
bus_handler (GstBus * bus, GstMessage * message, gpointer data)
 
28
{
 
29
  GMainLoop *loop = (GMainLoop *) data;
 
30
 
 
31
  switch (message->type) {
 
32
    case GST_MESSAGE_EOS:
 
33
      g_main_loop_quit (loop);
 
34
      break;
 
35
    case GST_MESSAGE_WARNING:
 
36
    case GST_MESSAGE_ERROR:{
 
37
      GError *gerror;
 
38
      gchar *debug;
 
39
 
 
40
      if (message->type == GST_MESSAGE_WARNING)
 
41
        gst_message_parse_warning (message, &gerror, &debug);
 
42
      else
 
43
        gst_message_parse_error (message, &gerror, &debug);
 
44
      gst_object_default_error (GST_MESSAGE_SRC (message), gerror, debug);
 
45
      gst_message_unref (message);
 
46
      g_error_free (gerror);
 
47
      g_free (debug);
 
48
      g_main_loop_quit (loop);
 
49
      break;
 
50
    }
 
51
    default:
 
52
      break;
 
53
  }
 
54
 
 
55
  return TRUE;
 
56
}
 
57
 
 
58
typedef struct
 
59
{
 
60
  GstClockTime ts;
 
61
  GstClockTime duration;
 
62
  const gchar buf[];
 
63
} TestBuffer;
 
64
 
 
65
static const TestBuffer buf0 = {
 
66
  0,
 
67
  0,
 
68
  "[Script Info]\n"
 
69
      "; This is a Sub Station Alpha v4 script.\n"
 
70
      "; For Sub Station Alpha info and downloads,\n"
 
71
      "; go to http://www.eswat.demon.co.uk/\n"
 
72
      "Title: Some Test\n"
 
73
      "Script Updated By: version 2.8.01\n"
 
74
      "ScriptType: v4.00\n"
 
75
      "Collisions: Normal\n"
 
76
      "PlayResY: 200\n"
 
77
      "PlayDepth: 0\n"
 
78
      "Timer: 100,0000\n"
 
79
      " \n"
 
80
      "[V4 Styles]\n"
 
81
      "Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, TertiaryColour, BackColour, Bold, Italic, BorderStyle, Outline, Shadow, \n"
 
82
      "   Alignment, MarginL, MarginR, MarginV, AlphaLevel, Encoding\n"
 
83
      "Style: DefaultVCD, Arial,28,11861244,11861244,11861244,-2147483640,-1,0,1,1,2,2,30,30,30,0,0\n"
 
84
      " \n"
 
85
      "[Events]\n"
 
86
      "Format: Marked, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text"
 
87
};
 
88
 
 
89
static const TestBuffer buf1 = {
 
90
  40 * GST_MSECOND,
 
91
  60 * GST_MSECOND,
 
92
  "1,,DefaultVCD, NTP,0000,0000,0000,,Some Test Blabla"
 
93
};
 
94
 
 
95
static void
 
96
sink_handoff_cb (GstElement * object, GstBuffer * buffer, GstPad * pad,
 
97
    gpointer user_data)
 
98
{
 
99
  guint *sink_pos = (guint *) user_data;
 
100
  gboolean contains_text = (*sink_pos == 1 || *sink_pos == 2);
 
101
  guint i, j;
 
102
  guint8 *data = GST_BUFFER_DATA (buffer);
 
103
  gboolean all_red = TRUE;
 
104
 
 
105
  fail_unless_equals_int (GST_BUFFER_SIZE (buffer), 640 * 480 * 4);
 
106
  for (i = 0; i < 640; i++) {
 
107
    for (j = 0; j < 480; j++) {
 
108
      all_red = all_red && (data[i * 480 * 4 + j * 4 + 1] == 255 &&
 
109
          data[i * 480 * 4 + j * 4 + 2] == 0 &&
 
110
          data[i * 480 * 4 + j * 4 + 3] == 0);
 
111
    }
 
112
  }
 
113
 
 
114
  fail_unless (contains_text != all_red,
 
115
      "Frame %d is incorrect (all red %d, contains text %d)", *sink_pos,
 
116
      all_red, contains_text);
 
117
  *sink_pos = *sink_pos + 1;
 
118
}
 
119
 
 
120
static void
 
121
_dummy_blocked_cb (GstPad * pad, gboolean blocked, gpointer user_data)
 
122
{
 
123
  GST_LOG_OBJECT (pad, "pad blocked: %d", blocked);
 
124
}
 
125
 
 
126
static gboolean
 
127
src_buffer_probe_cb (GstPad * pad, GstBuffer * buffer, gpointer user_data)
 
128
{
 
129
  GstPad *otherpad = GST_PAD (user_data);
 
130
 
 
131
  if (GST_BUFFER_TIMESTAMP (buffer) == buf1.ts)
 
132
    gst_pad_set_blocked_async (otherpad, FALSE, _dummy_blocked_cb, NULL);
 
133
  return TRUE;
 
134
}
 
135
 
 
136
GST_START_TEST (test_assrender_basic)
 
137
{
 
138
  GstElement *pipeline;
 
139
  GstElement *appsrc, *videotestsrc, *capsfilter, *assrender, *fakesink;
 
140
  guint sink_pos = 0;
 
141
  GstCaps *video_caps;
 
142
  GstCaps *text_caps;
 
143
  GstBuffer *buf;
 
144
  GstBus *bus;
 
145
  GMainLoop *loop;
 
146
  GstPad *pad, *blocked_pad;
 
147
 
 
148
  pipeline = gst_pipeline_new ("pipeline");
 
149
  fail_unless (pipeline != NULL);
 
150
 
 
151
  capsfilter = gst_element_factory_make ("capsfilter", NULL);
 
152
  fail_unless (capsfilter != NULL);
 
153
  video_caps =
 
154
      gst_video_format_new_caps (GST_VIDEO_FORMAT_xRGB, 640, 480, 25, 1, 1, 1);
 
155
  g_object_set (capsfilter, "caps", video_caps, NULL);
 
156
  gst_caps_unref (video_caps);
 
157
  blocked_pad = gst_element_get_static_pad (capsfilter, "src");
 
158
  gst_pad_set_blocked_async (blocked_pad, TRUE, _dummy_blocked_cb, NULL);
 
159
 
 
160
  appsrc = gst_element_factory_make ("appsrc", NULL);
 
161
  fail_unless (appsrc != NULL);
 
162
  buf = gst_buffer_new_and_alloc (strlen (buf0.buf) + 1);
 
163
  memcpy (GST_BUFFER_DATA (buf), buf0.buf, GST_BUFFER_SIZE (buf));
 
164
  GST_BUFFER_TIMESTAMP (buf) = buf0.ts;
 
165
  GST_BUFFER_DURATION (buf) = buf0.duration;
 
166
  text_caps =
 
167
      gst_caps_new_simple ("application/x-ssa", "codec_data", GST_TYPE_BUFFER,
 
168
      buf, NULL);
 
169
  gst_buffer_unref (buf);
 
170
  gst_app_src_set_caps (GST_APP_SRC (appsrc), text_caps);
 
171
  g_object_set (appsrc, "format", GST_FORMAT_TIME, NULL);
 
172
  pad = gst_element_get_static_pad (appsrc, "src");
 
173
  gst_pad_add_buffer_probe_full (pad, G_CALLBACK (src_buffer_probe_cb),
 
174
      gst_object_ref (blocked_pad), (GDestroyNotify) gst_object_unref);
 
175
  gst_object_unref (blocked_pad);
 
176
  gst_object_unref (pad);
 
177
 
 
178
  videotestsrc = gst_element_factory_make ("videotestsrc", NULL);
 
179
  fail_unless (videotestsrc != NULL);
 
180
  g_object_set (videotestsrc, "num-buffers", 5, "pattern", 4, NULL);
 
181
 
 
182
  assrender = gst_element_factory_make ("assrender", NULL);
 
183
  fail_unless (assrender != NULL);
 
184
 
 
185
  fakesink = gst_element_factory_make ("fakesink", NULL);
 
186
  fail_unless (fakesink != NULL);
 
187
  g_object_set (fakesink, "signal-handoffs", TRUE, "async", FALSE, NULL);
 
188
  g_signal_connect (fakesink, "handoff", G_CALLBACK (sink_handoff_cb),
 
189
      &sink_pos);
 
190
 
 
191
  gst_bin_add_many (GST_BIN (pipeline), appsrc, videotestsrc, capsfilter,
 
192
      assrender, fakesink, NULL);
 
193
 
 
194
  fail_unless (gst_element_link_pads (appsrc, "src", assrender, "text_sink"));
 
195
  fail_unless (gst_element_link_pads (videotestsrc, "src", capsfilter, "sink"));
 
196
  fail_unless (gst_element_link_pads (capsfilter, "src", assrender,
 
197
          "video_sink"));
 
198
  fail_unless (gst_element_link_pads (assrender, "src", fakesink, "sink"));
 
199
 
 
200
  loop = g_main_loop_new (NULL, TRUE);
 
201
  fail_unless (loop != NULL);
 
202
 
 
203
  bus = gst_element_get_bus (pipeline);
 
204
  fail_unless (bus != NULL);
 
205
  gst_bus_add_watch (bus, bus_handler, loop);
 
206
  gst_object_unref (bus);
 
207
 
 
208
  fail_unless_equals_int (gst_element_set_state (pipeline, GST_STATE_PLAYING),
 
209
      GST_STATE_CHANGE_SUCCESS);
 
210
 
 
211
  buf = gst_buffer_new_and_alloc (strlen (buf1.buf) + 1);
 
212
  memcpy (GST_BUFFER_DATA (buf), buf1.buf, GST_BUFFER_SIZE (buf));
 
213
  gst_buffer_set_caps (buf, text_caps);
 
214
  GST_BUFFER_TIMESTAMP (buf) = buf1.ts;
 
215
  GST_BUFFER_DURATION (buf) = buf1.duration;
 
216
  gst_app_src_push_buffer (GST_APP_SRC (appsrc), buf);
 
217
  gst_caps_unref (text_caps);
 
218
  gst_app_src_end_of_stream (GST_APP_SRC (appsrc));
 
219
 
 
220
  g_main_loop_run (loop);
 
221
 
 
222
  gst_element_set_state (pipeline, GST_STATE_NULL);
 
223
 
 
224
  fail_unless_equals_int (sink_pos, 5);
 
225
 
 
226
  g_object_unref (pipeline);
 
227
  g_main_loop_unref (loop);
 
228
}
 
229
 
 
230
GST_END_TEST;
 
231
 
 
232
Suite *
 
233
assrender_suite (void)
 
234
{
 
235
  Suite *s = suite_create ("assrender");
 
236
  TCase *tc_chain = tcase_create ("linear");
 
237
 
 
238
  /* time out after 120s, not the default 3 */
 
239
  tcase_set_timeout (tc_chain, 120);
 
240
 
 
241
  suite_add_tcase (s, tc_chain);
 
242
  tcase_add_test (tc_chain, test_assrender_basic);
 
243
 
 
244
  return s;
 
245
}
 
246
 
 
247
GST_CHECK_MAIN (assrender);