3 * Copyright (c) 2002 Fabrice Bellard
5 * This file is part of FFmpeg.
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.
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.
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
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_play(AVFormatContext *s)
36 RTSPState *rt = s->priv_data;
37 RTSPMessageHeader reply1, *reply = &reply1;
41 av_log(s, AV_LOG_DEBUG, "hello state=%d\n", rt->state);
44 if (!(rt->server_type == RTSP_SERVER_REAL && rt->need_subscription)) {
45 if (rt->transport == RTSP_TRANSPORT_RTP) {
46 for (i = 0; i < rt->nb_rtsp_streams; i++) {
47 RTSPStream *rtsp_st = rt->rtsp_streams[i];
48 RTPDemuxContext *rtpctx = rtsp_st->transport_priv;
51 ff_rtp_reset_packet_queue(rtpctx);
52 rtpctx->last_rtcp_ntp_time = AV_NOPTS_VALUE;
53 rtpctx->first_rtcp_ntp_time = AV_NOPTS_VALUE;
54 rtpctx->base_timestamp = 0;
55 rtpctx->timestamp = 0;
56 rtpctx->unwrapped_timestamp = 0;
57 rtpctx->rtcp_ts_offset = 0;
60 if (rt->state == RTSP_STATE_PAUSED) {
63 snprintf(cmd, sizeof(cmd),
64 "Range: npt=%"PRId64".%03"PRId64"-\r\n",
65 rt->seek_timestamp / AV_TIME_BASE,
66 rt->seek_timestamp / (AV_TIME_BASE / 1000) % 1000);
68 ff_rtsp_send_cmd(s, "PLAY", rt->control_uri, cmd, reply, NULL);
69 if (reply->status_code != RTSP_STATUS_OK) {
72 if (rt->transport == RTSP_TRANSPORT_RTP &&
73 reply->range_start != AV_NOPTS_VALUE) {
74 for (i = 0; i < rt->nb_rtsp_streams; i++) {
75 RTSPStream *rtsp_st = rt->rtsp_streams[i];
76 RTPDemuxContext *rtpctx = rtsp_st->transport_priv;
78 if (!rtpctx || rtsp_st->stream_index < 0)
80 st = s->streams[rtsp_st->stream_index];
81 rtpctx->range_start_offset =
82 av_rescale_q(reply->range_start, AV_TIME_BASE_Q,
87 rt->state = RTSP_STATE_STREAMING;
91 /* pause the stream */
92 static int rtsp_read_pause(AVFormatContext *s)
94 RTSPState *rt = s->priv_data;
95 RTSPMessageHeader reply1, *reply = &reply1;
97 if (rt->state != RTSP_STATE_STREAMING)
99 else if (!(rt->server_type == RTSP_SERVER_REAL && rt->need_subscription)) {
100 ff_rtsp_send_cmd(s, "PAUSE", rt->control_uri, NULL, reply, NULL);
101 if (reply->status_code != RTSP_STATUS_OK) {
105 rt->state = RTSP_STATE_PAUSED;
109 int ff_rtsp_setup_input_streams(AVFormatContext *s, RTSPMessageHeader *reply)
111 RTSPState *rt = s->priv_data;
113 unsigned char *content = NULL;
116 /* describe the stream */
117 snprintf(cmd, sizeof(cmd),
118 "Accept: application/sdp\r\n");
119 if (rt->server_type == RTSP_SERVER_REAL) {
121 * The Require: attribute is needed for proper streaming from
125 "Require: com.real.retain-entity-for-setup\r\n",
128 ff_rtsp_send_cmd(s, "DESCRIBE", rt->control_uri, cmd, reply, &content);
130 return AVERROR_INVALIDDATA;
131 if (reply->status_code != RTSP_STATUS_OK) {
133 return AVERROR_INVALIDDATA;
136 av_log(s, AV_LOG_VERBOSE, "SDP:\n%s\n", content);
137 /* now we got the SDP description, we parse it */
138 ret = ff_sdp_parse(s, (const char *)content);
146 static int rtsp_probe(AVProbeData *p)
148 if (av_strstart(p->filename, "rtsp:", NULL))
149 return AVPROBE_SCORE_MAX;
153 static int rtsp_read_header(AVFormatContext *s)
155 RTSPState *rt = s->priv_data;
158 ret = ff_rtsp_connect(s);
162 rt->real_setup_cache = av_mallocz(2 * s->nb_streams * sizeof(*rt->real_setup_cache));
163 if (!rt->real_setup_cache)
164 return AVERROR(ENOMEM);
165 rt->real_setup = rt->real_setup_cache + s->nb_streams;
167 if (rt->initial_pause) {
168 /* do not start immediately */
170 if (rtsp_read_play(s) < 0) {
171 ff_rtsp_close_streams(s);
172 ff_rtsp_close_connections(s);
173 return AVERROR_INVALIDDATA;
180 int ff_rtsp_tcp_read_packet(AVFormatContext *s, RTSPStream **prtsp_st,
181 uint8_t *buf, int buf_size)
183 RTSPState *rt = s->priv_data;
187 av_dlog(s, "tcp_read_packet:\n");
190 RTSPMessageHeader reply;
192 ret = ff_rtsp_read_reply(s, &reply, NULL, 1, NULL);
195 if (ret == 1) /* received '$' */
197 /* XXX: parse message */
198 if (rt->state != RTSP_STATE_STREAMING)
201 ret = ffurl_read_complete(rt->rtsp_hd, buf, 3);
205 len = AV_RB16(buf + 1);
206 av_dlog(s, "id=%d len=%d\n", id, len);
207 if (len > buf_size || len < 8)
210 ret = ffurl_read_complete(rt->rtsp_hd, buf, len);
213 if (rt->transport == RTSP_TRANSPORT_RDT &&
214 ff_rdt_parse_header(buf, len, &id, NULL, NULL, NULL, NULL) < 0)
217 /* find the matching stream */
218 for (i = 0; i < rt->nb_rtsp_streams; i++) {
219 rtsp_st = rt->rtsp_streams[i];
220 if (id >= rtsp_st->interleaved_min &&
221 id <= rtsp_st->interleaved_max)
230 static int resetup_tcp(AVFormatContext *s)
232 RTSPState *rt = s->priv_data;
236 av_url_split(NULL, 0, NULL, 0, host, sizeof(host), &port, NULL, 0,
238 ff_rtsp_undo_setup(s);
239 return ff_rtsp_make_setup_request(s, host, port, RTSP_LOWER_TRANSPORT_TCP,
243 static int rtsp_read_packet(AVFormatContext *s, AVPacket *pkt)
245 RTSPState *rt = s->priv_data;
247 RTSPMessageHeader reply1, *reply = &reply1;
251 if (rt->server_type == RTSP_SERVER_REAL) {
254 for (i = 0; i < s->nb_streams; i++)
255 rt->real_setup[i] = s->streams[i]->discard;
257 if (!rt->need_subscription) {
258 if (memcmp (rt->real_setup, rt->real_setup_cache,
259 sizeof(enum AVDiscard) * s->nb_streams)) {
260 snprintf(cmd, sizeof(cmd),
261 "Unsubscribe: %s\r\n",
262 rt->last_subscription);
263 ff_rtsp_send_cmd(s, "SET_PARAMETER", rt->control_uri,
265 if (reply->status_code != RTSP_STATUS_OK)
266 return AVERROR_INVALIDDATA;
267 rt->need_subscription = 1;
271 if (rt->need_subscription) {
272 int r, rule_nr, first = 1;
274 memcpy(rt->real_setup_cache, rt->real_setup,
275 sizeof(enum AVDiscard) * s->nb_streams);
276 rt->last_subscription[0] = 0;
278 snprintf(cmd, sizeof(cmd),
280 for (i = 0; i < rt->nb_rtsp_streams; i++) {
282 for (r = 0; r < s->nb_streams; r++) {
283 if (s->streams[r]->id == i) {
284 if (s->streams[r]->discard != AVDISCARD_ALL) {
286 av_strlcat(rt->last_subscription, ",",
287 sizeof(rt->last_subscription));
288 ff_rdt_subscribe_rule(
289 rt->last_subscription,
290 sizeof(rt->last_subscription), i, rule_nr);
297 av_strlcatf(cmd, sizeof(cmd), "%s\r\n", rt->last_subscription);
298 ff_rtsp_send_cmd(s, "SET_PARAMETER", rt->control_uri,
300 if (reply->status_code != RTSP_STATUS_OK)
301 return AVERROR_INVALIDDATA;
302 rt->need_subscription = 0;
304 if (rt->state == RTSP_STATE_STREAMING)
309 ret = ff_rtsp_fetch_packet(s, pkt);
311 if (ret == AVERROR(ETIMEDOUT) && !rt->packets) {
312 if (rt->lower_transport == RTSP_LOWER_TRANSPORT_UDP &&
313 rt->lower_transport_mask & (1 << RTSP_LOWER_TRANSPORT_TCP)) {
314 RTSPMessageHeader reply1, *reply = &reply1;
315 av_log(s, AV_LOG_WARNING, "UDP timeout, retrying with TCP\n");
316 if (rtsp_read_pause(s) != 0)
318 // TEARDOWN is required on Real-RTSP, but might make
319 // other servers close the connection.
320 if (rt->server_type == RTSP_SERVER_REAL)
321 ff_rtsp_send_cmd(s, "TEARDOWN", rt->control_uri, NULL,
323 rt->session_id[0] = '\0';
324 if (resetup_tcp(s) == 0) {
325 rt->state = RTSP_STATE_IDLE;
326 rt->need_subscription = 1;
327 if (rtsp_read_play(s) != 0)
337 /* send dummy request to keep TCP connection alive */
338 if ((av_gettime() - rt->last_cmd_time) / 1000000 >= rt->timeout / 2 ||
339 rt->auth_state.stale) {
340 if (rt->server_type == RTSP_SERVER_WMS ||
341 (rt->server_type != RTSP_SERVER_REAL &&
342 rt->get_parameter_supported)) {
343 ff_rtsp_send_cmd_async(s, "GET_PARAMETER", rt->control_uri, NULL);
345 ff_rtsp_send_cmd_async(s, "OPTIONS", "*", NULL);
347 /* The stale flag should be reset when creating the auth response in
348 * ff_rtsp_send_cmd_async, but reset it here just in case we never
349 * called the auth code (if we didn't have any credentials set). */
350 rt->auth_state.stale = 0;
356 static int rtsp_read_seek(AVFormatContext *s, int stream_index,
357 int64_t timestamp, int flags)
359 RTSPState *rt = s->priv_data;
361 rt->seek_timestamp = av_rescale_q(timestamp,
362 s->streams[stream_index]->time_base,
366 case RTSP_STATE_IDLE:
368 case RTSP_STATE_STREAMING:
369 if (rtsp_read_pause(s) != 0)
371 rt->state = RTSP_STATE_SEEKING;
372 if (rtsp_read_play(s) != 0)
375 case RTSP_STATE_PAUSED:
376 rt->state = RTSP_STATE_IDLE;
382 static int rtsp_read_close(AVFormatContext *s)
384 RTSPState *rt = s->priv_data;
386 ff_rtsp_send_cmd_async(s, "TEARDOWN", rt->control_uri, NULL);
388 ff_rtsp_close_streams(s);
389 ff_rtsp_close_connections(s);
391 rt->real_setup = NULL;
392 av_freep(&rt->real_setup_cache);
396 static const AVClass rtsp_demuxer_class = {
397 .class_name = "RTSP demuxer",
398 .item_name = av_default_item_name,
399 .option = ff_rtsp_options,
400 .version = LIBAVUTIL_VERSION_INT,
403 AVInputFormat ff_rtsp_demuxer = {
405 .long_name = NULL_IF_CONFIG_SMALL("RTSP input format"),
406 .priv_data_size = sizeof(RTSPState),
407 .read_probe = rtsp_probe,
408 .read_header = rtsp_read_header,
409 .read_packet = rtsp_read_packet,
410 .read_close = rtsp_read_close,
411 .read_seek = rtsp_read_seek,
412 .flags = AVFMT_NOFILE,
413 .read_play = rtsp_read_play,
414 .read_pause = rtsp_read_pause,
415 .priv_class = &rtsp_demuxer_class,