private void TryWriteSource(Span <byte> bytes, int?sourceIdentifier, int index)
 {
     if (sourceIdentifier.HasValue)
     {
         NetworkBitConverter.WriteBytes(bytes.Slice(index * IdentifierLength, IdentifierLength), sourceIdentifier.Value);
     }
 }
        public bool MoveNext()
        {
            currentSourceIdentifier = null;

            if (remainsOfBytes.IsEmpty)
            {
                return(false);
            }

            var identifierBytes = remainsOfBytes.Span.Slice(0, IdentifierLength);

            currentSourceIdentifier = NetworkBitConverter.ToInt32(identifierBytes);
            remainsOfBytes          = remainsOfBytes.Slice(IdentifierLength);
            return(true);
        }
        public static bool TryParse(ReadOnlyMemory <byte> bytes, out RtpMessage message)
        {
            message = new RtpMessage();

            if (bytes.Length < MinLength)
            {
                return(false);
            }

            var first2Bytes = bytes.Span.Slice(0, 2);
            var version     = first2Bytes[0] >> 6;

            if (version != CurrentVersion)
            {
                return(false);
            }

            var isPadded        = Convert.ToBoolean((first2Bytes[0] >> 5) & 1);
            var paddedByteCount = 0;

            if (isPadded)
            {
                paddedByteCount = bytes.Span[bytes.Length - 1];
            }

            if (paddedByteCount > bytes.Length - MinLength)
            {
                return(false);
            }

            var notPaddedBytes     = bytes.Slice(0, bytes.Length - paddedByteCount);
            var remainsOfBytes     = notPaddedBytes.Slice(MinLength);
            var hasHeaderExtension = Convert.ToBoolean((first2Bytes[0] >> 4) & 1);
            var CSRCCount          = first2Bytes[0] & 15;
            var CSRCLength         = CSRCCount * IdentifierLength;

            if (remainsOfBytes.Length < CSRCLength)
            {
                return(false);
            }

            var CSRCBytes        = remainsOfBytes.Slice(0, CSRCLength);
            var payload          = remainsOfBytes.Slice(CSRCLength);
            var sources          = new SourceIdentifiers(CSRCBytes, CSRCCount);
            var isMarker         = Convert.ToBoolean(first2Bytes[1] >> 7);
            var payloadType      = first2Bytes[1] & 127;
            var sequenceNumber   = NetworkBitConverter.ToUInt16(bytes.Span.Slice(2, 2));
            var timestamp        = NetworkBitConverter.ToInt32(bytes.Span.Slice(4, 4));
            var sourceIdentifier = NetworkBitConverter.ToInt32(bytes.Span.Slice(8, 4));

            message = new RtpMessage(version,
                                     hasHeaderExtension,
                                     isMarker,
                                     payloadType,
                                     sequenceNumber,
                                     timestamp,
                                     sourceIdentifier,
                                     sources,
                                     payload);
            return(true);
        }
 private Span <byte> WriteSourceIdentifier(Span <byte> bytes)
 {
     NetworkBitConverter.WriteBytes(bytes.Slice(0, 4), state.sourceIdentifier);
     return(bytes.Slice(4));
 }
 private Span <byte> WriteTimestamp(Span <byte> bytes)
 {
     NetworkBitConverter.WriteBytes(bytes.Slice(0, 4), state.timestamp);
     return(bytes.Slice(4));
 }
 private Span <byte> WriteSequenceNumber(Span <byte> bytes)
 {
     NetworkBitConverter.WriteBytes(bytes.Slice(0, 2), state.sequenceNumber);
     return(bytes.Slice(2));
 }