Skip to content

Commit

Permalink
Added a unit test to check the SDP media format order. Tightened up t…
Browse files Browse the repository at this point in the history
…he SDP owner parsing. (#1248)
  • Loading branch information
sipsorcery authored Dec 1, 2024
1 parent 0a5362b commit 82809d2
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 9 deletions.
26 changes: 17 additions & 9 deletions src/net/SDP/SDP.cs
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// Filename: SDP.cs
//
// Description: Session Description Protocol implementation as defined in RFC 2327.
Expand Down Expand Up @@ -234,13 +234,21 @@ public static SDP ParseSDPDescription(string sdpDescription)
break;

case var l when l.StartsWith("o="):
string[] ownerFields = sdpLineTrimmed.Substring(2).Split(' ');
sdp.Username = ownerFields[0];
sdp.SessionId = ownerFields[1];
UInt64.TryParse(ownerFields[2], out sdp.AnnouncementVersion);
sdp.NetworkType = ownerFields[3];
sdp.AddressType = ownerFields[4];
sdp.AddressOrHost = ownerFields[5];
var ownerFields = sdpLineTrimmed.Substring(2).Split(new []{' '}, 6, StringSplitOptions.RemoveEmptyEntries);

if (ownerFields.Length >= 5)
{
sdp.Username = ownerFields[0];
sdp.SessionId = ownerFields[1];
sdp.AnnouncementVersion = UInt64.TryParse(ownerFields[2], out var version) ? version : 0;
sdp.NetworkType = ownerFields[3];
sdp.AddressType = ownerFields[4];
sdp.AddressOrHost = ownerFields.ElementAtOrDefault(5); // Safely handle missing elements
}
else
{
logger.LogWarning($"The SDP message had an invalid SDP line format for 'o=': {sdpLineTrimmed}");
}
break;

case var l when l.StartsWith("s="):
Expand Down Expand Up @@ -779,7 +787,7 @@ public static SDP ParseSDPDescription(string sdpDescription)
}
catch (Exception excp)
{
logger.LogError("Exception ParseSDPDescription. " + excp.Message);
logger.LogError("Exception ParseSDPDescription. " + excp);
throw;
}
}
Expand Down
30 changes: 30 additions & 0 deletions test/unit/net/SDP/SDPUnitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
// BSD 3-Clause "New" or "Revised" License, see included LICENSE.md file.
//-----------------------------------------------------------------------------

using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net;
Expand Down Expand Up @@ -1280,5 +1281,34 @@ public void AnnoucementMediaCheckTest()
Assert.Equal("text/plain", sdp.Media.First().MessageMediaFormat.AcceptTypes[0]);
Assert.Equal("text/x-msrp-heartbeat", sdp.Media.First().MessageMediaFormat.AcceptTypes[1]);
}

/// <summary>
/// The media format negotiation rules specify that the first common format in the offer and answer
/// should be chiosen. This test checks that the media formats are maintained in the order they were added.
/// </summary>
[Fact]
public void Media_Formats_Order_Test()
{
logger.LogDebug("--> " + System.Reflection.MethodBase.GetCurrentMethod().Name);

var sdp = new SDP();

sdp.Media.Add(new SDPMediaAnnouncement()
{
Media = SDPMediaTypesEnum.audio,
MediaFormats = new Dictionary<int, SDPAudioVideoMediaFormat>
{
{ 8, new SDPAudioVideoMediaFormat(SDPMediaTypesEnum.audio, 8, "PCMA", 8000) },
{ 0, new SDPAudioVideoMediaFormat(SDPMediaTypesEnum.audio, 0, "PCMU", 8000) },
{ 101, new SDPAudioVideoMediaFormat(SDPMediaTypesEnum.audio, 101, "telephone-event", 8000) }
}
});

logger.LogDebug(sdp.ToString());

var sdpParsed = SDP.ParseSDPDescription(sdp.ToString());

Assert.Equal(8, sdpParsed.Media.Where(x => x.Media == SDPMediaTypesEnum.audio).First().MediaFormats.First().Key);
}
}
}

0 comments on commit 82809d2

Please sign in to comment.