forked from iizukanao/node-rtsp-rtmp-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflv.js
236 lines (229 loc) · 6.74 KB
/
flv.js
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
// Generated by CoffeeScript 1.10.0
(function() {
var Bits, aac, api, h264, logger;
h264 = require('./h264');
aac = require('./aac');
Bits = require('./bits');
logger = require('./logger');
api = {
SOUND_FORMAT_AAC: 10,
SOUND_RATE_5KHZ: 0,
SOUND_RATE_11KHZ: 1,
SOUND_RATE_22KHZ: 2,
SOUND_RATE_44KHZ: 3,
SOUND_SIZE_COMPRESSED: 1,
SOUND_TYPE_MONO: 0,
SOUND_TYPE_STEREO: 1,
AAC_PACKET_TYPE_SEQUENCE_HEADER: 0,
AAC_PACKET_TYPE_RAW: 1,
AVC_PACKET_TYPE_SEQUENCE_HEADER: 0,
AVC_PACKET_TYPE_NALU: 1,
AVC_PACKET_TYPE_EOS: 2,
getSoundType: function(channels) {
if (channels > 1) {
return 1;
} else {
return 0;
}
},
getSoundSize: function(numBits) {
switch (numBits) {
case 8:
return 0;
case 16:
return 1;
default:
throw new Error("Invalid number of bits in a sample: " + numBits);
}
},
getSampleRateFromSoundRate: function(soundRate) {
switch (soundRate) {
case 0:
return 5512;
case 1:
return 11025;
case 2:
return 22050;
case 3:
return 44100;
default:
throw new Error("Invalid SoundRate: " + soundRate);
}
},
getSoundRate: function(sampleRate) {
switch (parseInt(sampleRate)) {
case 5512:
return 0;
case 11025:
return 1;
case 22050:
return 2;
case 44100:
return 3;
default:
throw new Error("Sample rate is not supported by FLV: " + sampleRate);
}
},
createAACAudioDataTag: function(opts) {
return api.createAudioDataTag({
soundFormat: api.SOUND_FORMAT_AAC,
soundRate: api.SOUND_RATE_44KHZ,
soundSize: api.SOUND_SIZE_COMPRESSED,
soundType: api.SOUND_TYPE_STEREO,
aacPacketType: opts.aacPacketType
});
},
videoCodecID2Str: function(codecID) {
switch (codecID) {
case 2:
return "Sorenson H.263";
case 3:
return "Screen video";
case 4:
return "On2 VP6";
case 5:
return "On2 VP6 with alpha channel";
case 6:
return "Screen video version 2";
case 7:
return "AVC";
default:
return "unknown";
}
},
parseVideo: function(buf) {
var bits, info;
info = {};
bits = new Bits(buf);
info.videoDataTag = api.readVideoDataTag(bits);
if (info.videoDataTag.codecID !== 7) {
throw new Error(("flv: Video codec ID " + info.videoDataTag.codecID + " ") + ("(" + (api.videoCodecID2Str(info.videoDataTag.codecID)) + ") is not supported. Use H.264."));
}
switch (info.videoDataTag.avcPacketType) {
case api.AVC_PACKET_TYPE_SEQUENCE_HEADER:
info.avcDecoderConfigurationRecord = h264.readAVCDecoderConfigurationRecord(bits);
break;
case api.AVC_PACKET_TYPE_NALU:
info.nalUnits = bits.remaining_buffer();
break;
case api.AVC_PACKET_TYPE_EOS:
break;
default:
throw new Error("flv: unknown AVCPacketType: " + info.videoDataTag.avcPacketType);
}
return info;
},
splitNALUnits: function(buf, nalUnitLengthSize) {
var bits, nalUnitLen, nalUnits;
bits = new Bits(buf);
nalUnits = [];
while (bits.has_more_data()) {
nalUnitLen = bits.read_bits(nalUnitLengthSize * 8);
nalUnits.push(bits.read_bytes(nalUnitLen));
}
return nalUnits;
},
soundFormat2Str: function(soundFormat) {
switch (soundFormat) {
case 0:
return "Linear PCM, platform endian";
case 1:
return "ADPCM";
case 2:
return "MP3";
case 3:
return "Linear PCM, little endian";
case 4:
return "Nellymoser 16 kHz mono";
case 5:
return "Nellymoser 8 kHz mono";
case 6:
return "Nellymoser";
case 7:
return "G.711 A-law logarithmic PCM";
case 8:
return "G.711 mu-law logarithmic PCM";
case 9:
return "reserved";
case 10:
return "AAC";
case 11:
return "Speex";
case 14:
return "MP3 8 kHz";
case 15:
return "Device-specific sound";
default:
return "unknown";
}
},
parseAudio: function(buf) {
var bits, info;
info = {};
bits = new Bits(buf);
info.audioDataTag = api.readAudioDataTag(bits);
if (info.audioDataTag.soundFormat !== api.SOUND_FORMAT_AAC) {
throw new Error(("flv: Sound format " + info.audioDataTag.soundFormat + " ") + ("(" + (api.soundFormat2Str(info.audioDataTag.soundFormat)) + ") is not supported. Use AAC."));
}
switch (info.audioDataTag.aacPacketType) {
case api.AAC_PACKET_TYPE_SEQUENCE_HEADER:
if (bits.has_more_data()) {
bits.mark();
info.ascInfo = aac.readAudioSpecificConfig(bits);
info.audioSpecificConfig = bits.marked_bytes();
} else {
logger.warn("flv:parseAudio(): warn: AAC sequence header does not contain AudioSpecificConfig");
}
break;
case api.AAC_PACKET_TYPE_RAW:
info.rawDataBlock = bits.remaining_buffer();
break;
default:
throw new Error("flv: unknown AACPacketType: " + info.audioDataTag.aacPacketType);
}
return info;
},
readVideoDataTag: function(bits) {
var info;
info = {};
info.frameType = bits.read_bits(4);
info.codecID = bits.read_bits(4);
if (info.codecID === 7) {
info.avcPacketType = bits.read_byte();
info.compositionTime = bits.read_bits(24);
}
return info;
},
readAudioDataTag: function(bits) {
var b, info;
info = {};
b = bits.read_byte();
info.soundFormat = b >> 4;
info.soundRate = (b >> 2) & 0x3;
info.soundSize = (b >> 1) & 0x1;
info.soundType = b & 0x1;
if (info.soundFormat === api.SOUND_FORMAT_AAC) {
info.aacPacketType = bits.read_byte();
}
return info;
},
createAudioDataTag: function(opts) {
var buf, soundRate, soundType;
soundType = opts.soundType;
soundRate = opts.soundRate;
if (opts.soundFormat === api.SOUND_FORMAT_AAC) {
soundType = 1;
soundRate = api.SOUND_RATE_44KHZ;
}
buf = [(opts.soundFormat << 4) | (soundRate << 2) | (opts.soundSize << 1) | soundType];
if (opts.soundFormat === api.SOUND_FORMAT_AAC) {
buf.push(opts.aacPacketType);
}
return buf;
},
convertMsToPTS: function(ms) {
return ms * 90;
}
};
module.exports = api;
}).call(this);