00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "libavutil/avassert.h"
00023 #include "libavutil/base64.h"
00024 #include "libavutil/avstring.h"
00025 #include "libavutil/intreadwrite.h"
00026 #include "libavutil/mathematics.h"
00027 #include "libavutil/parseutils.h"
00028 #include "libavutil/random_seed.h"
00029 #include "libavutil/dict.h"
00030 #include "libavutil/opt.h"
00031 #include "libavutil/time.h"
00032 #include "avformat.h"
00033 #include "avio_internal.h"
00034
00035 #if HAVE_POLL_H
00036 #include <poll.h>
00037 #endif
00038 #include "internal.h"
00039 #include "network.h"
00040 #include "os_support.h"
00041 #include "http.h"
00042 #include "rtsp.h"
00043
00044 #include "rtpdec.h"
00045 #include "rdt.h"
00046 #include "rtpdec_formats.h"
00047 #include "rtpenc_chain.h"
00048 #include "url.h"
00049 #include "rtpenc.h"
00050 #include "mpegts.h"
00051
00052
00053
00054
00055
00056 #define POLL_TIMEOUT_MS 100
00057 #define READ_PACKET_TIMEOUT_S 10
00058 #define MAX_TIMEOUTS READ_PACKET_TIMEOUT_S * 1000 / POLL_TIMEOUT_MS
00059 #define SDP_MAX_SIZE 16384
00060 #define RECVBUF_SIZE 10 * RTP_MAX_PACKET_LENGTH
00061 #define DEFAULT_REORDERING_DELAY 100000
00062
00063 #define OFFSET(x) offsetof(RTSPState, x)
00064 #define DEC AV_OPT_FLAG_DECODING_PARAM
00065 #define ENC AV_OPT_FLAG_ENCODING_PARAM
00066
00067 #define RTSP_FLAG_OPTS(name, longname) \
00068 { name, longname, OFFSET(rtsp_flags), AV_OPT_TYPE_FLAGS, {.i64 = 0}, INT_MIN, INT_MAX, DEC, "rtsp_flags" }, \
00069 { "filter_src", "Only receive packets from the negotiated peer IP", 0, AV_OPT_TYPE_CONST, {.i64 = RTSP_FLAG_FILTER_SRC}, 0, 0, DEC, "rtsp_flags" }, \
00070 { "listen", "Wait for incoming connections", 0, AV_OPT_TYPE_CONST, {.i64 = RTSP_FLAG_LISTEN}, 0, 0, DEC, "rtsp_flags" }
00071
00072 #define RTSP_MEDIATYPE_OPTS(name, longname) \
00073 { name, longname, OFFSET(media_type_mask), AV_OPT_TYPE_FLAGS, { .i64 = (1 << (AVMEDIA_TYPE_DATA+1)) - 1 }, INT_MIN, INT_MAX, DEC, "allowed_media_types" }, \
00074 { "video", "Video", 0, AV_OPT_TYPE_CONST, {.i64 = 1 << AVMEDIA_TYPE_VIDEO}, 0, 0, DEC, "allowed_media_types" }, \
00075 { "audio", "Audio", 0, AV_OPT_TYPE_CONST, {.i64 = 1 << AVMEDIA_TYPE_AUDIO}, 0, 0, DEC, "allowed_media_types" }, \
00076 { "data", "Data", 0, AV_OPT_TYPE_CONST, {.i64 = 1 << AVMEDIA_TYPE_DATA}, 0, 0, DEC, "allowed_media_types" }
00077
00078 const AVOption ff_rtsp_options[] = {
00079 { "initial_pause", "Don't start playing the stream immediately", OFFSET(initial_pause), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, DEC },
00080 FF_RTP_FLAG_OPTS(RTSPState, rtp_muxer_flags),
00081 { "rtsp_transport", "RTSP transport protocols", OFFSET(lower_transport_mask), AV_OPT_TYPE_FLAGS, {.i64 = 0}, INT_MIN, INT_MAX, DEC|ENC, "rtsp_transport" }, \
00082 { "udp", "UDP", 0, AV_OPT_TYPE_CONST, {.i64 = 1 << RTSP_LOWER_TRANSPORT_UDP}, 0, 0, DEC|ENC, "rtsp_transport" }, \
00083 { "tcp", "TCP", 0, AV_OPT_TYPE_CONST, {.i64 = 1 << RTSP_LOWER_TRANSPORT_TCP}, 0, 0, DEC|ENC, "rtsp_transport" }, \
00084 { "udp_multicast", "UDP multicast", 0, AV_OPT_TYPE_CONST, {.i64 = 1 << RTSP_LOWER_TRANSPORT_UDP_MULTICAST}, 0, 0, DEC, "rtsp_transport" },
00085 { "http", "HTTP tunneling", 0, AV_OPT_TYPE_CONST, {.i64 = (1 << RTSP_LOWER_TRANSPORT_HTTP)}, 0, 0, DEC, "rtsp_transport" },
00086 RTSP_FLAG_OPTS("rtsp_flags", "RTSP flags"),
00087 RTSP_MEDIATYPE_OPTS("allowed_media_types", "Media types to accept from the server"),
00088 { "min_port", "Minimum local UDP port", OFFSET(rtp_port_min), AV_OPT_TYPE_INT, {.i64 = RTSP_RTP_PORT_MIN}, 0, 65535, DEC|ENC },
00089 { "max_port", "Maximum local UDP port", OFFSET(rtp_port_max), AV_OPT_TYPE_INT, {.i64 = RTSP_RTP_PORT_MAX}, 0, 65535, DEC|ENC },
00090 { "timeout", "Maximum timeout (in seconds) to wait for incoming connections. -1 is infinite. Implies flag listen", OFFSET(initial_timeout), AV_OPT_TYPE_INT, {.i64 = -1}, INT_MIN, INT_MAX, DEC },
00091 { NULL },
00092 };
00093
00094 static const AVOption sdp_options[] = {
00095 RTSP_FLAG_OPTS("sdp_flags", "SDP flags"),
00096 RTSP_MEDIATYPE_OPTS("allowed_media_types", "Media types to accept from the server"),
00097 { NULL },
00098 };
00099
00100 static const AVOption rtp_options[] = {
00101 RTSP_FLAG_OPTS("rtp_flags", "RTP flags"),
00102 { NULL },
00103 };
00104
00105 static void get_word_until_chars(char *buf, int buf_size,
00106 const char *sep, const char **pp)
00107 {
00108 const char *p;
00109 char *q;
00110
00111 p = *pp;
00112 p += strspn(p, SPACE_CHARS);
00113 q = buf;
00114 while (!strchr(sep, *p) && *p != '\0') {
00115 if ((q - buf) < buf_size - 1)
00116 *q++ = *p;
00117 p++;
00118 }
00119 if (buf_size > 0)
00120 *q = '\0';
00121 *pp = p;
00122 }
00123
00124 static void get_word_sep(char *buf, int buf_size, const char *sep,
00125 const char **pp)
00126 {
00127 if (**pp == '/') (*pp)++;
00128 get_word_until_chars(buf, buf_size, sep, pp);
00129 }
00130
00131 static void get_word(char *buf, int buf_size, const char **pp)
00132 {
00133 get_word_until_chars(buf, buf_size, SPACE_CHARS, pp);
00134 }
00135
00140 static void rtsp_parse_range_npt(const char *p, int64_t *start, int64_t *end)
00141 {
00142 char buf[256];
00143
00144 p += strspn(p, SPACE_CHARS);
00145 if (!av_stristart(p, "npt=", &p))
00146 return;
00147
00148 *start = AV_NOPTS_VALUE;
00149 *end = AV_NOPTS_VALUE;
00150
00151 get_word_sep(buf, sizeof(buf), "-", &p);
00152 av_parse_time(start, buf, 1);
00153 if (*p == '-') {
00154 p++;
00155 get_word_sep(buf, sizeof(buf), "-", &p);
00156 av_parse_time(end, buf, 1);
00157 }
00158
00159
00160 }
00161
00162 static int get_sockaddr(const char *buf, struct sockaddr_storage *sock)
00163 {
00164 struct addrinfo hints = { 0 }, *ai = NULL;
00165 hints.ai_flags = AI_NUMERICHOST;
00166 if (getaddrinfo(buf, NULL, &hints, &ai))
00167 return -1;
00168 memcpy(sock, ai->ai_addr, FFMIN(sizeof(*sock), ai->ai_addrlen));
00169 freeaddrinfo(ai);
00170 return 0;
00171 }
00172
00173 #if CONFIG_RTPDEC
00174 static void init_rtp_handler(RTPDynamicProtocolHandler *handler,
00175 RTSPStream *rtsp_st, AVCodecContext *codec)
00176 {
00177 if (!handler)
00178 return;
00179 codec->codec_id = handler->codec_id;
00180 rtsp_st->dynamic_handler = handler;
00181 if (handler->alloc) {
00182 rtsp_st->dynamic_protocol_context = handler->alloc();
00183 if (!rtsp_st->dynamic_protocol_context)
00184 rtsp_st->dynamic_handler = NULL;
00185 }
00186 }
00187
00188
00189 static int sdp_parse_rtpmap(AVFormatContext *s,
00190 AVStream *st, RTSPStream *rtsp_st,
00191 int payload_type, const char *p)
00192 {
00193 AVCodecContext *codec = st->codec;
00194 char buf[256];
00195 int i;
00196 AVCodec *c;
00197 const char *c_name;
00198
00199
00200
00201
00202
00203
00204 get_word_sep(buf, sizeof(buf), "/ ", &p);
00205 if (payload_type < RTP_PT_PRIVATE) {
00206
00207
00208
00209 codec->codec_id = ff_rtp_codec_id(buf, codec->codec_type);
00210 }
00211
00212 if (codec->codec_id == AV_CODEC_ID_NONE) {
00213 RTPDynamicProtocolHandler *handler =
00214 ff_rtp_handler_find_by_name(buf, codec->codec_type);
00215 init_rtp_handler(handler, rtsp_st, codec);
00216
00217
00218
00219
00220 if (!rtsp_st->dynamic_handler)
00221 codec->codec_id = ff_rtp_codec_id(buf, codec->codec_type);
00222 }
00223
00224 c = avcodec_find_decoder(codec->codec_id);
00225 if (c && c->name)
00226 c_name = c->name;
00227 else
00228 c_name = "(null)";
00229
00230 get_word_sep(buf, sizeof(buf), "/", &p);
00231 i = atoi(buf);
00232 switch (codec->codec_type) {
00233 case AVMEDIA_TYPE_AUDIO:
00234 av_log(s, AV_LOG_DEBUG, "audio codec set to: %s\n", c_name);
00235 codec->sample_rate = RTSP_DEFAULT_AUDIO_SAMPLERATE;
00236 codec->channels = RTSP_DEFAULT_NB_AUDIO_CHANNELS;
00237 if (i > 0) {
00238 codec->sample_rate = i;
00239 avpriv_set_pts_info(st, 32, 1, codec->sample_rate);
00240 get_word_sep(buf, sizeof(buf), "/", &p);
00241 i = atoi(buf);
00242 if (i > 0)
00243 codec->channels = i;
00244
00245
00246
00247
00248 }
00249 av_log(s, AV_LOG_DEBUG, "audio samplerate set to: %i\n",
00250 codec->sample_rate);
00251 av_log(s, AV_LOG_DEBUG, "audio channels set to: %i\n",
00252 codec->channels);
00253 break;
00254 case AVMEDIA_TYPE_VIDEO:
00255 av_log(s, AV_LOG_DEBUG, "video codec set to: %s\n", c_name);
00256 if (i > 0)
00257 avpriv_set_pts_info(st, 32, 1, i);
00258 break;
00259 default:
00260 break;
00261 }
00262 if (rtsp_st->dynamic_handler && rtsp_st->dynamic_handler->init)
00263 rtsp_st->dynamic_handler->init(s, st->index,
00264 rtsp_st->dynamic_protocol_context);
00265 return 0;
00266 }
00267
00268
00269
00270
00271 int ff_rtsp_next_attr_and_value(const char **p, char *attr, int attr_size,
00272 char *value, int value_size)
00273 {
00274 *p += strspn(*p, SPACE_CHARS);
00275 if (**p) {
00276 get_word_sep(attr, attr_size, "=", p);
00277 if (**p == '=')
00278 (*p)++;
00279 get_word_sep(value, value_size, ";", p);
00280 if (**p == ';')
00281 (*p)++;
00282 return 1;
00283 }
00284 return 0;
00285 }
00286
00287 typedef struct SDPParseState {
00288
00289 struct sockaddr_storage default_ip;
00290 int default_ttl;
00291 int skip_media;
00292 } SDPParseState;
00293
00294 static void sdp_parse_line(AVFormatContext *s, SDPParseState *s1,
00295 int letter, const char *buf)
00296 {
00297 RTSPState *rt = s->priv_data;
00298 char buf1[64], st_type[64];
00299 const char *p;
00300 enum AVMediaType codec_type;
00301 int payload_type, i;
00302 AVStream *st;
00303 RTSPStream *rtsp_st;
00304 struct sockaddr_storage sdp_ip;
00305 int ttl;
00306
00307 av_dlog(s, "sdp: %c='%s'\n", letter, buf);
00308
00309 p = buf;
00310 if (s1->skip_media && letter != 'm')
00311 return;
00312 switch (letter) {
00313 case 'c':
00314 get_word(buf1, sizeof(buf1), &p);
00315 if (strcmp(buf1, "IN") != 0)
00316 return;
00317 get_word(buf1, sizeof(buf1), &p);
00318 if (strcmp(buf1, "IP4") && strcmp(buf1, "IP6"))
00319 return;
00320 get_word_sep(buf1, sizeof(buf1), "/", &p);
00321 if (get_sockaddr(buf1, &sdp_ip))
00322 return;
00323 ttl = 16;
00324 if (*p == '/') {
00325 p++;
00326 get_word_sep(buf1, sizeof(buf1), "/", &p);
00327 ttl = atoi(buf1);
00328 }
00329 if (s->nb_streams == 0) {
00330 s1->default_ip = sdp_ip;
00331 s1->default_ttl = ttl;
00332 } else {
00333 rtsp_st = rt->rtsp_streams[rt->nb_rtsp_streams - 1];
00334 rtsp_st->sdp_ip = sdp_ip;
00335 rtsp_st->sdp_ttl = ttl;
00336 }
00337 break;
00338 case 's':
00339 av_dict_set(&s->metadata, "title", p, 0);
00340 break;
00341 case 'i':
00342 if (s->nb_streams == 0) {
00343 av_dict_set(&s->metadata, "comment", p, 0);
00344 break;
00345 }
00346 break;
00347 case 'm':
00348
00349 s1->skip_media = 0;
00350 codec_type = AVMEDIA_TYPE_UNKNOWN;
00351 get_word(st_type, sizeof(st_type), &p);
00352 if (!strcmp(st_type, "audio")) {
00353 codec_type = AVMEDIA_TYPE_AUDIO;
00354 } else if (!strcmp(st_type, "video")) {
00355 codec_type = AVMEDIA_TYPE_VIDEO;
00356 } else if (!strcmp(st_type, "application")) {
00357 codec_type = AVMEDIA_TYPE_DATA;
00358 }
00359 if (codec_type == AVMEDIA_TYPE_UNKNOWN || !(rt->media_type_mask & (1 << codec_type))) {
00360 s1->skip_media = 1;
00361 return;
00362 }
00363 rtsp_st = av_mallocz(sizeof(RTSPStream));
00364 if (!rtsp_st)
00365 return;
00366 rtsp_st->stream_index = -1;
00367 dynarray_add(&rt->rtsp_streams, &rt->nb_rtsp_streams, rtsp_st);
00368
00369 rtsp_st->sdp_ip = s1->default_ip;
00370 rtsp_st->sdp_ttl = s1->default_ttl;
00371
00372 get_word(buf1, sizeof(buf1), &p);
00373 rtsp_st->sdp_port = atoi(buf1);
00374
00375 get_word(buf1, sizeof(buf1), &p);
00376 if (!strcmp(buf1, "udp"))
00377 rt->transport = RTSP_TRANSPORT_RAW;
00378
00379
00380 get_word(buf1, sizeof(buf1), &p);
00381 rtsp_st->sdp_payload_type = atoi(buf1);
00382
00383 if (!strcmp(ff_rtp_enc_name(rtsp_st->sdp_payload_type), "MP2T")) {
00384
00385 if (rt->transport == RTSP_TRANSPORT_RAW && !rt->ts && CONFIG_RTPDEC)
00386 rt->ts = ff_mpegts_parse_open(s);
00387 } else if (rt->server_type == RTSP_SERVER_WMS &&
00388 codec_type == AVMEDIA_TYPE_DATA) {
00389
00390
00391 } else {
00392 st = avformat_new_stream(s, NULL);
00393 if (!st)
00394 return;
00395 st->id = rt->nb_rtsp_streams - 1;
00396 rtsp_st->stream_index = st->index;
00397 st->codec->codec_type = codec_type;
00398 if (rtsp_st->sdp_payload_type < RTP_PT_PRIVATE) {
00399 RTPDynamicProtocolHandler *handler;
00400
00401 ff_rtp_get_codec_info(st->codec, rtsp_st->sdp_payload_type);
00402 if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO &&
00403 st->codec->sample_rate > 0)
00404 avpriv_set_pts_info(st, 32, 1, st->codec->sample_rate);
00405
00406 handler = ff_rtp_handler_find_by_id(
00407 rtsp_st->sdp_payload_type, st->codec->codec_type);
00408 init_rtp_handler(handler, rtsp_st, st->codec);
00409 if (handler && handler->init)
00410 handler->init(s, st->index,
00411 rtsp_st->dynamic_protocol_context);
00412 }
00413 }
00414
00415 av_strlcpy(rtsp_st->control_url, rt->control_uri,
00416 sizeof(rtsp_st->control_url));
00417 break;
00418 case 'a':
00419 if (av_strstart(p, "control:", &p)) {
00420 if (s->nb_streams == 0) {
00421 if (!strncmp(p, "rtsp://", 7))
00422 av_strlcpy(rt->control_uri, p,
00423 sizeof(rt->control_uri));
00424 } else {
00425 char proto[32];
00426
00427 rtsp_st = rt->rtsp_streams[rt->nb_rtsp_streams - 1];
00428
00429
00430 av_url_split(proto, sizeof(proto), NULL, 0, NULL, 0,
00431 NULL, NULL, 0, p);
00432 if (proto[0] == '\0') {
00433
00434 if (rtsp_st->control_url[strlen(rtsp_st->control_url)-1]!='/')
00435 av_strlcat(rtsp_st->control_url, "/",
00436 sizeof(rtsp_st->control_url));
00437 av_strlcat(rtsp_st->control_url, p,
00438 sizeof(rtsp_st->control_url));
00439 } else
00440 av_strlcpy(rtsp_st->control_url, p,
00441 sizeof(rtsp_st->control_url));
00442 }
00443 } else if (av_strstart(p, "rtpmap:", &p) && s->nb_streams > 0) {
00444
00445 get_word(buf1, sizeof(buf1), &p);
00446 payload_type = atoi(buf1);
00447 rtsp_st = rt->rtsp_streams[rt->nb_rtsp_streams - 1];
00448 if (rtsp_st->stream_index >= 0) {
00449 st = s->streams[rtsp_st->stream_index];
00450 sdp_parse_rtpmap(s, st, rtsp_st, payload_type, p);
00451 }
00452 } else if (av_strstart(p, "fmtp:", &p) ||
00453 av_strstart(p, "framesize:", &p)) {
00454
00455
00456 get_word(buf1, sizeof(buf1), &p);
00457 payload_type = atoi(buf1);
00458 for (i = 0; i < rt->nb_rtsp_streams; i++) {
00459 rtsp_st = rt->rtsp_streams[i];
00460 if (rtsp_st->sdp_payload_type == payload_type &&
00461 rtsp_st->dynamic_handler &&
00462 rtsp_st->dynamic_handler->parse_sdp_a_line)
00463 rtsp_st->dynamic_handler->parse_sdp_a_line(s, i,
00464 rtsp_st->dynamic_protocol_context, buf);
00465 }
00466 } else if (av_strstart(p, "range:", &p)) {
00467 int64_t start, end;
00468
00469
00470 rtsp_parse_range_npt(p, &start, &end);
00471 s->start_time = start;
00472
00473 s->duration = (end == AV_NOPTS_VALUE) ?
00474 AV_NOPTS_VALUE : end - start;
00475 } else if (av_strstart(p, "IsRealDataType:integer;",&p)) {
00476 if (atoi(p) == 1)
00477 rt->transport = RTSP_TRANSPORT_RDT;
00478 } else if (av_strstart(p, "SampleRate:integer;", &p) &&
00479 s->nb_streams > 0) {
00480 st = s->streams[s->nb_streams - 1];
00481 st->codec->sample_rate = atoi(p);
00482 } else {
00483 if (rt->server_type == RTSP_SERVER_WMS)
00484 ff_wms_parse_sdp_a_line(s, p);
00485 if (s->nb_streams > 0) {
00486 rtsp_st = rt->rtsp_streams[rt->nb_rtsp_streams - 1];
00487
00488 if (rt->server_type == RTSP_SERVER_REAL)
00489 ff_real_parse_sdp_a_line(s, rtsp_st->stream_index, p);
00490
00491 if (rtsp_st->dynamic_handler &&
00492 rtsp_st->dynamic_handler->parse_sdp_a_line)
00493 rtsp_st->dynamic_handler->parse_sdp_a_line(s,
00494 rtsp_st->stream_index,
00495 rtsp_st->dynamic_protocol_context, buf);
00496 }
00497 }
00498 break;
00499 }
00500 }
00501
00502 int ff_sdp_parse(AVFormatContext *s, const char *content)
00503 {
00504 RTSPState *rt = s->priv_data;
00505 const char *p;
00506 int letter;
00507
00508
00509
00510
00511
00512
00513
00514
00515 char buf[16384], *q;
00516 SDPParseState sdp_parse_state = { { 0 } }, *s1 = &sdp_parse_state;
00517
00518 p = content;
00519 for (;;) {
00520 p += strspn(p, SPACE_CHARS);
00521 letter = *p;
00522 if (letter == '\0')
00523 break;
00524 p++;
00525 if (*p != '=')
00526 goto next_line;
00527 p++;
00528
00529 q = buf;
00530 while (*p != '\n' && *p != '\r' && *p != '\0') {
00531 if ((q - buf) < sizeof(buf) - 1)
00532 *q++ = *p;
00533 p++;
00534 }
00535 *q = '\0';
00536 sdp_parse_line(s, s1, letter, buf);
00537 next_line:
00538 while (*p != '\n' && *p != '\0')
00539 p++;
00540 if (*p == '\n')
00541 p++;
00542 }
00543 rt->p = av_malloc(sizeof(struct pollfd)*2*(rt->nb_rtsp_streams+1));
00544 if (!rt->p) return AVERROR(ENOMEM);
00545 return 0;
00546 }
00547 #endif
00548
00549 void ff_rtsp_undo_setup(AVFormatContext *s)
00550 {
00551 RTSPState *rt = s->priv_data;
00552 int i;
00553
00554 for (i = 0; i < rt->nb_rtsp_streams; i++) {
00555 RTSPStream *rtsp_st = rt->rtsp_streams[i];
00556 if (!rtsp_st)
00557 continue;
00558 if (rtsp_st->transport_priv) {
00559 if (s->oformat) {
00560 AVFormatContext *rtpctx = rtsp_st->transport_priv;
00561 av_write_trailer(rtpctx);
00562 if (rt->lower_transport == RTSP_LOWER_TRANSPORT_TCP) {
00563 uint8_t *ptr;
00564 avio_close_dyn_buf(rtpctx->pb, &ptr);
00565 av_free(ptr);
00566 } else {
00567 avio_close(rtpctx->pb);
00568 }
00569 avformat_free_context(rtpctx);
00570 } else if (rt->transport == RTSP_TRANSPORT_RDT && CONFIG_RTPDEC)
00571 ff_rdt_parse_close(rtsp_st->transport_priv);
00572 else if (rt->transport == RTSP_TRANSPORT_RTP && CONFIG_RTPDEC)
00573 ff_rtp_parse_close(rtsp_st->transport_priv);
00574 }
00575 rtsp_st->transport_priv = NULL;
00576 if (rtsp_st->rtp_handle)
00577 ffurl_close(rtsp_st->rtp_handle);
00578 rtsp_st->rtp_handle = NULL;
00579 }
00580 }
00581
00582
00583 void ff_rtsp_close_streams(AVFormatContext *s)
00584 {
00585 RTSPState *rt = s->priv_data;
00586 int i;
00587 RTSPStream *rtsp_st;
00588
00589 ff_rtsp_undo_setup(s);
00590 for (i = 0; i < rt->nb_rtsp_streams; i++) {
00591 rtsp_st = rt->rtsp_streams[i];
00592 if (rtsp_st) {
00593 if (rtsp_st->dynamic_handler && rtsp_st->dynamic_protocol_context)
00594 rtsp_st->dynamic_handler->free(
00595 rtsp_st->dynamic_protocol_context);
00596 av_free(rtsp_st);
00597 }
00598 }
00599 av_free(rt->rtsp_streams);
00600 if (rt->asf_ctx) {
00601 avformat_close_input(&rt->asf_ctx);
00602 }
00603 if (rt->ts && CONFIG_RTPDEC)
00604 ff_mpegts_parse_close(rt->ts);
00605 av_free(rt->p);
00606 av_free(rt->recvbuf);
00607 }
00608
00609 int ff_rtsp_open_transport_ctx(AVFormatContext *s, RTSPStream *rtsp_st)
00610 {
00611 RTSPState *rt = s->priv_data;
00612 AVStream *st = NULL;
00613
00614
00615 if (rtsp_st->stream_index >= 0)
00616 st = s->streams[rtsp_st->stream_index];
00617 if (!st)
00618 s->ctx_flags |= AVFMTCTX_NOHEADER;
00619
00620 if (s->oformat && CONFIG_RTSP_MUXER) {
00621 int ret = ff_rtp_chain_mux_open((AVFormatContext **)&rtsp_st->transport_priv, s, st,
00622 rtsp_st->rtp_handle,
00623 RTSP_TCP_MAX_PACKET_SIZE);
00624
00625 rtsp_st->rtp_handle = NULL;
00626 if (ret < 0)
00627 return ret;
00628 } else if (rt->transport == RTSP_TRANSPORT_RAW) {
00629 return 0;
00630 } else if (rt->transport == RTSP_TRANSPORT_RDT && CONFIG_RTPDEC)
00631 rtsp_st->transport_priv = ff_rdt_parse_open(s, st->index,
00632 rtsp_st->dynamic_protocol_context,
00633 rtsp_st->dynamic_handler);
00634 else if (CONFIG_RTPDEC)
00635 rtsp_st->transport_priv = ff_rtp_parse_open(s, st, rtsp_st->rtp_handle,
00636 rtsp_st->sdp_payload_type,
00637 (rt->lower_transport == RTSP_LOWER_TRANSPORT_TCP || !s->max_delay)
00638 ? 0 : RTP_REORDER_QUEUE_DEFAULT_SIZE);
00639
00640 if (!rtsp_st->transport_priv) {
00641 return AVERROR(ENOMEM);
00642 } else if (rt->transport == RTSP_TRANSPORT_RTP && CONFIG_RTPDEC) {
00643 if (rtsp_st->dynamic_handler) {
00644 ff_rtp_parse_set_dynamic_protocol(rtsp_st->transport_priv,
00645 rtsp_st->dynamic_protocol_context,
00646 rtsp_st->dynamic_handler);
00647 }
00648 }
00649
00650 return 0;
00651 }
00652
00653 #if CONFIG_RTSP_DEMUXER || CONFIG_RTSP_MUXER
00654 static void rtsp_parse_range(int *min_ptr, int *max_ptr, const char **pp)
00655 {
00656 const char *q;
00657 char *p;
00658 int v;
00659
00660 q = *pp;
00661 q += strspn(q, SPACE_CHARS);
00662 v = strtol(q, &p, 10);
00663 if (*p == '-') {
00664 p++;
00665 *min_ptr = v;
00666 v = strtol(p, &p, 10);
00667 *max_ptr = v;
00668 } else {
00669 *min_ptr = v;
00670 *max_ptr = v;
00671 }
00672 *pp = p;
00673 }
00674
00675
00676 static void rtsp_parse_transport(RTSPMessageHeader *reply, const char *p)
00677 {
00678 char transport_protocol[16];
00679 char profile[16];
00680 char lower_transport[16];
00681 char parameter[16];
00682 RTSPTransportField *th;
00683 char buf[256];
00684
00685 reply->nb_transports = 0;
00686
00687 for (;;) {
00688 p += strspn(p, SPACE_CHARS);
00689 if (*p == '\0')
00690 break;
00691
00692 th = &reply->transports[reply->nb_transports];
00693
00694 get_word_sep(transport_protocol, sizeof(transport_protocol),
00695 "/", &p);
00696 if (!av_strcasecmp (transport_protocol, "rtp")) {
00697 get_word_sep(profile, sizeof(profile), "/;,", &p);
00698 lower_transport[0] = '\0';
00699
00700 if (*p == '/') {
00701 get_word_sep(lower_transport, sizeof(lower_transport),
00702 ";,", &p);
00703 }
00704 th->transport = RTSP_TRANSPORT_RTP;
00705 } else if (!av_strcasecmp (transport_protocol, "x-pn-tng") ||
00706 !av_strcasecmp (transport_protocol, "x-real-rdt")) {
00707
00708 get_word_sep(lower_transport, sizeof(lower_transport), "/;,", &p);
00709 profile[0] = '\0';
00710 th->transport = RTSP_TRANSPORT_RDT;
00711 } else if (!av_strcasecmp(transport_protocol, "raw")) {
00712 get_word_sep(profile, sizeof(profile), "/;,", &p);
00713 lower_transport[0] = '\0';
00714
00715 if (*p == '/') {
00716 get_word_sep(lower_transport, sizeof(lower_transport),
00717 ";,", &p);
00718 }
00719 th->transport = RTSP_TRANSPORT_RAW;
00720 }
00721 if (!av_strcasecmp(lower_transport, "TCP"))
00722 th->lower_transport = RTSP_LOWER_TRANSPORT_TCP;
00723 else
00724 th->lower_transport = RTSP_LOWER_TRANSPORT_UDP;
00725
00726 if (*p == ';')
00727 p++;
00728
00729 while (*p != '\0' && *p != ',') {
00730 get_word_sep(parameter, sizeof(parameter), "=;,", &p);
00731 if (!strcmp(parameter, "port")) {
00732 if (*p == '=') {
00733 p++;
00734 rtsp_parse_range(&th->port_min, &th->port_max, &p);
00735 }
00736 } else if (!strcmp(parameter, "client_port")) {
00737 if (*p == '=') {
00738 p++;
00739 rtsp_parse_range(&th->client_port_min,
00740 &th->client_port_max, &p);
00741 }
00742 } else if (!strcmp(parameter, "server_port")) {
00743 if (*p == '=') {
00744 p++;
00745 rtsp_parse_range(&th->server_port_min,
00746 &th->server_port_max, &p);
00747 }
00748 } else if (!strcmp(parameter, "interleaved")) {
00749 if (*p == '=') {
00750 p++;
00751 rtsp_parse_range(&th->interleaved_min,
00752 &th->interleaved_max, &p);
00753 }
00754 } else if (!strcmp(parameter, "multicast")) {
00755 if (th->lower_transport == RTSP_LOWER_TRANSPORT_UDP)
00756 th->lower_transport = RTSP_LOWER_TRANSPORT_UDP_MULTICAST;
00757 } else if (!strcmp(parameter, "ttl")) {
00758 if (*p == '=') {
00759 p++;
00760 th->ttl = strtol(p, (char **)&p, 10);
00761 }
00762 } else if (!strcmp(parameter, "destination")) {
00763 if (*p == '=') {
00764 p++;
00765 get_word_sep(buf, sizeof(buf), ";,", &p);
00766 get_sockaddr(buf, &th->destination);
00767 }
00768 } else if (!strcmp(parameter, "source")) {
00769 if (*p == '=') {
00770 p++;
00771 get_word_sep(buf, sizeof(buf), ";,", &p);
00772 av_strlcpy(th->source, buf, sizeof(th->source));
00773 }
00774 } else if (!strcmp(parameter, "mode")) {
00775 if (*p == '=') {
00776 p++;
00777 get_word_sep(buf, sizeof(buf), ";, ", &p);
00778 if (!strcmp(buf, "record") ||
00779 !strcmp(buf, "receive"))
00780 th->mode_record = 1;
00781 }
00782 }
00783
00784 while (*p != ';' && *p != '\0' && *p != ',')
00785 p++;
00786 if (*p == ';')
00787 p++;
00788 }
00789 if (*p == ',')
00790 p++;
00791
00792 reply->nb_transports++;
00793 }
00794 }
00795
00796 static void handle_rtp_info(RTSPState *rt, const char *url,
00797 uint32_t seq, uint32_t rtptime)
00798 {
00799 int i;
00800 if (!rtptime || !url[0])
00801 return;
00802 if (rt->transport != RTSP_TRANSPORT_RTP)
00803 return;
00804 for (i = 0; i < rt->nb_rtsp_streams; i++) {
00805 RTSPStream *rtsp_st = rt->rtsp_streams[i];
00806 RTPDemuxContext *rtpctx = rtsp_st->transport_priv;
00807 if (!rtpctx)
00808 continue;
00809 if (!strcmp(rtsp_st->control_url, url)) {
00810 rtpctx->base_timestamp = rtptime;
00811 break;
00812 }
00813 }
00814 }
00815
00816 static void rtsp_parse_rtp_info(RTSPState *rt, const char *p)
00817 {
00818 int read = 0;
00819 char key[20], value[1024], url[1024] = "";
00820 uint32_t seq = 0, rtptime = 0;
00821
00822 for (;;) {
00823 p += strspn(p, SPACE_CHARS);
00824 if (!*p)
00825 break;
00826 get_word_sep(key, sizeof(key), "=", &p);
00827 if (*p != '=')
00828 break;
00829 p++;
00830 get_word_sep(value, sizeof(value), ";, ", &p);
00831 read++;
00832 if (!strcmp(key, "url"))
00833 av_strlcpy(url, value, sizeof(url));
00834 else if (!strcmp(key, "seq"))
00835 seq = strtoul(value, NULL, 10);
00836 else if (!strcmp(key, "rtptime"))
00837 rtptime = strtoul(value, NULL, 10);
00838 if (*p == ',') {
00839 handle_rtp_info(rt, url, seq, rtptime);
00840 url[0] = '\0';
00841 seq = rtptime = 0;
00842 read = 0;
00843 }
00844 if (*p)
00845 p++;
00846 }
00847 if (read > 0)
00848 handle_rtp_info(rt, url, seq, rtptime);
00849 }
00850
00851 void ff_rtsp_parse_line(RTSPMessageHeader *reply, const char *buf,
00852 RTSPState *rt, const char *method)
00853 {
00854 const char *p;
00855
00856
00857 p = buf;
00858 if (av_stristart(p, "Session:", &p)) {
00859 int t;
00860 get_word_sep(reply->session_id, sizeof(reply->session_id), ";", &p);
00861 if (av_stristart(p, ";timeout=", &p) &&
00862 (t = strtol(p, NULL, 10)) > 0) {
00863 reply->timeout = t;
00864 }
00865 } else if (av_stristart(p, "Content-Length:", &p)) {
00866 reply->content_length = strtol(p, NULL, 10);
00867 } else if (av_stristart(p, "Transport:", &p)) {
00868 rtsp_parse_transport(reply, p);
00869 } else if (av_stristart(p, "CSeq:", &p)) {
00870 reply->seq = strtol(p, NULL, 10);
00871 } else if (av_stristart(p, "Range:", &p)) {
00872 rtsp_parse_range_npt(p, &reply->range_start, &reply->range_end);
00873 } else if (av_stristart(p, "RealChallenge1:", &p)) {
00874 p += strspn(p, SPACE_CHARS);
00875 av_strlcpy(reply->real_challenge, p, sizeof(reply->real_challenge));
00876 } else if (av_stristart(p, "Server:", &p)) {
00877 p += strspn(p, SPACE_CHARS);
00878 av_strlcpy(reply->server, p, sizeof(reply->server));
00879 } else if (av_stristart(p, "Notice:", &p) ||
00880 av_stristart(p, "X-Notice:", &p)) {
00881 reply->notice = strtol(p, NULL, 10);
00882 } else if (av_stristart(p, "Location:", &p)) {
00883 p += strspn(p, SPACE_CHARS);
00884 av_strlcpy(reply->location, p , sizeof(reply->location));
00885 } else if (av_stristart(p, "WWW-Authenticate:", &p) && rt) {
00886 p += strspn(p, SPACE_CHARS);
00887 ff_http_auth_handle_header(&rt->auth_state, "WWW-Authenticate", p);
00888 } else if (av_stristart(p, "Authentication-Info:", &p) && rt) {
00889 p += strspn(p, SPACE_CHARS);
00890 ff_http_auth_handle_header(&rt->auth_state, "Authentication-Info", p);
00891 } else if (av_stristart(p, "Content-Base:", &p) && rt) {
00892 p += strspn(p, SPACE_CHARS);
00893 if (method && !strcmp(method, "DESCRIBE"))
00894 av_strlcpy(rt->control_uri, p , sizeof(rt->control_uri));
00895 } else if (av_stristart(p, "RTP-Info:", &p) && rt) {
00896 p += strspn(p, SPACE_CHARS);
00897 if (method && !strcmp(method, "PLAY"))
00898 rtsp_parse_rtp_info(rt, p);
00899 } else if (av_stristart(p, "Public:", &p) && rt) {
00900 if (strstr(p, "GET_PARAMETER") &&
00901 method && !strcmp(method, "OPTIONS"))
00902 rt->get_parameter_supported = 1;
00903 } else if (av_stristart(p, "x-Accept-Dynamic-Rate:", &p) && rt) {
00904 p += strspn(p, SPACE_CHARS);
00905 rt->accept_dynamic_rate = atoi(p);
00906 } else if (av_stristart(p, "Content-Type:", &p)) {
00907 p += strspn(p, SPACE_CHARS);
00908 av_strlcpy(reply->content_type, p, sizeof(reply->content_type));
00909 }
00910 }
00911
00912
00913 void ff_rtsp_skip_packet(AVFormatContext *s)
00914 {
00915 RTSPState *rt = s->priv_data;
00916 int ret, len, len1;
00917 uint8_t buf[1024];
00918
00919 ret = ffurl_read_complete(rt->rtsp_hd, buf, 3);
00920 if (ret != 3)
00921 return;
00922 len = AV_RB16(buf + 1);
00923
00924 av_dlog(s, "skipping RTP packet len=%d\n", len);
00925
00926
00927 while (len > 0) {
00928 len1 = len;
00929 if (len1 > sizeof(buf))
00930 len1 = sizeof(buf);
00931 ret = ffurl_read_complete(rt->rtsp_hd, buf, len1);
00932 if (ret != len1)
00933 return;
00934 len -= len1;
00935 }
00936 }
00937
00938 int ff_rtsp_read_reply(AVFormatContext *s, RTSPMessageHeader *reply,
00939 unsigned char **content_ptr,
00940 int return_on_interleaved_data, const char *method)
00941 {
00942 RTSPState *rt = s->priv_data;
00943 char buf[4096], buf1[1024], *q;
00944 unsigned char ch;
00945 const char *p;
00946 int ret, content_length, line_count = 0, request = 0;
00947 unsigned char *content = NULL;
00948
00949 start:
00950 line_count = 0;
00951 request = 0;
00952 content = NULL;
00953 memset(reply, 0, sizeof(*reply));
00954
00955
00956 rt->last_reply[0] = '\0';
00957 for (;;) {
00958 q = buf;
00959 for (;;) {
00960 ret = ffurl_read_complete(rt->rtsp_hd, &ch, 1);
00961 av_dlog(s, "ret=%d c=%02x [%c]\n", ret, ch, ch);
00962 if (ret != 1)
00963 return AVERROR_EOF;
00964 if (ch == '\n')
00965 break;
00966 if (ch == '$') {
00967
00968 if (return_on_interleaved_data) {
00969 return 1;
00970 } else
00971 ff_rtsp_skip_packet(s);
00972 } else if (ch != '\r') {
00973 if ((q - buf) < sizeof(buf) - 1)
00974 *q++ = ch;
00975 }
00976 }
00977 *q = '\0';
00978
00979 av_dlog(s, "line='%s'\n", buf);
00980
00981
00982 if (buf[0] == '\0')
00983 break;
00984 p = buf;
00985 if (line_count == 0) {
00986
00987 get_word(buf1, sizeof(buf1), &p);
00988 if (!strncmp(buf1, "RTSP/", 5)) {
00989 get_word(buf1, sizeof(buf1), &p);
00990 reply->status_code = atoi(buf1);
00991 av_strlcpy(reply->reason, p, sizeof(reply->reason));
00992 } else {
00993 av_strlcpy(reply->reason, buf1, sizeof(reply->reason));
00994 get_word(buf1, sizeof(buf1), &p);
00995 request = 1;
00996 }
00997 } else {
00998 ff_rtsp_parse_line(reply, p, rt, method);
00999 av_strlcat(rt->last_reply, p, sizeof(rt->last_reply));
01000 av_strlcat(rt->last_reply, "\n", sizeof(rt->last_reply));
01001 }
01002 line_count++;
01003 }
01004
01005 if (rt->session_id[0] == '\0' && reply->session_id[0] != '\0' && !request)
01006 av_strlcpy(rt->session_id, reply->session_id, sizeof(rt->session_id));
01007
01008 content_length = reply->content_length;
01009 if (content_length > 0) {
01010
01011 content = av_malloc(content_length + 1);
01012 ffurl_read_complete(rt->rtsp_hd, content, content_length);
01013 content[content_length] = '\0';
01014 }
01015 if (content_ptr)
01016 *content_ptr = content;
01017 else
01018 av_free(content);
01019
01020 if (request) {
01021 char buf[1024];
01022 char base64buf[AV_BASE64_SIZE(sizeof(buf))];
01023 const char* ptr = buf;
01024
01025 if (!strcmp(reply->reason, "OPTIONS")) {
01026 snprintf(buf, sizeof(buf), "RTSP/1.0 200 OK\r\n");
01027 if (reply->seq)
01028 av_strlcatf(buf, sizeof(buf), "CSeq: %d\r\n", reply->seq);
01029 if (reply->session_id[0])
01030 av_strlcatf(buf, sizeof(buf), "Session: %s\r\n",
01031 reply->session_id);
01032 } else {
01033 snprintf(buf, sizeof(buf), "RTSP/1.0 501 Not Implemented\r\n");
01034 }
01035 av_strlcat(buf, "\r\n", sizeof(buf));
01036
01037 if (rt->control_transport == RTSP_MODE_TUNNEL) {
01038 av_base64_encode(base64buf, sizeof(base64buf), buf, strlen(buf));
01039 ptr = base64buf;
01040 }
01041 ffurl_write(rt->rtsp_hd_out, ptr, strlen(ptr));
01042
01043 rt->last_cmd_time = av_gettime();
01044
01045
01046
01047 if (content_ptr)
01048 av_freep(content_ptr);
01049
01050
01051
01052
01053 if (method)
01054 goto start;
01055 return 0;
01056 }
01057
01058 if (rt->seq != reply->seq) {
01059 av_log(s, AV_LOG_WARNING, "CSeq %d expected, %d received.\n",
01060 rt->seq, reply->seq);
01061 }
01062
01063
01064 if (reply->notice == 2101 ||
01065 reply->notice == 2104 ||
01066 reply->notice == 2306 ) {
01067 rt->state = RTSP_STATE_IDLE;
01068 } else if (reply->notice >= 4400 && reply->notice < 5500) {
01069 return AVERROR(EIO);
01070 } else if (reply->notice == 2401 ||
01071 (reply->notice >= 5500 && reply->notice < 5600) )
01072 return AVERROR(EPERM);
01073
01074 return 0;
01075 }
01076
01090 static int ff_rtsp_send_cmd_with_content_async(AVFormatContext *s,
01091 const char *method, const char *url,
01092 const char *headers,
01093 const unsigned char *send_content,
01094 int send_content_length)
01095 {
01096 RTSPState *rt = s->priv_data;
01097 char buf[4096], *out_buf;
01098 char base64buf[AV_BASE64_SIZE(sizeof(buf))];
01099
01100
01101 out_buf = buf;
01102 rt->seq++;
01103 snprintf(buf, sizeof(buf), "%s %s RTSP/1.0\r\n", method, url);
01104 if (headers)
01105 av_strlcat(buf, headers, sizeof(buf));
01106 av_strlcatf(buf, sizeof(buf), "CSeq: %d\r\n", rt->seq);
01107 if (rt->session_id[0] != '\0' && (!headers ||
01108 !strstr(headers, "\nIf-Match:"))) {
01109 av_strlcatf(buf, sizeof(buf), "Session: %s\r\n", rt->session_id);
01110 }
01111 if (rt->auth[0]) {
01112 char *str = ff_http_auth_create_response(&rt->auth_state,
01113 rt->auth, url, method);
01114 if (str)
01115 av_strlcat(buf, str, sizeof(buf));
01116 av_free(str);
01117 }
01118 if (send_content_length > 0 && send_content)
01119 av_strlcatf(buf, sizeof(buf), "Content-Length: %d\r\n", send_content_length);
01120 av_strlcat(buf, "\r\n", sizeof(buf));
01121
01122
01123 if (rt->control_transport == RTSP_MODE_TUNNEL) {
01124 av_base64_encode(base64buf, sizeof(base64buf), buf, strlen(buf));
01125 out_buf = base64buf;
01126 }
01127
01128 av_dlog(s, "Sending:\n%s--\n", buf);
01129
01130 ffurl_write(rt->rtsp_hd_out, out_buf, strlen(out_buf));
01131 if (send_content_length > 0 && send_content) {
01132 if (rt->control_transport == RTSP_MODE_TUNNEL) {
01133 av_log(s, AV_LOG_ERROR, "tunneling of RTSP requests "
01134 "with content data not supported\n");
01135 return AVERROR_PATCHWELCOME;
01136 }
01137 ffurl_write(rt->rtsp_hd_out, send_content, send_content_length);
01138 }
01139 rt->last_cmd_time = av_gettime();
01140
01141 return 0;
01142 }
01143
01144 int ff_rtsp_send_cmd_async(AVFormatContext *s, const char *method,
01145 const char *url, const char *headers)
01146 {
01147 return ff_rtsp_send_cmd_with_content_async(s, method, url, headers, NULL, 0);
01148 }
01149
01150 int ff_rtsp_send_cmd(AVFormatContext *s, const char *method, const char *url,
01151 const char *headers, RTSPMessageHeader *reply,
01152 unsigned char **content_ptr)
01153 {
01154 return ff_rtsp_send_cmd_with_content(s, method, url, headers, reply,
01155 content_ptr, NULL, 0);
01156 }
01157
01158 int ff_rtsp_send_cmd_with_content(AVFormatContext *s,
01159 const char *method, const char *url,
01160 const char *header,
01161 RTSPMessageHeader *reply,
01162 unsigned char **content_ptr,
01163 const unsigned char *send_content,
01164 int send_content_length)
01165 {
01166 RTSPState *rt = s->priv_data;
01167 HTTPAuthType cur_auth_type;
01168 int ret, attempts = 0;
01169
01170 retry:
01171 cur_auth_type = rt->auth_state.auth_type;
01172 if ((ret = ff_rtsp_send_cmd_with_content_async(s, method, url, header,
01173 send_content,
01174 send_content_length)))
01175 return ret;
01176
01177 if ((ret = ff_rtsp_read_reply(s, reply, content_ptr, 0, method) ) < 0)
01178 return ret;
01179 attempts++;
01180
01181 if (reply->status_code == 401 &&
01182 (cur_auth_type == HTTP_AUTH_NONE || rt->auth_state.stale) &&
01183 rt->auth_state.auth_type != HTTP_AUTH_NONE && attempts < 2)
01184 goto retry;
01185
01186 if (reply->status_code > 400){
01187 av_log(s, AV_LOG_ERROR, "method %s failed: %d%s\n",
01188 method,
01189 reply->status_code,
01190 reply->reason);
01191 av_log(s, AV_LOG_DEBUG, "%s\n", rt->last_reply);
01192 }
01193
01194 return 0;
01195 }
01196
01197 int ff_rtsp_make_setup_request(AVFormatContext *s, const char *host, int port,
01198 int lower_transport, const char *real_challenge)
01199 {
01200 RTSPState *rt = s->priv_data;
01201 int rtx = 0, j, i, err, interleave = 0, port_off;
01202 RTSPStream *rtsp_st;
01203 RTSPMessageHeader reply1, *reply = &reply1;
01204 char cmd[2048];
01205 const char *trans_pref;
01206
01207 if (rt->transport == RTSP_TRANSPORT_RDT)
01208 trans_pref = "x-pn-tng";
01209 else if (rt->transport == RTSP_TRANSPORT_RAW)
01210 trans_pref = "RAW/RAW";
01211 else
01212 trans_pref = "RTP/AVP";
01213
01214
01215 rt->timeout = 60;
01216
01217
01218
01219
01220 port_off = av_get_random_seed() % ((rt->rtp_port_max - rt->rtp_port_min)/2);
01221
01222 port_off -= port_off & 0x01;
01223
01224 for (j = rt->rtp_port_min + port_off, i = 0; i < rt->nb_rtsp_streams; ++i) {
01225 char transport[2048];
01226
01227
01228
01229
01230
01231
01232 if (lower_transport == RTSP_LOWER_TRANSPORT_UDP &&
01233 rt->server_type == RTSP_SERVER_WMS) {
01234 if (i == 0) {
01235
01236 for (rtx = 0; rtx < rt->nb_rtsp_streams; rtx++) {
01237 int len = strlen(rt->rtsp_streams[rtx]->control_url);
01238 if (len >= 4 &&
01239 !strcmp(rt->rtsp_streams[rtx]->control_url + len - 4,
01240 "/rtx"))
01241 break;
01242 }
01243 if (rtx == rt->nb_rtsp_streams)
01244 return -1;
01245 rtsp_st = rt->rtsp_streams[rtx];
01246 } else
01247 rtsp_st = rt->rtsp_streams[i > rtx ? i : i - 1];
01248 } else
01249 rtsp_st = rt->rtsp_streams[i];
01250
01251
01252 if (lower_transport == RTSP_LOWER_TRANSPORT_UDP) {
01253 char buf[256];
01254
01255 if (rt->server_type == RTSP_SERVER_WMS && i > 1) {
01256 port = reply->transports[0].client_port_min;
01257 goto have_port;
01258 }
01259
01260
01261 while (j <= rt->rtp_port_max) {
01262 ff_url_join(buf, sizeof(buf), "rtp", NULL, host, -1,
01263 "?localport=%d", j);
01264
01265 j += 2;
01266 if (!ffurl_open(&rtsp_st->rtp_handle, buf, AVIO_FLAG_READ_WRITE,
01267 &s->interrupt_callback, NULL))
01268 goto rtp_opened;
01269 }
01270 av_log(s, AV_LOG_ERROR, "Unable to open an input RTP port\n");
01271 err = AVERROR(EIO);
01272 goto fail;
01273
01274 rtp_opened:
01275 port = ff_rtp_get_local_rtp_port(rtsp_st->rtp_handle);
01276 have_port:
01277 snprintf(transport, sizeof(transport) - 1,
01278 "%s/UDP;", trans_pref);
01279 if (rt->server_type != RTSP_SERVER_REAL)
01280 av_strlcat(transport, "unicast;", sizeof(transport));
01281 av_strlcatf(transport, sizeof(transport),
01282 "client_port=%d", port);
01283 if (rt->transport == RTSP_TRANSPORT_RTP &&
01284 !(rt->server_type == RTSP_SERVER_WMS && i > 0))
01285 av_strlcatf(transport, sizeof(transport), "-%d", port + 1);
01286 }
01287
01288
01289 else if (lower_transport == RTSP_LOWER_TRANSPORT_TCP) {
01290
01291
01292
01293 if (rt->server_type == RTSP_SERVER_WMS &&
01294 (rtsp_st->stream_index < 0 ||
01295 s->streams[rtsp_st->stream_index]->codec->codec_type ==
01296 AVMEDIA_TYPE_DATA))
01297 continue;
01298 snprintf(transport, sizeof(transport) - 1,
01299 "%s/TCP;", trans_pref);
01300 if (rt->transport != RTSP_TRANSPORT_RDT)
01301 av_strlcat(transport, "unicast;", sizeof(transport));
01302 av_strlcatf(transport, sizeof(transport),
01303 "interleaved=%d-%d",
01304 interleave, interleave + 1);
01305 interleave += 2;
01306 }
01307
01308 else if (lower_transport == RTSP_LOWER_TRANSPORT_UDP_MULTICAST) {
01309 snprintf(transport, sizeof(transport) - 1,
01310 "%s/UDP;multicast", trans_pref);
01311 }
01312 if (s->oformat) {
01313 av_strlcat(transport, ";mode=record", sizeof(transport));
01314 } else if (rt->server_type == RTSP_SERVER_REAL ||
01315 rt->server_type == RTSP_SERVER_WMS)
01316 av_strlcat(transport, ";mode=play", sizeof(transport));
01317 snprintf(cmd, sizeof(cmd),
01318 "Transport: %s\r\n",
01319 transport);
01320 if (rt->accept_dynamic_rate)
01321 av_strlcat(cmd, "x-Dynamic-Rate: 0\r\n", sizeof(cmd));
01322 if (i == 0 && rt->server_type == RTSP_SERVER_REAL && CONFIG_RTPDEC) {
01323 char real_res[41], real_csum[9];
01324 ff_rdt_calc_response_and_checksum(real_res, real_csum,
01325 real_challenge);
01326 av_strlcatf(cmd, sizeof(cmd),
01327 "If-Match: %s\r\n"
01328 "RealChallenge2: %s, sd=%s\r\n",
01329 rt->session_id, real_res, real_csum);
01330 }
01331 ff_rtsp_send_cmd(s, "SETUP", rtsp_st->control_url, cmd, reply, NULL);
01332 if (reply->status_code == 461 && i == 0) {
01333 err = 1;
01334 goto fail;
01335 } else if (reply->status_code != RTSP_STATUS_OK ||
01336 reply->nb_transports != 1) {
01337 err = AVERROR_INVALIDDATA;
01338 goto fail;
01339 }
01340
01341
01342 if (i > 0) {
01343 if (reply->transports[0].lower_transport != rt->lower_transport ||
01344 reply->transports[0].transport != rt->transport) {
01345 err = AVERROR_INVALIDDATA;
01346 goto fail;
01347 }
01348 } else {
01349 rt->lower_transport = reply->transports[0].lower_transport;
01350 rt->transport = reply->transports[0].transport;
01351 }
01352
01353
01354
01355 if (reply->transports[0].lower_transport != lower_transport) {
01356 av_log(s, AV_LOG_ERROR, "Nonmatching transport in server reply\n");
01357 err = AVERROR_INVALIDDATA;
01358 goto fail;
01359 }
01360
01361 switch(reply->transports[0].lower_transport) {
01362 case RTSP_LOWER_TRANSPORT_TCP:
01363 rtsp_st->interleaved_min = reply->transports[0].interleaved_min;
01364 rtsp_st->interleaved_max = reply->transports[0].interleaved_max;
01365 break;
01366
01367 case RTSP_LOWER_TRANSPORT_UDP: {
01368 char url[1024], options[30] = "";
01369
01370 if (rt->rtsp_flags & RTSP_FLAG_FILTER_SRC)
01371 av_strlcpy(options, "?connect=1", sizeof(options));
01372
01373 if (reply->transports[0].source[0]) {
01374 ff_url_join(url, sizeof(url), "rtp", NULL,
01375 reply->transports[0].source,
01376 reply->transports[0].server_port_min, "%s", options);
01377 } else {
01378 ff_url_join(url, sizeof(url), "rtp", NULL, host,
01379 reply->transports[0].server_port_min, "%s", options);
01380 }
01381 if (!(rt->server_type == RTSP_SERVER_WMS && i > 1) &&
01382 ff_rtp_set_remote_url(rtsp_st->rtp_handle, url) < 0) {
01383 err = AVERROR_INVALIDDATA;
01384 goto fail;
01385 }
01386
01387
01388
01389
01390 if (!(rt->server_type == RTSP_SERVER_WMS && i > 1) && s->iformat &&
01391 CONFIG_RTPDEC)
01392 ff_rtp_send_punch_packets(rtsp_st->rtp_handle);
01393 break;
01394 }
01395 case RTSP_LOWER_TRANSPORT_UDP_MULTICAST: {
01396 char url[1024], namebuf[50], optbuf[20] = "";
01397 struct sockaddr_storage addr;
01398 int port, ttl;
01399
01400 if (reply->transports[0].destination.ss_family) {
01401 addr = reply->transports[0].destination;
01402 port = reply->transports[0].port_min;
01403 ttl = reply->transports[0].ttl;
01404 } else {
01405 addr = rtsp_st->sdp_ip;
01406 port = rtsp_st->sdp_port;
01407 ttl = rtsp_st->sdp_ttl;
01408 }
01409 if (ttl > 0)
01410 snprintf(optbuf, sizeof(optbuf), "?ttl=%d", ttl);
01411 getnameinfo((struct sockaddr*) &addr, sizeof(addr),
01412 namebuf, sizeof(namebuf), NULL, 0, NI_NUMERICHOST);
01413 ff_url_join(url, sizeof(url), "rtp", NULL, namebuf,
01414 port, "%s", optbuf);
01415 if (ffurl_open(&rtsp_st->rtp_handle, url, AVIO_FLAG_READ_WRITE,
01416 &s->interrupt_callback, NULL) < 0) {
01417 err = AVERROR_INVALIDDATA;
01418 goto fail;
01419 }
01420 break;
01421 }
01422 }
01423
01424 if ((err = ff_rtsp_open_transport_ctx(s, rtsp_st)))
01425 goto fail;
01426 }
01427
01428 if (rt->nb_rtsp_streams && reply->timeout > 0)
01429 rt->timeout = reply->timeout;
01430
01431 if (rt->server_type == RTSP_SERVER_REAL)
01432 rt->need_subscription = 1;
01433
01434 return 0;
01435
01436 fail:
01437 ff_rtsp_undo_setup(s);
01438 return err;
01439 }
01440
01441 void ff_rtsp_close_connections(AVFormatContext *s)
01442 {
01443 RTSPState *rt = s->priv_data;
01444 if (rt->rtsp_hd_out != rt->rtsp_hd) ffurl_close(rt->rtsp_hd_out);
01445 ffurl_close(rt->rtsp_hd);
01446 rt->rtsp_hd = rt->rtsp_hd_out = NULL;
01447 }
01448
01449 int ff_rtsp_connect(AVFormatContext *s)
01450 {
01451 RTSPState *rt = s->priv_data;
01452 char host[1024], path[1024], tcpname[1024], cmd[2048], auth[128];
01453 int port, err, tcp_fd;
01454 RTSPMessageHeader reply1 = {0}, *reply = &reply1;
01455 int lower_transport_mask = 0;
01456 char real_challenge[64] = "";
01457 struct sockaddr_storage peer;
01458 socklen_t peer_len = sizeof(peer);
01459
01460 if (rt->rtp_port_max < rt->rtp_port_min) {
01461 av_log(s, AV_LOG_ERROR, "Invalid UDP port range, max port %d less "
01462 "than min port %d\n", rt->rtp_port_max,
01463 rt->rtp_port_min);
01464 return AVERROR(EINVAL);
01465 }
01466
01467 if (!ff_network_init())
01468 return AVERROR(EIO);
01469
01470 if (s->max_delay < 0)
01471 s->max_delay = s->iformat ? DEFAULT_REORDERING_DELAY : 0;
01472
01473 rt->control_transport = RTSP_MODE_PLAIN;
01474 if (rt->lower_transport_mask & (1 << RTSP_LOWER_TRANSPORT_HTTP)) {
01475 rt->lower_transport_mask = 1 << RTSP_LOWER_TRANSPORT_TCP;
01476 rt->control_transport = RTSP_MODE_TUNNEL;
01477 }
01478
01479 rt->lower_transport_mask &= (1 << RTSP_LOWER_TRANSPORT_NB) - 1;
01480
01481 redirect:
01482 lower_transport_mask = rt->lower_transport_mask;
01483
01484 av_url_split(NULL, 0, auth, sizeof(auth),
01485 host, sizeof(host), &port, path, sizeof(path), s->filename);
01486 if (*auth) {
01487 av_strlcpy(rt->auth, auth, sizeof(rt->auth));
01488 }
01489 if (port < 0)
01490 port = RTSP_DEFAULT_PORT;
01491
01492 if (!lower_transport_mask)
01493 lower_transport_mask = (1 << RTSP_LOWER_TRANSPORT_NB) - 1;
01494
01495 if (s->oformat) {
01496
01497 lower_transport_mask &= (1 << RTSP_LOWER_TRANSPORT_UDP) |
01498 (1 << RTSP_LOWER_TRANSPORT_TCP);
01499 if (!lower_transport_mask || rt->control_transport == RTSP_MODE_TUNNEL) {
01500 av_log(s, AV_LOG_ERROR, "Unsupported lower transport method, "
01501 "only UDP and TCP are supported for output.\n");
01502 err = AVERROR(EINVAL);
01503 goto fail;
01504 }
01505 }
01506
01507
01508
01509
01510 ff_url_join(rt->control_uri, sizeof(rt->control_uri), "rtsp", NULL,
01511 host, port, "%s", path);
01512
01513 if (rt->control_transport == RTSP_MODE_TUNNEL) {
01514
01515 char httpname[1024];
01516 char sessioncookie[17];
01517 char headers[1024];
01518
01519 ff_url_join(httpname, sizeof(httpname), "http", auth, host, port, "%s", path);
01520 snprintf(sessioncookie, sizeof(sessioncookie), "%08x%08x",
01521 av_get_random_seed(), av_get_random_seed());
01522
01523
01524 if (ffurl_alloc(&rt->rtsp_hd, httpname, AVIO_FLAG_READ,
01525 &s->interrupt_callback) < 0) {
01526 err = AVERROR(EIO);
01527 goto fail;
01528 }
01529
01530
01531 snprintf(headers, sizeof(headers),
01532 "x-sessioncookie: %s\r\n"
01533 "Accept: application/x-rtsp-tunnelled\r\n"
01534 "Pragma: no-cache\r\n"
01535 "Cache-Control: no-cache\r\n",
01536 sessioncookie);
01537 av_opt_set(rt->rtsp_hd->priv_data, "headers", headers, 0);
01538
01539
01540 if (ffurl_connect(rt->rtsp_hd, NULL)) {
01541 err = AVERROR(EIO);
01542 goto fail;
01543 }
01544
01545
01546 if (ffurl_alloc(&rt->rtsp_hd_out, httpname, AVIO_FLAG_WRITE,
01547 &s->interrupt_callback) < 0 ) {
01548 err = AVERROR(EIO);
01549 goto fail;
01550 }
01551
01552
01553 snprintf(headers, sizeof(headers),
01554 "x-sessioncookie: %s\r\n"
01555 "Content-Type: application/x-rtsp-tunnelled\r\n"
01556 "Pragma: no-cache\r\n"
01557 "Cache-Control: no-cache\r\n"
01558 "Content-Length: 32767\r\n"
01559 "Expires: Sun, 9 Jan 1972 00:00:00 GMT\r\n",
01560 sessioncookie);
01561 av_opt_set(rt->rtsp_hd_out->priv_data, "headers", headers, 0);
01562 av_opt_set(rt->rtsp_hd_out->priv_data, "chunked_post", "0", 0);
01563
01564
01565
01566
01567
01568
01569
01570
01571
01572
01573
01574
01575
01576
01577
01578
01579
01580 ff_http_init_auth_state(rt->rtsp_hd_out, rt->rtsp_hd);
01581
01582
01583 if (ffurl_connect(rt->rtsp_hd_out, NULL)) {
01584 err = AVERROR(EIO);
01585 goto fail;
01586 }
01587 } else {
01588
01589 ff_url_join(tcpname, sizeof(tcpname), "tcp", NULL, host, port, NULL);
01590 if (ffurl_open(&rt->rtsp_hd, tcpname, AVIO_FLAG_READ_WRITE,
01591 &s->interrupt_callback, NULL) < 0) {
01592 err = AVERROR(EIO);
01593 goto fail;
01594 }
01595 rt->rtsp_hd_out = rt->rtsp_hd;
01596 }
01597 rt->seq = 0;
01598
01599 tcp_fd = ffurl_get_file_handle(rt->rtsp_hd);
01600 if (!getpeername(tcp_fd, (struct sockaddr*) &peer, &peer_len)) {
01601 getnameinfo((struct sockaddr*) &peer, peer_len, host, sizeof(host),
01602 NULL, 0, NI_NUMERICHOST);
01603 }
01604
01605
01606
01607 for (rt->server_type = RTSP_SERVER_RTP;;) {
01608 cmd[0] = 0;
01609 if (rt->server_type == RTSP_SERVER_REAL)
01610 av_strlcat(cmd,
01611
01612
01613
01614
01615
01616
01617
01618
01619
01620 "ClientChallenge: 9e26d33f2984236010ef6253fb1887f7\r\n"
01621 "PlayerStarttime: [28/03/2003:22:50:23 00:00]\r\n"
01622 "CompanyID: KnKV4M4I/B2FjJ1TToLycw==\r\n"
01623 "GUID: 00000000-0000-0000-0000-000000000000\r\n",
01624 sizeof(cmd));
01625 ff_rtsp_send_cmd(s, "OPTIONS", rt->control_uri, cmd, reply, NULL);
01626 if (reply->status_code != RTSP_STATUS_OK) {
01627 err = AVERROR_INVALIDDATA;
01628 goto fail;
01629 }
01630
01631
01632 if (rt->server_type != RTSP_SERVER_REAL && reply->real_challenge[0]) {
01633 rt->server_type = RTSP_SERVER_REAL;
01634 continue;
01635 } else if (!av_strncasecmp(reply->server, "WMServer/", 9)) {
01636 rt->server_type = RTSP_SERVER_WMS;
01637 } else if (rt->server_type == RTSP_SERVER_REAL)
01638 strcpy(real_challenge, reply->real_challenge);
01639 break;
01640 }
01641
01642 if (s->iformat && CONFIG_RTSP_DEMUXER)
01643 err = ff_rtsp_setup_input_streams(s, reply);
01644 else if (CONFIG_RTSP_MUXER)
01645 err = ff_rtsp_setup_output_streams(s, host);
01646 if (err)
01647 goto fail;
01648
01649 do {
01650 int lower_transport = ff_log2_tab[lower_transport_mask &
01651 ~(lower_transport_mask - 1)];
01652
01653 err = ff_rtsp_make_setup_request(s, host, port, lower_transport,
01654 rt->server_type == RTSP_SERVER_REAL ?
01655 real_challenge : NULL);
01656 if (err < 0)
01657 goto fail;
01658 lower_transport_mask &= ~(1 << lower_transport);
01659 if (lower_transport_mask == 0 && err == 1) {
01660 err = AVERROR(EPROTONOSUPPORT);
01661 goto fail;
01662 }
01663 } while (err);
01664
01665 rt->lower_transport_mask = lower_transport_mask;
01666 av_strlcpy(rt->real_challenge, real_challenge, sizeof(rt->real_challenge));
01667 rt->state = RTSP_STATE_IDLE;
01668 rt->seek_timestamp = 0;
01669 return 0;
01670 fail:
01671 ff_rtsp_close_streams(s);
01672 ff_rtsp_close_connections(s);
01673 if (reply->status_code >=300 && reply->status_code < 400 && s->iformat) {
01674 av_strlcpy(s->filename, reply->location, sizeof(s->filename));
01675 av_log(s, AV_LOG_INFO, "Status %d: Redirecting to %s\n",
01676 reply->status_code,
01677 s->filename);
01678 goto redirect;
01679 }
01680 ff_network_close();
01681 return err;
01682 }
01683 #endif
01684
01685 #if CONFIG_RTPDEC
01686 static int udp_read_packet(AVFormatContext *s, RTSPStream **prtsp_st,
01687 uint8_t *buf, int buf_size, int64_t wait_end)
01688 {
01689 RTSPState *rt = s->priv_data;
01690 RTSPStream *rtsp_st;
01691 int n, i, ret, tcp_fd, timeout_cnt = 0;
01692 int max_p = 0;
01693 struct pollfd *p = rt->p;
01694 int *fds = NULL, fdsnum, fdsidx;
01695
01696 for (;;) {
01697 if (ff_check_interrupt(&s->interrupt_callback))
01698 return AVERROR_EXIT;
01699 if (wait_end && wait_end - av_gettime() < 0)
01700 return AVERROR(EAGAIN);
01701 max_p = 0;
01702 if (rt->rtsp_hd) {
01703 tcp_fd = ffurl_get_file_handle(rt->rtsp_hd);
01704 p[max_p].fd = tcp_fd;
01705 p[max_p++].events = POLLIN;
01706 } else {
01707 tcp_fd = -1;
01708 }
01709 for (i = 0; i < rt->nb_rtsp_streams; i++) {
01710 rtsp_st = rt->rtsp_streams[i];
01711 if (rtsp_st->rtp_handle) {
01712 if (ret = ffurl_get_multi_file_handle(rtsp_st->rtp_handle,
01713 &fds, &fdsnum)) {
01714 av_log(s, AV_LOG_ERROR, "Unable to recover rtp ports\n");
01715 return ret;
01716 }
01717 if (fdsnum != 2) {
01718 av_log(s, AV_LOG_ERROR,
01719 "Number of fds %d not supported\n", fdsnum);
01720 return AVERROR_INVALIDDATA;
01721 }
01722 for (fdsidx = 0; fdsidx < fdsnum; fdsidx++) {
01723 p[max_p].fd = fds[fdsidx];
01724 p[max_p++].events = POLLIN;
01725 }
01726 av_free(fds);
01727 }
01728 }
01729 n = poll(p, max_p, POLL_TIMEOUT_MS);
01730 if (n > 0) {
01731 int j = 1 - (tcp_fd == -1);
01732 timeout_cnt = 0;
01733 for (i = 0; i < rt->nb_rtsp_streams; i++) {
01734 rtsp_st = rt->rtsp_streams[i];
01735 if (rtsp_st->rtp_handle) {
01736 if (p[j].revents & POLLIN || p[j+1].revents & POLLIN) {
01737 ret = ffurl_read(rtsp_st->rtp_handle, buf, buf_size);
01738 if (ret > 0) {
01739 *prtsp_st = rtsp_st;
01740 return ret;
01741 }
01742 }
01743 j+=2;
01744 }
01745 }
01746 #if CONFIG_RTSP_DEMUXER
01747 if (tcp_fd != -1 && p[0].revents & POLLIN) {
01748 if (rt->rtsp_flags & RTSP_FLAG_LISTEN) {
01749 if (rt->state == RTSP_STATE_STREAMING) {
01750 if (!ff_rtsp_parse_streaming_commands(s))
01751 return AVERROR_EOF;
01752 else
01753 av_log(s, AV_LOG_WARNING,
01754 "Unable to answer to TEARDOWN\n");
01755 } else
01756 return 0;
01757 } else {
01758 RTSPMessageHeader reply;
01759 ret = ff_rtsp_read_reply(s, &reply, NULL, 0, NULL);
01760 if (ret < 0)
01761 return ret;
01762
01763 if (rt->state != RTSP_STATE_STREAMING)
01764 return 0;
01765 }
01766 }
01767 #endif
01768 } else if (n == 0 && ++timeout_cnt >= MAX_TIMEOUTS) {
01769 return AVERROR(ETIMEDOUT);
01770 } else if (n < 0 && errno != EINTR)
01771 return AVERROR(errno);
01772 }
01773 }
01774
01775 int ff_rtsp_fetch_packet(AVFormatContext *s, AVPacket *pkt)
01776 {
01777 RTSPState *rt = s->priv_data;
01778 int ret, len;
01779 RTSPStream *rtsp_st, *first_queue_st = NULL;
01780 int64_t wait_end = 0;
01781
01782 if (rt->nb_byes == rt->nb_rtsp_streams)
01783 return AVERROR_EOF;
01784
01785
01786 if (rt->cur_transport_priv) {
01787 if (rt->transport == RTSP_TRANSPORT_RDT) {
01788 ret = ff_rdt_parse_packet(rt->cur_transport_priv, pkt, NULL, 0);
01789 } else if (rt->transport == RTSP_TRANSPORT_RTP) {
01790 ret = ff_rtp_parse_packet(rt->cur_transport_priv, pkt, NULL, 0);
01791 } else if (rt->ts && CONFIG_RTPDEC) {
01792 ret = ff_mpegts_parse_packet(rt->ts, pkt, rt->recvbuf + rt->recvbuf_pos, rt->recvbuf_len - rt->recvbuf_pos);
01793 if (ret >= 0) {
01794 rt->recvbuf_pos += ret;
01795 ret = rt->recvbuf_pos < rt->recvbuf_len;
01796 }
01797 } else
01798 av_assert0(0);
01799 if (ret == 0) {
01800 rt->cur_transport_priv = NULL;
01801 return 0;
01802 } else if (ret == 1) {
01803 return 0;
01804 } else
01805 rt->cur_transport_priv = NULL;
01806 }
01807
01808 if (rt->transport == RTSP_TRANSPORT_RTP) {
01809 int i;
01810 int64_t first_queue_time = 0;
01811 for (i = 0; i < rt->nb_rtsp_streams; i++) {
01812 RTPDemuxContext *rtpctx = rt->rtsp_streams[i]->transport_priv;
01813 int64_t queue_time;
01814 if (!rtpctx)
01815 continue;
01816 queue_time = ff_rtp_queued_packet_time(rtpctx);
01817 if (queue_time && (queue_time - first_queue_time < 0 ||
01818 !first_queue_time)) {
01819 first_queue_time = queue_time;
01820 first_queue_st = rt->rtsp_streams[i];
01821 }
01822 }
01823 if (first_queue_time)
01824 wait_end = first_queue_time + s->max_delay;
01825 }
01826
01827
01828 redo:
01829 if (!rt->recvbuf) {
01830 rt->recvbuf = av_malloc(RECVBUF_SIZE);
01831 if (!rt->recvbuf)
01832 return AVERROR(ENOMEM);
01833 }
01834
01835 switch(rt->lower_transport) {
01836 default:
01837 #if CONFIG_RTSP_DEMUXER
01838 case RTSP_LOWER_TRANSPORT_TCP:
01839 len = ff_rtsp_tcp_read_packet(s, &rtsp_st, rt->recvbuf, RECVBUF_SIZE);
01840 break;
01841 #endif
01842 case RTSP_LOWER_TRANSPORT_UDP:
01843 case RTSP_LOWER_TRANSPORT_UDP_MULTICAST:
01844 len = udp_read_packet(s, &rtsp_st, rt->recvbuf, RECVBUF_SIZE, wait_end);
01845 if (len > 0 && rtsp_st->transport_priv && rt->transport == RTSP_TRANSPORT_RTP)
01846 ff_rtp_check_and_send_back_rr(rtsp_st->transport_priv, len);
01847 break;
01848 }
01849 if (len == AVERROR(EAGAIN) && first_queue_st &&
01850 rt->transport == RTSP_TRANSPORT_RTP) {
01851 rtsp_st = first_queue_st;
01852 ret = ff_rtp_parse_packet(rtsp_st->transport_priv, pkt, NULL, 0);
01853 goto end;
01854 }
01855 if (len < 0)
01856 return len;
01857 if (len == 0)
01858 return AVERROR_EOF;
01859 if (rt->transport == RTSP_TRANSPORT_RDT) {
01860 ret = ff_rdt_parse_packet(rtsp_st->transport_priv, pkt, &rt->recvbuf, len);
01861 } else if (rt->transport == RTSP_TRANSPORT_RTP) {
01862 ret = ff_rtp_parse_packet(rtsp_st->transport_priv, pkt, &rt->recvbuf, len);
01863 if (ret < 0) {
01864
01865
01866 RTPDemuxContext *rtpctx = rtsp_st->transport_priv;
01867 if (rtpctx->first_rtcp_ntp_time != AV_NOPTS_VALUE) {
01868
01869
01870
01871
01872 int i;
01873 AVStream *st = NULL;
01874 if (rtsp_st->stream_index >= 0)
01875 st = s->streams[rtsp_st->stream_index];
01876 for (i = 0; i < rt->nb_rtsp_streams; i++) {
01877 RTPDemuxContext *rtpctx2 = rt->rtsp_streams[i]->transport_priv;
01878 AVStream *st2 = NULL;
01879 if (rt->rtsp_streams[i]->stream_index >= 0)
01880 st2 = s->streams[rt->rtsp_streams[i]->stream_index];
01881 if (rtpctx2 && st && st2 &&
01882 rtpctx2->first_rtcp_ntp_time == AV_NOPTS_VALUE) {
01883 rtpctx2->first_rtcp_ntp_time = rtpctx->first_rtcp_ntp_time;
01884 rtpctx2->rtcp_ts_offset = av_rescale_q(
01885 rtpctx->rtcp_ts_offset, st->time_base,
01886 st2->time_base);
01887 }
01888 }
01889 }
01890 if (ret == -RTCP_BYE) {
01891 rt->nb_byes++;
01892
01893 av_log(s, AV_LOG_DEBUG, "Received BYE for stream %d (%d/%d)\n",
01894 rtsp_st->stream_index, rt->nb_byes, rt->nb_rtsp_streams);
01895
01896 if (rt->nb_byes == rt->nb_rtsp_streams)
01897 return AVERROR_EOF;
01898 }
01899 }
01900 } else if (rt->ts && CONFIG_RTPDEC) {
01901 ret = ff_mpegts_parse_packet(rt->ts, pkt, rt->recvbuf, len);
01902 if (ret >= 0) {
01903 if (ret < len) {
01904 rt->recvbuf_len = len;
01905 rt->recvbuf_pos = ret;
01906 rt->cur_transport_priv = rt->ts;
01907 return 1;
01908 } else {
01909 ret = 0;
01910 }
01911 }
01912 } else {
01913 return AVERROR_INVALIDDATA;
01914 }
01915 end:
01916 if (ret < 0)
01917 goto redo;
01918 if (ret == 1)
01919
01920 rt->cur_transport_priv = rtsp_st->transport_priv;
01921
01922 return ret;
01923 }
01924 #endif
01925
01926 #if CONFIG_SDP_DEMUXER
01927 static int sdp_probe(AVProbeData *p1)
01928 {
01929 const char *p = p1->buf, *p_end = p1->buf + p1->buf_size;
01930
01931
01932 while (p < p_end && *p != '\0') {
01933 if (p + sizeof("c=IN IP") - 1 < p_end &&
01934 av_strstart(p, "c=IN IP", NULL))
01935 return AVPROBE_SCORE_MAX / 2;
01936
01937 while (p < p_end - 1 && *p != '\n') p++;
01938 if (++p >= p_end)
01939 break;
01940 if (*p == '\r')
01941 p++;
01942 }
01943 return 0;
01944 }
01945
01946 static int sdp_read_header(AVFormatContext *s)
01947 {
01948 RTSPState *rt = s->priv_data;
01949 RTSPStream *rtsp_st;
01950 int size, i, err;
01951 char *content;
01952 char url[1024];
01953
01954 if (!ff_network_init())
01955 return AVERROR(EIO);
01956
01957 if (s->max_delay < 0)
01958 s->max_delay = DEFAULT_REORDERING_DELAY;
01959
01960
01961
01962 content = av_malloc(SDP_MAX_SIZE);
01963 size = avio_read(s->pb, content, SDP_MAX_SIZE - 1);
01964 if (size <= 0) {
01965 av_free(content);
01966 return AVERROR_INVALIDDATA;
01967 }
01968 content[size] ='\0';
01969
01970 err = ff_sdp_parse(s, content);
01971 av_free(content);
01972 if (err) goto fail;
01973
01974
01975 for (i = 0; i < rt->nb_rtsp_streams; i++) {
01976 char namebuf[50];
01977 rtsp_st = rt->rtsp_streams[i];
01978
01979 getnameinfo((struct sockaddr*) &rtsp_st->sdp_ip, sizeof(rtsp_st->sdp_ip),
01980 namebuf, sizeof(namebuf), NULL, 0, NI_NUMERICHOST);
01981 ff_url_join(url, sizeof(url), "rtp", NULL,
01982 namebuf, rtsp_st->sdp_port,
01983 "?localport=%d&ttl=%d&connect=%d", rtsp_st->sdp_port,
01984 rtsp_st->sdp_ttl,
01985 rt->rtsp_flags & RTSP_FLAG_FILTER_SRC ? 1 : 0);
01986 if (ffurl_open(&rtsp_st->rtp_handle, url, AVIO_FLAG_READ_WRITE,
01987 &s->interrupt_callback, NULL) < 0) {
01988 err = AVERROR_INVALIDDATA;
01989 goto fail;
01990 }
01991 if ((err = ff_rtsp_open_transport_ctx(s, rtsp_st)))
01992 goto fail;
01993 }
01994 return 0;
01995 fail:
01996 ff_rtsp_close_streams(s);
01997 ff_network_close();
01998 return err;
01999 }
02000
02001 static int sdp_read_close(AVFormatContext *s)
02002 {
02003 ff_rtsp_close_streams(s);
02004 ff_network_close();
02005 return 0;
02006 }
02007
02008 static const AVClass sdp_demuxer_class = {
02009 .class_name = "SDP demuxer",
02010 .item_name = av_default_item_name,
02011 .option = sdp_options,
02012 .version = LIBAVUTIL_VERSION_INT,
02013 };
02014
02015 AVInputFormat ff_sdp_demuxer = {
02016 .name = "sdp",
02017 .long_name = NULL_IF_CONFIG_SMALL("SDP"),
02018 .priv_data_size = sizeof(RTSPState),
02019 .read_probe = sdp_probe,
02020 .read_header = sdp_read_header,
02021 .read_packet = ff_rtsp_fetch_packet,
02022 .read_close = sdp_read_close,
02023 .priv_class = &sdp_demuxer_class,
02024 };
02025 #endif
02026
02027 #if CONFIG_RTP_DEMUXER
02028 static int rtp_probe(AVProbeData *p)
02029 {
02030 if (av_strstart(p->filename, "rtp:", NULL))
02031 return AVPROBE_SCORE_MAX;
02032 return 0;
02033 }
02034
02035 static int rtp_read_header(AVFormatContext *s)
02036 {
02037 uint8_t recvbuf[1500];
02038 char host[500], sdp[500];
02039 int ret, port;
02040 URLContext* in = NULL;
02041 int payload_type;
02042 AVCodecContext codec = { 0 };
02043 struct sockaddr_storage addr;
02044 AVIOContext pb;
02045 socklen_t addrlen = sizeof(addr);
02046 RTSPState *rt = s->priv_data;
02047
02048 if (!ff_network_init())
02049 return AVERROR(EIO);
02050
02051 ret = ffurl_open(&in, s->filename, AVIO_FLAG_READ,
02052 &s->interrupt_callback, NULL);
02053 if (ret)
02054 goto fail;
02055
02056 while (1) {
02057 ret = ffurl_read(in, recvbuf, sizeof(recvbuf));
02058 if (ret == AVERROR(EAGAIN))
02059 continue;
02060 if (ret < 0)
02061 goto fail;
02062 if (ret < 12) {
02063 av_log(s, AV_LOG_WARNING, "Received too short packet\n");
02064 continue;
02065 }
02066
02067 if ((recvbuf[0] & 0xc0) != 0x80) {
02068 av_log(s, AV_LOG_WARNING, "Unsupported RTP version packet "
02069 "received\n");
02070 continue;
02071 }
02072
02073 if (RTP_PT_IS_RTCP(recvbuf[1]))
02074 continue;
02075
02076 payload_type = recvbuf[1] & 0x7f;
02077 break;
02078 }
02079 getsockname(ffurl_get_file_handle(in), (struct sockaddr*) &addr, &addrlen);
02080 ffurl_close(in);
02081 in = NULL;
02082
02083 if (ff_rtp_get_codec_info(&codec, payload_type)) {
02084 av_log(s, AV_LOG_ERROR, "Unable to receive RTP payload type %d "
02085 "without an SDP file describing it\n",
02086 payload_type);
02087 goto fail;
02088 }
02089 if (codec.codec_type != AVMEDIA_TYPE_DATA) {
02090 av_log(s, AV_LOG_WARNING, "Guessing on RTP content - if not received "
02091 "properly you need an SDP file "
02092 "describing it\n");
02093 }
02094
02095 av_url_split(NULL, 0, NULL, 0, host, sizeof(host), &port,
02096 NULL, 0, s->filename);
02097
02098 snprintf(sdp, sizeof(sdp),
02099 "v=0\r\nc=IN IP%d %s\r\nm=%s %d RTP/AVP %d\r\n",
02100 addr.ss_family == AF_INET ? 4 : 6, host,
02101 codec.codec_type == AVMEDIA_TYPE_DATA ? "application" :
02102 codec.codec_type == AVMEDIA_TYPE_VIDEO ? "video" : "audio",
02103 port, payload_type);
02104 av_log(s, AV_LOG_VERBOSE, "SDP:\n%s\n", sdp);
02105
02106 ffio_init_context(&pb, sdp, strlen(sdp), 0, NULL, NULL, NULL, NULL);
02107 s->pb = &pb;
02108
02109
02110 ff_network_close();
02111
02112 rt->media_type_mask = (1 << (AVMEDIA_TYPE_DATA+1)) - 1;
02113
02114 ret = sdp_read_header(s);
02115 s->pb = NULL;
02116 return ret;
02117
02118 fail:
02119 if (in)
02120 ffurl_close(in);
02121 ff_network_close();
02122 return ret;
02123 }
02124
02125 static const AVClass rtp_demuxer_class = {
02126 .class_name = "RTP demuxer",
02127 .item_name = av_default_item_name,
02128 .option = rtp_options,
02129 .version = LIBAVUTIL_VERSION_INT,
02130 };
02131
02132 AVInputFormat ff_rtp_demuxer = {
02133 .name = "rtp",
02134 .long_name = NULL_IF_CONFIG_SMALL("RTP input"),
02135 .priv_data_size = sizeof(RTSPState),
02136 .read_probe = rtp_probe,
02137 .read_header = rtp_read_header,
02138 .read_packet = ff_rtsp_fetch_packet,
02139 .read_close = sdp_read_close,
02140 .flags = AVFMT_NOFILE,
02141 .priv_class = &rtp_demuxer_class,
02142 };
02143 #endif