Skip to content

Commit

Permalink
Avoid calling setDataSource twice for videos
Browse files Browse the repository at this point in the history
It's pretty expensive to do, so we're wasting time for videos. If
setDataSource is called with a non-video, it will throw an exception
which in turn will cause us to move on to the next decoder, so the
behavior shouldn't change.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=168390557
  • Loading branch information
sjudd committed Sep 15, 2017
1 parent c2eeb90 commit a84deb3
Showing 1 changed file with 7 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -101,16 +101,10 @@ public VideoBitmapDecoder(BitmapPool bitmapPool) {

@Override
public boolean handles(ParcelFileDescriptor data, Options options) {
MediaMetadataRetriever retriever = factory.build();
try {
retriever.setDataSource(data.getFileDescriptor());
return true;
} catch (RuntimeException e) {
// Throws a generic runtime exception when given invalid data.
return false;
} finally {
retriever.release();
}
// Calling setDataSource is expensive so avoid doing so unless we're actually called.
// For non-videos this isn't any cheaper, but for videos it safes the redundant call and
// 50-100ms.
return true;
}

@Override
Expand All @@ -134,6 +128,9 @@ public Resource<Bitmap> decode(ParcelFileDescriptor resource, int outWidth, int
} else {
result = mediaMetadataRetriever.getFrameAtTime(frameTimeMicros, frameOption);
}
} catch (RuntimeException e) {
// MediaMetadataRetriever APIs throw generic runtime exceptions when given invalid data.
throw new IOException(e);
} finally {
mediaMetadataRetriever.release();
}
Expand Down

0 comments on commit a84deb3

Please sign in to comment.