@@ -64,7 +64,7 @@ AdtsStream = function(handlePartialSegments) {
64
64
65
65
// Prepend any data in the buffer to the input data so that we can parse
66
66
// aac frames the cross a PES packet boundary
67
- if ( buffer ) {
67
+ if ( buffer && buffer . length ) {
68
68
oldBuffer = buffer ;
69
69
buffer = new Uint8Array ( oldBuffer . byteLength + packet . data . byteLength ) ;
70
70
buffer . set ( oldBuffer ) ;
@@ -75,8 +75,10 @@ AdtsStream = function(handlePartialSegments) {
75
75
76
76
// unpack any ADTS frames which have been fully received
77
77
// for details on the ADTS header, see http://wiki.multimedia.cx/index.php?title=ADTS
78
- while ( i + 5 < buffer . length ) {
79
78
79
+ // We use i + 7 here because we want to be able to parse the entire header.
80
+ // If we don't have enough bytes to do that, then we definitely won't have a full frame.
81
+ while ( ( i + 7 ) < buffer . length ) {
80
82
// Look for the start of an ADTS header..
81
83
if ( ( buffer [ i ] !== 0xFF ) || ( buffer [ i + 1 ] & 0xF6 ) !== 0xF0 ) {
82
84
// If a valid header was not found, jump one forward and attempt to
@@ -91,6 +93,7 @@ AdtsStream = function(handlePartialSegments) {
91
93
92
94
// Frame length is a 13 bit integer starting 16 bits from the
93
95
// end of the sync sequence
96
+ // NOTE: frame length includes the size of the header
94
97
frameLength = ( ( buffer [ i + 3 ] & 0x03 ) << 11 ) |
95
98
( buffer [ i + 4 ] << 3 ) |
96
99
( ( buffer [ i + 5 ] & 0xe0 ) >> 5 ) ;
@@ -99,12 +102,10 @@ AdtsStream = function(handlePartialSegments) {
99
102
adtsFrameDuration = ( sampleCount * ONE_SECOND_IN_TS ) /
100
103
ADTS_SAMPLING_FREQUENCIES [ ( buffer [ i + 2 ] & 0x3c ) >>> 2 ] ;
101
104
102
- frameEnd = i + frameLength ;
103
-
104
- // If we don't have enough data to actually finish this ADTS frame, return
105
- // and wait for more data
106
- if ( buffer . byteLength < frameEnd ) {
107
- return ;
105
+ // If we don't have enough data to actually finish this ADTS frame,
106
+ // then we have to wait for more data
107
+ if ( ( buffer . byteLength - i ) < frameLength ) {
108
+ break ;
108
109
}
109
110
110
111
// Otherwise, deliver the complete AAC frame
@@ -119,20 +120,16 @@ AdtsStream = function(handlePartialSegments) {
119
120
samplingfrequencyindex : ( buffer [ i + 2 ] & 0x3c ) >>> 2 ,
120
121
// assume ISO/IEC 14496-12 AudioSampleEntry default of 16
121
122
samplesize : 16 ,
122
- data : buffer . subarray ( i + 7 + protectionSkipBytes , frameEnd )
123
+ // data is the frame without it's header
124
+ data : buffer . subarray ( i + 7 + protectionSkipBytes , i + frameLength )
123
125
} ) ;
124
126
125
127
frameNum ++ ;
126
-
127
- // If the buffer is empty, clear it and return
128
- if ( buffer . byteLength === frameEnd ) {
129
- buffer = undefined ;
130
- return ;
131
- }
132
-
133
- // Remove the finished frame from the buffer and start the process again
134
- buffer = buffer . subarray ( frameEnd ) ;
128
+ i += frameLength ;
135
129
}
130
+
131
+ // remove processed bytes from the buffer.
132
+ buffer = buffer . subarray ( i ) ;
136
133
} ;
137
134
138
135
this . flush = function ( ) {
0 commit comments