FFmpeg
Loading...
Searching...
No Matches
rtspdec.c
Go to the documentation of this file.
1/*
2 * RTSP demuxer
3 * Copyright (c) 2002 Fabrice Bellard
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22#include "config_components.h"
23
24#include "libavutil/avstring.h"
25#include "libavutil/avassert.h"
28#include "libavutil/mem.h"
30#include "libavutil/time.h"
31#include "avformat.h"
32#include "demux.h"
33
34#include "internal.h"
35#include "network.h"
36#include "os_support.h"
37#include "rtpproto.h"
38#include "rtsp.h"
39#include "rdt.h"
40#include "tls.h"
41#include "url.h"
42#include "version.h"
43
44static const struct RTSPStatusMessage {
46 const char *message;
47} status_messages[] = {
48 { RTSP_STATUS_OK, "OK" },
49 { RTSP_STATUS_METHOD, "Method Not Allowed" },
50 { RTSP_STATUS_BANDWIDTH, "Not Enough Bandwidth" },
51 { RTSP_STATUS_SESSION, "Session Not Found" },
52 { RTSP_STATUS_STATE, "Method Not Valid in This State" },
53 { RTSP_STATUS_AGGREGATE, "Aggregate operation not allowed" },
54 { RTSP_STATUS_ONLY_AGGREGATE, "Only aggregate operation allowed" },
55 { RTSP_STATUS_TRANSPORT, "Unsupported transport" },
56 { RTSP_STATUS_INTERNAL, "Internal Server Error" },
57 { RTSP_STATUS_SERVICE, "Service Unavailable" },
58 { RTSP_STATUS_VERSION, "RTSP Version not supported" },
59 { 0, "NULL" }
60};
61
62/* Upper bound on the whole close-time TEARDOWN exchange. */
63#define RTSP_TEARDOWN_TIMEOUT (500 * 1000)
64
66{
67 RTSPState *rt = s->priv_data;
68
69 if (!(rt->rtsp_flags & RTSP_FLAG_LISTEN)) {
70 /* Arm the deadline consulted by the control connection's interrupt
71 * callback, which every nested URLContext (tls, http, httpproxy, tcp)
72 * inherited. A pending user interrupt (Ctrl-C) is thus ignored on the
73 * whole transport stack until the deadline expires, so TEARDOWN goes
74 * out and its reply can be read. It stays armed for the connection
75 * shutdown below, which keeps the total close time bounded. The
76 * command is sent synchronously so the 200 OK is consumed and the
77 * server releases the session. */
79 ff_rtsp_send_cmd(s, "TEARDOWN", rt->control_uri, NULL,
80 &(RTSPMessageHeader){0}, NULL);
81 }
82
86 rt->real_setup = NULL;
88 return 0;
89}
90
91static inline int read_line(AVFormatContext *s, char *rbuf, const int rbufsize,
92 int *rbuflen)
93{
94 RTSPState *rt = s->priv_data;
95 int idx = 0;
96 int ret = 0;
97 *rbuflen = 0;
98
99 do {
100 ret = ffurl_read_complete(rt->rtsp_hd, rbuf + idx, 1);
101 if (ret <= 0)
102 return ret ? ret : AVERROR_EOF;
103 if (rbuf[idx] == '\r') {
104 /* Ignore */
105 } else if (rbuf[idx] == '\n') {
106 rbuf[idx] = '\0';
107 *rbuflen = idx;
108 return 0;
109 } else
110 idx++;
111 } while (idx < rbufsize);
112 av_log(s, AV_LOG_ERROR, "Message too long\n");
113 return AVERROR(EIO);
114}
115
117 const char *extracontent, uint16_t seq)
118{
119 RTSPState *rt = s->priv_data;
120 char message[MAX_URL_SIZE];
121 int index = 0;
122 while (status_messages[index].code) {
123 if (status_messages[index].code == code) {
124 snprintf(message, sizeof(message), "RTSP/1.0 %d %s\r\n",
126 break;
127 }
128 index++;
129 }
131 return AVERROR(EINVAL);
132 av_strlcatf(message, sizeof(message), "CSeq: %d\r\n", seq);
133 av_strlcatf(message, sizeof(message), "Server: %s\r\n", LIBAVFORMAT_IDENT);
134 if (extracontent)
135 av_strlcat(message, extracontent, sizeof(message));
136 av_strlcat(message, "\r\n", sizeof(message));
137 av_log(s, AV_LOG_TRACE, "Sending response:\n%s", message);
138 ffurl_write(rt->rtsp_hd_out, message, strlen(message));
139
140 return 0;
141}
142
144 RTSPMessageHeader *request)
145{
146 RTSPState *rt = s->priv_data;
147 unsigned char *session_id = rt->session_id;
148 if (!session_id[0]) {
149 av_log(s, AV_LOG_WARNING, "There is no session-id at the moment\n");
150 return 0;
151 }
152 if (strcmp(session_id, request->session_id)) {
153 av_log(s, AV_LOG_ERROR, "Unexpected session-id %s\n",
154 request->session_id);
157 }
158 return 0;
159}
160
162 RTSPMessageHeader *request,
163 const char *method)
164{
165 RTSPState *rt = s->priv_data;
166 char rbuf[MAX_URL_SIZE];
167 int rbuflen, ret;
168 do {
169 ret = read_line(s, rbuf, sizeof(rbuf), &rbuflen);
170 if (ret)
171 return ret;
172 if (rbuflen > 1) {
173 av_log(s, AV_LOG_TRACE, "Parsing[%d]: %s\n", rbuflen, rbuf);
174 ff_rtsp_parse_line(s, request, rbuf, rt, method);
175 }
176 } while (rbuflen > 0);
177 if (request->seq != rt->seq + 1) {
178 av_log(s, AV_LOG_ERROR, "Unexpected Sequence number %d\n",
179 request->seq);
180 return AVERROR(EINVAL);
181 }
182 if (rt->session_id[0] && strcmp(method, "OPTIONS")) {
183 ret = check_sessionid(s, request);
184 if (ret)
185 return ret;
186 }
187
188 return 0;
189}
190
192{
193 RTSPState *rt = s->priv_data;
194 RTSPMessageHeader request = { 0 };
195 char *sdp;
196 int ret;
197
198 ret = rtsp_read_request(s, &request, "ANNOUNCE");
199 if (ret)
200 return ret;
201 rt->seq++;
202 if (strcmp(request.content_type, "application/sdp")) {
203 av_log(s, AV_LOG_ERROR, "Unexpected content type %s\n",
204 request.content_type);
207 }
208 if (request.content_length > 0 && request.content_length <= SDP_MAX_SIZE) {
209 sdp = av_malloc(request.content_length + 1);
210 if (!sdp)
211 return AVERROR(ENOMEM);
212
213 /* Read SDP */
214 if (ffurl_read_complete(rt->rtsp_hd, sdp, request.content_length)
215 < request.content_length) {
217 "Unable to get complete SDP Description in ANNOUNCE\n");
219 av_free(sdp);
220 return AVERROR(EIO);
221 }
222 sdp[request.content_length] = '\0';
223 av_log(s, AV_LOG_VERBOSE, "SDP: %s\n", sdp);
224 ret = ff_sdp_parse(s, sdp);
225 av_free(sdp);
226 if (ret)
227 return ret;
229 return 0;
230 }
232 "Invalid ANNOUNCE Content-Length %d\n", request.content_length);
234 "Invalid Content-Length", request.seq);
235 return AVERROR_INVALIDDATA;
236}
237
239{
240 RTSPState *rt = s->priv_data;
241 RTSPMessageHeader request = { 0 };
242 int ret = 0;
243
244 /* Parsing headers */
245 ret = rtsp_read_request(s, &request, "OPTIONS");
246 if (ret)
247 return ret;
248 rt->seq++;
249 /* Send Reply */
251 "Public: ANNOUNCE, PAUSE, SETUP, TEARDOWN, RECORD\r\n",
252 request.seq);
253 return 0;
254}
255
256static int rtsp_read_setup(AVFormatContext *s, char* host, char *controlurl)
257{
258 RTSPState *rt = s->priv_data;
259 RTSPMessageHeader request = { 0 };
260 int ret = 0;
261 char url[MAX_URL_SIZE];
262 RTSPStream *rtsp_st;
263 char responseheaders[MAX_URL_SIZE];
264 int localport = -1;
265 int transportidx = 0;
266 int streamid = 0;
267
268 ret = rtsp_read_request(s, &request, "SETUP");
269 if (ret)
270 return ret;
271 rt->seq++;
272 if (!request.nb_transports) {
273 av_log(s, AV_LOG_ERROR, "No transport defined in SETUP\n");
274 return AVERROR_INVALIDDATA;
275 }
276 for (transportidx = 0; transportidx < request.nb_transports;
277 transportidx++) {
278 if (!request.transports[transportidx].mode_record ||
279 (request.transports[transportidx].lower_transport !=
281 request.transports[transportidx].lower_transport !=
283 av_log(s, AV_LOG_ERROR, "mode=record/receive not set or transport"
284 " protocol not supported (yet)\n");
285 return AVERROR_INVALIDDATA;
286 }
287 }
288 if (request.nb_transports > 1)
289 av_log(s, AV_LOG_WARNING, "More than one transport not supported, "
290 "using first of all\n");
291 for (streamid = 0; streamid < rt->nb_rtsp_streams; streamid++) {
292 if (!strcmp(rt->rtsp_streams[streamid]->control_url,
293 controlurl))
294 break;
295 }
296 if (streamid == rt->nb_rtsp_streams) {
297 av_log(s, AV_LOG_ERROR, "Unable to find requested track\n");
299 }
300 rtsp_st = rt->rtsp_streams[streamid];
301 localport = rt->rtp_port_min;
302
303 /* check if the stream has already been setup */
304 if (rtsp_st->transport_priv) {
305 if (CONFIG_RTPDEC && rt->transport == RTSP_TRANSPORT_RDT)
307 else if (CONFIG_RTPDEC && rt->transport == RTSP_TRANSPORT_RTP)
309 rtsp_st->transport_priv = NULL;
310 }
311 if (rtsp_st->rtp_handle)
312 ffurl_closep(&rtsp_st->rtp_handle);
313
316 if ((ret = ff_rtsp_open_transport_ctx(s, rtsp_st))) {
318 return ret;
319 }
320 rtsp_st->interleaved_min = request.transports[0].interleaved_min;
321 rtsp_st->interleaved_max = request.transports[0].interleaved_max;
322 snprintf(responseheaders, sizeof(responseheaders), "Transport: "
323 "RTP/AVP/TCP;unicast;mode=record;interleaved=%d-%d"
324 "\r\n", request.transports[0].interleaved_min,
325 request.transports[0].interleaved_max);
326 } else {
327 do {
329 av_dict_set_int(&opts, "buffer_size", rt->buffer_size, 0);
330 ff_url_join(url, sizeof(url), "rtp", NULL, host, localport, NULL);
331 av_log(s, AV_LOG_TRACE, "Opening: %s\n", url);
333 &s->interrupt_callback, &opts,
334 s->protocol_whitelist, s->protocol_blacklist, NULL);
336 if (ret)
337 localport += 2;
338 } while (ret || localport > rt->rtp_port_max);
339 if (localport > rt->rtp_port_max) {
341 return ret;
342 }
343
344 av_log(s, AV_LOG_TRACE, "Listening on: %d\n",
346 if ((ret = ff_rtsp_open_transport_ctx(s, rtsp_st))) {
348 return ret;
349 }
350
351 localport = ff_rtp_get_local_rtp_port(rtsp_st->rtp_handle);
352 snprintf(responseheaders, sizeof(responseheaders), "Transport: "
353 "RTP/AVP/UDP;unicast;mode=record;source=%s;"
354 "client_port=%d-%d;server_port=%d-%d\r\n",
355 host, request.transports[0].client_port_min,
356 request.transports[0].client_port_max, localport,
357 localport + 1);
358 }
359
360 /* Establish sessionid if not previously set */
361 /* Put this in a function? */
362 /* RFC 2326: session id must be at least 8 digits */
363 while (strlen(rt->session_id) < 8)
364 av_strlcatf(rt->session_id, 512, "%u", av_get_random_seed());
365
366 av_strlcatf(responseheaders, sizeof(responseheaders), "Session: %s\r\n",
367 rt->session_id);
368 /* Send Reply */
369 rtsp_send_reply(s, RTSP_STATUS_OK, responseheaders, request.seq);
370
372 return 0;
373}
374
376{
377 RTSPState *rt = s->priv_data;
378 RTSPMessageHeader request = { 0 };
379 int ret = 0;
380 char responseheaders[MAX_URL_SIZE];
381
382 ret = rtsp_read_request(s, &request, "RECORD");
383 if (ret)
384 return ret;
385 ret = check_sessionid(s, &request);
386 if (ret)
387 return ret;
388 rt->seq++;
389 snprintf(responseheaders, sizeof(responseheaders), "Session: %s\r\n",
390 rt->session_id);
391 rtsp_send_reply(s, RTSP_STATUS_OK, responseheaders, request.seq);
392
394 return 0;
395}
396
397static inline int parse_command_line(AVFormatContext *s, const char *line,
398 int linelen, char *uri, int urisize,
399 char *method, int methodsize,
400 enum RTSPMethod *methodcode)
401{
402 RTSPState *rt = s->priv_data;
403 const char *linept, *searchlinept;
404 linept = strchr(line, ' ');
405
406 if (!linept) {
407 av_log(s, AV_LOG_ERROR, "Error parsing method string\n");
408 return AVERROR_INVALIDDATA;
409 }
410
411 if (linept - line > methodsize - 1) {
412 av_log(s, AV_LOG_ERROR, "Method string too long\n");
413 return AVERROR(EIO);
414 }
415 memcpy(method, line, linept - line);
416 method[linept - line] = '\0';
417 linept++;
418 if (!strcmp(method, "ANNOUNCE"))
419 *methodcode = ANNOUNCE;
420 else if (!strcmp(method, "OPTIONS"))
421 *methodcode = OPTIONS;
422 else if (!strcmp(method, "RECORD"))
423 *methodcode = RECORD;
424 else if (!strcmp(method, "SETUP"))
425 *methodcode = SETUP;
426 else if (!strcmp(method, "PAUSE"))
427 *methodcode = PAUSE;
428 else if (!strcmp(method, "TEARDOWN"))
429 *methodcode = TEARDOWN;
430 else
431 *methodcode = UNKNOWN;
432 /* Check method with the state */
433 if (rt->state == RTSP_STATE_IDLE) {
434 if ((*methodcode != ANNOUNCE) && (*methodcode != OPTIONS)) {
435 av_log(s, AV_LOG_ERROR, "Unexpected command in Idle State %s\n",
436 line);
438 }
439 } else if (rt->state == RTSP_STATE_PAUSED) {
440 if ((*methodcode != OPTIONS) && (*methodcode != RECORD)
441 && (*methodcode != SETUP)) {
442 av_log(s, AV_LOG_ERROR, "Unexpected command in Paused State %s\n",
443 line);
445 }
446 } else if (rt->state == RTSP_STATE_STREAMING) {
447 if ((*methodcode != PAUSE) && (*methodcode != OPTIONS)
448 && (*methodcode != TEARDOWN)) {
449 av_log(s, AV_LOG_ERROR, "Unexpected command in Streaming State"
450 " %s\n", line);
452 }
453 } else {
454 av_log(s, AV_LOG_ERROR, "Unexpected State [%d]\n", rt->state);
455 return AVERROR_BUG;
456 }
457
458 searchlinept = strchr(linept, ' ');
459 if (!searchlinept) {
460 av_log(s, AV_LOG_ERROR, "Error parsing message URI\n");
461 return AVERROR_INVALIDDATA;
462 }
463 if (searchlinept - linept > urisize - 1) {
464 av_log(s, AV_LOG_ERROR, "uri string length exceeded buffer size\n");
465 return AVERROR(EIO);
466 }
467 memcpy(uri, linept, searchlinept - linept);
468 uri[searchlinept - linept] = '\0';
469 if (strcmp(rt->control_uri, uri)) {
470 char host[128], path[512], auth[128];
471 int port;
472 char ctl_host[128], ctl_path[512], ctl_auth[128];
473 int ctl_port;
474 av_url_split(NULL, 0, auth, sizeof(auth), host, sizeof(host), &port,
475 path, sizeof(path), uri);
476 av_url_split(NULL, 0, ctl_auth, sizeof(ctl_auth), ctl_host,
477 sizeof(ctl_host), &ctl_port, ctl_path, sizeof(ctl_path),
478 rt->control_uri);
479 if (strcmp(host, ctl_host))
480 av_log(s, AV_LOG_INFO, "Host %s differs from expected %s\n",
481 host, ctl_host);
482 if (strcmp(path, ctl_path) && *methodcode != SETUP)
483 av_log(s, AV_LOG_WARNING, "WARNING: Path %s differs from expected"
484 " %s\n", path, ctl_path);
485 if (*methodcode == ANNOUNCE) {
487 "Updating control URI to %s\n", uri);
488 av_strlcpy(rt->control_uri, uri, sizeof(rt->control_uri));
489 }
490 }
491
492 linept = searchlinept + 1;
493 if (!av_strstart(linept, "RTSP/1.0", NULL)) {
494 av_log(s, AV_LOG_ERROR, "Error parsing protocol or version\n");
496 }
497 return 0;
498}
499
501{
502 RTSPState *rt = s->priv_data;
503 unsigned char rbuf[MAX_URL_SIZE];
504 unsigned char method[10];
505 char uri[500];
506 int ret;
507 int rbuflen = 0;
508 RTSPMessageHeader request = { 0 };
509 enum RTSPMethod methodcode;
510
511 ret = read_line(s, rbuf, sizeof(rbuf), &rbuflen);
512 if (ret < 0)
513 return ret;
514 av_log(s, AV_LOG_TRACE, "Parsing[%d]: %s\n", rbuflen, rbuf);
515 ret = parse_command_line(s, rbuf, rbuflen, uri, sizeof(uri), method,
516 sizeof(method), &methodcode);
517 if (ret) {
518 av_log(s, AV_LOG_ERROR, "RTSP: Unexpected Command\n");
519 return ret;
520 }
521
522 ret = rtsp_read_request(s, &request, method);
523 if (ret)
524 return ret;
525 rt->seq++;
526 if (methodcode == PAUSE) {
528 ret = rtsp_send_reply(s, RTSP_STATUS_OK, NULL , request.seq);
529 // TODO: Missing date header in response
530 } else if (methodcode == OPTIONS) {
532 "Public: ANNOUNCE, PAUSE, SETUP, TEARDOWN, "
533 "RECORD\r\n", request.seq);
534 } else if (methodcode == TEARDOWN) {
536 ret = rtsp_send_reply(s, RTSP_STATUS_OK, NULL , request.seq);
537 }
538 return ret;
539}
540
542{
543 RTSPState *rt = s->priv_data;
544 RTSPMessageHeader reply1, *reply = &reply1;
545 int i;
546 char cmd[MAX_URL_SIZE];
547
548 av_log(s, AV_LOG_DEBUG, "hello state=%d\n", rt->state);
549 rt->nb_byes = 0;
550
552 for (i = 0; i < rt->nb_rtsp_streams; i++) {
553 RTSPStream *rtsp_st = rt->rtsp_streams[i];
554 /* Try to initialize the connection state in a
555 * potential NAT router by sending dummy packets.
556 * RTP/RTCP dummy packets are used for RDT, too.
557 */
558 if (rtsp_st->rtp_handle &&
559 !(rt->server_type == RTSP_SERVER_WMS && i > 1))
561 }
562 }
563 if (!(rt->server_type == RTSP_SERVER_REAL && rt->need_subscription)) {
564 if (rt->transport == RTSP_TRANSPORT_RTP) {
565 for (i = 0; i < rt->nb_rtsp_streams; i++) {
566 RTSPStream *rtsp_st = rt->rtsp_streams[i];
567 RTPDemuxContext *rtpctx = rtsp_st->transport_priv;
568 if (!rtpctx)
569 continue;
573 rtpctx->base_timestamp = 0;
574 rtpctx->timestamp = 0;
575 rtpctx->unwrapped_timestamp = 0;
576 rtpctx->rtcp_ts_offset = 0;
577 }
578 }
579 if (rt->state == RTSP_STATE_PAUSED) {
580 cmd[0] = 0;
581 } else {
582 snprintf(cmd, sizeof(cmd),
583 "Range: npt=%"PRId64".%03"PRId64"-\r\n",
585 rt->seek_timestamp / (AV_TIME_BASE / 1000) % 1000);
586 }
587 ff_rtsp_send_cmd(s, "PLAY", rt->control_uri, cmd, reply, NULL);
588 if (reply->status_code != RTSP_STATUS_OK) {
589 return ff_rtsp_averror(reply->status_code, -1);
590 }
591 if (rt->transport == RTSP_TRANSPORT_RTP &&
592 reply->range_start != AV_NOPTS_VALUE) {
593 for (i = 0; i < rt->nb_rtsp_streams; i++) {
594 RTSPStream *rtsp_st = rt->rtsp_streams[i];
595 RTPDemuxContext *rtpctx = rtsp_st->transport_priv;
596 AVStream *st = NULL;
597 if (!rtpctx || rtsp_st->stream_index < 0)
598 continue;
599
600 st = s->streams[rtsp_st->stream_index];
601 rtpctx->range_start_offset =
603 st->time_base);
604 }
605 }
606 }
608 return 0;
609}
610
611/* pause the stream */
613{
614 RTSPState *rt = s->priv_data;
615 RTSPMessageHeader reply1, *reply = &reply1;
616
617 if (rt->state != RTSP_STATE_STREAMING)
618 return 0;
619 else if (!(rt->server_type == RTSP_SERVER_REAL && rt->need_subscription)) {
620 ff_rtsp_send_cmd(s, "PAUSE", rt->control_uri, NULL, reply, NULL);
621 if (reply->status_code != RTSP_STATUS_OK) {
622 return ff_rtsp_averror(reply->status_code, -1);
623 }
624 }
626 return 0;
627}
628
631{
632 switch (state) {
634 return rtsp_read_play(s);
636 return rtsp_read_pause(s);
637 default:
638 return AVERROR(ENOTSUP);
639 }
640}
641
642static char *dict_to_headers(AVDictionary *headers)
643{
644 char *buf;
645 AVBPrint bprint;
647
649 while ((header = av_dict_iterate(headers, header))) {
650 av_bprintf(&bprint, "%s: %s\r\n", header->key, header->value);
651 }
652
653 av_bprint_finalize(&bprint, &buf);
654 return buf;
655}
656
657static int rtsp_submit_command(struct AVFormatContext *s, enum AVFormatCommandID id, void *data)
658{
660 return AVERROR(ENOTSUP);
661 if (!data)
662 return AVERROR(EINVAL);
663
664 RTSPState *rt = s->priv_data;
666
667 if (rt->state == RTSP_STATE_IDLE)
668 // Not ready to send a SET_PARAMETERS command yet
669 return AVERROR(EAGAIN);
670
671 av_log(s, AV_LOG_DEBUG, "Sending SET_PARAMETER command to %s\n", rt->control_uri);
672 char *headers = dict_to_headers(req->headers);
673
674 int ret = ff_rtsp_send_cmd_with_content_async_stored(s, "SET_PARAMETER",
675 rt->control_uri, headers, req->body, req->body_len);
676 av_free(headers);
677
678 if (ret != 0)
679 av_log(s, AV_LOG_ERROR, "Failure sending SET_PARAMETER command: %s\n", av_err2str(ret));
680
681 return ret;
682}
683
684static int rtsp_read_command_reply(AVFormatContext *s, enum AVFormatCommandID id, void **data_out)
685{
687 return AVERROR(ENOTSUP);
688 if (!data_out)
689 return AVERROR(EINVAL);
690
691 unsigned char *body;
692 RTSPMessageHeader *reply;
693 int ret = ff_rtsp_read_reply_async_stored(s, &reply, &body);
694 if (ret < 0)
695 return ret;
696
697 AVRTSPResponse *res = av_malloc(sizeof(*res));
698 if (!res) {
699 av_free(body);
700 av_free(reply);
701 return AVERROR(ENOMEM);
702 }
703
704 res->status_code = reply->status_code;
705 res->body_len = reply->content_length;
706 res->body = body;
707
708 res->reason = av_strdup(reply->reason);
709 if (!res->reason) {
710 av_free(res->body);
711 av_free(res);
712 av_free(reply);
713 return AVERROR(ENOMEM);
714 }
715
716 av_free(reply);
717 *data_out = res;
718 return 0;
719}
720
723 enum AVFormatCommandID id, void *data)
724{
725 switch (opt) {
727 return rtsp_submit_command(s, id, data);
729 return rtsp_read_command_reply(s, id, data);
730 default:
731 av_unreachable("Invalid command option");
732 }
733}
734
736{
737 RTSPState *rt = s->priv_data;
738 char cmd[MAX_URL_SIZE];
739 unsigned char *content = NULL;
740 int ret;
741
742 /* describe the stream */
743 snprintf(cmd, sizeof(cmd),
744 "Accept: application/sdp\r\n");
745 if (rt->server_type == RTSP_SERVER_REAL) {
746 /**
747 * The Require: attribute is needed for proper streaming from
748 * Realmedia servers.
749 */
750 av_strlcat(cmd,
751 "Require: com.real.retain-entity-for-setup\r\n",
752 sizeof(cmd));
753 }
754 ff_rtsp_send_cmd(s, "DESCRIBE", rt->control_uri, cmd, reply, &content);
755 if (reply->status_code != RTSP_STATUS_OK) {
756 av_freep(&content);
758 }
759 if (!content)
760 return AVERROR_INVALIDDATA;
761
762 av_log(s, AV_LOG_VERBOSE, "SDP:\n%s\n", content);
763 /* now we got the SDP description, we parse it */
764 ret = ff_sdp_parse(s, (const char *)content);
765 av_freep(&content);
766 if (ret < 0)
767 return ret;
768
769 return 0;
770}
771
773{
774 RTSPState *rt = s->priv_data;
775 char proto[128], host[128], path[512], auth[128];
776 char uri[500];
777 int port;
778 int default_port = RTSP_DEFAULT_PORT;
779 char tcpname[500];
780 const char *lower_proto = "tcp";
781 unsigned char rbuf[MAX_URL_SIZE];
782 unsigned char method[10];
783 int rbuflen = 0;
784 int ret;
785 enum RTSPMethod methodcode;
786
787 if ((ret = ff_network_init()) < 0)
788 return ret;
789
790 /* extract hostname and port */
791 av_url_split(proto, sizeof(proto), auth, sizeof(auth), host, sizeof(host),
792 &port, path, sizeof(path), s->url);
793
794 /* ff_url_join. No authorization by now (NULL) */
795 ff_url_join(rt->control_uri, sizeof(rt->control_uri), proto, NULL, host,
796 port, "%s", path);
797
798 if (!strcmp(proto, "rtsps")) {
799 lower_proto = "tls";
800 default_port = RTSPS_DEFAULT_PORT;
801 }
802
803 if (port < 0)
804 port = default_port;
805
806 /* Create TCP connection */
807 ff_url_join(tcpname, sizeof(tcpname), lower_proto, NULL, host, port,
808 "?listen&listen_timeout=%d", rt->initial_timeout * 1000);
809
810 if (ret = ffurl_open_whitelist(&rt->rtsp_hd, tcpname, AVIO_FLAG_READ_WRITE,
811 &s->interrupt_callback, NULL,
812 s->protocol_whitelist, s->protocol_blacklist, NULL)) {
813 av_log(s, AV_LOG_ERROR, "Unable to open RTSP for listening\n");
814 goto fail;
815 }
817 rt->rtsp_hd_out = rt->rtsp_hd;
818 for (;;) { /* Wait for incoming RTSP messages */
819 ret = read_line(s, rbuf, sizeof(rbuf), &rbuflen);
820 if (ret < 0)
821 goto fail;
822 av_log(s, AV_LOG_TRACE, "Parsing[%d]: %s\n", rbuflen, rbuf);
823 ret = parse_command_line(s, rbuf, rbuflen, uri, sizeof(uri), method,
824 sizeof(method), &methodcode);
825 if (ret) {
826 av_log(s, AV_LOG_ERROR, "RTSP: Unexpected Command\n");
827 goto fail;
828 }
829
830 if (methodcode == ANNOUNCE) {
831 ret = rtsp_read_announce(s);
833 } else if (methodcode == OPTIONS) {
834 ret = rtsp_read_options(s);
835 } else if (methodcode == RECORD) {
836 ret = rtsp_read_record(s);
837 if (!ret)
838 return 0; // We are ready for streaming
839 } else if (methodcode == SETUP)
840 ret = rtsp_read_setup(s, host, uri);
841 if (ret) {
843 goto fail;
844 }
845 }
846fail:
850 return ret;
851}
852
853static int rtsp_probe(const AVProbeData *p)
854{
855 if (
856#if CONFIG_TLS_PROTOCOL
857 av_strstart(p->filename, "rtsps:", NULL) ||
858#endif
859 av_strstart(p->filename, "satip:", NULL) ||
860 av_strstart(p->filename, "rtsp:", NULL))
861 return AVPROBE_SCORE_MAX;
862 return 0;
863}
864
866{
867 RTSPState *rt = s->priv_data;
868 int ret;
869
870 if (rt->initial_timeout > 0)
872
873 if (rt->rtsp_flags & RTSP_FLAG_LISTEN) {
874 ret = rtsp_listen(s);
875 if (ret)
876 return ret;
877 } else {
878 ret = ff_rtsp_connect(s);
879 if (ret)
880 return ret;
881
882 rt->real_setup_cache = !s->nb_streams ? NULL :
883 av_calloc(s->nb_streams, 2 * sizeof(*rt->real_setup_cache));
884 if (!rt->real_setup_cache && s->nb_streams) {
885 ret = AVERROR(ENOMEM);
886 goto fail;
887 }
888 rt->real_setup = rt->real_setup_cache + s->nb_streams;
889
890 if (rt->initial_pause) {
891 /* do not start immediately */
892 } else {
893 ret = rtsp_read_play(s);
894 if (ret < 0)
895 goto fail;
896 }
897 }
898
899 return 0;
900
901fail:
903 return ret;
904}
905
907 uint8_t *buf, int buf_size)
908{
909 RTSPState *rt = s->priv_data;
910 int id, len, i, ret;
911 RTSPStream *rtsp_st;
912
913 av_log(s, AV_LOG_TRACE, "tcp_read_packet:\n");
914redo:
915 for (;;) {
916 RTSPMessageHeader reply;
917
918 ret = ff_rtsp_read_reply(s, &reply, NULL, 1, NULL);
919 if (ret < 0)
920 return ret;
921 if (ret == 1) /* received '$' */
922 break;
923 /* XXX: parse message */
924 if (rt->state != RTSP_STATE_STREAMING)
925 return 0;
926 }
927 ret = ffurl_read_complete(rt->rtsp_hd, buf, 3);
928 if (ret != 3)
929 return AVERROR(EIO);
930 rt->pending_packet = 0;
931 id = buf[0];
932 len = AV_RB16(buf + 1);
933 av_log(s, AV_LOG_TRACE, "id=%d len=%d\n", id, len);
934 if (len > buf_size || len < 8)
935 goto redo;
936 /* get the data */
937 ret = ffurl_read_complete(rt->rtsp_hd, buf, len);
938 if (ret != len)
939 return AVERROR(EIO);
940 if (rt->transport == RTSP_TRANSPORT_RDT &&
941 (ret = ff_rdt_parse_header(buf, len, &id, NULL, NULL, NULL, NULL)) < 0)
942 return ret;
943
944 /* find the matching stream */
945 for (i = 0; i < rt->nb_rtsp_streams; i++) {
946 rtsp_st = rt->rtsp_streams[i];
947 if (id >= rtsp_st->interleaved_min &&
948 id <= rtsp_st->interleaved_max)
949 goto found;
950 }
951 goto redo;
952found:
953 *prtsp_st = rtsp_st;
954 return len;
955}
956
958{
959 RTSPState *rt = s->priv_data;
960 char host[1024];
961 int port;
962
963 av_url_split(NULL, 0, NULL, 0, host, sizeof(host), &port, NULL, 0,
964 s->url);
967 rt->real_challenge);
968}
969
971{
972 RTSPState *rt = s->priv_data;
973 int ret;
974 RTSPMessageHeader reply1, *reply = &reply1;
975 char cmd[MAX_URL_SIZE];
976
977retry:
978 if (rt->server_type == RTSP_SERVER_REAL) {
979 int i;
980
981 for (i = 0; i < s->nb_streams; i++)
982 rt->real_setup[i] = s->streams[i]->discard;
983
984 if (!rt->need_subscription) {
985 if (memcmp (rt->real_setup, rt->real_setup_cache,
986 sizeof(enum AVDiscard) * s->nb_streams)) {
987 snprintf(cmd, sizeof(cmd),
988 "Unsubscribe: %s\r\n",
990 ff_rtsp_send_cmd(s, "SET_PARAMETER", rt->control_uri,
991 cmd, reply, NULL);
992 if (reply->status_code != RTSP_STATUS_OK)
994 rt->need_subscription = 1;
995 }
996 }
997
998 if (rt->need_subscription) {
999 int r, rule_nr, first = 1;
1000
1001 memcpy(rt->real_setup_cache, rt->real_setup,
1002 sizeof(enum AVDiscard) * s->nb_streams);
1003 rt->last_subscription[0] = 0;
1004
1005 snprintf(cmd, sizeof(cmd),
1006 "Subscribe: ");
1007 for (i = 0; i < rt->nb_rtsp_streams; i++) {
1008 rule_nr = 0;
1009 for (r = 0; r < s->nb_streams; r++) {
1010 if (s->streams[r]->id == i) {
1011 if (s->streams[r]->discard != AVDISCARD_ALL) {
1012 if (!first)
1014 sizeof(rt->last_subscription));
1017 sizeof(rt->last_subscription), i, rule_nr);
1018 first = 0;
1019 }
1020 rule_nr++;
1021 }
1022 }
1023 }
1024 av_strlcatf(cmd, sizeof(cmd), "%s\r\n", rt->last_subscription);
1025 ff_rtsp_send_cmd(s, "SET_PARAMETER", rt->control_uri,
1026 cmd, reply, NULL);
1027 if (reply->status_code != RTSP_STATUS_OK)
1029 rt->need_subscription = 0;
1030
1031 if (rt->state == RTSP_STATE_STREAMING)
1032 rtsp_read_play (s);
1033 }
1034 }
1035
1036 ret = ff_rtsp_fetch_packet(s, pkt);
1037 if (ret < 0) {
1038 if (ret == AVERROR(ETIMEDOUT) && !rt->packets) {
1041 RTSPMessageHeader reply1, *reply = &reply1;
1042 av_log(s, AV_LOG_WARNING, "UDP timeout, retrying with TCP\n");
1043 if (rtsp_read_pause(s) != 0)
1044 return -1;
1045 // TEARDOWN is required on Real-RTSP, but might make
1046 // other servers close the connection.
1047 if (rt->server_type == RTSP_SERVER_REAL)
1048 ff_rtsp_send_cmd(s, "TEARDOWN", rt->control_uri, NULL,
1049 reply, NULL);
1050 rt->session_id[0] = '\0';
1051 if (resetup_tcp(s) == 0) {
1052 rt->state = RTSP_STATE_IDLE;
1053 rt->need_subscription = 1;
1054 if (rtsp_read_play(s) != 0)
1055 return -1;
1056 goto retry;
1057 }
1058 }
1059 }
1060 return ret;
1061 }
1062 rt->packets++;
1063
1064 if (!(rt->rtsp_flags & RTSP_FLAG_LISTEN)) {
1065 /* send dummy request to keep TCP connection alive */
1066 if ((av_gettime_relative() - rt->last_cmd_time) / 1000000 >= rt->timeout / 2 ||
1067 rt->auth_state.stale) {
1068 if (rt->server_type == RTSP_SERVER_WMS ||
1069 (rt->server_type != RTSP_SERVER_REAL &&
1071 ff_rtsp_send_cmd_async(s, "GET_PARAMETER", rt->control_uri, NULL);
1072 } else {
1073 ff_rtsp_send_cmd_async(s, "OPTIONS", rt->control_uri, NULL);
1074 }
1075 /* The stale flag should be reset when creating the auth response in
1076 * ff_rtsp_send_cmd_async, but reset it here just in case we never
1077 * called the auth code (if we didn't have any credentials set). */
1078 rt->auth_state.stale = 0;
1079 }
1080 }
1081
1082 return 0;
1083}
1084
1085static int rtsp_read_seek(AVFormatContext *s, int stream_index,
1086 int64_t timestamp, int flags)
1087{
1088 RTSPState *rt = s->priv_data;
1089 int ret;
1090
1091 rt->seek_timestamp = av_rescale_q(timestamp,
1092 s->streams[stream_index]->time_base,
1094 switch(rt->state) {
1095 default:
1096 case RTSP_STATE_IDLE:
1097 break;
1099 if ((ret = rtsp_read_pause(s)) != 0)
1100 return ret;
1102 if ((ret = rtsp_read_play(s)) != 0)
1103 return ret;
1104 break;
1105 case RTSP_STATE_PAUSED:
1106 rt->state = RTSP_STATE_IDLE;
1107 break;
1108 }
1109 return 0;
1110}
1111
1113 .class_name = "RTSP demuxer",
1114 .item_name = av_default_item_name,
1115 .option = ff_rtsp_options,
1116 .version = LIBAVUTIL_VERSION_INT,
1117};
1118
1120 .p.name = "rtsp",
1121 .p.long_name = NULL_IF_CONFIG_SMALL("RTSP input"),
1122 .p.flags = AVFMT_NOFILE,
1123 .p.priv_class = &rtsp_demuxer_class,
1124 .priv_data_size = sizeof(RTSPState),
1130 .read_set_state = rtsp_read_set_state,
1131 .handle_command = rtsp_handle_command,
1132};
const FFInputFormat ff_rtsp_demuxer
Definition rtspdec.c:1119
static AVDictionary * opts
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_unreachable(msg)
Asserts that are used as compiler optimization hints depending upon ASSERT_LEVEL and NBDEBUG.
Definition avassert.h:109
Main libavformat public API header.
#define AVPROBE_SCORE_MAX
maximum score
Definition avformat.h:485
#define AVFMT_NOFILE
Demuxer will use avio_open, no opened file should be provided by the caller.
Definition avformat.h:490
int ffurl_open_whitelist(URLContext **puc, const char *filename, int flags, const AVIOInterruptCB *int_cb, AVDictionary **options, const char *whitelist, const char *blacklist, URLContext *parent)
Create an URLContext for accessing to the resource indicated by url, and open it.
Definition avio.c:466
int ffurl_closep(URLContext **hh)
Close the resource accessed by the URLContext h, and free the memory used by it.
Definition avio.c:663
int ffurl_read_complete(URLContext *h, unsigned char *buf, int size)
Read as many bytes as possible (up to size), calling the read function multiple times if necessary.
Definition avio.c:632
#define AVIO_FLAG_READ_WRITE
read-write pseudo flag
Definition avio.h:619
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
size_t av_strlcatf(char *dst, size_t size, const char *fmt,...)
Definition avstring.c:103
void av_bprintf(AVBPrint *buf, const char *fmt,...)
Definition bprint.c:121
void av_bprint_init(AVBPrint *buf, unsigned size_init, unsigned size_max)
Definition bprint.c:68
#define AV_BPRINT_SIZE_UNLIMITED
#define flags(name, subs,...)
Definition cbs_h264.c:74
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
static int read_probe(const AVProbeData *p)
Definition cdg.c:30
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
FFInputFormatStreamState
Input format stream state The stream states to be used for FFInputFormat::read_set_state.
Definition demux.h:51
@ FF_INFMT_STATE_PLAY
Definition demux.h:52
@ FF_INFMT_STATE_PAUSE
Definition demux.h:53
FFInputFormatCommandOption
Command handling options Different options influencing the behaviour of the FFInputFormat::handle_com...
Definition demux.h:61
@ FF_INFMT_COMMAND_GET_REPLY
Definition demux.h:63
@ FF_INFMT_COMMAND_SUBMIT
Definition demux.h:62
static AVPacket * pkt
enum AVCodecID id
Definition dts2pts.c:607
static struct @346255127015250356166251341105367306144006377143 state
static int read_header(FFV1Context *f, RangeCoder *c)
Definition ffv1dec.c:578
@ UNKNOWN
Definition ftp.c:39
#define fail
Definition test.h:479
AVDiscard
Definition defs.h:237
@ AVDISCARD_ALL
discard all
Definition defs.h:246
AVFormatCommandID
Command IDs that can be sent to the demuxer.
Definition avformat.h:2522
@ AVFORMAT_COMMAND_RTSP_SET_PARAMETER
Send a RTSP SET_PARAMETER request to the server.
Definition avformat.h:2533
void av_url_split(char *proto, int proto_size, char *authorization, int authorization_size, char *hostname, int hostname_size, int *port_ptr, char *path, int path_size, const char *url)
Split a URL string into components.
Definition utils.c:361
int av_bprint_finalize(AVBPrint *buf, char **ret_str)
Finalize a print buffer.
Definition bprint.c:234
uint32_t av_get_random_seed(void)
Get a seed to use in conjunction with random functions.
void av_dict_free(AVDictionary **pm)
Free all the memory allocated for an AVDictionary struct and all keys and values.
Definition dict.c:233
const AVDictionaryEntry * av_dict_iterate(const AVDictionary *m, const AVDictionaryEntry *prev)
Iterate over a dictionary.
Definition dict.c:42
int av_dict_set_int(AVDictionary **pm, const char *key, int64_t value, int flags)
Convenience wrapper for av_dict_set() that converts the value to a string and stores it.
Definition dict.c:177
#define AVERROR_PROTOCOL_NOT_FOUND
Protocol not found.
Definition error.h:65
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
Definition error.h:52
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR_STREAM_NOT_FOUND
Stream not found.
Definition error.h:67
#define AVERROR_EOF
End of file.
Definition error.h:57
#define av_err2str(errnum)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition error.h:122
#define AVERROR(e)
Definition error.h:45
#define AVERROR_OPTION_NOT_FOUND
Option not found.
Definition error.h:63
#define AV_LOG_TRACE
Extremely verbose debugging, useful for libav* development.
Definition log.h:236
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition log.h:231
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition log.h:216
#define AV_LOG_VERBOSE
Detailed information.
Definition log.h:226
#define AV_LOG_INFO
Standard information.
Definition log.h:221
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
const char * av_default_item_name(void *ptr)
Return the context name.
Definition log.c:241
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
size_t av_strlcat(char *dst, const char *src, size_t size)
Append the string src to the string dst, but to a total length of no more than size - 1 bytes,...
Definition avstring.c:95
int av_strstart(const char *str, const char *pfx, const char **ptr)
Return non-zero if pfx is a prefix of str.
Definition avstring.c:36
size_t av_strlcpy(char *dst, const char *src, size_t size)
Copy the string src to dst, but no more than size - 1 bytes, and null-terminate dst.
Definition avstring.c:85
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition avutil.h:247
#define AV_TIME_BASE
Internal time base represented as integer.
Definition avutil.h:253
#define AV_TIME_BASE_Q
Internal time base represented as fractional value.
Definition avutil.h:263
#define LIBAVUTIL_VERSION_INT
Definition version.h:85
int index
Definition gxfenc.c:90
#define r
Definition input.c:42
#define AV_RB16(p)
#define MAX_URL_SIZE
Definition internal.h:30
Libavformat version macros.
#define LIBAVFORMAT_IDENT
Definition version.h:45
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:97
static av_cold int read_close(AVFormatContext *ctx)
Definition libcdio.c:143
static int read_seek(AVFormatContext *ctx, int stream_index, int64_t timestamp, int flags)
Definition libcdio.c:151
static void body(uint32_t ABCD[4], const uint8_t *src, size_t nblocks)
Definition md5.c:103
void * av_calloc(size_t nmemb, size_t size)
Definition mem.c:370
Memory handling functions.
char * host
Definition mscl.c:249
const char data[16]
Definition mxf.c:149
int ff_network_init(void)
Initialize the network subsystem.
Definition network.c:63
void ff_network_close(void)
Definition network.c:121
#define av_strdup(s)
Definition ops_static.c:55
#define av_malloc(s)
Definition ops_static.c:52
miscellaneous OS support macros and functions.
void ff_rdt_parse_close(RDTDemuxContext *s)
Definition rdt.c:80
int ff_rdt_parse_header(const uint8_t *buf, int len, int *pset_id, int *pseq_no, int *pstream_id, int *pis_keyframe, uint32_t *ptimestamp)
Actual data handling.
Definition rdt.c:192
void ff_rdt_subscribe_rule(char *cmd, int size, int stream_nr, int rule_nr)
Add subscription information to Subscribe parameter string.
Definition rdt.c:388
void ff_rtp_parse_close(RTPDemuxContext *s)
Definition rtpdec.c:941
void ff_rtp_send_punch_packets(URLContext *rtp_handle)
Send a dummy packet on both port pairs to set up the connection state in potential NAT routers,...
Definition rtpdec.c:415
void ff_rtp_reset_packet_queue(RTPDemuxContext *s)
Definition rtpdec.c:764
int ff_rtp_get_local_rtp_port(URLContext *h)
Return the local rtp port used by the RTP connection.
Definition rtpproto.c:514
int ff_rtsp_open_transport_ctx(AVFormatContext *s, RTSPStream *rtsp_st)
Open RTSP transport context.
Definition rtsp.c:869
const AVOption ff_rtsp_options[]
Definition rtsp.c:87
void ff_rtsp_close_streams(AVFormatContext *s)
Close and free all streams within the RTSP (de)muxer.
Definition rtsp.c:833
void ff_rtsp_undo_setup(AVFormatContext *s, int send_packets)
Undo the effect of ff_rtsp_make_setup_request, close the transport_priv and rtp_handle fields.
Definition rtsp.c:800
int ff_sdp_parse(AVFormatContext *s, const char *content)
Parse an SDP description of streams by populating an RTSPState struct within the AVFormatContext; als...
#define RTSP_FLAG_LISTEN
Wait for incoming connections.
Definition rtsp.h:471
@ RTSP_SERVER_WMS
Windows Media server.
Definition rtsp.h:216
@ RTSP_SERVER_REAL
Realmedia-style server.
Definition rtsp.h:215
int ff_rtsp_send_cmd(AVFormatContext *s, const char *method, const char *url, const char *headers, RTSPMessageHeader *reply, unsigned char **content_ptr)
Send a command to the RTSP server and wait for the reply.
int ff_rtsp_send_cmd_with_content_async_stored(AVFormatContext *s, const char *method, const char *url, const char *headers, const unsigned char *send_content, int send_content_length)
Send a command to the RTSP server, storing the reply on future reads.
int ff_rtsp_read_reply(AVFormatContext *s, RTSPMessageHeader *reply, unsigned char **content_ptr, int return_on_interleaved_data, const char *method)
Read a RTSP message from the server, or prepare to read data packets if we're reading data interleave...
int ff_rtsp_make_setup_request(AVFormatContext *s, const char *host, int port, int lower_transport, const char *real_challenge)
Do the SETUP requests for each stream for the chosen lower transport mode.
int ff_rtsp_send_cmd_async(AVFormatContext *s, const char *method, const char *url, const char *headers)
Send a command to the RTSP server without waiting for the reply.
int ff_rtsp_fetch_packet(AVFormatContext *s, AVPacket *pkt)
Receive one packet from the RTSPStreams set up in the AVFormatContext (which should contain a RTSPSta...
void ff_rtsp_parse_line(AVFormatContext *s, RTSPMessageHeader *reply, const char *buf, RTSPState *rt, const char *method)
#define RTSPS_DEFAULT_PORT
Definition rtsp.h:76
@ RTSP_LOWER_TRANSPORT_TCP
TCP; interleaved in RTSP.
Definition rtsp.h:41
@ RTSP_LOWER_TRANSPORT_UDP
UDP/unicast.
Definition rtsp.h:40
int ff_rtsp_read_reply_async_stored(AVFormatContext *s, RTSPMessageHeader **reply, unsigned char **content_ptr)
Retrieve a previously stored RTSP reply message from the server.
void ff_rtsp_close_connections(AVFormatContext *s)
Close all connection handles within the RTSP (de)muxer.
@ RTSP_STATE_SEEKING
initialized, requesting a seek
Definition rtsp.h:206
@ RTSP_STATE_STREAMING
initialized and sending/receiving data
Definition rtsp.h:204
@ RTSP_STATE_PAUSED
initialized, but not receiving data
Definition rtsp.h:205
@ RTSP_STATE_IDLE
not initialized
Definition rtsp.h:203
#define SDP_MAX_SIZE
Definition rtsp.h:81
#define RTSP_DEFAULT_PORT
Definition rtsp.h:75
int ff_rtsp_connect(AVFormatContext *s)
Connect to the RTSP server and set up the individual media streams.
@ RTSP_TRANSPORT_RTP
Standards-compliant RTP.
Definition rtsp.h:60
@ RTSP_TRANSPORT_RDT
Realmedia Data Transport.
Definition rtsp.h:61
RTSPStatusCode
RTSP handling.
Definition rtspcodes.h:31
@ RTSP_STATUS_METHOD
Definition rtspcodes.h:47
@ RTSP_STATUS_SESSION
Definition rtspcodes.h:60
@ RTSP_STATUS_OK
Definition rtspcodes.h:33
@ RTSP_STATUS_VERSION
Definition rtspcodes.h:74
@ RTSP_STATUS_BANDWIDTH
Definition rtspcodes.h:59
@ RTSP_STATUS_STATE
Definition rtspcodes.h:61
@ RTSP_STATUS_SERVICE
Definition rtspcodes.h:72
@ RTSP_STATUS_TRANSPORT
Definition rtspcodes.h:67
@ RTSP_STATUS_AGGREGATE
Definition rtspcodes.h:65
@ RTSP_STATUS_ONLY_AGGREGATE
Definition rtspcodes.h:66
@ RTSP_STATUS_INTERNAL
Definition rtspcodes.h:69
static int ff_rtsp_averror(enum RTSPStatusCode status_code, int default_averror)
Definition rtspcodes.h:144
RTSPMethod
Definition rtspcodes.h:129
@ TEARDOWN
Definition rtspcodes.h:136
@ OPTIONS
Definition rtspcodes.h:132
@ ANNOUNCE
Definition rtspcodes.h:131
@ PAUSE
Definition rtspcodes.h:135
@ SETUP
Definition rtspcodes.h:133
@ RECORD
Definition rtspcodes.h:140
static int rtsp_probe(const AVProbeData *p)
Definition rtspdec.c:853
static int resetup_tcp(AVFormatContext *s)
Definition rtspdec.c:957
static int check_sessionid(AVFormatContext *s, RTSPMessageHeader *request)
Definition rtspdec.c:143
int ff_rtsp_setup_input_streams(AVFormatContext *s, RTSPMessageHeader *reply)
Get the description of the stream and set up the RTSPStream child objects.
Definition rtspdec.c:735
static int rtsp_read_setup(AVFormatContext *s, char *host, char *controlurl)
Definition rtspdec.c:256
static int rtsp_read_close(AVFormatContext *s)
Definition rtspdec.c:65
static int rtsp_read_options(AVFormatContext *s)
Definition rtspdec.c:238
static int read_line(AVFormatContext *s, char *rbuf, const int rbufsize, int *rbuflen)
Definition rtspdec.c:91
static int parse_command_line(AVFormatContext *s, const char *line, int linelen, char *uri, int urisize, char *method, int methodsize, enum RTSPMethod *methodcode)
Definition rtspdec.c:397
static int rtsp_read_set_state(AVFormatContext *s, enum FFInputFormatStreamState state)
Definition rtspdec.c:629
static int rtsp_send_reply(AVFormatContext *s, enum RTSPStatusCode code, const char *extracontent, uint16_t seq)
Definition rtspdec.c:116
static int rtsp_read_request(AVFormatContext *s, RTSPMessageHeader *request, const char *method)
Definition rtspdec.c:161
static int rtsp_read_record(AVFormatContext *s)
Definition rtspdec.c:375
static int rtsp_read_header(AVFormatContext *s)
Definition rtspdec.c:865
static const AVClass rtsp_demuxer_class
Definition rtspdec.c:1112
static int rtsp_handle_command(struct AVFormatContext *s, enum FFInputFormatCommandOption opt, enum AVFormatCommandID id, void *data)
Definition rtspdec.c:721
int ff_rtsp_parse_streaming_commands(AVFormatContext *s)
Parse RTSP commands (OPTIONS, PAUSE and TEARDOWN) during streaming in listen mode.
Definition rtspdec.c:500
static const struct RTSPStatusMessage status_messages[]
static int rtsp_read_announce(AVFormatContext *s)
Definition rtspdec.c:191
static int rtsp_read_play(AVFormatContext *s)
Definition rtspdec.c:541
static int rtsp_read_command_reply(AVFormatContext *s, enum AVFormatCommandID id, void **data_out)
Definition rtspdec.c:684
static int rtsp_submit_command(struct AVFormatContext *s, enum AVFormatCommandID id, void *data)
Definition rtspdec.c:657
int ff_rtsp_tcp_read_packet(AVFormatContext *s, RTSPStream **prtsp_st, uint8_t *buf, int buf_size)
Receive one RTP packet from an TCP interleaved RTSP stream.
Definition rtspdec.c:906
static int rtsp_listen(AVFormatContext *s)
Definition rtspdec.c:772
static int rtsp_read_pause(AVFormatContext *s)
Definition rtspdec.c:612
static int rtsp_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition rtspdec.c:970
static int rtsp_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
Definition rtspdec.c:1085
static char * dict_to_headers(AVDictionary *headers)
Definition rtspdec.c:642
#define RTSP_TEARDOWN_TIMEOUT
Definition rtspdec.c:63
static const uint8_t header[24]
Definition sdr2.c:68
const uint8_t * code
Definition spdifenc.c:433
Describe the class of an AVClass context structure.
Definition log.h:76
Format I/O context.
Definition avformat.h:1337
This structure stores compressed data.
Definition packet.h:580
This structure contains the data a format has to probe a file.
Definition avformat.h:473
uint64_t ntp_timestamp
NTP time when the report was sent.
Definition defs.h:361
size_t body_len
Body payload size.
Definition avformat.h:2545
AVDictionary * headers
Headers sent in the request to the server.
Definition avformat.h:2540
char * body
Body payload.
Definition avformat.h:2550
int status_code
Response status code from server.
Definition avformat.h:2557
size_t body_len
Body payload size.
Definition avformat.h:2568
unsigned char * body
Body payload.
Definition avformat.h:2573
char * reason
Reason phrase from the server, describing the status in a human-readable way.
Definition avformat.h:2563
Stream structure.
Definition avformat.h:768
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented.
Definition avformat.h:807
int stale
Auth ok, but needs to be resent with a new nonce.
Definition httpauth.h:71
uint64_t first_rtcp_ntp_time
Definition rtpdec.h:177
uint32_t base_timestamp
Definition rtpdec.h:155
int64_t unwrapped_timestamp
Definition rtpdec.h:156
int64_t rtcp_ts_offset
Definition rtpdec.h:178
AVRTCPSenderReport last_sr
Last RTCP SR data.
Definition rtpdec.h:179
int64_t range_start_offset
Definition rtpdec.h:157
uint32_t timestamp
Definition rtpdec.h:154
This describes the server response to each RTSP command.
Definition rtsp.h:129
char reason[256]
The "reason" is meant to specify better the meaning of the error code returned.
Definition rtsp.h:184
int64_t range_start
Time range of the streams that the server will stream.
Definition rtsp.h:140
char session_id[512]
the "Session:" field.
Definition rtsp.h:150
enum RTSPStatusCode status_code
response code from server
Definition rtsp.h:133
int seq
sequence number
Definition rtsp.h:146
char content_type[64]
Content type header.
Definition rtsp.h:189
RTSPTransportField transports[RTSP_MAX_TRANSPORTS]
describes the complete "Transport:" line of the server in response to a SETUP RTSP command by the cli...
Definition rtsp.h:144
int nb_transports
number of items in the 'transports' variable below
Definition rtsp.h:136
int content_length
length of the data following this header
Definition rtsp.h:131
Private data for the RTSP demuxer.
Definition rtsp.h:226
char real_challenge[64]
the "RealChallenge1:" field from the server
Definition rtsp.h:278
int nb_rtsp_streams
number of items in the 'rtsp_streams' variable
Definition rtsp.h:231
enum RTSPTransport transport
the negotiated data/packet transport protocol; e.g.
Definition rtsp.h:266
int rtp_port_max
Definition rtsp.h:429
char last_subscription[1024]
the last value of the "SET_PARAMETER Subscribe:" RTSP command.
Definition rtsp.h:331
int timeout
copy of RTSPMessageHeader->timeout, i.e.
Definition rtsp.h:258
HTTPAuthState auth_state
authentication state
Definition rtsp.h:284
int initial_timeout
Timeout to wait for incoming connections.
Definition rtsp.h:434
URLContext * rtsp_hd_out
Additional output handle, used when input and output are done separately, eg for HTTP tunneling.
Definition rtsp.h:358
int lower_transport_mask
A mask with all requested transport methods.
Definition rtsp.h:385
enum AVDiscard * real_setup
current stream setup.
Definition rtsp.h:326
enum RTSPLowerTransport lower_transport
the negotiated network layer transport protocol; e.g.
Definition rtsp.h:270
int64_t teardown_deadline
Monotonic deadline (av_gettime_relative() time base) bounding the close-time TEARDOWN exchange,...
Definition rtsp.h:369
int64_t last_cmd_time
timestamp of the last RTSP command that we sent to the RTSP server.
Definition rtsp.h:263
int need_subscription
The following are used for Real stream selection.
Definition rtsp.h:318
char session_id[512]
copy of RTSPMessageHeader->session_id, i.e.
Definition rtsp.h:253
int pending_packet
Indicates if a packet is pending to be read (useful for interleaved reads)
Definition rtsp.h:309
enum RTSPServerType server_type
brand of server that we're talking to; e.g.
Definition rtsp.h:275
int rtsp_flags
Various option flags for the RTSP muxer/demuxer.
Definition rtsp.h:419
int64_t seek_timestamp
the seek value requested when calling av_seek_frame().
Definition rtsp.h:247
char control_uri[MAX_URL_SIZE]
some MS RTSP streams contain a URL in the SDP that we need to use for all subsequent RTSP requests,...
Definition rtsp.h:347
int buffer_size
Definition rtsp.h:452
URLContext * rtsp_hd
Definition rtsp.h:228
int rtp_port_min
Minimum and maximum local UDP ports.
Definition rtsp.h:429
uint64_t packets
The number of returned packets.
Definition rtsp.h:390
enum RTSPClientState state
indicator of whether we are currently receiving data from the server.
Definition rtsp.h:239
enum AVDiscard * real_setup_cache
stream setup during the last frame read.
Definition rtsp.h:322
int initial_pause
Do not begin to play the stream immediately.
Definition rtsp.h:406
int seq
RTSP command sequence number.
Definition rtsp.h:249
int nb_byes
Definition rtsp.h:377
struct RTSPStream ** rtsp_streams
streams in this session
Definition rtsp.h:233
int get_parameter_supported
Whether the server supports the GET_PARAMETER method.
Definition rtsp.h:401
enum RTSPStatusCode code
Definition rtspdec.c:45
const char * message
Definition rtspdec.c:46
Describe a single stream, as identified by a single m= line block in the SDP content.
Definition rtsp.h:488
int interleaved_min
interleave IDs; copies of RTSPTransportField->interleaved_min/max for the selected transport.
Definition rtsp.h:497
char control_url[MAX_URL_SIZE]
url for this stream (from SDP)
Definition rtsp.h:499
URLContext * rtp_handle
RTP stream handle (if UDP)
Definition rtsp.h:489
int interleaved_max
Definition rtsp.h:497
int stream_index
corresponding stream index, if any.
Definition rtsp.h:493
void * transport_priv
RTP/RDT parse context if input, RTP AVFormatContext if output.
Definition rtsp.h:490
int interleaved_max
Definition rtsp.h:95
int client_port_min
UDP client ports; these should be the local ports of the UDP RTP (and RTCP) sockets over which we rec...
Definition rtsp.h:103
int mode_record
transport set to record data
Definition rtsp.h:114
int client_port_max
Definition rtsp.h:103
enum RTSPLowerTransport lower_transport
network layer transport protocol; e.g.
Definition rtsp.h:123
int interleaved_min
interleave ids, if TCP transport; each TCP/RTSP data packet starts with a '$', stream length and stre...
Definition rtsp.h:95
#define av_free(p)
#define av_freep(p)
#define av_log(a,...)
int64_t av_gettime_relative(void)
Get the current time in microseconds since some unspecified starting point.
Definition time.c:57
int ff_url_join(char *str, int size, const char *proto, const char *authorization, const char *hostname, int port, const char *fmt,...)
Definition url.c:40
unbuffered private I/O API
static int ffurl_write(URLContext *h, const uint8_t *buf, int size)
Write size bytes from buf to the resource accessed by h.
Definition url.h:205
int len