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