Skip to content

Commit

Permalink
independt framing wip.
Browse files Browse the repository at this point in the history
  • Loading branch information
juliusfriedman committed Nov 25, 2023
1 parent 8d6dd3f commit 8460a65
Showing 1 changed file with 37 additions and 35 deletions.
72 changes: 37 additions & 35 deletions Rtp/RtpClient.Methods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1211,49 +1211,51 @@ int ReadApplicationLayerFraming(ref int received, ref int sessionRequired, ref i

if (received <= 0 || sessionRequired < 0 || received < sessionRequired) return -1;

//Look for the frame control octet
int startOfFrame = System.Array.IndexOf(buffer, BigEndianFrameControl, bufferOffset, received);

//If not found everything belongs to the upper layer
if (startOfFrame == -1)
//When there was something to delemit the frames then we can do a quick search for it.
if(sessionRequired >= InterleavedOverhead)
{
//System.Diagnostics.Debug.WriteLine("Interleaving: " + received);
OnOutOfBandData(buffer, bufferOffset, received);
//Look for the frame control octet
int startOfFrame = System.Array.IndexOf(buffer, BigEndianFrameControl, bufferOffset, received);

raisedEvent = true;
//If not found everything belongs to the upper layer
if (startOfFrame == -1)
{
//System.Diagnostics.Debug.WriteLine("Interleaving: " + received);
OnOutOfBandData(buffer, bufferOffset, received);

//Indicate the amount of data consumed.
return received;
}
raisedEvent = true;

// If the start of the frame is not at the beginning of the buffer
if (startOfFrame > bufferOffset)
{
//Determine the amount of data which belongs to the upper layer
int upperLayerData = startOfFrame - bufferOffset;
//Indicate the amount of data consumed.
return received;
}

//System.Diagnostics.Debug.WriteLine("Moved To = " + startOfFrame + " Of = " + received + " - Bytes = " + upperLayerData + " = " + Encoding.ASCII.GetString(m_Buffer, mOffset, startOfFrame - mOffset));
// If the start of the frame is not at the beginning of the buffer
if (startOfFrame > bufferOffset)
{
//Determine the amount of data which belongs to the upper layer
int upperLayerData = startOfFrame - bufferOffset;

OnOutOfBandData(buffer, bufferOffset, upperLayerData);
//System.Diagnostics.Debug.WriteLine("Moved To = " + startOfFrame + " Of = " + received + " - Bytes = " + upperLayerData + " = " + Encoding.ASCII.GetString(m_Buffer, mOffset, startOfFrame - mOffset));

raisedEvent = true;
OnOutOfBandData(buffer, bufferOffset, upperLayerData);

//Indicate length from offset until next possible frame.
//(should always be positive, if somehow -1 is returned this will
//signal a end of buffer to callers)
raisedEvent = true;

//If there is more data related to upperLayerData it will be evented
//in the next run. (See RtspClient ProcessInterleaveData notes)
return upperLayerData;
}
//Indicate length from offset until next possible frame.
//(should always be positive, if somehow -1 is returned this will
//signal a end of buffer to callers)

//If there is not enough data for a frame header return
if (bufferOffset + sessionRequired > bufferLength)
{
return -1;
}
//If there is more data related to upperLayerData it will be evented
//in the next run. (See RtspClient ProcessInterleaveData notes)
return upperLayerData;
}

//TODO if RFC4571 is specified do check here to avoid reading channel.
//If there is not enough data for a frame header return
if (bufferOffset + sessionRequired > bufferLength)
{
return -1;
}
}

//The amount of data needed for the frame comes from TryReadFrameHeader
bool isInterleaved = sessionRequired >= InterleavedOverhead;
Expand Down Expand Up @@ -1646,7 +1648,7 @@ int ReadApplicationLayerFraming(ref int received, ref int sessionRequired, ref i
//TODO handle receiving when no $ and Channel is presenent... e.g. RFC4571
//Would only be 2 then...

int sessionRequired = TransportContexts.Min(tc => tc.MinimumPacketSize);
int sessionRequired = /*TransportContexts.Any() ? TransportContexts.Min(tc => tc.MinimumPacketSize) :*/ InterleavedOverhead;

//Todo, we allow a buffer to be given so we must also check if its changed to null...

Expand Down Expand Up @@ -1841,7 +1843,7 @@ int ReadApplicationLayerFraming(ref int received, ref int sessionRequired, ref i
}

//the context by payload type is null is not discovering the identity check the SSRC.
if (false.Equals(Common.IDisposedExtensions.IsNullOrDisposed(GetContextByPayloadType(common.RtpPayloadType))) /*&& relevent.InDiscovery is false*/)
if (Common.IDisposedExtensions.IsNullOrDisposed(GetContextByPayloadType(common.RtpPayloadType)) is false /*&& relevent.InDiscovery is false*/)
{
using (Rtp.RtpHeader header = new RtpHeader(buffer, offset + sessionRequired))
{
Expand Down Expand Up @@ -2003,7 +2005,7 @@ int ReadApplicationLayerFraming(ref int received, ref int sessionRequired, ref i

//Todo, don't waste allocations on 0
//Parse the data in the buffer
using (Common.MemorySegment memory = hasFrameHeader ? new Common.MemorySegment(buffer, offset + sessionRequired, Common.Binary.Min(ref frameLength, ref remainingInBuffer) - sessionRequired) : new Common.MemorySegment(buffer, offset, remainingInBuffer))
using (Common.MemorySegment memory = hasFrameHeader ? new Common.MemorySegment(buffer, offset + sessionRequired, Common.Binary.Min(frameLength - sessionRequired, remainingInBuffer)) : new Common.MemorySegment(buffer, offset, remainingInBuffer))
{
registerX = memory.Count;

Expand Down

0 comments on commit 8460a65

Please sign in to comment.