2 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3 * Copyright (C) <2004> Thomas Vander Stichele <thomas at apestaart dot org>
5 * dataprotocol.c: Functions implementing the GStreamer Data Protocol
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.
28 #include <gst/dataprotocol/dataprotocol.h>
29 #include <glib/gprintf.h> /* g_sprintf */
30 #include <string.h> /* strlen */
31 #include "dp-private.h"
34 GST_DEBUG_CATEGORY (data_protocol_debug);
35 #define GST_CAT_DEFAULT data_protocol_debug
37 /* calculate a CCITT 16 bit CRC check value for a given byte array */
39 * this code snippet is adapted from a web page I found
40 * it is identical except for cleanups, and a final XOR with 0xffff
41 * as outlined in the uecp spec
43 * XMODEM x^16 + x^12 + x^5 + 1
47 #define CRC_INIT 0xFFFF
50 gst_dp_crc (const guint8 * buffer, register guint length)
52 static gboolean initialized = FALSE;
53 static guint16 crc_table[256];
55 unsigned long i, j, k;
58 for (i = 0; i < 256; i++) {
61 j = j & 0x8000 ? (j << 1) ^ POLY : j << 1;
64 crc_table[i] = (guint16) j;
69 crc_register = CRC_INIT; /* always init register */
73 crc_register = (guint16) ((crc_register << 8) ^
74 crc_table[((crc_register >> 8) & 0x00ff) ^ *buffer++]);
76 return (0xffff ^ crc_register);
79 /* debugging function; dumps byte array values per 8 bytes */
80 /* FIXME: would be nice to merge this with gst_util_dump_mem () */
82 gst_dp_dump_byte_array (guint8 * array, guint length)
85 int n = 8; /* number of bytes per line */
86 gchar *line = g_malloc (3 * n + 1);
88 GST_LOG ("dumping byte array of length %d", length);
89 for (i = 0; i < length; ++i) {
90 g_sprintf (line + 3 * (i % n), "%02x ", array[i]);
91 if (i % n == (n - 1)) {
92 GST_LOG ("%03d: %s", i - (n - 1), line);
96 GST_LOG ("%03d: %s", (i / n) * n, line);
104 * Initialize GStreamer Data Protocol library.
106 * Should be called before using these functions; either from source linking
107 * to this source file or from plugin_init.
112 static gboolean _gst_dp_initialized = FALSE;
114 if (_gst_dp_initialized)
117 _gst_dp_initialized = TRUE;
119 GST_DEBUG_CATEGORY_INIT (data_protocol_debug, "gdp", 0,
120 "GStreamer Data Protocol");
123 /*** PUBLIC FUNCTIONS ***/
126 * gst_dp_header_payload_length:
127 * @header: the byte header of the packet array
129 * Returns: the length of the payload this header describes.
132 gst_dp_header_payload_length (const guint8 * header)
134 return GST_DP_HEADER_PAYLOAD_LENGTH (header);
138 * gst_dp_header_payload_type:
139 * @header: the byte header of the packet array
141 * Returns: the #GstDPPayloadType the payload this header describes.
144 gst_dp_header_payload_type (const guint8 * header)
146 return GST_DP_HEADER_PAYLOAD_TYPE (header);
150 * gst_dp_header_from_buffer:
151 * @buffer: a #GstBuffer to create a header for
152 * @flags: the #GDPHeaderFlags to create the header with
153 * @length: a guint pointer to store the header length in
154 * @header: a guint8 * pointer to store a newly allocated header byte array in
156 * Creates a GDP header from the given buffer.
158 * Returns: %TRUE if the header was successfully created.
162 gst_dp_header_from_buffer (const GstBuffer * buffer, GstDPHeaderFlag flags,
163 guint * length, guint8 ** header)
169 g_return_val_if_fail (GST_IS_BUFFER (buffer), FALSE);
170 g_return_val_if_fail (GST_BUFFER_REFCOUNT_VALUE (buffer) > 0, FALSE);
171 g_return_val_if_fail (header, FALSE);
173 *length = GST_DP_HEADER_LENGTH;
174 h = g_malloc (GST_DP_HEADER_LENGTH);
176 /* version, flags, type */
177 h[0] = (guint8) GST_DP_VERSION_MAJOR;
178 h[1] = (guint8) GST_DP_VERSION_MINOR;
179 h[2] = (guint8) flags;
180 h[3] = GST_DP_PAYLOAD_BUFFER;
182 /* buffer properties */
183 GST_WRITE_UINT32_BE (h + 4, GST_BUFFER_SIZE (buffer));
184 GST_WRITE_UINT64_BE (h + 8, GST_BUFFER_TIMESTAMP (buffer));
185 GST_WRITE_UINT64_BE (h + 16, GST_BUFFER_DURATION (buffer));
186 GST_WRITE_UINT64_BE (h + 24, GST_BUFFER_OFFSET (buffer));
187 GST_WRITE_UINT64_BE (h + 32, GST_BUFFER_OFFSET_END (buffer));
190 /* we only copy KEY_UNIT,DELTA_UNIT and IN_CAPS flags */
191 flags_mask = GST_DATA_FLAG_SHIFT (GST_BUFFER_PREROLL) |
192 GST_DATA_FLAG_SHIFT (GST_BUFFER_IN_CAPS) |
193 GST_DATA_FLAG_SHIFT (GST_BUFFER_DELTA_UNIT);
195 GST_WRITE_UINT16_BE (h + 40, GST_BUFFER_FLAGS (buffer) & flags_mask);
198 GST_WRITE_UINT16_BE (h + 42, (guint64) 0);
199 GST_WRITE_UINT32_BE (h + 44, (guint64) 0);
200 GST_WRITE_UINT64_BE (h + 48, (guint64) 0);
204 if (flags & GST_DP_HEADER_FLAG_CRC_HEADER) {
205 /* we don't crc the last four bytes of the header since they are crc's */
206 crc = gst_dp_crc (h, 56);
208 GST_WRITE_UINT16_BE (h + 56, crc);
211 if (flags & GST_DP_HEADER_FLAG_CRC_PAYLOAD) {
212 crc = gst_dp_crc (GST_BUFFER_DATA (buffer), GST_BUFFER_SIZE (buffer));
214 GST_WRITE_UINT16_BE (h + 58, crc);
216 GST_LOG ("created header from buffer:");
217 gst_dp_dump_byte_array (h, GST_DP_HEADER_LENGTH);
223 * gst_dp_packet_from_caps:
224 * @caps: a #GstCaps to create a packet for
225 * @flags: the #GDPHeaderFlags to create the header with
226 * @length: a guint pointer to store the header length in
227 * @header: a guint8 pointer to store a newly allocated header byte array in
228 * @payload: a guint8 pointer to store a newly allocated payload byte array in
230 * Creates a GDP packet from the given caps.
232 * Returns: %TRUE if the packet was successfully created.
235 gst_dp_packet_from_caps (const GstCaps * caps, GstDPHeaderFlag flags,
236 guint * length, guint8 ** header, guint8 ** payload)
242 /* FIXME: GST_IS_CAPS doesn't work
243 g_return_val_if_fail (GST_IS_CAPS (caps), FALSE); */
244 g_return_val_if_fail (caps, FALSE);
245 g_return_val_if_fail (header, FALSE);
246 g_return_val_if_fail (payload, FALSE);
248 *length = GST_DP_HEADER_LENGTH;
249 h = g_malloc (GST_DP_HEADER_LENGTH);
251 string = (guchar *) gst_caps_to_string (caps);
253 /* version, flags, type */
254 h[0] = (guint8) GST_DP_VERSION_MAJOR;
255 h[1] = (guint8) GST_DP_VERSION_MINOR;
256 h[2] = (guint8) flags;
257 h[3] = GST_DP_PAYLOAD_CAPS;
259 /* buffer properties */
260 GST_WRITE_UINT32_BE (h + 4, strlen ((gchar *) string) + 1); /* include trailing 0 */
261 GST_WRITE_UINT64_BE (h + 8, (guint64) 0);
262 GST_WRITE_UINT64_BE (h + 16, (guint64) 0);
263 GST_WRITE_UINT64_BE (h + 24, (guint64) 0);
264 GST_WRITE_UINT64_BE (h + 32, (guint64) 0);
267 GST_WRITE_UINT64_BE (h + 40, (guint64) 0);
268 GST_WRITE_UINT64_BE (h + 48, (guint64) 0);
272 if (flags & GST_DP_HEADER_FLAG_CRC_HEADER) {
273 crc = gst_dp_crc (h, 56);
275 GST_WRITE_UINT16_BE (h + 56, crc);
278 if (flags & GST_DP_HEADER_FLAG_CRC_PAYLOAD) {
279 crc = gst_dp_crc (string, strlen ((gchar *) string) + 1);
281 GST_WRITE_UINT16_BE (h + 58, crc);
283 GST_LOG ("created header from caps:");
284 gst_dp_dump_byte_array (h, GST_DP_HEADER_LENGTH);
291 * gst_dp_packet_from_event:
292 * @event: a #GstEvent to create a packet for
293 * @flags: the #GDPHeaderFlags to create the header with
294 * @length: a guint pointer to store the header length in
295 * @header: a guint8 pointer to store a newly allocated header byte array in
296 * @payload: a guint8 pointer to store a newly allocated payload byte array in
298 * Creates a GDP packet from the given event.
300 * Returns: %TRUE if the packet was successfully created.
303 gst_dp_packet_from_event (const GstEvent * event, GstDPHeaderFlag flags,
304 guint * length, guint8 ** header, guint8 ** payload)
308 guint pl_length; /* length of payload */
310 g_return_val_if_fail (event, FALSE);
311 g_return_val_if_fail (GST_IS_EVENT (event), FALSE);
312 g_return_val_if_fail (header, FALSE);
313 g_return_val_if_fail (payload, FALSE);
315 *length = GST_DP_HEADER_LENGTH;
316 h = g_malloc0 (GST_DP_HEADER_LENGTH);
318 /* first construct payload, since we need the length */
319 switch (GST_EVENT_TYPE (event)) {
320 case GST_EVENT_UNKNOWN:
321 g_warning ("Unknown event, ignoring");
326 case GST_EVENT_FLUSH:
327 case GST_EVENT_DISCONTINUOUS:
328 GST_WRITE_UINT64_BE (h + 8, GST_EVENT_TIMESTAMP (event));
333 pl_length = 4 + 8 + 8 + 4;
334 *payload = g_malloc0 (pl_length);
335 GST_WRITE_UINT32_BE (*payload, (guint32) GST_EVENT_SEEK_TYPE (event));
336 GST_WRITE_UINT64_BE (*payload + 4,
337 (guint64) GST_EVENT_SEEK_OFFSET (event));
338 GST_WRITE_UINT64_BE (*payload + 12,
339 (guint64) GST_EVENT_SEEK_ENDOFFSET (event));
340 GST_WRITE_UINT32_BE (*payload + 20,
341 (guint32) GST_EVENT_SEEK_ACCURACY (event));
346 case GST_EVENT_NAVIGATION:
348 g_warning ("Unhandled event type %d, ignoring", GST_EVENT_TYPE (event));
351 g_warning ("Unknown event type %d, ignoring", GST_EVENT_TYPE (event));
357 /* version, flags, type */
358 h[0] = (guint8) GST_DP_VERSION_MAJOR;
359 h[1] = (guint8) GST_DP_VERSION_MINOR;
360 h[2] = (guint8) flags;
361 h[3] = GST_DP_PAYLOAD_EVENT_NONE + GST_EVENT_TYPE (event);
364 GST_WRITE_UINT32_BE (h + 4, (guint32) pl_length);
366 GST_WRITE_UINT64_BE (h + 8, GST_EVENT_TIMESTAMP (event));
369 GST_WRITE_UINT64_BE (h + 40, (guint64) 0);
370 GST_WRITE_UINT64_BE (h + 48, (guint64) 0);
374 if (flags & GST_DP_HEADER_FLAG_CRC_HEADER) {
375 crc = gst_dp_crc (h, 56);
377 GST_WRITE_UINT16_BE (h + 56, crc);
380 /* events can have a NULL payload */
381 if (*payload && flags & GST_DP_HEADER_FLAG_CRC_PAYLOAD) {
382 crc = gst_dp_crc (*payload, strlen ((gchar *) * payload) + 1);
384 GST_WRITE_UINT16_BE (h + 58, crc);
386 GST_LOG ("created header from event:");
387 gst_dp_dump_byte_array (h, GST_DP_HEADER_LENGTH);
394 * gst_dp_buffer_from_header:
395 * @header_length: the length of the packet header
396 * @header: the byte array of the packet header
398 * Creates a newly allocated #GstBuffer from the given header.
399 * The buffer data needs to be copied into it before validating.
401 * Use this function if you want to pre-allocate a buffer based on the
402 * packet header to read the packet payload in to.
404 * Returns: %TRUE if the buffer was successfully created.
407 gst_dp_buffer_from_header (guint header_length, const guint8 * header)
411 g_return_val_if_fail (GST_DP_HEADER_PAYLOAD_TYPE (header) ==
412 GST_DP_PAYLOAD_BUFFER, FALSE);
414 gst_buffer_new_and_alloc ((guint) GST_DP_HEADER_PAYLOAD_LENGTH (header));
415 GST_BUFFER_TIMESTAMP (buffer) = GST_DP_HEADER_TIMESTAMP (header);
416 GST_BUFFER_DURATION (buffer) = GST_DP_HEADER_DURATION (header);
417 GST_BUFFER_OFFSET (buffer) = GST_DP_HEADER_OFFSET (header);
418 GST_BUFFER_OFFSET_END (buffer) = GST_DP_HEADER_OFFSET_END (header);
419 GST_BUFFER_FLAGS (buffer) = GST_DP_HEADER_BUFFER_FLAGS (header);
425 * gst_dp_caps_from_packet:
426 * @header_length: the length of the packet header
427 * @header: the byte array of the packet header
428 * @payload: the byte array of the packet payload
430 * Creates a newly allocated #GstCaps from the given packet.
432 * Returns: %TRUE if the caps was successfully created.
435 gst_dp_caps_from_packet (guint header_length, const guint8 * header,
436 const guint8 * payload)
441 g_return_val_if_fail (header, FALSE);
442 g_return_val_if_fail (payload, FALSE);
443 g_return_val_if_fail (GST_DP_HEADER_PAYLOAD_TYPE (header) ==
444 GST_DP_PAYLOAD_CAPS, FALSE);
446 string = (gchar *) payload;
447 caps = gst_caps_from_string (string);
452 * gst_dp_event_from_packet:
453 * @header_length: the length of the packet header
454 * @header: the byte array of the packet header
455 * @payload: the byte array of the packet payload
457 * Creates a newly allocated #GstEvent from the given packet.
459 * Returns: %TRUE if the event was successfully created.
462 gst_dp_event_from_packet (guint header_length, const guint8 * header,
463 const guint8 * payload)
465 GstEvent *event = NULL;
468 g_return_val_if_fail (header, FALSE);
469 /* payload can be NULL, e.g. for an EOS event */
471 type = GST_DP_HEADER_PAYLOAD_TYPE (header) - GST_DP_PAYLOAD_EVENT_NONE;
473 case GST_EVENT_UNKNOWN:
474 g_warning ("Unknown event, ignoring");
477 case GST_EVENT_FLUSH:
478 case GST_EVENT_DISCONTINUOUS:
479 event = gst_event_new (type);
480 GST_EVENT_TIMESTAMP (event) = GST_DP_HEADER_TIMESTAMP (header);
485 gint64 offset, endoffset;
486 GstSeekAccuracy accuracy;
488 type = (GstSeekType) GST_READ_UINT32_BE (payload);
489 offset = (gint64) GST_READ_UINT64_BE (payload + 4);
490 endoffset = (gint64) GST_READ_UINT64_BE (payload + 12);
491 accuracy = (GstSeekAccuracy) GST_READ_UINT32_BE (payload + 20);
492 event = gst_event_new_segment_seek (type, offset, endoffset);
493 GST_EVENT_TIMESTAMP (event) = GST_DP_HEADER_TIMESTAMP (header);
494 GST_EVENT_SEEK_ACCURACY (event) = accuracy;
500 case GST_EVENT_NAVIGATION:
502 g_warning ("Unhandled event type %d, ignoring", GST_EVENT_TYPE (event));
505 g_warning ("Unknown event type %d, ignoring", GST_EVENT_TYPE (event));
513 * gst_dp_validate_header:
514 * @header_length: the length of the packet header
515 * @header: the byte array of the packet header
517 * Validates the given packet header by checking the CRC checksum.
519 * Returns: %TRUE if the CRC matches, or no CRC checksum is present.
522 gst_dp_validate_header (guint header_length, const guint8 * header)
524 guint16 crc_read, crc_calculated;
526 if (!(GST_DP_HEADER_FLAGS (header) & GST_DP_HEADER_FLAG_CRC_HEADER))
528 crc_read = GST_DP_HEADER_CRC_HEADER (header);
529 /* don't included the last two crc fields for the crc check */
530 crc_calculated = gst_dp_crc (header, header_length - 4);
531 if (crc_read != crc_calculated) {
532 GST_WARNING ("header crc mismatch: read %02x, calculated %02x", crc_read,
536 GST_LOG ("header crc validation: %02x", crc_read);
541 * gst_dp_validate_payload:
542 * @header_length: the length of the packet header
543 * @header: the byte array of the packet header
544 * @payload: the byte array of the packet payload
546 * Validates the given packet payload using the given packet header
547 * by checking the CRC checksum.
549 * Returns: %TRUE if the CRC matches, or no CRC checksum is present.
552 gst_dp_validate_payload (guint header_length, const guint8 * header,
553 const guint8 * payload)
555 guint16 crc_read, crc_calculated;
557 if (!(GST_DP_HEADER_FLAGS (header) & GST_DP_HEADER_FLAG_CRC_PAYLOAD))
559 crc_read = GST_DP_HEADER_CRC_PAYLOAD (header);
560 crc_calculated = gst_dp_crc (payload, GST_DP_HEADER_PAYLOAD_LENGTH (header));
561 if (crc_read != crc_calculated) {
562 GST_WARNING ("payload crc mismatch: read %02x, calculated %02x", crc_read,
566 GST_LOG ("payload crc validation: %02x", crc_read);
571 * gst_dp_validate_packet:
572 * @header_length: the length of the packet header
573 * @header: the byte array of the packet header
574 * @payload: the byte array of the packet payload
576 * Validates the given packet by checking version information and checksums.
578 * Returns: %TRUE if the packet validates.
581 gst_dp_validate_packet (guint header_length, const guint8 * header,
582 const guint8 * payload)
584 if (!gst_dp_validate_header (header_length, header))
586 if (!gst_dp_validate_payload (header_length, header, payload))
592 /*** PLUGIN STUFF ***/
594 plugin_init (GstPlugin * plugin)
601 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
604 "a data protocol to serialize buffers, caps and events",
605 plugin_init, VERSION, GST_LICENSE, GST_PACKAGE, GST_ORIGIN)