No-op ConcatenatingMediaSource simplification
- Make addMediaSource a specific case of addMediaSources. - Make clear a specific case of removeMediaSourceRange. - Make removeMediaSource a specific case of removeMediaSourceRange. - Remove the unnecessary message handling and constants. - Move a method so that depending appears before depended. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=212809667
This commit is contained in:
parent
62532f9a2e
commit
3e4115ff3c
@ -48,14 +48,11 @@ public class ConcatenatingMediaSource extends CompositeMediaSource<MediaSourceHo
|
|||||||
implements PlayerMessage.Target {
|
implements PlayerMessage.Target {
|
||||||
|
|
||||||
private static final int MSG_ADD = 0;
|
private static final int MSG_ADD = 0;
|
||||||
private static final int MSG_ADD_MULTIPLE = 1;
|
private static final int MSG_REMOVE = 1;
|
||||||
private static final int MSG_REMOVE = 2;
|
private static final int MSG_MOVE = 2;
|
||||||
private static final int MSG_REMOVE_RANGE = 3;
|
private static final int MSG_SET_SHUFFLE_ORDER = 3;
|
||||||
private static final int MSG_MOVE = 4;
|
private static final int MSG_NOTIFY_LISTENER = 4;
|
||||||
private static final int MSG_CLEAR = 5;
|
private static final int MSG_ON_COMPLETION = 5;
|
||||||
private static final int MSG_SET_SHUFFLE_ORDER = 6;
|
|
||||||
private static final int MSG_NOTIFY_LISTENER = 7;
|
|
||||||
private static final int MSG_ON_COMPLETION = 8;
|
|
||||||
|
|
||||||
// Accessed on the app thread.
|
// Accessed on the app thread.
|
||||||
private final List<MediaSourceHolder> mediaSourcesPublic;
|
private final List<MediaSourceHolder> mediaSourcesPublic;
|
||||||
@ -180,18 +177,7 @@ public class ConcatenatingMediaSource extends CompositeMediaSource<MediaSourceHo
|
|||||||
*/
|
*/
|
||||||
public final synchronized void addMediaSource(
|
public final synchronized void addMediaSource(
|
||||||
int index, MediaSource mediaSource, @Nullable Runnable actionOnCompletion) {
|
int index, MediaSource mediaSource, @Nullable Runnable actionOnCompletion) {
|
||||||
Assertions.checkNotNull(mediaSource);
|
addMediaSources(index, Collections.singletonList(mediaSource), actionOnCompletion);
|
||||||
MediaSourceHolder mediaSourceHolder = new MediaSourceHolder(mediaSource);
|
|
||||||
mediaSourcesPublic.add(index, mediaSourceHolder);
|
|
||||||
if (player != null) {
|
|
||||||
player
|
|
||||||
.createMessage(this)
|
|
||||||
.setType(MSG_ADD)
|
|
||||||
.setPayload(new MessageData<>(index, mediaSourceHolder, actionOnCompletion))
|
|
||||||
.send();
|
|
||||||
} else if (actionOnCompletion != null) {
|
|
||||||
actionOnCompletion.run();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -253,7 +239,7 @@ public class ConcatenatingMediaSource extends CompositeMediaSource<MediaSourceHo
|
|||||||
if (player != null && !mediaSources.isEmpty()) {
|
if (player != null && !mediaSources.isEmpty()) {
|
||||||
player
|
player
|
||||||
.createMessage(this)
|
.createMessage(this)
|
||||||
.setType(MSG_ADD_MULTIPLE)
|
.setType(MSG_ADD)
|
||||||
.setPayload(new MessageData<>(index, mediaSourceHolders, actionOnCompletion))
|
.setPayload(new MessageData<>(index, mediaSourceHolders, actionOnCompletion))
|
||||||
.send();
|
.send();
|
||||||
} else if (actionOnCompletion != null) {
|
} else if (actionOnCompletion != null) {
|
||||||
@ -293,16 +279,7 @@ public class ConcatenatingMediaSource extends CompositeMediaSource<MediaSourceHo
|
|||||||
*/
|
*/
|
||||||
public final synchronized void removeMediaSource(
|
public final synchronized void removeMediaSource(
|
||||||
int index, @Nullable Runnable actionOnCompletion) {
|
int index, @Nullable Runnable actionOnCompletion) {
|
||||||
mediaSourcesPublic.remove(index);
|
removeMediaSourceRange(index, index + 1, actionOnCompletion);
|
||||||
if (player != null) {
|
|
||||||
player
|
|
||||||
.createMessage(this)
|
|
||||||
.setType(MSG_REMOVE)
|
|
||||||
.setPayload(new MessageData<Void>(index, null, actionOnCompletion))
|
|
||||||
.send();
|
|
||||||
} else if (actionOnCompletion != null) {
|
|
||||||
actionOnCompletion.run();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -351,7 +328,7 @@ public class ConcatenatingMediaSource extends CompositeMediaSource<MediaSourceHo
|
|||||||
if (player != null) {
|
if (player != null) {
|
||||||
player
|
player
|
||||||
.createMessage(this)
|
.createMessage(this)
|
||||||
.setType(MSG_REMOVE_RANGE)
|
.setType(MSG_REMOVE)
|
||||||
.setPayload(new MessageData<>(fromIndex, toIndex, actionOnCompletion))
|
.setPayload(new MessageData<>(fromIndex, toIndex, actionOnCompletion))
|
||||||
.send();
|
.send();
|
||||||
} else if (actionOnCompletion != null) {
|
} else if (actionOnCompletion != null) {
|
||||||
@ -411,12 +388,7 @@ public class ConcatenatingMediaSource extends CompositeMediaSource<MediaSourceHo
|
|||||||
* has been cleared.
|
* has been cleared.
|
||||||
*/
|
*/
|
||||||
public final synchronized void clear(@Nullable Runnable actionOnCompletion) {
|
public final synchronized void clear(@Nullable Runnable actionOnCompletion) {
|
||||||
mediaSourcesPublic.clear();
|
removeMediaSourceRange(0, getSize(), actionOnCompletion);
|
||||||
if (player != null) {
|
|
||||||
player.createMessage(this).setType(MSG_CLEAR).setPayload(actionOnCompletion).send();
|
|
||||||
} else if (actionOnCompletion != null) {
|
|
||||||
actionOnCompletion.run();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Returns the number of media sources in the playlist. */
|
/** Returns the number of media sources in the playlist. */
|
||||||
@ -578,37 +550,27 @@ public class ConcatenatingMediaSource extends CompositeMediaSource<MediaSourceHo
|
|||||||
}
|
}
|
||||||
switch (messageType) {
|
switch (messageType) {
|
||||||
case MSG_ADD:
|
case MSG_ADD:
|
||||||
MessageData<MediaSourceHolder> addMessage = (MessageData<MediaSourceHolder>) message;
|
MessageData<Collection<MediaSourceHolder>> addMessage =
|
||||||
shuffleOrder = shuffleOrder.cloneAndInsert(addMessage.index, 1);
|
(MessageData<Collection<MediaSourceHolder>>) message;
|
||||||
addMediaSourceInternal(addMessage.index, addMessage.customData);
|
shuffleOrder = shuffleOrder.cloneAndInsert(addMessage.index, addMessage.customData.size());
|
||||||
|
addMediaSourcesInternal(addMessage.index, addMessage.customData);
|
||||||
scheduleListenerNotification(addMessage.actionOnCompletion);
|
scheduleListenerNotification(addMessage.actionOnCompletion);
|
||||||
break;
|
break;
|
||||||
case MSG_ADD_MULTIPLE:
|
|
||||||
MessageData<Collection<MediaSourceHolder>> addMultipleMessage =
|
|
||||||
(MessageData<Collection<MediaSourceHolder>>) message;
|
|
||||||
shuffleOrder =
|
|
||||||
shuffleOrder.cloneAndInsert(
|
|
||||||
addMultipleMessage.index, addMultipleMessage.customData.size());
|
|
||||||
addMediaSourcesInternal(addMultipleMessage.index, addMultipleMessage.customData);
|
|
||||||
scheduleListenerNotification(addMultipleMessage.actionOnCompletion);
|
|
||||||
break;
|
|
||||||
case MSG_REMOVE:
|
case MSG_REMOVE:
|
||||||
MessageData<Void> removeMessage = (MessageData<Void>) message;
|
MessageData<Integer> removeMessage = (MessageData<Integer>) message;
|
||||||
shuffleOrder = shuffleOrder.cloneAndRemove(removeMessage.index);
|
int fromIndex = removeMessage.index;
|
||||||
removeMediaSourceInternal(removeMessage.index);
|
int toIndex = removeMessage.customData;
|
||||||
scheduleListenerNotification(removeMessage.actionOnCompletion);
|
if (fromIndex == 0 && toIndex == shuffleOrder.getLength()) {
|
||||||
break;
|
shuffleOrder = shuffleOrder.cloneAndClear();
|
||||||
case MSG_REMOVE_RANGE:
|
} else {
|
||||||
MessageData<Integer> removeRangeMessage = (MessageData<Integer>) message;
|
for (int index = toIndex - 1; index >= fromIndex; index--) {
|
||||||
int fromIndex = removeRangeMessage.index;
|
shuffleOrder = shuffleOrder.cloneAndRemove(index);
|
||||||
int toIndex = removeRangeMessage.customData;
|
}
|
||||||
for (int index = toIndex - 1; index >= fromIndex; index--) {
|
|
||||||
shuffleOrder = shuffleOrder.cloneAndRemove(index);
|
|
||||||
}
|
}
|
||||||
for (int index = toIndex - 1; index >= fromIndex; index--) {
|
for (int index = toIndex - 1; index >= fromIndex; index--) {
|
||||||
removeMediaSourceInternal(index);
|
removeMediaSourceInternal(index);
|
||||||
}
|
}
|
||||||
scheduleListenerNotification(removeRangeMessage.actionOnCompletion);
|
scheduleListenerNotification(removeMessage.actionOnCompletion);
|
||||||
break;
|
break;
|
||||||
case MSG_MOVE:
|
case MSG_MOVE:
|
||||||
MessageData<Integer> moveMessage = (MessageData<Integer>) message;
|
MessageData<Integer> moveMessage = (MessageData<Integer>) message;
|
||||||
@ -617,10 +579,6 @@ public class ConcatenatingMediaSource extends CompositeMediaSource<MediaSourceHo
|
|||||||
moveMediaSourceInternal(moveMessage.index, moveMessage.customData);
|
moveMediaSourceInternal(moveMessage.index, moveMessage.customData);
|
||||||
scheduleListenerNotification(moveMessage.actionOnCompletion);
|
scheduleListenerNotification(moveMessage.actionOnCompletion);
|
||||||
break;
|
break;
|
||||||
case MSG_CLEAR:
|
|
||||||
clearInternal();
|
|
||||||
scheduleListenerNotification((Runnable) message);
|
|
||||||
break;
|
|
||||||
case MSG_SET_SHUFFLE_ORDER:
|
case MSG_SET_SHUFFLE_ORDER:
|
||||||
MessageData<ShuffleOrder> shuffleOrderMessage = (MessageData<ShuffleOrder>) message;
|
MessageData<ShuffleOrder> shuffleOrderMessage = (MessageData<ShuffleOrder>) message;
|
||||||
shuffleOrder = shuffleOrderMessage.customData;
|
shuffleOrder = shuffleOrderMessage.customData;
|
||||||
@ -671,6 +629,13 @@ public class ConcatenatingMediaSource extends CompositeMediaSource<MediaSourceHo
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void addMediaSourcesInternal(
|
||||||
|
int index, Collection<MediaSourceHolder> mediaSourceHolders) {
|
||||||
|
for (MediaSourceHolder mediaSourceHolder : mediaSourceHolders) {
|
||||||
|
addMediaSourceInternal(index++, mediaSourceHolder);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void addMediaSourceInternal(int newIndex, MediaSourceHolder newMediaSourceHolder) {
|
private void addMediaSourceInternal(int newIndex, MediaSourceHolder newMediaSourceHolder) {
|
||||||
if (newIndex > 0) {
|
if (newIndex > 0) {
|
||||||
MediaSourceHolder previousHolder = mediaSourceHolders.get(newIndex - 1);
|
MediaSourceHolder previousHolder = mediaSourceHolders.get(newIndex - 1);
|
||||||
@ -695,13 +660,6 @@ public class ConcatenatingMediaSource extends CompositeMediaSource<MediaSourceHo
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void addMediaSourcesInternal(
|
|
||||||
int index, Collection<MediaSourceHolder> mediaSourceHolders) {
|
|
||||||
for (MediaSourceHolder mediaSourceHolder : mediaSourceHolders) {
|
|
||||||
addMediaSourceInternal(index++, mediaSourceHolder);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void updateMediaSourceInternal(MediaSourceHolder mediaSourceHolder, Timeline timeline) {
|
private void updateMediaSourceInternal(MediaSourceHolder mediaSourceHolder, Timeline timeline) {
|
||||||
if (mediaSourceHolder == null) {
|
if (mediaSourceHolder == null) {
|
||||||
throw new IllegalArgumentException();
|
throw new IllegalArgumentException();
|
||||||
@ -737,12 +695,6 @@ public class ConcatenatingMediaSource extends CompositeMediaSource<MediaSourceHo
|
|||||||
scheduleListenerNotification(/* actionOnCompletion= */ null);
|
scheduleListenerNotification(/* actionOnCompletion= */ null);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void clearInternal() {
|
|
||||||
for (int index = mediaSourceHolders.size() - 1; index >= 0; index--) {
|
|
||||||
removeMediaSourceInternal(index);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void removeMediaSourceInternal(int index) {
|
private void removeMediaSourceInternal(int index) {
|
||||||
MediaSourceHolder holder = mediaSourceHolders.remove(index);
|
MediaSourceHolder holder = mediaSourceHolders.remove(index);
|
||||||
mediaSourceByUid.remove(holder.uid);
|
mediaSourceByUid.remove(holder.uid);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user