Skip to content

Commit

Permalink
fix: force negative frame durations to zero (#769)
Browse files Browse the repository at this point in the history
Non monotonic streams can produce negative sample duration
and result in RangeError: "value" argument is out of bounds.

This isn't correct behaviour, but by setting the duration
to zero a crash is prevented.

Fixes #294
Fixes #652

Co-authored-by: Alexander Kharkov <[email protected]>
  • Loading branch information
steabert and shurkaxaa authored Apr 8, 2024
1 parent 18c26c4 commit e6773e7
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion streams/src/components/mp4muxer/helpers/boxbuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,9 +218,11 @@ export class BoxBuilder {
// The RTP timestamps are unsigned 32 bit and will overflow
// at some point. We can guard against the overflow by ORing with 0,
// which will bring any difference back into signed 32-bit domain.
// If the duration would be negative, it's set to zero to prevent
// possible issues later when it's written as an unsigned int.
const duration =
trackData.lastTimestamp !== 0
? (timestamp - trackData.lastTimestamp) | 0
? Math.max(0, (timestamp - trackData.lastTimestamp) | 0)
: trackData.defaultFrameDuration

trackData.lastTimestamp = timestamp
Expand Down

0 comments on commit e6773e7

Please sign in to comment.