天天看點

rtsp連接配接斷開_一個RTSP/RTP over TCP 的丢包引起的問題

背景知識:可以檢視https://www.cnblogs.com/lidabo/p/4483497.html

RTSP/RTP over TCP

TCP承載RTSP/RTP

When you use RTSP/RTP over TCP, all command and media data will be sent through the RTSP port, normally, port 554. Also, when using

當使用TCP協定承載RTSP/RTP時,所有的指令和媒體資料都将通過RTSP端口,通常是554,進行發送。同時,資料将經過二進制交織格式化之後才能發

RTSP/RTP over TCP, the data will be sent via binary interleave format.

送。

Below will describe the essential for using RTSP/RTP over TCP

接下來我們将描述使用TCP承載RTSP/RTP的主要要素:

SETUP

To use TCP communication, you need to request TCP connection during RTSP SETUP. You have to sent SETUP command with

要使用TCP連接配接,RTSP用戶端需要在SETUP階段請求TCP連接配接。SETUP指令中應該包括如下格式的Transport:

Transport: RTP/AVP/TCP;interleaved=0-1

This will tell the server to send media data with TCP and interleave the data in channel 0 and 1. Given in the specification, data channel is even

上述Transport将告訴服務端使用TCP協定發送媒體資料,并且使用信道 0 和 1 對流資料以及控制資訊進行交織。詳細說來,使用偶數信道作為資料

number and control channel is odd (data_ch_num + 1). So, if you data channel is 0, your control channel will be 0 + 1 = 1.

傳輸信道,使用奇數信道作為控制信道(資料信道 + 1)。是以,如果你設定資料信道為 0 ,那控制信道應該是 0 + 1 = 1。

Below is an example of TCP SETUP

rtsp連接配接斷開_一個RTSP/RTP over TCP 的丢包引起的問題

RTP Data

After the setup, RTP data will be sent through the TCP socket that is used for RTSP commands. The RTP data will be encapsulate in the following format

SETUP之後,RTP資料将通過用來發送RTSP指令的TCP Socket進行發送。RTP資料将以如下格式進行封裝:

| magic number | channel number | embedded data length | data |

magic number - 1 byte value of hex 0x24

RTP資料辨別符,"$"

channel number - 1 byte value to denote the channel

信道數字 - 1個位元組,用來訓示信道

embedded data length - 2 bytes to denote the embedded data length

資料長度 - 2個位元組,用來訓示插入資料長度

data - data packet, ie RTP packet, with the total length of the embedded data length

資料 - 資料包,比如說RTP包,總長度與上面的資料長度相同

Below is a full example of the communication exchanged

下面是互動過程的一個完整示例:

rtsp連接配接斷開_一個RTSP/RTP over TCP 的丢包引起的問題

問題描述:

當我們的rtp定位符丢失之後,機頂盒應該如何在不重建立聯的情況下,保證後面的播放?

1.定位符$(0x24)這部分丢失,也就是不知道信道,也不知道長度,由于是tcp,無法知道下一個定位符在哪。

2.如果周遊後面的封包,可能是媒體,也可能是信令,則0x24可能會找錯,比如媒體資料裡面有0x24.

3.定位符丢失的原因可能是因為前面的rtp媒體資料丢失,後面連着一個定位符,但是被機頂盒解碼為媒體資料,這樣相當于後面那個定位符就丢失了。

4.tcp按道理是不會丢包,但是由于使用者态在多次tryagain的時候,到時間就可能需要發送下一個封包了(處于實時性考慮),這樣就出現了丢包現象。

解決方法:

媒體伺服器發包的時候,保證原子性,因為tcp會重傳,那麼唯一丢失rtp定位符是的可能條件是,應用程式在發包的時候,發送到定位符部分,而socket由于buf是滿的,

會導緻我們應用程式重試,假設那段時間一直重試失敗,則有可能導緻發送下一個封包。這就要求在tcp發送的時候,必須一直重試,而在udp的時候,可以重試幾次後

發送下一個封包,因為udp的是有界的。