Remove unnecessary variables from ConcatenatingMediaSource.

The total window and period count, as well as the period offset for each holder
are not actually needed and can be removed.

Also added a TODO to remove two other variables if possible.

PiperOrigin-RevId: 255945584
This commit is contained in:
tonihei 2019-07-01 14:49:35 +01:00 committed by Oliver Woodman
parent 71de1d37ac
commit 04959ec648

View File

@ -18,7 +18,6 @@ package com.google.android.exoplayer2.source;
import android.os.Handler; import android.os.Handler;
import android.os.Message; import android.os.Message;
import androidx.annotation.GuardedBy; import androidx.annotation.GuardedBy;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import android.util.Pair; import android.util.Pair;
import com.google.android.exoplayer2.C; import com.google.android.exoplayer2.C;
@ -78,8 +77,6 @@ public final class ConcatenatingMediaSource extends CompositeMediaSource<MediaSo
private boolean timelineUpdateScheduled; private boolean timelineUpdateScheduled;
private Set<HandlerAndRunnable> nextTimelineUpdateOnCompletionActions; private Set<HandlerAndRunnable> nextTimelineUpdateOnCompletionActions;
private ShuffleOrder shuffleOrder; private ShuffleOrder shuffleOrder;
private int windowCount;
private int periodCount;
/** /**
* @param mediaSources The {@link MediaSource}s to concatenate. It is valid for the same * @param mediaSources The {@link MediaSource}s to concatenate. It is valid for the same
@ -483,8 +480,6 @@ public final class ConcatenatingMediaSource extends CompositeMediaSource<MediaSo
mediaSourceHolders.clear(); mediaSourceHolders.clear();
mediaSourceByUid.clear(); mediaSourceByUid.clear();
shuffleOrder = shuffleOrder.cloneAndClear(); shuffleOrder = shuffleOrder.cloneAndClear();
windowCount = 0;
periodCount = 0;
if (playbackThreadHandler != null) { if (playbackThreadHandler != null) {
playbackThreadHandler.removeCallbacksAndMessages(null); playbackThreadHandler.removeCallbacksAndMessages(null);
playbackThreadHandler = null; playbackThreadHandler = null;
@ -702,9 +697,7 @@ public final class ConcatenatingMediaSource extends CompositeMediaSource<MediaSo
Set<HandlerAndRunnable> onCompletionActions = nextTimelineUpdateOnCompletionActions; Set<HandlerAndRunnable> onCompletionActions = nextTimelineUpdateOnCompletionActions;
nextTimelineUpdateOnCompletionActions = new HashSet<>(); nextTimelineUpdateOnCompletionActions = new HashSet<>();
refreshSourceInfo( refreshSourceInfo(
new ConcatenatedTimeline( new ConcatenatedTimeline(mediaSourceHolders, shuffleOrder, isAtomic), /* manifest= */ null);
mediaSourceHolders, windowCount, periodCount, shuffleOrder, isAtomic),
/* manifest= */ null);
getPlaybackThreadHandlerOnPlaybackThread() getPlaybackThreadHandlerOnPlaybackThread()
.obtainMessage(MSG_ON_COMPLETION, onCompletionActions) .obtainMessage(MSG_ON_COMPLETION, onCompletionActions)
.sendToTarget(); .sendToTarget();
@ -737,17 +730,12 @@ public final class ConcatenatingMediaSource extends CompositeMediaSource<MediaSo
MediaSourceHolder previousHolder = mediaSourceHolders.get(newIndex - 1); MediaSourceHolder previousHolder = mediaSourceHolders.get(newIndex - 1);
newMediaSourceHolder.reset( newMediaSourceHolder.reset(
newIndex, newIndex,
previousHolder.firstWindowIndexInChild + previousHolder.timeline.getWindowCount(), previousHolder.firstWindowIndexInChild + previousHolder.timeline.getWindowCount());
previousHolder.firstPeriodIndexInChild + previousHolder.timeline.getPeriodCount());
} else { } else {
newMediaSourceHolder.reset( newMediaSourceHolder.reset(newIndex, /* firstWindowIndexInChild= */ 0);
newIndex, /* firstWindowIndexInChild= */ 0, /* firstPeriodIndexInChild= */ 0);
} }
correctOffsets( correctOffsets(
newIndex, newIndex, /* childIndexUpdate= */ 1, newMediaSourceHolder.timeline.getWindowCount());
/* childIndexUpdate= */ 1,
newMediaSourceHolder.timeline.getWindowCount(),
newMediaSourceHolder.timeline.getPeriodCount());
mediaSourceHolders.add(newIndex, newMediaSourceHolder); mediaSourceHolders.add(newIndex, newMediaSourceHolder);
mediaSourceByUid.put(newMediaSourceHolder.uid, newMediaSourceHolder); mediaSourceByUid.put(newMediaSourceHolder.uid, newMediaSourceHolder);
if (!useLazyPreparation) { if (!useLazyPreparation) {
@ -764,14 +752,15 @@ public final class ConcatenatingMediaSource extends CompositeMediaSource<MediaSo
if (deferredTimeline.getTimeline() == timeline) { if (deferredTimeline.getTimeline() == timeline) {
return; return;
} }
int windowOffsetUpdate = timeline.getWindowCount() - deferredTimeline.getWindowCount(); if (mediaSourceHolder.childIndex + 1 < mediaSourceHolders.size()) {
int periodOffsetUpdate = timeline.getPeriodCount() - deferredTimeline.getPeriodCount(); MediaSourceHolder nextHolder = mediaSourceHolders.get(mediaSourceHolder.childIndex + 1);
if (windowOffsetUpdate != 0 || periodOffsetUpdate != 0) { int windowOffsetUpdate =
correctOffsets( timeline.getWindowCount()
mediaSourceHolder.childIndex + 1, - (nextHolder.firstWindowIndexInChild - mediaSourceHolder.firstWindowIndexInChild);
/* childIndexUpdate= */ 0, if (windowOffsetUpdate != 0) {
windowOffsetUpdate, correctOffsets(
periodOffsetUpdate); mediaSourceHolder.childIndex + 1, /* childIndexUpdate= */ 0, windowOffsetUpdate);
}
} }
if (mediaSourceHolder.isPrepared) { if (mediaSourceHolder.isPrepared) {
mediaSourceHolder.timeline = deferredTimeline.cloneWithUpdatedTimeline(timeline); mediaSourceHolder.timeline = deferredTimeline.cloneWithUpdatedTimeline(timeline);
@ -828,11 +817,7 @@ public final class ConcatenatingMediaSource extends CompositeMediaSource<MediaSo
MediaSourceHolder holder = mediaSourceHolders.remove(index); MediaSourceHolder holder = mediaSourceHolders.remove(index);
mediaSourceByUid.remove(holder.uid); mediaSourceByUid.remove(holder.uid);
Timeline oldTimeline = holder.timeline; Timeline oldTimeline = holder.timeline;
correctOffsets( correctOffsets(index, /* childIndexUpdate= */ -1, -oldTimeline.getWindowCount());
index,
/* childIndexUpdate= */ -1,
-oldTimeline.getWindowCount(),
-oldTimeline.getPeriodCount());
holder.isRemoved = true; holder.isRemoved = true;
maybeReleaseChildSource(holder); maybeReleaseChildSource(holder);
} }
@ -841,25 +826,22 @@ public final class ConcatenatingMediaSource extends CompositeMediaSource<MediaSo
int startIndex = Math.min(currentIndex, newIndex); int startIndex = Math.min(currentIndex, newIndex);
int endIndex = Math.max(currentIndex, newIndex); int endIndex = Math.max(currentIndex, newIndex);
int windowOffset = mediaSourceHolders.get(startIndex).firstWindowIndexInChild; int windowOffset = mediaSourceHolders.get(startIndex).firstWindowIndexInChild;
int periodOffset = mediaSourceHolders.get(startIndex).firstPeriodIndexInChild;
mediaSourceHolders.add(newIndex, mediaSourceHolders.remove(currentIndex)); mediaSourceHolders.add(newIndex, mediaSourceHolders.remove(currentIndex));
for (int i = startIndex; i <= endIndex; i++) { for (int i = startIndex; i <= endIndex; i++) {
MediaSourceHolder holder = mediaSourceHolders.get(i); MediaSourceHolder holder = mediaSourceHolders.get(i);
holder.childIndex = i;
holder.firstWindowIndexInChild = windowOffset; holder.firstWindowIndexInChild = windowOffset;
holder.firstPeriodIndexInChild = periodOffset;
windowOffset += holder.timeline.getWindowCount(); windowOffset += holder.timeline.getWindowCount();
periodOffset += holder.timeline.getPeriodCount();
} }
} }
private void correctOffsets( private void correctOffsets(int startIndex, int childIndexUpdate, int windowOffsetUpdate) {
int startIndex, int childIndexUpdate, int windowOffsetUpdate, int periodOffsetUpdate) { // TODO: Replace window index with uid in reporting to get rid of this inefficient method and
windowCount += windowOffsetUpdate; // the childIndex and firstWindowIndexInChild variables.
periodCount += periodOffsetUpdate;
for (int i = startIndex; i < mediaSourceHolders.size(); i++) { for (int i = startIndex; i < mediaSourceHolders.size(); i++) {
mediaSourceHolders.get(i).childIndex += childIndexUpdate; MediaSourceHolder holder = mediaSourceHolders.get(i);
mediaSourceHolders.get(i).firstWindowIndexInChild += windowOffsetUpdate; holder.childIndex += childIndexUpdate;
mediaSourceHolders.get(i).firstPeriodIndexInChild += periodOffsetUpdate; holder.firstWindowIndexInChild += windowOffsetUpdate;
} }
} }
@ -892,7 +874,7 @@ public final class ConcatenatingMediaSource extends CompositeMediaSource<MediaSo
} }
/** Data class to hold playlist media sources together with meta data needed to process them. */ /** Data class to hold playlist media sources together with meta data needed to process them. */
/* package */ static final class MediaSourceHolder implements Comparable<MediaSourceHolder> { /* package */ static final class MediaSourceHolder {
public final MediaSource mediaSource; public final MediaSource mediaSource;
public final Object uid; public final Object uid;
@ -901,7 +883,6 @@ public final class ConcatenatingMediaSource extends CompositeMediaSource<MediaSo
public DeferredTimeline timeline; public DeferredTimeline timeline;
public int childIndex; public int childIndex;
public int firstWindowIndexInChild; public int firstWindowIndexInChild;
public int firstPeriodIndexInChild;
public boolean hasStartedPreparing; public boolean hasStartedPreparing;
public boolean isPrepared; public boolean isPrepared;
public boolean isRemoved; public boolean isRemoved;
@ -913,20 +894,14 @@ public final class ConcatenatingMediaSource extends CompositeMediaSource<MediaSo
this.uid = new Object(); this.uid = new Object();
} }
public void reset(int childIndex, int firstWindowIndexInChild, int firstPeriodIndexInChild) { public void reset(int childIndex, int firstWindowIndexInChild) {
this.childIndex = childIndex; this.childIndex = childIndex;
this.firstWindowIndexInChild = firstWindowIndexInChild; this.firstWindowIndexInChild = firstWindowIndexInChild;
this.firstPeriodIndexInChild = firstPeriodIndexInChild;
this.hasStartedPreparing = false; this.hasStartedPreparing = false;
this.isPrepared = false; this.isPrepared = false;
this.isRemoved = false; this.isRemoved = false;
this.activeMediaPeriods.clear(); this.activeMediaPeriods.clear();
} }
@Override
public int compareTo(@NonNull MediaSourceHolder other) {
return this.firstPeriodIndexInChild - other.firstPeriodIndexInChild;
}
} }
/** Message used to post actions from app thread to playback thread. */ /** Message used to post actions from app thread to playback thread. */
@ -956,13 +931,9 @@ public final class ConcatenatingMediaSource extends CompositeMediaSource<MediaSo
public ConcatenatedTimeline( public ConcatenatedTimeline(
Collection<MediaSourceHolder> mediaSourceHolders, Collection<MediaSourceHolder> mediaSourceHolders,
int windowCount,
int periodCount,
ShuffleOrder shuffleOrder, ShuffleOrder shuffleOrder,
boolean isAtomic) { boolean isAtomic) {
super(isAtomic, shuffleOrder); super(isAtomic, shuffleOrder);
this.windowCount = windowCount;
this.periodCount = periodCount;
int childCount = mediaSourceHolders.size(); int childCount = mediaSourceHolders.size();
firstPeriodInChildIndices = new int[childCount]; firstPeriodInChildIndices = new int[childCount];
firstWindowInChildIndices = new int[childCount]; firstWindowInChildIndices = new int[childCount];
@ -970,13 +941,19 @@ public final class ConcatenatingMediaSource extends CompositeMediaSource<MediaSo
uids = new Object[childCount]; uids = new Object[childCount];
childIndexByUid = new HashMap<>(); childIndexByUid = new HashMap<>();
int index = 0; int index = 0;
int windowCount = 0;
int periodCount = 0;
for (MediaSourceHolder mediaSourceHolder : mediaSourceHolders) { for (MediaSourceHolder mediaSourceHolder : mediaSourceHolders) {
timelines[index] = mediaSourceHolder.timeline; timelines[index] = mediaSourceHolder.timeline;
firstPeriodInChildIndices[index] = mediaSourceHolder.firstPeriodIndexInChild; firstWindowInChildIndices[index] = windowCount;
firstWindowInChildIndices[index] = mediaSourceHolder.firstWindowIndexInChild; firstPeriodInChildIndices[index] = periodCount;
windowCount += timelines[index].getWindowCount();
periodCount += timelines[index].getPeriodCount();
uids[index] = mediaSourceHolder.uid; uids[index] = mediaSourceHolder.uid;
childIndexByUid.put(uids[index], index++); childIndexByUid.put(uids[index], index++);
} }
this.windowCount = windowCount;
this.periodCount = periodCount;
} }
@Override @Override