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 if (!rt->seek_timestamp) {
63 snprintf(cmd, sizeof(cmd), "Range: npt=0.000-\r\n");
65 snprintf(cmd, sizeof(cmd),
66 "Range: npt=%"PRId64".%03"PRId64"-\r\n",
67 rt->seek_timestamp / AV_TIME_BASE,
68 rt->seek_timestamp / (AV_TIME_BASE / 1000) % 1000);
71 ff_rtsp_send_cmd(s, "PLAY", rt->control_uri, cmd, reply, NULL);
72 if (reply->status_code != RTSP_STATUS_OK) {
75 if (rt->transport == RTSP_TRANSPORT_RTP &&
76 reply->range_start != AV_NOPTS_VALUE) {
77 for (i = 0; i < rt->nb_rtsp_streams; i++) {
78 RTSPStream *rtsp_st = rt->rtsp_streams[i];
79 RTPDemuxContext *rtpctx = rtsp_st->transport_priv;
81 if (!rtpctx || rtsp_st->stream_index < 0)
83 st = s->streams[rtsp_st->stream_index];
84 rtpctx->range_start_offset =
85 av_rescale_q(reply->range_start, AV_TIME_BASE_Q,
90 rt->state = RTSP_STATE_STREAMING;
94 /* pause the stream */
95 static int rtsp_read_pause(AVFormatContext *s)
97 RTSPState *rt = s->priv_data;
98 RTSPMessageHeader reply1, *reply = &reply1;
100 if (rt->state != RTSP_STATE_STREAMING)
102 else if (!(rt->server_type == RTSP_SERVER_REAL && rt->need_subscription)) {
103 ff_rtsp_send_cmd(s, "PAUSE", rt->control_uri, NULL, reply, NULL);
104 if (reply->status_code != RTSP_STATUS_OK) {
108 rt->state = RTSP_STATE_PAUSED;
112 int ff_rtsp_setup_input_streams(AVFormatContext *s, RTSPMessageHeader *reply)
114 RTSPState *rt = s->priv_data;
116 unsigned char *content = NULL;
119 /* describe the stream */
120 snprintf(cmd, sizeof(cmd),
121 "Accept: application/sdp\r\n");
122 if (rt->server_type == RTSP_SERVER_REAL) {
124 * The Require: attribute is needed for proper streaming from
128 "Require: com.real.retain-entity-for-setup\r\n",
131 ff_rtsp_send_cmd(s, "DESCRIBE", rt->control_uri, cmd, reply, &content);
133 return AVERROR_INVALIDDATA;
134 if (reply->status_code != RTSP_STATUS_OK) {
136 return AVERROR_INVALIDDATA;
139 av_log(s, AV_LOG_VERBOSE, "SDP:\n%s\n", content);
140 /* now we got the SDP description, we parse it */
141 ret = ff_sdp_parse(s, (const char *)content);
149 static int rtsp_probe(AVProbeData *p)
151 if (av_strstart(p->filename, "rtsp:", NULL))
152 return AVPROBE_SCORE_MAX;
156 static int rtsp_read_header(AVFormatContext *s,
157 AVFormatParameters *ap)
159 RTSPState *rt = s->priv_data;
162 ret = ff_rtsp_connect(s);
166 rt->real_setup_cache = av_mallocz(2 * s->nb_streams * sizeof(*rt->real_setup_cache));
167 if (!rt->real_setup_cache)
168 return AVERROR(ENOMEM);
169 rt->real_setup = rt->real_setup_cache + s->nb_streams;
171 if (ap->initial_pause) {
172 /* do not start immediately */
174 if (rtsp_read_play(s) < 0) {
175 ff_rtsp_close_streams(s);
176 ff_rtsp_close_connections(s);
177 return AVERROR_INVALIDDATA;
184 int ff_rtsp_tcp_read_packet(AVFormatContext *s, RTSPStream **prtsp_st,
185 uint8_t *buf, int buf_size)
187 RTSPState *rt = s->priv_data;
192 av_dlog(s, "tcp_read_packet:\n");
196 RTSPMessageHeader reply;
198 ret = ff_rtsp_read_reply(s, &reply, NULL, 1, NULL);
201 if (ret == 1) /* received '$' */
203 /* XXX: parse message */
204 if (rt->state != RTSP_STATE_STREAMING)
207 ret = url_read_complete(rt->rtsp_hd, buf, 3);
211 len = AV_RB16(buf + 1);
213 av_dlog(s, "id=%d len=%d\n", id, len);
215 if (len > buf_size || len < 12)
218 ret = url_read_complete(rt->rtsp_hd, buf, len);
221 if (rt->transport == RTSP_TRANSPORT_RDT &&
222 ff_rdt_parse_header(buf, len, &id, NULL, NULL, NULL, NULL) < 0)
225 /* find the matching stream */
226 for (i = 0; i < rt->nb_rtsp_streams; i++) {
227 rtsp_st = rt->rtsp_streams[i];
228 if (id >= rtsp_st->interleaved_min &&
229 id <= rtsp_st->interleaved_max)
238 static int resetup_tcp(AVFormatContext *s)
240 RTSPState *rt = s->priv_data;
244 av_url_split(NULL, 0, NULL, 0, host, sizeof(host), &port, NULL, 0,
246 ff_rtsp_undo_setup(s);
247 return ff_rtsp_make_setup_request(s, host, port, RTSP_LOWER_TRANSPORT_TCP,
251 static int rtsp_read_packet(AVFormatContext *s, AVPacket *pkt)
253 RTSPState *rt = s->priv_data;
255 RTSPMessageHeader reply1, *reply = &reply1;
259 if (rt->server_type == RTSP_SERVER_REAL) {
262 for (i = 0; i < s->nb_streams; i++)
263 rt->real_setup[i] = s->streams[i]->discard;
265 if (!rt->need_subscription) {
266 if (memcmp (rt->real_setup, rt->real_setup_cache,
267 sizeof(enum AVDiscard) * s->nb_streams)) {
268 snprintf(cmd, sizeof(cmd),
269 "Unsubscribe: %s\r\n",
270 rt->last_subscription);
271 ff_rtsp_send_cmd(s, "SET_PARAMETER", rt->control_uri,
273 if (reply->status_code != RTSP_STATUS_OK)
274 return AVERROR_INVALIDDATA;
275 rt->need_subscription = 1;
279 if (rt->need_subscription) {
280 int r, rule_nr, first = 1;
282 memcpy(rt->real_setup_cache, rt->real_setup,
283 sizeof(enum AVDiscard) * s->nb_streams);
284 rt->last_subscription[0] = 0;
286 snprintf(cmd, sizeof(cmd),
288 for (i = 0; i < rt->nb_rtsp_streams; i++) {
290 for (r = 0; r < s->nb_streams; r++) {
291 if (s->streams[r]->id == i) {
292 if (s->streams[r]->discard != AVDISCARD_ALL) {
294 av_strlcat(rt->last_subscription, ",",
295 sizeof(rt->last_subscription));
296 ff_rdt_subscribe_rule(
297 rt->last_subscription,
298 sizeof(rt->last_subscription), i, rule_nr);
305 av_strlcatf(cmd, sizeof(cmd), "%s\r\n", rt->last_subscription);
306 ff_rtsp_send_cmd(s, "SET_PARAMETER", rt->control_uri,
308 if (reply->status_code != RTSP_STATUS_OK)
309 return AVERROR_INVALIDDATA;
310 rt->need_subscription = 0;
312 if (rt->state == RTSP_STATE_STREAMING)
317 ret = ff_rtsp_fetch_packet(s, pkt);
319 if (ret == AVERROR(ETIMEDOUT) && !rt->packets) {
320 if (rt->lower_transport == RTSP_LOWER_TRANSPORT_UDP &&
321 rt->lower_transport_mask & (1 << RTSP_LOWER_TRANSPORT_TCP)) {
322 RTSPMessageHeader reply1, *reply = &reply1;
323 av_log(s, AV_LOG_WARNING, "UDP timeout, retrying with TCP\n");
324 if (rtsp_read_pause(s) != 0)
326 // TEARDOWN is required on Real-RTSP, but might make
327 // other servers close the connection.
328 if (rt->server_type == RTSP_SERVER_REAL)
329 ff_rtsp_send_cmd(s, "TEARDOWN", rt->control_uri, NULL,
331 rt->session_id[0] = '\0';
332 if (resetup_tcp(s) == 0) {
333 rt->state = RTSP_STATE_IDLE;
334 rt->need_subscription = 1;
335 if (rtsp_read_play(s) != 0)
345 /* send dummy request to keep TCP connection alive */
346 if ((av_gettime() - rt->last_cmd_time) / 1000000 >= rt->timeout / 2) {
347 if (rt->server_type == RTSP_SERVER_WMS) {
348 ff_rtsp_send_cmd_async(s, "GET_PARAMETER", rt->control_uri, NULL);
350 ff_rtsp_send_cmd_async(s, "OPTIONS", "*", NULL);
357 static int rtsp_read_seek(AVFormatContext *s, int stream_index,
358 int64_t timestamp, int flags)
360 RTSPState *rt = s->priv_data;
362 rt->seek_timestamp = av_rescale_q(timestamp,
363 s->streams[stream_index]->time_base,
367 case RTSP_STATE_IDLE:
369 case RTSP_STATE_STREAMING:
370 if (rtsp_read_pause(s) != 0)
372 rt->state = RTSP_STATE_SEEKING;
373 if (rtsp_read_play(s) != 0)
376 case RTSP_STATE_PAUSED:
377 rt->state = RTSP_STATE_IDLE;
383 static int rtsp_read_close(AVFormatContext *s)
385 RTSPState *rt = s->priv_data;
388 /* NOTE: it is valid to flush the buffer here */
389 if (rt->lower_transport == RTSP_LOWER_TRANSPORT_TCP) {
390 avio_close(&rt->rtsp_gb);
393 ff_rtsp_send_cmd_async(s, "TEARDOWN", rt->control_uri, NULL);
395 ff_rtsp_close_streams(s);
396 ff_rtsp_close_connections(s);
398 rt->real_setup = NULL;
399 av_freep(&rt->real_setup_cache);
403 AVInputFormat ff_rtsp_demuxer = {
405 NULL_IF_CONFIG_SMALL("RTSP input format"),
412 .flags = AVFMT_NOFILE,
413 .read_play = rtsp_read_play,
414 .read_pause = rtsp_read_pause,