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
29 #include "os_support.h"
32 #include "libavutil/intreadwrite.h"
33 #include "libavutil/avstring.h"
35 #define SDP_MAX_SIZE 16384
37 int ff_rtsp_setup_output_streams(AVFormatContext *s, const char *addr)
39 RTSPState *rt = s->priv_data;
40 RTSPMessageHeader reply1, *reply = &reply1;
43 AVFormatContext sdp_ctx, *ctx_array[1];
45 s->start_time_realtime = av_gettime();
47 /* Announce the stream */
48 sdp = av_mallocz(SDP_MAX_SIZE);
50 return AVERROR(ENOMEM);
51 /* We create the SDP based on the RTSP AVFormatContext where we
52 * aren't allowed to change the filename field. (We create the SDP
53 * based on the RTSP context since the contexts for the RTP streams
54 * don't exist yet.) In order to specify a custom URL with the actual
55 * peer IP instead of the originally specified hostname, we create
56 * a temporary copy of the AVFormatContext, where the custom URL is set.
58 * FIXME: Create the SDP without copying the AVFormatContext.
59 * This either requires setting up the RTP stream AVFormatContexts
60 * already here (complicating things immensely) or getting a more
61 * flexible SDP creation interface.
64 ff_url_join(sdp_ctx.filename, sizeof(sdp_ctx.filename),
65 "rtsp", NULL, addr, -1, NULL);
66 ctx_array[0] = &sdp_ctx;
67 if (avf_sdp_create(ctx_array, 1, sdp, SDP_MAX_SIZE)) {
69 return AVERROR_INVALIDDATA;
71 av_log(s, AV_LOG_VERBOSE, "SDP:\n%s\n", sdp);
72 ff_rtsp_send_cmd_with_content(s, "ANNOUNCE", rt->control_uri,
73 "Content-Type: application/sdp\r\n",
74 reply, NULL, sdp, strlen(sdp));
76 if (reply->status_code != RTSP_STATUS_OK)
77 return AVERROR_INVALIDDATA;
79 /* Set up the RTSPStreams for each AVStream */
80 for (i = 0; i < s->nb_streams; i++) {
82 AVStream *st = s->streams[i];
84 rtsp_st = av_mallocz(sizeof(RTSPStream));
86 return AVERROR(ENOMEM);
87 dynarray_add(&rt->rtsp_streams, &rt->nb_rtsp_streams, rtsp_st);
89 st->priv_data = rtsp_st;
90 rtsp_st->stream_index = i;
92 av_strlcpy(rtsp_st->control_url, rt->control_uri, sizeof(rtsp_st->control_url));
93 /* Note, this must match the relative uri set in the sdp content */
94 av_strlcatf(rtsp_st->control_url, sizeof(rtsp_st->control_url),
101 static int rtsp_write_record(AVFormatContext *s)
103 RTSPState *rt = s->priv_data;
104 RTSPMessageHeader reply1, *reply = &reply1;
107 snprintf(cmd, sizeof(cmd),
108 "Range: npt=%0.3f-\r\n",
110 ff_rtsp_send_cmd(s, "RECORD", rt->control_uri, cmd, reply, NULL);
111 if (reply->status_code != RTSP_STATUS_OK)
113 rt->state = RTSP_STATE_STREAMING;
117 static int rtsp_write_header(AVFormatContext *s)
121 ret = ff_rtsp_connect(s);
125 if (rtsp_write_record(s) < 0) {
126 ff_rtsp_close_streams(s);
127 ff_rtsp_close_connections(s);
128 return AVERROR_INVALIDDATA;
133 static int tcp_write_packet(AVFormatContext *s, RTSPStream *rtsp_st)
135 RTSPState *rt = s->priv_data;
136 AVFormatContext *rtpctx = rtsp_st->transport_priv;
139 uint8_t *interleave_header, *interleaved_packet;
141 size = url_close_dyn_buf(rtpctx->pb, &buf);
144 uint32_t packet_len = AV_RB32(ptr);
146 /* The interleaving header is exactly 4 bytes, which happens to be
147 * the same size as the packet length header from
148 * url_open_dyn_packet_buf. So by writing the interleaving header
149 * over these bytes, we get a consecutive interleaved packet
150 * that can be written in one call. */
151 interleaved_packet = interleave_header = ptr;
154 if (packet_len > size || packet_len < 2)
156 if (ptr[1] >= RTCP_SR && ptr[1] <= RTCP_APP)
157 id = rtsp_st->interleaved_max; /* RTCP */
159 id = rtsp_st->interleaved_min; /* RTP */
160 interleave_header[0] = '$';
161 interleave_header[1] = id;
162 AV_WB16(interleave_header + 2, packet_len);
163 url_write(rt->rtsp_hd_out, interleaved_packet, 4 + packet_len);
168 url_open_dyn_packet_buf(&rtpctx->pb, RTSP_TCP_MAX_PACKET_SIZE);
172 static int rtsp_write_packet(AVFormatContext *s, AVPacket *pkt)
174 RTSPState *rt = s->priv_data;
177 struct pollfd p = {url_get_file_handle(rt->rtsp_hd), POLLIN, 0};
178 AVFormatContext *rtpctx;
185 if (p.revents & POLLIN) {
186 RTSPMessageHeader reply;
188 /* Don't let ff_rtsp_read_reply handle interleaved packets,
189 * since it would block and wait for an RTSP reply on the socket
190 * (which may not be coming any time soon) if it handles
191 * interleaved packets internally. */
192 ret = ff_rtsp_read_reply(s, &reply, NULL, 1, NULL);
194 return AVERROR(EPIPE);
196 ff_rtsp_skip_packet(s);
197 /* XXX: parse message */
198 if (rt->state != RTSP_STATE_STREAMING)
199 return AVERROR(EPIPE);
203 if (pkt->stream_index < 0 || pkt->stream_index >= rt->nb_rtsp_streams)
204 return AVERROR_INVALIDDATA;
205 rtsp_st = rt->rtsp_streams[pkt->stream_index];
206 rtpctx = rtsp_st->transport_priv;
208 ret = ff_write_chained(rtpctx, 0, pkt, s);
209 /* ff_write_chained does all the RTP packetization. If using TCP as
210 * transport, rtpctx->pb is only a dyn_packet_buf that queues up the
211 * packets, so we need to send them out on the TCP connection separately.
213 if (!ret && rt->lower_transport == RTSP_LOWER_TRANSPORT_TCP)
214 ret = tcp_write_packet(s, rtsp_st);
218 static int rtsp_write_close(AVFormatContext *s)
220 RTSPState *rt = s->priv_data;
222 ff_rtsp_send_cmd_async(s, "TEARDOWN", rt->control_uri, NULL);
224 ff_rtsp_close_streams(s);
225 ff_rtsp_close_connections(s);
230 AVOutputFormat ff_rtsp_muxer = {
232 NULL_IF_CONFIG_SMALL("RTSP output format"),
241 .flags = AVFMT_NOFILE | AVFMT_GLOBALHEADER,