Mercurial > mplayer.hg
annotate libmpdemux/realrtsp/rtsp.c @ 11877:9b2035d703da
add support for aac in real media files
patch by Moritz Bunkus <moritz@bunkus.org>
author | attila |
---|---|
date | Thu, 29 Jan 2004 12:11:13 +0000 |
parents | d700a93ab34c |
children | 4adb4a3b52a2 |
rev | line source |
---|---|
9922 | 1 /* |
2 * This file was ported to MPlayer from xine CVS rtsp.c,v 1.9 2003/04/10 02:30:48 | |
3 */ | |
4 | |
5 /* | |
6 * Copyright (C) 2000-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 * a minimalistic implementation of rtsp protocol, | |
26 * *not* RFC 2326 compilant yet. | |
27 */ | |
28 | |
29 #include <unistd.h> | |
30 #include <stdio.h> | |
31 #include <assert.h> | |
10281 | 32 #include "config.h" |
33 #ifndef HAVE_WINSOCK2 | |
34 #define closesocket close | |
9922 | 35 #include <sys/socket.h> |
36 #include <netinet/in.h> | |
37 #include <netdb.h> | |
10281 | 38 #else |
39 #include <winsock2.h> | |
40 #endif | |
9922 | 41 #include <string.h> |
42 #include <sys/stat.h> | |
43 #include <fcntl.h> | |
44 #include <errno.h> | |
45 #include <stdlib.h> | |
46 #include <time.h> | |
47 #include <sys/time.h> | |
48 #include <sys/types.h> | |
9939
6b88f67564ce
Fix compilation for *BSD, Mac OS X and maybe others (info by Steven M. Schultz and Dan Christiansen)
rtognimp
parents:
9922
diff
changeset
|
49 #include <inttypes.h> |
9922 | 50 |
51 #include "rtsp.h" | |
52 | |
53 /* | |
54 #define LOG | |
55 */ | |
56 | |
57 #define BUF_SIZE 4096 | |
58 #define HEADER_SIZE 1024 | |
59 #define MAX_FIELDS 256 | |
60 | |
61 struct rtsp_s { | |
62 | |
63 int s; | |
64 | |
65 char *host; | |
66 int port; | |
67 char *path; | |
11506
fff1b6f1a9cc
Real rtsp Range parameter (Start and End) support.
rtognimp
parents:
11000
diff
changeset
|
68 char *param; |
9922 | 69 char *mrl; |
70 char *user_agent; | |
71 | |
72 char *server; | |
73 unsigned int server_state; | |
74 uint32_t server_caps; | |
75 | |
76 char buffer[BUF_SIZE]; /* scratch buffer */ | |
77 | |
78 unsigned int cseq; | |
79 char *session; | |
80 | |
81 char *answers[MAX_FIELDS]; /* data of last message */ | |
82 char *scheduled[MAX_FIELDS]; /* will be sent with next message */ | |
83 }; | |
84 | |
85 /* | |
86 * constants | |
87 */ | |
88 | |
89 const char rtsp_protocol_version[]="RTSP/1.0"; | |
90 | |
91 /* server states */ | |
92 #define RTSP_CONNECTED 1 | |
93 #define RTSP_INIT 2 | |
94 #define RTSP_READY 4 | |
95 #define RTSP_PLAYING 8 | |
96 #define RTSP_RECORDING 16 | |
97 | |
98 /* server capabilities */ | |
99 #define RTSP_OPTIONS 0x001 | |
100 #define RTSP_DESCRIBE 0x002 | |
101 #define RTSP_ANNOUNCE 0x004 | |
102 #define RTSP_SETUP 0x008 | |
103 #define RTSP_GET_PARAMETER 0x010 | |
104 #define RTSP_SET_PARAMETER 0x020 | |
105 #define RTSP_TEARDOWN 0x040 | |
106 #define RTSP_PLAY 0x080 | |
107 #define RTSP_RECORD 0x100 | |
108 | |
109 /* | |
110 * network utilities | |
111 */ | |
112 | |
113 static int host_connect_attempt(struct in_addr ia, int port) { | |
114 | |
115 int s; | |
116 struct sockaddr_in sin; | |
117 | |
118 s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); | |
119 if (s == -1) { | |
120 printf ("rtsp: socket(): %s\n", strerror(errno)); | |
121 return -1; | |
122 } | |
123 | |
124 sin.sin_family = AF_INET; | |
125 sin.sin_addr = ia; | |
126 sin.sin_port = htons(port); | |
127 | |
128 if (connect(s, (struct sockaddr *)&sin, sizeof(sin))==-1 | |
10281 | 129 #ifndef HAVE_WINSOCK2 |
9922 | 130 && errno != EINPROGRESS) { |
10281 | 131 #else |
132 && WSAGetLastError() == WSAEINPROGRESS) { | |
133 #endif | |
9922 | 134 printf ("rtsp: connect(): %s\n", strerror(errno)); |
10281 | 135 closesocket(s); |
9922 | 136 return -1; |
137 } | |
138 | |
139 return s; | |
140 } | |
141 | |
142 static int host_connect(const char *host, int port) { | |
143 | |
144 struct hostent *h; | |
145 int i, s; | |
146 | |
147 h = gethostbyname(host); | |
148 if (h == NULL) { | |
149 printf ("rtsp: unable to resolve '%s'.\n", host); | |
150 return -1; | |
151 } | |
152 | |
153 for (i = 0; h->h_addr_list[i]; i++) { | |
154 struct in_addr ia; | |
155 | |
156 memcpy (&ia, h->h_addr_list[i], 4); | |
157 s = host_connect_attempt(ia, port); | |
158 if(s != -1) | |
159 return s; | |
160 } | |
161 printf ("rtsp: unable to connect to '%s'.\n", host); | |
162 return -1; | |
163 } | |
164 | |
165 static int write_stream(int s, const char *buf, int len) { | |
166 int total, timeout; | |
167 | |
168 total = 0; timeout = 30; | |
169 while (total < len){ | |
170 int n; | |
171 | |
10206
35e306346e59
Using recv/send instead read/write for proper MinGW support (it's a 4.2BSD standard). Patch by FloDt <flodt8@yahoo.de>
alex
parents:
9939
diff
changeset
|
172 n = send (s, &buf[total], len - total, 0); |
9922 | 173 |
174 if (n > 0) | |
175 total += n; | |
176 else if (n < 0) { | |
10281 | 177 #ifndef HAVE_WINSOCK2 |
9922 | 178 if ((timeout>0) && ((errno == EAGAIN) || (errno == EINPROGRESS))) { |
10281 | 179 #else |
180 if ((timeout>0) && ((errno == EAGAIN) || (WSAGetLastError() == WSAEINPROGRESS))) { | |
181 #endif | |
9922 | 182 sleep (1); timeout--; |
183 } else | |
184 return -1; | |
185 } | |
186 } | |
187 | |
188 return total; | |
189 } | |
190 | |
191 static ssize_t read_stream(int fd, void *buf, size_t count) { | |
192 | |
193 ssize_t ret, total; | |
194 | |
195 total = 0; | |
196 | |
197 while (total < count) { | |
198 | |
10206
35e306346e59
Using recv/send instead read/write for proper MinGW support (it's a 4.2BSD standard). Patch by FloDt <flodt8@yahoo.de>
alex
parents:
9939
diff
changeset
|
199 ret=recv (fd, ((uint8_t*)buf)+total, count-total, 0); |
9922 | 200 |
201 if (ret<0) { | |
202 if(errno == EAGAIN) { | |
203 fd_set rset; | |
204 struct timeval timeout; | |
205 | |
206 FD_ZERO (&rset); | |
207 FD_SET (fd, &rset); | |
208 | |
209 timeout.tv_sec = 30; | |
210 timeout.tv_usec = 0; | |
211 | |
212 if (select (fd+1, &rset, NULL, NULL, &timeout) <= 0) { | |
213 return -1; | |
214 } | |
215 continue; | |
216 } | |
217 | |
218 printf ("rtsp: read error.\n"); | |
219 return ret; | |
220 } else | |
221 total += ret; | |
222 | |
223 /* end of stream */ | |
224 if (!ret) break; | |
225 } | |
226 | |
227 return total; | |
228 } | |
229 | |
230 /* | |
231 * debugging utilities | |
232 */ | |
233 #if 0 | |
234 static void hexdump (char *buf, int length) { | |
235 | |
236 int i; | |
237 | |
238 printf ("rtsp: ascii>"); | |
239 for (i = 0; i < length; i++) { | |
240 unsigned char c = buf[i]; | |
241 | |
242 if ((c >= 32) && (c <= 128)) | |
243 printf ("%c", c); | |
244 else | |
245 printf ("."); | |
246 } | |
247 printf ("\n"); | |
248 | |
249 printf ("rtsp: hexdump> "); | |
250 for (i = 0; i < length; i++) { | |
251 unsigned char c = buf[i]; | |
252 | |
253 printf ("%02x", c); | |
254 | |
255 if ((i % 16) == 15) | |
256 printf ("\nrtsp: "); | |
257 | |
258 if ((i % 2) == 1) | |
259 printf (" "); | |
260 | |
261 } | |
262 printf ("\n"); | |
263 } | |
264 #endif | |
265 | |
266 /* | |
267 * rtsp_get gets a line from stream | |
268 * and returns a null terminated string. | |
269 */ | |
270 | |
271 static char *rtsp_get(rtsp_t *s) { | |
272 | |
273 int n=0; | |
274 char *string; | |
275 | |
276 while (n<BUF_SIZE) { | |
277 read_stream(s->s, &s->buffer[n], 1); | |
278 if ((s->buffer[n-1]==0x0d)&&(s->buffer[n]==0x0a)) break; | |
279 n++; | |
280 } | |
281 | |
282 if (n>=BUF_SIZE) { | |
283 printf("librtsp: buffer overflow in rtsp_get\n"); | |
284 exit(1); | |
285 } | |
286 string=malloc(sizeof(char)*n); | |
287 memcpy(string,s->buffer,n-1); | |
288 string[n-1]=0; | |
289 | |
290 #ifdef LOG | |
291 printf("librtsp: << '%s'\n", string); | |
292 #endif | |
293 | |
294 | |
295 return string; | |
296 } | |
297 | |
298 /* | |
299 * rtsp_put puts a line on stream | |
300 */ | |
301 | |
302 static void rtsp_put(rtsp_t *s, const char *string) { | |
303 | |
304 int len=strlen(string); | |
305 char *buf=malloc(sizeof(char)*len+2); | |
306 | |
307 #ifdef LOG | |
308 printf("librtsp: >> '%s'", string); | |
309 #endif | |
310 | |
311 memcpy(buf,string,len); | |
312 buf[len]=0x0d; | |
313 buf[len+1]=0x0a; | |
314 | |
315 write_stream(s->s, buf, len+2); | |
316 | |
317 #ifdef LOG | |
318 printf(" done.\n"); | |
319 #endif | |
320 | |
321 free(buf); | |
322 } | |
323 | |
324 /* | |
325 * extract server status code | |
326 */ | |
327 | |
328 static int rtsp_get_code(const char *string) { | |
329 | |
330 char buf[4]; | |
331 int code=0; | |
332 | |
333 if (!strncmp(string, rtsp_protocol_version, strlen(rtsp_protocol_version))) | |
334 { | |
335 memcpy(buf, string+strlen(rtsp_protocol_version)+1, 3); | |
336 buf[3]=0; | |
337 code=atoi(buf); | |
338 } else if (!strncmp(string, "SET_PARAMETER",8)) | |
339 { | |
340 return RTSP_STATUS_SET_PARAMETER; | |
341 } | |
342 | |
343 if(code != 200) printf("librtsp: server responds: '%s'\n",string); | |
344 | |
345 return code; | |
346 } | |
347 | |
348 /* | |
349 * send a request | |
350 */ | |
351 | |
352 static void rtsp_send_request(rtsp_t *s, const char *type, const char *what) { | |
353 | |
354 char **payload=s->scheduled; | |
355 sprintf(s->buffer,"%s %s %s",type, what, rtsp_protocol_version); | |
356 rtsp_put(s,s->buffer); | |
357 if (payload) | |
358 while (*payload) { | |
359 rtsp_put(s,*payload); | |
360 payload++; | |
361 } | |
362 rtsp_put(s,""); | |
363 rtsp_unschedule_all(s); | |
364 } | |
365 | |
366 /* | |
367 * schedule standard fields | |
368 */ | |
369 | |
370 static void rtsp_schedule_standard(rtsp_t *s) { | |
371 | |
372 sprintf(s->buffer, "Cseq: %u", s->cseq); | |
373 rtsp_schedule_field(s, s->buffer); | |
374 if (s->session) { | |
375 sprintf(s->buffer, "Session: %s", s->session); | |
376 rtsp_schedule_field(s, s->buffer); | |
377 } | |
378 } | |
379 /* | |
380 * get the answers, if server responses with something != 200, return NULL | |
381 */ | |
382 | |
383 static int rtsp_get_answers(rtsp_t *s) { | |
384 | |
385 char *answer=NULL; | |
386 unsigned int answer_seq; | |
387 char **answer_ptr=s->answers; | |
388 int code; | |
389 | |
390 answer=rtsp_get(s); | |
391 code=rtsp_get_code(answer); | |
392 free(answer); | |
393 | |
394 rtsp_free_answers(s); | |
395 | |
396 do { /* while we get answer lines */ | |
397 | |
398 answer=rtsp_get(s); | |
399 | |
400 if (!strncmp(answer,"Cseq:",5)) { | |
401 sscanf(answer,"Cseq: %u",&answer_seq); | |
402 if (s->cseq != answer_seq) { | |
403 #ifdef LOG | |
404 printf("librtsp: warning: Cseq mismatch. got %u, assumed %u", answer_seq, s->cseq); | |
405 #endif | |
406 s->cseq=answer_seq; | |
407 } | |
408 } | |
409 if (!strncmp(answer,"Server:",7)) { | |
410 sscanf(answer,"Server: %s",s->buffer); | |
411 if (s->server) free(s->server); | |
412 s->server=strdup(s->buffer); | |
413 } | |
414 if (!strncmp(answer,"Session:",8)) { | |
415 memset(s->buffer,0, BUF_SIZE); | |
416 sscanf(answer,"Session: %s",s->buffer); | |
417 if (s->session) { | |
418 if (strcmp(s->buffer, s->session)) { | |
419 printf("rtsp: warning: setting NEW session: %s\n", s->buffer); | |
420 free(s->session); | |
421 s->session=strdup(s->buffer); | |
422 } | |
423 } else | |
424 { | |
425 #ifdef LOG | |
426 printf("rtsp: setting session id to: %s\n", s->buffer); | |
427 #endif | |
428 s->session=strdup(s->buffer); | |
429 } | |
430 } | |
431 *answer_ptr=answer; | |
432 answer_ptr++; | |
433 } while (strlen(answer)!=0); | |
434 | |
435 s->cseq++; | |
436 | |
437 *answer_ptr=NULL; | |
438 rtsp_schedule_standard(s); | |
439 | |
440 return code; | |
441 } | |
442 | |
443 /* | |
444 * send an ok message | |
445 */ | |
446 | |
447 int rtsp_send_ok(rtsp_t *s) { | |
448 char cseq[16]; | |
449 | |
450 rtsp_put(s, "RTSP/1.0 200 OK"); | |
451 sprintf(cseq,"CSeq: %u", s->cseq); | |
452 rtsp_put(s, cseq); | |
453 rtsp_put(s, ""); | |
454 return 0; | |
455 } | |
456 | |
457 /* | |
458 * implementation of must-have rtsp requests; functions return | |
459 * server status code. | |
460 */ | |
461 | |
462 int rtsp_request_options(rtsp_t *s, const char *what) { | |
463 | |
464 char *buf; | |
465 | |
466 if (what) { | |
467 buf=strdup(what); | |
468 } else | |
469 { | |
470 buf=malloc(sizeof(char)*(strlen(s->host)+16)); | |
471 sprintf(buf,"rtsp://%s:%i", s->host, s->port); | |
472 } | |
473 rtsp_send_request(s,"OPTIONS",buf); | |
474 free(buf); | |
475 | |
476 return rtsp_get_answers(s); | |
477 } | |
478 | |
479 int rtsp_request_describe(rtsp_t *s, const char *what) { | |
480 | |
481 char *buf; | |
482 | |
483 if (what) { | |
484 buf=strdup(what); | |
485 } else | |
486 { | |
487 buf=malloc(sizeof(char)*(strlen(s->host)+strlen(s->path)+16)); | |
488 sprintf(buf,"rtsp://%s:%i/%s", s->host, s->port, s->path); | |
489 } | |
490 rtsp_send_request(s,"DESCRIBE",buf); | |
491 free(buf); | |
492 | |
493 return rtsp_get_answers(s); | |
494 } | |
495 | |
496 int rtsp_request_setup(rtsp_t *s, const char *what) { | |
497 | |
498 rtsp_send_request(s,"SETUP",what); | |
499 | |
500 return rtsp_get_answers(s); | |
501 } | |
502 | |
503 int rtsp_request_setparameter(rtsp_t *s, const char *what) { | |
504 | |
505 char *buf; | |
506 | |
507 if (what) { | |
508 buf=strdup(what); | |
509 } else | |
510 { | |
511 buf=malloc(sizeof(char)*(strlen(s->host)+strlen(s->path)+16)); | |
512 sprintf(buf,"rtsp://%s:%i/%s", s->host, s->port, s->path); | |
513 } | |
514 rtsp_send_request(s,"SET_PARAMETER",buf); | |
515 free(buf); | |
516 | |
517 return rtsp_get_answers(s); | |
518 } | |
519 | |
520 int rtsp_request_play(rtsp_t *s, const char *what) { | |
521 | |
522 char *buf; | |
523 | |
524 if (what) { | |
525 buf=strdup(what); | |
526 } else | |
527 { | |
528 buf=malloc(sizeof(char)*(strlen(s->host)+strlen(s->path)+16)); | |
529 sprintf(buf,"rtsp://%s:%i/%s", s->host, s->port, s->path); | |
530 } | |
531 rtsp_send_request(s,"PLAY",buf); | |
532 free(buf); | |
533 | |
534 return rtsp_get_answers(s); | |
535 } | |
536 | |
537 int rtsp_request_tearoff(rtsp_t *s, const char *what) { | |
538 | |
539 rtsp_send_request(s,"TEAROFF",what); | |
540 | |
541 return rtsp_get_answers(s); | |
542 } | |
543 | |
544 /* | |
545 * read opaque data from stream | |
546 */ | |
547 | |
548 int rtsp_read_data(rtsp_t *s, char *buffer, unsigned int size) { | |
549 | |
550 int i,seq; | |
551 | |
552 if (size>=4) { | |
553 i=read_stream(s->s, buffer, 4); | |
554 if (i<4) return i; | |
555 if ((buffer[0]=='S')&&(buffer[1]=='E')&&(buffer[2]=='T')&&(buffer[3]=='_')) | |
556 { | |
557 char *rest=rtsp_get(s); | |
558 /* a real server wanna play table tennis? */ | |
559 memcpy(s->buffer, buffer, 4); | |
560 strcpy(s->buffer+4, rest); | |
561 seq=-1; | |
562 do { | |
563 free(rest); | |
564 rest=rtsp_get(s); | |
565 if (!strncmp(rest,"Cseq:",5)) | |
566 sscanf(rest,"Cseq: %u",&seq); | |
567 } while (strlen(rest)!=0); | |
568 free(rest); | |
569 if (seq<0) { | |
570 #ifdef LOG | |
571 printf("rtsp: warning: cseq not recognized!\n"); | |
572 #endif | |
573 seq=1; | |
574 } | |
11000 | 575 /* let's make the server happy */ |
9922 | 576 rtsp_put(s, "RTSP/1.0 451 Parameter Not Understood"); |
577 rest=malloc(sizeof(char)*16); | |
578 sprintf(rest,"CSeq: %u", seq); | |
579 rtsp_put(s, rest); | |
580 rtsp_put(s, ""); | |
581 i=read_stream(s->s, buffer, size); | |
582 } else | |
583 { | |
584 i=read_stream(s->s, buffer+4, size-4); | |
585 i+=4; | |
586 } | |
587 } else | |
588 i=read_stream(s->s, buffer, size); | |
589 #ifdef LOG | |
590 printf("librtsp: << %d of %d bytes\n", i, size); | |
591 #endif | |
592 | |
593 return i; | |
594 } | |
595 | |
596 /* | |
597 * connect to a rtsp server | |
598 */ | |
599 | |
600 //rtsp_t *rtsp_connect(const char *mrl, const char *user_agent) { | |
601 rtsp_t *rtsp_connect(int fd, char* mrl, char *path, char *host, int port, char *user_agent) { | |
602 | |
603 rtsp_t *s=malloc(sizeof(rtsp_t)); | |
604 int i; | |
605 | |
606 for (i=0; i<MAX_FIELDS; i++) { | |
607 s->answers[i]=NULL; | |
608 s->scheduled[i]=NULL; | |
609 } | |
610 | |
611 s->server=NULL; | |
612 s->server_state=0; | |
613 s->server_caps=0; | |
614 | |
615 s->cseq=0; | |
616 s->session=NULL; | |
617 | |
618 if (user_agent) | |
619 s->user_agent=strdup(user_agent); | |
620 else | |
621 s->user_agent=strdup("User-Agent: RealMedia Player Version 6.0.9.1235 (linux-2.0-libc6-i386-gcc2.95)"); | |
622 | |
623 s->mrl = strdup(mrl); | |
624 s->host = strdup(host); | |
625 s->port = port; | |
11663
d700a93ab34c
10l let path behave like before the start/stop patch
rtognimp
parents:
11506
diff
changeset
|
626 s->path = strdup(path); |
11506
fff1b6f1a9cc
Real rtsp Range parameter (Start and End) support.
rtognimp
parents:
11000
diff
changeset
|
627 while (*path == '/') |
fff1b6f1a9cc
Real rtsp Range parameter (Start and End) support.
rtognimp
parents:
11000
diff
changeset
|
628 path++; |
fff1b6f1a9cc
Real rtsp Range parameter (Start and End) support.
rtognimp
parents:
11000
diff
changeset
|
629 if ((s->param = strchr(s->path, '?')) != NULL) |
fff1b6f1a9cc
Real rtsp Range parameter (Start and End) support.
rtognimp
parents:
11000
diff
changeset
|
630 s->param++; |
fff1b6f1a9cc
Real rtsp Range parameter (Start and End) support.
rtognimp
parents:
11000
diff
changeset
|
631 //printf("path=%s\n", s->path); |
fff1b6f1a9cc
Real rtsp Range parameter (Start and End) support.
rtognimp
parents:
11000
diff
changeset
|
632 //printf("param=%s\n", s->param ? s->param : "NULL"); |
9922 | 633 s->s = fd; |
634 | |
635 if (s->s < 0) { | |
636 printf ("rtsp: failed to connect to '%s'\n", s->host); | |
637 rtsp_close(s); | |
638 return NULL; | |
639 } | |
640 | |
641 s->server_state=RTSP_CONNECTED; | |
642 | |
11000 | 643 /* now let's send an options request. */ |
9922 | 644 rtsp_schedule_field(s, "CSeq: 1"); |
645 rtsp_schedule_field(s, s->user_agent); | |
646 rtsp_schedule_field(s, "ClientChallenge: 9e26d33f2984236010ef6253fb1887f7"); | |
647 rtsp_schedule_field(s, "PlayerStarttime: [28/03/2003:22:50:23 00:00]"); | |
648 rtsp_schedule_field(s, "CompanyID: KnKV4M4I/B2FjJ1TToLycw=="); | |
649 rtsp_schedule_field(s, "GUID: 00000000-0000-0000-0000-000000000000"); | |
650 rtsp_schedule_field(s, "RegionData: 0"); | |
651 rtsp_schedule_field(s, "ClientID: Linux_2.4_6.0.9.1235_play32_RN01_EN_586"); | |
652 /*rtsp_schedule_field(s, "Pragma: initiate-session");*/ | |
653 rtsp_request_options(s, NULL); | |
654 | |
655 return s; | |
656 } | |
657 | |
658 | |
659 /* | |
660 * closes an rtsp connection | |
661 */ | |
662 | |
663 void rtsp_close(rtsp_t *s) { | |
664 | |
10281 | 665 if (s->server_state) closesocket(s->s); /* TODO: send a TEAROFF */ |
9922 | 666 if (s->path) free(s->path); |
667 if (s->host) free(s->host); | |
668 if (s->mrl) free(s->mrl); | |
669 if (s->session) free(s->session); | |
670 if (s->user_agent) free(s->user_agent); | |
671 rtsp_free_answers(s); | |
672 rtsp_unschedule_all(s); | |
673 free(s); | |
674 } | |
675 | |
676 /* | |
677 * search in answers for tags. returns a pointer to the content | |
678 * after the first matched tag. returns NULL if no match found. | |
679 */ | |
680 | |
681 char *rtsp_search_answers(rtsp_t *s, const char *tag) { | |
682 | |
683 char **answer; | |
684 char *ptr; | |
685 | |
686 if (!s->answers) return NULL; | |
687 answer=s->answers; | |
688 | |
689 while (*answer) { | |
690 if (!strncasecmp(*answer,tag,strlen(tag))) { | |
691 ptr=strchr(*answer,':'); | |
692 ptr++; | |
693 while(*ptr==' ') ptr++; | |
694 return ptr; | |
695 } | |
696 answer++; | |
697 } | |
698 | |
699 return NULL; | |
700 } | |
701 | |
702 /* | |
703 * session id management | |
704 */ | |
705 | |
706 void rtsp_set_session(rtsp_t *s, const char *id) { | |
707 | |
708 if (s->session) free(s->session); | |
709 | |
710 s->session=strdup(id); | |
711 | |
712 } | |
713 | |
714 char *rtsp_get_session(rtsp_t *s) { | |
715 | |
716 return s->session; | |
717 | |
718 } | |
719 | |
720 char *rtsp_get_mrl(rtsp_t *s) { | |
721 | |
722 return s->mrl; | |
723 | |
724 } | |
725 | |
11506
fff1b6f1a9cc
Real rtsp Range parameter (Start and End) support.
rtognimp
parents:
11000
diff
changeset
|
726 char *rtsp_get_param(rtsp_t *s, char *p) { |
fff1b6f1a9cc
Real rtsp Range parameter (Start and End) support.
rtognimp
parents:
11000
diff
changeset
|
727 int len; |
fff1b6f1a9cc
Real rtsp Range parameter (Start and End) support.
rtognimp
parents:
11000
diff
changeset
|
728 char *param; |
fff1b6f1a9cc
Real rtsp Range parameter (Start and End) support.
rtognimp
parents:
11000
diff
changeset
|
729 if (!s->param) |
fff1b6f1a9cc
Real rtsp Range parameter (Start and End) support.
rtognimp
parents:
11000
diff
changeset
|
730 return NULL; |
fff1b6f1a9cc
Real rtsp Range parameter (Start and End) support.
rtognimp
parents:
11000
diff
changeset
|
731 if (!p) |
fff1b6f1a9cc
Real rtsp Range parameter (Start and End) support.
rtognimp
parents:
11000
diff
changeset
|
732 return strdup(s->param); |
fff1b6f1a9cc
Real rtsp Range parameter (Start and End) support.
rtognimp
parents:
11000
diff
changeset
|
733 len = strlen(p); |
fff1b6f1a9cc
Real rtsp Range parameter (Start and End) support.
rtognimp
parents:
11000
diff
changeset
|
734 param = s->param; |
fff1b6f1a9cc
Real rtsp Range parameter (Start and End) support.
rtognimp
parents:
11000
diff
changeset
|
735 while (param && *param) { |
fff1b6f1a9cc
Real rtsp Range parameter (Start and End) support.
rtognimp
parents:
11000
diff
changeset
|
736 char *nparam = strchr(param, '&'); |
fff1b6f1a9cc
Real rtsp Range parameter (Start and End) support.
rtognimp
parents:
11000
diff
changeset
|
737 if (strncmp(param, p, len) == 0 && param[len] == '=') { |
fff1b6f1a9cc
Real rtsp Range parameter (Start and End) support.
rtognimp
parents:
11000
diff
changeset
|
738 param += len + 1; |
fff1b6f1a9cc
Real rtsp Range parameter (Start and End) support.
rtognimp
parents:
11000
diff
changeset
|
739 len = nparam ? nparam - param : strlen(param); |
fff1b6f1a9cc
Real rtsp Range parameter (Start and End) support.
rtognimp
parents:
11000
diff
changeset
|
740 nparam = malloc(len + 1); |
fff1b6f1a9cc
Real rtsp Range parameter (Start and End) support.
rtognimp
parents:
11000
diff
changeset
|
741 memcpy(nparam, param, len); |
fff1b6f1a9cc
Real rtsp Range parameter (Start and End) support.
rtognimp
parents:
11000
diff
changeset
|
742 nparam[len] = 0; |
fff1b6f1a9cc
Real rtsp Range parameter (Start and End) support.
rtognimp
parents:
11000
diff
changeset
|
743 return nparam; |
fff1b6f1a9cc
Real rtsp Range parameter (Start and End) support.
rtognimp
parents:
11000
diff
changeset
|
744 } |
fff1b6f1a9cc
Real rtsp Range parameter (Start and End) support.
rtognimp
parents:
11000
diff
changeset
|
745 param = nparam ? nparam + 1 : NULL; |
fff1b6f1a9cc
Real rtsp Range parameter (Start and End) support.
rtognimp
parents:
11000
diff
changeset
|
746 } |
fff1b6f1a9cc
Real rtsp Range parameter (Start and End) support.
rtognimp
parents:
11000
diff
changeset
|
747 return NULL; |
fff1b6f1a9cc
Real rtsp Range parameter (Start and End) support.
rtognimp
parents:
11000
diff
changeset
|
748 } |
fff1b6f1a9cc
Real rtsp Range parameter (Start and End) support.
rtognimp
parents:
11000
diff
changeset
|
749 |
9922 | 750 /* |
751 * schedules a field for transmission | |
752 */ | |
753 | |
754 void rtsp_schedule_field(rtsp_t *s, const char *string) { | |
755 | |
756 int i=0; | |
757 | |
758 if (!string) return; | |
759 | |
760 while(s->scheduled[i]) { | |
761 i++; | |
762 } | |
763 s->scheduled[i]=strdup(string); | |
764 } | |
765 | |
766 /* | |
767 * removes the first scheduled field which prefix matches string. | |
768 */ | |
769 | |
770 void rtsp_unschedule_field(rtsp_t *s, const char *string) { | |
771 | |
772 char **ptr=s->scheduled; | |
773 | |
774 if (!string) return; | |
775 | |
776 while(*ptr) { | |
777 if (!strncmp(*ptr, string, strlen(string))) | |
778 break; | |
779 } | |
780 if (*ptr) free(*ptr); | |
781 ptr++; | |
782 do { | |
783 *(ptr-1)=*ptr; | |
784 } while(*ptr); | |
785 } | |
786 | |
787 /* | |
788 * unschedule all fields | |
789 */ | |
790 | |
791 void rtsp_unschedule_all(rtsp_t *s) { | |
792 | |
793 char **ptr; | |
794 | |
795 if (!s->scheduled) return; | |
796 ptr=s->scheduled; | |
797 | |
798 while (*ptr) { | |
799 free(*ptr); | |
800 *ptr=NULL; | |
801 ptr++; | |
802 } | |
803 } | |
804 /* | |
805 * free answers | |
806 */ | |
807 | |
808 void rtsp_free_answers(rtsp_t *s) { | |
809 | |
810 char **answer; | |
811 | |
812 if (!s->answers) return; | |
813 answer=s->answers; | |
814 | |
815 while (*answer) { | |
816 free(*answer); | |
817 *answer=NULL; | |
818 answer++; | |
819 } | |
820 } |