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);
139 return AVERROR_INVALIDDATA;
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 dprintf(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 dprintf(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)
232 static int rtsp_read_packet(AVFormatContext *s, AVPacket *pkt)
234 RTSPState *rt = s->priv_data;
236 RTSPMessageHeader reply1, *reply = &reply1;
239 if (rt->server_type == RTSP_SERVER_REAL) {
242 for (i = 0; i < s->nb_streams; i++)
243 rt->real_setup[i] = s->streams[i]->discard;
245 if (!rt->need_subscription) {
246 if (memcmp (rt->real_setup, rt->real_setup_cache,
247 sizeof(enum AVDiscard) * s->nb_streams)) {
248 snprintf(cmd, sizeof(cmd),
249 "Unsubscribe: %s\r\n",
250 rt->last_subscription);
251 ff_rtsp_send_cmd(s, "SET_PARAMETER", rt->control_uri,
253 if (reply->status_code != RTSP_STATUS_OK)
254 return AVERROR_INVALIDDATA;
255 rt->need_subscription = 1;
259 if (rt->need_subscription) {
260 int r, rule_nr, first = 1;
262 memcpy(rt->real_setup_cache, rt->real_setup,
263 sizeof(enum AVDiscard) * s->nb_streams);
264 rt->last_subscription[0] = 0;
266 snprintf(cmd, sizeof(cmd),
268 for (i = 0; i < rt->nb_rtsp_streams; i++) {
270 for (r = 0; r < s->nb_streams; r++) {
271 if (s->streams[r]->priv_data == rt->rtsp_streams[i]) {
272 if (s->streams[r]->discard != AVDISCARD_ALL) {
274 av_strlcat(rt->last_subscription, ",",
275 sizeof(rt->last_subscription));
276 ff_rdt_subscribe_rule(
277 rt->last_subscription,
278 sizeof(rt->last_subscription), i, rule_nr);
285 av_strlcatf(cmd, sizeof(cmd), "%s\r\n", rt->last_subscription);
286 ff_rtsp_send_cmd(s, "SET_PARAMETER", rt->control_uri,
288 if (reply->status_code != RTSP_STATUS_OK)
289 return AVERROR_INVALIDDATA;
290 rt->need_subscription = 0;
292 if (rt->state == RTSP_STATE_STREAMING)
297 ret = ff_rtsp_fetch_packet(s, pkt);
301 /* send dummy request to keep TCP connection alive */
302 if ((av_gettime() - rt->last_cmd_time) / 1000000 >= rt->timeout / 2) {
303 if (rt->server_type == RTSP_SERVER_WMS) {
304 ff_rtsp_send_cmd_async(s, "GET_PARAMETER", rt->control_uri, NULL);
306 ff_rtsp_send_cmd_async(s, "OPTIONS", "*", NULL);
313 static int rtsp_read_seek(AVFormatContext *s, int stream_index,
314 int64_t timestamp, int flags)
316 RTSPState *rt = s->priv_data;
318 rt->seek_timestamp = av_rescale_q(timestamp,
319 s->streams[stream_index]->time_base,
323 case RTSP_STATE_IDLE:
325 case RTSP_STATE_STREAMING:
326 if (rtsp_read_pause(s) != 0)
328 rt->state = RTSP_STATE_SEEKING;
329 if (rtsp_read_play(s) != 0)
332 case RTSP_STATE_PAUSED:
333 rt->state = RTSP_STATE_IDLE;
339 static int rtsp_read_close(AVFormatContext *s)
341 RTSPState *rt = s->priv_data;
344 /* NOTE: it is valid to flush the buffer here */
345 if (rt->lower_transport == RTSP_LOWER_TRANSPORT_TCP) {
346 url_fclose(&rt->rtsp_gb);
349 ff_rtsp_send_cmd_async(s, "TEARDOWN", rt->control_uri, NULL);
351 ff_rtsp_close_streams(s);
352 ff_rtsp_close_connections(s);
354 rt->real_setup = NULL;
355 av_freep(&rt->real_setup_cache);
359 AVInputFormat rtsp_demuxer = {
361 NULL_IF_CONFIG_SMALL("RTSP input format"),
368 .flags = AVFMT_NOFILE,
369 .read_play = rtsp_read_play,
370 .read_pause = rtsp_read_pause,