use of com.google.android.exoplayer2.source.dash.manifest.BaseUrl in project ExoPlayer by google.
the class BaseUrlExclusionListTest method selectBaseUrl_twiceTheSamePriorityExcluded_correctExpirationDuration.
@Test
public void selectBaseUrl_twiceTheSamePriorityExcluded_correctExpirationDuration() {
List<BaseUrl> baseUrls = ImmutableList.of(new BaseUrl(/* url= */
"a", /* serviceLocation= */
"a", /* priority= */
1, /* weight= */
1), new BaseUrl(/* url= */
"b", /* serviceLocation= */
"b", /* priority= */
1, /* weight= */
1), new BaseUrl(/* url= */
"c", /* serviceLocation= */
"c", /* priority= */
2, /* weight= */
1));
BaseUrlExclusionList baseUrlExclusionList = new BaseUrlExclusionList();
// Exclude priority 1.
baseUrlExclusionList.exclude(baseUrls.get(0), 5000);
// Exclude priority 1 again which increases the exclusion duration.
baseUrlExclusionList.exclude(baseUrls.get(1), 10000);
assertThat(baseUrlExclusionList.selectBaseUrl(baseUrls).url).isEqualTo("c");
ShadowSystemClock.advanceBy(Duration.ofMillis(9999));
assertThat(baseUrlExclusionList.getPriorityCountAfterExclusion(baseUrls)).isEqualTo(1);
ShadowSystemClock.advanceBy(Duration.ofMillis(1));
assertThat(baseUrlExclusionList.getPriorityCountAfterExclusion(baseUrls)).isEqualTo(2);
}
use of com.google.android.exoplayer2.source.dash.manifest.BaseUrl in project ExoPlayer by google.
the class BaseUrlExclusionListTest method selectBaseUrl_excludeByPriority_excludesAllBaseUrlsOfSamePriority.
@Test
public void selectBaseUrl_excludeByPriority_excludesAllBaseUrlsOfSamePriority() {
Random mockRandom = mock(Random.class);
when(mockRandom.nextInt(anyInt())).thenReturn(0);
BaseUrlExclusionList baseUrlExclusionList = new BaseUrlExclusionList(mockRandom);
List<BaseUrl> baseUrls = ImmutableList.of(new BaseUrl(/* url= */
"a", /* serviceLocation= */
"a", /* priority= */
1, /* weight= */
1), new BaseUrl(/* url= */
"b", /* serviceLocation= */
"b", /* priority= */
1, /* weight= */
99), new BaseUrl(/* url= */
"c", /* serviceLocation= */
"c", /* priority= */
2, /* weight= */
1));
baseUrlExclusionList.exclude(baseUrls.get(0), 5000);
ShadowSystemClock.advanceBy(Duration.ofMillis(4999));
assertThat(baseUrlExclusionList.selectBaseUrl(baseUrls).url).isEqualTo("c");
ShadowSystemClock.advanceBy(Duration.ofMillis(1));
assertThat(baseUrlExclusionList.selectBaseUrl(baseUrls).url).isEqualTo("a");
}
use of com.google.android.exoplayer2.source.dash.manifest.BaseUrl in project ExoPlayer by google.
the class BaseUrlExclusionListTest method selectBaseUrl_priorityUnset_isNotExcluded.
@Test
public void selectBaseUrl_priorityUnset_isNotExcluded() {
BaseUrlExclusionList baseUrlExclusionList = new BaseUrlExclusionList();
ImmutableList<BaseUrl> baseUrls = ImmutableList.of(new BaseUrl(/* url= */
"a-1", /* serviceLocation= */
"a", BaseUrl.PRIORITY_UNSET, /* weight= */
1), new BaseUrl(/* url= */
"a-2", /* serviceLocation= */
"a", BaseUrl.PRIORITY_UNSET, /* weight= */
1), new BaseUrl(/* url= */
"b", /* serviceLocation= */
"b", BaseUrl.PRIORITY_UNSET, /* weight= */
1));
baseUrlExclusionList.exclude(baseUrls.get(0), 10_000);
assertThat(baseUrlExclusionList.selectBaseUrl(baseUrls).serviceLocation).isEqualTo("b");
}
use of com.google.android.exoplayer2.source.dash.manifest.BaseUrl in project ExoPlayer by google.
the class BaseUrlExclusionList method selectBaseUrl.
/**
* Selects the base URL to use from the given list.
*
* <p>The list is reduced by service location and priority of base URLs that have been passed to
* {@link #exclude(BaseUrl, long)}. The base URL to use is then selected from the remaining base
* URLs by priority and weight.
*
* @param baseUrls The list of {@link BaseUrl base URLs} to select from.
* @return The selected base URL after exclusion or null if all elements have been excluded.
*/
@Nullable
public BaseUrl selectBaseUrl(List<BaseUrl> baseUrls) {
List<BaseUrl> includedBaseUrls = applyExclusions(baseUrls);
if (includedBaseUrls.size() < 2) {
return Iterables.getFirst(includedBaseUrls, /* defaultValue= */
null);
}
// Sort by priority and service location to make the sort order of the candidates deterministic.
Collections.sort(includedBaseUrls, BaseUrlExclusionList::compareBaseUrl);
// Get candidates of the lowest priority from the head of the sorted list.
List<Pair<String, Integer>> candidateKeys = new ArrayList<>();
int lowestPriority = includedBaseUrls.get(0).priority;
for (int i = 0; i < includedBaseUrls.size(); i++) {
BaseUrl baseUrl = includedBaseUrls.get(i);
if (lowestPriority != baseUrl.priority) {
if (candidateKeys.size() == 1) {
// Only a single candidate of lowest priority; no choice.
return includedBaseUrls.get(0);
}
break;
}
candidateKeys.add(new Pair<>(baseUrl.serviceLocation, baseUrl.weight));
}
// Check whether selection has already been taken.
@Nullable BaseUrl baseUrl = selectionsTaken.get(candidateKeys);
if (baseUrl == null) {
// Weighted random selection from multiple candidates of the same priority.
baseUrl = selectWeighted(includedBaseUrls.subList(0, candidateKeys.size()));
// Remember the selection taken for later.
selectionsTaken.put(candidateKeys, baseUrl);
}
return baseUrl;
}
use of com.google.android.exoplayer2.source.dash.manifest.BaseUrl in project ExoPlayer by google.
the class BaseUrlExclusionList method selectWeighted.
private BaseUrl selectWeighted(List<BaseUrl> candidates) {
int totalWeight = 0;
for (int i = 0; i < candidates.size(); i++) {
totalWeight += candidates.get(i).weight;
}
int randomChoice = random.nextInt(/* bound= */
totalWeight);
totalWeight = 0;
for (int i = 0; i < candidates.size(); i++) {
BaseUrl baseUrl = candidates.get(i);
totalWeight += baseUrl.weight;
if (randomChoice < totalWeight) {
return baseUrl;
}
}
return Iterables.getLast(candidates);
}
Aggregations