Ejemplo n.º 1
0
        public void Test()
        {
            var header = new OggsHeader(0);
            byte p1 = 0x1;
            header.StreamStructureVersion = p1;
            var p2 = OggsHeader.HeaderType.FirstPage | OggsHeader.HeaderType.LastPage;
            header.HeaderTypeFlag = p2;
            UInt64 p3 = 578437695752307201;
            header.GranulePosition = p3;
            UInt32 p4 = 67305985;
            header.BitstreamSerialNumber = p4;
            UInt32 p5 = 134678021;
            header.PageSequenceNumber = p5;
            UInt32 p6 = 50462985;
            header.CrcChecksum = p6;
            byte p7 = 0x7;
            header.NumberPageSegments = p7;
            byte p8 = 0x8;
            //header.SegmentTable = p8;

            Assert.IsTrue(header.CapturePattern.SequenceEqual("OggS".Select(c => (byte)c).ToArray()));
            Assert.IsTrue(p1 == header.StreamStructureVersion);
            Assert.IsTrue(p2 == header.HeaderTypeFlag);
            Assert.IsTrue(p3 == header.GranulePosition);
            Assert.IsTrue(p4 == header.BitstreamSerialNumber);
            Assert.IsTrue(p5 == header.PageSequenceNumber);
            Assert.IsTrue(p6 == header.CrcChecksum);
            Assert.IsTrue(p7 == header.NumberPageSegments);
            //Assert.IsTrue(p8 == header.SegmentTable);
        }
Ejemplo n.º 2
0
        public void Flush(Stream stream)
        {
            int paketPos = 0;
            var dataLen = _packets.Sum(p => p.PacketData.Length);
            while (_packets.Count > 0)
            {
                int segmentsCount = Math.Max((dataLen / 255) + 1, _packets.Count);
                int segmentPerPage = segmentsCount > 255 ? 255 : segmentsCount;

                var oggsHeader = new OggsHeader(PageNo, SerialNo, CreateSegmentalTable(segmentPerPage),
                                                _packets[0].Granulepos, paketPos != 0, Bos == 0,
                                                Eos && segmentPerPage == segmentsCount);
                Bos = 1;
                PageNo++;

                // set pointers in the ogg_page struct
                var og = new Page
                    {
                        header = oggsHeader,
                        header_len = oggsHeader.Data.Length,
                        body_len = oggsHeader.SegmentTableSum
                    };
                og.body = new byte[og.body_len];

                int saved = 0;
                while (saved < og.body_len)
                {
                    var savingPaket = _packets.First();
                    var copyLen = Math.Min(og.body_len - saved, savingPaket.PacketData.Length);
                    Array.Copy(savingPaket.PacketData, paketPos, og.body, saved, copyLen);
                    dataLen -= copyLen;
                    if (copyLen == savingPaket.PacketData.Length)
                    {
                        _packets.Remove(savingPaket);
                    }
                    else
                    {
            #if DEBUG
                        if (saved + copyLen != og.body_len)
                        {
                            throw new ArithmeticException();
                        }
            #endif
                        paketPos = savingPaket.PacketData.Length - copyLen;
                    }
                    saved += copyLen;
                    og.header.GranulePosition = savingPaket.Granulepos;
                }
                ogg_page_checksum_set(og);
                og.Write(stream);
            }
            _totalBits = 0;
        }
Ejemplo n.º 3
0
        private Page GetPage(ref int unsavedPaketIndex)
        {
            int savedPacketCount;
            int unsavedCount;
            var table = CreateSegmentalTable(unsavedPaketIndex, out savedPacketCount, out unsavedCount);

            var oggsHeader = new OggsHeader(_pageNo, SerialNo, table, _packets[savedPacketCount - 1].GranulePos,
                                            unsavedPaketIndex != 0, _bos == 0, Eos && savedPacketCount == _packets.Count);

            _bos = 1;
            _pageNo++;

            //set pointers in the ogg_page struct
            var og = new Page
            {
                Header = oggsHeader,
                HeaderLen = oggsHeader.Data.Length,
                BodyLen = oggsHeader.SegmentTableSum,
                Body = new byte[oggsHeader.SegmentTableSum]
            };

            int saved = 0;
            var savingPaket = _packets.FirstOrDefault();
            foreach (var count in table)
            {
                Array.Copy(savingPaket.PacketData, unsavedPaketIndex, og.Body, saved, count);
                saved += count;
                if (count == savingPaket.PacketDataLength - unsavedPaketIndex)
                {
                    _packets.Remove(savingPaket);
                    unsavedPaketIndex = 0;
                    savedPacketCount--;
                    savingPaket = _packets.FirstOrDefault();
                }
                else
                {
                    unsavedPaketIndex += count;
                }
                _totalBits -= count;
            }
            SetPageCheckSum(og);
            return og;
        }