2 * This file was ported to MPlayer from xine CVS sdpplin.c,v 1.1 2002/12/24 01:30:22
6 * Copyright (C) 2002 the xine project
8 * This file is part of xine, a free video player.
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.
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.
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
30 #include "stream/librtsp/rtsp.h"
40 * Decodes base64 strings (based upon b64 package)
43 static char *b64_decode(const char *in
, char *out
, int *size
)
45 char dtable
[256]; /* Encode / decode table */
48 for (i
= 0; i
< 255; i
++) {
51 for (i
= 'A'; i
<= 'Z'; i
++) {
52 dtable
[i
] = 0 + (i
- 'A');
54 for (i
= 'a'; i
<= 'z'; i
++) {
55 dtable
[i
] = 26 + (i
- 'a');
57 for (i
= '0'; i
<= '9'; i
++) {
58 dtable
[i
] = 52 + (i
- '0');
67 for (j
=0; j
<strlen(in
); j
+=4)
71 for (i
= 0; i
< 4; i
++) {
74 if (dtable
[c
] & 0x80) {
75 printf("Illegal character '%c' in input.\n", c
);
80 b
[i
] = (char) dtable
[c
];
82 out
= xbuffer_ensure_size(out
, k
+4);
83 out
[k
++] = (b
[0] << 2) | (b
[1] >> 4);
84 out
[k
++] = (b
[1] << 4) | (b
[2] >> 2);
85 out
[k
++] = (b
[2] << 6) | b
[3];
86 i
= a
[2] == '=' ? 1 : (a
[3] == '=' ? 2 : 3);
98 static char *nl(char *data
) {
100 char *nlptr
= (data
) ? strchr(data
,'\n') : NULL
;
101 return (nlptr
) ? nlptr
+ 1 : NULL
;
104 static int filter(const char *in
, const char *filter
, char **out
) {
106 int flen
=strlen(filter
);
112 len
= (strchr(in
,'\n')) ? strchr(in
,'\n')-in
: strlen(in
);
114 if (!strncmp(in
,filter
,flen
))
116 if(in
[flen
]=='"') flen
++;
117 if(in
[len
-1]==13) len
--;
118 if(in
[len
-1]=='"') len
--;
119 *out
= xbuffer_copyin(*out
, 0, in
+flen
, len
-flen
+1);
127 static sdpplin_stream_t
*sdpplin_parse_stream(char **data
) {
129 sdpplin_stream_t
*desc
=calloc(1,sizeof(sdpplin_stream_t
));
130 char *buf
=xbuffer_init(32);
131 char *decoded
=xbuffer_init(32);
135 if (filter(*data
, "m=", &buf
)) {
136 desc
->id
= strdup(buf
);
139 printf("sdpplin: no m= found.\n");
148 while (*data
&& **data
&& *data
[0]!='m') {
151 int len
=strchr(*data
,'\n')-(*data
);
152 buf
= xbuffer_copyin(buf
, 0, *data
, len
+1);
154 printf("libreal: sdpplin_stream: '%s'\n", buf
);
160 if(filter(*data
,"a=control:streamid=",&buf
)) {
161 desc
->stream_id
=atoi(buf
);
166 if(filter(*data
,"a=MaxBitRate:integer;",&buf
)) {
167 desc
->max_bit_rate
=atoi(buf
);
168 if (!desc
->avg_bit_rate
)
169 desc
->avg_bit_rate
=desc
->max_bit_rate
;
174 if(filter(*data
,"a=MaxPacketSize:integer;",&buf
)) {
175 desc
->max_packet_size
=atoi(buf
);
176 if (!desc
->avg_packet_size
)
177 desc
->avg_packet_size
=desc
->max_packet_size
;
182 if(filter(*data
,"a=StartTime:integer;",&buf
)) {
183 desc
->start_time
=atoi(buf
);
188 if(filter(*data
,"a=Preroll:integer;",&buf
)) {
189 desc
->preroll
=atoi(buf
);
194 if(filter(*data
,"a=length:npt=",&buf
)) {
195 desc
->duration
=(uint32_t)(atof(buf
)*1000);
200 if(filter(*data
,"a=StreamName:string;",&buf
)) {
201 desc
->stream_name
=strdup(buf
);
202 desc
->stream_name_size
=strlen(desc
->stream_name
);
207 if(filter(*data
,"a=mimetype:string;",&buf
)) {
208 desc
->mime_type
=strdup(buf
);
209 desc
->mime_type_size
=strlen(desc
->mime_type
);
215 if(filter(*data
,"a=OpaqueData:buffer;",&buf
)) {
216 decoded
= b64_decode(buf
, decoded
, &(desc
->mlti_data_size
));
217 desc
->mlti_data
=malloc(desc
->mlti_data_size
);
218 memcpy(desc
->mlti_data
, decoded
, desc
->mlti_data_size
);
222 printf("mlti_data_size: %i\n", desc
->mlti_data_size
);
226 if(filter(*data
,"a=ASMRuleBook:string;",&buf
)) {
227 desc
->asm_rule_book
=strdup(buf
);
234 int len
=strchr(*data
,'\n')-(*data
);
235 buf
= xbuffer_copyin(buf
, 0, *data
, len
+1);
237 printf("libreal: sdpplin_stream: not handled: '%s'\n", buf
);
244 mp_msg(MSGT_OPEN
, MSGL_V
, "libreal: sdpplin_stream: no mimetype\n");
245 desc
->mime_type
= strdup("audio/x-pn-realaudio");
246 desc
->mime_type_size
= strlen(desc
->mime_type
);
248 mp_msg(MSGT_OPEN
, MSGL_WARN
, "libreal: sdpplin_stream: implicit mimetype for stream_id != 0, weird.\n");
252 xbuffer_free(decoded
);
257 sdpplin_t
*sdpplin_parse(char *data
) {
259 sdpplin_t
*desc
=calloc(1,sizeof(sdpplin_t
));
260 char *buf
=xbuffer_init(32);
261 char *decoded
=xbuffer_init(32);
265 while (data
&& *data
) {
268 int len
=strchr(data
,'\n')-(data
);
269 buf
= xbuffer_copyin(buf
, 0, data
, len
+1);
271 printf("libreal: sdpplin: '%s'\n", buf
);
277 if (filter(data
, "m=", &buf
)) {
278 sdpplin_stream_t
*stream
=sdpplin_parse_stream(&data
);
280 printf("got data for stream id %u\n", stream
->stream_id
);
282 if (desc
->stream
&& (stream
->stream_id
>= 0) && (stream
->stream_id
< desc
->stream_count
))
283 desc
->stream
[stream
->stream_id
]=stream
;
284 else if (desc
->stream
)
286 mp_msg(MSGT_OPEN
, MSGL_ERR
, "sdpplin: bad stream_id %d (must be >= 0, < %d). Broken sdp?\n",
287 stream
->stream_id
, desc
->stream_count
);
290 mp_msg(MSGT_OPEN
, MSGL_V
, "sdpplin: got 'm=', but 'a=StreamCount' is still unknown.\n");
291 if (stream
->stream_id
== 0) {
292 desc
->stream_count
=1;
293 desc
->stream
=malloc(sizeof(sdpplin_stream_t
*));
294 desc
->stream
[0]=stream
;
296 mp_msg(MSGT_OPEN
, MSGL_ERR
, "sdpplin: got 'm=', but 'a=StreamCount' is still unknown and stream_id != 0. Broken sdp?\n");
303 if(filter(data
,"a=Title:buffer;",&buf
)) {
304 decoded
=b64_decode(buf
, decoded
, &len
);
305 desc
->title
=strdup(decoded
);
310 if(filter(data
,"a=Author:buffer;",&buf
)) {
311 decoded
=b64_decode(buf
, decoded
, &len
);
312 desc
->author
=strdup(decoded
);
317 if(filter(data
,"a=Copyright:buffer;",&buf
)) {
318 decoded
=b64_decode(buf
, decoded
, &len
);
319 desc
->copyright
=strdup(decoded
);
324 if(filter(data
,"a=Abstract:buffer;",&buf
)) {
325 decoded
=b64_decode(buf
, decoded
, &len
);
326 desc
->abstract
=strdup(decoded
);
331 if(filter(data
,"a=StreamCount:integer;",&buf
)) {
332 desc
->stream_count
=(unsigned int)atoi(buf
);
333 desc
->stream
=calloc(desc
->stream_count
, sizeof(sdpplin_stream_t
*));
334 if (!desc
->stream
) desc
->stream_count
= 0;
339 if(filter(data
,"a=Flags:integer;",&buf
)) {
340 desc
->flags
=atoi(buf
);
347 int len
=strchr(data
,'\n')-data
;
348 buf
= xbuffer_copyin(buf
, 0, data
, len
+1);
350 printf("libreal: sdpplin: not handled: '%s'\n", buf
);
357 xbuffer_free(decoded
);
362 void sdpplin_free(sdpplin_t
*description
) {
369 for (i
= 0; i
< description
->stream_count
; i
++) {
370 if (description
->stream
[i
]) {
371 if (description
->stream
[i
]->stream_name
)
372 free(description
->stream
[i
]->stream_name
);
373 if (description
->stream
[i
]->mime_type
)
374 free(description
->stream
[i
]->mime_type
);
375 if (description
->stream
[i
]->mlti_data
)
376 free(description
->stream
[i
]->mlti_data
);
377 if (description
->stream
[i
]->asm_rule_book
)
378 free(description
->stream
[i
]->asm_rule_book
);
379 free(description
->stream
[i
]->id
);
380 free(description
->stream
[i
]);
384 if(description
->stream_count
)
385 free(description
->stream
);
386 if (description
->title
)
387 free(description
->title
);
388 if (description
->author
)
389 free(description
->author
);
390 if (description
->copyright
)
391 free(description
->copyright
);
392 if (description
->abstract
)
393 free(description
->abstract
);