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
55
56 /* Announce the stream */
58 if (sdp == NULL)
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.
66 *
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.
71 */
74 "rtsp", NULL, addr, -1, NULL);
75 ctx_array[0] = &sdp_ctx;
79 }
82 "Content-Type: application/sdp\r\n",
83 reply, NULL, sdp, strlen(sdp));
87
88 /* Set up the RTSPStreams for each AVStream */
91
93 if (!rtsp_st)
96
98
100 /* Note, this must match the relative uri set in the sdp content */
102 "/streamid=%d", i);
103 }
104
105 return 0;
106 }
107
109 {
112 char cmd[1024];
113
115 "Range: npt=0.000-\r\n");
118 return -1;
120 return 0;
121 }
122
124 {
126
128 if (ret)
130
135 }
136 return 0;
137 }
138
140 {
145 uint8_t *interleave_header, *interleaved_packet;
146
150 while (size > 4) {
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;
159 ptr += 4;
160 size -= 4;
161 if (packet_len > size || packet_len < 2)
162 break;
165 else
167 interleave_header[0] = '$';
168 interleave_header[1] =
id;
169 AV_WB16(interleave_header + 2, packet_len);
171 ptr += packet_len;
172 size -= packet_len;
173 }
176 }
177
179 {
186
187 while (1) {
188 n = poll(&p, 1, 0);
189 if (n <= 0)
190 break;
191 if (p.revents & POLLIN) {
193
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. */
199 if (ret < 0)
201 if (ret == 1)
203 /* XXX: parse message */
206 }
207 }
208
213
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.
218 */
222 }
223
225 {
227
229
233 return 0;
234 }
235
246 .priv_class = &rtsp_muxer_class,
247 };