summaryrefslogtreecommitdiff
path: root/gst/asfdemux/gstrtpasfdepay.c
blob: 04802349e47af9aee606ed956fde8d216799247d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
/* GStreamer RTP ASF depayloader
 * Copyright (C) 2006 Tim-Philipp Müller  <tim centricular net>
 *               2009 Wim Taymans  <wim.taymans@gmail.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include "gstrtpasfdepay.h"
#include <gst/rtp/gstrtpbuffer.h>

#include <string.h>
#include <stdlib.h>

GST_DEBUG_CATEGORY_STATIC (rtpasfdepayload_debug);
#define GST_CAT_DEFAULT rtpasfdepayload_debug

static const GstElementDetails rtp_asf_depay_details =
GST_ELEMENT_DETAILS ("RTP ASF packet depayloader",
    "Codec/Depayloader/Network",
    "Extracts ASF streams from RTP",
    "Tim-Philipp Müller <tim centricular net>, "
    "Wim Taymans <wim.taymans@gmail.com>");

static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
    GST_PAD_SRC,
    GST_PAD_ALWAYS,
    GST_STATIC_CAPS ("video/x-ms-asf")
    );

/* Other parameters: config, maxps */
#define SINK_CAPS \
  "application/x-rtp, "                                          \
  "media = (string) { \"application\", \"video\", \"audio\" }, " \
  "clock-rate = (int) [1, MAX ], "                               \
  "encoding-name = (string) \"X-ASF-PF\""

static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
    GST_PAD_SINK,
    GST_PAD_ALWAYS,
    GST_STATIC_CAPS (SINK_CAPS)
    );

GST_BOILERPLATE (GstRtpAsfDepay, gst_rtp_asf_depay, GstBaseRTPDepayload,
    GST_TYPE_BASE_RTP_DEPAYLOAD);

static void gst_rtp_asf_depay_finalize (GObject * object);

static GstStateChangeReturn gst_rtp_asf_depay_change_state (GstElement *
    element, GstStateChange transition);

static gboolean gst_rtp_asf_depay_setcaps (GstBaseRTPDepayload * depay,
    GstCaps * caps);
static GstBuffer *gst_rtp_asf_depay_process (GstBaseRTPDepayload * basedepay,
    GstBuffer * buf);

static void
gst_rtp_asf_depay_base_init (gpointer klass)
{
  GstElementClass *element_class = GST_ELEMENT_CLASS (klass);

  gst_element_class_add_pad_template (element_class,
      gst_static_pad_template_get (&src_factory));
  gst_element_class_add_pad_template (element_class,
      gst_static_pad_template_get (&sink_factory));

  gst_element_class_set_details (element_class, &rtp_asf_depay_details);
}

static void
gst_rtp_asf_depay_class_init (GstRtpAsfDepayClass * klass)
{
  GObjectClass *gobject_class;
  GstElementClass *gstelement_class;
  GstBaseRTPDepayloadClass *gstbasertpdepayload_class;

  gobject_class = (GObjectClass *) klass;
  gstelement_class = (GstElementClass *) klass;
  gstbasertpdepayload_class = (GstBaseRTPDepayloadClass *) klass;

  gobject_class->finalize = gst_rtp_asf_depay_finalize;

  gstelement_class->change_state =
      GST_DEBUG_FUNCPTR (gst_rtp_asf_depay_change_state);

  gstbasertpdepayload_class->set_caps =
      GST_DEBUG_FUNCPTR (gst_rtp_asf_depay_setcaps);
  gstbasertpdepayload_class->process =
      GST_DEBUG_FUNCPTR (gst_rtp_asf_depay_process);

  GST_DEBUG_CATEGORY_INIT (rtpasfdepayload_debug, "rtpasfdepayload", 0,
      "RTP asf depayloader element");
}

static void
gst_rtp_asf_depay_init (GstRtpAsfDepay * depay, GstRtpAsfDepayClass * klass)
{
  depay->adapter = gst_adapter_new ();
}

static void
gst_rtp_asf_depay_finalize (GObject * object)
{
  GstRtpAsfDepay *depay;

  depay = GST_RTP_ASF_DEPAY (object);

  g_object_unref (depay->adapter);

  G_OBJECT_CLASS (parent_class)->finalize (object);
}

static const guint8 asf_marker[16] = { 0x30, 0x26, 0xb2, 0x75, 0x8e, 0x66,
  0xcf, 0x11, 0xa6, 0xd9, 0x00, 0xaa, 0x00, 0x62, 0xce, 0x6c
};

static gboolean
gst_rtp_asf_depay_setcaps (GstBaseRTPDepayload * depayload, GstCaps * caps)
{
  GstRtpAsfDepay *depay;
  GstStructure *s;
  const gchar *config_str, *ps_string;
  GstBuffer *buf;
  GstCaps *src_caps;
  guint8 *headers;
  gsize headers_len;
  gint clock_rate;

  depay = GST_RTP_ASF_DEPAY (depayload);

  s = gst_caps_get_structure (caps, 0);

  if (!gst_structure_get_int (s, "clock-rate", &clock_rate) || clock_rate < 0)
    clock_rate = 1000;
  depayload->clock_rate = clock_rate;

  /* config contains the asf headers in base64 coding */
  config_str = gst_structure_get_string (s, "config");
  if (config_str == NULL || *config_str == '\0')
    goto no_config;

  ps_string = gst_structure_get_string (s, "maxps");
  if (ps_string == NULL || *ps_string == '\0')
    goto no_packetsize;

  depay->packet_size = atoi (ps_string);
  if (depay->packet_size <= 16)
    goto invalid_packetsize;

  headers = (guint8 *) g_base64_decode (config_str, &headers_len);

  if (headers == NULL || headers_len < 16
      || memcmp (headers, asf_marker, 16) != 0)
    goto invalid_headers;

  src_caps = gst_caps_new_simple ("video/x-ms-asf", NULL);
  gst_pad_set_caps (depayload->srcpad, src_caps);

  buf = gst_buffer_new ();
  GST_BUFFER_DATA (buf) = headers;
  GST_BUFFER_MALLOCDATA (buf) = headers;
  GST_BUFFER_SIZE (buf) = headers_len;
  gst_buffer_set_caps (buf, src_caps);
  gst_caps_unref (src_caps);

  gst_base_rtp_depayload_push (depayload, buf);

  return TRUE;

  /* ERRORS */
no_config:
  {
    GST_WARNING_OBJECT (depay, "caps without 'config' field with asf headers");
    return FALSE;
  }
no_packetsize:
  {
    GST_WARNING_OBJECT (depay, "caps without 'maxps' (packet size) field");
    return FALSE;
  }
invalid_packetsize:
  {
    GST_WARNING_OBJECT (depay, "packet size %u invalid", depay->packet_size);
    return FALSE;
  }
invalid_headers:
  {
    GST_WARNING_OBJECT (depay, "headers don't look like valid ASF headers");
    g_free (headers);
    return FALSE;
  }
}

static gint
field_size (guint8 field)
{
  switch (field) {
      /* DWORD - 32 bits */
    case 3:
      return 4;

      /* WORD - 16 bits */
    case 2:
      return 2;

      /* BYTE - 8 bits */
    case 1:
      return 1;

      /* non-exitent */
    case 0:
    default:
      return 0;
  }
}

/* 
 * Set the padding field to te correct value as the spec
 * says it should be se to 0 in the rtp packets
 */
static void
gst_rtp_asf_depay_set_padding (GstRtpAsfDepay * depayload,
    GstBuffer * buf, guint32 padding)
{
  guint8 *data = GST_BUFFER_DATA (buf);
  gint offset = 0;
  guint8 aux;
  guint8 seq_type;
  guint8 pad_type;
  guint8 pkt_type;

  aux = data[offset++];
  if (aux & 0x80) {
    guint8 err_len = 0;
    if (aux & 0x60) {
      GST_WARNING_OBJECT (depayload, "Error correction length type should be "
          "set to 0");
      /* this packet doesn't follow the spec */
      return;
    }
    err_len = aux & 0x0F;
    offset += err_len;

    aux = data[offset++];
  }
  seq_type = (aux >> 1) & 0x3;
  pad_type = (aux >> 3) & 0x3;
  pkt_type = (aux >> 5) & 0x3;

  offset += 1;                  /* skip property flags */
  offset += field_size (pkt_type);      /* skip packet length */
  offset += field_size (seq_type);      /* skip sequence field */

  /* write padding */
  switch (pad_type) {
      /* DWORD */
    case 3:
      GST_WRITE_UINT32_LE (&(data[offset]), padding);
      break;

      /* WORD */
    case 2:
      GST_WRITE_UINT16_LE (&(data[offset]), padding);
      break;

      /* BYTE */
    case 1:
      data[offset] = (guint8) padding;
      break;

      /* non-existent */
    case 0:
    default:
      break;
  }
}

/* Docs: 'RTSP Protocol PDF' document from http://sdp.ppona.com/ (page 8) */

static GstBuffer *
gst_rtp_asf_depay_process (GstBaseRTPDepayload * depayload, GstBuffer * buf)
{
  GstRtpAsfDepay *depay;
  const guint8 *payload;
  GstBuffer *outbuf;
  gboolean S, L, R, D, I;
  guint payload_len, hdr_len, offset;
  guint len_offs;
  GstClockTime timestamp;

  depay = GST_RTP_ASF_DEPAY (depayload);

  /* flush remaining data on discont */
  if (GST_BUFFER_IS_DISCONT (buf)) {
    GST_LOG_OBJECT (depay, "got DISCONT");
    gst_adapter_clear (depay->adapter);
    depay->wait_start = TRUE;
    depay->discont = TRUE;
  }

  timestamp = GST_BUFFER_TIMESTAMP (buf);

  payload_len = gst_rtp_buffer_get_payload_len (buf);
  payload = gst_rtp_buffer_get_payload (buf);
  offset = 0;

  GST_LOG_OBJECT (depay, "got payload len of %u", payload_len);

  do {
    guint packet_len;

    /* packet header is at least 4 bytes */
    if (payload_len < 4)
      goto too_small;

    /*                      1                   2                   3
     *  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 
     * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
     * |S|L|R|D|I|RES  | Length/Offset                                 |
     * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
     * | Relative Timestamp (optional)                                 |
     * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
     * | Duration (optional)                                           |
     * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
     * | LocationId (optional)                                         |
     * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
     *
     * S: packet contains a keyframe.
     * L: If 1, Length/Offset contains length, else contains the byte offset
     *    of the fragment's first byte counted from the beginning of the
     *    complete ASF data packet.
     * R: relative timestamp present
     * D: duration present
     * I: locationid present
     */

    S = ((payload[0] & 0x80) != 0);
    L = ((payload[0] & 0x40) != 0);
    R = ((payload[0] & 0x20) != 0);
    D = ((payload[0] & 0x10) != 0);
    I = ((payload[0] & 0x08) != 0);

    hdr_len = 4;

    len_offs = (payload[1] << 16) | (payload[2] << 8) | payload[3];

    if (R) {
      GST_DEBUG ("Relative timestamp field present : %u",
          GST_READ_UINT32_BE (payload + hdr_len));
      hdr_len += 4;
    }
    if (D) {
      GST_DEBUG ("Duration field present : %u",
          GST_READ_UINT32_BE (payload + hdr_len));
      hdr_len += 4;
    }
    if (I) {
      GST_DEBUG ("LocationId field present : %u",
          GST_READ_UINT32_BE (payload + hdr_len));
      hdr_len += 4;
    }

    GST_LOG_OBJECT (depay, "S %d, L %d, R %d, D %d, I %d", S, L, R, D, I);
    GST_LOG_OBJECT (depay, "payload_len:%d, hdr_len:%d, len_offs:%d",
        payload_len, hdr_len, len_offs);

    if (payload_len < hdr_len)
      goto too_small;

    /* skip headers */
    payload_len -= hdr_len;
    payload += hdr_len;
    offset += hdr_len;

    if (L) {
      /* L bit set, len contains the length of the packet */
      packet_len = len_offs;
    } else {
      /* else it contains an offset which we don't handle yet */
      GST_LOG_OBJECT (depay, "We have a fragmented packet");
      packet_len = payload_len;
    }

    if (packet_len > payload_len)
      packet_len = payload_len;

    GST_LOG_OBJECT (depay, "packet len %u, payload len %u, packet_size:%u",
        packet_len, payload_len, depay->packet_size);

    if (!L) {
      guint available;
      GstBuffer *sub;

      /* Fragmented packet handling */
      outbuf = NULL;

      if (len_offs == 0 && (available = gst_adapter_available (depay->adapter))) {
        /* Beginning of a new fragmented packet, Extract the previous buffer if any */
        GST_DEBUG ("Extracting previous fragmented buffer from adapter");
        sub = gst_adapter_take_buffer (depay->adapter, available);
        if (available < depay->packet_size) {
          /* Add padding if needed */
          GST_DEBUG ("Padding outgoing buffer to packet_size (%d, was %d",
              depay->packet_size, available);
          outbuf = gst_buffer_new_and_alloc (depay->packet_size);
          memcpy (GST_BUFFER_DATA (outbuf), GST_BUFFER_DATA (sub), available);
          memset (GST_BUFFER_DATA (outbuf) + available, 0,
              depay->packet_size - available);
          gst_buffer_unref (sub);
          gst_rtp_asf_depay_set_padding (depay, outbuf,
              depay->packet_size - available);
        } else
          outbuf = sub;
      }
      GST_DEBUG ("storing fragmented buffer continuation and returning");
      available = gst_adapter_available (depay->adapter);
      GST_DEBUG ("Available bytes (%d), len_offs (%d)", available, len_offs);
      if ((available = gst_adapter_available (depay->adapter))) {
        if (available != len_offs) {
          GST_WARNING ("Available bytes (%d) != len_offs (%d), trimming buffer",
              available, len_offs);
          sub = gst_adapter_take_buffer (depay->adapter, len_offs);
          gst_adapter_clear (depay->adapter);
          if (sub)
            gst_adapter_push (depay->adapter, sub);
        }
      }
      sub = gst_rtp_buffer_get_payload_subbuffer (buf, offset, packet_len);
      gst_adapter_push (depay->adapter, sub);
      /* If we haven't completed a full ASF packet, return */
      if (!outbuf)
        return NULL;
    } else if (packet_len >= depay->packet_size) {
      GST_LOG_OBJECT (depay, "creating subbuffer");
      outbuf = gst_rtp_buffer_get_payload_subbuffer (buf, offset, packet_len);
    } else {
      GST_LOG_OBJECT (depay, "padding buffer");
      /* we need to pad with zeroes to packet_size if it's smaller */
      outbuf = gst_buffer_new_and_alloc (depay->packet_size);
      memcpy (GST_BUFFER_DATA (outbuf), payload, packet_len);
      memset (GST_BUFFER_DATA (outbuf) + packet_len, 0,
          depay->packet_size - packet_len);
      gst_rtp_asf_depay_set_padding (depay, outbuf,
          depay->packet_size - packet_len);
    }

    gst_buffer_set_caps (outbuf, GST_PAD_CAPS (depayload->srcpad));

    if (!S)
      GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DELTA_UNIT);

    if (depay->discont) {
      GST_LOG_OBJECT (depay, "setting DISCONT");
      GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DISCONT);
      depay->discont = FALSE;
    }

    GST_BUFFER_TIMESTAMP (outbuf) = timestamp;

    gst_base_rtp_depayload_push (depayload, outbuf);

    /* only apply the timestamp to the first buffer of this packet */
    timestamp = -1;

    /* skip packet data */
    payload += packet_len;
    offset += packet_len;
    payload_len -= packet_len;
  } while (payload_len > 0);

  return NULL;

/* ERRORS */
too_small:
  {
    GST_WARNING_OBJECT (depayload, "Payload too small, expected at least 4 "
        "bytes for header, but got only %d bytes", payload_len);
    return NULL;
  }
}

static GstStateChangeReturn
gst_rtp_asf_depay_change_state (GstElement * element, GstStateChange trans)
{
  GstStateChangeReturn ret;
  GstRtpAsfDepay *depay;

  depay = GST_RTP_ASF_DEPAY (element);

  switch (trans) {
    case GST_STATE_CHANGE_READY_TO_PAUSED:
      gst_adapter_clear (depay->adapter);
      depay->wait_start = TRUE;
      depay->discont = TRUE;
      break;
    default:
      break;
  }

  ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, trans);

  switch (trans) {
    case GST_STATE_CHANGE_PAUSED_TO_READY:
      gst_adapter_clear (depay->adapter);
      break;
    default:
      break;
  }

  return ret;
}