]> git.sesse.net Git - vlc/blob - modules/access/rtsp/real_sdpplin.c
d262d986e11f1d84bb4887e92ea30aad2c48bd5c
[vlc] / modules / access / rtsp / real_sdpplin.c
1 /*
2  * Copyright (C) 2002-2003 the xine project
3  *
4  * This file is part of xine, a free video player.
5  *
6  * xine is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * xine is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA
19  *
20  * $Id$
21  *
22  * sdp/sdpplin parser.
23  *
24  */
25  
26 #include "real.h"
27
28 /*
29  * Decodes base64 strings (based upon b64 package)
30  */
31
32 static char *b64_decode(const char *in, char *out, int *size) {
33
34   char dtable[256];              /* Encode / decode table */
35   int i,k;
36   unsigned int j;
37
38   for (i = 0; i < 255; i++) {
39     dtable[i] = 0x80;
40   }
41   for (i = 'A'; i <= 'Z'; i++) {
42     dtable[i] = 0 + (i - 'A');
43   }
44   for (i = 'a'; i <= 'z'; i++) {
45     dtable[i] = 26 + (i - 'a');
46   }
47   for (i = '0'; i <= '9'; i++) {
48     dtable[i] = 52 + (i - '0');
49   }
50   dtable['+'] = 62;
51   dtable['/'] = 63;
52   dtable['='] = 0;
53
54   k=0;
55   /*CONSTANTCONDITION*/
56   for (j=0; j<strlen(in); j+=4) {
57     char a[4], b[4];
58
59     for (i = 0; i < 4; i++) {
60       int c = in[i+j];
61
62       if (dtable[c] & 0x80) {
63         printf("Illegal character '%c' in input.\n", c);
64         exit(1);
65       }
66       a[i] = (char) c;
67       b[i] = (char) dtable[c];
68     }
69     //xine_buffer_ensure_size(out, k+3);
70     out[k++] = (b[0] << 2) | (b[1] >> 4);
71     out[k++] = (b[1] << 4) | (b[2] >> 2);
72     out[k++] = (b[2] << 6) | b[3];
73     i = a[2] == '=' ? 1 : (a[3] == '=' ? 2 : 3);
74     if (i < 3) {
75       out[k]=0;
76       *size=k;
77       return out;
78     }
79   }
80   out[k]=0;
81   *size=k;
82   return out;
83 }
84
85 static char *nl(char *data) {
86
87   char *nlptr = (data) ? strchr(data,'\n') : NULL;
88   return (nlptr) ? nlptr + 1 : NULL;
89 }
90
91 static int filter(const char *in, const char *filter, char **out) {
92
93   int flen=strlen(filter);
94   size_t len;
95
96   if (!in) return 0;
97
98   len = (strchr(in,'\n')) ? (size_t)(strchr(in,'\n')-in) : strlen(in);
99   if (!strncmp(in,filter,flen)) {
100     if(in[flen]=='"') flen++;
101     if(in[len-1]==13) len--;
102     if(in[len-1]=='"') len--;
103     memcpy(*out, in+flen, len-flen+1);
104     (*out)[len-flen]=0;
105     return len-flen;
106   }
107   return 0;
108 }
109
110 static sdpplin_stream_t *sdpplin_parse_stream(char **data) {
111
112   sdpplin_stream_t *desc = malloc(sizeof(sdpplin_stream_t));
113   char      *buf = malloc(32000);
114   char      *decoded = malloc(32000);
115   int       handled;
116
117   if( !desc ) return NULL;
118   memset(desc, 0, sizeof(sdpplin_stream_t));
119
120   if( !buf ) goto error;
121   if( !decoded ) goto error;
122
123   if (filter(*data, "m=", &buf)) {
124     desc->id = strdup(buf);
125   } else {
126     lprintf("sdpplin: no m= found.\n");
127     goto error;
128   }
129   *data=nl(*data);
130
131   while (*data && **data && *data[0]!='m') {
132     handled=0;
133
134     if(filter(*data,"a=control:streamid=",&buf)) {
135       desc->stream_id=atoi(buf);
136       handled=1;
137       *data=nl(*data);
138     }
139     if(filter(*data,"a=MaxBitRate:integer;",&buf)) {
140       desc->max_bit_rate=atoi(buf);
141       if (!desc->avg_bit_rate)
142         desc->avg_bit_rate=desc->max_bit_rate;
143       handled=1;
144       *data=nl(*data);
145     }
146     if(filter(*data,"a=MaxPacketSize:integer;",&buf)) {
147       desc->max_packet_size=atoi(buf);
148       if (!desc->avg_packet_size)
149         desc->avg_packet_size=desc->max_packet_size;
150       handled=1;
151       *data=nl(*data);
152     }
153     if(filter(*data,"a=StartTime:integer;",&buf)) {
154       desc->start_time=atoi(buf);
155       handled=1;
156       *data=nl(*data);
157     }
158     if(filter(*data,"a=Preroll:integer;",&buf)) {
159       desc->preroll=atoi(buf);
160       handled=1;
161       *data=nl(*data);
162     }
163     if(filter(*data,"a=length:npt=",&buf)) {
164       desc->duration=(uint32_t)(atof(buf)*1000);
165       handled=1;
166       *data=nl(*data);
167     }
168     if(filter(*data,"a=StreamName:string;",&buf)) {
169       desc->stream_name=strdup(buf);
170       desc->stream_name_size=strlen(desc->stream_name);
171       handled=1;
172       *data=nl(*data);
173     }
174     if(filter(*data,"a=mimetype:string;",&buf)) {
175       desc->mime_type=strdup(buf);
176       desc->mime_type_size=strlen(desc->mime_type);
177       handled=1;
178       *data=nl(*data);
179     }
180     if(filter(*data,"a=OpaqueData:buffer;",&buf)) {
181       decoded = b64_decode(buf, decoded, &(desc->mlti_data_size));
182       if ( decoded != NULL ) {
183           desc->mlti_data = malloc(sizeof(char)*desc->mlti_data_size);
184           memcpy(desc->mlti_data, decoded, desc->mlti_data_size);
185           handled=1;
186           *data=nl(*data);
187           lprintf("mlti_data_size: %i\n", desc->mlti_data_size);
188       }
189     }
190     if(filter(*data,"a=ASMRuleBook:string;",&buf)) {
191       desc->asm_rule_book=strdup(buf);
192       handled=1;
193       *data=nl(*data);
194     }
195
196     if(!handled) {
197 #ifdef LOG
198       int len=strchr(*data,'\n')-(*data);
199       memcpy(buf, *data, len+1);
200       buf[len]=0;
201       printf("libreal: sdpplin: not handled: '%s'\n", buf);
202 #endif
203       *data=nl(*data);
204     }
205   }
206   if( buf ) free(buf);
207   if( decoded )free(decoded);
208   return desc;
209
210 error:
211   if( decoded ) free(decoded);
212   if( desc ) free( desc );
213   if( buf ) free( buf );
214   return NULL;
215 }
216
217 sdpplin_t *sdpplin_parse(char *data) {
218
219   sdpplin_t        *desc = malloc(sizeof(sdpplin_t));
220   sdpplin_stream_t *stream;
221   char             *buf=malloc(3200);
222   char             *decoded=malloc(3200);
223   int              handled;
224   int              len;
225
226   if( !desc ) return NULL;
227   if( !buf ) {
228     free( desc );
229     return NULL;
230   }
231   if( !decoded ) {
232     free( buf );
233     free( desc );
234     return NULL;
235   }
236   memset(desc, 0, sizeof(sdpplin_t));
237
238   while (data && *data) {
239     handled=0;
240
241     if (filter(data, "m=", &buf)) {
242       stream=sdpplin_parse_stream(&data);
243       lprintf("got data for stream id %u\n", stream->stream_id);
244       desc->stream[stream->stream_id]=stream;
245       continue;
246     }
247     if(filter(data,"a=Title:buffer;",&buf)) {
248       decoded=b64_decode(buf, decoded, &len);
249           if ( decoded != NULL ) {
250           desc->title=strdup(decoded);
251           handled=1;
252           data=nl(data);
253       }
254     }
255     if(filter(data,"a=Author:buffer;",&buf)) {
256       decoded=b64_decode(buf, decoded, &len);
257           if ( decoded != NULL ) {
258           desc->author=strdup(decoded);
259           handled=1;
260           data=nl(data);
261       }
262     }
263     if(filter(data,"a=Copyright:buffer;",&buf)) {
264       decoded=b64_decode(buf, decoded, &len);
265           if ( decoded != NULL ) {
266           desc->copyright=strdup(decoded);
267           handled=1;
268           data=nl(data);
269       }
270     }
271     if(filter(data,"a=Abstract:buffer;",&buf)) {
272       decoded=b64_decode(buf, decoded, &len);
273       if ( decoded != NULL ) {
274            desc->abstract=strdup(decoded);
275            handled=1;
276            data=nl(data);
277       }
278     }
279     if(filter(data,"a=StreamCount:integer;",&buf)) {
280       desc->stream_count=atoi(buf);
281       desc->stream = malloc(sizeof(sdpplin_stream_t*)*desc->stream_count);
282       handled=1;
283       data=nl(data);
284     }
285     if(filter(data,"a=Flags:integer;",&buf)) {
286       desc->flags=atoi(buf);
287       handled=1;
288       data=nl(data);
289     }
290
291     if(!handled) {
292 #ifdef LOG
293       int len=strchr(data,'\n')-data;
294       memcpy(buf, data, len+1);
295       buf[len]=0;
296       printf("libreal: sdpplin: not handled: '%s'\n", buf);
297 #endif
298       data=nl(data);
299     }
300   }
301
302   free(decoded);
303   free(buf);
304   return desc;
305 }
306
307 void sdpplin_free(sdpplin_t *description) {
308
309   int i;
310
311   if( !description ) return;
312
313   for( i=0; i<description->stream_count; i++ ) {
314     if( description->stream[i] ) {
315       if( description->stream[i]->id ) free( description->stream[i]->id );
316       if( description->stream[i]->bandwidth ) free( description->stream[i]->bandwidth );
317       if( description->stream[i]->range ) free( description->stream[i]->range );
318       if( description->stream[i]->length ) free( description->stream[i]->length );
319       if( description->stream[i]->rtpmap ) free( description->stream[i]->rtpmap );
320       if( description->stream[i]->mimetype ) free( description->stream[i]->mimetype );
321       if( description->stream[i]->stream_name ) free( description->stream[i]->stream_name );
322       if( description->stream[i]->mime_type ) free( description->stream[i]->mime_type );
323       if( description->stream[i]->mlti_data ) free( description->stream[i]->mlti_data );
324       if( description->stream[i]->rmff_flags ) free( description->stream[i]->rmff_flags );
325       if( description->stream[i]->asm_rule_book ) free( description->stream[i]->asm_rule_book );
326       free( description->stream[i] );
327     }
328   }
329   if( description->stream_count ) free( description->stream );
330
331   if( description->owner ) free( description->owner );
332   if( description->session_name ) free( description->session_name );
333   if( description->session_info ) free( description->session_info );
334   if( description->uri ) free( description->uri );
335   if( description->email ) free( description->email );
336   if( description->phone ) free( description->phone );
337   if( description->connection ) free( description->connection );
338   if( description->bandwidth ) free( description->bandwidth );
339   if( description->title ) free( description->title );
340   if( description->author ) free( description->author );
341   if( description->copyright ) free( description->copyright );
342   if( description->keywords ) free( description->keywords );
343   if( description->asm_rule_book ) free( description->asm_rule_book );
344   if( description->abstract ) free( description->abstract );
345   if( description->range ) free( description->range );
346   free(description);
347 }