3 * Copyright (c) 2002 Fabrice Bellard
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 /* needed by inet_aton() */
25 #include "libavutil/avstring.h"
26 #include "libavutil/intreadwrite.h"
31 #include <sys/select.h>
42 //#define DEBUG_RTP_TCP
44 static int rtsp_read_play(AVFormatContext *s);
46 /* XXX: currently, the only way to change the protocols consists in
47 changing this variable */
49 #if LIBAVFORMAT_VERSION_INT < (53 << 16)
50 int rtsp_default_protocols = (1 << RTSP_LOWER_TRANSPORT_UDP);
53 static int rtsp_probe(AVProbeData *p)
55 if (av_strstart(p->filename, "rtsp:", NULL))
56 return AVPROBE_SCORE_MAX;
60 #define SPACE_CHARS " \t\r\n"
61 #define redir_isspace(c) strchr(SPACE_CHARS, c)
62 static void skip_spaces(const char **pp)
66 while (redir_isspace(*p))
71 static void get_word_until_chars(char *buf, int buf_size,
72 const char *sep, const char **pp)
80 while (!strchr(sep, *p) && *p != '\0') {
81 if ((q - buf) < buf_size - 1)
90 static void get_word_sep(char *buf, int buf_size, const char *sep,
93 if (**pp == '/') (*pp)++;
94 get_word_until_chars(buf, buf_size, sep, pp);
97 static void get_word(char *buf, int buf_size, const char **pp)
99 get_word_until_chars(buf, buf_size, SPACE_CHARS, pp);
102 /* parse the rtpmap description: <codec_name>/<clock_rate>[/<other
104 static int sdp_parse_rtpmap(AVCodecContext *codec, RTSPStream *rtsp_st, int payload_type, const char *p)
111 /* Loop into AVRtpDynamicPayloadTypes[] and AVRtpPayloadTypes[] and
112 see if we can handle this kind of payload */
113 get_word_sep(buf, sizeof(buf), "/", &p);
114 if (payload_type >= RTP_PT_PRIVATE) {
115 RTPDynamicProtocolHandler *handler= RTPFirstDynamicPayloadHandler;
117 if (!strcasecmp(buf, handler->enc_name) && (codec->codec_type == handler->codec_type)) {
118 codec->codec_id = handler->codec_id;
119 rtsp_st->dynamic_handler= handler;
121 rtsp_st->dynamic_protocol_context= handler->open();
125 handler= handler->next;
128 /* We are in a standard case ( from http://www.iana.org/assignments/rtp-parameters) */
129 /* search into AVRtpPayloadTypes[] */
130 codec->codec_id = ff_rtp_codec_id(buf, codec->codec_type);
133 c = avcodec_find_decoder(codec->codec_id);
137 c_name = (char *)NULL;
140 get_word_sep(buf, sizeof(buf), "/", &p);
142 switch (codec->codec_type) {
143 case CODEC_TYPE_AUDIO:
144 av_log(codec, AV_LOG_DEBUG, " audio codec set to : %s\n", c_name);
145 codec->sample_rate = RTSP_DEFAULT_AUDIO_SAMPLERATE;
146 codec->channels = RTSP_DEFAULT_NB_AUDIO_CHANNELS;
148 codec->sample_rate = i;
149 get_word_sep(buf, sizeof(buf), "/", &p);
153 // TODO: there is a bug here; if it is a mono stream, and less than 22000Hz, faad upconverts to stereo and twice the
154 // frequency. No problem, but the sample rate is being set here by the sdp line. Upcoming patch forthcoming. (rdm)
156 av_log(codec, AV_LOG_DEBUG, " audio samplerate set to : %i\n", codec->sample_rate);
157 av_log(codec, AV_LOG_DEBUG, " audio channels set to : %i\n", codec->channels);
159 case CODEC_TYPE_VIDEO:
160 av_log(codec, AV_LOG_DEBUG, " video codec set to : %s\n", c_name);
171 /* return the length and optionnaly the data */
172 static int hex_to_data(uint8_t *data, const char *p)
182 c = toupper((unsigned char)*p++);
183 if (c >= '0' && c <= '9')
185 else if (c >= 'A' && c <= 'F')
200 static void sdp_parse_fmtp_config(AVCodecContext *codec, char *attr, char *value)
202 switch (codec->codec_id) {
205 if (!strcmp(attr, "config")) {
206 /* decode the hexa encoded parameter */
207 int len = hex_to_data(NULL, value);
208 codec->extradata = av_mallocz(len + FF_INPUT_BUFFER_PADDING_SIZE);
209 if (!codec->extradata)
211 codec->extradata_size = len;
212 hex_to_data(codec->extradata, value);
227 /* All known fmtp parmeters and the corresping RTPAttrTypeEnum */
228 #define ATTR_NAME_TYPE_INT 0
229 #define ATTR_NAME_TYPE_STR 1
230 static const AttrNameMap attr_names[]=
232 {"SizeLength", ATTR_NAME_TYPE_INT, offsetof(RTPPayloadData, sizelength)},
233 {"IndexLength", ATTR_NAME_TYPE_INT, offsetof(RTPPayloadData, indexlength)},
234 {"IndexDeltaLength", ATTR_NAME_TYPE_INT, offsetof(RTPPayloadData, indexdeltalength)},
235 {"profile-level-id", ATTR_NAME_TYPE_INT, offsetof(RTPPayloadData, profile_level_id)},
236 {"StreamType", ATTR_NAME_TYPE_INT, offsetof(RTPPayloadData, streamtype)},
237 {"mode", ATTR_NAME_TYPE_STR, offsetof(RTPPayloadData, mode)},
241 /** parse the attribute line from the fmtp a line of an sdp resonse. This is broken out as a function
242 * because it is used in rtp_h264.c, which is forthcoming.
244 int rtsp_next_attr_and_value(const char **p, char *attr, int attr_size, char *value, int value_size)
249 get_word_sep(attr, attr_size, "=", p);
252 get_word_sep(value, value_size, ";", p);
260 /* parse a SDP line and save stream attributes */
261 static void sdp_parse_fmtp(AVStream *st, const char *p)
267 RTSPStream *rtsp_st = st->priv_data;
268 AVCodecContext *codec = st->codec;
269 RTPPayloadData *rtp_payload_data = &rtsp_st->rtp_payload_data;
271 /* loop on each attribute */
272 while(rtsp_next_attr_and_value(&p, attr, sizeof(attr), value, sizeof(value)))
274 /* grab the codec extra_data from the config parameter of the fmtp line */
275 sdp_parse_fmtp_config(codec, attr, value);
276 /* Looking for a known attribute */
277 for (i = 0; attr_names[i].str; ++i) {
278 if (!strcasecmp(attr, attr_names[i].str)) {
279 if (attr_names[i].type == ATTR_NAME_TYPE_INT)
280 *(int *)((char *)rtp_payload_data + attr_names[i].offset) = atoi(value);
281 else if (attr_names[i].type == ATTR_NAME_TYPE_STR)
282 *(char **)((char *)rtp_payload_data + attr_names[i].offset) = av_strdup(value);
288 /** Parse a string \p in the form of Range:npt=xx-xx, and determine the start
290 * Used for seeking in the rtp stream.
292 static void rtsp_parse_range_npt(const char *p, int64_t *start, int64_t *end)
297 if (!av_stristart(p, "npt=", &p))
300 *start = AV_NOPTS_VALUE;
301 *end = AV_NOPTS_VALUE;
303 get_word_sep(buf, sizeof(buf), "-", &p);
304 *start = parse_date(buf, 1);
307 get_word_sep(buf, sizeof(buf), "-", &p);
308 *end = parse_date(buf, 1);
310 // av_log(NULL, AV_LOG_DEBUG, "Range Start: %lld\n", *start);
311 // av_log(NULL, AV_LOG_DEBUG, "Range End: %lld\n", *end);
314 typedef struct SDPParseState {
316 struct in_addr default_ip;
318 int skip_media; ///< set if an unknown m= line occurs
321 static void sdp_parse_line(AVFormatContext *s, SDPParseState *s1,
322 int letter, const char *buf)
324 RTSPState *rt = s->priv_data;
325 char buf1[64], st_type[64];
327 enum CodecType codec_type;
331 struct in_addr sdp_ip;
335 printf("sdp: %c='%s'\n", letter, buf);
339 if (s1->skip_media && letter != 'm')
343 get_word(buf1, sizeof(buf1), &p);
344 if (strcmp(buf1, "IN") != 0)
346 get_word(buf1, sizeof(buf1), &p);
347 if (strcmp(buf1, "IP4") != 0)
349 get_word_sep(buf1, sizeof(buf1), "/", &p);
350 if (inet_aton(buf1, &sdp_ip) == 0)
355 get_word_sep(buf1, sizeof(buf1), "/", &p);
358 if (s->nb_streams == 0) {
359 s1->default_ip = sdp_ip;
360 s1->default_ttl = ttl;
362 st = s->streams[s->nb_streams - 1];
363 rtsp_st = st->priv_data;
364 rtsp_st->sdp_ip = sdp_ip;
365 rtsp_st->sdp_ttl = ttl;
369 av_metadata_set(&s->metadata, "title", p);
372 if (s->nb_streams == 0) {
373 av_metadata_set(&s->metadata, "comment", p);
380 get_word(st_type, sizeof(st_type), &p);
381 if (!strcmp(st_type, "audio")) {
382 codec_type = CODEC_TYPE_AUDIO;
383 } else if (!strcmp(st_type, "video")) {
384 codec_type = CODEC_TYPE_VIDEO;
385 } else if (!strcmp(st_type, "application")) {
386 codec_type = CODEC_TYPE_DATA;
391 rtsp_st = av_mallocz(sizeof(RTSPStream));
394 rtsp_st->stream_index = -1;
395 dynarray_add(&rt->rtsp_streams, &rt->nb_rtsp_streams, rtsp_st);
397 rtsp_st->sdp_ip = s1->default_ip;
398 rtsp_st->sdp_ttl = s1->default_ttl;
400 get_word(buf1, sizeof(buf1), &p); /* port */
401 rtsp_st->sdp_port = atoi(buf1);
403 get_word(buf1, sizeof(buf1), &p); /* protocol (ignored) */
405 /* XXX: handle list of formats */
406 get_word(buf1, sizeof(buf1), &p); /* format list */
407 rtsp_st->sdp_payload_type = atoi(buf1);
409 if (!strcmp(ff_rtp_enc_name(rtsp_st->sdp_payload_type), "MP2T")) {
410 /* no corresponding stream */
412 st = av_new_stream(s, 0);
415 st->priv_data = rtsp_st;
416 rtsp_st->stream_index = st->index;
417 st->codec->codec_type = codec_type;
418 if (rtsp_st->sdp_payload_type < RTP_PT_PRIVATE) {
419 /* if standard payload type, we can find the codec right now */
420 ff_rtp_get_codec_info(st->codec, rtsp_st->sdp_payload_type);
423 /* put a default control url */
424 av_strlcpy(rtsp_st->control_url, s->filename, sizeof(rtsp_st->control_url));
427 if (av_strstart(p, "control:", &p) && s->nb_streams > 0) {
429 /* get the control url */
430 st = s->streams[s->nb_streams - 1];
431 rtsp_st = st->priv_data;
433 /* XXX: may need to add full url resolution */
434 url_split(proto, sizeof(proto), NULL, 0, NULL, 0, NULL, NULL, 0, p);
435 if (proto[0] == '\0') {
436 /* relative control URL */
437 av_strlcat(rtsp_st->control_url, "/", sizeof(rtsp_st->control_url));
438 av_strlcat(rtsp_st->control_url, p, sizeof(rtsp_st->control_url));
440 av_strlcpy(rtsp_st->control_url, p, sizeof(rtsp_st->control_url));
442 } else if (av_strstart(p, "rtpmap:", &p) && s->nb_streams > 0) {
443 /* NOTE: rtpmap is only supported AFTER the 'm=' tag */
444 get_word(buf1, sizeof(buf1), &p);
445 payload_type = atoi(buf1);
446 st = s->streams[s->nb_streams - 1];
447 rtsp_st = st->priv_data;
448 sdp_parse_rtpmap(st->codec, rtsp_st, payload_type, p);
449 } else if (av_strstart(p, "fmtp:", &p)) {
450 /* NOTE: fmtp is only supported AFTER the 'a=rtpmap:xxx' tag */
451 get_word(buf1, sizeof(buf1), &p);
452 payload_type = atoi(buf1);
453 for(i = 0; i < s->nb_streams;i++) {
455 rtsp_st = st->priv_data;
456 if (rtsp_st->sdp_payload_type == payload_type) {
457 if(rtsp_st->dynamic_handler && rtsp_st->dynamic_handler->parse_sdp_a_line) {
458 if(!rtsp_st->dynamic_handler->parse_sdp_a_line(s, i, rtsp_st->dynamic_protocol_context, buf)) {
459 sdp_parse_fmtp(st, p);
462 sdp_parse_fmtp(st, p);
466 } else if(av_strstart(p, "framesize:", &p)) {
467 // let dynamic protocol handlers have a stab at the line.
468 get_word(buf1, sizeof(buf1), &p);
469 payload_type = atoi(buf1);
470 for(i = 0; i < s->nb_streams;i++) {
472 rtsp_st = st->priv_data;
473 if (rtsp_st->sdp_payload_type == payload_type) {
474 if(rtsp_st->dynamic_handler && rtsp_st->dynamic_handler->parse_sdp_a_line) {
475 rtsp_st->dynamic_handler->parse_sdp_a_line(s, i, rtsp_st->dynamic_protocol_context, buf);
479 } else if(av_strstart(p, "range:", &p)) {
482 // this is so that seeking on a streamed file can work.
483 rtsp_parse_range_npt(p, &start, &end);
484 s->start_time= start;
485 s->duration= (end==AV_NOPTS_VALUE)?AV_NOPTS_VALUE:end-start; // AV_NOPTS_VALUE means live broadcast (and can't seek)
486 } else if (av_strstart(p, "IsRealDataType:integer;",&p)) {
488 rt->transport = RTSP_TRANSPORT_RDT;
490 if (rt->server_type == RTSP_SERVER_WMS)
491 ff_wms_parse_sdp_a_line(s, p);
492 if (s->nb_streams > 0) {
493 if (rt->server_type == RTSP_SERVER_REAL)
494 ff_real_parse_sdp_a_line(s, s->nb_streams - 1, p);
496 rtsp_st = s->streams[s->nb_streams - 1]->priv_data;
497 if (rtsp_st->dynamic_handler &&
498 rtsp_st->dynamic_handler->parse_sdp_a_line)
499 rtsp_st->dynamic_handler->parse_sdp_a_line(s, s->nb_streams - 1,
500 rtsp_st->dynamic_protocol_context, buf);
507 static int sdp_parse(AVFormatContext *s, const char *content)
511 /* Some SDP lines, particularly for Realmedia or ASF RTSP streams,
512 * contain long SDP lines containing complete ASF Headers (several
513 * kB) or arrays of MDPR (RM stream descriptor) headers plus
514 * "rulebooks" describing their properties. Therefore, the SDP line
515 * buffer is large. */
517 SDPParseState sdp_parse_state, *s1 = &sdp_parse_state;
519 memset(s1, 0, sizeof(SDPParseState));
530 /* get the content */
532 while (*p != '\n' && *p != '\r' && *p != '\0') {
533 if ((q - buf) < sizeof(buf) - 1)
538 sdp_parse_line(s, s1, letter, buf);
540 while (*p != '\n' && *p != '\0')
548 static void rtsp_parse_range(int *min_ptr, int *max_ptr, const char **pp)
555 v = strtol(p, (char **)&p, 10);
559 v = strtol(p, (char **)&p, 10);
568 /* XXX: only one transport specification is parsed */
569 static void rtsp_parse_transport(RTSPMessageHeader *reply, const char *p)
571 char transport_protocol[16];
573 char lower_transport[16];
575 RTSPTransportField *th;
578 reply->nb_transports = 0;
585 th = &reply->transports[reply->nb_transports];
587 get_word_sep(transport_protocol, sizeof(transport_protocol),
591 if (!strcasecmp (transport_protocol, "rtp")) {
592 get_word_sep(profile, sizeof(profile), "/;,", &p);
593 lower_transport[0] = '\0';
594 /* rtp/avp/<protocol> */
597 get_word_sep(lower_transport, sizeof(lower_transport),
600 th->transport = RTSP_TRANSPORT_RTP;
601 } else if (!strcasecmp (transport_protocol, "x-pn-tng") ||
602 !strcasecmp (transport_protocol, "x-real-rdt")) {
603 /* x-pn-tng/<protocol> */
604 get_word_sep(lower_transport, sizeof(lower_transport), "/;,", &p);
606 th->transport = RTSP_TRANSPORT_RDT;
608 if (!strcasecmp(lower_transport, "TCP"))
609 th->lower_transport = RTSP_LOWER_TRANSPORT_TCP;
611 th->lower_transport = RTSP_LOWER_TRANSPORT_UDP;
615 /* get each parameter */
616 while (*p != '\0' && *p != ',') {
617 get_word_sep(parameter, sizeof(parameter), "=;,", &p);
618 if (!strcmp(parameter, "port")) {
621 rtsp_parse_range(&th->port_min, &th->port_max, &p);
623 } else if (!strcmp(parameter, "client_port")) {
626 rtsp_parse_range(&th->client_port_min,
627 &th->client_port_max, &p);
629 } else if (!strcmp(parameter, "server_port")) {
632 rtsp_parse_range(&th->server_port_min,
633 &th->server_port_max, &p);
635 } else if (!strcmp(parameter, "interleaved")) {
638 rtsp_parse_range(&th->interleaved_min,
639 &th->interleaved_max, &p);
641 } else if (!strcmp(parameter, "multicast")) {
642 if (th->lower_transport == RTSP_LOWER_TRANSPORT_UDP)
643 th->lower_transport = RTSP_LOWER_TRANSPORT_UDP_MULTICAST;
644 } else if (!strcmp(parameter, "ttl")) {
647 th->ttl = strtol(p, (char **)&p, 10);
649 } else if (!strcmp(parameter, "destination")) {
650 struct in_addr ipaddr;
654 get_word_sep(buf, sizeof(buf), ";,", &p);
655 if (inet_aton(buf, &ipaddr))
656 th->destination = ntohl(ipaddr.s_addr);
659 while (*p != ';' && *p != '\0' && *p != ',')
667 reply->nb_transports++;
671 void rtsp_parse_line(RTSPMessageHeader *reply, const char *buf)
675 /* NOTE: we do case independent match for broken servers */
677 if (av_stristart(p, "Session:", &p)) {
678 get_word_sep(reply->session_id, sizeof(reply->session_id), ";", &p);
679 } else if (av_stristart(p, "Content-Length:", &p)) {
680 reply->content_length = strtol(p, NULL, 10);
681 } else if (av_stristart(p, "Transport:", &p)) {
682 rtsp_parse_transport(reply, p);
683 } else if (av_stristart(p, "CSeq:", &p)) {
684 reply->seq = strtol(p, NULL, 10);
685 } else if (av_stristart(p, "Range:", &p)) {
686 rtsp_parse_range_npt(p, &reply->range_start, &reply->range_end);
687 } else if (av_stristart(p, "RealChallenge1:", &p)) {
689 av_strlcpy(reply->real_challenge, p, sizeof(reply->real_challenge));
690 } else if (av_stristart(p, "Server:", &p)) {
692 av_strlcpy(reply->server, p, sizeof(reply->server));
696 static int url_readbuf(URLContext *h, unsigned char *buf, int size)
702 ret = url_read(h, buf+len, size-len);
710 /* skip a RTP/TCP interleaved packet */
711 static void rtsp_skip_packet(AVFormatContext *s)
713 RTSPState *rt = s->priv_data;
717 ret = url_readbuf(rt->rtsp_hd, buf, 3);
720 len = AV_RB16(buf + 1);
722 printf("skipping RTP packet len=%d\n", len);
727 if (len1 > sizeof(buf))
729 ret = url_readbuf(rt->rtsp_hd, buf, len1);
737 * Read a RTSP message from the server, or prepare to read data
738 * packets if we're reading data interleaved over the TCP/RTSP
739 * connection as well.
741 * @param s RTSP demuxer context
742 * @param reply pointer where the RTSP message header will be stored
743 * @param content_ptr pointer where the RTSP message body, if any, will
744 * be stored (length is in \p reply)
745 * @param return_on_interleaved_data whether the function may return if we
746 * encounter a data marker ('$'), which precedes data
747 * packets over interleaved TCP/RTSP connections. If this
748 * is set, this function will return 1 after encountering
749 * a '$'. If it is not set, the function will skip any
750 * data packets (if they are encountered), until a reply
751 * has been fully parsed. If no more data is available
752 * without parsing a reply, it will return an error.
754 * @returns 1 if a data packets is ready to be received, -1 on error,
758 rtsp_read_reply (AVFormatContext *s, RTSPMessageHeader *reply,
759 unsigned char **content_ptr, int return_on_interleaved_data)
761 RTSPState *rt = s->priv_data;
762 char buf[4096], buf1[1024], *q;
765 int ret, content_length, line_count = 0;
766 unsigned char *content = NULL;
768 memset(reply, 0, sizeof(*reply));
770 /* parse reply (XXX: use buffers) */
771 rt->last_reply[0] = '\0';
775 ret = url_readbuf(rt->rtsp_hd, &ch, 1);
777 printf("ret=%d c=%02x [%c]\n", ret, ch, ch);
784 /* XXX: only parse it if first char on line ? */
785 if (return_on_interleaved_data) {
789 } else if (ch != '\r') {
790 if ((q - buf) < sizeof(buf) - 1)
796 printf("line='%s'\n", buf);
798 /* test if last line */
802 if (line_count == 0) {
804 get_word(buf1, sizeof(buf1), &p);
805 get_word(buf1, sizeof(buf1), &p);
806 reply->status_code = atoi(buf1);
808 rtsp_parse_line(reply, p);
809 av_strlcat(rt->last_reply, p, sizeof(rt->last_reply));
810 av_strlcat(rt->last_reply, "\n", sizeof(rt->last_reply));
815 if (rt->session_id[0] == '\0' && reply->session_id[0] != '\0')
816 av_strlcpy(rt->session_id, reply->session_id, sizeof(rt->session_id));
818 content_length = reply->content_length;
819 if (content_length > 0) {
820 /* leave some room for a trailing '\0' (useful for simple parsing) */
821 content = av_malloc(content_length + 1);
822 (void)url_readbuf(rt->rtsp_hd, content, content_length);
823 content[content_length] = '\0';
826 *content_ptr = content;
833 static void rtsp_send_cmd(AVFormatContext *s,
834 const char *cmd, RTSPMessageHeader *reply,
835 unsigned char **content_ptr)
837 RTSPState *rt = s->priv_data;
838 char buf[4096], buf1[1024];
841 av_strlcpy(buf, cmd, sizeof(buf));
842 snprintf(buf1, sizeof(buf1), "CSeq: %d\r\n", rt->seq);
843 av_strlcat(buf, buf1, sizeof(buf));
844 if (rt->session_id[0] != '\0' && !strstr(cmd, "\nIf-Match:")) {
845 snprintf(buf1, sizeof(buf1), "Session: %s\r\n", rt->session_id);
846 av_strlcat(buf, buf1, sizeof(buf));
848 av_strlcat(buf, "\r\n", sizeof(buf));
850 printf("Sending:\n%s--\n", buf);
852 url_write(rt->rtsp_hd, buf, strlen(buf));
854 rtsp_read_reply(s, reply, content_ptr, 0);
858 /* close and free RTSP streams */
859 static void rtsp_close_streams(RTSPState *rt)
864 for(i=0;i<rt->nb_rtsp_streams;i++) {
865 rtsp_st = rt->rtsp_streams[i];
867 if (rtsp_st->transport_priv) {
868 if (rt->transport == RTSP_TRANSPORT_RDT)
869 ff_rdt_parse_close(rtsp_st->transport_priv);
871 rtp_parse_close(rtsp_st->transport_priv);
873 if (rtsp_st->rtp_handle)
874 url_close(rtsp_st->rtp_handle);
875 if (rtsp_st->dynamic_handler && rtsp_st->dynamic_protocol_context)
876 rtsp_st->dynamic_handler->close(rtsp_st->dynamic_protocol_context);
879 av_free(rt->rtsp_streams);
881 av_close_input_stream (rt->asf_ctx);
887 rtsp_open_transport_ctx(AVFormatContext *s, RTSPStream *rtsp_st)
889 RTSPState *rt = s->priv_data;
892 /* open the RTP context */
893 if (rtsp_st->stream_index >= 0)
894 st = s->streams[rtsp_st->stream_index];
896 s->ctx_flags |= AVFMTCTX_NOHEADER;
898 if (rt->transport == RTSP_TRANSPORT_RDT)
899 rtsp_st->transport_priv = ff_rdt_parse_open(s, st->index,
900 rtsp_st->dynamic_protocol_context,
901 rtsp_st->dynamic_handler);
903 rtsp_st->transport_priv = rtp_parse_open(s, st, rtsp_st->rtp_handle,
904 rtsp_st->sdp_payload_type,
905 &rtsp_st->rtp_payload_data);
907 if (!rtsp_st->transport_priv) {
908 return AVERROR(ENOMEM);
909 } else if (rt->transport != RTSP_TRANSPORT_RDT) {
910 if(rtsp_st->dynamic_handler) {
911 rtp_parse_set_dynamic_protocol(rtsp_st->transport_priv,
912 rtsp_st->dynamic_protocol_context,
913 rtsp_st->dynamic_handler);
921 * @returns 0 on success, <0 on error, 1 if protocol is unavailable.
924 make_setup_request (AVFormatContext *s, const char *host, int port,
925 int lower_transport, const char *real_challenge)
927 RTSPState *rt = s->priv_data;
928 int rtx, j, i, err, interleave = 0;
930 RTSPMessageHeader reply1, *reply = &reply1;
932 const char *trans_pref;
934 if (rt->transport == RTSP_TRANSPORT_RDT)
935 trans_pref = "x-pn-tng";
937 trans_pref = "RTP/AVP";
939 /* for each stream, make the setup request */
940 /* XXX: we assume the same server is used for the control of each
943 for(j = RTSP_RTP_PORT_MIN, i = 0; i < rt->nb_rtsp_streams; ++i) {
944 char transport[2048];
947 * WMS serves all UDP data over a single connection, the RTX, which
948 * isn't necessarily the first in the SDP but has to be the first
949 * to be set up, else the second/third SETUP will fail with a 461.
951 if (lower_transport == RTSP_LOWER_TRANSPORT_UDP &&
952 rt->server_type == RTSP_SERVER_WMS) {
955 for (rtx = 0; rtx < rt->nb_rtsp_streams; rtx++) {
956 int len = strlen(rt->rtsp_streams[rtx]->control_url);
958 !strcmp(rt->rtsp_streams[rtx]->control_url + len - 4, "/rtx"))
961 if (rtx == rt->nb_rtsp_streams)
962 return -1; /* no RTX found */
963 rtsp_st = rt->rtsp_streams[rtx];
965 rtsp_st = rt->rtsp_streams[i > rtx ? i : i - 1];
967 rtsp_st = rt->rtsp_streams[i];
970 if (lower_transport == RTSP_LOWER_TRANSPORT_UDP) {
973 if (rt->server_type == RTSP_SERVER_WMS && i > 1) {
974 port = reply->transports[0].client_port_min;
978 /* first try in specified port range */
979 if (RTSP_RTP_PORT_MIN != 0) {
980 while(j <= RTSP_RTP_PORT_MAX) {
981 snprintf(buf, sizeof(buf), "rtp://%s?localport=%d", host, j);
982 j += 2; /* we will use two port by rtp stream (rtp and rtcp) */
983 if (url_open(&rtsp_st->rtp_handle, buf, URL_RDWR) == 0) {
989 /* then try on any port
990 ** if (url_open(&rtsp_st->rtp_handle, "rtp://", URL_RDONLY) < 0) {
991 ** err = AVERROR_INVALIDDATA;
997 port = rtp_get_local_port(rtsp_st->rtp_handle);
999 snprintf(transport, sizeof(transport) - 1,
1000 "%s/UDP;", trans_pref);
1001 if (rt->server_type != RTSP_SERVER_REAL)
1002 av_strlcat(transport, "unicast;", sizeof(transport));
1003 av_strlcatf(transport, sizeof(transport),
1004 "client_port=%d", port);
1005 if (rt->transport == RTSP_TRANSPORT_RTP &&
1006 !(rt->server_type == RTSP_SERVER_WMS && i > 0))
1007 av_strlcatf(transport, sizeof(transport), "-%d", port + 1);
1011 else if (lower_transport == RTSP_LOWER_TRANSPORT_TCP) {
1012 /** For WMS streams, the application streams are only used for
1013 * UDP. When trying to set it up for TCP streams, the server
1014 * will return an error. Therefore, we skip those streams. */
1015 if (rt->server_type == RTSP_SERVER_WMS &&
1016 s->streams[rtsp_st->stream_index]->codec->codec_type == CODEC_TYPE_DATA)
1018 snprintf(transport, sizeof(transport) - 1,
1019 "%s/TCP;", trans_pref);
1020 if (rt->server_type == RTSP_SERVER_WMS)
1021 av_strlcat(transport, "unicast;", sizeof(transport));
1022 av_strlcatf(transport, sizeof(transport),
1023 "interleaved=%d-%d",
1024 interleave, interleave + 1);
1028 else if (lower_transport == RTSP_LOWER_TRANSPORT_UDP_MULTICAST) {
1029 snprintf(transport, sizeof(transport) - 1,
1030 "%s/UDP;multicast", trans_pref);
1032 if (rt->server_type == RTSP_SERVER_REAL ||
1033 rt->server_type == RTSP_SERVER_WMS)
1034 av_strlcat(transport, ";mode=play", sizeof(transport));
1035 snprintf(cmd, sizeof(cmd),
1036 "SETUP %s RTSP/1.0\r\n"
1037 "Transport: %s\r\n",
1038 rtsp_st->control_url, transport);
1039 if (i == 0 && rt->server_type == RTSP_SERVER_REAL) {
1040 char real_res[41], real_csum[9];
1041 ff_rdt_calc_response_and_checksum(real_res, real_csum,
1043 av_strlcatf(cmd, sizeof(cmd),
1045 "RealChallenge2: %s, sd=%s\r\n",
1046 rt->session_id, real_res, real_csum);
1048 rtsp_send_cmd(s, cmd, reply, NULL);
1049 if (reply->status_code == 461 /* Unsupported protocol */ && i == 0) {
1052 } else if (reply->status_code != RTSP_STATUS_OK ||
1053 reply->nb_transports != 1) {
1054 err = AVERROR_INVALIDDATA;
1058 /* XXX: same protocol for all streams is required */
1060 if (reply->transports[0].lower_transport != rt->lower_transport ||
1061 reply->transports[0].transport != rt->transport) {
1062 err = AVERROR_INVALIDDATA;
1066 rt->lower_transport = reply->transports[0].lower_transport;
1067 rt->transport = reply->transports[0].transport;
1070 /* close RTP connection if not choosen */
1071 if (reply->transports[0].lower_transport != RTSP_LOWER_TRANSPORT_UDP &&
1072 (lower_transport == RTSP_LOWER_TRANSPORT_UDP)) {
1073 url_close(rtsp_st->rtp_handle);
1074 rtsp_st->rtp_handle = NULL;
1077 switch(reply->transports[0].lower_transport) {
1078 case RTSP_LOWER_TRANSPORT_TCP:
1079 rtsp_st->interleaved_min = reply->transports[0].interleaved_min;
1080 rtsp_st->interleaved_max = reply->transports[0].interleaved_max;
1083 case RTSP_LOWER_TRANSPORT_UDP:
1087 /* XXX: also use address if specified */
1088 snprintf(url, sizeof(url), "rtp://%s:%d",
1089 host, reply->transports[0].server_port_min);
1090 if (!(rt->server_type == RTSP_SERVER_WMS && i > 1) &&
1091 rtp_set_remote_url(rtsp_st->rtp_handle, url) < 0) {
1092 err = AVERROR_INVALIDDATA;
1097 case RTSP_LOWER_TRANSPORT_UDP_MULTICAST:
1102 in.s_addr = htonl(reply->transports[0].destination);
1103 snprintf(url, sizeof(url), "rtp://%s:%d?ttl=%d",
1105 reply->transports[0].port_min,
1106 reply->transports[0].ttl);
1107 if (url_open(&rtsp_st->rtp_handle, url, URL_RDWR) < 0) {
1108 err = AVERROR_INVALIDDATA;
1115 if ((err = rtsp_open_transport_ctx(s, rtsp_st)))
1119 if (rt->server_type == RTSP_SERVER_REAL)
1120 rt->need_subscription = 1;
1125 for (i=0; i<rt->nb_rtsp_streams; i++) {
1126 if (rt->rtsp_streams[i]->rtp_handle) {
1127 url_close(rt->rtsp_streams[i]->rtp_handle);
1128 rt->rtsp_streams[i]->rtp_handle = NULL;
1134 static int rtsp_read_header(AVFormatContext *s,
1135 AVFormatParameters *ap)
1137 RTSPState *rt = s->priv_data;
1138 char host[1024], path[1024], tcpname[1024], cmd[2048], *option_list, *option;
1139 URLContext *rtsp_hd;
1141 RTSPMessageHeader reply1, *reply = &reply1;
1142 unsigned char *content = NULL;
1143 int lower_transport_mask = 0;
1144 char real_challenge[64];
1146 /* extract hostname and port */
1147 url_split(NULL, 0, NULL, 0,
1148 host, sizeof(host), &port, path, sizeof(path), s->filename);
1150 port = RTSP_DEFAULT_PORT;
1152 /* search for options */
1153 option_list = strchr(path, '?');
1155 /* remove the options from the path */
1157 while(option_list) {
1158 /* move the option pointer */
1159 option = option_list;
1160 option_list = strchr(option_list, '&');
1162 *(option_list++) = 0;
1163 /* handle the options */
1164 if (strcmp(option, "udp") == 0)
1165 lower_transport_mask = (1<< RTSP_LOWER_TRANSPORT_UDP);
1166 else if (strcmp(option, "multicast") == 0)
1167 lower_transport_mask = (1<< RTSP_LOWER_TRANSPORT_UDP_MULTICAST);
1168 else if (strcmp(option, "tcp") == 0)
1169 lower_transport_mask = (1<< RTSP_LOWER_TRANSPORT_TCP);
1173 if (!lower_transport_mask)
1174 lower_transport_mask = (1 << RTSP_LOWER_TRANSPORT_NB) - 1;
1176 /* open the tcp connexion */
1177 snprintf(tcpname, sizeof(tcpname), "tcp://%s:%d", host, port);
1178 if (url_open(&rtsp_hd, tcpname, URL_RDWR) < 0)
1179 return AVERROR(EIO);
1180 rt->rtsp_hd = rtsp_hd;
1183 /* request options supported by the server; this also detects server type */
1184 for (rt->server_type = RTSP_SERVER_RTP;;) {
1185 snprintf(cmd, sizeof(cmd),
1186 "OPTIONS %s RTSP/1.0\r\n", s->filename);
1187 if (rt->server_type == RTSP_SERVER_REAL)
1190 * The following entries are required for proper
1191 * streaming from a Realmedia server. They are
1192 * interdependent in some way although we currently
1193 * don't quite understand how. Values were copied
1194 * from mplayer SVN r23589.
1195 * @param CompanyID is a 16-byte ID in base64
1196 * @param ClientChallenge is a 16-byte ID in hex
1198 "ClientChallenge: 9e26d33f2984236010ef6253fb1887f7\r\n"
1199 "PlayerStarttime: [28/03/2003:22:50:23 00:00]\r\n"
1200 "CompanyID: KnKV4M4I/B2FjJ1TToLycw==\r\n"
1201 "GUID: 00000000-0000-0000-0000-000000000000\r\n",
1203 rtsp_send_cmd(s, cmd, reply, NULL);
1204 if (reply->status_code != RTSP_STATUS_OK) {
1205 err = AVERROR_INVALIDDATA;
1209 /* detect server type if not standard-compliant RTP */
1210 if (rt->server_type != RTSP_SERVER_REAL && reply->real_challenge[0]) {
1211 rt->server_type = RTSP_SERVER_REAL;
1213 } else if (!strncasecmp(reply->server, "WMServer/", 9)) {
1214 rt->server_type = RTSP_SERVER_WMS;
1215 } else if (rt->server_type == RTSP_SERVER_REAL) {
1216 strcpy(real_challenge, reply->real_challenge);
1221 /* describe the stream */
1222 snprintf(cmd, sizeof(cmd),
1223 "DESCRIBE %s RTSP/1.0\r\n"
1224 "Accept: application/sdp\r\n",
1226 if (rt->server_type == RTSP_SERVER_REAL) {
1228 * The Require: attribute is needed for proper streaming from
1229 * Realmedia servers.
1232 "Require: com.real.retain-entity-for-setup\r\n",
1235 rtsp_send_cmd(s, cmd, reply, &content);
1237 err = AVERROR_INVALIDDATA;
1240 if (reply->status_code != RTSP_STATUS_OK) {
1241 err = AVERROR_INVALIDDATA;
1245 /* now we got the SDP description, we parse it */
1246 ret = sdp_parse(s, (const char *)content);
1249 err = AVERROR_INVALIDDATA;
1254 int lower_transport = ff_log2_tab[lower_transport_mask & ~(lower_transport_mask - 1)];
1256 err = make_setup_request(s, host, port, lower_transport,
1257 rt->server_type == RTSP_SERVER_REAL ?
1258 real_challenge : NULL);
1261 lower_transport_mask &= ~(1 << lower_transport);
1262 if (lower_transport_mask == 0 && err == 1) {
1263 err = AVERROR(FF_NETERROR(EPROTONOSUPPORT));
1268 rt->state = RTSP_STATE_IDLE;
1269 rt->seek_timestamp = 0; /* default is to start stream at position
1271 if (ap->initial_pause) {
1272 /* do not start immediately */
1274 if (rtsp_read_play(s) < 0) {
1275 err = AVERROR_INVALIDDATA;
1281 rtsp_close_streams(rt);
1283 url_close(rt->rtsp_hd);
1287 static int tcp_read_packet(AVFormatContext *s, RTSPStream **prtsp_st,
1288 uint8_t *buf, int buf_size)
1290 RTSPState *rt = s->priv_data;
1291 int id, len, i, ret;
1292 RTSPStream *rtsp_st;
1294 #ifdef DEBUG_RTP_TCP
1295 printf("tcp_read_packet:\n");
1299 RTSPMessageHeader reply;
1301 ret = rtsp_read_reply(s, &reply, NULL, 1);
1304 if (ret == 1) /* received '$' */
1306 /* XXX: parse message */
1308 ret = url_readbuf(rt->rtsp_hd, buf, 3);
1312 len = AV_RB16(buf + 1);
1313 #ifdef DEBUG_RTP_TCP
1314 printf("id=%d len=%d\n", id, len);
1316 if (len > buf_size || len < 12)
1319 ret = url_readbuf(rt->rtsp_hd, buf, len);
1322 if (rt->transport == RTSP_TRANSPORT_RDT &&
1323 ff_rdt_parse_header(buf, len, &id, NULL, NULL, NULL, NULL) < 0)
1326 /* find the matching stream */
1327 for(i = 0; i < rt->nb_rtsp_streams; i++) {
1328 rtsp_st = rt->rtsp_streams[i];
1329 if (id >= rtsp_st->interleaved_min &&
1330 id <= rtsp_st->interleaved_max)
1335 *prtsp_st = rtsp_st;
1339 static int udp_read_packet(AVFormatContext *s, RTSPStream **prtsp_st,
1340 uint8_t *buf, int buf_size)
1342 RTSPState *rt = s->priv_data;
1343 RTSPStream *rtsp_st;
1345 int fd, fd_max, n, i, ret, tcp_fd;
1349 if (url_interrupt_cb())
1350 return AVERROR(EINTR);
1352 tcp_fd = fd_max = url_get_file_handle(rt->rtsp_hd);
1353 FD_SET(tcp_fd, &rfds);
1354 for(i = 0; i < rt->nb_rtsp_streams; i++) {
1355 rtsp_st = rt->rtsp_streams[i];
1356 if (rtsp_st->rtp_handle) {
1357 /* currently, we cannot probe RTCP handle because of
1358 * blocking restrictions */
1359 fd = url_get_file_handle(rtsp_st->rtp_handle);
1366 tv.tv_usec = 100 * 1000;
1367 n = select(fd_max + 1, &rfds, NULL, NULL, &tv);
1369 for(i = 0; i < rt->nb_rtsp_streams; i++) {
1370 rtsp_st = rt->rtsp_streams[i];
1371 if (rtsp_st->rtp_handle) {
1372 fd = url_get_file_handle(rtsp_st->rtp_handle);
1373 if (FD_ISSET(fd, &rfds)) {
1374 ret = url_read(rtsp_st->rtp_handle, buf, buf_size);
1376 *prtsp_st = rtsp_st;
1382 if (FD_ISSET(tcp_fd, &rfds)) {
1383 RTSPMessageHeader reply;
1385 rtsp_read_reply(s, &reply, NULL, 0);
1386 /* XXX: parse message */
1392 static int rtsp_read_packet(AVFormatContext *s,
1395 RTSPState *rt = s->priv_data;
1396 RTSPStream *rtsp_st;
1398 uint8_t buf[10 * RTP_MAX_PACKET_LENGTH];
1400 if (rt->server_type == RTSP_SERVER_REAL) {
1402 RTSPMessageHeader reply1, *reply = &reply1;
1403 enum AVDiscard cache[MAX_STREAMS];
1406 for (i = 0; i < s->nb_streams; i++)
1407 cache[i] = s->streams[i]->discard;
1409 if (!rt->need_subscription) {
1410 if (memcmp (cache, rt->real_setup_cache,
1411 sizeof(enum AVDiscard) * s->nb_streams)) {
1412 av_strlcatf(cmd, sizeof(cmd),
1413 "SET_PARAMETER %s RTSP/1.0\r\n"
1414 "Unsubscribe: %s\r\n",
1415 s->filename, rt->last_subscription);
1416 rtsp_send_cmd(s, cmd, reply, NULL);
1417 if (reply->status_code != RTSP_STATUS_OK)
1418 return AVERROR_INVALIDDATA;
1419 rt->need_subscription = 1;
1423 if (rt->need_subscription) {
1424 int r, rule_nr, first = 1;
1426 memcpy(rt->real_setup_cache, cache,
1427 sizeof(enum AVDiscard) * s->nb_streams);
1428 rt->last_subscription[0] = 0;
1430 snprintf(cmd, sizeof(cmd),
1431 "SET_PARAMETER %s RTSP/1.0\r\n"
1434 for (i = 0; i < rt->nb_rtsp_streams; i++) {
1436 for (r = 0; r < s->nb_streams; r++) {
1437 if (s->streams[r]->priv_data == rt->rtsp_streams[i]) {
1438 if (s->streams[r]->discard != AVDISCARD_ALL) {
1440 av_strlcat(rt->last_subscription, ",",
1441 sizeof(rt->last_subscription));
1442 ff_rdt_subscribe_rule(
1443 rt->last_subscription,
1444 sizeof(rt->last_subscription), i, rule_nr);
1451 av_strlcatf(cmd, sizeof(cmd), "%s\r\n", rt->last_subscription);
1452 rtsp_send_cmd(s, cmd, reply, NULL);
1453 if (reply->status_code != RTSP_STATUS_OK)
1454 return AVERROR_INVALIDDATA;
1455 rt->need_subscription = 0;
1457 if (rt->state == RTSP_STATE_PLAYING)
1462 /* get next frames from the same RTP packet */
1463 if (rt->cur_transport_priv) {
1464 if (rt->transport == RTSP_TRANSPORT_RDT)
1465 ret = ff_rdt_parse_packet(rt->cur_transport_priv, pkt, NULL, 0);
1467 ret = rtp_parse_packet(rt->cur_transport_priv, pkt, NULL, 0);
1469 rt->cur_transport_priv = NULL;
1471 } else if (ret == 1) {
1474 rt->cur_transport_priv = NULL;
1478 /* read next RTP packet */
1480 switch(rt->lower_transport) {
1482 case RTSP_LOWER_TRANSPORT_TCP:
1483 len = tcp_read_packet(s, &rtsp_st, buf, sizeof(buf));
1485 case RTSP_LOWER_TRANSPORT_UDP:
1486 case RTSP_LOWER_TRANSPORT_UDP_MULTICAST:
1487 len = udp_read_packet(s, &rtsp_st, buf, sizeof(buf));
1488 if (len >=0 && rtsp_st->transport_priv && rt->transport == RTSP_TRANSPORT_RTP)
1489 rtp_check_and_send_back_rr(rtsp_st->transport_priv, len);
1494 if (rt->transport == RTSP_TRANSPORT_RDT)
1495 ret = ff_rdt_parse_packet(rtsp_st->transport_priv, pkt, buf, len);
1497 ret = rtp_parse_packet(rtsp_st->transport_priv, pkt, buf, len);
1501 /* more packets may follow, so we save the RTP context */
1502 rt->cur_transport_priv = rtsp_st->transport_priv;
1507 static int rtsp_read_play(AVFormatContext *s)
1509 RTSPState *rt = s->priv_data;
1510 RTSPMessageHeader reply1, *reply = &reply1;
1513 av_log(s, AV_LOG_DEBUG, "hello state=%d\n", rt->state);
1515 if (!(rt->server_type == RTSP_SERVER_REAL && rt->need_subscription)) {
1516 if (rt->state == RTSP_STATE_PAUSED) {
1517 snprintf(cmd, sizeof(cmd),
1518 "PLAY %s RTSP/1.0\r\n",
1521 snprintf(cmd, sizeof(cmd),
1522 "PLAY %s RTSP/1.0\r\n"
1523 "Range: npt=%0.3f-\r\n",
1525 (double)rt->seek_timestamp / AV_TIME_BASE);
1527 rtsp_send_cmd(s, cmd, reply, NULL);
1528 if (reply->status_code != RTSP_STATUS_OK) {
1532 rt->state = RTSP_STATE_PLAYING;
1536 /* pause the stream */
1537 static int rtsp_read_pause(AVFormatContext *s)
1539 RTSPState *rt = s->priv_data;
1540 RTSPMessageHeader reply1, *reply = &reply1;
1545 if (rt->state != RTSP_STATE_PLAYING)
1547 else if (!(rt->server_type == RTSP_SERVER_REAL && rt->need_subscription)) {
1548 snprintf(cmd, sizeof(cmd),
1549 "PAUSE %s RTSP/1.0\r\n",
1551 rtsp_send_cmd(s, cmd, reply, NULL);
1552 if (reply->status_code != RTSP_STATUS_OK) {
1556 rt->state = RTSP_STATE_PAUSED;
1560 static int rtsp_read_seek(AVFormatContext *s, int stream_index,
1561 int64_t timestamp, int flags)
1563 RTSPState *rt = s->priv_data;
1565 rt->seek_timestamp = av_rescale_q(timestamp, s->streams[stream_index]->time_base, AV_TIME_BASE_Q);
1568 case RTSP_STATE_IDLE:
1570 case RTSP_STATE_PLAYING:
1571 if (rtsp_read_play(s) != 0)
1574 case RTSP_STATE_PAUSED:
1575 rt->state = RTSP_STATE_IDLE;
1581 static int rtsp_read_close(AVFormatContext *s)
1583 RTSPState *rt = s->priv_data;
1584 RTSPMessageHeader reply1, *reply = &reply1;
1588 /* NOTE: it is valid to flush the buffer here */
1589 if (rt->lower_transport == RTSP_LOWER_TRANSPORT_TCP) {
1590 url_fclose(&rt->rtsp_gb);
1593 snprintf(cmd, sizeof(cmd),
1594 "TEARDOWN %s RTSP/1.0\r\n",
1596 rtsp_send_cmd(s, cmd, reply, NULL);
1598 rtsp_close_streams(rt);
1599 url_close(rt->rtsp_hd);
1603 #if CONFIG_RTSP_DEMUXER
1604 AVInputFormat rtsp_demuxer = {
1606 NULL_IF_CONFIG_SMALL("RTSP input format"),
1613 .flags = AVFMT_NOFILE,
1614 .read_play = rtsp_read_play,
1615 .read_pause = rtsp_read_pause,
1619 static int sdp_probe(AVProbeData *p1)
1621 const char *p = p1->buf, *p_end = p1->buf + p1->buf_size;
1623 /* we look for a line beginning "c=IN IP4" */
1624 while (p < p_end && *p != '\0') {
1625 if (p + sizeof("c=IN IP4") - 1 < p_end && av_strstart(p, "c=IN IP4", NULL))
1626 return AVPROBE_SCORE_MAX / 2;
1628 while(p < p_end - 1 && *p != '\n') p++;
1637 #define SDP_MAX_SIZE 8192
1639 static int sdp_read_header(AVFormatContext *s,
1640 AVFormatParameters *ap)
1642 RTSPState *rt = s->priv_data;
1643 RTSPStream *rtsp_st;
1648 /* read the whole sdp file */
1649 /* XXX: better loading */
1650 content = av_malloc(SDP_MAX_SIZE);
1651 size = get_buffer(s->pb, content, SDP_MAX_SIZE - 1);
1654 return AVERROR_INVALIDDATA;
1656 content[size] ='\0';
1658 sdp_parse(s, content);
1661 /* open each RTP stream */
1662 for(i=0;i<rt->nb_rtsp_streams;i++) {
1663 rtsp_st = rt->rtsp_streams[i];
1665 snprintf(url, sizeof(url), "rtp://%s:%d?localport=%d&ttl=%d",
1666 inet_ntoa(rtsp_st->sdp_ip),
1670 if (url_open(&rtsp_st->rtp_handle, url, URL_RDWR) < 0) {
1671 err = AVERROR_INVALIDDATA;
1674 if ((err = rtsp_open_transport_ctx(s, rtsp_st)))
1679 rtsp_close_streams(rt);
1683 static int sdp_read_packet(AVFormatContext *s,
1686 return rtsp_read_packet(s, pkt);
1689 static int sdp_read_close(AVFormatContext *s)
1691 RTSPState *rt = s->priv_data;
1692 rtsp_close_streams(rt);
1696 #if CONFIG_SDP_DEMUXER
1697 AVInputFormat sdp_demuxer = {
1699 NULL_IF_CONFIG_SMALL("SDP"),
1708 #if CONFIG_REDIR_DEMUXER
1709 /* dummy redirector format (used directly in av_open_input_file now) */
1710 static int redir_probe(AVProbeData *pd)
1715 if (av_strstart(p, "http://", NULL) ||
1716 av_strstart(p, "rtsp://", NULL))
1717 return AVPROBE_SCORE_MAX;
1721 static int redir_read_header(AVFormatContext *s, AVFormatParameters *ap)
1725 AVFormatContext *ic = NULL;
1726 ByteIOContext *f = s->pb;
1728 /* parse each URL and try to open it */
1730 while (c != URL_EOF) {
1733 if (!redir_isspace(c))
1742 if (c == URL_EOF || redir_isspace(c))
1744 if ((q - buf) < sizeof(buf) - 1)
1749 //printf("URL='%s'\n", buf);
1750 /* try to open the media file */
1751 if (av_open_input_file(&ic, buf, NULL, 0, NULL) == 0)
1755 return AVERROR(EIO);
1763 AVInputFormat redir_demuxer = {
1765 NULL_IF_CONFIG_SMALL("Redirector format"),