|
13 | 13 | import com.sedmelluq.lava.common.tools.ExecutorTools;
|
14 | 14 | import org.apache.http.client.config.RequestConfig;
|
15 | 15 | import org.apache.http.impl.client.HttpClientBuilder;
|
| 16 | +import org.jetbrains.annotations.Nullable; |
16 | 17 | import org.slf4j.Logger;
|
17 | 18 | import org.slf4j.LoggerFactory;
|
18 | 19 |
|
@@ -148,15 +149,23 @@ public List<AudioSourceManager> getSourceManagers() {
|
148 | 149 | return Collections.unmodifiableList(sourceManagers);
|
149 | 150 | }
|
150 | 151 |
|
| 152 | + @Override |
| 153 | + public @Nullable AudioItem loadItemSync(AudioReference reference) { |
| 154 | + AudioItem item = checkSourcesForItem(reference); |
| 155 | + if (item == null) { |
| 156 | + log.debug("No matches for track with identifier {}.", reference.identifier); |
| 157 | + } |
| 158 | + |
| 159 | + return item; |
| 160 | + } |
| 161 | + |
151 | 162 | @Override
|
152 | 163 | public void loadItemSync(final AudioReference reference, final AudioLoadResultHandler resultHandler) {
|
153 | 164 | boolean[] reported = new boolean[1];
|
154 | 165 |
|
155 | 166 | try {
|
156 |
| - if (!checkSourcesForItem(reference, resultHandler, reported)) { |
157 |
| - log.debug("No matches for track with identifier {}.", reference.identifier); |
158 |
| - resultHandler.noMatches(); |
159 |
| - } |
| 167 | + AudioItem item = loadItemSync(reference); |
| 168 | + submitItemToResultHandler(item, resultHandler, reported); |
160 | 169 | } catch (Throwable throwable) {
|
161 | 170 | if (reported[0]) {
|
162 | 171 | log.warn("Load result handler for {} threw an exception", reference.identifier, throwable);
|
@@ -387,40 +396,57 @@ public void setItemLoaderThreadPoolSize(int poolSize) {
|
387 | 396 | trackInfoExecutorService.setMaximumPoolSize(poolSize);
|
388 | 397 | }
|
389 | 398 |
|
390 |
| - private boolean checkSourcesForItem(AudioReference reference, AudioLoadResultHandler resultHandler, boolean[] reported) { |
| 399 | + private void submitItemToResultHandler(AudioItem item, AudioLoadResultHandler handler, boolean[] reported) { |
| 400 | + if (item == null) { |
| 401 | + reported[0] = true; |
| 402 | + handler.noMatches(); |
| 403 | + } else if (item instanceof AudioTrack) { |
| 404 | + reported[0] = true; |
| 405 | + handler.trackLoaded((AudioTrack) item); |
| 406 | + } else if (item instanceof AudioPlaylist) { |
| 407 | + reported[0] = true; |
| 408 | + handler.playlistLoaded((AudioPlaylist) item); |
| 409 | + } else { |
| 410 | + log.warn("Cannot submit unknown item to result handler: {}", item); |
| 411 | + } |
| 412 | + } |
| 413 | + |
| 414 | + /** |
| 415 | + * Attempts to load the provided {@link AudioReference} using the sources registered with this {@link AudioPlayerManager}. |
| 416 | + * Unlike {@link #checkSourcesForItemOnce} this method attempts to follow any returned redirects. |
| 417 | + */ |
| 418 | + @Nullable |
| 419 | + private AudioItem checkSourcesForItem(AudioReference reference) { |
391 | 420 | AudioReference currentReference = reference;
|
392 | 421 |
|
393 | 422 | for (int redirects = 0; redirects < MAXIMUM_LOAD_REDIRECTS && currentReference.identifier != null; redirects++) {
|
394 |
| - AudioItem item = checkSourcesForItemOnce(currentReference, resultHandler, reported); |
395 |
| - if (item == null) { |
396 |
| - return false; |
397 |
| - } else if (!(item instanceof AudioReference)) { |
398 |
| - return true; |
| 423 | + AudioItem item = checkSourcesForItemOnce(currentReference); |
| 424 | + if (item instanceof AudioReference) { |
| 425 | + currentReference = (AudioReference) item; |
| 426 | + continue; |
399 | 427 | }
|
400 |
| - currentReference = (AudioReference) item; |
| 428 | + |
| 429 | + return item; |
401 | 430 | }
|
402 | 431 |
|
403 |
| - return false; |
| 432 | + return null; |
404 | 433 | }
|
405 | 434 |
|
406 |
| - private AudioItem checkSourcesForItemOnce(AudioReference reference, AudioLoadResultHandler resultHandler, boolean[] reported) { |
| 435 | + @Nullable |
| 436 | + private AudioItem checkSourcesForItemOnce(AudioReference reference) { |
407 | 437 | for (AudioSourceManager sourceManager : sourceManagers) {
|
408 | 438 | if (reference.containerDescriptor != null && !(sourceManager instanceof ProbingAudioSourceManager)) {
|
409 | 439 | continue;
|
410 | 440 | }
|
411 | 441 |
|
412 | 442 | AudioItem item = sourceManager.loadItem(this, reference);
|
413 |
| - |
414 | 443 | if (item != null) {
|
415 | 444 | if (item instanceof AudioTrack) {
|
416 | 445 | log.debug("Loaded a track with identifier {} using {}.", reference.identifier, sourceManager.getClass().getSimpleName());
|
417 |
| - reported[0] = true; |
418 |
| - resultHandler.trackLoaded((AudioTrack) item); |
419 | 446 | } else if (item instanceof AudioPlaylist) {
|
420 | 447 | log.debug("Loaded a playlist with identifier {} using {}.", reference.identifier, sourceManager.getClass().getSimpleName());
|
421 |
| - reported[0] = true; |
422 |
| - resultHandler.playlistLoaded((AudioPlaylist) item); |
423 | 448 | }
|
| 449 | + |
424 | 450 | return item;
|
425 | 451 | }
|
426 | 452 | }
|
|
0 commit comments