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