Skip to content

Commit

Permalink
TryReadFrameHeader, do not assign a value when nothing was read. (Was…
Browse files Browse the repository at this point in the history
… previously only checked once)
  • Loading branch information
juliusfriedman committed Nov 18, 2023
1 parent 934e11f commit cf417f4
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
8 changes: 5 additions & 3 deletions Rtp/RtpClient.Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,14 @@ internal static void ConfigureRtpThread(System.Threading.Thread thread)//,Common
/// <param name="readFrameByte">Indicates if the frameByte should be read (RFC2326)</param>
/// <param name="frameByte">Indicates the frameByte to read</param>
/// <returns> -1 If the buffer does not contain a RFC2326 / RFC4751 frame at the offset given</returns>
internal static int TryReadFrameHeader(byte[] buffer, int offset, out byte channel, byte? frameByte = BigEndianFrameControl, bool readChannel = true)
internal static int TryReadFrameHeader(byte[] buffer, int offset, out byte? channel, ref int sessionRequired, byte? frameByte = BigEndianFrameControl, bool readChannel = true)
{
//Must be assigned
channel = default;
channel = null;

if (Common.Extensions.Array.ArrayExtensions.IsNullOrEmpty(buffer)) return -1;
//Ensure enough data is present to read the frame header of the Interlaved Binary Data
if (Common.Extensions.Array.ArrayExtensions.IsNullOrEmpty(buffer, out int bufferLength) ||
bufferLength - offset < sessionRequired) return -1;

//https://www.ietf.org/rfc/rfc2326.txt

Expand Down
20 changes: 10 additions & 10 deletions Rtp/RtpClient.Methods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1196,13 +1196,13 @@ public void DisposeAndClearTransportContexts()
/// <param name="buffer">The optional buffer to use.</param>
/// <returns>The amount of bytes the frame data SHOULD have</returns>
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
int ReadApplicationLayerFraming(ref int received, ref int sessionRequired, ref int offset, out byte frameChannel, out RtpClient.TransportContext context, out bool raisedEvent, byte[] buffer = null)
int ReadApplicationLayerFraming(ref int received, ref int sessionRequired, ref int offset, out byte? frameChannel, out RtpClient.TransportContext context, out bool raisedEvent, byte[] buffer = null)
{
//There is no relevant TransportContext assoicated yet.
context = null;

//The channel of the frame - The Framing Method
frameChannel = default(byte);
frameChannel = default;

raisedEvent = false;

Expand Down Expand Up @@ -1257,13 +1257,13 @@ int ReadApplicationLayerFraming(ref int received, ref int sessionRequired, ref i
//TODO if RFC4571 is specified do check here to avoid reading channel.

//The amount of data needed for the frame comes from TryReadFrameHeader
int frameLength = TryReadFrameHeader(buffer, bufferOffset, out frameChannel, BigEndianFrameControl, true);
int frameLength = TryReadFrameHeader(buffer, bufferOffset, out frameChannel, ref sessionRequired, BigEndianFrameControl, true);

//Assign a context if there is a frame of any size
if (frameLength >= 0)
if (frameChannel.HasValue && frameLength >= 0)
{
//Assign the context
context = GetContextByChannels(frameChannel);
context = GetContextByChannels(frameChannel.Value);

//Increase the result by the size of the header
frameLength += sessionRequired;
Expand Down Expand Up @@ -1548,7 +1548,7 @@ int ReadApplicationLayerFraming(ref int received, ref int sessionRequired, ref i
TransportContext relevent = null;

//The channel of the data
byte frameChannel = Common.Binary.Zero;
byte? frameChannel = null;

//Get the length of the given buffer (Should actually use m_Buffer.Count when using our own buffer)
int bufferLength = buffer.Length,
Expand Down Expand Up @@ -1681,10 +1681,10 @@ int ReadApplicationLayerFraming(ref int received, ref int sessionRequired, ref i
//If this is a valid context there must be at least a RtpHeader's worth of data in the buffer.
//If this was a RtcpPacket with only 4 bytes it wouldn't have a ssrc and wouldn't be valid to be sent.
if (incompatible is false &&
(frameChannel.Equals(relevent.DataChannel) &&
(frameChannel.HasValue && frameChannel.Equals(relevent.DataChannel) &&
remainingInBuffer < Rtp.RtpHeader.Length + sessionRequired)
||
(frameChannel.Equals(relevent.ControlChannel) &&
(frameChannel.HasValue && frameChannel.Equals(relevent.ControlChannel) &&
remainingInBuffer < Rtcp.RtcpHeader.Length + sessionRequired))
{
//Remove the context
Expand All @@ -1707,7 +1707,7 @@ int ReadApplicationLayerFraming(ref int received, ref int sessionRequired, ref i
if (incompatible is false)
{
//Determine if the packet is Rtcp by looking at the found channel and the relvent control channel
if (frameChannel.Equals(relevent.ControlChannel) && relevent.InDiscovery is false)
if (frameChannel.HasValue && frameChannel.Equals(relevent.ControlChannel) && relevent.InDiscovery is false)
{
//Rtcp

Expand Down Expand Up @@ -1795,7 +1795,7 @@ int ReadApplicationLayerFraming(ref int received, ref int sessionRequired, ref i
if (jumbo)
{
//If rtp or rtcp is expected check data
if (expectRtp || expectRtcp || frameChannel < TransportContexts.Count)
if (expectRtp || expectRtcp || frameChannel.HasValue)
{
Media.Common.ILoggingExtensions.Log(Logger, InternalId + "ProcessFrameData - Large Packet of " + frameLength + " for Channel " + frameChannel + " remainingInBuffer=" + remainingInBuffer);

Expand Down

0 comments on commit cf417f4

Please sign in to comment.