]> git.sesse.net Git - ffmpeg/blob - libavformat/rtspenc.c
avio: make url_get_file_handle() internal.
[ffmpeg] / libavformat / rtspenc.c
1 /*
2  * RTSP muxer
3  * Copyright (c) 2010 Martin Storsjo
4  *
5  * This file is part of Libav.
6  *
7  * Libav 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.
11  *
12  * Libav 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.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with Libav; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21
22 #include "avformat.h"
23
24 #include <sys/time.h>
25 #if HAVE_POLL_H
26 #include <poll.h>
27 #endif
28 #include "network.h"
29 #include "os_support.h"
30 #include "rtsp.h"
31 #include "internal.h"
32 #include "avio_internal.h"
33 #include "libavutil/intreadwrite.h"
34 #include "libavutil/avstring.h"
35 #include "url.h"
36
37 #define SDP_MAX_SIZE 16384
38
39 int ff_rtsp_setup_output_streams(AVFormatContext *s, const char *addr)
40 {
41     RTSPState *rt = s->priv_data;
42     RTSPMessageHeader reply1, *reply = &reply1;
43     int i;
44     char *sdp;
45     AVFormatContext sdp_ctx, *ctx_array[1];
46
47     s->start_time_realtime = av_gettime();
48
49     /* Announce the stream */
50     sdp = av_mallocz(SDP_MAX_SIZE);
51     if (sdp == NULL)
52         return AVERROR(ENOMEM);
53     /* We create the SDP based on the RTSP AVFormatContext where we
54      * aren't allowed to change the filename field. (We create the SDP
55      * based on the RTSP context since the contexts for the RTP streams
56      * don't exist yet.) In order to specify a custom URL with the actual
57      * peer IP instead of the originally specified hostname, we create
58      * a temporary copy of the AVFormatContext, where the custom URL is set.
59      *
60      * FIXME: Create the SDP without copying the AVFormatContext.
61      * This either requires setting up the RTP stream AVFormatContexts
62      * already here (complicating things immensely) or getting a more
63      * flexible SDP creation interface.
64      */
65     sdp_ctx = *s;
66     ff_url_join(sdp_ctx.filename, sizeof(sdp_ctx.filename),
67                 "rtsp", NULL, addr, -1, NULL);
68     ctx_array[0] = &sdp_ctx;
69     if (avf_sdp_create(ctx_array, 1, sdp, SDP_MAX_SIZE)) {
70         av_free(sdp);
71         return AVERROR_INVALIDDATA;
72     }
73     av_log(s, AV_LOG_VERBOSE, "SDP:\n%s\n", sdp);
74     ff_rtsp_send_cmd_with_content(s, "ANNOUNCE", rt->control_uri,
75                                   "Content-Type: application/sdp\r\n",
76                                   reply, NULL, sdp, strlen(sdp));
77     av_free(sdp);
78     if (reply->status_code != RTSP_STATUS_OK)
79         return AVERROR_INVALIDDATA;
80
81     /* Set up the RTSPStreams for each AVStream */
82     for (i = 0; i < s->nb_streams; i++) {
83         RTSPStream *rtsp_st;
84
85         rtsp_st = av_mallocz(sizeof(RTSPStream));
86         if (!rtsp_st)
87             return AVERROR(ENOMEM);
88         dynarray_add(&rt->rtsp_streams, &rt->nb_rtsp_streams, rtsp_st);
89
90         rtsp_st->stream_index = i;
91
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),
95                     "/streamid=%d", i);
96     }
97
98     return 0;
99 }
100
101 static int rtsp_write_record(AVFormatContext *s)
102 {
103     RTSPState *rt = s->priv_data;
104     RTSPMessageHeader reply1, *reply = &reply1;
105     char cmd[1024];
106
107     snprintf(cmd, sizeof(cmd),
108              "Range: npt=0.000-\r\n");
109     ff_rtsp_send_cmd(s, "RECORD", rt->control_uri, cmd, reply, NULL);
110     if (reply->status_code != RTSP_STATUS_OK)
111         return -1;
112     rt->state = RTSP_STATE_STREAMING;
113     return 0;
114 }
115
116 static int rtsp_write_header(AVFormatContext *s)
117 {
118     int ret;
119
120     ret = ff_rtsp_connect(s);
121     if (ret)
122         return ret;
123
124     if (rtsp_write_record(s) < 0) {
125         ff_rtsp_close_streams(s);
126         ff_rtsp_close_connections(s);
127         return AVERROR_INVALIDDATA;
128     }
129     return 0;
130 }
131
132 static int tcp_write_packet(AVFormatContext *s, RTSPStream *rtsp_st)
133 {
134     RTSPState *rt = s->priv_data;
135     AVFormatContext *rtpctx = rtsp_st->transport_priv;
136     uint8_t *buf, *ptr;
137     int size;
138     uint8_t *interleave_header, *interleaved_packet;
139
140     size = avio_close_dyn_buf(rtpctx->pb, &buf);
141     ptr = buf;
142     while (size > 4) {
143         uint32_t packet_len = AV_RB32(ptr);
144         int id;
145         /* The interleaving header is exactly 4 bytes, which happens to be
146          * the same size as the packet length header from
147          * ffio_open_dyn_packet_buf. So by writing the interleaving header
148          * over these bytes, we get a consecutive interleaved packet
149          * that can be written in one call. */
150         interleaved_packet = interleave_header = ptr;
151         ptr += 4;
152         size -= 4;
153         if (packet_len > size || packet_len < 2)
154             break;
155         if (ptr[1] >= RTCP_SR && ptr[1] <= RTCP_APP)
156             id = rtsp_st->interleaved_max; /* RTCP */
157         else
158             id = rtsp_st->interleaved_min; /* RTP */
159         interleave_header[0] = '$';
160         interleave_header[1] = id;
161         AV_WB16(interleave_header + 2, packet_len);
162         ffurl_write(rt->rtsp_hd_out, interleaved_packet, 4 + packet_len);
163         ptr += packet_len;
164         size -= packet_len;
165     }
166     av_free(buf);
167     ffio_open_dyn_packet_buf(&rtpctx->pb, RTSP_TCP_MAX_PACKET_SIZE);
168     return 0;
169 }
170
171 static int rtsp_write_packet(AVFormatContext *s, AVPacket *pkt)
172 {
173     RTSPState *rt = s->priv_data;
174     RTSPStream *rtsp_st;
175     int n;
176     struct pollfd p = {ffurl_get_file_handle(rt->rtsp_hd), POLLIN, 0};
177     AVFormatContext *rtpctx;
178     int ret;
179
180     while (1) {
181         n = poll(&p, 1, 0);
182         if (n <= 0)
183             break;
184         if (p.revents & POLLIN) {
185             RTSPMessageHeader reply;
186
187             /* Don't let ff_rtsp_read_reply handle interleaved packets,
188              * since it would block and wait for an RTSP reply on the socket
189              * (which may not be coming any time soon) if it handles
190              * interleaved packets internally. */
191             ret = ff_rtsp_read_reply(s, &reply, NULL, 1, NULL);
192             if (ret < 0)
193                 return AVERROR(EPIPE);
194             if (ret == 1)
195                 ff_rtsp_skip_packet(s);
196             /* XXX: parse message */
197             if (rt->state != RTSP_STATE_STREAMING)
198                 return AVERROR(EPIPE);
199         }
200     }
201
202     if (pkt->stream_index < 0 || pkt->stream_index >= rt->nb_rtsp_streams)
203         return AVERROR_INVALIDDATA;
204     rtsp_st = rt->rtsp_streams[pkt->stream_index];
205     rtpctx = rtsp_st->transport_priv;
206
207     ret = ff_write_chained(rtpctx, 0, pkt, s);
208     /* ff_write_chained does all the RTP packetization. If using TCP as
209      * transport, rtpctx->pb is only a dyn_packet_buf that queues up the
210      * packets, so we need to send them out on the TCP connection separately.
211      */
212     if (!ret && rt->lower_transport == RTSP_LOWER_TRANSPORT_TCP)
213         ret = tcp_write_packet(s, rtsp_st);
214     return ret;
215 }
216
217 static int rtsp_write_close(AVFormatContext *s)
218 {
219     RTSPState *rt = s->priv_data;
220
221     ff_rtsp_send_cmd_async(s, "TEARDOWN", rt->control_uri, NULL);
222
223     ff_rtsp_close_streams(s);
224     ff_rtsp_close_connections(s);
225     ff_network_close();
226     return 0;
227 }
228
229 AVOutputFormat ff_rtsp_muxer = {
230     "rtsp",
231     NULL_IF_CONFIG_SMALL("RTSP output format"),
232     NULL,
233     NULL,
234     sizeof(RTSPState),
235     CODEC_ID_AAC,
236     CODEC_ID_MPEG4,
237     rtsp_write_header,
238     rtsp_write_packet,
239     rtsp_write_close,
240     .flags = AVFMT_NOFILE | AVFMT_GLOBALHEADER,
241 };
242