From 8460a654fe499058a23be92f0e4445b58071a6fb Mon Sep 17 00:00:00 2001 From: Julius Friedman Date: Fri, 24 Nov 2023 22:11:28 -0500 Subject: [PATCH] independt framing wip. --- Rtp/RtpClient.Methods.cs | 72 +++++++++++++++++++++------------------- 1 file changed, 37 insertions(+), 35 deletions(-) diff --git a/Rtp/RtpClient.Methods.cs b/Rtp/RtpClient.Methods.cs index 4468c6dd..77f92620 100644 --- a/Rtp/RtpClient.Methods.cs +++ b/Rtp/RtpClient.Methods.cs @@ -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; @@ -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... @@ -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)) { @@ -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;