private void Write(libogg.ogg_page page) { var header = new byte[page.header_len]; var body = new byte[page.body_len]; Marshal.Copy(page.header, header, 0, page.header_len); Marshal.Copy(page.body, body, 0, page.body_len); this.writer.Write(header); this.writer.Write(body); }
private void BufferPage() { var liboggPage = new libogg.ogg_page(); while (libogg.ogg_sync_pageout(ref this.syncState, ref liboggPage) != 1) { var bufferPtr = libogg.ogg_sync_buffer(ref this.syncState, 8192); var readBytes = this.reader.ReadBytes(8192); if (readBytes.Length == 0) { throw new EndOfStreamException("No more data in stream."); } Marshal.Copy(readBytes, 0, bufferPtr, readBytes.Length); if (libogg.ogg_sync_wrote(ref this.syncState, readBytes.Length) != 0) { throw new InvalidOperationException("Could not confirm amount of bytes written to libogg."); } } // Initialize a stream state with the same serial number as the page we just read if (!this.isStreamStateInitialized) { var result = libogg.ogg_stream_init(ref this.streamState, libogg.ogg_page_serialno(ref liboggPage)); if (result != 0) { throw new InvalidOperationException("Failed to initialize libogg stream state."); } this.isStreamStateInitialized = true; } // Submit page to stream if (libogg.ogg_stream_pagein(ref this.streamState, ref liboggPage) != 0) { throw new InvalidOperationException("Adding page to packet buffer failed due to a serial or page number mismatch, or an internal error occurred."); } }
public void Flush(bool force = true) { var liboggPage = new libogg.ogg_page(); int result; // Output pages until no more are available using the chosen method do { // Try to obtain a page, or squeeze it out if forcing result = force ? libogg.ogg_stream_flush(ref this.streamState, ref liboggPage) : libogg.ogg_stream_pageout(ref this.streamState, ref liboggPage); // Write page data to stream if a page was accumulated if (result != 0) { this.Write(liboggPage); } }while (result != 0); }