Exemplo n.º 1
0
        private void ProcessParameterChange(DataPacket packet)
        {
            _parameterChangePacket = null;

            // try to do a stream header...
            var wasPeek     = false;
            var doFullReset = false;

            if (ProcessStreamHeader(packet))
            {
                packet.Done();
                wasPeek     = true;
                doFullReset = true;
                packet      = _packetProvider.PeekNextPacket();
                if (packet == null)
                {
                    throw new InvalidDataException("Couldn't get next packet!");
                }
            }

            // try to do a comment header...
            if (LoadComments(packet))
            {
                if (wasPeek)
                {
                    _packetProvider.GetNextPacket().Done();
                }
                else
                {
                    packet.Done();
                }
                wasPeek = true;
                packet  = _packetProvider.PeekNextPacket();
                if (packet == null)
                {
                    throw new InvalidDataException("Couldn't get next packet!");
                }
            }

            // try to do a book header...
            if (LoadBooks(packet))
            {
                if (wasPeek)
                {
                    _packetProvider.GetNextPacket().Done();
                }
                else
                {
                    packet.Done();
                }
            }

            ResetDecoder(doFullReset);
        }
Exemplo n.º 2
0
        private void DecodeNextPacket()
        {
            _sw.Start();

            DataPacket packet = null;

            try
            {
                // get the next packet
                var packetProvider = _packetProvider;
                if (packetProvider != null)
                {
                    packet = packetProvider.GetNextPacket();
                }

                // if the packet is null, we've hit the end or the packet reader has been disposed...
                if (packet == null)
                {
                    _eosFound = true;
                    return;
                }

                // keep our page count in sync
                if (!_pagesSeen.Contains((_lastPageSeen = packet.PageSequenceNumber)))
                {
                    _pagesSeen.Add(_lastPageSeen);
                }

                // check for resync
                if (packet.IsResync)
                {
                    ResetDecoder(false); // if we're a resync, our current decoder state is invalid...
                }

                // check for parameter change
                if (packet == _parameterChangePacket)
                {
                    _isParameterChange = true;
                    ProcessParameterChange(packet);
                    return;
                }

                if (!UnpackPacket(packet))
                {
                    packet.Done();
                    _wasteBits += 8 * packet.Length;
                    return;
                }
                packet.Done();

                // we can now safely decode all the data without having to worry about a corrupt or partial packet

                DecodePacket();
                var samplesDecoded = OverlapSamples();

                // we can do something cool here...  mark down how many samples were decoded in this packet
                if (packet.GranuleCount.HasValue == false)
                {
                    packet.GranuleCount = samplesDecoded;
                }

                // update our position

                UpdatePosition(samplesDecoded, packet);

                // a little statistical housekeeping...
                var sc = Utils.Sum(_sampleCountHistory) + samplesDecoded;

                _bitsPerPacketHistory.Enqueue((int)packet.BitsRead);
                _sampleCountHistory.Enqueue(samplesDecoded);

                while (sc > _sampleRate)
                {
                    _bitsPerPacketHistory.Dequeue();
                    sc -= _sampleCountHistory.Dequeue();
                }
            }
            catch
            {
                if (packet != null)
                {
                    packet.Done();
                }
                throw;
            }
            finally
            {
                _sw.Stop();
            }
        }