rtsp: Make the rtsp flags avoptions set via a define
[libav.git] / libavformat / rtsp.c
CommitLineData
1617ad97 1/*
93ced3e8 2 * RTSP/SDP client
406792e7 3 * Copyright (c) 2002 Fabrice Bellard
1617ad97 4 *
2912e87a 5 * This file is part of Libav.
b78e7197 6 *
2912e87a 7 * Libav is free software; you can redistribute it and/or
1617ad97
FB
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
b78e7197 10 * version 2.1 of the License, or (at your option) any later version.
1617ad97 11 *
2912e87a 12 * Libav is distributed in the hope that it will be useful,
1617ad97
FB
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.
16 *
17 * You should have received a copy of the GNU Lesser General Public
2912e87a 18 * License along with Libav; if not, write to the Free Software
5509bffa 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
1617ad97 20 */
245976da 21
f9337897 22#include "libavutil/base64.h"
245976da 23#include "libavutil/avstring.h"
6a5d31ac 24#include "libavutil/intreadwrite.h"
0ebcdf5c 25#include "libavutil/mathematics.h"
9fcae973 26#include "libavutil/parseutils.h"
f5d33f52 27#include "libavutil/random_seed.h"
d2d67e42 28#include "libavutil/dict.h"
1617ad97 29#include "avformat.h"
e731b8d8 30#include "avio_internal.h"
1617ad97 31
db0ed93e 32#include <sys/time.h>
a8475bbd
LB
33#if HAVE_POLL_H
34#include <poll.h>
6ad1c9c9 35#endif
ea452b54 36#include <strings.h>
e4a9e3cc 37#include "internal.h"
42572ef5 38#include "network.h"
cbfa66d0 39#include "os_support.h"
f5d33f52 40#include "http.h"
c971ff19 41#include "rtsp.h"
1617ad97 42
302879cb 43#include "rtpdec.h"
e9dea59f 44#include "rdt.h"
965a3ddb 45#include "rtpdec_formats.h"
a493f80a 46#include "rtpenc_chain.h"
5652bb94 47#include "url.h"
17fff881 48#include "rtpenc.h"
4934884a 49
1617ad97
FB
50//#define DEBUG
51
a8475bbd 52/* Timeout values for socket poll, in ms,
9cba6f5f 53 * and read_packet(), in seconds */
a8475bbd 54#define POLL_TIMEOUT_MS 100
9cba6f5f 55#define READ_PACKET_TIMEOUT_S 10
a8475bbd 56#define MAX_TIMEOUTS READ_PACKET_TIMEOUT_S * 1000 / POLL_TIMEOUT_MS
91af5601 57#define SDP_MAX_SIZE 16384
96a7c975 58#define RECVBUF_SIZE 10 * RTP_MAX_PACKET_LENGTH
9cba6f5f 59
2c9aa024
MS
60#define OFFSET(x) offsetof(RTSPState, x)
61#define DEC AV_OPT_FLAG_DECODING_PARAM
eca4850c 62#define ENC AV_OPT_FLAG_ENCODING_PARAM
3a6765fb
MS
63
64#define RTSP_FLAG_OPTS(name, longname) \
65 { name, longname, OFFSET(rtsp_flags), AV_OPT_TYPE_FLAGS, {0}, INT_MIN, INT_MAX, DEC, "rtsp_flags" }, \
66 { "filter_src", "Only receive packets from the negotiated peer IP", 0, AV_OPT_TYPE_CONST, {RTSP_FLAG_FILTER_SRC}, 0, 0, DEC, "rtsp_flags" }
67
17fff881 68const AVOption ff_rtsp_options[] = {
2c9aa024 69 { "initial_pause", "Don't start playing the stream immediately", OFFSET(initial_pause), AV_OPT_TYPE_INT, {0}, 0, 1, DEC },
17fff881 70 FF_RTP_FLAG_OPTS(RTSPState, rtp_muxer_flags),
eca4850c
MS
71 { "rtsp_transport", "RTSP transport protocols", OFFSET(lower_transport_mask), AV_OPT_TYPE_FLAGS, {0}, INT_MIN, INT_MAX, DEC|ENC, "rtsp_transport" }, \
72 { "udp", "UDP", 0, AV_OPT_TYPE_CONST, {1 << RTSP_LOWER_TRANSPORT_UDP}, 0, 0, DEC|ENC, "rtsp_transport" }, \
73 { "tcp", "TCP", 0, AV_OPT_TYPE_CONST, {1 << RTSP_LOWER_TRANSPORT_TCP}, 0, 0, DEC|ENC, "rtsp_transport" }, \
74 { "udp_multicast", "UDP multicast", 0, AV_OPT_TYPE_CONST, {1 << RTSP_LOWER_TRANSPORT_UDP_MULTICAST}, 0, 0, DEC, "rtsp_transport" },
75 { "http", "HTTP tunneling", 0, AV_OPT_TYPE_CONST, {(1 << RTSP_LOWER_TRANSPORT_HTTP)}, 0, 0, DEC, "rtsp_transport" },
3a6765fb 76 RTSP_FLAG_OPTS("rtsp_flags", "RTSP flags"),
17fff881
MS
77 { NULL },
78};
79
1ef36a70
RB
80static void get_word_until_chars(char *buf, int buf_size,
81 const char *sep, const char **pp)
1617ad97
FB
82{
83 const char *p;
84 char *q;
85
86 p = *pp;
30619e6e 87 p += strspn(p, SPACE_CHARS);
1617ad97
FB
88 q = buf;
89 while (!strchr(sep, *p) && *p != '\0') {
90 if ((q - buf) < buf_size - 1)
91 *q++ = *p;
92 p++;
93 }
94 if (buf_size > 0)
95 *q = '\0';
96 *pp = p;
97}
98
1ef36a70
RB
99static void get_word_sep(char *buf, int buf_size, const char *sep,
100 const char **pp)
1617ad97 101{
1ef36a70
RB
102 if (**pp == '/') (*pp)++;
103 get_word_until_chars(buf, buf_size, sep, pp);
104}
1617ad97 105
1ef36a70
RB
106static void get_word(char *buf, int buf_size, const char **pp)
107{
108 get_word_until_chars(buf, buf_size, SPACE_CHARS, pp);
1617ad97
FB
109}
110
8bf0f969
MS
111/** Parse a string p in the form of Range:npt=xx-xx, and determine the start
112 * and end time.
113 * Used for seeking in the rtp stream.
114 */
115static void rtsp_parse_range_npt(const char *p, int64_t *start, int64_t *end)
116{
117 char buf[256];
118
119 p += strspn(p, SPACE_CHARS);
120 if (!av_stristart(p, "npt=", &p))
121 return;
122
123 *start = AV_NOPTS_VALUE;
124 *end = AV_NOPTS_VALUE;
125
126 get_word_sep(buf, sizeof(buf), "-", &p);
9fcae973 127 av_parse_time(start, buf, 1);
8bf0f969
MS
128 if (*p == '-') {
129 p++;
130 get_word_sep(buf, sizeof(buf), "-", &p);
9fcae973 131 av_parse_time(end, buf, 1);
8bf0f969
MS
132 }
133// av_log(NULL, AV_LOG_DEBUG, "Range Start: %lld\n", *start);
134// av_log(NULL, AV_LOG_DEBUG, "Range End: %lld\n", *end);
135}
136
137static int get_sockaddr(const char *buf, struct sockaddr_storage *sock)
138{
139 struct addrinfo hints, *ai = NULL;
140 memset(&hints, 0, sizeof(hints));
141 hints.ai_flags = AI_NUMERICHOST;
142 if (getaddrinfo(buf, NULL, &hints, &ai))
143 return -1;
144 memcpy(sock, ai->ai_addr, FFMIN(sizeof(*sock), ai->ai_addrlen));
145 freeaddrinfo(ai);
146 return 0;
147}
148
44b70ce5 149#if CONFIG_RTPDEC
003eb642
MS
150static void init_rtp_handler(RTPDynamicProtocolHandler *handler,
151 RTSPStream *rtsp_st, AVCodecContext *codec)
152{
153 if (!handler)
154 return;
155 codec->codec_id = handler->codec_id;
156 rtsp_st->dynamic_handler = handler;
9261e6cf
MS
157 if (handler->alloc)
158 rtsp_st->dynamic_protocol_context = handler->alloc();
003eb642
MS
159}
160
c8965800 161/* parse the rtpmap description: <codec_name>/<clock_rate>[/<other params>] */
8f3c87f3 162static int sdp_parse_rtpmap(AVFormatContext *s,
86b6e387 163 AVStream *st, RTSPStream *rtsp_st,
c8965800 164 int payload_type, const char *p)
93ced3e8 165{
86b6e387 166 AVCodecContext *codec = st->codec;
93ced3e8 167 char buf[256];
d1ccf0e0
RD
168 int i;
169 AVCodec *c;
7b49ce2e 170 const char *c_name;
93ced3e8 171
d1ccf0e0 172 /* Loop into AVRtpDynamicPayloadTypes[] and AVRtpPayloadTypes[] and
9d50d396
RB
173 * see if we can handle this kind of payload.
174 * The space should normally not be there but some Real streams or
175 * particular servers ("RealServer Version 6.1.3.970", see issue 1658)
176 * have a trailing space. */
177 get_word_sep(buf, sizeof(buf), "/ ", &p);
d1ccf0e0 178 if (payload_type >= RTP_PT_PRIVATE) {
003eb642
MS
179 RTPDynamicProtocolHandler *handler =
180 ff_rtp_handler_find_by_name(buf, codec->codec_type);
181 init_rtp_handler(handler, rtsp_st, codec);
160918d5
MS
182 /* If no dynamic handler was found, check with the list of standard
183 * allocated types, if such a stream for some reason happens to
184 * use a private payload type. This isn't handled in rtpdec.c, since
185 * the format name from the rtpmap line never is passed into rtpdec. */
186 if (!rtsp_st->dynamic_handler)
187 codec->codec_id = ff_rtp_codec_id(buf, codec->codec_type);
93ced3e8 188 } else {
c8965800
RB
189 /* We are in a standard case
190 * (from http://www.iana.org/assignments/rtp-parameters). */
d1ccf0e0 191 /* search into AVRtpPayloadTypes[] */
7ed19d7f 192 codec->codec_id = ff_rtp_codec_id(buf, codec->codec_type);
d1ccf0e0
RD
193 }
194
195 c = avcodec_find_decoder(codec->codec_id);
196 if (c && c->name)
7b49ce2e 197 c_name = c->name;
d1ccf0e0 198 else
170870b7 199 c_name = "(null)";
d1ccf0e0 200
7515ed0c
RB
201 get_word_sep(buf, sizeof(buf), "/", &p);
202 i = atoi(buf);
203 switch (codec->codec_type) {
72415b2a 204 case AVMEDIA_TYPE_AUDIO:
7515ed0c
RB
205 av_log(s, AV_LOG_DEBUG, "audio codec set to: %s\n", c_name);
206 codec->sample_rate = RTSP_DEFAULT_AUDIO_SAMPLERATE;
207 codec->channels = RTSP_DEFAULT_NB_AUDIO_CHANNELS;
208 if (i > 0) {
209 codec->sample_rate = i;
86b6e387 210 av_set_pts_info(st, 32, 1, codec->sample_rate);
7515ed0c
RB
211 get_word_sep(buf, sizeof(buf), "/", &p);
212 i = atoi(buf);
213 if (i > 0)
214 codec->channels = i;
215 // TODO: there is a bug here; if it is a mono stream, and
216 // less than 22000Hz, faad upconverts to stereo and twice
217 // the frequency. No problem, but the sample rate is being
218 // set here by the sdp line. Patch on its way. (rdm)
d1ccf0e0 219 }
7515ed0c
RB
220 av_log(s, AV_LOG_DEBUG, "audio samplerate set to: %i\n",
221 codec->sample_rate);
222 av_log(s, AV_LOG_DEBUG, "audio channels set to: %i\n",
223 codec->channels);
224 break;
72415b2a 225 case AVMEDIA_TYPE_VIDEO:
7515ed0c 226 av_log(s, AV_LOG_DEBUG, "video codec set to: %s\n", c_name);
86b6e387
MS
227 if (i > 0)
228 av_set_pts_info(st, 32, 1, i);
7515ed0c
RB
229 break;
230 default:
231 break;
232 }
233 return 0;
93ced3e8
FB
234}
235
c2bfd816 236/* parse the attribute line from the fmtp a line of an sdp response. This
c8965800
RB
237 * is broken out as a function because it is used in rtp_h264.c, which is
238 * forthcoming. */
3307e6ea 239int ff_rtsp_next_attr_and_value(const char **p, char *attr, int attr_size,
93993933 240 char *value, int value_size)
d0deedcb 241{
30619e6e 242 *p += strspn(*p, SPACE_CHARS);
c8965800 243 if (**p) {
d0deedcb
RM
244 get_word_sep(attr, attr_size, "=", p);
245 if (**p == '=')
246 (*p)++;
247 get_word_sep(value, value_size, ";", p);
248 if (**p == ';')
249 (*p)++;
250 return 1;
251 }
252 return 0;
253}
254
93ced3e8
FB
255typedef struct SDPParseState {
256 /* SDP only */
3fbd12d1 257 struct sockaddr_storage default_ip;
c8965800
RB
258 int default_ttl;
259 int skip_media; ///< set if an unknown m= line occurs
93ced3e8
FB
260} SDPParseState;
261
262static void sdp_parse_line(AVFormatContext *s, SDPParseState *s1,
1617ad97
FB
263 int letter, const char *buf)
264{
8b1ab7bf 265 RTSPState *rt = s->priv_data;
1617ad97
FB
266 char buf1[64], st_type[64];
267 const char *p;
72415b2a 268 enum AVMediaType codec_type;
fb65d2ca 269 int payload_type, i;
1617ad97
FB
270 AVStream *st;
271 RTSPStream *rtsp_st;
3fbd12d1 272 struct sockaddr_storage sdp_ip;
93ced3e8
FB
273 int ttl;
274
dfd2a005 275 av_dlog(s, "sdp: %c='%s'\n", letter, buf);
1617ad97
FB
276
277 p = buf;
cb760a47
RB
278 if (s1->skip_media && letter != 'm')
279 return;
c8965800 280 switch (letter) {
93ced3e8
FB
281 case 'c':
282 get_word(buf1, sizeof(buf1), &p);
283 if (strcmp(buf1, "IN") != 0)
284 return;
285 get_word(buf1, sizeof(buf1), &p);
3fbd12d1 286 if (strcmp(buf1, "IP4") && strcmp(buf1, "IP6"))
93ced3e8
FB
287 return;
288 get_word_sep(buf1, sizeof(buf1), "/", &p);
3fbd12d1 289 if (get_sockaddr(buf1, &sdp_ip))
93ced3e8
FB
290 return;
291 ttl = 16;
292 if (*p == '/') {
293 p++;
294 get_word_sep(buf1, sizeof(buf1), "/", &p);
295 ttl = atoi(buf1);
296 }
297 if (s->nb_streams == 0) {
298 s1->default_ip = sdp_ip;
299 s1->default_ttl = ttl;
300 } else {
d9c0510e 301 rtsp_st = rt->rtsp_streams[rt->nb_rtsp_streams - 1];
93ced3e8
FB
302 rtsp_st->sdp_ip = sdp_ip;
303 rtsp_st->sdp_ttl = ttl;
304 }
305 break;
1617ad97 306 case 's':
d2d67e42 307 av_dict_set(&s->metadata, "title", p, 0);
1617ad97
FB
308 break;
309 case 'i':
310 if (s->nb_streams == 0) {
d2d67e42 311 av_dict_set(&s->metadata, "comment", p, 0);
1617ad97
FB
312 break;
313 }
314 break;
315 case 'm':
316 /* new stream */
cb760a47 317 s1->skip_media = 0;
1617ad97
FB
318 get_word(st_type, sizeof(st_type), &p);
319 if (!strcmp(st_type, "audio")) {
72415b2a 320 codec_type = AVMEDIA_TYPE_AUDIO;
1617ad97 321 } else if (!strcmp(st_type, "video")) {
72415b2a 322 codec_type = AVMEDIA_TYPE_VIDEO;
090438cc 323 } else if (!strcmp(st_type, "application")) {
72415b2a 324 codec_type = AVMEDIA_TYPE_DATA;
1617ad97 325 } else {
cb760a47 326 s1->skip_media = 1;
1617ad97
FB
327 return;
328 }
1617ad97
FB
329 rtsp_st = av_mallocz(sizeof(RTSPStream));
330 if (!rtsp_st)
331 return;
8b1ab7bf
FB
332 rtsp_st->stream_index = -1;
333 dynarray_add(&rt->rtsp_streams, &rt->nb_rtsp_streams, rtsp_st);
93ced3e8
FB
334
335 rtsp_st->sdp_ip = s1->default_ip;
336 rtsp_st->sdp_ttl = s1->default_ttl;
337
93ced3e8
FB
338 get_word(buf1, sizeof(buf1), &p); /* port */
339 rtsp_st->sdp_port = atoi(buf1);
340
341 get_word(buf1, sizeof(buf1), &p); /* protocol (ignored) */
115329f1 342
93ced3e8
FB
343 /* XXX: handle list of formats */
344 get_word(buf1, sizeof(buf1), &p); /* format list */
345 rtsp_st->sdp_payload_type = atoi(buf1);
93ced3e8 346
7ed19d7f 347 if (!strcmp(ff_rtp_enc_name(rtsp_st->sdp_payload_type), "MP2T")) {
8b1ab7bf
FB
348 /* no corresponding stream */
349 } else {
b2dd842d 350 st = av_new_stream(s, rt->nb_rtsp_streams - 1);
8b1ab7bf
FB
351 if (!st)
352 return;
8b1ab7bf 353 rtsp_st->stream_index = st->index;
01f4895c 354 st->codec->codec_type = codec_type;
d1ccf0e0 355 if (rtsp_st->sdp_payload_type < RTP_PT_PRIVATE) {
6a7e31a9 356 RTPDynamicProtocolHandler *handler;
8b1ab7bf 357 /* if standard payload type, we can find the codec right now */
bf6d9818 358 ff_rtp_get_codec_info(st->codec, rtsp_st->sdp_payload_type);
bbd8f547
MS
359 if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO &&
360 st->codec->sample_rate > 0)
86b6e387 361 av_set_pts_info(st, 32, 1, st->codec->sample_rate);
6a7e31a9
MS
362 /* Even static payload types may need a custom depacketizer */
363 handler = ff_rtp_handler_find_by_id(
364 rtsp_st->sdp_payload_type, st->codec->codec_type);
365 init_rtp_handler(handler, rtsp_st, st->codec);
8b1ab7bf
FB
366 }
367 }
1617ad97 368 /* put a default control url */
00eb13e0 369 av_strlcpy(rtsp_st->control_url, rt->control_uri,
c8965800 370 sizeof(rtsp_st->control_url));
1617ad97
FB
371 break;
372 case 'a':
00eb13e0
AS
373 if (av_strstart(p, "control:", &p)) {
374 if (s->nb_streams == 0) {
375 if (!strncmp(p, "rtsp://", 7))
376 av_strlcpy(rt->control_uri, p,
377 sizeof(rt->control_uri));
378 } else {
0b6a7ff4
MS
379 char proto[32];
380 /* get the control url */
d9c0510e 381 rtsp_st = rt->rtsp_streams[rt->nb_rtsp_streams - 1];
115329f1 382
0b6a7ff4
MS
383 /* XXX: may need to add full url resolution */
384 av_url_split(proto, sizeof(proto), NULL, 0, NULL, 0,
385 NULL, NULL, 0, p);
386 if (proto[0] == '\0') {
387 /* relative control URL */
388 if (rtsp_st->control_url[strlen(rtsp_st->control_url)-1]!='/')
389 av_strlcat(rtsp_st->control_url, "/",
390 sizeof(rtsp_st->control_url));
391 av_strlcat(rtsp_st->control_url, p,
392 sizeof(rtsp_st->control_url));
393 } else
394 av_strlcpy(rtsp_st->control_url, p,
395 sizeof(rtsp_st->control_url));
00eb13e0 396 }
83d14c85 397 } else if (av_strstart(p, "rtpmap:", &p) && s->nb_streams > 0) {
93ced3e8 398 /* NOTE: rtpmap is only supported AFTER the 'm=' tag */
115329f1 399 get_word(buf1, sizeof(buf1), &p);
93ced3e8 400 payload_type = atoi(buf1);
83d14c85 401 st = s->streams[s->nb_streams - 1];
d9c0510e 402 rtsp_st = rt->rtsp_streams[rt->nb_rtsp_streams - 1];
86b6e387 403 sdp_parse_rtpmap(s, st, rtsp_st, payload_type, p);
7fc8ac7f
JA
404 } else if (av_strstart(p, "fmtp:", &p) ||
405 av_strstart(p, "framesize:", &p)) {
93ced3e8 406 /* NOTE: fmtp is only supported AFTER the 'a=rtpmap:xxx' tag */
d0deedcb
RM
407 // let dynamic protocol handlers have a stab at the line.
408 get_word(buf1, sizeof(buf1), &p);
409 payload_type = atoi(buf1);
d9c0510e
MS
410 for (i = 0; i < rt->nb_rtsp_streams; i++) {
411 rtsp_st = rt->rtsp_streams[i];
c8965800
RB
412 if (rtsp_st->sdp_payload_type == payload_type &&
413 rtsp_st->dynamic_handler &&
414 rtsp_st->dynamic_handler->parse_sdp_a_line)
415 rtsp_st->dynamic_handler->parse_sdp_a_line(s, i,
416 rtsp_st->dynamic_protocol_context, buf);
d0deedcb 417 }
c8965800 418 } else if (av_strstart(p, "range:", &p)) {
31693e00
RM
419 int64_t start, end;
420
421 // this is so that seeking on a streamed file can work.
422 rtsp_parse_range_npt(p, &start, &end);
c8965800
RB
423 s->start_time = start;
424 /* AV_NOPTS_VALUE means live broadcast (and can't seek) */
425 s->duration = (end == AV_NOPTS_VALUE) ?
426 AV_NOPTS_VALUE : end - start;
119b4668
RB
427 } else if (av_strstart(p, "IsRealDataType:integer;",&p)) {
428 if (atoi(p) == 1)
429 rt->transport = RTSP_TRANSPORT_RDT;
bb776f3b
MS
430 } else if (av_strstart(p, "SampleRate:integer;", &p) &&
431 s->nb_streams > 0) {
432 st = s->streams[s->nb_streams - 1];
433 st->codec->sample_rate = atoi(p);
1a30d541
RB
434 } else {
435 if (rt->server_type == RTSP_SERVER_WMS)
436 ff_wms_parse_sdp_a_line(s, p);
437 if (s->nb_streams > 0) {
c4a3d032
RB
438 if (rt->server_type == RTSP_SERVER_REAL)
439 ff_real_parse_sdp_a_line(s, s->nb_streams - 1, p);
440
d9c0510e 441 rtsp_st = rt->rtsp_streams[rt->nb_rtsp_streams - 1];
c4a3d032
RB
442 if (rtsp_st->dynamic_handler &&
443 rtsp_st->dynamic_handler->parse_sdp_a_line)
c8965800
RB
444 rtsp_st->dynamic_handler->parse_sdp_a_line(s,
445 s->nb_streams - 1,
c4a3d032 446 rtsp_st->dynamic_protocol_context, buf);
1a30d541 447 }
1617ad97
FB
448 }
449 break;
450 }
451}
452
0526c6f7 453int ff_sdp_parse(AVFormatContext *s, const char *content)
1617ad97 454{
a8475bbd 455 RTSPState *rt = s->priv_data;
1617ad97
FB
456 const char *p;
457 int letter;
9211bcdd
RB
458 /* Some SDP lines, particularly for Realmedia or ASF RTSP streams,
459 * contain long SDP lines containing complete ASF Headers (several
460 * kB) or arrays of MDPR (RM stream descriptor) headers plus
461 * "rulebooks" describing their properties. Therefore, the SDP line
373afbaf
RB
462 * buffer is large.
463 *
afcea58c
JA
464 * The Vorbis FMTP line can be up to 16KB - see xiph_parse_sdp_line
465 * in rtpdec_xiph.c. */
373afbaf 466 char buf[16384], *q;
93ced3e8 467 SDPParseState sdp_parse_state, *s1 = &sdp_parse_state;
115329f1 468
93ced3e8 469 memset(s1, 0, sizeof(SDPParseState));
1617ad97 470 p = content;
c8965800 471 for (;;) {
30619e6e 472 p += strspn(p, SPACE_CHARS);
1617ad97
FB
473 letter = *p;
474 if (letter == '\0')
475 break;
476 p++;
477 if (*p != '=')
478 goto next_line;
479 p++;
480 /* get the content */
481 q = buf;
b6892136 482 while (*p != '\n' && *p != '\r' && *p != '\0') {
1617ad97
FB
483 if ((q - buf) < sizeof(buf) - 1)
484 *q++ = *p;
485 p++;
486 }
487 *q = '\0';
93ced3e8 488 sdp_parse_line(s, s1, letter, buf);
1617ad97
FB
489 next_line:
490 while (*p != '\n' && *p != '\0')
491 p++;
492 if (*p == '\n')
493 p++;
494 }
a8475bbd
LB
495 rt->p = av_malloc(sizeof(struct pollfd)*2*(rt->nb_rtsp_streams+1));
496 if (!rt->p) return AVERROR(ENOMEM);
1617ad97
FB
497 return 0;
498}
44b70ce5 499#endif /* CONFIG_RTPDEC */
1617ad97 500
93e7490e
MS
501void ff_rtsp_undo_setup(AVFormatContext *s)
502{
503 RTSPState *rt = s->priv_data;
504 int i;
505
506 for (i = 0; i < rt->nb_rtsp_streams; i++) {
507 RTSPStream *rtsp_st = rt->rtsp_streams[i];
508 if (!rtsp_st)
509 continue;
510 if (rtsp_st->transport_priv) {
511 if (s->oformat) {
512 AVFormatContext *rtpctx = rtsp_st->transport_priv;
513 av_write_trailer(rtpctx);
514 if (rt->lower_transport == RTSP_LOWER_TRANSPORT_TCP) {
515 uint8_t *ptr;
6dc7d80d 516 avio_close_dyn_buf(rtpctx->pb, &ptr);
93e7490e
MS
517 av_free(ptr);
518 } else {
22a3212e 519 avio_close(rtpctx->pb);
93e7490e 520 }
b22dbb29 521 avformat_free_context(rtpctx);
93e7490e
MS
522 } else if (rt->transport == RTSP_TRANSPORT_RDT && CONFIG_RTPDEC)
523 ff_rdt_parse_close(rtsp_st->transport_priv);
524 else if (CONFIG_RTPDEC)
bfc6db44 525 ff_rtp_parse_close(rtsp_st->transport_priv);
93e7490e
MS
526 }
527 rtsp_st->transport_priv = NULL;
528 if (rtsp_st->rtp_handle)
e52a9145 529 ffurl_close(rtsp_st->rtp_handle);
93e7490e
MS
530 rtsp_st->rtp_handle = NULL;
531 }
532}
533
1ced9da3 534/* close and free RTSP streams */
3307e6ea 535void ff_rtsp_close_streams(AVFormatContext *s)
1ced9da3 536{
52aa4338 537 RTSPState *rt = s->priv_data;
1ced9da3
LA
538 int i;
539 RTSPStream *rtsp_st;
540
93e7490e 541 ff_rtsp_undo_setup(s);
c8965800 542 for (i = 0; i < rt->nb_rtsp_streams; i++) {
1ced9da3
LA
543 rtsp_st = rt->rtsp_streams[i];
544 if (rtsp_st) {
1ced9da3 545 if (rtsp_st->dynamic_handler && rtsp_st->dynamic_protocol_context)
9261e6cf 546 rtsp_st->dynamic_handler->free(
c8965800 547 rtsp_st->dynamic_protocol_context);
ea7f0807 548 av_free(rtsp_st);
1ced9da3
LA
549 }
550 }
551 av_free(rt->rtsp_streams);
552 if (rt->asf_ctx) {
553 av_close_input_stream (rt->asf_ctx);
554 rt->asf_ctx = NULL;
555 }
a8475bbd 556 av_free(rt->p);
96a7c975 557 av_free(rt->recvbuf);
1ced9da3
LA
558}
559
c8965800 560static int rtsp_open_transport_ctx(AVFormatContext *s, RTSPStream *rtsp_st)
1ced9da3
LA
561{
562 RTSPState *rt = s->priv_data;
563 AVStream *st = NULL;
564
565 /* open the RTP context */
566 if (rtsp_st->stream_index >= 0)
567 st = s->streams[rtsp_st->stream_index];
568 if (!st)
569 s->ctx_flags |= AVFMTCTX_NOHEADER;
570
44b70ce5 571 if (s->oformat && CONFIG_RTSP_MUXER) {
a493f80a
MS
572 rtsp_st->transport_priv = ff_rtp_chain_mux_open(s, st,
573 rtsp_st->rtp_handle,
574 RTSP_TCP_MAX_PACKET_SIZE);
c2bfd816 575 /* Ownership of rtp_handle is passed to the rtp mux context */
fd450a51 576 rtsp_st->rtp_handle = NULL;
44b70ce5 577 } else if (rt->transport == RTSP_TRANSPORT_RDT && CONFIG_RTPDEC)
1ced9da3
LA
578 rtsp_st->transport_priv = ff_rdt_parse_open(s, st->index,
579 rtsp_st->dynamic_protocol_context,
580 rtsp_st->dynamic_handler);
44b70ce5 581 else if (CONFIG_RTPDEC)
bfc6db44 582 rtsp_st->transport_priv = ff_rtp_parse_open(s, st, rtsp_st->rtp_handle,
58ee0991
MS
583 rtsp_st->sdp_payload_type,
584 (rt->lower_transport == RTSP_LOWER_TRANSPORT_TCP || !s->max_delay)
585 ? 0 : RTP_REORDER_QUEUE_DEFAULT_SIZE);
1ced9da3
LA
586
587 if (!rtsp_st->transport_priv) {
588 return AVERROR(ENOMEM);
44b70ce5 589 } else if (rt->transport != RTSP_TRANSPORT_RDT && CONFIG_RTPDEC) {
c8965800 590 if (rtsp_st->dynamic_handler) {
bfc6db44
MS
591 ff_rtp_parse_set_dynamic_protocol(rtsp_st->transport_priv,
592 rtsp_st->dynamic_protocol_context,
593 rtsp_st->dynamic_handler);
1ced9da3
LA
594 }
595 }
596
597 return 0;
598}
599
6f5a3d0a 600#if CONFIG_RTSP_DEMUXER || CONFIG_RTSP_MUXER
1617ad97
FB
601static void rtsp_parse_range(int *min_ptr, int *max_ptr, const char **pp)
602{
603 const char *p;
604 int v;
605
606 p = *pp;
30619e6e 607 p += strspn(p, SPACE_CHARS);
1617ad97
FB
608 v = strtol(p, (char **)&p, 10);
609 if (*p == '-') {
610 p++;
611 *min_ptr = v;
612 v = strtol(p, (char **)&p, 10);
613 *max_ptr = v;
614 } else {
615 *min_ptr = v;
616 *max_ptr = v;
617 }
618 *pp = p;
619}
620
621/* XXX: only one transport specification is parsed */
a9e534d5 622static void rtsp_parse_transport(RTSPMessageHeader *reply, const char *p)
1617ad97
FB
623{
624 char transport_protocol[16];
625 char profile[16];
626 char lower_transport[16];
627 char parameter[16];
628 RTSPTransportField *th;
629 char buf[256];
115329f1 630
1617ad97 631 reply->nb_transports = 0;
115329f1 632
c8965800 633 for (;;) {
30619e6e 634 p += strspn(p, SPACE_CHARS);
1617ad97
FB
635 if (*p == '\0')
636 break;
637
638 th = &reply->transports[reply->nb_transports];
639
115329f1 640 get_word_sep(transport_protocol, sizeof(transport_protocol),
1617ad97 641 "/", &p);
e1502118 642 if (!strcasecmp (transport_protocol, "rtp")) {
7ecc634e
LB
643 get_word_sep(profile, sizeof(profile), "/;,", &p);
644 lower_transport[0] = '\0';
645 /* rtp/avp/<protocol> */
646 if (*p == '/') {
7ecc634e
LB
647 get_word_sep(lower_transport, sizeof(lower_transport),
648 ";,", &p);
119b4668
RB
649 }
650 th->transport = RTSP_TRANSPORT_RTP;
651 } else if (!strcasecmp (transport_protocol, "x-pn-tng") ||
652 !strcasecmp (transport_protocol, "x-real-rdt")) {
7ecc634e 653 /* x-pn-tng/<protocol> */
e1502118
LB
654 get_word_sep(lower_transport, sizeof(lower_transport), "/;,", &p);
655 profile[0] = '\0';
119b4668 656 th->transport = RTSP_TRANSPORT_RDT;
1617ad97 657 }
b6892136 658 if (!strcasecmp(lower_transport, "TCP"))
90abbdba 659 th->lower_transport = RTSP_LOWER_TRANSPORT_TCP;
1617ad97 660 else
90abbdba 661 th->lower_transport = RTSP_LOWER_TRANSPORT_UDP;
115329f1 662
1617ad97
FB
663 if (*p == ';')
664 p++;
665 /* get each parameter */
666 while (*p != '\0' && *p != ',') {
667 get_word_sep(parameter, sizeof(parameter), "=;,", &p);
668 if (!strcmp(parameter, "port")) {
669 if (*p == '=') {
670 p++;
671 rtsp_parse_range(&th->port_min, &th->port_max, &p);
672 }
673 } else if (!strcmp(parameter, "client_port")) {
674 if (*p == '=') {
675 p++;
115329f1 676 rtsp_parse_range(&th->client_port_min,
1617ad97
FB
677 &th->client_port_max, &p);
678 }
679 } else if (!strcmp(parameter, "server_port")) {
680 if (*p == '=') {
681 p++;
115329f1 682 rtsp_parse_range(&th->server_port_min,
1617ad97
FB
683 &th->server_port_max, &p);
684 }
685 } else if (!strcmp(parameter, "interleaved")) {
686 if (*p == '=') {
687 p++;
115329f1 688 rtsp_parse_range(&th->interleaved_min,
1617ad97
FB
689 &th->interleaved_max, &p);
690 }
691 } else if (!strcmp(parameter, "multicast")) {
90abbdba
RB
692 if (th->lower_transport == RTSP_LOWER_TRANSPORT_UDP)
693 th->lower_transport = RTSP_LOWER_TRANSPORT_UDP_MULTICAST;
1617ad97
FB
694 } else if (!strcmp(parameter, "ttl")) {
695 if (*p == '=') {
696 p++;
697 th->ttl = strtol(p, (char **)&p, 10);
698 }
699 } else if (!strcmp(parameter, "destination")) {
1617ad97
FB
700 if (*p == '=') {
701 p++;
702 get_word_sep(buf, sizeof(buf), ";,", &p);
7934b15d 703 get_sockaddr(buf, &th->destination);
1617ad97 704 }
619298a8
JW
705 } else if (!strcmp(parameter, "source")) {
706 if (*p == '=') {
707 p++;
708 get_word_sep(buf, sizeof(buf), ";,", &p);
709 av_strlcpy(th->source, buf, sizeof(th->source));
710 }
1617ad97 711 }
619298a8 712
1617ad97
FB
713 while (*p != ';' && *p != '\0' && *p != ',')
714 p++;
715 if (*p == ';')
716 p++;
717 }
718 if (*p == ',')
719 p++;
720
721 reply->nb_transports++;
722 }
723}
724
29db7c3a
MS
725static void handle_rtp_info(RTSPState *rt, const char *url,
726 uint32_t seq, uint32_t rtptime)
727{
728 int i;
729 if (!rtptime || !url[0])
730 return;
731 if (rt->transport != RTSP_TRANSPORT_RTP)
732 return;
733 for (i = 0; i < rt->nb_rtsp_streams; i++) {
734 RTSPStream *rtsp_st = rt->rtsp_streams[i];
735 RTPDemuxContext *rtpctx = rtsp_st->transport_priv;
736 if (!rtpctx)
737 continue;
738 if (!strcmp(rtsp_st->control_url, url)) {
739 rtpctx->base_timestamp = rtptime;
740 break;
741 }
742 }
743}
744
745static void rtsp_parse_rtp_info(RTSPState *rt, const char *p)
746{
747 int read = 0;
748 char key[20], value[1024], url[1024] = "";
749 uint32_t seq = 0, rtptime = 0;
750
751 for (;;) {
752 p += strspn(p, SPACE_CHARS);
753 if (!*p)
754 break;
755 get_word_sep(key, sizeof(key), "=", &p);
756 if (*p != '=')
757 break;
758 p++;
759 get_word_sep(value, sizeof(value), ";, ", &p);
760 read++;
761 if (!strcmp(key, "url"))
762 av_strlcpy(url, value, sizeof(url));
763 else if (!strcmp(key, "seq"))
4515f9b5 764 seq = strtoul(value, NULL, 10);
29db7c3a 765 else if (!strcmp(key, "rtptime"))
4515f9b5 766 rtptime = strtoul(value, NULL, 10);
29db7c3a
MS
767 if (*p == ',') {
768 handle_rtp_info(rt, url, seq, rtptime);
769 url[0] = '\0';
770 seq = rtptime = 0;
771 read = 0;
772 }
773 if (*p)
774 p++;
775 }
776 if (read > 0)
777 handle_rtp_info(rt, url, seq, rtptime);
778}
779
2626308a 780void ff_rtsp_parse_line(RTSPMessageHeader *reply, const char *buf,
77223c53 781 RTSPState *rt, const char *method)
1617ad97
FB
782{
783 const char *p;
784
785 /* NOTE: we do case independent match for broken servers */
786 p = buf;
f7d78f36 787 if (av_stristart(p, "Session:", &p)) {
30e79845 788 int t;
1617ad97 789 get_word_sep(reply->session_id, sizeof(reply->session_id), ";", &p);
30e79845
RB
790 if (av_stristart(p, ";timeout=", &p) &&
791 (t = strtol(p, NULL, 10)) > 0) {
792 reply->timeout = t;
793 }
f7d78f36 794 } else if (av_stristart(p, "Content-Length:", &p)) {
1617ad97 795 reply->content_length = strtol(p, NULL, 10);
f7d78f36 796 } else if (av_stristart(p, "Transport:", &p)) {
1617ad97 797 rtsp_parse_transport(reply, p);
f7d78f36 798 } else if (av_stristart(p, "CSeq:", &p)) {
1617ad97 799 reply->seq = strtol(p, NULL, 10);
f7d78f36 800 } else if (av_stristart(p, "Range:", &p)) {
31693e00 801 rtsp_parse_range_npt(p, &reply->range_start, &reply->range_end);
30aa6aed 802 } else if (av_stristart(p, "RealChallenge1:", &p)) {
30619e6e 803 p += strspn(p, SPACE_CHARS);
30aa6aed 804 av_strlcpy(reply->real_challenge, p, sizeof(reply->real_challenge));
7a86bafa 805 } else if (av_stristart(p, "Server:", &p)) {
30619e6e 806 p += strspn(p, SPACE_CHARS);
7a86bafa 807 av_strlcpy(reply->server, p, sizeof(reply->server));
fccb1770
RB
808 } else if (av_stristart(p, "Notice:", &p) ||
809 av_stristart(p, "X-Notice:", &p)) {
810 reply->notice = strtol(p, NULL, 10);
d243ba30 811 } else if (av_stristart(p, "Location:", &p)) {
30619e6e 812 p += strspn(p, SPACE_CHARS);
d243ba30 813 av_strlcpy(reply->location, p , sizeof(reply->location));
acc9ed14 814 } else if (av_stristart(p, "WWW-Authenticate:", &p) && rt) {
30619e6e 815 p += strspn(p, SPACE_CHARS);
acc9ed14
MS
816 ff_http_auth_handle_header(&rt->auth_state, "WWW-Authenticate", p);
817 } else if (av_stristart(p, "Authentication-Info:", &p) && rt) {
30619e6e 818 p += strspn(p, SPACE_CHARS);
acc9ed14 819 ff_http_auth_handle_header(&rt->auth_state, "Authentication-Info", p);
d2995eb9 820 } else if (av_stristart(p, "Content-Base:", &p) && rt) {
dd22cfb1 821 p += strspn(p, SPACE_CHARS);
d2995eb9
MS
822 if (method && !strcmp(method, "DESCRIBE"))
823 av_strlcpy(rt->control_uri, p , sizeof(rt->control_uri));
29db7c3a
MS
824 } else if (av_stristart(p, "RTP-Info:", &p) && rt) {
825 p += strspn(p, SPACE_CHARS);
826 if (method && !strcmp(method, "PLAY"))
827 rtsp_parse_rtp_info(rt, p);
0b4949b5
MS
828 } else if (av_stristart(p, "Public:", &p) && rt) {
829 if (strstr(p, "GET_PARAMETER") &&
830 method && !strcmp(method, "OPTIONS"))
831 rt->get_parameter_supported = 1;
30eae325
MS
832 } else if (av_stristart(p, "x-Accept-Dynamic-Rate:", &p) && rt) {
833 p += strspn(p, SPACE_CHARS);
834 rt->accept_dynamic_rate = atoi(p);
1617ad97
FB
835 }
836}
837
b7b8fc34 838/* skip a RTP/TCP interleaved packet */
ec55edba 839void ff_rtsp_skip_packet(AVFormatContext *s)
b7b8fc34
FB
840{
841 RTSPState *rt = s->priv_data;
842 int ret, len, len1;
843 uint8_t buf[1024];
844
dce37564 845 ret = ffurl_read_complete(rt->rtsp_hd, buf, 3);
b7b8fc34
FB
846 if (ret != 3)
847 return;
80fb8234 848 len = AV_RB16(buf + 1);
67c9cd69 849
dfd2a005 850 av_dlog(s, "skipping RTP packet len=%d\n", len);
67c9cd69 851
b7b8fc34
FB
852 /* skip payload */
853 while (len > 0) {
854 len1 = len;
855 if (len1 > sizeof(buf))
856 len1 = sizeof(buf);
dce37564 857 ret = ffurl_read_complete(rt->rtsp_hd, buf, len1);
b7b8fc34
FB
858 if (ret != len1)
859 return;
860 len -= len1;
861 }
862}
1617ad97 863
3307e6ea 864int ff_rtsp_read_reply(AVFormatContext *s, RTSPMessageHeader *reply,
93993933 865 unsigned char **content_ptr,
3df54c6b 866 int return_on_interleaved_data, const char *method)
1617ad97
FB
867{
868 RTSPState *rt = s->priv_data;
869 char buf[4096], buf1[1024], *q;
870 unsigned char ch;
871 const char *p;
7e726132 872 int ret, content_length, line_count = 0;
1617ad97
FB
873 unsigned char *content = NULL;
874
d541a7d2 875 memset(reply, 0, sizeof(*reply));
1617ad97 876
1617ad97 877 /* parse reply (XXX: use buffers) */
1617ad97 878 rt->last_reply[0] = '\0';
c8965800 879 for (;;) {
1617ad97 880 q = buf;
c8965800 881 for (;;) {
dce37564 882 ret = ffurl_read_complete(rt->rtsp_hd, &ch, 1);
dfd2a005 883 av_dlog(s, "ret=%d c=%02x [%c]\n", ret, ch, ch);
7e726132 884 if (ret != 1)
2401660d 885 return AVERROR_EOF;
1617ad97
FB
886 if (ch == '\n')
887 break;
b7b8fc34
FB
888 if (ch == '$') {
889 /* XXX: only parse it if first char on line ? */
7e726132
RB
890 if (return_on_interleaved_data) {
891 return 1;
892 } else
ec55edba 893 ff_rtsp_skip_packet(s);
b7b8fc34 894 } else if (ch != '\r') {
1617ad97
FB
895 if ((q - buf) < sizeof(buf) - 1)
896 *q++ = ch;
897 }
898 }
899 *q = '\0';
67c9cd69 900
dfd2a005 901 av_dlog(s, "line='%s'\n", buf);
67c9cd69 902
1617ad97
FB
903 /* test if last line */
904 if (buf[0] == '\0')
905 break;
906 p = buf;
907 if (line_count == 0) {
908 /* get reply code */
909 get_word(buf1, sizeof(buf1), &p);
910 get_word(buf1, sizeof(buf1), &p);
911 reply->status_code = atoi(buf1);
d93fdcbf 912 av_strlcpy(reply->reason, p, sizeof(reply->reason));
1617ad97 913 } else {
77223c53 914 ff_rtsp_parse_line(reply, p, rt, method);
75e61b0e
MR
915 av_strlcat(rt->last_reply, p, sizeof(rt->last_reply));
916 av_strlcat(rt->last_reply, "\n", sizeof(rt->last_reply));
1617ad97
FB
917 }
918 line_count++;
919 }
115329f1 920
1617ad97 921 if (rt->session_id[0] == '\0' && reply->session_id[0] != '\0')
75e61b0e 922 av_strlcpy(rt->session_id, reply->session_id, sizeof(rt->session_id));
115329f1 923
1617ad97
FB
924 content_length = reply->content_length;
925 if (content_length > 0) {
926 /* leave some room for a trailing '\0' (useful for simple parsing) */
927 content = av_malloc(content_length + 1);
dce37564 928 ffurl_read_complete(rt->rtsp_hd, content, content_length);
1617ad97
FB
929 content[content_length] = '\0';
930 }
931 if (content_ptr)
932 *content_ptr = content;
e2e2e7dd
AB
933 else
934 av_free(content);
7e726132 935
7ed8211b
LB
936 if (rt->seq != reply->seq) {
937 av_log(s, AV_LOG_WARNING, "CSeq %d expected, %d received.\n",
938 rt->seq, reply->seq);
939 }
940
fccb1770
RB
941 /* EOS */
942 if (reply->notice == 2101 /* End-of-Stream Reached */ ||
943 reply->notice == 2104 /* Start-of-Stream Reached */ ||
c8965800 944 reply->notice == 2306 /* Continuous Feed Terminated */) {
fccb1770 945 rt->state = RTSP_STATE_IDLE;
c8965800 946 } else if (reply->notice >= 4400 && reply->notice < 5500) {
fccb1770 947 return AVERROR(EIO); /* data or server error */
c8965800 948 } else if (reply->notice == 2401 /* Ticket Expired */ ||
fccb1770
RB
949 (reply->notice >= 5500 && reply->notice < 5600) /* end of term */ )
950 return AVERROR(EPERM);
951
7e726132 952 return 0;
1617ad97
FB
953}
954
57c4d01e
DEP
955/**
956 * Send a command to the RTSP server without waiting for the reply.
957 *
958 * @param s RTSP (de)muxer context
959 * @param method the method for the request
960 * @param url the target url for the request
961 * @param headers extra header lines to include in the request
962 * @param send_content if non-null, the data to send as request body content
963 * @param send_content_length the length of the send_content data, or 0 if
964 * send_content is null
965 *
966 * @return zero if success, nonzero otherwise
967 */
968static int ff_rtsp_send_cmd_with_content_async(AVFormatContext *s,
969 const char *method, const char *url,
970 const char *headers,
971 const unsigned char *send_content,
972 int send_content_length)
29b9f58b
RB
973{
974 RTSPState *rt = s->priv_data;
f5d33f52
JA
975 char buf[4096], *out_buf;
976 char base64buf[AV_BASE64_SIZE(sizeof(buf))];
29b9f58b 977
f5d33f52
JA
978 /* Add in RTSP headers */
979 out_buf = buf;
29b9f58b 980 rt->seq++;
b17d11c6
MS
981 snprintf(buf, sizeof(buf), "%s %s RTSP/1.0\r\n", method, url);
982 if (headers)
983 av_strlcat(buf, headers, sizeof(buf));
7b4a3645 984 av_strlcatf(buf, sizeof(buf), "CSeq: %d\r\n", rt->seq);
b17d11c6
MS
985 if (rt->session_id[0] != '\0' && (!headers ||
986 !strstr(headers, "\nIf-Match:"))) {
7b4a3645 987 av_strlcatf(buf, sizeof(buf), "Session: %s\r\n", rt->session_id);
29b9f58b 988 }
aa8bf2fb
MS
989 if (rt->auth[0]) {
990 char *str = ff_http_auth_create_response(&rt->auth_state,
991 rt->auth, url, method);
992 if (str)
993 av_strlcat(buf, str, sizeof(buf));
994 av_free(str);
995 }
dfd017bf
MS
996 if (send_content_length > 0 && send_content)
997 av_strlcatf(buf, sizeof(buf), "Content-Length: %d\r\n", send_content_length);
29b9f58b 998 av_strlcat(buf, "\r\n", sizeof(buf));
67c9cd69 999
f5d33f52
JA
1000 /* base64 encode rtsp if tunneling */
1001 if (rt->control_transport == RTSP_MODE_TUNNEL) {
1002 av_base64_encode(base64buf, sizeof(base64buf), buf, strlen(buf));
1003 out_buf = base64buf;
1004 }
1005
dfd2a005 1006 av_dlog(s, "Sending:\n%s--\n", buf);
67c9cd69 1007
925e908b 1008 ffurl_write(rt->rtsp_hd_out, out_buf, strlen(out_buf));
f5d33f52
JA
1009 if (send_content_length > 0 && send_content) {
1010 if (rt->control_transport == RTSP_MODE_TUNNEL) {
1011 av_log(s, AV_LOG_ERROR, "tunneling of RTSP requests "
1012 "with content data not supported\n");
1013 return AVERROR_PATCHWELCOME;
1014 }
925e908b 1015 ffurl_write(rt->rtsp_hd_out, send_content, send_content_length);
f5d33f52 1016 }
30e79845 1017 rt->last_cmd_time = av_gettime();
d0382374
JA
1018
1019 return 0;
30e79845
RB
1020}
1021
d0382374 1022int ff_rtsp_send_cmd_async(AVFormatContext *s, const char *method,
fc490fcf 1023 const char *url, const char *headers)
dfd017bf 1024{
d0382374 1025 return ff_rtsp_send_cmd_with_content_async(s, method, url, headers, NULL, 0);
dfd017bf
MS
1026}
1027
d0382374 1028int ff_rtsp_send_cmd(AVFormatContext *s, const char *method, const char *url,
fc490fcf
MS
1029 const char *headers, RTSPMessageHeader *reply,
1030 unsigned char **content_ptr)
30e79845 1031{
d0382374 1032 return ff_rtsp_send_cmd_with_content(s, method, url, headers, reply,
fc490fcf 1033 content_ptr, NULL, 0);
29b9f58b
RB
1034}
1035
d0382374 1036int ff_rtsp_send_cmd_with_content(AVFormatContext *s,
fc490fcf
MS
1037 const char *method, const char *url,
1038 const char *header,
1039 RTSPMessageHeader *reply,
1040 unsigned char **content_ptr,
1041 const unsigned char *send_content,
1042 int send_content_length)
dfd017bf 1043{
30af0779
MS
1044 RTSPState *rt = s->priv_data;
1045 HTTPAuthType cur_auth_type;
d0382374 1046 int ret;
30af0779
MS
1047
1048retry:
1049 cur_auth_type = rt->auth_state.auth_type;
d0382374 1050 if ((ret = ff_rtsp_send_cmd_with_content_async(s, method, url, header,
fc490fcf
MS
1051 send_content,
1052 send_content_length)))
d0382374 1053 return ret;
dfd017bf 1054
3df54c6b 1055 if ((ret = ff_rtsp_read_reply(s, reply, content_ptr, 0, method) ) < 0)
d0382374 1056 return ret;
30af0779
MS
1057
1058 if (reply->status_code == 401 && cur_auth_type == HTTP_AUTH_NONE &&
1059 rt->auth_state.auth_type != HTTP_AUTH_NONE)
1060 goto retry;
d0382374 1061
bf55cf19 1062 if (reply->status_code > 400){
d93fdcbf 1063 av_log(s, AV_LOG_ERROR, "method %s failed: %d%s\n",
bf55cf19 1064 method,
d93fdcbf
LB
1065 reply->status_code,
1066 reply->reason);
bf55cf19
LB
1067 av_log(s, AV_LOG_DEBUG, "%s\n", rt->last_reply);
1068 }
1069
d0382374 1070 return 0;
dfd017bf
MS
1071}
1072
fef5649a 1073int ff_rtsp_make_setup_request(AVFormatContext *s, const char *host, int port,
c8965800 1074 int lower_transport, const char *real_challenge)
1617ad97
FB
1075{
1076 RTSPState *rt = s->priv_data;
f830c9a4 1077 int rtx, j, i, err, interleave = 0;
1617ad97 1078 RTSPStream *rtsp_st;
a9e534d5 1079 RTSPMessageHeader reply1, *reply = &reply1;
53620bba 1080 char cmd[2048];
e9dea59f
RB
1081 const char *trans_pref;
1082
119b4668 1083 if (rt->transport == RTSP_TRANSPORT_RDT)
e9dea59f
RB
1084 trans_pref = "x-pn-tng";
1085 else
1086 trans_pref = "RTP/AVP";
115329f1 1087
30e79845
RB
1088 /* default timeout: 1 minute */
1089 rt->timeout = 60;
1090
1617ad97
FB
1091 /* for each stream, make the setup request */
1092 /* XXX: we assume the same server is used for the control of each
c8965800 1093 * RTSP stream */
d1ccf0e0 1094
c8965800 1095 for (j = RTSP_RTP_PORT_MIN, i = 0; i < rt->nb_rtsp_streams; ++i) {
1617ad97
FB
1096 char transport[2048];
1097
f75e3da5 1098 /*
f830c9a4
RB
1099 * WMS serves all UDP data over a single connection, the RTX, which
1100 * isn't necessarily the first in the SDP but has to be the first
1101 * to be set up, else the second/third SETUP will fail with a 461.
1102 */
1103 if (lower_transport == RTSP_LOWER_TRANSPORT_UDP &&
1104 rt->server_type == RTSP_SERVER_WMS) {
1105 if (i == 0) {
1106 /* rtx first */
1107 for (rtx = 0; rtx < rt->nb_rtsp_streams; rtx++) {
1108 int len = strlen(rt->rtsp_streams[rtx]->control_url);
1109 if (len >= 4 &&
c8965800
RB
1110 !strcmp(rt->rtsp_streams[rtx]->control_url + len - 4,
1111 "/rtx"))
f830c9a4
RB
1112 break;
1113 }
1114 if (rtx == rt->nb_rtsp_streams)
1115 return -1; /* no RTX found */
1116 rtsp_st = rt->rtsp_streams[rtx];
1117 } else
1118 rtsp_st = rt->rtsp_streams[i > rtx ? i : i - 1];
1119 } else
2fea9650 1120 rtsp_st = rt->rtsp_streams[i];
1617ad97 1121
1617ad97 1122 /* RTP/UDP */
90abbdba 1123 if (lower_transport == RTSP_LOWER_TRANSPORT_UDP) {
85fb7b34 1124 char buf[256];
85fb7b34 1125
f830c9a4
RB
1126 if (rt->server_type == RTSP_SERVER_WMS && i > 1) {
1127 port = reply->transports[0].client_port_min;
1128 goto have_port;
1129 }
1130
85fb7b34 1131 /* first try in specified port range */
d1ccf0e0 1132 if (RTSP_RTP_PORT_MIN != 0) {
c8965800 1133 while (j <= RTSP_RTP_PORT_MAX) {
57b5555c
MS
1134 ff_url_join(buf, sizeof(buf), "rtp", NULL, host, -1,
1135 "?localport=%d", j);
c8965800
RB
1136 /* we will use two ports per rtp stream (rtp and rtcp) */
1137 j += 2;
59d96941 1138 if (ffurl_open(&rtsp_st->rtp_handle, buf, AVIO_FLAG_READ_WRITE) == 0)
85fb7b34
FB
1139 goto rtp_opened;
1140 }
1617ad97 1141 }
85fb7b34 1142
a3b058b7
MS
1143 av_log(s, AV_LOG_ERROR, "Unable to open an input RTP port\n");
1144 err = AVERROR(EIO);
1145 goto fail;
85fb7b34
FB
1146
1147 rtp_opened:
bfc6db44 1148 port = ff_rtp_get_local_rtp_port(rtsp_st->rtp_handle);
f830c9a4 1149 have_port:
0ad306bc 1150 snprintf(transport, sizeof(transport) - 1,
eee2cbff
RB
1151 "%s/UDP;", trans_pref);
1152 if (rt->server_type != RTSP_SERVER_REAL)
1153 av_strlcat(transport, "unicast;", sizeof(transport));
1154 av_strlcatf(transport, sizeof(transport),
1155 "client_port=%d", port);
f830c9a4
RB
1156 if (rt->transport == RTSP_TRANSPORT_RTP &&
1157 !(rt->server_type == RTSP_SERVER_WMS && i > 0))
e9dea59f 1158 av_strlcatf(transport, sizeof(transport), "-%d", port + 1);
1617ad97
FB
1159 }
1160
1161 /* RTP/TCP */
90abbdba 1162 else if (lower_transport == RTSP_LOWER_TRANSPORT_TCP) {
f75e3da5 1163 /* For WMS streams, the application streams are only used for
090438cc
RB
1164 * UDP. When trying to set it up for TCP streams, the server
1165 * will return an error. Therefore, we skip those streams. */
1166 if (rt->server_type == RTSP_SERVER_WMS &&
c8965800 1167 s->streams[rtsp_st->stream_index]->codec->codec_type ==
72415b2a 1168 AVMEDIA_TYPE_DATA)
090438cc 1169 continue;
0ad306bc 1170 snprintf(transport, sizeof(transport) - 1,
d1c6e47c 1171 "%s/TCP;", trans_pref);
895678f8 1172 if (rt->transport != RTSP_TRANSPORT_RDT)
d1c6e47c
RB
1173 av_strlcat(transport, "unicast;", sizeof(transport));
1174 av_strlcatf(transport, sizeof(transport),
1175 "interleaved=%d-%d",
1176 interleave, interleave + 1);
1177 interleave += 2;
1617ad97
FB
1178 }
1179
90abbdba 1180 else if (lower_transport == RTSP_LOWER_TRANSPORT_UDP_MULTICAST) {
0ad306bc 1181 snprintf(transport, sizeof(transport) - 1,
e9dea59f 1182 "%s/UDP;multicast", trans_pref);
1617ad97 1183 }
69adcc4f
MS
1184 if (s->oformat) {
1185 av_strlcat(transport, ";mode=receive", sizeof(transport));
1186 } else if (rt->server_type == RTSP_SERVER_REAL ||
2efc97c2 1187 rt->server_type == RTSP_SERVER_WMS)
e9dea59f 1188 av_strlcat(transport, ";mode=play", sizeof(transport));
115329f1 1189 snprintf(cmd, sizeof(cmd),
b6892136 1190 "Transport: %s\r\n",
b17d11c6 1191 transport);
76b0d03d
MS
1192 if (rt->accept_dynamic_rate)
1193 av_strlcat(cmd, "x-Dynamic-Rate: 0\r\n", sizeof(cmd));
44b70ce5 1194 if (i == 0 && rt->server_type == RTSP_SERVER_REAL && CONFIG_RTPDEC) {
e9dea59f
RB
1195 char real_res[41], real_csum[9];
1196 ff_rdt_calc_response_and_checksum(real_res, real_csum,
1197 real_challenge);
1198 av_strlcatf(cmd, sizeof(cmd),
1199 "If-Match: %s\r\n"
1200 "RealChallenge2: %s, sd=%s\r\n",
1201 rt->session_id, real_res, real_csum);
1202 }
b17d11c6 1203 ff_rtsp_send_cmd(s, "SETUP", rtsp_st->control_url, cmd, reply, NULL);
8a8754d8
RB
1204 if (reply->status_code == 461 /* Unsupported protocol */ && i == 0) {
1205 err = 1;
1206 goto fail;
7e6ca34f
RB
1207 } else if (reply->status_code != RTSP_STATUS_OK ||
1208 reply->nb_transports != 1) {
1617ad97
FB
1209 err = AVERROR_INVALIDDATA;
1210 goto fail;
1211 }
1212
1213 /* XXX: same protocol for all streams is required */
1214 if (i > 0) {
119b4668
RB
1215 if (reply->transports[0].lower_transport != rt->lower_transport ||
1216 reply->transports[0].transport != rt->transport) {
1617ad97
FB
1217 err = AVERROR_INVALIDDATA;
1218 goto fail;
1219 }
1220 } else {
90abbdba 1221 rt->lower_transport = reply->transports[0].lower_transport;
119b4668 1222 rt->transport = reply->transports[0].transport;
1617ad97
FB
1223 }
1224
8c579c1c
MS
1225 /* Fail if the server responded with another lower transport mode
1226 * than what we requested. */
1227 if (reply->transports[0].lower_transport != lower_transport) {
1228 av_log(s, AV_LOG_ERROR, "Nonmatching transport in server reply\n");
1229 err = AVERROR_INVALIDDATA;
1230 goto fail;
1617ad97
FB
1231 }
1232
90abbdba
RB
1233 switch(reply->transports[0].lower_transport) {
1234 case RTSP_LOWER_TRANSPORT_TCP:
1617ad97
FB
1235 rtsp_st->interleaved_min = reply->transports[0].interleaved_min;
1236 rtsp_st->interleaved_max = reply->transports[0].interleaved_max;
1237 break;
115329f1 1238
c8965800 1239 case RTSP_LOWER_TRANSPORT_UDP: {
a92c30d7 1240 char url[1024], options[30] = "";
c8965800 1241
9867aea5 1242 if (rt->rtsp_flags & RTSP_FLAG_FILTER_SRC)
a92c30d7 1243 av_strlcpy(options, "?connect=1", sizeof(options));
619298a8
JW
1244 /* Use source address if specified */
1245 if (reply->transports[0].source[0]) {
1246 ff_url_join(url, sizeof(url), "rtp", NULL,
1247 reply->transports[0].source,
d8407339 1248 reply->transports[0].server_port_min, "%s", options);
619298a8 1249 } else {
7bac991f 1250 ff_url_join(url, sizeof(url), "rtp", NULL, host,
d8407339 1251 reply->transports[0].server_port_min, "%s", options);
619298a8 1252 }
c8965800 1253 if (!(rt->server_type == RTSP_SERVER_WMS && i > 1) &&
bfc6db44 1254 ff_rtp_set_remote_url(rtsp_st->rtp_handle, url) < 0) {
c8965800
RB
1255 err = AVERROR_INVALIDDATA;
1256 goto fail;
1617ad97 1257 }
9c8fa20d
MS
1258 /* Try to initialize the connection state in a
1259 * potential NAT router by sending dummy packets.
1260 * RTP/RTCP dummy packets are used for RDT, too.
1261 */
44b70ce5
MS
1262 if (!(rt->server_type == RTSP_SERVER_WMS && i > 1) && s->iformat &&
1263 CONFIG_RTPDEC)
bfc6db44 1264 ff_rtp_send_punch_packets(rtsp_st->rtp_handle);
1617ad97 1265 break;
c8965800
RB
1266 }
1267 case RTSP_LOWER_TRANSPORT_UDP_MULTICAST: {
7934b15d
MS
1268 char url[1024], namebuf[50];
1269 struct sockaddr_storage addr;
c8965800
RB
1270 int port, ttl;
1271
7934b15d
MS
1272 if (reply->transports[0].destination.ss_family) {
1273 addr = reply->transports[0].destination;
c8965800
RB
1274 port = reply->transports[0].port_min;
1275 ttl = reply->transports[0].ttl;
1276 } else {
7934b15d 1277 addr = rtsp_st->sdp_ip;
c8965800
RB
1278 port = rtsp_st->sdp_port;
1279 ttl = rtsp_st->sdp_ttl;
1280 }
7934b15d
MS
1281 getnameinfo((struct sockaddr*) &addr, sizeof(addr),
1282 namebuf, sizeof(namebuf), NULL, 0, NI_NUMERICHOST);
1283 ff_url_join(url, sizeof(url), "rtp", NULL, namebuf,
57b5555c 1284 port, "?ttl=%d", ttl);
59d96941 1285 if (ffurl_open(&rtsp_st->rtp_handle, url, AVIO_FLAG_READ_WRITE) < 0) {
c8965800
RB
1286 err = AVERROR_INVALIDDATA;
1287 goto fail;
1617ad97
FB
1288 }
1289 break;
1290 }
c8965800 1291 }
d1ccf0e0 1292
ee0cb67f 1293 if ((err = rtsp_open_transport_ctx(s, rtsp_st)))
8b1ab7bf 1294 goto fail;
1617ad97
FB
1295 }
1296
30e79845
RB
1297 if (reply->timeout > 0)
1298 rt->timeout = reply->timeout;
1299
2e889ae4 1300 if (rt->server_type == RTSP_SERVER_REAL)
1256d16b
RB
1301 rt->need_subscription = 1;
1302
53620bba
RB
1303 return 0;
1304
1305fail:
aeb2de1c 1306 ff_rtsp_undo_setup(s);
53620bba
RB
1307 return err;
1308}
1309
b8c2c41d
JA
1310void ff_rtsp_close_connections(AVFormatContext *s)
1311{
1312 RTSPState *rt = s->priv_data;
e52a9145
AK
1313 if (rt->rtsp_hd_out != rt->rtsp_hd) ffurl_close(rt->rtsp_hd_out);
1314 ffurl_close(rt->rtsp_hd);
6217b645 1315 rt->rtsp_hd = rt->rtsp_hd_out = NULL;
b8c2c41d
JA
1316}
1317
3307e6ea 1318int ff_rtsp_connect(AVFormatContext *s)
53620bba
RB
1319{
1320 RTSPState *rt = s->priv_data;
921da217
LB
1321 char host[1024], path[1024], tcpname[1024], cmd[2048], auth[128];
1322 char *option_list, *option, *filename;
03f8fc08 1323 int port, err, tcp_fd;
354b7573 1324 RTSPMessageHeader reply1 = {0}, *reply = &reply1;
90abbdba 1325 int lower_transport_mask = 0;
2762a7a2 1326 char real_challenge[64] = "";
03f8fc08
MS
1327 struct sockaddr_storage peer;
1328 socklen_t peer_len = sizeof(peer);
57b5555c
MS
1329
1330 if (!ff_network_init())
1331 return AVERROR(EIO);
eca4850c 1332
f5d33f52 1333 rt->control_transport = RTSP_MODE_PLAIN;
eca4850c
MS
1334 if (rt->lower_transport_mask & (1 << RTSP_LOWER_TRANSPORT_HTTP)) {
1335 rt->lower_transport_mask = 1 << RTSP_LOWER_TRANSPORT_TCP;
1336 rt->control_transport = RTSP_MODE_TUNNEL;
1337 }
1338 /* Only pass through valid flags from here */
1339 rt->lower_transport_mask &= (1 << RTSP_LOWER_TRANSPORT_NB) - 1;
eca4850c
MS
1340
1341redirect:
1342 lower_transport_mask = rt->lower_transport_mask;
53620bba 1343 /* extract hostname and port */
f3bfe388 1344 av_url_split(NULL, 0, auth, sizeof(auth),
f984dcf6 1345 host, sizeof(host), &port, path, sizeof(path), s->filename);
f9337897 1346 if (*auth) {
aa8bf2fb 1347 av_strlcpy(rt->auth, auth, sizeof(rt->auth));
f9337897 1348 }
53620bba
RB
1349 if (port < 0)
1350 port = RTSP_DEFAULT_PORT;
1351
eca4850c 1352#if FF_API_RTSP_URL_OPTIONS
53620bba 1353 /* search for options */
602eb779 1354 option_list = strrchr(path, '?');
53620bba 1355 if (option_list) {
2a21adf9
MS
1356 /* Strip out the RTSP specific options, write out the rest of
1357 * the options back into the same string. */
1358 filename = option_list;
c8965800 1359 while (option_list) {
eca4850c 1360 int handled = 1;
53620bba 1361 /* move the option pointer */
921da217 1362 option = ++option_list;
53620bba
RB
1363 option_list = strchr(option_list, '&');
1364 if (option_list)
921da217
LB
1365 *option_list = 0;
1366
53620bba 1367 /* handle the options */
c8965800 1368 if (!strcmp(option, "udp")) {
7a033e08 1369 lower_transport_mask |= (1<< RTSP_LOWER_TRANSPORT_UDP);
c8965800 1370 } else if (!strcmp(option, "multicast")) {
7a033e08 1371 lower_transport_mask |= (1<< RTSP_LOWER_TRANSPORT_UDP_MULTICAST);
c8965800 1372 } else if (!strcmp(option, "tcp")) {
7a033e08 1373 lower_transport_mask |= (1<< RTSP_LOWER_TRANSPORT_TCP);
f5d33f52
JA
1374 } else if(!strcmp(option, "http")) {
1375 lower_transport_mask |= (1<< RTSP_LOWER_TRANSPORT_TCP);
1376 rt->control_transport = RTSP_MODE_TUNNEL;
a92c30d7 1377 } else if (!strcmp(option, "filter_src")) {
9867aea5 1378 rt->rtsp_flags |= RTSP_FLAG_FILTER_SRC;
c8965800 1379 } else {
2a21adf9
MS
1380 /* Write options back into the buffer, using memmove instead
1381 * of strcpy since the strings may overlap. */
1382 int len = strlen(option);
1383 memmove(++filename, option, len);
1384 filename += len;
921da217 1385 if (option_list) *filename = '&';
eca4850c 1386 handled = 0;
921da217 1387 }
eca4850c
MS
1388 if (handled)
1389 av_log(s, AV_LOG_WARNING, "Options passed via URL are "
1390 "deprecated, use -rtsp_transport "
1391 "and -rtsp_flags instead.\n");
53620bba 1392 }
921da217 1393 *filename = 0;
53620bba 1394 }
eca4850c 1395#endif
53620bba 1396
90abbdba 1397 if (!lower_transport_mask)
2a1d51c5 1398 lower_transport_mask = (1 << RTSP_LOWER_TRANSPORT_NB) - 1;
53620bba 1399
3e24c770 1400 if (s->oformat) {
b7dc88fc
MS
1401 /* Only UDP or TCP - UDP multicast isn't supported. */
1402 lower_transport_mask &= (1 << RTSP_LOWER_TRANSPORT_UDP) |
1403 (1 << RTSP_LOWER_TRANSPORT_TCP);
f5d33f52 1404 if (!lower_transport_mask || rt->control_transport == RTSP_MODE_TUNNEL) {
3e24c770 1405 av_log(s, AV_LOG_ERROR, "Unsupported lower transport method, "
b7dc88fc 1406 "only UDP and TCP are supported for output.\n");
3e24c770
MS
1407 err = AVERROR(EINVAL);
1408 goto fail;
1409 }
1410 }
1411
4bc5cc23
MS
1412 /* Construct the URI used in request; this is similar to s->filename,
1413 * but with authentication credentials removed and RTSP specific options
1414 * stripped out. */
1415 ff_url_join(rt->control_uri, sizeof(rt->control_uri), "rtsp", NULL,
1416 host, port, "%s", path);
1417
f5d33f52
JA
1418 if (rt->control_transport == RTSP_MODE_TUNNEL) {
1419 /* set up initial handshake for tunneling */
1420 char httpname[1024];
1421 char sessioncookie[17];
1422 char headers[1024];
1423
10ed37b5 1424 ff_url_join(httpname, sizeof(httpname), "http", auth, host, port, "%s", path);
f5d33f52
JA
1425 snprintf(sessioncookie, sizeof(sessioncookie), "%08x%08x",
1426 av_get_random_seed(), av_get_random_seed());
1427
1428 /* GET requests */
59d96941 1429 if (ffurl_alloc(&rt->rtsp_hd, httpname, AVIO_FLAG_READ) < 0) {
f5d33f52
JA
1430 err = AVERROR(EIO);
1431 goto fail;
1432 }
1433
1434 /* generate GET headers */
1435 snprintf(headers, sizeof(headers),
1436 "x-sessioncookie: %s\r\n"
1437 "Accept: application/x-rtsp-tunnelled\r\n"
1438 "Pragma: no-cache\r\n"
1439 "Cache-Control: no-cache\r\n",
1440 sessioncookie);
00e4a1f4 1441 ff_http_set_headers(rt->rtsp_hd, headers);
f5d33f52
JA
1442
1443 /* complete the connection */
62eaaeac 1444 if (ffurl_connect(rt->rtsp_hd)) {
f5d33f52
JA
1445 err = AVERROR(EIO);
1446 goto fail;
1447 }
1448
1449 /* POST requests */
59d96941 1450 if (ffurl_alloc(&rt->rtsp_hd_out, httpname, AVIO_FLAG_WRITE) < 0 ) {
f5d33f52
JA
1451 err = AVERROR(EIO);
1452 goto fail;
1453 }
1454
1455 /* generate POST headers */
1456 snprintf(headers, sizeof(headers),
1457 "x-sessioncookie: %s\r\n"
1458 "Content-Type: application/x-rtsp-tunnelled\r\n"
1459 "Pragma: no-cache\r\n"
1460 "Cache-Control: no-cache\r\n"
1461 "Content-Length: 32767\r\n"
1462 "Expires: Sun, 9 Jan 1972 00:00:00 GMT\r\n",
1463 sessioncookie);
00e4a1f4
JA
1464 ff_http_set_headers(rt->rtsp_hd_out, headers);
1465 ff_http_set_chunked_transfer_encoding(rt->rtsp_hd_out, 0);
f5d33f52 1466
a8ead332
MS
1467 /* Initialize the authentication state for the POST session. The HTTP
1468 * protocol implementation doesn't properly handle multi-pass
1469 * authentication for POST requests, since it would require one of
1470 * the following:
1471 * - implementing Expect: 100-continue, which many HTTP servers
1472 * don't support anyway, even less the RTSP servers that do HTTP
1473 * tunneling
1474 * - sending the whole POST data until getting a 401 reply specifying
1475 * what authentication method to use, then resending all that data
1476 * - waiting for potential 401 replies directly after sending the
1477 * POST header (waiting for some unspecified time)
1478 * Therefore, we copy the full auth state, which works for both basic
1479 * and digest. (For digest, we would have to synchronize the nonce
1480 * count variable between the two sessions, if we'd do more requests
1481 * with the original session, though.)
1482 */
1483 ff_http_init_auth_state(rt->rtsp_hd_out, rt->rtsp_hd);
1484
9290f15d 1485 /* complete the connection */
62eaaeac 1486 if (ffurl_connect(rt->rtsp_hd_out)) {
9290f15d
MS
1487 err = AVERROR(EIO);
1488 goto fail;
1489 }
f5d33f52 1490 } else {
48e77473 1491 /* open the tcp connection */
41874d0a 1492 ff_url_join(tcpname, sizeof(tcpname), "tcp", NULL, host, port, NULL);
59d96941 1493 if (ffurl_open(&rt->rtsp_hd, tcpname, AVIO_FLAG_READ_WRITE) < 0) {
41874d0a
JA
1494 err = AVERROR(EIO);
1495 goto fail;
1496 }
00e4a1f4 1497 rt->rtsp_hd_out = rt->rtsp_hd;
f5d33f52 1498 }
53620bba
RB
1499 rt->seq = 0;
1500
1869ea03 1501 tcp_fd = ffurl_get_file_handle(rt->rtsp_hd);
03f8fc08
MS
1502 if (!getpeername(tcp_fd, (struct sockaddr*) &peer, &peer_len)) {
1503 getnameinfo((struct sockaddr*) &peer, peer_len, host, sizeof(host),
1504 NULL, 0, NI_NUMERICHOST);
1505 }
1506
c8965800
RB
1507 /* request options supported by the server; this also detects server
1508 * type */
1cf151e9 1509 for (rt->server_type = RTSP_SERVER_RTP;;) {
b17d11c6 1510 cmd[0] = 0;
2e889ae4 1511 if (rt->server_type == RTSP_SERVER_REAL)
1cf151e9 1512 av_strlcat(cmd,
f75e3da5 1513 /*
1cf151e9
RB
1514 * The following entries are required for proper
1515 * streaming from a Realmedia server. They are
1516 * interdependent in some way although we currently
1517 * don't quite understand how. Values were copied
1518 * from mplayer SVN r23589.
f75e3da5
DB
1519 * ClientChallenge is a 16-byte ID in hex
1520 * CompanyID is a 16-byte ID in base64
1cf151e9
RB
1521 */
1522 "ClientChallenge: 9e26d33f2984236010ef6253fb1887f7\r\n"
1523 "PlayerStarttime: [28/03/2003:22:50:23 00:00]\r\n"
1524 "CompanyID: KnKV4M4I/B2FjJ1TToLycw==\r\n"
1525 "GUID: 00000000-0000-0000-0000-000000000000\r\n",
1526 sizeof(cmd));
b17d11c6 1527 ff_rtsp_send_cmd(s, "OPTIONS", rt->control_uri, cmd, reply, NULL);
1cf151e9
RB
1528 if (reply->status_code != RTSP_STATUS_OK) {
1529 err = AVERROR_INVALIDDATA;
1530 goto fail;
1531 }
1532
1533 /* detect server type if not standard-compliant RTP */
2e889ae4
RB
1534 if (rt->server_type != RTSP_SERVER_REAL && reply->real_challenge[0]) {
1535 rt->server_type = RTSP_SERVER_REAL;
1cf151e9 1536 continue;
7a86bafa
RB
1537 } else if (!strncasecmp(reply->server, "WMServer/", 9)) {
1538 rt->server_type = RTSP_SERVER_WMS;
c8965800 1539 } else if (rt->server_type == RTSP_SERVER_REAL)
1cf151e9 1540 strcpy(real_challenge, reply->real_challenge);
1cf151e9
RB
1541 break;
1542 }
1543
44b70ce5 1544 if (s->iformat && CONFIG_RTSP_DEMUXER)
0526c6f7 1545 err = ff_rtsp_setup_input_streams(s, reply);
44b70ce5 1546 else if (CONFIG_RTSP_MUXER)
c2688f3a 1547 err = ff_rtsp_setup_output_streams(s, host);
e23d195d 1548 if (err)
53620bba 1549 goto fail;
53620bba 1550
8a8754d8 1551 do {
c8965800
RB
1552 int lower_transport = ff_log2_tab[lower_transport_mask &
1553 ~(lower_transport_mask - 1)];
8a8754d8 1554
fef5649a 1555 err = ff_rtsp_make_setup_request(s, host, port, lower_transport,
2e889ae4 1556 rt->server_type == RTSP_SERVER_REAL ?
e9dea59f 1557 real_challenge : NULL);
8a8754d8 1558 if (err < 0)
7e6ca34f 1559 goto fail;
90abbdba
RB
1560 lower_transport_mask &= ~(1 << lower_transport);
1561 if (lower_transport_mask == 0 && err == 1) {
28c4741a 1562 err = AVERROR(EPROTONOSUPPORT);
8a8754d8
RB
1563 goto fail;
1564 }
1565 } while (err);
53620bba 1566
2762a7a2
MS
1567 rt->lower_transport_mask = lower_transport_mask;
1568 av_strlcpy(rt->real_challenge, real_challenge, sizeof(rt->real_challenge));
ff762d6e 1569 rt->state = RTSP_STATE_IDLE;
c8965800 1570 rt->seek_timestamp = 0; /* default is to start stream at position zero */
1617ad97
FB
1571 return 0;
1572 fail:
3307e6ea 1573 ff_rtsp_close_streams(s);
b8c2c41d 1574 ff_rtsp_close_connections(s);
35cfd646 1575 if (reply->status_code >=300 && reply->status_code < 400 && s->iformat) {
d243ba30
LB
1576 av_strlcpy(s->filename, reply->location, sizeof(s->filename));
1577 av_log(s, AV_LOG_INFO, "Status %d: Redirecting to %s\n",
1578 reply->status_code,
1579 s->filename);
1580 goto redirect;
1581 }
57b5555c 1582 ff_network_close();
1617ad97
FB
1583 return err;
1584}
2e802e38 1585#endif /* CONFIG_RTSP_DEMUXER || CONFIG_RTSP_MUXER */
1617ad97 1586
44b70ce5 1587#if CONFIG_RTPDEC
0e59034e 1588static int udp_read_packet(AVFormatContext *s, RTSPStream **prtsp_st,
321259c1 1589 uint8_t *buf, int buf_size, int64_t wait_end)
0e59034e
RB
1590{
1591 RTSPState *rt = s->priv_data;
1592 RTSPStream *rtsp_st;
a8475bbd
LB
1593 int n, i, ret, tcp_fd, timeout_cnt = 0;
1594 int max_p = 0;
1595 struct pollfd *p = rt->p;
0e59034e 1596
c8965800 1597 for (;;) {
0e59034e 1598 if (url_interrupt_cb())
c76374c6 1599 return AVERROR_EXIT;
321259c1
MS
1600 if (wait_end && wait_end - av_gettime() < 0)
1601 return AVERROR(EAGAIN);
a8475bbd 1602 max_p = 0;
0e59034e 1603 if (rt->rtsp_hd) {
1869ea03 1604 tcp_fd = ffurl_get_file_handle(rt->rtsp_hd);
a8475bbd
LB
1605 p[max_p].fd = tcp_fd;
1606 p[max_p++].events = POLLIN;
0e59034e 1607 } else {
0e59034e
RB
1608 tcp_fd = -1;
1609 }
c8965800 1610 for (i = 0; i < rt->nb_rtsp_streams; i++) {
0e59034e
RB
1611 rtsp_st = rt->rtsp_streams[i];
1612 if (rtsp_st->rtp_handle) {
1869ea03 1613 p[max_p].fd = ffurl_get_file_handle(rtsp_st->rtp_handle);
a8475bbd 1614 p[max_p++].events = POLLIN;
bfc6db44 1615 p[max_p].fd = ff_rtp_get_rtcp_file_handle(rtsp_st->rtp_handle);
a8475bbd 1616 p[max_p++].events = POLLIN;
0e59034e
RB
1617 }
1618 }
a8475bbd 1619 n = poll(p, max_p, POLL_TIMEOUT_MS);
0e59034e 1620 if (n > 0) {
a8475bbd 1621 int j = 1 - (tcp_fd == -1);
9cba6f5f 1622 timeout_cnt = 0;
c8965800 1623 for (i = 0; i < rt->nb_rtsp_streams; i++) {
0e59034e
RB
1624 rtsp_st = rt->rtsp_streams[i];
1625 if (rtsp_st->rtp_handle) {
a8475bbd 1626 if (p[j].revents & POLLIN || p[j+1].revents & POLLIN) {
bc371aca 1627 ret = ffurl_read(rtsp_st->rtp_handle, buf, buf_size);
0e59034e
RB
1628 if (ret > 0) {
1629 *prtsp_st = rtsp_st;
1630 return ret;
1631 }
1632 }
a8475bbd 1633 j+=2;
0e59034e
RB
1634 }
1635 }
5fe8021a 1636#if CONFIG_RTSP_DEMUXER
a8475bbd 1637 if (tcp_fd != -1 && p[0].revents & POLLIN) {
0e59034e
RB
1638 RTSPMessageHeader reply;
1639
3df54c6b 1640 ret = ff_rtsp_read_reply(s, &reply, NULL, 0, NULL);
3032276b
MS
1641 if (ret < 0)
1642 return ret;
0e59034e 1643 /* XXX: parse message */
c02fd3d2 1644 if (rt->state != RTSP_STATE_STREAMING)
0e59034e
RB
1645 return 0;
1646 }
5fe8021a 1647#endif
9cba6f5f 1648 } else if (n == 0 && ++timeout_cnt >= MAX_TIMEOUTS) {
28c4741a 1649 return AVERROR(ETIMEDOUT);
9cba6f5f
SG
1650 } else if (n < 0 && errno != EINTR)
1651 return AVERROR(errno);
0e59034e
RB
1652 }
1653}
1654
0526c6f7 1655int ff_rtsp_fetch_packet(AVFormatContext *s, AVPacket *pkt)
0e59034e
RB
1656{
1657 RTSPState *rt = s->priv_data;
1658 int ret, len;
321259c1
MS
1659 RTSPStream *rtsp_st, *first_queue_st = NULL;
1660 int64_t wait_end = 0;
0e59034e 1661
b20359f5
JA
1662 if (rt->nb_byes == rt->nb_rtsp_streams)
1663 return AVERROR_EOF;
1664
0e59034e
RB
1665 /* get next frames from the same RTP packet */
1666 if (rt->cur_transport_priv) {
c8965800 1667 if (rt->transport == RTSP_TRANSPORT_RDT) {
0e59034e 1668 ret = ff_rdt_parse_packet(rt->cur_transport_priv, pkt, NULL, 0);
c8965800 1669 } else
bfc6db44 1670 ret = ff_rtp_parse_packet(rt->cur_transport_priv, pkt, NULL, 0);
0e59034e
RB
1671 if (ret == 0) {
1672 rt->cur_transport_priv = NULL;
1673 return 0;
1674 } else if (ret == 1) {
1675 return 0;
c8965800 1676 } else
0e59034e 1677 rt->cur_transport_priv = NULL;
0e59034e
RB
1678 }
1679
321259c1
MS
1680 if (rt->transport == RTSP_TRANSPORT_RTP) {
1681 int i;
1682 int64_t first_queue_time = 0;
1683 for (i = 0; i < rt->nb_rtsp_streams; i++) {
1684 RTPDemuxContext *rtpctx = rt->rtsp_streams[i]->transport_priv;
9e99f84f
MS
1685 int64_t queue_time;
1686 if (!rtpctx)
1687 continue;
1688 queue_time = ff_rtp_queued_packet_time(rtpctx);
321259c1
MS
1689 if (queue_time && (queue_time - first_queue_time < 0 ||
1690 !first_queue_time)) {
1691 first_queue_time = queue_time;
1692 first_queue_st = rt->rtsp_streams[i];
1693 }
1694 }
1695 if (first_queue_time)
1696 wait_end = first_queue_time + s->max_delay;
1697 }
1698
0e59034e
RB
1699 /* read next RTP packet */
1700 redo:
96a7c975
MS
1701 if (!rt->recvbuf) {
1702 rt->recvbuf = av_malloc(RECVBUF_SIZE);
1703 if (!rt->recvbuf)
1704 return AVERROR(ENOMEM);
1705 }
1706
0e59034e
RB
1707 switch(rt->lower_transport) {
1708 default:
5fe8021a 1709#if CONFIG_RTSP_DEMUXER
0e59034e 1710 case RTSP_LOWER_TRANSPORT_TCP:
0526c6f7 1711 len = ff_rtsp_tcp_read_packet(s, &rtsp_st, rt->recvbuf, RECVBUF_SIZE);
0e59034e 1712 break;
5fe8021a 1713#endif
0e59034e
RB
1714 case RTSP_LOWER_TRANSPORT_UDP:
1715 case RTSP_LOWER_TRANSPORT_UDP_MULTICAST:
321259c1 1716 len = udp_read_packet(s, &rtsp_st, rt->recvbuf, RECVBUF_SIZE, wait_end);
2c35a6bd 1717 if (len > 0 && rtsp_st->transport_priv && rt->transport == RTSP_TRANSPORT_RTP)
bfc6db44 1718 ff_rtp_check_and_send_back_rr(rtsp_st->transport_priv, len);
0e59034e
RB
1719 break;
1720 }
321259c1
MS
1721 if (len == AVERROR(EAGAIN) && first_queue_st &&
1722 rt->transport == RTSP_TRANSPORT_RTP) {
1723 rtsp_st = first_queue_st;
bfc6db44 1724 ret = ff_rtp_parse_packet(rtsp_st->transport_priv, pkt, NULL, 0);
321259c1
MS
1725 goto end;
1726 }
0e59034e
RB
1727 if (len < 0)
1728 return len;
1729 if (len == 0)
1730 return AVERROR_EOF;
c8965800 1731 if (rt->transport == RTSP_TRANSPORT_RDT) {
ad4ad27f 1732 ret = ff_rdt_parse_packet(rtsp_st->transport_priv, pkt, &rt->recvbuf, len);
2cab6b48 1733 } else {
bfc6db44 1734 ret = ff_rtp_parse_packet(rtsp_st->transport_priv, pkt, &rt->recvbuf, len);
2cab6b48
MS
1735 if (ret < 0) {
1736 /* Either bad packet, or a RTCP packet. Check if the
1737 * first_rtcp_ntp_time field was initialized. */
1738 RTPDemuxContext *rtpctx = rtsp_st->transport_priv;
1739 if (rtpctx->first_rtcp_ntp_time != AV_NOPTS_VALUE) {
1740 /* first_rtcp_ntp_time has been initialized for this stream,
1741 * copy the same value to all other uninitialized streams,
1742 * in order to map their timestamp origin to the same ntp time
1743 * as this one. */
1744 int i;
3a1cdcc7
MS
1745 AVStream *st = NULL;
1746 if (rtsp_st->stream_index >= 0)
1747 st = s->streams[rtsp_st->stream_index];
2cab6b48 1748 for (i = 0; i < rt->nb_rtsp_streams; i++) {
744a882f 1749 RTPDemuxContext *rtpctx2 = rt->rtsp_streams[i]->transport_priv;
3a1cdcc7
MS
1750 AVStream *st2 = NULL;
1751 if (rt->rtsp_streams[i]->stream_index >= 0)
1752 st2 = s->streams[rt->rtsp_streams[i]->stream_index];
1753 if (rtpctx2 && st && st2 &&
1754 rtpctx2->first_rtcp_ntp_time == AV_NOPTS_VALUE) {
2cab6b48 1755 rtpctx2->first_rtcp_ntp_time = rtpctx->first_rtcp_ntp_time;
3a1cdcc7
MS
1756 rtpctx2->rtcp_ts_offset = av_rescale_q(
1757 rtpctx->rtcp_ts_offset, st->time_base,
1758 st2->time_base);
1759 }
2cab6b48
MS
1760 }
1761 }
b20359f5
JA
1762 if (ret == -RTCP_BYE) {
1763 rt->nb_byes++;
1764
1765 av_log(s, AV_LOG_DEBUG, "Received BYE for stream %d (%d/%d)\n",
1766 rtsp_st->stream_index, rt->nb_byes, rt->nb_rtsp_streams);
1767
1768 if (rt->nb_byes == rt->nb_rtsp_streams)
1769 return AVERROR_EOF;
1770 }
2cab6b48
MS
1771 }
1772 }
321259c1 1773end:
0e59034e
RB
1774 if (ret < 0)
1775 goto redo;
c8965800 1776 if (ret == 1)
0e59034e
RB
1777 /* more packets may follow, so we save the RTP context */
1778 rt->cur_transport_priv = rtsp_st->transport_priv;
0e59034e
RB
1779
1780 return ret;
1781}
44b70ce5 1782#endif /* CONFIG_RTPDEC */
0e59034e 1783
44b70ce5 1784#if CONFIG_SDP_DEMUXER
cb1fdc61 1785static int sdp_probe(AVProbeData *p1)
93ced3e8 1786{
0e1ceacd 1787 const char *p = p1->buf, *p_end = p1->buf + p1->buf_size;
cb1fdc61 1788
3fbd12d1 1789 /* we look for a line beginning "c=IN IP" */
0e1ceacd 1790 while (p < p_end && *p != '\0') {
3fbd12d1
MS
1791 if (p + sizeof("c=IN IP") - 1 < p_end &&
1792 av_strstart(p, "c=IN IP", NULL))
cb1fdc61 1793 return AVPROBE_SCORE_MAX / 2;
0e1ceacd 1794
c8965800 1795 while (p < p_end - 1 && *p != '\n') p++;
0e1ceacd 1796 if (++p >= p_end)
cb1fdc61 1797 break;
cb1fdc61
FB
1798 if (*p == '\r')
1799 p++;
1800 }
93ced3e8
FB
1801 return 0;
1802}
1803
c8965800 1804static int sdp_read_header(AVFormatContext *s, AVFormatParameters *ap)
93ced3e8 1805{
8b1ab7bf 1806 RTSPState *rt = s->priv_data;
93ced3e8
FB
1807 RTSPStream *rtsp_st;
1808 int size, i, err;
1809 char *content;
1810 char url[1024];
1811
57b5555c
MS
1812 if (!ff_network_init())
1813 return AVERROR(EIO);
1814
93ced3e8
FB
1815 /* read the whole sdp file */
1816 /* XXX: better loading */
1817 content = av_malloc(SDP_MAX_SIZE);
b7effd4e 1818 size = avio_read(s->pb, content, SDP_MAX_SIZE - 1);
93ced3e8
FB
1819 if (size <= 0) {
1820 av_free(content);
1821 return AVERROR_INVALIDDATA;
1822 }
1823 content[size] ='\0';
1824
f81c7ac7 1825 err = ff_sdp_parse(s, content);
93ced3e8 1826 av_free(content);
f81c7ac7 1827 if (err) goto fail;
93ced3e8
FB
1828
1829 /* open each RTP stream */
c8965800 1830 for (i = 0; i < rt->nb_rtsp_streams; i++) {
3fbd12d1 1831 char namebuf[50];
8b1ab7bf 1832 rtsp_st = rt->rtsp_streams[i];
115329f1 1833
3fbd12d1
MS
1834 getnameinfo((struct sockaddr*) &rtsp_st->sdp_ip, sizeof(rtsp_st->sdp_ip),
1835 namebuf, sizeof(namebuf), NULL, 0, NI_NUMERICHOST);
57b5555c 1836 ff_url_join(url, sizeof(url), "rtp", NULL,
3fbd12d1 1837 namebuf, rtsp_st->sdp_port,
57b5555c
MS
1838 "?localport=%d&ttl=%d", rtsp_st->sdp_port,
1839 rtsp_st->sdp_ttl);
59d96941 1840 if (ffurl_open(&rtsp_st->rtp_handle, url, AVIO_FLAG_READ_WRITE) < 0) {
93ced3e8
FB
1841 err = AVERROR_INVALIDDATA;
1842 goto fail;
1843 }
ee0cb67f 1844 if ((err = rtsp_open_transport_ctx(s, rtsp_st)))
8b1ab7bf 1845 goto fail;
93ced3e8
FB
1846 }
1847 return 0;
c8965800 1848fail:
3307e6ea 1849 ff_rtsp_close_streams(s);
57b5555c 1850 ff_network_close();
93ced3e8
FB
1851 return err;
1852}
1853
93ced3e8
FB
1854static int sdp_read_close(AVFormatContext *s)
1855{
3307e6ea 1856 ff_rtsp_close_streams(s);
57b5555c 1857 ff_network_close();
93ced3e8
FB
1858 return 0;
1859}
1860
c6610a21 1861AVInputFormat ff_sdp_demuxer = {
dfc2c4d9
AK
1862 .name = "sdp",
1863 .long_name = NULL_IF_CONFIG_SMALL("SDP"),
1864 .priv_data_size = sizeof(RTSPState),
1865 .read_probe = sdp_probe,
1866 .read_header = sdp_read_header,
1867 .read_packet = ff_rtsp_fetch_packet,
1868 .read_close = sdp_read_close,
93ced3e8 1869};
44b70ce5 1870#endif /* CONFIG_SDP_DEMUXER */
44594cc7 1871
44b70ce5 1872#if CONFIG_RTP_DEMUXER
44594cc7
MS
1873static int rtp_probe(AVProbeData *p)
1874{
1875 if (av_strstart(p->filename, "rtp:", NULL))
1876 return AVPROBE_SCORE_MAX;
1877 return 0;
1878}
1879
1880static int rtp_read_header(AVFormatContext *s,
1881 AVFormatParameters *ap)
1882{
1883 uint8_t recvbuf[1500];
1884 char host[500], sdp[500];
1885 int ret, port;
1886 URLContext* in = NULL;
1887 int payload_type;
1888 AVCodecContext codec;
1889 struct sockaddr_storage addr;
ae628ec1 1890 AVIOContext pb;
44594cc7
MS
1891 socklen_t addrlen = sizeof(addr);
1892
1893 if (!ff_network_init())
1894 return AVERROR(EIO);
1895
59d96941 1896 ret = ffurl_open(&in, s->filename, AVIO_FLAG_READ);
44594cc7
MS
1897 if (ret)
1898 goto fail;
1899
1900 while (1) {
bc371aca 1901 ret = ffurl_read(in, recvbuf, sizeof(recvbuf));
44594cc7
MS
1902 if (ret == AVERROR(EAGAIN))
1903 continue;
1904 if (ret < 0)
1905 goto fail;
1906 if (ret < 12) {
1907 av_log(s, AV_LOG_WARNING, "Received too short packet\n");
1908 continue;
1909 }
1910
1911 if ((recvbuf[0] & 0xc0) != 0x80) {
1912 av_log(s, AV_LOG_WARNING, "Unsupported RTP version packet "
1913 "received\n");
1914 continue;
1915 }
1916
1917 payload_type = recvbuf[1] & 0x7f;
1918 break;
1919 }
1869ea03 1920 getsockname(ffurl_get_file_handle(in), (struct sockaddr*) &addr, &addrlen);
e52a9145 1921 ffurl_close(in);
44594cc7
MS
1922 in = NULL;
1923
1924 memset(&codec, 0, sizeof(codec));
1925 if (ff_rtp_get_codec_info(&codec, payload_type)) {
1926 av_log(s, AV_LOG_ERROR, "Unable to receive RTP payload type %d "
1927 "without an SDP file describing it\n",
1928 payload_type);
1929 goto fail;
1930 }
1931 if (codec.codec_type != AVMEDIA_TYPE_DATA) {
1932 av_log(s, AV_LOG_WARNING, "Guessing on RTP content - if not received "
1933 "properly you need an SDP file "
1934 "describing it\n");
1935 }
1936
1937 av_url_split(NULL, 0, NULL, 0, host, sizeof(host), &port,
1938 NULL, 0, s->filename);
1939
1940 snprintf(sdp, sizeof(sdp),
1941 "v=0\r\nc=IN IP%d %s\r\nm=%s %d RTP/AVP %d\r\n",
1942 addr.ss_family == AF_INET ? 4 : 6, host,
1943 codec.codec_type == AVMEDIA_TYPE_DATA ? "application" :
1944 codec.codec_type == AVMEDIA_TYPE_VIDEO ? "video" : "audio",
1945 port, payload_type);
1946 av_log(s, AV_LOG_VERBOSE, "SDP:\n%s\n", sdp);
1947
e731b8d8 1948 ffio_init_context(&pb, sdp, strlen(sdp), 0, NULL, NULL, NULL, NULL);
44594cc7
MS
1949 s->pb = &pb;
1950
1951 /* sdp_read_header initializes this again */
1952 ff_network_close();
1953
1954 ret = sdp_read_header(s, ap);
1955 s->pb = NULL;
1956 return ret;
1957
1958fail:
1959 if (in)
e52a9145 1960 ffurl_close(in);
44594cc7
MS
1961 ff_network_close();
1962 return ret;
1963}
1964
c6610a21 1965AVInputFormat ff_rtp_demuxer = {
dfc2c4d9
AK
1966 .name = "rtp",
1967 .long_name = NULL_IF_CONFIG_SMALL("RTP input format"),
1968 .priv_data_size = sizeof(RTSPState),
1969 .read_probe = rtp_probe,
1970 .read_header = rtp_read_header,
1971 .read_packet = ff_rtsp_fetch_packet,
1972 .read_close = sdp_read_close,
44594cc7
MS
1973 .flags = AVFMT_NOFILE,
1974};
44b70ce5 1975#endif /* CONFIG_RTP_DEMUXER */
44594cc7 1976