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