Skip to content

Commit

Permalink
Merge pull request #425 from vishnuchilakala:set_min_live_position
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 538173603
  • Loading branch information
tof-tof committed Jun 6, 2023
2 parents f4bf376 + e7c5b87 commit 9ca6e5d
Showing 1 changed file with 38 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ public static final class Factory implements MediaSourceFactory {
private CompositeSequenceableLoaderFactory compositeSequenceableLoaderFactory;
private LoadErrorHandlingPolicy loadErrorHandlingPolicy;
private long fallbackTargetLiveOffsetMs;
private long minLiveStartPositionUs;
@Nullable private ParsingLoadable.Parser<? extends DashManifest> manifestParser;

/**
Expand Down Expand Up @@ -145,7 +146,7 @@ public Factory(DataSource.Factory dataSourceFactory) {
* @param chunkSourceFactory A factory for {@link DashChunkSource} instances.
* @param manifestDataSourceFactory A factory for {@link DataSource} instances that will be used
* to load (and refresh) the manifest. May be {@code null} if the factory will only ever be
* used to create create media sources with sideloaded manifests via {@link
* used to create media sources with sideloaded manifests via {@link
* #createMediaSource(DashManifest, MediaItem)}.
*/
public Factory(
Expand All @@ -156,6 +157,7 @@ public Factory(
drmSessionManagerProvider = new DefaultDrmSessionManagerProvider();
loadErrorHandlingPolicy = new DefaultLoadErrorHandlingPolicy();
fallbackTargetLiveOffsetMs = DEFAULT_FALLBACK_TARGET_LIVE_OFFSET_MS;
minLiveStartPositionUs = MIN_LIVE_DEFAULT_START_POSITION_US;
compositeSequenceableLoaderFactory = new DefaultCompositeSequenceableLoaderFactory();
}

Expand Down Expand Up @@ -199,6 +201,25 @@ public Factory setFallbackTargetLiveOffsetMs(long fallbackTargetLiveOffsetMs) {
return this;
}

/**
* Sets the minimum position to start playback from in a live stream, in microseconds relative
* to the start of the live window.
*
* <p>This value will override any suggested value from the manifest and helps to prevent {@link
* androidx.media3.exoplayer.source.BehindLiveWindowException} issues.
*
* <p>The default value is {@link #MIN_LIVE_DEFAULT_START_POSITION_US}.
*
* @param minLiveStartPositionUs The minimum live start position, in microseconds relative to
* the start of the live window.
* @return This factory, for convenience.
*/
@CanIgnoreReturnValue
public Factory setMinLiveStartPositionUs(long minLiveStartPositionUs) {
this.minLiveStartPositionUs = minLiveStartPositionUs;
return this;
}

/**
* Sets the manifest parser to parse loaded manifest data when loading a manifest URI.
*
Expand Down Expand Up @@ -278,7 +299,8 @@ public DashMediaSource createMediaSource(DashManifest manifest, MediaItem mediaI
compositeSequenceableLoaderFactory,
drmSessionManagerProvider.get(mediaItem),
loadErrorHandlingPolicy,
fallbackTargetLiveOffsetMs);
fallbackTargetLiveOffsetMs,
minLiveStartPositionUs);
}

/**
Expand Down Expand Up @@ -309,7 +331,8 @@ public DashMediaSource createMediaSource(MediaItem mediaItem) {
compositeSequenceableLoaderFactory,
drmSessionManagerProvider.get(mediaItem),
loadErrorHandlingPolicy,
fallbackTargetLiveOffsetMs);
fallbackTargetLiveOffsetMs,
minLiveStartPositionUs);
}

@Override
Expand All @@ -330,16 +353,18 @@ public DashMediaSource createMediaSource(MediaItem mediaItem) {
/** The media id used by media items of dash media sources without a manifest URI. */
public static final String DEFAULT_MEDIA_ID = "DashMediaSource";

/**
* The minimum default start position for live streams, in microseconds relative to the start of
* the live window.
*/
public static final long MIN_LIVE_DEFAULT_START_POSITION_US = 5_000_000;

/**
* The interval in milliseconds between invocations of {@link
* MediaSourceCaller#onSourceInfoRefreshed(MediaSource, Timeline)} when the source's {@link
* Timeline} is changing dynamically (for example, for incomplete live streams).
*/
private static final long DEFAULT_NOTIFY_MANIFEST_INTERVAL_MS = 5000;
/**
* The minimum default start position for live streams, relative to the start of the live window.
*/
private static final long MIN_LIVE_DEFAULT_START_POSITION_US = 5_000_000;

private static final String TAG = "DashMediaSource";

Expand All @@ -352,6 +377,7 @@ public DashMediaSource createMediaSource(MediaItem mediaItem) {
private final LoadErrorHandlingPolicy loadErrorHandlingPolicy;
private final BaseUrlExclusionList baseUrlExclusionList;
private final long fallbackTargetLiveOffsetMs;
private final long minLiveStartPositionUs;
private final EventDispatcher manifestEventDispatcher;
private final ParsingLoadable.Parser<? extends DashManifest> manifestParser;
private final ManifestCallback manifestCallback;
Expand Down Expand Up @@ -392,7 +418,8 @@ private DashMediaSource(
CompositeSequenceableLoaderFactory compositeSequenceableLoaderFactory,
DrmSessionManager drmSessionManager,
LoadErrorHandlingPolicy loadErrorHandlingPolicy,
long fallbackTargetLiveOffsetMs) {
long fallbackTargetLiveOffsetMs,
long minLiveStartPositionUs) {
this.mediaItem = mediaItem;
this.liveConfiguration = mediaItem.liveConfiguration;
this.manifestUri = checkNotNull(mediaItem.localConfiguration).uri;
Expand All @@ -404,6 +431,7 @@ private DashMediaSource(
this.drmSessionManager = drmSessionManager;
this.loadErrorHandlingPolicy = loadErrorHandlingPolicy;
this.fallbackTargetLiveOffsetMs = fallbackTargetLiveOffsetMs;
this.minLiveStartPositionUs = minLiveStartPositionUs;
this.compositeSequenceableLoaderFactory = compositeSequenceableLoaderFactory;
baseUrlExclusionList = new BaseUrlExclusionList();
sideloadedManifest = manifest != null;
Expand Down Expand Up @@ -829,8 +857,7 @@ private void processManifest(boolean scheduleRefresh) {
windowStartUnixTimeMs =
manifest.availabilityStartTimeMs + Util.usToMs(windowStartTimeInManifestUs);
windowDefaultPositionUs = nowInWindowUs - Util.msToUs(liveConfiguration.targetOffsetMs);
long minimumWindowDefaultPositionUs =
min(MIN_LIVE_DEFAULT_START_POSITION_US, windowDurationUs / 2);
long minimumWindowDefaultPositionUs = min(minLiveStartPositionUs, windowDurationUs / 2);
if (windowDefaultPositionUs < minimumWindowDefaultPositionUs) {
// The default position is too close to the start of the live window. Set it to the minimum
// default position provided the window is at least twice as big. Else set it to the middle
Expand Down Expand Up @@ -939,8 +966,7 @@ private void updateLiveConfiguration(long nowInWindowUs, long windowDurationUs)
targetOffsetMs = minLiveOffsetMs;
}
if (targetOffsetMs > maxLiveOffsetMs) {
long safeDistanceFromWindowStartUs =
min(MIN_LIVE_DEFAULT_START_POSITION_US, windowDurationUs / 2);
long safeDistanceFromWindowStartUs = min(minLiveStartPositionUs, windowDurationUs / 2);
long maxTargetOffsetForSafeDistanceToWindowStartMs =
usToMs(nowInWindowUs - safeDistanceFromWindowStartUs);
targetOffsetMs =
Expand Down

0 comments on commit 9ca6e5d

Please sign in to comment.