Commit c24a699b by tonihei Committed by Oliver Woodman

Update demo apps to use player-provided default bandwidth meter.

This simplifies the player setup and prevents using deprecated methods in the demo.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=205642981
parent 1d636520
...@@ -43,7 +43,6 @@ import com.google.android.exoplayer2.source.smoothstreaming.SsMediaSource; ...@@ -43,7 +43,6 @@ import com.google.android.exoplayer2.source.smoothstreaming.SsMediaSource;
import com.google.android.exoplayer2.trackselection.DefaultTrackSelector; import com.google.android.exoplayer2.trackselection.DefaultTrackSelector;
import com.google.android.exoplayer2.ui.PlayerControlView; import com.google.android.exoplayer2.ui.PlayerControlView;
import com.google.android.exoplayer2.ui.PlayerView; import com.google.android.exoplayer2.ui.PlayerView;
import com.google.android.exoplayer2.upstream.DefaultBandwidthMeter;
import com.google.android.exoplayer2.upstream.DefaultHttpDataSourceFactory; import com.google.android.exoplayer2.upstream.DefaultHttpDataSourceFactory;
import com.google.android.gms.cast.MediaInfo; import com.google.android.gms.cast.MediaInfo;
import com.google.android.gms.cast.MediaMetadata; import com.google.android.gms.cast.MediaMetadata;
...@@ -68,9 +67,8 @@ import java.util.ArrayList; ...@@ -68,9 +67,8 @@ import java.util.ArrayList;
} }
private static final String USER_AGENT = "ExoCastDemoPlayer"; private static final String USER_AGENT = "ExoCastDemoPlayer";
private static final DefaultBandwidthMeter BANDWIDTH_METER = new DefaultBandwidthMeter();
private static final DefaultHttpDataSourceFactory DATA_SOURCE_FACTORY = private static final DefaultHttpDataSourceFactory DATA_SOURCE_FACTORY =
new DefaultHttpDataSourceFactory(USER_AGENT, BANDWIDTH_METER); new DefaultHttpDataSourceFactory(USER_AGENT);
private final PlayerView localPlayerView; private final PlayerView localPlayerView;
private final PlayerControlView castControlView; private final PlayerControlView castControlView;
...@@ -117,7 +115,7 @@ import java.util.ArrayList; ...@@ -117,7 +115,7 @@ import java.util.ArrayList;
currentItemIndex = C.INDEX_UNSET; currentItemIndex = C.INDEX_UNSET;
concatenatingMediaSource = new ConcatenatingMediaSource(); concatenatingMediaSource = new ConcatenatingMediaSource();
DefaultTrackSelector trackSelector = new DefaultTrackSelector(BANDWIDTH_METER); DefaultTrackSelector trackSelector = new DefaultTrackSelector();
RenderersFactory renderersFactory = new DefaultRenderersFactory(context); RenderersFactory renderersFactory = new DefaultRenderersFactory(context);
exoPlayer = ExoPlayerFactory.newSimpleInstance(renderersFactory, trackSelector); exoPlayer = ExoPlayerFactory.newSimpleInstance(renderersFactory, trackSelector);
exoPlayer.addListener(this); exoPlayer.addListener(this);
......
...@@ -36,9 +36,7 @@ import com.google.android.exoplayer2.trackselection.DefaultTrackSelector; ...@@ -36,9 +36,7 @@ import com.google.android.exoplayer2.trackselection.DefaultTrackSelector;
import com.google.android.exoplayer2.trackselection.TrackSelection; import com.google.android.exoplayer2.trackselection.TrackSelection;
import com.google.android.exoplayer2.trackselection.TrackSelector; import com.google.android.exoplayer2.trackselection.TrackSelector;
import com.google.android.exoplayer2.ui.PlayerView; import com.google.android.exoplayer2.ui.PlayerView;
import com.google.android.exoplayer2.upstream.BandwidthMeter;
import com.google.android.exoplayer2.upstream.DataSource; import com.google.android.exoplayer2.upstream.DataSource;
import com.google.android.exoplayer2.upstream.DefaultBandwidthMeter;
import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory; import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory;
import com.google.android.exoplayer2.util.Util; import com.google.android.exoplayer2.util.Util;
...@@ -46,8 +44,7 @@ import com.google.android.exoplayer2.util.Util; ...@@ -46,8 +44,7 @@ import com.google.android.exoplayer2.util.Util;
/* package */ final class PlayerManager implements AdsMediaSource.MediaSourceFactory { /* package */ final class PlayerManager implements AdsMediaSource.MediaSourceFactory {
private final ImaAdsLoader adsLoader; private final ImaAdsLoader adsLoader;
private final DataSource.Factory manifestDataSourceFactory; private final DataSource.Factory dataSourceFactory;
private final DataSource.Factory mediaDataSourceFactory;
private SimpleExoPlayer player; private SimpleExoPlayer player;
private long contentPosition; private long contentPosition;
...@@ -55,21 +52,14 @@ import com.google.android.exoplayer2.util.Util; ...@@ -55,21 +52,14 @@ import com.google.android.exoplayer2.util.Util;
public PlayerManager(Context context) { public PlayerManager(Context context) {
String adTag = context.getString(R.string.ad_tag_url); String adTag = context.getString(R.string.ad_tag_url);
adsLoader = new ImaAdsLoader(context, Uri.parse(adTag)); adsLoader = new ImaAdsLoader(context, Uri.parse(adTag));
manifestDataSourceFactory = dataSourceFactory =
new DefaultDataSourceFactory( new DefaultDataSourceFactory(
context, Util.getUserAgent(context, context.getString(R.string.application_name))); context, Util.getUserAgent(context, context.getString(R.string.application_name)));
mediaDataSourceFactory =
new DefaultDataSourceFactory(
context,
Util.getUserAgent(context, context.getString(R.string.application_name)),
new DefaultBandwidthMeter());
} }
public void init(Context context, PlayerView playerView) { public void init(Context context, PlayerView playerView) {
// Create a default track selector. // Create a default track selector.
BandwidthMeter bandwidthMeter = new DefaultBandwidthMeter(); TrackSelection.Factory videoTrackSelectionFactory = new AdaptiveTrackSelection.Factory();
TrackSelection.Factory videoTrackSelectionFactory =
new AdaptiveTrackSelection.Factory(bandwidthMeter);
TrackSelector trackSelector = new DefaultTrackSelector(videoTrackSelectionFactory); TrackSelector trackSelector = new DefaultTrackSelector(videoTrackSelectionFactory);
// Create a player instance. // Create a player instance.
...@@ -134,17 +124,16 @@ import com.google.android.exoplayer2.util.Util; ...@@ -134,17 +124,16 @@ import com.google.android.exoplayer2.util.Util;
switch (type) { switch (type) {
case C.TYPE_DASH: case C.TYPE_DASH:
return new DashMediaSource.Factory( return new DashMediaSource.Factory(
new DefaultDashChunkSource.Factory(mediaDataSourceFactory), new DefaultDashChunkSource.Factory(dataSourceFactory), dataSourceFactory)
manifestDataSourceFactory)
.createMediaSource(uri); .createMediaSource(uri);
case C.TYPE_SS: case C.TYPE_SS:
return new SsMediaSource.Factory( return new SsMediaSource.Factory(
new DefaultSsChunkSource.Factory(mediaDataSourceFactory), manifestDataSourceFactory) new DefaultSsChunkSource.Factory(dataSourceFactory), dataSourceFactory)
.createMediaSource(uri); .createMediaSource(uri);
case C.TYPE_HLS: case C.TYPE_HLS:
return new HlsMediaSource.Factory(mediaDataSourceFactory).createMediaSource(uri); return new HlsMediaSource.Factory(dataSourceFactory).createMediaSource(uri);
case C.TYPE_OTHER: case C.TYPE_OTHER:
return new ExtractorMediaSource.Factory(mediaDataSourceFactory).createMediaSource(uri); return new ExtractorMediaSource.Factory(dataSourceFactory).createMediaSource(uri);
default: default:
throw new IllegalStateException("Unsupported type: " + type); throw new IllegalStateException("Unsupported type: " + type);
} }
......
...@@ -23,7 +23,6 @@ import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory; ...@@ -23,7 +23,6 @@ import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory;
import com.google.android.exoplayer2.upstream.DefaultHttpDataSourceFactory; import com.google.android.exoplayer2.upstream.DefaultHttpDataSourceFactory;
import com.google.android.exoplayer2.upstream.FileDataSourceFactory; import com.google.android.exoplayer2.upstream.FileDataSourceFactory;
import com.google.android.exoplayer2.upstream.HttpDataSource; import com.google.android.exoplayer2.upstream.HttpDataSource;
import com.google.android.exoplayer2.upstream.TransferListener;
import com.google.android.exoplayer2.upstream.cache.Cache; import com.google.android.exoplayer2.upstream.cache.Cache;
import com.google.android.exoplayer2.upstream.cache.CacheDataSource; import com.google.android.exoplayer2.upstream.cache.CacheDataSource;
import com.google.android.exoplayer2.upstream.cache.CacheDataSourceFactory; import com.google.android.exoplayer2.upstream.cache.CacheDataSourceFactory;
...@@ -56,16 +55,15 @@ public class DemoApplication extends Application { ...@@ -56,16 +55,15 @@ public class DemoApplication extends Application {
} }
/** Returns a {@link DataSource.Factory}. */ /** Returns a {@link DataSource.Factory}. */
public DataSource.Factory buildDataSourceFactory(TransferListener<? super DataSource> listener) { public DataSource.Factory buildDataSourceFactory() {
DefaultDataSourceFactory upstreamFactory = DefaultDataSourceFactory upstreamFactory =
new DefaultDataSourceFactory(this, listener, buildHttpDataSourceFactory(listener)); new DefaultDataSourceFactory(this, buildHttpDataSourceFactory());
return buildReadOnlyCacheDataSource(upstreamFactory, getDownloadCache()); return buildReadOnlyCacheDataSource(upstreamFactory, getDownloadCache());
} }
/** Returns a {@link HttpDataSource.Factory}. */ /** Returns a {@link HttpDataSource.Factory}. */
public HttpDataSource.Factory buildHttpDataSourceFactory( public HttpDataSource.Factory buildHttpDataSourceFactory() {
TransferListener<? super DataSource> listener) { return new DefaultHttpDataSourceFactory(userAgent);
return new DefaultHttpDataSourceFactory(userAgent, listener);
} }
/** Returns whether extension renderers should be used. */ /** Returns whether extension renderers should be used. */
...@@ -86,8 +84,7 @@ public class DemoApplication extends Application { ...@@ -86,8 +84,7 @@ public class DemoApplication extends Application {
private synchronized void initDownloadManager() { private synchronized void initDownloadManager() {
if (downloadManager == null) { if (downloadManager == null) {
DownloaderConstructorHelper downloaderConstructorHelper = DownloaderConstructorHelper downloaderConstructorHelper =
new DownloaderConstructorHelper( new DownloaderConstructorHelper(getDownloadCache(), buildHttpDataSourceFactory());
getDownloadCache(), buildHttpDataSourceFactory(/* listener= */ null));
downloadManager = downloadManager =
new DownloadManager( new DownloadManager(
downloaderConstructorHelper, downloaderConstructorHelper,
...@@ -97,7 +94,7 @@ public class DemoApplication extends Application { ...@@ -97,7 +94,7 @@ public class DemoApplication extends Application {
downloadTracker = downloadTracker =
new DownloadTracker( new DownloadTracker(
/* context= */ this, /* context= */ this,
buildDataSourceFactory(/* listener= */ null), buildDataSourceFactory(),
new File(getDownloadDirectory(), DOWNLOAD_TRACKER_ACTION_FILE)); new File(getDownloadDirectory(), DOWNLOAD_TRACKER_ACTION_FILE));
downloadManager.addListener(downloadTracker); downloadManager.addListener(downloadTracker);
} }
......
...@@ -76,7 +76,6 @@ import com.google.android.exoplayer2.ui.PlayerControlView; ...@@ -76,7 +76,6 @@ import com.google.android.exoplayer2.ui.PlayerControlView;
import com.google.android.exoplayer2.ui.PlayerView; import com.google.android.exoplayer2.ui.PlayerView;
import com.google.android.exoplayer2.ui.TrackSelectionView; import com.google.android.exoplayer2.ui.TrackSelectionView;
import com.google.android.exoplayer2.upstream.DataSource; import com.google.android.exoplayer2.upstream.DataSource;
import com.google.android.exoplayer2.upstream.DefaultBandwidthMeter;
import com.google.android.exoplayer2.upstream.HttpDataSource; import com.google.android.exoplayer2.upstream.HttpDataSource;
import com.google.android.exoplayer2.util.ErrorMessageProvider; import com.google.android.exoplayer2.util.ErrorMessageProvider;
import com.google.android.exoplayer2.util.EventLogger; import com.google.android.exoplayer2.util.EventLogger;
...@@ -121,7 +120,6 @@ public class PlayerActivity extends Activity ...@@ -121,7 +120,6 @@ public class PlayerActivity extends Activity
private static final String KEY_POSITION = "position"; private static final String KEY_POSITION = "position";
private static final String KEY_AUTO_PLAY = "auto_play"; private static final String KEY_AUTO_PLAY = "auto_play";
private static final DefaultBandwidthMeter BANDWIDTH_METER = new DefaultBandwidthMeter();
private static final CookieManager DEFAULT_COOKIE_MANAGER; private static final CookieManager DEFAULT_COOKIE_MANAGER;
static { static {
DEFAULT_COOKIE_MANAGER = new CookieManager(); DEFAULT_COOKIE_MANAGER = new CookieManager();
...@@ -132,7 +130,7 @@ public class PlayerActivity extends Activity ...@@ -132,7 +130,7 @@ public class PlayerActivity extends Activity
private LinearLayout debugRootView; private LinearLayout debugRootView;
private TextView debugTextView; private TextView debugTextView;
private DataSource.Factory mediaDataSourceFactory; private DataSource.Factory dataSourceFactory;
private SimpleExoPlayer player; private SimpleExoPlayer player;
private FrameworkMediaDrm mediaDrm; private FrameworkMediaDrm mediaDrm;
private MediaSource mediaSource; private MediaSource mediaSource;
...@@ -156,7 +154,7 @@ public class PlayerActivity extends Activity ...@@ -156,7 +154,7 @@ public class PlayerActivity extends Activity
@Override @Override
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
mediaDataSourceFactory = buildDataSourceFactory(true); dataSourceFactory = buildDataSourceFactory();
if (CookieHandler.getDefault() != DEFAULT_COOKIE_MANAGER) { if (CookieHandler.getDefault() != DEFAULT_COOKIE_MANAGER) {
CookieHandler.setDefault(DEFAULT_COOKIE_MANAGER); CookieHandler.setDefault(DEFAULT_COOKIE_MANAGER);
} }
...@@ -367,7 +365,7 @@ public class PlayerActivity extends Activity ...@@ -367,7 +365,7 @@ public class PlayerActivity extends Activity
TrackSelection.Factory trackSelectionFactory; TrackSelection.Factory trackSelectionFactory;
String abrAlgorithm = intent.getStringExtra(ABR_ALGORITHM_EXTRA); String abrAlgorithm = intent.getStringExtra(ABR_ALGORITHM_EXTRA);
if (abrAlgorithm == null || ABR_ALGORITHM_DEFAULT.equals(abrAlgorithm)) { if (abrAlgorithm == null || ABR_ALGORITHM_DEFAULT.equals(abrAlgorithm)) {
trackSelectionFactory = new AdaptiveTrackSelection.Factory(BANDWIDTH_METER); trackSelectionFactory = new AdaptiveTrackSelection.Factory();
} else if (ABR_ALGORITHM_RANDOM.equals(abrAlgorithm)) { } else if (ABR_ALGORITHM_RANDOM.equals(abrAlgorithm)) {
trackSelectionFactory = new RandomTrackSelection.Factory(); trackSelectionFactory = new RandomTrackSelection.Factory();
} else { } else {
...@@ -441,25 +439,23 @@ public class PlayerActivity extends Activity ...@@ -441,25 +439,23 @@ public class PlayerActivity extends Activity
switch (type) { switch (type) {
case C.TYPE_DASH: case C.TYPE_DASH:
return new DashMediaSource.Factory( return new DashMediaSource.Factory(
new DefaultDashChunkSource.Factory(mediaDataSourceFactory), new DefaultDashChunkSource.Factory(dataSourceFactory), dataSourceFactory)
buildDataSourceFactory(false))
.setManifestParser( .setManifestParser(
new FilteringManifestParser<>(new DashManifestParser(), getOfflineStreamKeys(uri))) new FilteringManifestParser<>(new DashManifestParser(), getOfflineStreamKeys(uri)))
.createMediaSource(uri); .createMediaSource(uri);
case C.TYPE_SS: case C.TYPE_SS:
return new SsMediaSource.Factory( return new SsMediaSource.Factory(
new DefaultSsChunkSource.Factory(mediaDataSourceFactory), new DefaultSsChunkSource.Factory(dataSourceFactory), dataSourceFactory)
buildDataSourceFactory(false))
.setManifestParser( .setManifestParser(
new FilteringManifestParser<>(new SsManifestParser(), getOfflineStreamKeys(uri))) new FilteringManifestParser<>(new SsManifestParser(), getOfflineStreamKeys(uri)))
.createMediaSource(uri); .createMediaSource(uri);
case C.TYPE_HLS: case C.TYPE_HLS:
return new HlsMediaSource.Factory(mediaDataSourceFactory) return new HlsMediaSource.Factory(dataSourceFactory)
.setPlaylistParser( .setPlaylistParser(
new FilteringManifestParser<>(new HlsPlaylistParser(), getOfflineStreamKeys(uri))) new FilteringManifestParser<>(new HlsPlaylistParser(), getOfflineStreamKeys(uri)))
.createMediaSource(uri); .createMediaSource(uri);
case C.TYPE_OTHER: case C.TYPE_OTHER:
return new ExtractorMediaSource.Factory(mediaDataSourceFactory).createMediaSource(uri); return new ExtractorMediaSource.Factory(dataSourceFactory).createMediaSource(uri);
default: { default: {
throw new IllegalStateException("Unsupported type: " + type); throw new IllegalStateException("Unsupported type: " + type);
} }
...@@ -474,7 +470,7 @@ public class PlayerActivity extends Activity ...@@ -474,7 +470,7 @@ public class PlayerActivity extends Activity
UUID uuid, String licenseUrl, String[] keyRequestPropertiesArray, boolean multiSession) UUID uuid, String licenseUrl, String[] keyRequestPropertiesArray, boolean multiSession)
throws UnsupportedDrmException { throws UnsupportedDrmException {
HttpDataSource.Factory licenseDataSourceFactory = HttpDataSource.Factory licenseDataSourceFactory =
((DemoApplication) getApplication()).buildHttpDataSourceFactory(/* listener= */ null); ((DemoApplication) getApplication()).buildHttpDataSourceFactory();
HttpMediaDrmCallback drmCallback = HttpMediaDrmCallback drmCallback =
new HttpMediaDrmCallback(licenseUrl, licenseDataSourceFactory); new HttpMediaDrmCallback(licenseUrl, licenseDataSourceFactory);
if (keyRequestPropertiesArray != null) { if (keyRequestPropertiesArray != null) {
...@@ -538,16 +534,9 @@ public class PlayerActivity extends Activity ...@@ -538,16 +534,9 @@ public class PlayerActivity extends Activity
startPosition = C.TIME_UNSET; startPosition = C.TIME_UNSET;
} }
/** /** Returns a new DataSource factory. */
* Returns a new DataSource factory. private DataSource.Factory buildDataSourceFactory() {
* return ((DemoApplication) getApplication()).buildDataSourceFactory();
* @param useBandwidthMeter Whether to set {@link #BANDWIDTH_METER} as a listener to the new
* DataSource factory.
* @return A new DataSource factory.
*/
private DataSource.Factory buildDataSourceFactory(boolean useBandwidthMeter) {
return ((DemoApplication) getApplication())
.buildDataSourceFactory(useBandwidthMeter ? BANDWIDTH_METER : null);
} }
/** Returns an ads media source, reusing the ads loader if one exists. */ /** Returns an ads media source, reusing the ads loader if one exists. */
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment