Skip to content

Commit

Permalink
Load Types (lavalink-devs#116)
Browse files Browse the repository at this point in the history
* add result status enum

* move load result out into its own class

* load changes

* add search result value + logic for it

* document load types

* removed isPlaylist field from docs

* add checks for unknown value

* remove isPlaylist field

* clarify version changes

* add references to pr's
  • Loading branch information
Sam Pritchard authored and freyacodes committed Jun 19, 2018
1 parent 778a007 commit fdfdb04
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 24 deletions.
13 changes: 10 additions & 3 deletions IMPLEMENTATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ The Java client has support for JDA, but can also be adapted to work with other
* Hixie 75

## Significant changes v2.0 -> v3.0
* The response of `/loadtracks` has been completely changed.
* The response of `/loadtracks` has been completely changed (again since the initial v3.0 pre-release).
* Lavalink v3.0 now reports its version as a handshake response header.
`Lavalink-Major-Version` has a value of `3` for v3.0 only. It's missing for any older version.

Expand Down Expand Up @@ -232,7 +232,7 @@ Authorization: youshallnotpass
Response:
```json
{
"isPlaylist": false,
"loadType": "TRACK_LOADED",
"playlistInfo": {},
"tracks": [
{
Expand All @@ -255,7 +255,7 @@ Response:
If the identifier leads to a playlist, `playlistInfo` will contain two properties, `name` and `selectedTrack`
```json
{
"isPlaylist": true,
"loadType": "PLAYLIST_LOADED",
"playlistInfo": {
"name": "Example YouTube Playlist",
"selectedTrack": 3
Expand All @@ -266,6 +266,13 @@ If the identifier leads to a playlist, `playlistInfo` will contain two propertie
}
```

Additionally, in every `/loadtracks` response, a `loadType` property is returned which can be used to judge the response from Lavalink properly. It can be one of the following:
* `TRACK_LOADED` - Returned when a single track is loaded.
* `PLAYLIST_LOADED` - Returned when a playlist is loaded.
* `SEARCH_RESULT` - Returned when a search result is made (i.e `ytsearch: some song`).
* `NO_MATCHES` - Returned if no matches/sources could be found for a given identifier.
* `LOAD_FAILED` - Returned if Lavaplayer failed to load something for some reason.

### Special notes
* When your shard's mainWS connection dies, so does all your lavalink audio connections.
* This also includes resumes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class AudioLoader implements AudioLoadResultHandler {
Expand All @@ -40,9 +39,9 @@ public class AudioLoader implements AudioLoadResultHandler {
private final AudioPlayerManager audioPlayerManager;

private List<AudioTrack> loadedItems;
private boolean isPlaylist = false;
private String playlistName = null;
private Integer selectedTrack = null;
private ResultStatus status = ResultStatus.UNKNOWN;
private boolean used = false;

public AudioLoader(AudioPlayerManager audioPlayerManager) {
Expand All @@ -61,13 +60,16 @@ LoadResult loadSync(String identifier) throws InterruptedException {
this.wait();
}

return new LoadResult(loadedItems, isPlaylist, playlistName, selectedTrack);
if (status == ResultStatus.UNKNOWN)
throw new IllegalStateException("Load Type == UNKNOWN (shouldn't happen!)");
return new LoadResult(loadedItems, playlistName, status, selectedTrack);
}

@Override
public void trackLoaded(AudioTrack audioTrack) {
loadedItems = new ArrayList<>();
loadedItems.add(audioTrack);
status = ResultStatus.TRACK_LOADED;
log.info("Loaded track " + audioTrack.getInfo().title);
synchronized (this) {
this.notify();
Expand All @@ -77,12 +79,12 @@ public void trackLoaded(AudioTrack audioTrack) {
@Override
public void playlistLoaded(AudioPlaylist audioPlaylist) {
if (!audioPlaylist.isSearchResult()) {
isPlaylist = true;
playlistName = audioPlaylist.getName();
selectedTrack = audioPlaylist.getTracks().indexOf(audioPlaylist.getSelectedTrack());
}

log.info("Loaded playlist " + audioPlaylist.getName());
status = audioPlaylist.isSearchResult() ? ResultStatus.SEARCH_RESULT : ResultStatus.PLAYLIST_LOADED;
loadedItems = audioPlaylist.getTracks();
synchronized (this) {
this.notify();
Expand All @@ -92,6 +94,7 @@ public void playlistLoaded(AudioPlaylist audioPlaylist) {
@Override
public void noMatches() {
log.info("No matches found");
status = ResultStatus.NO_MATCHES;
loadedItems = new ArrayList<>();
synchronized (this) {
this.notify();
Expand All @@ -101,24 +104,11 @@ public void noMatches() {
@Override
public void loadFailed(FriendlyException e) {
log.error("Load failed", e);
status = ResultStatus.LOAD_FAILED;
loadedItems = new ArrayList<>();
synchronized (this) {
this.notify();
}
}

}

class LoadResult {
public List<AudioTrack> tracks;
public boolean isPlaylist;
public String playlistName;
public Integer selectedTrack;

public LoadResult(List<AudioTrack> tracks, boolean isPlaylist, String playlistName, Integer selectedTrack) {
this.tracks = Collections.unmodifiableList(tracks);
this.isPlaylist = isPlaylist;
this.playlistName = playlistName;
this.selectedTrack = selectedTrack;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ public String getLoadTracks(HttpServletRequest request, HttpServletResponse resp
playlist.put("name", result.playlistName);
playlist.put("selectedTrack", result.selectedTrack);

json.put("isPlaylist", result.isPlaylist);
json.put("playlistInfo", playlist);
json.put("loadType", result.loadResultType);
json.put("tracks", tracks);

return json.toString();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package lavalink.server.player;

import com.sedmelluq.discord.lavaplayer.track.AudioTrack;

import java.util.Collections;
import java.util.List;

class LoadResult {
public List<AudioTrack> tracks;
public String playlistName;
public ResultStatus loadResultType;
public Integer selectedTrack;

LoadResult(List<AudioTrack> tracks, String playlistName, ResultStatus loadResultType, Integer selectedTrack) {
this.tracks = Collections.unmodifiableList(tracks);
this.playlistName = playlistName;
this.loadResultType = loadResultType;
this.selectedTrack = selectedTrack;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package lavalink.server.player;

public enum ResultStatus {
TRACK_LOADED,
PLAYLIST_LOADED,
SEARCH_RESULT,
NO_MATCHES,
LOAD_FAILED,
UNKNOWN
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Being used in production by FredBoat, Dyno, Rythm, LewdBot, and more.
* Basic authentication

## Changes in 3.0
* Breaking changes to the output of the /loadtracks endpoint https://github.com/Frederikam/Lavalink/pull/91
* Breaking changes to the output of the /loadtracks endpoint. [See PR #91](https://github.com/Frederikam/Lavalink/pull/91) [and PR #116](https://github.com/Frederikam/Lavalink/pull/116).
* The Java client has been made generic. This is a breaking change so please read the documentation.

## Client libraries:
Expand Down

0 comments on commit fdfdb04

Please sign in to comment.