index.js
3.42 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
/**
* mux.js
*
* Copyright (c) Brightcove
* Licensed Apache-2.0 https://github.com/videojs/mux.js/blob/master/LICENSE
*
* A stream-based aac to mp4 converter. This utility can be used to
* deliver mp4s to a SourceBuffer on platforms that support native
* Media Source Extensions.
*/
'use strict';
var Stream = require('../utils/stream.js');
var aacUtils = require('./utils');
// Constants
var AacStream;
/**
* Splits an incoming stream of binary data into ADTS and ID3 Frames.
*/
AacStream = function() {
var
everything = new Uint8Array(),
timeStamp = 0;
AacStream.prototype.init.call(this);
this.setTimestamp = function(timestamp) {
timeStamp = timestamp;
};
this.push = function(bytes) {
var
frameSize = 0,
byteIndex = 0,
bytesLeft,
chunk,
packet,
tempLength;
// If there are bytes remaining from the last segment, prepend them to the
// bytes that were pushed in
if (everything.length) {
tempLength = everything.length;
everything = new Uint8Array(bytes.byteLength + tempLength);
everything.set(everything.subarray(0, tempLength));
everything.set(bytes, tempLength);
} else {
everything = bytes;
}
while (everything.length - byteIndex >= 3) {
if ((everything[byteIndex] === 'I'.charCodeAt(0)) &&
(everything[byteIndex + 1] === 'D'.charCodeAt(0)) &&
(everything[byteIndex + 2] === '3'.charCodeAt(0))) {
// Exit early because we don't have enough to parse
// the ID3 tag header
if (everything.length - byteIndex < 10) {
break;
}
// check framesize
frameSize = aacUtils.parseId3TagSize(everything, byteIndex);
// Exit early if we don't have enough in the buffer
// to emit a full packet
// Add to byteIndex to support multiple ID3 tags in sequence
if (byteIndex + frameSize > everything.length) {
break;
}
chunk = {
type: 'timed-metadata',
data: everything.subarray(byteIndex, byteIndex + frameSize)
};
this.trigger('data', chunk);
byteIndex += frameSize;
continue;
} else if (((everything[byteIndex] & 0xff) === 0xff) &&
((everything[byteIndex + 1] & 0xf0) === 0xf0)) {
// Exit early because we don't have enough to parse
// the ADTS frame header
if (everything.length - byteIndex < 7) {
break;
}
frameSize = aacUtils.parseAdtsSize(everything, byteIndex);
// Exit early if we don't have enough in the buffer
// to emit a full packet
if (byteIndex + frameSize > everything.length) {
break;
}
packet = {
type: 'audio',
data: everything.subarray(byteIndex, byteIndex + frameSize),
pts: timeStamp,
dts: timeStamp
};
this.trigger('data', packet);
byteIndex += frameSize;
continue;
}
byteIndex++;
}
bytesLeft = everything.length - byteIndex;
if (bytesLeft > 0) {
everything = everything.subarray(byteIndex);
} else {
everything = new Uint8Array();
}
};
this.reset = function() {
everything = new Uint8Array();
this.trigger('reset');
};
this.endTimeline = function() {
everything = new Uint8Array();
this.trigger('endedtimeline');
};
};
AacStream.prototype = new Stream();
module.exports = AacStream;