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
|
|
99 return strchr(data,'\n')+1;
|
|
100 }
|
|
101
|
|
102 static int filter(const char *in, const char *filter, char **out) {
|
|
103
|
|
104 int flen=strlen(filter);
|
|
105 int len=strchr(in,'\n')-in;
|
|
106
|
|
107 if (!strncmp(in,filter,flen))
|
|
108 {
|
|
109 if(in[flen]=='"') flen++;
|
|
110 if(in[len-1]==13) len--;
|
|
111 if(in[len-1]=='"') len--;
|
|
112 *out = xbuffer_copyin(*out, 0, in+flen, len-flen+1);
|
|
113 (*out)[len-flen]=0;
|
|
114
|
|
115 return len-flen;
|
|
116 }
|
|
117
|
|
118 return 0;
|
|
119 }
|
|
120 static sdpplin_stream_t *sdpplin_parse_stream(char **data) {
|
|
121
|
|
122 sdpplin_stream_t *desc=calloc(1,sizeof(sdpplin_stream_t));
|
|
123 char *buf=xbuffer_init(32);
|
|
124 char *decoded=xbuffer_init(32);
|
|
125 int handled;
|
|
126
|
|
127 if (filter(*data, "m=", &buf)) {
|
|
128 desc->id = strdup(buf);
|
|
129 } else
|
|
130 {
|
|
131 printf("sdpplin: no m= found.\n");
|
|
132 free(desc);
|
|
133 xbuffer_free(buf);
|
|
134 return NULL;
|
|
135 }
|
|
136 *data=nl(*data);
|
|
137
|
|
138 while (**data && *data[0]!='m') {
|
|
139
|
|
140 handled=0;
|
|
141
|
|
142 if(filter(*data,"a=control:streamid=",&buf)) {
|
|
143 desc->stream_id=atoi(buf);
|
|
144 handled=1;
|
|
145 *data=nl(*data);
|
|
146 }
|
|
147
|
|
148 if(filter(*data,"a=MaxBitRate:integer;",&buf)) {
|
|
149 desc->max_bit_rate=atoi(buf);
|
|
150 if (!desc->avg_bit_rate)
|
|
151 desc->avg_bit_rate=desc->max_bit_rate;
|
|
152 handled=1;
|
|
153 *data=nl(*data);
|
|
154 }
|
|
155
|
|
156 if(filter(*data,"a=MaxPacketSize:integer;",&buf)) {
|
|
157 desc->max_packet_size=atoi(buf);
|
|
158 if (!desc->avg_packet_size)
|
|
159 desc->avg_packet_size=desc->max_packet_size;
|
|
160 handled=1;
|
|
161 *data=nl(*data);
|
|
162 }
|
|
163
|
|
164 if(filter(*data,"a=StartTime:integer;",&buf)) {
|
|
165 desc->start_time=atoi(buf);
|
|
166 handled=1;
|
|
167 *data=nl(*data);
|
|
168 }
|
|
169
|
|
170 if(filter(*data,"a=Preroll:integer;",&buf)) {
|
|
171 desc->preroll=atoi(buf);
|
|
172 handled=1;
|
|
173 *data=nl(*data);
|
|
174 }
|
|
175
|
|
176 if(filter(*data,"a=length:npt=",&buf)) {
|
|
177 desc->duration=(uint32_t)(atof(buf)*1000);
|
|
178 handled=1;
|
|
179 *data=nl(*data);
|
|
180 }
|
|
181
|
|
182 if(filter(*data,"a=StreamName:string;",&buf)) {
|
|
183 desc->stream_name=strdup(buf);
|
|
184 desc->stream_name_size=strlen(desc->stream_name);
|
|
185 handled=1;
|
|
186 *data=nl(*data);
|
|
187 }
|
|
188
|
|
189 if(filter(*data,"a=mimetype:string;",&buf)) {
|
|
190 desc->mime_type=strdup(buf);
|
|
191 desc->mime_type_size=strlen(desc->mime_type);
|
|
192 handled=1;
|
|
193 *data=nl(*data);
|
|
194 }
|
|
195
|
|
196 if(filter(*data,"a=OpaqueData:buffer;",&buf)) {
|
|
197 decoded = b64_decode(buf, decoded, &(desc->mlti_data_size));
|
|
198 desc->mlti_data=malloc(sizeof(char)*desc->mlti_data_size);
|
|
199 memcpy(desc->mlti_data, decoded, desc->mlti_data_size);
|
|
200 handled=1;
|
|
201 *data=nl(*data);
|
|
202 #ifdef LOG
|
|
203 printf("mlti_data_size: %i\n", desc->mlti_data_size);
|
|
204 #endif
|
|
205 }
|
|
206
|
|
207 if(filter(*data,"a=ASMRuleBook:string;",&buf)) {
|
|
208 desc->asm_rule_book=strdup(buf);
|
|
209 handled=1;
|
|
210 *data=nl(*data);
|
|
211 }
|
|
212
|
|
213 if(!handled) {
|
|
214 #ifdef LOG
|
|
215 int len=strchr(*data,'\n')-(*data);
|
|
216 buf = xbuffer_copyin(buf, 0, *data, len+1);
|
|
217 buf[len]=0;
|
|
218 printf("libreal: sdpplin: not handled: '%s'\n", buf);
|
|
219 #endif
|
|
220 *data=nl(*data);
|
|
221 }
|
|
222 }
|
|
223
|
|
224 xbuffer_free(buf);
|
|
225 xbuffer_free(decoded);
|
|
226
|
|
227 return desc;
|
|
228 }
|
|
229
|
|
230 sdpplin_t *sdpplin_parse(char *data) {
|
|
231
|
|
232 sdpplin_t *desc=calloc(1,sizeof(sdpplin_t));
|
|
233 sdpplin_stream_t *stream;
|
|
234 char *buf=xbuffer_init(32);
|
|
235 char *decoded=xbuffer_init(32);
|
|
236 int handled;
|
|
237 int len;
|
|
238
|
|
239 while (*data) {
|
|
240
|
|
241 handled=0;
|
|
242
|
|
243 if (filter(data, "m=", &buf)) {
|
|
244 stream=sdpplin_parse_stream(&data);
|
|
245 #ifdef LOG
|
|
246 printf("got data for stream id %u\n", stream->stream_id);
|
|
247 #endif
|
|
248 desc->stream[stream->stream_id]=stream;
|
|
249 continue;
|
|
250 }
|
|
251
|
|
252 if(filter(data,"a=Title:buffer;",&buf)) {
|
|
253 decoded=b64_decode(buf, decoded, &len);
|
|
254 desc->title=strdup(decoded);
|
|
255 handled=1;
|
|
256 data=nl(data);
|
|
257 }
|
|
258
|
|
259 if(filter(data,"a=Author:buffer;",&buf)) {
|
|
260 decoded=b64_decode(buf, decoded, &len);
|
|
261 desc->author=strdup(decoded);
|
|
262 handled=1;
|
|
263 data=nl(data);
|
|
264 }
|
|
265
|
|
266 if(filter(data,"a=Copyright:buffer;",&buf)) {
|
|
267 decoded=b64_decode(buf, decoded, &len);
|
|
268 desc->copyright=strdup(decoded);
|
|
269 handled=1;
|
|
270 data=nl(data);
|
|
271 }
|
|
272
|
|
273 if(filter(data,"a=Abstract:buffer;",&buf)) {
|
|
274 decoded=b64_decode(buf, decoded, &len);
|
|
275 desc->abstract=strdup(decoded);
|
|
276 handled=1;
|
|
277 data=nl(data);
|
|
278 }
|
|
279
|
|
280 if(filter(data,"a=StreamCount:integer;",&buf)) {
|
|
281 desc->stream_count=atoi(buf);
|
|
282 desc->stream=malloc(sizeof(sdpplin_stream_t*)*desc->stream_count);
|
|
283 handled=1;
|
|
284 data=nl(data);
|
|
285 }
|
|
286
|
|
287 if(filter(data,"a=Flags:integer;",&buf)) {
|
|
288 desc->flags=atoi(buf);
|
|
289 handled=1;
|
|
290 data=nl(data);
|
|
291 }
|
|
292
|
|
293 if(!handled) {
|
|
294 #ifdef LOG
|
|
295 int len=strchr(data,'\n')-data;
|
|
296 buf = xbuffer_copyin(buf, 0, data, len+1);
|
|
297 buf[len]=0;
|
|
298 printf("libreal: sdpplin: not handled: '%s'\n", buf);
|
|
299 #endif
|
|
300 data=nl(data);
|
|
301 }
|
|
302 }
|
|
303
|
|
304 xbuffer_free(buf);
|
|
305 xbuffer_free(decoded);
|
|
306
|
|
307 return desc;
|
|
308 }
|
|
309
|
|
310 void sdpplin_free(sdpplin_t *description) {
|
|
311
|
|
312 /* TODO: free strings */
|
|
313 free(description);
|
|
314 }
|
|
315
|