Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix duplicate items in queue causing endless buffering #6712

Merged
merged 3 commits into from
Jul 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -813,7 +813,7 @@ public void selectAndLoadVideo(final int newServiceId,
@NonNull final String newTitle,
@Nullable final PlayQueue newQueue) {
if (isPlayerAvailable() && newQueue != null && playQueue != null
&& !Objects.equals(newQueue.getItem(), playQueue.getItem())) {
&& playQueue.getItem() != null && !playQueue.getItem().getUrl().equals(newUrl)) {
// Preloading can be disabled since playback is surely being replaced.
player.disablePreloadingOfCurrentTrack();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,20 +64,6 @@ private PlayQueueItem(@Nullable final String name, @Nullable final String url,
this.recoveryPosition = RECOVERY_UNSET;
}

@Override
public boolean equals(final Object o) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh my! what a bug xD
Did you check the usages of the method? Maybe someone implemented this on purpose.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Android Studio's "Show usages" yields all of the usages of the equals() method on any object of any class, so that's not much useful. I tried to manually look for usages by checking out the methods in PlayQueue and in Player that use PlayQueueItem comparison and could not find any issue.
The only downside I can think of is that if the same video is queued multiple times in a row, the loading strategy may start to load the current and the next stream contemporarily, but since those streams point to the same url but do not compare equal, they will be loaded twice. But this only happens if two play queue items with the same url start loading contemporarily, because otherwise the caching method prevents reloading something that already was loaded. I don't think this is really a downside.

if (o instanceof PlayQueueItem) {
return url.equals(((PlayQueueItem) o).url);
} else {
return false;
}
}

@Override
public int hashCode() {
return url.hashCode();
}

@NonNull
public String getTitle() {
return title;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.schabi.newpipe.player.playqueue;

import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;

public class PlayQueueItemTest {

public static final String URL = "MY_URL";

@Test
public void equalsMustNotBeOverloaded() {
final PlayQueueItem a = PlayQueueTest.makeItemWithUrl(URL);
final PlayQueueItem b = PlayQueueTest.makeItemWithUrl(URL);
assertEquals(a, a);
assertNotEquals(a, b); // they should compare different even if they have the same data
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.spy;
Expand Down Expand Up @@ -148,6 +149,15 @@ public void outOfBounds() {
assertNull(queue.getItem(-1));
assertNull(queue.getItem(5));
}

@Test
public void itemsAreNotCloned() {
final PlayQueueItem item = makeItemWithUrl("A url");
final PlayQueue playQueue = makePlayQueue(0, Collections.singletonList(item));

// make sure that items are not cloned when added to the queue
assertSame(playQueue.getItem(), item);
}
}

public static class EqualsTests {
Expand All @@ -162,6 +172,14 @@ public void sameStreams() {
assertEquals(queue1, queue2);
}

@Test
public void sameStreamsDifferentIndex() {
final List<PlayQueueItem> streams = Collections.nCopies(5, item1);
final PlayQueue queue1 = makePlayQueue(1, streams);
final PlayQueue queue2 = makePlayQueue(4, streams);
assertEquals(queue1, queue2);
}

@Test
public void sameSizeDifferentItems() {
final List<PlayQueueItem> streams1 = Collections.nCopies(5, item1);
Expand Down