@@ -364,24 +364,14 @@ public Header decodeHeader(ChunkHeader chh, RTMPDecodeState state, IoBuffer in,
364
364
// represents "packet" header length via "format" only 1 byte in the chunk header is needed here
365
365
int headerLength = RTMPUtils .getHeaderLength (headerSize );
366
366
headerLength += chh .getSize () - 1 ;
367
- if (in .remaining () < headerLength || in .remaining () < 3 ) {
367
+ //If remaining bytes is less than known headerLength return null and set decoder state.
368
+ //This length does not include 4-byte extended timestamp field if present.
369
+ if (in .remaining () < headerLength ) {
368
370
state .bufferDecoding (headerLength - in .remaining ());
369
371
in .position (startPostion );
370
372
return null ;
371
- } else {
372
- int currentPostition = in .position ();
373
- // medium int is 3 bytes
374
- int timeBase = RTMPUtils .readUnsignedMediumInt (in );
375
- in .position (currentPostition );
376
- if (timeBase >= MEDIUM_INT_MAX ) {
377
- headerLength += 4 ;
378
- if (in .remaining () < headerLength ) {
379
- state .bufferDecoding (headerLength - in .remaining ());
380
- in .position (startPostion );
381
- return null ;
382
- }
383
- }
384
373
}
374
+
385
375
Header lastHeader = rtmp .getLastReadHeader (channelId );
386
376
if (log .isTraceEnabled ()) {
387
377
log .trace ("{} lastHeader: {}" , Header .HeaderType .values ()[headerSize ], lastHeader );
@@ -403,11 +393,7 @@ public Header decodeHeader(ChunkHeader chh, RTMPDecodeState state, IoBuffer in,
403
393
// if (log.isTraceEnabled()) {
404
394
// log.trace("headerLength: {}", headerLength);
405
395
// }
406
- if (in .remaining () < headerLength ) {
407
- //log.trace("Header too small (hlen: {}), buffering. remaining: {}", headerLength, remaining);
408
- state .bufferDecoding (headerLength );
409
- return null ;
410
- }
396
+
411
397
int timeBase = 0 , timeDelta = 0 ;
412
398
Header header = new Header ();
413
399
header .setChannelId (channelId );
@@ -420,6 +406,12 @@ public Header decodeHeader(ChunkHeader chh, RTMPDecodeState state, IoBuffer in,
420
406
header .setStreamId (RTMPUtils .readReverseInt (in ));
421
407
// read the extended timestamp if we have the indication that it exists
422
408
if (timeBase >= MEDIUM_INT_MAX ) {
409
+ headerLength += 4 ;
410
+ if (in .remaining () < 4 ) {
411
+ state .bufferDecoding (headerLength - in .remaining ());
412
+ in .position (startPostion );
413
+ return null ;
414
+ }
423
415
long ext = in .getUnsignedInt ();
424
416
timeBase = (int ) (ext ^ (ext >>> 32 ));
425
417
if (log .isTraceEnabled ()) {
@@ -440,6 +432,12 @@ public Header decodeHeader(ChunkHeader chh, RTMPDecodeState state, IoBuffer in,
440
432
header .setStreamId (lastHeader .getStreamId ());
441
433
// read the extended timestamp if we have the indication that it exists
442
434
if (timeDelta >= MEDIUM_INT_MAX ) {
435
+ headerLength += 4 ;
436
+ if (in .remaining () < 4 ) {
437
+ state .bufferDecoding (headerLength - in .remaining ());
438
+ in .position (startPostion );
439
+ return null ;
440
+ }
443
441
long ext = in .getUnsignedInt ();
444
442
timeDelta = (int ) (ext ^ (ext >>> 32 ));
445
443
header .setExtended (true );
@@ -457,6 +455,12 @@ public Header decodeHeader(ChunkHeader chh, RTMPDecodeState state, IoBuffer in,
457
455
header .setStreamId (lastHeader .getStreamId ());
458
456
// read the extended timestamp if we have the indication that it exists
459
457
if (timeDelta >= MEDIUM_INT_MAX ) {
458
+ headerLength += 4 ;
459
+ if (in .remaining () < 4 ) {
460
+ state .bufferDecoding (headerLength - in .remaining ());
461
+ in .position (startPostion );
462
+ return null ;
463
+ }
460
464
long ext = in .getUnsignedInt ();
461
465
timeDelta = (int ) (ext ^ (ext >>> 32 ));
462
466
header .setExtended (true );
@@ -475,6 +479,12 @@ public Header decodeHeader(ChunkHeader chh, RTMPDecodeState state, IoBuffer in,
475
479
// This field is present in Type 3 chunks when the most recent Type 0, 1, or 2 chunk for the same chunk stream ID
476
480
// indicated the presence of an extended timestamp field
477
481
if (lastHeader .isExtended ()) {
482
+ headerLength += 4 ;
483
+ if (in .remaining () < 4 ) {
484
+ state .bufferDecoding (headerLength - in .remaining ());
485
+ in .position (startPostion );
486
+ return null ;
487
+ }
478
488
long ext = in .getUnsignedInt ();
479
489
int timeExt = (int ) (ext ^ (ext >>> 32 ));
480
490
if (log .isTraceEnabled ()) {
0 commit comments