Exemplo n.º 1
0
        public bool TryParse(ref SequenceReader <byte> reader)
        {
            if (!reader.TryRead(out var value))
            {
                return(false);
            }

            HeaderType = (RtmpChunkHeaderType)(value >> 6);
            StreamId   = value & 0x3f;
            if (StreamId == 0)
            {
                if (!reader.TryRead(out value))
                {
                    return(false);
                }
                StreamId = value + 64;
            }
            else if (StreamId == 1)
            {
                if (!reader.TryReadBigEndian(out short temp))
                {
                    return(false);
                }
                StreamId = temp + 64;
            }

            return(true);
        }
Exemplo n.º 2
0
        public static int GetHeaderLength(RtmpChunkHeaderType headerType)
        {
            switch (headerType)
            {
            case RtmpChunkHeaderType.Type0:
                return(11);

            case RtmpChunkHeaderType.Type1:
                return(7);

            case RtmpChunkHeaderType.Type2:
                return(3);

            default:
                return(0);
            }
        }
Exemplo n.º 3
0
        public void Write(Span <byte> buffer, RtmpChunkHeaderType headerType)
        {
            if (headerType < RtmpChunkHeaderType.Type3)
            {
                buffer.WriteInt24BigEndian(Timestamp > 0xFFFFF ? 0xFFFFFF : Timestamp);
                if (headerType < RtmpChunkHeaderType.Type2)
                {
                    buffer = buffer.Slice(3);
                    buffer.WriteInt24BigEndian(Length);
                    buffer[3] = (byte)MessageType;
                    buffer    = buffer.Slice(4);
                }

                if (headerType < RtmpChunkHeaderType.Type1)
                {
                    buffer = buffer.Slice(3);
                    BinaryPrimitives.WriteInt32LittleEndian(buffer, StreamId);
                }
                if (Timestamp > 0xFFFFFF)
                {
                    BinaryPrimitives.WriteInt32BigEndian(buffer, Timestamp);
                }
            }
        }
Exemplo n.º 4
0
 public RtmpChunkHeader(RtmpChunkHeaderType messageType, int streamId)
 {
     HeaderType = messageType;
     StreamId   = streamId;
 }
Exemplo n.º 5
0
        public bool TryParse(ref SequenceReader <byte> reader, RtmpChunkHeaderType headerType, RtmpMessageHeader?previous)
        {
            var extendedTimestamp = false;

            if (headerType < RtmpChunkHeaderType.Type3)
            {
                if (!reader.TryReadInt24BigEndian(out var time))
                {
                    return(false);
                }
                if (time == 0xFFFFFF)
                {
                    extendedTimestamp = true;
                }
                else if (headerType == RtmpChunkHeaderType.Type0)
                {
                    Timestamp = time;
                }
                else
                {
                    Timestamp = (previous?.Timestamp ?? 0) + time;
                }
            }
            else
            {
                Timestamp = previous?.Timestamp ?? 0;
            }

            if (headerType < RtmpChunkHeaderType.Type2)
            {
                if (!reader.TryReadInt24BigEndian(out var length))
                {
                    return(false);
                }
                Length = length;

                if (!reader.TryRead(out byte id))
                {
                    return(false);
                }
                MessageType = (RtmpMessageType)id;
                if (!Enum.IsDefined(typeof(RtmpMessageType), id))
                {
                    throw new Exception($"Unknown Message type {id}!");
                }
            }
            else
            {
                Length      = previous?.Length ?? 0;
                MessageType = previous?.MessageType ?? RtmpMessageType.None;
            }

            if (headerType == RtmpChunkHeaderType.Type0)
            {
                if (!reader.TryReadLittleEndian(out int streamId))
                {
                    return(false);
                }
                StreamId = streamId;
            }
            else
            {
                StreamId = previous?.StreamId ?? 0;
            }

            if (extendedTimestamp)
            {
                if (!reader.TryReadBigEndian(out int time))
                {
                    return(false);
                }
                Timestamp = time;
            }

            return(true);
        }