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_register, crc_table[256];
54 unsigned long i, j, k;
57 for (i = 0; i < 256; i++) {
60 j = j & 0x8000 ? (j << 1) ^ POLY : j << 1;
63 crc_table[i] = (guint16) j;
68 crc_register = CRC_INIT; /* always init register */
72 crc_register = (guint16) ((crc_register << 8) ^
73 crc_table[((crc_register >> 8) & 0x00ff) ^ *buffer++]);
75 return (0xffff ^ crc_register);
78 /* debugging function; dumps byte array values per 8 bytes */
79 /* FIXME: would be nice to merge this with gst_util_dump_mem () */
81 gst_dp_dump_byte_array (guint8 * array, guint length)
84 int n = 8; /* number of bytes per line */
85 gchar *line = g_malloc (3 * n);
87 GST_LOG ("dumping byte array of length %d", length);
88 for (i = 0; i < length; ++i) {
89 g_sprintf (line + 3 * (i % n), "%02x ", array[i]);
90 if (i % n == (n - 1)) {
91 GST_LOG ("%03d: %s", i - (n - 1), line);
95 GST_LOG ("%03d: %s", (i / n) * n, line);
103 * Initialize GStreamer Data Protocol library.
105 * Should be called before using these functions; either from source linking
106 * to this source file or from plugin_init.
111 static gboolean _gst_dp_initialized = FALSE;
113 if (_gst_dp_initialized)
116 _gst_dp_initialized = TRUE;
118 GST_DEBUG_CATEGORY_INIT (data_protocol_debug, "gdp", 0,
119 "GStreamer Data Protocol");
122 /*** PUBLIC FUNCTIONS ***/
125 * gst_dp_header_payload_length:
126 * @header: the byte header of the packet array
128 * Returns: the length of the payload this header describes.
131 gst_dp_header_payload_length (const guint8 * header)
133 return GST_DP_HEADER_PAYLOAD_LENGTH (header);
137 * gst_dp_header_payload_type:
138 * @header: the byte header of the packet array
140 * Returns: the #GstDPPayloadType the payload this header describes.
143 gst_dp_header_payload_type (const guint8 * header)
145 return GST_DP_HEADER_PAYLOAD_TYPE (header);
149 * gst_dp_header_from_buffer:
150 * @buffer: a #GstBuffer to create a header for
151 * @flags: the #GDPHeaderFlags to create the header with
152 * @length: a guint pointer to store the header length in
153 * @header: a guint8 * pointer to store a newly allocated header byte array in
155 * Creates a GDP header from the given buffer.
157 * Returns: %TRUE if the header was successfully created
161 gst_dp_header_from_buffer (const GstBuffer * buffer, GstDPHeaderFlag flags,
162 guint * length, guint8 ** header)
168 g_return_val_if_fail (GST_IS_BUFFER (buffer), FALSE);
169 g_return_val_if_fail (GST_BUFFER_REFCOUNT_VALUE (buffer) > 0, FALSE);
170 g_return_val_if_fail (header, FALSE);
172 *length = GST_DP_HEADER_LENGTH;
173 h = g_malloc (GST_DP_HEADER_LENGTH);
175 /* version, flags, type */
176 h[0] = (guint8) GST_DP_VERSION_MAJOR;
177 h[1] = (guint8) GST_DP_VERSION_MINOR;
178 h[2] = (guint8) flags;
179 h[3] = GST_DP_PAYLOAD_BUFFER;
181 /* buffer properties */
182 GST_WRITE_UINT32_BE (h + 4, GST_BUFFER_SIZE (buffer));
183 GST_WRITE_UINT64_BE (h + 8, GST_BUFFER_TIMESTAMP (buffer));
184 GST_WRITE_UINT64_BE (h + 16, GST_BUFFER_DURATION (buffer));
185 GST_WRITE_UINT64_BE (h + 24, GST_BUFFER_OFFSET (buffer));
186 GST_WRITE_UINT64_BE (h + 32, GST_BUFFER_OFFSET_END (buffer));
189 /* we only copy KEY_UNIT and IN_CAPS flags */
190 flags_mask = GST_DATA_FLAG_SHIFT (GST_BUFFER_KEY_UNIT) |
191 GST_DATA_FLAG_SHIFT (GST_BUFFER_IN_CAPS);
193 GST_WRITE_UINT16_BE (h + 40, GST_BUFFER_FLAGS (buffer) & flags_mask);
196 GST_WRITE_UINT16_BE (h + 42, (guint64) 0);
197 GST_WRITE_UINT32_BE (h + 44, (guint64) 0);
198 GST_WRITE_UINT64_BE (h + 48, (guint64) 0);
202 if (flags & GST_DP_HEADER_FLAG_CRC_HEADER) {
203 /* we don't crc the last four bytes of the header since they are crc's */
204 crc = gst_dp_crc (h, 56);
206 GST_WRITE_UINT16_BE (h + 56, crc);
209 if (flags & GST_DP_HEADER_FLAG_CRC_PAYLOAD) {
210 crc = gst_dp_crc (GST_BUFFER_DATA (buffer), GST_BUFFER_SIZE (buffer));
212 GST_WRITE_UINT16_BE (h + 58, crc);
214 GST_LOG ("created header from buffer:");
215 gst_dp_dump_byte_array (h, GST_DP_HEADER_LENGTH);
221 * gst_dp_packet_from_caps:
222 * @caps: a #GstCaps to create a packet for
223 * @flags: the #GDPHeaderFlags to create the header with
224 * @length: a guint pointer to store the header length in
225 * @header: a guint8 pointer to store a newly allocated header byte array in
226 * @payload: a guint8 pointer to store a newly allocated payload byte array in
228 * Creates a GDP packet from the given caps.
230 * Returns: %TRUE if the packet was successfully created
233 gst_dp_packet_from_caps (const GstCaps * caps, GstDPHeaderFlag flags,
234 guint * length, guint8 ** header, guint8 ** payload)
240 /* FIXME: GST_IS_CAPS doesn't work
241 g_return_val_if_fail (GST_IS_CAPS (caps), FALSE); */
242 g_return_val_if_fail (caps, FALSE);
243 g_return_val_if_fail (header, FALSE);
244 g_return_val_if_fail (payload, FALSE);
246 *length = GST_DP_HEADER_LENGTH;
247 h = g_malloc (GST_DP_HEADER_LENGTH);
249 string = gst_caps_to_string (caps);
251 /* version, flags, type */
252 h[0] = (guint8) GST_DP_VERSION_MAJOR;
253 h[1] = (guint8) GST_DP_VERSION_MINOR;
254 h[2] = (guint8) flags;
255 h[3] = GST_DP_PAYLOAD_CAPS;
257 /* buffer properties */
258 GST_WRITE_UINT32_BE (h + 4, strlen (string) + 1); /* include trailing 0 */
259 GST_WRITE_UINT64_BE (h + 8, (guint64) 0);
260 GST_WRITE_UINT64_BE (h + 16, (guint64) 0);
261 GST_WRITE_UINT64_BE (h + 24, (guint64) 0);
262 GST_WRITE_UINT64_BE (h + 32, (guint64) 0);
265 GST_WRITE_UINT64_BE (h + 40, (guint64) 0);
266 GST_WRITE_UINT64_BE (h + 48, (guint64) 0);
270 if (flags & GST_DP_HEADER_FLAG_CRC_HEADER) {
271 crc = gst_dp_crc (h, 56);
273 GST_WRITE_UINT16_BE (h + 56, crc);
276 if (flags & GST_DP_HEADER_FLAG_CRC_PAYLOAD) {
277 crc = gst_dp_crc (string, strlen (string) + 1);
279 GST_WRITE_UINT16_BE (h + 58, crc);
281 GST_LOG ("created header from caps:");
282 gst_dp_dump_byte_array (h, GST_DP_HEADER_LENGTH);
289 * gst_dp_packet_from_event:
290 * @event: a #GstEvent to create a packet for
291 * @flags: the #GDPHeaderFlags to create the header with
292 * @length: a guint pointer to store the header length in
293 * @header: a guint8 pointer to store a newly allocated header byte array in
294 * @payload: a guint8 pointer to store a newly allocated payload byte array in
296 * Creates a GDP packet from the given event.
298 * Returns: %TRUE if the packet was successfully created
301 gst_dp_packet_from_event (const GstEvent * event, GstDPHeaderFlag flags,
302 guint * length, guint8 ** header, guint8 ** payload)
306 guint pl_length; /* length of payload */
308 g_return_val_if_fail (event, FALSE);
309 g_return_val_if_fail (GST_IS_EVENT (event), FALSE);
310 g_return_val_if_fail (header, FALSE);
311 g_return_val_if_fail (payload, FALSE);
313 *length = GST_DP_HEADER_LENGTH;
314 h = g_malloc0 (GST_DP_HEADER_LENGTH);
316 /* first construct payload, since we need the length */
317 switch (GST_EVENT_TYPE (event)) {
318 case GST_EVENT_UNKNOWN:
319 g_warning ("Unknown event, ignoring");
324 case GST_EVENT_FLUSH:
325 case GST_EVENT_EMPTY:
326 case GST_EVENT_DISCONTINUOUS:
327 GST_WRITE_UINT64_BE (h + 8, GST_EVENT_TIMESTAMP (event));
332 pl_length = 4 + 8 + 4;
333 *payload = g_malloc0 (pl_length);
334 GST_WRITE_UINT32_BE (*payload, (guint32) GST_EVENT_SEEK_TYPE (event));
335 GST_WRITE_UINT64_BE (*payload + 4,
336 (guint64) GST_EVENT_SEEK_OFFSET (event));
337 GST_WRITE_UINT32_BE (*payload + 12,
338 (guint32) GST_EVENT_SEEK_ACCURACY (event));
340 case GST_EVENT_SEEK_SEGMENT:
341 pl_length = 4 + 8 + 8 + 4;
342 *payload = g_malloc0 (pl_length);
343 GST_WRITE_UINT32_BE (*payload, (guint32) GST_EVENT_SEEK_TYPE (event));
344 GST_WRITE_UINT64_BE (*payload + 4,
345 (guint64) GST_EVENT_SEEK_OFFSET (event));
346 GST_WRITE_UINT64_BE (*payload + 12,
347 (guint64) GST_EVENT_SEEK_ENDOFFSET (event));
348 GST_WRITE_UINT32_BE (*payload + 20,
349 (guint32) GST_EVENT_SEEK_ACCURACY (event));
352 case GST_EVENT_SEGMENT_DONE:
355 case GST_EVENT_FILLER:
356 case GST_EVENT_TS_OFFSET:
357 case GST_EVENT_INTERRUPT:
358 case GST_EVENT_NAVIGATION:
360 g_warning ("Unhandled event type %d, ignoring", GST_EVENT_TYPE (event));
363 g_warning ("Unknown event type %d, ignoring", GST_EVENT_TYPE (event));
369 /* version, flags, type */
370 h[0] = (guint8) GST_DP_VERSION_MAJOR;
371 h[1] = (guint8) GST_DP_VERSION_MINOR;
372 h[2] = (guint8) flags;
373 h[3] = GST_DP_PAYLOAD_EVENT_NONE + GST_EVENT_TYPE (event);
376 GST_WRITE_UINT32_BE (h + 4, (guint32) pl_length);
378 GST_WRITE_UINT64_BE (h + 8, GST_EVENT_TIMESTAMP (event));
381 GST_WRITE_UINT64_BE (h + 40, (guint64) 0);
382 GST_WRITE_UINT64_BE (h + 48, (guint64) 0);
386 if (flags & GST_DP_HEADER_FLAG_CRC_HEADER) {
387 crc = gst_dp_crc (h, 56);
389 GST_WRITE_UINT16_BE (h + 56, crc);
392 /* events can have a NULL payload */
393 if (*payload && flags & GST_DP_HEADER_FLAG_CRC_PAYLOAD) {
394 crc = gst_dp_crc (*payload, strlen (*payload) + 1);
396 GST_WRITE_UINT16_BE (h + 58, crc);
398 GST_LOG ("created header from event:");
399 gst_dp_dump_byte_array (h, GST_DP_HEADER_LENGTH);
406 * gst_dp_buffer_from_header:
407 * @header_length: the length of the packet header
408 * @header: the byte array of the packet header
410 * Creates a newly allocated #GstBuffer from the given header.
411 * The buffer data needs to be copied into it before validating.
413 * Use this function if you want to pre-allocate a buffer based on the
414 * packet header to read the packet payload in to.
416 * Returns: %TRUE if the buffer was successfully created
419 gst_dp_buffer_from_header (guint header_length, const guint8 * header)
423 g_return_val_if_fail (GST_DP_HEADER_PAYLOAD_TYPE (header) ==
424 GST_DP_PAYLOAD_BUFFER, FALSE);
426 gst_buffer_new_and_alloc ((guint) GST_DP_HEADER_PAYLOAD_LENGTH (header));
427 GST_BUFFER_TIMESTAMP (buffer) = GST_DP_HEADER_TIMESTAMP (header);
428 GST_BUFFER_DURATION (buffer) = GST_DP_HEADER_DURATION (header);
429 GST_BUFFER_OFFSET (buffer) = GST_DP_HEADER_OFFSET (header);
430 GST_BUFFER_OFFSET_END (buffer) = GST_DP_HEADER_OFFSET_END (header);
431 GST_BUFFER_FLAGS (buffer) = GST_DP_HEADER_BUFFER_FLAGS (header);
437 * gst_dp_caps_from_packet:
438 * @header_length: the length of the packet header
439 * @header: the byte array of the packet header
440 * @payload: the byte array of the packet payload
442 * Creates a newly allocated #GstCaps from the given packet.
444 * Returns: %TRUE if the caps was successfully created
447 gst_dp_caps_from_packet (guint header_length, const guint8 * header,
448 const guint8 * payload)
453 g_return_val_if_fail (header, FALSE);
454 g_return_val_if_fail (payload, FALSE);
455 g_return_val_if_fail (GST_DP_HEADER_PAYLOAD_TYPE (header) ==
456 GST_DP_PAYLOAD_CAPS, FALSE);
459 caps = gst_caps_from_string (string);
464 * gst_dp_event_from_packet:
465 * @header_length: the length of the packet header
466 * @header: the byte array of the packet header
467 * @payload: the byte array of the packet payload
469 * Creates a newly allocated #GstEvent from the given packet.
471 * Returns: %TRUE if the event was successfully created
474 gst_dp_event_from_packet (guint header_length, const guint8 * header,
475 const guint8 * payload)
477 GstEvent *event = NULL;
480 g_return_val_if_fail (header, FALSE);
481 /* payload can be NULL, e.g. for an EOS event */
483 type = GST_DP_HEADER_PAYLOAD_TYPE (header) - GST_DP_PAYLOAD_EVENT_NONE;
485 case GST_EVENT_UNKNOWN:
486 g_warning ("Unknown event, ignoring");
489 case GST_EVENT_FLUSH:
490 case GST_EVENT_EMPTY:
491 case GST_EVENT_DISCONTINUOUS:
492 event = gst_event_new (type);
493 GST_EVENT_TIMESTAMP (event) = GST_DP_HEADER_TIMESTAMP (header);
499 GstSeekAccuracy accuracy;
501 type = (GstSeekType) GST_READ_UINT32_BE (payload);
502 offset = (gint64) GST_READ_UINT64_BE (payload + 4);
503 accuracy = (GstSeekAccuracy) GST_READ_UINT32_BE (payload + 12);
504 event = gst_event_new_seek (type, offset);
505 GST_EVENT_TIMESTAMP (event) = GST_DP_HEADER_TIMESTAMP (header);
506 GST_EVENT_SEEK_ACCURACY (event) = accuracy;
509 case GST_EVENT_SEEK_SEGMENT:
512 gint64 offset, endoffset;
513 GstSeekAccuracy accuracy;
515 type = (GstSeekType) GST_READ_UINT32_BE (payload);
516 offset = (gint64) GST_READ_UINT64_BE (payload + 4);
517 endoffset = (gint64) GST_READ_UINT64_BE (payload + 12);
518 accuracy = (GstSeekAccuracy) GST_READ_UINT32_BE (payload + 20);
519 event = gst_event_new_segment_seek (type, offset, endoffset);
520 GST_EVENT_TIMESTAMP (event) = GST_DP_HEADER_TIMESTAMP (header);
521 GST_EVENT_SEEK_ACCURACY (event) = accuracy;
525 case GST_EVENT_SEGMENT_DONE:
528 case GST_EVENT_FILLER:
529 case GST_EVENT_TS_OFFSET:
530 case GST_EVENT_INTERRUPT:
531 case GST_EVENT_NAVIGATION:
533 g_warning ("Unhandled event type %d, ignoring", GST_EVENT_TYPE (event));
536 g_warning ("Unknown event type %d, ignoring", GST_EVENT_TYPE (event));
544 * gst_dp_validate_header:
545 * @header_length: the length of the packet header
546 * @header: the byte array of the packet header
548 * Validates the given packet header by checking the CRC checksum.
550 * Returns: %TRUE if the CRC matches, or no CRC checksum is present
553 gst_dp_validate_header (guint header_length, const guint8 * header)
555 guint16 crc_read, crc_calculated;
557 if (!(GST_DP_HEADER_FLAGS (header) & GST_DP_HEADER_FLAG_CRC_HEADER))
559 crc_read = GST_DP_HEADER_CRC_HEADER (header);
560 /* don't included the last two crc fields for the crc check */
561 crc_calculated = gst_dp_crc (header, header_length - 4);
562 if (crc_read != crc_calculated) {
563 GST_WARNING ("header crc mismatch: read %02x, calculated %02x", crc_read,
567 GST_LOG ("header crc validation: %02x", crc_read);
572 * gst_dp_validate_payload:
573 * @header_length: the length of the packet header
574 * @header: the byte array of the packet header
575 * @payload: the byte array of the packet payload
577 * Validates the given packet payload using the given packet header
578 * by checking the CRC checksum.
580 * Returns: %TRUE if the CRC matches, or no CRC checksum is present
583 gst_dp_validate_payload (guint header_length, const guint8 * header,
584 const guint8 * payload)
586 guint16 crc_read, crc_calculated;
588 if (!(GST_DP_HEADER_FLAGS (header) & GST_DP_HEADER_FLAG_CRC_PAYLOAD))
590 crc_read = GST_DP_HEADER_CRC_PAYLOAD (header);
591 crc_calculated = gst_dp_crc (payload, GST_DP_HEADER_PAYLOAD_LENGTH (header));
592 if (crc_read != crc_calculated) {
593 GST_WARNING ("payload crc mismatch: read %02x, calculated %02x", crc_read,
597 GST_LOG ("payload crc validation: %02x", crc_read);
602 * gst_dp_validate_packet:
603 * @header_length: the length of the packet header
604 * @header: the byte array of the packet header
605 * @payload: the byte array of the packet payload
607 * Validates the given packet by checking version information and checksums.
609 * Returns: %TRUE if the packet validates
612 gst_dp_validate_packet (guint header_length, const guint8 * header,
613 const guint8 * payload)
615 if (!gst_dp_validate_header (header_length, header))
617 if (!gst_dp_validate_payload (header_length, header, payload))
623 /*** PLUGIN STUFF ***/
625 plugin_init (GstPlugin * plugin)
632 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
635 "a data protocol to serialize buffers, caps and events",
636 plugin_init, VERSION, GST_LICENSE, GST_PACKAGE, GST_ORIGIN)