Re-indent to more closely follow general coding standards used in other
[libav.git] / libavformat / rtsp.c
CommitLineData
1617ad97 1/*
93ced3e8 2 * RTSP/SDP client
406792e7 3 * Copyright (c) 2002 Fabrice Bellard
1617ad97 4 *
b78e7197
DB
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg 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 *
b78e7197 12 * FFmpeg 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
b78e7197 18 * License along with FFmpeg; 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
7246177d
AJ
22/* needed by inet_aton() */
23#define _SVID_SOURCE
24
f9337897 25#include "libavutil/base64.h"
245976da 26#include "libavutil/avstring.h"
6a5d31ac 27#include "libavutil/intreadwrite.h"
1617ad97
FB
28#include "avformat.h"
29
db0ed93e 30#include <sys/time.h>
b250f9c6 31#if HAVE_SYS_SELECT_H
933bd8e2 32#include <sys/select.h>
6ad1c9c9 33#endif
ea452b54 34#include <strings.h>
42572ef5 35#include "network.h"
c971ff19 36#include "rtsp.h"
1617ad97 37
302879cb 38#include "rtpdec.h"
e9dea59f 39#include "rdt.h"
1a30d541 40#include "rtp_asf.h"
e6327fba 41#include "rtp_vorbis.h"
4934884a 42
1617ad97 43//#define DEBUG
b6892136 44//#define DEBUG_RTP_TCP
1617ad97 45
c482500f 46#if LIBAVFORMAT_VERSION_INT < (53 << 16)
90abbdba 47int rtsp_default_protocols = (1 << RTSP_LOWER_TRANSPORT_UDP);
c482500f 48#endif
85fb7b34 49
1ef36a70 50#define SPACE_CHARS " \t\r\n"
da1e126e
RB
51/* we use memchr() instead of strchr() here because strchr() will return
52 * the terminating '\0' of SPACE_CHARS instead of NULL if c is '\0'. */
53#define redir_isspace(c) memchr(SPACE_CHARS, c, 4)
1617ad97
FB
54static void skip_spaces(const char **pp)
55{
56 const char *p;
57 p = *pp;
58 while (redir_isspace(*p))
59 p++;
60 *pp = p;
61}
62
1ef36a70
RB
63static void get_word_until_chars(char *buf, int buf_size,
64 const char *sep, const char **pp)
1617ad97
FB
65{
66 const char *p;
67 char *q;
68
69 p = *pp;
70 skip_spaces(&p);
71 q = buf;
72 while (!strchr(sep, *p) && *p != '\0') {
73 if ((q - buf) < buf_size - 1)
74 *q++ = *p;
75 p++;
76 }
77 if (buf_size > 0)
78 *q = '\0';
79 *pp = p;
80}
81
1ef36a70
RB
82static void get_word_sep(char *buf, int buf_size, const char *sep,
83 const char **pp)
1617ad97 84{
1ef36a70
RB
85 if (**pp == '/') (*pp)++;
86 get_word_until_chars(buf, buf_size, sep, pp);
87}
1617ad97 88
1ef36a70
RB
89static void get_word(char *buf, int buf_size, const char **pp)
90{
91 get_word_until_chars(buf, buf_size, SPACE_CHARS, pp);
1617ad97
FB
92}
93
c8965800
RB
94/* parse the rtpmap description: <codec_name>/<clock_rate>[/<other params>] */
95static int sdp_parse_rtpmap(AVCodecContext *codec, RTSPStream *rtsp_st,
96 int payload_type, const char *p)
93ced3e8
FB
97{
98 char buf[256];
d1ccf0e0
RD
99 int i;
100 AVCodec *c;
7b49ce2e 101 const char *c_name;
93ced3e8 102
d1ccf0e0 103 /* Loop into AVRtpDynamicPayloadTypes[] and AVRtpPayloadTypes[] and
c8965800 104 * see if we can handle this kind of payload. */
93ced3e8 105 get_word_sep(buf, sizeof(buf), "/", &p);
d1ccf0e0 106 if (payload_type >= RTP_PT_PRIVATE) {
c8965800
RB
107 RTPDynamicProtocolHandler *handler;
108 for (handler = RTPFirstDynamicPayloadHandler;
109 handler; handler = handler->next) {
110 if (!strcasecmp(buf, handler->enc_name) &&
111 codec->codec_type == handler->codec_type) {
112 codec->codec_id = handler->codec_id;
113 rtsp_st->dynamic_handler = handler;
114 if (handler->open)
115 rtsp_st->dynamic_protocol_context = handler->open();
d1ccf0e0
RD
116 break;
117 }
4934884a 118 }
93ced3e8 119 } else {
c8965800
RB
120 /* We are in a standard case
121 * (from http://www.iana.org/assignments/rtp-parameters). */
d1ccf0e0 122 /* search into AVRtpPayloadTypes[] */
7ed19d7f 123 codec->codec_id = ff_rtp_codec_id(buf, codec->codec_type);
d1ccf0e0
RD
124 }
125
126 c = avcodec_find_decoder(codec->codec_id);
127 if (c && c->name)
7b49ce2e 128 c_name = c->name;
d1ccf0e0 129 else
c8965800 130 c_name = (char *) NULL;
d1ccf0e0
RD
131
132 if (c_name) {
133 get_word_sep(buf, sizeof(buf), "/", &p);
134 i = atoi(buf);
135 switch (codec->codec_type) {
c8965800
RB
136 case CODEC_TYPE_AUDIO:
137 av_log(codec, AV_LOG_DEBUG, "audio codec set to: %s\n", c_name);
138 codec->sample_rate = RTSP_DEFAULT_AUDIO_SAMPLERATE;
139 codec->channels = RTSP_DEFAULT_NB_AUDIO_CHANNELS;
140 if (i > 0) {
141 codec->sample_rate = i;
142 get_word_sep(buf, sizeof(buf), "/", &p);
143 i = atoi(buf);
144 if (i > 0)
145 codec->channels = i;
146 // TODO: there is a bug here; if it is a mono stream, and
147 // less than 22000Hz, faad upconverts to stereo and twice
148 // the frequency. No problem, but the sample rate is being
149 // set here by the sdp line. Patch on its way. (rdm)
150 }
151 av_log(codec, AV_LOG_DEBUG, "audio samplerate set to: %i\n",
152 codec->sample_rate);
153 av_log(codec, AV_LOG_DEBUG, "audio channels set to: %i\n",
154 codec->channels);
155 break;
156 case CODEC_TYPE_VIDEO:
157 av_log(codec, AV_LOG_DEBUG, "video codec set to: %s\n", c_name);
158 break;
159 default:
160 break;
d1ccf0e0
RD
161 }
162 return 0;
93ced3e8 163 }
d1ccf0e0
RD
164
165 return -1;
93ced3e8
FB
166}
167
c8965800 168/* return the length and optionally the data */
93ced3e8
FB
169static int hex_to_data(uint8_t *data, const char *p)
170{
171 int c, len, v;
172
173 len = 0;
174 v = 1;
c8965800 175 for (;;) {
93ced3e8 176 skip_spaces(&p);
6a8c8b36 177 if (*p == '\0')
93ced3e8 178 break;
c8965800 179 c = toupper((unsigned char) *p++);
93ced3e8
FB
180 if (c >= '0' && c <= '9')
181 c = c - '0';
182 else if (c >= 'A' && c <= 'F')
183 c = c - 'A' + 10;
184 else
185 break;
186 v = (v << 4) | c;
187 if (v & 0x100) {
188 if (data)
189 data[len] = v;
190 len++;
191 v = 1;
192 }
193 }
194 return len;
195}
196
e6327fba
RB
197static void sdp_parse_fmtp_config(AVCodecContext * codec, void *ctx,
198 char *attr, char *value)
d1ccf0e0
RD
199{
200 switch (codec->codec_id) {
c8965800
RB
201 case CODEC_ID_MPEG4:
202 case CODEC_ID_AAC:
203 if (!strcmp(attr, "config")) {
204 /* decode the hexa encoded parameter */
205 int len = hex_to_data(NULL, value);
206 if (codec->extradata)
207 av_free(codec->extradata);
208 codec->extradata = av_mallocz(len + FF_INPUT_BUFFER_PADDING_SIZE);
209 if (!codec->extradata)
210 return;
211 codec->extradata_size = len;
212 hex_to_data(codec->extradata, value);
213 }
214 break;
215 case CODEC_ID_VORBIS:
216 ff_vorbis_parse_fmtp_config(codec, ctx, attr, value);
217 break;
218 default:
219 break;
d1ccf0e0
RD
220 }
221 return;
222}
223
644e7acb 224typedef struct {
7b49ce2e 225 const char *str;
c8965800
RB
226 uint16_t type;
227 uint32_t offset;
644e7acb 228} AttrNameMap;
d1ccf0e0
RD
229
230/* All known fmtp parmeters and the corresping RTPAttrTypeEnum */
231#define ATTR_NAME_TYPE_INT 0
232#define ATTR_NAME_TYPE_STR 1
644e7acb 233static const AttrNameMap attr_names[]=
d1ccf0e0 234{
c8965800
RB
235 { "SizeLength", ATTR_NAME_TYPE_INT,
236 offsetof(RTPPayloadData, sizelength) },
237 { "IndexLength", ATTR_NAME_TYPE_INT,
238 offsetof(RTPPayloadData, indexlength) },
239 { "IndexDeltaLength", ATTR_NAME_TYPE_INT,
240 offsetof(RTPPayloadData, indexdeltalength) },
241 { "profile-level-id", ATTR_NAME_TYPE_INT,
242 offsetof(RTPPayloadData, profile_level_id) },
243 { "StreamType", ATTR_NAME_TYPE_INT,
244 offsetof(RTPPayloadData, streamtype) },
245 { "mode", ATTR_NAME_TYPE_STR,
246 offsetof(RTPPayloadData, mode) },
247 { NULL, -1, -1 },
d1ccf0e0
RD
248};
249
c8965800
RB
250/* parse the attribute line from the fmtp a line of an sdp resonse. This
251 * is broken out as a function because it is used in rtp_h264.c, which is
252 * forthcoming. */
253int rtsp_next_attr_and_value(const char **p, char *attr, int attr_size,
254 char *value, int value_size)
d0deedcb
RM
255{
256 skip_spaces(p);
c8965800 257 if (**p) {
d0deedcb
RM
258 get_word_sep(attr, attr_size, "=", p);
259 if (**p == '=')
260 (*p)++;
261 get_word_sep(value, value_size, ";", p);
262 if (**p == ';')
263 (*p)++;
264 return 1;
265 }
266 return 0;
267}
268
d1ccf0e0
RD
269/* parse a SDP line and save stream attributes */
270static void sdp_parse_fmtp(AVStream *st, const char *p)
93ced3e8
FB
271{
272 char attr[256];
373afbaf
RB
273 /* Vorbis setup headers can be up to 12KB and are sent base64
274 * encoded, giving a 12KB * (4/3) = 16KB FMTP line. */
275 char value[16384];
d1ccf0e0 276 int i;
d1ccf0e0 277 RTSPStream *rtsp_st = st->priv_data;
01f4895c 278 AVCodecContext *codec = st->codec;
be73a544 279 RTPPayloadData *rtp_payload_data = &rtsp_st->rtp_payload_data;
93ced3e8
FB
280
281 /* loop on each attribute */
c8965800
RB
282 while (rtsp_next_attr_and_value(&p, attr, sizeof(attr),
283 value, sizeof(value))) {
284 /* grab the codec extra_data from the config parameter of the fmtp
285 * line */
e6327fba
RB
286 sdp_parse_fmtp_config(codec, rtsp_st->dynamic_protocol_context,
287 attr, value);
d1ccf0e0
RD
288 /* Looking for a known attribute */
289 for (i = 0; attr_names[i].str; ++i) {
290 if (!strcasecmp(attr, attr_names[i].str)) {
c8965800
RB
291 if (attr_names[i].type == ATTR_NAME_TYPE_INT) {
292 *(int *)((char *)rtp_payload_data +
293 attr_names[i].offset) = atoi(value);
294 } else if (attr_names[i].type == ATTR_NAME_TYPE_STR)
295 *(char **)((char *)rtp_payload_data +
296 attr_names[i].offset) = av_strdup(value);
bb270c08 297 }
93ced3e8 298 }
93ced3e8
FB
299 }
300}
301
debe86bf 302/** Parse a string p in the form of Range:npt=xx-xx, and determine the start
31693e00
RM
303 * and end time.
304 * Used for seeking in the rtp stream.
305 */
306static void rtsp_parse_range_npt(const char *p, int64_t *start, int64_t *end)
307{
308 char buf[256];
309
310 skip_spaces(&p);
f7d78f36 311 if (!av_stristart(p, "npt=", &p))
31693e00
RM
312 return;
313
314 *start = AV_NOPTS_VALUE;
315 *end = AV_NOPTS_VALUE;
316
317 get_word_sep(buf, sizeof(buf), "-", &p);
318 *start = parse_date(buf, 1);
319 if (*p == '-') {
320 p++;
321 get_word_sep(buf, sizeof(buf), "-", &p);
322 *end = parse_date(buf, 1);
323 }
324// av_log(NULL, AV_LOG_DEBUG, "Range Start: %lld\n", *start);
325// av_log(NULL, AV_LOG_DEBUG, "Range End: %lld\n", *end);
326}
327
93ced3e8
FB
328typedef struct SDPParseState {
329 /* SDP only */
330 struct in_addr default_ip;
c8965800
RB
331 int default_ttl;
332 int skip_media; ///< set if an unknown m= line occurs
93ced3e8
FB
333} SDPParseState;
334
335static void sdp_parse_line(AVFormatContext *s, SDPParseState *s1,
1617ad97
FB
336 int letter, const char *buf)
337{
8b1ab7bf 338 RTSPState *rt = s->priv_data;
1617ad97
FB
339 char buf1[64], st_type[64];
340 const char *p;
fb65d2ca
DP
341 enum CodecType codec_type;
342 int payload_type, i;
1617ad97
FB
343 AVStream *st;
344 RTSPStream *rtsp_st;
93ced3e8
FB
345 struct in_addr sdp_ip;
346 int ttl;
347
67c9cd69 348 dprintf(s, "sdp: %c='%s'\n", letter, buf);
1617ad97
FB
349
350 p = buf;
cb760a47
RB
351 if (s1->skip_media && letter != 'm')
352 return;
c8965800 353 switch (letter) {
93ced3e8
FB
354 case 'c':
355 get_word(buf1, sizeof(buf1), &p);
356 if (strcmp(buf1, "IN") != 0)
357 return;
358 get_word(buf1, sizeof(buf1), &p);
359 if (strcmp(buf1, "IP4") != 0)
360 return;
361 get_word_sep(buf1, sizeof(buf1), "/", &p);
362 if (inet_aton(buf1, &sdp_ip) == 0)
363 return;
364 ttl = 16;
365 if (*p == '/') {
366 p++;
367 get_word_sep(buf1, sizeof(buf1), "/", &p);
368 ttl = atoi(buf1);
369 }
370 if (s->nb_streams == 0) {
371 s1->default_ip = sdp_ip;
372 s1->default_ttl = ttl;
373 } else {
374 st = s->streams[s->nb_streams - 1];
375 rtsp_st = st->priv_data;
376 rtsp_st->sdp_ip = sdp_ip;
377 rtsp_st->sdp_ttl = ttl;
378 }
379 break;
1617ad97 380 case 's':
da61e413 381 av_metadata_set(&s->metadata, "title", p);
1617ad97
FB
382 break;
383 case 'i':
384 if (s->nb_streams == 0) {
da61e413 385 av_metadata_set(&s->metadata, "comment", p);
1617ad97
FB
386 break;
387 }
388 break;
389 case 'm':
390 /* new stream */
cb760a47 391 s1->skip_media = 0;
1617ad97
FB
392 get_word(st_type, sizeof(st_type), &p);
393 if (!strcmp(st_type, "audio")) {
394 codec_type = CODEC_TYPE_AUDIO;
395 } else if (!strcmp(st_type, "video")) {
396 codec_type = CODEC_TYPE_VIDEO;
090438cc
RB
397 } else if (!strcmp(st_type, "application")) {
398 codec_type = CODEC_TYPE_DATA;
1617ad97 399 } else {
cb760a47 400 s1->skip_media = 1;
1617ad97
FB
401 return;
402 }
1617ad97
FB
403 rtsp_st = av_mallocz(sizeof(RTSPStream));
404 if (!rtsp_st)
405 return;
8b1ab7bf
FB
406 rtsp_st->stream_index = -1;
407 dynarray_add(&rt->rtsp_streams, &rt->nb_rtsp_streams, rtsp_st);
93ced3e8
FB
408
409 rtsp_st->sdp_ip = s1->default_ip;
410 rtsp_st->sdp_ttl = s1->default_ttl;
411
93ced3e8
FB
412 get_word(buf1, sizeof(buf1), &p); /* port */
413 rtsp_st->sdp_port = atoi(buf1);
414
415 get_word(buf1, sizeof(buf1), &p); /* protocol (ignored) */
115329f1 416
93ced3e8
FB
417 /* XXX: handle list of formats */
418 get_word(buf1, sizeof(buf1), &p); /* format list */
419 rtsp_st->sdp_payload_type = atoi(buf1);
93ced3e8 420
7ed19d7f 421 if (!strcmp(ff_rtp_enc_name(rtsp_st->sdp_payload_type), "MP2T")) {
8b1ab7bf
FB
422 /* no corresponding stream */
423 } else {
424 st = av_new_stream(s, 0);
425 if (!st)
426 return;
427 st->priv_data = rtsp_st;
428 rtsp_st->stream_index = st->index;
01f4895c 429 st->codec->codec_type = codec_type;
d1ccf0e0 430 if (rtsp_st->sdp_payload_type < RTP_PT_PRIVATE) {
8b1ab7bf 431 /* if standard payload type, we can find the codec right now */
bf6d9818 432 ff_rtp_get_codec_info(st->codec, rtsp_st->sdp_payload_type);
8b1ab7bf
FB
433 }
434 }
1617ad97 435 /* put a default control url */
c8965800
RB
436 av_strlcpy(rtsp_st->control_url, s->filename,
437 sizeof(rtsp_st->control_url));
1617ad97
FB
438 break;
439 case 'a':
f7d78f36 440 if (av_strstart(p, "control:", &p) && s->nb_streams > 0) {
1617ad97
FB
441 char proto[32];
442 /* get the control url */
443 st = s->streams[s->nb_streams - 1];
444 rtsp_st = st->priv_data;
115329f1 445
1617ad97 446 /* XXX: may need to add full url resolution */
c8965800
RB
447 url_split(proto, sizeof(proto), NULL, 0, NULL, 0,
448 NULL, NULL, 0, p);
1617ad97
FB
449 if (proto[0] == '\0') {
450 /* relative control URL */
c8965800
RB
451 av_strlcat(rtsp_st->control_url, "/",
452 sizeof(rtsp_st->control_url));
453 av_strlcat(rtsp_st->control_url, p,
454 sizeof(rtsp_st->control_url));
455 } else
456 av_strlcpy(rtsp_st->control_url, p,
457 sizeof(rtsp_st->control_url));
83d14c85 458 } else if (av_strstart(p, "rtpmap:", &p) && s->nb_streams > 0) {
93ced3e8 459 /* NOTE: rtpmap is only supported AFTER the 'm=' tag */
115329f1 460 get_word(buf1, sizeof(buf1), &p);
93ced3e8 461 payload_type = atoi(buf1);
83d14c85 462 st = s->streams[s->nb_streams - 1];
ff16f551
RB
463 rtsp_st = st->priv_data;
464 sdp_parse_rtpmap(st->codec, rtsp_st, payload_type, p);
f7d78f36 465 } else if (av_strstart(p, "fmtp:", &p)) {
93ced3e8 466 /* NOTE: fmtp is only supported AFTER the 'a=rtpmap:xxx' tag */
115329f1 467 get_word(buf1, sizeof(buf1), &p);
93ced3e8 468 payload_type = atoi(buf1);
c8965800
RB
469 for (i = 0; i < s->nb_streams; i++) {
470 st = s->streams[i];
93ced3e8
FB
471 rtsp_st = st->priv_data;
472 if (rtsp_st->sdp_payload_type == payload_type) {
c8965800
RB
473 if (!(rtsp_st->dynamic_handler &&
474 rtsp_st->dynamic_handler->parse_sdp_a_line &&
475 rtsp_st->dynamic_handler->parse_sdp_a_line(s,
476 i, rtsp_st->dynamic_protocol_context, buf)))
ed787542 477 sdp_parse_fmtp(st, p);
93ced3e8
FB
478 }
479 }
c8965800 480 } else if (av_strstart(p, "framesize:", &p)) {
d0deedcb
RM
481 // let dynamic protocol handlers have a stab at the line.
482 get_word(buf1, sizeof(buf1), &p);
483 payload_type = atoi(buf1);
c8965800
RB
484 for (i = 0; i < s->nb_streams; i++) {
485 st = s->streams[i];
d0deedcb 486 rtsp_st = st->priv_data;
c8965800
RB
487 if (rtsp_st->sdp_payload_type == payload_type &&
488 rtsp_st->dynamic_handler &&
489 rtsp_st->dynamic_handler->parse_sdp_a_line)
490 rtsp_st->dynamic_handler->parse_sdp_a_line(s, i,
491 rtsp_st->dynamic_protocol_context, buf);
d0deedcb 492 }
c8965800 493 } else if (av_strstart(p, "range:", &p)) {
31693e00
RM
494 int64_t start, end;
495
496 // this is so that seeking on a streamed file can work.
497 rtsp_parse_range_npt(p, &start, &end);
c8965800
RB
498 s->start_time = start;
499 /* AV_NOPTS_VALUE means live broadcast (and can't seek) */
500 s->duration = (end == AV_NOPTS_VALUE) ?
501 AV_NOPTS_VALUE : end - start;
119b4668
RB
502 } else if (av_strstart(p, "IsRealDataType:integer;",&p)) {
503 if (atoi(p) == 1)
504 rt->transport = RTSP_TRANSPORT_RDT;
1a30d541
RB
505 } else {
506 if (rt->server_type == RTSP_SERVER_WMS)
507 ff_wms_parse_sdp_a_line(s, p);
508 if (s->nb_streams > 0) {
c4a3d032
RB
509 if (rt->server_type == RTSP_SERVER_REAL)
510 ff_real_parse_sdp_a_line(s, s->nb_streams - 1, p);
511
512 rtsp_st = s->streams[s->nb_streams - 1]->priv_data;
513 if (rtsp_st->dynamic_handler &&
514 rtsp_st->dynamic_handler->parse_sdp_a_line)
c8965800
RB
515 rtsp_st->dynamic_handler->parse_sdp_a_line(s,
516 s->nb_streams - 1,
c4a3d032 517 rtsp_st->dynamic_protocol_context, buf);
1a30d541 518 }
1617ad97
FB
519 }
520 break;
521 }
522}
523
5c91a675 524static int sdp_parse(AVFormatContext *s, const char *content)
1617ad97
FB
525{
526 const char *p;
527 int letter;
9211bcdd
RB
528 /* Some SDP lines, particularly for Realmedia or ASF RTSP streams,
529 * contain long SDP lines containing complete ASF Headers (several
530 * kB) or arrays of MDPR (RM stream descriptor) headers plus
531 * "rulebooks" describing their properties. Therefore, the SDP line
373afbaf
RB
532 * buffer is large.
533 *
534 * The Vorbis FMTP line can be up to 16KB - see sdp_parse_fmtp. */
535 char buf[16384], *q;
93ced3e8 536 SDPParseState sdp_parse_state, *s1 = &sdp_parse_state;
115329f1 537
93ced3e8 538 memset(s1, 0, sizeof(SDPParseState));
1617ad97 539 p = content;
c8965800 540 for (;;) {
1617ad97
FB
541 skip_spaces(&p);
542 letter = *p;
543 if (letter == '\0')
544 break;
545 p++;
546 if (*p != '=')
547 goto next_line;
548 p++;
549 /* get the content */
550 q = buf;
b6892136 551 while (*p != '\n' && *p != '\r' && *p != '\0') {
1617ad97
FB
552 if ((q - buf) < sizeof(buf) - 1)
553 *q++ = *p;
554 p++;
555 }
556 *q = '\0';
93ced3e8 557 sdp_parse_line(s, s1, letter, buf);
1617ad97
FB
558 next_line:
559 while (*p != '\n' && *p != '\0')
560 p++;
561 if (*p == '\n')
562 p++;
563 }
564 return 0;
565}
566
1ced9da3
LA
567/* close and free RTSP streams */
568static void rtsp_close_streams(RTSPState *rt)
569{
570 int i;
571 RTSPStream *rtsp_st;
572
c8965800 573 for (i = 0; i < rt->nb_rtsp_streams; i++) {
1ced9da3
LA
574 rtsp_st = rt->rtsp_streams[i];
575 if (rtsp_st) {
576 if (rtsp_st->transport_priv) {
577 if (rt->transport == RTSP_TRANSPORT_RDT)
578 ff_rdt_parse_close(rtsp_st->transport_priv);
579 else
580 rtp_parse_close(rtsp_st->transport_priv);
581 }
582 if (rtsp_st->rtp_handle)
583 url_close(rtsp_st->rtp_handle);
584 if (rtsp_st->dynamic_handler && rtsp_st->dynamic_protocol_context)
c8965800
RB
585 rtsp_st->dynamic_handler->close(
586 rtsp_st->dynamic_protocol_context);
1ced9da3
LA
587 }
588 }
589 av_free(rt->rtsp_streams);
590 if (rt->asf_ctx) {
591 av_close_input_stream (rt->asf_ctx);
592 rt->asf_ctx = NULL;
593 }
594 av_freep(&rt->auth_b64);
595}
596
c8965800 597static int rtsp_open_transport_ctx(AVFormatContext *s, RTSPStream *rtsp_st)
1ced9da3
LA
598{
599 RTSPState *rt = s->priv_data;
600 AVStream *st = NULL;
601
602 /* open the RTP context */
603 if (rtsp_st->stream_index >= 0)
604 st = s->streams[rtsp_st->stream_index];
605 if (!st)
606 s->ctx_flags |= AVFMTCTX_NOHEADER;
607
608 if (rt->transport == RTSP_TRANSPORT_RDT)
609 rtsp_st->transport_priv = ff_rdt_parse_open(s, st->index,
610 rtsp_st->dynamic_protocol_context,
611 rtsp_st->dynamic_handler);
612 else
613 rtsp_st->transport_priv = rtp_parse_open(s, st, rtsp_st->rtp_handle,
614 rtsp_st->sdp_payload_type,
615 &rtsp_st->rtp_payload_data);
616
617 if (!rtsp_st->transport_priv) {
618 return AVERROR(ENOMEM);
619 } else if (rt->transport != RTSP_TRANSPORT_RDT) {
c8965800 620 if (rtsp_st->dynamic_handler) {
1ced9da3
LA
621 rtp_parse_set_dynamic_protocol(rtsp_st->transport_priv,
622 rtsp_st->dynamic_protocol_context,
623 rtsp_st->dynamic_handler);
624 }
625 }
626
627 return 0;
628}
629
103dfbe2 630#if CONFIG_RTSP_DEMUXER
1ced9da3
LA
631static int rtsp_probe(AVProbeData *p)
632{
633 if (av_strstart(p->filename, "rtsp:", NULL))
634 return AVPROBE_SCORE_MAX;
635 return 0;
636}
637
1617ad97
FB
638static void rtsp_parse_range(int *min_ptr, int *max_ptr, const char **pp)
639{
640 const char *p;
641 int v;
642
643 p = *pp;
644 skip_spaces(&p);
645 v = strtol(p, (char **)&p, 10);
646 if (*p == '-') {
647 p++;
648 *min_ptr = v;
649 v = strtol(p, (char **)&p, 10);
650 *max_ptr = v;
651 } else {
652 *min_ptr = v;
653 *max_ptr = v;
654 }
655 *pp = p;
656}
657
658/* XXX: only one transport specification is parsed */
a9e534d5 659static void rtsp_parse_transport(RTSPMessageHeader *reply, const char *p)
1617ad97
FB
660{
661 char transport_protocol[16];
662 char profile[16];
663 char lower_transport[16];
664 char parameter[16];
665 RTSPTransportField *th;
666 char buf[256];
115329f1 667
1617ad97 668 reply->nb_transports = 0;
115329f1 669
c8965800 670 for (;;) {
1617ad97
FB
671 skip_spaces(&p);
672 if (*p == '\0')
673 break;
674
675 th = &reply->transports[reply->nb_transports];
676
115329f1 677 get_word_sep(transport_protocol, sizeof(transport_protocol),
1617ad97 678 "/", &p);
e1502118 679 if (!strcasecmp (transport_protocol, "rtp")) {
7ecc634e
LB
680 get_word_sep(profile, sizeof(profile), "/;,", &p);
681 lower_transport[0] = '\0';
682 /* rtp/avp/<protocol> */
683 if (*p == '/') {
7ecc634e
LB
684 get_word_sep(lower_transport, sizeof(lower_transport),
685 ";,", &p);
119b4668
RB
686 }
687 th->transport = RTSP_TRANSPORT_RTP;
688 } else if (!strcasecmp (transport_protocol, "x-pn-tng") ||
689 !strcasecmp (transport_protocol, "x-real-rdt")) {
7ecc634e 690 /* x-pn-tng/<protocol> */
e1502118
LB
691 get_word_sep(lower_transport, sizeof(lower_transport), "/;,", &p);
692 profile[0] = '\0';
119b4668 693 th->transport = RTSP_TRANSPORT_RDT;
1617ad97 694 }
b6892136 695 if (!strcasecmp(lower_transport, "TCP"))
90abbdba 696 th->lower_transport = RTSP_LOWER_TRANSPORT_TCP;
1617ad97 697 else
90abbdba 698 th->lower_transport = RTSP_LOWER_TRANSPORT_UDP;
115329f1 699
1617ad97
FB
700 if (*p == ';')
701 p++;
702 /* get each parameter */
703 while (*p != '\0' && *p != ',') {
704 get_word_sep(parameter, sizeof(parameter), "=;,", &p);
705 if (!strcmp(parameter, "port")) {
706 if (*p == '=') {
707 p++;
708 rtsp_parse_range(&th->port_min, &th->port_max, &p);
709 }
710 } else if (!strcmp(parameter, "client_port")) {
711 if (*p == '=') {
712 p++;
115329f1 713 rtsp_parse_range(&th->client_port_min,
1617ad97
FB
714 &th->client_port_max, &p);
715 }
716 } else if (!strcmp(parameter, "server_port")) {
717 if (*p == '=') {
718 p++;
115329f1 719 rtsp_parse_range(&th->server_port_min,
1617ad97
FB
720 &th->server_port_max, &p);
721 }
722 } else if (!strcmp(parameter, "interleaved")) {
723 if (*p == '=') {
724 p++;
115329f1 725 rtsp_parse_range(&th->interleaved_min,
1617ad97
FB
726 &th->interleaved_max, &p);
727 }
728 } else if (!strcmp(parameter, "multicast")) {
90abbdba
RB
729 if (th->lower_transport == RTSP_LOWER_TRANSPORT_UDP)
730 th->lower_transport = RTSP_LOWER_TRANSPORT_UDP_MULTICAST;
1617ad97
FB
731 } else if (!strcmp(parameter, "ttl")) {
732 if (*p == '=') {
733 p++;
734 th->ttl = strtol(p, (char **)&p, 10);
735 }
736 } else if (!strcmp(parameter, "destination")) {
737 struct in_addr ipaddr;
738
739 if (*p == '=') {
740 p++;
741 get_word_sep(buf, sizeof(buf), ";,", &p);
115329f1 742 if (inet_aton(buf, &ipaddr))
1617ad97
FB
743 th->destination = ntohl(ipaddr.s_addr);
744 }
745 }
746 while (*p != ';' && *p != '\0' && *p != ',')
747 p++;
748 if (*p == ';')
749 p++;
750 }
751 if (*p == ',')
752 p++;
753
754 reply->nb_transports++;
755 }
756}
757
a9e534d5 758void rtsp_parse_line(RTSPMessageHeader *reply, const char *buf)
1617ad97
FB
759{
760 const char *p;
761
762 /* NOTE: we do case independent match for broken servers */
763 p = buf;
f7d78f36 764 if (av_stristart(p, "Session:", &p)) {
30e79845 765 int t;
1617ad97 766 get_word_sep(reply->session_id, sizeof(reply->session_id), ";", &p);
30e79845
RB
767 if (av_stristart(p, ";timeout=", &p) &&
768 (t = strtol(p, NULL, 10)) > 0) {
769 reply->timeout = t;
770 }
f7d78f36 771 } else if (av_stristart(p, "Content-Length:", &p)) {
1617ad97 772 reply->content_length = strtol(p, NULL, 10);
f7d78f36 773 } else if (av_stristart(p, "Transport:", &p)) {
1617ad97 774 rtsp_parse_transport(reply, p);
f7d78f36 775 } else if (av_stristart(p, "CSeq:", &p)) {
1617ad97 776 reply->seq = strtol(p, NULL, 10);
f7d78f36 777 } else if (av_stristart(p, "Range:", &p)) {
31693e00 778 rtsp_parse_range_npt(p, &reply->range_start, &reply->range_end);
30aa6aed
RB
779 } else if (av_stristart(p, "RealChallenge1:", &p)) {
780 skip_spaces(&p);
781 av_strlcpy(reply->real_challenge, p, sizeof(reply->real_challenge));
7a86bafa
RB
782 } else if (av_stristart(p, "Server:", &p)) {
783 skip_spaces(&p);
784 av_strlcpy(reply->server, p, sizeof(reply->server));
fccb1770
RB
785 } else if (av_stristart(p, "Notice:", &p) ||
786 av_stristart(p, "X-Notice:", &p)) {
787 reply->notice = strtol(p, NULL, 10);
d243ba30
LB
788 } else if (av_stristart(p, "Location:", &p)) {
789 skip_spaces(&p);
790 av_strlcpy(reply->location, p , sizeof(reply->location));
1617ad97
FB
791 }
792}
793
b7b8fc34
FB
794/* skip a RTP/TCP interleaved packet */
795static void rtsp_skip_packet(AVFormatContext *s)
796{
797 RTSPState *rt = s->priv_data;
798 int ret, len, len1;
799 uint8_t buf[1024];
800
0e848977 801 ret = url_read_complete(rt->rtsp_hd, buf, 3);
b7b8fc34
FB
802 if (ret != 3)
803 return;
80fb8234 804 len = AV_RB16(buf + 1);
67c9cd69
BC
805
806 dprintf(s, "skipping RTP packet len=%d\n", len);
807
b7b8fc34
FB
808 /* skip payload */
809 while (len > 0) {
810 len1 = len;
811 if (len1 > sizeof(buf))
812 len1 = sizeof(buf);
0e848977 813 ret = url_read_complete(rt->rtsp_hd, buf, len1);
b7b8fc34
FB
814 if (ret != len1)
815 return;
816 len -= len1;
817 }
818}
1617ad97 819
7e726132
RB
820/**
821 * Read a RTSP message from the server, or prepare to read data
822 * packets if we're reading data interleaved over the TCP/RTSP
823 * connection as well.
824 *
825 * @param s RTSP demuxer context
826 * @param reply pointer where the RTSP message header will be stored
827 * @param content_ptr pointer where the RTSP message body, if any, will
702d0a9e 828 * be stored (length is in reply)
7e726132
RB
829 * @param return_on_interleaved_data whether the function may return if we
830 * encounter a data marker ('$'), which precedes data
831 * packets over interleaved TCP/RTSP connections. If this
832 * is set, this function will return 1 after encountering
833 * a '$'. If it is not set, the function will skip any
834 * data packets (if they are encountered), until a reply
835 * has been fully parsed. If no more data is available
836 * without parsing a reply, it will return an error.
837 *
838 * @returns 1 if a data packets is ready to be received, -1 on error,
839 * and 0 on success.
840 */
c8965800
RB
841static int rtsp_read_reply(AVFormatContext *s, RTSPMessageHeader *reply,
842 unsigned char **content_ptr,
843 int return_on_interleaved_data)
1617ad97
FB
844{
845 RTSPState *rt = s->priv_data;
846 char buf[4096], buf1[1024], *q;
847 unsigned char ch;
848 const char *p;
7e726132 849 int ret, content_length, line_count = 0;
1617ad97
FB
850 unsigned char *content = NULL;
851
d541a7d2 852 memset(reply, 0, sizeof(*reply));
1617ad97 853
1617ad97 854 /* parse reply (XXX: use buffers) */
1617ad97 855 rt->last_reply[0] = '\0';
c8965800 856 for (;;) {
1617ad97 857 q = buf;
c8965800 858 for (;;) {
0e848977 859 ret = url_read_complete(rt->rtsp_hd, &ch, 1);
7e726132 860#ifdef DEBUG_RTP_TCP
67c9cd69 861 dprintf(s, "ret=%d c=%02x [%c]\n", ret, ch, ch);
7e726132
RB
862#endif
863 if (ret != 1)
864 return -1;
1617ad97
FB
865 if (ch == '\n')
866 break;
b7b8fc34
FB
867 if (ch == '$') {
868 /* XXX: only parse it if first char on line ? */
7e726132
RB
869 if (return_on_interleaved_data) {
870 return 1;
871 } else
b7b8fc34
FB
872 rtsp_skip_packet(s);
873 } else if (ch != '\r') {
1617ad97
FB
874 if ((q - buf) < sizeof(buf) - 1)
875 *q++ = ch;
876 }
877 }
878 *q = '\0';
67c9cd69
BC
879
880 dprintf(s, "line='%s'\n", buf);
881
1617ad97
FB
882 /* test if last line */
883 if (buf[0] == '\0')
884 break;
885 p = buf;
886 if (line_count == 0) {
887 /* get reply code */
888 get_word(buf1, sizeof(buf1), &p);
889 get_word(buf1, sizeof(buf1), &p);
890 reply->status_code = atoi(buf1);
891 } else {
892 rtsp_parse_line(reply, p);
75e61b0e
MR
893 av_strlcat(rt->last_reply, p, sizeof(rt->last_reply));
894 av_strlcat(rt->last_reply, "\n", sizeof(rt->last_reply));
1617ad97
FB
895 }
896 line_count++;
897 }
115329f1 898
1617ad97 899 if (rt->session_id[0] == '\0' && reply->session_id[0] != '\0')
75e61b0e 900 av_strlcpy(rt->session_id, reply->session_id, sizeof(rt->session_id));
115329f1 901
1617ad97
FB
902 content_length = reply->content_length;
903 if (content_length > 0) {
904 /* leave some room for a trailing '\0' (useful for simple parsing) */
905 content = av_malloc(content_length + 1);
0e848977 906 (void)url_read_complete(rt->rtsp_hd, content, content_length);
1617ad97
FB
907 content[content_length] = '\0';
908 }
909 if (content_ptr)
910 *content_ptr = content;
e2e2e7dd
AB
911 else
912 av_free(content);
7e726132 913
fccb1770
RB
914 /* EOS */
915 if (reply->notice == 2101 /* End-of-Stream Reached */ ||
916 reply->notice == 2104 /* Start-of-Stream Reached */ ||
c8965800 917 reply->notice == 2306 /* Continuous Feed Terminated */) {
fccb1770 918 rt->state = RTSP_STATE_IDLE;
c8965800 919 } else if (reply->notice >= 4400 && reply->notice < 5500) {
fccb1770 920 return AVERROR(EIO); /* data or server error */
c8965800 921 } else if (reply->notice == 2401 /* Ticket Expired */ ||
fccb1770
RB
922 (reply->notice >= 5500 && reply->notice < 5600) /* end of term */ )
923 return AVERROR(EPERM);
924
7e726132 925 return 0;
1617ad97
FB
926}
927
c8965800
RB
928static void rtsp_send_cmd_async(AVFormatContext *s,
929 const char *cmd, RTSPMessageHeader *reply,
930 unsigned char **content_ptr)
29b9f58b
RB
931{
932 RTSPState *rt = s->priv_data;
933 char buf[4096], buf1[1024];
934
935 rt->seq++;
936 av_strlcpy(buf, cmd, sizeof(buf));
937 snprintf(buf1, sizeof(buf1), "CSeq: %d\r\n", rt->seq);
938 av_strlcat(buf, buf1, sizeof(buf));
939 if (rt->session_id[0] != '\0' && !strstr(cmd, "\nIf-Match:")) {
940 snprintf(buf1, sizeof(buf1), "Session: %s\r\n", rt->session_id);
941 av_strlcat(buf, buf1, sizeof(buf));
942 }
f9337897
RB
943 if (rt->auth_b64)
944 av_strlcatf(buf, sizeof(buf),
ba93ea6d
RB
945 "Authorization: Basic %s\r\n",
946 rt->auth_b64);
29b9f58b 947 av_strlcat(buf, "\r\n", sizeof(buf));
67c9cd69
BC
948
949 dprintf(s, "Sending:\n%s--\n", buf);
950
29b9f58b 951 url_write(rt->rtsp_hd, buf, strlen(buf));
30e79845
RB
952 rt->last_cmd_time = av_gettime();
953}
954
c8965800
RB
955static void rtsp_send_cmd(AVFormatContext *s,
956 const char *cmd, RTSPMessageHeader *reply,
957 unsigned char **content_ptr)
30e79845
RB
958{
959 rtsp_send_cmd_async(s, cmd, reply, content_ptr);
29b9f58b 960
7e726132 961 rtsp_read_reply(s, reply, content_ptr, 0);
29b9f58b
RB
962}
963
53620bba
RB
964/**
965 * @returns 0 on success, <0 on error, 1 if protocol is unavailable.
966 */
c8965800
RB
967static int make_setup_request(AVFormatContext *s, const char *host, int port,
968 int lower_transport, const char *real_challenge)
1617ad97
FB
969{
970 RTSPState *rt = s->priv_data;
f830c9a4 971 int rtx, j, i, err, interleave = 0;
1617ad97 972 RTSPStream *rtsp_st;
a9e534d5 973 RTSPMessageHeader reply1, *reply = &reply1;
53620bba 974 char cmd[2048];
e9dea59f
RB
975 const char *trans_pref;
976
119b4668 977 if (rt->transport == RTSP_TRANSPORT_RDT)
e9dea59f
RB
978 trans_pref = "x-pn-tng";
979 else
980 trans_pref = "RTP/AVP";
115329f1 981
30e79845
RB
982 /* default timeout: 1 minute */
983 rt->timeout = 60;
984
1617ad97
FB
985 /* for each stream, make the setup request */
986 /* XXX: we assume the same server is used for the control of each
c8965800 987 * RTSP stream */
d1ccf0e0 988
c8965800 989 for (j = RTSP_RTP_PORT_MIN, i = 0; i < rt->nb_rtsp_streams; ++i) {
1617ad97
FB
990 char transport[2048];
991
f830c9a4
RB
992 /**
993 * WMS serves all UDP data over a single connection, the RTX, which
994 * isn't necessarily the first in the SDP but has to be the first
995 * to be set up, else the second/third SETUP will fail with a 461.
996 */
997 if (lower_transport == RTSP_LOWER_TRANSPORT_UDP &&
998 rt->server_type == RTSP_SERVER_WMS) {
999 if (i == 0) {
1000 /* rtx first */
1001 for (rtx = 0; rtx < rt->nb_rtsp_streams; rtx++) {
1002 int len = strlen(rt->rtsp_streams[rtx]->control_url);
1003 if (len >= 4 &&
c8965800
RB
1004 !strcmp(rt->rtsp_streams[rtx]->control_url + len - 4,
1005 "/rtx"))
f830c9a4
RB
1006 break;
1007 }
1008 if (rtx == rt->nb_rtsp_streams)
1009 return -1; /* no RTX found */
1010 rtsp_st = rt->rtsp_streams[rtx];
1011 } else
1012 rtsp_st = rt->rtsp_streams[i > rtx ? i : i - 1];
1013 } else
2fea9650 1014 rtsp_st = rt->rtsp_streams[i];
1617ad97 1015
1617ad97 1016 /* RTP/UDP */
90abbdba 1017 if (lower_transport == RTSP_LOWER_TRANSPORT_UDP) {
85fb7b34 1018 char buf[256];
85fb7b34 1019
f830c9a4
RB
1020 if (rt->server_type == RTSP_SERVER_WMS && i > 1) {
1021 port = reply->transports[0].client_port_min;
1022 goto have_port;
1023 }
1024
85fb7b34 1025 /* first try in specified port range */
d1ccf0e0 1026 if (RTSP_RTP_PORT_MIN != 0) {
c8965800
RB
1027 while (j <= RTSP_RTP_PORT_MAX) {
1028 snprintf(buf, sizeof(buf), "rtp://%s?localport=%d",
1029 host, j);
1030 /* we will use two ports per rtp stream (rtp and rtcp) */
1031 j += 2;
1032 if (url_open(&rtsp_st->rtp_handle, buf, URL_RDWR) == 0)
85fb7b34
FB
1033 goto rtp_opened;
1034 }
1617ad97 1035 }
85fb7b34 1036
c8965800
RB
1037#if 0
1038 /* then try on any port */
1039 if (url_open(&rtsp_st->rtp_handle, "rtp://", URL_RDONLY) < 0) {
1040 err = AVERROR_INVALIDDATA;
1041 goto fail;
1042 }
1043#endif
85fb7b34
FB
1044
1045 rtp_opened:
8b1ab7bf 1046 port = rtp_get_local_port(rtsp_st->rtp_handle);
f830c9a4 1047 have_port:
0ad306bc 1048 snprintf(transport, sizeof(transport) - 1,
eee2cbff
RB
1049 "%s/UDP;", trans_pref);
1050 if (rt->server_type != RTSP_SERVER_REAL)
1051 av_strlcat(transport, "unicast;", sizeof(transport));
1052 av_strlcatf(transport, sizeof(transport),
1053 "client_port=%d", port);
f830c9a4
RB
1054 if (rt->transport == RTSP_TRANSPORT_RTP &&
1055 !(rt->server_type == RTSP_SERVER_WMS && i > 0))
e9dea59f 1056 av_strlcatf(transport, sizeof(transport), "-%d", port + 1);
1617ad97
FB
1057 }
1058
1059 /* RTP/TCP */
90abbdba 1060 else if (lower_transport == RTSP_LOWER_TRANSPORT_TCP) {
090438cc
RB
1061 /** For WMS streams, the application streams are only used for
1062 * UDP. When trying to set it up for TCP streams, the server
1063 * will return an error. Therefore, we skip those streams. */
1064 if (rt->server_type == RTSP_SERVER_WMS &&
c8965800
RB
1065 s->streams[rtsp_st->stream_index]->codec->codec_type ==
1066 CODEC_TYPE_DATA)
090438cc 1067 continue;
0ad306bc 1068 snprintf(transport, sizeof(transport) - 1,
d1c6e47c
RB
1069 "%s/TCP;", trans_pref);
1070 if (rt->server_type == RTSP_SERVER_WMS)
1071 av_strlcat(transport, "unicast;", sizeof(transport));
1072 av_strlcatf(transport, sizeof(transport),
1073 "interleaved=%d-%d",
1074 interleave, interleave + 1);
1075 interleave += 2;
1617ad97
FB
1076 }
1077
90abbdba 1078 else if (lower_transport == RTSP_LOWER_TRANSPORT_UDP_MULTICAST) {
0ad306bc 1079 snprintf(transport, sizeof(transport) - 1,
e9dea59f 1080 "%s/UDP;multicast", trans_pref);
1617ad97 1081 }
d1c6e47c
RB
1082 if (rt->server_type == RTSP_SERVER_REAL ||
1083 rt->server_type == RTSP_SERVER_WMS)
e9dea59f 1084 av_strlcat(transport, ";mode=play", sizeof(transport));
115329f1 1085 snprintf(cmd, sizeof(cmd),
b6892136
FB
1086 "SETUP %s RTSP/1.0\r\n"
1087 "Transport: %s\r\n",
1617ad97 1088 rtsp_st->control_url, transport);
2e889ae4 1089 if (i == 0 && rt->server_type == RTSP_SERVER_REAL) {
e9dea59f
RB
1090 char real_res[41], real_csum[9];
1091 ff_rdt_calc_response_and_checksum(real_res, real_csum,
1092 real_challenge);
1093 av_strlcatf(cmd, sizeof(cmd),
1094 "If-Match: %s\r\n"
1095 "RealChallenge2: %s, sd=%s\r\n",
1096 rt->session_id, real_res, real_csum);
1097 }
1617ad97 1098 rtsp_send_cmd(s, cmd, reply, NULL);
8a8754d8
RB
1099 if (reply->status_code == 461 /* Unsupported protocol */ && i == 0) {
1100 err = 1;
1101 goto fail;
7e6ca34f
RB
1102 } else if (reply->status_code != RTSP_STATUS_OK ||
1103 reply->nb_transports != 1) {
1617ad97
FB
1104 err = AVERROR_INVALIDDATA;
1105 goto fail;
1106 }
1107
1108 /* XXX: same protocol for all streams is required */
1109 if (i > 0) {
119b4668
RB
1110 if (reply->transports[0].lower_transport != rt->lower_transport ||
1111 reply->transports[0].transport != rt->transport) {
1617ad97
FB
1112 err = AVERROR_INVALIDDATA;
1113 goto fail;
1114 }
1115 } else {
90abbdba 1116 rt->lower_transport = reply->transports[0].lower_transport;
119b4668 1117 rt->transport = reply->transports[0].transport;
1617ad97
FB
1118 }
1119
1120 /* close RTP connection if not choosen */
90abbdba
RB
1121 if (reply->transports[0].lower_transport != RTSP_LOWER_TRANSPORT_UDP &&
1122 (lower_transport == RTSP_LOWER_TRANSPORT_UDP)) {
8b1ab7bf
FB
1123 url_close(rtsp_st->rtp_handle);
1124 rtsp_st->rtp_handle = NULL;
1617ad97
FB
1125 }
1126
90abbdba
RB
1127 switch(reply->transports[0].lower_transport) {
1128 case RTSP_LOWER_TRANSPORT_TCP:
1617ad97
FB
1129 rtsp_st->interleaved_min = reply->transports[0].interleaved_min;
1130 rtsp_st->interleaved_max = reply->transports[0].interleaved_max;
1131 break;
115329f1 1132
c8965800
RB
1133 case RTSP_LOWER_TRANSPORT_UDP: {
1134 char url[1024];
1135
1136 /* XXX: also use address if specified */
1137 snprintf(url, sizeof(url), "rtp://%s:%d",
1138 host, reply->transports[0].server_port_min);
1139 if (!(rt->server_type == RTSP_SERVER_WMS && i > 1) &&
1140 rtp_set_remote_url(rtsp_st->rtp_handle, url) < 0) {
1141 err = AVERROR_INVALIDDATA;
1142 goto fail;
1617ad97
FB
1143 }
1144 break;
c8965800
RB
1145 }
1146 case RTSP_LOWER_TRANSPORT_UDP_MULTICAST: {
1147 char url[1024];
1148 struct in_addr in;
1149 int port, ttl;
1150
1151 if (reply->transports[0].destination) {
1152 in.s_addr = htonl(reply->transports[0].destination);
1153 port = reply->transports[0].port_min;
1154 ttl = reply->transports[0].ttl;
1155 } else {
1156 in = rtsp_st->sdp_ip;
1157 port = rtsp_st->sdp_port;
1158 ttl = rtsp_st->sdp_ttl;
1159 }
1160 snprintf(url, sizeof(url), "rtp://%s:%d?ttl=%d",
1161 inet_ntoa(in), port, ttl);
1162 if (url_open(&rtsp_st->rtp_handle, url, URL_RDWR) < 0) {
1163 err = AVERROR_INVALIDDATA;
1164 goto fail;
1617ad97
FB
1165 }
1166 break;
1167 }
c8965800 1168 }
d1ccf0e0 1169
ee0cb67f 1170 if ((err = rtsp_open_transport_ctx(s, rtsp_st)))
8b1ab7bf 1171 goto fail;
1617ad97
FB
1172 }
1173
30e79845
RB
1174 if (reply->timeout > 0)
1175 rt->timeout = reply->timeout;
1176
2e889ae4 1177 if (rt->server_type == RTSP_SERVER_REAL)
1256d16b
RB
1178 rt->need_subscription = 1;
1179
53620bba
RB
1180 return 0;
1181
1182fail:
c8965800 1183 for (i = 0; i < rt->nb_rtsp_streams; i++) {
8a8754d8
RB
1184 if (rt->rtsp_streams[i]->rtp_handle) {
1185 url_close(rt->rtsp_streams[i]->rtp_handle);
1186 rt->rtsp_streams[i]->rtp_handle = NULL;
1187 }
1188 }
53620bba
RB
1189 return err;
1190}
1191
1ced9da3
LA
1192static int rtsp_read_play(AVFormatContext *s)
1193{
1194 RTSPState *rt = s->priv_data;
1195 RTSPMessageHeader reply1, *reply = &reply1;
1196 char cmd[1024];
1197
1198 av_log(s, AV_LOG_DEBUG, "hello state=%d\n", rt->state);
1199
1200 if (!(rt->server_type == RTSP_SERVER_REAL && rt->need_subscription)) {
1201 if (rt->state == RTSP_STATE_PAUSED) {
1202 snprintf(cmd, sizeof(cmd),
1203 "PLAY %s RTSP/1.0\r\n",
1204 s->filename);
1205 } else {
1206 snprintf(cmd, sizeof(cmd),
1207 "PLAY %s RTSP/1.0\r\n"
1208 "Range: npt=%0.3f-\r\n",
1209 s->filename,
1210 (double)rt->seek_timestamp / AV_TIME_BASE);
1211 }
1212 rtsp_send_cmd(s, cmd, reply, NULL);
1213 if (reply->status_code != RTSP_STATUS_OK) {
1214 return -1;
1215 }
1216 }
1217 rt->state = RTSP_STATE_PLAYING;
1218 return 0;
1219}
1220
53620bba
RB
1221static int rtsp_read_header(AVFormatContext *s,
1222 AVFormatParameters *ap)
1223{
1224 RTSPState *rt = s->priv_data;
921da217
LB
1225 char host[1024], path[1024], tcpname[1024], cmd[2048], auth[128];
1226 char *option_list, *option, *filename;
53620bba
RB
1227 URLContext *rtsp_hd;
1228 int port, ret, err;
a9e534d5 1229 RTSPMessageHeader reply1, *reply = &reply1;
53620bba 1230 unsigned char *content = NULL;
90abbdba 1231 int lower_transport_mask = 0;
1cf151e9 1232 char real_challenge[64];
c8965800 1233redirect:
53620bba 1234 /* extract hostname and port */
f9337897 1235 url_split(NULL, 0, auth, sizeof(auth),
53620bba 1236 host, sizeof(host), &port, path, sizeof(path), s->filename);
f9337897
RB
1237 if (*auth) {
1238 int auth_len = strlen(auth), b64_len = ((auth_len + 2) / 3) * 4 + 1;
1239
1240 if (!(rt->auth_b64 = av_malloc(b64_len)))
1241 return AVERROR(ENOMEM);
1242 if (!av_base64_encode(rt->auth_b64, b64_len, auth, auth_len)) {
1243 err = AVERROR(EINVAL);
1244 goto fail;
1245 }
1246 }
53620bba
RB
1247 if (port < 0)
1248 port = RTSP_DEFAULT_PORT;
1249
1250 /* search for options */
1251 option_list = strchr(path, '?');
1252 if (option_list) {
921da217 1253 filename = strchr(s->filename, '?');
c8965800 1254 while (option_list) {
53620bba 1255 /* move the option pointer */
921da217 1256 option = ++option_list;
53620bba
RB
1257 option_list = strchr(option_list, '&');
1258 if (option_list)
921da217
LB
1259 *option_list = 0;
1260
53620bba 1261 /* handle the options */
c8965800 1262 if (!strcmp(option, "udp")) {
90abbdba 1263 lower_transport_mask = (1<< RTSP_LOWER_TRANSPORT_UDP);
c8965800 1264 } else if (!strcmp(option, "multicast")) {
90abbdba 1265 lower_transport_mask = (1<< RTSP_LOWER_TRANSPORT_UDP_MULTICAST);
c8965800 1266 } else if (!strcmp(option, "tcp")) {
90abbdba 1267 lower_transport_mask = (1<< RTSP_LOWER_TRANSPORT_TCP);
c8965800 1268 } else {
921da217
LB
1269 strcpy(++filename, option);
1270 filename += strlen(option);
1271 if (option_list) *filename = '&';
1272 }
53620bba 1273 }
921da217 1274 *filename = 0;
53620bba
RB
1275 }
1276
90abbdba 1277 if (!lower_transport_mask)
2a1d51c5 1278 lower_transport_mask = (1 << RTSP_LOWER_TRANSPORT_NB) - 1;
53620bba
RB
1279
1280 /* open the tcp connexion */
1281 snprintf(tcpname, sizeof(tcpname), "tcp://%s:%d", host, port);
f9337897
RB
1282 if (url_open(&rtsp_hd, tcpname, URL_RDWR) < 0) {
1283 err = AVERROR(EIO);
1284 goto fail;
1285 }
53620bba
RB
1286 rt->rtsp_hd = rtsp_hd;
1287 rt->seq = 0;
1288
c8965800
RB
1289 /* request options supported by the server; this also detects server
1290 * type */
1cf151e9
RB
1291 for (rt->server_type = RTSP_SERVER_RTP;;) {
1292 snprintf(cmd, sizeof(cmd),
1293 "OPTIONS %s RTSP/1.0\r\n", s->filename);
2e889ae4 1294 if (rt->server_type == RTSP_SERVER_REAL)
1cf151e9
RB
1295 av_strlcat(cmd,
1296 /**
1297 * The following entries are required for proper
1298 * streaming from a Realmedia server. They are
1299 * interdependent in some way although we currently
1300 * don't quite understand how. Values were copied
1301 * from mplayer SVN r23589.
1302 * @param CompanyID is a 16-byte ID in base64
1303 * @param ClientChallenge is a 16-byte ID in hex
1304 */
1305 "ClientChallenge: 9e26d33f2984236010ef6253fb1887f7\r\n"
1306 "PlayerStarttime: [28/03/2003:22:50:23 00:00]\r\n"
1307 "CompanyID: KnKV4M4I/B2FjJ1TToLycw==\r\n"
1308 "GUID: 00000000-0000-0000-0000-000000000000\r\n",
1309 sizeof(cmd));
1310 rtsp_send_cmd(s, cmd, reply, NULL);
1311 if (reply->status_code != RTSP_STATUS_OK) {
1312 err = AVERROR_INVALIDDATA;
1313 goto fail;
1314 }
1315
1316 /* detect server type if not standard-compliant RTP */
2e889ae4
RB
1317 if (rt->server_type != RTSP_SERVER_REAL && reply->real_challenge[0]) {
1318 rt->server_type = RTSP_SERVER_REAL;
1cf151e9 1319 continue;
7a86bafa
RB
1320 } else if (!strncasecmp(reply->server, "WMServer/", 9)) {
1321 rt->server_type = RTSP_SERVER_WMS;
c8965800 1322 } else if (rt->server_type == RTSP_SERVER_REAL)
1cf151e9 1323 strcpy(real_challenge, reply->real_challenge);
1cf151e9
RB
1324 break;
1325 }
1326
53620bba
RB
1327 /* describe the stream */
1328 snprintf(cmd, sizeof(cmd),
1329 "DESCRIBE %s RTSP/1.0\r\n"
1330 "Accept: application/sdp\r\n",
1331 s->filename);
2e889ae4 1332 if (rt->server_type == RTSP_SERVER_REAL) {
897ade1b
RB
1333 /**
1334 * The Require: attribute is needed for proper streaming from
1335 * Realmedia servers.
1336 */
1337 av_strlcat(cmd,
1338 "Require: com.real.retain-entity-for-setup\r\n",
1339 sizeof(cmd));
1340 }
53620bba
RB
1341 rtsp_send_cmd(s, cmd, reply, &content);
1342 if (!content) {
1343 err = AVERROR_INVALIDDATA;
1344 goto fail;
1345 }
1346 if (reply->status_code != RTSP_STATUS_OK) {
1347 err = AVERROR_INVALIDDATA;
1348 goto fail;
1349 }
1350
1351 /* now we got the SDP description, we parse it */
1352 ret = sdp_parse(s, (const char *)content);
1353 av_freep(&content);
1354 if (ret < 0) {
1355 err = AVERROR_INVALIDDATA;
1356 goto fail;
1357 }
1358
8a8754d8 1359 do {
c8965800
RB
1360 int lower_transport = ff_log2_tab[lower_transport_mask &
1361 ~(lower_transport_mask - 1)];
8a8754d8 1362
90abbdba 1363 err = make_setup_request(s, host, port, lower_transport,
2e889ae4 1364 rt->server_type == RTSP_SERVER_REAL ?
e9dea59f 1365 real_challenge : NULL);
8a8754d8 1366 if (err < 0)
7e6ca34f 1367 goto fail;
90abbdba
RB
1368 lower_transport_mask &= ~(1 << lower_transport);
1369 if (lower_transport_mask == 0 && err == 1) {
5ee0e139 1370 err = AVERROR(FF_NETERROR(EPROTONOSUPPORT));
8a8754d8
RB
1371 goto fail;
1372 }
1373 } while (err);
53620bba 1374
ff762d6e 1375 rt->state = RTSP_STATE_IDLE;
c8965800 1376 rt->seek_timestamp = 0; /* default is to start stream at position zero */
c04c3282 1377 if (ap->initial_pause) {
ff762d6e
FB
1378 /* do not start immediately */
1379 } else {
1380 if (rtsp_read_play(s) < 0) {
1381 err = AVERROR_INVALIDDATA;
1617ad97
FB
1382 goto fail;
1383 }
1384 }
1617ad97
FB
1385 return 0;
1386 fail:
8b1ab7bf 1387 rtsp_close_streams(rt);
1617ad97
FB
1388 av_freep(&content);
1389 url_close(rt->rtsp_hd);
d243ba30
LB
1390 if (reply->status_code >=300 && reply->status_code < 400) {
1391 av_strlcpy(s->filename, reply->location, sizeof(s->filename));
1392 av_log(s, AV_LOG_INFO, "Status %d: Redirecting to %s\n",
1393 reply->status_code,
1394 s->filename);
1395 goto redirect;
1396 }
1617ad97
FB
1397 return err;
1398}
1399
0e59034e
RB
1400static int udp_read_packet(AVFormatContext *s, RTSPStream **prtsp_st,
1401 uint8_t *buf, int buf_size)
1402{
1403 RTSPState *rt = s->priv_data;
1404 RTSPStream *rtsp_st;
1405 fd_set rfds;
1406 int fd, fd_max, n, i, ret, tcp_fd;
1407 struct timeval tv;
1408
c8965800 1409 for (;;) {
0e59034e
RB
1410 if (url_interrupt_cb())
1411 return AVERROR(EINTR);
1412 FD_ZERO(&rfds);
1413 if (rt->rtsp_hd) {
1414 tcp_fd = fd_max = url_get_file_handle(rt->rtsp_hd);
1415 FD_SET(tcp_fd, &rfds);
1416 } else {
1417 fd_max = 0;
1418 tcp_fd = -1;
1419 }
c8965800 1420 for (i = 0; i < rt->nb_rtsp_streams; i++) {
0e59034e
RB
1421 rtsp_st = rt->rtsp_streams[i];
1422 if (rtsp_st->rtp_handle) {
1423 /* currently, we cannot probe RTCP handle because of
1424 * blocking restrictions */
1425 fd = url_get_file_handle(rtsp_st->rtp_handle);
1426 if (fd > fd_max)
1427 fd_max = fd;
1428 FD_SET(fd, &rfds);
1429 }
1430 }
1431 tv.tv_sec = 0;
1432 tv.tv_usec = 100 * 1000;
1433 n = select(fd_max + 1, &rfds, NULL, NULL, &tv);
1434 if (n > 0) {
c8965800 1435 for (i = 0; i < rt->nb_rtsp_streams; i++) {
0e59034e
RB
1436 rtsp_st = rt->rtsp_streams[i];
1437 if (rtsp_st->rtp_handle) {
1438 fd = url_get_file_handle(rtsp_st->rtp_handle);
1439 if (FD_ISSET(fd, &rfds)) {
1440 ret = url_read(rtsp_st->rtp_handle, buf, buf_size);
1441 if (ret > 0) {
1442 *prtsp_st = rtsp_st;
1443 return ret;
1444 }
1445 }
1446 }
1447 }
1448#if CONFIG_RTSP_DEMUXER
1449 if (FD_ISSET(tcp_fd, &rfds)) {
1450 RTSPMessageHeader reply;
1451
1452 rtsp_read_reply(s, &reply, NULL, 0);
1453 /* XXX: parse message */
1454 if (rt->state != RTSP_STATE_PLAYING)
1455 return 0;
1456 }
1457#endif
1458 }
1459 }
1460}
1461
8b1ab7bf
FB
1462static int tcp_read_packet(AVFormatContext *s, RTSPStream **prtsp_st,
1463 uint8_t *buf, int buf_size)
1617ad97
FB
1464{
1465 RTSPState *rt = s->priv_data;
b6892136 1466 int id, len, i, ret;
1617ad97 1467 RTSPStream *rtsp_st;
1617ad97 1468
b6892136 1469#ifdef DEBUG_RTP_TCP
67c9cd69 1470 dprintf(s, "tcp_read_packet:\n");
b6892136 1471#endif
c8965800
RB
1472redo:
1473 for (;;) {
7e726132
RB
1474 RTSPMessageHeader reply;
1475
1476 ret = rtsp_read_reply(s, &reply, NULL, 1);
1477 if (ret == -1)
8b1ab7bf 1478 return -1;
7e726132 1479 if (ret == 1) /* received '$' */
1617ad97 1480 break;
7e726132 1481 /* XXX: parse message */
fccb1770
RB
1482 if (rt->state != RTSP_STATE_PLAYING)
1483 return 0;
1617ad97 1484 }
0e848977 1485 ret = url_read_complete(rt->rtsp_hd, buf, 3);
b6892136 1486 if (ret != 3)
8b1ab7bf 1487 return -1;
c8965800 1488 id = buf[0];
80fb8234 1489 len = AV_RB16(buf + 1);
b6892136 1490#ifdef DEBUG_RTP_TCP
67c9cd69 1491 dprintf(s, "id=%d len=%d\n", id, len);
b6892136 1492#endif
8b1ab7bf 1493 if (len > buf_size || len < 12)
1617ad97
FB
1494 goto redo;
1495 /* get the data */
0e848977 1496 ret = url_read_complete(rt->rtsp_hd, buf, len);
b6892136 1497 if (ret != len)
8b1ab7bf 1498 return -1;
985b05d3 1499 if (rt->transport == RTSP_TRANSPORT_RDT &&
114732f4 1500 ff_rdt_parse_header(buf, len, &id, NULL, NULL, NULL, NULL) < 0)
985b05d3 1501 return -1;
115329f1 1502
1617ad97 1503 /* find the matching stream */
c8965800 1504 for (i = 0; i < rt->nb_rtsp_streams; i++) {
8b1ab7bf 1505 rtsp_st = rt->rtsp_streams[i];
115329f1
DB
1506 if (id >= rtsp_st->interleaved_min &&
1507 id <= rtsp_st->interleaved_max)
1617ad97
FB
1508 goto found;
1509 }
1510 goto redo;
c8965800 1511found:
8b1ab7bf
FB
1512 *prtsp_st = rtsp_st;
1513 return len;
1617ad97
FB
1514}
1515
0e59034e
RB
1516static int rtsp_fetch_packet(AVFormatContext *s, AVPacket *pkt)
1517{
1518 RTSPState *rt = s->priv_data;
1519 int ret, len;
1520 uint8_t buf[10 * RTP_MAX_PACKET_LENGTH];
1521 RTSPStream *rtsp_st;
1522
1523 /* get next frames from the same RTP packet */
1524 if (rt->cur_transport_priv) {
c8965800 1525 if (rt->transport == RTSP_TRANSPORT_RDT) {
0e59034e 1526 ret = ff_rdt_parse_packet(rt->cur_transport_priv, pkt, NULL, 0);
c8965800 1527 } else
0e59034e
RB
1528 ret = rtp_parse_packet(rt->cur_transport_priv, pkt, NULL, 0);
1529 if (ret == 0) {
1530 rt->cur_transport_priv = NULL;
1531 return 0;
1532 } else if (ret == 1) {
1533 return 0;
c8965800 1534 } else
0e59034e 1535 rt->cur_transport_priv = NULL;
0e59034e
RB
1536 }
1537
1538 /* read next RTP packet */
1539 redo:
1540 switch(rt->lower_transport) {
1541 default:
1542#if CONFIG_RTSP_DEMUXER
1543 case RTSP_LOWER_TRANSPORT_TCP:
1544 len = tcp_read_packet(s, &rtsp_st, buf, sizeof(buf));
1545 break;
1546#endif
1547 case RTSP_LOWER_TRANSPORT_UDP:
1548 case RTSP_LOWER_TRANSPORT_UDP_MULTICAST:
1549 len = udp_read_packet(s, &rtsp_st, buf, sizeof(buf));
1550 if (len >=0 && rtsp_st->transport_priv && rt->transport == RTSP_TRANSPORT_RTP)
1551 rtp_check_and_send_back_rr(rtsp_st->transport_priv, len);
1552 break;
1553 }
1554 if (len < 0)
1555 return len;
1556 if (len == 0)
1557 return AVERROR_EOF;
c8965800 1558 if (rt->transport == RTSP_TRANSPORT_RDT) {
0e59034e 1559 ret = ff_rdt_parse_packet(rtsp_st->transport_priv, pkt, buf, len);
c8965800 1560 } else
0e59034e
RB
1561 ret = rtp_parse_packet(rtsp_st->transport_priv, pkt, buf, len);
1562 if (ret < 0)
1563 goto redo;
c8965800 1564 if (ret == 1)
0e59034e
RB
1565 /* more packets may follow, so we save the RTP context */
1566 rt->cur_transport_priv = rtsp_st->transport_priv;
0e59034e
RB
1567
1568 return ret;
1569}
1570
c8965800 1571static int rtsp_read_packet(AVFormatContext *s, AVPacket *pkt)
1617ad97
FB
1572{
1573 RTSPState *rt = s->priv_data;
98713182 1574 int ret;
30e79845
RB
1575 RTSPMessageHeader reply1, *reply = &reply1;
1576 char cmd[1024];
8b1ab7bf 1577
572c6a38 1578 if (rt->server_type == RTSP_SERVER_REAL) {
1256d16b 1579 int i;
572c6a38 1580 enum AVDiscard cache[MAX_STREAMS];
1256d16b 1581
572c6a38
RB
1582 for (i = 0; i < s->nb_streams; i++)
1583 cache[i] = s->streams[i]->discard;
1584
1585 if (!rt->need_subscription) {
1586 if (memcmp (cache, rt->real_setup_cache,
1587 sizeof(enum AVDiscard) * s->nb_streams)) {
1588 av_strlcatf(cmd, sizeof(cmd),
1589 "SET_PARAMETER %s RTSP/1.0\r\n"
1590 "Unsubscribe: %s\r\n",
1591 s->filename, rt->last_subscription);
1592 rtsp_send_cmd(s, cmd, reply, NULL);
1593 if (reply->status_code != RTSP_STATUS_OK)
1594 return AVERROR_INVALIDDATA;
1595 rt->need_subscription = 1;
1596 }
1256d16b 1597 }
1256d16b 1598
572c6a38
RB
1599 if (rt->need_subscription) {
1600 int r, rule_nr, first = 1;
1601
1602 memcpy(rt->real_setup_cache, cache,
1603 sizeof(enum AVDiscard) * s->nb_streams);
1604 rt->last_subscription[0] = 0;
1605
1606 snprintf(cmd, sizeof(cmd),
1607 "SET_PARAMETER %s RTSP/1.0\r\n"
1608 "Subscribe: ",
1609 s->filename);
1610 for (i = 0; i < rt->nb_rtsp_streams; i++) {
1611 rule_nr = 0;
1612 for (r = 0; r < s->nb_streams; r++) {
1613 if (s->streams[r]->priv_data == rt->rtsp_streams[i]) {
1614 if (s->streams[r]->discard != AVDISCARD_ALL) {
1615 if (!first)
1616 av_strlcat(rt->last_subscription, ",",
1617 sizeof(rt->last_subscription));
1618 ff_rdt_subscribe_rule(
1619 rt->last_subscription,
1620 sizeof(rt->last_subscription), i, rule_nr);
1621 first = 0;
1622 }
1623 rule_nr++;
1624 }
1625 }
1626 }
1627 av_strlcatf(cmd, sizeof(cmd), "%s\r\n", rt->last_subscription);
1628 rtsp_send_cmd(s, cmd, reply, NULL);
1629 if (reply->status_code != RTSP_STATUS_OK)
1630 return AVERROR_INVALIDDATA;
1631 rt->need_subscription = 0;
1632
1633 if (rt->state == RTSP_STATE_PLAYING)
1634 rtsp_read_play (s);
1635 }
1256d16b
RB
1636 }
1637
d7250724 1638 ret = rtsp_fetch_packet(s, pkt);
c8965800 1639 if (ret < 0)
98713182 1640 return ret;
30e79845
RB
1641
1642 /* send dummy request to keep TCP connection alive */
1643 if ((rt->server_type == RTSP_SERVER_WMS ||
1644 rt->server_type == RTSP_SERVER_REAL) &&
1645 (av_gettime() - rt->last_cmd_time) / 1000000 >= rt->timeout / 2) {
1646 if (rt->server_type == RTSP_SERVER_WMS) {
1647 snprintf(cmd, sizeof(cmd) - 1,
1648 "GET_PARAMETER %s RTSP/1.0\r\n",
1649 s->filename);
1650 rtsp_send_cmd_async(s, cmd, reply, NULL);
1651 } else {
1652 rtsp_send_cmd_async(s, "OPTIONS * RTSP/1.0\r\n",
1653 reply, NULL);
1654 }
1655 }
1656
8b1ab7bf 1657 return 0;
1617ad97
FB
1658}
1659
ff762d6e
FB
1660/* pause the stream */
1661static int rtsp_read_pause(AVFormatContext *s)
b7b8fc34 1662{
ff762d6e 1663 RTSPState *rt = s->priv_data;
a9e534d5 1664 RTSPMessageHeader reply1, *reply = &reply1;
b7b8fc34
FB
1665 char cmd[1024];
1666
b7b8fc34 1667 rt = s->priv_data;
115329f1 1668
ff762d6e
FB
1669 if (rt->state != RTSP_STATE_PLAYING)
1670 return 0;
2e889ae4 1671 else if (!(rt->server_type == RTSP_SERVER_REAL && rt->need_subscription)) {
99b2ac07
RB
1672 snprintf(cmd, sizeof(cmd),
1673 "PAUSE %s RTSP/1.0\r\n",
1674 s->filename);
1675 rtsp_send_cmd(s, cmd, reply, NULL);
1676 if (reply->status_code != RTSP_STATUS_OK) {
1677 return -1;
1678 }
1256d16b 1679 }
5f86057f
RB
1680 rt->state = RTSP_STATE_PAUSED;
1681 return 0;
b7b8fc34
FB
1682}
1683
115329f1 1684static int rtsp_read_seek(AVFormatContext *s, int stream_index,
7b3c1382 1685 int64_t timestamp, int flags)
ff762d6e
FB
1686{
1687 RTSPState *rt = s->priv_data;
115329f1 1688
c8965800
RB
1689 rt->seek_timestamp = av_rescale_q(timestamp,
1690 s->streams[stream_index]->time_base,
1691 AV_TIME_BASE_Q);
ff762d6e
FB
1692 switch(rt->state) {
1693 default:
1694 case RTSP_STATE_IDLE:
1695 break;
1696 case RTSP_STATE_PLAYING:
ec606b36
LB
1697 if (rtsp_read_pause(s) != 0)
1698 return -1;
1699 rt->state = RTSP_STATE_SEEKING;
ff762d6e
FB
1700 if (rtsp_read_play(s) != 0)
1701 return -1;
1702 break;
1703 case RTSP_STATE_PAUSED:
1704 rt->state = RTSP_STATE_IDLE;
1705 break;
1706 }
1707 return 0;
1708}
1709
1617ad97
FB
1710static int rtsp_read_close(AVFormatContext *s)
1711{
1712 RTSPState *rt = s->priv_data;
a9e534d5 1713 RTSPMessageHeader reply1, *reply = &reply1;
1617ad97
FB
1714 char cmd[1024];
1715
b6892136 1716#if 0
1617ad97 1717 /* NOTE: it is valid to flush the buffer here */
90abbdba 1718 if (rt->lower_transport == RTSP_LOWER_TRANSPORT_TCP) {
1617ad97
FB
1719 url_fclose(&rt->rtsp_gb);
1720 }
b6892136 1721#endif
115329f1 1722 snprintf(cmd, sizeof(cmd),
b6892136 1723 "TEARDOWN %s RTSP/1.0\r\n",
1617ad97
FB
1724 s->filename);
1725 rtsp_send_cmd(s, cmd, reply, NULL);
1726
8b1ab7bf 1727 rtsp_close_streams(rt);
1617ad97
FB
1728 url_close(rt->rtsp_hd);
1729 return 0;
1730}
1731
d2a067d1 1732AVInputFormat rtsp_demuxer = {
1617ad97 1733 "rtsp",
bde15e74 1734 NULL_IF_CONFIG_SMALL("RTSP input format"),
1617ad97
FB
1735 sizeof(RTSPState),
1736 rtsp_probe,
1737 rtsp_read_header,
1738 rtsp_read_packet,
1739 rtsp_read_close,
ff762d6e 1740 rtsp_read_seek,
bb76a117 1741 .flags = AVFMT_NOFILE,
ff762d6e
FB
1742 .read_play = rtsp_read_play,
1743 .read_pause = rtsp_read_pause,
1617ad97 1744};
96862926 1745#endif
1617ad97 1746
cb1fdc61 1747static int sdp_probe(AVProbeData *p1)
93ced3e8 1748{
0e1ceacd 1749 const char *p = p1->buf, *p_end = p1->buf + p1->buf_size;
cb1fdc61
FB
1750
1751 /* we look for a line beginning "c=IN IP4" */
0e1ceacd 1752 while (p < p_end && *p != '\0') {
c8965800
RB
1753 if (p + sizeof("c=IN IP4") - 1 < p_end &&
1754 av_strstart(p, "c=IN IP4", NULL))
cb1fdc61 1755 return AVPROBE_SCORE_MAX / 2;
0e1ceacd 1756
c8965800 1757 while (p < p_end - 1 && *p != '\n') p++;
0e1ceacd 1758 if (++p >= p_end)
cb1fdc61 1759 break;
cb1fdc61
FB
1760 if (*p == '\r')
1761 p++;
1762 }
93ced3e8
FB
1763 return 0;
1764}
1765
1766#define SDP_MAX_SIZE 8192
1767
c8965800 1768static int sdp_read_header(AVFormatContext *s, AVFormatParameters *ap)
93ced3e8 1769{
8b1ab7bf 1770 RTSPState *rt = s->priv_data;
93ced3e8
FB
1771 RTSPStream *rtsp_st;
1772 int size, i, err;
1773 char *content;
1774 char url[1024];
1775
1776 /* read the whole sdp file */
1777 /* XXX: better loading */
1778 content = av_malloc(SDP_MAX_SIZE);
899681cd 1779 size = get_buffer(s->pb, content, SDP_MAX_SIZE - 1);
93ced3e8
FB
1780 if (size <= 0) {
1781 av_free(content);
1782 return AVERROR_INVALIDDATA;
1783 }
1784 content[size] ='\0';
1785
1786 sdp_parse(s, content);
1787 av_free(content);
1788
1789 /* open each RTP stream */
c8965800 1790 for (i = 0; i < rt->nb_rtsp_streams; i++) {
8b1ab7bf 1791 rtsp_st = rt->rtsp_streams[i];
115329f1 1792
d2bf42be 1793 snprintf(url, sizeof(url), "rtp://%s:%d?localport=%d&ttl=%d",
115329f1 1794 inet_ntoa(rtsp_st->sdp_ip),
93ced3e8 1795 rtsp_st->sdp_port,
d2bf42be 1796 rtsp_st->sdp_port,
93ced3e8 1797 rtsp_st->sdp_ttl);
dbf30963 1798 if (url_open(&rtsp_st->rtp_handle, url, URL_RDWR) < 0) {
93ced3e8
FB
1799 err = AVERROR_INVALIDDATA;
1800 goto fail;
1801 }
ee0cb67f 1802 if ((err = rtsp_open_transport_ctx(s, rtsp_st)))
8b1ab7bf 1803 goto fail;
93ced3e8
FB
1804 }
1805 return 0;
c8965800 1806fail:
8b1ab7bf 1807 rtsp_close_streams(rt);
93ced3e8
FB
1808 return err;
1809}
1810
93ced3e8
FB
1811static int sdp_read_close(AVFormatContext *s)
1812{
8b1ab7bf
FB
1813 RTSPState *rt = s->priv_data;
1814 rtsp_close_streams(rt);
93ced3e8
FB
1815 return 0;
1816}
1817
ff70e601 1818AVInputFormat sdp_demuxer = {
93ced3e8 1819 "sdp",
bde15e74 1820 NULL_IF_CONFIG_SMALL("SDP"),
93ced3e8
FB
1821 sizeof(RTSPState),
1822 sdp_probe,
1823 sdp_read_header,
d7250724 1824 rtsp_fetch_packet,
93ced3e8
FB
1825 sdp_read_close,
1826};