亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb

首頁 > 編程 > C# > 正文

使用C#實現RTP數據包傳輸 參照RFC3550

2020-01-24 03:27:24
字體:
來源:轉載
供稿:網友

閑暇時折騰IP網絡視頻監控系統,需要支持視頻幀數據包在網絡內的傳輸。
未采用H.264或MPEG4等編碼壓縮方式,直接使用Bitmap圖片。
由于對幀的準確到達要求不好,所以采用UDP傳輸。如果發生網絡丟包現象則直接將幀丟棄。
為了記錄數據包的傳輸順序和幀的時間戳,所以研究了下RFC3550協議,采用RTP包封裝視頻幀。
并未全面深究,所以未使用SSRC和CSRC,因為不確切了解其用意。不過目前的實現情況已經足夠了。

復制代碼 代碼如下:

/// <summary>
   /// RTP(RFC3550)協議數據包
   /// </summary>
   /// <remarks>
   /// The RTP header has the following format:
   ///  0                   1                   2                   3
   ///  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
   /// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   /// |V=2|P|X| CC    |M| PT          | sequence number               |
   /// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   /// | timestamp                                                     |
   /// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   /// | synchronization source (SSRC) identifier                      |
   /// +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
   /// | contributing source (CSRC) identifiers                        |
   /// | ....                                                          |
   /// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   /// </remarks>
   public class RtpPacket
   {
     /// <summary>
     /// version (V): 2 bits
     /// RTP版本標識,當前規范定義值為2.
     /// This field identifies the version of RTP. The version defined by this specification is two (2).
     /// (The value 1 is used by the first draft version of RTP and the value 0 is used by the protocol
     /// initially implemented in the /vat" audio tool.)
     /// </summary>
     public int Version { get { return 2; } }

     /// <summary>
     /// padding (P):1 bit
     /// 如果設定padding,在報文的末端就會包含一個或者多個padding 字節,這不屬于payload。
     /// 最后一個字節的padding 有一個計數器,標識需要忽略多少個padding 字節(包括自己)。
     /// 一些加密算法可能需要固定塊長度的padding,或者是為了在更低層數據單元中攜帶一些RTP 報文。
     /// If the padding bit is set, the packet contains one or more additional padding octets at the
     /// end which are not part of the payload. The last octet of the padding contains a count of
     /// how many padding octets should be ignored, including itself. Padding may be needed by
     /// some encryption algorithms with fixed block sizes or for carrying several RTP packets in a
     /// lower-layer protocol data unit.
     /// </summary>
     public int Padding { get { return 0; } }

     /// <summary>
     /// extension (X):1 bit
     /// 如果設定了extension 位,定長頭字段后面會有一個頭擴展。
     /// If the extension bit is set, the fixed header must be followed by exactly one header extensio.
     /// </summary>
     public int Extension { get { return 0; } }

     /// <summary>
     /// CSRC count (CC):4 bits
     /// CSRC count 標識了定長頭字段中包含的CSRC identifier 的數量。
     /// The CSRC count contains the number of CSRC identifiers that follow the fixed header.
     /// </summary>
     public int CC { get { return 0; } }

     /// <summary>
     /// marker (M):1 bit
     /// marker 是由一個profile 定義的。用來允許標識在像報文流中界定幀界等的事件。
     /// 一個profile 可能定義了附加的標識位或者通過修改payload type 域中的位數量來指定沒有標識位.
     /// The interpretation of the marker is defined by a profile. It is intended to allow significant
     /// events such as frame boundaries to be marked in the packet stream. A profile may define
     /// additional marker bits or specify that there is no marker bit by changing the number of bits
     /// in the payload type field.
     /// </summary>
     public int Marker { get { return 0; } }

     /// <summary>
     /// payload type (PT):7 bits
     /// 這個字段定一個RTPpayload 的格式和在應用中定義解釋。
     /// profile 可能指定一個從payload type 碼字到payload format 的默認靜態映射。
     /// 也可以通過non-RTP 方法來定義附加的payload type 碼字(見第3 章)。
     /// 在 RFC 3551[1]中定義了一系列的默認音視頻映射。
     /// 一個RTP 源有可能在會話中改變payload type,但是這個域在復用獨立的媒體時是不同的。(見5.2 節)。
     /// 接收者必須忽略它不識別的payload type。
     /// This field identifies the format of the RTP payload and determines its interpretation by the
     /// application. A profile may specify a default static mapping of payload type codes to payload
     /// formats. Additional payload type codes may be defined dynamically through non-RTP means
     /// (see Section 3). A set of default mappings for audio and video is specified in the companion
     /// RFC 3551 [1]. An RTP source may change the payload type during a session, but this field
     /// should not be used for multiplexing separate media streams (see Section 5.2).
     /// A receiver must ignore packets with payload types that it does not understand.
     /// </summary>
     public RtpPayloadType PayloadType { get; private set; }

     /// <summary>
     /// sequence number:16 bits
     /// 每發送一個RTP 數據報文序列號值加一,接收者也可用來檢測丟失的包或者重建報文序列。
     /// 初始的值是隨機的,這樣就使得known-plaintext 攻擊更加困難, 即使源并沒有加密(見9。1),
     /// 因為要通過的translator 會做這些事情。關于選擇隨機數方面的技術見[17]。
     /// The sequence number increments by one for each RTP data packet sent, and may be used
     /// by the receiver to detect packet loss and to restore packet sequence. The initial value of the
     /// sequence number should be random (unpredictable) to make known-plaintext attacks on
     /// encryption more dificult, even if the source itself does not encrypt according to the method
     /// in Section 9.1, because the packets may flow through a translator that does. Techniques for
     /// choosing unpredictable numbers are discussed in [17].
     /// </summary>
     public int SequenceNumber { get; private set; }

     /// <summary>
     /// timestamp:32 bits
     /// timestamp 反映的是RTP 數據報文中的第一個字段的采樣時刻的時間瞬時值。
     /// 采樣時間值必須是從恒定的和線性的時間中得到以便于同步和jitter 計算(見第6.4.1 節)。
     /// 必須保證同步和測量保溫jitter 到來所需要的時間精度(一幀一個tick 一般情況下是不夠的)。
     /// 時鐘頻率是與payload 所攜帶的數據格式有關的,在profile 中靜態的定義或是在定義格式的payload format 中,
     /// 或通過non-RTP 方法所定義的payload format 中動態的定義。如果RTP 報文周期的生成,就采用虛擬的(nominal)
     /// 采樣時鐘而不是從系統時鐘讀數。例如,在固定比特率的音頻中,timestamp 時鐘會在每個采樣周期時加一。
     /// 如果音頻應用中從輸入設備中讀入160 個采樣周期的塊,the timestamp 就會每一塊增加160,
     /// 而不管塊是否傳輸了或是丟棄了。
     /// 對于序列號來說,timestamp 初始值是隨機的。只要它們是同時(邏輯上)同時生成的,
     /// 這些連續的的 RTP 報文就會有相同的timestamp,
     /// 例如,同屬一個視頻幀。正像在MPEG 中內插視頻幀一樣,
     /// 連續的但不是按順序發送的RTP 報文可能含有相同的timestamp。
     /// The timestamp reflects the sampling instant of the first octet in the RTP data packet. The
     /// sampling instant must be derived from a clock that increments monotonically and linearly
     /// in time to allow synchronization and jitter calculations (see Section 6.4.1). The resolution
     /// of the clock must be suficient for the desired synchronization accuracy and for measuring
     /// packet arrival jitter (one tick per video frame is typically not suficient). The clock frequency
     /// is dependent on the format of data carried as payload and is specified statically in the profile
     /// or payload format specification that defines the format, or may be specified dynamically for
     /// payload formats defined through non-RTP means. If RTP packets are generated periodically,
     /// the nominal sampling instant as determined from the sampling clock is to be used, not a
     /// reading of the system clock. As an example, for fixed-rate audio the timestamp clock would
     /// likely increment by one for each sampling period. If an audio application reads blocks covering
     /// 160 sampling periods from the input device, the timestamp would be increased by 160 for
     /// each such block, regardless of whether the block is transmitted in a packet or dropped as silent.
     /// </summary>
     public long Timestamp { get; private set; }

     /// <summary>
     /// SSRC:32 bits
     /// SSRC 域識別同步源。為了防止在一個會話中有相同的同步源有相同的SSRC identifier,
     /// 這個identifier 必須隨機選取。
     /// 生成隨機 identifier 的算法見目錄A.6 。雖然選擇相同的identifier 概率很小,
     /// 但是所有的RTP implementation 必須檢測和解決沖突。
     /// 第8 章描述了沖突的概率和解決機制和RTP 級的檢測機制,根據唯一的 SSRCidentifier 前向循環。
     /// 如果有源改變了它的源傳輸地址,
     /// 就必須為它選擇一個新的SSRCidentifier 來避免被識別為循環過的源(見第8.2 節)。
     /// The SSRC field identifies the synchronization source. This identifier should be chosen
     /// randomly, with the intent that no two synchronization sources within the same RTP session
     /// will have the same SSRC identifier. An example algorithm for generating a random identifier
     /// is presented in Appendix A.6. Although the probability of multiple sources choosing the same
     /// identifier is low, all RTP implementations must be prepared to detect and resolve collisions.
     /// Section 8 describes the probability of collision along with a mechanism for resolving collisions
     /// and detecting RTP-level forwarding loops based on the uniqueness of the SSRC identifier. If
     /// a source changes its source transport address, it must also choose a new SSRC identifier to
     /// avoid being interpreted as a looped source (see Section 8.2).
     /// </summary>
     public int SSRC { get { return 0; } }

     /// <summary>
     /// 每一個RTP包中都有前12個字節定長的頭字段
     /// The first twelve octets are present in every RTP packet
     /// </summary>
     public const int HeaderSize = 12;
     /// <summary>
     /// RTP消息頭
     /// </summary>
     private byte[] _header;
     /// <summary>
     /// RTP消息頭
     /// </summary>
     public byte[] Header { get { return _header; } }

     /// <summary>
     /// RTP有效載荷長度
     /// </summary>
     private int _payloadSize;
     /// <summary>
     /// RTP有效載荷長度
     /// </summary>
     public int PayloadSize { get { return _payloadSize; } }

     /// <summary>
     /// RTP有效載荷
     /// </summary>
     private byte[] _payload;
     /// <summary>
     /// RTP有效載荷
     /// </summary>
     public byte[] Payload { get { return _payload; } }

     /// <summary>
     /// RTP消息總長度,包括Header和Payload
     /// </summary>
     public int Length { get { return HeaderSize + PayloadSize; } }

     /// <summary>
     /// RTP(RFC3550)協議數據包
     /// </summary>
     /// <param name="playloadType">數據報文有效載荷類型</param>
     /// <param name="sequenceNumber">數據報文序列號值</param>
     /// <param name="timestamp">數據報文采樣時刻</param>
     /// <param name="data">數據</param>
     /// <param name="dataSize">數據長度</param>
     public RtpPacket(
       RtpPayloadType playloadType,
       int sequenceNumber,
       long timestamp,
       byte[] data,
       int dataSize)
     {
       // fill changing header fields
       SequenceNumber = sequenceNumber;
       Timestamp = timestamp;
       PayloadType = playloadType;

       // build the header bistream
       _header = new byte[HeaderSize];

       // fill the header array of byte with RTP header fields
       _header[0] = (byte)((Version << 6) | (Padding << 5) | (Extension << 4) | CC);
       _header[1] = (byte)((Marker << 7) | (int)PayloadType);
       _header[2] = (byte)(SequenceNumber >> 8);
       _header[3] = (byte)(SequenceNumber);
       for (int i = 0; i < 4; i++)
       {
         _header[7 - i] = (byte)(Timestamp >> (8 * i));
       }
       for (int i = 0; i < 4; i++)
       {
         _header[11 - i] = (byte)(SSRC >> (8 * i));
       }

       // fill the payload bitstream
       _payload = new byte[dataSize];
       _payloadSize = dataSize;

       // fill payload array of byte from data (given in parameter of the constructor)
       Array.Copy(data, 0, _payload, 0, dataSize);
     }

     /// <summary>
     /// RTP(RFC3550)協議數據包
     /// </summary>
     /// <param name="playloadType">數據報文有效載荷類型</param>
     /// <param name="sequenceNumber">數據報文序列號值</param>
     /// <param name="timestamp">數據報文采樣時刻</param>
     /// <param name="frame">圖片</param>
     public RtpPacket(
       RtpPayloadType playloadType,
       int sequenceNumber,
       long timestamp,
       Image frame)
     {
       // fill changing header fields
       SequenceNumber = sequenceNumber;
       Timestamp = timestamp;
       PayloadType = playloadType;

       // build the header bistream
       _header = new byte[HeaderSize];

       // fill the header array of byte with RTP header fields
       _header[0] = (byte)((Version << 6) | (Padding << 5) | (Extension << 4) | CC);
       _header[1] = (byte)((Marker << 7) | (int)PayloadType);
       _header[2] = (byte)(SequenceNumber >> 8);
       _header[3] = (byte)(SequenceNumber);
       for (int i = 0; i < 4; i++)
       {
         _header[7 - i] = (byte)(Timestamp >> (8 * i));
       }
       for (int i = 0; i < 4; i++)
       {
         _header[11 - i] = (byte)(SSRC >> (8 * i));
       }

       // fill the payload bitstream
       using (MemoryStream ms = new MemoryStream())
       {
         frame.Save(ms, ImageFormat.Jpeg);
         _payload = ms.ToArray();
         _payloadSize = _payload.Length;
       }
     }

     /// <summary>
     /// RTP(RFC3550)協議數據包
     /// </summary>
     /// <param name="packet">數據包</param>
     /// <param name="packetSize">數據包長度</param>
     public RtpPacket(byte[] packet, int packetSize)
     {
       //check if total packet size is lower than the header size
       if (packetSize >= HeaderSize)
       {
         //get the header bitsream
         _header = new byte[HeaderSize];
         for (int i = 0; i < HeaderSize; i++)
         {
           _header[i] = packet[i];
         }

         //get the payload bitstream
         _payloadSize = packetSize - HeaderSize;
         _payload = new byte[_payloadSize];
         for (int i = HeaderSize; i < packetSize; i++)
         {
           _payload[i - HeaderSize] = packet[i];
         }

         //interpret the changing fields of the header
         PayloadType = (RtpPayloadType)(_header[1] & 127);
         SequenceNumber = UnsignedInt(_header[3]) + 256 * UnsignedInt(_header[2]);
         Timestamp = UnsignedInt(_header[7])
           + 256 * UnsignedInt(_header[6])
           + 65536 * UnsignedInt(_header[5])
           + 16777216 * UnsignedInt(_header[4]);
       }
     }

     /// <summary>
     /// 將消息轉換成byte數組
     /// </summary>
     /// <returns>消息byte數組</returns>
     public byte[] ToArray()
     {
       byte[] packet = new byte[Length];

       Array.Copy(_header, 0, packet, 0, HeaderSize);
       Array.Copy(_payload, 0, packet, HeaderSize, PayloadSize);

       return packet;
     }

     /// <summary>
     /// 將消息體轉換成圖片
     /// </summary>
     /// <returns>圖片</returns>
     public Bitmap ToBitmap()
     {
       return new Bitmap(new MemoryStream(_payload));
     }

     /// <summary>
     /// 將消息體轉換成圖片
     /// </summary>
     /// <returns>圖片</returns>
     public Image ToImage()
     {
       return Image.FromStream(new MemoryStream(_payload));
     }

     /// <summary>
     /// 將圖片轉換成消息
     /// </summary>
     /// <param name="playloadType">數據報文有效載荷類型</param>
     /// <param name="sequenceNumber">數據報文序列號值</param>
     /// <param name="timestamp">數據報文采樣時刻</param>
     /// <param name="frame">圖片幀</param>
     /// <returns>
     /// RTP消息
     /// </returns>
     public static RtpPacket FromImage(
       RtpPayloadType playloadType,
       int sequenceNumber,
       long timestamp,
       Image frame)
     {
       return new RtpPacket(playloadType, sequenceNumber, timestamp, frame);
     }

     /// <summary>
     /// return the unsigned value of 8-bit integer nb
     /// </summary>
     /// <param name="nb"></param>
     /// <returns></returns>
     private static int UnsignedInt(int nb)
     {
       if (nb >= 0)
         return (nb);
       else
         return (256 + nb);
     }
   }

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
国产免费久久av| 81精品国产乱码久久久久久| 精品美女国产在线| 久久成人人人人精品欧| 国产精品av在线播放| 538国产精品视频一区二区| 欧美高清性猛交| 日韩在线一区二区三区免费视频| 在线观看欧美日韩国产| 国产欧美精品一区二区| 精品亚洲一区二区三区在线观看| 成人国产精品av| 久久人人爽人人爽爽久久| 日韩高清电影免费观看完整版| 日韩av成人在线观看| 国产视频在线观看一区二区| 欧美精品成人91久久久久久久| 亚洲免费视频一区二区| 亚洲精品不卡在线| 久久久久久久影院| 久久久久久国产三级电影| 欧美在线视频一二三| 超碰日本道色综合久久综合| 欧美最顶级的aⅴ艳星| 国产成人精品日本亚洲| 国产欧美精品xxxx另类| 亚洲人成亚洲人成在线观看| 国产精品∨欧美精品v日韩精品| 日韩av在线高清| 亚洲精品网站在线播放gif| 久久久久久中文字幕| 欧美激情视频一区| 91最新在线免费观看| 国产剧情日韩欧美| 91影视免费在线观看| 91亚洲精品在线观看| 日本久久精品视频| 亚洲美女福利视频网站| 91亚洲精华国产精华| 欧美综合在线第二页| 亚洲色图欧美制服丝袜另类第一页| 欧美极品少妇与黑人| 日韩成人在线网站| 亚洲丁香婷深爱综合| 自拍视频国产精品| 高清欧美性猛交xxxx| 中文字幕在线看视频国产欧美在线看完整| 日韩中文字幕在线视频| 中文字幕精品影院| 国产午夜精品全部视频播放| 亚洲精品久久久久国产| 亚洲va码欧洲m码| 国产日韩精品入口| 日韩欧美中文免费| 这里只有精品视频| 日韩欧美综合在线视频| 欧美大尺度电影在线观看| 国产精品丝袜久久久久久高清| 久久99国产综合精品女同| 成人黄色免费网站在线观看| 国产成人综合一区二区三区| www.日韩不卡电影av| 日韩高清欧美高清| xxxx欧美18另类的高清| 国产精品91视频| 成人精品视频99在线观看免费| 日韩精品中文在线观看| 欧美一区二区视频97| 欧美中文字幕在线视频| 欧美电影第一页| 欧美疯狂性受xxxxx另类| 亚洲国产欧美自拍| www.亚洲男人天堂| 欧美亚洲国产日本| 亚洲人成在线免费观看| 欧美另类极品videosbestfree| 国产精品久久久久久av福利软件| 97视频免费在线看| 欧美激情网友自拍| 成人写真视频福利网| 亚洲欧洲一区二区三区久久| 国产啪精品视频网站| 欧美午夜性色大片在线观看| 日本国产精品视频| 亚洲精品一区二区久| 激情亚洲一区二区三区四区| 欧美噜噜久久久xxx| 久精品免费视频| 国产成人福利网站| 国内精品免费午夜毛片| 欧美日韩视频在线| 国产suv精品一区二区| 97视频com| 国产亚洲欧洲高清| 综合激情国产一区| 久久99热精品这里久久精品| 国产不卡精品视男人的天堂| 亚洲色图激情小说| 欧美日韩一区二区精品| 久久精品亚洲94久久精品| 久久伊人精品一区二区三区| 国产va免费精品高清在线| 亚洲精品久久7777777| 欧美黄色片视频| 欧美交受高潮1| 亚洲欧美制服丝袜| 成人免费视频xnxx.com| 成人免费黄色网| 精品视频—区二区三区免费| 久久久精品在线观看| 国产精品久久久久久久久久三级| 国产丝袜高跟一区| 久久99久国产精品黄毛片入口| 亚洲国产成人久久综合一区| 亲子乱一区二区三区电影| 国产精品大陆在线观看| 欧美日韩裸体免费视频| 91社区国产高清| 久久综合伊人77777蜜臀| 成人写真视频福利网| 日韩av123| 色播久久人人爽人人爽人人片视av| 97久久精品国产| 亚洲free嫩bbb| 国精产品一区一区三区有限在线| 色777狠狠综合秋免鲁丝| 国产伦精品免费视频| 国产日韩欧美另类| 国产视频综合在线| 欧美激情视频给我| 国产精品video| 日韩视频欧美视频| 欧美性理论片在线观看片免费| 国产一区二区三区丝袜| 国外成人免费在线播放| 日本精品久久久| 色伦专区97中文字幕| 欧美性猛交丰臀xxxxx网站| 亚洲精品小视频在线观看| 亚洲石原莉奈一区二区在线观看| 色噜噜狠狠狠综合曰曰曰| 国产午夜精品一区理论片飘花| 亚洲a∨日韩av高清在线观看| 国产精品欧美一区二区三区奶水| 亚洲第一国产精品| 亚洲一区亚洲二区亚洲三区| 久久精品成人一区二区三区| 亚洲国产成人精品久久久国产成人一区| 中文字幕日韩在线视频| 按摩亚洲人久久| 国产91精品视频在线观看| 日韩成人av一区| 日韩欧美国产视频| 成人午夜高潮视频| 中文字幕精品一区二区精品| 国产91在线播放九色快色| 亚洲综合日韩在线| 另类专区欧美制服同性| 久久久久久久久久久人体| 欧美亚洲日本网站| 欧美午夜激情视频| 好吊成人免视频| 亚洲精品小视频在线观看| 97香蕉久久夜色精品国产|