2012-02-26 21:25:13 +01:00
|
|
|
/*
|
|
|
|
This file is part of Subsonic.
|
|
|
|
|
|
|
|
Subsonic is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Subsonic is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with Subsonic. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
Copyright 2009 (C) Sindre Mehus
|
|
|
|
*/
|
2015-07-26 18:15:07 +02:00
|
|
|
package org.moire.ultrasonic.util;
|
2012-02-26 21:25:13 +01:00
|
|
|
|
2020-09-18 09:37:19 +02:00
|
|
|
import org.moire.ultrasonic.data.ActiveServerProvider;
|
2015-07-26 18:15:07 +02:00
|
|
|
import org.moire.ultrasonic.domain.MusicDirectory;
|
|
|
|
import org.moire.ultrasonic.service.MusicService;
|
|
|
|
import org.moire.ultrasonic.service.MusicServiceFactory;
|
2012-02-26 21:25:13 +01:00
|
|
|
|
2013-12-04 07:36:02 +01:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.concurrent.Executors;
|
|
|
|
import java.util.concurrent.ScheduledExecutorService;
|
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
2021-05-21 23:08:28 +02:00
|
|
|
import timber.log.Timber;
|
|
|
|
|
2012-02-26 21:25:13 +01:00
|
|
|
/**
|
|
|
|
* @author Sindre Mehus
|
|
|
|
* @version $Id$
|
|
|
|
*/
|
2013-12-04 07:36:02 +01:00
|
|
|
public class ShufflePlayBuffer
|
|
|
|
{
|
|
|
|
private static final int CAPACITY = 50;
|
|
|
|
private static final int REFILL_THRESHOLD = 40;
|
|
|
|
|
2021-02-14 15:55:16 +01:00
|
|
|
private final List<MusicDirectory.Entry> buffer = new ArrayList<>();
|
2020-06-23 18:40:44 +02:00
|
|
|
private ScheduledExecutorService executorService;
|
2013-12-04 07:36:02 +01:00
|
|
|
private int currentServer;
|
|
|
|
|
2020-06-23 18:40:44 +02:00
|
|
|
public boolean isEnabled = false;
|
|
|
|
|
2021-05-21 23:08:28 +02:00
|
|
|
public ShufflePlayBuffer()
|
2013-12-04 07:36:02 +01:00
|
|
|
{
|
2020-06-23 18:40:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public void onCreate()
|
|
|
|
{
|
2013-12-04 07:36:02 +01:00
|
|
|
executorService = Executors.newSingleThreadScheduledExecutor();
|
2021-05-21 23:08:28 +02:00
|
|
|
Runnable runnable = this::refill;
|
2013-12-04 07:36:02 +01:00
|
|
|
executorService.scheduleWithFixedDelay(runnable, 1, 10, TimeUnit.SECONDS);
|
2020-09-30 14:47:59 +02:00
|
|
|
Timber.i("ShufflePlayBuffer created");
|
2013-12-04 07:36:02 +01:00
|
|
|
}
|
|
|
|
|
2020-06-23 18:40:44 +02:00
|
|
|
public void onDestroy()
|
|
|
|
{
|
|
|
|
executorService.shutdown();
|
2020-09-30 14:47:59 +02:00
|
|
|
Timber.i("ShufflePlayBuffer destroyed");
|
2020-06-23 18:40:44 +02:00
|
|
|
}
|
|
|
|
|
2013-12-04 07:36:02 +01:00
|
|
|
public List<MusicDirectory.Entry> get(int size)
|
|
|
|
{
|
|
|
|
clearBufferIfNecessary();
|
|
|
|
|
2021-02-14 15:55:16 +01:00
|
|
|
List<MusicDirectory.Entry> result = new ArrayList<>(size);
|
2013-12-04 07:36:02 +01:00
|
|
|
synchronized (buffer)
|
|
|
|
{
|
|
|
|
while (!buffer.isEmpty() && result.size() < size)
|
|
|
|
{
|
|
|
|
result.add(buffer.remove(buffer.size() - 1));
|
|
|
|
}
|
|
|
|
}
|
2020-09-30 14:47:59 +02:00
|
|
|
Timber.i("Taking %d songs from shuffle play buffer. %d remaining.", result.size(), buffer.size());
|
2013-12-04 07:36:02 +01:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void refill()
|
|
|
|
{
|
2020-06-23 18:40:44 +02:00
|
|
|
if (!isEnabled) return;
|
2013-12-04 07:36:02 +01:00
|
|
|
|
|
|
|
// Check if active server has changed.
|
|
|
|
clearBufferIfNecessary();
|
|
|
|
|
2021-05-21 23:03:42 +02:00
|
|
|
if (buffer.size() > REFILL_THRESHOLD || (!Util.isNetworkConnected() && !ActiveServerProvider.Companion.isOffline()))
|
2013-12-04 07:36:02 +01:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2021-05-09 10:38:03 +02:00
|
|
|
MusicService service = MusicServiceFactory.getMusicService();
|
2013-12-04 07:36:02 +01:00
|
|
|
int n = CAPACITY - buffer.size();
|
2021-05-11 12:57:29 +02:00
|
|
|
MusicDirectory songs = service.getRandomSongs(n);
|
2013-12-04 07:36:02 +01:00
|
|
|
|
|
|
|
synchronized (buffer)
|
|
|
|
{
|
2021-11-26 17:03:33 +01:00
|
|
|
buffer.addAll(songs.getTracks());
|
|
|
|
Timber.i("Refilled shuffle play buffer with %d songs.", songs.getTracks().size());
|
2013-12-04 07:36:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception x)
|
|
|
|
{
|
2020-09-30 14:47:59 +02:00
|
|
|
Timber.w(x, "Failed to refill shuffle play buffer.");
|
2013-12-04 07:36:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void clearBufferIfNecessary()
|
|
|
|
{
|
|
|
|
synchronized (buffer)
|
|
|
|
{
|
2021-05-09 10:25:04 +02:00
|
|
|
if (currentServer != ActiveServerProvider.Companion.getActiveServerId())
|
2013-12-04 07:36:02 +01:00
|
|
|
{
|
2021-05-09 10:25:04 +02:00
|
|
|
currentServer = ActiveServerProvider.Companion.getActiveServerId();
|
2013-12-04 07:36:02 +01:00
|
|
|
buffer.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-05-16 09:59:55 +02:00
|
|
|
}
|