1 /*
2 * RTSP muxer
3 * Copyright (c) 2010 Martin Storsjo
4 *
5 * This file is part of FFmpeg.
6 *
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.
11 *
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.
16 *
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
20 */
21
23
24 #if HAVE_POLL_H
25 #include <poll.h>
26 #endif
36
37 #define SDP_MAX_SIZE 16384
38
44 };
45
47 {
50 int i;
51 char *sdp;
53
56
57 /* Announce the stream */
59 if (!sdp)
61 /* We create the SDP based on the RTSP AVFormatContext where we
62 * aren't allowed to change the filename field. (We create the SDP
63 * based on the RTSP context since the contexts for the RTP streams
64 * don't exist yet.) In order to specify a custom URL with the actual
65 * peer IP instead of the originally specified hostname, we create
66 * a temporary copy of the AVFormatContext, where the custom URL is set.
67 *
68 * FIXME: Create the SDP without copying the AVFormatContext.
69 * This either requires setting up the RTP stream AVFormatContexts
70 * already here (complicating things immensely) or getting a more
71 * flexible SDP creation interface.
72 */
76 ctx_array[0] = &sdp_ctx;
80 }
83 "Content-Type: application/sdp\r\n",
84 reply,
NULL, sdp, strlen(sdp));
88
89 /* Set up the RTSPStreams for each AVStream */
92
94 if (!rtsp_st)
97
99
101 /* Note, this must match the relative uri set in the sdp content */
103 "/streamid=%d", i);
104 }
105
106 return 0;
107 }
108
110 {
113 char cmd[1024];
114
116 "Range: npt=0.000-\r\n");
121 return 0;
122 }
123
125 {
127
129 if (ret)
131
136 }
137 return 0;
138 }
139
141 {
146 uint8_t *interleave_header, *interleaved_packet;
147
151 while (size > 4) {
152 uint32_t packet_len =
AV_RB32(ptr);
154 /* The interleaving header is exactly 4 bytes, which happens to be
155 * the same size as the packet length header from
156 * ffio_open_dyn_packet_buf. So by writing the interleaving header
157 * over these bytes, we get a consecutive interleaved packet
158 * that can be written in one call. */
159 interleaved_packet = interleave_header = ptr;
160 ptr += 4;
161 size -= 4;
162 if (packet_len > size || packet_len < 2)
163 break;
166 else
168 interleave_header[0] = '$';
169 interleave_header[1] =
id;
170 AV_WB16(interleave_header + 2, packet_len);
172 ptr += packet_len;
173 size -= packet_len;
174 }
177 }
178
180 {
187
188 while (1) {
189 n = poll(&p, 1, 0);
190 if (n <= 0)
191 break;
192 if (p.revents & POLLIN) {
194
195 /* Don't let ff_rtsp_read_reply handle interleaved packets,
196 * since it would block and wait for an RTSP reply on the socket
197 * (which may not be coming any time soon) if it handles
198 * interleaved packets internally. */
200 if (ret < 0)
202 if (ret == 1)
204 /* XXX: parse message */
207 }
208 }
209
214
216 /* ff_write_chained does all the RTP packetization. If using TCP as
217 * transport, rtpctx->pb is only a dyn_packet_buf that queues up the
218 * packets, so we need to send them out on the TCP connection separately.
219 */
223 }
224
226 {
228
229 // If we want to send RTCP_BYE packets, these are sent by av_write_trailer.
230 // Thus call this on all streams before doing the teardown. This is
231 // done within ff_rtsp_undo_setup.
233
235
239 return 0;
240 }
241
252 .priv_class = &rtsp_muxer_class,
253 };