Use getDeclaredLength() in setDataSource for AssetFileDescriptor

Aligns `MediaExtractorCompat` with platform behavior by using `getDeclaredLength()`
instead of `getLength()` when setting the data source. This ensures compatibility
with asset files where the length is not predefined, even though it may result
in reading across multiple logical files when backed by the same physical file.

PiperOrigin-RevId: 655153390
This commit is contained in:
rohks 2024-07-23 07:15:50 -07:00 committed by Copybara-Service
parent 1797359950
commit 0ac90855b4

View File

@ -197,7 +197,11 @@ public final class MediaExtractorCompat {
* @throws IllegalStateException If this method is called twice on the same instance.
*/
public void setDataSource(AssetFileDescriptor assetFileDescriptor) throws IOException {
if (assetFileDescriptor.getLength() == AssetFileDescriptor.UNKNOWN_LENGTH) {
// Using getDeclaredLength() to match platform behavior, even though it may result in reading
// across multiple logical files when they share the same physical file.
// Reference implementation in platform MediaExtractor:
// https://cs.android.com/android/platform/superproject/main/+/main:frameworks/base/media/java/android/media/MediaExtractor.java;l=219-226;drc=3181772b27c6bf3e9625677d1d8afc89d938a60e
if (assetFileDescriptor.getDeclaredLength() == AssetFileDescriptor.UNKNOWN_LENGTH) {
setDataSource(assetFileDescriptor.getFileDescriptor());
} else {
setDataSource(