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 */
37 struct RTSPStream **rtsp_streams;
39 /* XXX: currently we use unbuffered input */
40 // ByteIOContext rtsp_gb;
41 int seq; /* RTSP command sequence number */
43 enum RTSPProtocol protocol;
44 char last_reply[2048]; /* XXX: allocate ? */
45 RTPDemuxContext *cur_rtp;
48 typedef struct RTSPStream {
49 URLContext *rtp_handle; /* RTP stream handle */
50 RTPDemuxContext *rtp_ctx; /* RTP parse context */
52 int stream_index; /* corresponding stream index, if any. -1 if none (MPEG2TS case) */
53 int interleaved_min, interleaved_max; /* interleave ids, if TCP transport */
54 char control_url[1024]; /* url for this stream (from SDP) */
56 int sdp_port; /* port (from SDP content - not used in RTSP) */
57 struct in_addr sdp_ip; /* IP address (from SDP content - not used in RTSP) */
58 int sdp_ttl; /* IP TTL (from SDP content - not used in RTSP) */
59 int sdp_payload_type; /* payload type - only used in SDP */
62 /* XXX: currently, the only way to change the protocols consists in
63 changing this variable */
65 int rtsp_default_protocols = (1 << RTSP_PROTOCOL_RTP_TCP) | (1 << RTSP_PROTOCOL_RTP_UDP) | (1 << RTSP_PROTOCOL_RTP_UDP_MULTICAST);
67 /* try it if a proxy is used */
68 int rtsp_default_protocols = (1 << RTSP_PROTOCOL_RTP_TCP);
71 /* if non zero, then set a range for RTP ports */
72 int rtsp_rtp_port_min = 0;
73 int rtsp_rtp_port_max = 0;
75 FFRTSPCallback *ff_rtsp_callback = NULL;
77 static int rtsp_probe(AVProbeData *p)
79 if (strstart(p->filename, "rtsp:", NULL))
80 return AVPROBE_SCORE_MAX;
84 static int redir_isspace(int c)
86 return (c == ' ' || c == '\t' || c == '\n' || c == '\r');
89 static void skip_spaces(const char **pp)
93 while (redir_isspace(*p))
98 static void get_word_sep(char *buf, int buf_size, const char *sep,
107 while (!strchr(sep, *p) && *p != '\0') {
108 if ((q - buf) < buf_size - 1)
117 static void get_word(char *buf, int buf_size, const char **pp)
125 while (!redir_isspace(*p) && *p != '\0') {
126 if ((q - buf) < buf_size - 1)
135 /* parse the rtpmap description: <codec_name>/<clock_rate>[/<other
137 static int sdp_parse_rtpmap(AVCodecContext *codec, const char *p)
142 get_word_sep(buf, sizeof(buf), "/", &p);
143 if (!strcmp(buf, "MP4V-ES")) {
144 codec->codec_id = CODEC_ID_MPEG4;
151 /* return the length and optionnaly the data */
152 static int hex_to_data(uint8_t *data, const char *p)
162 c = toupper((unsigned char)*p++);
163 if (c >= '0' && c <= '9')
165 else if (c >= 'A' && c <= 'F')
180 static void sdp_parse_fmtp(AVCodecContext *codec, const char *p)
186 /* loop on each attribute */
191 get_word_sep(attr, sizeof(attr), "=", &p);
194 get_word_sep(value, sizeof(value), ";", &p);
197 /* handle MPEG4 video */
198 switch(codec->codec_id) {
200 if (!strcmp(attr, "config")) {
201 /* decode the hexa encoded parameter */
202 len = hex_to_data(NULL, value);
203 codec->extradata = av_mallocz(len);
204 if (!codec->extradata)
206 codec->extradata_size = len;
207 hex_to_data(codec->extradata, value);
211 /* ignore data for other codecs */
215 // printf("'%s' = '%s'\n", attr, value);
219 typedef struct SDPParseState {
221 struct in_addr default_ip;
225 static void sdp_parse_line(AVFormatContext *s, SDPParseState *s1,
226 int letter, const char *buf)
228 RTSPState *rt = s->priv_data;
229 char buf1[64], st_type[64];
231 int codec_type, payload_type, i;
234 struct in_addr sdp_ip;
238 printf("sdp: %c='%s'\n", letter, buf);
244 get_word(buf1, sizeof(buf1), &p);
245 if (strcmp(buf1, "IN") != 0)
247 get_word(buf1, sizeof(buf1), &p);
248 if (strcmp(buf1, "IP4") != 0)
250 get_word_sep(buf1, sizeof(buf1), "/", &p);
251 if (inet_aton(buf1, &sdp_ip) == 0)
256 get_word_sep(buf1, sizeof(buf1), "/", &p);
259 if (s->nb_streams == 0) {
260 s1->default_ip = sdp_ip;
261 s1->default_ttl = ttl;
263 st = s->streams[s->nb_streams - 1];
264 rtsp_st = st->priv_data;
265 rtsp_st->sdp_ip = sdp_ip;
266 rtsp_st->sdp_ttl = ttl;
270 pstrcpy(s->title, sizeof(s->title), p);
273 if (s->nb_streams == 0) {
274 pstrcpy(s->comment, sizeof(s->comment), p);
280 get_word(st_type, sizeof(st_type), &p);
281 if (!strcmp(st_type, "audio")) {
282 codec_type = CODEC_TYPE_AUDIO;
283 } else if (!strcmp(st_type, "video")) {
284 codec_type = CODEC_TYPE_VIDEO;
288 rtsp_st = av_mallocz(sizeof(RTSPStream));
291 rtsp_st->stream_index = -1;
292 dynarray_add(&rt->rtsp_streams, &rt->nb_rtsp_streams, rtsp_st);
294 rtsp_st->sdp_ip = s1->default_ip;
295 rtsp_st->sdp_ttl = s1->default_ttl;
297 get_word(buf1, sizeof(buf1), &p); /* port */
298 rtsp_st->sdp_port = atoi(buf1);
300 get_word(buf1, sizeof(buf1), &p); /* protocol (ignored) */
302 /* XXX: handle list of formats */
303 get_word(buf1, sizeof(buf1), &p); /* format list */
304 rtsp_st->sdp_payload_type = atoi(buf1);
306 if (rtsp_st->sdp_payload_type == RTP_PT_MPEG2TS) {
307 /* no corresponding stream */
309 st = av_new_stream(s, 0);
312 st->priv_data = rtsp_st;
313 rtsp_st->stream_index = st->index;
314 st->codec.codec_type = codec_type;
315 if (rtsp_st->sdp_payload_type < 96) {
316 /* if standard payload type, we can find the codec right now */
317 rtp_get_codec_info(&st->codec, rtsp_st->sdp_payload_type);
320 /* put a default control url */
321 pstrcpy(rtsp_st->control_url, sizeof(rtsp_st->control_url), s->filename);
324 if (strstart(p, "control:", &p) && s->nb_streams > 0) {
326 /* get the control url */
327 st = s->streams[s->nb_streams - 1];
328 rtsp_st = st->priv_data;
330 /* XXX: may need to add full url resolution */
331 url_split(proto, sizeof(proto), NULL, 0, NULL, NULL, 0, p);
332 if (proto[0] == '\0') {
333 /* relative control URL */
334 pstrcat(rtsp_st->control_url, sizeof(rtsp_st->control_url), "/");
335 pstrcat(rtsp_st->control_url, sizeof(rtsp_st->control_url), p);
337 pstrcpy(rtsp_st->control_url, sizeof(rtsp_st->control_url), p);
339 } else if (strstart(p, "rtpmap:", &p)) {
340 /* NOTE: rtpmap is only supported AFTER the 'm=' tag */
341 get_word(buf1, sizeof(buf1), &p);
342 payload_type = atoi(buf1);
343 for(i = 0; i < s->nb_streams;i++) {
345 rtsp_st = st->priv_data;
346 if (rtsp_st->sdp_payload_type == payload_type) {
347 sdp_parse_rtpmap(&st->codec, p);
350 } else if (strstart(p, "fmtp:", &p)) {
351 /* NOTE: fmtp is only supported AFTER the 'a=rtpmap:xxx' tag */
352 get_word(buf1, sizeof(buf1), &p);
353 payload_type = atoi(buf1);
354 for(i = 0; i < s->nb_streams;i++) {
356 rtsp_st = st->priv_data;
357 if (rtsp_st->sdp_payload_type == payload_type) {
358 sdp_parse_fmtp(&st->codec, p);
366 static int sdp_parse(AVFormatContext *s, const char *content)
371 SDPParseState sdp_parse_state, *s1 = &sdp_parse_state;
373 memset(s1, 0, sizeof(SDPParseState));
384 /* get the content */
386 while (*p != '\n' && *p != '\r' && *p != '\0') {
387 if ((q - buf) < sizeof(buf) - 1)
392 sdp_parse_line(s, s1, letter, buf);
394 while (*p != '\n' && *p != '\0')
402 static void rtsp_parse_range(int *min_ptr, int *max_ptr, const char **pp)
409 v = strtol(p, (char **)&p, 10);
413 v = strtol(p, (char **)&p, 10);
422 /* XXX: only one transport specification is parsed */
423 static void rtsp_parse_transport(RTSPHeader *reply, const char *p)
425 char transport_protocol[16];
427 char lower_transport[16];
429 RTSPTransportField *th;
432 reply->nb_transports = 0;
439 th = &reply->transports[reply->nb_transports];
441 get_word_sep(transport_protocol, sizeof(transport_protocol),
445 get_word_sep(profile, sizeof(profile), "/;,", &p);
446 lower_transport[0] = '\0';
449 get_word_sep(lower_transport, sizeof(lower_transport),
452 if (!strcasecmp(lower_transport, "TCP"))
453 th->protocol = RTSP_PROTOCOL_RTP_TCP;
455 th->protocol = RTSP_PROTOCOL_RTP_UDP;
459 /* get each parameter */
460 while (*p != '\0' && *p != ',') {
461 get_word_sep(parameter, sizeof(parameter), "=;,", &p);
462 if (!strcmp(parameter, "port")) {
465 rtsp_parse_range(&th->port_min, &th->port_max, &p);
467 } else if (!strcmp(parameter, "client_port")) {
470 rtsp_parse_range(&th->client_port_min,
471 &th->client_port_max, &p);
473 } else if (!strcmp(parameter, "server_port")) {
476 rtsp_parse_range(&th->server_port_min,
477 &th->server_port_max, &p);
479 } else if (!strcmp(parameter, "interleaved")) {
482 rtsp_parse_range(&th->interleaved_min,
483 &th->interleaved_max, &p);
485 } else if (!strcmp(parameter, "multicast")) {
486 if (th->protocol == RTSP_PROTOCOL_RTP_UDP)
487 th->protocol = RTSP_PROTOCOL_RTP_UDP_MULTICAST;
488 } else if (!strcmp(parameter, "ttl")) {
491 th->ttl = strtol(p, (char **)&p, 10);
493 } else if (!strcmp(parameter, "destination")) {
494 struct in_addr ipaddr;
498 get_word_sep(buf, sizeof(buf), ";,", &p);
499 if (inet_aton(buf, &ipaddr))
500 th->destination = ntohl(ipaddr.s_addr);
503 while (*p != ';' && *p != '\0' && *p != ',')
511 reply->nb_transports++;
515 void rtsp_parse_line(RTSPHeader *reply, const char *buf)
519 /* NOTE: we do case independent match for broken servers */
521 if (stristart(p, "Session:", &p)) {
522 get_word_sep(reply->session_id, sizeof(reply->session_id), ";", &p);
523 } else if (stristart(p, "Content-Length:", &p)) {
524 reply->content_length = strtol(p, NULL, 10);
525 } else if (stristart(p, "Transport:", &p)) {
526 rtsp_parse_transport(reply, p);
527 } else if (stristart(p, "CSeq:", &p)) {
528 reply->seq = strtol(p, NULL, 10);
532 /* skip a RTP/TCP interleaved packet */
533 static void rtsp_skip_packet(AVFormatContext *s)
535 RTSPState *rt = s->priv_data;
539 ret = url_read(rt->rtsp_hd, buf, 3);
542 len = (buf[1] << 8) | buf[2];
544 printf("skipping RTP packet len=%d\n", len);
549 if (len1 > sizeof(buf))
551 ret = url_read(rt->rtsp_hd, buf, len1);
558 static void rtsp_send_cmd(AVFormatContext *s,
559 const char *cmd, RTSPHeader *reply,
560 unsigned char **content_ptr)
562 RTSPState *rt = s->priv_data;
563 char buf[4096], buf1[1024], *q;
566 int content_length, line_count;
567 unsigned char *content = NULL;
569 memset(reply, 0, sizeof(RTSPHeader));
572 pstrcpy(buf, sizeof(buf), cmd);
573 snprintf(buf1, sizeof(buf1), "CSeq: %d\r\n", rt->seq);
574 pstrcat(buf, sizeof(buf), buf1);
575 if (rt->session_id[0] != '\0' && !strstr(cmd, "\nIf-Match:")) {
576 snprintf(buf1, sizeof(buf1), "Session: %s\r\n", rt->session_id);
577 pstrcat(buf, sizeof(buf), buf1);
579 pstrcat(buf, sizeof(buf), "\r\n");
581 printf("Sending:\n%s--\n", buf);
583 url_write(rt->rtsp_hd, buf, strlen(buf));
585 /* parse reply (XXX: use buffers) */
587 rt->last_reply[0] = '\0';
591 if (url_read(rt->rtsp_hd, &ch, 1) != 1)
596 /* XXX: only parse it if first char on line ? */
598 } else if (ch != '\r') {
599 if ((q - buf) < sizeof(buf) - 1)
605 printf("line='%s'\n", buf);
607 /* test if last line */
611 if (line_count == 0) {
613 get_word(buf1, sizeof(buf1), &p);
614 get_word(buf1, sizeof(buf1), &p);
615 reply->status_code = atoi(buf1);
617 rtsp_parse_line(reply, p);
618 pstrcat(rt->last_reply, sizeof(rt->last_reply), p);
619 pstrcat(rt->last_reply, sizeof(rt->last_reply), "\n");
624 if (rt->session_id[0] == '\0' && reply->session_id[0] != '\0')
625 pstrcpy(rt->session_id, sizeof(rt->session_id), reply->session_id);
627 content_length = reply->content_length;
628 if (content_length > 0) {
629 /* leave some room for a trailing '\0' (useful for simple parsing) */
630 content = av_malloc(content_length + 1);
631 url_read(rt->rtsp_hd, content, content_length);
632 content[content_length] = '\0';
635 *content_ptr = content;
638 /* useful for modules: set RTSP callback function */
640 void rtsp_set_callback(FFRTSPCallback *rtsp_cb)
642 ff_rtsp_callback = rtsp_cb;
646 /* close and free RTSP streams */
647 static void rtsp_close_streams(RTSPState *rt)
652 for(i=0;i<rt->nb_rtsp_streams;i++) {
653 rtsp_st = rt->rtsp_streams[i];
655 if (rtsp_st->rtp_ctx)
656 rtp_parse_close(rtsp_st->rtp_ctx);
657 if (rtsp_st->rtp_handle)
658 url_close(rtsp_st->rtp_handle);
662 av_free(rt->rtsp_streams);
665 static int rtsp_read_header(AVFormatContext *s,
666 AVFormatParameters *ap)
668 RTSPState *rt = s->priv_data;
669 char host[1024], path[1024], tcpname[1024], cmd[2048];
671 int port, i, ret, err;
672 RTSPHeader reply1, *reply = &reply1;
673 unsigned char *content = NULL;
678 /* extract hostname and port */
680 host, sizeof(host), &port, path, sizeof(path), s->filename);
682 port = RTSP_DEFAULT_PORT;
684 /* open the tcp connexion */
685 snprintf(tcpname, sizeof(tcpname), "tcp://%s:%d", host, port);
686 if (url_open(&rtsp_hd, tcpname, URL_RDWR) < 0)
688 rt->rtsp_hd = rtsp_hd;
691 /* describe the stream */
692 snprintf(cmd, sizeof(cmd),
693 "DESCRIBE %s RTSP/1.0\r\n"
694 "Accept: application/sdp\r\n",
696 rtsp_send_cmd(s, cmd, reply, &content);
698 err = AVERROR_INVALIDDATA;
701 if (reply->status_code != RTSP_STATUS_OK) {
702 err = AVERROR_INVALIDDATA;
706 /* now we got the SDP description, we parse it */
707 ret = sdp_parse(s, (const char *)content);
710 err = AVERROR_INVALIDDATA;
714 protocol_mask = rtsp_default_protocols;
716 /* for each stream, make the setup request */
717 /* XXX: we assume the same server is used for the control of each
719 for(i=0;i<rt->nb_rtsp_streams;i++) {
720 char transport[2048];
722 rtsp_st = rt->rtsp_streams[i];
724 /* compute available transports */
728 if (protocol_mask & (1 << RTSP_PROTOCOL_RTP_UDP)) {
732 /* first try in specified port range */
733 if (rtsp_rtp_port_min != 0) {
734 for(j=rtsp_rtp_port_min;j<=rtsp_rtp_port_max;j++) {
735 snprintf(buf, sizeof(buf), "rtp://?localport=%d", j);
736 if (url_open(&rtsp_st->rtp_handle, buf, URL_RDONLY) == 0)
741 /* then try on any port */
742 if (url_open(&rtsp_st->rtp_handle, "rtp://", URL_RDONLY) < 0) {
743 err = AVERROR_INVALIDDATA;
748 port = rtp_get_local_port(rtsp_st->rtp_handle);
749 if (transport[0] != '\0')
750 pstrcat(transport, sizeof(transport), ",");
751 snprintf(transport + strlen(transport), sizeof(transport) - strlen(transport) - 1,
752 "RTP/AVP/UDP;unicast;client_port=%d-%d",
757 if (protocol_mask & (1 << RTSP_PROTOCOL_RTP_TCP)) {
758 if (transport[0] != '\0')
759 pstrcat(transport, sizeof(transport), ",");
760 snprintf(transport + strlen(transport), sizeof(transport) - strlen(transport) - 1,
764 if (protocol_mask & (1 << RTSP_PROTOCOL_RTP_UDP_MULTICAST)) {
765 if (transport[0] != '\0')
766 pstrcat(transport, sizeof(transport), ",");
767 snprintf(transport + strlen(transport),
768 sizeof(transport) - strlen(transport) - 1,
769 "RTP/AVP/UDP;multicast");
771 snprintf(cmd, sizeof(cmd),
772 "SETUP %s RTSP/1.0\r\n"
774 rtsp_st->control_url, transport);
775 rtsp_send_cmd(s, cmd, reply, NULL);
776 if (reply->status_code != RTSP_STATUS_OK ||
777 reply->nb_transports != 1) {
778 err = AVERROR_INVALIDDATA;
782 /* XXX: same protocol for all streams is required */
784 if (reply->transports[0].protocol != rt->protocol) {
785 err = AVERROR_INVALIDDATA;
789 rt->protocol = reply->transports[0].protocol;
792 /* close RTP connection if not choosen */
793 if (reply->transports[0].protocol != RTSP_PROTOCOL_RTP_UDP &&
794 (protocol_mask & (1 << RTSP_PROTOCOL_RTP_UDP))) {
795 url_close(rtsp_st->rtp_handle);
796 rtsp_st->rtp_handle = NULL;
799 switch(reply->transports[0].protocol) {
800 case RTSP_PROTOCOL_RTP_TCP:
801 rtsp_st->interleaved_min = reply->transports[0].interleaved_min;
802 rtsp_st->interleaved_max = reply->transports[0].interleaved_max;
805 case RTSP_PROTOCOL_RTP_UDP:
809 /* XXX: also use address if specified */
810 snprintf(url, sizeof(url), "rtp://%s:%d",
811 host, reply->transports[0].server_port_min);
812 if (rtp_set_remote_url(rtsp_st->rtp_handle, url) < 0) {
813 err = AVERROR_INVALIDDATA;
818 case RTSP_PROTOCOL_RTP_UDP_MULTICAST:
823 ttl = reply->transports[0].ttl;
826 snprintf(url, sizeof(url), "rtp://%s:%d?multicast=1&ttl=%d",
828 reply->transports[0].server_port_min,
830 if (url_open(&rtsp_st->rtp_handle, url, URL_RDONLY) < 0) {
831 err = AVERROR_INVALIDDATA;
837 /* open the RTP context */
839 if (rtsp_st->stream_index >= 0)
840 st = s->streams[rtsp_st->stream_index];
842 s->ctx_flags |= AVFMTCTX_NOHEADER;
843 rtsp_st->rtp_ctx = rtp_parse_open(s, st, rtsp_st->sdp_payload_type);
844 if (!rtsp_st->rtp_ctx) {
850 /* use callback if available to extend setup */
851 if (ff_rtsp_callback) {
852 if (ff_rtsp_callback(RTSP_ACTION_CLIENT_SETUP, rt->session_id,
853 NULL, 0, rt->last_reply) < 0) {
854 err = AVERROR_INVALIDDATA;
860 snprintf(cmd, sizeof(cmd),
861 "PLAY %s RTSP/1.0\r\n"
864 rtsp_send_cmd(s, cmd, reply, NULL);
865 if (reply->status_code != RTSP_STATUS_OK) {
866 err = AVERROR_INVALIDDATA;
871 /* open TCP with bufferized input */
872 if (rt->protocol == RTSP_PROTOCOL_RTP_TCP) {
873 if (url_fdopen(&rt->rtsp_gb, rt->rtsp_hd) < 0) {
882 rtsp_close_streams(rt);
884 url_close(rt->rtsp_hd);
888 static int tcp_read_packet(AVFormatContext *s, RTSPStream **prtsp_st,
889 uint8_t *buf, int buf_size)
891 RTSPState *rt = s->priv_data;
896 printf("tcp_read_packet:\n");
900 ret = url_read(rt->rtsp_hd, buf, 1);
902 printf("ret=%d c=%02x [%c]\n", ret, buf[0], buf[0]);
909 ret = url_read(rt->rtsp_hd, buf, 3);
913 len = (buf[1] << 8) | buf[2];
915 printf("id=%d len=%d\n", id, len);
917 if (len > buf_size || len < 12)
920 ret = url_read(rt->rtsp_hd, buf, len);
924 /* find the matching stream */
925 for(i = 0; i < rt->nb_rtsp_streams; i++) {
926 rtsp_st = rt->rtsp_streams[i];
927 if (id >= rtsp_st->interleaved_min &&
928 id <= rtsp_st->interleaved_max)
937 static int udp_read_packet(AVFormatContext *s, RTSPStream **prtsp_st,
938 uint8_t *buf, int buf_size)
940 RTSPState *rt = s->priv_data;
943 int fd1, fd2, fd_max, n, i, ret;
947 if (url_interrupt_cb())
951 for(i = 0; i < rt->nb_rtsp_streams; i++) {
952 rtsp_st = rt->rtsp_streams[i];
953 /* currently, we cannot probe RTCP handle because of blocking restrictions */
954 rtp_get_file_handles(rtsp_st->rtp_handle, &fd1, &fd2);
960 tv.tv_usec = 100 * 1000;
961 n = select(fd_max + 1, &rfds, NULL, NULL, &tv);
963 for(i = 0; i < rt->nb_rtsp_streams; i++) {
964 rtsp_st = rt->rtsp_streams[i];
965 rtp_get_file_handles(rtsp_st->rtp_handle, &fd1, &fd2);
966 if (FD_ISSET(fd1, &rfds)) {
967 ret = url_read(rtsp_st->rtp_handle, buf, buf_size);
978 static int rtsp_read_packet(AVFormatContext *s,
981 RTSPState *rt = s->priv_data;
984 uint8_t buf[RTP_MAX_PACKET_LENGTH];
986 /* get next frames from the same RTP packet */
988 ret = rtp_parse_packet(rt->cur_rtp, pkt, NULL, 0);
992 } else if (ret == 1) {
999 /* read next RTP packet */
1001 switch(rt->protocol) {
1003 case RTSP_PROTOCOL_RTP_TCP:
1004 len = tcp_read_packet(s, &rtsp_st, buf, sizeof(buf));
1006 case RTSP_PROTOCOL_RTP_UDP:
1007 case RTSP_PROTOCOL_RTP_UDP_MULTICAST:
1008 len = udp_read_packet(s, &rtsp_st, buf, sizeof(buf));
1013 ret = rtp_parse_packet(rtsp_st->rtp_ctx, pkt, buf, len);
1017 /* more packets may follow, so we save the RTP context */
1018 rt->cur_rtp = rtsp_st->rtp_ctx;
1023 /* pause the stream */
1024 int rtsp_pause(AVFormatContext *s)
1027 RTSPHeader reply1, *reply = &reply1;
1030 if (s->iformat != &rtsp_demux)
1035 snprintf(cmd, sizeof(cmd),
1036 "PAUSE %s RTSP/1.0\r\n",
1038 rtsp_send_cmd(s, cmd, reply, NULL);
1039 if (reply->status_code != RTSP_STATUS_OK) {
1046 /* resume the stream */
1047 int rtsp_resume(AVFormatContext *s)
1050 RTSPHeader reply1, *reply = &reply1;
1053 if (s->iformat != &rtsp_demux)
1058 snprintf(cmd, sizeof(cmd),
1059 "PLAY %s RTSP/1.0\r\n",
1061 rtsp_send_cmd(s, cmd, reply, NULL);
1062 if (reply->status_code != RTSP_STATUS_OK) {
1069 static int rtsp_read_close(AVFormatContext *s)
1071 RTSPState *rt = s->priv_data;
1072 RTSPHeader reply1, *reply = &reply1;
1076 /* NOTE: it is valid to flush the buffer here */
1077 if (rt->protocol == RTSP_PROTOCOL_RTP_TCP) {
1078 url_fclose(&rt->rtsp_gb);
1081 snprintf(cmd, sizeof(cmd),
1082 "TEARDOWN %s RTSP/1.0\r\n",
1084 rtsp_send_cmd(s, cmd, reply, NULL);
1086 if (ff_rtsp_callback) {
1087 ff_rtsp_callback(RTSP_ACTION_CLIENT_TEARDOWN, rt->session_id,
1091 rtsp_close_streams(rt);
1092 url_close(rt->rtsp_hd);
1096 AVInputFormat rtsp_demux = {
1098 "RTSP input format",
1104 .flags = AVFMT_NOFILE,
1107 static int sdp_probe(AVProbeData *p1)
1111 /* we look for a line beginning "c=IN IP4" */
1113 while (*p != '\0') {
1114 if (strstart(p, "c=IN IP4", NULL))
1115 return AVPROBE_SCORE_MAX / 2;
1116 p = strchr(p, '\n');
1126 #define SDP_MAX_SIZE 8192
1128 static int sdp_read_header(AVFormatContext *s,
1129 AVFormatParameters *ap)
1131 RTSPState *rt = s->priv_data;
1132 RTSPStream *rtsp_st;
1138 /* read the whole sdp file */
1139 /* XXX: better loading */
1140 content = av_malloc(SDP_MAX_SIZE);
1141 size = get_buffer(&s->pb, content, SDP_MAX_SIZE - 1);
1144 return AVERROR_INVALIDDATA;
1146 content[size] ='\0';
1148 sdp_parse(s, content);
1151 /* open each RTP stream */
1152 for(i=0;i<rt->nb_rtsp_streams;i++) {
1153 rtsp_st = rt->rtsp_streams[i];
1155 snprintf(url, sizeof(url), "rtp://%s:%d?multicast=1&ttl=%d",
1156 inet_ntoa(rtsp_st->sdp_ip),
1159 if (url_open(&rtsp_st->rtp_handle, url, URL_RDONLY) < 0) {
1160 err = AVERROR_INVALIDDATA;
1163 /* open the RTP context */
1165 if (rtsp_st->stream_index >= 0)
1166 st = s->streams[rtsp_st->stream_index];
1168 s->ctx_flags |= AVFMTCTX_NOHEADER;
1169 rtsp_st->rtp_ctx = rtp_parse_open(s, st, rtsp_st->sdp_payload_type);
1170 if (!rtsp_st->rtp_ctx) {
1171 err = AVERROR_NOMEM;
1177 rtsp_close_streams(rt);
1181 static int sdp_read_packet(AVFormatContext *s,
1184 return rtsp_read_packet(s, pkt);
1187 static int sdp_read_close(AVFormatContext *s)
1189 RTSPState *rt = s->priv_data;
1190 rtsp_close_streams(rt);
1195 static AVInputFormat sdp_demux = {
1206 /* dummy redirector format (used directly in av_open_input_file now) */
1207 static int redir_probe(AVProbeData *pd)
1211 while (redir_isspace(*p))
1213 if (strstart(p, "http://", NULL) ||
1214 strstart(p, "rtsp://", NULL))
1215 return AVPROBE_SCORE_MAX;
1219 /* called from utils.c */
1220 int redir_open(AVFormatContext **ic_ptr, ByteIOContext *f)
1224 AVFormatContext *ic = NULL;
1226 /* parse each URL and try to open it */
1228 while (c != URL_EOF) {
1231 if (!redir_isspace(c))
1240 if (c == URL_EOF || redir_isspace(c))
1242 if ((q - buf) < sizeof(buf) - 1)
1247 //printf("URL='%s'\n", buf);
1248 /* try to open the media file */
1249 if (av_open_input_file(&ic, buf, NULL, 0, NULL) == 0)
1259 AVInputFormat redir_demux = {
1261 "Redirector format",
1271 av_register_input_format(&rtsp_demux);
1272 av_register_input_format(&redir_demux);
1273 av_register_input_format(&sdp_demux);