Class: MP4::Layout::Container

Inherits:
Object
  • Object
show all
Defined in:
lib/mp4/layout/container.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root: false, name: nil) ⇒ Container

Returns a new instance of Container.



7
8
9
10
11
# File 'lib/mp4/layout/container.rb', line 7

def initialize(root: false, name: nil)
  @root = root
  @name = name
  @descendants ||= []
end

Instance Attribute Details

#descendantsObject

include ::Layout::Base



5
6
7
# File 'lib/mp4/layout/container.rb', line 5

def descendants
  @descendants
end

#nameObject

include ::Layout::Base



5
6
7
# File 'lib/mp4/layout/container.rb', line 5

def name
  @name
end

#rootObject

include ::Layout::Base



5
6
7
# File 'lib/mp4/layout/container.rb', line 5

def root
  @root
end

Instance Method Details

#add_atom(atom) ⇒ Object



41
42
43
44
# File 'lib/mp4/layout/container.rb', line 41

def add_atom(atom)
  @descendants << atom
  atom
end

#add_container(name, &block) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'lib/mp4/layout/container.rb', line 31

def add_container(name, &block)
  container = self.class.new(name: name)

  block.call(container)

  @descendants << container.generate

  container
end

#add_mdat(mp4, source) ⇒ Object



46
47
48
# File 'lib/mp4/layout/container.rb', line 46

def add_mdat(mp4, source)
  @descendants << ::Layout::Mdat.new(mp4: mp4, source: source)
end

#binary_descendantsObject



23
24
25
# File 'lib/mp4/layout/container.rb', line 23

def binary_descendants
  descendants.join
end

#generateObject



13
14
15
16
17
18
19
20
21
# File 'lib/mp4/layout/container.rb', line 13

def generate
  return binary_descendants if root

  [
    BinData::Uint32be.new(size).to_binary_s,
    BinData::String.new(name).to_binary_s,
    binary_descendants,
  ].join
end

#packObject



50
51
52
53
54
55
56
57
# File 'lib/mp4/layout/container.rb', line 50

def pack
  return descendants.map(&:pack).join if root

  [
    *header,
    descendants.map(&:pack),
  ].join
end

#sizeObject



27
28
29
# File 'lib/mp4/layout/container.rb', line 27

def size
  ::MP4::Constants::GENERIC_HEADER_SIZE + binary_descendants.size
end