使用C#实现RTP数据包传输 参照RFC3550
程序员文章站
2023-12-17 14:54:46
闲暇时折腾ip网络视频监控系统,需要支持视频帧数据包在网络内的传输。未采用h.264或mpeg4等编码压缩方式,直接使用bitmap图片。由于对帧的准确到达要求不好,所以采...
闲暇时折腾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);
}
}