+/**
+ * Private data for the RTSP demuxer.
+ *
+ * @todo Use AVIOContext instead of URLContext
+ */
+typedef struct RTSPState {
+ const AVClass *class; /**< Class for private options. */
+ URLContext *rtsp_hd; /* RTSP TCP connection handle */
+
+ /** number of items in the 'rtsp_streams' variable */
+ int nb_rtsp_streams;
+
+ struct RTSPStream **rtsp_streams; /**< streams in this session */
+
+ /** indicator of whether we are currently receiving data from the
+ * server. Basically this isn't more than a simple cache of the
+ * last PLAY/PAUSE command sent to the server, to make sure we don't
+ * send 2x the same unexpectedly or commands in the wrong state. */
+ enum RTSPClientState state;
+
+ /** the seek value requested when calling av_seek_frame(). This value
+ * is subsequently used as part of the "Range" parameter when emitting
+ * the RTSP PLAY command. If we are currently playing, this command is
+ * called instantly. If we are currently paused, this command is called
+ * whenever we resume playback. Either way, the value is only used once,
+ * see rtsp_read_play() and rtsp_read_seek(). */
+ int64_t seek_timestamp;
+
+ int seq; /**< RTSP command sequence number */
+
+ /** copy of RTSPMessageHeader->session_id, i.e. the server-provided session
+ * identifier that the client should re-transmit in each RTSP command */
+ char session_id[512];
+
+ /** copy of RTSPMessageHeader->timeout, i.e. the time (in seconds) that
+ * the server will go without traffic on the RTSP/TCP line before it
+ * closes the connection. */
+ int timeout;
+
+ /** timestamp of the last RTSP command that we sent to the RTSP server.
+ * This is used to calculate when to send dummy commands to keep the
+ * connection alive, in conjunction with timeout. */
+ int64_t last_cmd_time;
+
+ /** the negotiated data/packet transport protocol; e.g. RTP or RDT */
+ enum RTSPTransport transport;
+
+ /** the negotiated network layer transport protocol; e.g. TCP or UDP
+ * uni-/multicast */
+ enum RTSPLowerTransport lower_transport;
+
+ /** brand of server that we're talking to; e.g. WMS, REAL or other.
+ * Detected based on the value of RTSPMessageHeader->server or the presence
+ * of RTSPMessageHeader->real_challenge */
+ enum RTSPServerType server_type;
+
+ /** the "RealChallenge1:" field from the server */
+ char real_challenge[64];
+
+ /** plaintext authorization line (username:password) */
+ char auth[128];
+
+ /** authentication state */
+ HTTPAuthState auth_state;
+
+ /** The last reply of the server to a RTSP command */
+ char last_reply[2048]; /* XXX: allocate ? */
+
+ /** RTSPStream->transport_priv of the last stream that we read a
+ * packet from */
+ void *cur_transport_priv;
+
+ /** The following are used for Real stream selection */
+ //@{
+ /** whether we need to send a "SET_PARAMETER Subscribe:" command */
+ int need_subscription;
+
+ /** stream setup during the last frame read. This is used to detect if
+ * we need to subscribe or unsubscribe to any new streams. */
+ enum AVDiscard *real_setup_cache;
+
+ /** current stream setup. This is a temporary buffer used to compare
+ * current setup to previous frame setup. */
+ enum AVDiscard *real_setup;
+
+ /** the last value of the "SET_PARAMETER Subscribe:" RTSP command.
+ * this is used to send the same "Unsubscribe:" if stream setup changed,
+ * before sending a new "Subscribe:" command. */
+ char last_subscription[1024];
+ //@}
+
+ /** The following are used for RTP/ASF streams */
+ //@{
+ /** ASF demuxer context for the embedded ASF stream from WMS servers */
+ AVFormatContext *asf_ctx;
+
+ /** cache for position of the asf demuxer, since we load a new
+ * data packet in the bytecontext for each incoming RTSP packet. */
+ uint64_t asf_pb_pos;
+ //@}
+
+ /** some MS RTSP streams contain a URL in the SDP that we need to use
+ * for all subsequent RTSP requests, rather than the input URI; in
+ * other cases, this is a copy of AVFormatContext->filename. */
+ char control_uri[1024];
+
+ /** Additional output handle, used when input and output are done
+ * separately, eg for HTTP tunneling. */
+ URLContext *rtsp_hd_out;
+
+ /** RTSP transport mode, such as plain or tunneled. */
+ enum RTSPControlTransport control_transport;
+
+ /* Number of RTCP BYE packets the RTSP session has received.
+ * An EOF is propagated back if nb_byes == nb_streams.
+ * This is reset after a seek. */
+ int nb_byes;
+
+ /** Reusable buffer for receiving packets */
+ uint8_t* recvbuf;
+
+ /**
+ * A mask with all requested transport methods
+ */
+ int lower_transport_mask;
+
+ /**
+ * The number of returned packets
+ */
+ uint64_t packets;
+
+ /**
+ * Polling array for udp
+ */
+ struct pollfd *p;
+
+ /**
+ * Whether the server supports the GET_PARAMETER method.
+ */
+ int get_parameter_supported;
+
+ /**
+ * Do not begin to play the stream immediately.
+ */
+ int initial_pause;
+
+ /**
+ * Option flags for the chained RTP muxer.
+ */
+ int rtp_muxer_flags;
+
+ /** Whether the server accepts the x-Dynamic-Rate header */
+ int accept_dynamic_rate;
+
+ /**
+ * Various option flags for the RTSP muxer/demuxer.
+ */
+ int rtsp_flags;
+
+ /**
+ * Mask of all requested media types
+ */
+ int media_type_mask;
+
+ /**
+ * Minimum and maximum local UDP ports.
+ */
+ int rtp_port_min, rtp_port_max;
+} RTSPState;
+
+#define RTSP_FLAG_FILTER_SRC 0x1 /**< Filter incoming UDP packets -
+ receive packets only from the right
+ source address and port. */
+
+/**
+ * Describe a single stream, as identified by a single m= line block in the
+ * SDP content. In the case of RDT, one RTSPStream can represent multiple
+ * AVStreams. In this case, each AVStream in this set has similar content
+ * (but different codec/bitrate).
+ */
+typedef struct RTSPStream {
+ URLContext *rtp_handle; /**< RTP stream handle (if UDP) */
+ void *transport_priv; /**< RTP/RDT parse context if input, RTP AVFormatContext if output */
+
+ /** corresponding stream index, if any. -1 if none (MPEG2TS case) */
+ int stream_index;
+
+ /** interleave IDs; copies of RTSPTransportField->interleaved_min/max
+ * for the selected transport. Only used for TCP. */
+ int interleaved_min, interleaved_max;
+
+ char control_url[1024]; /**< url for this stream (from SDP) */
+
+ /** The following are used only in SDP, not RTSP */
+ //@{
+ int sdp_port; /**< port (from SDP content) */
+ struct sockaddr_storage sdp_ip; /**< IP address (from SDP content) */
+ int sdp_ttl; /**< IP Time-To-Live (from SDP content) */
+ int sdp_payload_type; /**< payload type */
+ //@}
+
+ /** The following are used for dynamic protocols (rtp_*.c/rdt.c) */
+ //@{
+ /** handler structure */
+ RTPDynamicProtocolHandler *dynamic_handler;
+
+ /** private data associated with the dynamic protocol */
+ PayloadContext *dynamic_protocol_context;
+ //@}
+} RTSPStream;
+
+void ff_rtsp_parse_line(RTSPMessageHeader *reply, const char *buf,
+ RTSPState *rt, const char *method);
+
+/**
+ * Send a command to the RTSP server without waiting for the reply.
+ *
+ * @see rtsp_send_cmd_with_content_async
+ */
+int ff_rtsp_send_cmd_async(AVFormatContext *s, const char *method,
+ const char *url, const char *headers);
+
+/**
+ * Send a command to the RTSP server and wait for the reply.
+ *
+ * @param s RTSP (de)muxer context
+ * @param method the method for the request
+ * @param url the target url for the request
+ * @param headers extra header lines to include in the request
+ * @param reply pointer where the RTSP message header will be stored
+ * @param content_ptr pointer where the RTSP message body, if any, will
+ * be stored (length is in reply)
+ * @param send_content if non-null, the data to send as request body content
+ * @param send_content_length the length of the send_content data, or 0 if
+ * send_content is null
+ *
+ * @return zero if success, nonzero otherwise
+ */
+int ff_rtsp_send_cmd_with_content(AVFormatContext *s,
+ const char *method, const char *url,
+ const char *headers,
+ RTSPMessageHeader *reply,
+ unsigned char **content_ptr,
+ const unsigned char *send_content,
+ int send_content_length);
+
+/**
+ * Send a command to the RTSP server and wait for the reply.
+ *
+ * @see rtsp_send_cmd_with_content
+ */
+int ff_rtsp_send_cmd(AVFormatContext *s, const char *method,
+ const char *url, const char *headers,
+ RTSPMessageHeader *reply, unsigned char **content_ptr);
+
+/**
+ * Read a RTSP message from the server, or prepare to read data
+ * packets if we're reading data interleaved over the TCP/RTSP
+ * connection as well.
+ *
+ * @param s RTSP (de)muxer context
+ * @param reply pointer where the RTSP message header will be stored
+ * @param content_ptr pointer where the RTSP message body, if any, will
+ * be stored (length is in reply)
+ * @param return_on_interleaved_data whether the function may return if we
+ * encounter a data marker ('$'), which precedes data
+ * packets over interleaved TCP/RTSP connections. If this
+ * is set, this function will return 1 after encountering
+ * a '$'. If it is not set, the function will skip any
+ * data packets (if they are encountered), until a reply
+ * has been fully parsed. If no more data is available
+ * without parsing a reply, it will return an error.
+ * @param method the RTSP method this is a reply to. This affects how
+ * some response headers are acted upon. May be NULL.
+ *
+ * @return 1 if a data packets is ready to be received, -1 on error,
+ * and 0 on success.
+ */
+int ff_rtsp_read_reply(AVFormatContext *s, RTSPMessageHeader *reply,
+ unsigned char **content_ptr,
+ int return_on_interleaved_data, const char *method);
+
+/**
+ * Skip a RTP/TCP interleaved packet.
+ */
+void ff_rtsp_skip_packet(AVFormatContext *s);
+
+/**
+ * Connect to the RTSP server and set up the individual media streams.
+ * This can be used for both muxers and demuxers.
+ *
+ * @param s RTSP (de)muxer context
+ *
+ * @return 0 on success, < 0 on error. Cleans up all allocations done
+ * within the function on error.
+ */
+int ff_rtsp_connect(AVFormatContext *s);
+
+/**
+ * Close and free all streams within the RTSP (de)muxer
+ *
+ * @param s RTSP (de)muxer context
+ */
+void ff_rtsp_close_streams(AVFormatContext *s);
+
+/**
+ * Close all connection handles within the RTSP (de)muxer
+ *
+ * @param s RTSP (de)muxer context
+ */
+void ff_rtsp_close_connections(AVFormatContext *s);
+
+/**
+ * Get the description of the stream and set up the RTSPStream child
+ * objects.
+ */
+int ff_rtsp_setup_input_streams(AVFormatContext *s, RTSPMessageHeader *reply);