Пример #1
0
        public static bool TryParse(ArraySegment <byte> byteSegment, out RtpPacket rtpPacket)
        {
            rtpPacket = new RtpPacket();

            Debug.Assert(byteSegment.Array != null, "byteSegment.Array != null");

            if (byteSegment.Count < RtpHeaderSize)
            {
                return(false);
            }

            int offset = byteSegment.Offset;

            rtpPacket.ProtocolVersion = byteSegment.Array[offset] >> 6;

            if (rtpPacket.ProtocolVersion != RtpProtocolVersion)
            {
                return(false);
            }

            rtpPacket.PaddingFlag   = (byteSegment.Array[offset] >> 5 & 1) != 0;
            rtpPacket.ExtensionFlag = (byteSegment.Array[offset] >> 4 & 1) != 0;
            rtpPacket.CsrcCount     = byteSegment.Array[offset++] & 0xF;

            rtpPacket.MarkerBit   = byteSegment.Array[offset] >> 7 != 0;
            rtpPacket.PayloadType = byteSegment.Array[offset++] & 0x7F;

            rtpPacket.SeqNumber = (ushort)BigEndianConverter.ReadUInt16(byteSegment.Array, offset);
            offset += 2;

            rtpPacket.Timestamp = BigEndianConverter.ReadUInt32(byteSegment.Array, offset);
            offset += 4;

            //Debug.WriteLine(rtpPacket.Timestamp);

            rtpPacket.SyncSourceId = BigEndianConverter.ReadUInt32(byteSegment.Array, offset);
            offset += 4 + 4 * rtpPacket.CsrcCount;

            if (rtpPacket.ExtensionFlag)
            {
                rtpPacket.ExtensionHeaderId = BigEndianConverter.ReadUInt16(byteSegment.Array, offset);
                offset += 2;

                int extensionHeaderLength = BigEndianConverter.ReadUInt16(byteSegment.Array, offset) * 4;
                offset += 2 + extensionHeaderLength;
            }

            int payloadSize = byteSegment.Offset + byteSegment.Count - offset;

            if (rtpPacket.PaddingFlag)
            {
                int paddingBytes = byteSegment.Array[byteSegment.Offset + byteSegment.Count - 1];
                payloadSize -= paddingBytes;
            }

            rtpPacket.PayloadSegment = new ArraySegment <byte>(byteSegment.Array, offset, payloadSize);
            return(true);
        }
        public void ReadUInt32_TestBuffer_ReturnsCorrectInt()
        {
            uint expected = 0x11 << 24 | 0x22 << 16 | 0x33 << 8 | 0x44;
            var  buffer   = new byte[] { 0x11, 0x22, 0x33, 0x44 };

            uint result = BigEndianConverter.ReadUInt32(buffer, 0);

            Assert.AreEqual(expected, result);
        }
        protected override void FillFromByteSegment(ArraySegment <byte> byteSegment)
        {
            SyncSourceId = BigEndianConverter.ReadUInt32(byteSegment.Array, byteSegment.Offset);

            uint ntpTimestampMw = BigEndianConverter.ReadUInt32(byteSegment.Array, byteSegment.Offset + 4);
            uint ntpTimestampLw = BigEndianConverter.ReadUInt32(byteSegment.Array, byteSegment.Offset + 8);

            NtpTimestamp = (long)ntpTimestampMw << 32 | ntpTimestampLw;
        }
Пример #4
0
        protected override void FillFromByteSegment(ArraySegment <byte> byteSegment)
        {
            int offset = byteSegment.Offset;

            for (int i = 0; i < SourceCount; i++)
            {
                uint ssrc = BigEndianConverter.ReadUInt32(byteSegment.Array, offset);

                offset += 4;

                _syncSourcesIds.Add(ssrc);
            }
        }
Пример #5
0
        public HcaMetadata(byte[] hcaBytes)
        {
            Channels     = 0;
            SampleRate   = 0;
            BlockCount   = 0;
            LoopStart    = 0;
            LoopEnd      = 0;
            Loop_r01     = 0;
            Loop_r02     = 0;
            HasLoopData  = false;
            ValidHcaFile = false;

            if (hcaBytes != null)
            {
                if (BitConverter.ToInt32(hcaBytes, 0) != HCA_SIGNATURE)
                {
                    return;
                }

                ValidHcaFile = true;
                int dataOffset = BigEndianConverter.ReadUInt16(hcaBytes, 6);
                int fmtOffset  = hcaBytes.IndexOfValue(FMT_SIGNATURE, 0, dataOffset, false);
                int loopOffset = hcaBytes.IndexOfValue(LOOP_SIGNATURE, 0, dataOffset, false);

                //Load fmt
                if (hcaBytes.Length > fmtOffset + 16 && fmtOffset != -1)
                {
                    Channels   = hcaBytes[fmtOffset + 4];
                    SampleRate = BigEndianConverter.ReadUInt16(hcaBytes, fmtOffset + 6);
                    BlockCount = BigEndianConverter.ReadInt32(hcaBytes, fmtOffset + 8);
                }

                //Load loop
                if (hcaBytes.Length > loopOffset + 16 && loopOffset != -1)
                {
                    HasLoopData = true;
                    LoopStart   = BigEndianConverter.ReadUInt32(hcaBytes, loopOffset + 4);
                    LoopEnd     = BigEndianConverter.ReadUInt32(hcaBytes, loopOffset + 8);
                    Loop_r01    = BigEndianConverter.ReadUInt16(hcaBytes, loopOffset + 12);
                    Loop_r02    = BigEndianConverter.ReadUInt16(hcaBytes, loopOffset + 14);
                }
            }
        }
Пример #6
0
        public TrackMetadata(byte[] bytes)
        {
            IsValidAudioFile = false;
            Channels         = 0;
            SampleRate       = 0;
            NumSamples       = 0;

            try
            {
                if (BitConverter.ToUInt16(bytes, 0) == ADX_SIGNATURE)
                {
                    //Is ADX
                    IsValidAudioFile = true;
                    Channels         = bytes[7];
                    SampleRate       = BigEndianConverter.ReadUInt32(bytes, 8);
                    NumSamples       = BigEndianConverter.ReadUInt32(bytes, 12);
                }
                else if (BitConverter.ToInt32(bytes, 0) == HCA_SIGNATURE)
                {
                    IsValidAudioFile = true;
                    int dataOffset = BigEndianConverter.ReadUInt16(bytes, 6);
                    int fmtOffset  = bytes.IndexOfValue(FMT_SIGNATURE, 0, dataOffset, false);

                    if (fmtOffset != -1)
                    {
                        Channels   = bytes[fmtOffset + 4];
                        SampleRate = BigEndianConverter.ReadUInt16(bytes, fmtOffset + 6);
                        int blockCount = BigEndianConverter.ReadInt32(bytes, fmtOffset + 8);
                        NumSamples = (uint)(blockCount * 1024);
                    }
                }
                else
                {
                    //DSP, which has no signature... thanks nintendo!
                    NumSamples = BigEndianConverter.ReadUInt32(bytes, 0);
                    SampleRate = BigEndianConverter.ReadUInt32(bytes, 8);
                    Channels   = (byte)BigEndianConverter.ReadUInt16(bytes, 74);

                    if (Channels == 0)
                    {
                        Channels = 1;
                    }
                }
            }
            catch
            {
                IsValidAudioFile = false;
                Channels         = 0;
                SampleRate       = 0;
                NumSamples       = 0;
            }

            if (Channels > 10 || SampleRate < 0 || NumSamples < 0)
            {
                //Values make no sense
                IsValidAudioFile = false;
                Channels         = 0;
                SampleRate       = 0;
                NumSamples       = 0;
            }
        }