Commit 2938d40b by tonihei Committed by Oliver Woodman

Allow to specify the number of removed ad groups in AdPlaybackState.

This helps to remove old ad groups (e.g. those that fell out
of the live window) to keep the data size of AdPlaybackState small.

Also added this case to some existing unit tests to ensure it's
covered.

PiperOrigin-RevId: 376170653
parent 0d0ccadc
...@@ -700,6 +700,14 @@ public abstract class Timeline implements Bundleable { ...@@ -700,6 +700,14 @@ public abstract class Timeline implements Bundleable {
} }
/** /**
* Returns the number of removed ad groups in the period. Ad groups with indices between {@code
* 0} (inclusive) and {@code removedAdGroupCount} (exclusive) will be empty.
*/
public int getRemovedAdGroupCount() {
return adPlaybackState.adGroupCount;
}
/**
* Returns the time of the ad group at index {@code adGroupIndex} in the period, in * Returns the time of the ad group at index {@code adGroupIndex} in the period, in
* microseconds. * microseconds.
* *
......
...@@ -121,7 +121,7 @@ public final class ServerSideInsertedAdsMediaSource extends BaseMediaSource ...@@ -121,7 +121,7 @@ public final class ServerSideInsertedAdsMediaSource extends BaseMediaSource
*/ */
public void setAdPlaybackState(AdPlaybackState adPlaybackState) { public void setAdPlaybackState(AdPlaybackState adPlaybackState) {
checkArgument(adPlaybackState.adGroupCount >= this.adPlaybackState.adGroupCount); checkArgument(adPlaybackState.adGroupCount >= this.adPlaybackState.adGroupCount);
for (int i = 0; i < adPlaybackState.adGroupCount; i++) { for (int i = adPlaybackState.removedAdGroupCount; i < adPlaybackState.adGroupCount; i++) {
AdPlaybackState.AdGroup adGroup = adPlaybackState.getAdGroup(i); AdPlaybackState.AdGroup adGroup = adPlaybackState.getAdGroup(i);
checkArgument(adGroup.isServerSideInserted); checkArgument(adGroup.isServerSideInserted);
if (i < this.adPlaybackState.adGroupCount) { if (i < this.adPlaybackState.adGroupCount) {
......
...@@ -53,7 +53,7 @@ public final class ServerSideInsertedAdsUtil { ...@@ -53,7 +53,7 @@ public final class ServerSideInsertedAdsUtil {
long adGroupInsertionPositionUs = long adGroupInsertionPositionUs =
getMediaPeriodPositionUsForContent( getMediaPeriodPositionUsForContent(
fromPositionUs, /* nextAdGroupIndex= */ C.INDEX_UNSET, adPlaybackState); fromPositionUs, /* nextAdGroupIndex= */ C.INDEX_UNSET, adPlaybackState);
int insertionIndex = 0; int insertionIndex = adPlaybackState.removedAdGroupCount;
while (insertionIndex < adPlaybackState.adGroupCount while (insertionIndex < adPlaybackState.adGroupCount
&& adPlaybackState.getAdGroup(insertionIndex).timeUs != C.TIME_END_OF_SOURCE && adPlaybackState.getAdGroup(insertionIndex).timeUs != C.TIME_END_OF_SOURCE
&& adPlaybackState.getAdGroup(insertionIndex).timeUs <= adGroupInsertionPositionUs) { && adPlaybackState.getAdGroup(insertionIndex).timeUs <= adGroupInsertionPositionUs) {
...@@ -184,7 +184,7 @@ public final class ServerSideInsertedAdsUtil { ...@@ -184,7 +184,7 @@ public final class ServerSideInsertedAdsUtil {
long positionUs, int adGroupIndex, int adIndexInAdGroup, AdPlaybackState adPlaybackState) { long positionUs, int adGroupIndex, int adIndexInAdGroup, AdPlaybackState adPlaybackState) {
AdPlaybackState.AdGroup currentAdGroup = adPlaybackState.getAdGroup(adGroupIndex); AdPlaybackState.AdGroup currentAdGroup = adPlaybackState.getAdGroup(adGroupIndex);
positionUs += currentAdGroup.timeUs; positionUs += currentAdGroup.timeUs;
for (int i = 0; i < adGroupIndex; i++) { for (int i = adPlaybackState.removedAdGroupCount; i < adGroupIndex; i++) {
AdPlaybackState.AdGroup adGroup = adPlaybackState.getAdGroup(i); AdPlaybackState.AdGroup adGroup = adPlaybackState.getAdGroup(i);
for (int j = 0; j < getAdCountInGroup(adPlaybackState, /* adGroupIndex= */ i); j++) { for (int j = 0; j < getAdCountInGroup(adPlaybackState, /* adGroupIndex= */ i); j++) {
positionUs += adGroup.durationsUs[j]; positionUs += adGroup.durationsUs[j];
...@@ -214,7 +214,7 @@ public final class ServerSideInsertedAdsUtil { ...@@ -214,7 +214,7 @@ public final class ServerSideInsertedAdsUtil {
long positionUs, int adGroupIndex, int adIndexInAdGroup, AdPlaybackState adPlaybackState) { long positionUs, int adGroupIndex, int adIndexInAdGroup, AdPlaybackState adPlaybackState) {
AdPlaybackState.AdGroup currentAdGroup = adPlaybackState.getAdGroup(adGroupIndex); AdPlaybackState.AdGroup currentAdGroup = adPlaybackState.getAdGroup(adGroupIndex);
positionUs -= currentAdGroup.timeUs; positionUs -= currentAdGroup.timeUs;
for (int i = 0; i < adGroupIndex; i++) { for (int i = adPlaybackState.removedAdGroupCount; i < adGroupIndex; i++) {
AdPlaybackState.AdGroup adGroup = adPlaybackState.getAdGroup(i); AdPlaybackState.AdGroup adGroup = adPlaybackState.getAdGroup(i);
for (int j = 0; j < getAdCountInGroup(adPlaybackState, /* adGroupIndex= */ i); j++) { for (int j = 0; j < getAdCountInGroup(adPlaybackState, /* adGroupIndex= */ i); j++) {
positionUs -= adGroup.durationsUs[j]; positionUs -= adGroup.durationsUs[j];
...@@ -246,7 +246,7 @@ public final class ServerSideInsertedAdsUtil { ...@@ -246,7 +246,7 @@ public final class ServerSideInsertedAdsUtil {
if (nextAdGroupIndex == C.INDEX_UNSET) { if (nextAdGroupIndex == C.INDEX_UNSET) {
nextAdGroupIndex = adPlaybackState.adGroupCount; nextAdGroupIndex = adPlaybackState.adGroupCount;
} }
for (int i = 0; i < nextAdGroupIndex; i++) { for (int i = adPlaybackState.removedAdGroupCount; i < nextAdGroupIndex; i++) {
AdPlaybackState.AdGroup adGroup = adPlaybackState.getAdGroup(i); AdPlaybackState.AdGroup adGroup = adPlaybackState.getAdGroup(i);
if (adGroup.timeUs == C.TIME_END_OF_SOURCE || adGroup.timeUs > positionUs) { if (adGroup.timeUs == C.TIME_END_OF_SOURCE || adGroup.timeUs > positionUs) {
break; break;
...@@ -283,7 +283,7 @@ public final class ServerSideInsertedAdsUtil { ...@@ -283,7 +283,7 @@ public final class ServerSideInsertedAdsUtil {
if (nextAdGroupIndex == C.INDEX_UNSET) { if (nextAdGroupIndex == C.INDEX_UNSET) {
nextAdGroupIndex = adPlaybackState.adGroupCount; nextAdGroupIndex = adPlaybackState.adGroupCount;
} }
for (int i = 0; i < nextAdGroupIndex; i++) { for (int i = adPlaybackState.removedAdGroupCount; i < nextAdGroupIndex; i++) {
AdPlaybackState.AdGroup adGroup = adPlaybackState.getAdGroup(i); AdPlaybackState.AdGroup adGroup = adPlaybackState.getAdGroup(i);
if (adGroup.timeUs == C.TIME_END_OF_SOURCE if (adGroup.timeUs == C.TIME_END_OF_SOURCE
|| adGroup.timeUs > positionUs - totalAdDurationBeforePositionUs) { || adGroup.timeUs > positionUs - totalAdDurationBeforePositionUs) {
......
...@@ -982,8 +982,9 @@ public class PlayerControlView extends FrameLayout { ...@@ -982,8 +982,9 @@ public class PlayerControlView extends FrameLayout {
} }
for (int j = window.firstPeriodIndex; j <= window.lastPeriodIndex; j++) { for (int j = window.firstPeriodIndex; j <= window.lastPeriodIndex; j++) {
timeline.getPeriod(j, period); timeline.getPeriod(j, period);
int periodAdGroupCount = period.getAdGroupCount(); int removedGroups = period.getRemovedAdGroupCount();
for (int adGroupIndex = 0; adGroupIndex < periodAdGroupCount; adGroupIndex++) { int totalGroups = period.getAdGroupCount();
for (int adGroupIndex = removedGroups; adGroupIndex < totalGroups; adGroupIndex++) {
long adGroupTimeInPeriodUs = period.getAdGroupTimeUs(adGroupIndex); long adGroupTimeInPeriodUs = period.getAdGroupTimeUs(adGroupIndex);
if (adGroupTimeInPeriodUs == C.TIME_END_OF_SOURCE) { if (adGroupTimeInPeriodUs == C.TIME_END_OF_SOURCE) {
if (period.durationUs == C.TIME_UNSET) { if (period.durationUs == C.TIME_UNSET) {
......
...@@ -1345,8 +1345,9 @@ public class StyledPlayerControlView extends FrameLayout { ...@@ -1345,8 +1345,9 @@ public class StyledPlayerControlView extends FrameLayout {
} }
for (int j = window.firstPeriodIndex; j <= window.lastPeriodIndex; j++) { for (int j = window.firstPeriodIndex; j <= window.lastPeriodIndex; j++) {
timeline.getPeriod(j, period); timeline.getPeriod(j, period);
int periodAdGroupCount = period.getAdGroupCount(); int removedGroups = period.getRemovedAdGroupCount();
for (int adGroupIndex = 0; adGroupIndex < periodAdGroupCount; adGroupIndex++) { int totalGroups = period.getAdGroupCount();
for (int adGroupIndex = removedGroups; adGroupIndex < totalGroups; adGroupIndex++) {
long adGroupTimeInPeriodUs = period.getAdGroupTimeUs(adGroupIndex); long adGroupTimeInPeriodUs = period.getAdGroupTimeUs(adGroupIndex);
if (adGroupTimeInPeriodUs == C.TIME_END_OF_SOURCE) { if (adGroupTimeInPeriodUs == C.TIME_END_OF_SOURCE) {
if (period.durationUs == C.TIME_UNSET) { if (period.durationUs == C.TIME_UNSET) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment