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"
28 #include "os_support.h"
33 //#define DEBUG_RTP_TCP
35 static int rtsp_read_play(AVFormatContext *s)
37 RTSPState *rt = s->priv_data;
38 RTSPMessageHeader reply1, *reply = &reply1;
42 av_log(s, AV_LOG_DEBUG, "hello state=%d\n", rt->state);
45 if (!(rt->server_type == RTSP_SERVER_REAL && rt->need_subscription)) {
46 if (rt->transport == RTSP_TRANSPORT_RTP) {
47 for (i = 0; i < rt->nb_rtsp_streams; i++) {
48 RTSPStream *rtsp_st = rt->rtsp_streams[i];
49 RTPDemuxContext *rtpctx = rtsp_st->transport_priv;
52 ff_rtp_reset_packet_queue(rtpctx);
53 rtpctx->last_rtcp_ntp_time = AV_NOPTS_VALUE;
54 rtpctx->first_rtcp_ntp_time = AV_NOPTS_VALUE;
55 rtpctx->base_timestamp = 0;
56 rtpctx->rtcp_ts_offset = 0;
59 if (rt->state == RTSP_STATE_PAUSED) {
62 snprintf(cmd, sizeof(cmd),
63 "Range: npt=%"PRId64".%03"PRId64"-\r\n",
64 rt->seek_timestamp / AV_TIME_BASE,
65 rt->seek_timestamp / (AV_TIME_BASE / 1000) % 1000);
67 ff_rtsp_send_cmd(s, "PLAY", rt->control_uri, cmd, reply, NULL);
68 if (reply->status_code != RTSP_STATUS_OK) {
71 if (rt->transport == RTSP_TRANSPORT_RTP &&
72 reply->range_start != AV_NOPTS_VALUE) {
73 for (i = 0; i < rt->nb_rtsp_streams; i++) {
74 RTSPStream *rtsp_st = rt->rtsp_streams[i];
75 RTPDemuxContext *rtpctx = rtsp_st->transport_priv;
77 if (!rtpctx || rtsp_st->stream_index < 0)
79 st = s->streams[rtsp_st->stream_index];
80 rtpctx->range_start_offset =
81 av_rescale_q(reply->range_start, AV_TIME_BASE_Q,
86 rt->state = RTSP_STATE_STREAMING;
90 /* pause the stream */
91 static int rtsp_read_pause(AVFormatContext *s)
93 RTSPState *rt = s->priv_data;
94 RTSPMessageHeader reply1, *reply = &reply1;
96 if (rt->state != RTSP_STATE_STREAMING)
98 else if (!(rt->server_type == RTSP_SERVER_REAL && rt->need_subscription)) {
99 ff_rtsp_send_cmd(s, "PAUSE", rt->control_uri, NULL, reply, NULL);
100 if (reply->status_code != RTSP_STATUS_OK) {
104 rt->state = RTSP_STATE_PAUSED;
108 int ff_rtsp_setup_input_streams(AVFormatContext *s, RTSPMessageHeader *reply)
110 RTSPState *rt = s->priv_data;
112 unsigned char *content = NULL;
115 /* describe the stream */
116 snprintf(cmd, sizeof(cmd),
117 "Accept: application/sdp\r\n");
118 if (rt->server_type == RTSP_SERVER_REAL) {
120 * The Require: attribute is needed for proper streaming from
124 "Require: com.real.retain-entity-for-setup\r\n",
127 ff_rtsp_send_cmd(s, "DESCRIBE", rt->control_uri, cmd, reply, &content);
129 return AVERROR_INVALIDDATA;
130 if (reply->status_code != RTSP_STATUS_OK) {
132 return AVERROR_INVALIDDATA;
135 av_log(s, AV_LOG_VERBOSE, "SDP:\n%s\n", content);
136 /* now we got the SDP description, we parse it */
137 ret = ff_sdp_parse(s, (const char *)content);
145 static int rtsp_probe(AVProbeData *p)
147 if (av_strstart(p->filename, "rtsp:", NULL))
148 return AVPROBE_SCORE_MAX;
152 static int rtsp_read_header(AVFormatContext *s,
153 AVFormatParameters *ap)
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 (ap->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;
188 av_dlog(s, "tcp_read_packet:\n");
192 RTSPMessageHeader reply;
194 ret = ff_rtsp_read_reply(s, &reply, NULL, 1, NULL);
197 if (ret == 1) /* received '$' */
199 /* XXX: parse message */
200 if (rt->state != RTSP_STATE_STREAMING)
203 ret = url_read_complete(rt->rtsp_hd, buf, 3);
207 len = AV_RB16(buf + 1);
209 av_dlog(s, "id=%d len=%d\n", id, len);
211 if (len > buf_size || len < 12)
214 ret = url_read_complete(rt->rtsp_hd, buf, len);
217 if (rt->transport == RTSP_TRANSPORT_RDT &&
218 ff_rdt_parse_header(buf, len, &id, NULL, NULL, NULL, NULL) < 0)
221 /* find the matching stream */
222 for (i = 0; i < rt->nb_rtsp_streams; i++) {
223 rtsp_st = rt->rtsp_streams[i];
224 if (id >= rtsp_st->interleaved_min &&
225 id <= rtsp_st->interleaved_max)
234 static int resetup_tcp(AVFormatContext *s)
236 RTSPState *rt = s->priv_data;
240 av_url_split(NULL, 0, NULL, 0, host, sizeof(host), &port, NULL, 0,
242 ff_rtsp_undo_setup(s);
243 return ff_rtsp_make_setup_request(s, host, port, RTSP_LOWER_TRANSPORT_TCP,
247 static int rtsp_read_packet(AVFormatContext *s, AVPacket *pkt)
249 RTSPState *rt = s->priv_data;
251 RTSPMessageHeader reply1, *reply = &reply1;
255 if (rt->server_type == RTSP_SERVER_REAL) {
258 for (i = 0; i < s->nb_streams; i++)
259 rt->real_setup[i] = s->streams[i]->discard;
261 if (!rt->need_subscription) {
262 if (memcmp (rt->real_setup, rt->real_setup_cache,
263 sizeof(enum AVDiscard) * s->nb_streams)) {
264 snprintf(cmd, sizeof(cmd),
265 "Unsubscribe: %s\r\n",
266 rt->last_subscription);
267 ff_rtsp_send_cmd(s, "SET_PARAMETER", rt->control_uri,
269 if (reply->status_code != RTSP_STATUS_OK)
270 return AVERROR_INVALIDDATA;
271 rt->need_subscription = 1;
275 if (rt->need_subscription) {
276 int r, rule_nr, first = 1;
278 memcpy(rt->real_setup_cache, rt->real_setup,
279 sizeof(enum AVDiscard) * s->nb_streams);
280 rt->last_subscription[0] = 0;
282 snprintf(cmd, sizeof(cmd),
284 for (i = 0; i < rt->nb_rtsp_streams; i++) {
286 for (r = 0; r < s->nb_streams; r++) {
287 if (s->streams[r]->id == i) {
288 if (s->streams[r]->discard != AVDISCARD_ALL) {
290 av_strlcat(rt->last_subscription, ",",
291 sizeof(rt->last_subscription));
292 ff_rdt_subscribe_rule(
293 rt->last_subscription,
294 sizeof(rt->last_subscription), i, rule_nr);
301 av_strlcatf(cmd, sizeof(cmd), "%s\r\n", rt->last_subscription);
302 ff_rtsp_send_cmd(s, "SET_PARAMETER", rt->control_uri,
304 if (reply->status_code != RTSP_STATUS_OK)
305 return AVERROR_INVALIDDATA;
306 rt->need_subscription = 0;
308 if (rt->state == RTSP_STATE_STREAMING)
313 ret = ff_rtsp_fetch_packet(s, pkt);
315 if (ret == AVERROR(ETIMEDOUT) && !rt->packets) {
316 if (rt->lower_transport == RTSP_LOWER_TRANSPORT_UDP &&
317 rt->lower_transport_mask & (1 << RTSP_LOWER_TRANSPORT_TCP)) {
318 RTSPMessageHeader reply1, *reply = &reply1;
319 av_log(s, AV_LOG_WARNING, "UDP timeout, retrying with TCP\n");
320 if (rtsp_read_pause(s) != 0)
322 // TEARDOWN is required on Real-RTSP, but might make
323 // other servers close the connection.
324 if (rt->server_type == RTSP_SERVER_REAL)
325 ff_rtsp_send_cmd(s, "TEARDOWN", rt->control_uri, NULL,
327 rt->session_id[0] = '\0';
328 if (resetup_tcp(s) == 0) {
329 rt->state = RTSP_STATE_IDLE;
330 rt->need_subscription = 1;
331 if (rtsp_read_play(s) != 0)
341 /* send dummy request to keep TCP connection alive */
342 if ((av_gettime() - rt->last_cmd_time) / 1000000 >= rt->timeout / 2) {
343 if (rt->server_type == RTSP_SERVER_WMS) {
344 ff_rtsp_send_cmd_async(s, "GET_PARAMETER", rt->control_uri, NULL);
346 ff_rtsp_send_cmd_async(s, "OPTIONS", "*", NULL);
353 static int rtsp_read_seek(AVFormatContext *s, int stream_index,
354 int64_t timestamp, int flags)
356 RTSPState *rt = s->priv_data;
358 rt->seek_timestamp = av_rescale_q(timestamp,
359 s->streams[stream_index]->time_base,
363 case RTSP_STATE_IDLE:
365 case RTSP_STATE_STREAMING:
366 if (rtsp_read_pause(s) != 0)
368 rt->state = RTSP_STATE_SEEKING;
369 if (rtsp_read_play(s) != 0)
372 case RTSP_STATE_PAUSED:
373 rt->state = RTSP_STATE_IDLE;
379 static int rtsp_read_close(AVFormatContext *s)
381 RTSPState *rt = s->priv_data;
384 /* NOTE: it is valid to flush the buffer here */
385 if (rt->lower_transport == RTSP_LOWER_TRANSPORT_TCP) {
386 avio_close(&rt->rtsp_gb);
389 ff_rtsp_send_cmd_async(s, "TEARDOWN", rt->control_uri, NULL);
391 ff_rtsp_close_streams(s);
392 ff_rtsp_close_connections(s);
394 rt->real_setup = NULL;
395 av_freep(&rt->real_setup_cache);
399 AVInputFormat ff_rtsp_demuxer = {
401 NULL_IF_CONFIG_SMALL("RTSP input format"),
408 .flags = AVFMT_NOFILE,
409 .read_play = rtsp_read_play,
410 .read_pause = rtsp_read_pause,