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=%0.3f-\r\n",
64 (double)rt->seek_timestamp / AV_TIME_BASE);
66 ff_rtsp_send_cmd(s, "PLAY", rt->control_uri, cmd, reply, NULL);
67 if (reply->status_code != RTSP_STATUS_OK) {
70 if (rt->transport == RTSP_TRANSPORT_RTP &&
71 reply->range_start != AV_NOPTS_VALUE) {
72 for (i = 0; i < rt->nb_rtsp_streams; i++) {
73 RTSPStream *rtsp_st = rt->rtsp_streams[i];
74 RTPDemuxContext *rtpctx = rtsp_st->transport_priv;
76 if (!rtpctx || rtsp_st->stream_index < 0)
78 st = s->streams[rtsp_st->stream_index];
79 rtpctx->range_start_offset =
80 av_rescale_q(reply->range_start, AV_TIME_BASE_Q,
85 rt->state = RTSP_STATE_STREAMING;
89 /* pause the stream */
90 static int rtsp_read_pause(AVFormatContext *s)
92 RTSPState *rt = s->priv_data;
93 RTSPMessageHeader reply1, *reply = &reply1;
95 if (rt->state != RTSP_STATE_STREAMING)
97 else if (!(rt->server_type == RTSP_SERVER_REAL && rt->need_subscription)) {
98 ff_rtsp_send_cmd(s, "PAUSE", rt->control_uri, NULL, reply, NULL);
99 if (reply->status_code != RTSP_STATUS_OK) {
103 rt->state = RTSP_STATE_PAUSED;
107 int ff_rtsp_setup_input_streams(AVFormatContext *s, RTSPMessageHeader *reply)
109 RTSPState *rt = s->priv_data;
111 unsigned char *content = NULL;
114 /* describe the stream */
115 snprintf(cmd, sizeof(cmd),
116 "Accept: application/sdp\r\n");
117 if (rt->server_type == RTSP_SERVER_REAL) {
119 * The Require: attribute is needed for proper streaming from
123 "Require: com.real.retain-entity-for-setup\r\n",
126 ff_rtsp_send_cmd(s, "DESCRIBE", rt->control_uri, cmd, reply, &content);
128 return AVERROR_INVALIDDATA;
129 if (reply->status_code != RTSP_STATUS_OK) {
131 return AVERROR_INVALIDDATA;
134 av_log(s, AV_LOG_VERBOSE, "SDP:\n%s\n", content);
135 /* now we got the SDP description, we parse it */
136 ret = ff_sdp_parse(s, (const char *)content);
144 static int rtsp_probe(AVProbeData *p)
146 if (av_strstart(p->filename, "rtsp:", NULL))
147 return AVPROBE_SCORE_MAX;
151 static int rtsp_read_header(AVFormatContext *s,
152 AVFormatParameters *ap)
154 RTSPState *rt = s->priv_data;
157 ret = ff_rtsp_connect(s);
161 rt->real_setup_cache = av_mallocz(2 * s->nb_streams * sizeof(*rt->real_setup_cache));
162 if (!rt->real_setup_cache)
163 return AVERROR(ENOMEM);
164 rt->real_setup = rt->real_setup_cache + s->nb_streams;
166 if (ap->initial_pause) {
167 /* do not start immediately */
169 if (rtsp_read_play(s) < 0) {
170 ff_rtsp_close_streams(s);
171 ff_rtsp_close_connections(s);
172 return AVERROR_INVALIDDATA;
179 int ff_rtsp_tcp_read_packet(AVFormatContext *s, RTSPStream **prtsp_st,
180 uint8_t *buf, int buf_size)
182 RTSPState *rt = s->priv_data;
187 av_dlog(s, "tcp_read_packet:\n");
191 RTSPMessageHeader reply;
193 ret = ff_rtsp_read_reply(s, &reply, NULL, 1, NULL);
196 if (ret == 1) /* received '$' */
198 /* XXX: parse message */
199 if (rt->state != RTSP_STATE_STREAMING)
202 ret = url_read_complete(rt->rtsp_hd, buf, 3);
206 len = AV_RB16(buf + 1);
208 av_dlog(s, "id=%d len=%d\n", id, len);
210 if (len > buf_size || len < 12)
213 ret = url_read_complete(rt->rtsp_hd, buf, len);
216 if (rt->transport == RTSP_TRANSPORT_RDT &&
217 ff_rdt_parse_header(buf, len, &id, NULL, NULL, NULL, NULL) < 0)
220 /* find the matching stream */
221 for (i = 0; i < rt->nb_rtsp_streams; i++) {
222 rtsp_st = rt->rtsp_streams[i];
223 if (id >= rtsp_st->interleaved_min &&
224 id <= rtsp_st->interleaved_max)
233 static int resetup_tcp(AVFormatContext *s)
235 RTSPState *rt = s->priv_data;
239 av_url_split(NULL, 0, NULL, 0, host, sizeof(host), &port, NULL, 0,
241 ff_rtsp_undo_setup(s);
242 return ff_rtsp_make_setup_request(s, host, port, RTSP_LOWER_TRANSPORT_TCP,
246 static int rtsp_read_packet(AVFormatContext *s, AVPacket *pkt)
248 RTSPState *rt = s->priv_data;
250 RTSPMessageHeader reply1, *reply = &reply1;
254 if (rt->server_type == RTSP_SERVER_REAL) {
257 for (i = 0; i < s->nb_streams; i++)
258 rt->real_setup[i] = s->streams[i]->discard;
260 if (!rt->need_subscription) {
261 if (memcmp (rt->real_setup, rt->real_setup_cache,
262 sizeof(enum AVDiscard) * s->nb_streams)) {
263 snprintf(cmd, sizeof(cmd),
264 "Unsubscribe: %s\r\n",
265 rt->last_subscription);
266 ff_rtsp_send_cmd(s, "SET_PARAMETER", rt->control_uri,
268 if (reply->status_code != RTSP_STATUS_OK)
269 return AVERROR_INVALIDDATA;
270 rt->need_subscription = 1;
274 if (rt->need_subscription) {
275 int r, rule_nr, first = 1;
277 memcpy(rt->real_setup_cache, rt->real_setup,
278 sizeof(enum AVDiscard) * s->nb_streams);
279 rt->last_subscription[0] = 0;
281 snprintf(cmd, sizeof(cmd),
283 for (i = 0; i < rt->nb_rtsp_streams; i++) {
285 for (r = 0; r < s->nb_streams; r++) {
286 if (s->streams[r]->id == i) {
287 if (s->streams[r]->discard != AVDISCARD_ALL) {
289 av_strlcat(rt->last_subscription, ",",
290 sizeof(rt->last_subscription));
291 ff_rdt_subscribe_rule(
292 rt->last_subscription,
293 sizeof(rt->last_subscription), i, rule_nr);
300 av_strlcatf(cmd, sizeof(cmd), "%s\r\n", rt->last_subscription);
301 ff_rtsp_send_cmd(s, "SET_PARAMETER", rt->control_uri,
303 if (reply->status_code != RTSP_STATUS_OK)
304 return AVERROR_INVALIDDATA;
305 rt->need_subscription = 0;
307 if (rt->state == RTSP_STATE_STREAMING)
312 ret = ff_rtsp_fetch_packet(s, pkt);
314 if (ret == AVERROR(ETIMEDOUT) && !rt->packets) {
315 if (rt->lower_transport == RTSP_LOWER_TRANSPORT_UDP &&
316 rt->lower_transport_mask & (1 << RTSP_LOWER_TRANSPORT_TCP)) {
317 RTSPMessageHeader reply1, *reply = &reply1;
318 av_log(s, AV_LOG_WARNING, "UDP timeout, retrying with TCP\n");
319 if (rtsp_read_pause(s) != 0)
321 // TEARDOWN is required on Real-RTSP, but might make
322 // other servers close the connection.
323 if (rt->server_type == RTSP_SERVER_REAL)
324 ff_rtsp_send_cmd(s, "TEARDOWN", rt->control_uri, NULL,
326 rt->session_id[0] = '\0';
327 if (resetup_tcp(s) == 0) {
328 rt->state = RTSP_STATE_IDLE;
329 rt->need_subscription = 1;
330 if (rtsp_read_play(s) != 0)
340 /* send dummy request to keep TCP connection alive */
341 if ((av_gettime() - rt->last_cmd_time) / 1000000 >= rt->timeout / 2) {
342 if (rt->server_type == RTSP_SERVER_WMS) {
343 ff_rtsp_send_cmd_async(s, "GET_PARAMETER", rt->control_uri, NULL);
345 ff_rtsp_send_cmd_async(s, "OPTIONS", "*", NULL);
352 static int rtsp_read_seek(AVFormatContext *s, int stream_index,
353 int64_t timestamp, int flags)
355 RTSPState *rt = s->priv_data;
357 rt->seek_timestamp = av_rescale_q(timestamp,
358 s->streams[stream_index]->time_base,
362 case RTSP_STATE_IDLE:
364 case RTSP_STATE_STREAMING:
365 if (rtsp_read_pause(s) != 0)
367 rt->state = RTSP_STATE_SEEKING;
368 if (rtsp_read_play(s) != 0)
371 case RTSP_STATE_PAUSED:
372 rt->state = RTSP_STATE_IDLE;
378 static int rtsp_read_close(AVFormatContext *s)
380 RTSPState *rt = s->priv_data;
383 /* NOTE: it is valid to flush the buffer here */
384 if (rt->lower_transport == RTSP_LOWER_TRANSPORT_TCP) {
385 avio_close(&rt->rtsp_gb);
388 ff_rtsp_send_cmd_async(s, "TEARDOWN", rt->control_uri, NULL);
390 ff_rtsp_close_streams(s);
391 ff_rtsp_close_connections(s);
393 rt->real_setup = NULL;
394 av_freep(&rt->real_setup_cache);
398 AVInputFormat ff_rtsp_demuxer = {
400 NULL_IF_CONFIG_SMALL("RTSP input format"),
407 .flags = AVFMT_NOFILE,
408 .read_play = rtsp_read_play,
409 .read_pause = rtsp_read_pause,