Section 10. Connections

RTSP messages are transferred between RTSP agents and proxies using a
   transport connection.  This transport connection uses TCP or TCP/TLS.
   This transport connection is referred to as the "connection" or "RTSP
   connection" within this document.
   RTSP requests can be transmitted using the two different connection
   scenarios listed below:
   o  persistent - a transport connection is used for several request/
      response transactions;
   o  transient - a transport connection is used for each single
      request/response transaction.
   RFC 2326 attempted to specify an optional mechanism for transmitting
   RTSP messages in connectionless mode over a transport protocol such
   as UDP.  However, it was not specified in sufficient detail to allow
   for interoperable implementations.  In an attempt to reduce
   complexity and scope, and due to lack of interest, RTSP 2.0 does not
   attempt to define a mechanism for supporting RTSP over UDP or other
   connectionless transport protocols.  A side effect of this is that
   RTSP requests MUST NOT be sent to multicast groups since no
   connection can be established with a specific receiver in multicast
   environments.
   Certain RTSP headers, such as the CSeq header (Section 18.20), which
   may appear to be relevant only to connectionless transport scenarios,
   are still retained and MUST be implemented according to this
   specification.  In the case of CSeq, it is quite useful for matching
   responses to requests if the requests are pipelined (see Section 12).
   It is also useful in proxies for keeping track of the different
   requests when aggregating several client requests on a single TCP
   connection.
10.1.  Reliability and Acknowledgements
   Since RTSP messages are transmitted using reliable transport
   protocols, they MUST NOT be retransmitted at the RTSP level.
   Instead, the implementation must rely on the underlying transport to
   provide reliability.  The RTSP implementation may use any indication
   of reception acknowledgment of the message from the underlying
   transport protocols to optimize the RTSP behavior.
      If both the underlying reliable transport, such as TCP, and the
      RTSP application retransmit requests, each packet loss or message
      loss may result in two retransmissions.  The receiver typically
      cannot take advantage of the application-layer retransmission
      since the transport stack will not deliver the application-layer
      retransmission before the first attempt has reached the receiver.
      If the packet loss is caused by congestion, multiple
      retransmissions at different layers will exacerbate the
      congestion.
   Lack of acknowledgment of an RTSP request should be handled within
   the constraints of the connection timeout considerations described
   below (Section 10.4).
10.2.  Using Connections
   A TCP transport can be used for both persistent connections (for
   several message exchanges) and transient connections (for a single
   message exchange).  Implementations of this specification MUST
   support RTSP over TCP.  The scheme of the RTSP URI (Section 4.2)
   allows the client to specify the port it will contact the server on,
   and defines the default port to use if one is not explicitly given.
   In addition to the registered default ports, i.e., 554 (rtsp) and 322
   (rtsps), there is an alternative port 8554 registered.  This port may
   provide some benefits over non-registered ports if an RTSP server is
   unable to use the default ports.  The benefits may include
   preconfigured security policies as well as classifiers in network
   monitoring tools.
   An RTSP client opening a TCP connection to access a particular
   resource as identified by a URI uses the IP address and port derived
   from the host and port parts of the URI.  The IP address is either
   the explicit address provided in the URI or any of the addresses
   provided when performing A and AAAA record DNS lookups of the
   hostname in the URI.
   A server MUST handle both persistent and transient connections.
      Transient connections facilitate mechanisms for fault tolerance.
      They also allow for application-layer mobility.  A server-and-
      client pair that supports transient connections can survive the
      loss of a TCP connection; e.g., due to a NAT timeout.  When the
      client has discovered that the TCP connection has been lost, it
      can set up a new one when there is need to communicate again.
   A persistent connection is RECOMMENDED to be used for all
   transactions between the server and client, including messages for
   multiple RTSP sessions.  However, a persistent connection MAY be
   closed after a few message exchanges.  For example, a client may use
   a persistent connection for the initial SETUP and PLAY message
   exchanges in a session and then close the connection.  Later, when
   the client wishes to send a new request, such as a PAUSE for the
   session, a new connection would be opened.  This connection may be
   either transient or persistent.
   An RTSP agent MAY use one connection to handle multiple RTSP sessions
   on the same server.  The RTSP agent SHALL NOT use more than one
   connection per RTSP session at any given point.
      Having only one connection in use at any time avoids confusion
      regarding on which connection any server-to-client requests shall
      be sent.  Using a single connection for multiple RTSP sessions
      also saves complexity by enabling the server to maintain less
      state about its connection resources on the server.  Not using
      more than one connection at a time for a particular RTSP session
      avoids wasting connection resources and allows the server to track
      only the most recently used client-to-server connection for each
      RTSP session as being the currently valid server-to-client
      connection.
   RTSP allows a server to send requests to a client.  However, this can
   be supported only if a client establishes a persistent connection
   with the server.  In cases where a persistent connection does not
   exist between a server and its client, due to the lack of a signaling
   channel, the server may be forced to silently discard RTSP messages,
   and it may even drop an RTSP session without notifying the client.
   An example of such a case is when the server desires to send a
   REDIRECT request for an RTSP session to the client but is not able to
   do so because it cannot reach the client.  A server that attempts to
   send a request to a client that has no connection currently to the
   server SHALL discard the request.
      Without a persistent connection between the client and the server,
      the media server has no reliable way of reaching the client.
      Because of the likely failure of server-to-client established
      connections, the server will not even attempt establishing any
      connection.
      Queuing of server-to-client requests has been considered.
      However, a security issue exists as to how it might be possible to
      authorize a client establishing a new connection as being a
      legitimate receiver of a request related to a particular RTSP
      session, without the client first issuing requests related to the
      pending request.  Thus, it would be likely to make any such
      requests even more delayed and less useful.
   The sending of client and server requests can be asynchronous events.
   To avoid deadlock situations, both client and server MUST be able to
   send and receive requests simultaneously.  As an RTSP response may be
   queued up for transmission, reception or processing behind the peer
   RTSP agent's own requests, all RTSP agents are required to have a
   certain capability of handling outstanding messages.  A potential
   issue is that outstanding requests may time out despite being
   processed by the peer; this can be due to the response being caught
   in the queue behind a number of requests that the RTSP agent is
   processing but that take some time to complete.  To avoid this
   problem, an RTSP agent should buffer incoming messages locally so
   that any response messages can be processed immediately upon
   reception.  If responses are separated from requests and directly
   forwarded for processing, not only can the result be used
   immediately, the state associated with that outstanding request can
   also be released.  However, buffering a number of requests on the
   receiving RTSP agent consumes resources and enables a resource
   exhaustion attack on the agent.  Therefore, this buffer should be
   limited so that an unreasonable number of requests or total message
   size is not allowed to consume the receiving agent's resources.  In
   most APIs, having the receiving agent stop reading from the TCP
   socket will result in TCP's window being clamped, thus forcing the
   buffering onto the sending agent when the load is larger than
   expected.  However, as both RTSP message sizes and frequency may be
   changed in the future by protocol extensions, an agent should be
   careful about taking harsher measurements against a potential attack.
   When under attack, an RTSP agent can close TCP connections and
   release state associated with that TCP connection.
   To provide some guidance on what is reasonable, the following
   guidelines are given.  It is RECOMMENDED that:
   o  an RTSP agent should not have more than 10 outstanding requests
      per RTSP session;
   o  an RTSP agent should not have more than 10 outstanding requests
      that are not related to an RTSP session or that are requesting to
      create an RTSP session.
   In light of the above, it is RECOMMENDED that clients use persistent
   connections whenever possible.  A client that supports persistent
   connections MAY "pipeline" its requests (see Section 12).
   RTSP agents can send requests to multiple different destinations,
   either server or client contexts over the same connection to a proxy.
   Then, the proxy forks the message to the different destinations over
   proxy-to-agent connections.  In these cases when multiple requests
   are outstanding, the requesting agent MUST be ready to receive the
   responses out of order compared to the order they where sent on the
   connection.  The order between multiple messages for each destination
   will be maintained; however, the order between response from
   different destinations can be different.
      The reason for this is to avoid a head-of-line blocking situation.
      In a sequence of requests, an early outstanding request may take
      time to be processed at one destination.  Simultaneously, a
      response from any other destination that was later in the sequence
      of requests may have arrived at the proxy; thus, allowing out-of-
      order responses avoids forcing the proxy to buffer this response
      and instead deliver it as soon as possible.  Note, this will not
      affect the order in which the messages sent to each separate
      destination were processed at the request destination.
   This scenario can occur in two cases involving proxies.  The first is
   a client issuing requests for sessions on different servers using a
   common client-to-proxy connection.  The second is for server-to-
   client requests, like REDIRECT being sent by the server over a common
   transport connection the proxy created for its different connecting
   clients.
10.3.  Closing Connections
   The client MAY close a connection at any point when no outstanding
   request/response transactions exist for any RTSP session being
   managed through the connection.  The server, however, SHOULD NOT
   close a connection until all RTSP sessions being managed through the
   connection have been timed out (Section 18.49).  A server SHOULD NOT
   close a connection immediately after responding to a session-level
   TEARDOWN request for the last RTSP session being controlled through
   the connection.  Instead, the server should wait for a reasonable
   amount of time for the client to receive and act upon the TEARDOWN
   response and then initiate the connection closing.  The server SHOULD
   wait at least 10 seconds after sending the TEARDOWN response before
   closing the connection.
      This is to ensure that the client has time to issue a SETUP for a
      new session on the existing connection after having torn the last
      one down.  Ten seconds should give the client ample opportunity to
      get its message to the server.
   A server SHOULD NOT close the connection directly as a result of
   responding to a request with an error code.
      Certain error responses such as 460 (Only Aggregate Operation
      Allowed) (Section 17.4.24) are used for negotiating capabilities
      of a server with respect to content or other factors.  In such
      cases, it is inefficient for the server to close a connection on
      an error response.  Also, such behavior would prevent
      implementation of advanced or special types of requests or result
      in extra overhead for the client when testing for new features.
      On the other hand, keeping connections open after sending an error
      response poses a Denial-of-Service (DoS) security risk
      (Section 21).
   The server MAY close a connection if it receives an incomplete
   message and if the message is not completed within a reasonable
   amount of time.  It is RECOMMENDED that the server wait at least 10
   seconds for the completion of a message or for the next part of the
   message to arrive (which is an indication that the transport and the
   client are still alive).  Servers believing they are under attack or
   that are otherwise starved for resources during that event MAY
   consider using a shorter timeout.
   If a server closes a connection while the client is attempting to
   send a new request, the client will have to close its current
   connection, establish a new connection, and send its request over the
   new connection.
   An RTSP message SHOULD NOT be terminated by closing the connection.
   Such a message MAY be considered to be incomplete by the receiver and
   discarded.  An RTSP message is properly terminated as defined in
   Section 5.
10.4.  Timing Out Connections and RTSP Messages
   Receivers of a request (responders) SHOULD respond to requests in a
   timely manner even when a reliable transport such as TCP is used.
   Similarly, the sender of a request (requester) SHOULD wait for a
   sufficient time for a response before concluding that the responder
   will not be acting upon its request.
   A responder SHOULD respond to all requests within 5 seconds.  If the
   responder recognizes that the processing of a request will take
   longer than 5 seconds, it SHOULD send a 100 (Continue) response as
   soon as possible.  It SHOULD continue sending a 100 response every 5
   seconds thereafter until it is ready to send the final response to
   the requester.  After sending a 100 response, the responder MUST send
   a final response indicating the success or failure of the request.
   A requester SHOULD wait at least 10 seconds for a response before
   concluding that the responder will not be responding to its request.
   After receiving a 100 response, the requester SHOULD continue waiting
   for further responses.  If more than 10 seconds elapse without
   receiving any response, the requester MAY assume that the responder
   is unresponsive and abort the connection by closing the TCP
   connection.
   In some cases, multiple RTSP sessions share the same transport
   connection; abandoning a request and closing the connection may have
   significant impact on those other sessions.  First of all, other RTSP
   requests may have become queued up due to the request taking a long
   time to process.  Secondly, those sessions also lose the possibility
   to receive server-to-client requests.  To mitigate that situation,
   the RTSP client or server SHOULD establish a new connection and send
   any requests that are queued up or that haven't received a response
   on this new connection.  Thirdly, to ensure that the RTSP server
   knows which connection is valid for a particular RTSP session, the
   RTSP agent SHOULD send a keep-alive request, if no other request will
   be sent immediately for that RTSP session, for each RTSP session on
   the old connection.  The keep-alive request will normally be a
   SET_PARAMETER with a session header to inform the server that this
   agent cares about this RTSP session.
   A requester SHOULD wait longer than 10 seconds for a response if it
   is experiencing significant transport delays on its connection to the
   responder.  The requester is capable of determining the Round-Trip
   Time (RTT) of the request/response cycle using the Timestamp header
   (Section 18.53) in any RTSP request.
      The 10-second wait was chosen for the following reasons.  It gives
      TCP time to perform a couple of retransmissions, even if operating
      on default values.  It is short enough that users may not abandon
      the process themselves.  However, it should be noted that 10
      seconds can be aggressive on certain types of networks.  The
      5-second value for 1xx messages is half the timeout giving a
      reasonable chance of successful delivery before timeout happens on
      the requester side.
10.5.  Showing Liveness
   RTSP requires the client to periodically show its liveness to the
   server or the server may terminate any session state.  Several
   different protocol mechanism include in their usage a liveness proof
   from the client.  These mechanisms are RTSP requests with a Session
   header to the server; if RTP & RTCP is used for media data transport
   and the transport is established, the RTCP message proves liveness;
   or through any other used media-transport protocol capable of
   indicating liveness of the RTSP client.  It is RECOMMENDED that a
   client not wait to the last second of the timeout before trying to
   send a liveness message.  The RTSP message may take some time to
   arrive safely at the receiver, due to packet loss and TCP
   retransmissions.  To show liveness between RTSP requests being issued
   to accomplish other things, the following mechanisms can be used, in
   descending order of preference:
   RTCP: If RTP is used for media transport, RTCP SHOULD be used.  If
         RTCP is used to report transport statistics, it will
         necessarily also function as a keep-alive.  The server can
         determine the client by network address and port together with
         the fact that the client is reporting on the server's RTP
         sender sources (synchronization source (SSRCs)).  A downside of
         using RTCP is that it only gives statistical guarantees of
         reaching the server.  However, the probability of a false
         client timeout is so low that it can be ignored in most cases.
         For example, assume a session with a 60-second timeout and
         enough bitrate assigned to RTCP messages to send a message from
         client to server on average every 5 seconds.  That client has,
         for a network with 5% packet loss, a probability of failing to
         confirm liveness within the timeout interval for that session
         of 2.4*E-16.  Sessions with shorter timeouts, much higher
         packet loss, or small RTCP bandwidths SHOULD also implement one
         or more of the mechanisms below.
   SET_PARAMETER:  When using SET_PARAMETER for keep-alives, a body
         SHOULD NOT be included.  This method is the RECOMMENDED RTSP
         method to use for a request intended only to perform keep-
         alives.  RTSP servers MUST support the SET_PARAMETER method, so
         that clients can always use this mechanism.
   GET_PARAMETER:  When using GET_PARAMETER for keep-alives, a body
         SHOULD NOT be included, dependent on implementation support in
         the server.  Use the OPTIONS method to determine if there is
         method support or simply try.
   OPTIONS:  This method is also usable, but it causes the server to
         perform more unnecessary processing and results in bigger
         responses than necessary for the task.  The reason is that the
         server needs to determine the capabilities associated with the
         media resource to correctly populate the Public and Allow
         headers.
   The timeout parameter of the Session header (Section 18.49) MAY be
   included in a SETUP response and MUST NOT be included in requests.
   The server uses it to indicate to the client how long the server is
   prepared to wait between RTSP commands or other signs of life before
   closing the session due to lack of activity (see Appendix B).  The
   timeout is measured in seconds, with a default of 60 seconds.  The
   length of the session timeout MUST NOT be changed in an established
   session.
10.6.  Use of IPv6
   2.0 has been updated for explicit IPv6 support.  Implementations of
   RTSP 2.0 MUST understand literal IPv6 addresses in URIs and RTSP
   headers.  Although the general URI format envisages potential future
   new versions of the literal IP address, usage of any such new version
   would require other modifications to the RTSP specification (e.g.,
   address fields in the Transport header (Section 18.54)).
10.7.  Overload Control
   Overload in RTSP can occur when servers and proxies have insufficient
   resources to complete the processing of a request.  An improper
   handling of such an overload situation at proxies and servers can
   impact the operation of the RTSP deployment, and probably worsen the
   situation.  RTSP defines the 503 (Service Unavailable) response
   (Section 17.5.4) to let servers and proxies notify requesting proxies
   and RTSP clients about an overload situation.  In conjunction with
   the Retry-After header (Section 18.44), the server or proxy can
   indicate the time after which the requesting entity can send another
   request to the proxy or server.
   There are two scopes of such 503 answers.  The first scope is for an
   established RTSP session, where the request resulting in the 503
   response as well as the response itself carries a Session header
   identifying the session that is suffering overload.  This response
   only applies to this particular session.  The other scope is the
   general RTSP server as identified by the host in the Request-URI.
   Such a 503 answer with any Retry-After header applies to all requests
   that are not session specific to that server, including a SETUP
   request intended to create a new RTSP session.
   Another scope for overload situations exists: the RTSP proxy.  To
   enable an RTSP proxy to signal that it is overloaded, or otherwise
   unavailable and unable to handle the request, a 553 response code has
   been defined with the meaning "Proxy Unavailable".  As with servers,
   there is a separation in response scopes between requests associated
   with existing RTSP sessions and requests to create new sessions or
   general proxy requests.
   Simply implementing and using the 503 (Service Unavailable) and 553
   (Proxy Unavailable) response codes is not sufficient for properly
   handling overload situations.  For instance, a simplistic approach
   would be to send the 503 response with a Retry-After header set to a
   fixed value.  However, this can cause a situation in which multiple
   RTSP clients again send requests to a proxy or server at roughly the
   same time, which may again cause an overload situation.  Another
   situation would be if the "old" overload situation is not yet
   resolved, i.e., the length indicated in the Retry-After header was
   too short for the overload situation to subside.
   An RTSP server or proxy in an overload situation must select the
   value of the Retry-After header carefully, bearing in mind its
   current load situation.  It is REQUIRED to increase the timeout
   period in proportion to the current load on the server, i.e., an
   increasing workload should result in an increased length of the
   indicated unavailability.  It is REQUIRED not to send the same value
   in the Retry-After header to all requesting proxies and clients, but
   to add a variation to the mean value of the Retry-After header.
   A more complex case may arise when a load-balancing RTSP proxy is in
   use.  This is the case when an RTSP proxy is used to select amongst a
   set of RTSP servers to handle the requests or when multiple server
   addresses are available for a given server name.  The proxy or client
   may receive a 503 (Service Unavailable) or 553 (Proxy Unavailable)
   response code from one of its RTSP servers or proxies, or a TCP
   timeout (if the server is even unable to handle the request message).
   The proxy or client simply retries the other addresses or configured
   proxies, but it may also receive a 503 (Service Unavailable) or 553
   (Proxy Unavailable) response or TCP timeouts from those addresses.
   In such a situation, where none of the RTSP servers/proxies/addresses
   can handle the request, the RTSP agent has to wait before it can send
   any new requests to the RTSP server.  Any additional request to a
   specific address MUST be delayed according to the Retry-After headers
   received.  For addresses where no response was received or TCP
   timeout occurred, an initial wait timer SHOULD be set to 5 seconds.
   That timer MUST be doubled for each additional failure to connect or
   receive response until the value exceeds 30 minutes when the timer's
   mean value may be set to 30 minutes.  It is REQUIRED not to set the
   same value in the timer for each scheduling, but instead to add a
   variation to the mean value, resulting in picking a random value
   within the range of 0.5 to 1.5 times the mean value.