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>
26 # include <arpa/inet.h>
28 # include "barpainet.h"
32 //#define DEBUG_RTP_TCP
34 typedef struct RTSPState {
35 URLContext *rtsp_hd; /* RTSP TCP connexion handle */
36 /* XXX: currently we use unbuffered input */
37 // ByteIOContext rtsp_gb;
38 int seq; /* RTSP command sequence number */
40 enum RTSPProtocol protocol;
41 char last_reply[2048]; /* XXX: allocate ? */
44 typedef struct RTSPStream {
46 int interleaved_min, interleaved_max; /* interleave ids, if TCP transport */
47 char control_url[1024]; /* url for this stream (from SDP) */
49 int sdp_port; /* port (from SDP content - not used in RTSP) */
50 struct in_addr sdp_ip; /* IP address (from SDP content - not used in RTSP) */
51 int sdp_ttl; /* IP TTL (from SDP content - not used in RTSP) */
52 int sdp_payload_type; /* payload type - only used in SDP */
55 /* XXX: currently, the only way to change the protocols consists in
56 changing this variable */
58 int rtsp_default_protocols = (1 << RTSP_PROTOCOL_RTP_TCP) | (1 << RTSP_PROTOCOL_RTP_UDP) | (1 << RTSP_PROTOCOL_RTP_UDP_MULTICAST);
60 /* try it if a proxy is used */
61 int rtsp_default_protocols = (1 << RTSP_PROTOCOL_RTP_TCP);
64 /* if non zero, then set a range for RTP ports */
65 int rtsp_rtp_port_min = 0;
66 int rtsp_rtp_port_max = 0;
68 FFRTSPCallback *ff_rtsp_callback = NULL;
70 static int rtsp_probe(AVProbeData *p)
72 if (strstart(p->filename, "rtsp:", NULL))
73 return AVPROBE_SCORE_MAX;
77 static int redir_isspace(int c)
79 return (c == ' ' || c == '\t' || c == '\n' || c == '\r');
82 static void skip_spaces(const char **pp)
86 while (redir_isspace(*p))
91 static void get_word_sep(char *buf, int buf_size, const char *sep,
100 while (!strchr(sep, *p) && *p != '\0') {
101 if ((q - buf) < buf_size - 1)
110 static void get_word(char *buf, int buf_size, const char **pp)
118 while (!redir_isspace(*p) && *p != '\0') {
119 if ((q - buf) < buf_size - 1)
128 /* parse the rtpmap description: <codec_name>/<clock_rate>[/<other
130 static int sdp_parse_rtpmap(AVCodecContext *codec, const char *p)
135 get_word_sep(buf, sizeof(buf), "/", &p);
136 if (!strcmp(buf, "MP4V-ES")) {
137 codec->codec_id = CODEC_ID_MPEG4;
144 /* return the length and optionnaly the data */
145 static int hex_to_data(uint8_t *data, const char *p)
155 c = toupper((unsigned char)*p++);
156 if (c >= '0' && c <= '9')
158 else if (c >= 'A' && c <= 'F')
173 static void sdp_parse_fmtp(AVCodecContext *codec, const char *p)
179 /* loop on each attribute */
184 get_word_sep(attr, sizeof(attr), "=", &p);
187 get_word_sep(value, sizeof(value), ";", &p);
190 /* handle MPEG4 video */
191 switch(codec->codec_id) {
193 if (!strcmp(attr, "config")) {
194 /* decode the hexa encoded parameter */
195 len = hex_to_data(NULL, value);
196 codec->extradata = av_mallocz(len);
197 if (!codec->extradata)
199 codec->extradata_size = len;
200 hex_to_data(codec->extradata, value);
204 /* ignore data for other codecs */
208 // printf("'%s' = '%s'\n", attr, value);
212 typedef struct SDPParseState {
214 struct in_addr default_ip;
218 static void sdp_parse_line(AVFormatContext *s, SDPParseState *s1,
219 int letter, const char *buf)
221 char buf1[64], st_type[64];
223 int codec_type, payload_type, i;
226 struct in_addr sdp_ip;
230 printf("sdp: %c='%s'\n", letter, buf);
236 get_word(buf1, sizeof(buf1), &p);
237 if (strcmp(buf1, "IN") != 0)
239 get_word(buf1, sizeof(buf1), &p);
240 if (strcmp(buf1, "IP4") != 0)
242 get_word_sep(buf1, sizeof(buf1), "/", &p);
243 if (inet_aton(buf1, &sdp_ip) == 0)
248 get_word_sep(buf1, sizeof(buf1), "/", &p);
251 if (s->nb_streams == 0) {
252 s1->default_ip = sdp_ip;
253 s1->default_ttl = ttl;
255 st = s->streams[s->nb_streams - 1];
256 rtsp_st = st->priv_data;
257 rtsp_st->sdp_ip = sdp_ip;
258 rtsp_st->sdp_ttl = ttl;
262 pstrcpy(s->title, sizeof(s->title), p);
265 if (s->nb_streams == 0) {
266 pstrcpy(s->comment, sizeof(s->comment), p);
272 get_word(st_type, sizeof(st_type), &p);
273 if (!strcmp(st_type, "audio")) {
274 codec_type = CODEC_TYPE_AUDIO;
275 } else if (!strcmp(st_type, "video")) {
276 codec_type = CODEC_TYPE_VIDEO;
280 rtsp_st = av_mallocz(sizeof(RTSPStream));
283 st = av_new_stream(s, s->nb_streams);
286 st->priv_data = rtsp_st;
288 rtsp_st->sdp_ip = s1->default_ip;
289 rtsp_st->sdp_ttl = s1->default_ttl;
291 st->codec.codec_type = codec_type;
293 get_word(buf1, sizeof(buf1), &p); /* port */
294 rtsp_st->sdp_port = atoi(buf1);
296 get_word(buf1, sizeof(buf1), &p); /* protocol (ignored) */
298 /* XXX: handle list of formats */
299 get_word(buf1, sizeof(buf1), &p); /* format list */
300 rtsp_st->sdp_payload_type = atoi(buf1);
301 if (rtsp_st->sdp_payload_type < 96) {
302 /* if standard payload type, we can find the codec right now */
303 rtp_get_codec_info(&st->codec, rtsp_st->sdp_payload_type);
306 /* put a default control url */
307 pstrcpy(rtsp_st->control_url, sizeof(rtsp_st->control_url), s->filename);
310 if (strstart(p, "control:", &p) && s->nb_streams > 0) {
312 /* get the control url */
313 st = s->streams[s->nb_streams - 1];
314 rtsp_st = st->priv_data;
316 /* XXX: may need to add full url resolution */
317 url_split(proto, sizeof(proto), NULL, 0, NULL, NULL, 0, p);
318 if (proto[0] == '\0') {
319 /* relative control URL */
320 pstrcat(rtsp_st->control_url, sizeof(rtsp_st->control_url), "/");
321 pstrcat(rtsp_st->control_url, sizeof(rtsp_st->control_url), p);
323 pstrcpy(rtsp_st->control_url, sizeof(rtsp_st->control_url), p);
325 } else if (strstart(p, "rtpmap:", &p)) {
326 /* NOTE: rtpmap is only supported AFTER the 'm=' tag */
327 get_word(buf1, sizeof(buf1), &p);
328 payload_type = atoi(buf1);
329 for(i = 0; i < s->nb_streams;i++) {
331 rtsp_st = st->priv_data;
332 if (rtsp_st->sdp_payload_type == payload_type) {
333 sdp_parse_rtpmap(&st->codec, p);
336 } else if (strstart(p, "fmtp:", &p)) {
337 /* NOTE: fmtp is only supported AFTER the 'a=rtpmap:xxx' tag */
338 get_word(buf1, sizeof(buf1), &p);
339 payload_type = atoi(buf1);
340 for(i = 0; i < s->nb_streams;i++) {
342 rtsp_st = st->priv_data;
343 if (rtsp_st->sdp_payload_type == payload_type) {
344 sdp_parse_fmtp(&st->codec, p);
352 static int sdp_parse(AVFormatContext *s, const char *content)
357 SDPParseState sdp_parse_state, *s1 = &sdp_parse_state;
359 memset(s1, 0, sizeof(SDPParseState));
370 /* get the content */
372 while (*p != '\n' && *p != '\r' && *p != '\0') {
373 if ((q - buf) < sizeof(buf) - 1)
378 sdp_parse_line(s, s1, letter, buf);
380 while (*p != '\n' && *p != '\0')
388 static void rtsp_parse_range(int *min_ptr, int *max_ptr, const char **pp)
395 v = strtol(p, (char **)&p, 10);
399 v = strtol(p, (char **)&p, 10);
408 /* XXX: only one transport specification is parsed */
409 static void rtsp_parse_transport(RTSPHeader *reply, const char *p)
411 char transport_protocol[16];
413 char lower_transport[16];
415 RTSPTransportField *th;
418 reply->nb_transports = 0;
425 th = &reply->transports[reply->nb_transports];
427 get_word_sep(transport_protocol, sizeof(transport_protocol),
431 get_word_sep(profile, sizeof(profile), "/;,", &p);
432 lower_transport[0] = '\0';
435 get_word_sep(lower_transport, sizeof(lower_transport),
438 if (!strcasecmp(lower_transport, "TCP"))
439 th->protocol = RTSP_PROTOCOL_RTP_TCP;
441 th->protocol = RTSP_PROTOCOL_RTP_UDP;
445 /* get each parameter */
446 while (*p != '\0' && *p != ',') {
447 get_word_sep(parameter, sizeof(parameter), "=;,", &p);
448 if (!strcmp(parameter, "port")) {
451 rtsp_parse_range(&th->port_min, &th->port_max, &p);
453 } else if (!strcmp(parameter, "client_port")) {
456 rtsp_parse_range(&th->client_port_min,
457 &th->client_port_max, &p);
459 } else if (!strcmp(parameter, "server_port")) {
462 rtsp_parse_range(&th->server_port_min,
463 &th->server_port_max, &p);
465 } else if (!strcmp(parameter, "interleaved")) {
468 rtsp_parse_range(&th->interleaved_min,
469 &th->interleaved_max, &p);
471 } else if (!strcmp(parameter, "multicast")) {
472 if (th->protocol == RTSP_PROTOCOL_RTP_UDP)
473 th->protocol = RTSP_PROTOCOL_RTP_UDP_MULTICAST;
474 } else if (!strcmp(parameter, "ttl")) {
477 th->ttl = strtol(p, (char **)&p, 10);
479 } else if (!strcmp(parameter, "destination")) {
480 struct in_addr ipaddr;
484 get_word_sep(buf, sizeof(buf), ";,", &p);
485 if (inet_aton(buf, &ipaddr))
486 th->destination = ntohl(ipaddr.s_addr);
489 while (*p != ';' && *p != '\0' && *p != ',')
497 reply->nb_transports++;
501 void rtsp_parse_line(RTSPHeader *reply, const char *buf)
505 /* NOTE: we do case independent match for broken servers */
507 if (stristart(p, "Session:", &p)) {
508 get_word_sep(reply->session_id, sizeof(reply->session_id), ";", &p);
509 } else if (stristart(p, "Content-Length:", &p)) {
510 reply->content_length = strtol(p, NULL, 10);
511 } else if (stristart(p, "Transport:", &p)) {
512 rtsp_parse_transport(reply, p);
513 } else if (stristart(p, "CSeq:", &p)) {
514 reply->seq = strtol(p, NULL, 10);
518 /* skip a RTP/TCP interleaved packet */
519 static void rtsp_skip_packet(AVFormatContext *s)
521 RTSPState *rt = s->priv_data;
525 ret = url_read(rt->rtsp_hd, buf, 3);
528 len = (buf[1] << 8) | buf[2];
530 printf("skipping RTP packet len=%d\n", len);
535 if (len1 > sizeof(buf))
537 ret = url_read(rt->rtsp_hd, buf, len1);
544 static void rtsp_send_cmd(AVFormatContext *s,
545 const char *cmd, RTSPHeader *reply,
546 unsigned char **content_ptr)
548 RTSPState *rt = s->priv_data;
549 char buf[4096], buf1[1024], *q;
552 int content_length, line_count;
553 unsigned char *content = NULL;
555 memset(reply, 0, sizeof(RTSPHeader));
558 pstrcpy(buf, sizeof(buf), cmd);
559 snprintf(buf1, sizeof(buf1), "CSeq: %d\r\n", rt->seq);
560 pstrcat(buf, sizeof(buf), buf1);
561 if (rt->session_id[0] != '\0' && !strstr(cmd, "\nIf-Match:")) {
562 snprintf(buf1, sizeof(buf1), "Session: %s\r\n", rt->session_id);
563 pstrcat(buf, sizeof(buf), buf1);
565 pstrcat(buf, sizeof(buf), "\r\n");
567 printf("Sending:\n%s--\n", buf);
569 url_write(rt->rtsp_hd, buf, strlen(buf));
571 /* parse reply (XXX: use buffers) */
573 rt->last_reply[0] = '\0';
577 if (url_read(rt->rtsp_hd, &ch, 1) != 1)
582 /* XXX: only parse it if first char on line ? */
584 } else if (ch != '\r') {
585 if ((q - buf) < sizeof(buf) - 1)
591 printf("line='%s'\n", buf);
593 /* test if last line */
597 if (line_count == 0) {
599 get_word(buf1, sizeof(buf1), &p);
600 get_word(buf1, sizeof(buf1), &p);
601 reply->status_code = atoi(buf1);
603 rtsp_parse_line(reply, p);
604 pstrcat(rt->last_reply, sizeof(rt->last_reply), p);
605 pstrcat(rt->last_reply, sizeof(rt->last_reply), "\n");
610 if (rt->session_id[0] == '\0' && reply->session_id[0] != '\0')
611 pstrcpy(rt->session_id, sizeof(rt->session_id), reply->session_id);
613 content_length = reply->content_length;
614 if (content_length > 0) {
615 /* leave some room for a trailing '\0' (useful for simple parsing) */
616 content = av_malloc(content_length + 1);
617 url_read(rt->rtsp_hd, content, content_length);
618 content[content_length] = '\0';
621 *content_ptr = content;
624 /* useful for modules: set RTSP callback function */
626 void rtsp_set_callback(FFRTSPCallback *rtsp_cb)
628 ff_rtsp_callback = rtsp_cb;
632 static int rtsp_read_header(AVFormatContext *s,
633 AVFormatParameters *ap)
635 RTSPState *rt = s->priv_data;
636 char host[1024], path[1024], tcpname[1024], cmd[2048];
638 int port, i, ret, err;
639 RTSPHeader reply1, *reply = &reply1;
640 unsigned char *content = NULL;
645 /* extract hostname and port */
647 host, sizeof(host), &port, path, sizeof(path), s->filename);
649 port = RTSP_DEFAULT_PORT;
651 /* open the tcp connexion */
652 snprintf(tcpname, sizeof(tcpname), "tcp://%s:%d", host, port);
653 if (url_open(&rtsp_hd, tcpname, URL_RDWR) < 0)
655 rt->rtsp_hd = rtsp_hd;
658 /* describe the stream */
659 snprintf(cmd, sizeof(cmd),
660 "DESCRIBE %s RTSP/1.0\r\n"
661 "Accept: application/sdp\r\n",
663 rtsp_send_cmd(s, cmd, reply, &content);
665 err = AVERROR_INVALIDDATA;
668 if (reply->status_code != RTSP_STATUS_OK) {
669 err = AVERROR_INVALIDDATA;
673 /* now we got the SDP description, we parse it */
674 ret = sdp_parse(s, (const char *)content);
677 err = AVERROR_INVALIDDATA;
681 protocol_mask = rtsp_default_protocols;
683 /* for each stream, make the setup request */
684 /* XXX: we assume the same server is used for the control of each
686 for(i=0;i<s->nb_streams;i++) {
687 char transport[2048];
691 rtsp_st = st->priv_data;
693 /* compute available transports */
697 if (protocol_mask & (1 << RTSP_PROTOCOL_RTP_UDP)) {
701 /* first try in specified port range */
702 if (rtsp_rtp_port_min != 0) {
703 for(j=rtsp_rtp_port_min;j<=rtsp_rtp_port_max;j++) {
704 snprintf(buf, sizeof(buf), "rtp://?localport=%d", j);
705 if (!av_open_input_file(&rtsp_st->ic, buf,
706 &rtp_demux, 0, NULL))
711 /* then try on any port */
712 if (av_open_input_file(&rtsp_st->ic, "rtp://",
713 &rtp_demux, 0, NULL) < 0) {
714 err = AVERROR_INVALIDDATA;
719 port = rtp_get_local_port(url_fileno(&rtsp_st->ic->pb));
720 if (transport[0] != '\0')
721 pstrcat(transport, sizeof(transport), ",");
722 snprintf(transport + strlen(transport), sizeof(transport) - strlen(transport) - 1,
723 "RTP/AVP/UDP;unicast;client_port=%d-%d",
728 if (protocol_mask & (1 << RTSP_PROTOCOL_RTP_TCP)) {
729 if (transport[0] != '\0')
730 pstrcat(transport, sizeof(transport), ",");
731 snprintf(transport + strlen(transport), sizeof(transport) - strlen(transport) - 1,
735 if (protocol_mask & (1 << RTSP_PROTOCOL_RTP_UDP_MULTICAST)) {
736 if (transport[0] != '\0')
737 pstrcat(transport, sizeof(transport), ",");
738 snprintf(transport + strlen(transport),
739 sizeof(transport) - strlen(transport) - 1,
740 "RTP/AVP/UDP;multicast");
742 snprintf(cmd, sizeof(cmd),
743 "SETUP %s RTSP/1.0\r\n"
745 rtsp_st->control_url, transport);
746 rtsp_send_cmd(s, cmd, reply, NULL);
747 if (reply->status_code != RTSP_STATUS_OK ||
748 reply->nb_transports != 1) {
749 err = AVERROR_INVALIDDATA;
753 /* XXX: same protocol for all streams is required */
755 if (reply->transports[0].protocol != rt->protocol) {
756 err = AVERROR_INVALIDDATA;
760 rt->protocol = reply->transports[0].protocol;
763 /* close RTP connection if not choosen */
764 if (reply->transports[0].protocol != RTSP_PROTOCOL_RTP_UDP &&
765 (protocol_mask & (1 << RTSP_PROTOCOL_RTP_UDP))) {
766 av_close_input_file(rtsp_st->ic);
770 switch(reply->transports[0].protocol) {
771 case RTSP_PROTOCOL_RTP_TCP:
773 if (av_open_input_file(&rtsp_st->ic, "null", fmt, 0, NULL) < 0) {
774 err = AVERROR_INVALIDDATA;
777 rtsp_st->interleaved_min = reply->transports[0].interleaved_min;
778 rtsp_st->interleaved_max = reply->transports[0].interleaved_max;
781 case RTSP_PROTOCOL_RTP_UDP:
785 /* XXX: also use address if specified */
786 snprintf(url, sizeof(url), "rtp://%s:%d",
787 host, reply->transports[0].server_port_min);
788 if (rtp_set_remote_url(url_fileno(&rtsp_st->ic->pb), url) < 0) {
789 err = AVERROR_INVALIDDATA;
794 case RTSP_PROTOCOL_RTP_UDP_MULTICAST:
800 ttl = reply->transports[0].ttl;
803 snprintf(url, sizeof(url), "rtp://%s:%d?multicast=1&ttl=%d",
805 reply->transports[0].server_port_min,
807 if (av_open_input_file(&rtsp_st->ic, url, fmt, 0, NULL) < 0) {
808 err = AVERROR_INVALIDDATA;
816 /* use callback if available to extend setup */
817 if (ff_rtsp_callback) {
818 if (ff_rtsp_callback(RTSP_ACTION_CLIENT_SETUP, rt->session_id,
819 NULL, 0, rt->last_reply) < 0) {
820 err = AVERROR_INVALIDDATA;
826 snprintf(cmd, sizeof(cmd),
827 "PLAY %s RTSP/1.0\r\n"
830 rtsp_send_cmd(s, cmd, reply, NULL);
831 if (reply->status_code != RTSP_STATUS_OK) {
832 err = AVERROR_INVALIDDATA;
837 /* open TCP with bufferized input */
838 if (rt->protocol == RTSP_PROTOCOL_RTP_TCP) {
839 if (url_fdopen(&rt->rtsp_gb, rt->rtsp_hd) < 0) {
848 for(i=0;i<s->nb_streams;i++) {
850 rtsp_st = st->priv_data;
853 av_close_input_file(rtsp_st->ic);
858 url_close(rt->rtsp_hd);
862 static int tcp_read_packet(AVFormatContext *s,
865 RTSPState *rt = s->priv_data;
869 uint8_t buf[RTP_MAX_PACKET_LENGTH];
872 printf("tcp_read_packet:\n");
876 ret = url_read(rt->rtsp_hd, buf, 1);
878 printf("ret=%d c=%02x [%c]\n", ret, buf[0], buf[0]);
885 ret = url_read(rt->rtsp_hd, buf, 3);
889 len = (buf[1] << 8) | buf[2];
891 printf("id=%d len=%d\n", id, len);
893 if (len > RTP_MAX_PACKET_LENGTH || len < 12)
896 ret = url_read(rt->rtsp_hd, buf, len);
900 /* find the matching stream */
901 for(i = 0; i < s->nb_streams; i++) {
903 rtsp_st = st->priv_data;
904 if (id >= rtsp_st->interleaved_min &&
905 id <= rtsp_st->interleaved_max)
910 ret = rtp_parse_packet(rtsp_st->ic, pkt, buf, len);
913 pkt->stream_index = i;
917 /* NOTE: output one packet at a time. May need to add a small fifo */
918 static int udp_read_packet(AVFormatContext *s,
925 int fd1, fd2, fd_max, n, i, ret;
926 char buf[RTP_MAX_PACKET_LENGTH];
930 if (url_interrupt_cb())
934 for(i = 0; i < s->nb_streams; i++) {
936 rtsp_st = st->priv_data;
938 /* currently, we cannot probe RTCP handle because of blocking restrictions */
939 rtp_get_file_handles(url_fileno(&ic->pb), &fd1, &fd2);
944 /* XXX: also add proper API to abort */
946 tv.tv_usec = 100 * 1000;
947 n = select(fd_max + 1, &rfds, NULL, NULL, &tv);
949 for(i = 0; i < s->nb_streams; i++) {
951 rtsp_st = st->priv_data;
953 rtp_get_file_handles(url_fileno(&ic->pb), &fd1, &fd2);
954 if (FD_ISSET(fd1, &rfds)) {
955 ret = url_read(url_fileno(&ic->pb), buf, sizeof(buf));
957 rtp_parse_packet(ic, pkt, buf, ret) == 0) {
958 pkt->stream_index = i;
967 static int rtsp_read_packet(AVFormatContext *s,
970 RTSPState *rt = s->priv_data;
973 switch(rt->protocol) {
975 case RTSP_PROTOCOL_RTP_TCP:
976 ret = tcp_read_packet(s, pkt);
978 case RTSP_PROTOCOL_RTP_UDP:
979 ret = udp_read_packet(s, pkt);
985 /* pause the stream */
986 int rtsp_pause(AVFormatContext *s)
989 RTSPHeader reply1, *reply = &reply1;
992 if (s->iformat != &rtsp_demux)
997 snprintf(cmd, sizeof(cmd),
998 "PAUSE %s RTSP/1.0\r\n",
1000 rtsp_send_cmd(s, cmd, reply, NULL);
1001 if (reply->status_code != RTSP_STATUS_OK) {
1008 /* resume the stream */
1009 int rtsp_resume(AVFormatContext *s)
1012 RTSPHeader reply1, *reply = &reply1;
1015 if (s->iformat != &rtsp_demux)
1020 snprintf(cmd, sizeof(cmd),
1021 "PLAY %s RTSP/1.0\r\n",
1023 rtsp_send_cmd(s, cmd, reply, NULL);
1024 if (reply->status_code != RTSP_STATUS_OK) {
1031 static int rtsp_read_close(AVFormatContext *s)
1033 RTSPState *rt = s->priv_data;
1035 RTSPStream *rtsp_st;
1036 RTSPHeader reply1, *reply = &reply1;
1041 /* NOTE: it is valid to flush the buffer here */
1042 if (rt->protocol == RTSP_PROTOCOL_RTP_TCP) {
1043 url_fclose(&rt->rtsp_gb);
1046 snprintf(cmd, sizeof(cmd),
1047 "TEARDOWN %s RTSP/1.0\r\n",
1049 rtsp_send_cmd(s, cmd, reply, NULL);
1051 if (ff_rtsp_callback) {
1052 ff_rtsp_callback(RTSP_ACTION_CLIENT_TEARDOWN, rt->session_id,
1056 for(i=0;i<s->nb_streams;i++) {
1058 rtsp_st = st->priv_data;
1061 av_close_input_file(rtsp_st->ic);
1065 url_close(rt->rtsp_hd);
1069 AVInputFormat rtsp_demux = {
1071 "RTSP input format",
1077 .flags = AVFMT_NOFILE,
1080 static int sdp_probe(AVProbeData *p1)
1084 /* we look for a line beginning "c=IN IP4" */
1086 while (*p != '\0') {
1087 if (strstart(p, "c=IN IP4", NULL))
1088 return AVPROBE_SCORE_MAX / 2;
1089 p = strchr(p, '\n');
1099 #define SDP_MAX_SIZE 8192
1101 static int sdp_read_header(AVFormatContext *s,
1102 AVFormatParameters *ap)
1105 RTSPStream *rtsp_st;
1110 /* read the whole sdp file */
1111 /* XXX: better loading */
1112 content = av_malloc(SDP_MAX_SIZE);
1113 size = get_buffer(&s->pb, content, SDP_MAX_SIZE - 1);
1116 return AVERROR_INVALIDDATA;
1118 content[size] ='\0';
1120 sdp_parse(s, content);
1123 /* open each RTP stream */
1124 for(i=0;i<s->nb_streams;i++) {
1126 rtsp_st = st->priv_data;
1128 snprintf(url, sizeof(url), "rtp://%s:%d?multicast=1&ttl=%d",
1129 inet_ntoa(rtsp_st->sdp_ip),
1132 if (av_open_input_file(&rtsp_st->ic, url, &rtp_demux, 0, NULL) < 0) {
1133 err = AVERROR_INVALIDDATA;
1139 for(i=0;i<s->nb_streams;i++) {
1141 rtsp_st = st->priv_data;
1144 av_close_input_file(rtsp_st->ic);
1151 static int sdp_read_packet(AVFormatContext *s,
1154 return udp_read_packet(s, pkt);
1157 static int sdp_read_close(AVFormatContext *s)
1160 RTSPStream *rtsp_st;
1163 for(i=0;i<s->nb_streams;i++) {
1165 rtsp_st = st->priv_data;
1168 av_close_input_file(rtsp_st->ic);
1176 static AVInputFormat sdp_demux = {
1187 /* dummy redirector format (used directly in av_open_input_file now) */
1188 static int redir_probe(AVProbeData *pd)
1192 while (redir_isspace(*p))
1194 if (strstart(p, "http://", NULL) ||
1195 strstart(p, "rtsp://", NULL))
1196 return AVPROBE_SCORE_MAX;
1200 /* called from utils.c */
1201 int redir_open(AVFormatContext **ic_ptr, ByteIOContext *f)
1205 AVFormatContext *ic = NULL;
1207 /* parse each URL and try to open it */
1209 while (c != URL_EOF) {
1212 if (!redir_isspace(c))
1221 if (c == URL_EOF || redir_isspace(c))
1223 if ((q - buf) < sizeof(buf) - 1)
1228 //printf("URL='%s'\n", buf);
1229 /* try to open the media file */
1230 if (av_open_input_file(&ic, buf, NULL, 0, NULL) == 0)
1240 AVInputFormat redir_demux = {
1242 "Redirector format",
1252 av_register_input_format(&rtsp_demux);
1253 av_register_input_format(&redir_demux);
1254 av_register_input_format(&sdp_demux);