Skip to content

Commit

Permalink
Move random adaptation to an intent extra.
Browse files Browse the repository at this point in the history
This will help with removing selection factories from
SelectionOverride. It doesn't seem worth the effort to
have this togglable and in the UI of our demo app.

This change will also:

- Help to move TrackSelectionHelper into the UI module,
  since it removes the part that's obviously debug-only.
- Make it possible to specify the ABR algorithm for each
  sample, which might be nice as we start implementing
  more of them.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=192760196
  • Loading branch information
ojw28 committed Apr 16, 2018
1 parent 67cde97 commit 1944ebb
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 89 deletions.
11 changes: 11 additions & 0 deletions demos/main/src/main/assets/media.exolist.json
Original file line number Diff line number Diff line change
Expand Up @@ -578,5 +578,16 @@
"ad_tag_uri": "http://vastsynthesizer.appspot.com/empty-midroll-2"
}
]
},
{
"name": "ABR",
"samples": [
{
"name": "Random ABR - Google Glass (MP4,H264)",
"uri": "http://www.youtube.com/api/manifest/dash/id/bf5bb2419360daf1/source/youtube?as=fmp4_audio_clear,fmp4_sd_hd_clear&sparams=ip,ipbits,expire,source,id,as&ip=0.0.0.0&ipbits=0&expire=19000000000&signature=51AF5F39AB0CEC3E5497CD9C900EBFEAECCCB5C7.8506521BFC350652163895D4C26DEE124209AA9E&key=ik0",
"extension": "mpd",
"abr_algorithm": "random"
}
]
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
import com.google.android.exoplayer2.trackselection.AdaptiveTrackSelection;
import com.google.android.exoplayer2.trackselection.DefaultTrackSelector;
import com.google.android.exoplayer2.trackselection.MappingTrackSelector.MappedTrackInfo;
import com.google.android.exoplayer2.trackselection.RandomTrackSelection;
import com.google.android.exoplayer2.trackselection.TrackSelection;
import com.google.android.exoplayer2.trackselection.TrackSelectionArray;
import com.google.android.exoplayer2.ui.DebugTextViewHelper;
Expand All @@ -91,10 +92,10 @@ public class PlayerActivity extends Activity
implements OnClickListener, PlaybackPreparer, PlayerControlView.VisibilityListener {

public static final String DRM_SCHEME_EXTRA = "drm_scheme";
public static final String DRM_LICENSE_URL = "drm_license_url";
public static final String DRM_KEY_REQUEST_PROPERTIES = "drm_key_request_properties";
public static final String DRM_MULTI_SESSION = "drm_multi_session";
public static final String PREFER_EXTENSION_DECODERS = "prefer_extension_decoders";
public static final String DRM_LICENSE_URL_EXTRA = "drm_license_url";
public static final String DRM_KEY_REQUEST_PROPERTIES_EXTRA = "drm_key_request_properties";
public static final String DRM_MULTI_SESSION_EXTRA = "drm_multi_session";
public static final String PREFER_EXTENSION_DECODERS_EXTRA = "prefer_extension_decoders";

public static final String ACTION_VIEW = "com.google.android.exoplayer.demo.action.VIEW";
public static final String EXTENSION_EXTRA = "extension";
Expand All @@ -108,7 +109,11 @@ public class PlayerActivity extends Activity

public static final String AD_TAG_URI_EXTRA = "ad_tag_uri";

// For backwards compatibility.
public static final String ABR_ALGORITHM_EXTRA = "abr_algorithm";
private static final String ABR_ALGORITHM_DEFAULT = "default";
private static final String ABR_ALGORITHM_RANDOM = "random";

// For backwards compatibility only.
private static final String DRM_SCHEME_UUID_EXTRA = "drm_scheme_uuid";

private static final DefaultBandwidthMeter BANDWIDTH_METER = new DefaultBandwidthMeter();
Expand Down Expand Up @@ -292,6 +297,7 @@ private void initializePlayer() {
}
} else {
showToast(getString(R.string.unexpected_intent_action, action));
finish();
return;
}
if (Util.maybeRequestReadExternalStoragePermission(this, uris)) {
Expand All @@ -301,9 +307,10 @@ private void initializePlayer() {

DefaultDrmSessionManager<FrameworkMediaCrypto> drmSessionManager = null;
if (intent.hasExtra(DRM_SCHEME_EXTRA) || intent.hasExtra(DRM_SCHEME_UUID_EXTRA)) {
String drmLicenseUrl = intent.getStringExtra(DRM_LICENSE_URL);
String[] keyRequestPropertiesArray = intent.getStringArrayExtra(DRM_KEY_REQUEST_PROPERTIES);
boolean multiSession = intent.getBooleanExtra(DRM_MULTI_SESSION, false);
String drmLicenseUrl = intent.getStringExtra(DRM_LICENSE_URL_EXTRA);
String[] keyRequestPropertiesArray =
intent.getStringArrayExtra(DRM_KEY_REQUEST_PROPERTIES_EXTRA);
boolean multiSession = intent.getBooleanExtra(DRM_MULTI_SESSION_EXTRA, false);
int errorStringId = R.string.error_drm_unknown;
if (Util.SDK_INT < 18) {
errorStringId = R.string.error_drm_not_supported;
Expand All @@ -326,11 +333,25 @@ private void initializePlayer() {
}
if (drmSessionManager == null) {
showToast(errorStringId);
finish();
return;
}
}

boolean preferExtensionDecoders = intent.getBooleanExtra(PREFER_EXTENSION_DECODERS, false);
TrackSelection.Factory trackSelectionFactory;
String abrAlgorithm = intent.getStringExtra(ABR_ALGORITHM_EXTRA);
if (abrAlgorithm == null || ABR_ALGORITHM_DEFAULT.equals(abrAlgorithm)) {
trackSelectionFactory = new AdaptiveTrackSelection.Factory(BANDWIDTH_METER);
} else if (ABR_ALGORITHM_RANDOM.equals(abrAlgorithm)) {
trackSelectionFactory = new RandomTrackSelection.Factory();
} else {
showToast(R.string.error_unrecognized_abr_algorithm);
finish();
return;
}

boolean preferExtensionDecoders =
intent.getBooleanExtra(PREFER_EXTENSION_DECODERS_EXTRA, false);
@DefaultRenderersFactory.ExtensionRendererMode int extensionRendererMode =
((DemoApplication) getApplication()).useExtensionRenderers()
? (preferExtensionDecoders ? DefaultRenderersFactory.EXTENSION_RENDERER_MODE_PREFER
Expand All @@ -339,10 +360,8 @@ private void initializePlayer() {
DefaultRenderersFactory renderersFactory =
new DefaultRenderersFactory(this, extensionRendererMode);

TrackSelection.Factory adaptiveTrackSelectionFactory =
new AdaptiveTrackSelection.Factory(BANDWIDTH_METER);
trackSelector = new DefaultTrackSelector(adaptiveTrackSelectionFactory);
trackSelectionHelper = new TrackSelectionHelper(trackSelector, adaptiveTrackSelectionFactory);
trackSelector = new DefaultTrackSelector(trackSelectionFactory);
trackSelectionHelper = new TrackSelectionHelper(trackSelector, trackSelectionFactory);
lastSeenTrackGroupArray = null;

player =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;

/**
* An activity for selecting from a list of samples.
Expand Down Expand Up @@ -178,13 +177,14 @@ private Sample readEntry(JsonReader reader, boolean insidePlaylist) throws IOExc
String sampleName = null;
String uri = null;
String extension = null;
UUID drmUuid = null;
String drmScheme = null;
String drmLicenseUrl = null;
String[] drmKeyRequestProperties = null;
boolean drmMultiSession = false;
boolean preferExtensionDecoders = false;
ArrayList<UriSample> playlistSamples = null;
String adTagUri = null;
String abrAlgorithm = null;

reader.beginObject();
while (reader.hasNext()) {
Expand All @@ -201,9 +201,7 @@ private Sample readEntry(JsonReader reader, boolean insidePlaylist) throws IOExc
break;
case "drm_scheme":
Assertions.checkState(!insidePlaylist, "Invalid attribute on nested item: drm_scheme");
String drmScheme = reader.nextString();
drmUuid = Util.getDrmUuid(drmScheme);
Assertions.checkState(drmUuid != null, "Invalid drm_scheme: " + drmScheme);
drmScheme = reader.nextString();
break;
case "drm_license_url":
Assertions.checkState(!insidePlaylist,
Expand Down Expand Up @@ -242,21 +240,28 @@ private Sample readEntry(JsonReader reader, boolean insidePlaylist) throws IOExc
case "ad_tag_uri":
adTagUri = reader.nextString();
break;
case "abr_algorithm":
Assertions.checkState(
!insidePlaylist, "Invalid attribute on nested item: abr_algorithm");
abrAlgorithm = reader.nextString();
break;
default:
throw new ParserException("Unsupported attribute name: " + name);
}
}
reader.endObject();
DrmInfo drmInfo = drmUuid == null ? null : new DrmInfo(drmUuid, drmLicenseUrl,
drmKeyRequestProperties, drmMultiSession);
DrmInfo drmInfo =
drmScheme == null
? null
: new DrmInfo(drmScheme, drmLicenseUrl, drmKeyRequestProperties, drmMultiSession);
if (playlistSamples != null) {
UriSample[] playlistSamplesArray = playlistSamples.toArray(
new UriSample[playlistSamples.size()]);
return new PlaylistSample(sampleName, preferExtensionDecoders, drmInfo,
playlistSamplesArray);
return new PlaylistSample(
sampleName, preferExtensionDecoders, abrAlgorithm, drmInfo, playlistSamplesArray);
} else {
return new UriSample(sampleName, preferExtensionDecoders, drmInfo, uri, extension,
adTagUri);
return new UriSample(
sampleName, preferExtensionDecoders, abrAlgorithm, drmInfo, uri, extension, adTagUri);
}
}

Expand Down Expand Up @@ -362,46 +367,52 @@ public SampleGroup(String title) {
}

private static final class DrmInfo {
public final UUID drmSchemeUuid;
public final String drmScheme;
public final String drmLicenseUrl;
public final String[] drmKeyRequestProperties;
public final boolean drmMultiSession;

public DrmInfo(UUID drmSchemeUuid, String drmLicenseUrl,
String[] drmKeyRequestProperties, boolean drmMultiSession) {
this.drmSchemeUuid = drmSchemeUuid;
public DrmInfo(
String drmScheme,
String drmLicenseUrl,
String[] drmKeyRequestProperties,
boolean drmMultiSession) {
this.drmScheme = drmScheme;
this.drmLicenseUrl = drmLicenseUrl;
this.drmKeyRequestProperties = drmKeyRequestProperties;
this.drmMultiSession = drmMultiSession;
}

public void updateIntent(Intent intent) {
Assertions.checkNotNull(intent);
intent.putExtra(PlayerActivity.DRM_SCHEME_EXTRA, drmSchemeUuid.toString());
intent.putExtra(PlayerActivity.DRM_LICENSE_URL, drmLicenseUrl);
intent.putExtra(PlayerActivity.DRM_KEY_REQUEST_PROPERTIES, drmKeyRequestProperties);
intent.putExtra(PlayerActivity.DRM_MULTI_SESSION, drmMultiSession);
intent.putExtra(PlayerActivity.DRM_SCHEME_EXTRA, drmScheme);
intent.putExtra(PlayerActivity.DRM_LICENSE_URL_EXTRA, drmLicenseUrl);
intent.putExtra(PlayerActivity.DRM_KEY_REQUEST_PROPERTIES_EXTRA, drmKeyRequestProperties);
intent.putExtra(PlayerActivity.DRM_MULTI_SESSION_EXTRA, drmMultiSession);
}
}

private abstract static class Sample {
public final String name;
public final boolean preferExtensionDecoders;
public final String abrAlgorithm;
public final DrmInfo drmInfo;

public Sample(String name, boolean preferExtensionDecoders, DrmInfo drmInfo) {
public Sample(
String name, boolean preferExtensionDecoders, String abrAlgorithm, DrmInfo drmInfo) {
this.name = name;
this.preferExtensionDecoders = preferExtensionDecoders;
this.abrAlgorithm = abrAlgorithm;
this.drmInfo = drmInfo;
}

public Intent buildIntent(Context context) {
Intent intent = new Intent(context, PlayerActivity.class);
intent.putExtra(PlayerActivity.PREFER_EXTENSION_DECODERS, preferExtensionDecoders);
intent.putExtra(PlayerActivity.PREFER_EXTENSION_DECODERS_EXTRA, preferExtensionDecoders);
intent.putExtra(PlayerActivity.ABR_ALGORITHM_EXTRA, abrAlgorithm);
if (drmInfo != null) {
drmInfo.updateIntent(intent);
}

return intent;
}

Expand All @@ -413,9 +424,15 @@ private static final class UriSample extends Sample {
public final String extension;
public final String adTagUri;

public UriSample(String name, boolean preferExtensionDecoders, DrmInfo drmInfo, String uri,
String extension, String adTagUri) {
super(name, preferExtensionDecoders, drmInfo);
public UriSample(
String name,
boolean preferExtensionDecoders,
String abrAlgorithm,
DrmInfo drmInfo,
String uri,
String extension,
String adTagUri) {
super(name, preferExtensionDecoders, abrAlgorithm, drmInfo);
this.uri = uri;
this.extension = extension;
this.adTagUri = adTagUri;
Expand All @@ -436,9 +453,13 @@ private static final class PlaylistSample extends Sample {

public final UriSample[] children;

public PlaylistSample(String name, boolean preferExtensionDecoders, DrmInfo drmInfo,
public PlaylistSample(
String name,
boolean preferExtensionDecoders,
String abrAlgorithm,
DrmInfo drmInfo,
UriSample... children) {
super(name, preferExtensionDecoders, drmInfo);
super(name, preferExtensionDecoders, abrAlgorithm, drmInfo);
this.children = children;
}

Expand Down
Loading

0 comments on commit 1944ebb

Please sign in to comment.