2013年1月10日 星期四

RTSP -- Keep alive 的作法

用來實作 RTSP KeppAlive 的方法,大致上分成兩類。

1. 使用 RTSP method,例如:Play, GET_PARAMETER, SET_PARAMETER, OPTIONS
2. 使用 Receiver report RTCP packet




資料參考出處如下

1. RTSP Method: Play
參考 RFC 2326, chapter 10.5 PLAY 
A PLAY request without a Range header is legal. It starts playing a stream from the beginning unless the stream has been paused. If a stream has been paused via PAUSE, stream delivery resumes at the pause point. If a stream is playing, such a PLAY request causes no further action and can be used by the client to test server liveness.
 
2. RTSP Method: GET_PARAMETER
參考 RFC 2326, chapter 10.8 GET_PARAMETER 
The GET_PARAMETER request retrieves the value of a parameter of a
presentation or stream specified in the URI. The content of the reply
and response is left to the implementation. GET_PARAMETER with no
entity body may be used to test client or server liveness ("ping").
Example: 
S->C: GET_PARAMETER rtsp://example.com/fizzle/foo RTSP/1.0
CSeq: 431
Content-Type: text/parameters
Session: 12345678

 
3. RTSP Method: SET_PARAMETER
VLC實作發現,使用 GET_PARAMETER 作 keep alive,曾在與 DSS 互通時遇過問題,建議更改方式如下:
  • (1) Use SET_PARAMETER(vlc=1)
  • (2) Use GET_PARAMETER only if declared as available in OPTIONS
  • (3) Invert the test so that the default is to use GET_PARAMETER, and provide some detection of DSS as a workround for their failure to implement it (or its absence) properly.
參考 VLC 討論區文章 
  • http://trac.videolan.org/vlc/ticket/1881
  • http://trac.videolan.org/vlc/ticket/2726  
  • http://forum.videolan.org/viewtopic.php?f=14&t=48794&start=0&st=0&sk=t&sd=a 

4. 使用 RTCP RR(Receiver report)
ONVIF 定義除了 RTCP 原本的方法外,還可以使用 RTCP RR 或是 SET_PARAMETER。以下摘錄 ONVIF-Streaming-Spec-v221.pdf 
5.2.1.1.1 Keep-alive method for RTSP session 
A RTSP client keeps the RTSP Session alive and prevents it from session timeout (see [RFC 2326] Section 12.37). This specification recommends the following methods to keep RTSP alive for both Unicast and Multicast streaming.
1) The client can optionally set the Timeout parameter (in seconds) using the Set<configurationEntity>EncoderConfiguration command defined in the ONVIF Media Service Specification, otherwise a default value of ”60” is used.
2) In all RTSP SETUP responses, a transmitter should include the Timeout value according to [RFC 2326] Section 12.37 and the transmitter should use the Timeout value for keep-alive.
3) To keep the RTSP Session alive, a client shall call the RTSP server using any RTSP method or send RTCP receiver reports. SET_PARAMETER is the RECOMMENDED RTSP method to use.

6.6 RTSP Keepalive

When rate control is disabled and the RTP stream is tunneled through the RTSP connection (i.e. using the RTP/RTSP/TCP or RTP/RTSP/HTTP/TCP transports), the client must be aware that it may not be able to receive the response to any request if for example replay is paused.

5. RTSP Method: OPTIONS
ONVIF的測試項目
  • RTSS-1-1-2 使用 SET_PARAMENTER 測試 RTSP Keep Alive
  • RTSS-1-1-3 使用 OPTIONS 來實作 RTSP Keep Alive

6. 程式實作舉例 (參考 live555  RTSPServer.cpp)
// 從 RTSP Setup 中取得 timeout 時間,並紀錄在fOurServer.fReclamationTestSeconds
void RTSPServer::RTSPClientSession::handleCmd_SETUP()
sprintf(timeoutParameterString, ";timeout=%u", fOurServer.fReclamationTestSeconds); 
// 當 timeout 發生時,會執行 livenessTimeoutTask()
void RTSPServer::RTSPClientSession::livenessTimeoutTask() 
// 若有收到新的 RTSP 命令,便會延長 timeout 時間,註:live555預設timeout為65秒
void RTSPServer::RTSPClientSession::noteLiveness()
{
  if (fOurServer.fReclamationTestSeconds > 0) {
    envir().taskScheduler()
    .rescheduleDelayedTask(fLivenessCheckTask,
    fOurServer.fReclamationTestSeconds*1000000,
    (TaskFunc*)livenessTimeoutTask, this);
  }
}