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/base64.h"
26 #include "libavutil/avstring.h"
27 #include "libavutil/intreadwrite.h"
32 #include <sys/select.h>
41 #include "rtp_vorbis.h"
44 //#define DEBUG_RTP_TCP
46 static int rtsp_read_reply(AVFormatContext *s, RTSPMessageHeader *reply,
47 unsigned char **content_ptr,
48 int return_on_interleaved_data);
50 #if LIBAVFORMAT_VERSION_INT < (53 << 16)
51 int rtsp_default_protocols = (1 << RTSP_LOWER_TRANSPORT_UDP);
54 #define SPACE_CHARS " \t\r\n"
55 /* we use memchr() instead of strchr() here because strchr() will return
56 * the terminating '\0' of SPACE_CHARS instead of NULL if c is '\0'. */
57 #define redir_isspace(c) memchr(SPACE_CHARS, c, 4)
58 static void skip_spaces(const char **pp)
62 while (redir_isspace(*p))
67 static void get_word_until_chars(char *buf, int buf_size,
68 const char *sep, const char **pp)
76 while (!strchr(sep, *p) && *p != '\0') {
77 if ((q - buf) < buf_size - 1)
86 static void get_word_sep(char *buf, int buf_size, const char *sep,
89 if (**pp == '/') (*pp)++;
90 get_word_until_chars(buf, buf_size, sep, pp);
93 static void get_word(char *buf, int buf_size, const char **pp)
95 get_word_until_chars(buf, buf_size, SPACE_CHARS, pp);
98 /* parse the rtpmap description: <codec_name>/<clock_rate>[/<other
100 static int sdp_parse_rtpmap(AVCodecContext *codec, RTSPStream *rtsp_st, int payload_type, const char *p)
107 /* Loop into AVRtpDynamicPayloadTypes[] and AVRtpPayloadTypes[] and
108 see if we can handle this kind of payload */
109 get_word_sep(buf, sizeof(buf), "/", &p);
110 if (payload_type >= RTP_PT_PRIVATE) {
111 RTPDynamicProtocolHandler *handler= RTPFirstDynamicPayloadHandler;
113 if (!strcasecmp(buf, handler->enc_name) && (codec->codec_type == handler->codec_type)) {
114 codec->codec_id = handler->codec_id;
115 rtsp_st->dynamic_handler= handler;
117 rtsp_st->dynamic_protocol_context= handler->open();
121 handler= handler->next;
124 /* We are in a standard case ( from http://www.iana.org/assignments/rtp-parameters) */
125 /* search into AVRtpPayloadTypes[] */
126 codec->codec_id = ff_rtp_codec_id(buf, codec->codec_type);
129 c = avcodec_find_decoder(codec->codec_id);
133 c_name = (char *)NULL;
136 get_word_sep(buf, sizeof(buf), "/", &p);
138 switch (codec->codec_type) {
139 case CODEC_TYPE_AUDIO:
140 av_log(codec, AV_LOG_DEBUG, " audio codec set to : %s\n", c_name);
141 codec->sample_rate = RTSP_DEFAULT_AUDIO_SAMPLERATE;
142 codec->channels = RTSP_DEFAULT_NB_AUDIO_CHANNELS;
144 codec->sample_rate = i;
145 get_word_sep(buf, sizeof(buf), "/", &p);
149 // TODO: there is a bug here; if it is a mono stream, and less than 22000Hz, faad upconverts to stereo and twice the
150 // frequency. No problem, but the sample rate is being set here by the sdp line. Upcoming patch forthcoming. (rdm)
152 av_log(codec, AV_LOG_DEBUG, " audio samplerate set to : %i\n", codec->sample_rate);
153 av_log(codec, AV_LOG_DEBUG, " audio channels set to : %i\n", codec->channels);
155 case CODEC_TYPE_VIDEO:
156 av_log(codec, AV_LOG_DEBUG, " video codec set to : %s\n", c_name);
167 /* return the length and optionnaly the data */
168 static int hex_to_data(uint8_t *data, const char *p)
178 c = toupper((unsigned char)*p++);
179 if (c >= '0' && c <= '9')
181 else if (c >= 'A' && c <= 'F')
196 static void sdp_parse_fmtp_config(AVCodecContext * codec, void *ctx,
197 char *attr, char *value)
199 switch (codec->codec_id) {
202 if (!strcmp(attr, "config")) {
203 /* decode the hexa encoded parameter */
204 int len = hex_to_data(NULL, value);
205 if (codec->extradata)
206 av_free(codec->extradata);
207 codec->extradata = av_mallocz(len + FF_INPUT_BUFFER_PADDING_SIZE);
208 if (!codec->extradata)
210 codec->extradata_size = len;
211 hex_to_data(codec->extradata, value);
214 case CODEC_ID_VORBIS:
215 ff_vorbis_parse_fmtp_config(codec, ctx, attr, value);
229 /* All known fmtp parmeters and the corresping RTPAttrTypeEnum */
230 #define ATTR_NAME_TYPE_INT 0
231 #define ATTR_NAME_TYPE_STR 1
232 static const AttrNameMap attr_names[]=
234 {"SizeLength", ATTR_NAME_TYPE_INT, offsetof(RTPPayloadData, sizelength)},
235 {"IndexLength", ATTR_NAME_TYPE_INT, offsetof(RTPPayloadData, indexlength)},
236 {"IndexDeltaLength", ATTR_NAME_TYPE_INT, offsetof(RTPPayloadData, indexdeltalength)},
237 {"profile-level-id", ATTR_NAME_TYPE_INT, offsetof(RTPPayloadData, profile_level_id)},
238 {"StreamType", ATTR_NAME_TYPE_INT, offsetof(RTPPayloadData, streamtype)},
239 {"mode", ATTR_NAME_TYPE_STR, offsetof(RTPPayloadData, mode)},
243 /** parse the attribute line from the fmtp a line of an sdp resonse. This is broken out as a function
244 * because it is used in rtp_h264.c, which is forthcoming.
246 int rtsp_next_attr_and_value(const char **p, char *attr, int attr_size, char *value, int value_size)
250 get_word_sep(attr, attr_size, "=", p);
253 get_word_sep(value, value_size, ";", p);
261 /* parse a SDP line and save stream attributes */
262 static void sdp_parse_fmtp(AVStream *st, const char *p)
265 /* Vorbis setup headers can be up to 12KB and are sent base64
266 * encoded, giving a 12KB * (4/3) = 16KB FMTP line. */
270 RTSPStream *rtsp_st = st->priv_data;
271 AVCodecContext *codec = st->codec;
272 RTPPayloadData *rtp_payload_data = &rtsp_st->rtp_payload_data;
274 /* loop on each attribute */
275 while(rtsp_next_attr_and_value(&p, attr, sizeof(attr), value, sizeof(value)))
277 /* grab the codec extra_data from the config parameter of the fmtp line */
278 sdp_parse_fmtp_config(codec, rtsp_st->dynamic_protocol_context,
280 /* Looking for a known attribute */
281 for (i = 0; attr_names[i].str; ++i) {
282 if (!strcasecmp(attr, attr_names[i].str)) {
283 if (attr_names[i].type == ATTR_NAME_TYPE_INT)
284 *(int *)((char *)rtp_payload_data + attr_names[i].offset) = atoi(value);
285 else if (attr_names[i].type == ATTR_NAME_TYPE_STR)
286 *(char **)((char *)rtp_payload_data + attr_names[i].offset) = av_strdup(value);
292 /** Parse a string \p in the form of Range:npt=xx-xx, and determine the start
294 * Used for seeking in the rtp stream.
296 static void rtsp_parse_range_npt(const char *p, int64_t *start, int64_t *end)
301 if (!av_stristart(p, "npt=", &p))
304 *start = AV_NOPTS_VALUE;
305 *end = AV_NOPTS_VALUE;
307 get_word_sep(buf, sizeof(buf), "-", &p);
308 *start = parse_date(buf, 1);
311 get_word_sep(buf, sizeof(buf), "-", &p);
312 *end = parse_date(buf, 1);
314 // av_log(NULL, AV_LOG_DEBUG, "Range Start: %lld\n", *start);
315 // av_log(NULL, AV_LOG_DEBUG, "Range End: %lld\n", *end);
318 typedef struct SDPParseState {
320 struct in_addr default_ip;
322 int skip_media; ///< set if an unknown m= line occurs
325 static void sdp_parse_line(AVFormatContext *s, SDPParseState *s1,
326 int letter, const char *buf)
328 RTSPState *rt = s->priv_data;
329 char buf1[64], st_type[64];
331 enum CodecType codec_type;
335 struct in_addr sdp_ip;
338 dprintf(s, "sdp: %c='%s'\n", letter, buf);
341 if (s1->skip_media && letter != 'm')
345 get_word(buf1, sizeof(buf1), &p);
346 if (strcmp(buf1, "IN") != 0)
348 get_word(buf1, sizeof(buf1), &p);
349 if (strcmp(buf1, "IP4") != 0)
351 get_word_sep(buf1, sizeof(buf1), "/", &p);
352 if (inet_aton(buf1, &sdp_ip) == 0)
357 get_word_sep(buf1, sizeof(buf1), "/", &p);
360 if (s->nb_streams == 0) {
361 s1->default_ip = sdp_ip;
362 s1->default_ttl = ttl;
364 st = s->streams[s->nb_streams - 1];
365 rtsp_st = st->priv_data;
366 rtsp_st->sdp_ip = sdp_ip;
367 rtsp_st->sdp_ttl = ttl;
371 av_metadata_set(&s->metadata, "title", p);
374 if (s->nb_streams == 0) {
375 av_metadata_set(&s->metadata, "comment", p);
382 get_word(st_type, sizeof(st_type), &p);
383 if (!strcmp(st_type, "audio")) {
384 codec_type = CODEC_TYPE_AUDIO;
385 } else if (!strcmp(st_type, "video")) {
386 codec_type = CODEC_TYPE_VIDEO;
387 } else if (!strcmp(st_type, "application")) {
388 codec_type = CODEC_TYPE_DATA;
393 rtsp_st = av_mallocz(sizeof(RTSPStream));
396 rtsp_st->stream_index = -1;
397 dynarray_add(&rt->rtsp_streams, &rt->nb_rtsp_streams, rtsp_st);
399 rtsp_st->sdp_ip = s1->default_ip;
400 rtsp_st->sdp_ttl = s1->default_ttl;
402 get_word(buf1, sizeof(buf1), &p); /* port */
403 rtsp_st->sdp_port = atoi(buf1);
405 get_word(buf1, sizeof(buf1), &p); /* protocol (ignored) */
407 /* XXX: handle list of formats */
408 get_word(buf1, sizeof(buf1), &p); /* format list */
409 rtsp_st->sdp_payload_type = atoi(buf1);
411 if (!strcmp(ff_rtp_enc_name(rtsp_st->sdp_payload_type), "MP2T")) {
412 /* no corresponding stream */
414 st = av_new_stream(s, 0);
417 st->priv_data = rtsp_st;
418 rtsp_st->stream_index = st->index;
419 st->codec->codec_type = codec_type;
420 if (rtsp_st->sdp_payload_type < RTP_PT_PRIVATE) {
421 /* if standard payload type, we can find the codec right now */
422 ff_rtp_get_codec_info(st->codec, rtsp_st->sdp_payload_type);
425 /* put a default control url */
426 av_strlcpy(rtsp_st->control_url, s->filename, sizeof(rtsp_st->control_url));
429 if (av_strstart(p, "control:", &p) && s->nb_streams > 0) {
431 /* get the control url */
432 st = s->streams[s->nb_streams - 1];
433 rtsp_st = st->priv_data;
435 /* XXX: may need to add full url resolution */
436 url_split(proto, sizeof(proto), NULL, 0, NULL, 0, NULL, NULL, 0, p);
437 if (proto[0] == '\0') {
438 /* relative control URL */
439 av_strlcat(rtsp_st->control_url, "/", sizeof(rtsp_st->control_url));
440 av_strlcat(rtsp_st->control_url, p, sizeof(rtsp_st->control_url));
442 av_strlcpy(rtsp_st->control_url, p, sizeof(rtsp_st->control_url));
444 } else if (av_strstart(p, "rtpmap:", &p) && s->nb_streams > 0) {
445 /* NOTE: rtpmap is only supported AFTER the 'm=' tag */
446 get_word(buf1, sizeof(buf1), &p);
447 payload_type = atoi(buf1);
448 st = s->streams[s->nb_streams - 1];
449 rtsp_st = st->priv_data;
450 sdp_parse_rtpmap(st->codec, rtsp_st, payload_type, p);
451 } else if (av_strstart(p, "fmtp:", &p)) {
452 /* NOTE: fmtp is only supported AFTER the 'a=rtpmap:xxx' tag */
453 get_word(buf1, sizeof(buf1), &p);
454 payload_type = atoi(buf1);
455 for(i = 0; i < s->nb_streams;i++) {
457 rtsp_st = st->priv_data;
458 if (rtsp_st->sdp_payload_type == payload_type) {
459 if(rtsp_st->dynamic_handler && rtsp_st->dynamic_handler->parse_sdp_a_line) {
460 if(!rtsp_st->dynamic_handler->parse_sdp_a_line(s, i, rtsp_st->dynamic_protocol_context, buf)) {
461 sdp_parse_fmtp(st, p);
464 sdp_parse_fmtp(st, p);
468 } else if(av_strstart(p, "framesize:", &p)) {
469 // let dynamic protocol handlers have a stab at the line.
470 get_word(buf1, sizeof(buf1), &p);
471 payload_type = atoi(buf1);
472 for(i = 0; i < s->nb_streams;i++) {
474 rtsp_st = st->priv_data;
475 if (rtsp_st->sdp_payload_type == payload_type) {
476 if(rtsp_st->dynamic_handler && rtsp_st->dynamic_handler->parse_sdp_a_line) {
477 rtsp_st->dynamic_handler->parse_sdp_a_line(s, i, rtsp_st->dynamic_protocol_context, buf);
481 } else if(av_strstart(p, "range:", &p)) {
484 // this is so that seeking on a streamed file can work.
485 rtsp_parse_range_npt(p, &start, &end);
486 s->start_time= start;
487 s->duration= (end==AV_NOPTS_VALUE)?AV_NOPTS_VALUE:end-start; // AV_NOPTS_VALUE means live broadcast (and can't seek)
488 } else if (av_strstart(p, "IsRealDataType:integer;",&p)) {
490 rt->transport = RTSP_TRANSPORT_RDT;
492 if (rt->server_type == RTSP_SERVER_WMS)
493 ff_wms_parse_sdp_a_line(s, p);
494 if (s->nb_streams > 0) {
495 if (rt->server_type == RTSP_SERVER_REAL)
496 ff_real_parse_sdp_a_line(s, s->nb_streams - 1, p);
498 rtsp_st = s->streams[s->nb_streams - 1]->priv_data;
499 if (rtsp_st->dynamic_handler &&
500 rtsp_st->dynamic_handler->parse_sdp_a_line)
501 rtsp_st->dynamic_handler->parse_sdp_a_line(s, s->nb_streams - 1,
502 rtsp_st->dynamic_protocol_context, buf);
509 static int sdp_parse(AVFormatContext *s, const char *content)
513 /* Some SDP lines, particularly for Realmedia or ASF RTSP streams,
514 * contain long SDP lines containing complete ASF Headers (several
515 * kB) or arrays of MDPR (RM stream descriptor) headers plus
516 * "rulebooks" describing their properties. Therefore, the SDP line
519 * The Vorbis FMTP line can be up to 16KB - see sdp_parse_fmtp. */
521 SDPParseState sdp_parse_state, *s1 = &sdp_parse_state;
523 memset(s1, 0, sizeof(SDPParseState));
534 /* get the content */
536 while (*p != '\n' && *p != '\r' && *p != '\0') {
537 if ((q - buf) < sizeof(buf) - 1)
542 sdp_parse_line(s, s1, letter, buf);
544 while (*p != '\n' && *p != '\0')
552 static int udp_read_packet(AVFormatContext *s, RTSPStream **prtsp_st,
553 uint8_t *buf, int buf_size)
555 RTSPState *rt = s->priv_data;
558 int fd, fd_max, n, i, ret, tcp_fd;
562 if (url_interrupt_cb())
563 return AVERROR(EINTR);
566 tcp_fd = fd_max = url_get_file_handle(rt->rtsp_hd);
567 FD_SET(tcp_fd, &rfds);
572 for(i = 0; i < rt->nb_rtsp_streams; i++) {
573 rtsp_st = rt->rtsp_streams[i];
574 if (rtsp_st->rtp_handle) {
575 /* currently, we cannot probe RTCP handle because of
576 * blocking restrictions */
577 fd = url_get_file_handle(rtsp_st->rtp_handle);
584 tv.tv_usec = 100 * 1000;
585 n = select(fd_max + 1, &rfds, NULL, NULL, &tv);
587 for(i = 0; i < rt->nb_rtsp_streams; i++) {
588 rtsp_st = rt->rtsp_streams[i];
589 if (rtsp_st->rtp_handle) {
590 fd = url_get_file_handle(rtsp_st->rtp_handle);
591 if (FD_ISSET(fd, &rfds)) {
592 ret = url_read(rtsp_st->rtp_handle, buf, buf_size);
600 if (FD_ISSET(tcp_fd, &rfds)) {
601 RTSPMessageHeader reply;
603 rtsp_read_reply(s, &reply, NULL, 0);
604 /* XXX: parse message */
605 if (rt->state != RTSP_STATE_PLAYING)
612 /* close and free RTSP streams */
613 static void rtsp_close_streams(RTSPState *rt)
618 for(i=0;i<rt->nb_rtsp_streams;i++) {
619 rtsp_st = rt->rtsp_streams[i];
621 if (rtsp_st->transport_priv) {
622 if (rt->transport == RTSP_TRANSPORT_RDT)
623 ff_rdt_parse_close(rtsp_st->transport_priv);
625 rtp_parse_close(rtsp_st->transport_priv);
627 if (rtsp_st->rtp_handle)
628 url_close(rtsp_st->rtp_handle);
629 if (rtsp_st->dynamic_handler && rtsp_st->dynamic_protocol_context)
630 rtsp_st->dynamic_handler->close(rtsp_st->dynamic_protocol_context);
633 av_free(rt->rtsp_streams);
635 av_close_input_stream (rt->asf_ctx);
638 av_freep(&rt->auth_b64);
642 rtsp_open_transport_ctx(AVFormatContext *s, RTSPStream *rtsp_st)
644 RTSPState *rt = s->priv_data;
647 /* open the RTP context */
648 if (rtsp_st->stream_index >= 0)
649 st = s->streams[rtsp_st->stream_index];
651 s->ctx_flags |= AVFMTCTX_NOHEADER;
653 if (rt->transport == RTSP_TRANSPORT_RDT)
654 rtsp_st->transport_priv = ff_rdt_parse_open(s, st->index,
655 rtsp_st->dynamic_protocol_context,
656 rtsp_st->dynamic_handler);
658 rtsp_st->transport_priv = rtp_parse_open(s, st, rtsp_st->rtp_handle,
659 rtsp_st->sdp_payload_type,
660 &rtsp_st->rtp_payload_data);
662 if (!rtsp_st->transport_priv) {
663 return AVERROR(ENOMEM);
664 } else if (rt->transport != RTSP_TRANSPORT_RDT) {
665 if(rtsp_st->dynamic_handler) {
666 rtp_parse_set_dynamic_protocol(rtsp_st->transport_priv,
667 rtsp_st->dynamic_protocol_context,
668 rtsp_st->dynamic_handler);
675 static int rtsp_probe(AVProbeData *p)
677 if (av_strstart(p->filename, "rtsp:", NULL))
678 return AVPROBE_SCORE_MAX;
682 static void rtsp_parse_range(int *min_ptr, int *max_ptr, const char **pp)
689 v = strtol(p, (char **)&p, 10);
693 v = strtol(p, (char **)&p, 10);
702 /* XXX: only one transport specification is parsed */
703 static void rtsp_parse_transport(RTSPMessageHeader *reply, const char *p)
705 char transport_protocol[16];
707 char lower_transport[16];
709 RTSPTransportField *th;
712 reply->nb_transports = 0;
719 th = &reply->transports[reply->nb_transports];
721 get_word_sep(transport_protocol, sizeof(transport_protocol),
723 if (!strcasecmp (transport_protocol, "rtp")) {
724 get_word_sep(profile, sizeof(profile), "/;,", &p);
725 lower_transport[0] = '\0';
726 /* rtp/avp/<protocol> */
728 get_word_sep(lower_transport, sizeof(lower_transport),
731 th->transport = RTSP_TRANSPORT_RTP;
732 } else if (!strcasecmp (transport_protocol, "x-pn-tng") ||
733 !strcasecmp (transport_protocol, "x-real-rdt")) {
734 /* x-pn-tng/<protocol> */
735 get_word_sep(lower_transport, sizeof(lower_transport), "/;,", &p);
737 th->transport = RTSP_TRANSPORT_RDT;
739 if (!strcasecmp(lower_transport, "TCP"))
740 th->lower_transport = RTSP_LOWER_TRANSPORT_TCP;
742 th->lower_transport = RTSP_LOWER_TRANSPORT_UDP;
746 /* get each parameter */
747 while (*p != '\0' && *p != ',') {
748 get_word_sep(parameter, sizeof(parameter), "=;,", &p);
749 if (!strcmp(parameter, "port")) {
752 rtsp_parse_range(&th->port_min, &th->port_max, &p);
754 } else if (!strcmp(parameter, "client_port")) {
757 rtsp_parse_range(&th->client_port_min,
758 &th->client_port_max, &p);
760 } else if (!strcmp(parameter, "server_port")) {
763 rtsp_parse_range(&th->server_port_min,
764 &th->server_port_max, &p);
766 } else if (!strcmp(parameter, "interleaved")) {
769 rtsp_parse_range(&th->interleaved_min,
770 &th->interleaved_max, &p);
772 } else if (!strcmp(parameter, "multicast")) {
773 if (th->lower_transport == RTSP_LOWER_TRANSPORT_UDP)
774 th->lower_transport = RTSP_LOWER_TRANSPORT_UDP_MULTICAST;
775 } else if (!strcmp(parameter, "ttl")) {
778 th->ttl = strtol(p, (char **)&p, 10);
780 } else if (!strcmp(parameter, "destination")) {
781 struct in_addr ipaddr;
785 get_word_sep(buf, sizeof(buf), ";,", &p);
786 if (inet_aton(buf, &ipaddr))
787 th->destination = ntohl(ipaddr.s_addr);
790 while (*p != ';' && *p != '\0' && *p != ',')
798 reply->nb_transports++;
802 void rtsp_parse_line(RTSPMessageHeader *reply, const char *buf)
806 /* NOTE: we do case independent match for broken servers */
808 if (av_stristart(p, "Session:", &p)) {
810 get_word_sep(reply->session_id, sizeof(reply->session_id), ";", &p);
811 if (av_stristart(p, ";timeout=", &p) &&
812 (t = strtol(p, NULL, 10)) > 0) {
815 } else if (av_stristart(p, "Content-Length:", &p)) {
816 reply->content_length = strtol(p, NULL, 10);
817 } else if (av_stristart(p, "Transport:", &p)) {
818 rtsp_parse_transport(reply, p);
819 } else if (av_stristart(p, "CSeq:", &p)) {
820 reply->seq = strtol(p, NULL, 10);
821 } else if (av_stristart(p, "Range:", &p)) {
822 rtsp_parse_range_npt(p, &reply->range_start, &reply->range_end);
823 } else if (av_stristart(p, "RealChallenge1:", &p)) {
825 av_strlcpy(reply->real_challenge, p, sizeof(reply->real_challenge));
826 } else if (av_stristart(p, "Server:", &p)) {
828 av_strlcpy(reply->server, p, sizeof(reply->server));
829 } else if (av_stristart(p, "Notice:", &p) ||
830 av_stristart(p, "X-Notice:", &p)) {
831 reply->notice = strtol(p, NULL, 10);
832 } else if (av_stristart(p, "Location:", &p)) {
834 av_strlcpy(reply->location, p , sizeof(reply->location));
838 /* skip a RTP/TCP interleaved packet */
839 static void rtsp_skip_packet(AVFormatContext *s)
841 RTSPState *rt = s->priv_data;
845 ret = url_read_complete(rt->rtsp_hd, buf, 3);
848 len = AV_RB16(buf + 1);
850 dprintf(s, "skipping RTP packet len=%d\n", len);
855 if (len1 > sizeof(buf))
857 ret = url_read_complete(rt->rtsp_hd, buf, len1);
865 * Read a RTSP message from the server, or prepare to read data
866 * packets if we're reading data interleaved over the TCP/RTSP
867 * connection as well.
869 * @param s RTSP demuxer context
870 * @param reply pointer where the RTSP message header will be stored
871 * @param content_ptr pointer where the RTSP message body, if any, will
872 * be stored (length is in \p reply)
873 * @param return_on_interleaved_data whether the function may return if we
874 * encounter a data marker ('$'), which precedes data
875 * packets over interleaved TCP/RTSP connections. If this
876 * is set, this function will return 1 after encountering
877 * a '$'. If it is not set, the function will skip any
878 * data packets (if they are encountered), until a reply
879 * has been fully parsed. If no more data is available
880 * without parsing a reply, it will return an error.
882 * @returns 1 if a data packets is ready to be received, -1 on error,
886 rtsp_read_reply (AVFormatContext *s, RTSPMessageHeader *reply,
887 unsigned char **content_ptr, int return_on_interleaved_data)
889 RTSPState *rt = s->priv_data;
890 char buf[4096], buf1[1024], *q;
893 int ret, content_length, line_count = 0;
894 unsigned char *content = NULL;
896 memset(reply, 0, sizeof(*reply));
898 /* parse reply (XXX: use buffers) */
899 rt->last_reply[0] = '\0';
903 ret = url_read_complete(rt->rtsp_hd, &ch, 1);
905 dprintf(s, "ret=%d c=%02x [%c]\n", ret, ch, ch);
912 /* XXX: only parse it if first char on line ? */
913 if (return_on_interleaved_data) {
917 } else if (ch != '\r') {
918 if ((q - buf) < sizeof(buf) - 1)
924 dprintf(s, "line='%s'\n", buf);
926 /* test if last line */
930 if (line_count == 0) {
932 get_word(buf1, sizeof(buf1), &p);
933 get_word(buf1, sizeof(buf1), &p);
934 reply->status_code = atoi(buf1);
936 rtsp_parse_line(reply, p);
937 av_strlcat(rt->last_reply, p, sizeof(rt->last_reply));
938 av_strlcat(rt->last_reply, "\n", sizeof(rt->last_reply));
943 if (rt->session_id[0] == '\0' && reply->session_id[0] != '\0')
944 av_strlcpy(rt->session_id, reply->session_id, sizeof(rt->session_id));
946 content_length = reply->content_length;
947 if (content_length > 0) {
948 /* leave some room for a trailing '\0' (useful for simple parsing) */
949 content = av_malloc(content_length + 1);
950 (void)url_read_complete(rt->rtsp_hd, content, content_length);
951 content[content_length] = '\0';
954 *content_ptr = content;
959 if (reply->notice == 2101 /* End-of-Stream Reached */ ||
960 reply->notice == 2104 /* Start-of-Stream Reached */ ||
961 reply->notice == 2306 /* Continuous Feed Terminated */)
962 rt->state = RTSP_STATE_IDLE;
963 else if (reply->notice >= 4400 && reply->notice < 5500)
964 return AVERROR(EIO); /* data or server error */
965 else if (reply->notice == 2401 /* Ticket Expired */ ||
966 (reply->notice >= 5500 && reply->notice < 5600) /* end of term */ )
967 return AVERROR(EPERM);
972 static void rtsp_send_cmd_async (AVFormatContext *s,
973 const char *cmd, RTSPMessageHeader *reply,
974 unsigned char **content_ptr)
976 RTSPState *rt = s->priv_data;
977 char buf[4096], buf1[1024];
980 av_strlcpy(buf, cmd, sizeof(buf));
981 snprintf(buf1, sizeof(buf1), "CSeq: %d\r\n", rt->seq);
982 av_strlcat(buf, buf1, sizeof(buf));
983 if (rt->session_id[0] != '\0' && !strstr(cmd, "\nIf-Match:")) {
984 snprintf(buf1, sizeof(buf1), "Session: %s\r\n", rt->session_id);
985 av_strlcat(buf, buf1, sizeof(buf));
988 av_strlcatf(buf, sizeof(buf),
989 "Authorization: Basic %s\r\n",
991 av_strlcat(buf, "\r\n", sizeof(buf));
993 dprintf(s, "Sending:\n%s--\n", buf);
995 url_write(rt->rtsp_hd, buf, strlen(buf));
996 rt->last_cmd_time = av_gettime();
999 static void rtsp_send_cmd (AVFormatContext *s,
1000 const char *cmd, RTSPMessageHeader *reply,
1001 unsigned char **content_ptr)
1003 rtsp_send_cmd_async(s, cmd, reply, content_ptr);
1005 rtsp_read_reply(s, reply, content_ptr, 0);
1009 * @returns 0 on success, <0 on error, 1 if protocol is unavailable.
1012 make_setup_request (AVFormatContext *s, const char *host, int port,
1013 int lower_transport, const char *real_challenge)
1015 RTSPState *rt = s->priv_data;
1016 int rtx, j, i, err, interleave = 0;
1017 RTSPStream *rtsp_st;
1018 RTSPMessageHeader reply1, *reply = &reply1;
1020 const char *trans_pref;
1022 if (rt->transport == RTSP_TRANSPORT_RDT)
1023 trans_pref = "x-pn-tng";
1025 trans_pref = "RTP/AVP";
1027 /* default timeout: 1 minute */
1030 /* for each stream, make the setup request */
1031 /* XXX: we assume the same server is used for the control of each
1034 for(j = RTSP_RTP_PORT_MIN, i = 0; i < rt->nb_rtsp_streams; ++i) {
1035 char transport[2048];
1038 * WMS serves all UDP data over a single connection, the RTX, which
1039 * isn't necessarily the first in the SDP but has to be the first
1040 * to be set up, else the second/third SETUP will fail with a 461.
1042 if (lower_transport == RTSP_LOWER_TRANSPORT_UDP &&
1043 rt->server_type == RTSP_SERVER_WMS) {
1046 for (rtx = 0; rtx < rt->nb_rtsp_streams; rtx++) {
1047 int len = strlen(rt->rtsp_streams[rtx]->control_url);
1049 !strcmp(rt->rtsp_streams[rtx]->control_url + len - 4, "/rtx"))
1052 if (rtx == rt->nb_rtsp_streams)
1053 return -1; /* no RTX found */
1054 rtsp_st = rt->rtsp_streams[rtx];
1056 rtsp_st = rt->rtsp_streams[i > rtx ? i : i - 1];
1058 rtsp_st = rt->rtsp_streams[i];
1061 if (lower_transport == RTSP_LOWER_TRANSPORT_UDP) {
1064 if (rt->server_type == RTSP_SERVER_WMS && i > 1) {
1065 port = reply->transports[0].client_port_min;
1069 /* first try in specified port range */
1070 if (RTSP_RTP_PORT_MIN != 0) {
1071 while(j <= RTSP_RTP_PORT_MAX) {
1072 snprintf(buf, sizeof(buf), "rtp://%s?localport=%d", host, j);
1073 j += 2; /* we will use two port by rtp stream (rtp and rtcp) */
1074 if (url_open(&rtsp_st->rtp_handle, buf, URL_RDWR) == 0) {
1080 /* then try on any port
1081 ** if (url_open(&rtsp_st->rtp_handle, "rtp://", URL_RDONLY) < 0) {
1082 ** err = AVERROR_INVALIDDATA;
1088 port = rtp_get_local_port(rtsp_st->rtp_handle);
1090 snprintf(transport, sizeof(transport) - 1,
1091 "%s/UDP;", trans_pref);
1092 if (rt->server_type != RTSP_SERVER_REAL)
1093 av_strlcat(transport, "unicast;", sizeof(transport));
1094 av_strlcatf(transport, sizeof(transport),
1095 "client_port=%d", port);
1096 if (rt->transport == RTSP_TRANSPORT_RTP &&
1097 !(rt->server_type == RTSP_SERVER_WMS && i > 0))
1098 av_strlcatf(transport, sizeof(transport), "-%d", port + 1);
1102 else if (lower_transport == RTSP_LOWER_TRANSPORT_TCP) {
1103 /** For WMS streams, the application streams are only used for
1104 * UDP. When trying to set it up for TCP streams, the server
1105 * will return an error. Therefore, we skip those streams. */
1106 if (rt->server_type == RTSP_SERVER_WMS &&
1107 s->streams[rtsp_st->stream_index]->codec->codec_type == CODEC_TYPE_DATA)
1109 snprintf(transport, sizeof(transport) - 1,
1110 "%s/TCP;", trans_pref);
1111 if (rt->server_type == RTSP_SERVER_WMS)
1112 av_strlcat(transport, "unicast;", sizeof(transport));
1113 av_strlcatf(transport, sizeof(transport),
1114 "interleaved=%d-%d",
1115 interleave, interleave + 1);
1119 else if (lower_transport == RTSP_LOWER_TRANSPORT_UDP_MULTICAST) {
1120 snprintf(transport, sizeof(transport) - 1,
1121 "%s/UDP;multicast", trans_pref);
1123 if (rt->server_type == RTSP_SERVER_REAL ||
1124 rt->server_type == RTSP_SERVER_WMS)
1125 av_strlcat(transport, ";mode=play", sizeof(transport));
1126 snprintf(cmd, sizeof(cmd),
1127 "SETUP %s RTSP/1.0\r\n"
1128 "Transport: %s\r\n",
1129 rtsp_st->control_url, transport);
1130 if (i == 0 && rt->server_type == RTSP_SERVER_REAL) {
1131 char real_res[41], real_csum[9];
1132 ff_rdt_calc_response_and_checksum(real_res, real_csum,
1134 av_strlcatf(cmd, sizeof(cmd),
1136 "RealChallenge2: %s, sd=%s\r\n",
1137 rt->session_id, real_res, real_csum);
1139 rtsp_send_cmd(s, cmd, reply, NULL);
1140 if (reply->status_code == 461 /* Unsupported protocol */ && i == 0) {
1143 } else if (reply->status_code != RTSP_STATUS_OK ||
1144 reply->nb_transports != 1) {
1145 err = AVERROR_INVALIDDATA;
1149 /* XXX: same protocol for all streams is required */
1151 if (reply->transports[0].lower_transport != rt->lower_transport ||
1152 reply->transports[0].transport != rt->transport) {
1153 err = AVERROR_INVALIDDATA;
1157 rt->lower_transport = reply->transports[0].lower_transport;
1158 rt->transport = reply->transports[0].transport;
1161 /* close RTP connection if not choosen */
1162 if (reply->transports[0].lower_transport != RTSP_LOWER_TRANSPORT_UDP &&
1163 (lower_transport == RTSP_LOWER_TRANSPORT_UDP)) {
1164 url_close(rtsp_st->rtp_handle);
1165 rtsp_st->rtp_handle = NULL;
1168 switch(reply->transports[0].lower_transport) {
1169 case RTSP_LOWER_TRANSPORT_TCP:
1170 rtsp_st->interleaved_min = reply->transports[0].interleaved_min;
1171 rtsp_st->interleaved_max = reply->transports[0].interleaved_max;
1174 case RTSP_LOWER_TRANSPORT_UDP:
1178 /* XXX: also use address if specified */
1179 snprintf(url, sizeof(url), "rtp://%s:%d",
1180 host, reply->transports[0].server_port_min);
1181 if (!(rt->server_type == RTSP_SERVER_WMS && i > 1) &&
1182 rtp_set_remote_url(rtsp_st->rtp_handle, url) < 0) {
1183 err = AVERROR_INVALIDDATA;
1188 case RTSP_LOWER_TRANSPORT_UDP_MULTICAST:
1194 if (reply->transports[0].destination) {
1195 in.s_addr = htonl(reply->transports[0].destination);
1196 port = reply->transports[0].port_min;
1197 ttl = reply->transports[0].ttl;
1199 in = rtsp_st->sdp_ip;
1200 port = rtsp_st->sdp_port;
1201 ttl = rtsp_st->sdp_ttl;
1203 snprintf(url, sizeof(url), "rtp://%s:%d?ttl=%d",
1204 inet_ntoa(in), port, ttl);
1205 if (url_open(&rtsp_st->rtp_handle, url, URL_RDWR) < 0) {
1206 err = AVERROR_INVALIDDATA;
1213 if ((err = rtsp_open_transport_ctx(s, rtsp_st)))
1217 if (reply->timeout > 0)
1218 rt->timeout = reply->timeout;
1220 if (rt->server_type == RTSP_SERVER_REAL)
1221 rt->need_subscription = 1;
1226 for (i=0; i<rt->nb_rtsp_streams; i++) {
1227 if (rt->rtsp_streams[i]->rtp_handle) {
1228 url_close(rt->rtsp_streams[i]->rtp_handle);
1229 rt->rtsp_streams[i]->rtp_handle = NULL;
1235 static int rtsp_read_play(AVFormatContext *s)
1237 RTSPState *rt = s->priv_data;
1238 RTSPMessageHeader reply1, *reply = &reply1;
1241 av_log(s, AV_LOG_DEBUG, "hello state=%d\n", rt->state);
1243 if (!(rt->server_type == RTSP_SERVER_REAL && rt->need_subscription)) {
1244 if (rt->state == RTSP_STATE_PAUSED) {
1245 snprintf(cmd, sizeof(cmd),
1246 "PLAY %s RTSP/1.0\r\n",
1249 snprintf(cmd, sizeof(cmd),
1250 "PLAY %s RTSP/1.0\r\n"
1251 "Range: npt=%0.3f-\r\n",
1253 (double)rt->seek_timestamp / AV_TIME_BASE);
1255 rtsp_send_cmd(s, cmd, reply, NULL);
1256 if (reply->status_code != RTSP_STATUS_OK) {
1260 rt->state = RTSP_STATE_PLAYING;
1264 static int rtsp_read_header(AVFormatContext *s,
1265 AVFormatParameters *ap)
1267 RTSPState *rt = s->priv_data;
1268 char host[1024], path[1024], tcpname[1024], cmd[2048], auth[128];
1269 char *option_list, *option, *filename;
1270 URLContext *rtsp_hd;
1272 RTSPMessageHeader reply1, *reply = &reply1;
1273 unsigned char *content = NULL;
1274 int lower_transport_mask = 0;
1275 char real_challenge[64];
1277 /* extract hostname and port */
1278 url_split(NULL, 0, auth, sizeof(auth),
1279 host, sizeof(host), &port, path, sizeof(path), s->filename);
1281 int auth_len = strlen(auth), b64_len = ((auth_len + 2) / 3) * 4 + 1;
1283 if (!(rt->auth_b64 = av_malloc(b64_len)))
1284 return AVERROR(ENOMEM);
1285 if (!av_base64_encode(rt->auth_b64, b64_len, auth, auth_len)) {
1286 err = AVERROR(EINVAL);
1291 port = RTSP_DEFAULT_PORT;
1293 /* search for options */
1294 option_list = strchr(path, '?');
1296 filename = strchr(s->filename, '?');
1297 while(option_list) {
1298 /* move the option pointer */
1299 option = ++option_list;
1300 option_list = strchr(option_list, '&');
1304 /* handle the options */
1305 if (strcmp(option, "udp") == 0)
1306 lower_transport_mask = (1<< RTSP_LOWER_TRANSPORT_UDP);
1307 else if (strcmp(option, "multicast") == 0)
1308 lower_transport_mask = (1<< RTSP_LOWER_TRANSPORT_UDP_MULTICAST);
1309 else if (strcmp(option, "tcp") == 0)
1310 lower_transport_mask = (1<< RTSP_LOWER_TRANSPORT_TCP);
1312 strcpy(++filename, option);
1313 filename += strlen(option);
1314 if (option_list) *filename = '&';
1320 if (!lower_transport_mask)
1321 lower_transport_mask = (1 << RTSP_LOWER_TRANSPORT_NB) - 1;
1323 /* open the tcp connexion */
1324 snprintf(tcpname, sizeof(tcpname), "tcp://%s:%d", host, port);
1325 if (url_open(&rtsp_hd, tcpname, URL_RDWR) < 0) {
1329 rt->rtsp_hd = rtsp_hd;
1332 /* request options supported by the server; this also detects server type */
1333 for (rt->server_type = RTSP_SERVER_RTP;;) {
1334 snprintf(cmd, sizeof(cmd),
1335 "OPTIONS %s RTSP/1.0\r\n", s->filename);
1336 if (rt->server_type == RTSP_SERVER_REAL)
1339 * The following entries are required for proper
1340 * streaming from a Realmedia server. They are
1341 * interdependent in some way although we currently
1342 * don't quite understand how. Values were copied
1343 * from mplayer SVN r23589.
1344 * @param CompanyID is a 16-byte ID in base64
1345 * @param ClientChallenge is a 16-byte ID in hex
1347 "ClientChallenge: 9e26d33f2984236010ef6253fb1887f7\r\n"
1348 "PlayerStarttime: [28/03/2003:22:50:23 00:00]\r\n"
1349 "CompanyID: KnKV4M4I/B2FjJ1TToLycw==\r\n"
1350 "GUID: 00000000-0000-0000-0000-000000000000\r\n",
1352 rtsp_send_cmd(s, cmd, reply, NULL);
1353 if (reply->status_code != RTSP_STATUS_OK) {
1354 err = AVERROR_INVALIDDATA;
1358 /* detect server type if not standard-compliant RTP */
1359 if (rt->server_type != RTSP_SERVER_REAL && reply->real_challenge[0]) {
1360 rt->server_type = RTSP_SERVER_REAL;
1362 } else if (!strncasecmp(reply->server, "WMServer/", 9)) {
1363 rt->server_type = RTSP_SERVER_WMS;
1364 } else if (rt->server_type == RTSP_SERVER_REAL) {
1365 strcpy(real_challenge, reply->real_challenge);
1370 /* describe the stream */
1371 snprintf(cmd, sizeof(cmd),
1372 "DESCRIBE %s RTSP/1.0\r\n"
1373 "Accept: application/sdp\r\n",
1375 if (rt->server_type == RTSP_SERVER_REAL) {
1377 * The Require: attribute is needed for proper streaming from
1378 * Realmedia servers.
1381 "Require: com.real.retain-entity-for-setup\r\n",
1384 rtsp_send_cmd(s, cmd, reply, &content);
1386 err = AVERROR_INVALIDDATA;
1389 if (reply->status_code != RTSP_STATUS_OK) {
1390 err = AVERROR_INVALIDDATA;
1394 /* now we got the SDP description, we parse it */
1395 ret = sdp_parse(s, (const char *)content);
1398 err = AVERROR_INVALIDDATA;
1403 int lower_transport = ff_log2_tab[lower_transport_mask & ~(lower_transport_mask - 1)];
1405 err = make_setup_request(s, host, port, lower_transport,
1406 rt->server_type == RTSP_SERVER_REAL ?
1407 real_challenge : NULL);
1410 lower_transport_mask &= ~(1 << lower_transport);
1411 if (lower_transport_mask == 0 && err == 1) {
1412 err = AVERROR(FF_NETERROR(EPROTONOSUPPORT));
1417 rt->state = RTSP_STATE_IDLE;
1418 rt->seek_timestamp = 0; /* default is to start stream at position
1420 if (ap->initial_pause) {
1421 /* do not start immediately */
1423 if (rtsp_read_play(s) < 0) {
1424 err = AVERROR_INVALIDDATA;
1430 rtsp_close_streams(rt);
1432 url_close(rt->rtsp_hd);
1433 if (reply->status_code >=300 && reply->status_code < 400) {
1434 av_strlcpy(s->filename, reply->location, sizeof(s->filename));
1435 av_log(s, AV_LOG_INFO, "Status %d: Redirecting to %s\n",
1443 static int tcp_read_packet(AVFormatContext *s, RTSPStream **prtsp_st,
1444 uint8_t *buf, int buf_size)
1446 RTSPState *rt = s->priv_data;
1447 int id, len, i, ret;
1448 RTSPStream *rtsp_st;
1450 #ifdef DEBUG_RTP_TCP
1451 dprintf(s, "tcp_read_packet:\n");
1455 RTSPMessageHeader reply;
1457 ret = rtsp_read_reply(s, &reply, NULL, 1);
1460 if (ret == 1) /* received '$' */
1462 /* XXX: parse message */
1463 if (rt->state != RTSP_STATE_PLAYING)
1466 ret = url_read_complete(rt->rtsp_hd, buf, 3);
1470 len = AV_RB16(buf + 1);
1471 #ifdef DEBUG_RTP_TCP
1472 dprintf(s, "id=%d len=%d\n", id, len);
1474 if (len > buf_size || len < 12)
1477 ret = url_read_complete(rt->rtsp_hd, buf, len);
1480 if (rt->transport == RTSP_TRANSPORT_RDT &&
1481 ff_rdt_parse_header(buf, len, &id, NULL, NULL, NULL, NULL) < 0)
1484 /* find the matching stream */
1485 for(i = 0; i < rt->nb_rtsp_streams; i++) {
1486 rtsp_st = rt->rtsp_streams[i];
1487 if (id >= rtsp_st->interleaved_min &&
1488 id <= rtsp_st->interleaved_max)
1493 *prtsp_st = rtsp_st;
1497 static int rtsp_read_packet(AVFormatContext *s,
1500 RTSPState *rt = s->priv_data;
1501 RTSPStream *rtsp_st;
1503 uint8_t buf[10 * RTP_MAX_PACKET_LENGTH];
1504 RTSPMessageHeader reply1, *reply = &reply1;
1507 if (rt->server_type == RTSP_SERVER_REAL) {
1509 enum AVDiscard cache[MAX_STREAMS];
1511 for (i = 0; i < s->nb_streams; i++)
1512 cache[i] = s->streams[i]->discard;
1514 if (!rt->need_subscription) {
1515 if (memcmp (cache, rt->real_setup_cache,
1516 sizeof(enum AVDiscard) * s->nb_streams)) {
1517 av_strlcatf(cmd, sizeof(cmd),
1518 "SET_PARAMETER %s RTSP/1.0\r\n"
1519 "Unsubscribe: %s\r\n",
1520 s->filename, rt->last_subscription);
1521 rtsp_send_cmd(s, cmd, reply, NULL);
1522 if (reply->status_code != RTSP_STATUS_OK)
1523 return AVERROR_INVALIDDATA;
1524 rt->need_subscription = 1;
1528 if (rt->need_subscription) {
1529 int r, rule_nr, first = 1;
1531 memcpy(rt->real_setup_cache, cache,
1532 sizeof(enum AVDiscard) * s->nb_streams);
1533 rt->last_subscription[0] = 0;
1535 snprintf(cmd, sizeof(cmd),
1536 "SET_PARAMETER %s RTSP/1.0\r\n"
1539 for (i = 0; i < rt->nb_rtsp_streams; i++) {
1541 for (r = 0; r < s->nb_streams; r++) {
1542 if (s->streams[r]->priv_data == rt->rtsp_streams[i]) {
1543 if (s->streams[r]->discard != AVDISCARD_ALL) {
1545 av_strlcat(rt->last_subscription, ",",
1546 sizeof(rt->last_subscription));
1547 ff_rdt_subscribe_rule(
1548 rt->last_subscription,
1549 sizeof(rt->last_subscription), i, rule_nr);
1556 av_strlcatf(cmd, sizeof(cmd), "%s\r\n", rt->last_subscription);
1557 rtsp_send_cmd(s, cmd, reply, NULL);
1558 if (reply->status_code != RTSP_STATUS_OK)
1559 return AVERROR_INVALIDDATA;
1560 rt->need_subscription = 0;
1562 if (rt->state == RTSP_STATE_PLAYING)
1567 /* get next frames from the same RTP packet */
1568 if (rt->cur_transport_priv) {
1569 if (rt->transport == RTSP_TRANSPORT_RDT)
1570 ret = ff_rdt_parse_packet(rt->cur_transport_priv, pkt, NULL, 0);
1572 ret = rtp_parse_packet(rt->cur_transport_priv, pkt, NULL, 0);
1574 rt->cur_transport_priv = NULL;
1576 } else if (ret == 1) {
1579 rt->cur_transport_priv = NULL;
1583 /* read next RTP packet */
1585 switch(rt->lower_transport) {
1587 case RTSP_LOWER_TRANSPORT_TCP:
1588 len = tcp_read_packet(s, &rtsp_st, buf, sizeof(buf));
1590 case RTSP_LOWER_TRANSPORT_UDP:
1591 case RTSP_LOWER_TRANSPORT_UDP_MULTICAST:
1592 len = udp_read_packet(s, &rtsp_st, buf, sizeof(buf));
1593 if (len >=0 && rtsp_st->transport_priv && rt->transport == RTSP_TRANSPORT_RTP)
1594 rtp_check_and_send_back_rr(rtsp_st->transport_priv, len);
1601 if (rt->transport == RTSP_TRANSPORT_RDT)
1602 ret = ff_rdt_parse_packet(rtsp_st->transport_priv, pkt, buf, len);
1604 ret = rtp_parse_packet(rtsp_st->transport_priv, pkt, buf, len);
1608 /* more packets may follow, so we save the RTP context */
1609 rt->cur_transport_priv = rtsp_st->transport_priv;
1612 /* send dummy request to keep TCP connection alive */
1613 if ((rt->server_type == RTSP_SERVER_WMS ||
1614 rt->server_type == RTSP_SERVER_REAL) &&
1615 (av_gettime() - rt->last_cmd_time) / 1000000 >= rt->timeout / 2) {
1616 if (rt->server_type == RTSP_SERVER_WMS) {
1617 snprintf(cmd, sizeof(cmd) - 1,
1618 "GET_PARAMETER %s RTSP/1.0\r\n",
1620 rtsp_send_cmd_async(s, cmd, reply, NULL);
1622 rtsp_send_cmd_async(s, "OPTIONS * RTSP/1.0\r\n",
1630 /* pause the stream */
1631 static int rtsp_read_pause(AVFormatContext *s)
1633 RTSPState *rt = s->priv_data;
1634 RTSPMessageHeader reply1, *reply = &reply1;
1639 if (rt->state != RTSP_STATE_PLAYING)
1641 else if (!(rt->server_type == RTSP_SERVER_REAL && rt->need_subscription)) {
1642 snprintf(cmd, sizeof(cmd),
1643 "PAUSE %s RTSP/1.0\r\n",
1645 rtsp_send_cmd(s, cmd, reply, NULL);
1646 if (reply->status_code != RTSP_STATUS_OK) {
1650 rt->state = RTSP_STATE_PAUSED;
1654 static int rtsp_read_seek(AVFormatContext *s, int stream_index,
1655 int64_t timestamp, int flags)
1657 RTSPState *rt = s->priv_data;
1659 rt->seek_timestamp = av_rescale_q(timestamp, s->streams[stream_index]->time_base, AV_TIME_BASE_Q);
1662 case RTSP_STATE_IDLE:
1664 case RTSP_STATE_PLAYING:
1665 if (rtsp_read_pause(s) != 0)
1667 rt->state = RTSP_STATE_SEEKING;
1668 if (rtsp_read_play(s) != 0)
1671 case RTSP_STATE_PAUSED:
1672 rt->state = RTSP_STATE_IDLE;
1678 static int rtsp_read_close(AVFormatContext *s)
1680 RTSPState *rt = s->priv_data;
1681 RTSPMessageHeader reply1, *reply = &reply1;
1685 /* NOTE: it is valid to flush the buffer here */
1686 if (rt->lower_transport == RTSP_LOWER_TRANSPORT_TCP) {
1687 url_fclose(&rt->rtsp_gb);
1690 snprintf(cmd, sizeof(cmd),
1691 "TEARDOWN %s RTSP/1.0\r\n",
1693 rtsp_send_cmd(s, cmd, reply, NULL);
1695 rtsp_close_streams(rt);
1696 url_close(rt->rtsp_hd);
1700 #if CONFIG_RTSP_DEMUXER
1701 AVInputFormat rtsp_demuxer = {
1703 NULL_IF_CONFIG_SMALL("RTSP input format"),
1710 .flags = AVFMT_NOFILE,
1711 .read_play = rtsp_read_play,
1712 .read_pause = rtsp_read_pause,
1716 static int sdp_probe(AVProbeData *p1)
1718 const char *p = p1->buf, *p_end = p1->buf + p1->buf_size;
1720 /* we look for a line beginning "c=IN IP4" */
1721 while (p < p_end && *p != '\0') {
1722 if (p + sizeof("c=IN IP4") - 1 < p_end && av_strstart(p, "c=IN IP4", NULL))
1723 return AVPROBE_SCORE_MAX / 2;
1725 while(p < p_end - 1 && *p != '\n') p++;
1734 #define SDP_MAX_SIZE 8192
1736 static int sdp_read_header(AVFormatContext *s,
1737 AVFormatParameters *ap)
1739 RTSPState *rt = s->priv_data;
1740 RTSPStream *rtsp_st;
1745 /* read the whole sdp file */
1746 /* XXX: better loading */
1747 content = av_malloc(SDP_MAX_SIZE);
1748 size = get_buffer(s->pb, content, SDP_MAX_SIZE - 1);
1751 return AVERROR_INVALIDDATA;
1753 content[size] ='\0';
1755 sdp_parse(s, content);
1758 /* open each RTP stream */
1759 for(i=0;i<rt->nb_rtsp_streams;i++) {
1760 rtsp_st = rt->rtsp_streams[i];
1762 snprintf(url, sizeof(url), "rtp://%s:%d?localport=%d&ttl=%d",
1763 inet_ntoa(rtsp_st->sdp_ip),
1767 if (url_open(&rtsp_st->rtp_handle, url, URL_RDWR) < 0) {
1768 err = AVERROR_INVALIDDATA;
1771 if ((err = rtsp_open_transport_ctx(s, rtsp_st)))
1776 rtsp_close_streams(rt);
1780 static int sdp_read_packet(AVFormatContext *s,
1783 return rtsp_read_packet(s, pkt);
1786 static int sdp_read_close(AVFormatContext *s)
1788 RTSPState *rt = s->priv_data;
1789 rtsp_close_streams(rt);
1793 #if CONFIG_SDP_DEMUXER
1794 AVInputFormat sdp_demuxer = {
1796 NULL_IF_CONFIG_SMALL("SDP"),