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/opt.h"
29 #include "os_support.h"
35 //#define DEBUG_RTP_TCP
37 static int rtsp_read_play(AVFormatContext *s)
39 RTSPState *rt = s->priv_data;
40 RTSPMessageHeader reply1, *reply = &reply1;
44 av_log(s, AV_LOG_DEBUG, "hello state=%d\n", rt->state);
47 if (!(rt->server_type == RTSP_SERVER_REAL && rt->need_subscription)) {
48 if (rt->transport == RTSP_TRANSPORT_RTP) {
49 for (i = 0; i < rt->nb_rtsp_streams; i++) {
50 RTSPStream *rtsp_st = rt->rtsp_streams[i];
51 RTPDemuxContext *rtpctx = rtsp_st->transport_priv;
54 ff_rtp_reset_packet_queue(rtpctx);
55 rtpctx->last_rtcp_ntp_time = AV_NOPTS_VALUE;
56 rtpctx->first_rtcp_ntp_time = AV_NOPTS_VALUE;
57 rtpctx->base_timestamp = 0;
58 rtpctx->rtcp_ts_offset = 0;
61 if (rt->state == RTSP_STATE_PAUSED) {
64 snprintf(cmd, sizeof(cmd),
65 "Range: npt=%"PRId64".%03"PRId64"-\r\n",
66 rt->seek_timestamp / AV_TIME_BASE,
67 rt->seek_timestamp / (AV_TIME_BASE / 1000) % 1000);
69 ff_rtsp_send_cmd(s, "PLAY", rt->control_uri, cmd, reply, NULL);
70 if (reply->status_code != RTSP_STATUS_OK) {
73 if (rt->transport == RTSP_TRANSPORT_RTP &&
74 reply->range_start != AV_NOPTS_VALUE) {
75 for (i = 0; i < rt->nb_rtsp_streams; i++) {
76 RTSPStream *rtsp_st = rt->rtsp_streams[i];
77 RTPDemuxContext *rtpctx = rtsp_st->transport_priv;
79 if (!rtpctx || rtsp_st->stream_index < 0)
81 st = s->streams[rtsp_st->stream_index];
82 rtpctx->range_start_offset =
83 av_rescale_q(reply->range_start, AV_TIME_BASE_Q,
88 rt->state = RTSP_STATE_STREAMING;
92 /* pause the stream */
93 static int rtsp_read_pause(AVFormatContext *s)
95 RTSPState *rt = s->priv_data;
96 RTSPMessageHeader reply1, *reply = &reply1;
98 if (rt->state != RTSP_STATE_STREAMING)
100 else if (!(rt->server_type == RTSP_SERVER_REAL && rt->need_subscription)) {
101 ff_rtsp_send_cmd(s, "PAUSE", rt->control_uri, NULL, reply, NULL);
102 if (reply->status_code != RTSP_STATUS_OK) {
106 rt->state = RTSP_STATE_PAUSED;
110 int ff_rtsp_setup_input_streams(AVFormatContext *s, RTSPMessageHeader *reply)
112 RTSPState *rt = s->priv_data;
114 unsigned char *content = NULL;
117 /* describe the stream */
118 snprintf(cmd, sizeof(cmd),
119 "Accept: application/sdp\r\n");
120 if (rt->server_type == RTSP_SERVER_REAL) {
122 * The Require: attribute is needed for proper streaming from
126 "Require: com.real.retain-entity-for-setup\r\n",
129 ff_rtsp_send_cmd(s, "DESCRIBE", rt->control_uri, cmd, reply, &content);
131 return AVERROR_INVALIDDATA;
132 if (reply->status_code != RTSP_STATUS_OK) {
134 return AVERROR_INVALIDDATA;
137 av_log(s, AV_LOG_VERBOSE, "SDP:\n%s\n", content);
138 /* now we got the SDP description, we parse it */
139 ret = ff_sdp_parse(s, (const char *)content);
147 static int rtsp_probe(AVProbeData *p)
149 if (av_strstart(p->filename, "rtsp:", NULL))
150 return AVPROBE_SCORE_MAX;
154 static int rtsp_read_header(AVFormatContext *s,
155 AVFormatParameters *ap)
157 RTSPState *rt = s->priv_data;
160 ret = ff_rtsp_connect(s);
164 rt->real_setup_cache = av_mallocz(2 * s->nb_streams * sizeof(*rt->real_setup_cache));
165 if (!rt->real_setup_cache)
166 return AVERROR(ENOMEM);
167 rt->real_setup = rt->real_setup_cache + s->nb_streams;
169 #if FF_API_FORMAT_PARAMETERS
170 if (ap->initial_pause)
171 rt->initial_pause = ap->initial_pause;
174 if (rt->initial_pause) {
175 /* do not start immediately */
177 if (rtsp_read_play(s) < 0) {
178 ff_rtsp_close_streams(s);
179 ff_rtsp_close_connections(s);
180 return AVERROR_INVALIDDATA;
187 int ff_rtsp_tcp_read_packet(AVFormatContext *s, RTSPStream **prtsp_st,
188 uint8_t *buf, int buf_size)
190 RTSPState *rt = s->priv_data;
195 av_dlog(s, "tcp_read_packet:\n");
199 RTSPMessageHeader reply;
201 ret = ff_rtsp_read_reply(s, &reply, NULL, 1, NULL);
204 if (ret == 1) /* received '$' */
206 /* XXX: parse message */
207 if (rt->state != RTSP_STATE_STREAMING)
210 ret = ffurl_read_complete(rt->rtsp_hd, buf, 3);
214 len = AV_RB16(buf + 1);
216 av_dlog(s, "id=%d len=%d\n", id, len);
218 if (len > buf_size || len < 12)
221 ret = ffurl_read_complete(rt->rtsp_hd, buf, len);
224 if (rt->transport == RTSP_TRANSPORT_RDT &&
225 ff_rdt_parse_header(buf, len, &id, NULL, NULL, NULL, NULL) < 0)
228 /* find the matching stream */
229 for (i = 0; i < rt->nb_rtsp_streams; i++) {
230 rtsp_st = rt->rtsp_streams[i];
231 if (id >= rtsp_st->interleaved_min &&
232 id <= rtsp_st->interleaved_max)
241 static int resetup_tcp(AVFormatContext *s)
243 RTSPState *rt = s->priv_data;
247 av_url_split(NULL, 0, NULL, 0, host, sizeof(host), &port, NULL, 0,
249 ff_rtsp_undo_setup(s);
250 return ff_rtsp_make_setup_request(s, host, port, RTSP_LOWER_TRANSPORT_TCP,
254 static int rtsp_read_packet(AVFormatContext *s, AVPacket *pkt)
256 RTSPState *rt = s->priv_data;
258 RTSPMessageHeader reply1, *reply = &reply1;
262 if (rt->server_type == RTSP_SERVER_REAL) {
265 for (i = 0; i < s->nb_streams; i++)
266 rt->real_setup[i] = s->streams[i]->discard;
268 if (!rt->need_subscription) {
269 if (memcmp (rt->real_setup, rt->real_setup_cache,
270 sizeof(enum AVDiscard) * s->nb_streams)) {
271 snprintf(cmd, sizeof(cmd),
272 "Unsubscribe: %s\r\n",
273 rt->last_subscription);
274 ff_rtsp_send_cmd(s, "SET_PARAMETER", rt->control_uri,
276 if (reply->status_code != RTSP_STATUS_OK)
277 return AVERROR_INVALIDDATA;
278 rt->need_subscription = 1;
282 if (rt->need_subscription) {
283 int r, rule_nr, first = 1;
285 memcpy(rt->real_setup_cache, rt->real_setup,
286 sizeof(enum AVDiscard) * s->nb_streams);
287 rt->last_subscription[0] = 0;
289 snprintf(cmd, sizeof(cmd),
291 for (i = 0; i < rt->nb_rtsp_streams; i++) {
293 for (r = 0; r < s->nb_streams; r++) {
294 if (s->streams[r]->id == i) {
295 if (s->streams[r]->discard != AVDISCARD_ALL) {
297 av_strlcat(rt->last_subscription, ",",
298 sizeof(rt->last_subscription));
299 ff_rdt_subscribe_rule(
300 rt->last_subscription,
301 sizeof(rt->last_subscription), i, rule_nr);
308 av_strlcatf(cmd, sizeof(cmd), "%s\r\n", rt->last_subscription);
309 ff_rtsp_send_cmd(s, "SET_PARAMETER", rt->control_uri,
311 if (reply->status_code != RTSP_STATUS_OK)
312 return AVERROR_INVALIDDATA;
313 rt->need_subscription = 0;
315 if (rt->state == RTSP_STATE_STREAMING)
320 ret = ff_rtsp_fetch_packet(s, pkt);
322 if (ret == AVERROR(ETIMEDOUT) && !rt->packets) {
323 if (rt->lower_transport == RTSP_LOWER_TRANSPORT_UDP &&
324 rt->lower_transport_mask & (1 << RTSP_LOWER_TRANSPORT_TCP)) {
325 RTSPMessageHeader reply1, *reply = &reply1;
326 av_log(s, AV_LOG_WARNING, "UDP timeout, retrying with TCP\n");
327 if (rtsp_read_pause(s) != 0)
329 // TEARDOWN is required on Real-RTSP, but might make
330 // other servers close the connection.
331 if (rt->server_type == RTSP_SERVER_REAL)
332 ff_rtsp_send_cmd(s, "TEARDOWN", rt->control_uri, NULL,
334 rt->session_id[0] = '\0';
335 if (resetup_tcp(s) == 0) {
336 rt->state = RTSP_STATE_IDLE;
337 rt->need_subscription = 1;
338 if (rtsp_read_play(s) != 0)
348 /* send dummy request to keep TCP connection alive */
349 if ((av_gettime() - rt->last_cmd_time) / 1000000 >= rt->timeout / 2) {
350 if (rt->server_type == RTSP_SERVER_WMS ||
351 (rt->server_type != RTSP_SERVER_REAL &&
352 rt->get_parameter_supported)) {
353 ff_rtsp_send_cmd_async(s, "GET_PARAMETER", rt->control_uri, NULL);
355 ff_rtsp_send_cmd_async(s, "OPTIONS", "*", NULL);
362 static int rtsp_read_seek(AVFormatContext *s, int stream_index,
363 int64_t timestamp, int flags)
365 RTSPState *rt = s->priv_data;
367 rt->seek_timestamp = av_rescale_q(timestamp,
368 s->streams[stream_index]->time_base,
372 case RTSP_STATE_IDLE:
374 case RTSP_STATE_STREAMING:
375 if (rtsp_read_pause(s) != 0)
377 rt->state = RTSP_STATE_SEEKING;
378 if (rtsp_read_play(s) != 0)
381 case RTSP_STATE_PAUSED:
382 rt->state = RTSP_STATE_IDLE;
388 static int rtsp_read_close(AVFormatContext *s)
390 RTSPState *rt = s->priv_data;
393 /* NOTE: it is valid to flush the buffer here */
394 if (rt->lower_transport == RTSP_LOWER_TRANSPORT_TCP) {
395 avio_close(&rt->rtsp_gb);
398 ff_rtsp_send_cmd_async(s, "TEARDOWN", rt->control_uri, NULL);
400 ff_rtsp_close_streams(s);
401 ff_rtsp_close_connections(s);
403 rt->real_setup = NULL;
404 av_freep(&rt->real_setup_cache);
408 static const AVOption options[] = {
409 { "initial_pause", "Don't start playing the stream immediately", offsetof(RTSPState, initial_pause), FF_OPT_TYPE_INT, {.dbl = 0}, 0, 1, AV_OPT_FLAG_DECODING_PARAM },
413 const AVClass rtsp_demuxer_class = {
414 .class_name = "RTSP demuxer",
415 .item_name = av_default_item_name,
417 .version = LIBAVUTIL_VERSION_INT,
420 AVInputFormat ff_rtsp_demuxer = {
422 NULL_IF_CONFIG_SMALL("RTSP input format"),
429 .flags = AVFMT_NOFILE,
430 .read_play = rtsp_read_play,
431 .read_pause = rtsp_read_pause,
432 .priv_class = &rtsp_demuxer_class,