Improve MediaSource threading constraints documentation

And fix violation of this in AdsMediaSource.

#minor-release

PiperOrigin-RevId: 534441648
This commit is contained in:
tonihei 2023-05-23 17:00:31 +01:00
parent 5a5c3ce3bd
commit c73955a4cd
2 changed files with 57 additions and 19 deletions

View File

@ -46,9 +46,13 @@ import java.io.IOException;
* way for the player to load and read the media. * way for the player to load and read the media.
* </ul> * </ul>
* *
* All methods are called on the player's internal playback thread, as described in the {@link * <p>{@code MediaSource} methods should not be called from application code. Instead, the playback
* ExoPlayer} Javadoc. They should not be called directly from application code. Instances can be * logic in {@link ExoPlayer} will trigger methods at the right time.
* re-used, but only for one {@link ExoPlayer} instance simultaneously. *
* <p>Instances can be re-used, but only for one {@link ExoPlayer} instance simultaneously.
*
* <p>MediaSource methods will be called on one of two threads, an application thread or a playback
* thread. Each method is documented with the thread it is called on.
*/ */
public interface MediaSource { public interface MediaSource {
@ -165,6 +169,10 @@ public interface MediaSource {
* Adds a {@link MediaSourceEventListener} to the list of listeners which are notified of media * Adds a {@link MediaSourceEventListener} to the list of listeners which are notified of media
* source events. * source events.
* *
* <p>Should not be called directly from application code.
*
* <p>This method must be called on the playback thread.
*
* @param handler A handler on the which listener events will be posted. * @param handler A handler on the which listener events will be posted.
* @param eventListener The listener to be added. * @param eventListener The listener to be added.
*/ */
@ -175,6 +183,10 @@ public interface MediaSource {
* Removes a {@link MediaSourceEventListener} from the list of listeners which are notified of * Removes a {@link MediaSourceEventListener} from the list of listeners which are notified of
* media source events. * media source events.
* *
* <p>Should not be called directly from application code.
*
* <p>This method must be called on the playback thread.
*
* @param eventListener The listener to be removed. * @param eventListener The listener to be removed.
*/ */
@UnstableApi @UnstableApi
@ -184,6 +196,10 @@ public interface MediaSource {
* Adds a {@link DrmSessionEventListener} to the list of listeners which are notified of DRM * Adds a {@link DrmSessionEventListener} to the list of listeners which are notified of DRM
* events for this media source. * events for this media source.
* *
* <p>Should not be called directly from application code.
*
* <p>This method must be called on the playback thread.
*
* @param handler A handler on the which listener events will be posted. * @param handler A handler on the which listener events will be posted.
* @param eventListener The listener to be added. * @param eventListener The listener to be added.
*/ */
@ -194,6 +210,10 @@ public interface MediaSource {
* Removes a {@link DrmSessionEventListener} from the list of listeners which are notified of DRM * Removes a {@link DrmSessionEventListener} from the list of listeners which are notified of DRM
* events for this media source. * events for this media source.
* *
* <p>Should not be called directly from application code.
*
* <p>This method must be called on the playback thread.
*
* @param eventListener The listener to be removed. * @param eventListener The listener to be removed.
*/ */
@UnstableApi @UnstableApi
@ -203,12 +223,16 @@ public interface MediaSource {
* Returns the initial placeholder timeline that is returned immediately when the real timeline is * Returns the initial placeholder timeline that is returned immediately when the real timeline is
* not yet known, or null to let the player create an initial timeline. * not yet known, or null to let the player create an initial timeline.
* *
* <p>Should not be called directly from application code.
*
* <p>The initial timeline must use the same uids for windows and periods that the real timeline * <p>The initial timeline must use the same uids for windows and periods that the real timeline
* will use. It also must provide windows which are marked as dynamic to indicate that the window * will use. It also must provide windows which are marked as dynamic to indicate that the window
* is expected to change when the real timeline arrives. * is expected to change when the real timeline arrives.
* *
* <p>Any media source which has multiple windows should typically provide such an initial * <p>Any media source which has multiple windows should typically provide such an initial
* timeline to make sure the player reports the correct number of windows immediately. * timeline to make sure the player reports the correct number of windows immediately.
*
* <p>This method must be called on the application thread.
*/ */
@UnstableApi @UnstableApi
@Nullable @Nullable
@ -219,8 +243,12 @@ public interface MediaSource {
/** /**
* Returns true if the media source is guaranteed to never have zero or more than one window. * Returns true if the media source is guaranteed to never have zero or more than one window.
* *
* <p>Should not be called directly from application code.
*
* <p>The default implementation returns {@code true}. * <p>The default implementation returns {@code true}.
* *
* <p>This method must be called on the application thread.
*
* @return true if the source has exactly one window. * @return true if the source has exactly one window.
*/ */
@UnstableApi @UnstableApi
@ -228,7 +256,13 @@ public interface MediaSource {
return true; return true;
} }
/** Returns the {@link MediaItem} whose media is provided by the source. */ /**
* Returns the {@link MediaItem} whose media is provided by the source.
*
* <p>Should not be called directly from application code.
*
* <p>This method must be called on the application thread.
*/
@UnstableApi @UnstableApi
MediaItem getMediaItem(); MediaItem getMediaItem();
@ -255,6 +289,8 @@ public interface MediaSource {
* <p>For each call to this method, a call to {@link #releaseSource(MediaSourceCaller)} is needed * <p>For each call to this method, a call to {@link #releaseSource(MediaSourceCaller)} is needed
* to remove the caller and to release the source if no longer required. * to remove the caller and to release the source if no longer required.
* *
* <p>This method must be called on the playback thread.
*
* @param caller The {@link MediaSourceCaller} to be registered. * @param caller The {@link MediaSourceCaller} to be registered.
* @param mediaTransferListener The transfer listener which should be informed of any media data * @param mediaTransferListener The transfer listener which should be informed of any media data
* transfers. May be null if no listener is available. Note that this listener should be only * transfers. May be null if no listener is available. Note that this listener should be only
@ -273,8 +309,8 @@ public interface MediaSource {
* *
* <p>Should not be called directly from application code. * <p>Should not be called directly from application code.
* *
* <p>Must only be called after {@link #prepareSource(MediaSourceCaller, TransferListener, * <p>This method must be called on the playback thread and only after {@link
* PlayerId)}. * #prepareSource(MediaSourceCaller, TransferListener, PlayerId)}.
*/ */
@UnstableApi @UnstableApi
void maybeThrowSourceInfoRefreshError() throws IOException; void maybeThrowSourceInfoRefreshError() throws IOException;
@ -284,8 +320,8 @@ public interface MediaSource {
* *
* <p>Should not be called directly from application code. * <p>Should not be called directly from application code.
* *
* <p>Must only be called after {@link #prepareSource(MediaSourceCaller, TransferListener, * <p>This method must be called on the playback thread and only after {@link
* PlayerId)}. * #prepareSource(MediaSourceCaller, TransferListener, PlayerId)}.
* *
* @param caller The {@link MediaSourceCaller} enabling the source. * @param caller The {@link MediaSourceCaller} enabling the source.
*/ */
@ -297,7 +333,7 @@ public interface MediaSource {
* *
* <p>Should not be called directly from application code. * <p>Should not be called directly from application code.
* *
* <p>Must only be called if the source is enabled. * <p>This method must be called on the playback thread and only if the source is enabled.
* *
* @param id The identifier of the period. * @param id The identifier of the period.
* @param allocator An {@link Allocator} from which to obtain media buffer allocations. * @param allocator An {@link Allocator} from which to obtain media buffer allocations.
@ -312,6 +348,8 @@ public interface MediaSource {
* *
* <p>Should not be called directly from application code. * <p>Should not be called directly from application code.
* *
* <p>This method must be called on the playback thread.
*
* @param mediaPeriod The period to release. * @param mediaPeriod The period to release.
*/ */
@UnstableApi @UnstableApi
@ -323,9 +361,9 @@ public interface MediaSource {
* *
* <p>Should not be called directly from application code. * <p>Should not be called directly from application code.
* *
* <p>Must only be called after all {@link MediaPeriod MediaPeriods} previously created by {@link * <p>This method must be called on the playback thread and only after all {@link MediaPeriod
* #createPeriod(MediaPeriodId, Allocator, long)} have been released by {@link * MediaPeriods} previously created by {@link #createPeriod(MediaPeriodId, Allocator, long)} have
* #releasePeriod(MediaPeriod)}. * been released by {@link #releasePeriod(MediaPeriod)}.
* *
* @param caller The {@link MediaSourceCaller} disabling the source. * @param caller The {@link MediaSourceCaller} disabling the source.
*/ */
@ -337,8 +375,8 @@ public interface MediaSource {
* *
* <p>Should not be called directly from application code. * <p>Should not be called directly from application code.
* *
* <p>Must only be called if all created {@link MediaPeriod MediaPeriods} have been released by * <p>This method must be called on the playback thread and only if all created {@link MediaPeriod
* {@link #releasePeriod(MediaPeriod)}. * MediaPeriods} have been released by {@link #releasePeriod(MediaPeriod)}.
* *
* @param caller The {@link MediaSourceCaller} to be unregistered. * @param caller The {@link MediaSourceCaller} to be unregistered.
*/ */

View File

@ -132,6 +132,7 @@ public final class AdsMediaSource extends CompositeMediaSource<MediaPeriodId> {
new MediaPeriodId(/* periodUid= */ new Object()); new MediaPeriodId(/* periodUid= */ new Object());
private final MediaSource contentMediaSource; private final MediaSource contentMediaSource;
@Nullable final MediaItem.DrmConfiguration contentDrmConfiguration;
private final MediaSource.Factory adMediaSourceFactory; private final MediaSource.Factory adMediaSourceFactory;
private final AdsLoader adsLoader; private final AdsLoader adsLoader;
private final AdViewProvider adViewProvider; private final AdViewProvider adViewProvider;
@ -168,6 +169,8 @@ public final class AdsMediaSource extends CompositeMediaSource<MediaPeriodId> {
AdsLoader adsLoader, AdsLoader adsLoader,
AdViewProvider adViewProvider) { AdViewProvider adViewProvider) {
this.contentMediaSource = contentMediaSource; this.contentMediaSource = contentMediaSource;
this.contentDrmConfiguration =
checkNotNull(contentMediaSource.getMediaItem().localConfiguration).drmConfiguration;
this.adMediaSourceFactory = adMediaSourceFactory; this.adMediaSourceFactory = adMediaSourceFactory;
this.adsLoader = adsLoader; this.adsLoader = adsLoader;
this.adViewProvider = adViewProvider; this.adViewProvider = adViewProvider;
@ -318,11 +321,8 @@ public final class AdsMediaSource extends CompositeMediaSource<MediaPeriodId> {
if (adUri != null) { if (adUri != null) {
MediaItem.Builder adMediaItem = new MediaItem.Builder().setUri(adUri); MediaItem.Builder adMediaItem = new MediaItem.Builder().setUri(adUri);
// Propagate the content's DRM config into the ad media source. // Propagate the content's DRM config into the ad media source.
@Nullable if (contentDrmConfiguration != null) {
MediaItem.LocalConfiguration contentLocalConfiguration = adMediaItem.setDrmConfiguration(contentDrmConfiguration);
contentMediaSource.getMediaItem().localConfiguration;
if (contentLocalConfiguration != null) {
adMediaItem.setDrmConfiguration(contentLocalConfiguration.drmConfiguration);
} }
MediaSource adMediaSource = adMediaSourceFactory.createMediaSource(adMediaItem.build()); MediaSource adMediaSource = adMediaSourceFactory.createMediaSource(adMediaItem.build());
adMediaSourceHolder.initializeWithMediaSource(adMediaSource, adUri); adMediaSourceHolder.initializeWithMediaSource(adMediaSource, adUri);