1. 程式人生 > >C#實現RTP資料包傳輸

C#實現RTP資料包傳輸

複製程式碼
  1   /// <summary>
  2   /// RTP(RFC3550)協議資料包
  3   /// </summary>
  4   /// <remarks>
  5   /// The RTP header has the following format:
  6   ///  0                   1                   2                   3
  7   ///  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
  8   /// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
9 /// |V=2|P|X| CC |M| PT | sequence number | 10 /// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 11 /// | timestamp | 12 /// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 13 /// | synchronization source (SSRC) identifier |
14 /// +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+ 15 /// | contributing source (CSRC) identifiers | 16 /// | .... | 17 /// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 18 /// </remarks>
19 public class RtpPacket 20 { 21 /// <summary> 22 /// version (V): 2 bits 23 /// RTP版本標識,當前規範定義值為2. 24 /// This field identifies the version of RTP. The version defined by this specification is two (2). 25 /// (The value 1 is used by the first draft version of RTP and the value 0 is used by the protocol 26 /// initially implemented in the \vat" audio tool.) 27 /// </summary> 28 public int Version { get { return 2; } } 29 30 /// <summary> 31 /// padding (P):1 bit 32 /// 如果設定padding,在報文的末端就會包含一個或者多個padding 位元組,這不屬於payload。 33 /// 最後一個位元組的padding 有一個計數器,標識需要忽略多少個padding 位元組(包括自己)。 34 /// 一些加密演算法可能需要固定塊長度的padding,或者是為了在更低層資料單元中攜帶一些RTP 報文。 35 /// If the padding bit is set, the packet contains one or more additional padding octets at the 36 /// end which are not part of the payload. The last octet of the padding contains a count of 37 /// how many padding octets should be ignored, including itself. Padding may be needed by 38 /// some encryption algorithms with fixed block sizes or for carrying several RTP packets in a 39 /// lower-layer protocol data unit. 40 /// </summary> 41 public int Padding { get { return 0; } } 42 43 /// <summary> 44 /// extension (X):1 bit 45 /// 如果設定了extension 位,定長頭欄位後面會有一個頭擴充套件。 46 /// If the extension bit is set, the fixed header must be followed by exactly one header extensio. 47 /// </summary> 48 public int Extension { get { return 0; } } 49 50 /// <summary> 51 /// CSRC count (CC):4 bits 52 /// CSRC count 標識了定長頭欄位中包含的CSRC identifier 的數量。 53 /// The CSRC count contains the number of CSRC identifiers that follow the fixed header. 54 /// </summary> 55 public int CC { get { return 0; } } 56 57 /// <summary> 58 /// marker (M):1 bit 59 /// marker 是由一個profile 定義的。用來允許標識在像報文流中界定幀界等的事件。 60 /// 一個profile 可能定義了附加的標識位或者通過修改payload type 域中的位數量來指定沒有標識位. 61 /// The interpretation of the marker is defined by a profile. It is intended to allow significant 62 /// events such as frame boundaries to be marked in the packet stream. A profile may define 63 /// additional marker bits or specify that there is no marker bit by changing the number of bits 64 /// in the payload type field. 65 /// </summary> 66 public int Marker { get { return 0; } } 67 68 /// <summary> 69 /// payload type (PT):7 bits 70 /// 這個欄位定一個RTPpayload 的格式和在應用中定義解釋。 71 /// profile 可能指定一個從payload type 碼字到payload format 的預設靜態對映。 72 /// 也可以通過non-RTP 方法來定義附加的payload type 碼字(見第3 章)。 73 /// 在 RFC 3551[1]中定義了一系列的預設音視訊對映。 74 /// 一個RTP 源有可能在會話中改變payload type,但是這個域在複用獨立的媒體時是不同的。(見5.2 節)。 75 /// 接收者必須忽略它不識別的payload type。 76 /// This field identifies the format of the RTP payload and determines its interpretation by the 77 /// application. A profile may specify a default static mapping of payload type codes to payload 78 /// formats. Additional payload type codes may be defined dynamically through non-RTP means 79 /// (see Section 3). A set of default mappings for audio and video is specified in the companion 80 /// RFC 3551 [1]. An RTP source may change the payload type during a session, but this field 81 /// should not be used for multiplexing separate media streams (see Section 5.2). 82 /// A receiver must ignore packets with payload types that it does not understand. 83 /// </summary> 84 public RtpPayloadType PayloadType { get; private set; } 85 86 /// <summary> 87 /// sequence number:16 bits 88 /// 每傳送一個RTP 資料報文序列號值加一,接收者也可用來檢測丟失的包或者重建報文序列。 89 /// 初始的值是隨機的,這樣就使得known-plaintext 攻擊更加困難, 即使源並沒有加密(見9。1), 90 /// 因為要通過的translator 會做這些事情。關於選擇隨機數方面的技術見[17]。 91 /// The sequence number increments by one for each RTP data packet sent, and may be used 92 /// by the receiver to detect packet loss and to restore packet sequence. The initial value of the 93 /// sequence number should be random (unpredictable) to make known-plaintext attacks on 94 /// encryption more dificult, even if the source itself does not encrypt according to the method 95 /// in Section 9.1, because the packets may flow through a translator that does. Techniques for 96 /// choosing unpredictable numbers are discussed in [17]. 97 /// </summary> 98 public int SequenceNumber { get; private set; } 99 100 /// <summary> 101 /// timestamp:32 bits 102 /// timestamp 反映的是RTP 資料報文中的第一個欄位的取樣時刻的時間瞬時值。 103 /// 取樣時間值必須是從恆定的和線性的時間中得到以便於同步和jitter 計算(見第6.4.1 節)。 104 /// 必須保證同步和測量保溫jitter 到來所需要的時間精度(一幀一個tick 一般情況下是不夠的)。 105 /// 時鐘頻率是與payload 所攜帶的資料格式有關的,在profile 中靜態的定義或是在定義格式的payload format 中, 106 /// 或通過non-RTP 方法所定義的payload format 中動態的定義。如果RTP 報文週期的生成,就採用虛擬的(nominal) 107 /// 取樣時鐘而不是從系統時鐘讀數。例如,在固定位元率的音訊中,timestamp 時鐘會在每個取樣週期時加一。 108 /// 如果音訊應用中從輸入裝置中讀入160 個取樣週期的塊,the timestamp 就會每一塊增加160, 109 /// 而不管塊是否傳輸了或是丟棄了。 110 /// 對於序列號來說,timestamp 初始值是隨機的。只要它們是同時(邏輯上)同時生成的, 111 /// 這些連續的的 RTP 報文就會有相同的timestamp, 112 /// 例如,同屬一個視訊幀。正像在MPEG 中內插視訊幀一樣, 113 /// 連續的但不是按順序傳送的RTP 報文可能含有相同的timestamp。 114 /// The timestamp reflects the sampling instant of the first octet in the RTP data packet. The 115 /// sampling instant must be derived from a clock that increments monotonically and linearly 116 /// in time to allow synchronization and jitter calculations (see Section 6.4.1). The resolution 117 /// of the clock must be suficient for the desired synchronization accuracy and for measuring 118 /// packet arrival jitter (one tick per video frame is typically not suficient). The clock frequency 119 /// is dependent on the format of data carried as payload and is specified statically in the profile 120 /// or payload format specification that defines the format, or may be specified dynamically for 121 /// payload formats defined through non-RTP means. If RTP packets are generated periodically, 122 /// the nominal sampling instant as determined from the sampling clock is to be used, not a 123 /// reading of the system clock. As an example, for fixed-rate audio the timestamp clock would 124 /// likely increment by one for each sampling period. If an audio application reads blocks covering 125 /// 160 sampling periods from the input device, the timestamp would be increased by 160 for 126 /// each such block, regardless of whether the block is transmitted in a packet or dropped as silent. 127 /// </summary> 128 public long Timestamp { get; private set; } 129 130 /// <summary> 131 /// SSRC:32 bits 132 /// SSRC 域識別同步源。為了防止在一個會話中有相同的同步源有相同的SSRC identifier, 133 /// 這個identifier 必須隨機選取。 134 /// 生成隨機 identifier 的演算法見目錄A.6 。雖然選擇相同的identifier 概率很小, 135 /// 但是所有的RTP implementation 必須檢測和解決衝突。 136 /// 第8 章描述了衝突的概率和解決機制和RTP 級的檢測機制,根據唯一的 SSRCidentifier 前向迴圈。 137 /// 如果有源改變了它的源傳輸地址, 138 /// 就必須為它選擇一個新的SSRCidentifier 來避免被識別為迴圈過的源(見第8.2 節)。 139 /// The SSRC field identifies the synchronization source. This identifier should be chosen 140 /// randomly, with the intent that no two synchronization sources within the same RTP session 141 /// will have the same SSRC identifier. An example algorithm for generating a random identifier 142 /// is presented in Appendix A.6. Although the probability of multiple sources choosing the same 143 /// identifier is low, all RTP implementations must be prepared to detect and resolve collisions. 144 /// Section 8 describes the probability of collision along with a mechanism for resolving collisions 145 /// and detecting RTP-level forwarding loops based on the uniqueness of the SSRC identifier. If 146 /// a source changes its source transport address, it must also choose a new SSRC identifier to 147 /// avoid being interpreted as a looped source (see Section 8.2). 148 /// </summary> 149 public int SSRC { get { return 0; } } 150 151 /// <summary> 152 /// 每一個RTP包中都有前12個位元組定長的頭欄位 153 /// The first twelve octets are present in every RTP packet 154 /// </summary> 155 public const int HeaderSize = 12; 156 /// <summary> 157 /// RTP訊息頭 158 /// </summary> 159 private byte[] _header; 160 /// <summary> 161 /// RTP訊息頭 162 /// </summary> 163 public byte[] Header { get { return _header; } } 164 165 /// <summary> 166 /// RTP有效載荷長度 167 /// </summary> 168 private int _payloadSize; 169 /// <summary> 170 /// RTP有效載荷長度 171 /// </summary> 172 public int PayloadSize { get { return _payloadSize; } } 173 174 /// <summary> 175 /// RTP有效載荷 176 /// </summary> 177 private byte[] _payload; 178 /// <summary> 179 /// RTP有效載荷 180 /// </summary> 181 public byte[] Payload { get { return _payload; } } 182 183 /// <summary> 184 /// RTP訊息總長度,包括Header和Payload 185 /// </summary> 186 public int Length { get { return HeaderSize + PayloadSize; } } 187 188 /// <summary> 189 /// RTP(RFC3550)協議資料包 190 /// </summary> 191 /// <param name="playloadType">資料報文有效載荷型別</param> 192 /// <param name="sequenceNumber">資料報文序列號值</param> 193 /// <param name="timestamp">資料報文采樣時刻</param> 194 /// <param name="data">資料</param> 195 /// <param name="dataSize">資料長度</param> 196 public RtpPacket( 197 RtpPayloadType playloadType, 198 int sequenceNumber, 199 long timestamp, 200 byte[] data, 201 int dataSize) 202 { 203 // fill changing header fields 204 SequenceNumber = sequenceNumber; 205 Timestamp = timestamp; 206 PayloadType = playloadType; 207 208 // build the header bistream 209 _header = new byte[HeaderSize]; 210 211 // fill the header array of byte with RTP header fields 212 _header[0] = (byte)((Version << 6) | (Padding << 5) | (Extension << 4) | CC); 213 _header[1] = (byte)((Marker << 7) | (int)PayloadType); 214 _header[2] = (byte)(SequenceNumber >> 8); 215 _header[3] = (byte)(SequenceNumber); 216 for (int i = 0; i < 4; i++) 217 { 218 _header[7 - i] = (byte)(Timestamp >> (8 * i)); 219 } 220 for (int i = 0; i < 4; i++) 221 { 222 _header[11 - i] = (byte)(SSRC >> (8 * i)); 223 } 224 225 // fill the payload bitstream 226 _payload = new byte[dataSize]; 227 _payloadSize = dataSize; 228 229 // fill payload array of byte from data (given in parameter of the constructor) 230 Array.Copy(data, 0, _payload, 0, dataSize); 231 } 232 233 /// <summary> 234