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