Пример #1
0
 protected void EnsureBufferSpace(int length)
 {
     if (null == this._packet)
     {
         this._index      = 0;
         this._startIndex = 0;
         this._packet     = (TsPesPacket)null;
         this._packet     = this.CreatePacket(length);
     }
     else
     {
         if (this._index + length <= this._packet.Buffer.Length)
         {
             return;
         }
         TsPesPacket packet = this.CreatePacket(length);
         if (this._index > this._startIndex)
         {
             this._index -= this._startIndex;
             Array.Copy((Array)this._packet.Buffer, this._startIndex, (Array)packet.Buffer, 0, this._index);
         }
         else
         {
             this._index = 0;
         }
         this._startIndex    = 0;
         this._packet.Length = 0;
         this._pesPacketPool.FreePesPacket(this._packet);
         this._packet = packet;
     }
 }
Пример #2
0
 public override void PacketHandler(TsPesPacket packet)
 {
     base.PacketHandler(packet);
     if (null == packet)
     {
         if (null != this.Parser)
         {
             this.Parser.FlushBuffers();
         }
         if (null == this.NextHandler)
         {
             return;
         }
         this.NextHandler((TsPesPacket)null);
     }
     else if (null != this.Parser)
     {
         this.Parser.Position = new TimeSpan?(packet.PresentationTimestamp);
         this.Parser.ProcessData(packet.Buffer, packet.Index, packet.Length);
         this._pesPacketPool.FreePesPacket(packet);
     }
     else if (packet.Length < this._minimumPacketSize)
     {
         this._pesPacketPool.FreePesPacket(packet);
     }
     else
     {
         if (!this._isConfigured && this._frameHeader.Parse(packet.Buffer, packet.Index, packet.Length, true))
         {
             this._isConfigured = true;
             this.AudioConfigurator.Configure(this._frameHeader);
         }
         this.NextHandler(packet);
     }
 }
Пример #3
0
        public void Dispose()
        {
            if (0 != Interlocked.Exchange(ref this._isDisposed, 1))
            {
                return;
            }
            bool   lockTaken = false;
            object obj       = null;

            try
            {
                Monitor.Enter(obj = this._packetsLock, ref lockTaken);
                while (this._packets.Count > 0)
                {
                    TsPesPacket tsPesPacket = this._packets.Dequeue();
                    if (null != tsPesPacket)
                    {
                        this._freePesPacket(tsPesPacket);
                    }
                }
            }
            finally
            {
                if (lockTaken)
                {
                    Monitor.Exit(obj);
                }
            }
            this._freePesPacket    = (Action <TsPesPacket>)null;
            this._bufferingManager = (IBufferingManager)null;
        }
Пример #4
0
        private TsPesPacket CreatePacket(int length)
        {
            TsPesPacket tsPesPacket = this._pesPacketPool.AllocatePesPacket(length);

            tsPesPacket.Length = tsPesPacket.Buffer.Length;
            return(tsPesPacket);
        }
Пример #5
0
        public TsPesPacket CopyPesPacket(TsPesPacket packet, int index, int length)
        {
            if (packet == null)
                throw new ArgumentNullException(nameof(packet));

            if (index < 0 || index < packet.Index)
                throw new ArgumentOutOfRangeException(nameof(index));

            if (length < 0 || index + length > packet.Index + packet.Length)
                throw new ArgumentOutOfRangeException(nameof(length));

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

            Debug.Assert(packet.Index >= 0);
            Debug.Assert(packet.Index + packet.Length <= packet.Buffer.Length);

            var clone = AllocatePesPacket(packet.BufferEntry);

            clone.Index = index;
            clone.Length = length;

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

            return clone;
        }
Пример #6
0
        private TsPesPacket AllocatePacketWithOwnedBuffer(BufferInstance bufferEntry)
        {
            TsPesPacket tsPesPacket = this._packetPool.Allocate();

            tsPesPacket.BufferEntry = bufferEntry;
            tsPesPacket.Clear();
            return(tsPesPacket);
        }
Пример #7
0
 private void FreeBuffer()
 {
     if (null != this._packet)
     {
         TsPesPacket packet = this._packet;
         this._packet  = (TsPesPacket)null;
         packet.Length = 0;
         this._pesPacketPool.FreePesPacket(packet);
     }
     this._startIndex = 0;
     this._index      = 0;
 }
Пример #8
0
        public override int Read(byte[] buffer, int offset, int count)
        {
            TsPesPacket packet = this.Packet;

            count = Math.Min(count, packet.Length - this._location);
            if (count < 1)
            {
                return(0);
            }
            Array.Copy((Array)packet.Buffer, packet.Index + this._location, (Array)buffer, offset, count);
            this._location += count;
            return(count);
        }
Пример #9
0
        public bool DiscardPacketsBefore(TimeSpan value)
        {
            bool   flag1     = false;
            bool   lockTaken = false;
            object obj       = null;
            bool   flag2;

            try
            {
                Monitor.Enter(obj = this._packetsLock, ref lockTaken);
                while (this._packets.Count > 0)
                {
                    TsPesPacket packet = this._packets.Peek();
                    if (!(packet.PresentationTimestamp >= value))
                    {
                        this._packets.Dequeue();
                        this.FreeSample(packet);
                        flag1 = true;
                    }
                    else
                    {
                        break;
                    }
                }
                flag2 = !this._bufferingManager.IsBuffering;
            }
            finally
            {
                if (lockTaken)
                {
                    Monitor.Exit(obj);
                }
            }
            if (flag2)
            {
                this._bufferingManager.ReportExhaustion();
            }
            else
            {
                this._bufferingManager.Refresh();
            }
            return(flag1);
        }
Пример #10
0
        public override TimeSpan?GetDuration(TsPesPacket packet)
        {
            if (packet.Duration.HasValue)
            {
                return(packet.Duration);
            }
            TimeSpan timeSpan = TimeSpan.Zero;
            int      length   = packet.Length;
            int      num1     = packet.Index + length;
            int      num2     = 0;
            int      index    = packet.Index;

            while (index < num1)
            {
                int num3;
                if (this._frameHeader.Parse(packet.Buffer, index, length, false))
                {
                    timeSpan += this._frameHeader.Duration;
                    if (this._frameHeader.HeaderOffset > 0)
                    {
                        Debug.WriteLine("AudioStreamHandler.GetDuration() skipping {0} bytes before frame", (object)this._frameHeader.HeaderOffset);
                    }
                    num3 = this._frameHeader.HeaderOffset + this._frameHeader.FrameLength;
                    num2 = 0;
                }
                else if (length > this._frameHeader.HeaderOffset + this._minimumPacketSize)
                {
                    num3  = this._frameHeader.HeaderOffset + 1;
                    num2 += num3;
                }
                else
                {
                    Debug.WriteLine("AudioStreamHandler.GetDuration() unable to find frame, skipping {0} bytes", (object)(length + num2));
                    break;
                }
                index  += num3;
                length -= num3;
            }
            packet.Duration = new TimeSpan?(timeSpan);
            return(new TimeSpan?(timeSpan));
        }
Пример #11
0
 public override void PacketHandler(TsPesPacket packet)
 {
     base.PacketHandler(packet);
     if (null == packet)
     {
         this._nextHandler((TsPesPacket)null);
     }
     else if (packet.Length < 1)
     {
         this._pesPacketPool.FreePesPacket(packet);
     }
     else
     {
         if (!this._foundframe)
         {
             this._configurator.Configure();
             this._foundframe = true;
         }
         this._nextHandler(packet);
     }
 }
Пример #12
0
 private void AdjustTimestamps(TimeSpan offset)
 {
     foreach (TsTimestamp.PacketsState packetsState in this._packetsStates)
     {
         foreach (TsPesPacket tsPesPacket1 in (IEnumerable <TsPesPacket>)packetsState.Packets)
         {
             if (null != tsPesPacket1)
             {
                 tsPesPacket1.PresentationTimestamp -= offset;
                 if (tsPesPacket1.DecodeTimestamp.HasValue)
                 {
                     TsPesPacket tsPesPacket2 = tsPesPacket1;
                     TimeSpan?   nullable1    = tsPesPacket2.DecodeTimestamp;
                     TimeSpan    timeSpan     = offset;
                     TimeSpan?   nullable2    = nullable1.HasValue ? new TimeSpan?(nullable1.GetValueOrDefault() - timeSpan) : new TimeSpan?();
                     tsPesPacket2.DecodeTimestamp = nullable2;
                 }
             }
         }
     }
 }
Пример #13
0
        public TsPesPacket CopyPesPacket(TsPesPacket packet, int index, int length)
        {
            if (packet == null)
            {
                throw new ArgumentNullException("packet");
            }
            if (index < 0 || index < packet.Index)
            {
                throw new ArgumentOutOfRangeException("index");
            }
            if (length < 0 || index + length > packet.Index + packet.Length)
            {
                throw new ArgumentOutOfRangeException("length");
            }
            Debug.Assert(packet.Index >= 0);
            Debug.Assert(packet.Index + packet.Length <= packet.Buffer.Length);
            TsPesPacket tsPesPacket = this.AllocatePesPacket(packet.BufferEntry);

            tsPesPacket.Index  = index;
            tsPesPacket.Length = length;
            return(tsPesPacket);
        }
Пример #14
0
        protected virtual void SubmitFrame()
        {
            Debug.Assert(this._index >= this._startIndex, "_index less than _startIndex");
            int length = this._index - this._startIndex;

            if (length > 0)
            {
                TsPesPacket tsPesPacket;
                if (this._index + 128 >= this._packet.Buffer.Length)
                {
                    tsPesPacket        = this._packet;
                    this._packet       = (TsPesPacket)null;
                    tsPesPacket.Length = length;
                    tsPesPacket.Index  = this._startIndex;
                }
                else
                {
                    tsPesPacket = this._pesPacketPool.CopyPesPacket(this._packet, this._startIndex, length);
                }
                if (!this.Position.HasValue)
                {
                    this.Position = new TimeSpan?(this.StartPosition);
                }
                tsPesPacket.PresentationTimestamp = this.Position.Value;
                tsPesPacket.Duration = new TimeSpan?(this._frameHeader.Duration);
                AudioParserBase audioParserBase = this;
                // ISSUE: explicit non-virtual call
                TimeSpan?position = __nonvirtual(audioParserBase.Position);
                TimeSpan duration = this._frameHeader.Duration;
                TimeSpan?nullable = position.HasValue ? new TimeSpan?(position.GetValueOrDefault() + duration) : new TimeSpan?();
                // ISSUE: explicit non-virtual call
                __nonvirtual(audioParserBase.Position) = nullable;
                this._submitPacket(tsPesPacket);
                this._hasSeenValidFrames = true;
                this._badBytes           = 0;
            }
            this._startIndex = this._index;
            this.EnsureBufferSpace(128);
        }
Пример #15
0
 public override void PacketHandler(TsPesPacket packet)
 {
     base.PacketHandler(packet);
     if (null == packet)
     {
         this._nextHandler((TsPesPacket)null);
     }
     else if (packet.Length < 1)
     {
         this._pesPacketPool.FreePesPacket(packet);
     }
     else
     {
         if (!this._isConfigured)
         {
             this._parser.Reset();
             this._parser.Parse(packet.Buffer, packet.Index, packet.Length, true);
             this._isConfigured = this._configurator.IsConfigured;
         }
         this._nextHandler(packet);
     }
 }
Пример #16
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);
        }
Пример #17
0
        public bool ProcessPackets()
        {
            if (this._packetsStates.Count <= 0)
            {
                return(false);
            }
            bool flag1 = TsTimestamp.EnableDiscontinutityFilter;

            if (!this._timestampOffset.HasValue)
            {
                TimeSpan timeSpan1 = TimeSpan.MaxValue;
                TimeSpan timeSpan2 = TimeSpan.MaxValue;
                foreach (TsTimestamp.PacketsState packetsState in this._packetsStates)
                {
                    bool flag2 = false;
                    if (packetsState.IsMedia)
                    {
                        foreach (TsPesPacket tsPesPacket in (IEnumerable <TsPesPacket>)packetsState.Packets)
                        {
                            if (null != tsPesPacket)
                            {
                                flag2 = true;
                                if (tsPesPacket.PresentationTimestamp < timeSpan1)
                                {
                                    timeSpan1 = tsPesPacket.PresentationTimestamp;
                                }
                                TimeSpan?nullable  = tsPesPacket.DecodeTimestamp;
                                TimeSpan timeSpan3 = timeSpan2;
                                if ((nullable.HasValue ? (nullable.GetValueOrDefault() < timeSpan3 ? 1 : 0) : 0) != 0 && tsPesPacket.DecodeTimestamp.HasValue)
                                {
                                    timeSpan2 = tsPesPacket.DecodeTimestamp.Value;
                                }
                            }
                        }
                        if (!flag2)
                        {
                            return(false);
                        }
                    }
                }
                TimeSpan timeSpan4 = timeSpan1;
                if (timeSpan2 < timeSpan4)
                {
                    timeSpan4 = timeSpan2;
                }
                this._timestampOffset = new TimeSpan?(timeSpan4 - this.StartPosition);
                Debug.WriteLine("TsTimestamp.ProcessPackets() syncing pts {0} dts {1} target {2} => offset {3}", (object)timeSpan1, (object)timeSpan2, (object)this.StartPosition, (object)this._timestampOffset);
            }
            else if (flag1)
            {
                foreach (TsTimestamp.PacketsState packetsState in this._packetsStates)
                {
                    if (packetsState.Packets.Count > 0 && (packetsState.Duration.HasValue && packetsState.PresentationTimestamp.HasValue))
                    {
                        TsPesPacket tsPesPacket = Enumerable.First <TsPesPacket>((IEnumerable <TsPesPacket>)packetsState.Packets);
                        if (null != tsPesPacket)
                        {
                            TimeSpan timeSpan1 = tsPesPacket.PresentationTimestamp - this._timestampOffset.Value;
                            TimeSpan timeSpan2 = packetsState.PresentationTimestamp.Value + packetsState.Duration.Value;
                            TimeSpan timeSpan3 = timeSpan1 - timeSpan2;
                            if (!(timeSpan3 < TsTimestamp.MaximumError) || !(timeSpan3 > -TsTimestamp.MaximumError))
                            {
                                TimeSpan timeSpan4 = tsPesPacket.PresentationTimestamp - timeSpan2;
                                Debug.WriteLine("TsTimestamp.ProcessPackets() resyncing expected pts {0} actual pts {1} target {2} => offset {3} (was {4})", (object)timeSpan2, (object)timeSpan1, (object)this.StartPosition, (object)timeSpan4, (object)this._timestampOffset);
                                this._timestampOffset = new TimeSpan?(timeSpan4);
                            }
                        }
                    }
                }
            }
            TimeSpan?nullable1 = this._timestampOffset;
            TimeSpan timeSpan  = TimeSpan.Zero;

            if ((!nullable1.HasValue ? 1 : (nullable1.GetValueOrDefault() != timeSpan ? 1 : 0)) != 0)
            {
                this.AdjustTimestamps(this._timestampOffset.Value);
            }
            if (flag1)
            {
                foreach (TsTimestamp.PacketsState packetsState in this._packetsStates)
                {
                    if (packetsState.Packets.Count > 0)
                    {
                        TsPesPacket tsPesPacket = Enumerable.Last <TsPesPacket>((IEnumerable <TsPesPacket>)packetsState.Packets);
                        if (null != tsPesPacket)
                        {
                            packetsState.PresentationTimestamp = new TimeSpan?(tsPesPacket.PresentationTimestamp);
                            packetsState.DecodeTimestamp       = tsPesPacket.DecodeTimestamp;
                            packetsState.Duration = !tsPesPacket.Duration.HasValue ? (null == packetsState.GetDuration ? new TimeSpan?() : packetsState.GetDuration(tsPesPacket)) : tsPesPacket.Duration;
                        }
                    }
                }
            }
            return(true);
        }
Пример #18
0
 public void FreeSample(TsPesPacket packet)
 {
     this._freePesPacket(packet);
 }
Пример #19
0
        public TsPesPacket GetNextSample()
        {
            this.ThrowIfDisposed();
            TsPesPacket tsPesPacket1 = (TsPesPacket)null;

            try
            {
                bool   flag      = false;
                bool   lockTaken = false;
                object obj       = null;
                try
                {
                    Monitor.Enter(obj = this._packetsLock, ref lockTaken);
                    if (!this._isDone && this._bufferingManager.IsBuffering)
                    {
                        return((TsPesPacket)null);
                    }
                    if (this._packets.Count > 0)
                    {
                        tsPesPacket1 = this._packets.Dequeue();
                        if (null != tsPesPacket1)
                        {
                            this._oldest = new TimeSpan?(tsPesPacket1.PresentationTimestamp);
                            if (!this._newest.HasValue)
                            {
                                this._newest = this._oldest;
                            }
                            this._size -= tsPesPacket1.Length;
                        }
                        else
                        {
                            this._eofPacketRead = true;
                        }
                    }
                    else
                    {
                        flag = true;
                    }
                }
                finally
                {
                    if (lockTaken)
                    {
                        Monitor.Exit(obj);
                    }
                }
                if (flag)
                {
                    this._bufferingManager.ReportExhaustion();
                }
                else
                {
                    this._bufferingManager.Refresh();
                }
                if (null == tsPesPacket1)
                {
                    return((TsPesPacket)null);
                }
                TsPesPacket tsPesPacket2 = tsPesPacket1;
                tsPesPacket1 = (TsPesPacket)null;
                return(tsPesPacket2);
            }
            catch (Exception ex)
            {
                Debug.WriteLine("GetNextSample: " + ex.Message);
            }
            finally
            {
                if (null != tsPesPacket1)
                {
                    this._freePesPacket(tsPesPacket1);
                }
                this.ThrowIfDisposed();
            }
            return((TsPesPacket)null);
        }
Пример #20
0
 public virtual TimeSpan?GetDuration(TsPesPacket packet)
 {
     return(packet.Duration);
 }
Пример #21
0
 public virtual void PacketHandler(TsPesPacket packet)
 {
 }
Пример #22
0
 public override void PacketHandler(TsPesPacket packet)
 {
     base.PacketHandler(packet);
     this._nextHandler(packet);
 }
Пример #23
0
 protected void SubmitPacket(TsPesPacket packet)
 {
     this._mediaStream.EnqueuePacket(packet);
 }
Пример #24
0
 public void EnqueuePacket(TsPesPacket packet)
 {
     this._packets.Add(packet);
 }
Пример #25
0
        public void FreePesPacket(TsPesPacket packet)
        {
            if (null == packet)
                throw new ArgumentNullException(nameof(packet));
#if DEBUG
            //Debug.WriteLine("Free PES Packet({0}) Index {1} Length {2} Time {3} {4}", packet.PacketId, packet.Index, packet.Length, packet.PresentationTimestamp, packet.BufferEntry);
#endif

            var buffer = packet.BufferEntry;

            if (null != buffer)
            {
#if DEBUG
                for (var i = packet.Index; i < packet.Index + packet.Length; ++i)
                    packet.Buffer[i] = 0xcc;
#endif
                packet.BufferEntry = null;

                _bufferPool.Free(buffer);
            }

#if DEBUG
            packet.Index = int.MinValue;
            packet.Length = int.MinValue;
            packet.PresentationTimestamp = TimeSpan.MaxValue;
            packet.DecodeTimestamp = TimeSpan.MaxValue;
#endif

            _packetPool.Free(packet);
        }
Пример #26
0
 public void SubmitPacket(TsPesPacket packet)
 {
     this._mediaStream.EnqueuePacket(packet);
 }
Пример #27
0
        private bool SendStreamSample(IStreamSource source, MediaStreamDescription mediaStreamDescription, bool canCallReportBufferingProgress)
        {
            this._taskScheduler.ThrowIfNotOnThread();
            TsPesPacket nextSample = source.GetNextSample();

            if (null == nextSample)
            {
                if (source.IsEof)
                {
                    return(this.SendLastStreamSample(mediaStreamDescription));
                }
                if (canCallReportBufferingProgress)
                {
                    float?bufferingProgress = source.BufferingProgress;
                    if (bufferingProgress.HasValue)
                    {
                        if ((double)Math.Abs(this._bufferingProgress - bufferingProgress.Value) < 0.05)
                        {
                            return(false);
                        }
                        string   format    = "Sample {0} buffering {1:F2}%";
                        object[] objArray1 = new object[2]
                        {
                            (object)mediaStreamDescription.Type,
                            null
                        };
                        object[] objArray2 = objArray1;
                        int      index     = 1;
                        float?   nullable  = bufferingProgress;
                        // ISSUE: variable of a boxed type
                        var local = (ValueType)(nullable.HasValue ? new float?(nullable.GetValueOrDefault() * 100f) : new float?());
                        objArray2[index] = (object)local;
                        object[] objArray3 = objArray1;
                        Debug.WriteLine(format, objArray3);
                        this._bufferingProgress = bufferingProgress.Value;
                        this.ValidateEvent(MediaStreamFsm.MediaEvent.CallingReportSampleCompleted);
                        this.ReportGetSampleProgress((double)bufferingProgress.Value);
                    }
                    else
                    {
                        Debug.WriteLine("Sample {0} not buffering", (object)mediaStreamDescription.Type);
                        nextSample = source.GetNextSample();
                    }
                }
                if (null == nextSample)
                {
                    return(false);
                }
            }
            this._bufferingProgress = -1f;
            try
            {
                this._pesStream.Packet = nextSample;
                MediaStreamSample mediaStreamSample = new MediaStreamSample(mediaStreamDescription, (Stream)this._pesStream, 0L, (long)nextSample.Length, nextSample.PresentationTimestamp.Ticks, (IDictionary <MediaSampleAttributeKeys, string>)TsMediaStreamSource.NoMediaSampleAttributes);
                this.ValidateEvent(MediaStreamFsm.MediaEvent.CallingReportSampleCompleted);
                this.ReportGetSampleCompleted(mediaStreamSample);
            }
            finally
            {
                this._pesStream.Packet = (TsPesPacket)null;
                source.FreeSample(nextSample);
            }
            return(true);
        }