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
28 #include "os_support.h"
31 #include "avio_internal.h"
32 #include "libavutil/intreadwrite.h"
33 #include "libavutil/avstring.h"
34 #include "libavutil/time.h"
37 #define SDP_MAX_SIZE 16384
39 static const AVClass rtsp_muxer_class = {
40 .class_name = "RTSP muxer",
41 .item_name = av_default_item_name,
42 .option = ff_rtsp_options,
43 .version = LIBAVUTIL_VERSION_INT,
46 int ff_rtsp_setup_output_streams(AVFormatContext *s, const char *addr)
48 RTSPState *rt = s->priv_data;
49 RTSPMessageHeader reply1, *reply = &reply1;
52 AVFormatContext sdp_ctx, *ctx_array[1];
54 s->start_time_realtime = av_gettime();
56 /* Announce the stream */
57 sdp = av_mallocz(SDP_MAX_SIZE);
59 return AVERROR(ENOMEM);
60 /* We create the SDP based on the RTSP AVFormatContext where we
61 * aren't allowed to change the filename field. (We create the SDP
62 * based on the RTSP context since the contexts for the RTP streams
63 * don't exist yet.) In order to specify a custom URL with the actual
64 * peer IP instead of the originally specified hostname, we create
65 * a temporary copy of the AVFormatContext, where the custom URL is set.
67 * FIXME: Create the SDP without copying the AVFormatContext.
68 * This either requires setting up the RTP stream AVFormatContexts
69 * already here (complicating things immensely) or getting a more
70 * flexible SDP creation interface.
73 ff_url_join(sdp_ctx.filename, sizeof(sdp_ctx.filename),
74 "rtsp", NULL, addr, -1, NULL);
75 ctx_array[0] = &sdp_ctx;
76 if (av_sdp_create(ctx_array, 1, sdp, SDP_MAX_SIZE)) {
78 return AVERROR_INVALIDDATA;
80 av_log(s, AV_LOG_VERBOSE, "SDP:\n%s\n", sdp);
81 ff_rtsp_send_cmd_with_content(s, "ANNOUNCE", rt->control_uri,
82 "Content-Type: application/sdp\r\n",
83 reply, NULL, sdp, strlen(sdp));
85 if (reply->status_code != RTSP_STATUS_OK)
86 return AVERROR_INVALIDDATA;
88 /* Set up the RTSPStreams for each AVStream */
89 for (i = 0; i < s->nb_streams; i++) {
92 rtsp_st = av_mallocz(sizeof(RTSPStream));
94 return AVERROR(ENOMEM);
95 dynarray_add(&rt->rtsp_streams, &rt->nb_rtsp_streams, rtsp_st);
97 rtsp_st->stream_index = i;
99 av_strlcpy(rtsp_st->control_url, rt->control_uri, sizeof(rtsp_st->control_url));
100 /* Note, this must match the relative uri set in the sdp content */
101 av_strlcatf(rtsp_st->control_url, sizeof(rtsp_st->control_url),
108 static int rtsp_write_record(AVFormatContext *s)
110 RTSPState *rt = s->priv_data;
111 RTSPMessageHeader reply1, *reply = &reply1;
114 snprintf(cmd, sizeof(cmd),
115 "Range: npt=0.000-\r\n");
116 ff_rtsp_send_cmd(s, "RECORD", rt->control_uri, cmd, reply, NULL);
117 if (reply->status_code != RTSP_STATUS_OK)
119 rt->state = RTSP_STATE_STREAMING;
123 static int rtsp_write_header(AVFormatContext *s)
127 ret = ff_rtsp_connect(s);
131 if (rtsp_write_record(s) < 0) {
132 ff_rtsp_close_streams(s);
133 ff_rtsp_close_connections(s);
134 return AVERROR_INVALIDDATA;
139 int ff_rtsp_tcp_write_packet(AVFormatContext *s, RTSPStream *rtsp_st)
141 RTSPState *rt = s->priv_data;
142 AVFormatContext *rtpctx = rtsp_st->transport_priv;
145 uint8_t *interleave_header, *interleaved_packet;
147 size = avio_close_dyn_buf(rtpctx->pb, &buf);
151 uint32_t packet_len = AV_RB32(ptr);
153 /* The interleaving header is exactly 4 bytes, which happens to be
154 * the same size as the packet length header from
155 * ffio_open_dyn_packet_buf. So by writing the interleaving header
156 * over these bytes, we get a consecutive interleaved packet
157 * that can be written in one call. */
158 interleaved_packet = interleave_header = ptr;
161 if (packet_len > size || packet_len < 2)
163 if (RTP_PT_IS_RTCP(ptr[1]))
164 id = rtsp_st->interleaved_max; /* RTCP */
166 id = rtsp_st->interleaved_min; /* RTP */
167 interleave_header[0] = '$';
168 interleave_header[1] = id;
169 AV_WB16(interleave_header + 2, packet_len);
170 ffurl_write(rt->rtsp_hd_out, interleaved_packet, 4 + packet_len);
175 return ffio_open_dyn_packet_buf(&rtpctx->pb, RTSP_TCP_MAX_PACKET_SIZE);
178 static int rtsp_write_packet(AVFormatContext *s, AVPacket *pkt)
180 RTSPState *rt = s->priv_data;
183 struct pollfd p = {ffurl_get_file_handle(rt->rtsp_hd), POLLIN, 0};
184 AVFormatContext *rtpctx;
191 if (p.revents & POLLIN) {
192 RTSPMessageHeader reply;
194 /* Don't let ff_rtsp_read_reply handle interleaved packets,
195 * since it would block and wait for an RTSP reply on the socket
196 * (which may not be coming any time soon) if it handles
197 * interleaved packets internally. */
198 ret = ff_rtsp_read_reply(s, &reply, NULL, 1, NULL);
200 return AVERROR(EPIPE);
202 ff_rtsp_skip_packet(s);
203 /* XXX: parse message */
204 if (rt->state != RTSP_STATE_STREAMING)
205 return AVERROR(EPIPE);
209 if (pkt->stream_index < 0 || pkt->stream_index >= rt->nb_rtsp_streams)
210 return AVERROR_INVALIDDATA;
211 rtsp_st = rt->rtsp_streams[pkt->stream_index];
212 rtpctx = rtsp_st->transport_priv;
214 ret = ff_write_chained(rtpctx, 0, pkt, s);
215 /* ff_write_chained does all the RTP packetization. If using TCP as
216 * transport, rtpctx->pb is only a dyn_packet_buf that queues up the
217 * packets, so we need to send them out on the TCP connection separately.
219 if (!ret && rt->lower_transport == RTSP_LOWER_TRANSPORT_TCP)
220 ret = ff_rtsp_tcp_write_packet(s, rtsp_st);
224 static int rtsp_write_close(AVFormatContext *s)
226 RTSPState *rt = s->priv_data;
228 // If we want to send RTCP_BYE packets, these are sent by av_write_trailer.
229 // Thus call this on all streams before doing the teardown. This is
230 // done within ff_rtsp_undo_setup.
231 ff_rtsp_undo_setup(s, 1);
233 ff_rtsp_send_cmd_async(s, "TEARDOWN", rt->control_uri, NULL);
235 ff_rtsp_close_streams(s);
236 ff_rtsp_close_connections(s);
241 AVOutputFormat ff_rtsp_muxer = {
243 .long_name = NULL_IF_CONFIG_SMALL("RTSP output"),
244 .priv_data_size = sizeof(RTSPState),
245 .audio_codec = AV_CODEC_ID_AAC,
246 .video_codec = AV_CODEC_ID_MPEG4,
247 .write_header = rtsp_write_header,
248 .write_packet = rtsp_write_packet,
249 .write_trailer = rtsp_write_close,
250 .flags = AVFMT_NOFILE | AVFMT_GLOBALHEADER,
251 .priv_class = &rtsp_muxer_class,