예제 #1
0
        private TsPesPacket AllocatePacketWithOwnedBuffer(BufferInstance bufferEntry)
        {
            TsPesPacket tsPesPacket = this._packetPool.Allocate();

            tsPesPacket.BufferEntry = bufferEntry;
            tsPesPacket.Clear();
            return(tsPesPacket);
        }
예제 #2
0
 public TsPesPacket AllocatePesPacket(BufferInstance bufferEntry)
 {
     if (null == bufferEntry)
     {
         throw new ArgumentNullException("bufferEntry");
     }
     bufferEntry.Reference();
     return(this.AllocatePacketWithOwnedBuffer(bufferEntry));
 }
예제 #3
0
        public TsPesPacket AllocatePesPacket(BufferInstance bufferEntry)
        {
            if (null == bufferEntry)
                throw new ArgumentNullException(nameof(bufferEntry));

            bufferEntry.Reference();

            var packet = AllocatePacketWithOwnedBuffer(bufferEntry);

#if DEBUG
            //Debug.WriteLine("Allocate PES Packet({0}) Index {1} Length {2} Time {3} {4}", packet.PacketId, packet.Index, packet.Length, packet.PresentationTimestamp, packet.BufferEntry);
#endif

            return packet;
        }
예제 #4
0
        public void FreePesPacket(TsPesPacket packet)
        {
            if (null == packet)
            {
                throw new ArgumentNullException("packet");
            }
            BufferInstance bufferInstance = packet.BufferEntry;

            if (null != bufferInstance)
            {
                for (int index = packet.Index; index < packet.Index + packet.Length; ++index)
                {
                    packet.Buffer[index] = (byte)204;
                }
                packet.BufferEntry = (BufferInstance)null;
                this._bufferPool.Free(bufferInstance);
            }
            packet.Index  = int.MinValue;
            packet.Length = int.MinValue;
            packet.PresentationTimestamp = TimeSpan.MaxValue;
            packet.DecodeTimestamp       = new TimeSpan?(TimeSpan.MaxValue);
            this._packetPool.Free(packet);
        }
예제 #5
0
        TsPesPacket AllocatePacketWithOwnedBuffer(BufferInstance bufferEntry)
        {
            var packet = _packetPool.Allocate();

            packet.BufferEntry = bufferEntry;

            packet.Clear();

            return packet;
        }
예제 #6
0
        public void Free(BufferInstance bufferInstance)
        {
            //Debug.WriteLine("Free Buffer {0}", bufferInstance);

            if (!bufferInstance.Dereference())
                return;

#if BUFFER_POOL_DEBUG
            for (var i = 0; i < bufferInstance.Buffer.Length; ++i)
                bufferInstance.Buffer[i] = 0xff;
#endif

#if BUFFER_POOL_STATS
            Interlocked.Increment(ref _freeCount);
            Interlocked.Add(ref _actualFreeBytes, bufferInstance.Buffer.Length);
#endif
            var pool = FindPool(bufferInstance.Buffer.Length);

            if (null == pool)
                return; // Oversize buffer relegated to the GC.

            if (pool.Size != bufferInstance.Buffer.Length)
                throw new ArgumentException("Invalid buffer size", nameof(bufferInstance));

            pool.Free((PoolBufferInstance)bufferInstance);
        }
        public void Add(TsPacket packet)
        {
            if (null == _handler)
                return; // Don't bother parsing if we can't do anything with the results.

            if (null != packet)
            {
                if (packet.IsStart)
                {
                    if (0 == _length && _index > _startIndex)
                        Flush();

                    _index = _startIndex;

                    ParseHeader(packet);
                }

                if (null != _bufferEntry)
                {
                    if (_index + packet.BufferLength > _bufferEntry.Buffer.Length)
                    {
                        var requiredLength = _index - _startIndex + packet.BufferLength;

                        var newLength = Math.Max(requiredLength, 512);

                        if (_index < _bufferEntry.Buffer.Length / 2)
                            newLength *= 2;

                        var newBuffer = _bufferPool.Allocate(newLength);

                        Array.Copy(_bufferEntry.Buffer, _startIndex, newBuffer.Buffer, 0, _index - _startIndex);

                        _bufferPool.Free(_bufferEntry);

                        _bufferEntry = newBuffer;

                        _index -= _startIndex;
                        _startIndex = 0;
                    }

                    packet.CopyTo(_bufferEntry.Buffer, _index);
                    _index += packet.BufferLength;
                }
            }

            if (null == packet || _length > 0 && _index - _startIndex == _length)
                Flush();

            if (null == packet && null != _bufferEntry)
            {
                _bufferPool.Free(_bufferEntry);
                _bufferEntry = null;
            }

            if (null == packet && null != _handler)
                _handler(null); // Propagate end-of-stream
        }
        public void Clear()
        {
            _startIndex = 0;
            _index = 0;
            _length = 0;

            if (null != _bufferEntry)
            {
                _bufferPool.Free(_bufferEntry);
                _bufferEntry = null;
            }
        }
        void ParseHeader(TsPacket packet)
        {
            var length = packet.BufferLength;

            if (length < 6)
                return;

            var i0 = packet.BufferOffset;
            var i = i0;

            var buffer = packet.Buffer;

            var packet_start_code_prefix = ((uint)buffer[i] << 16) | ((uint)buffer[i + 1] << 8) | buffer[i + 2];
            i += 3;

            if (1 != packet_start_code_prefix)
                return;

            _streamId = buffer[i++];

            var packet_length = ((uint)buffer[i] << 8) | buffer[i + 1];
            i += 2;

            var bufferLength = DefaultPacketSize;

            if (packet_length > 0)
            {
                _length = packet_length + (uint)(i - i0);
                bufferLength = (int)_length;
            }
            else
                _length = 0;

            // If we still have a buffer, make sure the size is reasonable.
            if (null != _bufferEntry && _bufferEntry.Buffer.Length - _startIndex < bufferLength)
            {
                _bufferPool.Free(_bufferEntry);
                _bufferEntry = null;
            }

            if (null == _bufferEntry)
            {
                _bufferEntry = _bufferPool.Allocate(bufferLength);
                _startIndex = 0;
            }

            _index = _startIndex;
        }