SETUP請求消息處理過程
前面已經提到RTSPClientSession類,用於處理單獨的客戶會話。其類成員函數handleCmd_SETUP()處理客戶端的SETUP請求。調用parseTransportHeader()對SETUP請求的傳輸頭解析,調用子會話(這裏具體實現類爲OnDemandServerMediaSubsession)的getStreamParameters()函數獲取流媒體發送傳輸參數。將這些參數組裝成響應消息,返回給客戶端。
獲取發送傳輸參數的過程:調用子會話(具體實現類MPEG1or2DemuxedServerMediaSubsession)的createNewStreamSource(...)創建MPEG1or2VideoStreamFramer,選擇發送傳輸參數,並調用子會話的createNewRTPSink(...)創建MPEG1or2VideoRTPSink。同時將這些信息保存在StreamState類對象中,用於記錄流的狀態。
客戶端發送兩個SETUP請求,分別用於建立音頻和視頻的RTP接收。
PLAY請求消息處理過程
RTSPClientSession類成員函數handleCmd_PLAY()處理客戶端的播放請求。首先調用子會話的startStream(),內部調用MediaSink::startPlaying(...),然後是MultiFramedRTPSink::continuePlaying(),接着調用MultiFramedRTPSink::buildAndSendPacket(...)。buildAndSendPacke內部先設置RTP包頭,內部再調用MultiFramedRTPSink::packFrame()填充編碼幀數據。
packFrame內部通過FramedSource::getNextFrame(), 接着MPEGVideoStreamFramer::doGetNextFrame(),再接着經過MPEGVideoStreamFramer::continueReadProcessing(), FramedSource::afterGetting(...), MultiFramedRTPSink::afterGettingFrame(...), MultiFramedRTPSink::afterGettingFrame1(...)等一系列繁瑣調用,最後到了MultiFramedRTPSink::sendPacketIfNecessary(),
這裏才真正發送RTP數據包。然後是計算下一個數據包發送時間,把MultiFramedRTPSink::sendNext(...)函數句柄傳給任務調度器,作爲一個延時事件調度。在主循環中,當MultiFramedRTPSink::sendNext()被調度時,又開始調用MultiFramedRTPSink::buildAndSendPacket(...)開始新的發送數據過程,這樣客戶端可以源源不斷的收到服務器傳來的RTP包了。
發送RTP數據包的間隔計算方法:
Update the time at which the next packet should be sent, based on the duration of the frame that we just packed into it.
涉及到一些類有:
MPEGVideoStreamFramer: A filter that breaks up an MPEG video elementary stream into headers and frames
MPEG1or2VideoStreamFramer: A filter that breaks up an MPEG 1 or 2 video elementary stream into frames for: Video_Sequence_Header, GOP_Header, Picture_Header
MPEG1or2DemuxedElementaryStream: A MPEG 1 or 2 Elementary Stream, demultiplexed from a Program Stream
MPEG1or2Demux: Demultiplexer for a MPEG 1 or 2 Program Stream
ByteStreamFileSource: A file source that is a plain byte stream (rather than frames)
MPEGProgramStreamParser: Class for parsing MPEG program stream
StreamParser: Abstract class for parsing a byte stream
StreamState:A class that represents the state of an ongoing stream