flv-tag.js
6.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
(function(window) {
window.videojs.hls.FlvTag = function() {};
/*
package com.videojs.providers.hls.utils{
import flash.utils.ByteArray;
import flash.utils.Endian;
public class FlvTag extends ByteArray
{
public static const AUDIO_TAG:uint = 0x08;
public static const VIDEO_TAG:uint = 0x09;
public static const METADATA_TAG:uint = 0x12;
public var keyFrame:Boolean = false;
private var extraData:Boolean = false;
private var adHoc:uint = 0; // Counter if this is a metadata tag, nal start marker if this is a video tag. unused if this is an audio tag
public var pts:uint;
public var dts:uint;
public static function isAudioFrame(tag:ByteArray):Boolean
{
return AUDIO_TAG == tag[0];
}
public static function isVideoFrame(tag:ByteArray):Boolean
{
return VIDEO_TAG == tag[0];
}
public static function isMetaData(tag:ByteArray):Boolean
{
return METADATA_TAG == tag[0];
}
public static function isKeyFrame(tag:ByteArray):Boolean
{
if ( isVideoFrame(tag) )
return tag[11] == 0x17;
if( isAudioFrame(tag) )
return true;
if( isMetaData(tag) )
return true;
return false;
}
public static function frameTime(tag:ByteArray):uint
{
var pts:uint = tag[ 4] << 16;
pts |= tag[ 5] << 8;
pts |= tag[ 6] << 0;
pts |= tag[ 7] << 24;
return pts;
}
public function FlvTag(type:uint, ed:Boolean = false)
{
super();
extraData = ed;
this.endian = Endian.BIG_ENDIAN;
switch(type)
{
case VIDEO_TAG: this.length = 16; break;
case AUDIO_TAG: this.length = 13; keyFrame = true; break;
case METADATA_TAG: this.length = 29; keyFrame = true; break;
default: throw("Error Unknown TagType");
}
this[0] = type
this.position = this.length;
keyFrame = extraData; // Defaults to false
pts = dts = 0;
}
// Negative index into array
public function negIndex(pos:uint):int
{
return this[this.length - pos];
}
// The functions below ONLY work when this[0] == VIDEO_TAG.
// We are not going to check for that because we dont want the overhead
public function nalUnitSize(nal:ByteArray = null):int
{
if( 0 == adHoc )
return 0;
return this.length - ( adHoc + 4 );
}
public function startNalUnit():void
{ // remember position and add 4 bytes
if ( 0 < adHoc )
{
throw new Error("Attempted to create new NAL wihout closing the old one");
}
// reserve 4 bytes for nal unit size
adHoc = this.length;
this.length += 4;
this.position = this.length;
}
public function endNalUnit(nal:ByteArray = null):void
{ // Rewind to the marker and write the size
if ( this.length == adHoc + 4 )
{
this.length -= 4; // we started a nal unit, but didnt write one, so roll back the 4 byte size value
}
else
if ( 0 < adHoc )
{
var nalStart:uint = adHoc + 4;
var nalLength:uint = this.length - nalStart;
this.position = adHoc;
this.writeUnsignedInt( nalLength );
this.position = this.length;
if ( null != nal ) // If the user pass in a ByteArray, copy the NAL to it.
nal.writeBytes( this, nalStart, nalLength );
}
adHoc = 0;
}
public function writeMetaDataDouble(key:String, val:Number):void
{
writeShort ( key.length );
writeUTFBytes ( key );
writeByte ( 0x00 );
writeDouble ( val );
++adHoc;
}
public function writeMetaDataBoolean(key:String, val:Boolean):void
{
writeShort ( key.length );
writeUTFBytes ( key );
writeByte ( 0x01 );
writeByte ( true == val ? 0x01 : 0x00 );
++adHoc;
}
public function finalize():ByteArray
{
switch(this[0])
{ // Video Data
case VIDEO_TAG:
this[11] = ( ( keyFrame || extraData ) ? 0x10 : 0x20 ) | 0x07; // We only support AVC, 1 = key frame (for AVC, a seekable frame), 2 = inter frame (for AVC, a non-seekable frame)
this[12] = extraData ? 0x00 : 0x01;
var dtsDelta:int = pts - dts;
this[13] = ( dtsDelta & 0x00FF0000 ) >>> 16;
this[14] = ( dtsDelta & 0x0000FF00 ) >>> 8;
this[15] = ( dtsDelta & 0x000000FF ) >>> 0;
break;
case AUDIO_TAG:
this[11] = 0xAF;
this[12] = extraData ? 0x00 : 0x01;
break;
case METADATA_TAG:
this.position = 11;
writeByte(0x02); // String type
writeShort(0x0A); // 10 Bytes
writeUTFBytes("onMetaData");
writeByte(0x08); // Array type
writeUnsignedInt( adHoc );
this.position = this.length;
writeUnsignedInt( 0x09 ); // End Data Tag
break;
}
var len:int = this.length - 11;
this[ 1] = ( len & 0x00FF0000 ) >>> 16;
this[ 2] = ( len & 0x0000FF00 ) >>> 8;
this[ 3] = ( len & 0x000000FF ) >>> 0;
this[ 4] = ( pts & 0x00FF0000 ) >>> 16;
this[ 5] = ( pts & 0x0000FF00 ) >>> 8;
this[ 6] = ( pts & 0x000000FF ) >>> 0;
this[ 7] = ( pts & 0xFF000000 ) >>> 24;
this[ 8] = 0;
this[ 9] = 0;
this[10] = 0;
this.writeUnsignedInt( this.length );
return this;
}
}
}
*/
})(this);