Class: Ogg::Writer

Inherits:
Object
  • Object
show all
Defined in:
lib/ogg/writer.rb

Overview

Writes pages or packets to an output io

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bitstream_serial_no, output) ⇒ Writer

Returns a new instance of Writer.



9
10
11
12
13
# File 'lib/ogg/writer.rb', line 9

def initialize(bitstream_serial_no, output)
  @output = output
  @page_sequence = 0
  @bitstream_serial_no = bitstream_serial_no
end

Instance Attribute Details

#output ⇒ Object (readonly)

Returns the value of attribute output.



7
8
9
# File 'lib/ogg/writer.rb', line 7

def output
  @output
end

Instance Method Details

#write_packets(granule_pos, *packets) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/ogg/writer.rb', line 23

def write_packets(granule_pos, *packets)
  written_pages_count = 1
  page = Page.new(@bitstream_serial_no, granule_pos)
  packets.each do |packet|
    io = StringIO.new(packet)
    
    while !io.eof? do
      page.segments << io.read(255)
      if (page.segments.length == 255)
        page.granule_pos = -1
        write_page(page)
        page = Page.new(@bitstream_serial_no, granule_pos)
        written_pages_count += 1
      end
    end
    #If our packet was an exact multiple of 255 we need to put in an empty closing segment
    if (page.segments.length == 0 || page.segments.last.length == 255)
      page.segments << ""
    end
  end
  #we always need to flush the final page.
  write_page(page)
  written_pages_count
end

#write_page(page) ⇒ Object

Writes a page to the output, the serial number and page sequence are are overwritten to be appropriate for this stream.



17
18
19
20
21
# File 'lib/ogg/writer.rb', line 17

def write_page(page)
  page.sequence_no = @page_sequence
  @output << page.pack
  @page_sequence += 1
end