Debian Codegraph @

gst/asfdemux/gstrtpasfdepay.c

raw symbols SCIP
1 /* GStreamer RTP ASF depayloader
2 * Copyright (C) 2006 Tim-Philipp Müller <tim centricular net>
3 * 2009 Wim Taymans <[email protected]>
4 *
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.
9 *
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.
14 *
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.
19 */
20
21 #ifdef HAVE_CONFIG_H
22 # include "config.h"
23 #endif
24
25 #include "gstasfelements.h"
26 #include "gstrtpasfdepay.h"
27 #include <gst/rtp/gstrtpbuffer.h>
28
29 #include <string.h>
30 #include <stdlib.h>
31
32 GST_DEBUG_CATEGORY_STATIC (rtpasfdepayload_debug);
33 #define GST_CAT_DEFAULT rtpasfdepayload_debug
34
35 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
36 GST_PAD_SRC,
37 GST_PAD_ALWAYS,
38 GST_STATIC_CAPS ("video/x-ms-asf")
39 );
40
41 /* Other parameters: config, maxps */
42 #define SINK_CAPS \
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\""
48
49 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
50 GST_PAD_SINK,
51 GST_PAD_ALWAYS,
52 GST_STATIC_CAPS (SINK_CAPS)
53 );
54
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));
59
60 static void gst_rtp_asf_depay_finalize (GObject * object);
61
62 static GstStateChangeReturn gst_rtp_asf_depay_change_state (GstElement *
63 element, GstStateChange transition);
64
65 static gboolean gst_rtp_asf_depay_setcaps (GstRTPBaseDepayload * depay,
66 GstCaps * caps);
67 static GstBuffer *gst_rtp_asf_depay_process (GstRTPBaseDepayload * basedepay,
68 GstBuffer * buf);
69
70 static void
71 gst_rtp_asf_depay_class_init (GstRtpAsfDepayClass * klass)
72 {
73 GObjectClass *gobject_class;
74 GstElementClass *gstelement_class;
75 GstRTPBaseDepayloadClass *gstrtpbasedepayload_class;
76
77 gobject_class = (GObjectClass *) klass;
78 gstelement_class = (GstElementClass *) klass;
79 gstrtpbasedepayload_class = (GstRTPBaseDepayloadClass *) klass;
80
81 gst_element_class_add_static_pad_template (gstelement_class, &src_factory);
82 gst_element_class_add_static_pad_template (gstelement_class, &sink_factory);
83
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 <[email protected]>");
89
90 gobject_class->finalize = gst_rtp_asf_depay_finalize;
91
92 gstelement_class->change_state =
93 GST_DEBUG_FUNCPTR (gst_rtp_asf_depay_change_state);
94
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);
99
100 GST_DEBUG_CATEGORY_INIT (rtpasfdepayload_debug, "rtpasfdepayload", 0,
101 "RTP asf depayloader element");
102 }
103
104 static void
105 gst_rtp_asf_depay_init (GstRtpAsfDepay * depay)
106 {
107 gst_rtp_base_depayload_set_aggregate_hdrext_enabled (GST_RTP_BASE_DEPAYLOAD
108 (depay), TRUE);
109
110 depay->adapter = gst_adapter_new ();
111 }
112
113 static void
114 gst_rtp_asf_depay_finalize (GObject * object)
115 {
116 GstRtpAsfDepay *depay;
117
118 depay = GST_RTP_ASF_DEPAY (object);
119
120 g_object_unref (depay->adapter);
121
122 G_OBJECT_CLASS (parent_class)->finalize (object);
123 }
124
125 static const guint8 asf_marker[16] = { 0x30, 0x26, 0xb2, 0x75, 0x8e, 0x66,
126 0xcf, 0x11, 0xa6, 0xd9, 0x00, 0xaa, 0x00, 0x62, 0xce, 0x6c
127 };
128
129 static gboolean
130 gst_rtp_asf_depay_setcaps (GstRTPBaseDepayload * depayload, GstCaps * caps)
131 {
132 GstRtpAsfDepay *depay;
133 GstStructure *s;
134 const gchar *config_str, *ps_string;
135 GstBuffer *buf;
136 GstCaps *src_caps;
137 guint8 *headers;
138 gsize headers_len;
139 gint clock_rate;
140
141 depay = GST_RTP_ASF_DEPAY (depayload);
142
143 s = gst_caps_get_structure (caps, 0);
144
145 if (!gst_structure_get_int (s, "clock-rate", &clock_rate) || clock_rate < 0)
146 clock_rate = 1000;
147 depayload->clock_rate = clock_rate;
148
149 /* config contains the asf headers in base64 coding */
150 config_str = gst_structure_get_string (s, "config");
151 if (config_str == NULL || *config_str == '\0')
152 goto no_config;
153
154 ps_string = gst_structure_get_string (s, "maxps");
155 if (ps_string == NULL || *ps_string == '\0')
156 goto no_packetsize;
157
158 if (depay->packet_size) {
159 /* header sent again following seek;
160 * discard to avoid confusing upstream */
161 if (depay->packet_size == atoi (ps_string)) {
162 goto duplicate_header;
163 } else {
164 /* since we should fiddle with downstream state to handle this */
165 goto refuse_renegotiation;
166 }
167 } else
168 depay->packet_size = atoi (ps_string);
169 if (depay->packet_size <= 16)
170 goto invalid_packetsize;
171
172 headers = (guint8 *) g_base64_decode (config_str, &headers_len);
173
174 if (headers == NULL || headers_len < 16
175 || memcmp (headers, asf_marker, 16) != 0)
176 goto invalid_headers;
177
178 src_caps = gst_caps_new_empty_simple ("video/x-ms-asf");
179 gst_pad_set_caps (depayload->srcpad, src_caps);
180 gst_caps_unref (src_caps);
181
182 buf = gst_buffer_new ();
183 gst_buffer_append_memory (buf,
184 gst_memory_new_wrapped (0, headers, headers_len, 0, headers_len, headers,
185 g_free));
186
187 gst_rtp_base_depayload_push (depayload, buf);
188
189 return TRUE;
190
191 /* ERRORS */
192 no_config:
193 {
194 GST_WARNING_OBJECT (depay, "caps without 'config' field with asf headers");
195 return FALSE;
196 }
197 no_packetsize:
198 {
199 GST_WARNING_OBJECT (depay, "caps without 'maxps' (packet size) field");
200 return FALSE;
201 }
202 invalid_packetsize:
203 {
204 GST_WARNING_OBJECT (depay, "packet size %u invalid", depay->packet_size);
205 return FALSE;
206 }
207 invalid_headers:
208 {
209 GST_WARNING_OBJECT (depay, "headers don't look like valid ASF headers");
210 g_free (headers);
211 return FALSE;
212 }
213 duplicate_header:
214 {
215 GST_DEBUG_OBJECT (depayload, "discarding duplicate header");
216 return TRUE;
217 }
218 refuse_renegotiation:
219 {
220 GST_WARNING_OBJECT (depayload, "cannot renegotiate to different header");
221 return FALSE;
222 }
223 }
224
225 static gint
226 field_size (guint8 field)
227 {
228 switch (field) {
229 /* DWORD - 32 bits */
230 case 3:
231 return 4;
232
233 /* WORD - 16 bits */
234 case 2:
235 return 2;
236
237 /* BYTE - 8 bits */
238 case 1:
239 return 1;
240
241 /* non-exitent */
242 case 0:
243 default:
244 return 0;
245 }
246 }
247
248 /* Update the packet with the required amount of padding. For RTP packets the
249 * padding is stripped off and here we need to add it back to create valid
250 * ASF packets again. This requires allocating a packet_size large buffer,
251 * adding zero padding and rewriting the padding length field with the correct
252 * value. */
253 static GstBuffer *
254 gst_rtp_asf_depay_update_padding (GstRtpAsfDepay * depayload, GstBuffer * buf)
255 {
256 GstBuffer *result;
257 GstMapInfo map;
258 guint8 *data;
259 guint offset = 0;
260 guint8 aux;
261 guint8 seq_type;
262 guint8 pad_type;
263 guint8 pkt_type;
264 gsize plen, padding;
265
266 plen = gst_buffer_get_size (buf);
267 if (plen == depayload->packet_size)
268 return buf;
269
270 padding = depayload->packet_size - plen;
271
272 GST_LOG_OBJECT (depayload,
273 "padding buffer size %" G_GSIZE_FORMAT " to packet size %u", plen,
274 depayload->packet_size);
275
276 result = gst_buffer_new_and_alloc (depayload->packet_size);
277
278 gst_buffer_map (result, &map, GST_MAP_READWRITE);
279 data = map.data;
280 memset (data + plen, 0, padding);
281
282 gst_buffer_extract (buf, 0, data, plen);
283 gst_buffer_unref (buf);
284
285 if (offset + 1 > depayload->packet_size)
286 goto malformed;
287 aux = data[offset++];
288 if (aux & 0x80) {
289 guint8 err_len = 0;
290 if (aux & 0x60) {
291 GST_WARNING_OBJECT (depayload, "Error correction length type should be "
292 "set to 0");
293 /* this packet doesn't follow the spec */
294 gst_buffer_unmap (result, &map);
295 return result;
296 }
297 err_len = aux & 0x0F;
298 offset += err_len;
299
300 if (offset + 1 > depayload->packet_size)
301 goto malformed;
302 aux = data[offset++];
303 }
304 seq_type = (aux >> 1) & 0x3;
305 pad_type = (aux >> 3) & 0x3;
306 pkt_type = (aux >> 5) & 0x3;
307
308 offset += 1; /* skip property flags */
309 offset += field_size (pkt_type); /* skip packet length */
310 offset += field_size (seq_type); /* skip sequence field */
311
312 /* write padding */
313 switch (pad_type) {
314 /* DWORD */
315 case 3:
316 if (offset + 4 > depayload->packet_size)
317 goto malformed;
318 GST_WRITE_UINT32_LE (&(data[offset]), padding);
319 break;
320
321 /* WORD */
322 case 2:
323 if (offset + 2 > depayload->packet_size)
324 goto malformed;
325 GST_WRITE_UINT16_LE (&(data[offset]), padding);
326 break;
327
328 /* BYTE */
329 case 1:
330 if (offset + 1 > depayload->packet_size)
331 goto malformed;
332 data[offset] = (guint8) padding;
333 break;
334
335 /* non-existent */
336 case 0:
337 default:
338 break;
339 }
340 gst_buffer_unmap (result, &map);
341
342 return result;
343
344 malformed:
345 {
346 GST_WARNING_OBJECT (depayload, "ASF packet too small to contain header");
347 gst_buffer_unmap (result, &map);
348 gst_buffer_unref (result);
349 return NULL;
350 }
351 }
352
353 /* Docs: 'RTSP Protocol PDF' document from http://sdp.ppona.com/ (page 8) */
354
355 static GstBuffer *
356 gst_rtp_asf_depay_process (GstRTPBaseDepayload * depayload, GstBuffer * buf)
357 {
358 GstRtpAsfDepay *depay;
359 const guint8 *payload;
360 GstBuffer *outbuf;
361 GstBufferList *outbufs = NULL;
362 gboolean S, L, R, D, I;
363 guint payload_len, hdr_len, offset;
364 guint len_offs;
365 GstClockTime timestamp;
366 GstRTPBuffer rtpbuf = { NULL };
367 guint packet_len = 0;
368
369 depay = GST_RTP_ASF_DEPAY (depayload);
370
371 /* flush remaining data on discont */
372 if (GST_BUFFER_IS_DISCONT (buf)) {
373 GST_LOG_OBJECT (depay, "got DISCONT");
374 gst_adapter_clear (depay->adapter);
375 depay->discont = TRUE;
376 }
377
378 gst_rtp_buffer_map (buf, GST_MAP_READ, &rtpbuf);
379 timestamp = GST_BUFFER_TIMESTAMP (buf);
380
381 payload_len = gst_rtp_buffer_get_payload_len (&rtpbuf);
382 payload = gst_rtp_buffer_get_payload (&rtpbuf);
383 offset = 0;
384
385 GST_LOG_OBJECT (depay, "got payload len of %u", payload_len);
386
387 outbufs = gst_buffer_list_new ();
388 do {
389 hdr_len = 4;
390
391 /* packet header is at least 4 bytes */
392 if (payload_len < hdr_len)
393 goto too_small;
394
395 /* 1 2 3
396 * 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
397 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
398 * |S|L|R|D|I|RES | Length/Offset |
399 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
400 * | Relative Timestamp (optional) |
401 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
402 * | Duration (optional) |
403 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
404 * | LocationId (optional) |
405 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
406 *
407 * S: packet contains a keyframe.
408 * L: If 1, Length/Offset contains length, else contains the byte offset
409 * of the fragment's first byte counted from the beginning of the
410 * complete ASF data packet.
411 * R: relative timestamp present
412 * D: duration present
413 * I: locationid present
414 */
415
416 S = ((payload[0] & 0x80) != 0);
417 L = ((payload[0] & 0x40) != 0);
418 R = ((payload[0] & 0x20) != 0);
419 D = ((payload[0] & 0x10) != 0);
420 I = ((payload[0] & 0x08) != 0);
421
422 len_offs = (payload[1] << 16) | (payload[2] << 8) | payload[3];
423
424 if (R) {
425 hdr_len += 4;
426 if (payload_len < hdr_len)
427 goto too_small;
428 GST_DEBUG ("Relative timestamp field present : %u",
429 GST_READ_UINT32_BE (payload + hdr_len - 4));
430 }
431 if (D) {
432 hdr_len += 4;
433 if (payload_len < hdr_len)
434 goto too_small;
435 GST_DEBUG ("Duration field present : %u",
436 GST_READ_UINT32_BE (payload + hdr_len - 4));
437 }
438 if (I) {
439 hdr_len += 4;
440 if (payload_len < hdr_len)
441 goto too_small;
442 GST_DEBUG ("LocationId field present : %u",
443 GST_READ_UINT32_BE (payload + hdr_len - 4));
444 }
445
446 GST_LOG_OBJECT (depay, "S %d, L %d, R %d, D %d, I %d", S, L, R, D, I);
447 GST_LOG_OBJECT (depay, "payload_len:%u, hdr_len:%u, len_offs:%u",
448 payload_len, hdr_len, len_offs);
449
450 /* skip headers */
451 payload_len -= hdr_len;
452 payload += hdr_len;
453 offset += hdr_len;
454
455 if (L) {
456 /* L bit set, len contains the length of the packet */
457 packet_len = len_offs;
458 } else {
459 /* else it contains an offset which we don't handle yet */
460 GST_LOG_OBJECT (depay, "We have a fragmented packet");
461 packet_len = payload_len;
462 }
463
464 if (packet_len > payload_len)
465 packet_len = payload_len;
466
467 if (packet_len > depay->packet_size)
468 goto too_big;
469
470 GST_LOG_OBJECT (depay, "packet len %u, payload len %u, packet_size:%u",
471 packet_len, payload_len, depay->packet_size);
472
473 if (!L) {
474 guint available;
475 GstBuffer *sub;
476
477 /* Fragmented packet handling */
478 outbuf = NULL;
479
480 if (len_offs == (available = gst_adapter_available (depay->adapter))) {
481 /* fragment aligns with what we have, add it */
482 GST_LOG_OBJECT (depay, "collecting fragment");
483 sub =
484 gst_rtp_buffer_get_payload_subbuffer (&rtpbuf, offset, packet_len);
485 gst_adapter_push (depay->adapter, sub);
486 /* RTP marker bit M is set if this is last fragment */
487 if (gst_rtp_buffer_get_marker (&rtpbuf)) {
488 if (available + packet_len > depay->packet_size) {
489 /* Update packet_len for the debug log further below */
490 packet_len = available + packet_len;
491 gst_adapter_clear (depay->adapter);
492 goto too_big;
493 }
494 GST_LOG_OBJECT (depay, "last fragment, assembling packet");
495 outbuf =
496 gst_adapter_take_buffer (depay->adapter, available + packet_len);
497 }
498 } else {
499 if (available) {
500 GST_WARNING_OBJECT (depay, "Offset doesn't match previous data?!");
501 GST_DEBUG_OBJECT (depay, "clearing for re-sync");
502 gst_adapter_clear (depay->adapter);
503 } else
504 GST_DEBUG_OBJECT (depay, "waiting for start of packet");
505 }
506 } else {
507 GST_LOG_OBJECT (depay, "collecting packet");
508 outbuf =
509 gst_rtp_buffer_get_payload_subbuffer (&rtpbuf, offset, packet_len);
510 }
511
512 /* If we haven't completed a full ASF packet, return but first
513 push what we have so far */
514 if (!outbuf) {
515 gst_rtp_buffer_unmap (&rtpbuf);
516 gst_rtp_base_depayload_push_list (depayload, outbufs);
517 return NULL;
518 }
519
520 outbuf = gst_rtp_asf_depay_update_padding (depay, outbuf);
521 if (!outbuf) {
522 gst_rtp_buffer_unmap (&rtpbuf);
523 gst_rtp_base_depayload_push_list (depayload, outbufs);
524 return NULL;
525 }
526
527 if (!S)
528 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DELTA_UNIT);
529
530 if (depay->discont) {
531 GST_LOG_OBJECT (depay, "setting DISCONT");
532 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DISCONT);
533 depay->discont = FALSE;
534 }
535
536 GST_BUFFER_TIMESTAMP (outbuf) = timestamp;
537
538 gst_buffer_list_add (outbufs, outbuf);
539
540 /* only apply the timestamp to the first buffer of this packet */
541 timestamp = -1;
542
543 /* skip packet data */
544 payload += packet_len;
545 offset += packet_len;
546 payload_len -= packet_len;
547 } while (payload_len > 0);
548
549 gst_rtp_base_depayload_push_list (depayload, outbufs);
550
551 gst_rtp_buffer_unmap (&rtpbuf);
552
553 return NULL;
554
555 /* ERRORS */
556 too_small:
557 {
558 gst_rtp_buffer_unmap (&rtpbuf);
559 GST_WARNING_OBJECT (depayload, "Payload too small, expected at least %u "
560 "bytes for header, but got only %u bytes", hdr_len, payload_len);
561 if (gst_buffer_list_length (outbufs) == 0) {
562 gst_rtp_base_depayload_dropped (depayload);
563 gst_buffer_list_unref (outbufs);
564 } else {
565 gst_rtp_base_depayload_push_list (depayload, outbufs);
566 }
567 return NULL;
568 }
569 too_big:
570 {
571 gst_rtp_buffer_unmap (&rtpbuf);
572 GST_WARNING_OBJECT (depayload, "Payload too big, expected at most %u "
573 "bytes, but got %u bytes", depay->packet_size, packet_len);
574 if (gst_buffer_list_length (outbufs) == 0) {
575 gst_rtp_base_depayload_dropped (depayload);
576 gst_buffer_list_unref (outbufs);
577 } else {
578 gst_rtp_base_depayload_push_list (depayload, outbufs);
579 }
580 return NULL;
581 }
582 }
583
584 static GstStateChangeReturn
585 gst_rtp_asf_depay_change_state (GstElement * element, GstStateChange trans)
586 {
587 GstStateChangeReturn ret;
588 GstRtpAsfDepay *depay;
589
590 depay = GST_RTP_ASF_DEPAY (element);
591
592 switch (trans) {
593 case GST_STATE_CHANGE_READY_TO_PAUSED:
594 gst_adapter_clear (depay->adapter);
595 depay->discont = TRUE;
596 break;
597 default:
598 break;
599 }
600
601 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, trans);
602
603 switch (trans) {
604 case GST_STATE_CHANGE_PAUSED_TO_READY:
605 gst_adapter_clear (depay->adapter);
606 break;
607 default:
608 break;
609 }
610
611 return ret;
612 }