Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multicast, improvements, bug fixes #64

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 31 additions & 3 deletions RtspClientSharp.UnitTests/Rtcp/RtcpReceiverReportsProviderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ public void GetReportPackets_FakeDataProviders_ResetStateOfRtpStatisticsProvider
var rtcpReportsProvider = new RtcpReceiverReportsProvider(rtpStatisticsProviderMock.Object,
rtcpSenderStatisticsProviderFake.Object, 1);

rtcpReportsProvider.GetReportPackets().ToList();
rtcpReportsProvider.GetReportSdesPackets().ToList();

rtcpReportsProvider.GetReportByePackets().ToList();

rtpStatisticsProviderMock.Verify(x => x.ResetState());
}
Expand All @@ -45,7 +47,7 @@ public void GetReportPackets_TestDataProviders_ReturnsPacketWithValidReceiverRep
var rtcpReportsProvider = new RtcpReceiverReportsProvider(rtpStatisticsProviderFake.Object,
rtcpSenderStatisticsProviderFake.Object, 1112234);

IReadOnlyList<RtcpPacket> packets = rtcpReportsProvider.GetReportPackets().ToList();
IReadOnlyList<RtcpPacket> packets = rtcpReportsProvider.GetReportSdesPackets().ToList();

var receiverReportPacket = (RtcpReceiverReportPacket) packets.First(p => p is RtcpReceiverReportPacket);
Assert.IsFalse(receiverReportPacket.PaddingFlag);
Expand All @@ -60,6 +62,16 @@ public void GetReportPackets_TestDataProviders_ReturnsPacketWithValidReceiverRep
Assert.AreEqual(2 << 16 | 10u, receiverReportPacket.Reports[0].ExtHighestSequenceNumberReceived);
Assert.AreEqual(1234u, receiverReportPacket.Reports[0].LastNtpTimeSenderReportReceived);
Assert.AreEqual(0u, receiverReportPacket.Reports[0].DelaySinceLastTimeSenderReportReceived);

packets = rtcpReportsProvider.GetReportByePackets().ToList();

var receiverReportByePacket = (RtcpByePacket)packets.First(p => p is RtcpByePacket);
Assert.IsFalse(receiverReportByePacket.PaddingFlag);
Assert.AreNotEqual(0, receiverReportByePacket.SourceCount);
Assert.AreEqual(203, receiverReportByePacket.PayloadType);
Assert.AreNotEqual(0, receiverReportByePacket.DwordLength);
Assert.AreNotEqual(0, receiverReportByePacket.Length);
Assert.AreEqual(1112234u, receiverReportByePacket.SyncSourcesIds.First());
}

[TestMethod]
Expand All @@ -71,7 +83,7 @@ public void GetReportPackets_TestDataProviders_ReturnsPacketWithValidSdesReport(
var rtcpReportsProvider = new RtcpReceiverReportsProvider(rtpStatisticsProviderFake.Object,
rtcpSenderStatisticsProviderFake.Object, 1112234);

IReadOnlyList<RtcpPacket> packets = rtcpReportsProvider.GetReportPackets().ToList();
IReadOnlyList<RtcpPacket> packets = rtcpReportsProvider.GetReportSdesPackets().ToList();

var sdesReportPacket = (RtcpSdesReportPacket) packets.First(p => p is RtcpSdesReportPacket);

Expand All @@ -80,5 +92,21 @@ public void GetReportPackets_TestDataProviders_ReturnsPacketWithValidSdesReport(
var nameItem = (RtcpSdesNameItem) sdesReportPacket.Chunks[0].Items.First(i => i is RtcpSdesNameItem);
Assert.IsNotNull(nameItem.DomainName);
}

[TestMethod]
public void GetReportPackets_TestDataProviders_ReturnsPacketWithByeReport()
{
var rtpStatisticsProviderFake = new Mock<IRtpStatisticsProvider>();
var rtcpSenderStatisticsProviderFake = new Mock<IRtcpSenderStatisticsProvider>();

var rtcpReportsProvider = new RtcpReceiverReportsProvider(rtpStatisticsProviderFake.Object,
rtcpSenderStatisticsProviderFake.Object, 1112234);

IReadOnlyList<RtcpPacket> packets = rtcpReportsProvider.GetReportByePackets().ToList();

var byePacket = (RtcpByePacket)packets.First(p => p is RtcpByePacket);

Assert.AreEqual(1112234u, byePacket.SyncSourcesIds.First());
}
}
}
2 changes: 2 additions & 0 deletions RtspClientSharp.UnitTests/Rtsp/RtspTransportClientEmulator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ class RtspTransportClientEmulator : IRtspTransportClient

public EndPoint RemoteEndPoint => new IPEndPoint(IPAddress.Loopback, 11080);

public EndPoint LocalEndPoint => new IPEndPoint(IPAddress.Loopback, 11080);

public virtual Task ConnectAsync(CancellationToken token)
{
return Task.CompletedTask;
Expand Down
1 change: 1 addition & 0 deletions RtspClientSharp.UnitTests/Rtsp/RtspTransportClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ private class RtspTransportClientFake : RtspTransportClient
private MemoryStream _responseStream;

public override EndPoint RemoteEndPoint => new IPEndPoint(0, 0);
public override EndPoint LocalEndPoint => new IPEndPoint(0, 0);

public RtspTransportClientFake(ConnectionParameters connectionParameters,
Func<string, string> responseProvider)
Expand Down
33 changes: 32 additions & 1 deletion RtspClientSharp/Rtcp/RtcpByePacket.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,31 @@
using System;
using System.Collections.Generic;
using System.IO;
using RtspClientSharp.Rtsp;
using RtspClientSharp.Utils;

namespace RtspClientSharp.Rtcp
{
class RtcpByePacket : RtcpPacket
class RtcpByePacket : RtcpPacket, ISerializablePacket
{
private readonly List<uint> _syncSourcesIds = new List<uint>();

public IEnumerable<uint> SyncSourcesIds => _syncSourcesIds;

public RtcpByePacket()
{
PaddingFlag = false;
PayloadType = 203;
}

public RtcpByePacket(uint syncSourceId): this()
{
_syncSourcesIds.Add(syncSourceId);
SourceCount = 1;
DwordLength = 1;
Length = (DwordLength + 1) * 4;
}

protected override void FillFromByteSegment(ArraySegment<byte> byteSegment)
{
int offset = byteSegment.Offset;
Expand All @@ -23,5 +39,20 @@ protected override void FillFromByteSegment(ArraySegment<byte> byteSegment)
_syncSourcesIds.Add(ssrc);
}
}

public new void Serialize(Stream stream)
{
base.Serialize(stream);

if (_syncSourcesIds.Count > 0)
{
stream.WriteByte((byte)(_syncSourcesIds[0] >> 24));
stream.WriteByte((byte)(_syncSourcesIds[0] >> 16));
stream.WriteByte((byte)(_syncSourcesIds[0] >> 8));
stream.WriteByte((byte)_syncSourcesIds[0]);
}
else
throw new RtspClientException("Can't make RTCP packet without Identifier");
}
}
}
13 changes: 12 additions & 1 deletion RtspClientSharp/Rtcp/RtcpReceiverReportsProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public RtcpReceiverReportsProvider(IRtpStatisticsProvider rtpStatisticsProvider,
_machineName = Environment.MachineName;
}

public IEnumerable<RtcpPacket> GetReportPackets()
public IEnumerable<RtcpPacket> GetReportSdesPackets()
{
RtcpReceiverReportPacket receiverReport = CreateReceiverReport();

Expand All @@ -34,6 +34,17 @@ public IEnumerable<RtcpPacket> GetReportPackets()
yield return sdesReport;
}

public IEnumerable<RtcpPacket> GetReportByePackets()
{
RtcpReceiverReportPacket receiverReport = CreateReceiverReport();

yield return receiverReport;

RtcpByePacket byeReport = new RtcpByePacket(_senderSyncSourceId);

yield return byeReport;
}

private RtcpReceiverReportPacket CreateReceiverReport()
{
int fractionLost;
Expand Down
2 changes: 1 addition & 1 deletion RtspClientSharp/Rtcp/RtcpSdesNameItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public override void Serialize(Stream stream)
byte[] domainNameBytes = Encoding.ASCII.GetBytes(DomainName);

stream.WriteByte(1);
stream.WriteByte((byte) (domainByteLength + 1));
stream.WriteByte((byte) domainByteLength);
stream.Write(domainNameBytes, 0, domainByteLength);
stream.WriteByte(0);
}
Expand Down
13 changes: 3 additions & 10 deletions RtspClientSharp/Rtcp/RtcpSdesReportPacket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,11 @@ public RtcpSdesReportPacket(IReadOnlyList<RtcpSdesChunk> chunks)

SourceCount = chunks.Count;
PayloadType = 202;
PaddingFlag = false; // this is different padding, see https://www.ietf.org/rfc/rfc3550.txt page 46

int length = chunks.Sum(chunk => chunk.SerializedLength);

int fraction = length % 4;

if (fraction == 0)
PaddingFlag = false;
else
{
PaddingFlag = true;
_paddingByteCount = 4 - fraction;
}
_paddingByteCount = 4 - length % 4;

DwordLength = (length + 3) / 4;
Length = (DwordLength + 1) * 4;
Expand All @@ -49,7 +42,7 @@ protected override void FillFromByteSegment(ArraySegment<byte> byteSegment)
chunk.Serialize(stream);
}

if (PaddingFlag)
if ((_paddingByteCount > 0) && (_paddingByteCount < 4))
stream.Write(PaddingBytes, 0, _paddingByteCount);
}
}
Expand Down
3 changes: 2 additions & 1 deletion RtspClientSharp/RtpTransportProtocol.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
public enum RtpTransportProtocol
{
TCP,
UDP
UDP,
MULTICAST
}
}
1 change: 1 addition & 0 deletions RtspClientSharp/Rtsp/IRtspTransportClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace RtspClientSharp.Rtsp
internal interface IRtspTransportClient : IDisposable
{
EndPoint RemoteEndPoint { get; }
EndPoint LocalEndPoint { get; }

Task ConnectAsync(CancellationToken token);

Expand Down
Loading