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