3 * Copyright (c) 2010 Martin Storsjo
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
26 #include <sys/select.h>
31 #include <libavutil/intreadwrite.h>
33 static int rtsp_write_record(AVFormatContext *s)
35 RTSPState *rt = s->priv_data;
36 RTSPMessageHeader reply1, *reply = &reply1;
39 snprintf(cmd, sizeof(cmd),
40 "Range: npt=%0.3f-\r\n",
42 ff_rtsp_send_cmd(s, "RECORD", rt->control_uri, cmd, reply, NULL);
43 if (reply->status_code != RTSP_STATUS_OK)
45 rt->state = RTSP_STATE_STREAMING;
49 static int rtsp_write_header(AVFormatContext *s)
51 RTSPState *rt = s->priv_data;
54 ret = ff_rtsp_connect(s);
58 if (rtsp_write_record(s) < 0) {
59 ff_rtsp_close_streams(s);
60 url_close(rt->rtsp_hd);
61 return AVERROR_INVALIDDATA;
66 static int tcp_write_packet(AVFormatContext *s, RTSPStream *rtsp_st)
68 RTSPState *rt = s->priv_data;
69 AVFormatContext *rtpctx = rtsp_st->transport_priv;
72 uint8_t *interleave_header, *interleaved_packet;
74 size = url_close_dyn_buf(rtpctx->pb, &buf);
77 uint32_t packet_len = AV_RB32(ptr);
79 /* The interleaving header is exactly 4 bytes, which happens to be
80 * the same size as the packet length header from
81 * url_open_dyn_packet_buf. So by writing the interleaving header
82 * over these bytes, we get a consecutive interleaved packet
83 * that can be written in one call. */
84 interleaved_packet = interleave_header = ptr;
87 if (packet_len > size || packet_len < 2)
89 if (ptr[1] >= 200 && ptr[1] <= 204)
90 id = rtsp_st->interleaved_max; /* RTCP */
92 id = rtsp_st->interleaved_min; /* RTP */
93 interleave_header[0] = '$';
94 interleave_header[1] = id;
95 AV_WB16(interleave_header + 2, packet_len);
96 url_write(rt->rtsp_hd, interleaved_packet, 4 + packet_len);
101 url_open_dyn_packet_buf(&rtpctx->pb, RTSP_TCP_MAX_PACKET_SIZE);
105 static int rtsp_write_packet(AVFormatContext *s, AVPacket *pkt)
107 RTSPState *rt = s->priv_data;
112 AVFormatContext *rtpctx;
115 tcp_fd = url_get_file_handle(rt->rtsp_hd);
119 FD_SET(tcp_fd, &rfds);
122 n = select(tcp_fd + 1, &rfds, NULL, NULL, &tv);
125 if (FD_ISSET(tcp_fd, &rfds)) {
126 RTSPMessageHeader reply;
128 /* Don't let ff_rtsp_read_reply handle interleaved packets,
129 * since it would block and wait for an RTSP reply on the socket
130 * (which may not be coming any time soon) if it handles
131 * interleaved packets internally. */
132 ret = ff_rtsp_read_reply(s, &reply, NULL, 1);
134 return AVERROR(EPIPE);
136 ff_rtsp_skip_packet(s);
137 /* XXX: parse message */
138 if (rt->state != RTSP_STATE_STREAMING)
139 return AVERROR(EPIPE);
143 if (pkt->stream_index < 0 || pkt->stream_index >= rt->nb_rtsp_streams)
144 return AVERROR_INVALIDDATA;
145 rtsp_st = rt->rtsp_streams[pkt->stream_index];
146 rtpctx = rtsp_st->transport_priv;
148 ret = ff_write_chained(rtpctx, 0, pkt, s);
149 /* ff_write_chained does all the RTP packetization. If using TCP as
150 * transport, rtpctx->pb is only a dyn_packet_buf that queues up the
151 * packets, so we need to send them out on the TCP connection separately.
153 if (!ret && rt->lower_transport == RTSP_LOWER_TRANSPORT_TCP)
154 ret = tcp_write_packet(s, rtsp_st);
158 static int rtsp_write_close(AVFormatContext *s)
160 RTSPState *rt = s->priv_data;
162 ff_rtsp_send_cmd_async(s, "TEARDOWN", rt->control_uri, NULL);
164 ff_rtsp_close_streams(s);
165 url_close(rt->rtsp_hd);
170 AVOutputFormat rtsp_muxer = {
172 NULL_IF_CONFIG_SMALL("RTSP output format"),
181 .flags = AVFMT_NOFILE | AVFMT_GLOBALHEADER,