]> git.sesse.net Git - ffmpeg/blob - libavformat/rtspenc.c
avio: avio_ prefix for url_close_dyn_buf
[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 "libavutil/intreadwrite.h"
33 #include "libavutil/avstring.h"
34
35 #define SDP_MAX_SIZE 16384
36
37 int ff_rtsp_setup_output_streams(AVFormatContext *s, const char *addr)
38 {
39     RTSPState *rt = s->priv_data;
40     RTSPMessageHeader reply1, *reply = &reply1;
41     int i;
42     char *sdp;
43     AVFormatContext sdp_ctx, *ctx_array[1];
44
45     s->start_time_realtime = av_gettime();
46
47     /* Announce the stream */
48     sdp = av_mallocz(SDP_MAX_SIZE);
49     if (sdp == NULL)
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.
57      *
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.
62      */
63     sdp_ctx = *s;
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)) {
68         av_free(sdp);
69         return AVERROR_INVALIDDATA;
70     }
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));
75     av_free(sdp);
76     if (reply->status_code != RTSP_STATUS_OK)
77         return AVERROR_INVALIDDATA;
78
79     /* Set up the RTSPStreams for each AVStream */
80     for (i = 0; i < s->nb_streams; i++) {
81         RTSPStream *rtsp_st;
82
83         rtsp_st = av_mallocz(sizeof(RTSPStream));
84         if (!rtsp_st)
85             return AVERROR(ENOMEM);
86         dynarray_add(&rt->rtsp_streams, &rt->nb_rtsp_streams, rtsp_st);
87
88         rtsp_st->stream_index = i;
89
90         av_strlcpy(rtsp_st->control_url, rt->control_uri, sizeof(rtsp_st->control_url));
91         /* Note, this must match the relative uri set in the sdp content */
92         av_strlcatf(rtsp_st->control_url, sizeof(rtsp_st->control_url),
93                     "/streamid=%d", i);
94     }
95
96     return 0;
97 }
98
99 static int rtsp_write_record(AVFormatContext *s)
100 {
101     RTSPState *rt = s->priv_data;
102     RTSPMessageHeader reply1, *reply = &reply1;
103     char cmd[1024];
104
105     snprintf(cmd, sizeof(cmd),
106              "Range: npt=0.000-\r\n");
107     ff_rtsp_send_cmd(s, "RECORD", rt->control_uri, cmd, reply, NULL);
108     if (reply->status_code != RTSP_STATUS_OK)
109         return -1;
110     rt->state = RTSP_STATE_STREAMING;
111     return 0;
112 }
113
114 static int rtsp_write_header(AVFormatContext *s)
115 {
116     int ret;
117
118     ret = ff_rtsp_connect(s);
119     if (ret)
120         return ret;
121
122     if (rtsp_write_record(s) < 0) {
123         ff_rtsp_close_streams(s);
124         ff_rtsp_close_connections(s);
125         return AVERROR_INVALIDDATA;
126     }
127     return 0;
128 }
129
130 static int tcp_write_packet(AVFormatContext *s, RTSPStream *rtsp_st)
131 {
132     RTSPState *rt = s->priv_data;
133     AVFormatContext *rtpctx = rtsp_st->transport_priv;
134     uint8_t *buf, *ptr;
135     int size;
136     uint8_t *interleave_header, *interleaved_packet;
137
138     size = avio_close_dyn_buf(rtpctx->pb, &buf);
139     ptr = buf;
140     while (size > 4) {
141         uint32_t packet_len = AV_RB32(ptr);
142         int id;
143         /* The interleaving header is exactly 4 bytes, which happens to be
144          * the same size as the packet length header from
145          * url_open_dyn_packet_buf. So by writing the interleaving header
146          * over these bytes, we get a consecutive interleaved packet
147          * that can be written in one call. */
148         interleaved_packet = interleave_header = ptr;
149         ptr += 4;
150         size -= 4;
151         if (packet_len > size || packet_len < 2)
152             break;
153         if (ptr[1] >= RTCP_SR && ptr[1] <= RTCP_APP)
154             id = rtsp_st->interleaved_max; /* RTCP */
155         else
156             id = rtsp_st->interleaved_min; /* RTP */
157         interleave_header[0] = '$';
158         interleave_header[1] = id;
159         AV_WB16(interleave_header + 2, packet_len);
160         url_write(rt->rtsp_hd_out, interleaved_packet, 4 + packet_len);
161         ptr += packet_len;
162         size -= packet_len;
163     }
164     av_free(buf);
165     url_open_dyn_packet_buf(&rtpctx->pb, RTSP_TCP_MAX_PACKET_SIZE);
166     return 0;
167 }
168
169 static int rtsp_write_packet(AVFormatContext *s, AVPacket *pkt)
170 {
171     RTSPState *rt = s->priv_data;
172     RTSPStream *rtsp_st;
173     int n;
174     struct pollfd p = {url_get_file_handle(rt->rtsp_hd), POLLIN, 0};
175     AVFormatContext *rtpctx;
176     int ret;
177
178     while (1) {
179         n = poll(&p, 1, 0);
180         if (n <= 0)
181             break;
182         if (p.revents & POLLIN) {
183             RTSPMessageHeader reply;
184
185             /* Don't let ff_rtsp_read_reply handle interleaved packets,
186              * since it would block and wait for an RTSP reply on the socket
187              * (which may not be coming any time soon) if it handles
188              * interleaved packets internally. */
189             ret = ff_rtsp_read_reply(s, &reply, NULL, 1, NULL);
190             if (ret < 0)
191                 return AVERROR(EPIPE);
192             if (ret == 1)
193                 ff_rtsp_skip_packet(s);
194             /* XXX: parse message */
195             if (rt->state != RTSP_STATE_STREAMING)
196                 return AVERROR(EPIPE);
197         }
198     }
199
200     if (pkt->stream_index < 0 || pkt->stream_index >= rt->nb_rtsp_streams)
201         return AVERROR_INVALIDDATA;
202     rtsp_st = rt->rtsp_streams[pkt->stream_index];
203     rtpctx = rtsp_st->transport_priv;
204
205     ret = ff_write_chained(rtpctx, 0, pkt, s);
206     /* ff_write_chained does all the RTP packetization. If using TCP as
207      * transport, rtpctx->pb is only a dyn_packet_buf that queues up the
208      * packets, so we need to send them out on the TCP connection separately.
209      */
210     if (!ret && rt->lower_transport == RTSP_LOWER_TRANSPORT_TCP)
211         ret = tcp_write_packet(s, rtsp_st);
212     return ret;
213 }
214
215 static int rtsp_write_close(AVFormatContext *s)
216 {
217     RTSPState *rt = s->priv_data;
218
219     ff_rtsp_send_cmd_async(s, "TEARDOWN", rt->control_uri, NULL);
220
221     ff_rtsp_close_streams(s);
222     ff_rtsp_close_connections(s);
223     ff_network_close();
224     return 0;
225 }
226
227 AVOutputFormat ff_rtsp_muxer = {
228     "rtsp",
229     NULL_IF_CONFIG_SMALL("RTSP output format"),
230     NULL,
231     NULL,
232     sizeof(RTSPState),
233     CODEC_ID_AAC,
234     CODEC_ID_MPEG4,
235     rtsp_write_header,
236     rtsp_write_packet,
237     rtsp_write_close,
238     .flags = AVFMT_NOFILE | AVFMT_GLOBALHEADER,
239 };
240