1 | # Copyright (c) 2005 Norman Timmler (inlet media e.K., Hamburg, Germany)
|
---|
2 | # All rights reserved.
|
---|
3 | #
|
---|
4 | # Redistribution and use in source and binary forms, with or without
|
---|
5 | # modification, are permitted provided that the following conditions
|
---|
6 | # are met:
|
---|
7 | # 1. Redistributions of source code must retain the above copyright
|
---|
8 | # notice, this list of conditions and the following disclaimer.
|
---|
9 | # 2. Redistributions in binary form must reproduce the above copyright
|
---|
10 | # notice, this list of conditions and the following disclaimer in the
|
---|
11 | # documentation and/or other materials provided with the distribution.
|
---|
12 | # 3. The name of the author may not be used to endorse or promote products
|
---|
13 | # derived from this software without specific prior written permission.
|
---|
14 | #
|
---|
15 | # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
16 | # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
17 | # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
18 | # IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
19 | # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
20 | # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
21 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
22 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
23 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
24 | # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
25 |
|
---|
26 | require 'flv/core_extensions'
|
---|
27 | require 'flv/stream'
|
---|
28 |
|
---|
29 |
|
---|
30 | module FLV
|
---|
31 |
|
---|
32 | class FLVTag
|
---|
33 |
|
---|
34 | AUDIO = 8
|
---|
35 | VIDEO = 9
|
---|
36 | META = 18
|
---|
37 | UNDEFINED = 0
|
---|
38 |
|
---|
39 | attr_accessor :tag_type,
|
---|
40 | :timestamp,
|
---|
41 | :byte_offset
|
---|
42 |
|
---|
43 | def initialize(stream = nil)
|
---|
44 | @tag_type = UNDEFINED
|
---|
45 | @byte_offset = nil
|
---|
46 |
|
---|
47 | unless stream.nil?
|
---|
48 | data_size = stream.read__UI24
|
---|
49 | @timestamp = stream.read__UI24
|
---|
50 | stream.read__UI32
|
---|
51 | @data = stream.read(data_size)
|
---|
52 | else
|
---|
53 | @timestamp = 0
|
---|
54 | @data = ''
|
---|
55 | end
|
---|
56 | after_initialize(stream.nil?) if respond_to? :after_initialize
|
---|
57 | end
|
---|
58 |
|
---|
59 | def size
|
---|
60 | # header(11) + body(data_size)
|
---|
61 | 11 + data_size
|
---|
62 | end
|
---|
63 |
|
---|
64 | def name
|
---|
65 | 'Unknown Tag'
|
---|
66 | end
|
---|
67 |
|
---|
68 | def data
|
---|
69 | @data
|
---|
70 | end
|
---|
71 |
|
---|
72 | def data_size
|
---|
73 | data.length
|
---|
74 | end
|
---|
75 |
|
---|
76 | def serialize(stream)
|
---|
77 | stream.write__UI8 tag_type
|
---|
78 | stream.write__UI24 data_size
|
---|
79 | stream.write__UI24 timestamp
|
---|
80 | stream.write__UI32 0
|
---|
81 | stream.write__STRING data
|
---|
82 | end
|
---|
83 |
|
---|
84 | def info
|
---|
85 | "#{name}: timestamp #{timestamp}, size #{size}, data size #{data_size}"
|
---|
86 | end
|
---|
87 |
|
---|
88 | def inspect
|
---|
89 | out = ["tag: #{self.class}"]
|
---|
90 | out << "timestamp: #{@timestamp}"
|
---|
91 | out << "size: #{size}"
|
---|
92 | out << "data_size: #{data_size}"
|
---|
93 | out
|
---|
94 | end
|
---|
95 |
|
---|
96 | def self.type2name(type)
|
---|
97 | case type
|
---|
98 | when AUDIO
|
---|
99 | 'audio'
|
---|
100 | when VIDEO
|
---|
101 | 'video'
|
---|
102 | when META
|
---|
103 | 'meta'
|
---|
104 | when UNDEFINED
|
---|
105 | 'undefined'
|
---|
106 | else
|
---|
107 | "unknown(#{type})"
|
---|
108 | end
|
---|
109 | end
|
---|
110 |
|
---|
111 | private
|
---|
112 | def bit2uint(sequence)
|
---|
113 | int = 0
|
---|
114 | sequence.split(//).each_with_index do |character, i|
|
---|
115 | int += 2 ** (sequence.length - i - 1) if character == '1'
|
---|
116 | end
|
---|
117 | int
|
---|
118 | end
|
---|
119 | end
|
---|
120 | end
|
---|