Search in sources :

Example 21 with BaseUrl

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);
}
Also used : BaseUrl(com.google.android.exoplayer2.source.dash.manifest.BaseUrl) Test(org.junit.Test)

Example 22 with BaseUrl

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");
}
Also used : Random(java.util.Random) BaseUrl(com.google.android.exoplayer2.source.dash.manifest.BaseUrl) Test(org.junit.Test)

Example 23 with BaseUrl

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");
}
Also used : BaseUrl(com.google.android.exoplayer2.source.dash.manifest.BaseUrl) Test(org.junit.Test)

Example 24 with BaseUrl

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;
}
Also used : ArrayList(java.util.ArrayList) BaseUrl(com.google.android.exoplayer2.source.dash.manifest.BaseUrl) Nullable(androidx.annotation.Nullable) Pair(android.util.Pair) Nullable(androidx.annotation.Nullable)

Example 25 with 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);
}
Also used : BaseUrl(com.google.android.exoplayer2.source.dash.manifest.BaseUrl)

Aggregations

BaseUrl (com.google.android.exoplayer2.source.dash.manifest.BaseUrl)17 Test (org.junit.Test)14 ArrayList (java.util.ArrayList)9 Nullable (androidx.annotation.Nullable)7 SingleSegmentBase (com.google.android.exoplayer2.source.dash.manifest.SegmentBase.SingleSegmentBase)6 Random (java.util.Random)5 Format (com.google.android.exoplayer2.Format)4 RangedUri (com.google.android.exoplayer2.source.dash.manifest.RangedUri)4 Representation (com.google.android.exoplayer2.source.dash.manifest.Representation)4 Uri (android.net.Uri)3 Pair (android.util.Pair)3 DataSpec (com.google.android.exoplayer2.upstream.DataSpec)3 LoadErrorHandlingPolicy (com.google.android.exoplayer2.upstream.LoadErrorHandlingPolicy)3 SystemClock (android.os.SystemClock)2 ApplicationProvider (androidx.test.core.app.ApplicationProvider)2 AndroidJUnit4 (androidx.test.ext.junit.runners.AndroidJUnit4)2 C (com.google.android.exoplayer2.C)2 PlayerId (com.google.android.exoplayer2.analytics.PlayerId)2 SchemeData (com.google.android.exoplayer2.drm.DrmInitData.SchemeData)2 LoadEventInfo (com.google.android.exoplayer2.source.LoadEventInfo)2