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