Prevent adding multiple IMA SSAI media source instances to the playlist

Currently only a single instance of ImaServerSideAdInsertionMediaSource is
supported at the same time in a playlist. This change makes sure that an
attempt to add multiple instances is prevented by throwing a an exception.

#minor-release

PiperOrigin-RevId: 428743140
This commit is contained in:
bachinger 2022-02-15 12:12:34 +00:00 committed by Ian Baker
parent fe55d982f1
commit b5d5f492e7
2 changed files with 18 additions and 1 deletions

View File

@ -379,6 +379,7 @@ public final class ImaServerSideAdInsertionMediaSource extends CompositeMediaSou
@Override @Override
public void prepareSourceInternal(@Nullable TransferListener mediaTransferListener) { public void prepareSourceInternal(@Nullable TransferListener mediaTransferListener) {
mainHandler.post(() -> assertSingleInstanceInPlaylist(checkNotNull(player)));
super.prepareSourceInternal(mediaTransferListener); super.prepareSourceInternal(mediaTransferListener);
if (loader == null) { if (loader == null) {
Loader loader = new Loader("ImaServerSideAdInsertionMediaSource"); Loader loader = new Loader("ImaServerSideAdInsertionMediaSource");
@ -1152,4 +1153,20 @@ public final class ImaServerSideAdInsertionMediaSource extends CompositeMediaSou
overlayInfo.reasonDetail != null ? overlayInfo.reasonDetail : "Unknown reason")); overlayInfo.reasonDetail != null ? overlayInfo.reasonDetail : "Unknown reason"));
} }
} }
private static void assertSingleInstanceInPlaylist(Player player) {
int counter = 0;
for (int i = 0; i < player.getMediaItemCount(); i++) {
MediaItem mediaItem = player.getMediaItemAt(i);
if (mediaItem.localConfiguration != null
&& C.SSAI_SCHEME.equals(mediaItem.localConfiguration.uri.getScheme())
&& ImaServerSideAdInsertionUriBuilder.IMA_AUTHORITY.equals(
mediaItem.localConfiguration.uri.getAuthority())) {
if (++counter > 1) {
throw new IllegalStateException(
"Multiple IMA server side ad insertion sources not supported.");
}
}
}
}
} }

View File

@ -43,7 +43,7 @@ public final class ImaServerSideAdInsertionUriBuilder {
/** The default timeout for loading the video URI, in milliseconds. */ /** The default timeout for loading the video URI, in milliseconds. */
public static final int DEFAULT_LOAD_VIDEO_TIMEOUT_MS = 10_000; public static final int DEFAULT_LOAD_VIDEO_TIMEOUT_MS = 10_000;
private static final String IMA_AUTHORITY = "dai.google.com"; /* package */ static final String IMA_AUTHORITY = "dai.google.com";
private static final String ADS_ID = "adsId"; private static final String ADS_ID = "adsId";
private static final String ASSET_KEY = "assetKey"; private static final String ASSET_KEY = "assetKey";
private static final String API_KEY = "apiKey"; private static final String API_KEY = "apiKey";