Skip to content

Commit

Permalink
Fix track selection nullability issues.
Browse files Browse the repository at this point in the history
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=199266768
  • Loading branch information
tonihei authored and ojw28 committed Jun 5, 2018
1 parent 6a82f99 commit 3c6ca19
Show file tree
Hide file tree
Showing 12 changed files with 130 additions and 96 deletions.
1 change: 1 addition & 0 deletions library/core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ android {
dependencies {
implementation 'com.android.support:support-annotations:' + supportLibraryVersion
compileOnly 'org.checkerframework:checker-qual:' + checkerframeworkVersion
compileOnly 'org.checkerframework:checker-compat-qual:' + checkerframeworkVersion
androidTestImplementation 'com.google.dexmaker:dexmaker:' + dexmakerVersion
androidTestImplementation 'com.google.dexmaker:dexmaker-mockito:' + dexmakerVersion
androidTestImplementation 'com.google.truth:truth:' + truthVersion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.google.android.exoplayer2.trackselection;

import android.support.annotation.Nullable;
import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.Format;
import com.google.android.exoplayer2.source.TrackGroup;
Expand Down Expand Up @@ -242,9 +243,11 @@ public AdaptiveTrackSelection(
this.minTimeBetweenBufferReevaluationMs = minTimeBetweenBufferReevaluationMs;
this.clock = clock;
playbackSpeed = 1f;
selectedIndex = determineIdealSelectedIndex(Long.MIN_VALUE);
reason = C.SELECTION_REASON_INITIAL;
lastBufferEvaluationMs = C.TIME_UNSET;
@SuppressWarnings("nullness:method.invocation.invalid")
int selectedIndex = determineIdealSelectedIndex(Long.MIN_VALUE);
this.selectedIndex = selectedIndex;
}

@Override
Expand Down Expand Up @@ -301,7 +304,7 @@ public int getSelectionReason() {
}

@Override
public Object getSelectionData() {
public @Nullable Object getSelectionData() {
return null;
}

Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.google.android.exoplayer2.trackselection;

import android.support.annotation.Nullable;
import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.source.TrackGroup;
import com.google.android.exoplayer2.util.Assertions;
Expand All @@ -30,7 +31,7 @@ public final class FixedTrackSelection extends BaseTrackSelection {
public static final class Factory implements TrackSelection.Factory {

private final int reason;
private final Object data;
private final @Nullable Object data;

public Factory() {
this.reason = C.SELECTION_REASON_UNKNOWN;
Expand All @@ -41,7 +42,7 @@ public Factory() {
* @param reason A reason for the track selection.
* @param data Optional data associated with the track selection.
*/
public Factory(int reason, Object data) {
public Factory(int reason, @Nullable Object data) {
this.reason = reason;
this.data = data;
}
Expand All @@ -51,11 +52,10 @@ public FixedTrackSelection createTrackSelection(TrackGroup group, int... tracks)
Assertions.checkArgument(tracks.length == 1);
return new FixedTrackSelection(group, tracks[0], reason, data);
}

}

private final int reason;
private final Object data;
private final @Nullable Object data;

/**
* @param group The {@link TrackGroup}. Must not be null.
Expand All @@ -71,7 +71,7 @@ public FixedTrackSelection(TrackGroup group, int track) {
* @param reason A reason for the track selection.
* @param data Optional data associated with the track selection.
*/
public FixedTrackSelection(TrackGroup group, int track, int reason, Object data) {
public FixedTrackSelection(TrackGroup group, int track, int reason, @Nullable Object data) {
super(group, track);
this.reason = reason;
this.data = data;
Expand All @@ -94,7 +94,7 @@ public int getSelectionReason() {
}

@Override
public Object getSelectionData() {
public @Nullable Object getSelectionData() {
return data;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package com.google.android.exoplayer2.trackselection;

import android.support.annotation.IntDef;
import android.support.annotation.Nullable;
import android.util.Pair;
import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.ExoPlaybackException;
Expand All @@ -28,6 +29,7 @@
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.Arrays;
import org.checkerframework.checker.nullness.compatqual.NullableType;

/**
* Base class for {@link TrackSelector}s that first establish a mapping between {@link TrackGroup}s
Expand Down Expand Up @@ -301,13 +303,13 @@ public TrackGroupArray getUnmappedTrackGroups() {

}

private MappedTrackInfo currentMappedTrackInfo;
private @Nullable MappedTrackInfo currentMappedTrackInfo;

/**
* Returns the mapping information for the currently active track selection, or null if no
* selection is currently active.
*/
public final MappedTrackInfo getCurrentMappedTrackInfo() {
public final @Nullable MappedTrackInfo getCurrentMappedTrackInfo() {
return currentMappedTrackInfo;
}

Expand Down Expand Up @@ -357,17 +359,19 @@ public final TrackSelectorResult selectTracks(RendererCapabilities[] rendererCap
int[] rendererTrackTypes = new int[rendererCapabilities.length];
for (int i = 0; i < rendererCapabilities.length; i++) {
int rendererTrackGroupCount = rendererTrackGroupCounts[i];
rendererTrackGroupArrays[i] = new TrackGroupArray(
Arrays.copyOf(rendererTrackGroups[i], rendererTrackGroupCount));
rendererFormatSupports[i] = Arrays.copyOf(rendererFormatSupports[i], rendererTrackGroupCount);
rendererTrackGroupArrays[i] =
new TrackGroupArray(
Util.nullSafeArrayCopy(rendererTrackGroups[i], rendererTrackGroupCount));
rendererFormatSupports[i] =
Util.nullSafeArrayCopy(rendererFormatSupports[i], rendererTrackGroupCount);
rendererTrackTypes[i] = rendererCapabilities[i].getTrackType();
}

// Create a track group array for track groups not mapped to a renderer.
int unmappedTrackGroupCount = rendererTrackGroupCounts[rendererCapabilities.length];
TrackGroupArray unmappedTrackGroupArray =
new TrackGroupArray(
Arrays.copyOf(
Util.nullSafeArrayCopy(
rendererTrackGroups[rendererCapabilities.length], unmappedTrackGroupCount));

// Package up the track information and selections.
Expand All @@ -379,7 +383,7 @@ public final TrackSelectorResult selectTracks(RendererCapabilities[] rendererCap
rendererFormatSupports,
unmappedTrackGroupArray);

Pair<RendererConfiguration[], TrackSelection[]> result =
Pair<@NullableType RendererConfiguration[], @NullableType TrackSelection[]> result =
selectTracks(
mappedTrackInfo, rendererFormatSupports, rendererMixedMimeTypeAdaptationSupports);
return new TrackSelectorResult(result.first, result.second, mappedTrackInfo);
Expand All @@ -399,11 +403,12 @@ public final TrackSelectorResult selectTracks(RendererCapabilities[] rendererCap
* RendererCapabilities#getTrackType()} is {@link C#TRACK_TYPE_NONE}.
* @throws ExoPlaybackException If an error occurs while selecting the tracks.
*/
protected abstract Pair<RendererConfiguration[], TrackSelection[]> selectTracks(
MappedTrackInfo mappedTrackInfo,
int[][][] rendererFormatSupports,
int[] rendererMixedMimeTypeAdaptationSupport)
throws ExoPlaybackException;
protected abstract Pair<@NullableType RendererConfiguration[], @NullableType TrackSelection[]>
selectTracks(
MappedTrackInfo mappedTrackInfo,
int[][][] rendererFormatSupports,
int[] rendererMixedMimeTypeAdaptationSupport)
throws ExoPlaybackException;

/**
* Finds the renderer to which the provided {@link TrackGroup} should be mapped.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package com.google.android.exoplayer2.trackselection;

import android.os.SystemClock;
import android.support.annotation.Nullable;
import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.source.TrackGroup;
import java.util.Random;
Expand Down Expand Up @@ -47,7 +48,6 @@ public Factory(int seed) {
public RandomTrackSelection createTrackSelection(TrackGroup group, int... tracks) {
return new RandomTrackSelection(group, tracks, random);
}

}

private final Random random;
Expand Down Expand Up @@ -123,7 +123,7 @@ public int getSelectionReason() {
}

@Override
public Object getSelectionData() {
public @Nullable Object getSelectionData() {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.google.android.exoplayer2.trackselection;

import android.support.annotation.Nullable;
import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.Format;
import com.google.android.exoplayer2.source.TrackGroup;
Expand Down Expand Up @@ -129,10 +130,8 @@ interface Factory {
*/
int getSelectionReason();

/**
* Returns optional data associated with the current track selection.
*/
Object getSelectionData();
/** Returns optional data associated with the current track selection. */
@Nullable Object getSelectionData();

// Adaptation.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,21 @@

import android.support.annotation.Nullable;
import java.util.Arrays;
import org.checkerframework.checker.nullness.compatqual.NullableType;

/** An array of {@link TrackSelection}s. */
public final class TrackSelectionArray {

/** The length of this array. */
public final int length;

private final TrackSelection[] trackSelections;
private final @NullableType TrackSelection[] trackSelections;

// Lazily initialized hashcode.
private int hashCode;

/**
* @param trackSelections The selections. Must not be null, but may contain null elements.
*/
public TrackSelectionArray(TrackSelection... trackSelections) {
/** @param trackSelections The selections. Must not be null, but may contain null elements. */
public TrackSelectionArray(@NullableType TrackSelection... trackSelections) {
this.trackSelections = trackSelections;
this.length = trackSelections.length;
}
Expand All @@ -43,14 +42,12 @@ public TrackSelectionArray(TrackSelection... trackSelections) {
* @param index The index of the selection.
* @return The selection.
*/
public TrackSelection get(int index) {
public @Nullable TrackSelection get(int index) {
return trackSelections[index];
}

/**
* Returns the selections in a newly allocated array.
*/
public TrackSelection[] getAll() {
/** Returns the selections in a newly allocated array. */
public @NullableType TrackSelection[] getAll() {
return trackSelections.clone();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.google.android.exoplayer2.trackselection;

import android.support.annotation.Nullable;
import com.google.android.exoplayer2.ExoPlaybackException;
import com.google.android.exoplayer2.ExoPlayer;
import com.google.android.exoplayer2.Renderer;
Expand Down Expand Up @@ -89,7 +90,7 @@ public interface InvalidationListener {

}

private InvalidationListener listener;
private @Nullable InvalidationListener listener;

/**
* Called by the player to initialize the selector.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import com.google.android.exoplayer2.RendererConfiguration;
import com.google.android.exoplayer2.util.Util;
import org.checkerframework.checker.nullness.compatqual.NullableType;

/**
* The result of a {@link TrackSelector} operation.
Expand All @@ -29,7 +30,7 @@ public final class TrackSelectorResult {
* A {@link RendererConfiguration} for each renderer. A null entry indicates the corresponding
* renderer should be disabled.
*/
public final RendererConfiguration[] rendererConfigurations;
public final @NullableType RendererConfiguration[] rendererConfigurations;
/**
* A {@link TrackSelectionArray} containing the track selection for each renderer.
*/
Expand All @@ -48,7 +49,9 @@ public final class TrackSelectorResult {
* TrackSelector#onSelectionActivated(Object)} should the selection be activated.
*/
public TrackSelectorResult(
RendererConfiguration[] rendererConfigurations, TrackSelection[] selections, Object info) {
@NullableType RendererConfiguration[] rendererConfigurations,
@NullableType TrackSelection[] selections,
Object info) {
this.rendererConfigurations = rendererConfigurations;
this.selections = new TrackSelectionArray(selections);
this.info = info;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -311,10 +311,10 @@ public static void writeBoolean(Parcel parcel, boolean value) {
* Returns a normalized RFC 639-2/T code for {@code language}.
*
* @param language A case-insensitive ISO 639 alpha-2 or alpha-3 language code.
* @return The all-lowercase normalized code, or null if the input was null, or
* {@code language.toLowerCase()} if the language could not be normalized.
* @return The all-lowercase normalized code, or null if the input was null, or {@code
* language.toLowerCase()} if the language could not be normalized.
*/
public static String normalizeLanguageCode(String language) {
public static @Nullable String normalizeLanguageCode(@Nullable String language) {
try {
return language == null ? null : new Locale(language).getISO3Language();
} catch (MissingResourceException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,9 @@ private void updateViews() {
removeViewAt(i);
}

if (trackSelector == null) {
MappingTrackSelector.MappedTrackInfo trackInfo =
trackSelector == null ? null : trackSelector.getCurrentMappedTrackInfo();
if (trackSelector == null || trackInfo == null) {
// The view is not initialized.
disableView.setEnabled(false);
defaultView.setEnabled(false);
Expand All @@ -212,7 +214,6 @@ private void updateViews() {
disableView.setEnabled(true);
defaultView.setEnabled(true);

MappingTrackSelector.MappedTrackInfo trackInfo = trackSelector.getCurrentMappedTrackInfo();
trackGroups = trackInfo.getTrackGroups(rendererIndex);

DefaultTrackSelector.Parameters parameters = trackSelector.getParameters();
Expand Down

0 comments on commit 3c6ca19

Please sign in to comment.