Section Appendix B. RTSP Protocol State Machine

The RTSP session state machine describes the behavior of the protocol
   from RTSP session initialization through RTSP session termination.
   It is probably easiest to think of this as the server's state and
   then view the client as needing to track what it believes the
   server's state will be based on sent or received RTSP messages.
   Thus, in most cases, the state tables below can be read as: if the
   client does X, and assuming it fulfills any prerequisite(s), the
   (server) state will move to the new state and the indicated response
   will returned.  However, there are also server-to-client
   notifications or requests, where the action describes what
   notification or request will occur, its requisites, what new state
   will result after the server has received the response, as well as
   describing the client's response to the action.
   The State machine is defined on a per-session basis, which is
   uniquely identified by the RTSP session identifier.  The session may
   contain one or more media streams depending on state.  If a single
   media stream is part of the session, it is in non-aggregated control.
   If two or more are part of the session, it is in aggregated control.
   The below state machine is an informative description of the
   protocol's behavior.  In case of ambiguity with the earlier parts of
   this specification, the description in the earlier parts take
   precedence.
B.1.  States
   The state machine contains three states, described below.  For each
   state, there exists a table that shows which requests and events are
   allowed and whether they will result in a state change.
   Init: Initial state, no session exists.
   Ready:  Session is ready to start playing.
   Play: Session is playing, i.e., sending media-stream data in the
         direction S->C.
B.2.  State Variables
   This representation of the state machine needs more than its state to
   work.  A small number of variables are also needed, and they are
   explained below.
   NRM:  The number of media streams that are part of this session.
   RP:   Resume point, the point in the presentation time line at which
         a request to continue playing will resume from.  A time format
         for the variable is not mandated.
B.3.  Abbreviations
   To make the state tables more compact, a number of abbreviations are
   used, which are explained below.
   IFI:  IF Implemented.
   md:   Media
   PP:   Pause Point, the point in the presentation timeline at which
         the presentation was paused.
   Prs:  Presentation, the complete multimedia presentation.
   RedP: Redirect Point, the point in the presentation timeline at which
         a REDIRECT was specified to occur.
   SES:  Session.
B.4.  State Tables
   This section contains a table for each state.  The table contains all
   the requests and events on which this state is allowed to act.  The
   events that are method names are, unless noted, requests with the
   given method in the direction client to server (C->S).  In some
   cases, there exists one or more requisites.  The response column
   tells what type of response actions should be performed.  Possible
   actions that are requested for an event include: response codes,
   e.g., 200, headers that need to be included in the response, setting
   of state variables, or settings of other session-related parameters.
   The new state column tells which state the state machine changes to.
   The response to a valid request meeting the requisites is normally a
   2xx (SUCCESS) unless otherwise noted in the response column.  The
   exceptions need to be given a response according to the response
   column.  If the request does not meet the requisite, is erroneous, or
   some other type of error occurs, the appropriate response code is to
   be sent.  If the response code is a 4xx, the session state is
   unchanged.  A response code of 3rr will result in that the session
   being ended and its state changed to Init.  A response code of 304
   results in no state change.  However, there are restrictions to when
   a 3rr response may be used.  A 5xx response does not result in any
   change of the session state, except if the error is not possible to
   recover from.  An unrecoverable error results in the ending of the
   session.  In the general case, if it can't be determined whether or
   not it was an unrecoverable error, the client will be required to
   test.  In the case that the next request after a 5xx is responded to
   with a 454 (Session Not Found), the client knows that the session has
   ended.  For any request message that cannot be responded to within
   the time defined in Section 10.4, a 100 response must be sent.
   The server will time out the session after the period of time
   specified in the SETUP response, if no activity from the client is
   detected.  Therefore, there exists a timeout event for all states
   except Init.
   In the case that NRM = 1, the presentation URI is equal to the media
   URI or a specified presentation URI.  For NRM > 1, the presentation
   URI needs to be other than any of the media that are part of the
   session.  This applies to all states.
   +---------------+-----------------+---------------------------------+
   | Event         | Prerequisite    | Response                        |
   +---------------+-----------------+---------------------------------+
   | DESCRIBE      | Needs REDIRECT  | 3rr, Redirect                   |
   |               |                 |                                 |
   | DESCRIBE      |                 | 200, Session description        |
   |               |                 |                                 |
   | OPTIONS       | Session ID      | 200, Reset session timeout      |
   |               |                 | timer                           |
   |               |                 |                                 |
   | OPTIONS       |                 | 200                             |
   |               |                 |                                 |
   | SET_PARAMETER | Valid parameter | 200, change value of parameter  |
   |               |                 |                                 |
   | GET_PARAMETER | Valid parameter | 200, return value of parameter  |
   +---------------+-----------------+---------------------------------+
                Table 9: Non-State-Machine Changing Events
   The methods in Table 9 do not have any effect on the state machine or
   the state variables.  However, some methods do change other session-
   related parameters, for example, SET_PARAMETER, which will set the
   parameter(s) specified in its body.  Also, all of these methods that
   allow the Session header will also update the keep-alive timer for
   the session.
   +------------------+----------------+-----------+-------------------+
   | Action           | Requisite      | New State | Response          |
   +------------------+----------------+-----------+-------------------+
   | SETUP            |                | Ready     | NRM=1, RP=0.0     |
   |                  |                |           |                   |
   | SETUP            | Needs Redirect | Init      | 3rr Redirect      |
   |                  |                |           |                   |
   | S -> C: REDIRECT | No Session hdr | Init      | Terminate all SES |
   +------------------+----------------+-----------+-------------------+
                           Table 10: State: Init
   The initial state of the state machine (Table 10) can only be left by
   processing a correct SETUP request.  As seen in the table, the two
   state variables are also set by a correct request.  This table also
   shows that a correct SETUP can in some cases be redirected to another
   URI or server by a 3rr response.
   +-------------+------------------------+---------+------------------+
   | Action      | Requisite              | New     | Response         |
   |             |                        | State   |                  |
   +-------------+------------------------+---------+------------------+
   | SETUP       | New URI                | Ready   | NRM +=1          |
   |             |                        |         |                  |
   | SETUP       | URI Setup prior        | Ready   | Change transport |
   |             |                        |         | param            |
   |             |                        |         |                  |
   | TEARDOWN    | Prs URI,               | Init    | No session hdr,  |
   |             |                        |         | NRM = 0          |
   |             |                        |         |                  |
   | TEARDOWN    | md URI,NRM=1           | Init    | No Session hdr,  |
   |             |                        |         | NRM = 0          |
   |             |                        |         |                  |
   | TEARDOWN    | md URI,NRM>1           | Ready   | Session hdr, NRM |
   |             |                        |         | -= 1             |
   |             |                        |         |                  |
   | PLAY        | Prs URI, No range      | Play    | Play from RP     |
   |             |                        |         |                  |
   | PLAY        | Prs URI, Range         | Play    | According to     |
   |             |                        |         | range            |
   |             |                        |         |                  |
   | PLAY        | md URI, NRM=1, Range   | Play    | According to     |
   |             |                        |         | range            |
   |             |                        |         |                  |
   | PLAY        | md URI, NRM=1          | Play    | Play from RP     |
   |             |                        |         |                  |
   | PAUSE       | Prs URI                | Ready   | Return PP        |
   |             |                        |         |                  |
   | SC:REDIRECT | Terminate-Reason       | Ready   | Set RedP         |
   |             |                        |         |                  |
   | SC:REDIRECT | No Terminate-Reason    | Init    | Session is       |
   |             | time parameter         |         | removed          |
   |             |                        |         |                  |
   | Timeout     |                        | Init    |                  |
   |             |                        |         |                  |
   | RedP        |                        | Init    | TEARDOWN of      |
   | reached     |                        |         | session          |
   +-------------+------------------------+---------+------------------+
                          Table 11: State: Ready
   In the Ready state (Table 11), some of the actions depend on the
   number of media streams (NRM) in the session, i.e., aggregated or
   non-aggregated control.  A SETUP request in the Ready state can
   either add one more media stream to the session or, if the media
   stream (same URI) already is part of the session, change the
   transport parameters.  TEARDOWN depends on both the Request-URI and
   the number of media streams within the session.  If the Request-URI
   is the presentation URI, the whole session is torn down.  If a media
   URI is used in the TEARDOWN request and more than one media exists in
   the session, the session will remain and a session header is returned
   in the response.  If only a single media stream remains in the
   session when performing a TEARDOWN with a media URI, the session is
   removed.  The number of media streams remaining after tearing down a
   media stream determines the new state.
   +----------------+-----------------------+--------+-----------------+
   | Action         | Requisite             | New    | Response        |
   |                |                       | State  |                 |
   +----------------+-----------------------+--------+-----------------+
   | PAUSE          | Prs URI               | Ready  | Set RP to       |
   |                |                       |        | present point   |
   |                |                       |        |                 |
   | End of media   | All media             | Play   | Set RP = End of |
   |                |                       |        | media           |
   |                |                       |        |                 |
   | End of range   |                       | Play   | Set RP = End of |
   |                |                       |        | range           |
   |                |                       |        |                 |
   | PLAY           | Prs URI, No range     | Play   | Play from       |
   |                |                       |        | present point   |
   |                |                       |        |                 |
   | PLAY           | Prs URI, Range        | Play   | According to    |
   |                |                       |        | range           |
   |                |                       |        |                 |
   | SC:PLAY_NOTIFY |                       | Play   | 200             |
   |                |                       |        |                 |
   | SETUP          | New URI               | Play   | 455             |
   |                |                       |        |                 |
   | SETUP          | md URI                | Play   | 455             |
   |                |                       |        |                 |
   | SETUP          | md URI, IFI           | Play   | Change          |
   |                |                       |        | transport param.|
   |                |                       |        |                 |
   | TEARDOWN       | Prs URI               | Init   | No session hdr  |
   |                |                       |        |                 |
   | TEARDOWN       | md URI,NRM=1          | Init   | No Session hdr, |
   |                |                       |        | NRM=0           |
   |                |                       |        |                 |
   | TEARDOWN       | md URI                | Play   | 455             |
   |                |                       |        |                 |
   | SC:REDIRECT    | Terminate Reason with | Play   | Set RedP        |
   |                | Time parameter        |        |                 |
   |                |                       |        |                 |
   | SC:REDIRECT    |                       | Init   | Session is      |
   |                |                       |        | removed         |
   |                |                       |        |                 |
   | RedP reached   |                       | Init   | TEARDOWN of     |
   |                |                       |        | session         |
   |                |                       |        |                 |
   | Timeout        |                       | Init   | Stop Media      |
   |                |                       |        | playout         |
   +----------------+-----------------------+--------+-----------------+
                           Table 12: State: Play
   The Play state table (Table 12) contains a number of requests that
   need a presentation URI (labeled as Prs URI) to work on (i.e., the
   presentation URI has to be used as the Request-URI).  This is due to
   the exclusion of non-aggregated stream control in sessions with more
   than one media stream.
   To avoid inconsistencies between the client and server, automatic
   state transitions are avoided.  This can be seen at, for example, an
   "End of media" event when all media has finished playing but the
   session still remains in Play state.  An explicit PAUSE request needs
   to be sent to change the state to Ready.  It may appear that there
   exist automatic transitions in "RedP reached" and "PP reached".
   However, they are requested and acknowledged before they take place.
   The time at which the transition will happen is known by looking at
   the Terminate-Reason header's time parameter and Range header,
   respectively.  If the client sends a request close in time to these
   transitions, it needs to be prepared for receiving error messages, as
   the state may or may not have changed.