Skip to content

Commit 82809d2

Browse files
authored
Added a unit test to check the SDP media format order. Tightened up the SDP owner parsing. (sipsorcery-org#1248)
1 parent 0a5362b commit 82809d2

File tree

2 files changed

+47
-9
lines changed

2 files changed

+47
-9
lines changed

src/net/SDP/SDP.cs

100644100755
+17-9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//-----------------------------------------------------------------------------
1+
//-----------------------------------------------------------------------------
22
// Filename: SDP.cs
33
//
44
// Description: Session Description Protocol implementation as defined in RFC 2327.
@@ -234,13 +234,21 @@ public static SDP ParseSDPDescription(string sdpDescription)
234234
break;
235235

236236
case var l when l.StartsWith("o="):
237-
string[] ownerFields = sdpLineTrimmed.Substring(2).Split(' ');
238-
sdp.Username = ownerFields[0];
239-
sdp.SessionId = ownerFields[1];
240-
UInt64.TryParse(ownerFields[2], out sdp.AnnouncementVersion);
241-
sdp.NetworkType = ownerFields[3];
242-
sdp.AddressType = ownerFields[4];
243-
sdp.AddressOrHost = ownerFields[5];
237+
var ownerFields = sdpLineTrimmed.Substring(2).Split(new []{' '}, 6, StringSplitOptions.RemoveEmptyEntries);
238+
239+
if (ownerFields.Length >= 5)
240+
{
241+
sdp.Username = ownerFields[0];
242+
sdp.SessionId = ownerFields[1];
243+
sdp.AnnouncementVersion = UInt64.TryParse(ownerFields[2], out var version) ? version : 0;
244+
sdp.NetworkType = ownerFields[3];
245+
sdp.AddressType = ownerFields[4];
246+
sdp.AddressOrHost = ownerFields.ElementAtOrDefault(5); // Safely handle missing elements
247+
}
248+
else
249+
{
250+
logger.LogWarning($"The SDP message had an invalid SDP line format for 'o=': {sdpLineTrimmed}");
251+
}
244252
break;
245253

246254
case var l when l.StartsWith("s="):
@@ -779,7 +787,7 @@ public static SDP ParseSDPDescription(string sdpDescription)
779787
}
780788
catch (Exception excp)
781789
{
782-
logger.LogError("Exception ParseSDPDescription. " + excp.Message);
790+
logger.LogError("Exception ParseSDPDescription. " + excp);
783791
throw;
784792
}
785793
}

test/unit/net/SDP/SDPUnitTests.cs

+30
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
// BSD 3-Clause "New" or "Revised" License, see included LICENSE.md file.
1010
//-----------------------------------------------------------------------------
1111

12+
using System.Collections.Generic;
1213
using System.Diagnostics;
1314
using System.Linq;
1415
using System.Net;
@@ -1280,5 +1281,34 @@ public void AnnoucementMediaCheckTest()
12801281
Assert.Equal("text/plain", sdp.Media.First().MessageMediaFormat.AcceptTypes[0]);
12811282
Assert.Equal("text/x-msrp-heartbeat", sdp.Media.First().MessageMediaFormat.AcceptTypes[1]);
12821283
}
1284+
1285+
/// <summary>
1286+
/// The media format negotiation rules specify that the first common format in the offer and answer
1287+
/// should be chiosen. This test checks that the media formats are maintained in the order they were added.
1288+
/// </summary>
1289+
[Fact]
1290+
public void Media_Formats_Order_Test()
1291+
{
1292+
logger.LogDebug("--> " + System.Reflection.MethodBase.GetCurrentMethod().Name);
1293+
1294+
var sdp = new SDP();
1295+
1296+
sdp.Media.Add(new SDPMediaAnnouncement()
1297+
{
1298+
Media = SDPMediaTypesEnum.audio,
1299+
MediaFormats = new Dictionary<int, SDPAudioVideoMediaFormat>
1300+
{
1301+
{ 8, new SDPAudioVideoMediaFormat(SDPMediaTypesEnum.audio, 8, "PCMA", 8000) },
1302+
{ 0, new SDPAudioVideoMediaFormat(SDPMediaTypesEnum.audio, 0, "PCMU", 8000) },
1303+
{ 101, new SDPAudioVideoMediaFormat(SDPMediaTypesEnum.audio, 101, "telephone-event", 8000) }
1304+
}
1305+
});
1306+
1307+
logger.LogDebug(sdp.ToString());
1308+
1309+
var sdpParsed = SDP.ParseSDPDescription(sdp.ToString());
1310+
1311+
Assert.Equal(8, sdpParsed.Media.Where(x => x.Media == SDPMediaTypesEnum.audio).First().MediaFormats.First().Key);
1312+
}
12831313
}
12841314
}

0 commit comments

Comments
 (0)