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