summaryrefslogtreecommitdiff
path: root/gst/realmedia/rtspreal.c
blob: 3283c79beabb1bc3748ddda11b76db10c166cd53 (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
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
/* GStreamer
 * Copyright (C) <2005,2006> Wim Taymans <wim@fluendo.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.
 */
/* Element-Checklist-Version: 5 */

/**
 * SECTION:element-rtspreal
 *
 * <refsect2>
 * <para>
 * A RealMedia RTSP extension
 * </para>
 * </refsect2>
 *
 * Last reviewed on 2007-07-25 (0.10.14)
 */

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

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

#include <gst/rtsp/gstrtspextension.h>
#include <gst/rtsp/gstrtspbase64.h>

#include "realhash.h"
#include "rtspreal.h"
#include "asmrules.h"

GST_DEBUG_CATEGORY_STATIC (rtspreal_debug);
#define GST_CAT_DEFAULT (rtspreal_debug)

/* elementfactory information */
static const GstElementDetails rtspreal_details =
GST_ELEMENT_DETAILS ("RealMedia RTSP Extension",
    "Network/Extension/Protocol",
    "Extends RTSP so that it can handle RealMedia setup",
    "Wim Taymans <wim.taymans@gmail.com>");

#define SERVER_PREFIX "RealServer"

static GstRTSPResult
rtsp_ext_real_get_transports (GstRTSPExtension * ext,
    GstRTSPLowerTrans protocols, gchar ** transport)
{
  GstRTSPReal *ctx = (GstRTSPReal *) ext;
  GString *str;

  if (!ctx->isreal)
    return GST_RTSP_OK;

  GST_DEBUG_OBJECT (ext, "generating transports for %d", protocols);

  str = g_string_new ("");

  /*
     if (protocols & GST_RTSP_LOWER_TRANS_UDP_MCAST) {
     g_string_append (str, "x-real-rdt/mcast;client_port=%%u1;mode=play,");
     }
     if (protocols & GST_RTSP_LOWER_TRANS_UDP) {
     g_string_append (str, "x-real-rdt/udp;client_port=%%u1;mode=play,");
     g_string_append (str, "x-pn-tng/udp;client_port=%%u1;mode=play,");
     }
   */
  if (protocols & GST_RTSP_LOWER_TRANS_TCP) {
    g_string_append (str, "x-real-rdt/tcp;mode=play,");
    g_string_append (str, "x-pn-tng/tcp;mode=play,");
  }

  /* if we added something, remove trailing ',' */
  if (str->len > 0)
    g_string_truncate (str, str->len - 1);

  *transport = g_string_free (str, FALSE);

  return GST_RTSP_OK;
}

static GstRTSPResult
rtsp_ext_real_before_send (GstRTSPExtension * ext, GstRTSPMessage * request)
{
  GstRTSPReal *ctx = (GstRTSPReal *) ext;

  switch (request->type_data.request.method) {
    case GST_RTSP_OPTIONS:
    {
      gst_rtsp_message_add_header (request, GST_RTSP_HDR_USER_AGENT,
          //"RealMedia Player (" GST_PACKAGE_NAME ")");
          "RealMedia Player Version 6.0.9.1235 (linux-2.0-libc6-i386-gcc2.95)");
      gst_rtsp_message_add_header (request, GST_RTSP_HDR_CLIENT_CHALLENGE,
          "9e26d33f2984236010ef6253fb1887f7");
      gst_rtsp_message_add_header (request, GST_RTSP_HDR_COMPANY_ID,
          "KnKV4M4I/B2FjJ1TToLycw==");
      gst_rtsp_message_add_header (request, GST_RTSP_HDR_GUID,
          "00000000-0000-0000-0000-000000000000");
      gst_rtsp_message_add_header (request, GST_RTSP_HDR_REGION_DATA, "0");
      gst_rtsp_message_add_header (request, GST_RTSP_HDR_PLAYER_START_TIME,
          "[28/03/2003:22:50:23 00:00]");
      gst_rtsp_message_add_header (request, GST_RTSP_HDR_CLIENT_ID,
          "Linux_2.4_6.0.9.1235_play32_RN01_EN_586");
      ctx->isreal = FALSE;
      break;
    }
    case GST_RTSP_DESCRIBE:
    {
      gst_rtsp_message_add_header (request, GST_RTSP_HDR_BANDWIDTH, "10485800");
      gst_rtsp_message_add_header (request, GST_RTSP_HDR_GUID,
          "00000000-0000-0000-0000-000000000000");
      gst_rtsp_message_add_header (request, GST_RTSP_HDR_REGION_DATA, "0");
      gst_rtsp_message_add_header (request, GST_RTSP_HDR_CLIENT_ID,
          "Linux_2.4_6.0.9.1235_play32_RN01_EN_586");
      gst_rtsp_message_add_header (request, GST_RTSP_HDR_MAX_ASM_WIDTH, "1");
      gst_rtsp_message_add_header (request, GST_RTSP_HDR_LANGUAGE, "en-US");
      break;
    }
    case GST_RTSP_SETUP:
    {
      if (ctx->isreal) {
        gchar *value =
            g_strdup_printf ("%s, sd=%s", ctx->challenge2, ctx->checksum);
        gst_rtsp_message_add_header (request, GST_RTSP_HDR_REAL_CHALLENGE2,
            value);
        g_free (value);
      }
      break;
    }
    default:
      break;
  }
  return GST_RTSP_OK;
}

static GstRTSPResult
rtsp_ext_real_after_send (GstRTSPExtension * ext, GstRTSPMessage * req,
    GstRTSPMessage * resp)
{
  GstRTSPReal *ctx = (GstRTSPReal *) ext;

  switch (req->type_data.request.method) {
    case GST_RTSP_OPTIONS:
    {
      gchar *challenge1 = NULL;
      gchar *server = NULL;

      gst_rtsp_message_get_header (resp, GST_RTSP_HDR_SERVER, &server, 0);

      gst_rtsp_message_get_header (resp, GST_RTSP_HDR_REAL_CHALLENGE1,
          &challenge1, 0);
      if (!challenge1)
        goto no_challenge1;

      gst_rtsp_ext_real_calc_response_and_checksum (ctx->challenge2,
          ctx->checksum, challenge1);
      break;
    }
    default:
      break;
  }
  return GST_RTSP_OK;

  /* ERRORS */
no_challenge1:
  {
    GST_DEBUG_OBJECT (ctx, "Could not find challenge tag.");
    ctx->isreal = FALSE;
    return GST_RTSP_OK;
  }
}

#define ENSURE_SIZE(size)              \
G_STMT_START {			       \
  while (data_len < size) {            \
    data_len += 1024;                  \
    data = g_realloc (data, data_len); \
  }                                    \
} G_STMT_END

#define READ_BUFFER_GEN(src, func, name, dest, dest_len)    \
G_STMT_START {			                            \
  dest = (gchar *)func (src, name);                                  \
  if (!dest) {                                              \
    dest = "";                                              \
    dest_len = 0;                                           \
  }                                                         \
  else if (!strncmp (dest, "buffer;\"", 8)) {               \
    dest += 8;                                              \
    dest_len = strlen (dest) - 1;                           \
    dest[dest_len] = '\0';                                  \
    gst_rtsp_base64_decode_ip (dest, &dest_len);            \
  }                                                         \
} G_STMT_END

#define READ_BUFFER(sdp, name, dest, dest_len)        \
 READ_BUFFER_GEN(sdp, gst_sdp_message_get_attribute_val, name, dest, dest_len)
#define READ_BUFFER_M(media, name, dest, dest_len)    \
 READ_BUFFER_GEN(media, gst_sdp_media_get_attribute_val, name, dest, dest_len)

#define READ_INT_GEN(src, func, name, dest)               \
G_STMT_START {			                          \
  const gchar *val = func (src, name);                          \
  if (val && !strncmp (val, "integer;", 8))               \
      dest = atoi (val + 8);                              \
    else                                                  \
      dest = 0;                                           \
} G_STMT_END

#define READ_INT(sdp, name, dest)                             \
 READ_INT_GEN(sdp, gst_sdp_message_get_attribute_val, name, dest)
#define READ_INT_M(media, name, dest)                         \
 READ_INT_GEN(media, gst_sdp_media_get_attribute_val, name, dest)

#define READ_STRING(media, name, dest, dest_len)              \
G_STMT_START {			                              \
  const gchar *val = gst_sdp_media_get_attribute_val (media, name); \
  if (val && !strncmp (val, "string;\"", 8)) {                \
    dest = (gchar *) val + 8;                                           \
    dest_len = strlen (dest) - 1;                             \
    dest[dest_len] = '\0';                                    \
  } else {                                                    \
    dest = "";                                                \
    dest_len = 0;                                             \
  }                                                           \
} G_STMT_END

#define WRITE_STRING1(datap, str, str_len)            \
G_STMT_START {			                      \
  *datap = str_len;                                   \
  memcpy ((datap) + 1, str, str_len);                 \
  datap += str_len + 1;                               \
} G_STMT_END

#define WRITE_STRING2(datap, str, str_len)            \
G_STMT_START {			                      \
  GST_WRITE_UINT16_BE (datap, str_len);               \
  memcpy (datap + 2, str, str_len);                   \
  datap += str_len + 2;                               \
} G_STMT_END

static GstRTSPResult
rtsp_ext_real_parse_sdp (GstRTSPExtension * ext, GstSDPMessage * sdp,
    GstStructure * props)
{
  GstRTSPReal *ctx = (GstRTSPReal *) ext;
  guint size;
  gint i;
  gchar *title, *author, *copyright, *comment;
  gsize title_len, author_len, copyright_len, comment_len;
  guint8 *data = NULL, *datap;
  guint data_len = 0, offset;
  GstBuffer *buf;
  gchar *opaque_data;
  gsize opaque_data_len, asm_rule_book_len;

  /* don't bother for non-real formats */
  READ_INT (sdp, "IsRealDataType", ctx->isreal);
  if (!ctx->isreal)
    return TRUE;

  /* Force PAUSE | PLAY */
  //src->methods |= GST_RTSP_PLAY | GST_RTSP_PAUSE;

  ctx->n_streams = gst_sdp_message_medias_len (sdp);

  ctx->max_bit_rate = 0;
  ctx->avg_bit_rate = 0;
  ctx->max_packet_size = 0;
  ctx->avg_packet_size = 0;
  ctx->duration = 0;

  for (i = 0; i < ctx->n_streams; i++) {
    const GstSDPMedia *media;
    gint intval;

    media = gst_sdp_message_get_media (sdp, i);

    READ_INT_M (media, "MaxBitRate", intval);
    ctx->max_bit_rate += intval;
    READ_INT_M (media, "AvgBitRate", intval);
    ctx->avg_bit_rate += intval;
    READ_INT_M (media, "MaxPacketSize", intval);
    ctx->max_packet_size = MAX (ctx->max_packet_size, intval);
    READ_INT_M (media, "AvgPacketSize", intval);
    ctx->avg_packet_size = (ctx->avg_packet_size * i + intval) / (i + 1);
    READ_INT_M (media, "Duration", intval);
    ctx->duration = MAX (ctx->duration, intval);
  }

  /* PROP */
  offset = 0;
  size = 50;
  ENSURE_SIZE (size);
  datap = data + offset;

  memcpy (datap + 0, "PROP", 4);
  GST_WRITE_UINT32_BE (datap + 4, size);
  GST_WRITE_UINT16_BE (datap + 8, 0);
  GST_WRITE_UINT32_BE (datap + 10, ctx->max_bit_rate);
  GST_WRITE_UINT32_BE (datap + 14, ctx->avg_bit_rate);
  GST_WRITE_UINT32_BE (datap + 18, ctx->max_packet_size);
  GST_WRITE_UINT32_BE (datap + 22, ctx->avg_packet_size);
  GST_WRITE_UINT32_BE (datap + 26, 0);
  GST_WRITE_UINT32_BE (datap + 30, ctx->duration);
  GST_WRITE_UINT32_BE (datap + 34, 0);
  GST_WRITE_UINT32_BE (datap + 38, 0);
  GST_WRITE_UINT32_BE (datap + 42, 0);
  GST_WRITE_UINT16_BE (datap + 46, ctx->n_streams);
  GST_WRITE_UINT16_BE (datap + 48, 0);
  offset += size;

  /* CONT */
  READ_BUFFER (sdp, "Title", title, title_len);
  READ_BUFFER (sdp, "Author", author, author_len);
  READ_BUFFER (sdp, "Comment", comment, comment_len);
  READ_BUFFER (sdp, "Copyright", copyright, copyright_len);

  size = 22 + title_len + author_len + comment_len + copyright_len;
  ENSURE_SIZE (offset + size);
  datap = data + offset;

  memcpy (datap, "CONT", 4);
  GST_WRITE_UINT32_BE (datap + 4, size);
  GST_WRITE_UINT16_BE (datap + 8, 0);   /* Version */
  datap += 10;
  WRITE_STRING2 (datap, title, title_len);
  WRITE_STRING2 (datap, author, author_len);
  WRITE_STRING2 (datap, copyright, copyright_len);
  WRITE_STRING2 (datap, comment, comment_len);
  offset += size;

  /* MDPR */
  for (i = 0; i < ctx->n_streams; i++) {
    const GstSDPMedia *media;
    guint16 j, sel;
    guint32 len;
    GstRTSPRealStream *stream;
    gchar *str;

    media = gst_sdp_message_get_media (sdp, i);

    stream = g_new0 (GstRTSPRealStream, 1);
    ctx->streams = g_list_append (ctx->streams, stream);

    READ_INT_M (media, "MaxBitRate", stream->max_bit_rate);
    READ_INT_M (media, "AvgBitRate", stream->avg_bit_rate);
    READ_INT_M (media, "MaxPacketSize", stream->max_packet_size);
    READ_INT_M (media, "AvgPacketSize", stream->avg_packet_size);
    READ_INT_M (media, "StartTime", stream->start_time);
    READ_INT_M (media, "Preroll", stream->preroll);
    READ_INT_M (media, "Duration", stream->duration);
    READ_STRING (media, "StreamName", str, stream->stream_name_len);
    stream->stream_name = g_strndup (str, stream->stream_name_len);
    READ_STRING (media, "mimetype", str, stream->mime_type_len);
    stream->mime_type = g_strndup (str, stream->mime_type_len);

    /* FIXME: Depending on the current bandwidth, we need to select one
     * bandwith out of a list offered by the server. Someone needs to write
     * a parser for strings like
     *
     * #($Bandwidth < 67959),TimestampDelivery=T,DropByN=T,priority=9;
     * #($Bandwidth >= 67959) && ($Bandwidth < 167959),AverageBandwidth=67959,
     * Priority=9;#($Bandwidth >= 67959) && ($Bandwidth < 167959),
     * AverageBandwidth=0,Priority=5,OnDepend=\"1\";
     * #($Bandwidth >= 167959) && ($Bandwidth < 267959),
     * AverageBandwidth=167959,Priority=9;
     * #($Bandwidth >= 167959) && ($Bandwidth < 267959),AverageBandwidth=0,
     * Priority=5,OnDepend=\"3\";#($Bandwidth >= 267959),
     * AverageBandwidth=267959,Priority=9;#($Bandwidth >= 267959),
     * AverageBandwidth=0,Priority=5,OnDepend=\"5\";
     *
     * As I don't know how to do that, I just use the first entry (sel = 0).
     * But to give you a starting point, I offer you above string
     * in the variable 'asm_rule_book'.
     */
    READ_STRING (media, "ASMRuleBook", str, asm_rule_book_len);
    stream->rulebook = gst_asm_rule_book_new (str);
    sel = 0;

    READ_BUFFER_M (media, "OpaqueData", opaque_data, opaque_data_len);

    if (opaque_data_len < 4) {
      GST_DEBUG_OBJECT (ctx, "opaque_data_len %" G_GSIZE_FORMAT " < 4",
          opaque_data_len);
      goto strange_opaque_data;
    }
    if (strncmp (opaque_data, "MLTI", 4)) {
      GST_DEBUG_OBJECT (ctx, "no MLTI found, appending all");
      stream->type_specific_data_len = opaque_data_len;
      stream->type_specific_data = g_memdup (opaque_data, opaque_data_len);
      goto no_type_specific;
    }
    opaque_data += 4;
    opaque_data_len -= 4;

    if (opaque_data_len < 2) {
      GST_DEBUG_OBJECT (ctx, "opaque_data_len %" G_GSIZE_FORMAT " < 2",
          opaque_data_len);
      goto strange_opaque_data;
    }
    stream->num_rules = GST_READ_UINT16_BE (opaque_data);
    opaque_data += 2;
    opaque_data_len -= 2;

    if (sel >= stream->num_rules) {
      GST_DEBUG_OBJECT (ctx, "sel %d >= num_rules %d", sel, stream->num_rules);
      goto strange_opaque_data;
    }

    if (opaque_data_len < 2 * sel) {
      GST_DEBUG_OBJECT (ctx, "opaque_data_len %" G_GSIZE_FORMAT
          " < 2 * sel (%d)", opaque_data_len, 2 * sel);
      goto strange_opaque_data;
    }
    opaque_data += 2 * sel;
    opaque_data_len -= 2 * sel;

    if (opaque_data_len < 2) {
      GST_DEBUG_OBJECT (ctx, "opaque_data_len %" G_GSIZE_FORMAT " < 2",
          opaque_data_len);
      goto strange_opaque_data;
    }
    stream->codec = GST_READ_UINT16_BE (opaque_data);
    opaque_data += 2;
    opaque_data_len -= 2;

    if (opaque_data_len < 2 * (stream->num_rules - sel - 1)) {
      GST_DEBUG_OBJECT (ctx, "opaque_data_len %" G_GSIZE_FORMAT
          " < %d", opaque_data_len, 2 * (stream->num_rules - sel - 1));
      goto strange_opaque_data;
    }
    opaque_data += 2 * (stream->num_rules - sel - 1);
    opaque_data_len -= 2 * (stream->num_rules - sel - 1);

    if (opaque_data_len < 2) {
      GST_DEBUG_OBJECT (ctx, "opaque_data_len %" G_GSIZE_FORMAT " < 2",
          opaque_data_len);
      goto strange_opaque_data;
    }
    stream->num_rules = GST_READ_UINT16_BE (opaque_data);
    opaque_data += 2;
    opaque_data_len -= 2;

    if (stream->codec > stream->num_rules) {
      GST_DEBUG_OBJECT (ctx, "codec %d > num_rules %d", stream->codec,
          stream->num_rules);
      goto strange_opaque_data;
    }

    for (j = 0; j < stream->codec; j++) {
      if (opaque_data_len < 4) {
        GST_DEBUG_OBJECT (ctx, "opaque_data_len %" G_GSIZE_FORMAT " < 4",
            opaque_data_len);
        goto strange_opaque_data;
      }
      len = GST_READ_UINT32_BE (opaque_data);
      opaque_data += 4;
      opaque_data_len -= 4;

      if (opaque_data_len < len) {
        GST_DEBUG_OBJECT (ctx, "opaque_data_len %" G_GSIZE_FORMAT " < len %d",
            opaque_data_len, len);
        goto strange_opaque_data;
      }
      opaque_data += len;
      opaque_data_len -= len;
    }

    if (opaque_data_len < 4) {
      GST_DEBUG_OBJECT (ctx, "opaque_data_len %" G_GSIZE_FORMAT " < 4",
          opaque_data_len);
      goto strange_opaque_data;
    }
    stream->type_specific_data_len = GST_READ_UINT32_BE (opaque_data);
    opaque_data += 4;
    opaque_data_len -= 4;

    if (opaque_data_len < stream->type_specific_data_len) {
      GST_DEBUG_OBJECT (ctx, "opaque_data_len %" G_GSIZE_FORMAT " < %d",
          opaque_data_len, stream->type_specific_data_len);
      goto strange_opaque_data;
    }
    stream->type_specific_data =
        g_memdup (opaque_data, stream->type_specific_data_len);

  no_type_specific:
    size =
        46 + stream->stream_name_len + stream->mime_type_len +
        stream->type_specific_data_len;
    ENSURE_SIZE (offset + size);
    datap = data + offset;

    memcpy (datap, "MDPR", 4);
    GST_WRITE_UINT32_BE (datap + 4, size);
    GST_WRITE_UINT16_BE (datap + 8, 0);
    GST_WRITE_UINT16_BE (datap + 10, i);
    GST_WRITE_UINT32_BE (datap + 12, stream->max_bit_rate);
    GST_WRITE_UINT32_BE (datap + 16, stream->avg_bit_rate);
    GST_WRITE_UINT32_BE (datap + 20, stream->max_packet_size);
    GST_WRITE_UINT32_BE (datap + 24, stream->avg_packet_size);
    GST_WRITE_UINT32_BE (datap + 28, stream->start_time);
    GST_WRITE_UINT32_BE (datap + 32, stream->preroll);
    GST_WRITE_UINT32_BE (datap + 36, stream->duration);
    datap += 40;
    WRITE_STRING1 (datap, stream->stream_name, stream->stream_name_len);
    WRITE_STRING1 (datap, stream->mime_type, stream->mime_type_len);
    GST_WRITE_UINT32_BE (datap, stream->type_specific_data_len);
    if (stream->type_specific_data_len)
      memcpy (datap + 4, stream->type_specific_data,
          stream->type_specific_data_len);
    offset += size;
  }

  /* DATA */
  size = 18;
  ENSURE_SIZE (offset + size);
  datap = data + offset;

  memcpy (datap, "DATA", 4);
  GST_WRITE_UINT32_BE (datap + 4, size);
  GST_WRITE_UINT16_BE (datap + 8, 0);
  GST_WRITE_UINT32_BE (datap + 10, 0);  /* number of packets */
  GST_WRITE_UINT32_BE (datap + 14, 0);  /* next data header */
  offset += size;

  buf = gst_buffer_new ();
  GST_BUFFER_DATA (buf) = data;
  GST_BUFFER_MALLOCDATA (buf) = data;
  GST_BUFFER_SIZE (buf) = offset;

  /* Set on caps */
  gst_structure_set (props, "config", GST_TYPE_BUFFER, buf, NULL);

  /* Overwrite encoding and media fields */
  gst_structure_set (props, "encoding-name", G_TYPE_STRING, "X-REAL-RDT", NULL);
  gst_structure_set (props, "media", G_TYPE_STRING, "application", NULL);

  return TRUE;

  /* ERRORS */
strange_opaque_data:
  {
    GST_ELEMENT_ERROR (ctx, RESOURCE, WRITE, ("Strange opaque data."), (NULL));
    return FALSE;
  }
}

static GstRTSPResult
rtsp_ext_real_stream_select (GstRTSPExtension * ext, GstRTSPUrl * url)
{
  GstRTSPReal *ctx = (GstRTSPReal *) ext;
  GstRTSPResult res;
  GstRTSPMessage request = { 0 };
  GstRTSPMessage response = { 0 };
  gchar *req_url;
  GString *rules;
  GList *walk;
  gint i;
  GHashTable *vars;

  if (!ctx->isreal)
    return GST_RTSP_OK;

  req_url = gst_rtsp_url_get_request_uri (url);

  /* create SET_PARAMETER */
  if ((res = gst_rtsp_message_init_request (&request, GST_RTSP_SET_PARAMETER,
              req_url)) < 0)
    goto create_request_failed;

  g_free (req_url);

  rules = g_string_new ("");
  vars = g_hash_table_new (g_str_hash, g_str_equal);
  g_hash_table_insert (vars, "Bandwidth", "300000");

  for (walk = ctx->streams, i = 0; walk; walk = g_list_next (walk), i++) {
    GstRTSPRealStream *stream;
    gint rulematches[MAX_RULEMATCHES];
    gint j, n;

    stream = (GstRTSPRealStream *) walk->data;

    n = gst_asm_rule_book_match (stream->rulebook, vars, rulematches);
    for (j = 0; j < n; j++) {
      g_string_append_printf (rules, "stream=%u;rule=%u,", i, rulematches[j]);
    }
  }

  g_hash_table_destroy (vars);

  /* strip final , if we added some stream rules */
  if (rules->len > 0) {
    rules = g_string_truncate (rules, rules->len - 1);
  }

  /* do selection */
  gst_rtsp_message_add_header (&request, GST_RTSP_HDR_SUBSCRIBE, rules->str);

  g_string_free (rules, TRUE);

  /* send SET_PARAMETER */
  if ((res = gst_rtsp_extension_send (ext, &request, &response)) < 0)
    goto send_error;

  gst_rtsp_message_unset (&request);
  gst_rtsp_message_unset (&response);

  return GST_RTSP_OK;

  /* ERRORS */
create_request_failed:
  {
    GST_ELEMENT_ERROR (ctx, LIBRARY, INIT,
        ("Could not create request."), (NULL));
    goto reset;
  }
send_error:
  {
    GST_ELEMENT_ERROR (ctx, RESOURCE, WRITE,
        ("Could not send message."), (NULL));
    goto reset;
  }
reset:
  {
    gst_rtsp_message_unset (&request);
    gst_rtsp_message_unset (&response);
    return res;
  }
}

static void gst_rtsp_real_finalize (GObject * object);

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

static void gst_rtsp_real_extension_init (gpointer g_iface,
    gpointer iface_data);

static void
_do_init (GType rtspreal_type)
{
  static const GInterfaceInfo rtspextension_info = {
    gst_rtsp_real_extension_init,
    NULL,
    NULL
  };

  g_type_add_interface_static (rtspreal_type, GST_TYPE_RTSP_EXTENSION,
      &rtspextension_info);
}

GST_BOILERPLATE_FULL (GstRTSPReal, gst_rtsp_real, GstElement, GST_TYPE_ELEMENT,
    _do_init);

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

  gst_element_class_set_details (element_class, &rtspreal_details);
}

static void
gst_rtsp_real_class_init (GstRTSPRealClass * g_class)
{
  GObjectClass *gobject_class;
  GstElementClass *gstelement_class;
  GstRTSPRealClass *klass;

  klass = (GstRTSPRealClass *) g_class;
  gobject_class = (GObjectClass *) klass;
  gstelement_class = (GstElementClass *) klass;

  gobject_class->finalize = gst_rtsp_real_finalize;

  gstelement_class->change_state =
      GST_DEBUG_FUNCPTR (gst_rtsp_real_change_state);

  GST_DEBUG_CATEGORY_INIT (rtspreal_debug, "rtspreal", 0,
      "RealMedia RTSP extension");
}

static void
gst_rtsp_real_init (GstRTSPReal * rtspreal, GstRTSPRealClass * klass)
{
  rtspreal->isreal = FALSE;
}

static void
gst_rtsp_real_finalize (GObject * object)
{
  GstRTSPReal *rtspreal;

  rtspreal = GST_RTSP_REAL (object);

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

static GstStateChangeReturn
gst_rtsp_real_change_state (GstElement * element, GstStateChange transition)
{
  GstStateChangeReturn ret;
  GstRTSPReal *rtspreal;

  rtspreal = GST_RTSP_REAL (element);

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

  return ret;
}

static void
gst_rtsp_real_extension_init (gpointer g_iface, gpointer iface_data)
{
  GstRTSPExtensionInterface *iface = (GstRTSPExtensionInterface *) g_iface;

  iface->before_send = rtsp_ext_real_before_send;
  iface->after_send = rtsp_ext_real_after_send;
  iface->parse_sdp = rtsp_ext_real_parse_sdp;
  iface->stream_select = rtsp_ext_real_stream_select;
  iface->get_transports = rtsp_ext_real_get_transports;
}

gboolean
gst_rtsp_real_plugin_init (GstPlugin * plugin)
{
  return gst_element_register (plugin, "rtspreal",
      GST_RANK_MARGINAL, GST_TYPE_RTSP_REAL);
}