-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Attempt to implement rtcp receiver reports; Attempt to implement fec;…
… minor improvements here and there
- Loading branch information
Showing
18 changed files
with
304 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
using System; | ||
using System.Buffers.Binary; | ||
|
||
namespace DSharpPlus.VoiceLink.Rtp | ||
{ | ||
public readonly record struct RtcpHeader | ||
{ | ||
/// <summary> | ||
/// Gets the version of the RTCP header. | ||
/// </summary> | ||
public int Version { get; init; } | ||
|
||
/// <summary> | ||
/// Gets whether the RTCP header has padding. | ||
/// </summary> | ||
public int Padding { get; init; } | ||
|
||
/// <summary> | ||
/// Gets the report count of the RTCP header. | ||
/// </summary> | ||
public int ReportCount { get; init; } | ||
|
||
/// <summary> | ||
/// Gets the packet type of the RTCP header. | ||
/// </summary> | ||
public int PacketType { get; init; } | ||
|
||
/// <summary> | ||
/// Gets the length of the RTCP header. | ||
/// </summary> | ||
public int Length { get; init; } | ||
|
||
/// <summary> | ||
/// Gets the SSRC of the RTCP header. | ||
/// </summary> | ||
public uint Ssrc { get; init; } | ||
|
||
public RtcpHeader(ReadOnlySpan<byte> data) | ||
{ | ||
if (data.Length < 8) | ||
{ | ||
throw new ArgumentException("The source buffer must have a minimum of 8 bytes for it to be a RTCP header.", nameof(data)); | ||
} | ||
else if (data[1] != 201) | ||
{ | ||
throw new ArgumentException("The source buffer must contain a RTCP receiver report.", nameof(data)); | ||
} | ||
|
||
Version = data[0] >> 6; | ||
Padding = (data[0] >> 5) & 0b00000001; | ||
ReportCount = data[0] & 0b00011111; | ||
PacketType = data[1]; | ||
Length = BinaryPrimitives.ReadUInt16BigEndian(data[2..4]); | ||
Ssrc = BinaryPrimitives.ReadUInt32BigEndian(data[4..8]); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace DSharpPlus.VoiceLink.Rtp | ||
{ | ||
public readonly record struct RtcpReceiverReportPacket | ||
{ | ||
/// <summary> | ||
/// Gets the header of the RTCP packet. | ||
/// </summary> | ||
public RtcpHeader Header { get; init; } | ||
|
||
/// <summary> | ||
/// Gets the report blocks of the RTCP packet. | ||
/// </summary> | ||
public IReadOnlyList<RtcpReportBlock> ReportBlocks { get; init; } | ||
|
||
public RtcpReceiverReportPacket(RtcpHeader header, ReadOnlySpan<byte> data) | ||
{ | ||
List<RtcpReportBlock> reportBlocks = new(header.ReportCount); | ||
for (int i = 0; i < header.ReportCount; i++) | ||
{ | ||
reportBlocks.Add(new RtcpReportBlock(data)); | ||
data = data[24..]; | ||
} | ||
|
||
Header = header; | ||
ReportBlocks = reportBlocks; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
using System; | ||
using System.Buffers.Binary; | ||
|
||
namespace DSharpPlus.VoiceLink.Rtp | ||
{ | ||
public readonly record struct RtcpReportBlock | ||
{ | ||
public ushort SynchronizationSource { get; } | ||
public ushort FractionLost { get; } | ||
public uint CumulativePacketsLost { get; } | ||
public uint ExtendedHighestSequenceNumberReceived { get; } | ||
public uint InterarrivalJitter { get; } | ||
public uint LastSenderReport { get; } | ||
public uint DelaySinceLastSenderReport { get; } | ||
|
||
public RtcpReportBlock(ReadOnlySpan<byte> data) | ||
{ | ||
SynchronizationSource = BinaryPrimitives.ReadUInt16BigEndian(data); | ||
FractionLost = data[2]; | ||
CumulativePacketsLost = BinaryPrimitives.ReadUInt32BigEndian(data[3..]); | ||
ExtendedHighestSequenceNumberReceived = BinaryPrimitives.ReadUInt32BigEndian(data[7..]); | ||
InterarrivalJitter = BinaryPrimitives.ReadUInt32BigEndian(data[11..]); | ||
LastSenderReport = BinaryPrimitives.ReadUInt32BigEndian(data[15..]); | ||
DelaySinceLastSenderReport = BinaryPrimitives.ReadUInt32BigEndian(data[19..]); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
using System; | ||
|
||
namespace DSharpPlus.VoiceLink.Rtp | ||
{ | ||
public static class RtcpUtilities | ||
{ | ||
/// <summary> | ||
/// Determines if the given buffer contains a valid RTCP header. | ||
/// </summary> | ||
/// <param name="source">The data to reference.</param> | ||
/// <returns>Whether the data contains a valid RTCP header.</returns> | ||
public static bool IsRtcpReceiverReport(ReadOnlySpan<byte> source) => source.Length >= 8 && source[1] == 201; | ||
|
||
public static RtcpHeader DecodeHeader(ReadOnlySpan<byte> source) | ||
{ | ||
if (source.Length < 8) | ||
{ | ||
throw new ArgumentException("The source buffer must have a minimum of 8 bytes for it to be a RTCP header.", nameof(source)); | ||
} | ||
else if (source[1] != 201) | ||
{ | ||
throw new ArgumentException("The source buffer must contain a RTCP receiver report.", nameof(source)); | ||
} | ||
|
||
return new RtcpHeader(source); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,29 @@ | ||
namespace DSharpPlus.VoiceLink.Rtp | ||
{ | ||
public readonly struct RtpHeader | ||
public readonly record struct RtpHeader | ||
{ | ||
public byte FirstMetadata { get; init; } | ||
public byte SecondMetadata { get; init; } | ||
public ushort Sequence { get; init; } | ||
public uint Timestamp { get; init; } | ||
public uint Ssrc { get; init; } | ||
|
||
// The version is the first two bits of the first byte. | ||
public byte Version => (byte)(FirstMetadata & 0b11000000); | ||
|
||
// The padding bit is the third bit of the first byte. | ||
public bool HasPadding => (FirstMetadata & 0b00100000) != 0; | ||
|
||
// The extension bit is the fourth bit of the first byte. | ||
public bool HasExtension => (FirstMetadata & 0b00010000) != 0; | ||
|
||
// The CSRC count is the last four bits of the first byte. | ||
public byte CsrcCount => (byte)((FirstMetadata & 0b00001111) >> 4); | ||
|
||
// The marker bit is the first bit of the second byte. | ||
public bool HasMarker => (SecondMetadata & 0b10000000) != 0; | ||
|
||
// The payload type is the last seven bits of the second byte. | ||
public byte PayloadType => (byte)(SecondMetadata & 0b01111111); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.