Class: FLV::Header

Inherits:
Object
  • Object
show all
Includes:
Base
Defined in:
lib/flvedit/flv/header.rb

Overview

Represents the header chunk present at the very beginning of any FLV file.

Constant Summary collapse

FLV_SIGNATURE =
[String, {:bytes => 3}].freeze
SIGNATURE =
'FLV'
MIN_OFFSET =
9
FLAGS =
{ :has_video => 1, :has_audio => 4 }.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Base

#getters, included, #is?, #size, #to_hash

Instance Attribute Details

#extraObject

Returns the value of attribute extra.



5
6
7
# File 'lib/flvedit/flv/header.rb', line 5

def extra
  @extra
end

#has_audioObject

Returns the value of attribute has_audio.



5
6
7
# File 'lib/flvedit/flv/header.rb', line 5

def has_audio
  @has_audio
end

#has_videoObject

Returns the value of attribute has_video.



5
6
7
# File 'lib/flvedit/flv/header.rb', line 5

def has_video
  @has_video
end

#pathObject

Returns the value of attribute path.



5
6
7
# File 'lib/flvedit/flv/header.rb', line 5

def path
  @path
end

#versionObject

Returns the value of attribute version.



5
6
7
# File 'lib/flvedit/flv/header.rb', line 5

def version
  @version
end

Instance Method Details

#bodyObject



41
42
43
# File 'lib/flvedit/flv/header.rb', line 41

def body
  self
end

#debug(format) ⇒ Object



32
33
34
35
# File 'lib/flvedit/flv/header.rb', line 32

def debug(format, *)
  format.header("Header", path)
  format.values(to_hash.tap{|h| [:path, :timestamp, :body].each{|key| h.delete(key)}})
end

#read_packed(io) ⇒ Object

:nodoc:

Raises:

  • (RuntimeError)


12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/flvedit/flv/header.rb', line 12

def read_packed(io, *) #:nodoc:
      signature,        self.version, type_flags, offset  = \
  io  >> FLV_SIGNATURE  >> :char      >> :char    >> :unsigned_long 

  raise RuntimeError.new("typeflags is #{signature}, #{version}, #{type_flags}, #{offset}") unless Fixnum === type_flags
  
  FLAGS.each {|flag,mask| send("#{flag}=", type_flags & mask > 0) }
  self.extra = io.read(offset - MIN_OFFSET)
  ignore_PreviousTagSize0 = io.read :unsigned_long
  self.path = io.try :path
  raise IOError("Wrong Signature (#{signature} instead of #{SIGNATURE})") unless SIGNATURE == signature
end

#timestampObject



37
38
39
# File 'lib/flvedit/flv/header.rb', line 37

def timestamp
  0
end

#write_packed(io) ⇒ Object

:nodoc:



25
26
27
28
29
30
# File 'lib/flvedit/flv/header.rb', line 25

def write_packed(io, *) #:nodoc:
  self.extra ||= ""
  type_flags = FLAGS.sum{|flag, mask | send(flag) ? mask : 0}
  io << SIGNATURE << [self.version || 1, :char] << [type_flags, :char] <<
        [MIN_OFFSET + self.extra.length, :unsigned_long] << self.extra << [0, :unsigned_long]
end