1 /* GStreamer RTP ASF depayloader
2 * Copyright (C) 2006 Tim-Philipp Müller <tim centricular net>
3 * 2009 Wim Taymans <wim.taymans@gmail.com>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
25 #include "gstasfelements.h"
26 #include "gstrtpasfdepay.h"
27 #include <gst/rtp/gstrtpbuffer.h>
32 GST_DEBUG_CATEGORY_STATIC (rtpasfdepayload_debug);
33 #define GST_CAT_DEFAULT rtpasfdepayload_debug
35 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
38 GST_STATIC_CAPS ("video/x-ms-asf")
41 /* Other parameters: config, maxps */
43 "application/x-rtp, " \
44 "media = (string) { \"application\", \"video\", \"audio\" }, " \
45 "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", " \
46 "clock-rate = (int) [1, MAX ], " \
47 "encoding-name = (string) \"X-ASF-PF\""
49 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
52 GST_STATIC_CAPS (SINK_CAPS)
55 #define gst_rtp_asf_depay_parent_class parent_class
56 G_DEFINE_TYPE (GstRtpAsfDepay, gst_rtp_asf_depay, GST_TYPE_RTP_BASE_DEPAYLOAD);
57 GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (rtpasfdepay, "rtpasfdepay",
58 GST_RANK_MARGINAL, GST_TYPE_RTP_ASF_DEPAY, asf_element_init (plugin));
60 static void gst_rtp_asf_depay_finalize (GObject * object);
62 static GstStateChangeReturn gst_rtp_asf_depay_change_state (GstElement *
63 element, GstStateChange transition);
65 static gboolean gst_rtp_asf_depay_setcaps (GstRTPBaseDepayload * depay,
67 static GstBuffer *gst_rtp_asf_depay_process (GstRTPBaseDepayload * basedepay,
71 gst_rtp_asf_depay_class_init (GstRtpAsfDepayClass * klass)
73 GObjectClass *gobject_class;
74 GstElementClass *gstelement_class;
75 GstRTPBaseDepayloadClass *gstrtpbasedepayload_class;
77 gobject_class = (GObjectClass *) klass;
78 gstelement_class = (GstElementClass *) klass;
79 gstrtpbasedepayload_class = (GstRTPBaseDepayloadClass *) klass;
81 gst_element_class_add_static_pad_template (gstelement_class, &src_factory);
82 gst_element_class_add_static_pad_template (gstelement_class, &sink_factory);
84 gst_element_class_set_static_metadata (gstelement_class,
85 "RTP ASF packet depayloader", "Codec/Depayloader/Network",
86 "Extracts ASF streams from RTP",
87 "Tim-Philipp Müller <tim centricular net>, "
88 "Wim Taymans <wim.taymans@gmail.com>");
90 gobject_class->finalize = gst_rtp_asf_depay_finalize;
92 gstelement_class->change_state =
93 GST_DEBUG_FUNCPTR (gst_rtp_asf_depay_change_state);
95 gstrtpbasedepayload_class->set_caps =
96 GST_DEBUG_FUNCPTR (gst_rtp_asf_depay_setcaps);
97 gstrtpbasedepayload_class->process =
98 GST_DEBUG_FUNCPTR (gst_rtp_asf_depay_process);
100 GST_DEBUG_CATEGORY_INIT (rtpasfdepayload_debug, "rtpasfdepayload", 0,
101 "RTP asf depayloader element");
105 gst_rtp_asf_depay_init (GstRtpAsfDepay * depay)
107 depay->adapter = gst_adapter_new ();
111 gst_rtp_asf_depay_finalize (GObject * object)
113 GstRtpAsfDepay *depay;
115 depay = GST_RTP_ASF_DEPAY (object);
117 g_object_unref (depay->adapter);
119 G_OBJECT_CLASS (parent_class)->finalize (object);
122 static const guint8 asf_marker[16] = { 0x30, 0x26, 0xb2, 0x75, 0x8e, 0x66,
123 0xcf, 0x11, 0xa6, 0xd9, 0x00, 0xaa, 0x00, 0x62, 0xce, 0x6c
127 gst_rtp_asf_depay_setcaps (GstRTPBaseDepayload * depayload, GstCaps * caps)
129 GstRtpAsfDepay *depay;
131 const gchar *config_str, *ps_string;
138 depay = GST_RTP_ASF_DEPAY (depayload);
140 s = gst_caps_get_structure (caps, 0);
142 if (!gst_structure_get_int (s, "clock-rate", &clock_rate) || clock_rate < 0)
144 depayload->clock_rate = clock_rate;
146 /* config contains the asf headers in base64 coding */
147 config_str = gst_structure_get_string (s, "config");
148 if (config_str == NULL || *config_str == '\0')
151 ps_string = gst_structure_get_string (s, "maxps");
152 if (ps_string == NULL || *ps_string == '\0')
155 if (depay->packet_size) {
156 /* header sent again following seek;
157 * discard to avoid confusing upstream */
158 if (depay->packet_size == atoi (ps_string)) {
159 goto duplicate_header;
161 /* since we should fiddle with downstream state to handle this */
162 goto refuse_renegotiation;
165 depay->packet_size = atoi (ps_string);
166 if (depay->packet_size <= 16)
167 goto invalid_packetsize;
169 headers = (guint8 *) g_base64_decode (config_str, &headers_len);
171 if (headers == NULL || headers_len < 16
172 || memcmp (headers, asf_marker, 16) != 0)
173 goto invalid_headers;
175 src_caps = gst_caps_new_empty_simple ("video/x-ms-asf");
176 gst_pad_set_caps (depayload->srcpad, src_caps);
177 gst_caps_unref (src_caps);
179 buf = gst_buffer_new ();
180 gst_buffer_append_memory (buf,
181 gst_memory_new_wrapped (0, headers, headers_len, 0, headers_len, headers,
184 gst_rtp_base_depayload_push (depayload, buf);
191 GST_WARNING_OBJECT (depay, "caps without 'config' field with asf headers");
196 GST_WARNING_OBJECT (depay, "caps without 'maxps' (packet size) field");
201 GST_WARNING_OBJECT (depay, "packet size %u invalid", depay->packet_size);
206 GST_WARNING_OBJECT (depay, "headers don't look like valid ASF headers");
212 GST_DEBUG_OBJECT (depayload, "discarding duplicate header");
215 refuse_renegotiation:
217 GST_WARNING_OBJECT (depayload, "cannot renegotiate to different header");
223 field_size (guint8 field)
226 /* DWORD - 32 bits */
245 /* Set the padding field to te correct value as the spec
246 * says it should be se to 0 in the rtp packets
249 gst_rtp_asf_depay_update_padding (GstRtpAsfDepay * depayload, GstBuffer * buf)
261 plen = gst_buffer_get_size (buf);
262 if (plen == depayload->packet_size)
265 padding = depayload->packet_size - plen;
267 GST_LOG_OBJECT (depayload,
268 "padding buffer size %" G_GSIZE_FORMAT " to packet size %d", plen,
269 depayload->packet_size);
271 result = gst_buffer_new_and_alloc (depayload->packet_size);
273 gst_buffer_map (result, &map, GST_MAP_READ);
275 memset (data + plen, 0, padding);
277 gst_buffer_extract (buf, 0, data, plen);
278 gst_buffer_unref (buf);
280 aux = data[offset++];
284 GST_WARNING_OBJECT (depayload, "Error correction length type should be "
286 /* this packet doesn't follow the spec */
287 gst_buffer_unmap (result, &map);
290 err_len = aux & 0x0F;
293 aux = data[offset++];
295 seq_type = (aux >> 1) & 0x3;
296 pad_type = (aux >> 3) & 0x3;
297 pkt_type = (aux >> 5) & 0x3;
299 offset += 1; /* skip property flags */
300 offset += field_size (pkt_type); /* skip packet length */
301 offset += field_size (seq_type); /* skip sequence field */
307 GST_WRITE_UINT32_LE (&(data[offset]), padding);
312 GST_WRITE_UINT16_LE (&(data[offset]), padding);
317 data[offset] = (guint8) padding;
325 gst_buffer_unmap (result, &map);
330 /* Docs: 'RTSP Protocol PDF' document from http://sdp.ppona.com/ (page 8) */
333 gst_rtp_asf_depay_process (GstRTPBaseDepayload * depayload, GstBuffer * buf)
335 GstRtpAsfDepay *depay;
336 const guint8 *payload;
338 gboolean S, L, R, D, I;
339 guint payload_len, hdr_len, offset;
341 GstClockTime timestamp;
342 GstRTPBuffer rtpbuf = { NULL };
344 depay = GST_RTP_ASF_DEPAY (depayload);
346 /* flush remaining data on discont */
347 if (GST_BUFFER_IS_DISCONT (buf)) {
348 GST_LOG_OBJECT (depay, "got DISCONT");
349 gst_adapter_clear (depay->adapter);
350 depay->discont = TRUE;
353 gst_rtp_buffer_map (buf, GST_MAP_READ, &rtpbuf);
354 timestamp = GST_BUFFER_TIMESTAMP (buf);
356 payload_len = gst_rtp_buffer_get_payload_len (&rtpbuf);
357 payload = gst_rtp_buffer_get_payload (&rtpbuf);
360 GST_LOG_OBJECT (depay, "got payload len of %u", payload_len);
365 /* packet header is at least 4 bytes */
370 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
371 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
372 * |S|L|R|D|I|RES | Length/Offset |
373 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
374 * | Relative Timestamp (optional) |
375 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
376 * | Duration (optional) |
377 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
378 * | LocationId (optional) |
379 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
381 * S: packet contains a keyframe.
382 * L: If 1, Length/Offset contains length, else contains the byte offset
383 * of the fragment's first byte counted from the beginning of the
384 * complete ASF data packet.
385 * R: relative timestamp present
386 * D: duration present
387 * I: locationid present
390 S = ((payload[0] & 0x80) != 0);
391 L = ((payload[0] & 0x40) != 0);
392 R = ((payload[0] & 0x20) != 0);
393 D = ((payload[0] & 0x10) != 0);
394 I = ((payload[0] & 0x08) != 0);
398 len_offs = (payload[1] << 16) | (payload[2] << 8) | payload[3];
401 GST_DEBUG ("Relative timestamp field present : %u",
402 GST_READ_UINT32_BE (payload + hdr_len));
406 GST_DEBUG ("Duration field present : %u",
407 GST_READ_UINT32_BE (payload + hdr_len));
411 GST_DEBUG ("LocationId field present : %u",
412 GST_READ_UINT32_BE (payload + hdr_len));
416 GST_LOG_OBJECT (depay, "S %d, L %d, R %d, D %d, I %d", S, L, R, D, I);
417 GST_LOG_OBJECT (depay, "payload_len:%d, hdr_len:%d, len_offs:%d",
418 payload_len, hdr_len, len_offs);
420 if (payload_len < hdr_len)
424 payload_len -= hdr_len;
429 /* L bit set, len contains the length of the packet */
430 packet_len = len_offs;
432 /* else it contains an offset which we don't handle yet */
433 GST_LOG_OBJECT (depay, "We have a fragmented packet");
434 packet_len = payload_len;
437 if (packet_len > payload_len)
438 packet_len = payload_len;
440 GST_LOG_OBJECT (depay, "packet len %u, payload len %u, packet_size:%u",
441 packet_len, payload_len, depay->packet_size);
447 /* Fragmented packet handling */
450 if (len_offs == (available = gst_adapter_available (depay->adapter))) {
451 /* fragment aligns with what we have, add it */
452 GST_LOG_OBJECT (depay, "collecting fragment");
454 gst_rtp_buffer_get_payload_subbuffer (&rtpbuf, offset, packet_len);
455 gst_adapter_push (depay->adapter, sub);
456 /* RTP marker bit M is set if this is last fragment */
457 if (gst_rtp_buffer_get_marker (&rtpbuf)) {
458 GST_LOG_OBJECT (depay, "last fragment, assembling packet");
460 gst_adapter_take_buffer (depay->adapter, available + packet_len);
464 GST_WARNING_OBJECT (depay, "Offset doesn't match previous data?!");
465 GST_DEBUG_OBJECT (depay, "clearing for re-sync");
466 gst_adapter_clear (depay->adapter);
468 GST_DEBUG_OBJECT (depay, "waiting for start of packet");
471 GST_LOG_OBJECT (depay, "collecting packet");
473 gst_rtp_buffer_get_payload_subbuffer (&rtpbuf, offset, packet_len);
476 /* If we haven't completed a full ASF packet, return */
480 outbuf = gst_rtp_asf_depay_update_padding (depay, outbuf);
483 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DELTA_UNIT);
485 if (depay->discont) {
486 GST_LOG_OBJECT (depay, "setting DISCONT");
487 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DISCONT);
488 depay->discont = FALSE;
491 GST_BUFFER_TIMESTAMP (outbuf) = timestamp;
493 gst_rtp_base_depayload_push (depayload, outbuf);
495 /* only apply the timestamp to the first buffer of this packet */
498 /* skip packet data */
499 payload += packet_len;
500 offset += packet_len;
501 payload_len -= packet_len;
502 } while (payload_len > 0);
504 gst_rtp_buffer_unmap (&rtpbuf);
511 gst_rtp_buffer_unmap (&rtpbuf);
512 GST_WARNING_OBJECT (depayload, "Payload too small, expected at least 4 "
513 "bytes for header, but got only %d bytes", payload_len);
518 static GstStateChangeReturn
519 gst_rtp_asf_depay_change_state (GstElement * element, GstStateChange trans)
521 GstStateChangeReturn ret;
522 GstRtpAsfDepay *depay;
524 depay = GST_RTP_ASF_DEPAY (element);
527 case GST_STATE_CHANGE_READY_TO_PAUSED:
528 gst_adapter_clear (depay->adapter);
529 depay->discont = TRUE;
535 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, trans);
538 case GST_STATE_CHANGE_PAUSED_TO_READY:
539 gst_adapter_clear (depay->adapter);