3 * Copyright (c) 2002 Fabrice Bellard
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
21 #ifndef AVFORMAT_RTSP_H
22 #define AVFORMAT_RTSP_H
26 #include "rtspcodes.h"
32 * Network layer over which RTP/etc packet data will be transported.
34 enum RTSPLowerTransport {
35 RTSP_LOWER_TRANSPORT_UDP = 0, /**< UDP/unicast */
36 RTSP_LOWER_TRANSPORT_TCP = 1, /**< TCP; interleaved in RTSP */
37 RTSP_LOWER_TRANSPORT_UDP_MULTICAST = 2, /**< UDP/multicast */
38 RTSP_LOWER_TRANSPORT_NB
42 * Packet profile of the data that we will be receiving. Real servers
43 * commonly send RDT (although they can sometimes send RTP as well),
44 * whereas most others will send RTP.
47 RTSP_TRANSPORT_RTP, /**< Standards-compliant RTP */
48 RTSP_TRANSPORT_RDT, /**< Realmedia Data Transport */
53 * Transport mode for the RTSP data. This may be plain, or
54 * tunneled, which is done over HTTP.
56 enum RTSPControlTransport {
57 RTSP_MODE_PLAIN, /**< Normal RTSP */
58 RTSP_MODE_TUNNEL /**< RTSP over HTTP (tunneling) */
61 #define RTSP_DEFAULT_PORT 554
62 #define RTSP_MAX_TRANSPORTS 8
63 #define RTSP_TCP_MAX_PACKET_SIZE 1472
64 #define RTSP_DEFAULT_NB_AUDIO_CHANNELS 1
65 #define RTSP_DEFAULT_AUDIO_SAMPLERATE 44100
66 #define RTSP_RTP_PORT_MIN 5000
67 #define RTSP_RTP_PORT_MAX 10000
70 * This describes a single item in the "Transport:" line of one stream as
71 * negotiated by the SETUP RTSP command. Multiple transports are comma-
72 * separated ("Transport: x-read-rdt/tcp;interleaved=0-1,rtp/avp/udp;
73 * client_port=1000-1001;server_port=1800-1801") and described in separate
74 * RTSPTransportFields.
76 typedef struct RTSPTransportField {
77 /** interleave ids, if TCP transport; each TCP/RTSP data packet starts
78 * with a '$', stream length and stream ID. If the stream ID is within
79 * the range of this interleaved_min-max, then the packet belongs to
81 int interleaved_min, interleaved_max;
83 /** UDP multicast port range; the ports to which we should connect to
84 * receive multicast UDP data. */
85 int port_min, port_max;
87 /** UDP client ports; these should be the local ports of the UDP RTP
88 * (and RTCP) sockets over which we receive RTP/RTCP data. */
89 int client_port_min, client_port_max;
91 /** UDP unicast server port range; the ports to which we should connect
92 * to receive unicast UDP RTP/RTCP data. */
93 int server_port_min, server_port_max;
95 /** time-to-live value (required for multicast); the amount of HOPs that
96 * packets will be allowed to make before being discarded. */
99 uint32_t destination; /**< destination IP address */
101 /** data/packet transport protocol; e.g. RTP or RDT */
102 enum RTSPTransport transport;
104 /** network layer transport protocol; e.g. TCP or UDP uni-/multicast */
105 enum RTSPLowerTransport lower_transport;
106 } RTSPTransportField;
109 * This describes the server response to each RTSP command.
111 typedef struct RTSPMessageHeader {
112 /** length of the data following this header */
115 enum RTSPStatusCode status_code; /**< response code from server */
117 /** number of items in the 'transports' variable below */
120 /** Time range of the streams that the server will stream. In
121 * AV_TIME_BASE unit, AV_NOPTS_VALUE if not used */
122 int64_t range_start, range_end;
124 /** describes the complete "Transport:" line of the server in response
125 * to a SETUP RTSP command by the client */
126 RTSPTransportField transports[RTSP_MAX_TRANSPORTS];
128 int seq; /**< sequence number */
130 /** the "Session:" field. This value is initially set by the server and
131 * should be re-transmitted by the client in every RTSP command. */
132 char session_id[512];
134 /** the "Location:" field. This value is used to handle redirection.
138 /** the "RealChallenge1:" field from the server */
139 char real_challenge[64];
141 /** the "Server: field, which can be used to identify some special-case
142 * servers that are not 100% standards-compliant. We use this to identify
143 * Windows Media Server, which has a value "WMServer/v.e.r.sion", where
144 * version is a sequence of digits (e.g. 9.0.0.3372). Helix/Real servers
145 * use something like "Helix [..] Server Version v.e.r.sion (platform)
146 * (RealServer compatible)" or "RealServer Version v.e.r.sion (platform)",
147 * where platform is the output of $uname -msr | sed 's/ /-/g'. */
150 /** The "timeout" comes as part of the server response to the "SETUP"
151 * command, in the "Session: <xyz>[;timeout=<value>]" line. It is the
152 * time, in seconds, that the server will go without traffic over the
153 * RTSP/TCP connection before it closes the connection. To prevent
154 * this, sent dummy requests (e.g. OPTIONS) with intervals smaller
155 * than this value. */
158 /** The "Notice" or "X-Notice" field value. See
159 * http://tools.ietf.org/html/draft-stiemerling-rtsp-announce-00
160 * for a complete list of supported values. */
163 /** The "reason" is meant to specify better the meaning of the error code
170 * Client state, i.e. whether we are currently receiving data (PLAYING) or
171 * setup-but-not-receiving (PAUSED). State can be changed in applications
172 * by calling av_read_play/pause().
174 enum RTSPClientState {
175 RTSP_STATE_IDLE, /**< not initialized */
176 RTSP_STATE_STREAMING, /**< initialized and sending/receiving data */
177 RTSP_STATE_PAUSED, /**< initialized, but not receiving data */
178 RTSP_STATE_SEEKING, /**< initialized, requesting a seek */
182 * Identifies particular servers that require special handling, such as
183 * standards-incompliant "Transport:" lines in the SETUP request.
185 enum RTSPServerType {
186 RTSP_SERVER_RTP, /**< Standards-compliant RTP-server */
187 RTSP_SERVER_REAL, /**< Realmedia-style server */
188 RTSP_SERVER_WMS, /**< Windows Media server */
193 * Private data for the RTSP demuxer.
195 * @todo Use ByteIOContext instead of URLContext
197 typedef struct RTSPState {
198 URLContext *rtsp_hd; /* RTSP TCP connection handle */
200 /** number of items in the 'rtsp_streams' variable */
203 struct RTSPStream **rtsp_streams; /**< streams in this session */
205 /** indicator of whether we are currently receiving data from the
206 * server. Basically this isn't more than a simple cache of the
207 * last PLAY/PAUSE command sent to the server, to make sure we don't
208 * send 2x the same unexpectedly or commands in the wrong state. */
209 enum RTSPClientState state;
211 /** the seek value requested when calling av_seek_frame(). This value
212 * is subsequently used as part of the "Range" parameter when emitting
213 * the RTSP PLAY command. If we are currently playing, this command is
214 * called instantly. If we are currently paused, this command is called
215 * whenever we resume playback. Either way, the value is only used once,
216 * see rtsp_read_play() and rtsp_read_seek(). */
217 int64_t seek_timestamp;
219 /* XXX: currently we use unbuffered input */
220 // ByteIOContext rtsp_gb;
222 int seq; /**< RTSP command sequence number */
224 /** copy of RTSPMessageHeader->session_id, i.e. the server-provided session
225 * identifier that the client should re-transmit in each RTSP command */
226 char session_id[512];
228 /** copy of RTSPMessageHeader->timeout, i.e. the time (in seconds) that
229 * the server will go without traffic on the RTSP/TCP line before it
230 * closes the connection. */
233 /** timestamp of the last RTSP command that we sent to the RTSP server.
234 * This is used to calculate when to send dummy commands to keep the
235 * connection alive, in conjunction with timeout. */
236 int64_t last_cmd_time;
238 /** the negotiated data/packet transport protocol; e.g. RTP or RDT */
239 enum RTSPTransport transport;
241 /** the negotiated network layer transport protocol; e.g. TCP or UDP
243 enum RTSPLowerTransport lower_transport;
245 /** brand of server that we're talking to; e.g. WMS, REAL or other.
246 * Detected based on the value of RTSPMessageHeader->server or the presence
247 * of RTSPMessageHeader->real_challenge */
248 enum RTSPServerType server_type;
250 /** plaintext authorization line (username:password) */
253 /** authentication state */
254 HTTPAuthState auth_state;
256 /** The last reply of the server to a RTSP command */
257 char last_reply[2048]; /* XXX: allocate ? */
259 /** RTSPStream->transport_priv of the last stream that we read a
261 void *cur_transport_priv;
263 /** The following are used for Real stream selection */
265 /** whether we need to send a "SET_PARAMETER Subscribe:" command */
266 int need_subscription;
268 /** stream setup during the last frame read. This is used to detect if
269 * we need to subscribe or unsubscribe to any new streams. */
270 enum AVDiscard *real_setup_cache;
272 /** current stream setup. This is a temporary buffer used to compare
273 * current setup to previous frame setup. */
274 enum AVDiscard *real_setup;
276 /** the last value of the "SET_PARAMETER Subscribe:" RTSP command.
277 * this is used to send the same "Unsubscribe:" if stream setup changed,
278 * before sending a new "Subscribe:" command. */
279 char last_subscription[1024];
282 /** The following are used for RTP/ASF streams */
284 /** ASF demuxer context for the embedded ASF stream from WMS servers */
285 AVFormatContext *asf_ctx;
287 /** cache for position of the asf demuxer, since we load a new
288 * data packet in the bytecontext for each incoming RTSP packet. */
292 /** some MS RTSP streams contain a URL in the SDP that we need to use
293 * for all subsequent RTSP requests, rather than the input URI; in
294 * other cases, this is a copy of AVFormatContext->filename. */
295 char control_uri[1024];
297 /** The synchronized start time of the output streams. */
300 /** Additional output handle, used when input and output are done
301 * separately, eg for HTTP tunneling. */
302 URLContext *rtsp_hd_out;
304 /** RTSP transport mode, such as plain or tunneled. */
305 enum RTSPControlTransport control_transport;
309 * Describes a single stream, as identified by a single m= line block in the
310 * SDP content. In the case of RDT, one RTSPStream can represent multiple
311 * AVStreams. In this case, each AVStream in this set has similar content
312 * (but different codec/bitrate).
314 typedef struct RTSPStream {
315 URLContext *rtp_handle; /**< RTP stream handle (if UDP) */
316 void *transport_priv; /**< RTP/RDT parse context if input, RTP AVFormatContext if output */
318 /** corresponding stream index, if any. -1 if none (MPEG2TS case) */
321 /** interleave IDs; copies of RTSPTransportField->interleaved_min/max
322 * for the selected transport. Only used for TCP. */
323 int interleaved_min, interleaved_max;
325 char control_url[1024]; /**< url for this stream (from SDP) */
327 /** The following are used only in SDP, not RTSP */
329 int sdp_port; /**< port (from SDP content) */
330 struct in_addr sdp_ip; /**< IP address (from SDP content) */
331 int sdp_ttl; /**< IP Time-To-Live (from SDP content) */
332 int sdp_payload_type; /**< payload type */
335 /** The following are used for dynamic protocols (rtp_*.c/rdt.c) */
337 /** handler structure */
338 RTPDynamicProtocolHandler *dynamic_handler;
340 /** private data associated with the dynamic protocol */
341 PayloadContext *dynamic_protocol_context;
345 void ff_rtsp_parse_line(RTSPMessageHeader *reply, const char *buf,
346 HTTPAuthState *auth_state);
348 #if LIBAVFORMAT_VERSION_INT < (53 << 16)
349 extern int rtsp_default_protocols;
351 extern int rtsp_rtp_port_min;
352 extern int rtsp_rtp_port_max;
355 * Send a command to the RTSP server without waiting for the reply.
357 * @param s RTSP (de)muxer context
358 * @param method the method for the request
359 * @param url the target url for the request
360 * @param headers extra header lines to include in the request
361 * @param send_content if non-null, the data to send as request body content
362 * @param send_content_length the length of the send_content data, or 0 if
363 * send_content is null
365 * @return zero if success, nonzero otherwise
367 int ff_rtsp_send_cmd_with_content_async(AVFormatContext *s,
368 const char *method, const char *url,
370 const unsigned char *send_content,
371 int send_content_length);
373 * Send a command to the RTSP server without waiting for the reply.
375 * @see rtsp_send_cmd_with_content_async
377 int ff_rtsp_send_cmd_async(AVFormatContext *s, const char *method,
378 const char *url, const char *headers);
381 * Send a command to the RTSP server and wait for the reply.
383 * @param s RTSP (de)muxer context
384 * @param method the method for the request
385 * @param url the target url for the request
386 * @param headers extra header lines to include in the request
387 * @param reply pointer where the RTSP message header will be stored
388 * @param content_ptr pointer where the RTSP message body, if any, will
389 * be stored (length is in reply)
390 * @param send_content if non-null, the data to send as request body content
391 * @param send_content_length the length of the send_content data, or 0 if
392 * send_content is null
394 * @return zero if success, nonzero otherwise
396 int ff_rtsp_send_cmd_with_content(AVFormatContext *s,
397 const char *method, const char *url,
399 RTSPMessageHeader *reply,
400 unsigned char **content_ptr,
401 const unsigned char *send_content,
402 int send_content_length);
405 * Send a command to the RTSP server and wait for the reply.
407 * @see rtsp_send_cmd_with_content
409 int ff_rtsp_send_cmd(AVFormatContext *s, const char *method,
410 const char *url, const char *headers,
411 RTSPMessageHeader *reply, unsigned char **content_ptr);
414 * Read a RTSP message from the server, or prepare to read data
415 * packets if we're reading data interleaved over the TCP/RTSP
416 * connection as well.
418 * @param s RTSP (de)muxer context
419 * @param reply pointer where the RTSP message header will be stored
420 * @param content_ptr pointer where the RTSP message body, if any, will
421 * be stored (length is in reply)
422 * @param return_on_interleaved_data whether the function may return if we
423 * encounter a data marker ('$'), which precedes data
424 * packets over interleaved TCP/RTSP connections. If this
425 * is set, this function will return 1 after encountering
426 * a '$'. If it is not set, the function will skip any
427 * data packets (if they are encountered), until a reply
428 * has been fully parsed. If no more data is available
429 * without parsing a reply, it will return an error.
431 * @return 1 if a data packets is ready to be received, -1 on error,
434 int ff_rtsp_read_reply(AVFormatContext *s, RTSPMessageHeader *reply,
435 unsigned char **content_ptr,
436 int return_on_interleaved_data);
439 * Skip a RTP/TCP interleaved packet.
441 void ff_rtsp_skip_packet(AVFormatContext *s);
444 * Connect to the RTSP server and set up the individual media streams.
445 * This can be used for both muxers and demuxers.
447 * @param s RTSP (de)muxer context
449 * @return 0 on success, < 0 on error. Cleans up all allocations done
450 * within the function on error.
452 int ff_rtsp_connect(AVFormatContext *s);
455 * Close and free all streams within the RTSP (de)muxer
457 * @param s RTSP (de)muxer context
459 void ff_rtsp_close_streams(AVFormatContext *s);
462 * Close all connection handles within the RTSP (de)muxer
464 * @param rt RTSP (de)muxer context
466 void ff_rtsp_close_connections(AVFormatContext *rt);
468 #endif /* AVFORMAT_RTSP_H */