Skip to content

Commit

Permalink
Fix media duration parsing in mdhd box of MP4 files to handle -1 values
Browse files Browse the repository at this point in the history
Treats the media duration as unknown (`C.TIME_UNSET`) when all bytes are `-1` to prevent exceptions during playback.

Issue: #1819

#cherrypick

PiperOrigin-RevId: 688103949
  • Loading branch information
rohitjoins authored and copybara-github committed Oct 21, 2024
1 parent 40cd64a commit 457bc55
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
3 changes: 3 additions & 0 deletions RELEASENOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
* Common Library:
* ExoPlayer:
* Transformer:
* Extractors:
* Fix media duration parsing in `mdhd` box of MP4 files to handle `-1`
values ([#1819](https://github.com/androidx/media/issues/1819)).
* DataSource:
* `DataSourceContractTest`: Assert that `DataSource.getUri()` returns the
resolved URI (as documented). Where this is different to the requested
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -958,14 +958,28 @@ private static MdhdData parseMdhd(ParsableByteArray mdhd) {
int version = parseFullBoxVersion(fullAtom);
mdhd.skipBytes(version == 0 ? 8 : 16);
long timescale = mdhd.readUnsignedInt();
long mediaDuration = version == 0 ? mdhd.readUnsignedInt() : mdhd.readUnsignedLongToLong();
boolean mediaDurationUnknown = true;
int mediaDurationPosition = mdhd.getPosition();
int mediaDurationByteCount = version == 0 ? 4 : 8;
for (int i = 0; i < mediaDurationByteCount; i++) {
if (mdhd.getData()[mediaDurationPosition + i] != -1) {
mediaDurationUnknown = false;
break;
}
}
long mediaDurationUs;
if (mediaDuration == 0) {
// 0 duration normally indicates that the file is fully fragmented (i.e. all of the media
// samples are in fragments). Treat as unknown.
if (mediaDurationUnknown) {
mdhd.skipBytes(mediaDurationByteCount);
mediaDurationUs = C.TIME_UNSET;
} else {
mediaDurationUs = Util.scaleLargeTimestamp(mediaDuration, C.MICROS_PER_SECOND, timescale);
long mediaDuration = version == 0 ? mdhd.readUnsignedInt() : mdhd.readUnsignedLongToLong();
if (mediaDuration == 0) {
// 0 duration normally indicates that the file is fully fragmented (i.e. all of the media
// samples are in fragments). Treat as unknown.
mediaDurationUs = C.TIME_UNSET;
} else {
mediaDurationUs = Util.scaleLargeTimestamp(mediaDuration, C.MICROS_PER_SECOND, timescale);
}
}
int languageCode = mdhd.readUnsignedShort();
String language =
Expand Down

0 comments on commit 457bc55

Please sign in to comment.