5804747ee79933a0a4c8ac6f0036b2eeda58952b
[platform/upstream/gstreamer.git] / gst / realmedia / rtspreal.c
1 /* GStreamer
2  * Copyright (C) <2005,2006> Wim Taymans <wim@fluendo.com>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19 /* Element-Checklist-Version: 5 */
20
21 /**
22  * SECTION:element-rtspreal
23  * @title: rtspreal
24  *
25  * A RealMedia RTSP extension
26  */
27
28 #ifdef HAVE_CONFIG_H
29 #include "config.h"
30 #endif
31
32 #ifdef HAVE_STDINT_H
33 #include <stdint.h>
34 #endif
35
36 #include <stdlib.h>
37 #include <string.h>
38
39 #include <gst/rtsp/gstrtspextension.h>
40
41 #include "realhash.h"
42 #include "rtspreal.h"
43 #include "asmrules.h"
44
45 GST_DEBUG_CATEGORY_STATIC (rtspreal_debug);
46 #define GST_CAT_DEFAULT (rtspreal_debug)
47
48 #define SERVER_PREFIX "RealServer"
49 #define DEFAULT_BANDWIDTH       "10485800"
50
51 static GstRTSPResult
52 rtsp_ext_real_get_transports (GstRTSPExtension * ext,
53     GstRTSPLowerTrans protocols, gchar ** transport)
54 {
55   GstRTSPReal *ctx = (GstRTSPReal *) ext;
56   GString *str;
57
58   if (!ctx->isreal)
59     return GST_RTSP_OK;
60
61   GST_DEBUG_OBJECT (ext, "generating transports for %d", protocols);
62
63   str = g_string_new ("");
64
65   /*
66      if (protocols & GST_RTSP_LOWER_TRANS_UDP_MCAST) {
67      g_string_append (str, "x-real-rdt/mcast;client_port=%%u1;mode=play,");
68      }
69      if (protocols & GST_RTSP_LOWER_TRANS_UDP) {
70      g_string_append (str, "x-real-rdt/udp;client_port=%%u1;mode=play,");
71      g_string_append (str, "x-pn-tng/udp;client_port=%%u1;mode=play,");
72      }
73    */
74   if (protocols & GST_RTSP_LOWER_TRANS_TCP) {
75     g_string_append (str, "x-real-rdt/tcp;mode=play,");
76     g_string_append (str, "x-pn-tng/tcp;mode=play,");
77   }
78
79   /* if we added something, remove trailing ',' */
80   if (str->len > 0)
81     g_string_truncate (str, str->len - 1);
82
83   *transport = g_string_free (str, FALSE);
84
85   return GST_RTSP_OK;
86 }
87
88 static GstRTSPResult
89 rtsp_ext_real_before_send (GstRTSPExtension * ext, GstRTSPMessage * request)
90 {
91   GstRTSPReal *ctx = (GstRTSPReal *) ext;
92
93   switch (request->type_data.request.method) {
94     case GST_RTSP_OPTIONS:
95     {
96       gst_rtsp_message_add_header (request, GST_RTSP_HDR_USER_AGENT,
97           //"RealMedia Player (" GST_PACKAGE_NAME ")");
98           "RealMedia Player Version 6.0.9.1235 (linux-2.0-libc6-i386-gcc2.95)");
99       gst_rtsp_message_add_header (request, GST_RTSP_HDR_CLIENT_CHALLENGE,
100           "9e26d33f2984236010ef6253fb1887f7");
101       gst_rtsp_message_add_header (request, GST_RTSP_HDR_COMPANY_ID,
102           "KnKV4M4I/B2FjJ1TToLycw==");
103       gst_rtsp_message_add_header (request, GST_RTSP_HDR_GUID,
104           "00000000-0000-0000-0000-000000000000");
105       gst_rtsp_message_add_header (request, GST_RTSP_HDR_REGION_DATA, "0");
106       gst_rtsp_message_add_header (request, GST_RTSP_HDR_PLAYER_START_TIME,
107           "[28/03/2003:22:50:23 00:00]");
108       gst_rtsp_message_add_header (request, GST_RTSP_HDR_CLIENT_ID,
109           "Linux_2.4_6.0.9.1235_play32_RN01_EN_586");
110       ctx->isreal = FALSE;
111       break;
112     }
113     case GST_RTSP_DESCRIBE:
114     {
115       if (ctx->isreal) {
116         gst_rtsp_message_add_header (request, GST_RTSP_HDR_BANDWIDTH,
117             DEFAULT_BANDWIDTH);
118         gst_rtsp_message_add_header (request, GST_RTSP_HDR_GUID,
119             "00000000-0000-0000-0000-000000000000");
120         gst_rtsp_message_add_header (request, GST_RTSP_HDR_REGION_DATA, "0");
121         gst_rtsp_message_add_header (request, GST_RTSP_HDR_CLIENT_ID,
122             "Linux_2.4_6.0.9.1235_play32_RN01_EN_586");
123         gst_rtsp_message_add_header (request, GST_RTSP_HDR_MAX_ASM_WIDTH, "1");
124         gst_rtsp_message_add_header (request, GST_RTSP_HDR_LANGUAGE, "en-US");
125         gst_rtsp_message_add_header (request, GST_RTSP_HDR_REQUIRE,
126             "com.real.retain-entity-for-setup");
127       }
128       break;
129     }
130     case GST_RTSP_SETUP:
131     {
132       if (ctx->isreal) {
133         gchar *value =
134             g_strdup_printf ("%s, sd=%s", ctx->challenge2, ctx->checksum);
135         gst_rtsp_message_add_header (request, GST_RTSP_HDR_REAL_CHALLENGE2,
136             value);
137         gst_rtsp_message_add_header (request, GST_RTSP_HDR_IF_MATCH, ctx->etag);
138         g_free (value);
139       }
140       break;
141     }
142     default:
143       break;
144   }
145   return GST_RTSP_OK;
146 }
147
148 static GstRTSPResult
149 rtsp_ext_real_after_send (GstRTSPExtension * ext, GstRTSPMessage * req,
150     GstRTSPMessage * resp)
151 {
152   GstRTSPReal *ctx = (GstRTSPReal *) ext;
153
154   switch (req->type_data.request.method) {
155     case GST_RTSP_OPTIONS:
156     {
157       gchar *challenge1 = NULL;
158       gchar *server = NULL;
159
160       gst_rtsp_message_get_header (resp, GST_RTSP_HDR_SERVER, &server, 0);
161
162       gst_rtsp_message_get_header (resp, GST_RTSP_HDR_REAL_CHALLENGE1,
163           &challenge1, 0);
164       if (!challenge1)
165         goto no_challenge1;
166
167       gst_rtsp_ext_real_calc_response_and_checksum (ctx->challenge2,
168           ctx->checksum, challenge1);
169
170       GST_DEBUG_OBJECT (ctx, "Found Real challenge tag");
171       ctx->isreal = TRUE;
172       break;
173     }
174     case GST_RTSP_DESCRIBE:
175     {
176       gchar *etag = NULL;
177       guint len;
178
179       gst_rtsp_message_get_header (resp, GST_RTSP_HDR_ETAG, &etag, 0);
180       if (etag) {
181         len = sizeof (ctx->etag);
182         strncpy (ctx->etag, etag, len);
183         ctx->etag[len - 1] = '\0';
184       }
185       break;
186     }
187     default:
188       break;
189   }
190   return GST_RTSP_OK;
191
192   /* ERRORS */
193 no_challenge1:
194   {
195     GST_DEBUG_OBJECT (ctx, "Could not find challenge tag.");
196     ctx->isreal = FALSE;
197     return GST_RTSP_OK;
198   }
199 }
200
201 #define ENSURE_SIZE(size)              \
202 G_STMT_START {                         \
203   while (data_len < size) {            \
204     data_len += 1024;                  \
205     data = g_realloc (data, data_len); \
206   }                                    \
207 } G_STMT_END
208
209 #define READ_BUFFER_GEN(src, func, name, dest, dest_len)    \
210 G_STMT_START {                                              \
211   dest = (gchar *)func (src, name);                         \
212   dest_len = 0;                                             \
213   if (!dest) {                                              \
214     dest = (char *) "";                                     \
215   }                                                         \
216   else if (!strncmp (dest, "buffer;\"", 8)) {               \
217     dest += 8;                                              \
218     dest_len = strlen (dest) - 1;                           \
219     dest[dest_len] = '\0';                                  \
220     g_base64_decode_inplace (dest, &dest_len);            \
221   }                                                         \
222 } G_STMT_END
223
224 #define READ_BUFFER(sdp, name, dest, dest_len)        \
225  READ_BUFFER_GEN(sdp, gst_sdp_message_get_attribute_val, name, dest, dest_len)
226 #define READ_BUFFER_M(media, name, dest, dest_len)    \
227  READ_BUFFER_GEN(media, gst_sdp_media_get_attribute_val, name, dest, dest_len)
228
229 #define READ_INT_GEN(src, func, name, dest)               \
230 G_STMT_START {                                            \
231   const gchar *val = func (src, name);                          \
232   if (val && !strncmp (val, "integer;", 8))               \
233       dest = atoi (val + 8);                              \
234     else                                                  \
235       dest = 0;                                           \
236 } G_STMT_END
237
238 #define READ_INT(sdp, name, dest)                             \
239  READ_INT_GEN(sdp, gst_sdp_message_get_attribute_val, name, dest)
240 #define READ_INT_M(media, name, dest)                         \
241  READ_INT_GEN(media, gst_sdp_media_get_attribute_val, name, dest)
242
243 #define READ_STRING(media, name, dest, dest_len)              \
244 G_STMT_START {                                                \
245   const gchar *val = gst_sdp_media_get_attribute_val (media, name); \
246   if (val && !strncmp (val, "string;\"", 8)) {                \
247     dest = (gchar *) val + 8;                                 \
248     dest_len = strlen (dest) - 1;                             \
249     dest[dest_len] = '\0';                                    \
250   } else {                                                    \
251     dest = (char *) "";                                       \
252     dest_len = 0;                                             \
253   }                                                           \
254 } G_STMT_END
255
256 #define WRITE_STRING1(datap, str, str_len)            \
257 G_STMT_START {                                        \
258   *datap = str_len;                                   \
259   memcpy ((datap) + 1, str, str_len);                 \
260   datap += str_len + 1;                               \
261 } G_STMT_END
262
263 #define WRITE_STRING2(datap, str, str_len)            \
264 G_STMT_START {                                        \
265   GST_WRITE_UINT16_BE (datap, str_len);               \
266   memcpy (datap + 2, str, str_len);                   \
267   datap += str_len + 2;                               \
268 } G_STMT_END
269
270 static GstRTSPResult
271 rtsp_ext_real_parse_sdp (GstRTSPExtension * ext, GstSDPMessage * sdp,
272     GstStructure * props)
273 {
274   GstRTSPReal *ctx = (GstRTSPReal *) ext;
275   guint size;
276   gint i;
277   gchar *title, *author, *copyright, *comment;
278   gsize title_len, author_len, copyright_len, comment_len;
279   guint8 *data = NULL, *datap;
280   guint data_len = 0, offset;
281   GstBuffer *buf;
282   gchar *opaque_data;
283   gsize opaque_data_len, asm_rule_book_len;
284   GHashTable *vars;
285   GString *rules;
286
287   /* don't bother for non-real formats */
288   READ_INT (sdp, "IsRealDataType", ctx->isreal);
289   if (!ctx->isreal)
290     return TRUE;
291
292   /* Force PAUSE | PLAY */
293   //src->methods |= GST_RTSP_PLAY | GST_RTSP_PAUSE;
294
295   ctx->n_streams = gst_sdp_message_medias_len (sdp);
296
297   ctx->max_bit_rate = 0;
298   ctx->avg_bit_rate = 0;
299   ctx->max_packet_size = 0;
300   ctx->avg_packet_size = 0;
301   ctx->duration = 0;
302
303   for (i = 0; i < ctx->n_streams; i++) {
304     const GstSDPMedia *media;
305     gint intval;
306
307     media = gst_sdp_message_get_media (sdp, i);
308
309     READ_INT_M (media, "MaxBitRate", intval);
310     ctx->max_bit_rate += intval;
311     READ_INT_M (media, "AvgBitRate", intval);
312     ctx->avg_bit_rate += intval;
313     READ_INT_M (media, "MaxPacketSize", intval);
314     ctx->max_packet_size = MAX (ctx->max_packet_size, intval);
315     READ_INT_M (media, "AvgPacketSize", intval);
316     ctx->avg_packet_size = (ctx->avg_packet_size * i + intval) / (i + 1);
317     READ_INT_M (media, "Duration", intval);
318     ctx->duration = MAX (ctx->duration, intval);
319   }
320
321   /* FIXME: use GstByteWriter to write the header */
322   /* PROP */
323   offset = 0;
324   size = 50;
325   ENSURE_SIZE (size);
326   datap = data + offset;
327
328   memcpy (datap + 0, "PROP", 4);
329   GST_WRITE_UINT32_BE (datap + 4, size);
330   GST_WRITE_UINT16_BE (datap + 8, 0);
331   GST_WRITE_UINT32_BE (datap + 10, ctx->max_bit_rate);
332   GST_WRITE_UINT32_BE (datap + 14, ctx->avg_bit_rate);
333   GST_WRITE_UINT32_BE (datap + 18, ctx->max_packet_size);
334   GST_WRITE_UINT32_BE (datap + 22, ctx->avg_packet_size);
335   GST_WRITE_UINT32_BE (datap + 26, 0);
336   GST_WRITE_UINT32_BE (datap + 30, ctx->duration);
337   GST_WRITE_UINT32_BE (datap + 34, 0);
338   GST_WRITE_UINT32_BE (datap + 38, 0);
339   GST_WRITE_UINT32_BE (datap + 42, 0);
340   GST_WRITE_UINT16_BE (datap + 46, ctx->n_streams);
341   GST_WRITE_UINT16_BE (datap + 48, 0);
342   offset += size;
343
344   /* CONT */
345   READ_BUFFER (sdp, "Title", title, title_len);
346   READ_BUFFER (sdp, "Author", author, author_len);
347   READ_BUFFER (sdp, "Comment", comment, comment_len);
348   READ_BUFFER (sdp, "Copyright", copyright, copyright_len);
349
350   size = 18 + title_len + author_len + comment_len + copyright_len;
351   ENSURE_SIZE (offset + size);
352   datap = data + offset;
353
354   memcpy (datap, "CONT", 4);
355   GST_WRITE_UINT32_BE (datap + 4, size);
356   GST_WRITE_UINT16_BE (datap + 8, 0);   /* Version */
357   datap += 10;
358   WRITE_STRING2 (datap, title, title_len);
359   WRITE_STRING2 (datap, author, author_len);
360   WRITE_STRING2 (datap, copyright, copyright_len);
361   WRITE_STRING2 (datap, comment, comment_len);
362   offset += size;
363
364   /* fix the hashtale for the rule parser */
365   rules = g_string_new ("");
366   vars = g_hash_table_new (g_str_hash, g_str_equal);
367   g_hash_table_insert (vars, (gchar *) "Bandwidth",
368       (gchar *) DEFAULT_BANDWIDTH);
369
370   /* MDPR */
371   for (i = 0; i < ctx->n_streams; i++) {
372     const GstSDPMedia *media;
373     guint32 len;
374     GstRTSPRealStream *stream;
375     gchar *str;
376     gint rulematches[MAX_RULEMATCHES];
377     gint sel, j, n;
378
379     media = gst_sdp_message_get_media (sdp, i);
380
381     if (media->media && !strcmp (media->media, "data"))
382       continue;
383
384     stream = g_new0 (GstRTSPRealStream, 1);
385     ctx->streams = g_list_append (ctx->streams, stream);
386
387     READ_INT_M (media, "MaxBitRate", stream->max_bit_rate);
388     READ_INT_M (media, "AvgBitRate", stream->avg_bit_rate);
389     READ_INT_M (media, "MaxPacketSize", stream->max_packet_size);
390     READ_INT_M (media, "AvgPacketSize", stream->avg_packet_size);
391     READ_INT_M (media, "StartTime", stream->start_time);
392     READ_INT_M (media, "Preroll", stream->preroll);
393     READ_INT_M (media, "Duration", stream->duration);
394     READ_STRING (media, "StreamName", str, stream->stream_name_len);
395     stream->stream_name = g_strndup (str, stream->stream_name_len);
396     READ_STRING (media, "mimetype", str, stream->mime_type_len);
397     stream->mime_type = g_strndup (str, stream->mime_type_len);
398
399     /* FIXME: Depending on the current bandwidth, we need to select one
400      * bandwidth out of a list offered by the server. Someone needs to write
401      * a parser for strings like
402      *
403      * #($Bandwidth < 67959),TimestampDelivery=T,DropByN=T,priority=9;
404      * #($Bandwidth >= 67959) && ($Bandwidth < 167959),AverageBandwidth=67959,
405      * Priority=9;#($Bandwidth >= 67959) && ($Bandwidth < 167959),
406      * AverageBandwidth=0,Priority=5,OnDepend=\"1\";
407      * #($Bandwidth >= 167959) && ($Bandwidth < 267959),
408      * AverageBandwidth=167959,Priority=9;
409      * #($Bandwidth >= 167959) && ($Bandwidth < 267959),AverageBandwidth=0,
410      * Priority=5,OnDepend=\"3\";#($Bandwidth >= 267959),
411      * AverageBandwidth=267959,Priority=9;#($Bandwidth >= 267959),
412      * AverageBandwidth=0,Priority=5,OnDepend=\"5\";
413      *
414      * As I don't know how to do that, I just use the first entry (sel = 0).
415      * But to give you a starting point, I offer you above string
416      * in the variable 'asm_rule_book'.
417      */
418     READ_STRING (media, "ASMRuleBook", str, asm_rule_book_len);
419     stream->rulebook = gst_asm_rule_book_new (str);
420
421     n = gst_asm_rule_book_match (stream->rulebook, vars, rulematches);
422     for (j = 0; j < n; j++) {
423       g_string_append_printf (rules, "stream=%u;rule=%u,", i, rulematches[j]);
424     }
425
426     /* get the MLTI for the first matched rules */
427     sel = rulematches[0];
428
429     READ_BUFFER_M (media, "OpaqueData", opaque_data, opaque_data_len);
430
431     if (opaque_data_len < 4) {
432       GST_DEBUG_OBJECT (ctx, "opaque_data_len %" G_GSIZE_FORMAT " < 4",
433           opaque_data_len);
434       goto strange_opaque_data;
435     }
436     if (strncmp (opaque_data, "MLTI", 4)) {
437       GST_DEBUG_OBJECT (ctx, "no MLTI found, appending all");
438       stream->type_specific_data_len = opaque_data_len;
439       stream->type_specific_data = g_memdup2 (opaque_data, opaque_data_len);
440       goto no_type_specific;
441     }
442     opaque_data += 4;
443     opaque_data_len -= 4;
444
445     if (opaque_data_len < 2) {
446       GST_DEBUG_OBJECT (ctx, "opaque_data_len %" G_GSIZE_FORMAT " < 2",
447           opaque_data_len);
448       goto strange_opaque_data;
449     }
450     stream->num_rules = GST_READ_UINT16_BE (opaque_data);
451     opaque_data += 2;
452     opaque_data_len -= 2;
453
454     if (sel >= stream->num_rules) {
455       GST_DEBUG_OBJECT (ctx, "sel %d >= num_rules %d", sel, stream->num_rules);
456       goto strange_opaque_data;
457     }
458
459     if (opaque_data_len < 2 * sel) {
460       GST_DEBUG_OBJECT (ctx, "opaque_data_len %" G_GSIZE_FORMAT
461           " < 2 * sel (%d)", opaque_data_len, 2 * sel);
462       goto strange_opaque_data;
463     }
464     opaque_data += 2 * sel;
465     opaque_data_len -= 2 * sel;
466
467     if (opaque_data_len < 2) {
468       GST_DEBUG_OBJECT (ctx, "opaque_data_len %" G_GSIZE_FORMAT " < 2",
469           opaque_data_len);
470       goto strange_opaque_data;
471     }
472     stream->codec = GST_READ_UINT16_BE (opaque_data);
473     opaque_data += 2;
474     opaque_data_len -= 2;
475
476     if (opaque_data_len < 2 * (stream->num_rules - sel - 1)) {
477       GST_DEBUG_OBJECT (ctx, "opaque_data_len %" G_GSIZE_FORMAT
478           " < %d", opaque_data_len, 2 * (stream->num_rules - sel - 1));
479       goto strange_opaque_data;
480     }
481     opaque_data += 2 * (stream->num_rules - sel - 1);
482     opaque_data_len -= 2 * (stream->num_rules - sel - 1);
483
484     if (opaque_data_len < 2) {
485       GST_DEBUG_OBJECT (ctx, "opaque_data_len %" G_GSIZE_FORMAT " < 2",
486           opaque_data_len);
487       goto strange_opaque_data;
488     }
489     stream->num_rules = GST_READ_UINT16_BE (opaque_data);
490     opaque_data += 2;
491     opaque_data_len -= 2;
492
493     if (stream->codec > stream->num_rules) {
494       GST_DEBUG_OBJECT (ctx, "codec %d > num_rules %d", stream->codec,
495           stream->num_rules);
496       goto strange_opaque_data;
497     }
498
499     for (j = 0; j < stream->codec; j++) {
500       if (opaque_data_len < 4) {
501         GST_DEBUG_OBJECT (ctx, "opaque_data_len %" G_GSIZE_FORMAT " < 4",
502             opaque_data_len);
503         goto strange_opaque_data;
504       }
505       len = GST_READ_UINT32_BE (opaque_data);
506       opaque_data += 4;
507       opaque_data_len -= 4;
508
509       if (opaque_data_len < len) {
510         GST_DEBUG_OBJECT (ctx, "opaque_data_len %" G_GSIZE_FORMAT " < len %d",
511             opaque_data_len, len);
512         goto strange_opaque_data;
513       }
514       opaque_data += len;
515       opaque_data_len -= len;
516     }
517
518     if (opaque_data_len < 4) {
519       GST_DEBUG_OBJECT (ctx, "opaque_data_len %" G_GSIZE_FORMAT " < 4",
520           opaque_data_len);
521       goto strange_opaque_data;
522     }
523     stream->type_specific_data_len = GST_READ_UINT32_BE (opaque_data);
524     opaque_data += 4;
525     opaque_data_len -= 4;
526
527     if (opaque_data_len < stream->type_specific_data_len) {
528       GST_DEBUG_OBJECT (ctx, "opaque_data_len %" G_GSIZE_FORMAT " < %d",
529           opaque_data_len, stream->type_specific_data_len);
530       goto strange_opaque_data;
531     }
532     stream->type_specific_data =
533         g_memdup2 (opaque_data, stream->type_specific_data_len);
534
535   no_type_specific:
536     size =
537         46 + stream->stream_name_len + stream->mime_type_len +
538         stream->type_specific_data_len;
539     ENSURE_SIZE (offset + size);
540     datap = data + offset;
541
542     memcpy (datap, "MDPR", 4);
543     GST_WRITE_UINT32_BE (datap + 4, size);
544     GST_WRITE_UINT16_BE (datap + 8, 0);
545     GST_WRITE_UINT16_BE (datap + 10, i);
546     GST_WRITE_UINT32_BE (datap + 12, stream->max_bit_rate);
547     GST_WRITE_UINT32_BE (datap + 16, stream->avg_bit_rate);
548     GST_WRITE_UINT32_BE (datap + 20, stream->max_packet_size);
549     GST_WRITE_UINT32_BE (datap + 24, stream->avg_packet_size);
550     GST_WRITE_UINT32_BE (datap + 28, stream->start_time);
551     GST_WRITE_UINT32_BE (datap + 32, stream->preroll);
552     GST_WRITE_UINT32_BE (datap + 36, stream->duration);
553     datap += 40;
554     WRITE_STRING1 (datap, stream->stream_name, stream->stream_name_len);
555     WRITE_STRING1 (datap, stream->mime_type, stream->mime_type_len);
556     GST_WRITE_UINT32_BE (datap, stream->type_specific_data_len);
557     if (stream->type_specific_data_len)
558       memcpy (datap + 4, stream->type_specific_data,
559           stream->type_specific_data_len);
560     offset += size;
561   }
562
563   /* destroy the rulebook hashtable now */
564   g_hash_table_destroy (vars);
565
566   /* strip final , if we added some stream rules */
567   if (rules->len > 0) {
568     rules = g_string_truncate (rules, rules->len - 1);
569   }
570
571   /* and store rules in the context */
572   ctx->rules = g_string_free (rules, FALSE);
573
574   /* DATA */
575   size = 18;
576   ENSURE_SIZE (offset + size);
577   datap = data + offset;
578
579   memcpy (datap, "DATA", 4);
580   GST_WRITE_UINT32_BE (datap + 4, size);
581   GST_WRITE_UINT16_BE (datap + 8, 0);
582   GST_WRITE_UINT32_BE (datap + 10, 0);  /* number of packets */
583   GST_WRITE_UINT32_BE (datap + 14, 0);  /* next data header */
584   offset += size;
585
586   buf = gst_buffer_new_wrapped (data, offset);
587
588   /* Set on caps */
589   GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_HEADER);
590   gst_structure_set (props, "config", GST_TYPE_BUFFER, buf, NULL);
591   gst_buffer_unref (buf);
592
593   /* Overwrite encoding and media fields */
594   gst_structure_set (props, "encoding-name", G_TYPE_STRING, "X-REAL-RDT", NULL);
595   gst_structure_set (props, "media", G_TYPE_STRING, "application", NULL);
596
597   return TRUE;
598
599   /* ERRORS */
600 strange_opaque_data:
601   {
602     g_string_free (rules, TRUE);
603     g_hash_table_destroy (vars);
604     g_free (data);
605
606     GST_ELEMENT_ERROR (ctx, RESOURCE, WRITE, ("Strange opaque data."), (NULL));
607     return FALSE;
608   }
609 }
610
611 static GstRTSPResult
612 rtsp_ext_real_stream_select (GstRTSPExtension * ext, GstRTSPUrl * url)
613 {
614   GstRTSPReal *ctx = (GstRTSPReal *) ext;
615   GstRTSPResult res;
616   GstRTSPMessage request = { 0 };
617   GstRTSPMessage response = { 0 };
618   gchar *req_url;
619
620   if (!ctx->isreal)
621     return GST_RTSP_OK;
622
623   if (!ctx->rules)
624     return GST_RTSP_OK;
625
626   req_url = gst_rtsp_url_get_request_uri (url);
627
628   /* create SET_PARAMETER */
629   if ((res = gst_rtsp_message_init_request (&request, GST_RTSP_SET_PARAMETER,
630               req_url)) < 0)
631     goto create_request_failed;
632
633   g_free (req_url);
634
635   gst_rtsp_message_add_header (&request, GST_RTSP_HDR_SUBSCRIBE, ctx->rules);
636
637   /* send SET_PARAMETER */
638   if ((res = gst_rtsp_extension_send (ext, &request, &response)) < 0)
639     goto send_error;
640
641   gst_rtsp_message_unset (&request);
642   gst_rtsp_message_unset (&response);
643
644   return GST_RTSP_OK;
645
646   /* ERRORS */
647 create_request_failed:
648   {
649     GST_ELEMENT_ERROR (ctx, LIBRARY, INIT,
650         ("Could not create request."), (NULL));
651     g_free (req_url);
652     goto reset;
653   }
654 send_error:
655   {
656     GST_ELEMENT_ERROR (ctx, RESOURCE, WRITE,
657         ("Could not send message."), (NULL));
658     goto reset;
659   }
660 reset:
661   {
662     gst_rtsp_message_unset (&request);
663     gst_rtsp_message_unset (&response);
664     return res;
665   }
666 }
667
668 static void gst_rtsp_real_extension_init (gpointer g_iface,
669     gpointer iface_data);
670 static void gst_rtsp_real_finalize (GObject * obj);
671
672 #define gst_rtsp_real_parent_class parent_class
673 G_DEFINE_TYPE_WITH_CODE (GstRTSPReal, gst_rtsp_real, GST_TYPE_ELEMENT,
674     G_IMPLEMENT_INTERFACE (GST_TYPE_RTSP_EXTENSION,
675         gst_rtsp_real_extension_init));
676 GST_ELEMENT_REGISTER_DEFINE (rtspreal, "rtspreal",
677     GST_RANK_MARGINAL, GST_TYPE_RTSP_REAL);
678
679 static void
680 gst_rtsp_real_class_init (GstRTSPRealClass * g_class)
681 {
682   GObjectClass *gobject_class = (GObjectClass *) g_class;
683   GstElementClass *gstelement_class = (GstElementClass *) g_class;
684
685   gobject_class->finalize = gst_rtsp_real_finalize;
686
687   gst_element_class_set_static_metadata (gstelement_class,
688       "RealMedia RTSP Extension", "Network/Extension/Protocol",
689       "Extends RTSP so that it can handle RealMedia setup",
690       "Wim Taymans <wim.taymans@gmail.com>");
691
692   GST_DEBUG_CATEGORY_INIT (rtspreal_debug, "rtspreal", 0,
693       "RealMedia RTSP extension");
694 }
695
696 static void
697 gst_rtsp_real_init (GstRTSPReal * rtspreal)
698 {
699   rtspreal->isreal = FALSE;
700 }
701
702 static void
703 gst_rtsp_stream_free (GstRTSPRealStream * stream)
704 {
705   g_free (stream->stream_name);
706   g_free (stream->mime_type);
707   gst_asm_rule_book_free (stream->rulebook);
708   g_free (stream->type_specific_data);
709
710   g_free (stream);
711 }
712
713 static void
714 gst_rtsp_real_finalize (GObject * obj)
715 {
716   GstRTSPReal *r = (GstRTSPReal *) obj;
717
718   g_list_foreach (r->streams, (GFunc) gst_rtsp_stream_free, NULL);
719   g_list_free (r->streams);
720   g_free (r->rules);
721
722   G_OBJECT_CLASS (parent_class)->finalize (obj);
723 }
724
725 static void
726 gst_rtsp_real_extension_init (gpointer g_iface, gpointer iface_data)
727 {
728   GstRTSPExtensionInterface *iface = (GstRTSPExtensionInterface *) g_iface;
729
730   iface->before_send = rtsp_ext_real_before_send;
731   iface->after_send = rtsp_ext_real_after_send;
732   iface->parse_sdp = rtsp_ext_real_parse_sdp;
733   iface->stream_select = rtsp_ext_real_stream_select;
734   iface->get_transports = rtsp_ext_real_get_transports;
735 }