3 * Copyright (c) 2002 Fabrice Bellard.
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.
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.
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
21 #include <unistd.h> /* for select() prototype */
23 #include <netinet/in.h>
24 #include <sys/socket.h>
27 # include <arpa/inet.h>
29 # include "barpainet.h"
33 //#define DEBUG_RTP_TCP
35 typedef struct RTSPState {
36 URLContext *rtsp_hd; /* RTSP TCP connexion handle */
37 /* XXX: currently we use unbuffered input */
38 // ByteIOContext rtsp_gb;
39 int seq; /* RTSP command sequence number */
41 enum RTSPProtocol protocol;
42 char last_reply[2048]; /* XXX: allocate ? */
45 typedef struct RTSPStream {
47 int interleaved_min, interleaved_max; /* interleave ids, if TCP transport */
48 char control_url[1024]; /* url for this stream (from SDP) */
50 int sdp_port; /* port (from SDP content - not used in RTSP) */
51 struct in_addr sdp_ip; /* IP address (from SDP content - not used in RTSP) */
52 int sdp_ttl; /* IP TTL (from SDP content - not used in RTSP) */
53 int sdp_payload_type; /* payload type - only used in SDP */
56 /* XXX: currently, the only way to change the protocols consists in
57 changing this variable */
59 int rtsp_default_protocols = (1 << RTSP_PROTOCOL_RTP_TCP) | (1 << RTSP_PROTOCOL_RTP_UDP) | (1 << RTSP_PROTOCOL_RTP_UDP_MULTICAST);
61 /* try it if a proxy is used */
62 int rtsp_default_protocols = (1 << RTSP_PROTOCOL_RTP_TCP);
65 /* if non zero, then set a range for RTP ports */
66 int rtsp_rtp_port_min = 0;
67 int rtsp_rtp_port_max = 0;
69 FFRTSPCallback *ff_rtsp_callback = NULL;
71 static int rtsp_probe(AVProbeData *p)
73 if (strstart(p->filename, "rtsp:", NULL))
74 return AVPROBE_SCORE_MAX;
78 static int redir_isspace(int c)
80 return (c == ' ' || c == '\t' || c == '\n' || c == '\r');
83 static void skip_spaces(const char **pp)
87 while (redir_isspace(*p))
92 static void get_word_sep(char *buf, int buf_size, const char *sep,
101 while (!strchr(sep, *p) && *p != '\0') {
102 if ((q - buf) < buf_size - 1)
111 static void get_word(char *buf, int buf_size, const char **pp)
119 while (!redir_isspace(*p) && *p != '\0') {
120 if ((q - buf) < buf_size - 1)
129 /* parse the rtpmap description: <codec_name>/<clock_rate>[/<other
131 static int sdp_parse_rtpmap(AVCodecContext *codec, const char *p)
136 get_word_sep(buf, sizeof(buf), "/", &p);
137 if (!strcmp(buf, "MP4V-ES")) {
138 codec->codec_id = CODEC_ID_MPEG4;
145 /* return the length and optionnaly the data */
146 static int hex_to_data(uint8_t *data, const char *p)
156 c = toupper((unsigned char)*p++);
157 if (c >= '0' && c <= '9')
159 else if (c >= 'A' && c <= 'F')
174 static void sdp_parse_fmtp(AVCodecContext *codec, const char *p)
180 /* loop on each attribute */
185 get_word_sep(attr, sizeof(attr), "=", &p);
188 get_word_sep(value, sizeof(value), ";", &p);
191 /* handle MPEG4 video */
192 switch(codec->codec_id) {
194 if (!strcmp(attr, "config")) {
195 /* decode the hexa encoded parameter */
196 len = hex_to_data(NULL, value);
197 codec->extradata = av_mallocz(len);
198 if (!codec->extradata)
200 codec->extradata_size = len;
201 hex_to_data(codec->extradata, value);
205 /* ignore data for other codecs */
209 // printf("'%s' = '%s'\n", attr, value);
213 typedef struct SDPParseState {
215 struct in_addr default_ip;
219 static void sdp_parse_line(AVFormatContext *s, SDPParseState *s1,
220 int letter, const char *buf)
222 char buf1[64], st_type[64];
224 int codec_type, payload_type, i;
227 struct in_addr sdp_ip;
231 printf("sdp: %c='%s'\n", letter, buf);
237 get_word(buf1, sizeof(buf1), &p);
238 if (strcmp(buf1, "IN") != 0)
240 get_word(buf1, sizeof(buf1), &p);
241 if (strcmp(buf1, "IP4") != 0)
243 get_word_sep(buf1, sizeof(buf1), "/", &p);
244 if (inet_aton(buf1, &sdp_ip) == 0)
249 get_word_sep(buf1, sizeof(buf1), "/", &p);
252 if (s->nb_streams == 0) {
253 s1->default_ip = sdp_ip;
254 s1->default_ttl = ttl;
256 st = s->streams[s->nb_streams - 1];
257 rtsp_st = st->priv_data;
258 rtsp_st->sdp_ip = sdp_ip;
259 rtsp_st->sdp_ttl = ttl;
263 pstrcpy(s->title, sizeof(s->title), p);
266 if (s->nb_streams == 0) {
267 pstrcpy(s->comment, sizeof(s->comment), p);
273 get_word(st_type, sizeof(st_type), &p);
274 if (!strcmp(st_type, "audio")) {
275 codec_type = CODEC_TYPE_AUDIO;
276 } else if (!strcmp(st_type, "video")) {
277 codec_type = CODEC_TYPE_VIDEO;
281 rtsp_st = av_mallocz(sizeof(RTSPStream));
284 st = av_new_stream(s, s->nb_streams);
287 st->priv_data = rtsp_st;
289 rtsp_st->sdp_ip = s1->default_ip;
290 rtsp_st->sdp_ttl = s1->default_ttl;
292 st->codec.codec_type = codec_type;
294 get_word(buf1, sizeof(buf1), &p); /* port */
295 rtsp_st->sdp_port = atoi(buf1);
297 get_word(buf1, sizeof(buf1), &p); /* protocol (ignored) */
299 /* XXX: handle list of formats */
300 get_word(buf1, sizeof(buf1), &p); /* format list */
301 rtsp_st->sdp_payload_type = atoi(buf1);
302 if (rtsp_st->sdp_payload_type < 96) {
303 /* if standard payload type, we can find the codec right now */
304 rtp_get_codec_info(&st->codec, rtsp_st->sdp_payload_type);
307 /* put a default control url */
308 pstrcpy(rtsp_st->control_url, sizeof(rtsp_st->control_url), s->filename);
311 if (strstart(p, "control:", &p) && s->nb_streams > 0) {
313 /* get the control url */
314 st = s->streams[s->nb_streams - 1];
315 rtsp_st = st->priv_data;
317 /* XXX: may need to add full url resolution */
318 url_split(proto, sizeof(proto), NULL, 0, NULL, NULL, 0, p);
319 if (proto[0] == '\0') {
320 /* relative control URL */
321 pstrcat(rtsp_st->control_url, sizeof(rtsp_st->control_url), "/");
322 pstrcat(rtsp_st->control_url, sizeof(rtsp_st->control_url), p);
324 pstrcpy(rtsp_st->control_url, sizeof(rtsp_st->control_url), p);
326 } else if (strstart(p, "rtpmap:", &p)) {
327 /* NOTE: rtpmap is only supported AFTER the 'm=' tag */
328 get_word(buf1, sizeof(buf1), &p);
329 payload_type = atoi(buf1);
330 for(i = 0; i < s->nb_streams;i++) {
332 rtsp_st = st->priv_data;
333 if (rtsp_st->sdp_payload_type == payload_type) {
334 sdp_parse_rtpmap(&st->codec, p);
337 } else if (strstart(p, "fmtp:", &p)) {
338 /* NOTE: fmtp is only supported AFTER the 'a=rtpmap:xxx' tag */
339 get_word(buf1, sizeof(buf1), &p);
340 payload_type = atoi(buf1);
341 for(i = 0; i < s->nb_streams;i++) {
343 rtsp_st = st->priv_data;
344 if (rtsp_st->sdp_payload_type == payload_type) {
345 sdp_parse_fmtp(&st->codec, p);
353 static int sdp_parse(AVFormatContext *s, const char *content)
358 SDPParseState sdp_parse_state, *s1 = &sdp_parse_state;
360 memset(s1, 0, sizeof(SDPParseState));
371 /* get the content */
373 while (*p != '\n' && *p != '\r' && *p != '\0') {
374 if ((q - buf) < sizeof(buf) - 1)
379 sdp_parse_line(s, s1, letter, buf);
381 while (*p != '\n' && *p != '\0')
389 static void rtsp_parse_range(int *min_ptr, int *max_ptr, const char **pp)
396 v = strtol(p, (char **)&p, 10);
400 v = strtol(p, (char **)&p, 10);
409 /* XXX: only one transport specification is parsed */
410 static void rtsp_parse_transport(RTSPHeader *reply, const char *p)
412 char transport_protocol[16];
414 char lower_transport[16];
416 RTSPTransportField *th;
419 reply->nb_transports = 0;
426 th = &reply->transports[reply->nb_transports];
428 get_word_sep(transport_protocol, sizeof(transport_protocol),
432 get_word_sep(profile, sizeof(profile), "/;,", &p);
433 lower_transport[0] = '\0';
436 get_word_sep(lower_transport, sizeof(lower_transport),
439 if (!strcasecmp(lower_transport, "TCP"))
440 th->protocol = RTSP_PROTOCOL_RTP_TCP;
442 th->protocol = RTSP_PROTOCOL_RTP_UDP;
446 /* get each parameter */
447 while (*p != '\0' && *p != ',') {
448 get_word_sep(parameter, sizeof(parameter), "=;,", &p);
449 if (!strcmp(parameter, "port")) {
452 rtsp_parse_range(&th->port_min, &th->port_max, &p);
454 } else if (!strcmp(parameter, "client_port")) {
457 rtsp_parse_range(&th->client_port_min,
458 &th->client_port_max, &p);
460 } else if (!strcmp(parameter, "server_port")) {
463 rtsp_parse_range(&th->server_port_min,
464 &th->server_port_max, &p);
466 } else if (!strcmp(parameter, "interleaved")) {
469 rtsp_parse_range(&th->interleaved_min,
470 &th->interleaved_max, &p);
472 } else if (!strcmp(parameter, "multicast")) {
473 if (th->protocol == RTSP_PROTOCOL_RTP_UDP)
474 th->protocol = RTSP_PROTOCOL_RTP_UDP_MULTICAST;
475 } else if (!strcmp(parameter, "ttl")) {
478 th->ttl = strtol(p, (char **)&p, 10);
480 } else if (!strcmp(parameter, "destination")) {
481 struct in_addr ipaddr;
485 get_word_sep(buf, sizeof(buf), ";,", &p);
486 if (inet_aton(buf, &ipaddr))
487 th->destination = ntohl(ipaddr.s_addr);
490 while (*p != ';' && *p != '\0' && *p != ',')
498 reply->nb_transports++;
502 void rtsp_parse_line(RTSPHeader *reply, const char *buf)
506 /* NOTE: we do case independent match for broken servers */
508 if (stristart(p, "Session:", &p)) {
509 get_word_sep(reply->session_id, sizeof(reply->session_id), ";", &p);
510 } else if (stristart(p, "Content-Length:", &p)) {
511 reply->content_length = strtol(p, NULL, 10);
512 } else if (stristart(p, "Transport:", &p)) {
513 rtsp_parse_transport(reply, p);
514 } else if (stristart(p, "CSeq:", &p)) {
515 reply->seq = strtol(p, NULL, 10);
519 /* skip a RTP/TCP interleaved packet */
520 static void rtsp_skip_packet(AVFormatContext *s)
522 RTSPState *rt = s->priv_data;
526 ret = url_read(rt->rtsp_hd, buf, 3);
529 len = (buf[1] << 8) | buf[2];
531 printf("skipping RTP packet len=%d\n", len);
536 if (len1 > sizeof(buf))
538 ret = url_read(rt->rtsp_hd, buf, len1);
545 static void rtsp_send_cmd(AVFormatContext *s,
546 const char *cmd, RTSPHeader *reply,
547 unsigned char **content_ptr)
549 RTSPState *rt = s->priv_data;
550 char buf[4096], buf1[1024], *q;
553 int content_length, line_count;
554 unsigned char *content = NULL;
556 memset(reply, 0, sizeof(RTSPHeader));
559 pstrcpy(buf, sizeof(buf), cmd);
560 snprintf(buf1, sizeof(buf1), "CSeq: %d\r\n", rt->seq);
561 pstrcat(buf, sizeof(buf), buf1);
562 if (rt->session_id[0] != '\0' && !strstr(cmd, "\nIf-Match:")) {
563 snprintf(buf1, sizeof(buf1), "Session: %s\r\n", rt->session_id);
564 pstrcat(buf, sizeof(buf), buf1);
566 pstrcat(buf, sizeof(buf), "\r\n");
568 printf("Sending:\n%s--\n", buf);
570 url_write(rt->rtsp_hd, buf, strlen(buf));
572 /* parse reply (XXX: use buffers) */
574 rt->last_reply[0] = '\0';
578 if (url_read(rt->rtsp_hd, &ch, 1) != 1)
583 /* XXX: only parse it if first char on line ? */
585 } else if (ch != '\r') {
586 if ((q - buf) < sizeof(buf) - 1)
592 printf("line='%s'\n", buf);
594 /* test if last line */
598 if (line_count == 0) {
600 get_word(buf1, sizeof(buf1), &p);
601 get_word(buf1, sizeof(buf1), &p);
602 reply->status_code = atoi(buf1);
604 rtsp_parse_line(reply, p);
605 pstrcat(rt->last_reply, sizeof(rt->last_reply), p);
606 pstrcat(rt->last_reply, sizeof(rt->last_reply), "\n");
611 if (rt->session_id[0] == '\0' && reply->session_id[0] != '\0')
612 pstrcpy(rt->session_id, sizeof(rt->session_id), reply->session_id);
614 content_length = reply->content_length;
615 if (content_length > 0) {
616 /* leave some room for a trailing '\0' (useful for simple parsing) */
617 content = av_malloc(content_length + 1);
618 url_read(rt->rtsp_hd, content, content_length);
619 content[content_length] = '\0';
622 *content_ptr = content;
625 /* useful for modules: set RTSP callback function */
627 void rtsp_set_callback(FFRTSPCallback *rtsp_cb)
629 ff_rtsp_callback = rtsp_cb;
633 static int rtsp_read_header(AVFormatContext *s,
634 AVFormatParameters *ap)
636 RTSPState *rt = s->priv_data;
637 char host[1024], path[1024], tcpname[1024], cmd[2048];
639 int port, i, ret, err;
640 RTSPHeader reply1, *reply = &reply1;
641 unsigned char *content = NULL;
646 /* extract hostname and port */
648 host, sizeof(host), &port, path, sizeof(path), s->filename);
650 port = RTSP_DEFAULT_PORT;
652 /* open the tcp connexion */
653 snprintf(tcpname, sizeof(tcpname), "tcp://%s:%d", host, port);
654 if (url_open(&rtsp_hd, tcpname, URL_RDWR) < 0)
656 rt->rtsp_hd = rtsp_hd;
659 /* describe the stream */
660 snprintf(cmd, sizeof(cmd),
661 "DESCRIBE %s RTSP/1.0\r\n"
662 "Accept: application/sdp\r\n",
664 rtsp_send_cmd(s, cmd, reply, &content);
666 err = AVERROR_INVALIDDATA;
669 if (reply->status_code != RTSP_STATUS_OK) {
670 err = AVERROR_INVALIDDATA;
674 /* now we got the SDP description, we parse it */
675 ret = sdp_parse(s, (const char *)content);
678 err = AVERROR_INVALIDDATA;
682 protocol_mask = rtsp_default_protocols;
684 /* for each stream, make the setup request */
685 /* XXX: we assume the same server is used for the control of each
687 for(i=0;i<s->nb_streams;i++) {
688 char transport[2048];
692 rtsp_st = st->priv_data;
694 /* compute available transports */
698 if (protocol_mask & (1 << RTSP_PROTOCOL_RTP_UDP)) {
702 /* first try in specified port range */
703 if (rtsp_rtp_port_min != 0) {
704 for(j=rtsp_rtp_port_min;j<=rtsp_rtp_port_max;j++) {
705 snprintf(buf, sizeof(buf), "rtp://?localport=%d", j);
706 if (!av_open_input_file(&rtsp_st->ic, buf,
707 &rtp_demux, 0, NULL))
712 /* then try on any port */
713 if (av_open_input_file(&rtsp_st->ic, "rtp://",
714 &rtp_demux, 0, NULL) < 0) {
715 err = AVERROR_INVALIDDATA;
720 port = rtp_get_local_port(url_fileno(&rtsp_st->ic->pb));
721 if (transport[0] != '\0')
722 pstrcat(transport, sizeof(transport), ",");
723 snprintf(transport + strlen(transport), sizeof(transport) - strlen(transport) - 1,
724 "RTP/AVP/UDP;unicast;client_port=%d-%d",
729 if (protocol_mask & (1 << RTSP_PROTOCOL_RTP_TCP)) {
730 if (transport[0] != '\0')
731 pstrcat(transport, sizeof(transport), ",");
732 snprintf(transport + strlen(transport), sizeof(transport) - strlen(transport) - 1,
736 if (protocol_mask & (1 << RTSP_PROTOCOL_RTP_UDP_MULTICAST)) {
737 if (transport[0] != '\0')
738 pstrcat(transport, sizeof(transport), ",");
739 snprintf(transport + strlen(transport),
740 sizeof(transport) - strlen(transport) - 1,
741 "RTP/AVP/UDP;multicast");
743 snprintf(cmd, sizeof(cmd),
744 "SETUP %s RTSP/1.0\r\n"
746 rtsp_st->control_url, transport);
747 rtsp_send_cmd(s, cmd, reply, NULL);
748 if (reply->status_code != RTSP_STATUS_OK ||
749 reply->nb_transports != 1) {
750 err = AVERROR_INVALIDDATA;
754 /* XXX: same protocol for all streams is required */
756 if (reply->transports[0].protocol != rt->protocol) {
757 err = AVERROR_INVALIDDATA;
761 rt->protocol = reply->transports[0].protocol;
764 /* close RTP connection if not choosen */
765 if (reply->transports[0].protocol != RTSP_PROTOCOL_RTP_UDP &&
766 (protocol_mask & (1 << RTSP_PROTOCOL_RTP_UDP))) {
767 av_close_input_file(rtsp_st->ic);
771 switch(reply->transports[0].protocol) {
772 case RTSP_PROTOCOL_RTP_TCP:
774 if (av_open_input_file(&rtsp_st->ic, "null", fmt, 0, NULL) < 0) {
775 err = AVERROR_INVALIDDATA;
778 rtsp_st->interleaved_min = reply->transports[0].interleaved_min;
779 rtsp_st->interleaved_max = reply->transports[0].interleaved_max;
782 case RTSP_PROTOCOL_RTP_UDP:
786 /* XXX: also use address if specified */
787 snprintf(url, sizeof(url), "rtp://%s:%d",
788 host, reply->transports[0].server_port_min);
789 if (rtp_set_remote_url(url_fileno(&rtsp_st->ic->pb), url) < 0) {
790 err = AVERROR_INVALIDDATA;
795 case RTSP_PROTOCOL_RTP_UDP_MULTICAST:
801 ttl = reply->transports[0].ttl;
804 snprintf(url, sizeof(url), "rtp://%s:%d?multicast=1&ttl=%d",
806 reply->transports[0].server_port_min,
808 if (av_open_input_file(&rtsp_st->ic, url, fmt, 0, NULL) < 0) {
809 err = AVERROR_INVALIDDATA;
817 /* use callback if available to extend setup */
818 if (ff_rtsp_callback) {
819 if (ff_rtsp_callback(RTSP_ACTION_CLIENT_SETUP, rt->session_id,
820 NULL, 0, rt->last_reply) < 0) {
821 err = AVERROR_INVALIDDATA;
827 snprintf(cmd, sizeof(cmd),
828 "PLAY %s RTSP/1.0\r\n"
831 rtsp_send_cmd(s, cmd, reply, NULL);
832 if (reply->status_code != RTSP_STATUS_OK) {
833 err = AVERROR_INVALIDDATA;
838 /* open TCP with bufferized input */
839 if (rt->protocol == RTSP_PROTOCOL_RTP_TCP) {
840 if (url_fdopen(&rt->rtsp_gb, rt->rtsp_hd) < 0) {
849 for(i=0;i<s->nb_streams;i++) {
851 rtsp_st = st->priv_data;
854 av_close_input_file(rtsp_st->ic);
859 url_close(rt->rtsp_hd);
863 static int tcp_read_packet(AVFormatContext *s,
866 RTSPState *rt = s->priv_data;
870 uint8_t buf[RTP_MAX_PACKET_LENGTH];
873 printf("tcp_read_packet:\n");
877 ret = url_read(rt->rtsp_hd, buf, 1);
879 printf("ret=%d c=%02x [%c]\n", ret, buf[0], buf[0]);
886 ret = url_read(rt->rtsp_hd, buf, 3);
890 len = (buf[1] << 8) | buf[2];
892 printf("id=%d len=%d\n", id, len);
894 if (len > RTP_MAX_PACKET_LENGTH || len < 12)
897 ret = url_read(rt->rtsp_hd, buf, len);
901 /* find the matching stream */
902 for(i = 0; i < s->nb_streams; i++) {
904 rtsp_st = st->priv_data;
905 if (id >= rtsp_st->interleaved_min &&
906 id <= rtsp_st->interleaved_max)
911 ret = rtp_parse_packet(rtsp_st->ic, pkt, buf, len);
914 pkt->stream_index = i;
918 /* NOTE: output one packet at a time. May need to add a small fifo */
919 static int udp_read_packet(AVFormatContext *s,
926 int fd1, fd2, fd_max, n, i, ret;
927 char buf[RTP_MAX_PACKET_LENGTH];
931 if (url_interrupt_cb())
935 for(i = 0; i < s->nb_streams; i++) {
937 rtsp_st = st->priv_data;
939 /* currently, we cannot probe RTCP handle because of blocking restrictions */
940 rtp_get_file_handles(url_fileno(&ic->pb), &fd1, &fd2);
945 /* XXX: also add proper API to abort */
947 tv.tv_usec = 100 * 1000;
948 n = select(fd_max + 1, &rfds, NULL, NULL, &tv);
950 for(i = 0; i < s->nb_streams; i++) {
952 rtsp_st = st->priv_data;
954 rtp_get_file_handles(url_fileno(&ic->pb), &fd1, &fd2);
955 if (FD_ISSET(fd1, &rfds)) {
956 ret = url_read(url_fileno(&ic->pb), buf, sizeof(buf));
958 rtp_parse_packet(ic, pkt, buf, ret) == 0) {
959 pkt->stream_index = i;
968 static int rtsp_read_packet(AVFormatContext *s,
971 RTSPState *rt = s->priv_data;
974 switch(rt->protocol) {
976 case RTSP_PROTOCOL_RTP_TCP:
977 ret = tcp_read_packet(s, pkt);
979 case RTSP_PROTOCOL_RTP_UDP:
980 ret = udp_read_packet(s, pkt);
986 /* pause the stream */
987 int rtsp_pause(AVFormatContext *s)
990 RTSPHeader reply1, *reply = &reply1;
993 if (s->iformat != &rtsp_demux)
998 snprintf(cmd, sizeof(cmd),
999 "PAUSE %s RTSP/1.0\r\n",
1001 rtsp_send_cmd(s, cmd, reply, NULL);
1002 if (reply->status_code != RTSP_STATUS_OK) {
1009 /* resume the stream */
1010 int rtsp_resume(AVFormatContext *s)
1013 RTSPHeader reply1, *reply = &reply1;
1016 if (s->iformat != &rtsp_demux)
1021 snprintf(cmd, sizeof(cmd),
1022 "PLAY %s RTSP/1.0\r\n",
1024 rtsp_send_cmd(s, cmd, reply, NULL);
1025 if (reply->status_code != RTSP_STATUS_OK) {
1032 static int rtsp_read_close(AVFormatContext *s)
1034 RTSPState *rt = s->priv_data;
1036 RTSPStream *rtsp_st;
1037 RTSPHeader reply1, *reply = &reply1;
1042 /* NOTE: it is valid to flush the buffer here */
1043 if (rt->protocol == RTSP_PROTOCOL_RTP_TCP) {
1044 url_fclose(&rt->rtsp_gb);
1047 snprintf(cmd, sizeof(cmd),
1048 "TEARDOWN %s RTSP/1.0\r\n",
1050 rtsp_send_cmd(s, cmd, reply, NULL);
1052 if (ff_rtsp_callback) {
1053 ff_rtsp_callback(RTSP_ACTION_CLIENT_TEARDOWN, rt->session_id,
1057 for(i=0;i<s->nb_streams;i++) {
1059 rtsp_st = st->priv_data;
1062 av_close_input_file(rtsp_st->ic);
1066 url_close(rt->rtsp_hd);
1070 AVInputFormat rtsp_demux = {
1072 "RTSP input format",
1078 .flags = AVFMT_NOFILE,
1081 static int sdp_probe(AVProbeData *p1)
1085 /* we look for a line beginning "c=IN IP4" */
1087 while (*p != '\0') {
1088 if (strstart(p, "c=IN IP4", NULL))
1089 return AVPROBE_SCORE_MAX / 2;
1090 p = strchr(p, '\n');
1100 #define SDP_MAX_SIZE 8192
1102 static int sdp_read_header(AVFormatContext *s,
1103 AVFormatParameters *ap)
1106 RTSPStream *rtsp_st;
1111 /* read the whole sdp file */
1112 /* XXX: better loading */
1113 content = av_malloc(SDP_MAX_SIZE);
1114 size = get_buffer(&s->pb, content, SDP_MAX_SIZE - 1);
1117 return AVERROR_INVALIDDATA;
1119 content[size] ='\0';
1121 sdp_parse(s, content);
1124 /* open each RTP stream */
1125 for(i=0;i<s->nb_streams;i++) {
1127 rtsp_st = st->priv_data;
1129 snprintf(url, sizeof(url), "rtp://%s:%d?multicast=1&ttl=%d",
1130 inet_ntoa(rtsp_st->sdp_ip),
1133 if (av_open_input_file(&rtsp_st->ic, url, &rtp_demux, 0, NULL) < 0) {
1134 err = AVERROR_INVALIDDATA;
1140 for(i=0;i<s->nb_streams;i++) {
1142 rtsp_st = st->priv_data;
1145 av_close_input_file(rtsp_st->ic);
1152 static int sdp_read_packet(AVFormatContext *s,
1155 return udp_read_packet(s, pkt);
1158 static int sdp_read_close(AVFormatContext *s)
1161 RTSPStream *rtsp_st;
1164 for(i=0;i<s->nb_streams;i++) {
1166 rtsp_st = st->priv_data;
1169 av_close_input_file(rtsp_st->ic);
1177 static AVInputFormat sdp_demux = {
1188 /* dummy redirector format (used directly in av_open_input_file now) */
1189 static int redir_probe(AVProbeData *pd)
1193 while (redir_isspace(*p))
1195 if (strstart(p, "http://", NULL) ||
1196 strstart(p, "rtsp://", NULL))
1197 return AVPROBE_SCORE_MAX;
1201 /* called from utils.c */
1202 int redir_open(AVFormatContext **ic_ptr, ByteIOContext *f)
1206 AVFormatContext *ic = NULL;
1208 /* parse each URL and try to open it */
1210 while (c != URL_EOF) {
1213 if (!redir_isspace(c))
1222 if (c == URL_EOF || redir_isspace(c))
1224 if ((q - buf) < sizeof(buf) - 1)
1229 //printf("URL='%s'\n", buf);
1230 /* try to open the media file */
1231 if (av_open_input_file(&ic, buf, NULL, 0, NULL) == 0)
1241 AVInputFormat redir_demux = {
1243 "Redirector format",
1253 av_register_input_format(&rtsp_demux);
1254 av_register_input_format(&redir_demux);
1255 av_register_input_format(&sdp_demux);