Debian Codegraph @

gst/asfdemux/gstrtpasfdepay.c

raw
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 /* Set the padding field to te correct value as the spec
249 * says it should be se to 0 in the rtp packets
250 */
251 static GstBuffer *
252 gst_rtp_asf_depay_update_padding (GstRtpAsfDepay * depayload, GstBuffer * buf)
253 {
254 GstBuffer *result;
255 GstMapInfo map;
256 guint8 *data;
257 gint offset = 0;
258 guint8 aux;
259 guint8 seq_type;
260 guint8 pad_type;
261 guint8 pkt_type;
262 gsize plen, padding;
263
264 plen = gst_buffer_get_size (buf);
265 if (plen == depayload->packet_size)
266 return buf;
267
268 padding = depayload->packet_size - plen;
269
270 GST_LOG_OBJECT (depayload,
271 "padding buffer size %" G_GSIZE_FORMAT " to packet size %d", plen,
272 depayload->packet_size);
273
274 result = gst_buffer_new_and_alloc (depayload->packet_size);
275
276 gst_buffer_map (result, &map, GST_MAP_READ);
277 data = map.data;
278 memset (data + plen, 0, padding);
279
280 gst_buffer_extract (buf, 0, data, plen);
281 gst_buffer_unref (buf);
282
283 aux = data[offset++];
284 if (aux & 0x80) {
285 guint8 err_len = 0;
286 if (aux & 0x60) {
287 GST_WARNING_OBJECT (depayload, "Error correction length type should be "
288 "set to 0");
289 /* this packet doesn't follow the spec */
290 gst_buffer_unmap (result, &map);
291 return result;
292 }
293 err_len = aux & 0x0F;
294 offset += err_len;
295
296 aux = data[offset++];
297 }
298 seq_type = (aux >> 1) & 0x3;
299 pad_type = (aux >> 3) & 0x3;
300 pkt_type = (aux >> 5) & 0x3;
301
302 offset += 1; /* skip property flags */
303 offset += field_size (pkt_type); /* skip packet length */
304 offset += field_size (seq_type); /* skip sequence field */
305
306 /* write padding */
307 switch (pad_type) {
308 /* DWORD */
309 case 3:
310 GST_WRITE_UINT32_LE (&(data[offset]), padding);
311 break;
312
313 /* WORD */
314 case 2:
315 GST_WRITE_UINT16_LE (&(data[offset]), padding);
316 break;
317
318 /* BYTE */
319 case 1:
320 data[offset] = (guint8) padding;
321 break;
322
323 /* non-existent */
324 case 0:
325 default:
326 break;
327 }
328 gst_buffer_unmap (result, &map);
329
330 return result;
331 }
332
333 /* Docs: 'RTSP Protocol PDF' document from http://sdp.ppona.com/ (page 8) */
334
335 static GstBuffer *
336 gst_rtp_asf_depay_process (GstRTPBaseDepayload * depayload, GstBuffer * buf)
337 {
338 GstRtpAsfDepay *depay;
339 const guint8 *payload;
340 GstBuffer *outbuf;
341 GstBufferList *outbufs = NULL;
342 gboolean S, L, R, D, I;
343 guint payload_len, hdr_len, offset;
344 guint len_offs;
345 GstClockTime timestamp;
346 GstRTPBuffer rtpbuf = { NULL };
347
348 depay = GST_RTP_ASF_DEPAY (depayload);
349
350 /* flush remaining data on discont */
351 if (GST_BUFFER_IS_DISCONT (buf)) {
352 GST_LOG_OBJECT (depay, "got DISCONT");
353 gst_adapter_clear (depay->adapter);
354 depay->discont = TRUE;
355 }
356
357 gst_rtp_buffer_map (buf, GST_MAP_READ, &rtpbuf);
358 timestamp = GST_BUFFER_TIMESTAMP (buf);
359
360 payload_len = gst_rtp_buffer_get_payload_len (&rtpbuf);
361 payload = gst_rtp_buffer_get_payload (&rtpbuf);
362 offset = 0;
363
364 GST_LOG_OBJECT (depay, "got payload len of %u", payload_len);
365
366 outbufs = gst_buffer_list_new ();
367 do {
368 guint packet_len;
369
370 /* packet header is at least 4 bytes */
371 if (payload_len < 4)
372 goto too_small;
373
374 /* 1 2 3
375 * 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
376 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
377 * |S|L|R|D|I|RES | Length/Offset |
378 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
379 * | Relative Timestamp (optional) |
380 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
381 * | Duration (optional) |
382 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
383 * | LocationId (optional) |
384 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
385 *
386 * S: packet contains a keyframe.
387 * L: If 1, Length/Offset contains length, else contains the byte offset
388 * of the fragment's first byte counted from the beginning of the
389 * complete ASF data packet.
390 * R: relative timestamp present
391 * D: duration present
392 * I: locationid present
393 */
394
395 S = ((payload[0] & 0x80) != 0);
396 L = ((payload[0] & 0x40) != 0);
397 R = ((payload[0] & 0x20) != 0);
398 D = ((payload[0] & 0x10) != 0);
399 I = ((payload[0] & 0x08) != 0);
400
401 hdr_len = 4;
402
403 len_offs = (payload[1] << 16) | (payload[2] << 8) | payload[3];
404
405 if (R) {
406 GST_DEBUG ("Relative timestamp field present : %u",
407 GST_READ_UINT32_BE (payload + hdr_len));
408 hdr_len += 4;
409 }
410 if (D) {
411 GST_DEBUG ("Duration field present : %u",
412 GST_READ_UINT32_BE (payload + hdr_len));
413 hdr_len += 4;
414 }
415 if (I) {
416 GST_DEBUG ("LocationId field present : %u",
417 GST_READ_UINT32_BE (payload + hdr_len));
418 hdr_len += 4;
419 }
420
421 GST_LOG_OBJECT (depay, "S %d, L %d, R %d, D %d, I %d", S, L, R, D, I);
422 GST_LOG_OBJECT (depay, "payload_len:%d, hdr_len:%d, len_offs:%d",
423 payload_len, hdr_len, len_offs);
424
425 if (payload_len < hdr_len)
426 goto too_small;
427
428 /* skip headers */
429 payload_len -= hdr_len;
430 payload += hdr_len;
431 offset += hdr_len;
432
433 if (L) {
434 /* L bit set, len contains the length of the packet */
435 packet_len = len_offs;
436 } else {
437 /* else it contains an offset which we don't handle yet */
438 GST_LOG_OBJECT (depay, "We have a fragmented packet");
439 packet_len = payload_len;
440 }
441
442 if (packet_len > payload_len)
443 packet_len = payload_len;
444
445 GST_LOG_OBJECT (depay, "packet len %u, payload len %u, packet_size:%u",
446 packet_len, payload_len, depay->packet_size);
447
448 if (!L) {
449 guint available;
450 GstBuffer *sub;
451
452 /* Fragmented packet handling */
453 outbuf = NULL;
454
455 if (len_offs == (available = gst_adapter_available (depay->adapter))) {
456 /* fragment aligns with what we have, add it */
457 GST_LOG_OBJECT (depay, "collecting fragment");
458 sub =
459 gst_rtp_buffer_get_payload_subbuffer (&rtpbuf, offset, packet_len);
460 gst_adapter_push (depay->adapter, sub);
461 /* RTP marker bit M is set if this is last fragment */
462 if (gst_rtp_buffer_get_marker (&rtpbuf)) {
463 GST_LOG_OBJECT (depay, "last fragment, assembling packet");
464 outbuf =
465 gst_adapter_take_buffer (depay->adapter, available + packet_len);
466 }
467 } else {
468 if (available) {
469 GST_WARNING_OBJECT (depay, "Offset doesn't match previous data?!");
470 GST_DEBUG_OBJECT (depay, "clearing for re-sync");
471 gst_adapter_clear (depay->adapter);
472 } else
473 GST_DEBUG_OBJECT (depay, "waiting for start of packet");
474 }
475 } else {
476 GST_LOG_OBJECT (depay, "collecting packet");
477 outbuf =
478 gst_rtp_buffer_get_payload_subbuffer (&rtpbuf, offset, packet_len);
479 }
480
481 /* If we haven't completed a full ASF packet, return but first
482 push what we have so far */
483 if (!outbuf) {
484 gst_rtp_base_depayload_push_list (depayload, outbufs);
485 return NULL;
486 }
487
488 outbuf = gst_rtp_asf_depay_update_padding (depay, outbuf);
489
490 if (!S)
491 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DELTA_UNIT);
492
493 if (depay->discont) {
494 GST_LOG_OBJECT (depay, "setting DISCONT");
495 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DISCONT);
496 depay->discont = FALSE;
497 }
498
499 GST_BUFFER_TIMESTAMP (outbuf) = timestamp;
500
501 gst_buffer_list_add (outbufs, outbuf);
502
503 /* only apply the timestamp to the first buffer of this packet */
504 timestamp = -1;
505
506 /* skip packet data */
507 payload += packet_len;
508 offset += packet_len;
509 payload_len -= packet_len;
510 } while (payload_len > 0);
511
512 gst_rtp_base_depayload_push_list (depayload, outbufs);
513
514 gst_rtp_buffer_unmap (&rtpbuf);
515
516 return NULL;
517
518 /* ERRORS */
519 too_small:
520 {
521 gst_rtp_buffer_unmap (&rtpbuf);
522 GST_WARNING_OBJECT (depayload, "Payload too small, expected at least 4 "
523 "bytes for header, but got only %d bytes", payload_len);
524 if (gst_buffer_list_length (outbufs) == 0) {
525 gst_rtp_base_depayload_dropped (depayload);
526 gst_buffer_list_unref (outbufs);
527 } else {
528 gst_rtp_base_depayload_push_list (depayload, outbufs);
529 }
530 return NULL;
531 }
532 }
533
534 static GstStateChangeReturn
535 gst_rtp_asf_depay_change_state (GstElement * element, GstStateChange trans)
536 {
537 GstStateChangeReturn ret;
538 GstRtpAsfDepay *depay;
539
540 depay = GST_RTP_ASF_DEPAY (element);
541
542 switch (trans) {
543 case GST_STATE_CHANGE_READY_TO_PAUSED:
544 gst_adapter_clear (depay->adapter);
545 depay->discont = TRUE;
546 break;
547 default:
548 break;
549 }
550
551 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, trans);
552
553 switch (trans) {
554 case GST_STATE_CHANGE_PAUSED_TO_READY:
555 gst_adapter_clear (depay->adapter);
556 break;
557 default:
558 break;
559 }
560
561 return ret;
562 }