\section{RTSP Protocol State Machines}
\label{sec:machine}

The RTSP client and server state machines describe the behavior of the
protocol from RTSP session initialization through RTSP session
termination.

State is defined on a per object basis.  An object is uniquely
identified by the stream URL and the RTSP session identifier.  Any
request/reply using aggregate URLs denoting RTSP presentations composed
of multiple streams will have an effect on the individual states of all
the streams.  For example, if the presentation {\tt /movie} contains two
streams, {\tt /movie/audio} and {\tt /movie/video}, then the following
command:

\begin{verbatim}
  PLAY rtsp://foo.com/movie RTSP/1.0
  CSeq: 559
  Session: 12345678
\end{verbatim}

will have an effect on the states of {\tt movie/audio} and {\tt
movie/video}.

\motivation{This example does not imply a standard way to represent
streams in URLs or a relation to the filesystem.  See
Section~\ref{sec:url}.}

The requests \header{OPTIONS}, \header{ANNOUNCE}, \header{DESCRIBE},
\header{GET\_PARAMETER}, \header{SET\_PARAMETER} do not have any effect
on client or server state and are therefore not listed in the state
tables.

\subsection{Client State Machine}

The client can assume the following states:

\begin{description}
\item[{\it Init}:] \header{SETUP} has been sent, waiting for reply.

\item[{\it Ready}:] \header{SETUP} reply received or \header{PAUSE}
reply received while in {\it Playing} state.

\item[{\it Playing}:] \header{PLAY} reply received

\item[{\it Recording}:] \header{RECORD} reply received
\end{description}

In general, the client changes state on receipt of replies to requests. 
Note that some requests are effective at a future time or position (such
as a \header{PAUSE}), and state also changes accordingly.  If no explicit
\header{SETUP} is required for the object (for example, it is available via
a multicast group), state begins at {\it Ready}.  In this case, there
are only two states, {\it Ready} and {\it Playing}.
The client also changes state from {\it Playing/Recording} to {\it
Ready} when the end of the requested range is reached. 

The ``next state'' column indicates the state assumed after receiving a
success response (2xx).  If a request yields a status code of 3xx, the
state becomes {\it Init}, and a status code of 4xx yields no change in state. 
Messages not listed for each state MUST NOT be issued by the client in
that state, with the exception of messages not affecting state, as
listed above.  Receiving a \header{REDIRECT} from the server is equivalent
to receiving a 3xx redirect status from the server.

\begin{flushleft}
\begin{tabular}{lll}
state & message sent & next state after response\\
\hline
Init      & \header{SETUP}    & {\it Ready}\\
          & \header{TEARDOWN} & {\it Init}\\
Ready     & \header{PLAY}     & {\it Playing}\\
          & \header{RECORD}   & {\it Recording}\\
          & \header{TEARDOWN} & {\it Init}\\
	  & \header{SETUP}    & {\it Ready}\\
Playing   & \header{PAUSE}    & {\it Ready}\\
          & \header{TEARDOWN} & {\it Init}\\
          & \header{PLAY}     & {\it Playing}\\
          & \header{SETUP}    & {\it Playing} (changed transport)\\
Recording & \header{PAUSE}    & {\it Ready}\\
          & \header{TEARDOWN} & {\it Init}\\
          & \header{RECORD}   & {\it Recording}\\
          & \header{SETUP}    & {\it Recording} (changed transport)\\
\end{tabular}
\end{flushleft}

\subsection{Server State Machine}

The server can assume the following states:

\begin{description}

\item[{\it Init}:] The initial state, no valid \header{SETUP} has been
received yet.

\item[{\it Ready}:] Last \header{SETUP} received was successful, reply sent
or after playing, last \header{PAUSE} received was successful, reply sent.

\item[{\it Playing}:] Last \header{PLAY} received was successful, reply
sent.  Data is being sent.

\item[{\it Recording}:] The server is recording media data.

\end{description}

In general, the server changes state on receiving requests.  If the
server is in state {\it Playing} or {\it Recording} and in unicast mode,
it MAY revert to {\it Init} and tear down the RTSP session if it has not
received ``wellness'' information, such as RTCP reports or RTSP
commands, from the client for a defined interval, with a default of one
minute.  The server can declare another timeout value in the
\header{Session} response header (Section~\ref{sec:Session}).  If the
server is in state {\it Ready}, it MAY revert to {\it Init} if it does
not receive an RTSP request for an interval of more than one minute. 
Note that some requests (such as PAUSE) may be effective at a future
time or position, and server state changes at the appropriate time.  The
server reverts from state {\it Playing} or {\it Recording} to state {\it
Ready} at the end of the range requested by the client.
 
The \header{REDIRECT} message, when sent, is effective immediately unless
it has a \header{Range} header specifying when the redirect is effective. 
In such a case, server state will also change at the appropriate time.

If no explicit \header{SETUP} is required for the object, the state
starts at {\it Ready} and there are only two states, {\it Ready} and
{\it Playing}.

The ``next state'' column indicates the state assumed after sending a
success response (2xx).  If a request results in a status code of 3xx,
the state becomes {\it Init}.  A status code of 4xx results in no
change.

\begin{flushleft}
\begin{tabular}{lll}
state & message received & next state\\
\hline
{\it Init}      & \header{SETUP}    & {\it Ready}\\
                & \header{TEARDOWN} & {\it Init}\\
{\it Ready}     & \header{PLAY}     & {\it Playing}\\
                & \header{SETUP}    & {\it Ready}\\
                & \header{TEARDOWN} & {\it Init}\\
                & \header{RECORD}   & {\it Recording}\\
{\it Playing}   & \header{PLAY}     & {\it Playing}\\
                & \header{PAUSE}    & {\it Ready}\\
                & \header{TEARDOWN} & {\it Init}\\
                & \header{SETUP}    & {\it Playing}\\
{\it Recording} & \header{RECORD}   & {\it Recording}\\
                & \header{PAUSE}    & {\it Ready}\\
                & \header{TEARDOWN} & {\it Init}\\
                & \header{SETUP}    & {\it Recording}\\
\end{tabular}
\end{flushleft}
