Skip to content

Commit

Permalink
Fix the fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Thompson3142 committed Oct 1, 2024
1 parent 9844d75 commit 03accb0
Showing 1 changed file with 18 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ private void generateDataFrom(final Frameset frameset, final UUID updateRequestI
final Stopwatch sw = Log.isLoggable(TAG, Log.DEBUG) ? Stopwatch.createStarted() : null;

int currentPosMs = 0;
int pos = 1;

final int urlFrameCount = frameset.getFramesPerPageX() * frameset.getFramesPerPageY();

Expand All @@ -117,14 +118,6 @@ private void generateDataFrom(final Frameset frameset, final UUID updateRequestI
// get the bitmap
final Bitmap srcBitMap = getBitMapFrom(url);

// It can happen, that the original bitmap could not be downloaded
// In such a case - we don't want a NullPointer - simply return null
// Recycled bitmaps can also not be created, so we also need to check for that
if (srcBitMap == null || srcBitMap.isRecycled()) {
Log.e(TAG, "Failed to retrieve or use bitmap from url: " + url);
continue;
}

// The data is not added directly to "seekbarPreviewData" due to
// concurrency and checks for "updateRequestIdentifier"
final var generatedDataForUrl = new SparseArrayCompat<Supplier<Bitmap>>(urlFrameCount);
Expand All @@ -133,19 +126,34 @@ private void generateDataFrom(final Frameset frameset, final UUID updateRequestI
// foreach frame in the returned bitmap
for (int i = 0; i < urlFrameCount; i++) {
// Frames outside the video length are skipped
if (i >= frameset.getTotalCount()) {
if (pos > frameset.getTotalCount()) {
break;
}

// Get the bounds where the frame is found
final int[] bounds = frameset.getFrameBoundsAt(currentPosMs);
generatedDataForUrl.put(currentPosMs, () -> {
// It can happen, that the original bitmap could not be downloaded
// In such a case - we don't want a NullPointer - simply return null;
// Recycled bitmaps are also unusable here so we should check for that too
if (srcBitMap == null || srcBitMap.isRecycled()) {
return null;
}

// Cut out the corresponding bitmap form the "srcBitMap"
return Bitmap.createBitmap(srcBitMap, bounds[1], bounds[2],
final Bitmap cutOutBitmap = Bitmap.createBitmap(srcBitMap, bounds[1], bounds[2],
frameset.getFrameWidth(), frameset.getFrameHeight());

// We need to copy the bitmap to create a new instance since createBitmap
// allows itself to return the original object that is was created with
// this leads to recycled bitmaps being returned (if they are identical)
// Reference: https://stackoverflow.com/a/23683075 + first comment
// Fixes: https://github.com/TeamNewPipe/NewPipe/issues/11461
return cutOutBitmap.copy(cutOutBitmap.getConfig(), true);
});

currentPosMs += frameset.getDurationPerFrame();
pos++;
}

// Check if we are still the latest request
Expand Down

0 comments on commit 03accb0

Please sign in to comment.