3 * Copyright (c) 2002 Fabrice Bellard
5 * This file is part of Libav.
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.
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.
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
22 #include "libavutil/avstring.h"
23 #include "libavutil/intreadwrite.h"
24 #include "libavutil/mathematics.h"
29 #include "os_support.h"
34 static int rtsp_read_close(AVFormatContext *s)
36 RTSPState *rt = s->priv_data;
38 ff_rtsp_send_cmd_async(s, "TEARDOWN", rt->control_uri, NULL);
40 ff_rtsp_close_streams(s);
41 ff_rtsp_close_connections(s);
43 rt->real_setup = NULL;
44 av_freep(&rt->real_setup_cache);
48 static int rtsp_read_play(AVFormatContext *s)
50 RTSPState *rt = s->priv_data;
51 RTSPMessageHeader reply1, *reply = &reply1;
55 av_log(s, AV_LOG_DEBUG, "hello state=%d\n", rt->state);
58 if (!(rt->server_type == RTSP_SERVER_REAL && rt->need_subscription)) {
59 if (rt->transport == RTSP_TRANSPORT_RTP) {
60 for (i = 0; i < rt->nb_rtsp_streams; i++) {
61 RTSPStream *rtsp_st = rt->rtsp_streams[i];
62 RTPDemuxContext *rtpctx = rtsp_st->transport_priv;
65 ff_rtp_reset_packet_queue(rtpctx);
66 rtpctx->last_rtcp_ntp_time = AV_NOPTS_VALUE;
67 rtpctx->first_rtcp_ntp_time = AV_NOPTS_VALUE;
68 rtpctx->base_timestamp = 0;
69 rtpctx->timestamp = 0;
70 rtpctx->unwrapped_timestamp = 0;
71 rtpctx->rtcp_ts_offset = 0;
74 if (rt->state == RTSP_STATE_PAUSED) {
77 snprintf(cmd, sizeof(cmd),
78 "Range: npt=%"PRId64".%03"PRId64"-\r\n",
79 rt->seek_timestamp / AV_TIME_BASE,
80 rt->seek_timestamp / (AV_TIME_BASE / 1000) % 1000);
82 ff_rtsp_send_cmd(s, "PLAY", rt->control_uri, cmd, reply, NULL);
83 if (reply->status_code != RTSP_STATUS_OK) {
86 if (rt->transport == RTSP_TRANSPORT_RTP &&
87 reply->range_start != AV_NOPTS_VALUE) {
88 for (i = 0; i < rt->nb_rtsp_streams; i++) {
89 RTSPStream *rtsp_st = rt->rtsp_streams[i];
90 RTPDemuxContext *rtpctx = rtsp_st->transport_priv;
92 if (!rtpctx || rtsp_st->stream_index < 0)
94 st = s->streams[rtsp_st->stream_index];
95 rtpctx->range_start_offset =
96 av_rescale_q(reply->range_start, AV_TIME_BASE_Q,
101 rt->state = RTSP_STATE_STREAMING;
105 /* pause the stream */
106 static int rtsp_read_pause(AVFormatContext *s)
108 RTSPState *rt = s->priv_data;
109 RTSPMessageHeader reply1, *reply = &reply1;
111 if (rt->state != RTSP_STATE_STREAMING)
113 else if (!(rt->server_type == RTSP_SERVER_REAL && rt->need_subscription)) {
114 ff_rtsp_send_cmd(s, "PAUSE", rt->control_uri, NULL, reply, NULL);
115 if (reply->status_code != RTSP_STATUS_OK) {
119 rt->state = RTSP_STATE_PAUSED;
123 int ff_rtsp_setup_input_streams(AVFormatContext *s, RTSPMessageHeader *reply)
125 RTSPState *rt = s->priv_data;
127 unsigned char *content = NULL;
130 /* describe the stream */
131 snprintf(cmd, sizeof(cmd),
132 "Accept: application/sdp\r\n");
133 if (rt->server_type == RTSP_SERVER_REAL) {
135 * The Require: attribute is needed for proper streaming from
139 "Require: com.real.retain-entity-for-setup\r\n",
142 ff_rtsp_send_cmd(s, "DESCRIBE", rt->control_uri, cmd, reply, &content);
144 return AVERROR_INVALIDDATA;
145 if (reply->status_code != RTSP_STATUS_OK) {
147 return AVERROR_INVALIDDATA;
150 av_log(s, AV_LOG_VERBOSE, "SDP:\n%s\n", content);
151 /* now we got the SDP description, we parse it */
152 ret = ff_sdp_parse(s, (const char *)content);
160 static int rtsp_probe(AVProbeData *p)
162 if (av_strstart(p->filename, "rtsp:", NULL))
163 return AVPROBE_SCORE_MAX;
167 static int rtsp_read_header(AVFormatContext *s)
169 RTSPState *rt = s->priv_data;
172 ret = ff_rtsp_connect(s);
176 rt->real_setup_cache = !s->nb_streams ? NULL :
177 av_mallocz(2 * s->nb_streams * sizeof(*rt->real_setup_cache));
178 if (!rt->real_setup_cache && s->nb_streams)
179 return AVERROR(ENOMEM);
180 rt->real_setup = rt->real_setup_cache + s->nb_streams;
182 if (rt->initial_pause) {
183 /* do not start immediately */
185 if (rtsp_read_play(s) < 0) {
186 ff_rtsp_close_streams(s);
187 ff_rtsp_close_connections(s);
188 return AVERROR_INVALIDDATA;
195 int ff_rtsp_tcp_read_packet(AVFormatContext *s, RTSPStream **prtsp_st,
196 uint8_t *buf, int buf_size)
198 RTSPState *rt = s->priv_data;
202 av_dlog(s, "tcp_read_packet:\n");
205 RTSPMessageHeader reply;
207 ret = ff_rtsp_read_reply(s, &reply, NULL, 1, NULL);
210 if (ret == 1) /* received '$' */
212 /* XXX: parse message */
213 if (rt->state != RTSP_STATE_STREAMING)
216 ret = ffurl_read_complete(rt->rtsp_hd, buf, 3);
220 len = AV_RB16(buf + 1);
221 av_dlog(s, "id=%d len=%d\n", id, len);
222 if (len > buf_size || len < 12)
225 ret = ffurl_read_complete(rt->rtsp_hd, buf, len);
228 if (rt->transport == RTSP_TRANSPORT_RDT &&
229 ff_rdt_parse_header(buf, len, &id, NULL, NULL, NULL, NULL) < 0)
232 /* find the matching stream */
233 for (i = 0; i < rt->nb_rtsp_streams; i++) {
234 rtsp_st = rt->rtsp_streams[i];
235 if (id >= rtsp_st->interleaved_min &&
236 id <= rtsp_st->interleaved_max)
245 static int resetup_tcp(AVFormatContext *s)
247 RTSPState *rt = s->priv_data;
251 av_url_split(NULL, 0, NULL, 0, host, sizeof(host), &port, NULL, 0,
253 ff_rtsp_undo_setup(s);
254 return ff_rtsp_make_setup_request(s, host, port, RTSP_LOWER_TRANSPORT_TCP,
258 static int rtsp_read_packet(AVFormatContext *s, AVPacket *pkt)
260 RTSPState *rt = s->priv_data;
262 RTSPMessageHeader reply1, *reply = &reply1;
266 if (rt->server_type == RTSP_SERVER_REAL) {
269 for (i = 0; i < s->nb_streams; i++)
270 rt->real_setup[i] = s->streams[i]->discard;
272 if (!rt->need_subscription) {
273 if (memcmp (rt->real_setup, rt->real_setup_cache,
274 sizeof(enum AVDiscard) * s->nb_streams)) {
275 snprintf(cmd, sizeof(cmd),
276 "Unsubscribe: %s\r\n",
277 rt->last_subscription);
278 ff_rtsp_send_cmd(s, "SET_PARAMETER", rt->control_uri,
280 if (reply->status_code != RTSP_STATUS_OK)
281 return AVERROR_INVALIDDATA;
282 rt->need_subscription = 1;
286 if (rt->need_subscription) {
287 int r, rule_nr, first = 1;
289 memcpy(rt->real_setup_cache, rt->real_setup,
290 sizeof(enum AVDiscard) * s->nb_streams);
291 rt->last_subscription[0] = 0;
293 snprintf(cmd, sizeof(cmd),
295 for (i = 0; i < rt->nb_rtsp_streams; i++) {
297 for (r = 0; r < s->nb_streams; r++) {
298 if (s->streams[r]->id == i) {
299 if (s->streams[r]->discard != AVDISCARD_ALL) {
301 av_strlcat(rt->last_subscription, ",",
302 sizeof(rt->last_subscription));
303 ff_rdt_subscribe_rule(
304 rt->last_subscription,
305 sizeof(rt->last_subscription), i, rule_nr);
312 av_strlcatf(cmd, sizeof(cmd), "%s\r\n", rt->last_subscription);
313 ff_rtsp_send_cmd(s, "SET_PARAMETER", rt->control_uri,
315 if (reply->status_code != RTSP_STATUS_OK)
316 return AVERROR_INVALIDDATA;
317 rt->need_subscription = 0;
319 if (rt->state == RTSP_STATE_STREAMING)
324 ret = ff_rtsp_fetch_packet(s, pkt);
326 if (ret == AVERROR(ETIMEDOUT) && !rt->packets) {
327 if (rt->lower_transport == RTSP_LOWER_TRANSPORT_UDP &&
328 rt->lower_transport_mask & (1 << RTSP_LOWER_TRANSPORT_TCP)) {
329 RTSPMessageHeader reply1, *reply = &reply1;
330 av_log(s, AV_LOG_WARNING, "UDP timeout, retrying with TCP\n");
331 if (rtsp_read_pause(s) != 0)
333 // TEARDOWN is required on Real-RTSP, but might make
334 // other servers close the connection.
335 if (rt->server_type == RTSP_SERVER_REAL)
336 ff_rtsp_send_cmd(s, "TEARDOWN", rt->control_uri, NULL,
338 rt->session_id[0] = '\0';
339 if (resetup_tcp(s) == 0) {
340 rt->state = RTSP_STATE_IDLE;
341 rt->need_subscription = 1;
342 if (rtsp_read_play(s) != 0)
352 /* send dummy request to keep TCP connection alive */
353 if ((av_gettime() - rt->last_cmd_time) / 1000000 >= rt->timeout / 2 ||
354 rt->auth_state.stale) {
355 if (rt->server_type == RTSP_SERVER_WMS ||
356 (rt->server_type != RTSP_SERVER_REAL &&
357 rt->get_parameter_supported)) {
358 ff_rtsp_send_cmd_async(s, "GET_PARAMETER", rt->control_uri, NULL);
360 ff_rtsp_send_cmd_async(s, "OPTIONS", "*", NULL);
362 /* The stale flag should be reset when creating the auth response in
363 * ff_rtsp_send_cmd_async, but reset it here just in case we never
364 * called the auth code (if we didn't have any credentials set). */
365 rt->auth_state.stale = 0;
371 static int rtsp_read_seek(AVFormatContext *s, int stream_index,
372 int64_t timestamp, int flags)
374 RTSPState *rt = s->priv_data;
376 rt->seek_timestamp = av_rescale_q(timestamp,
377 s->streams[stream_index]->time_base,
381 case RTSP_STATE_IDLE:
383 case RTSP_STATE_STREAMING:
384 if (rtsp_read_pause(s) != 0)
386 rt->state = RTSP_STATE_SEEKING;
387 if (rtsp_read_play(s) != 0)
390 case RTSP_STATE_PAUSED:
391 rt->state = RTSP_STATE_IDLE;
397 static const AVClass rtsp_demuxer_class = {
398 .class_name = "RTSP demuxer",
399 .item_name = av_default_item_name,
400 .option = ff_rtsp_options,
401 .version = LIBAVUTIL_VERSION_INT,
404 AVInputFormat ff_rtsp_demuxer = {
406 .long_name = NULL_IF_CONFIG_SMALL("RTSP input format"),
407 .priv_data_size = sizeof(RTSPState),
408 .read_probe = rtsp_probe,
409 .read_header = rtsp_read_header,
410 .read_packet = rtsp_read_packet,
411 .read_close = rtsp_read_close,
412 .read_seek = rtsp_read_seek,
413 .flags = AVFMT_NOFILE,
414 .read_play = rtsp_read_play,
415 .read_pause = rtsp_read_pause,
416 .priv_class = &rtsp_demuxer_class,