annotate ffmpeg/libavformat/rtspdec.c @ 13:844d341cf643 tip

Back up before ISMIR
author Yading Song <yading.song@eecs.qmul.ac.uk>
date Thu, 31 Oct 2013 13:17:06 +0000
parents f445c3017523
children
rev   line source
yading@11 1 /*
yading@11 2 * RTSP demuxer
yading@11 3 * Copyright (c) 2002 Fabrice Bellard
yading@11 4 *
yading@11 5 * This file is part of FFmpeg.
yading@11 6 *
yading@11 7 * FFmpeg is free software; you can redistribute it and/or
yading@11 8 * modify it under the terms of the GNU Lesser General Public
yading@11 9 * License as published by the Free Software Foundation; either
yading@11 10 * version 2.1 of the License, or (at your option) any later version.
yading@11 11 *
yading@11 12 * FFmpeg is distributed in the hope that it will be useful,
yading@11 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
yading@11 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
yading@11 15 * Lesser General Public License for more details.
yading@11 16 *
yading@11 17 * You should have received a copy of the GNU Lesser General Public
yading@11 18 * License along with FFmpeg; if not, write to the Free Software
yading@11 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
yading@11 20 */
yading@11 21
yading@11 22 #include "libavutil/avstring.h"
yading@11 23 #include "libavutil/intreadwrite.h"
yading@11 24 #include "libavutil/mathematics.h"
yading@11 25 #include "libavutil/random_seed.h"
yading@11 26 #include "libavutil/time.h"
yading@11 27 #include "avformat.h"
yading@11 28
yading@11 29 #include "internal.h"
yading@11 30 #include "network.h"
yading@11 31 #include "os_support.h"
yading@11 32 #include "rtsp.h"
yading@11 33 #include "rdt.h"
yading@11 34 #include "url.h"
yading@11 35
yading@11 36 static const struct RTSPStatusMessage {
yading@11 37 enum RTSPStatusCode code;
yading@11 38 const char *message;
yading@11 39 } status_messages[] = {
yading@11 40 { RTSP_STATUS_OK, "OK" },
yading@11 41 { RTSP_STATUS_METHOD, "Method Not Allowed" },
yading@11 42 { RTSP_STATUS_BANDWIDTH, "Not Enough Bandwidth" },
yading@11 43 { RTSP_STATUS_SESSION, "Session Not Found" },
yading@11 44 { RTSP_STATUS_STATE, "Method Not Valid in This State" },
yading@11 45 { RTSP_STATUS_AGGREGATE, "Aggregate operation not allowed" },
yading@11 46 { RTSP_STATUS_ONLY_AGGREGATE, "Only aggregate operation allowed" },
yading@11 47 { RTSP_STATUS_TRANSPORT, "Unsupported transport" },
yading@11 48 { RTSP_STATUS_INTERNAL, "Internal Server Error" },
yading@11 49 { RTSP_STATUS_SERVICE, "Service Unavailable" },
yading@11 50 { RTSP_STATUS_VERSION, "RTSP Version not supported" },
yading@11 51 { 0, "NULL" }
yading@11 52 };
yading@11 53
yading@11 54 static int rtsp_read_close(AVFormatContext *s)
yading@11 55 {
yading@11 56 RTSPState *rt = s->priv_data;
yading@11 57
yading@11 58 if (!(rt->rtsp_flags & RTSP_FLAG_LISTEN))
yading@11 59 ff_rtsp_send_cmd_async(s, "TEARDOWN", rt->control_uri, NULL);
yading@11 60
yading@11 61 ff_rtsp_close_streams(s);
yading@11 62 ff_rtsp_close_connections(s);
yading@11 63 ff_network_close();
yading@11 64 rt->real_setup = NULL;
yading@11 65 av_freep(&rt->real_setup_cache);
yading@11 66 return 0;
yading@11 67 }
yading@11 68
yading@11 69 static inline int read_line(AVFormatContext *s, char *rbuf, const int rbufsize,
yading@11 70 int *rbuflen)
yading@11 71 {
yading@11 72 RTSPState *rt = s->priv_data;
yading@11 73 int idx = 0;
yading@11 74 int ret = 0;
yading@11 75 *rbuflen = 0;
yading@11 76
yading@11 77 do {
yading@11 78 ret = ffurl_read_complete(rt->rtsp_hd, rbuf + idx, 1);
yading@11 79 if (ret <= 0)
yading@11 80 return ret ? ret : AVERROR_EOF;
yading@11 81 if (rbuf[idx] == '\r') {
yading@11 82 /* Ignore */
yading@11 83 } else if (rbuf[idx] == '\n') {
yading@11 84 rbuf[idx] = '\0';
yading@11 85 *rbuflen = idx;
yading@11 86 return 0;
yading@11 87 } else
yading@11 88 idx++;
yading@11 89 } while (idx < rbufsize);
yading@11 90 av_log(s, AV_LOG_ERROR, "Message too long\n");
yading@11 91 return AVERROR(EIO);
yading@11 92 }
yading@11 93
yading@11 94 static int rtsp_send_reply(AVFormatContext *s, enum RTSPStatusCode code,
yading@11 95 const char *extracontent, uint16_t seq)
yading@11 96 {
yading@11 97 RTSPState *rt = s->priv_data;
yading@11 98 char message[4096];
yading@11 99 int index = 0;
yading@11 100 while (status_messages[index].code) {
yading@11 101 if (status_messages[index].code == code) {
yading@11 102 snprintf(message, sizeof(message), "RTSP/1.0 %d %s\r\n",
yading@11 103 code, status_messages[index].message);
yading@11 104 break;
yading@11 105 }
yading@11 106 index++;
yading@11 107 }
yading@11 108 if (!status_messages[index].code)
yading@11 109 return AVERROR(EINVAL);
yading@11 110 av_strlcatf(message, sizeof(message), "CSeq: %d\r\n", seq);
yading@11 111 av_strlcatf(message, sizeof(message), "Server: %s\r\n", LIBAVFORMAT_IDENT);
yading@11 112 if (extracontent)
yading@11 113 av_strlcat(message, extracontent, sizeof(message));
yading@11 114 av_strlcat(message, "\r\n", sizeof(message));
yading@11 115 av_dlog(s, "Sending response:\n%s", message);
yading@11 116 ffurl_write(rt->rtsp_hd, message, strlen(message));
yading@11 117
yading@11 118 return 0;
yading@11 119 }
yading@11 120
yading@11 121 static inline int check_sessionid(AVFormatContext *s,
yading@11 122 RTSPMessageHeader *request)
yading@11 123 {
yading@11 124 RTSPState *rt = s->priv_data;
yading@11 125 unsigned char *session_id = rt->session_id;
yading@11 126 if (!session_id[0]) {
yading@11 127 av_log(s, AV_LOG_WARNING, "There is no session-id at the moment\n");
yading@11 128 return 0;
yading@11 129 }
yading@11 130 if (strcmp(session_id, request->session_id)) {
yading@11 131 av_log(s, AV_LOG_ERROR, "Unexpected session-id %s\n",
yading@11 132 request->session_id);
yading@11 133 rtsp_send_reply(s, RTSP_STATUS_SESSION, NULL, request->seq);
yading@11 134 return AVERROR_STREAM_NOT_FOUND;
yading@11 135 }
yading@11 136 return 0;
yading@11 137 }
yading@11 138
yading@11 139 static inline int rtsp_read_request(AVFormatContext *s,
yading@11 140 RTSPMessageHeader *request,
yading@11 141 const char *method)
yading@11 142 {
yading@11 143 RTSPState *rt = s->priv_data;
yading@11 144 char rbuf[1024];
yading@11 145 int rbuflen, ret;
yading@11 146 do {
yading@11 147 ret = read_line(s, rbuf, sizeof(rbuf), &rbuflen);
yading@11 148 if (ret)
yading@11 149 return ret;
yading@11 150 if (rbuflen > 1) {
yading@11 151 av_dlog(s, "Parsing[%d]: %s\n", rbuflen, rbuf);
yading@11 152 ff_rtsp_parse_line(request, rbuf, rt, method);
yading@11 153 }
yading@11 154 } while (rbuflen > 0);
yading@11 155 if (request->seq != rt->seq + 1) {
yading@11 156 av_log(s, AV_LOG_ERROR, "Unexpected Sequence number %d\n",
yading@11 157 request->seq);
yading@11 158 return AVERROR(EINVAL);
yading@11 159 }
yading@11 160 if (rt->session_id[0] && strcmp(method, "OPTIONS")) {
yading@11 161 ret = check_sessionid(s, request);
yading@11 162 if (ret)
yading@11 163 return ret;
yading@11 164 }
yading@11 165
yading@11 166 return 0;
yading@11 167 }
yading@11 168
yading@11 169 static int rtsp_read_announce(AVFormatContext *s)
yading@11 170 {
yading@11 171 RTSPState *rt = s->priv_data;
yading@11 172 RTSPMessageHeader request = { 0 };
yading@11 173 char sdp[4096];
yading@11 174 int ret;
yading@11 175
yading@11 176 ret = rtsp_read_request(s, &request, "ANNOUNCE");
yading@11 177 if (ret)
yading@11 178 return ret;
yading@11 179 rt->seq++;
yading@11 180 if (strcmp(request.content_type, "application/sdp")) {
yading@11 181 av_log(s, AV_LOG_ERROR, "Unexpected content type %s\n",
yading@11 182 request.content_type);
yading@11 183 rtsp_send_reply(s, RTSP_STATUS_SERVICE, NULL, request.seq);
yading@11 184 return AVERROR_OPTION_NOT_FOUND;
yading@11 185 }
yading@11 186 if (request.content_length && request.content_length < sizeof(sdp) - 1) {
yading@11 187 /* Read SDP */
yading@11 188 if (ffurl_read_complete(rt->rtsp_hd, sdp, request.content_length)
yading@11 189 < request.content_length) {
yading@11 190 av_log(s, AV_LOG_ERROR,
yading@11 191 "Unable to get complete SDP Description in ANNOUNCE\n");
yading@11 192 rtsp_send_reply(s, RTSP_STATUS_INTERNAL, NULL, request.seq);
yading@11 193 return AVERROR(EIO);
yading@11 194 }
yading@11 195 sdp[request.content_length] = '\0';
yading@11 196 av_log(s, AV_LOG_VERBOSE, "SDP: %s\n", sdp);
yading@11 197 ret = ff_sdp_parse(s, sdp);
yading@11 198 if (ret)
yading@11 199 return ret;
yading@11 200 rtsp_send_reply(s, RTSP_STATUS_OK, NULL, request.seq);
yading@11 201 return 0;
yading@11 202 }
yading@11 203 av_log(s, AV_LOG_ERROR,
yading@11 204 "Content-Length header value exceeds sdp allocated buffer (4KB)\n");
yading@11 205 rtsp_send_reply(s, RTSP_STATUS_INTERNAL,
yading@11 206 "Content-Length exceeds buffer size", request.seq);
yading@11 207 return AVERROR(EIO);
yading@11 208 }
yading@11 209
yading@11 210 static int rtsp_read_options(AVFormatContext *s)
yading@11 211 {
yading@11 212 RTSPState *rt = s->priv_data;
yading@11 213 RTSPMessageHeader request = { 0 };
yading@11 214 int ret = 0;
yading@11 215
yading@11 216 /* Parsing headers */
yading@11 217 ret = rtsp_read_request(s, &request, "OPTIONS");
yading@11 218 if (ret)
yading@11 219 return ret;
yading@11 220 rt->seq++;
yading@11 221 /* Send Reply */
yading@11 222 rtsp_send_reply(s, RTSP_STATUS_OK,
yading@11 223 "Public: ANNOUNCE, PAUSE, SETUP, TEARDOWN, RECORD\r\n",
yading@11 224 request.seq);
yading@11 225 return 0;
yading@11 226 }
yading@11 227
yading@11 228 static int rtsp_read_setup(AVFormatContext *s, char* host, char *controlurl)
yading@11 229 {
yading@11 230 RTSPState *rt = s->priv_data;
yading@11 231 RTSPMessageHeader request = { 0 };
yading@11 232 int ret = 0;
yading@11 233 char url[1024];
yading@11 234 RTSPStream *rtsp_st;
yading@11 235 char responseheaders[1024];
yading@11 236 int localport = -1;
yading@11 237 int transportidx = 0;
yading@11 238 int streamid = 0;
yading@11 239
yading@11 240 ret = rtsp_read_request(s, &request, "SETUP");
yading@11 241 if (ret)
yading@11 242 return ret;
yading@11 243 rt->seq++;
yading@11 244 if (!request.nb_transports) {
yading@11 245 av_log(s, AV_LOG_ERROR, "No transport defined in SETUP\n");
yading@11 246 return AVERROR_INVALIDDATA;
yading@11 247 }
yading@11 248 for (transportidx = 0; transportidx < request.nb_transports;
yading@11 249 transportidx++) {
yading@11 250 if (!request.transports[transportidx].mode_record ||
yading@11 251 (request.transports[transportidx].lower_transport !=
yading@11 252 RTSP_LOWER_TRANSPORT_UDP &&
yading@11 253 request.transports[transportidx].lower_transport !=
yading@11 254 RTSP_LOWER_TRANSPORT_TCP)) {
yading@11 255 av_log(s, AV_LOG_ERROR, "mode=record/receive not set or transport"
yading@11 256 " protocol not supported (yet)\n");
yading@11 257 return AVERROR_INVALIDDATA;
yading@11 258 }
yading@11 259 }
yading@11 260 if (request.nb_transports > 1)
yading@11 261 av_log(s, AV_LOG_WARNING, "More than one transport not supported, "
yading@11 262 "using first of all\n");
yading@11 263 for (streamid = 0; streamid < rt->nb_rtsp_streams; streamid++) {
yading@11 264 if (!strcmp(rt->rtsp_streams[streamid]->control_url,
yading@11 265 controlurl))
yading@11 266 break;
yading@11 267 }
yading@11 268 if (streamid == rt->nb_rtsp_streams) {
yading@11 269 av_log(s, AV_LOG_ERROR, "Unable to find requested track\n");
yading@11 270 return AVERROR_STREAM_NOT_FOUND;
yading@11 271 }
yading@11 272 rtsp_st = rt->rtsp_streams[streamid];
yading@11 273 localport = rt->rtp_port_min;
yading@11 274
yading@11 275 if (request.transports[0].lower_transport == RTSP_LOWER_TRANSPORT_TCP) {
yading@11 276 rt->lower_transport = RTSP_LOWER_TRANSPORT_TCP;
yading@11 277 if ((ret = ff_rtsp_open_transport_ctx(s, rtsp_st))) {
yading@11 278 rtsp_send_reply(s, RTSP_STATUS_TRANSPORT, NULL, request.seq);
yading@11 279 return ret;
yading@11 280 }
yading@11 281 rtsp_st->interleaved_min = request.transports[0].interleaved_min;
yading@11 282 rtsp_st->interleaved_max = request.transports[0].interleaved_max;
yading@11 283 snprintf(responseheaders, sizeof(responseheaders), "Transport: "
yading@11 284 "RTP/AVP/TCP;unicast;mode=receive;interleaved=%d-%d"
yading@11 285 "\r\n", request.transports[0].interleaved_min,
yading@11 286 request.transports[0].interleaved_max);
yading@11 287 } else {
yading@11 288 do {
yading@11 289 ff_url_join(url, sizeof(url), "rtp", NULL, host, localport, NULL);
yading@11 290 av_dlog(s, "Opening: %s", url);
yading@11 291 ret = ffurl_open(&rtsp_st->rtp_handle, url, AVIO_FLAG_READ_WRITE,
yading@11 292 &s->interrupt_callback, NULL);
yading@11 293 if (ret)
yading@11 294 localport += 2;
yading@11 295 } while (ret || localport > rt->rtp_port_max);
yading@11 296 if (localport > rt->rtp_port_max) {
yading@11 297 rtsp_send_reply(s, RTSP_STATUS_TRANSPORT, NULL, request.seq);
yading@11 298 return ret;
yading@11 299 }
yading@11 300
yading@11 301 av_dlog(s, "Listening on: %d",
yading@11 302 ff_rtp_get_local_rtp_port(rtsp_st->rtp_handle));
yading@11 303 if ((ret = ff_rtsp_open_transport_ctx(s, rtsp_st))) {
yading@11 304 rtsp_send_reply(s, RTSP_STATUS_TRANSPORT, NULL, request.seq);
yading@11 305 return ret;
yading@11 306 }
yading@11 307
yading@11 308 localport = ff_rtp_get_local_rtp_port(rtsp_st->rtp_handle);
yading@11 309 snprintf(responseheaders, sizeof(responseheaders), "Transport: "
yading@11 310 "RTP/AVP/UDP;unicast;mode=receive;source=%s;"
yading@11 311 "client_port=%d-%d;server_port=%d-%d\r\n",
yading@11 312 host, request.transports[0].client_port_min,
yading@11 313 request.transports[0].client_port_max, localport,
yading@11 314 localport + 1);
yading@11 315 }
yading@11 316
yading@11 317 /* Establish sessionid if not previously set */
yading@11 318 /* Put this in a function? */
yading@11 319 /* RFC 2326: session id must be at least 8 digits */
yading@11 320 while (strlen(rt->session_id) < 8)
yading@11 321 av_strlcatf(rt->session_id, 512, "%u", av_get_random_seed());
yading@11 322
yading@11 323 av_strlcatf(responseheaders, sizeof(responseheaders), "Session: %s\r\n",
yading@11 324 rt->session_id);
yading@11 325 /* Send Reply */
yading@11 326 rtsp_send_reply(s, RTSP_STATUS_OK, responseheaders, request.seq);
yading@11 327
yading@11 328 rt->state = RTSP_STATE_PAUSED;
yading@11 329 return 0;
yading@11 330 }
yading@11 331
yading@11 332 static int rtsp_read_record(AVFormatContext *s)
yading@11 333 {
yading@11 334 RTSPState *rt = s->priv_data;
yading@11 335 RTSPMessageHeader request = { 0 };
yading@11 336 int ret = 0;
yading@11 337 char responseheaders[1024];
yading@11 338
yading@11 339 ret = rtsp_read_request(s, &request, "RECORD");
yading@11 340 if (ret)
yading@11 341 return ret;
yading@11 342 ret = check_sessionid(s, &request);
yading@11 343 if (ret)
yading@11 344 return ret;
yading@11 345 rt->seq++;
yading@11 346 snprintf(responseheaders, sizeof(responseheaders), "Session: %s\r\n",
yading@11 347 rt->session_id);
yading@11 348 rtsp_send_reply(s, RTSP_STATUS_OK, responseheaders, request.seq);
yading@11 349
yading@11 350 rt->state = RTSP_STATE_STREAMING;
yading@11 351 return 0;
yading@11 352 }
yading@11 353
yading@11 354 static inline int parse_command_line(AVFormatContext *s, const char *line,
yading@11 355 int linelen, char *uri, int urisize,
yading@11 356 char *method, int methodsize,
yading@11 357 enum RTSPMethod *methodcode)
yading@11 358 {
yading@11 359 RTSPState *rt = s->priv_data;
yading@11 360 const char *linept, *searchlinept;
yading@11 361 linept = strchr(line, ' ');
yading@11 362 if (linept - line > methodsize - 1) {
yading@11 363 av_log(s, AV_LOG_ERROR, "Method string too long\n");
yading@11 364 return AVERROR(EIO);
yading@11 365 }
yading@11 366 memcpy(method, line, linept - line);
yading@11 367 method[linept - line] = '\0';
yading@11 368 linept++;
yading@11 369 if (!strcmp(method, "ANNOUNCE"))
yading@11 370 *methodcode = ANNOUNCE;
yading@11 371 else if (!strcmp(method, "OPTIONS"))
yading@11 372 *methodcode = OPTIONS;
yading@11 373 else if (!strcmp(method, "RECORD"))
yading@11 374 *methodcode = RECORD;
yading@11 375 else if (!strcmp(method, "SETUP"))
yading@11 376 *methodcode = SETUP;
yading@11 377 else if (!strcmp(method, "PAUSE"))
yading@11 378 *methodcode = PAUSE;
yading@11 379 else if (!strcmp(method, "TEARDOWN"))
yading@11 380 *methodcode = TEARDOWN;
yading@11 381 else
yading@11 382 *methodcode = UNKNOWN;
yading@11 383 /* Check method with the state */
yading@11 384 if (rt->state == RTSP_STATE_IDLE) {
yading@11 385 if ((*methodcode != ANNOUNCE) && (*methodcode != OPTIONS)) {
yading@11 386 av_log(s, AV_LOG_ERROR, "Unexpected command in Idle State %s\n",
yading@11 387 line);
yading@11 388 return AVERROR_PROTOCOL_NOT_FOUND;
yading@11 389 }
yading@11 390 } else if (rt->state == RTSP_STATE_PAUSED) {
yading@11 391 if ((*methodcode != OPTIONS) && (*methodcode != RECORD)
yading@11 392 && (*methodcode != SETUP)) {
yading@11 393 av_log(s, AV_LOG_ERROR, "Unexpected command in Paused State %s\n",
yading@11 394 line);
yading@11 395 return AVERROR_PROTOCOL_NOT_FOUND;
yading@11 396 }
yading@11 397 } else if (rt->state == RTSP_STATE_STREAMING) {
yading@11 398 if ((*methodcode != PAUSE) && (*methodcode != OPTIONS)
yading@11 399 && (*methodcode != TEARDOWN)) {
yading@11 400 av_log(s, AV_LOG_ERROR, "Unexpected command in Streaming State"
yading@11 401 " %s\n", line);
yading@11 402 return AVERROR_PROTOCOL_NOT_FOUND;
yading@11 403 }
yading@11 404 } else {
yading@11 405 av_log(s, AV_LOG_ERROR, "Unexpected State [%d]\n", rt->state);
yading@11 406 return AVERROR_BUG;
yading@11 407 }
yading@11 408
yading@11 409 searchlinept = strchr(linept, ' ');
yading@11 410 if (searchlinept == NULL) {
yading@11 411 av_log(s, AV_LOG_ERROR, "Error parsing message URI\n");
yading@11 412 return AVERROR_INVALIDDATA;
yading@11 413 }
yading@11 414 if (searchlinept - linept > urisize - 1) {
yading@11 415 av_log(s, AV_LOG_ERROR, "uri string length exceeded buffer size\n");
yading@11 416 return AVERROR(EIO);
yading@11 417 }
yading@11 418 memcpy(uri, linept, searchlinept - linept);
yading@11 419 uri[searchlinept - linept] = '\0';
yading@11 420 if (strcmp(rt->control_uri, uri)) {
yading@11 421 char host[128], path[512], auth[128];
yading@11 422 int port;
yading@11 423 char ctl_host[128], ctl_path[512], ctl_auth[128];
yading@11 424 int ctl_port;
yading@11 425 av_url_split(NULL, 0, auth, sizeof(auth), host, sizeof(host), &port,
yading@11 426 path, sizeof(path), uri);
yading@11 427 av_url_split(NULL, 0, ctl_auth, sizeof(ctl_auth), ctl_host,
yading@11 428 sizeof(ctl_host), &ctl_port, ctl_path, sizeof(ctl_path),
yading@11 429 rt->control_uri);
yading@11 430 if (strcmp(host, ctl_host))
yading@11 431 av_log(s, AV_LOG_INFO, "Host %s differs from expected %s\n",
yading@11 432 host, ctl_host);
yading@11 433 if (strcmp(path, ctl_path) && *methodcode != SETUP)
yading@11 434 av_log(s, AV_LOG_WARNING, "WARNING: Path %s differs from expected"
yading@11 435 " %s\n", path, ctl_path);
yading@11 436 if (*methodcode == ANNOUNCE) {
yading@11 437 av_log(s, AV_LOG_INFO,
yading@11 438 "Updating control URI to %s\n", uri);
yading@11 439 av_strlcpy(rt->control_uri, uri, sizeof(rt->control_uri));
yading@11 440 }
yading@11 441 }
yading@11 442
yading@11 443 linept = searchlinept + 1;
yading@11 444 if (!av_strstart(linept, "RTSP/1.0", NULL)) {
yading@11 445 av_log(s, AV_LOG_ERROR, "Error parsing protocol or version\n");
yading@11 446 return AVERROR_PROTOCOL_NOT_FOUND;
yading@11 447 }
yading@11 448 return 0;
yading@11 449 }
yading@11 450
yading@11 451 int ff_rtsp_parse_streaming_commands(AVFormatContext *s)
yading@11 452 {
yading@11 453 RTSPState *rt = s->priv_data;
yading@11 454 unsigned char rbuf[4096];
yading@11 455 unsigned char method[10];
yading@11 456 char uri[500];
yading@11 457 int ret;
yading@11 458 int rbuflen = 0;
yading@11 459 RTSPMessageHeader request = { 0 };
yading@11 460 enum RTSPMethod methodcode;
yading@11 461
yading@11 462 ret = read_line(s, rbuf, sizeof(rbuf), &rbuflen);
yading@11 463 if (ret < 0)
yading@11 464 return ret;
yading@11 465 ret = parse_command_line(s, rbuf, rbuflen, uri, sizeof(uri), method,
yading@11 466 sizeof(method), &methodcode);
yading@11 467 if (ret) {
yading@11 468 av_log(s, AV_LOG_ERROR, "RTSP: Unexpected Command\n");
yading@11 469 return ret;
yading@11 470 }
yading@11 471
yading@11 472 ret = rtsp_read_request(s, &request, method);
yading@11 473 if (ret)
yading@11 474 return ret;
yading@11 475 rt->seq++;
yading@11 476 if (methodcode == PAUSE) {
yading@11 477 rt->state = RTSP_STATE_PAUSED;
yading@11 478 ret = rtsp_send_reply(s, RTSP_STATUS_OK, NULL , request.seq);
yading@11 479 // TODO: Missing date header in response
yading@11 480 } else if (methodcode == OPTIONS) {
yading@11 481 ret = rtsp_send_reply(s, RTSP_STATUS_OK,
yading@11 482 "Public: ANNOUNCE, PAUSE, SETUP, TEARDOWN, "
yading@11 483 "RECORD\r\n", request.seq);
yading@11 484 } else if (methodcode == TEARDOWN) {
yading@11 485 rt->state = RTSP_STATE_IDLE;
yading@11 486 ret = rtsp_send_reply(s, RTSP_STATUS_OK, NULL , request.seq);
yading@11 487 return 0;
yading@11 488 }
yading@11 489 return ret;
yading@11 490 }
yading@11 491
yading@11 492 static int rtsp_read_play(AVFormatContext *s)
yading@11 493 {
yading@11 494 RTSPState *rt = s->priv_data;
yading@11 495 RTSPMessageHeader reply1, *reply = &reply1;
yading@11 496 int i;
yading@11 497 char cmd[1024];
yading@11 498
yading@11 499 av_log(s, AV_LOG_DEBUG, "hello state=%d\n", rt->state);
yading@11 500 rt->nb_byes = 0;
yading@11 501
yading@11 502 if (!(rt->server_type == RTSP_SERVER_REAL && rt->need_subscription)) {
yading@11 503 if (rt->transport == RTSP_TRANSPORT_RTP) {
yading@11 504 for (i = 0; i < rt->nb_rtsp_streams; i++) {
yading@11 505 RTSPStream *rtsp_st = rt->rtsp_streams[i];
yading@11 506 RTPDemuxContext *rtpctx = rtsp_st->transport_priv;
yading@11 507 if (!rtpctx)
yading@11 508 continue;
yading@11 509 ff_rtp_reset_packet_queue(rtpctx);
yading@11 510 rtpctx->last_rtcp_ntp_time = AV_NOPTS_VALUE;
yading@11 511 rtpctx->first_rtcp_ntp_time = AV_NOPTS_VALUE;
yading@11 512 rtpctx->base_timestamp = 0;
yading@11 513 rtpctx->timestamp = 0;
yading@11 514 rtpctx->unwrapped_timestamp = 0;
yading@11 515 rtpctx->rtcp_ts_offset = 0;
yading@11 516 }
yading@11 517 }
yading@11 518 if (rt->state == RTSP_STATE_PAUSED) {
yading@11 519 cmd[0] = 0;
yading@11 520 } else {
yading@11 521 snprintf(cmd, sizeof(cmd),
yading@11 522 "Range: npt=%"PRId64".%03"PRId64"-\r\n",
yading@11 523 rt->seek_timestamp / AV_TIME_BASE,
yading@11 524 rt->seek_timestamp / (AV_TIME_BASE / 1000) % 1000);
yading@11 525 }
yading@11 526 ff_rtsp_send_cmd(s, "PLAY", rt->control_uri, cmd, reply, NULL);
yading@11 527 if (reply->status_code != RTSP_STATUS_OK) {
yading@11 528 return -1;
yading@11 529 }
yading@11 530 if (rt->transport == RTSP_TRANSPORT_RTP &&
yading@11 531 reply->range_start != AV_NOPTS_VALUE) {
yading@11 532 for (i = 0; i < rt->nb_rtsp_streams; i++) {
yading@11 533 RTSPStream *rtsp_st = rt->rtsp_streams[i];
yading@11 534 RTPDemuxContext *rtpctx = rtsp_st->transport_priv;
yading@11 535 AVStream *st = NULL;
yading@11 536 if (!rtpctx || rtsp_st->stream_index < 0)
yading@11 537 continue;
yading@11 538 st = s->streams[rtsp_st->stream_index];
yading@11 539 rtpctx->range_start_offset =
yading@11 540 av_rescale_q(reply->range_start, AV_TIME_BASE_Q,
yading@11 541 st->time_base);
yading@11 542 }
yading@11 543 }
yading@11 544 }
yading@11 545 rt->state = RTSP_STATE_STREAMING;
yading@11 546 return 0;
yading@11 547 }
yading@11 548
yading@11 549 /* pause the stream */
yading@11 550 static int rtsp_read_pause(AVFormatContext *s)
yading@11 551 {
yading@11 552 RTSPState *rt = s->priv_data;
yading@11 553 RTSPMessageHeader reply1, *reply = &reply1;
yading@11 554
yading@11 555 if (rt->state != RTSP_STATE_STREAMING)
yading@11 556 return 0;
yading@11 557 else if (!(rt->server_type == RTSP_SERVER_REAL && rt->need_subscription)) {
yading@11 558 ff_rtsp_send_cmd(s, "PAUSE", rt->control_uri, NULL, reply, NULL);
yading@11 559 if (reply->status_code != RTSP_STATUS_OK) {
yading@11 560 return -1;
yading@11 561 }
yading@11 562 }
yading@11 563 rt->state = RTSP_STATE_PAUSED;
yading@11 564 return 0;
yading@11 565 }
yading@11 566
yading@11 567 int ff_rtsp_setup_input_streams(AVFormatContext *s, RTSPMessageHeader *reply)
yading@11 568 {
yading@11 569 RTSPState *rt = s->priv_data;
yading@11 570 char cmd[1024];
yading@11 571 unsigned char *content = NULL;
yading@11 572 int ret;
yading@11 573
yading@11 574 /* describe the stream */
yading@11 575 snprintf(cmd, sizeof(cmd),
yading@11 576 "Accept: application/sdp\r\n");
yading@11 577 if (rt->server_type == RTSP_SERVER_REAL) {
yading@11 578 /**
yading@11 579 * The Require: attribute is needed for proper streaming from
yading@11 580 * Realmedia servers.
yading@11 581 */
yading@11 582 av_strlcat(cmd,
yading@11 583 "Require: com.real.retain-entity-for-setup\r\n",
yading@11 584 sizeof(cmd));
yading@11 585 }
yading@11 586 ff_rtsp_send_cmd(s, "DESCRIBE", rt->control_uri, cmd, reply, &content);
yading@11 587 if (!content)
yading@11 588 return AVERROR_INVALIDDATA;
yading@11 589 if (reply->status_code != RTSP_STATUS_OK) {
yading@11 590 av_freep(&content);
yading@11 591 return AVERROR_INVALIDDATA;
yading@11 592 }
yading@11 593
yading@11 594 av_log(s, AV_LOG_VERBOSE, "SDP:\n%s\n", content);
yading@11 595 /* now we got the SDP description, we parse it */
yading@11 596 ret = ff_sdp_parse(s, (const char *)content);
yading@11 597 av_freep(&content);
yading@11 598 if (ret < 0)
yading@11 599 return ret;
yading@11 600
yading@11 601 return 0;
yading@11 602 }
yading@11 603
yading@11 604 static int rtsp_listen(AVFormatContext *s)
yading@11 605 {
yading@11 606 RTSPState *rt = s->priv_data;
yading@11 607 char host[128], path[512], auth[128];
yading@11 608 char uri[500];
yading@11 609 int port;
yading@11 610 char tcpname[500];
yading@11 611 unsigned char rbuf[4096];
yading@11 612 unsigned char method[10];
yading@11 613 int rbuflen = 0;
yading@11 614 int ret;
yading@11 615 enum RTSPMethod methodcode;
yading@11 616
yading@11 617 /* extract hostname and port */
yading@11 618 av_url_split(NULL, 0, auth, sizeof(auth), host, sizeof(host), &port,
yading@11 619 path, sizeof(path), s->filename);
yading@11 620
yading@11 621 /* ff_url_join. No authorization by now (NULL) */
yading@11 622 ff_url_join(rt->control_uri, sizeof(rt->control_uri), "rtsp", NULL, host,
yading@11 623 port, "%s", path);
yading@11 624
yading@11 625 if (port < 0)
yading@11 626 port = RTSP_DEFAULT_PORT;
yading@11 627
yading@11 628 /* Create TCP connection */
yading@11 629 ff_url_join(tcpname, sizeof(tcpname), "tcp", NULL, host, port,
yading@11 630 "?listen&listen_timeout=%d", rt->initial_timeout * 1000);
yading@11 631
yading@11 632 if (ret = ffurl_open(&rt->rtsp_hd, tcpname, AVIO_FLAG_READ_WRITE,
yading@11 633 &s->interrupt_callback, NULL)) {
yading@11 634 av_log(s, AV_LOG_ERROR, "Unable to open RTSP for listening\n");
yading@11 635 return ret;
yading@11 636 }
yading@11 637 rt->state = RTSP_STATE_IDLE;
yading@11 638 rt->rtsp_hd_out = rt->rtsp_hd;
yading@11 639 for (;;) { /* Wait for incoming RTSP messages */
yading@11 640 ret = read_line(s, rbuf, sizeof(rbuf), &rbuflen);
yading@11 641 if (ret < 0)
yading@11 642 return ret;
yading@11 643 ret = parse_command_line(s, rbuf, rbuflen, uri, sizeof(uri), method,
yading@11 644 sizeof(method), &methodcode);
yading@11 645 if (ret) {
yading@11 646 av_log(s, AV_LOG_ERROR, "RTSP: Unexpected Command\n");
yading@11 647 return ret;
yading@11 648 }
yading@11 649
yading@11 650 if (methodcode == ANNOUNCE) {
yading@11 651 ret = rtsp_read_announce(s);
yading@11 652 rt->state = RTSP_STATE_PAUSED;
yading@11 653 } else if (methodcode == OPTIONS) {
yading@11 654 ret = rtsp_read_options(s);
yading@11 655 } else if (methodcode == RECORD) {
yading@11 656 ret = rtsp_read_record(s);
yading@11 657 if (!ret)
yading@11 658 return 0; // We are ready for streaming
yading@11 659 } else if (methodcode == SETUP)
yading@11 660 ret = rtsp_read_setup(s, host, uri);
yading@11 661 if (ret) {
yading@11 662 ffurl_close(rt->rtsp_hd);
yading@11 663 return AVERROR_INVALIDDATA;
yading@11 664 }
yading@11 665 }
yading@11 666 return 0;
yading@11 667 }
yading@11 668
yading@11 669 static int rtsp_probe(AVProbeData *p)
yading@11 670 {
yading@11 671 if (av_strstart(p->filename, "rtsp:", NULL))
yading@11 672 return AVPROBE_SCORE_MAX;
yading@11 673 return 0;
yading@11 674 }
yading@11 675
yading@11 676 static int rtsp_read_header(AVFormatContext *s)
yading@11 677 {
yading@11 678 RTSPState *rt = s->priv_data;
yading@11 679 int ret;
yading@11 680
yading@11 681 if (rt->initial_timeout > 0)
yading@11 682 rt->rtsp_flags |= RTSP_FLAG_LISTEN;
yading@11 683
yading@11 684 if (rt->rtsp_flags & RTSP_FLAG_LISTEN) {
yading@11 685 ret = rtsp_listen(s);
yading@11 686 if (ret)
yading@11 687 return ret;
yading@11 688 } else {
yading@11 689 ret = ff_rtsp_connect(s);
yading@11 690 if (ret)
yading@11 691 return ret;
yading@11 692
yading@11 693 rt->real_setup_cache = !s->nb_streams ? NULL :
yading@11 694 av_mallocz(2 * s->nb_streams * sizeof(*rt->real_setup_cache));
yading@11 695 if (!rt->real_setup_cache && s->nb_streams)
yading@11 696 return AVERROR(ENOMEM);
yading@11 697 rt->real_setup = rt->real_setup_cache + s->nb_streams;
yading@11 698
yading@11 699 if (rt->initial_pause) {
yading@11 700 /* do not start immediately */
yading@11 701 } else {
yading@11 702 if (rtsp_read_play(s) < 0) {
yading@11 703 ff_rtsp_close_streams(s);
yading@11 704 ff_rtsp_close_connections(s);
yading@11 705 return AVERROR_INVALIDDATA;
yading@11 706 }
yading@11 707 }
yading@11 708 }
yading@11 709
yading@11 710 return 0;
yading@11 711 }
yading@11 712
yading@11 713 int ff_rtsp_tcp_read_packet(AVFormatContext *s, RTSPStream **prtsp_st,
yading@11 714 uint8_t *buf, int buf_size)
yading@11 715 {
yading@11 716 RTSPState *rt = s->priv_data;
yading@11 717 int id, len, i, ret;
yading@11 718 RTSPStream *rtsp_st;
yading@11 719
yading@11 720 av_dlog(s, "tcp_read_packet:\n");
yading@11 721 redo:
yading@11 722 for (;;) {
yading@11 723 RTSPMessageHeader reply;
yading@11 724
yading@11 725 ret = ff_rtsp_read_reply(s, &reply, NULL, 1, NULL);
yading@11 726 if (ret < 0)
yading@11 727 return ret;
yading@11 728 if (ret == 1) /* received '$' */
yading@11 729 break;
yading@11 730 /* XXX: parse message */
yading@11 731 if (rt->state != RTSP_STATE_STREAMING)
yading@11 732 return 0;
yading@11 733 }
yading@11 734 ret = ffurl_read_complete(rt->rtsp_hd, buf, 3);
yading@11 735 if (ret != 3)
yading@11 736 return -1;
yading@11 737 id = buf[0];
yading@11 738 len = AV_RB16(buf + 1);
yading@11 739 av_dlog(s, "id=%d len=%d\n", id, len);
yading@11 740 if (len > buf_size || len < 8)
yading@11 741 goto redo;
yading@11 742 /* get the data */
yading@11 743 ret = ffurl_read_complete(rt->rtsp_hd, buf, len);
yading@11 744 if (ret != len)
yading@11 745 return -1;
yading@11 746 if (rt->transport == RTSP_TRANSPORT_RDT &&
yading@11 747 ff_rdt_parse_header(buf, len, &id, NULL, NULL, NULL, NULL) < 0)
yading@11 748 return -1;
yading@11 749
yading@11 750 /* find the matching stream */
yading@11 751 for (i = 0; i < rt->nb_rtsp_streams; i++) {
yading@11 752 rtsp_st = rt->rtsp_streams[i];
yading@11 753 if (id >= rtsp_st->interleaved_min &&
yading@11 754 id <= rtsp_st->interleaved_max)
yading@11 755 goto found;
yading@11 756 }
yading@11 757 goto redo;
yading@11 758 found:
yading@11 759 *prtsp_st = rtsp_st;
yading@11 760 return len;
yading@11 761 }
yading@11 762
yading@11 763 static int resetup_tcp(AVFormatContext *s)
yading@11 764 {
yading@11 765 RTSPState *rt = s->priv_data;
yading@11 766 char host[1024];
yading@11 767 int port;
yading@11 768
yading@11 769 av_url_split(NULL, 0, NULL, 0, host, sizeof(host), &port, NULL, 0,
yading@11 770 s->filename);
yading@11 771 ff_rtsp_undo_setup(s);
yading@11 772 return ff_rtsp_make_setup_request(s, host, port, RTSP_LOWER_TRANSPORT_TCP,
yading@11 773 rt->real_challenge);
yading@11 774 }
yading@11 775
yading@11 776 static int rtsp_read_packet(AVFormatContext *s, AVPacket *pkt)
yading@11 777 {
yading@11 778 RTSPState *rt = s->priv_data;
yading@11 779 int ret;
yading@11 780 RTSPMessageHeader reply1, *reply = &reply1;
yading@11 781 char cmd[1024];
yading@11 782
yading@11 783 retry:
yading@11 784 if (rt->server_type == RTSP_SERVER_REAL) {
yading@11 785 int i;
yading@11 786
yading@11 787 for (i = 0; i < s->nb_streams; i++)
yading@11 788 rt->real_setup[i] = s->streams[i]->discard;
yading@11 789
yading@11 790 if (!rt->need_subscription) {
yading@11 791 if (memcmp (rt->real_setup, rt->real_setup_cache,
yading@11 792 sizeof(enum AVDiscard) * s->nb_streams)) {
yading@11 793 snprintf(cmd, sizeof(cmd),
yading@11 794 "Unsubscribe: %s\r\n",
yading@11 795 rt->last_subscription);
yading@11 796 ff_rtsp_send_cmd(s, "SET_PARAMETER", rt->control_uri,
yading@11 797 cmd, reply, NULL);
yading@11 798 if (reply->status_code != RTSP_STATUS_OK)
yading@11 799 return AVERROR_INVALIDDATA;
yading@11 800 rt->need_subscription = 1;
yading@11 801 }
yading@11 802 }
yading@11 803
yading@11 804 if (rt->need_subscription) {
yading@11 805 int r, rule_nr, first = 1;
yading@11 806
yading@11 807 memcpy(rt->real_setup_cache, rt->real_setup,
yading@11 808 sizeof(enum AVDiscard) * s->nb_streams);
yading@11 809 rt->last_subscription[0] = 0;
yading@11 810
yading@11 811 snprintf(cmd, sizeof(cmd),
yading@11 812 "Subscribe: ");
yading@11 813 for (i = 0; i < rt->nb_rtsp_streams; i++) {
yading@11 814 rule_nr = 0;
yading@11 815 for (r = 0; r < s->nb_streams; r++) {
yading@11 816 if (s->streams[r]->id == i) {
yading@11 817 if (s->streams[r]->discard != AVDISCARD_ALL) {
yading@11 818 if (!first)
yading@11 819 av_strlcat(rt->last_subscription, ",",
yading@11 820 sizeof(rt->last_subscription));
yading@11 821 ff_rdt_subscribe_rule(
yading@11 822 rt->last_subscription,
yading@11 823 sizeof(rt->last_subscription), i, rule_nr);
yading@11 824 first = 0;
yading@11 825 }
yading@11 826 rule_nr++;
yading@11 827 }
yading@11 828 }
yading@11 829 }
yading@11 830 av_strlcatf(cmd, sizeof(cmd), "%s\r\n", rt->last_subscription);
yading@11 831 ff_rtsp_send_cmd(s, "SET_PARAMETER", rt->control_uri,
yading@11 832 cmd, reply, NULL);
yading@11 833 if (reply->status_code != RTSP_STATUS_OK)
yading@11 834 return AVERROR_INVALIDDATA;
yading@11 835 rt->need_subscription = 0;
yading@11 836
yading@11 837 if (rt->state == RTSP_STATE_STREAMING)
yading@11 838 rtsp_read_play (s);
yading@11 839 }
yading@11 840 }
yading@11 841
yading@11 842 ret = ff_rtsp_fetch_packet(s, pkt);
yading@11 843 if (ret < 0) {
yading@11 844 if (ret == AVERROR(ETIMEDOUT) && !rt->packets) {
yading@11 845 if (rt->lower_transport == RTSP_LOWER_TRANSPORT_UDP &&
yading@11 846 rt->lower_transport_mask & (1 << RTSP_LOWER_TRANSPORT_TCP)) {
yading@11 847 RTSPMessageHeader reply1, *reply = &reply1;
yading@11 848 av_log(s, AV_LOG_WARNING, "UDP timeout, retrying with TCP\n");
yading@11 849 if (rtsp_read_pause(s) != 0)
yading@11 850 return -1;
yading@11 851 // TEARDOWN is required on Real-RTSP, but might make
yading@11 852 // other servers close the connection.
yading@11 853 if (rt->server_type == RTSP_SERVER_REAL)
yading@11 854 ff_rtsp_send_cmd(s, "TEARDOWN", rt->control_uri, NULL,
yading@11 855 reply, NULL);
yading@11 856 rt->session_id[0] = '\0';
yading@11 857 if (resetup_tcp(s) == 0) {
yading@11 858 rt->state = RTSP_STATE_IDLE;
yading@11 859 rt->need_subscription = 1;
yading@11 860 if (rtsp_read_play(s) != 0)
yading@11 861 return -1;
yading@11 862 goto retry;
yading@11 863 }
yading@11 864 }
yading@11 865 }
yading@11 866 return ret;
yading@11 867 }
yading@11 868 rt->packets++;
yading@11 869
yading@11 870 if (!(rt->rtsp_flags & RTSP_FLAG_LISTEN)) {
yading@11 871 /* send dummy request to keep TCP connection alive */
yading@11 872 if ((av_gettime() - rt->last_cmd_time) / 1000000 >= rt->timeout / 2 ||
yading@11 873 rt->auth_state.stale) {
yading@11 874 if (rt->server_type == RTSP_SERVER_WMS ||
yading@11 875 (rt->server_type != RTSP_SERVER_REAL &&
yading@11 876 rt->get_parameter_supported)) {
yading@11 877 ff_rtsp_send_cmd_async(s, "GET_PARAMETER", rt->control_uri, NULL);
yading@11 878 } else {
yading@11 879 ff_rtsp_send_cmd_async(s, "OPTIONS", "*", NULL);
yading@11 880 }
yading@11 881 /* The stale flag should be reset when creating the auth response in
yading@11 882 * ff_rtsp_send_cmd_async, but reset it here just in case we never
yading@11 883 * called the auth code (if we didn't have any credentials set). */
yading@11 884 rt->auth_state.stale = 0;
yading@11 885 }
yading@11 886 }
yading@11 887
yading@11 888 return 0;
yading@11 889 }
yading@11 890
yading@11 891 static int rtsp_read_seek(AVFormatContext *s, int stream_index,
yading@11 892 int64_t timestamp, int flags)
yading@11 893 {
yading@11 894 RTSPState *rt = s->priv_data;
yading@11 895
yading@11 896 rt->seek_timestamp = av_rescale_q(timestamp,
yading@11 897 s->streams[stream_index]->time_base,
yading@11 898 AV_TIME_BASE_Q);
yading@11 899 switch(rt->state) {
yading@11 900 default:
yading@11 901 case RTSP_STATE_IDLE:
yading@11 902 break;
yading@11 903 case RTSP_STATE_STREAMING:
yading@11 904 if (rtsp_read_pause(s) != 0)
yading@11 905 return -1;
yading@11 906 rt->state = RTSP_STATE_SEEKING;
yading@11 907 if (rtsp_read_play(s) != 0)
yading@11 908 return -1;
yading@11 909 break;
yading@11 910 case RTSP_STATE_PAUSED:
yading@11 911 rt->state = RTSP_STATE_IDLE;
yading@11 912 break;
yading@11 913 }
yading@11 914 return 0;
yading@11 915 }
yading@11 916
yading@11 917 static const AVClass rtsp_demuxer_class = {
yading@11 918 .class_name = "RTSP demuxer",
yading@11 919 .item_name = av_default_item_name,
yading@11 920 .option = ff_rtsp_options,
yading@11 921 .version = LIBAVUTIL_VERSION_INT,
yading@11 922 };
yading@11 923
yading@11 924 AVInputFormat ff_rtsp_demuxer = {
yading@11 925 .name = "rtsp",
yading@11 926 .long_name = NULL_IF_CONFIG_SMALL("RTSP input"),
yading@11 927 .priv_data_size = sizeof(RTSPState),
yading@11 928 .read_probe = rtsp_probe,
yading@11 929 .read_header = rtsp_read_header,
yading@11 930 .read_packet = rtsp_read_packet,
yading@11 931 .read_close = rtsp_read_close,
yading@11 932 .read_seek = rtsp_read_seek,
yading@11 933 .flags = AVFMT_NOFILE,
yading@11 934 .read_play = rtsp_read_play,
yading@11 935 .read_pause = rtsp_read_pause,
yading@11 936 .priv_class = &rtsp_demuxer_class,
yading@11 937 };