3 * unit test for data protocol
5 * Copyright (C) <2004> Thomas Vander Stichele <thomas at apestaart dot org>
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.
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.
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.
23 #include <gst/check/gstcheck.h>
25 #include <gst/dataprotocol/dataprotocol.h>
26 #include "libs/gst/dataprotocol/dp-private.h" /* private header */
28 /* test our method of reading and writing headers using TO/FROM_BE */
29 GST_START_TEST (test_conversion)
32 guint8 write_array[9];
33 guint16 read_two, expect_two;
34 guint32 read_four, expect_four;
35 guint64 read_eight, expect_eight;
38 for (i = 0; i < 9; ++i) {
43 for (i = 0; i < 8; ++i) {
44 read_two = GST_READ_UINT16_BE (array + i);
45 expect_two = array[i] * (1 << 8) + array[i + 1];
46 fail_unless (read_two == expect_two,
47 "GST_READ_UINT16_BE %d: read %d != %d\n", i, read_two, expect_two);
51 for (i = 0; i < 8; ++i) {
52 GST_WRITE_UINT16_BE (&write_array[i], read_two);
53 fail_unless (memcmp (array + 7, write_array + i, 2) == 0,
54 "GST_WRITE_UINT16_BE %d: memcmp failed", i);
58 for (i = 0; i < 5; ++i) {
59 read_four = GST_READ_UINT32_BE (array + i);
60 expect_four = array[i] * (1 << 24) + array[i + 1] * (1 << 16)
61 + array[i + 2] * (1 << 8) + array[i + 3];
62 fail_unless (read_four == expect_four,
63 "GST_READ_UINT32_BE %d: read %d != %d\n", i, read_four, expect_four);
67 for (i = 0; i < 2; ++i) {
68 read_eight = GST_READ_UINT64_BE (array + i);
69 expect_eight = array[i] * (1LL << 56) + array[i + 1] * (1LL << 48)
70 + array[i + 2] * (1LL << 40) + array[i + 3] * (1LL << 32)
71 + array[i + 4] * (1 << 24) + array[i + 5] * (1 << 16)
72 + array[i + 6] * (1 << 8) + array[i + 7];
73 fail_unless (read_eight == expect_eight,
74 "GST_READ_UINT64_BE %d: read %" G_GUINT64_FORMAT
75 " != %" G_GUINT64_FORMAT "\n", i, read_eight, expect_eight);
79 GST_WRITE_UINT64_BE (&write_array[0], read_eight);
80 fail_unless (memcmp (array + 1, write_array, 8) == 0,
81 "GST_WRITE_UINT64_BE: memcmp failed");
86 /* test creation of header from buffer and back again */
87 GST_START_TEST (test_buffer)
96 g_message ("Creating a new 8-byte buffer with ts 0.5 sec, dur 1 sec\n");
97 buffer = gst_buffer_new_and_alloc (8);
98 GST_BUFFER_TIMESTAMP (buffer) = (GstClockTime) (GST_SECOND * 0.5);
99 GST_BUFFER_DURATION (buffer) = (GstClockTime) GST_SECOND;
100 GST_BUFFER_OFFSET (buffer) = (guint64) 10;
101 GST_BUFFER_OFFSET_END (buffer) = (guint64) 19;
102 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_IN_CAPS);
103 memmove (GST_BUFFER_DATA (buffer), "a buffer", 8);
105 /* create a buffer with CRC checking */
106 fail_unless (gst_dp_header_from_buffer (buffer, GST_DP_HEADER_FLAG_CRC,
107 &header_length, &header), "Could not create header from buffer.");
109 /* validate the header */
110 fail_unless (gst_dp_validate_header (header_length, header),
111 "Could not validate header");
112 /* create a new, empty buffer with the right size */
113 newbuffer = gst_dp_buffer_from_header (header_length, header);
114 fail_unless (newbuffer != NULL, "Could not create a new buffer from header");
115 fail_unless (GST_IS_BUFFER (newbuffer), "Created buffer is not a GstBuffer");
116 /* read/copy the data */
117 memmove (GST_BUFFER_DATA (newbuffer), GST_BUFFER_DATA (buffer),
118 GST_BUFFER_SIZE (buffer));
119 /* validate the buffer */
120 fail_unless (gst_dp_validate_payload (header_length, header,
121 GST_BUFFER_DATA (newbuffer)), "Could not validate payload");
123 g_message ("new buffer timestamp: %" GST_TIME_FORMAT "\n",
124 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (newbuffer)));
125 g_message ("new buffer duration: %" GST_TIME_FORMAT "\n",
126 GST_TIME_ARGS (GST_BUFFER_DURATION (newbuffer)));
127 g_message ("new buffer offset: %" G_GUINT64_FORMAT "\n",
128 GST_BUFFER_OFFSET (newbuffer));
129 g_message ("new buffer offset_end: %" G_GUINT64_FORMAT "\n",
130 GST_BUFFER_OFFSET_END (newbuffer));
131 fail_unless (GST_BUFFER_TIMESTAMP (newbuffer) ==
132 GST_BUFFER_TIMESTAMP (buffer), "Timestamps don't match !");
133 fail_unless (GST_BUFFER_DURATION (newbuffer) == GST_BUFFER_DURATION (buffer),
134 "Durations don't match !");
135 fail_unless (GST_BUFFER_OFFSET (newbuffer) == GST_BUFFER_OFFSET (buffer),
136 "Offsets don't match !");
137 fail_unless (GST_BUFFER_OFFSET_END (newbuffer) ==
138 GST_BUFFER_OFFSET_END (buffer), "Offset ends don't match !");
139 fail_unless (GST_BUFFER_FLAG_IS_SET (newbuffer, GST_BUFFER_FLAG_IN_CAPS),
140 "GST_BUFFER_IN_CAPS flag should have been copied !");
143 gst_buffer_unref (buffer);
144 gst_buffer_unref (newbuffer);
150 GST_START_TEST (test_caps)
152 gchar *string, *newstring;
153 GstCaps *caps, *newcaps;
156 guint8 *header, *payload;
158 caps = gst_caps_from_string ("audio/x-raw-float, "
159 "rate = (int) [ 11025, 48000 ], "
160 "channels = (int) [ 1, 2 ], " "endianness = (int) BYTE_ORDER, "
161 "width = (int) 32, " "buffer-frames = (int) 0");
162 string = gst_caps_to_string (caps);
163 g_message ("Created caps: %s\n", string);
164 fail_unless (gst_dp_packet_from_caps (caps, 0, &header_length, &header,
165 &payload), "Could not create packet from caps.");
167 /* validate the packet */
168 fail_unless (gst_dp_validate_packet (header_length, header, payload),
169 "Could not validate packet");
170 newcaps = gst_dp_caps_from_packet (header_length, header, payload);
171 fail_unless (newcaps != NULL, "Could not create caps from packet");
172 fail_unless (GST_IS_CAPS (newcaps));
173 newstring = gst_caps_to_string (newcaps);
174 g_message ("Received caps: %s\n", newstring);
175 fail_unless (strcmp (string, newstring) == 0,
176 "Created caps do not match original caps");
179 gst_caps_unref (caps);
180 gst_caps_unref (newcaps);
189 GST_START_TEST (test_event)
194 guint8 *header, *payload;
196 g_message ("Testing EOS event at 1s\n");
197 send = gst_event_new_eos ();
198 GST_EVENT_TIMESTAMP (send) = GST_SECOND;
199 fail_unless (gst_dp_packet_from_event (send, GST_DP_HEADER_FLAG_CRC,
200 &header_length, &header, &payload),
201 "Could not create packet from eos event");
203 receive = gst_dp_event_from_packet (header_length, header, payload);
205 g_message ("EOS, timestamp %" GST_TIME_FORMAT "\n",
206 GST_TIME_ARGS (GST_EVENT_TIMESTAMP (receive)));
207 fail_unless (GST_EVENT_TYPE (receive) == GST_EVENT_EOS,
208 "Received event is not EOS");
209 fail_unless (GST_EVENT_TIMESTAMP (receive) == GST_SECOND,
210 "EOS timestamp is not 1.0 sec");
215 gst_event_unref (send);
216 gst_event_unref (receive);
218 g_message ("Testing FLUSH event at 2s\n");
219 send = gst_event_new_flush_start ();
220 GST_EVENT_TIMESTAMP (send) = GST_SECOND * 2;
221 fail_unless (gst_dp_packet_from_event (send, GST_DP_HEADER_FLAG_CRC,
222 &header_length, &header, &payload),
223 "Could not create packet from flush event");
225 receive = gst_dp_event_from_packet (header_length, header, payload);
227 g_message ("Flush, timestamp %" GST_TIME_FORMAT "\n",
228 GST_TIME_ARGS (GST_EVENT_TIMESTAMP (receive)));
229 fail_unless (GST_EVENT_TYPE (receive) == GST_EVENT_FLUSH_START,
230 "Received event is not flush");
231 fail_unless (GST_EVENT_TIMESTAMP (receive) == GST_SECOND * 2,
232 "Flush timestamp is not 2.0 sec");
237 gst_event_unref (send);
238 gst_event_unref (receive);
240 g_message ("Testing SEEK event with 1 second at 3 seconds\n");
242 gst_event_new_seek (1.0, GST_FORMAT_TIME, 0, GST_SEEK_TYPE_SET,
243 GST_SECOND, GST_SEEK_TYPE_NONE, 0);
244 GST_EVENT_TIMESTAMP (send) = GST_SECOND * 3;
245 fail_unless (gst_dp_packet_from_event (send, GST_DP_HEADER_FLAG_CRC,
246 &header_length, &header, &payload),
247 "Could not create packet from seek event");
249 receive = gst_dp_event_from_packet (header_length, header, payload);
255 GstSeekType cur_type, stop_type;
258 gst_event_parse_seek (receive, &rate, &format, &flags,
259 &cur_type, &cur, &stop_type, &stop);
261 g_message ("Seek, timestamp %" GST_TIME_FORMAT ", to %" GST_TIME_FORMAT
262 "\n", GST_TIME_ARGS (GST_EVENT_TIMESTAMP (receive)),
263 GST_TIME_ARGS (cur));
264 fail_unless (GST_EVENT_TYPE (receive) == GST_EVENT_SEEK,
265 "Returned event is not seek");
266 fail_unless (GST_EVENT_TIMESTAMP (receive) == GST_SECOND * 3,
267 "Seek timestamp is not 3.0 sec");
268 fail_unless (format == GST_FORMAT_TIME, "Seek format is not time");
269 fail_unless (cur == GST_SECOND, "Seek cur is not 1.0 sec");
275 gst_event_unref (send);
276 gst_event_unref (receive);
284 Suite *s = suite_create ("data protocol");
285 TCase *tc_chain = tcase_create ("general");
287 suite_add_tcase (s, tc_chain);
288 tcase_add_test (tc_chain, test_conversion);
289 tcase_add_test (tc_chain, test_buffer);
290 tcase_add_test (tc_chain, test_caps);
291 tcase_add_test (tc_chain, test_event);
296 GST_CHECK_MAIN (gst_dp);