added rtp port range support
[libav.git] / libav / rtsp.c
CommitLineData
1617ad97
FB
1/*
2 * RTSP client
3 * Copyright (c) 2002 Fabrice Bellard.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19#include "avformat.h"
20
21#include <sys/time.h>
22#include <netinet/in.h>
23#include <sys/socket.h>
24#include <arpa/inet.h>
25
26//#define DEBUG
27
28typedef struct RTSPState {
29 URLContext *rtsp_hd; /* RTSP TCP connexion handle */
30 ByteIOContext rtsp_gb;
31 int seq; /* RTSP command sequence number */
32 char session_id[512];
33 enum RTSPProtocol protocol;
34 char last_reply[2048]; /* XXX: allocate ? */
35} RTSPState;
36
37typedef struct RTSPStream {
38 AVFormatContext *ic;
39 int interleaved_min, interleaved_max; /* interleave ids, if TCP transport */
40 char control_url[1024]; /* url for this stream */
41} RTSPStream;
42
43/* suppress this hack */
44int rtsp_abort_req = 0;
45
46/* XXX: currently, the only way to change the protocols consists in
47 changing this variable */
48int rtsp_default_protocols = (1 << RTSP_PROTOCOL_RTP_TCP) | (1 << RTSP_PROTOCOL_RTP_UDP) | (1 << RTSP_PROTOCOL_RTP_UDP_MULTICAST);
49
85fb7b34
FB
50/* if non zero, then set a range for RTP ports */
51int rtsp_rtp_port_min = 0;
52int rtsp_rtp_port_max = 0;
53
1617ad97
FB
54FFRTSPCallback *ff_rtsp_callback = NULL;
55
56static int rtsp_probe(AVProbeData *p)
57{
58 if (strstart(p->filename, "rtsp:", NULL))
59 return AVPROBE_SCORE_MAX;
60 return 0;
61}
62
63static int redir_isspace(int c)
64{
65 return (c == ' ' || c == '\t' || c == '\n' || c == '\r');
66}
67
68static void skip_spaces(const char **pp)
69{
70 const char *p;
71 p = *pp;
72 while (redir_isspace(*p))
73 p++;
74 *pp = p;
75}
76
77static void get_word_sep(char *buf, int buf_size, const char *sep,
78 const char **pp)
79{
80 const char *p;
81 char *q;
82
83 p = *pp;
84 skip_spaces(&p);
85 q = buf;
86 while (!strchr(sep, *p) && *p != '\0') {
87 if ((q - buf) < buf_size - 1)
88 *q++ = *p;
89 p++;
90 }
91 if (buf_size > 0)
92 *q = '\0';
93 *pp = p;
94}
95
96static void get_word(char *buf, int buf_size, const char **pp)
97{
98 const char *p;
99 char *q;
100
101 p = *pp;
102 skip_spaces(&p);
103 q = buf;
104 while (!redir_isspace(*p) && *p != '\0') {
105 if ((q - buf) < buf_size - 1)
106 *q++ = *p;
107 p++;
108 }
109 if (buf_size > 0)
110 *q = '\0';
111 *pp = p;
112}
113
114static void sdp_parse_line(AVFormatContext *s,
115 int letter, const char *buf)
116{
117 char buf1[64], st_type[64];
118 const char *p;
119 int codec_type, payload_type;
120 AVStream *st;
121 RTSPStream *rtsp_st;
122
123#ifdef DEBUG
124 printf("sdp: %c='%s'\n", letter, buf);
125#endif
126
127 p = buf;
128 switch(letter) {
129 case 's':
130 pstrcpy(s->title, sizeof(s->title), p);
131 break;
132 case 'i':
133 if (s->nb_streams == 0) {
134 pstrcpy(s->comment, sizeof(s->comment), p);
135 break;
136 }
137 break;
138 case 'm':
139 /* new stream */
140 get_word(st_type, sizeof(st_type), &p);
141 if (!strcmp(st_type, "audio")) {
142 codec_type = CODEC_TYPE_AUDIO;
143 } else if (!strcmp(st_type, "video")) {
144 codec_type = CODEC_TYPE_VIDEO;
145 } else {
146 return;
147 }
148 get_word(buf1, sizeof(buf1), &p); /* port */
149 get_word(buf1, sizeof(buf1), &p); /* protocol */
150 /* XXX: handle list of formats */
151 get_word(buf1, sizeof(buf1), &p); /* format list */
152 payload_type = atoi(buf1);
153
154 rtsp_st = av_mallocz(sizeof(RTSPStream));
155 if (!rtsp_st)
156 return;
157 st = av_new_stream(s, s->nb_streams);
158 if (!st)
159 return;
160 st->priv_data = rtsp_st;
161 /* put a default control url */
162 pstrcpy(rtsp_st->control_url, sizeof(rtsp_st->control_url), s->filename);
163 st->codec.codec_type = codec_type;
164 rtp_get_codec_info(&st->codec, payload_type);
165 break;
166 case 'a':
167 if (strstart(p, "control:", &p) && s->nb_streams > 0) {
168 char proto[32];
169 /* get the control url */
170 st = s->streams[s->nb_streams - 1];
171 rtsp_st = st->priv_data;
172
173 /* XXX: may need to add full url resolution */
174 url_split(proto, sizeof(proto), NULL, 0, NULL, NULL, 0, p);
175 if (proto[0] == '\0') {
176 /* relative control URL */
177 pstrcat(rtsp_st->control_url, sizeof(rtsp_st->control_url), "/");
178 pstrcat(rtsp_st->control_url, sizeof(rtsp_st->control_url), p);
179 } else {
180 pstrcpy(rtsp_st->control_url, sizeof(rtsp_st->control_url), p);
181 }
182 }
183 break;
184 }
185}
186
187int sdp_parse(AVFormatContext *s, const char *content)
188{
189 const char *p;
190 int letter;
191 char buf[1024], *q;
192
193 p = content;
194 for(;;) {
195 skip_spaces(&p);
196 letter = *p;
197 if (letter == '\0')
198 break;
199 p++;
200 if (*p != '=')
201 goto next_line;
202 p++;
203 /* get the content */
204 q = buf;
205 while (*p != '\n' && *p != '\0') {
206 if ((q - buf) < sizeof(buf) - 1)
207 *q++ = *p;
208 p++;
209 }
210 *q = '\0';
211 sdp_parse_line(s, letter, buf);
212 next_line:
213 while (*p != '\n' && *p != '\0')
214 p++;
215 if (*p == '\n')
216 p++;
217 }
218 return 0;
219}
220
221static void rtsp_parse_range(int *min_ptr, int *max_ptr, const char **pp)
222{
223 const char *p;
224 int v;
225
226 p = *pp;
227 skip_spaces(&p);
228 v = strtol(p, (char **)&p, 10);
229 if (*p == '-') {
230 p++;
231 *min_ptr = v;
232 v = strtol(p, (char **)&p, 10);
233 *max_ptr = v;
234 } else {
235 *min_ptr = v;
236 *max_ptr = v;
237 }
238 *pp = p;
239}
240
241/* XXX: only one transport specification is parsed */
242static void rtsp_parse_transport(RTSPHeader *reply, const char *p)
243{
244 char transport_protocol[16];
245 char profile[16];
246 char lower_transport[16];
247 char parameter[16];
248 RTSPTransportField *th;
249 char buf[256];
250
251 reply->nb_transports = 0;
252
253 for(;;) {
254 skip_spaces(&p);
255 if (*p == '\0')
256 break;
257
258 th = &reply->transports[reply->nb_transports];
259
260 get_word_sep(transport_protocol, sizeof(transport_protocol),
261 "/", &p);
262 if (*p == '/')
263 p++;
264 get_word_sep(profile, sizeof(profile), "/;,", &p);
265 lower_transport[0] = '\0';
266 if (*p == '/') {
267 get_word_sep(lower_transport, sizeof(lower_transport),
268 ";,", &p);
269 }
270 if (!strcmp(lower_transport, "TCP"))
271 th->protocol = RTSP_PROTOCOL_RTP_TCP;
272 else
273 th->protocol = RTSP_PROTOCOL_RTP_UDP;
274
275 if (*p == ';')
276 p++;
277 /* get each parameter */
278 while (*p != '\0' && *p != ',') {
279 get_word_sep(parameter, sizeof(parameter), "=;,", &p);
280 if (!strcmp(parameter, "port")) {
281 if (*p == '=') {
282 p++;
283 rtsp_parse_range(&th->port_min, &th->port_max, &p);
284 }
285 } else if (!strcmp(parameter, "client_port")) {
286 if (*p == '=') {
287 p++;
288 rtsp_parse_range(&th->client_port_min,
289 &th->client_port_max, &p);
290 }
291 } else if (!strcmp(parameter, "server_port")) {
292 if (*p == '=') {
293 p++;
294 rtsp_parse_range(&th->server_port_min,
295 &th->server_port_max, &p);
296 }
297 } else if (!strcmp(parameter, "interleaved")) {
298 if (*p == '=') {
299 p++;
300 rtsp_parse_range(&th->interleaved_min,
301 &th->interleaved_max, &p);
302 }
303 } else if (!strcmp(parameter, "multicast")) {
304 if (th->protocol == RTSP_PROTOCOL_RTP_UDP)
305 th->protocol = RTSP_PROTOCOL_RTP_UDP_MULTICAST;
306 } else if (!strcmp(parameter, "ttl")) {
307 if (*p == '=') {
308 p++;
309 th->ttl = strtol(p, (char **)&p, 10);
310 }
311 } else if (!strcmp(parameter, "destination")) {
312 struct in_addr ipaddr;
313
314 if (*p == '=') {
315 p++;
316 get_word_sep(buf, sizeof(buf), ";,", &p);
317 if (inet_aton(buf, &ipaddr))
318 th->destination = ntohl(ipaddr.s_addr);
319 }
320 }
321 while (*p != ';' && *p != '\0' && *p != ',')
322 p++;
323 if (*p == ';')
324 p++;
325 }
326 if (*p == ',')
327 p++;
328
329 reply->nb_transports++;
330 }
331}
332
333void rtsp_parse_line(RTSPHeader *reply, const char *buf)
334{
335 const char *p;
336
337 /* NOTE: we do case independent match for broken servers */
338 p = buf;
339 if (stristart(p, "Session:", &p)) {
340 get_word_sep(reply->session_id, sizeof(reply->session_id), ";", &p);
341 } else if (stristart(p, "Content-Length:", &p)) {
342 reply->content_length = strtol(p, NULL, 10);
343 } else if (stristart(p, "Transport:", &p)) {
344 rtsp_parse_transport(reply, p);
345 } else if (stristart(p, "CSeq:", &p)) {
346 reply->seq = strtol(p, NULL, 10);
347 }
348}
349
350
351static void rtsp_send_cmd(AVFormatContext *s,
352 const char *cmd, RTSPHeader *reply,
353 unsigned char **content_ptr)
354{
355 RTSPState *rt = s->priv_data;
356 char buf[4096], buf1[1024], *q;
357 unsigned char ch;
358 const char *p;
359 int content_length, line_count;
360 unsigned char *content = NULL;
361
362 memset(reply, 0, sizeof(RTSPHeader));
363
364 rt->seq++;
365 pstrcpy(buf, sizeof(buf), cmd);
366 snprintf(buf1, sizeof(buf1), "CSeq: %d\n", rt->seq);
367 pstrcat(buf, sizeof(buf), buf1);
368 if (rt->session_id[0] != '\0' && !strstr(cmd, "\nIf-Match:")) {
369 snprintf(buf1, sizeof(buf1), "Session: %s\n", rt->session_id);
370 pstrcat(buf, sizeof(buf), buf1);
371 }
372 pstrcat(buf, sizeof(buf), "\n");
373#ifdef DEBUG
374 printf("Sending:\n%s--\n", buf);
375#endif
376 url_write(rt->rtsp_hd, buf, strlen(buf));
377
378 /* parse reply (XXX: use buffers) */
379 line_count = 0;
380 rt->last_reply[0] = '\0';
381 for(;;) {
382 q = buf;
383 for(;;) {
384 if (url_read(rt->rtsp_hd, &ch, 1) == 0)
385 break;
386 if (ch == '\n')
387 break;
388 if (ch != '\r') {
389 if ((q - buf) < sizeof(buf) - 1)
390 *q++ = ch;
391 }
392 }
393 *q = '\0';
394#ifdef DEBUG
395 printf("line='%s'\n", buf);
396#endif
397 /* test if last line */
398 if (buf[0] == '\0')
399 break;
400 p = buf;
401 if (line_count == 0) {
402 /* get reply code */
403 get_word(buf1, sizeof(buf1), &p);
404 get_word(buf1, sizeof(buf1), &p);
405 reply->status_code = atoi(buf1);
406 } else {
407 rtsp_parse_line(reply, p);
408 pstrcat(rt->last_reply, sizeof(rt->last_reply), p);
409 pstrcat(rt->last_reply, sizeof(rt->last_reply), "\n");
410 }
411 line_count++;
412 }
413
414 if (rt->session_id[0] == '\0' && reply->session_id[0] != '\0')
415 pstrcpy(rt->session_id, sizeof(rt->session_id), reply->session_id);
416
417 content_length = reply->content_length;
418 if (content_length > 0) {
419 /* leave some room for a trailing '\0' (useful for simple parsing) */
420 content = av_malloc(content_length + 1);
421 url_read(rt->rtsp_hd, content, content_length);
422 content[content_length] = '\0';
423 }
424 if (content_ptr)
425 *content_ptr = content;
426}
427
428/* useful for modules: set RTSP callback function */
429
430void rtsp_set_callback(FFRTSPCallback *rtsp_cb)
431{
432 ff_rtsp_callback = rtsp_cb;
433}
434
435
436static int rtsp_read_header(AVFormatContext *s,
437 AVFormatParameters *ap)
438{
439 RTSPState *rt = s->priv_data;
440 char host[1024], path[1024], tcpname[1024], cmd[2048];
441 URLContext *rtsp_hd;
442 int port, i, ret, err;
443 RTSPHeader reply1, *reply = &reply1;
444 unsigned char *content = NULL;
445 AVStream *st;
446 RTSPStream *rtsp_st;
447 int protocol_mask;
448
449 rtsp_abort_req = 0;
450
451 /* extract hostname and port */
452 url_split(NULL, 0,
453 host, sizeof(host), &port, path, sizeof(path), s->filename);
454 if (port < 0)
455 port = RTSP_DEFAULT_PORT;
456
457 /* open the tcp connexion */
458 snprintf(tcpname, sizeof(tcpname), "tcp://%s:%d", host, port);
459 if (url_open(&rtsp_hd, tcpname, URL_RDWR) < 0)
460 return AVERROR_IO;
461 rt->rtsp_hd = rtsp_hd;
462 rt->seq = 0;
463
464 /* describe the stream */
465 snprintf(cmd, sizeof(cmd),
466 "DESCRIBE %s RTSP/1.0\n"
467 "Accept: application/sdp\n",
468 s->filename);
469 rtsp_send_cmd(s, cmd, reply, &content);
470 if (!content) {
471 err = AVERROR_INVALIDDATA;
472 goto fail;
473 }
474 if (reply->status_code != RTSP_STATUS_OK) {
475 err = AVERROR_INVALIDDATA;
476 goto fail;
477 }
478
479 /* now we got the SDP description, we parse it */
480 ret = sdp_parse(s, (const char *)content);
481 av_freep(&content);
482 if (ret < 0) {
483 err = AVERROR_INVALIDDATA;
484 goto fail;
485 }
486
487 protocol_mask = rtsp_default_protocols;
488
489 /* for each stream, make the setup request */
490 /* XXX: we assume the same server is used for the control of each
491 RTSP stream */
492 for(i=0;i<s->nb_streams;i++) {
1617ad97 493 char transport[2048];
85fb7b34 494 AVInputFormat *fmt;
1617ad97
FB
495
496 st = s->streams[i];
497 rtsp_st = st->priv_data;
498
499 /* compute available transports */
500 transport[0] = '\0';
501
502 /* RTP/UDP */
503 if (protocol_mask & (1 << RTSP_PROTOCOL_RTP_UDP)) {
85fb7b34
FB
504 char buf[256];
505 int j;
506
507 /* first try in specified port range */
508 if (rtsp_rtp_port_min != 0) {
509 for(j=rtsp_rtp_port_min;j<=rtsp_rtp_port_max;j++) {
510 snprintf(buf, sizeof(buf), "rtp://?localport=%d", j);
511 if (!av_open_input_file(&rtsp_st->ic, buf,
512 &rtp_demux, 0, NULL))
513 goto rtp_opened;
514 }
1617ad97 515 }
85fb7b34
FB
516
517 /* then try on any port */
518 if (av_open_input_file(&rtsp_st->ic, "rtp://",
519 &rtp_demux, 0, NULL) < 0) {
520 err = AVERROR_INVALIDDATA;
521 goto fail;
522 }
523
524 rtp_opened:
1617ad97
FB
525 port = rtp_get_local_port(url_fileno(&rtsp_st->ic->pb));
526 if (transport[0] != '\0')
527 pstrcat(transport, sizeof(transport), ",");
528 snprintf(transport + strlen(transport), sizeof(transport) - strlen(transport) - 1,
529 "RTP/AVP/UDP;unicast;client_port=%d-%d",
530 port, port + 1);
531 }
532
533 /* RTP/TCP */
534 if (protocol_mask & (1 << RTSP_PROTOCOL_RTP_TCP)) {
535 if (transport[0] != '\0')
536 pstrcat(transport, sizeof(transport), ",");
537 snprintf(transport + strlen(transport), sizeof(transport) - strlen(transport) - 1,
538 "RTP/AVP/TCP");
539 }
540
541 if (protocol_mask & (1 << RTSP_PROTOCOL_RTP_UDP_MULTICAST)) {
542 if (transport[0] != '\0')
543 pstrcat(transport, sizeof(transport), ",");
544 snprintf(transport + strlen(transport),
545 sizeof(transport) - strlen(transport) - 1,
546 "RTP/AVP/UDP;multicast");
547 }
548
549 snprintf(cmd, sizeof(cmd),
550 "SETUP %s RTSP/1.0\n"
551 "Transport: %s\n",
552 rtsp_st->control_url, transport);
553 rtsp_send_cmd(s, cmd, reply, NULL);
554 if (reply->status_code != RTSP_STATUS_OK ||
555 reply->nb_transports != 1) {
556 err = AVERROR_INVALIDDATA;
557 goto fail;
558 }
559
560 /* XXX: same protocol for all streams is required */
561 if (i > 0) {
562 if (reply->transports[0].protocol != rt->protocol) {
563 err = AVERROR_INVALIDDATA;
564 goto fail;
565 }
566 } else {
567 rt->protocol = reply->transports[0].protocol;
568 }
569
570 /* close RTP connection if not choosen */
571 if (reply->transports[0].protocol != RTSP_PROTOCOL_RTP_UDP &&
572 (protocol_mask & (1 << RTSP_PROTOCOL_RTP_UDP))) {
573 av_close_input_file(rtsp_st->ic);
574 rtsp_st->ic = NULL;
575 }
576
577 switch(reply->transports[0].protocol) {
578 case RTSP_PROTOCOL_RTP_TCP:
579 fmt = &rtp_demux;
580 if (av_open_input_file(&rtsp_st->ic, "null", fmt, 0, NULL) < 0) {
581 err = AVERROR_INVALIDDATA;
582 goto fail;
583 }
584 rtsp_st->interleaved_min = reply->transports[0].interleaved_min;
585 rtsp_st->interleaved_max = reply->transports[0].interleaved_max;
586 break;
587
588 case RTSP_PROTOCOL_RTP_UDP:
589 {
590 char url[1024];
591
592 /* XXX: also use address if specified */
593 snprintf(url, sizeof(url), "rtp://%s:%d",
594 host, reply->transports[0].server_port_min);
595 if (rtp_set_remote_url(url_fileno(&rtsp_st->ic->pb), url) < 0) {
596 err = AVERROR_INVALIDDATA;
597 goto fail;
598 }
599 }
600 break;
601 case RTSP_PROTOCOL_RTP_UDP_MULTICAST:
602 {
603 char url[1024];
604 int ttl;
605
606 fmt = &rtp_demux;
607 ttl = reply->transports[0].ttl;
608 if (!ttl)
609 ttl = 16;
610 snprintf(url, sizeof(url), "rtp://%s:%d?multicast=1&ttl=%d",
611 host,
612 reply->transports[0].server_port_min,
613 ttl);
614 if (av_open_input_file(&rtsp_st->ic, url, fmt, 0, NULL) < 0) {
615 err = AVERROR_INVALIDDATA;
616 goto fail;
617 }
618 }
619 break;
620 }
621 }
622
623 /* use callback if available to extend setup */
624 if (ff_rtsp_callback) {
625 if (ff_rtsp_callback(RTSP_ACTION_CLIENT_SETUP, rt->session_id,
626 NULL, 0, rt->last_reply) < 0) {
627 err = AVERROR_INVALIDDATA;
628 goto fail;
629 }
630 }
631
632 /* start playing */
633 snprintf(cmd, sizeof(cmd),
634 "PLAY %s RTSP/1.0\n",
635 s->filename);
636 rtsp_send_cmd(s, cmd, reply, NULL);
637 if (reply->status_code != RTSP_STATUS_OK) {
638 err = AVERROR_INVALIDDATA;
639 goto fail;
640 }
641
642 /* open TCP with bufferized input */
643 if (rt->protocol == RTSP_PROTOCOL_RTP_TCP) {
644 if (url_fdopen(&rt->rtsp_gb, rt->rtsp_hd) < 0) {
645 err = AVERROR_NOMEM;
646 goto fail;
647 }
648 }
649
650 return 0;
651 fail:
652 for(i=0;i<s->nb_streams;i++) {
653 st = s->streams[i];
654 rtsp_st = st->priv_data;
655 if (rtsp_st) {
656 if (rtsp_st->ic)
657 av_close_input_file(rtsp_st->ic);
658 }
659 av_free(rtsp_st);
660 }
661 av_freep(&content);
662 url_close(rt->rtsp_hd);
663 return err;
664}
665
666static int tcp_read_packet(AVFormatContext *s,
667 AVPacket *pkt)
668{
669 RTSPState *rt = s->priv_data;
670 ByteIOContext *rtsp_gb = &rt->rtsp_gb;
671 int c, id, len, i, ret;
672 AVStream *st;
673 RTSPStream *rtsp_st;
674 char buf[RTP_MAX_PACKET_LENGTH];
675
676 redo:
677 for(;;) {
678 c = url_fgetc(rtsp_gb);
679 if (c == URL_EOF)
680 return AVERROR_IO;
681 if (c == '$')
682 break;
683 }
684 id = get_byte(rtsp_gb);
685 len = get_be16(rtsp_gb);
686 if (len > RTP_MAX_PACKET_LENGTH || len < 12)
687 goto redo;
688 /* get the data */
689 get_buffer(rtsp_gb, buf, len);
690
691 /* find the matching stream */
692 for(i = 0; i < s->nb_streams; i++) {
693 st = s->streams[i];
694 rtsp_st = st->priv_data;
695 if (i >= rtsp_st->interleaved_min &&
696 i <= rtsp_st->interleaved_max)
697 goto found;
698 }
699 goto redo;
700 found:
701 ret = rtp_parse_packet(rtsp_st->ic, pkt, buf, len);
702 if (ret < 0)
703 goto redo;
704 pkt->stream_index = i;
705 return ret;
706}
707
708/* NOTE: output one packet at a time. May need to add a small fifo */
709static int udp_read_packet(AVFormatContext *s,
710 AVPacket *pkt)
711{
712 AVFormatContext *ic;
713 AVStream *st;
714 RTSPStream *rtsp_st;
715 fd_set rfds;
716 int fd1, fd2, fd_max, n, i, ret;
717 char buf[RTP_MAX_PACKET_LENGTH];
718 struct timeval tv;
719
720 for(;;) {
721 if (rtsp_abort_req)
722 return -EIO;
723 FD_ZERO(&rfds);
724 fd_max = -1;
725 for(i = 0; i < s->nb_streams; i++) {
726 st = s->streams[i];
727 rtsp_st = st->priv_data;
728 ic = rtsp_st->ic;
729 /* currently, we cannot probe RTCP handle because of blocking restrictions */
730 rtp_get_file_handles(url_fileno(&ic->pb), &fd1, &fd2);
731 if (fd1 > fd_max)
732 fd_max = fd1;
733 FD_SET(fd1, &rfds);
734 }
735 /* XXX: also add proper API to abort */
736 tv.tv_sec = 0;
737 tv.tv_usec = 500000;
738 n = select(fd_max + 1, &rfds, NULL, NULL, &tv);
739 if (n > 0) {
740 for(i = 0; i < s->nb_streams; i++) {
741 st = s->streams[i];
742 rtsp_st = st->priv_data;
743 ic = rtsp_st->ic;
744 rtp_get_file_handles(url_fileno(&ic->pb), &fd1, &fd2);
745 if (FD_ISSET(fd1, &rfds)) {
746 ret = url_read(url_fileno(&ic->pb), buf, sizeof(buf));
747 if (ret >= 0 &&
748 rtp_parse_packet(ic, pkt, buf, ret) == 0) {
749 pkt->stream_index = i;
750 return ret;
751 }
752 }
753 }
754 }
755 }
756}
757
758static int rtsp_read_packet(AVFormatContext *s,
759 AVPacket *pkt)
760{
761 RTSPState *rt = s->priv_data;
762 int ret;
763
764 switch(rt->protocol) {
765 default:
766 case RTSP_PROTOCOL_RTP_TCP:
767 ret = tcp_read_packet(s, pkt);
768 break;
769 case RTSP_PROTOCOL_RTP_UDP:
770 ret = udp_read_packet(s, pkt);
771 break;
772 }
773 return ret;
774}
775
776static int rtsp_read_close(AVFormatContext *s)
777{
778 RTSPState *rt = s->priv_data;
779 AVStream *st;
780 RTSPStream *rtsp_st;
781 RTSPHeader reply1, *reply = &reply1;
782 int i;
783 char cmd[1024];
784
785 /* NOTE: it is valid to flush the buffer here */
786 if (rt->protocol == RTSP_PROTOCOL_RTP_TCP) {
787 url_fclose(&rt->rtsp_gb);
788 }
789
790 snprintf(cmd, sizeof(cmd),
791 "TEARDOWN %s RTSP/1.0\n",
792 s->filename);
793 rtsp_send_cmd(s, cmd, reply, NULL);
794
795 if (ff_rtsp_callback) {
796 ff_rtsp_callback(RTSP_ACTION_CLIENT_TEARDOWN, rt->session_id,
797 NULL, 0, NULL);
798 }
799
800 for(i=0;i<s->nb_streams;i++) {
801 st = s->streams[i];
802 rtsp_st = st->priv_data;
803 if (rtsp_st) {
804 if (rtsp_st->ic)
805 av_close_input_file(rtsp_st->ic);
806 }
807 av_free(rtsp_st);
808 }
809 url_close(rt->rtsp_hd);
810 return 0;
811}
812
813static AVInputFormat rtsp_demux = {
814 "rtsp",
815 "RTSP input format",
816 sizeof(RTSPState),
817 rtsp_probe,
818 rtsp_read_header,
819 rtsp_read_packet,
820 rtsp_read_close,
821 flags: AVFMT_NOFILE,
822};
823
824/* dummy redirector format (used directly in av_open_input_file now) */
825static int redir_probe(AVProbeData *pd)
826{
827 const char *p;
828 p = pd->buf;
829 while (redir_isspace(*p))
830 p++;
831 if (strstart(p, "http://", NULL) ||
832 strstart(p, "rtsp://", NULL))
833 return AVPROBE_SCORE_MAX;
834 return 0;
835}
836
837/* called from utils.c */
838int redir_open(AVFormatContext **ic_ptr, ByteIOContext *f)
839{
840 char buf[4096], *q;
841 int c;
842 AVFormatContext *ic = NULL;
843
844 /* parse each URL and try to open it */
845 c = url_fgetc(f);
846 while (c != URL_EOF) {
847 /* skip spaces */
848 for(;;) {
849 if (!redir_isspace(c))
850 break;
851 c = url_fgetc(f);
852 }
853 if (c == URL_EOF)
854 break;
855 /* record url */
856 q = buf;
857 for(;;) {
858 if (c == URL_EOF || redir_isspace(c))
859 break;
860 if ((q - buf) < sizeof(buf) - 1)
861 *q++ = c;
862 c = url_fgetc(f);
863 }
864 *q = '\0';
865 //printf("URL='%s'\n", buf);
866 /* try to open the media file */
867 if (av_open_input_file(&ic, buf, NULL, 0, NULL) == 0)
868 break;
869 }
870 *ic_ptr = ic;
871 if (!ic)
872 return AVERROR_IO;
873 else
874 return 0;
875}
876
877AVInputFormat redir_demux = {
878 "redir",
879 "Redirector format",
880 0,
881 redir_probe,
882 NULL,
883 NULL,
884 NULL,
885};
886
887int rtsp_init(void)
888{
889 av_register_input_format(&rtsp_demux);
890 av_register_input_format(&redir_demux);
891 return 0;
892}