-
Notifications
You must be signed in to change notification settings - Fork 6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move ad playback state into ImaAdsLoader
Once background and resuming is supported, the ads loader will be kept when the player is destroyed and recreated. Move the state relating to the structure of ads and what ads have been loaded/played out of the media source and into the loader so the information is not lost when the source is released, in preparation for supporting background and resuming. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=161503571
- Loading branch information
1 parent
b31fd5b
commit ef56c9f
Showing
6 changed files
with
206 additions
and
190 deletions.
There are no files selected for viewing
120 changes: 120 additions & 0 deletions
120
extensions/ima/src/main/java/com/google/android/exoplayer2/ext/ima/AdPlaybackState.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
/* | ||
* Copyright (C) 2017 The Android Open Source Project | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.google.android.exoplayer2.ext.ima; | ||
|
||
import android.net.Uri; | ||
import com.google.android.exoplayer2.C; | ||
import java.util.Arrays; | ||
|
||
/** | ||
* Represents the structure of ads to play and the state of loaded/played ads. | ||
*/ | ||
/* package */ final class AdPlaybackState { | ||
|
||
/** | ||
* The number of ad groups. | ||
*/ | ||
public final int adGroupCount; | ||
/** | ||
* The times of ad groups, in microseconds. A final element with the value | ||
* {@link C#TIME_END_OF_SOURCE} indicates a postroll ad. | ||
*/ | ||
public final long[] adGroupTimesUs; | ||
/** | ||
* The number of ads in each ad group. An element may be {@link C#LENGTH_UNSET} if the number of | ||
* ads is not yet known. | ||
*/ | ||
public final int[] adCounts; | ||
/** | ||
* The number of ads loaded so far in each ad group. | ||
*/ | ||
public final int[] adsLoadedCounts; | ||
/** | ||
* The number of ads played so far in each ad group. | ||
*/ | ||
public final int[] adsPlayedCounts; | ||
/** | ||
* The URI of each ad in each ad group. | ||
*/ | ||
public final Uri[][] adUris; | ||
|
||
/** | ||
* Creates a new ad playback state with the specified ad group times. | ||
* | ||
* @param adGroupTimesUs The times of ad groups in microseconds. A final element with the value | ||
* {@link C#TIME_END_OF_SOURCE} indicates that there is a postroll ad. | ||
*/ | ||
public AdPlaybackState(long[] adGroupTimesUs) { | ||
this.adGroupTimesUs = adGroupTimesUs; | ||
adGroupCount = adGroupTimesUs.length; | ||
adsPlayedCounts = new int[adGroupCount]; | ||
adCounts = new int[adGroupCount]; | ||
Arrays.fill(adCounts, C.LENGTH_UNSET); | ||
adUris = new Uri[adGroupCount][]; | ||
Arrays.fill(adUris, new Uri[0]); | ||
adsLoadedCounts = new int[adGroupTimesUs.length]; | ||
} | ||
|
||
private AdPlaybackState(long[] adGroupTimesUs, int[] adCounts, int[] adsLoadedCounts, | ||
int[] adsPlayedCounts, Uri[][] adUris) { | ||
this.adGroupTimesUs = adGroupTimesUs; | ||
this.adCounts = adCounts; | ||
this.adsLoadedCounts = adsLoadedCounts; | ||
this.adsPlayedCounts = adsPlayedCounts; | ||
this.adUris = adUris; | ||
adGroupCount = adGroupTimesUs.length; | ||
} | ||
|
||
/** | ||
* Returns a deep copy of this instance. | ||
*/ | ||
public AdPlaybackState copy() { | ||
Uri[][] adUris = new Uri[adGroupTimesUs.length][]; | ||
for (int i = 0; i < this.adUris.length; i++) { | ||
adUris[i] = Arrays.copyOf(this.adUris[i], this.adUris[i].length); | ||
} | ||
return new AdPlaybackState(Arrays.copyOf(adGroupTimesUs, adGroupCount), | ||
Arrays.copyOf(adCounts, adGroupCount), | ||
Arrays.copyOf(adsLoadedCounts, adGroupCount), | ||
Arrays.copyOf(adsPlayedCounts, adGroupCount), | ||
adUris); | ||
} | ||
|
||
/** | ||
* Sets the number of ads in the specified ad group. | ||
*/ | ||
public void setAdCount(int adGroupIndex, int adCount) { | ||
adCounts[adGroupIndex] = adCount; | ||
} | ||
|
||
/** | ||
* Adds an ad to the specified ad group. | ||
*/ | ||
public void addAdUri(int adGroupIndex, Uri uri) { | ||
int adIndexInAdGroup = adUris[adGroupIndex].length; | ||
adUris[adGroupIndex] = Arrays.copyOf(adUris[adGroupIndex], adIndexInAdGroup + 1); | ||
adUris[adGroupIndex][adIndexInAdGroup] = uri; | ||
adsLoadedCounts[adGroupIndex]++; | ||
} | ||
|
||
/** | ||
* Marks the last ad in the specified ad group as played. | ||
*/ | ||
public void playedAd(int adGroupIndex) { | ||
adsPlayedCounts[adGroupIndex]++; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.