Merge "Fix fd leak while bypassing transcoding in media APIs" into sc-dev
This commit is contained in:
committed by
Android (Google) Code Review
commit
3fefc5c3ed
@@ -1460,15 +1460,15 @@ public final class FileUtils {
|
||||
/** {@hide} */
|
||||
@VisibleForTesting
|
||||
public static ParcelFileDescriptor convertToModernFd(FileDescriptor fd) {
|
||||
try {
|
||||
Context context = AppGlobals.getInitialApplication();
|
||||
if (UserHandle.getAppId(Process.myUid()) == getMediaProviderAppId(context)) {
|
||||
// Never convert modern fd for MediaProvider, because this requires
|
||||
// MediaStore#scanFile and can cause infinite loops when MediaProvider scans
|
||||
return null;
|
||||
}
|
||||
return MediaStore.getOriginalMediaFormatFileDescriptor(context,
|
||||
ParcelFileDescriptor.dup(fd));
|
||||
Context context = AppGlobals.getInitialApplication();
|
||||
if (UserHandle.getAppId(Process.myUid()) == getMediaProviderAppId(context)) {
|
||||
// Never convert modern fd for MediaProvider, because this requires
|
||||
// MediaStore#scanFile and can cause infinite loops when MediaProvider scans
|
||||
return null;
|
||||
}
|
||||
|
||||
try (ParcelFileDescriptor dupFd = ParcelFileDescriptor.dup(fd)) {
|
||||
return MediaStore.getOriginalMediaFormatFileDescriptor(context, dupFd);
|
||||
} catch (Exception e) {
|
||||
Log.d(TAG, "Failed to convert to modern format file descriptor", e);
|
||||
return null;
|
||||
|
||||
@@ -1573,6 +1573,9 @@ public class ExifInterface {
|
||||
if (isFdDuped) {
|
||||
closeFileDescriptor(fileDescriptor);
|
||||
}
|
||||
if (modernFd != null) {
|
||||
modernFd.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2554,12 +2557,13 @@ public class ExifInterface {
|
||||
|
||||
private void initForFilename(String filename) throws IOException {
|
||||
FileInputStream in = null;
|
||||
ParcelFileDescriptor modernFd = null;
|
||||
mAssetInputStream = null;
|
||||
mFilename = filename;
|
||||
mIsInputStream = false;
|
||||
try {
|
||||
in = new FileInputStream(filename);
|
||||
ParcelFileDescriptor modernFd = FileUtils.convertToModernFd(in.getFD());
|
||||
modernFd = FileUtils.convertToModernFd(in.getFD());
|
||||
if (modernFd != null) {
|
||||
closeQuietly(in);
|
||||
in = new FileInputStream(modernFd.getFileDescriptor());
|
||||
@@ -2570,6 +2574,9 @@ public class ExifInterface {
|
||||
loadAttributes(in);
|
||||
} finally {
|
||||
closeQuietly(in);
|
||||
if (modernFd != null) {
|
||||
modernFd.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -36,6 +36,7 @@ import android.os.IBinder;
|
||||
import android.os.ParcelFileDescriptor;
|
||||
import android.os.SystemProperties;
|
||||
import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
|
||||
import java.io.FileDescriptor;
|
||||
import java.io.FileInputStream;
|
||||
@@ -52,6 +53,8 @@ import java.util.Map;
|
||||
* frame and meta data from an input media file.
|
||||
*/
|
||||
public class MediaMetadataRetriever implements AutoCloseable {
|
||||
private static final String TAG = "MediaMetadataRetriever";
|
||||
|
||||
// borrowed from ExoPlayer
|
||||
private static final String[] STANDARD_GENRES = new String[] {
|
||||
// These are the official ID3v1 genres.
|
||||
@@ -301,11 +304,15 @@ public class MediaMetadataRetriever implements AutoCloseable {
|
||||
*/
|
||||
public void setDataSource(FileDescriptor fd, long offset, long length)
|
||||
throws IllegalArgumentException {
|
||||
ParcelFileDescriptor modernFd = FileUtils.convertToModernFd(fd);
|
||||
if (modernFd == null) {
|
||||
_setDataSource(fd, offset, length);
|
||||
} else {
|
||||
_setDataSource(modernFd.getFileDescriptor(), offset, length);
|
||||
|
||||
try (ParcelFileDescriptor modernFd = FileUtils.convertToModernFd(fd)) {
|
||||
if (modernFd == null) {
|
||||
_setDataSource(fd, offset, length);
|
||||
} else {
|
||||
_setDataSource(modernFd.getFileDescriptor(), offset, length);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
Log.w(TAG, "Ignoring IO error while setting data source", e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1271,11 +1271,14 @@ public class MediaPlayer extends PlayerBase
|
||||
*/
|
||||
public void setDataSource(FileDescriptor fd, long offset, long length)
|
||||
throws IOException, IllegalArgumentException, IllegalStateException {
|
||||
ParcelFileDescriptor modernFd = FileUtils.convertToModernFd(fd);
|
||||
if (modernFd == null) {
|
||||
_setDataSource(fd, offset, length);
|
||||
} else {
|
||||
_setDataSource(modernFd.getFileDescriptor(), offset, length);
|
||||
try (ParcelFileDescriptor modernFd = FileUtils.convertToModernFd(fd)) {
|
||||
if (modernFd == null) {
|
||||
_setDataSource(fd, offset, length);
|
||||
} else {
|
||||
_setDataSource(modernFd.getFileDescriptor(), offset, length);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
Log.w(TAG, "Ignoring IO error while setting data source", e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user