mirror of
https://github.com/androidx/media.git
synced 2025-05-07 23:50:44 +08:00
Move default SmoothStreaming track selection to library.
This commit is contained in:
parent
4b2991267e
commit
6cf261aed7
@ -24,17 +24,16 @@ import com.google.android.exoplayer.audio.AudioCapabilities;
|
|||||||
import com.google.android.exoplayer.chunk.ChunkSampleSource;
|
import com.google.android.exoplayer.chunk.ChunkSampleSource;
|
||||||
import com.google.android.exoplayer.chunk.ChunkSource;
|
import com.google.android.exoplayer.chunk.ChunkSource;
|
||||||
import com.google.android.exoplayer.chunk.FormatEvaluator.AdaptiveEvaluator;
|
import com.google.android.exoplayer.chunk.FormatEvaluator.AdaptiveEvaluator;
|
||||||
import com.google.android.exoplayer.chunk.VideoFormatSelectorUtil;
|
|
||||||
import com.google.android.exoplayer.demo.player.DemoPlayer.RendererBuilder;
|
import com.google.android.exoplayer.demo.player.DemoPlayer.RendererBuilder;
|
||||||
import com.google.android.exoplayer.drm.DrmSessionManager;
|
import com.google.android.exoplayer.drm.DrmSessionManager;
|
||||||
import com.google.android.exoplayer.drm.MediaDrmCallback;
|
import com.google.android.exoplayer.drm.MediaDrmCallback;
|
||||||
import com.google.android.exoplayer.drm.StreamingDrmSessionManager;
|
import com.google.android.exoplayer.drm.StreamingDrmSessionManager;
|
||||||
import com.google.android.exoplayer.drm.UnsupportedDrmException;
|
import com.google.android.exoplayer.drm.UnsupportedDrmException;
|
||||||
|
import com.google.android.exoplayer.smoothstreaming.DefaultSmoothStreamingTrackSelector;
|
||||||
import com.google.android.exoplayer.smoothstreaming.SmoothStreamingChunkSource;
|
import com.google.android.exoplayer.smoothstreaming.SmoothStreamingChunkSource;
|
||||||
import com.google.android.exoplayer.smoothstreaming.SmoothStreamingManifest;
|
import com.google.android.exoplayer.smoothstreaming.SmoothStreamingManifest;
|
||||||
import com.google.android.exoplayer.smoothstreaming.SmoothStreamingManifest.StreamElement;
|
import com.google.android.exoplayer.smoothstreaming.SmoothStreamingManifest.StreamElement;
|
||||||
import com.google.android.exoplayer.smoothstreaming.SmoothStreamingManifestParser;
|
import com.google.android.exoplayer.smoothstreaming.SmoothStreamingManifestParser;
|
||||||
import com.google.android.exoplayer.smoothstreaming.SmoothStreamingTrackSelector;
|
|
||||||
import com.google.android.exoplayer.text.TextTrackRenderer;
|
import com.google.android.exoplayer.text.TextTrackRenderer;
|
||||||
import com.google.android.exoplayer.upstream.DataSource;
|
import com.google.android.exoplayer.upstream.DataSource;
|
||||||
import com.google.android.exoplayer.upstream.DefaultAllocator;
|
import com.google.android.exoplayer.upstream.DefaultAllocator;
|
||||||
@ -49,7 +48,6 @@ import android.media.MediaCodec;
|
|||||||
import android.os.Handler;
|
import android.os.Handler;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.Arrays;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A {@link RendererBuilder} for SmoothStreaming.
|
* A {@link RendererBuilder} for SmoothStreaming.
|
||||||
@ -160,8 +158,8 @@ public class SmoothStreamingRendererBuilder implements RendererBuilder {
|
|||||||
// Build the video renderer.
|
// Build the video renderer.
|
||||||
DataSource videoDataSource = new DefaultUriDataSource(context, bandwidthMeter, userAgent);
|
DataSource videoDataSource = new DefaultUriDataSource(context, bandwidthMeter, userAgent);
|
||||||
ChunkSource videoChunkSource = new SmoothStreamingChunkSource(manifestFetcher,
|
ChunkSource videoChunkSource = new SmoothStreamingChunkSource(manifestFetcher,
|
||||||
new TrackSelector(context, StreamElement.TYPE_VIDEO), videoDataSource,
|
new DefaultSmoothStreamingTrackSelector(context, StreamElement.TYPE_VIDEO),
|
||||||
new AdaptiveEvaluator(bandwidthMeter), LIVE_EDGE_LATENCY_MS);
|
videoDataSource, new AdaptiveEvaluator(bandwidthMeter), LIVE_EDGE_LATENCY_MS);
|
||||||
ChunkSampleSource videoSampleSource = new ChunkSampleSource(videoChunkSource, loadControl,
|
ChunkSampleSource videoSampleSource = new ChunkSampleSource(videoChunkSource, loadControl,
|
||||||
VIDEO_BUFFER_SEGMENTS * BUFFER_SEGMENT_SIZE, mainHandler, player,
|
VIDEO_BUFFER_SEGMENTS * BUFFER_SEGMENT_SIZE, mainHandler, player,
|
||||||
DemoPlayer.TYPE_VIDEO);
|
DemoPlayer.TYPE_VIDEO);
|
||||||
@ -172,8 +170,8 @@ public class SmoothStreamingRendererBuilder implements RendererBuilder {
|
|||||||
// Build the audio renderer.
|
// Build the audio renderer.
|
||||||
DataSource audioDataSource = new DefaultUriDataSource(context, bandwidthMeter, userAgent);
|
DataSource audioDataSource = new DefaultUriDataSource(context, bandwidthMeter, userAgent);
|
||||||
ChunkSource audioChunkSource = new SmoothStreamingChunkSource(manifestFetcher,
|
ChunkSource audioChunkSource = new SmoothStreamingChunkSource(manifestFetcher,
|
||||||
new TrackSelector(context, StreamElement.TYPE_AUDIO), audioDataSource, null,
|
new DefaultSmoothStreamingTrackSelector(context, StreamElement.TYPE_AUDIO),
|
||||||
LIVE_EDGE_LATENCY_MS);
|
audioDataSource, null, LIVE_EDGE_LATENCY_MS);
|
||||||
ChunkSampleSource audioSampleSource = new ChunkSampleSource(audioChunkSource, loadControl,
|
ChunkSampleSource audioSampleSource = new ChunkSampleSource(audioChunkSource, loadControl,
|
||||||
AUDIO_BUFFER_SEGMENTS * BUFFER_SEGMENT_SIZE, mainHandler, player,
|
AUDIO_BUFFER_SEGMENTS * BUFFER_SEGMENT_SIZE, mainHandler, player,
|
||||||
DemoPlayer.TYPE_AUDIO);
|
DemoPlayer.TYPE_AUDIO);
|
||||||
@ -183,8 +181,8 @@ public class SmoothStreamingRendererBuilder implements RendererBuilder {
|
|||||||
// Build the text renderer.
|
// Build the text renderer.
|
||||||
DataSource textDataSource = new DefaultUriDataSource(context, bandwidthMeter, userAgent);
|
DataSource textDataSource = new DefaultUriDataSource(context, bandwidthMeter, userAgent);
|
||||||
ChunkSource textChunkSource = new SmoothStreamingChunkSource(manifestFetcher,
|
ChunkSource textChunkSource = new SmoothStreamingChunkSource(manifestFetcher,
|
||||||
new TrackSelector(context, StreamElement.TYPE_TEXT), textDataSource, null,
|
new DefaultSmoothStreamingTrackSelector(context, StreamElement.TYPE_TEXT),
|
||||||
LIVE_EDGE_LATENCY_MS);
|
textDataSource, null, LIVE_EDGE_LATENCY_MS);
|
||||||
ChunkSampleSource textSampleSource = new ChunkSampleSource(textChunkSource, loadControl,
|
ChunkSampleSource textSampleSource = new ChunkSampleSource(textChunkSource, loadControl,
|
||||||
TEXT_BUFFER_SEGMENTS * BUFFER_SEGMENT_SIZE, mainHandler, player,
|
TEXT_BUFFER_SEGMENTS * BUFFER_SEGMENT_SIZE, mainHandler, player,
|
||||||
DemoPlayer.TYPE_TEXT);
|
DemoPlayer.TYPE_TEXT);
|
||||||
@ -201,36 +199,4 @@ public class SmoothStreamingRendererBuilder implements RendererBuilder {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static final class TrackSelector implements SmoothStreamingTrackSelector {
|
|
||||||
|
|
||||||
private final Context context;
|
|
||||||
private final int elementType;
|
|
||||||
|
|
||||||
private TrackSelector(Context context, int type) {
|
|
||||||
this.context = context;
|
|
||||||
this.elementType = type;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void selectTracks(SmoothStreamingManifest manifest, Output output) throws IOException {
|
|
||||||
for (int i = 0; i < manifest.streamElements.length; i++) {
|
|
||||||
if (manifest.streamElements[i].type == elementType) {
|
|
||||||
if (elementType == StreamElement.TYPE_VIDEO) {
|
|
||||||
int[] trackIndices = VideoFormatSelectorUtil.selectVideoFormatsForDefaultDisplay(
|
|
||||||
context, Arrays.asList(manifest.streamElements[i].tracks), null, false);
|
|
||||||
output.adaptiveTrack(manifest, i, trackIndices);
|
|
||||||
for (int j = 0; j < trackIndices.length; j++) {
|
|
||||||
output.fixedTrack(manifest, i, trackIndices[j]);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
for (int j = 0; j < manifest.streamElements[i].tracks.length; j++) {
|
|
||||||
output.fixedTrack(manifest, i, j);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,66 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2014 The Android Open Source Project
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
package com.google.android.exoplayer.smoothstreaming;
|
||||||
|
|
||||||
|
import com.google.android.exoplayer.chunk.VideoFormatSelectorUtil;
|
||||||
|
import com.google.android.exoplayer.smoothstreaming.SmoothStreamingManifest.StreamElement;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A default {@link SmoothStreamingTrackSelector} implementation.
|
||||||
|
*/
|
||||||
|
// TODO: Add configuration options (e.g. ability to disable adaptive track output, disable format
|
||||||
|
// filtering etc).
|
||||||
|
public final class DefaultSmoothStreamingTrackSelector implements SmoothStreamingTrackSelector {
|
||||||
|
|
||||||
|
private final Context context;
|
||||||
|
private final int streamElementType;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param context A context.
|
||||||
|
* @param streamElementType The type of stream to select. One of {@link StreamElement#TYPE_AUDIO},
|
||||||
|
* {@link StreamElement#TYPE_VIDEO} and {@link StreamElement#TYPE_TEXT}.
|
||||||
|
*/
|
||||||
|
public DefaultSmoothStreamingTrackSelector(Context context, int streamElementType) {
|
||||||
|
this.context = context;
|
||||||
|
this.streamElementType = streamElementType;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void selectTracks(SmoothStreamingManifest manifest, Output output) throws IOException {
|
||||||
|
for (int i = 0; i < manifest.streamElements.length; i++) {
|
||||||
|
if (manifest.streamElements[i].type == streamElementType) {
|
||||||
|
if (streamElementType == StreamElement.TYPE_VIDEO) {
|
||||||
|
int[] trackIndices = VideoFormatSelectorUtil.selectVideoFormatsForDefaultDisplay(
|
||||||
|
context, Arrays.asList(manifest.streamElements[i].tracks), null, false);
|
||||||
|
output.adaptiveTrack(manifest, i, trackIndices);
|
||||||
|
for (int j = 0; j < trackIndices.length; j++) {
|
||||||
|
output.fixedTrack(manifest, i, trackIndices[j]);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
for (int j = 0; j < manifest.streamElements[i].tracks.length; j++) {
|
||||||
|
output.fixedTrack(manifest, i, j);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -185,8 +185,6 @@ public class SmoothStreamingChunkSource implements ChunkSource,
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void enable(int track) {
|
public void enable(int track) {
|
||||||
fatalError = null;
|
|
||||||
evaluation.format = null;
|
|
||||||
enabledTrack = tracks.get(track);
|
enabledTrack = tracks.get(track);
|
||||||
if (enabledTrack.isAdaptive()) {
|
if (enabledTrack.isAdaptive()) {
|
||||||
adaptiveFormatEvaluator.enable();
|
adaptiveFormatEvaluator.enable();
|
||||||
@ -339,6 +337,8 @@ public class SmoothStreamingChunkSource implements ChunkSource,
|
|||||||
if (manifestFetcher != null) {
|
if (manifestFetcher != null) {
|
||||||
manifestFetcher.disable();
|
manifestFetcher.disable();
|
||||||
}
|
}
|
||||||
|
evaluation.format = null;
|
||||||
|
fatalError = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
// SmoothStreamingTrackSelector.Output implementation.
|
// SmoothStreamingTrackSelector.Output implementation.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user