Filter unsupported video formats for HLS.

This commit is contained in:
Oliver Woodman 2015-05-01 20:34:57 +01:00
parent b405d3d9b7
commit 79cdd03682
4 changed files with 27 additions and 8 deletions

View file

@ -229,7 +229,7 @@ public class PlayerActivity extends Activity implements SurfaceHolder.Callback,
return new DashRendererBuilder(this, userAgent, contentUri.toString(),
new WidevineTestMediaDrmCallback(contentId), debugTextView, audioCapabilities);
case DemoUtil.TYPE_HLS:
return new HlsRendererBuilder(userAgent, contentUri.toString(), debugTextView);
return new HlsRendererBuilder(this, userAgent, contentUri.toString(), debugTextView);
case DemoUtil.TYPE_MP4:
return new ExtractorRendererBuilder(userAgent, contentUri, debugTextView,
new Mp4Extractor());

View file

@ -16,11 +16,14 @@
package com.google.android.exoplayer.demo.player;
import com.google.android.exoplayer.MediaCodecAudioTrackRenderer;
import com.google.android.exoplayer.MediaCodecUtil.DecoderQueryException;
import com.google.android.exoplayer.MediaCodecVideoTrackRenderer;
import com.google.android.exoplayer.TrackRenderer;
import com.google.android.exoplayer.chunk.VideoFormatSelectorUtil;
import com.google.android.exoplayer.demo.player.DemoPlayer.RendererBuilder;
import com.google.android.exoplayer.demo.player.DemoPlayer.RendererBuilderCallback;
import com.google.android.exoplayer.hls.HlsChunkSource;
import com.google.android.exoplayer.hls.HlsMasterPlaylist;
import com.google.android.exoplayer.hls.HlsPlaylist;
import com.google.android.exoplayer.hls.HlsPlaylistParser;
import com.google.android.exoplayer.hls.HlsSampleSource;
@ -33,6 +36,7 @@ import com.google.android.exoplayer.upstream.DefaultUriDataSource;
import com.google.android.exoplayer.util.ManifestFetcher;
import com.google.android.exoplayer.util.ManifestFetcher.ManifestCallback;
import android.content.Context;
import android.media.MediaCodec;
import android.os.Handler;
import android.widget.TextView;
@ -48,6 +52,7 @@ public class HlsRendererBuilder implements RendererBuilder, ManifestCallback<Hls
private static final int REQUESTED_BUFFER_SIZE = 18 * 1024 * 1024;
private static final long REQUESTED_BUFFER_DURATION_MS = 40000;
private final Context context;
private final String userAgent;
private final String url;
private final TextView debugTextView;
@ -55,7 +60,8 @@ public class HlsRendererBuilder implements RendererBuilder, ManifestCallback<Hls
private DemoPlayer player;
private RendererBuilderCallback callback;
public HlsRendererBuilder(String userAgent, String url, TextView debugTextView) {
public HlsRendererBuilder(Context context, String userAgent, String url, TextView debugTextView) {
this.context = context;
this.userAgent = userAgent;
this.url = url;
this.debugTextView = debugTextView;
@ -81,9 +87,21 @@ public class HlsRendererBuilder implements RendererBuilder, ManifestCallback<Hls
Handler mainHandler = player.getMainHandler();
DefaultBandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();
int[] variantIndices = null;
if (manifest instanceof HlsMasterPlaylist) {
HlsMasterPlaylist masterPlaylist = (HlsMasterPlaylist) manifest;
try {
variantIndices = VideoFormatSelectorUtil.selectVideoFormatsForDefaultDisplay(
context, masterPlaylist.variants, null, false);
} catch (DecoderQueryException e) {
callback.onRenderersError(e);
return;
}
}
DataSource dataSource = new DefaultUriDataSource(userAgent, bandwidthMeter);
HlsChunkSource chunkSource = new HlsChunkSource(dataSource, url, manifest, bandwidthMeter, null,
HlsChunkSource.ADAPTIVE_MODE_SPLICE);
HlsChunkSource chunkSource = new HlsChunkSource(dataSource, url, manifest, bandwidthMeter,
variantIndices, HlsChunkSource.ADAPTIVE_MODE_SPLICE);
HlsSampleSource sampleSource = new HlsSampleSource(chunkSource, true, 3, REQUESTED_BUFFER_SIZE,
REQUESTED_BUFFER_DURATION_MS, mainHandler, player, DemoPlayer.TYPE_VIDEO);
MediaCodecVideoTrackRenderer videoRenderer = new MediaCodecVideoTrackRenderer(sampleSource,

View file

@ -126,8 +126,8 @@ public final class VideoFormatSelectorUtil {
// viewport.
for (int i = selectedIndexList.size() - 1; i >= 0; i--) {
Format format = formatWrappers.get(i).getFormat();
int videoPixels = format.width * format.height;
if (format.width != -1 && format.height != -1 && videoPixels > maxVideoPixelsToRetain) {
if (format.width != -1 && format.height != -1
&& format.width * format.height > maxVideoPixelsToRetain) {
selectedIndexList.remove(i);
}
}

View file

@ -150,8 +150,9 @@ public class HlsChunkSource {
* @param playlistUrl The playlist URL.
* @param playlist The hls playlist.
* @param bandwidthMeter provides an estimate of the currently available bandwidth.
* @param variantIndices A subset of variant indices to consider, or null to consider all of the
* variants in the master playlist.
* @param variantIndices If {@code playlist} is a {@link HlsMasterPlaylist}, the subset of variant
* indices to consider, or null to consider all of the variants. For other playlist types
* this parameter is ignored.
* @param adaptiveMode The mode for switching from one variant to another. One of
* {@link #ADAPTIVE_MODE_NONE}, {@link #ADAPTIVE_MODE_ABRUPT} and
* {@link #ADAPTIVE_MODE_SPLICE}.