forked from kthompson/RtpLib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RtcpHeader.cs
102 lines (80 loc) · 2.28 KB
/
RtcpHeader.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net;
namespace RtpLib
{
public class RtcpHeader : ICloneable
{
#region Constructor
public RtcpHeader()
{
this.Version = 2;
this.PacketType = Rtcp.PacketType.Unknown;
}
#endregion
#region Public Methods
public void Parse(Stream stream)
{
byte[] bytes = new byte[4];
if (stream.Read(bytes, 0, bytes.Length) < bytes.Length)
throw new InvalidDataException();
this.Version = (byte)((bytes[0] >> 6) & 0x03);
this.IsPadded = (0 != (bytes[0] & 0x20));
this.ItemCount = (byte)(bytes[0] & 0x1F);
this.PacketType = (Rtcp.PacketType)bytes[1];
this.OctetCount = (ushort)IPAddress.NetworkToHostOrder(BitConverter.ToInt16(bytes, 2));
}
public void ToStream(Stream stream)
{
byte first = (byte)((this.Version << 6) & ~0x03);
if (this.IsPadded)
first |= 0x20;
first |= (byte)(this.ItemCount & 0x1F);
stream.WriteByte(first);
stream.WriteByte((byte)this.PacketType);
stream.Write(BitConverter.GetBytes(IPAddress.HostToNetworkOrder((short)this.OctetCount)), 0, 2);
}
#endregion
#region Properties
public byte Version
{
get;
set;
}
public bool IsPadded
{
get;
set;
}
public byte ItemCount
{
get;
set;
}
public Rtcp.PacketType PacketType
{
get;
set;
}
public ushort OctetCount
{
get;
set;
}
public int ByteCount
{
get { return (ushort)(this.OctetCount * 4); }
set { this.OctetCount = (ushort)(value / 4); }
}
#endregion
#region ICloneableImplementation
public object Clone()
{
return this.MemberwiseClone();
}
#endregion
}
}