Пример #1
0
        public bool TryReadPacket(out Packet packet)
        {
            //Trace.WriteLine ("Entering", "TryReadPacket");
            //Trace.Indent();

            packet = null;

            int lastSegment = 0;
            OggPage page, firstPage;
            OggStream ostream;
            if (!this.continuation)
            {
                //Trace.WriteLine ("Not a continuation, reading page", "TryReadPacket");

                firstPage = page = ReadPage();
                if (page == null)
                {
                    //Trace.Unindent();
                    //Trace.WriteLine ("Exiting (no packet)", "TryReadPacket");
                    return false;
                }

                if (!this.oggStreams.TryGetValue (page.StreamIndex, out ostream))
                {
                    //Trace.WriteLine ("New ogg stream: " + page.StreamIndex, "TryReadPacket");
                    this.oggStreams[page.StreamIndex] = ostream = new OggStream (ReadPage);
                }

                //Trace.WriteLine (String.Format("SI: {0}; G: {1:N0}; S: {2:N0}; SL: {3:N0}", page.StreamIndex, page.GranuleNumber, page.SequenceNumber, page.Segments.Length));

                ostream.Enqueue (page);
            }
            else
            {
                //Trace.WriteLine ("Continuing on page from " + this.lastStreamIndex, "TryReadPacket");

                ostream = this.oggStreams[this.lastStreamIndex];
                if ((firstPage = page = ostream.Peek()) == null && (firstPage = page = ReadPage()) == null)
                {
                    //Trace.Unindent();
                    //Trace.WriteLine ("Exiting (no page)", "TryReadPacket");
                    return false;
                }

                lastSegment = ostream.LastSegment;
                //Trace.WriteLine (this.lastStreamIndex + " Last segment: " + lastSegment, "TryReadPacket");
            }

            List<byte[]> segments = new List<byte[]>();

            int packetLength = 0;
            bool packetEndFound = false;
            while (!packetEndFound)
            {
                int i;
                for (i = lastSegment; i < page.Segments.Length; ++i)
                {
                    int length = page.Segments[i].Length;
                    packetLength += length;
                    segments.Add (page.Segments[i]);

                    //Trace.WriteLine (String.Format ("Segment {0:N0}, length: {1}", i, length), "TryReadPacket");
                    if (length < 255)
                    {
                        packetEndFound = true;
                        i++;
                        break;
                    }
                }

                if (!packetEndFound)
                {
                    //Trace.WriteLine ("Reached end of page, continuing with another", "TryReadPacket");
                    ostream.Dequeue(); // used up whole last page

                    OggPage nextPage;
                    while (true)
                    {
                        if ((nextPage = ostream.Peek()) == null && (nextPage = ReadPage()) == null)
                        {
                            this.continuation = true; // Let's try this stream again next call.
                            this.lastStreamIndex = page.StreamIndex;
                            ostream.Enqueue (page);
                            //Trace.Unindent();
                            //Trace.WriteLine ("Exiting (no page to continue with)", "TryReadPacket");
                            return false;
                        }

                        if (nextPage.StreamIndex != page.StreamIndex)
                        {
                            OggStream nextStream;
                            if (!this.oggStreams.TryGetValue (page.StreamIndex, out nextStream))
                            {
                                //Trace.WriteLine ("New ogg stream: " + page.StreamIndex, "TryReadPacket");
                                this.oggStreams[page.StreamIndex] = nextStream = new OggStream (ReadPage);
                            }

                            nextStream.Enqueue (page);
                        }
                        else
                        {
                            ostream.Enqueue (page);
                            break;
                        }
                    }

                    page = nextPage;
                    lastSegment = 0;
                }
                else
                {
                    ostream.PacketCount++;

                    if (i != page.Segments.Length)
                    {
                        //Trace.WriteLine ("Not done with this page yet (" + i + "/" + page.Segments.Length + ")", "TryReadPacket");
                        this.continuation = true;
                        this.lastStreamIndex = page.StreamIndex;
                        ostream.LastSegment = i;
                    }
                    else
                    {
                        //Trace.WriteLine ("Finished packet", "TryReadPacket");
                        ostream.Dequeue();
                        this.continuation = false;
                    }
                }
            }

            byte[] pdata = AssemblePacket (segments);
            packet = new OggPacket (page.StreamIndex, ostream.PacketCount - 1, firstPage.GranuleNumber, pdata);

            //Trace.Unindent();
            //Trace.WriteLine ("Exiting", "TryReadPacket");

            return true;
        }
Пример #2
0
        public bool TryReadPacket(out Packet packet)
        {
            //Trace.WriteLine ("Entering", "TryReadPacket");
            //Trace.Indent();

            packet = null;

            int       lastSegment = 0;
            OggPage   page, firstPage;
            OggStream ostream;

            if (!this.continuation)
            {
                //Trace.WriteLine ("Not a continuation, reading page", "TryReadPacket");

                firstPage = page = ReadPage();
                if (page == null)
                {
                    //Trace.Unindent();
                    //Trace.WriteLine ("Exiting (no packet)", "TryReadPacket");
                    return(false);
                }

                if (!this.oggStreams.TryGetValue(page.StreamIndex, out ostream))
                {
                    //Trace.WriteLine ("New ogg stream: " + page.StreamIndex, "TryReadPacket");
                    this.oggStreams[page.StreamIndex] = ostream = new OggStream(ReadPage);
                }

                //Trace.WriteLine (String.Format("SI: {0}; G: {1:N0}; S: {2:N0}; SL: {3:N0}", page.StreamIndex, page.GranuleNumber, page.SequenceNumber, page.Segments.Length));

                ostream.Enqueue(page);
            }
            else
            {
                //Trace.WriteLine ("Continuing on page from " + this.lastStreamIndex, "TryReadPacket");

                ostream = this.oggStreams[this.lastStreamIndex];
                if ((firstPage = page = ostream.Peek()) == null && (firstPage = page = ReadPage()) == null)
                {
                    //Trace.Unindent();
                    //Trace.WriteLine ("Exiting (no page)", "TryReadPacket");
                    return(false);
                }

                lastSegment = ostream.LastSegment;
                //Trace.WriteLine (this.lastStreamIndex + " Last segment: " + lastSegment, "TryReadPacket");
            }

            List <byte[]> segments = new List <byte[]>();

            int  packetLength   = 0;
            bool packetEndFound = false;

            while (!packetEndFound)
            {
                int i;
                for (i = lastSegment; i < page.Segments.Length; ++i)
                {
                    int length = page.Segments[i].Length;
                    packetLength += length;
                    segments.Add(page.Segments[i]);

                    //Trace.WriteLine (String.Format ("Segment {0:N0}, length: {1}", i, length), "TryReadPacket");
                    if (length < 255)
                    {
                        packetEndFound = true;
                        i++;
                        break;
                    }
                }

                if (!packetEndFound)
                {
                    //Trace.WriteLine ("Reached end of page, continuing with another", "TryReadPacket");
                    ostream.Dequeue();                     // used up whole last page

                    OggPage nextPage;
                    while (true)
                    {
                        if ((nextPage = ostream.Peek()) == null && (nextPage = ReadPage()) == null)
                        {
                            this.continuation    = true;                          // Let's try this stream again next call.
                            this.lastStreamIndex = page.StreamIndex;
                            ostream.Enqueue(page);
                            //Trace.Unindent();
                            //Trace.WriteLine ("Exiting (no page to continue with)", "TryReadPacket");
                            return(false);
                        }

                        if (nextPage.StreamIndex != page.StreamIndex)
                        {
                            OggStream nextStream;
                            if (!this.oggStreams.TryGetValue(page.StreamIndex, out nextStream))
                            {
                                //Trace.WriteLine ("New ogg stream: " + page.StreamIndex, "TryReadPacket");
                                this.oggStreams[page.StreamIndex] = nextStream = new OggStream(ReadPage);
                            }

                            nextStream.Enqueue(page);
                        }
                        else
                        {
                            ostream.Enqueue(page);
                            break;
                        }
                    }

                    page        = nextPage;
                    lastSegment = 0;
                }
                else
                {
                    ostream.PacketCount++;

                    if (i != page.Segments.Length)
                    {
                        //Trace.WriteLine ("Not done with this page yet (" + i + "/" + page.Segments.Length + ")", "TryReadPacket");
                        this.continuation    = true;
                        this.lastStreamIndex = page.StreamIndex;
                        ostream.LastSegment  = i;
                    }
                    else
                    {
                        //Trace.WriteLine ("Finished packet", "TryReadPacket");
                        ostream.Dequeue();
                        this.continuation = false;
                    }
                }
            }

            byte[] pdata = AssemblePacket(segments);
            packet = new OggPacket(page.StreamIndex, ostream.PacketCount - 1, firstPage.GranuleNumber, pdata);

            //Trace.Unindent();
            //Trace.WriteLine ("Exiting", "TryReadPacket");

            return(true);
        }