Avoid querying the db during getoriginalMediaFormatFileDescriptor
Querying the db can ANR the system as per the following trace:
at android.provider.MediaStore.getOriginalMediaFormatFileDescriptor
at android.media.MediaPlayer.setDataSource
at android.media.Ringtone.setUri
at android.media.Ringtone.setAudioAttributes
To avoid this, instead of checking if the file is a modern format,
we instead check if the file supports transcoding. This can be
misleading for legacy video files in DCIM/Camera because we'd end up
returning a similar fd as the input fd. This however does not violate
the API contract.
Test: atest TranscodeTest
Bug: 187690359
Change-Id: Id89599fa6a75a81cb3fb825af51e12ff90e00018
diff --git a/src/com/android/providers/media/MediaProvider.java b/src/com/android/providers/media/MediaProvider.java
index e76bde9..7a1c5d0 100644
--- a/src/com/android/providers/media/MediaProvider.java
+++ b/src/com/android/providers/media/MediaProvider.java
@@ -5710,11 +5710,16 @@
extras.getParcelable(MediaStore.EXTRA_FILE_DESCRIPTOR);
try {
File file = getFileFromFileDescriptor(inputPfd);
- boolean isModernFormat = mTranscodeHelper.isModernFormat(file.getPath());
- if (!isModernFormat) {
+ if (!mTranscodeHelper.supportsTranscode(file.getPath())) {
// 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
+ // case where the file does not support transcode. This avoids a misleading
// warning in android.database.DatabaseUtils#writeExceptionToParcel
+ //
+ // Note that we should be checking if a file is a modern format and not just
+ // that it supports transcoding, unfortunately, checking modern format
+ // requires either a db query or media scan which can lead to ANRs if apps
+ // or the system implicitly call this method as part of a
+ // MediaPlayer#setDataSource.
return new Bundle();
}
diff --git a/src/com/android/providers/media/TranscodeHelper.java b/src/com/android/providers/media/TranscodeHelper.java
index 9a74ddc..19efa8d 100644
--- a/src/com/android/providers/media/TranscodeHelper.java
+++ b/src/com/android/providers/media/TranscodeHelper.java
@@ -802,18 +802,6 @@
return isHevc(mimeType) || isHdr10Plus(colorStandard, colorTransfer);
}
- 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();