]> git.sesse.net Git - ffmpeg/blob - libavformat/rtsp.h
get rid of MAX_STREAMS limit in RTSP
[ffmpeg] / libavformat / rtsp.h
1 /*
2  * RTSP definitions
3  * Copyright (c) 2002 Fabrice Bellard
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 #ifndef AVFORMAT_RTSP_H
22 #define AVFORMAT_RTSP_H
23
24 #include <stdint.h>
25 #include "avformat.h"
26 #include "rtspcodes.h"
27 #include "rtpdec.h"
28 #include "network.h"
29 #include "httpauth.h"
30
31 /**
32  * Network layer over which RTP/etc packet data will be transported.
33  */
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
39 };
40
41 /**
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.
45  */
46 enum RTSPTransport {
47     RTSP_TRANSPORT_RTP, /**< Standards-compliant RTP */
48     RTSP_TRANSPORT_RDT, /**< Realmedia Data Transport */
49     RTSP_TRANSPORT_NB
50 };
51
52 /**
53  * Transport mode for the RTSP data. This may be plain, or
54  * tunneled, which is done over HTTP.
55  */
56 enum RTSPControlTransport {
57     RTSP_MODE_PLAIN,   /**< Normal RTSP */
58     RTSP_MODE_TUNNEL   /**< RTSP over HTTP (tunneling) */
59 };
60
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
68
69 /**
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.
75  */
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
80      * this stream. */
81     int interleaved_min, interleaved_max;
82
83     /** UDP multicast port range; the ports to which we should connect to
84      * receive multicast UDP data. */
85     int port_min, port_max;
86
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;
90
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;
94
95     /** time-to-live value (required for multicast); the amount of HOPs that
96      * packets will be allowed to make before being discarded. */
97     int ttl;
98
99     uint32_t destination; /**< destination IP address */
100
101     /** data/packet transport protocol; e.g. RTP or RDT */
102     enum RTSPTransport transport;
103
104     /** network layer transport protocol; e.g. TCP or UDP uni-/multicast */
105     enum RTSPLowerTransport lower_transport;
106 } RTSPTransportField;
107
108 /**
109  * This describes the server response to each RTSP command.
110  */
111 typedef struct RTSPMessageHeader {
112     /** length of the data following this header */
113     int content_length;
114
115     enum RTSPStatusCode status_code; /**< response code from server */
116
117     /** number of items in the 'transports' variable below */
118     int nb_transports;
119
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;
123
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];
127
128     int seq;                         /**< sequence number */
129
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];
133
134     /** the "Location:" field. This value is used to handle redirection.
135      */
136     char location[4096];
137
138     /** the "RealChallenge1:" field from the server */
139     char real_challenge[64];
140
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'. */
148     char server[64];
149
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. */
156     int timeout;
157
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. */
161     int notice;
162
163     /** The "reason" is meant to specify better the meaning of the error code
164      * returned
165      */
166     char reason[256];
167 } RTSPMessageHeader;
168
169 /**
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().
173  */
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 */
179 };
180
181 /**
182  * Identifies particular servers that require special handling, such as
183  * standards-incompliant "Transport:" lines in the SETUP request.
184  */
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 */
189     RTSP_SERVER_NB
190 };
191
192 /**
193  * Private data for the RTSP demuxer.
194  *
195  * @todo Use ByteIOContext instead of URLContext
196  */
197 typedef struct RTSPState {
198     URLContext *rtsp_hd; /* RTSP TCP connection handle */
199
200     /** number of items in the 'rtsp_streams' variable */
201     int nb_rtsp_streams;
202
203     struct RTSPStream **rtsp_streams; /**< streams in this session */
204
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;
210
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;
218
219     /* XXX: currently we use unbuffered input */
220     //    ByteIOContext rtsp_gb;
221
222     int seq;                          /**< RTSP command sequence number */
223
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];
227
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. */
231     int timeout;
232
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;
237
238     /** the negotiated data/packet transport protocol; e.g. RTP or RDT */
239     enum RTSPTransport transport;
240
241     /** the negotiated network layer transport protocol; e.g. TCP or UDP
242      * uni-/multicast */
243     enum RTSPLowerTransport lower_transport;
244
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;
249
250     /** plaintext authorization line (username:password) */
251     char auth[128];
252
253     /** authentication state */
254     HTTPAuthState auth_state;
255
256     /** The last reply of the server to a RTSP command */
257     char last_reply[2048]; /* XXX: allocate ? */
258
259     /** RTSPStream->transport_priv of the last stream that we read a
260      * packet from */
261     void *cur_transport_priv;
262
263     /** The following are used for Real stream selection */
264     //@{
265     /** whether we need to send a "SET_PARAMETER Subscribe:" command */
266     int need_subscription;
267
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;
271
272     /** current stream setup. This is a temporary buffer used to compare
273      * current setup to previous frame setup. */
274     enum AVDiscard *real_setup;
275
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];
280     //@}
281
282     /** The following are used for RTP/ASF streams */
283     //@{
284     /** ASF demuxer context for the embedded ASF stream from WMS servers */
285     AVFormatContext *asf_ctx;
286
287     /** cache for position of the asf demuxer, since we load a new
288      * data packet in the bytecontext for each incoming RTSP packet. */
289     uint64_t asf_pb_pos;
290     //@}
291
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];
296
297     /** The synchronized start time of the output streams. */
298     int64_t start_time;
299
300     /** Additional output handle, used when input and output are done
301      * separately, eg for HTTP tunneling. */
302     URLContext *rtsp_hd_out;
303
304     /** RTSP transport mode, such as plain or tunneled. */
305     enum RTSPControlTransport control_transport;
306 } RTSPState;
307
308 /**
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).
313  */
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 */
317
318     /** corresponding stream index, if any. -1 if none (MPEG2TS case) */
319     int stream_index;
320
321     /** interleave IDs; copies of RTSPTransportField->interleaved_min/max
322      * for the selected transport. Only used for TCP. */
323     int interleaved_min, interleaved_max;
324
325     char control_url[1024];   /**< url for this stream (from SDP) */
326
327     /** The following are used only in SDP, not RTSP */
328     //@{
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 */
333     //@}
334
335     /** The following are used for dynamic protocols (rtp_*.c/rdt.c) */
336     //@{
337     /** handler structure */
338     RTPDynamicProtocolHandler *dynamic_handler;
339
340     /** private data associated with the dynamic protocol */
341     PayloadContext *dynamic_protocol_context;
342     //@}
343 } RTSPStream;
344
345 void ff_rtsp_parse_line(RTSPMessageHeader *reply, const char *buf,
346                         HTTPAuthState *auth_state);
347
348 #if LIBAVFORMAT_VERSION_INT < (53 << 16)
349 extern int rtsp_default_protocols;
350 #endif
351 extern int rtsp_rtp_port_min;
352 extern int rtsp_rtp_port_max;
353
354 /**
355  * Send a command to the RTSP server without waiting for the reply.
356  *
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
364  *
365  * @return zero if success, nonzero otherwise
366  */
367 int ff_rtsp_send_cmd_with_content_async(AVFormatContext *s,
368                                         const char *method, const char *url,
369                                         const char *headers,
370                                         const unsigned char *send_content,
371                                         int send_content_length);
372 /**
373  * Send a command to the RTSP server without waiting for the reply.
374  *
375  * @see rtsp_send_cmd_with_content_async
376  */
377 int ff_rtsp_send_cmd_async(AVFormatContext *s, const char *method,
378                            const char *url, const char *headers);
379
380 /**
381  * Send a command to the RTSP server and wait for the reply.
382  *
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
393  *
394  * @return zero if success, nonzero otherwise
395  */
396 int ff_rtsp_send_cmd_with_content(AVFormatContext *s,
397                                   const char *method, const char *url,
398                                   const char *headers,
399                                   RTSPMessageHeader *reply,
400                                   unsigned char **content_ptr,
401                                   const unsigned char *send_content,
402                                   int send_content_length);
403
404 /**
405  * Send a command to the RTSP server and wait for the reply.
406  *
407  * @see rtsp_send_cmd_with_content
408  */
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);
412
413 /**
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.
417  *
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.
430  *
431  * @return 1 if a data packets is ready to be received, -1 on error,
432  *          and 0 on success.
433  */
434 int ff_rtsp_read_reply(AVFormatContext *s, RTSPMessageHeader *reply,
435                        unsigned char **content_ptr,
436                        int return_on_interleaved_data);
437
438 /**
439  * Skip a RTP/TCP interleaved packet.
440  */
441 void ff_rtsp_skip_packet(AVFormatContext *s);
442
443 /**
444  * Connect to the RTSP server and set up the individual media streams.
445  * This can be used for both muxers and demuxers.
446  *
447  * @param s RTSP (de)muxer context
448  *
449  * @return 0 on success, < 0 on error. Cleans up all allocations done
450  *          within the function on error.
451  */
452 int ff_rtsp_connect(AVFormatContext *s);
453
454 /**
455  * Close and free all streams within the RTSP (de)muxer
456  *
457  * @param s RTSP (de)muxer context
458  */
459 void ff_rtsp_close_streams(AVFormatContext *s);
460
461 /**
462  * Close all connection handles within the RTSP (de)muxer
463  *
464  * @param rt RTSP (de)muxer context
465  */
466 void ff_rtsp_close_connections(AVFormatContext *rt);
467
468 #endif /* AVFORMAT_RTSP_H */