Fix misleading transcoding exception
When opening a file with the platform media APIs, we attempt to
convert the fd to the original fd. If the input fd is already the
original fd and not a transcoded one, we fail the conversion and use
the input fd directly. The conversion failure logged a misleading
exception:
04-28 13:26:58.409 3594 10238 E DatabaseUtils: Writing exception to parcel
04-28 13:26:58.409 3594 10238 E DatabaseUtils:
java.lang.IllegalArgumentException: Input file descriptor is already
original
Now, we removed this harmless and WAI log.
We also hardened the check for a file supporting transcode to actually
check the codec type and not just the file location and extension.
Test: atest TranscodeTest
Bug: 186672282
Change-Id: I7399bf10fd4de98512327f9b6d055b7417e54ef1
diff --git a/apex/framework/java/android/provider/MediaStore.java b/apex/framework/java/android/provider/MediaStore.java
index 103e09e..2a17e34 100644
--- a/apex/framework/java/android/provider/MediaStore.java
+++ b/apex/framework/java/android/provider/MediaStore.java
@@ -928,13 +928,19 @@
@NonNull ParcelFileDescriptor fileDescriptor) throws IOException {
Bundle input = new Bundle();
input.putParcelable(EXTRA_FILE_DESCRIPTOR, fileDescriptor);
+ ParcelFileDescriptor pfd;
try {
Bundle output = context.getContentResolver().call(AUTHORITY,
GET_ORIGINAL_MEDIA_FORMAT_FILE_DESCRIPTOR_CALL, null, input);
- return output.getParcelable(EXTRA_FILE_DESCRIPTOR);
+ pfd = output.getParcelable(EXTRA_FILE_DESCRIPTOR);
} catch (Exception e) {
throw new IOException(e);
}
+
+ if (pfd == null) {
+ throw new IOException("Input file descriptor already original");
+ }
+ return pfd;
}
/**
diff --git a/src/com/android/providers/media/MediaProvider.java b/src/com/android/providers/media/MediaProvider.java
index 5bb4070..8fa70f3 100644
--- a/src/com/android/providers/media/MediaProvider.java
+++ b/src/com/android/providers/media/MediaProvider.java
@@ -5766,10 +5766,12 @@
extras.getParcelable(MediaStore.EXTRA_FILE_DESCRIPTOR);
try {
File file = getFileFromFileDescriptor(inputPfd);
- boolean supportsTranscode = mTranscodeHelper.supportsTranscode(file.getPath());
- if (!supportsTranscode) {
- throw new IllegalArgumentException(
- "Input file descriptor is already original");
+ boolean isModernFormat = mTranscodeHelper.isModernFormat(file.getPath());
+ if (!isModernFormat) {
+ // Return an empty bundle instead of throwing an exception in the special
+ // case where the file is not a modern format. This avoids a misleading
+ // warning in android.database.DatabaseUtils#writeExceptionToParcel
+ return new Bundle();
}
FuseDaemon fuseDaemon = getFuseDaemonForFile(file);
diff --git a/src/com/android/providers/media/TranscodeHelper.java b/src/com/android/providers/media/TranscodeHelper.java
index 596db57..e1ffbca 100644
--- a/src/com/android/providers/media/TranscodeHelper.java
+++ b/src/com/android/providers/media/TranscodeHelper.java
@@ -792,7 +792,19 @@
return isHevc(mimeType) || isHdr10Plus(colorStandard, colorTransfer);
}
- public boolean supportsTranscode(String path) {
+ public boolean isModernFormat(String filePath) {
+ if (supportsTranscode(filePath)) {
+ Pair<Integer, Long> result = getFileFlagsAndDurationMs(filePath);
+ int fileFlags = result.first;
+ if (fileFlags != 0) {
+ // File is HEVC or HDR
+ return true;
+ }
+ }
+ return false;
+ }
+
+ public static boolean supportsTranscode(String path) {
File file = new File(path);
String name = file.getName();
final String cameraRelativePath =