示例#1
0
        /// <summary>
        /// Gets the byte section of the table.
        /// </summary>
        /// <param name="packet">The starting packet for the table.</param>
        /// <param name="tableId">TableId</param>
        /// <returns>Section of bytes.</returns>
        private byte[] GetEntireTable(TSPacketInfo packet, int tableId)
        {
            Ensure.IsNotNull(Log, packet, "packet is null");

            if (!packet.PayloadUnitStartIndicator || !packet.HasPayload)
            {
                //throw new CustomException("The packet doesn't contain a table section.");
                return(null);
            }

            var pointerField = packet.GetDataByte(0);

            if (pointerField != 0)
            {
                //Ensure.IsZero(Log, pointerField, "pointer field must be zero.");
                return(null);
            }

            // TODO: move to PATSection
            var pTableId = packet.GetDataByte(1);

            if (tableId != pTableId)
            {
                //Ensure.AreEqual(Log, tableId, pTableId, "TableId is invalid.");
                return(null);
            }

            var sectionSyntaxIndicator = BinaryUtils.ReadBool(packet.GetDataByte(2), 0);

            if (!sectionSyntaxIndicator)
            {
                //Ensure.IsTrue(Log, sectionSyntaxIndicator, "sectionSyntaxIndicator is not true.");
                return(null);
            }

            var padding = BinaryUtils.ReadBool(packet.GetDataByte(2), 1);

            if (padding)
            {
                //Ensure.IsFalse(Log, padding, "padding is not false.");
                return(null);
            }

            var reserved = BinaryUtils.GetBits(packet.GetDataByte(2), 2, 2);

            if (reserved != 3)
            {
                //Ensure.AreEqual(Log, 3, reserved, "reserved is not 11b (3).");
                return(null);
            }

            var sectionLen = BinaryUtils.GetBitsEx(packet.Segment, packet.DataBytePos + 2, 4, 12);

            sectionLen = Math.Min(4096, sectionLen) - (Constants.TSPacketSize - packet.DataBytePos - 3); // TODO: Review.

            var sectionData   = new byte[4096];
            var sectionOffset = 0;

            Buffer.BlockCopy(packet.Segment.Array, packet.Segment.Offset, sectionData, sectionOffset, packet.Segment.Count);
            sectionOffset += packet.Segment.Count;

            while (sectionLen > 0)
            {
                var nextPacket = this.GetPacket();
                if (nextPacket == null)
                {
                    continue;
                }

                if (!nextPacket.PayloadUnitStartIndicator &&
                    nextPacket.AdaptationFieldControl == AdaptationType.Payload &&
                    nextPacket.PID == packet.PID)
                {
                    var len = nextPacket.Segment.Count - 4;
                    Buffer.BlockCopy(nextPacket.Segment.Array, nextPacket.Segment.Offset + 4, sectionData, sectionOffset, len);
                    sectionOffset += len;
                    sectionLen    -= len;
                }
            }

            return(sectionData);
        }
示例#2
0
        /// <summary>
        /// Gets the byte section of the table.
        /// </summary>
        /// <param name="packet">The starting packet for the table.</param>
        /// <param name="tableId">TableId</param>
        /// <returns>Section of bytes.</returns>
        private byte[] GetEntireTable(TSPacketInfo packet, int tableId)
        {
            Ensure.IsNotNull(Log, packet, "packet is null");

            if (!packet.PayloadUnitStartIndicator || !packet.HasPayload)
            {
                //throw new CustomException("The packet doesn't contain a table section.");
                return null;
            }

            var pointerField = packet.GetDataByte(0);
            if (pointerField != 0)
            {
                //Ensure.IsZero(Log, pointerField, "pointer field must be zero.");
                return null;
            }

            // TODO: move to PATSection
            var pTableId = packet.GetDataByte(1);
            if (tableId != pTableId)
            {
                //Ensure.AreEqual(Log, tableId, pTableId, "TableId is invalid.");
                return null;
            }

            var sectionSyntaxIndicator = BinaryUtils.ReadBool(packet.GetDataByte(2), 0);
            if (!sectionSyntaxIndicator)
            {
                //Ensure.IsTrue(Log, sectionSyntaxIndicator, "sectionSyntaxIndicator is not true.");
                return null;
            }

            var padding = BinaryUtils.ReadBool(packet.GetDataByte(2), 1);
            if (padding)
            {
                //Ensure.IsFalse(Log, padding, "padding is not false.");
                return null;
            }

            var reserved = BinaryUtils.GetBits(packet.GetDataByte(2), 2, 2);
            if (reserved != 3)
            {
                //Ensure.AreEqual(Log, 3, reserved, "reserved is not 11b (3).");
                return null;
            }

            var sectionLen = BinaryUtils.GetBitsEx(packet.Segment, packet.DataBytePos + 2, 4, 12);
            sectionLen = Math.Min(4096, sectionLen) - (Constants.TSPacketSize - packet.DataBytePos - 3); // TODO: Review.

            var sectionData = new byte[4096];
            var sectionOffset = 0;

            Buffer.BlockCopy(packet.Segment.Array, packet.Segment.Offset, sectionData, sectionOffset, packet.Segment.Count);
            sectionOffset += packet.Segment.Count;

            while (sectionLen > 0)
            {
                var nextPacket = this.GetPacket();
                if (nextPacket == null)
                {
                    continue;
                }

                if (!nextPacket.PayloadUnitStartIndicator &&
                    nextPacket.AdaptationFieldControl == AdaptationType.Payload &&
                    nextPacket.PID == packet.PID)
                {
                    var len = nextPacket.Segment.Count - 4;
                    Buffer.BlockCopy(nextPacket.Segment.Array, nextPacket.Segment.Offset + 4, sectionData, sectionOffset, len);
                    sectionOffset += len;
                    sectionLen -= len;
                }
            }

            return sectionData;
        }