diff --git a/.github/ISSUE_TEMPLATE/bug.yml b/.github/ISSUE_TEMPLATE/bug.yml
index c11e3e85cdf..d81faabc400 100644
--- a/.github/ISSUE_TEMPLATE/bug.yml
+++ b/.github/ISSUE_TEMPLATE/bug.yml
@@ -19,6 +19,8 @@ body:
options:
- Media3 main branch
- Media3 pre-release (alpha, beta or RC not in this list)
+ - Media3 1.11.1
+ - Media3 1.11.0
- Media3 1.10.1
- Media3 1.10.0
- Media3 1.9.2
diff --git a/RELEASENOTES.md b/RELEASENOTES.md
index fdb642707a5..0212bfbdad0 100644
--- a/RELEASENOTES.md
+++ b/RELEASENOTES.md
@@ -1,101 +1,73 @@
# Release notes
-### Unreleased changes
+### 1.11.1 (2026-09-10)
+
+This release includes the following changes since the
+[1.11.0 release](#1110-2026-08-05):
-* Common Library:
* ExoPlayer:
- * Add `Format.selectionPriority` representing the relative preference of
- the `Format` in track selection
- ([#3236](https://github.com/androidx/media/pull/3236)).
-* CompositionPlayer:
-* Transformer:
-* Track Selection:
+ * Move the flag to enable/disable HAGC metadata for progressive media from
+ `MediaSource.Factory` to `ExtractorsFactory`. The previous
+ `setExperimentalEnableHagcPlayback` method is removed and replaced by
+ `ExtractorsFactory.setParseHagcMetadata`. This also resolves an issue
+ where disabling didn't work for Matroska/WebM containers.
+ * Fix a playback stall caused when prewarming a non-transitioning
+ secondary renderer.
+ * Fix pre-warming issue where the `Surface` should be returned to the
+ primary renderer when a seek resets and disables both renderers.
+ * Fix `ArrayIndexOutOfBoundsException` when a live timeline refresh moves
+ the default position past a server-side inserted ad that is currently
+ being played ([#3348](https://github.com/androidx/media/issues/3348)).
+ * Fix a scrubbing mode issue where stale video frames could be briefly
+ displayed when seeking with
+ `ScrubbingModeParameters.allowSkippingMediaCodecFlush` enabled.
* Extractors:
- * AVI: Fix an issue where playing files without keyframe flags on audio
- tracks caused audio loss and `OutOfMemoryError`.
- * MP3: Use gapless-aware durations from Xing/Info headers
- ([#3183](https://github.com/androidx/media/issues/3183)).
- * MP3: Adjust LAME/Xing encoder delay and padding metadata to match
- decoded PCM trimming
- ([#3200](https://github.com/androidx/media/pull/3200)).
-* Inspector:
-* Inspector Frame:
+ * Matroska: Fix issue where Tracks placed after clusters wouldn't result
+ in a seekable timeline
+ ([#3377](https://github.com/androidx/media/issues/3377)).
* Audio:
- * Remove fallback path of EAC3-JOC to EAC3 for playback on Google Pixel
- devices as their standard EAC3 decoders cannot decode EAC3-JOC streams
- ([#3257](https://github.com/androidx/media/pull/3257)).
-* Video:
- * Fix bug in `VideoFrameReleaseHelper` where display changes could trigger
- redundant `Choreographer` callbacks and excessive CPU wakeups on the
- main thread.
-* Text:
-* Metadata:
-* Image:
-* DataSource:
-* DRM:
-* Effect:
-* Effect Lottie:
-* Muxers:
-* IMA extension:
+ * Fix buffer size calculation in `SilenceSkippingAudioProcessor` so that
+ the minimum silence duration is not incorrectly scaled down by the frame
+ size ([#3271](https://github.com/androidx/media/pull/3271)).
+ * Fix 8-bit PCM handling in `PcmAudioUtil` to treat samples as unsigned as
+ defined by Android
+ ([#3271](https://github.com/androidx/media/pull/3271)).
+ * Fix offload issue in which playback could stall during pre-roll or
+ gapless transitions due to limited hardware buffer sizes.
+ * Fix bug in `DefaultAudioSink` where release count doesn't decrease when
+ playback thread is no longer alive
+ ([#3338](https://github.com/androidx/media/issues/3338)).
* Session:
- * Fix double-downscaling of artwork in `MediaSession` when the image size
- is close to the platform limit, resolving blurriness in notifications
- ([#3134](https://github.com/androidx/media/issues/3134)).
- * Fix `ForegroundServiceStartNotAllowedException` crash in
- `MediaNotificationManager` when an asynchronous artwork bitmap load
- callback attempts to start the foreground service while the app is in
- the background ([#3270](https://github.com/androidx/media/issues/3270)).
- * Fix an issue where `PositionInfo` objects received by a
- `MediaController` were missing their original `Timeline` UIDs. The
- session now reliably translates and propagates these UIDs during state
- updates.
-* UI:
- * Fix shutter suppression bug in `PlayerView` and Compose
- `PresentationState` that caused crashes on timelines that do not support
- period UID lookup
- ([#3264](https://github.com/androidx/media/issues/3264)).
-* Downloads:
-* OkHttp extension:
-* Cronet extension:
-* RTMP extension:
+ * Fix `NullPointerException` when an in-process `MediaController` is
+ released from a `Player.Listener` callback
+ ([#3375](https://github.com/androidx/media/issues/3375)).
+ * Fix deadlock on the main thread when a legacy `MediaBrowser` connects to
+ a service returning an asynchronous result from `onGetLibraryRoot()` or
+ `onConnectAsync()`
+ ([#3393](https://github.com/androidx/media/issues/3393)).
* HLS extension:
- * Parse `SCORE` attribute of `EXT-X-STREAM-INF` and
- `EXT-X-I-FRAME-STREAM-INF`, and populate it to
- `Format.selectionPriority`
- ([#3236](https://github.com/androidx/media/pull/3236)).
+ * Fix calculation of content resume offset when the target segment for
+ snapping is not yet in the playlist
+ ([#3322](https://github.com/androidx/media/issues/3322)).
+ * Fix an issue where a fully consumed `HlsMediaChunk` retries loading on
+ receiving `EOFException` from the extractor
+ ([#3350](https://github.com/androidx/media/issues/3350)).
* DASH extension:
- * Add support for parsing multiple `Location` elements from the manifest
- (MPD) and performing location fallback in `DashMediaSource` when
- manifest loads fail.
-* Smooth Streaming extension:
-* RTSP extension:
+ * Fix incorrect sample timestamp calculation for image tracks with a
+ `presentationTimeOffset`
+ ([#3334](https://github.com/androidx/media/issues/3334)).
* Decoder extensions (FFmpeg, VP9, AV1, etc.):
- * Add monochrome video support to bundled dav1d JNI.
-* MIDI extension:
-* Leanback extension:
-* Cast extension:
- * Add `MediaRouteButtonState` and `rememberMediaRouteButtonState` to
- support observing the visibility state of the media route picker, and
- add `state` parameter to `MediaRouteButton`
- ([3172](https://github.com/androidx/media/issues/3172)).
- * Fix bug where `RemoteCastPlayer` could get permanently stuck in a
- track-masking state after a track selection, causing it to ignore
- subsequent track updates from the Cast receiver
- ([#3237](https://github.com/androidx/media/issues/3237)).
- * Fix bug where `DefaultMediaItemConverter` throws a
- `NullPointerException` when trying to join a cast session started from a
- non-Media3 sender.
-* Test Utilities:
- * Add `setPlaybackLooper` to `TestExoPlayerBuilder`.
-* Remove deprecated symbols:
- * Remove `androidx.media3.common.C.generateAudioSessionIdV21`. Use
- `androidx.media3.common.util.Util.generateAudioSessionId` instead.
-
-## 1.11
+ * Opus: Fix memory corruption when multiple `OpusDecoder` instances are
+ initialized concurrently.
+ * MPEG-H: Fix memory leak, truncation of non-ASCII characters, and
+ potential native crash under low-memory conditions when sending commands
+ to the `MpeghUiManager`
+ ([#3365](https://github.com/androidx/media/issues/3365)).
-### 1.11.0-alpha01 (2026-06-23)
+### 1.11.0 (2026-08-05)
-This release includes the following changes since [1.10.1](#1101-2026-05-12):
+This release includes the following changes since the
+[1.10.1 release](#1101-2026-05-12):
* Common library:
* Add `Format.channelMask` to explicitly represent the audio channel mask,
@@ -109,8 +81,6 @@ This release includes the following changes since [1.10.1](#1101-2026-05-12):
count to decrease when an ad group is fully processed
(`hasUnplayedAds()` is `false`), accommodating dynamic ad group resizing
during reset workflows.
- * Add support for ads in multi-period content (e.g., DASH) by splitting
- and offsetting the `AdPlaybackState` for each period.
* Add `getFlags()` and `FLAG_STRICT_DURATION` to `SampleStream` to allow
streams to report flags, and update renderers to check these flags
dynamically.
@@ -180,6 +150,11 @@ This release includes the following changes since [1.10.1](#1101-2026-05-12):
metadata out-of-band to the decoder on API 37+ devices. This behavior
can be disabled via
`DefaultMediaSourceFactory.setExperimentalEnableHagcPlayback(false)`.
+ * Add `Format.selectionPriority` representing the relative preference of
+ the `Format` in track selection
+ ([#3236](https://github.com/androidx/media/pull/3236)).
+ * Fix potential scrubbing mode issue where scrubbing could stall when
+ seeking to 'end of stream'.
* CompositionPlayer:
* Fix an issue where `TrackSelector` instances were not being released.
* Transformer:
@@ -189,6 +164,9 @@ This release includes the following changes since [1.10.1](#1101-2026-05-12):
`AdaptiveTrackSelection.Factory.setTrackFormatComparator` to allow
custom format ordering and ABR selection priority beyond bitrate-only
ordering.
+ * Fix a timing-dependent concurrency issue where rapid sequential updates
+ to track selection parameters could result in some updates being lost or
+ overwritten.
* Extractors:
* MP4, MP3, and FLAC: Add `FLAG_DISABLE_ARTWORK_METADATA` to allow
discarding attached pictures and cover art metadata during container
@@ -233,6 +211,13 @@ This release includes the following changes since [1.10.1](#1101-2026-05-12):
([#3268](https://github.com/androidx/media/pull/3268)).
* MP4: Allow variable length `sgpd` boxes in fragmented MP4 files
([#3243](https://github.com/androidx/media/pull/3243)).
+ * AVI: Fix an issue where playing files without keyframe flags on audio
+ tracks caused audio loss and `OutOfMemoryError`.
+ * MP3: Use gapless-aware durations from Xing/Info headers
+ ([#3183](https://github.com/androidx/media/issues/3183)).
+ * MP3: Adjust LAME/Xing encoder delay and padding metadata to match
+ decoded PCM trimming
+ ([#3200](https://github.com/androidx/media/pull/3200)).
* Audio:
* Add a 100ms grace period in ExoPlayer's audio renderers when
transitioning from ready to not-ready state, to debounce transient
@@ -263,6 +248,9 @@ This release includes the following changes since [1.10.1](#1101-2026-05-12):
time-based audio processing
([#418](https://github.com/androidx/media/issues/418)).
* Add support for big endian 32-bit and 64-bit floating point PCM.
+ * Remove fallback path of EAC3-JOC to EAC3 for playback on Google Pixel
+ devices as their standard EAC3 decoders cannot decode EAC3-JOC streams
+ ([#3257](https://github.com/androidx/media/pull/3257)).
* Video:
* Fix video joining logic to count dropped buffers during joining as
skipped instead of dropped.
@@ -273,6 +261,9 @@ This release includes the following changes since [1.10.1](#1101-2026-05-12):
* Allow configuration of the threshold used to schedule frames early. The
default is 50ms but one can customize the value through
`MediaCodecVideoRenderer.Builder.setMaxEarlyUsThreshold()`.
+ * Fix bug in `VideoFrameReleaseHelper` where display changes could trigger
+ redundant `Choreographer` callbacks and excessive CPU wakeups on the
+ main thread.
* Text:
* TTML: Fallback to `displayAlign` from `style` for regions
([#2559](https://github.com/androidx/media/issues/2559)).
@@ -334,6 +325,23 @@ This release includes the following changes since [1.10.1](#1101-2026-05-12):
`Callback.onConnect` is a candidate to be deprecated.
* Fix issue where Bluetooth AVRCP browsing didn't recognise
`MediaLibraryService` on API 36 and 37.
+ * Fix double-downscaling of artwork in `MediaSession` when the image size
+ is close to the platform limit, resolving blurriness in notifications
+ ([#3134](https://github.com/androidx/media/issues/3134)).
+ * Fix `ForegroundServiceStartNotAllowedException` crash in
+ `MediaNotificationManager` when an asynchronous artwork bitmap load
+ callback attempts to start the foreground service while the app is in
+ the background ([#3270](https://github.com/androidx/media/issues/3270)).
+ * Fix an issue where `PositionInfo` objects received by a
+ `MediaController` were missing their original `Timeline` UIDs. The
+ session now reliably translates and propagates these UIDs during state
+ updates.
+ * Fix crash by catching `ForegroundServiceStartNotAllowedException` in
+ `MediaSessionService.stopSelfSafely()`
+ ([#3310](https://github.com/androidx/media/issues/3310)).
+ * Fix unexpected `onMediaItemTransition()` callback on `MediaController`
+ when seeking inside the same media item
+ ([#3248](https://github.com/androidx/media/issues/3248)).
* UI:
* Fix first frame rendering at the wrong size when `ContentFrame` is
recomposed mid-playback
@@ -364,18 +372,49 @@ This release includes the following changes since [1.10.1](#1101-2026-05-12):
(`ui-compose`) to support preloading of `MediaItems` in sliding window
UI environments. Showcase this functionality in `demo-compose` with a
new `ShortFormPlayerScreen`.
+ * Fix shutter suppression bug in `PlayerView` and Compose
+ `PresentationState` that caused crashes on timelines that do not support
+ period UID lookup
+ ([#3264](https://github.com/androidx/media/issues/3264)).
+* Downloads:
+ * Resolve HLS variables in media playlist when fetched for download
+ ([#3258](https://github.com/androidx/media/issues/3258)).
* Ktor extension:
* Add a new `media3-datasource-ktor` extension module providing a
`KtorDataSource` backed by the [Ktor](https://ktor.io/) HTTP stack.
+ * Fix `KtorDataSource` loading the entire HTTP response body into memory
+ when opening a connection
+ ([#3305](https://github.com/androidx/media/issues/3305)).
* HLS extension:
* Add support for HLS Content Steering and Pathway Cloning
([#1689](https://github.com/androidx/media/issues/1689)).
* Add `setWithAssetListReset` to `HlsInterstitialsAdsLoader` to allow apps
to reset fully processed asset list interstitials back to an unavailable
state for on-demand re-resolution.
+ * Parse `SCORE` attribute of `EXT-X-STREAM-INF` and
+ `EXT-X-I-FRAME-STREAM-INF`, and populate it to
+ `Format.selectionPriority`
+ ([#3236](https://github.com/androidx/media/pull/3236)).
+ * Fix bug where scheduled HLS playlist refreshes continue even if the
+ playlist is no longer primary or active
+ ([#3254](https://github.com/androidx/media/issues/3254)).
+ * Fix calculation of content resume offset when resolving interstitial
+ asset lists ([#3322](https://github.com/androidx/media/issues/3322)).
+ * Fix unexpected audio track timestamp discontinuity when resuming low
+ latency live playback after a playlist request failure
+ ([#3311](https://github.com/androidx/media/issues/3311)).
+ * Fix bug in `HlsMediaPeriod.getStreamKeys()` where the ID3 track in audio
+ renditions is considered as subtitles
+ ([#3333](https://github.com/androidx/media/issues/3333)).
* DASH extension:
* Separate CEA closed captions into different `TrackGroup`s based on
language ([#3113](https://github.com/androidx/media/issues/3113)).
+ * Add support for parsing multiple `Location` elements from the manifest
+ (MPD) and performing location fallback in `DashMediaSource` when
+ manifest loads fail.
+ * Fix crash (`IndexOutOfBoundsException`) when opening manifests with an
+ empty `` element
+ ([#3326](https://github.com/androidx/media/issues/3326)).
* RTSP extension:
* Fix transient playback stalls or failures during RTSP playback
preparation caused by UDP port binding errors.
@@ -385,6 +424,23 @@ This release includes the following changes since [1.10.1](#1101-2026-05-12):
* Add new `CastParams.getShowSystemOutputSwitcherOnCastIconClick()`
option, which triggers the use of the SystemUI Output Switcher instead
of the in-app Cast device picker when available.
+ * Add `MediaRouteButtonState` and `rememberMediaRouteButtonState` to
+ support observing the visibility state of the media route picker, and
+ add `state` parameter to `MediaRouteButton`
+ ([#3172](https://github.com/androidx/media/issues/3172)).
+ * Fix bug where `RemoteCastPlayer` could get permanently stuck in a
+ track-masking state after a track selection, causing it to ignore
+ subsequent track updates from the Cast receiver
+ ([#3237](https://github.com/androidx/media/issues/3237)).
+ * Fix bug where `DefaultMediaItemConverter` throws a
+ `NullPointerException` when trying to join a cast session started from a
+ non-Media3 sender.
+ * Prevent `CastPlayer` from transferring `MediaItems` that are not
+ playable when changing the active player
+ ([#3199](https://github.com/androidx/media/issues/3199)).
+ * Fix bug where unconfigured live streams (`liveConfiguration == UNSET`)
+ were queued as `STREAM_TYPE_BUFFERED`
+ ([#3318](https://github.com/androidx/media/issues/3318)).
* Test utilities:
* Expand `DataSourceContractTest` to include support for `POST` requests
with headers & bodies, add some `POST` resources to
@@ -394,6 +450,7 @@ This release includes the following changes since [1.10.1](#1101-2026-05-12):
`DataSource.open()`. Resources that may result in `C.LENGTH_UNSET`
should indicate this by using
`TestResource.Builder.setMayResolveToUnknownLength(boolean)`.
+ * Add `setPlaybackLooper` to `TestExoPlayerBuilder`.
* Remove deprecated symbols:
* Remove `androidx.media3.exoplayer.MetadataRetriever`. Use
`androidx.media3.inspector.MetadataRetriever` instead.
@@ -408,12 +465,26 @@ This release includes the following changes since [1.10.1](#1101-2026-05-12):
`androidx.media3.extractor.DiscardingTrackOutput` instead.
* Remove `androidx.media3.extractor.DummyExtractorOutput`. Use
`androidx.media3.extractor.NoOpExtractorOutput` instead.
+ * Remove `androidx.media3.common.C.generateAudioSessionIdV21`. Use
+ `androidx.media3.common.util.Util.generateAudioSessionId` instead.
* Build
* Updated the `README.md` instructions for depending on Media3 locally to
use Gradle Composite Builds (`includeBuild`). The `core_settings.gradle`
script has been removed, and the `androidxMediaModulePrefix` and
`androidxMediaEnableMidiModule` properties are no longer used.
+### 1.11.0-rc01 (2026-07-22)
+
+Use the 1.11.0 [stable version](#1110-2026-08-05).
+
+### 1.11.0-beta01 (2026-07-08)
+
+Use the 1.11.0 [stable version](#1110-2026-08-05).
+
+### 1.11.0-alpha01 (2026-06-23)
+
+Use the 1.11.0 [stable version](#1110-2026-08-05).
+
## 1.10
### 1.10.1 (2026-05-12)
diff --git a/demos/compose/src/main/assets/media.exolist.json b/demos/compose/src/main/assets/media.exolist.json
index 17d6a5b30a3..f60ed4cc318 100644
--- a/demos/compose/src/main/assets/media.exolist.json
+++ b/demos/compose/src/main/assets/media.exolist.json
@@ -162,6 +162,10 @@
"name": "AV1 Rooftop (Everyframe T35)",
"uri": "https://storage.googleapis.com/exoplayer-test-media-1/hagc/rooftop_av1_t35track_everyframe_v0.mp4"
},
+ {
+ "name": "HEVC Rooftop (Everyframe T35)",
+ "uri": "https://storage.googleapis.com/exoplayer-test-media-1/hagc/rooftop_hevc_t35track_everyframe_v0.mp4"
+ },
{
"name": "HEVC Lights (SDR Base, Sparse T35)",
"uri": "https://storage.googleapis.com/exoplayer-test-media-1/hagc/lights_hevc_sdrbase_t35track_v0.mp4"
diff --git a/demos/main/src/main/assets/media.exolist.json b/demos/main/src/main/assets/media.exolist.json
index 19b696d049f..1913a74cdb4 100644
--- a/demos/main/src/main/assets/media.exolist.json
+++ b/demos/main/src/main/assets/media.exolist.json
@@ -854,6 +854,10 @@
"name": "AV1 Rooftop (Everyframe T35)",
"uri": "https://storage.googleapis.com/exoplayer-test-media-1/hagc/rooftop_av1_t35track_everyframe_v0.mp4"
},
+ {
+ "name": "HEVC Rooftop (Everyframe T35)",
+ "uri": "https://storage.googleapis.com/exoplayer-test-media-1/hagc/rooftop_hevc_t35track_everyframe_v0.mp4"
+ },
{
"name": "HEVC Lights (SDR Base, Sparse T35)",
"uri": "https://storage.googleapis.com/exoplayer-test-media-1/hagc/lights_hevc_sdrbase_t35track_v0.mp4"
diff --git a/demos/session/src/main/AndroidManifest.xml b/demos/session/src/main/AndroidManifest.xml
index 90819f51fec..91d5053aa3a 100644
--- a/demos/session/src/main/AndroidManifest.xml
+++ b/demos/session/src/main/AndroidManifest.xml
@@ -32,9 +32,6 @@
android:name="com.google.android.gms.car.application"
android:resource="@xml/auto_app_desc" />
-
-
- PlayerTransferState.fromPlayer(sourcePlayer).setToPlayer(targetPlayer);
+ TransferCallback DEFAULT = new DefaultCastPlayerTransferCallback();
/**
* Called immediately before changing the active {@link Player}, with the intended use of
diff --git a/libraries/cast/src/main/java/androidx/media3/cast/CastTimelineTracker.java b/libraries/cast/src/main/java/androidx/media3/cast/CastTimelineTracker.java
index b0b75f1c20c..4ce8545334b 100644
--- a/libraries/cast/src/main/java/androidx/media3/cast/CastTimelineTracker.java
+++ b/libraries/cast/src/main/java/androidx/media3/cast/CastTimelineTracker.java
@@ -108,21 +108,11 @@ public CastTimeline getCastTimeline(RemoteMediaClient remoteMediaClient) {
return CastTimeline.EMPTY_CAST_TIMELINE;
}
- int currentItemId = mediaStatus.getCurrentItemId();
- String currentContentId = checkNotNull(mediaStatus.getMediaInfo()).getContentId();
- MediaItem mediaItem = mediaItemsByContentId.get(currentContentId);
- updateItemData(
- currentItemId,
- mediaItem != null ? mediaItem : MediaItem.EMPTY,
- mediaStatus.getMediaInfo(),
- currentContentId,
- /* defaultPositionUs= */ C.TIME_UNSET);
-
for (MediaQueueItem queueItem : mediaStatus.getQueueItems()) {
long defaultPositionUs = (long) (queueItem.getStartTime() * C.MICROS_PER_SECOND);
@Nullable MediaInfo mediaInfo = queueItem.getMedia();
String contentId = mediaInfo != null ? mediaInfo.getContentId() : UNKNOWN_CONTENT_ID;
- mediaItem = mediaItemsByContentId.get(contentId);
+ MediaItem mediaItem = mediaItemsByContentId.get(contentId);
updateItemData(
queueItem.getItemId(),
mediaItem != null ? mediaItem : mediaItemConverter.toMediaItem(queueItem),
@@ -130,6 +120,23 @@ public CastTimeline getCastTimeline(RemoteMediaClient remoteMediaClient) {
contentId,
defaultPositionUs);
}
+
+ // Process mediaStatus.getMediaInfo() after mediaStatus.getQueueItems()[...].getMedia(). Static
+ // queue items in the Cast SDK do not receive dynamic runtime updates (such as transitioning
+ // from buffered to live upon manifest loading). Updating from mediaStatus.getMediaInfo() second
+ // ensures that active runtime playback state is preserved and not overwritten by stale queue
+ // item metadata.
+ int currentItemId = mediaStatus.getCurrentItemId();
+ MediaInfo currentMediaInfo = checkNotNull(mediaStatus.getMediaInfo());
+ String currentContentId = currentMediaInfo.getContentId();
+ MediaItem mediaItem = mediaItemsByContentId.get(currentContentId);
+ updateItemData(
+ currentItemId,
+ mediaItem != null ? mediaItem : MediaItem.EMPTY,
+ currentMediaInfo,
+ currentContentId,
+ /* defaultPositionUs= */ C.TIME_UNSET);
+
return new CastTimeline(itemIds, itemIdToData);
}
@@ -151,6 +158,11 @@ private void updateItemData(
if (defaultPositionUs == C.TIME_UNSET) {
defaultPositionUs = previousData.defaultPositionUs;
}
+ // The media item can be empty if it originates from another sender. We use previous data as
+ // itemIdToData is populated when all media items from the queue are updated.
+ if (mediaItem == MediaItem.EMPTY) {
+ mediaItem = previousData.mediaItem;
+ }
itemIdToData.put(
itemId,
previousData.copyWithNewValues(
diff --git a/libraries/cast/src/main/java/androidx/media3/cast/DefaultCastPlayerTransferCallback.java b/libraries/cast/src/main/java/androidx/media3/cast/DefaultCastPlayerTransferCallback.java
new file mode 100644
index 00000000000..1c9718fd0f8
--- /dev/null
+++ b/libraries/cast/src/main/java/androidx/media3/cast/DefaultCastPlayerTransferCallback.java
@@ -0,0 +1,78 @@
+/*
+ * Copyright 2026 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 androidx.media3.cast;
+
+import android.net.Uri;
+import androidx.media3.common.C;
+import androidx.media3.common.MediaItem;
+import androidx.media3.common.Player;
+import androidx.media3.common.PlayerTransferState;
+import androidx.media3.common.util.UnstableApi;
+import com.google.common.collect.ImmutableList;
+import java.util.List;
+
+/**
+ * The default {@link CastPlayer.TransferCallback} implementation.
+ *
+ *
Filters out {@link MediaItem}s that do not have a playback URI and adjusts the current media
+ * item index accordingly.
+ */
+@UnstableApi
+public final class DefaultCastPlayerTransferCallback implements CastPlayer.TransferCallback {
+
+ @Override
+ public void transferState(Player sourcePlayer, Player targetPlayer) {
+ PlayerTransferState transferState = PlayerTransferState.fromPlayer(sourcePlayer);
+ List mediaItems = transferState.getMediaItems();
+ ImmutableList.Builder playableMediaItemsBuilder = ImmutableList.builder();
+ int currentIndex = transferState.getCurrentMediaItemIndex();
+ int newCurrentIndex = currentIndex;
+ boolean currentIndexFiltered = false;
+ for (int i = 0; i < mediaItems.size(); i++) {
+ MediaItem mediaItem = mediaItems.get(i);
+ if (isPlayable(mediaItem)) {
+ playableMediaItemsBuilder.add(mediaItem);
+ } else if (i < currentIndex) {
+ newCurrentIndex--;
+ } else if (i == currentIndex) {
+ currentIndexFiltered = true;
+ }
+ }
+ List playableMediaItems = playableMediaItemsBuilder.build();
+ if (playableMediaItems.size() < mediaItems.size()) {
+ if (playableMediaItems.isEmpty()) {
+ newCurrentIndex = C.INDEX_UNSET;
+ } else {
+ newCurrentIndex = Math.min(newCurrentIndex, playableMediaItems.size() - 1);
+ }
+ PlayerTransferState.Builder builder =
+ transferState
+ .buildUpon()
+ .setMediaItems(playableMediaItems)
+ .setCurrentMediaItemIndex(newCurrentIndex);
+ if (currentIndexFiltered) {
+ builder.setCurrentPosition(0);
+ }
+ transferState = builder.build();
+ }
+ transferState.setToPlayer(targetPlayer);
+ }
+
+ private static boolean isPlayable(MediaItem mediaItem) {
+ return mediaItem.localConfiguration != null
+ && !Uri.EMPTY.equals(mediaItem.localConfiguration.uri);
+ }
+}
diff --git a/libraries/cast/src/main/java/androidx/media3/cast/DefaultMediaItemConverter.java b/libraries/cast/src/main/java/androidx/media3/cast/DefaultMediaItemConverter.java
index 136e196f175..26d4279129b 100644
--- a/libraries/cast/src/main/java/androidx/media3/cast/DefaultMediaItemConverter.java
+++ b/libraries/cast/src/main/java/androidx/media3/cast/DefaultMediaItemConverter.java
@@ -50,6 +50,12 @@ public final class DefaultMediaItemConverter implements MediaItemConverter {
private static final String KEY_UUID = "uuid";
private static final String KEY_LICENSE_URI = "licenseUri";
private static final String KEY_REQUEST_HEADERS = "requestHeaders";
+ private static final String KEY_LIVE_CONFIGURATION = "m3-liveConfiguration";
+ private static final String KEY_TARGET_OFFSET_MS = "m3-targetOffsetMs";
+ private static final String KEY_MIN_OFFSET_MS = "m3-minOffsetMs";
+ private static final String KEY_MAX_OFFSET_MS = "m3-maxOffsetMs";
+ private static final String KEY_MIN_PLAYBACK_SPEED = "m3-minPlaybackSpeed";
+ private static final String KEY_MAX_PLAYBACK_SPEED = "m3-maxPlaybackSpeed";
@Override
public MediaItem toMediaItem(MediaQueueItem mediaQueueItem) {
@@ -165,14 +171,20 @@ public MediaQueueItem toMediaQueueItem(MediaItem mediaItem) {
String contentUrl = mediaItem.localConfiguration.uri.toString();
String contentId =
mediaItem.mediaId.equals(MediaItem.DEFAULT_MEDIA_ID) ? contentUrl : mediaItem.mediaId;
- MediaInfo mediaInfo =
+ // If the MediaItem has a LiveConfiguration, we set the stream type to STREAM_TYPE_LIVE.
+ // Otherwise, we leave it unset letting the receiver decide whether the item is live.
+ boolean isExplicitlyLive =
+ !mediaItem.liveConfiguration.equals(MediaItem.LiveConfiguration.UNSET);
+ MediaInfo.Builder mediaInfoBuilder =
new MediaInfo.Builder(contentId)
- .setStreamType(MediaInfo.STREAM_TYPE_BUFFERED)
.setContentType(mediaItem.localConfiguration.mimeType)
.setContentUrl(contentUrl)
.setMetadata(metadata)
- .setCustomData(getCustomData(mediaItem))
- .build();
+ .setCustomData(getCustomData(mediaItem));
+ if (isExplicitlyLive) {
+ mediaInfoBuilder.setStreamType(MediaInfo.STREAM_TYPE_LIVE);
+ }
+ MediaInfo mediaInfo = mediaInfoBuilder.build();
return new MediaQueueItem.Builder(mediaInfo).build();
}
@@ -231,12 +243,37 @@ private static MediaItem getMediaItem(
if (mediaItemJson.has(KEY_DRM_CONFIGURATION)) {
populateDrmConfiguration(mediaItemJson.getJSONObject(KEY_DRM_CONFIGURATION), builder);
}
+ if (mediaItemJson.has(KEY_LIVE_CONFIGURATION)) {
+ builder.setLiveConfiguration(
+ getLiveConfiguration(mediaItemJson.getJSONObject(KEY_LIVE_CONFIGURATION)));
+ }
return builder.build();
} catch (JSONException e) {
throw new RuntimeException(e);
}
}
+ private static MediaItem.LiveConfiguration getLiveConfiguration(JSONObject json)
+ throws JSONException {
+ MediaItem.LiveConfiguration.Builder builder = new MediaItem.LiveConfiguration.Builder();
+ if (json.has(KEY_TARGET_OFFSET_MS)) {
+ builder.setTargetOffsetMs(json.getLong(KEY_TARGET_OFFSET_MS));
+ }
+ if (json.has(KEY_MIN_OFFSET_MS)) {
+ builder.setMinOffsetMs(json.getLong(KEY_MIN_OFFSET_MS));
+ }
+ if (json.has(KEY_MAX_OFFSET_MS)) {
+ builder.setMaxOffsetMs(json.getLong(KEY_MAX_OFFSET_MS));
+ }
+ if (json.has(KEY_MIN_PLAYBACK_SPEED)) {
+ builder.setMinPlaybackSpeed((float) json.getDouble(KEY_MIN_PLAYBACK_SPEED));
+ }
+ if (json.has(KEY_MAX_PLAYBACK_SPEED)) {
+ builder.setMaxPlaybackSpeed((float) json.getDouble(KEY_MAX_PLAYBACK_SPEED));
+ }
+ return builder.build();
+ }
+
private static void populateDrmConfiguration(JSONObject json, MediaItem.Builder mediaItem)
throws JSONException {
MediaItem.DrmConfiguration.Builder drmConfiguration =
@@ -280,6 +317,31 @@ private static JSONObject getMediaItemJson(MediaItem mediaItem) throws JSONExcep
KEY_DRM_CONFIGURATION,
getDrmConfigurationJson(mediaItem.localConfiguration.drmConfiguration));
}
+ if (!mediaItem.liveConfiguration.equals(MediaItem.LiveConfiguration.UNSET)) {
+ JSONObject liveConfigurationJson = getLiveConfigurationJson(mediaItem.liveConfiguration);
+ json.put(KEY_LIVE_CONFIGURATION, liveConfigurationJson);
+ }
+ return json;
+ }
+
+ private static JSONObject getLiveConfigurationJson(MediaItem.LiveConfiguration liveConfiguration)
+ throws JSONException {
+ JSONObject json = new JSONObject();
+ if (liveConfiguration.targetOffsetMs != C.TIME_UNSET) {
+ json.put(KEY_TARGET_OFFSET_MS, liveConfiguration.targetOffsetMs);
+ }
+ if (liveConfiguration.minOffsetMs != C.TIME_UNSET) {
+ json.put(KEY_MIN_OFFSET_MS, liveConfiguration.minOffsetMs);
+ }
+ if (liveConfiguration.maxOffsetMs != C.TIME_UNSET) {
+ json.put(KEY_MAX_OFFSET_MS, liveConfiguration.maxOffsetMs);
+ }
+ if (liveConfiguration.minPlaybackSpeed != C.RATE_UNSET) {
+ json.put(KEY_MIN_PLAYBACK_SPEED, liveConfiguration.minPlaybackSpeed);
+ }
+ if (liveConfiguration.maxPlaybackSpeed != C.RATE_UNSET) {
+ json.put(KEY_MAX_PLAYBACK_SPEED, liveConfiguration.maxPlaybackSpeed);
+ }
return json;
}
diff --git a/libraries/cast/src/test/java/androidx/media3/cast/CastPlayerTest.java b/libraries/cast/src/test/java/androidx/media3/cast/CastPlayerTest.java
index 962e5d0250c..fa4d1800051 100644
--- a/libraries/cast/src/test/java/androidx/media3/cast/CastPlayerTest.java
+++ b/libraries/cast/src/test/java/androidx/media3/cast/CastPlayerTest.java
@@ -252,6 +252,15 @@ public void release_whenCastSessionIsAvailable_releasesBothPlayers() {
public void playerTransfer_whenSourcePlayerIsNonIdle_callsPrepare() {
// We need a non-empty timeline to be in a non-idle state, and check that the target player is
// prepared as a result.
+ MediaInfo mediaInfo =
+ new MediaInfo.Builder("https://example.com/media.mp4")
+ .setStreamType(MediaInfo.STREAM_TYPE_BUFFERED)
+ .setContentType("video/mp4")
+ .build();
+ MediaQueueItem queueItem = new MediaQueueItem.Builder(mediaInfo).setItemId(1).build();
+ when(mockMediaStatus.getQueueItems()).thenReturn(ImmutableList.of(queueItem));
+ when(mockMediaStatus.getCurrentItemId()).thenReturn(1);
+ when(mockMediaStatus.getMediaInfo()).thenReturn(mediaInfo);
when(mockMediaQueue.getItemIds()).thenReturn(new int[] {1});
when(mockRemoteMediaClient.getPlayerState()).thenReturn(MediaStatus.PLAYER_STATE_PLAYING);
castSessionListener.onSessionStarted(mockCastSession, /* sessionId= */ "ignored");
diff --git a/libraries/cast/src/test/java/androidx/media3/cast/CastTimelineTrackerTest.java b/libraries/cast/src/test/java/androidx/media3/cast/CastTimelineTrackerTest.java
index 73637399874..87c5127890f 100644
--- a/libraries/cast/src/test/java/androidx/media3/cast/CastTimelineTrackerTest.java
+++ b/libraries/cast/src/test/java/androidx/media3/cast/CastTimelineTrackerTest.java
@@ -25,13 +25,13 @@
import androidx.media3.common.Timeline.Window;
import androidx.media3.common.util.Util;
import androidx.media3.test.utils.TimelineAsserts;
-import androidx.test.ext.junit.runners.AndroidJUnit4;
import com.google.android.gms.cast.MediaInfo;
import com.google.android.gms.cast.MediaQueueItem;
import com.google.android.gms.cast.MediaStatus;
import com.google.android.gms.cast.framework.media.MediaQueue;
import com.google.android.gms.cast.framework.media.RemoteMediaClient;
import com.google.common.collect.ImmutableList;
+import com.google.testing.junit.testparameterinjector.TestParameter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
@@ -39,9 +39,10 @@
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
+import org.robolectric.RobolectricTestParameterInjector;
/** Tests for {@link CastTimelineTracker}. */
-@RunWith(AndroidJUnit4.class)
+@RunWith(RobolectricTestParameterInjector.class)
public class CastTimelineTrackerTest {
private static final long DURATION_2_MS = 2000;
@@ -332,6 +333,102 @@ public void getCastTimeline_noCustomData_returnsFallbackMediaItems() {
assertThat(window.mediaItem.localConfiguration.mimeType).isEqualTo(MimeTypes.AUDIO_MPEG);
}
+ @Test
+ public void getCastTimeline_receiverReportsStreamTypeLive_windowIsLive() {
+ RemoteMediaClient mockRemoteMediaClient = mock(RemoteMediaClient.class);
+ MediaQueue mockMediaQueue = mock(MediaQueue.class);
+ MediaStatus mockMediaStatus = mock(MediaStatus.class);
+ MediaInfo mediaInfo =
+ new MediaInfo.Builder("https://example.com/live.m3u8")
+ .setContentType(MimeTypes.APPLICATION_M3U8)
+ .setStreamType(MediaInfo.STREAM_TYPE_LIVE)
+ .build();
+ MediaQueueItem queueItem = new MediaQueueItem.Builder(mediaInfo).setItemId(1).build();
+ when(mockRemoteMediaClient.getMediaQueue()).thenReturn(mockMediaQueue);
+ when(mockMediaQueue.getItemIds()).thenReturn(new int[] {1});
+ when(mockRemoteMediaClient.getMediaStatus()).thenReturn(mockMediaStatus);
+ when(mockMediaStatus.getCurrentItemId()).thenReturn(1);
+ when(mockMediaStatus.getMediaInfo()).thenReturn(mediaInfo);
+ when(mockMediaStatus.getQueueItems()).thenReturn(Collections.singletonList(queueItem));
+
+ CastTimeline castTimeline = castTimelineTracker.getCastTimeline(mockRemoteMediaClient);
+
+ assertThat(castTimeline.getWindowCount()).isEqualTo(1);
+ Window window = castTimeline.getWindow(/* windowIndex= */ 0, new Window());
+ assertThat(window.isLive()).isTrue();
+ assertThat(window.liveConfiguration).isEqualTo(MediaItem.LiveConfiguration.UNSET);
+ }
+
+ @Test
+ public void getCastTimeline_activeMediaInfoLiveAndQueueItemStale_windowIsLive(
+ @TestParameter({"0" /* STREAM_TYPE_NONE */, "1" /* STREAM_TYPE_BUFFERED */})
+ int staleStreamType) {
+ RemoteMediaClient mockRemoteMediaClient = mock(RemoteMediaClient.class);
+ MediaQueue mockMediaQueue = mock(MediaQueue.class);
+ MediaStatus mockMediaStatus = mock(MediaStatus.class);
+ MediaInfo staleQueueMediaInfo =
+ new MediaInfo.Builder("https://example.com/live.m3u8")
+ .setContentType(MimeTypes.APPLICATION_M3U8)
+ .setStreamType(staleStreamType)
+ .build();
+ MediaQueueItem queueItem = new MediaQueueItem.Builder(staleQueueMediaInfo).setItemId(1).build();
+ MediaInfo activeMediaInfo =
+ new MediaInfo.Builder("https://example.com/live.m3u8")
+ .setContentType(MimeTypes.APPLICATION_M3U8)
+ .setStreamType(MediaInfo.STREAM_TYPE_LIVE)
+ .build();
+ when(mockRemoteMediaClient.getMediaQueue()).thenReturn(mockMediaQueue);
+ when(mockMediaQueue.getItemIds()).thenReturn(new int[] {1});
+ when(mockRemoteMediaClient.getMediaStatus()).thenReturn(mockMediaStatus);
+ when(mockMediaStatus.getCurrentItemId()).thenReturn(1);
+ when(mockMediaStatus.getMediaInfo()).thenReturn(activeMediaInfo);
+ when(mockMediaStatus.getQueueItems()).thenReturn(Collections.singletonList(queueItem));
+
+ CastTimeline castTimeline = castTimelineTracker.getCastTimeline(mockRemoteMediaClient);
+
+ assertThat(castTimeline.getWindowCount()).isEqualTo(1);
+ Window window = castTimeline.getWindow(/* windowIndex= */ 0, new Window());
+ assertThat(window.isLive()).isTrue();
+ assertThat(window.liveConfiguration).isEqualTo(MediaItem.LiveConfiguration.UNSET);
+ }
+
+ @Test
+ public void getCastTimeline_unconfiguredLiveStreamResolvedByReceiver_windowIsLive() {
+ RemoteMediaClient mockRemoteMediaClient = mock(RemoteMediaClient.class);
+ MediaQueue mockMediaQueue = mock(MediaQueue.class);
+ MediaStatus mockMediaStatus = mock(MediaStatus.class);
+ MediaItem unconfiguredItem =
+ new MediaItem.Builder()
+ .setUri("https://example.com/live.m3u8")
+ .setMimeType(MimeTypes.APPLICATION_M3U8)
+ .build();
+ MediaQueueItem queueItem = mediaItemConverter.toMediaQueueItem(unconfiguredItem);
+ MediaInfo resolvedMediaInfo =
+ new MediaInfo.Builder(queueItem.getMedia().getContentId())
+ .setContentType(queueItem.getMedia().getContentType())
+ .setContentUrl(queueItem.getMedia().getContentUrl())
+ .setStreamType(MediaInfo.STREAM_TYPE_LIVE)
+ .build();
+ MediaQueueItem activeQueueItem =
+ new MediaQueueItem.Builder(resolvedMediaInfo).setItemId(1).build();
+ when(mockRemoteMediaClient.getMediaQueue()).thenReturn(mockMediaQueue);
+ when(mockMediaQueue.getItemIds()).thenReturn(new int[] {1});
+ when(mockRemoteMediaClient.getMediaStatus()).thenReturn(mockMediaStatus);
+ when(mockMediaStatus.getCurrentItemId()).thenReturn(1);
+ when(mockMediaStatus.getMediaInfo()).thenReturn(resolvedMediaInfo);
+ when(mockMediaStatus.getQueueItems()).thenReturn(Collections.singletonList(activeQueueItem));
+
+ castTimelineTracker.onMediaItemsSet(
+ Collections.singletonList(unconfiguredItem), new MediaQueueItem[] {activeQueueItem});
+ CastTimeline castTimeline = castTimelineTracker.getCastTimeline(mockRemoteMediaClient);
+
+ assertThat(castTimeline.getWindowCount()).isEqualTo(1);
+ Window window = castTimeline.getWindow(/* windowIndex= */ 0, new Window());
+ assertThat(window.isLive()).isTrue();
+ assertThat(window.liveConfiguration).isEqualTo(MediaItem.LiveConfiguration.UNSET);
+ assertThat(window.mediaItem).isEqualTo(unconfiguredItem);
+ }
+
private MediaItem createMediaItem(int uid) {
return new MediaItem.Builder()
.setUri("http://www.google.com/" + uid)
diff --git a/libraries/cast/src/test/java/androidx/media3/cast/DefaultCastPlayerTransferCallbackTest.java b/libraries/cast/src/test/java/androidx/media3/cast/DefaultCastPlayerTransferCallbackTest.java
new file mode 100644
index 00000000000..14b08c39b72
--- /dev/null
+++ b/libraries/cast/src/test/java/androidx/media3/cast/DefaultCastPlayerTransferCallbackTest.java
@@ -0,0 +1,99 @@
+/*
+ * Copyright 2026 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 androidx.media3.cast;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import androidx.media3.common.MediaItem;
+import androidx.media3.test.utils.FakePlayer;
+import androidx.test.ext.junit.runners.AndroidJUnit4;
+import com.google.common.collect.ImmutableList;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+/** Unit tests for {@link DefaultCastPlayerTransferCallback}. */
+@RunWith(AndroidJUnit4.class)
+public final class DefaultCastPlayerTransferCallbackTest {
+
+ private FakePlayer sourcePlayer;
+ private FakePlayer targetPlayer;
+ private DefaultCastPlayerTransferCallback transferCallback;
+
+ @Before
+ public void setUp() {
+ sourcePlayer = new FakePlayer();
+ targetPlayer = new FakePlayer();
+ transferCallback = new DefaultCastPlayerTransferCallback();
+ }
+
+ @Test
+ public void transferState_filtersItemsWithNoPlaybackUri() {
+ MediaItem uriItem = new MediaItem.Builder().setUri("http://uri").build();
+ MediaItem noUriItem = new MediaItem.Builder().setMediaId("no-uri").build();
+ sourcePlayer.setMediaItems(
+ ImmutableList.of(uriItem, noUriItem), /* startIndex= */ 0, /* startPositionMs= */ 0);
+
+ transferCallback.transferState(sourcePlayer, targetPlayer);
+
+ assertThat(targetPlayer.getMediaItemCount()).isEqualTo(1);
+ assertThat(targetPlayer.getMediaItemAt(0)).isEqualTo(uriItem);
+ assertThat(targetPlayer.getCurrentMediaItemIndex()).isEqualTo(0);
+ }
+
+ @Test
+ public void transferState_adjustsCurrentIndexWhenCurrentItemIsFiltered() {
+ MediaItem uriItem = new MediaItem.Builder().setUri("http://uri").build();
+ MediaItem noUriItem = new MediaItem.Builder().setMediaId("no-uri").build();
+ sourcePlayer.setMediaItems(
+ ImmutableList.of(uriItem, noUriItem), /* startIndex= */ 1, /* startPositionMs= */ 1000);
+
+ transferCallback.transferState(sourcePlayer, targetPlayer);
+
+ assertThat(targetPlayer.getMediaItemCount()).isEqualTo(1);
+ assertThat(targetPlayer.getMediaItemAt(0)).isEqualTo(uriItem);
+ assertThat(targetPlayer.getCurrentMediaItemIndex()).isEqualTo(0);
+ assertThat(targetPlayer.getCurrentPosition()).isEqualTo(0);
+ }
+
+ @Test
+ public void transferState_whenCurrentItemIsNotFiltered_preservesPosition() {
+ MediaItem uriItem = new MediaItem.Builder().setUri("http://uri").build();
+ MediaItem noUriItem = new MediaItem.Builder().setMediaId("no-uri").build();
+ sourcePlayer.setMediaItems(
+ ImmutableList.of(uriItem, noUriItem), /* startIndex= */ 0, /* startPositionMs= */ 1000);
+
+ transferCallback.transferState(sourcePlayer, targetPlayer);
+
+ assertThat(targetPlayer.getMediaItemCount()).isEqualTo(1);
+ assertThat(targetPlayer.getMediaItemAt(0)).isEqualTo(uriItem);
+ assertThat(targetPlayer.getCurrentMediaItemIndex()).isEqualTo(0);
+ assertThat(targetPlayer.getCurrentPosition()).isEqualTo(1000);
+ }
+
+ @Test
+ public void transferState_whenAllItemsAreFiltered_setsEmptyPlaylistAndIndexUnset() {
+ MediaItem noUriItem1 = new MediaItem.Builder().setMediaId("no-uri-1").build();
+ MediaItem noUriItem2 = new MediaItem.Builder().setMediaId("no-uri-2").build();
+ sourcePlayer.setMediaItems(
+ ImmutableList.of(noUriItem1, noUriItem2), /* startIndex= */ 0, /* startPositionMs= */ 0);
+
+ transferCallback.transferState(sourcePlayer, targetPlayer);
+
+ assertThat(targetPlayer.getMediaItemCount()).isEqualTo(0);
+ assertThat(targetPlayer.getCurrentMediaItemIndex()).isEqualTo(0);
+ }
+}
diff --git a/libraries/cast/src/test/java/androidx/media3/cast/DefaultMediaItemConverterTest.java b/libraries/cast/src/test/java/androidx/media3/cast/DefaultMediaItemConverterTest.java
index 5976b5824d5..7af275ca4ab 100644
--- a/libraries/cast/src/test/java/androidx/media3/cast/DefaultMediaItemConverterTest.java
+++ b/libraries/cast/src/test/java/androidx/media3/cast/DefaultMediaItemConverterTest.java
@@ -228,4 +228,50 @@ public void toMediaItem_noCustomData_fallbackWithNonUriContentIdAndNoContentUrl(
DefaultMediaItemConverter converter = new DefaultMediaItemConverter();
assertThrows(IllegalArgumentException.class, () -> converter.toMediaItem(queueItem));
}
+
+ @Test
+ public void toMediaQueueItem_withLiveConfiguration_setsStreamTypeLive() {
+ DefaultMediaItemConverter converter = new DefaultMediaItemConverter();
+ MediaItem mediaItem =
+ new MediaItem.Builder()
+ .setUri("http://example.com/live.m3u8")
+ .setLiveConfiguration(
+ new MediaItem.LiveConfiguration.Builder().setTargetOffsetMs(10000).build())
+ .build();
+
+ MediaQueueItem queueItem = converter.toMediaQueueItem(mediaItem);
+
+ assertThat(queueItem.getMedia().getStreamType()).isEqualTo(MediaInfo.STREAM_TYPE_LIVE);
+ }
+
+ @Test
+ public void serialize_deserialize_liveConfiguration() {
+ MediaItem item =
+ new MediaItem.Builder()
+ .setUri("http://example.com/live.m3u8")
+ .setLiveConfiguration(
+ new MediaItem.LiveConfiguration.Builder()
+ .setTargetOffsetMs(10000)
+ .setMinOffsetMs(5000)
+ .setMaxOffsetMs(20000)
+ .setMinPlaybackSpeed(0.9f)
+ .setMaxPlaybackSpeed(1.1f)
+ .build())
+ .build();
+
+ DefaultMediaItemConverter converter = new DefaultMediaItemConverter();
+ MediaQueueItem queueItem = converter.toMediaQueueItem(item);
+ MediaItem reconstructedItem = converter.toMediaItem(queueItem);
+ assertThat(reconstructedItem.liveConfiguration).isEqualTo(item.liveConfiguration);
+ }
+
+ @Test
+ public void toMediaQueueItem_withoutLiveConfiguration_setsStreamTypeInvalid() {
+ DefaultMediaItemConverter converter = new DefaultMediaItemConverter();
+ MediaItem mediaItem = new MediaItem.Builder().setUri("http://example.com/live.m3u8").build();
+
+ MediaQueueItem queueItem = converter.toMediaQueueItem(mediaItem);
+
+ assertThat(queueItem.getMedia().getStreamType()).isEqualTo(MediaInfo.STREAM_TYPE_INVALID);
+ }
}
diff --git a/libraries/common/src/main/java/androidx/media3/common/AdPlaybackState.java b/libraries/common/src/main/java/androidx/media3/common/AdPlaybackState.java
index 20dc7f81756..b9637bb6b8f 100644
--- a/libraries/common/src/main/java/androidx/media3/common/AdPlaybackState.java
+++ b/libraries/common/src/main/java/androidx/media3/common/AdPlaybackState.java
@@ -1795,6 +1795,8 @@ public String toString() {
for (int i = 0; i < adGroups.length; i++) {
sb.append("adGroup(timeUs=");
sb.append(adGroups[i].timeUs);
+ sb.append(", contentResumeOffsetUs=");
+ sb.append(adGroups[i].contentResumeOffsetUs);
sb.append(", ads=[");
for (int j = 0; j < adGroups[i].states.length; j++) {
sb.append("ad(state=");
diff --git a/libraries/common/src/main/java/androidx/media3/common/MediaLibraryInfo.java b/libraries/common/src/main/java/androidx/media3/common/MediaLibraryInfo.java
index 5967d221a58..fac86ad5ef6 100644
--- a/libraries/common/src/main/java/androidx/media3/common/MediaLibraryInfo.java
+++ b/libraries/common/src/main/java/androidx/media3/common/MediaLibraryInfo.java
@@ -29,11 +29,11 @@ public final class MediaLibraryInfo {
/** The version of the library expressed as a string, for example "1.2.3" or "1.2.0-beta01". */
// Intentionally hardcoded. Do not derive from other constants (e.g. VERSION_INT) or vice versa.
- public static final String VERSION = "1.11.0-alpha01";
+ public static final String VERSION = "1.11.1";
/** The version of the library expressed as {@code TAG + "/" + VERSION}. */
// Intentionally hardcoded. Do not derive from other constants (e.g. VERSION) or vice versa.
- public static final String VERSION_SLASHY = "AndroidXMedia3/1.11.0-alpha01";
+ public static final String VERSION_SLASHY = "AndroidXMedia3/1.11.1";
/**
* The version of the library expressed as an integer, for example 1002003300.
@@ -47,7 +47,7 @@ public final class MediaLibraryInfo {
* (123-045-006-3-00).
*/
// Intentionally hardcoded. Do not derive from other constants (e.g. VERSION) or vice versa.
- public static final int VERSION_INT = 1_011_000_0_01;
+ public static final int VERSION_INT = 1_011_001_3_00;
/** Whether the library was compiled with {@link TraceUtil} trace enabled. */
public static final boolean TRACE_ENABLED = true;
diff --git a/libraries/common/src/main/java/androidx/media3/common/util/CodecSpecificDataUtil.java b/libraries/common/src/main/java/androidx/media3/common/util/CodecSpecificDataUtil.java
index 6e4e83c4e81..f07ea4f79d0 100644
--- a/libraries/common/src/main/java/androidx/media3/common/util/CodecSpecificDataUtil.java
+++ b/libraries/common/src/main/java/androidx/media3/common/util/CodecSpecificDataUtil.java
@@ -2363,5 +2363,71 @@ public static int h263ConstToLevelNumber(int levelConst) {
}
}
+ // Expected magic bytes for HAGC (ST 2094-50) metadata in AV1 OBU or HEVC SEI.
+ private static final byte[] expectedHagcPayloadPrefix = {
+ // itu_t_t35_country_code == 0xB5
+ (byte) 0xB5,
+ // itu_t_t35_terminal_provider_code == 0x0090
+ (byte) 0x00,
+ (byte) 0x90,
+ // itu_t_t35_terminal_provider_oriented_code == 0x0001
+ (byte) 0x00,
+ (byte) 0x01,
+ };
+
+ /**
+ * Returns whether the given {@code data} contains HAGC (ST 2094-50) ITU-T T.35 metadata.
+ *
+ * @param data The payload data.
+ * @param length The length of the payload data.
+ */
+ public static boolean isHagcMetadata(byte[] data, int length) {
+ if (length < expectedHagcPayloadPrefix.length) {
+ return false;
+ }
+ for (int i = 0; i < expectedHagcPayloadPrefix.length; i++) {
+ if (data[i] != expectedHagcPayloadPrefix[i]) {
+ return false;
+ }
+ }
+ return true;
+ }
+
+ /**
+ * Returns whether the given {@link ByteBuffer} contains HAGC (ST 2094-50) ITU-T T.35 metadata,
+ * without consuming any bytes.
+ *
+ * @param buffer The payload data.
+ */
+ public static boolean isHagcMetadata(ByteBuffer buffer) {
+ if (buffer.remaining() < expectedHagcPayloadPrefix.length) {
+ return false;
+ }
+ int pos = buffer.position();
+ for (int i = 0; i < expectedHagcPayloadPrefix.length; i++) {
+ if (buffer.get(pos + i) != expectedHagcPayloadPrefix[i]) {
+ return false;
+ }
+ }
+ return true;
+ }
+
+ /**
+ * Returns whether the given {@link Format} represents an HAGC (ST 2094-50) metadata track.
+ *
+ * @param format The format of the track.
+ */
+ public static boolean isHagcTrack(Format format) {
+ if (!Objects.equals(format.sampleMimeType, MimeTypes.APPLICATION_ITUT_T35)) {
+ return false;
+ }
+ for (byte[] initData : format.initializationData) {
+ if (isHagcMetadata(initData, initData.length)) {
+ return true;
+ }
+ }
+ return false;
+ }
+
private CodecSpecificDataUtil() {}
}
diff --git a/libraries/common/src/main/java/androidx/media3/common/util/GlUtil.java b/libraries/common/src/main/java/androidx/media3/common/util/GlUtil.java
index 7a2d06c5537..e733576d82f 100644
--- a/libraries/common/src/main/java/androidx/media3/common/util/GlUtil.java
+++ b/libraries/common/src/main/java/androidx/media3/common/util/GlUtil.java
@@ -615,10 +615,18 @@ private static void assertValidTextureSize(int width, int height) throws GlExcep
* #createFocusedPlaceholderEglSurface}.
*/
public static void clearFocusedBuffers() throws GlException {
- GLES20.glClearColor(/* red= */ 0, /* green= */ 0, /* blue= */ 0, /* alpha= */ 0);
- GLES20.glClearDepthf(1.0f);
- GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);
- GlUtil.checkGlError();
+ clearFocusedBuffersInternal(/* r= */ 0, /* g= */ 0, /* b= */ 0, /* a= */ 0);
+ }
+
+ /**
+ * Fills the pixels in the current output render target buffers with (r=0, g=0, b=0, a=1).
+ *
+ *
Buffers can be focused using {@link #focusEglSurface} and {@link
+ * #focusFramebufferUsingCurrentContext}, {@link #focusFramebuffer}, and {@link
+ * #createFocusedPlaceholderEglSurface}.
+ */
+ public static void clearFocusedBuffersOpaque() throws GlException {
+ clearFocusedBuffersInternal(/* r= */ 0, /* g= */ 0, /* b= */ 0, /* a= */ 1);
}
/**
@@ -1151,4 +1159,12 @@ && getContextMajorVersion() >= 3) {
checkGlError();
}
}
+
+ private static void clearFocusedBuffersInternal(float r, float g, float b, float a)
+ throws GlException {
+ GLES20.glClearColor(r, g, b, a);
+ GLES20.glClearDepthf(1.0f);
+ GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);
+ GlUtil.checkGlError();
+ }
}
diff --git a/libraries/common/src/main/java/androidx/media3/common/util/MediaFormatUtil.java b/libraries/common/src/main/java/androidx/media3/common/util/MediaFormatUtil.java
index 14e7a14e496..8f1cc324b06 100644
--- a/libraries/common/src/main/java/androidx/media3/common/util/MediaFormatUtil.java
+++ b/libraries/common/src/main/java/androidx/media3/common/util/MediaFormatUtil.java
@@ -85,9 +85,7 @@ public static Format createFormatFromMediaFormat(MediaFormat mediaFormat) {
getInteger(
mediaFormat, MediaFormat.KEY_BIT_RATE, /* defaultValue= */ Format.NO_VALUE))
.setCodecs(getCodecString(mediaFormat))
- .setFrameRate(
- getFloatFromIntOrFloat(
- mediaFormat, MediaFormat.KEY_FRAME_RATE, /* defaultValue= */ Format.NO_VALUE))
+ .setFrameRate(getFrameRate(mediaFormat))
.setWidth(
getInteger(mediaFormat, MediaFormat.KEY_WIDTH, /* defaultValue= */ Format.NO_VALUE))
.setHeight(
@@ -435,6 +433,20 @@ private static String getCodecString(MediaFormat mediaFormat) {
}
}
+ /** Returns the frame rate from the given {@link MediaFormat}. */
+ @SuppressLint("InlinedApi") // Inlined MediaFormat.KEY_FRAME_RATE.
+ private static float getFrameRate(MediaFormat mediaFormat) {
+ float frameRate =
+ getFloatFromIntOrFloat(
+ mediaFormat, MediaFormat.KEY_FRAME_RATE, /* defaultValue= */ Format.NO_VALUE);
+ // Some devices may produce non-positive frame rates from MediaFormat.
+ // See b/493371202 for more information.
+ if (frameRate <= 0f) {
+ frameRate = Format.NO_VALUE;
+ }
+ return frameRate;
+ }
+
/** Returns the ratio between a pixel's width and height for a {@link MediaFormat}. */
// Inlined MediaFormat.KEY_PIXEL_ASPECT_RATIO_WIDTH and MediaFormat.KEY_PIXEL_ASPECT_RATIO_HEIGHT.
@SuppressLint("InlinedApi")
diff --git a/libraries/common/src/main/java/androidx/media3/common/util/ThrowingRunnable.java b/libraries/common/src/main/java/androidx/media3/common/util/ThrowingRunnable.java
new file mode 100644
index 00000000000..c1110fd08d6
--- /dev/null
+++ b/libraries/common/src/main/java/androidx/media3/common/util/ThrowingRunnable.java
@@ -0,0 +1,29 @@
+/*
+ * Copyright 2026 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 androidx.media3.common.util;
+
+/**
+ * A {@link Runnable}-like interface that allows throwing checked exceptions.
+ *
+ * @param The type of exception that the operation may throw.
+ */
+@UnstableApi
+@FunctionalInterface
+public interface ThrowingRunnable {
+
+ /** Runs the operation. */
+ void run() throws E;
+}
diff --git a/libraries/common/src/main/java/androidx/media3/common/util/Util.java b/libraries/common/src/main/java/androidx/media3/common/util/Util.java
index 1456fdef55e..aa355920fa2 100644
--- a/libraries/common/src/main/java/androidx/media3/common/util/Util.java
+++ b/libraries/common/src/main/java/androidx/media3/common/util/Util.java
@@ -234,6 +234,12 @@ public static byte[] toByteArray(InputStream inputStream) throws IOException {
return ByteStreams.toByteArray(inputStream);
}
+ /** Converts a single integer into an equivalent 4-byte array in big-endian order. */
+ @UnstableApi
+ public static byte[] toByteArray(int value) {
+ return Ints.toByteArray(value);
+ }
+
/**
* Converts an array of integers into an equivalent byte array.
*
diff --git a/libraries/common/src/main/java/androidx/media3/common/video/FrameProcessor.java b/libraries/common/src/main/java/androidx/media3/common/video/FrameProcessor.java
index a20a98f7b0a..963706ca932 100644
--- a/libraries/common/src/main/java/androidx/media3/common/video/FrameProcessor.java
+++ b/libraries/common/src/main/java/androidx/media3/common/video/FrameProcessor.java
@@ -57,9 +57,6 @@ interface Factory {
/**
* Creates a {@link FrameProcessor} that sends frames to the given {@link FrameWriter}.
*
- *
This method converts the input {@link Bitmap} to the OpenGL coordinate system that {@link
+ * FrameWriterGlTextureFrameConsumer} always receives.
+ */
private static GlTextureFrame createGlTextureFrame(Bitmap bitmap, long presentationTimeUs)
throws GlException {
int width = bitmap.getWidth();
int height = bitmap.getHeight();
- int texId = GlUtil.createTexture(bitmap);
+ Matrix matrix = new Matrix();
+ matrix.postScale(/* sx= */ 1f, /* sy= */ -1f, width / 2f, height / 2f);
+ Bitmap flippedBitmap =
+ Bitmap.createBitmap(
+ bitmap, /* x= */ 0, /* y= */ 0, width, height, matrix, /* filter= */ true);
+ int texId = GlUtil.createTexture(flippedBitmap);
+ flippedBitmap.recycle();
return new GlTextureFrame.Builder(
new GlTextureInfo(
/* texId= */ texId,
@@ -319,7 +515,12 @@ private static GlTextureFrame createGlTextureFrame(Bitmap bitmap, long presentat
}
})
.setPresentationTimeUs(presentationTimeUs)
- .setFormat(new Format.Builder().setWidth(width).setHeight(height).build())
+ .setFormat(
+ new Format.Builder()
+ .setWidth(width)
+ .setHeight(height)
+ .setColorInfo(ColorInfo.SRGB_BT709_FULL)
+ .build())
.build();
}
@@ -357,15 +558,18 @@ public boolean nativeCopyHardwareBufferToHardwareBuffer(
}
private static final class BitmapSavingFrameWriter implements FrameWriter {
- private final HardwareBuffer hardwareBuffer;
+ @Nullable private HardwareBuffer hardwareBuffer;
private final List outputBitmaps;
- private boolean hasCapacity = true;
+ private boolean hasCapacity;
@Nullable private Runnable pendingWakeupListener;
@Nullable private Executor pendingWakeupExecutor;
+ private boolean supportsPortrait;
+ @Nullable private Format configuredFormat;
- BitmapSavingFrameWriter(HardwareBuffer hardwareBuffer, List outputBitmaps) {
- this.hardwareBuffer = hardwareBuffer;
+ BitmapSavingFrameWriter(List outputBitmaps) {
this.outputBitmaps = outputBitmaps;
+ supportsPortrait = true;
+ hasCapacity = true;
}
void setCapacity(boolean hasCapacity) {
@@ -379,11 +583,29 @@ void setCapacity(boolean hasCapacity) {
@Override
public Info getInfo() {
- return (format, usage) -> true;
+ return (format, usage) -> supportsPortrait || format.width >= format.height;
}
@Override
- public void configure(Format format, long usage) {}
+ public void configure(Format format, long usage) {
+ this.configuredFormat = format;
+ if (hardwareBuffer == null
+ || hardwareBuffer.getWidth() != format.width
+ || hardwareBuffer.getHeight() != format.height) {
+ if (hardwareBuffer != null) {
+ hardwareBuffer.close();
+ }
+ hardwareBuffer =
+ HardwareBuffer.create(
+ format.width,
+ format.height,
+ HardwareBuffer.RGBA_8888,
+ /* layers= */ 1,
+ HardwareBuffer.USAGE_CPU_READ_OFTEN
+ | HardwareBuffer.USAGE_GPU_COLOR_OUTPUT
+ | HardwareBuffer.USAGE_GPU_SAMPLED_IMAGE);
+ }
+ }
@Override
public AsyncFrame dequeueInputFrame(Executor wakeupExecutor, Runnable wakeupListener) {
@@ -393,7 +615,8 @@ public AsyncFrame dequeueInputFrame(Executor wakeupExecutor, Runnable wakeupList
return null;
}
return new AsyncFrame(
- new DefaultHardwareBufferFrame.Builder(hardwareBuffer).build(), /* acquireFence= */ null);
+ new DefaultHardwareBufferFrame.Builder(checkNotNull(hardwareBuffer)).build(),
+ /* acquireFence= */ null);
}
@Override
@@ -419,6 +642,10 @@ public void queueInputFrame(Frame frame, @Nullable SyncFenceWrapper writeComplet
public void signalEndOfStream() {}
@Override
- public void close() {}
+ public void close() {
+ if (hardwareBuffer != null) {
+ hardwareBuffer.close();
+ }
+ }
}
}
diff --git a/libraries/effect/src/androidTest/java/androidx/media3/effect/HardwareBufferToGlTextureConverterTest.java b/libraries/effect/src/androidTest/java/androidx/media3/effect/HardwareBufferToGlTextureConverterTest.java
index 016965a0b3d..7a5f53005a1 100644
--- a/libraries/effect/src/androidTest/java/androidx/media3/effect/HardwareBufferToGlTextureConverterTest.java
+++ b/libraries/effect/src/androidTest/java/androidx/media3/effect/HardwareBufferToGlTextureConverterTest.java
@@ -311,23 +311,41 @@ public void onFrameProcessed(Frame frame, @Nullable SyncFenceWrapper releaseFenc
@Test
public void convert_withRgba8888HardwareBufferAndRotation_outputsCorrectGlTexture()
throws Exception {
+ int rotationDegrees = 90;
Bitmap inputBitmap = BitmapPixelTestUtil.readBitmap("media/png/first_frame_1920x1080.png");
Bitmap hardwareBitmap = inputBitmap.copy(Bitmap.Config.HARDWARE, /* isMutable= */ false);
HardwareBuffer hardwareBuffer = hardwareBitmap.getHardwareBuffer();
- assertThat(hardwareBuffer.getFormat()).isEqualTo(HardwareBuffer.RGBA_8888);
+ Format inputFormat =
+ new Format.Builder()
+ .setWidth(inputBitmap.getWidth())
+ .setHeight(inputBitmap.getHeight())
+ .setRotationDegrees(rotationDegrees)
+ .setColorInfo(ColorInfo.SDR_BT709_LIMITED)
+ .build();
+ HardwareBufferFrame inputHardwareBufferFrame =
+ new DefaultHardwareBufferFrame.Builder(hardwareBuffer).setFormat(inputFormat).build();
- int rotationDegrees = 90;
- int formatWidth = inputBitmap.getHeight();
- int formatHeight = inputBitmap.getWidth();
+ Matrix rotationMatrix = new Matrix();
+ rotationMatrix.postRotate(rotationDegrees);
+ Bitmap expectedBitmap =
+ Bitmap.createBitmap(
+ inputBitmap,
+ /* x= */ 0,
+ /* y= */ 0,
+ /* width= */ inputBitmap.getWidth(),
+ /* height= */ inputBitmap.getHeight(),
+ rotationMatrix,
+ /* filter= */ true);
- AtomicReference completedFrame = new AtomicReference<>();
FrameProcessor.Listener listener =
new FrameProcessor.Listener() {
@Override
public void onWakeup() {}
@Override
- public void onError(VideoFrameProcessingException exception) {}
+ public void onError(VideoFrameProcessingException exception) {
+ throw new AssertionError(exception);
+ }
@Override
public void onFrameProcessed(Frame frame, @Nullable SyncFenceWrapper releaseFence) {
@@ -336,40 +354,15 @@ public void onFrameProcessed(Frame frame, @Nullable SyncFenceWrapper releaseFenc
releaseFence.close();
}
hardwareBuffer.close();
- completedFrame.set(frame);
}
};
- Format inputFormat =
- new Format.Builder()
- .setWidth(formatWidth)
- .setHeight(formatHeight)
- .setRotationDegrees(rotationDegrees)
- .setColorInfo(ColorInfo.SDR_BT709_LIMITED)
- .build();
-
- HardwareBufferFrame inputHardwareBufferFrame =
- new DefaultHardwareBufferFrame.Builder(hardwareBuffer).setFormat(inputFormat).build();
-
Bitmap actualBitmap = convertAndCaptureBitmap(inputHardwareBufferFrame, listener);
- Matrix rotationMatrix = new Matrix();
- rotationMatrix.postRotate(rotationDegrees);
- Bitmap expectedBitmap =
- Bitmap.createBitmap(
- inputBitmap,
- /* x= */ 0,
- /* y= */ 0,
- /* width= */ inputBitmap.getWidth(),
- /* height= */ inputBitmap.getHeight(),
- rotationMatrix,
- /* filter= */ true);
-
assertThat(
getBitmapAveragePixelAbsoluteDifferenceArgb8888(
expectedBitmap, actualBitmap, testName.getMethodName()))
.isLessThan(MAX_PIXEL_DIFFERENCE);
- assertThat(completedFrame.get()).isSameInstanceAs(inputHardwareBufferFrame);
}
@SdkSuppress(minSdkVersion = 31)
@@ -448,6 +441,7 @@ private Bitmap convertAndCaptureBitmap(
actualBitmap.set(
createArgb8888BitmapFromFocusedGlFramebuffer(textureWidth, textureHeight));
glTextureFrame.release(/* releaseFence= */ null);
+ assertThat(glTextureFrame.format.rotationDegrees).isEqualTo(0);
GlUtil.deleteFbo(fboId);
// createArgb8888BitmapFromFocusedGlFramebuffer calls glReadPixels, which syncs
// OpenGL
diff --git a/libraries/effect/src/main/java/androidx/media3/effect/BitmapToHardwareBufferProcessor.java b/libraries/effect/src/main/java/androidx/media3/effect/BitmapToHardwareBufferProcessor.java
index 9c150ad01e9..9cfaa394d88 100644
--- a/libraries/effect/src/main/java/androidx/media3/effect/BitmapToHardwareBufferProcessor.java
+++ b/libraries/effect/src/main/java/androidx/media3/effect/BitmapToHardwareBufferProcessor.java
@@ -165,9 +165,16 @@ public HardwareBufferFrame process(HardwareBufferFrame inputFrame) {
outputFrame = checkNotNull(currentFrame).retain();
}
- inputFrame.release(/* releaseFence= */ null);
- return outputFrame
- .buildUpon()
+ return new HardwareBufferFrame.Builder(
+ checkNotNull(outputFrame.hardwareBuffer),
+ directExecutor(),
+ /* releaseCallback= */ (fence) -> {
+ outputFrame.release(fence);
+ // inputFrame is used when creating the outputFrame synchronously, it can be safely
+ // released earlier without a fence. Releasing it here with the outputFrame to keep
+ // backpressuring the upstream.
+ inputFrame.release(/* releaseFence= */ null);
+ })
.setFormat(inputFrame.format)
.setInternalFrame(nextBitmap)
.setReleaseTimeNs(inputFrame.releaseTimeNs)
diff --git a/libraries/effect/src/main/java/androidx/media3/effect/DefaultGlFrameProcessor.java b/libraries/effect/src/main/java/androidx/media3/effect/DefaultGlFrameProcessor.java
index 6b7c146a559..96b85d2eccf 100644
--- a/libraries/effect/src/main/java/androidx/media3/effect/DefaultGlFrameProcessor.java
+++ b/libraries/effect/src/main/java/androidx/media3/effect/DefaultGlFrameProcessor.java
@@ -37,13 +37,14 @@
import androidx.media3.common.util.Consumer;
import androidx.media3.common.util.ExperimentalApi;
import androidx.media3.common.util.Log;
+import androidx.media3.common.util.ThrowingRunnable;
import androidx.media3.common.video.AsyncFrame;
import androidx.media3.common.video.Frame;
import androidx.media3.common.video.FrameProcessor;
import androidx.media3.common.video.FrameWriter;
import androidx.media3.common.video.HardwareBufferFrame;
+import androidx.media3.common.util.ThrowingRunnable;
import androidx.media3.effect.DefaultGlTextureFrameCompositingProcessor.CompositorGlProgram;
-import androidx.media3.effect.FrameProcessorUtils.ThrowingRunnable;
import com.google.common.collect.ImmutableList;
import com.google.common.util.concurrent.ListeningExecutorService;
import java.util.Collections;
@@ -390,7 +391,7 @@ public void close() {
}
submitToGlExecutor(
() -> {
- ImmutableList.Builder closeActions = ImmutableList.builder();
+ ImmutableList.Builder> closeActions = ImmutableList.builder();
closeActions.addAll(getReleaseUnqueuedFramesActions());
closeActions.add(hardwareBufferConverter::close);
for (int i = 0; i < preProcessingChains.size(); i++) {
@@ -402,7 +403,7 @@ public void close() {
.add(compositingProcessor::close)
.add(postProcessingChain::close)
.add(frameWriterGlTextureFrameConsumer::close);
- runAllAndAccumulateExceptions(closeActions.build().toArray(new ThrowingRunnable[0]));
+ runAllAndAccumulateExceptions(closeActions.build().toArray(new ThrowingRunnable>[0]));
return null;
});
}
@@ -478,7 +479,7 @@ private void handleError(Exception exception) {
}
runAllAndAccumulateExceptions(
/* errorConsumer= */ exception::addSuppressed,
- getReleaseUnqueuedFramesActions().toArray(new ThrowingRunnable[0]));
+ getReleaseUnqueuedFramesActions().toArray(new ThrowingRunnable>[0]));
convertedGlTextureFrames.clear();
glTextureFramesQueuedDownstream.clear();
listenerExecutor.execute(() -> listener.onError(VideoFrameProcessingException.from(exception)));
@@ -524,8 +525,8 @@ private static int extractSequenceIndex(Frame frame) {
return (Integer) checkNotNull(frame.getMetadata().get(KEY_COMPOSITION_SEQUENCE_INDEX));
}
- private ImmutableList getReleaseUnqueuedFramesActions() {
- ImmutableList.Builder actions = ImmutableList.builder();
+ private ImmutableList> getReleaseUnqueuedFramesActions() {
+ ImmutableList.Builder> actions = ImmutableList.builder();
for (int i = 0; i < convertedGlTextureFrames.size(); i++) {
GlTextureFrame glTextureFrame = convertedGlTextureFrames.valueAt(i);
if (!glTextureFramesQueuedDownstream.contains(glTextureFrame)) {
diff --git a/libraries/effect/src/main/java/androidx/media3/effect/DefaultGlTextureFrameCompositingProcessor.java b/libraries/effect/src/main/java/androidx/media3/effect/DefaultGlTextureFrameCompositingProcessor.java
index b6d24a2d41a..3304df25783 100644
--- a/libraries/effect/src/main/java/androidx/media3/effect/DefaultGlTextureFrameCompositingProcessor.java
+++ b/libraries/effect/src/main/java/androidx/media3/effect/DefaultGlTextureFrameCompositingProcessor.java
@@ -81,6 +81,7 @@ void drawFrame(List framesToComposite, GlTextureInfo outputT
@Nullable
private Executor pendingWakeupExecutor;
+ // This is reset when a new batch of frames is queued.
private boolean inputStreamEnded;
/**
@@ -142,6 +143,7 @@ public boolean queue(
return false;
}
+ inputStreamEnded = false;
if (frames.size() == 1) {
return downstreamConsumer.queue(frames.get(0), listenerExecutor, wakeupListener);
}
diff --git a/libraries/effect/src/main/java/androidx/media3/effect/FrameProcessorUtils.java b/libraries/effect/src/main/java/androidx/media3/effect/FrameProcessorUtils.java
index a00ed0d0ef7..bd08ac6ed63 100644
--- a/libraries/effect/src/main/java/androidx/media3/effect/FrameProcessorUtils.java
+++ b/libraries/effect/src/main/java/androidx/media3/effect/FrameProcessorUtils.java
@@ -38,6 +38,7 @@
import androidx.media3.common.util.GlUtil;
import androidx.media3.common.util.GlUtil.GlException;
import androidx.media3.common.util.Log;
+import androidx.media3.common.util.ThrowingRunnable;
import androidx.media3.common.video.AsyncFrame;
import androidx.media3.common.video.SyncFenceWrapper;
import com.google.common.collect.ImmutableList;
@@ -50,7 +51,7 @@
@ExperimentalApi // TODO: b/505721737 Remove once FrameProcessor is production ready.
@RequiresApi(26)
-public final class FrameProcessorUtils {
+/* package */ final class FrameProcessorUtils {
private static final String TAG = "FrameProcessorUtils";
private static final long TIMEOUT_MS = 500;
@@ -256,12 +257,6 @@ public void onFailure(Throwable t) {
directExecutor());
}
- /** A runnable that throws an exception. */
- public interface ThrowingRunnable {
- /** Runs the operation. */
- void run() throws Exception;
- }
-
/**
* Executes multiple throwing actions sequentially, ensuring all are run even if some fail.
*
@@ -271,7 +266,7 @@ public interface ThrowingRunnable {
* @param actions The throwing actions to execute.
* @throws VideoFrameProcessingException If any of the actions fail.
*/
- public static void runAllAndAccumulateExceptions(ThrowingRunnable... actions)
+ public static void runAllAndAccumulateExceptions(ThrowingRunnable>... actions)
throws VideoFrameProcessingException {
@Nullable
VideoFrameProcessingException exception = runAllAndAccumulateExceptionInternal(actions);
@@ -291,7 +286,7 @@ public static void runAllAndAccumulateExceptions(ThrowingRunnable... actions)
* @param actions The throwing actions to execute.
*/
public static void runAllAndAccumulateExceptions(
- Consumer errorConsumer, ThrowingRunnable... actions) {
+ Consumer errorConsumer, ThrowingRunnable>... actions) {
@Nullable
VideoFrameProcessingException exception = runAllAndAccumulateExceptionInternal(actions);
if (exception != null) {
@@ -301,9 +296,9 @@ public static void runAllAndAccumulateExceptions(
@Nullable
private static VideoFrameProcessingException runAllAndAccumulateExceptionInternal(
- ThrowingRunnable... actions) {
+ ThrowingRunnable>... actions) {
@Nullable VideoFrameProcessingException firstException = null;
- for (ThrowingRunnable action : actions) {
+ for (ThrowingRunnable> action : actions) {
if (action == null) {
continue;
}
diff --git a/libraries/effect/src/main/java/androidx/media3/effect/FrameWriterGlTextureFrameConsumer.java b/libraries/effect/src/main/java/androidx/media3/effect/FrameWriterGlTextureFrameConsumer.java
index 0e6d2045714..cbcde6091cc 100644
--- a/libraries/effect/src/main/java/androidx/media3/effect/FrameWriterGlTextureFrameConsumer.java
+++ b/libraries/effect/src/main/java/androidx/media3/effect/FrameWriterGlTextureFrameConsumer.java
@@ -21,6 +21,7 @@
import static androidx.media3.effect.FrameProcessorUtils.releaseEglImageTexture;
import static androidx.media3.effect.FrameProcessorUtils.waitAndCloseFence;
import static com.google.common.base.Preconditions.checkArgument;
+import static com.google.common.base.Preconditions.checkNotNull;
import android.content.Context;
import android.hardware.HardwareBuffer;
@@ -28,26 +29,31 @@
import android.opengl.GLES20;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
+import androidx.media3.common.ColorInfo;
+import androidx.media3.common.Format;
import androidx.media3.common.VideoFrameProcessingException;
import androidx.media3.common.util.ExperimentalApi;
import androidx.media3.common.util.GlUtil;
import androidx.media3.common.util.GlUtil.GlException;
import androidx.media3.common.util.Log;
+import androidx.media3.common.util.Size;
import androidx.media3.common.video.AsyncFrame;
import androidx.media3.common.video.FrameWriter;
import androidx.media3.common.video.HardwareBufferFrame;
import androidx.media3.common.video.SyncFenceWrapper;
import androidx.media3.effect.FrameProcessorUtils.EglImageTextureWrapper;
import com.google.common.collect.ImmutableList;
+import java.util.Objects;
import java.util.concurrent.Executor;
import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
-/** A {@link GlTextureFrameConsumer} that writes the input to a {@link FrameWriter}. */
+/** Writes input texture frames to a {@link FrameWriter}. */
@ExperimentalApi // TODO: b/505721737 Remove once FrameProcessor is production ready.
@RequiresApi(26)
/* package */ final class FrameWriterGlTextureFrameConsumer implements GlTextureFrameConsumer {
private static final String TAG = "FrameWriterGlTexCons";
+ private static final long OUTPUT_USAGE = USAGE_GPU_COLOR_OUTPUT;
private final Context context;
private final FrameWriter frameWriter;
@@ -55,6 +61,9 @@
@Nullable private DefaultShaderProgram defaultShaderProgram;
private @MonotonicNonNull EGLDisplay eglDisplay;
+ private @MonotonicNonNull Size inputSize;
+ private @MonotonicNonNull ColorInfo inputColorInfo;
+ private @MonotonicNonNull Format outputFormat;
private boolean isFrameWriterConfigured;
FrameWriterGlTextureFrameConsumer(
@@ -70,10 +79,13 @@ public boolean queue(
GlTextureFrame inputFrame, Executor listenerExecutor, Runnable wakeupListener)
throws VideoFrameProcessingException {
if (!isFrameWriterConfigured) {
- frameWriter.configure(inputFrame.format, USAGE_GPU_COLOR_OUTPUT);
+ outputFormat = establishOutputFormat(inputFrame.format);
+ frameWriter.configure(outputFormat, OUTPUT_USAGE);
isFrameWriterConfigured = true;
}
+ maybeReconfigureShader(inputFrame);
+
AsyncFrame asyncFrame = frameWriter.dequeueInputFrame(listenerExecutor, wakeupListener);
if (asyncFrame == null) {
return false;
@@ -101,15 +113,9 @@ public boolean queue(
/* target= */ GLES20.GL_TEXTURE_2D,
/* writesToBoundImage= */ true);
- if (defaultShaderProgram == null) {
- defaultShaderProgram =
- DefaultShaderProgram.create(
- context,
- /* matrixTransformations= */ ImmutableList.of(),
- /* rgbMatrices= */ ImmutableList.of(),
- /* useHdr= */ false);
- }
- defaultShaderProgram.drawFrame(inputFrame.glTextureInfo.texId, inputFrame.presentationTimeUs);
+ GlUtil.clearFocusedBuffersOpaque();
+ checkNotNull(defaultShaderProgram)
+ .drawFrame(inputFrame.glTextureInfo.texId, inputFrame.presentationTimeUs);
GlUtil.checkGlError();
ImmutableList fences = generateSyncFences(/* count= */ 2);
if (fences.size() == 2) {
@@ -151,8 +157,119 @@ public void signalEndOfStream() {
@Override
public void close() throws VideoFrameProcessingException {
+ // Release frame writer first in case releasing the shader program throw
+ frameWriter.close();
if (defaultShaderProgram != null) {
defaultShaderProgram.release();
+ defaultShaderProgram = null;
+ }
+ }
+
+ /** Establishes the output format based on the first frame. */
+ private Format establishOutputFormat(Format inputFormat) {
+ int rotationDegrees = calculateOutputRotationDegrees(inputFormat);
+ int outputWidth =
+ (rotationDegrees == 90 || rotationDegrees == 270) ? inputFormat.height : inputFormat.width;
+ int outputHeight =
+ (rotationDegrees == 90 || rotationDegrees == 270) ? inputFormat.width : inputFormat.height;
+ return updateFormat(
+ inputFormat,
+ outputWidth,
+ outputHeight,
+ // Sets the degrees that the player needs to rotate. If we rotated 90 degrees, the player
+ // needs to rotate -90 degrees, which is equivalent to rotating it 270 degrees.
+ /* rotationDegrees= */ (360 - rotationDegrees) % 360);
+ }
+
+ /** Reconfigures the shader program if the input size or color space changed. */
+ private void maybeReconfigureShader(GlTextureFrame inputFrame)
+ throws VideoFrameProcessingException {
+ Format inputFormat = inputFrame.format;
+ // The frames in the GL pipeline should always be in the intended orientation.
+ checkArgument(inputFormat.rotationDegrees == 0);
+ ColorInfo inputColorInfo = checkNotNull(inputFormat.colorInfo);
+ if (inputSize != null
+ && inputFormat.width == inputSize.getWidth()
+ && inputFormat.height == inputSize.getHeight()
+ && Objects.equals(inputColorInfo, this.inputColorInfo)) {
+ return;
+ }
+
+ if (defaultShaderProgram != null) {
+ defaultShaderProgram.release();
+ defaultShaderProgram = null;
+ }
+
+ // Force physical rotation to match the logical rotation of the established output format.
+ // The output format stores the rotation that player applies. We convert it back to the degrees
+ // the pipeline needs to rotate.
+ int rotationDegrees = (360 - checkNotNull(outputFormat).rotationDegrees) % 360;
+
+ // Flip OpenGL coordinate system back and rotate.
+ GlMatrixTransformation flipAndRotate =
+ new ScaleAndRotateTransformation.Builder()
+ .setScale(/* scaleX= */ 1f, /* scaleY= */ -1f)
+ .setRotationDegrees(rotationDegrees)
+ .build();
+
+ ImmutableList.Builder transformationsBuilder = ImmutableList.builder();
+ transformationsBuilder
+ .add(flipAndRotate)
+ .add(
+ Presentation.createForWidthAndHeight(
+ outputFormat.width, outputFormat.height, Presentation.LAYOUT_SCALE_TO_FIT));
+
+ defaultShaderProgram =
+ DefaultShaderProgram.create(
+ context,
+ /* matrixTransformations= */ transformationsBuilder.build(),
+ /* rgbMatrices= */ ImmutableList.of(),
+ /* useHdr= */ ColorInfo.isTransferHdr(inputColorInfo));
+
+ inputSize = new Size(inputFormat.width, inputFormat.height);
+ this.inputColorInfo = inputColorInfo;
+ Size unusedSize =
+ defaultShaderProgram.configure(
+ inputFrame.glTextureInfo.width, inputFrame.glTextureInfo.height);
+ }
+
+ private int calculateOutputRotationDegrees(Format format) {
+ if (format.width >= format.height) {
+ // Input is landscape, no rotation needed.
+ return 0;
+ }
+
+ if (frameWriter.getInfo().isSupported(format, OUTPUT_USAGE)) {
+ // Portrait is supported, no rotation needed.
+ return 0;
}
+
+ // If frameWriter doesn't support portrait, try rotating the input by 90 or 270 degrees to swap
+ // to landscape dimensions supported by the encoder.
+ int rotatedWidth = format.height;
+ int rotatedHeight = format.width;
+ Format formatRotate90 =
+ updateFormat(format, rotatedWidth, rotatedHeight, /* rotationDegrees= */ 90);
+ if (frameWriter.getInfo().isSupported(formatRotate90, OUTPUT_USAGE)) {
+ return 90;
+ }
+
+ Format formatRotate270 =
+ updateFormat(format, rotatedWidth, rotatedHeight, /* rotationDegrees= */ 270);
+ if (frameWriter.getInfo().isSupported(formatRotate270, OUTPUT_USAGE)) {
+ return 270;
+ }
+
+ // Fallback if nothing is supported.
+ return 0;
+ }
+
+ private static Format updateFormat(Format format, int width, int height, int rotationDegrees) {
+ return format
+ .buildUpon()
+ .setWidth(width)
+ .setHeight(height)
+ .setRotationDegrees(rotationDegrees)
+ .build();
}
}
diff --git a/libraries/effect/src/main/java/androidx/media3/effect/GlTextureFrameAggregator.java b/libraries/effect/src/main/java/androidx/media3/effect/GlTextureFrameAggregator.java
index e9808e26145..ba52167794c 100644
--- a/libraries/effect/src/main/java/androidx/media3/effect/GlTextureFrameAggregator.java
+++ b/libraries/effect/src/main/java/androidx/media3/effect/GlTextureFrameAggregator.java
@@ -26,7 +26,7 @@
import androidx.annotation.RequiresApi;
import androidx.media3.common.VideoFrameProcessingException;
import androidx.media3.common.util.Consumer;
-import androidx.media3.effect.FrameProcessorUtils.ThrowingRunnable;
+import androidx.media3.common.util.ThrowingRunnable;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.util.concurrent.ListeningExecutorService;
@@ -52,6 +52,7 @@
@Nullable private ImmutableSet pendingSequenceIndices;
private boolean isClosed;
+ // This is reset when a new batch of frames is queued.
private boolean allInputStreamsEnded;
public GlTextureFrameAggregator(
@@ -288,12 +289,12 @@ private ImmutableList retrieveAndClearPendingFrames() {
}
private void releaseFrames(List frames) throws VideoFrameProcessingException {
- ImmutableList.Builder releaseActions = new ImmutableList.Builder<>();
+ ImmutableList.Builder> releaseActions = new ImmutableList.Builder<>();
for (int i = 0; i < frames.size(); i++) {
GlTextureFrame frame = frames.get(i);
releaseActions.add(() -> frame.release(/* releaseFence= */ null));
}
- runAllAndAccumulateExceptions(releaseActions.build().toArray(new ThrowingRunnable[0]));
+ runAllAndAccumulateExceptions(releaseActions.build().toArray(new ThrowingRunnable>[0]));
}
private final class InputFrameConsumer implements GlTextureFrameConsumer {
@@ -301,6 +302,7 @@ private final class InputFrameConsumer implements GlTextureFrameConsumer {
@Nullable private Runnable pendingWakeupListener;
@Nullable private Executor pendingWakeupExecutor;
@Nullable private GlTextureFrame pendingFrame;
+ // This is reset when a new batch of frames is queued.
private boolean isEnded;
private boolean isActive;
@@ -324,6 +326,8 @@ public boolean queue(GlTextureFrame frame, Executor listenerExecutor, Runnable w
return false;
}
+ isEnded = false;
+ allInputStreamsEnded = false;
pendingFrame = frame;
tryQueueToCompositingProcessor();
// Returning true to signal the input frame is queued. If queueing to the compositing
diff --git a/libraries/effect/src/main/java/androidx/media3/effect/GlTextureFrameProcessorChain.java b/libraries/effect/src/main/java/androidx/media3/effect/GlTextureFrameProcessorChain.java
index ef49887f156..76ab5e6fef8 100644
--- a/libraries/effect/src/main/java/androidx/media3/effect/GlTextureFrameProcessorChain.java
+++ b/libraries/effect/src/main/java/androidx/media3/effect/GlTextureFrameProcessorChain.java
@@ -27,8 +27,8 @@
import androidx.media3.common.GlObjectsProvider;
import androidx.media3.common.VideoFrameProcessingException;
import androidx.media3.common.util.Consumer;
+import androidx.media3.common.util.ThrowingRunnable;
import androidx.media3.common.util.Util;
-import androidx.media3.effect.FrameProcessorUtils.ThrowingRunnable;
import com.google.common.collect.ImmutableList;
import com.google.common.util.concurrent.ListeningExecutorService;
import java.util.ArrayList;
@@ -156,7 +156,7 @@ private ImmutableList extractEffects(GlTextureFrame frame) {
}
private void closeAllProcessors() throws VideoFrameProcessingException {
- ThrowingRunnable[] closeActions = new ThrowingRunnable[effectProcessorChain.size()];
+ ThrowingRunnable>[] closeActions = new ThrowingRunnable>[effectProcessorChain.size()];
for (int i = 0; i < effectProcessorChain.size(); i++) {
closeActions[i] = effectProcessorChain.get(i)::close;
}
diff --git a/libraries/effect/src/main/java/androidx/media3/effect/HardwareBufferToGlTextureConverter.java b/libraries/effect/src/main/java/androidx/media3/effect/HardwareBufferToGlTextureConverter.java
index 62da11b6324..8d11c7ee2cf 100644
--- a/libraries/effect/src/main/java/androidx/media3/effect/HardwareBufferToGlTextureConverter.java
+++ b/libraries/effect/src/main/java/androidx/media3/effect/HardwareBufferToGlTextureConverter.java
@@ -97,8 +97,9 @@ public GlTextureFrame convert(
@Nullable EglImageTextureWrapper eglImageTextureWrapper = null;
int internalTexId = C.INDEX_UNSET;
Format inputFormat = hardwareBufferFrame.getFormat();
- int outputWidth = inputFormat.width;
- int outputHeight = inputFormat.height;
+ boolean isRotated = inputFormat.rotationDegrees == 90 || inputFormat.rotationDegrees == 270;
+ int outputWidth = isRotated ? inputFormat.height : inputFormat.width;
+ int outputHeight = isRotated ? inputFormat.width : inputFormat.height;
try {
if (eglDisplay == null) {
eglDisplay = GlUtil.getDefaultEglDisplay();
@@ -198,7 +199,16 @@ public GlTextureFrame convert(
}
})
.setPresentationTimeUs(hardwareBufferFrame.getContentTimeUs())
- .setFormat(hardwareBufferFrame.getFormat())
+ .setFormat(
+ hardwareBufferFrame
+ .getFormat()
+ .buildUpon()
+ .setWidth(outputWidth)
+ .setHeight(outputHeight)
+ // Reset rotation to 0 because we rotated the frame physically with OpenGL. The
+ // pipeline should always receive frames in their intended orientation.
+ .setRotationDegrees(0)
+ .build())
.setMetadata(hardwareBufferFrame.getMetadata())
.build();
}
diff --git a/libraries/effect/src/main/res/raw/fragment_shader_transformation_external_yuv_es3.glsl b/libraries/effect/src/main/res/raw/fragment_shader_transformation_external_yuv_es3.glsl
index d28264aeb9b..5a8dcba4e10 100644
--- a/libraries/effect/src/main/res/raw/fragment_shader_transformation_external_yuv_es3.glsl
+++ b/libraries/effect/src/main/res/raw/fragment_shader_transformation_external_yuv_es3.glsl
@@ -305,5 +305,8 @@ void main() {
? vec4(applyBt2020ToBt709Ootf(opticalColorBt2020), 1.0)
: vec4(scaleHdrLuminance(opticalColorBt2020), 1.0);
vec4 transformedColors = uRgbMatrix * opticalColor;
- outColor = vec4(applyOetf(transformedColors.rgb), 1.0);
+ vec3 clampedColors = (uApplyHdrToSdrToneMapping == 1)
+ ? clamp(transformedColors.rgb, 0.0, 1.0)
+ : max(transformedColors.rgb, 0.0);
+ outColor = vec4(applyOetf(clampedColors), 1.0);
}
diff --git a/libraries/effect/src/main/res/raw/fragment_shader_transformation_hdr_internal_es3.glsl b/libraries/effect/src/main/res/raw/fragment_shader_transformation_hdr_internal_es3.glsl
index b50114187d1..f62291d4bd5 100644
--- a/libraries/effect/src/main/res/raw/fragment_shader_transformation_hdr_internal_es3.glsl
+++ b/libraries/effect/src/main/res/raw/fragment_shader_transformation_hdr_internal_es3.glsl
@@ -292,5 +292,8 @@ void main() {
? vec4(applyBt2020ToBt709Ootf(opticalColorBt2020), 1.0)
: vec4(scaleHdrLuminance(opticalColorBt2020), 1.0);
vec4 transformedColors = uRgbMatrix * opticalColor;
- outColor = vec4(applyOetf(transformedColors.rgb), 1.0);
+ vec3 clampedColors = (uApplyHdrToSdrToneMapping == 1)
+ ? clamp(transformedColors.rgb, 0.0, 1.0)
+ : max(transformedColors.rgb, 0.0);
+ outColor = vec4(applyOetf(clampedColors), 1.0);
}
diff --git a/libraries/effect/src/test/java/androidx/media3/effect/BitmapToHardwareBufferProcessorTest.java b/libraries/effect/src/test/java/androidx/media3/effect/BitmapToHardwareBufferProcessorTest.java
new file mode 100644
index 00000000000..d312006583d
--- /dev/null
+++ b/libraries/effect/src/test/java/androidx/media3/effect/BitmapToHardwareBufferProcessorTest.java
@@ -0,0 +1,90 @@
+/*
+ * Copyright 2026 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 androidx.media3.effect;
+
+import static com.google.common.truth.Truth.assertThat;
+import static com.google.common.util.concurrent.MoreExecutors.directExecutor;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import android.graphics.Bitmap;
+import androidx.test.ext.junit.runners.AndroidJUnit4;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.robolectric.annotation.Config;
+
+/** Robolectric tests for {@link BitmapToHardwareBufferProcessor}. */
+@RunWith(AndroidJUnit4.class)
+@Config(minSdk = 26) // HardwareBuffers only exist on API26+.
+public final class BitmapToHardwareBufferProcessorTest {
+
+ private ExecutorService executorService;
+ private BitmapToHardwareBufferProcessor processor;
+ private HardwareBufferJniWrapper mockJniWrapper;
+
+ @Before
+ public void setUp() {
+ executorService = Executors.newSingleThreadExecutor();
+ mockJniWrapper = mock(HardwareBufferJniWrapper.class);
+ when(mockJniWrapper.nativeCopyBitmapToHardwareBuffer(any(), any())).thenReturn(true);
+
+ processor =
+ new BitmapToHardwareBufferProcessor(
+ mockJniWrapper,
+ /* internalExecutor= */ executorService,
+ /* errorExecutor= */ directExecutor(),
+ /* errorCallback= */ (e) -> {
+ throw new AssertionError(e);
+ });
+ }
+
+ @After
+ public void tearDown() {
+ if (processor != null) {
+ processor.close();
+ }
+ if (executorService != null) {
+ executorService.shutdown();
+ }
+ }
+
+ @Test
+ public void process_defersInputFrameReleaseUntilOutputFrameRelease() {
+ CountDownLatch releasedLatch = new CountDownLatch(1);
+ HardwareBufferFrame inputFrame =
+ new HardwareBufferFrame.Builder(
+ /* hardwareBuffer= */ null,
+ directExecutor(),
+ /* releaseCallback= */ (fence) -> releasedLatch.countDown())
+ .setInternalFrame(Bitmap.createBitmap(10, 10, Bitmap.Config.ARGB_8888))
+ .build();
+
+ HardwareBufferFrame outputFrame = processor.process(inputFrame);
+
+ // The output frame was generated, but the input frame should NOT be released yet.
+ assertThat(releasedLatch.getCount()).isEqualTo(1);
+
+ // Release the downstream output frame should trigger releasing the inputFrame
+ outputFrame.release(/* releaseFence= */ null);
+ assertThat(releasedLatch.getCount()).isEqualTo(0);
+ }
+}
diff --git a/libraries/effect/src/test/java/androidx/media3/effect/DefaultGlTextureFrameCompositingProcessorTest.java b/libraries/effect/src/test/java/androidx/media3/effect/DefaultGlTextureFrameCompositingProcessorTest.java
index 08af529b044..27576f7fc40 100644
--- a/libraries/effect/src/test/java/androidx/media3/effect/DefaultGlTextureFrameCompositingProcessorTest.java
+++ b/libraries/effect/src/test/java/androidx/media3/effect/DefaultGlTextureFrameCompositingProcessorTest.java
@@ -250,6 +250,59 @@ public void queue_multipleFrames_preservesPrimaryFrameMetadata() throws Exceptio
.isEqualTo("CUSTOM_VALUE");
}
+ @Test
+ public void queue_afterSignalEndOfStream_doesNotPrematurelySignalEos() throws Exception {
+ GlTextureFrame frame1 = createGlTextureFrame(/* texId= */ 0, /* sequenceIndex= */ 0);
+ GlTextureFrame frame2 = createGlTextureFrame(/* texId= */ 1, /* sequenceIndex= */ 1);
+ assertThat(
+ compositingProcessor.queue(
+ ImmutableList.of(frame1, frame2), directExecutor(), () -> {}))
+ .isTrue();
+ assertThat(downstreamConsumer.framesReceived).isEqualTo(1);
+
+ compositingProcessor.signalEndOfStream();
+ assertThat(downstreamConsumer.signalEndOfStreamCalled).isTrue();
+
+ downstreamConsumer.signalEndOfStreamCalled = false;
+
+ GlTextureFrame frame3 = createGlTextureFrame(/* texId= */ 2, /* sequenceIndex= */ 0);
+ GlTextureFrame frame4 = createGlTextureFrame(/* texId= */ 3, /* sequenceIndex= */ 1);
+ assertThat(
+ compositingProcessor.queue(
+ ImmutableList.of(frame3, frame4), directExecutor(), () -> {}))
+ .isTrue();
+
+ assertThat(downstreamConsumer.framesReceived).isEqualTo(2);
+ assertThat(downstreamConsumer.signalEndOfStreamCalled).isFalse();
+
+ compositingProcessor.signalEndOfStream();
+ assertThat(downstreamConsumer.signalEndOfStreamCalled).isTrue();
+ }
+
+ @Test
+ public void queue_singleFrameAfterSignalEndOfStream_doesNotPrematurelySignalEos()
+ throws Exception {
+ GlTextureFrame frame1 = createGlTextureFrame(/* texId= */ 0, /* sequenceIndex= */ 0);
+ assertThat(compositingProcessor.queue(ImmutableList.of(frame1), directExecutor(), () -> {}))
+ .isTrue();
+ assertThat(downstreamConsumer.framesReceived).isEqualTo(1);
+
+ compositingProcessor.signalEndOfStream();
+ assertThat(downstreamConsumer.signalEndOfStreamCalled).isTrue();
+
+ downstreamConsumer.signalEndOfStreamCalled = false;
+
+ GlTextureFrame frame2 = createGlTextureFrame(/* texId= */ 1, /* sequenceIndex= */ 0);
+ assertThat(compositingProcessor.queue(ImmutableList.of(frame2), directExecutor(), () -> {}))
+ .isTrue();
+
+ assertThat(downstreamConsumer.framesReceived).isEqualTo(2);
+ assertThat(downstreamConsumer.signalEndOfStreamCalled).isFalse();
+
+ compositingProcessor.signalEndOfStream();
+ assertThat(downstreamConsumer.signalEndOfStreamCalled).isTrue();
+ }
+
private static GlTextureFrame createGlTextureFrame(int texId, int sequenceIndex) {
return new GlTextureFrame.Builder(
new GlTextureInfo(
diff --git a/libraries/effect/src/test/java/androidx/media3/effect/GlTextureFrameAggregatorTest.java b/libraries/effect/src/test/java/androidx/media3/effect/GlTextureFrameAggregatorTest.java
index a0ada35128a..1ed5a9b74a9 100644
--- a/libraries/effect/src/test/java/androidx/media3/effect/GlTextureFrameAggregatorTest.java
+++ b/libraries/effect/src/test/java/androidx/media3/effect/GlTextureFrameAggregatorTest.java
@@ -214,6 +214,63 @@ public void signalEndOfStream_whenCompositorRejectsInput_defersEosUntilWakeup()
assertThat(downstreamConsumer.signalEndOfStreamCalled).isTrue();
}
+ @Test
+ public void signalEndOfStream_afterEndOfStreamAndQueue_signalsDownstreamAgain() throws Exception {
+ frameAggregator.configureSequenceIndices(ImmutableSet.of(0));
+ GlTextureFrame frame1 = createGlTextureFrame(/* sequenceIndex= */ 0);
+
+ assertThat(frameAggregator.getInputConsumer(0).queue(frame1, directExecutor(), () -> {}))
+ .isTrue();
+ frameAggregator.getInputConsumer(0).signalEndOfStream();
+ assertThat(downstreamConsumer.signalEndOfStreamCalled).isTrue();
+
+ downstreamConsumer.signalEndOfStreamCalled = false;
+
+ GlTextureFrame frame2 = createGlTextureFrame(/* sequenceIndex= */ 0);
+ assertThat(frameAggregator.getInputConsumer(0).queue(frame2, directExecutor(), () -> {}))
+ .isTrue();
+ assertThat(downstreamConsumer.signalEndOfStreamCalled).isFalse();
+
+ frameAggregator.getInputConsumer(0).signalEndOfStream();
+ assertThat(downstreamConsumer.signalEndOfStreamCalled).isTrue();
+ }
+
+ @Test
+ public void
+ signalEndOfStream_afterEndOfStreamAndQueueWithMultipleSequences_signalsDownstreamAgain()
+ throws Exception {
+ frameAggregator.configureSequenceIndices(/* requestedSequenceIndices= */ ImmutableSet.of(0, 1));
+ // Batch A, sequences 0 and 1
+ GlTextureFrame frame0A = createGlTextureFrame(/* sequenceIndex= */ 0);
+ GlTextureFrame frame1A = createGlTextureFrame(/* sequenceIndex= */ 1);
+
+ assertThat(frameAggregator.getInputConsumer(0).queue(frame0A, directExecutor(), () -> {}))
+ .isTrue();
+ assertThat(frameAggregator.getInputConsumer(1).queue(frame1A, directExecutor(), () -> {}))
+ .isTrue();
+ frameAggregator.getInputConsumer(0).signalEndOfStream();
+ frameAggregator.getInputConsumer(1).signalEndOfStream();
+ assertThat(downstreamConsumer.signalEndOfStreamCalled).isTrue();
+
+ downstreamConsumer.signalEndOfStreamCalled = false;
+ // Batch B, sequences 0 and 1
+ GlTextureFrame frame0B = createGlTextureFrame(/* sequenceIndex= */ 0);
+ GlTextureFrame frame1B = createGlTextureFrame(/* sequenceIndex= */ 1);
+ assertThat(frameAggregator.getInputConsumer(0).queue(frame0B, directExecutor(), () -> {}))
+ .isTrue();
+ assertThat(frameAggregator.getInputConsumer(1).queue(frame1B, directExecutor(), () -> {}))
+ .isTrue();
+ assertThat(downstreamConsumer.signalEndOfStreamCalled).isFalse();
+
+ frameAggregator.getInputConsumer(0).signalEndOfStream();
+ // Downstream EOS is not yet signaled because sequence 1 hasn't signaled EOS since frame1B.
+ assertThat(downstreamConsumer.signalEndOfStreamCalled).isFalse();
+
+ // Verify downstream EOS is signaled only after sequence 1 also signals EOS again.
+ frameAggregator.getInputConsumer(1).signalEndOfStream();
+ assertThat(downstreamConsumer.signalEndOfStreamCalled).isTrue();
+ }
+
@Test
public void queue_whenDownstreamThrowsVideoFrameProcessingException_throwsIllegalStateException()
throws Exception {
diff --git a/libraries/effect_ndk/proguard-rules.txt b/libraries/effect_ndk/proguard-rules.txt
new file mode 100644
index 00000000000..5264ca5ff73
--- /dev/null
+++ b/libraries/effect_ndk/proguard-rules.txt
@@ -0,0 +1,6 @@
+# Proguard rules specific to the effect NDK module.
+
+# This prevents the names of native methods from being obfuscated.
+-keep,includedescriptorclasses class androidx.media3.effect.ndk.** {
+ native ;
+}
diff --git a/libraries/effect_ndk/src/main/jni/CMakeLists.txt b/libraries/effect_ndk/src/main/jni/CMakeLists.txt
index 635554366e6..4e25dc5363c 100644
--- a/libraries/effect_ndk/src/main/jni/CMakeLists.txt
+++ b/libraries/effect_ndk/src/main/jni/CMakeLists.txt
@@ -30,7 +30,9 @@ find_library(jnigraphics-lib jnigraphics)
add_library(${PROJECT_NAME} SHARED hardware_buffer_jni.cc)
-
+set_target_properties(${PROJECT_NAME} PROPERTIES
+ CXX_VISIBILITY_PRESET hidden
+ VISIBILITY_INLINES_HIDDEN ON)
target_link_libraries(${PROJECT_NAME}
${log-lib}
diff --git a/libraries/effect_ndk/src/main/jni/hardware_buffer_jni.cc b/libraries/effect_ndk/src/main/jni/hardware_buffer_jni.cc
index 103962e8117..1b2722e03f7 100644
--- a/libraries/effect_ndk/src/main/jni/hardware_buffer_jni.cc
+++ b/libraries/effect_ndk/src/main/jni/hardware_buffer_jni.cc
@@ -59,39 +59,9 @@ static void initializeEGLFunctions() {
}
}
-jint JNI_OnLoad(JavaVM* vm, void* reserved) {
- JNIEnv* env;
- if (vm->GetEnv(reinterpret_cast(&env), JNI_VERSION_1_6) != JNI_OK) {
- return -1;
- }
-
- if (API_AT_LEAST(26)) {
- s_AHardwareBuffer_fromHardwareBuffer = AHardwareBuffer_fromHardwareBuffer;
- s_AHardwareBuffer_describe = AHardwareBuffer_describe;
- s_AHardwareBuffer_lock = AHardwareBuffer_lock;
- s_AHardwareBuffer_unlock = AHardwareBuffer_unlock;
- } else {
- LOGE(
- "AHardwareBuffer APIs are not available on this Android version (API < "
- "26)");
- return -1;
- }
-
- initializeEGLFunctions();
- if (eglCreateImageKHR == nullptr ||
- eglGetNativeClientBufferANDROID == nullptr ||
- glEGLImageTargetTexture2DOES == nullptr ||
- eglDestroyImageKHR == nullptr) {
- LOGE("Failed to get addresses of GL/EGL functions.");
- return -1;
- }
- return JNI_VERSION_1_6;
-}
-
-extern "C" JNIEXPORT jlong JNICALL
-Java_androidx_media3_effect_ndk_HardwareBufferJni_nativeCreateEglImageFromHardwareBuffer(
- JNIEnv* env, jobject /* this */, jlong displayHandle,
- jobject hardwareBufferJava) {
+jlong nativeCreateEglImageFromHardwareBuffer(JNIEnv* env, jobject /* this */,
+ jlong displayHandle,
+ jobject hardwareBufferJava) {
EGLDisplay display = reinterpret_cast(displayHandle);
if (display == EGL_NO_DISPLAY) {
LOGE("Invalid EGL display");
@@ -119,9 +89,8 @@ Java_androidx_media3_effect_ndk_HardwareBufferJni_nativeCreateEglImageFromHardwa
return (jlong)eglImage;
}
-extern "C" JNIEXPORT jboolean JNICALL
-Java_androidx_media3_effect_ndk_HardwareBufferJni_nativeBindEGLImage(
- JNIEnv* env, jobject clazz, jint target, jlong eglImageHandle) {
+jboolean nativeBindEGLImage(JNIEnv* /* env */, jobject /* this */, jint target,
+ jlong eglImageHandle) {
if (eglImageHandle == 0) {
LOGE("Invalid eglImageHandle (0)");
return JNI_FALSE;
@@ -140,10 +109,9 @@ Java_androidx_media3_effect_ndk_HardwareBufferJni_nativeBindEGLImage(
return JNI_TRUE;
}
-extern "C" JNIEXPORT jboolean JNICALL
-Java_androidx_media3_effect_ndk_HardwareBufferJni_nativeDestroyEGLImage(
- JNIEnv* env, jobject clazz, jlong displayHandle, jlong imageHandle) {
- if (imageHandle == 0) {
+jboolean nativeDestroyEGLImage(JNIEnv* /* env */, jobject /* this */,
+ jlong displayHandle, jlong eglImageHandle) {
+ if (eglImageHandle == 0) {
LOGE("Invalid eglImageHandle (0)");
return JNI_FALSE;
}
@@ -153,7 +121,7 @@ Java_androidx_media3_effect_ndk_HardwareBufferJni_nativeDestroyEGLImage(
return JNI_FALSE;
}
- EGLImageKHR image = reinterpret_cast(imageHandle);
+ EGLImageKHR image = reinterpret_cast(eglImageHandle);
EGLBoolean result = eglDestroyImageKHR(display, image);
if (result == EGL_TRUE) {
@@ -164,12 +132,12 @@ Java_androidx_media3_effect_ndk_HardwareBufferJni_nativeDestroyEGLImage(
}
}
-extern "C" JNIEXPORT jboolean JNICALL
-Java_androidx_media3_effect_ndk_HardwareBufferJni_nativeCopyBitmapToHardwareBuffer(
- JNIEnv* env, jobject clazz, jobject bitmap, jobject hardwareBuffer) {
+jboolean nativeCopyBitmapToHardwareBuffer(JNIEnv* env, jobject /* this */,
+ jobject bitmap,
+ jobject hardwareBufferJava) {
AHardwareBuffer* hb;
if (API_AT_LEAST(26)) {
- hb = s_AHardwareBuffer_fromHardwareBuffer(env, hardwareBuffer);
+ hb = s_AHardwareBuffer_fromHardwareBuffer(env, hardwareBufferJava);
} else {
LOGE(
"AHardwareBuffer APIs are not available on this Android version (API < "
@@ -254,9 +222,10 @@ Java_androidx_media3_effect_ndk_HardwareBufferJni_nativeCopyBitmapToHardwareBuff
return success;
}
-extern "C" JNIEXPORT jboolean JNICALL
-Java_androidx_media3_effect_ndk_HardwareBufferJni_nativeCopyHardwareBufferToHardwareBuffer(
- JNIEnv* env, jobject clazz, jobject srcHbJava, jobject dstHbJava) {
+jboolean nativeCopyHardwareBufferToHardwareBuffer(JNIEnv* env,
+ jobject /* this */,
+ jobject srcHbJava,
+ jobject dstHbJava) {
if (env->IsSameObject(srcHbJava, dstHbJava)) {
return JNI_TRUE;
}
@@ -338,3 +307,59 @@ Java_androidx_media3_effect_ndk_HardwareBufferJni_nativeCopyHardwareBufferToHard
return JNI_TRUE;
}
+
+extern "C" JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* vm, void* reserved) {
+ JNIEnv* env;
+ if (vm->GetEnv(reinterpret_cast(&env), JNI_VERSION_1_6) != JNI_OK) {
+ return -1;
+ }
+
+ if (API_AT_LEAST(26)) {
+ s_AHardwareBuffer_fromHardwareBuffer = AHardwareBuffer_fromHardwareBuffer;
+ s_AHardwareBuffer_describe = AHardwareBuffer_describe;
+ s_AHardwareBuffer_lock = AHardwareBuffer_lock;
+ s_AHardwareBuffer_unlock = AHardwareBuffer_unlock;
+ } else {
+ LOGE(
+ "AHardwareBuffer APIs are not available on this Android version (API < "
+ "26)");
+ return -1;
+ }
+
+ initializeEGLFunctions();
+ if (eglCreateImageKHR == nullptr ||
+ eglGetNativeClientBufferANDROID == nullptr ||
+ glEGLImageTargetTexture2DOES == nullptr ||
+ eglDestroyImageKHR == nullptr) {
+ LOGE("Failed to get addresses of GL/EGL functions.");
+ return -1;
+ }
+
+ jclass clazz = env->FindClass("androidx/media3/effect/ndk/HardwareBufferJni");
+ if (!clazz) {
+ LOGE("JNI_OnLoad: FindClass failed for HardwareBufferJni");
+ return -1;
+ }
+ static const JNINativeMethod kMethods[] = {
+ {"nativeCreateEglImageFromHardwareBuffer",
+ "(JLandroid/hardware/HardwareBuffer;)J",
+ reinterpret_cast(nativeCreateEglImageFromHardwareBuffer)},
+ {"nativeBindEGLImage", "(IJ)Z",
+ reinterpret_cast(nativeBindEGLImage)},
+ {"nativeDestroyEGLImage", "(JJ)Z",
+ reinterpret_cast(nativeDestroyEGLImage)},
+ {"nativeCopyBitmapToHardwareBuffer",
+ "(Landroid/graphics/Bitmap;Landroid/hardware/HardwareBuffer;)Z",
+ reinterpret_cast(nativeCopyBitmapToHardwareBuffer)},
+ {"nativeCopyHardwareBufferToHardwareBuffer",
+ "(Landroid/hardware/HardwareBuffer;Landroid/hardware/HardwareBuffer;)Z",
+ reinterpret_cast(nativeCopyHardwareBufferToHardwareBuffer)},
+ };
+ if (env->RegisterNatives(clazz, kMethods,
+ sizeof(kMethods) / sizeof(kMethods[0])) < 0) {
+ LOGE("JNI_OnLoad: RegisterNatives failed for HardwareBufferJni");
+ return -1;
+ }
+
+ return JNI_VERSION_1_6;
+}
diff --git a/libraries/exoplayer/src/androidTest/java/androidx/media3/exoplayer/audio/DefaultAudioSinkTest.java b/libraries/exoplayer/src/androidTest/java/androidx/media3/exoplayer/audio/DefaultAudioSinkTest.java
index c392e95eb62..006582ca636 100644
--- a/libraries/exoplayer/src/androidTest/java/androidx/media3/exoplayer/audio/DefaultAudioSinkTest.java
+++ b/libraries/exoplayer/src/androidTest/java/androidx/media3/exoplayer/audio/DefaultAudioSinkTest.java
@@ -17,20 +17,24 @@
import static androidx.media3.common.util.Util.sampleCountToDurationUs;
import static androidx.media3.common.util.Util.usToMs;
-import static androidx.test.platform.app.InstrumentationRegistry.getInstrumentation;
import static com.google.common.collect.Iterables.getLast;
import static com.google.common.truth.Truth.assertThat;
import android.content.Context;
+import android.os.Handler;
+import android.os.HandlerThread;
import androidx.media3.common.C;
import androidx.media3.common.Format;
import androidx.media3.common.MimeTypes;
+import androidx.media3.common.util.ThrowingRunnable;
+import androidx.media3.common.util.Util;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.filters.SdkSuppress;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.ArrayList;
+import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
@@ -43,12 +47,16 @@ public class DefaultAudioSinkTest {
@Test
@SdkSuppress(minSdkVersion = 24) // TODO: b/399130330 - Debug why this fails on API 23.
- public void audioTrackExceedsSharedMemory_retriesUntilOngoingReleasesAreDone() throws Exception {
+ public void
+ audioTrackExceedsSharedMemory_playbackThreadStillAlive_retriesUntilOngoingReleasesAreDone()
+ throws Exception {
Context context = ApplicationProvider.getApplicationContext();
+ Handler mainHandler = Util.createHandlerForCurrentOrMainLooper();
// Create audio sinks in parallel until we exceed the device's shared audio memory.
ArrayList audioSinks = new ArrayList<>();
while (true) {
- runOnMainSync(
+ runOnHandlerSync(
+ mainHandler,
() -> {
AudioOutputProvider defaultProvider =
new AudioTrackAudioOutputProvider.Builder(context).build();
@@ -71,7 +79,7 @@ public OutputConfig getOutputConfig(FormatConfig formatConfig)
audioSinks.add(audioSink);
});
try {
- configureAudioSinkAndFeedData(getLast(audioSinks));
+ configureAudioSinkAndFeedDataOnHandler(mainHandler, getLast(audioSinks));
} catch (Exception e) {
// Expected to happen once we reached the shared audio memory limit of the device.
break;
@@ -79,19 +87,21 @@ public OutputConfig getOutputConfig(FormatConfig formatConfig)
}
// Trigger release of one sink and immediately try the failed sink again. This should
// now succeed even if the sink is released asynchronously.
- runOnMainSync(
+ runOnHandlerSync(
+ mainHandler,
() -> {
audioSinks.get(0).flush();
audioSinks.get(0).release();
});
try {
- configureAudioSinkAndFeedData(getLast(audioSinks));
+ configureAudioSinkAndFeedDataOnHandler(mainHandler, getLast(audioSinks));
} catch (Exception e) {
throw new IllegalStateException(e);
}
// Clean-up
- runOnMainSync(
+ runOnHandlerSync(
+ mainHandler,
() -> {
for (int i = 1; i < audioSinks.size(); i++) {
audioSinks.get(i).flush();
@@ -100,6 +110,114 @@ public OutputConfig getOutputConfig(FormatConfig formatConfig)
});
}
+ @Test
+ @SdkSuppress(minSdkVersion = 24)
+ public void
+ audioTrackExceedsSharedMemory_playbackThreadNotAlive_retriesUntilOngoingReleasesAreDone()
+ throws Exception {
+ Context context = ApplicationProvider.getApplicationContext();
+ Handler mainHandler = Util.createHandlerForCurrentOrMainLooper();
+ // Create audio sinks in parallel until we exceed the device's shared audio memory.
+ ArrayList mainThreadSinks = new ArrayList<>();
+ while (true) {
+ AtomicReference sinkRef = new AtomicReference<>();
+ runOnHandlerSync(
+ mainHandler,
+ () -> {
+ AudioOutputProvider defaultProvider =
+ new AudioTrackAudioOutputProvider.Builder(context).build();
+ DefaultAudioSink audioSink =
+ new DefaultAudioSink.Builder(context)
+ .setAudioOutputProvider(
+ new ForwardingAudioOutputProvider(defaultProvider) {
+ @Override
+ public OutputConfig getOutputConfig(FormatConfig formatConfig)
+ throws ConfigurationException {
+ return super.getOutputConfig(formatConfig)
+ .buildUpon()
+ .setBufferSize(2_000_000)
+ .build();
+ }
+ })
+ .build();
+ sinkRef.set(audioSink);
+ mainThreadSinks.add(audioSink);
+ });
+ try {
+ // Configure the audio sinks on the main thread.
+ configureAudioSinkAndFeedDataOnHandler(mainHandler, sinkRef.get());
+ } catch (Exception e) {
+ // Expected to happen once we reached the shared audio memory limit.
+ break;
+ }
+ }
+
+ // Free one audio sink on the main thread so we have room to create one sink on the background
+ // playback thread.
+ runOnHandlerSync(
+ mainHandler,
+ () -> {
+ mainThreadSinks.get(0).flush();
+ mainThreadSinks.get(0).release();
+ });
+ mainThreadSinks.remove(0);
+
+ HandlerThread playbackThread = new HandlerThread("PlaybackThread");
+ playbackThread.start();
+ Handler playbackHandler = new Handler(playbackThread.getLooper());
+ // Create and configure one audio sink on the background playback thread.
+ AtomicReference bgSinkRef = new AtomicReference<>();
+ runOnHandlerSync(
+ playbackHandler,
+ () -> {
+ AudioOutputProvider defaultProvider =
+ new AudioTrackAudioOutputProvider.Builder(context).build();
+ DefaultAudioSink audioSink =
+ new DefaultAudioSink.Builder(context)
+ .setAudioOutputProvider(
+ new ForwardingAudioOutputProvider(defaultProvider) {
+ @Override
+ public OutputConfig getOutputConfig(FormatConfig formatConfig)
+ throws ConfigurationException {
+ return super.getOutputConfig(formatConfig)
+ .buildUpon()
+ .setBufferSize(2_000_000)
+ .build();
+ }
+ })
+ .build();
+ bgSinkRef.set(audioSink);
+ });
+ configureAudioSinkAndFeedDataOnHandler(playbackHandler, bgSinkRef.get());
+
+ // Trigger release of the background sink and quit the thread. This simulates the player being
+ // destroyed.
+ runOnHandlerSync(
+ playbackHandler,
+ () -> {
+ bgSinkRef.get().flush();
+ bgSinkRef.get().release();
+ });
+ playbackThread.quit();
+
+ // Immediately configure an audio sink again on the main thread.
+ try {
+ configureAudioSinkAndFeedDataOnHandler(mainHandler, getLast(mainThreadSinks));
+ } catch (Exception e) {
+ throw new IllegalStateException(e);
+ }
+
+ // Clean-up all main thread sinks safely on the main thread.
+ runOnHandlerSync(
+ mainHandler,
+ () -> {
+ for (DefaultAudioSink sink : mainThreadSinks) {
+ sink.flush();
+ sink.release();
+ }
+ });
+ }
+
@Test
@SdkSuppress(minSdkVersion = 24) // The test depends on AudioTrack#getUnderrunCount() (API 24+).
public void audioTrackUnderruns_callsOnUnderrun() throws Exception {
@@ -152,7 +270,8 @@ public void onSkipSilenceEnabledChanged(boolean skipSilenceEnabled) {}
sampleCountToDurationUs(/* sampleCount= */ 25, /* sampleRate= */ 44_100);
ByteBuffer smallBuffer = ByteBuffer.allocateDirect(50).order(ByteOrder.nativeOrder());
- runOnMainSync(
+ runOnHandlerSync(
+ Util.createHandlerForCurrentOrMainLooper(),
() -> {
try {
sink.configure(new AudioSink.AudioSinkConfig.Builder(format).build());
@@ -182,9 +301,11 @@ public void onSkipSilenceEnabledChanged(boolean skipSilenceEnabled) {}
assertThat(underrunCount.get()).isGreaterThan(0);
}
- private void configureAudioSinkAndFeedData(DefaultAudioSink audioSink) throws Exception {
+ private void configureAudioSinkAndFeedDataOnHandler(Handler handler, DefaultAudioSink audioSink)
+ throws Exception {
ByteBuffer buffer = ByteBuffer.allocateDirect(8000).order(ByteOrder.nativeOrder());
- runOnMainSync(
+ runOnHandlerSync(
+ handler,
() -> {
Format format =
new Format.Builder()
@@ -198,7 +319,8 @@ private void configureAudioSinkAndFeedData(DefaultAudioSink audioSink) throws Ex
});
AtomicBoolean handledBuffer = new AtomicBoolean();
while (!handledBuffer.get()) {
- runOnMainSync(
+ runOnHandlerSync(
+ handler,
() ->
handledBuffer.set(
audioSink.handleBuffer(
@@ -206,23 +328,23 @@ private void configureAudioSinkAndFeedData(DefaultAudioSink audioSink) throws Ex
}
}
- private static void runOnMainSync(ThrowingRunnable runnable) throws Exception {
- AtomicReference exceptionOnMain = new AtomicReference<>();
- getInstrumentation()
- .runOnMainSync(
- () -> {
- try {
- runnable.run();
- } catch (Exception e) {
- exceptionOnMain.set(e);
- }
- });
- if (exceptionOnMain.get() != null) {
- throw exceptionOnMain.get();
+ private static void runOnHandlerSync(Handler handler, ThrowingRunnable> runnable)
+ throws Exception {
+ AtomicReference exceptionOnHandler = new AtomicReference<>();
+ CountDownLatch latch = new CountDownLatch(1);
+ handler.post(
+ () -> {
+ try {
+ runnable.run();
+ } catch (Exception e) {
+ exceptionOnHandler.set(e);
+ } finally {
+ latch.countDown();
+ }
+ });
+ latch.await();
+ if (exceptionOnHandler.get() != null) {
+ throw exceptionOnHandler.get();
}
}
-
- private interface ThrowingRunnable {
- void run() throws Exception;
- }
}
diff --git a/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/ExoPlayerImplInternal.java b/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/ExoPlayerImplInternal.java
index 3f039101ca5..264f9dd6e2e 100644
--- a/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/ExoPlayerImplInternal.java
+++ b/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/ExoPlayerImplInternal.java
@@ -57,6 +57,7 @@
import androidx.media3.common.Player.PlaybackSuppressionReason;
import androidx.media3.common.Player.RepeatMode;
import androidx.media3.common.Timeline;
+import androidx.media3.common.TrackSelectionParameters;
import androidx.media3.common.audio.AudioFocusManager;
import androidx.media3.common.util.Clock;
import androidx.media3.common.util.ConditionVariable;
@@ -691,8 +692,8 @@ public void onContinueLoadingRequested(MediaPeriod source) {
// TrackSelector.InvalidationListener implementation.
@Override
- public void onTrackSelectionsInvalidated() {
- handler.sendEmptyMessage(MSG_TRACK_SELECTION_INVALIDATED);
+ public void onTrackSelectionsInvalidated(@Nullable TrackSelectionParameters parameters) {
+ handler.obtainMessage(MSG_TRACK_SELECTION_INVALIDATED, parameters).sendToTarget();
}
@Override
@@ -807,6 +808,8 @@ public boolean handleMessage(Message msg) {
handleContinueLoadingRequested((MediaPeriod) msg.obj);
break;
case MSG_TRACK_SELECTION_INVALIDATED:
+ @Nullable TrackSelectionParameters parameters = (TrackSelectionParameters) msg.obj;
+ trackSelector.onParametersActivated(parameters);
reselectTracksInternal();
break;
case MSG_PLAYBACK_PARAMETERS_CHANGED_INTERNAL:
@@ -1467,6 +1470,7 @@ private void doSomeWork() throws ExoPlaybackException, IOException {
boolean renderersEnded = true;
boolean renderersAllowPlayback = true;
+ boolean hasActiveVideoOrImageRenderer = false;
if (playingPeriodHolder.prepared) {
rendererPositionElapsedRealtimeUs = msToUs(clock.elapsedRealtime());
playingPeriodHolder.mediaPeriod.discardBuffer(
@@ -1484,6 +1488,13 @@ private void doSomeWork() throws ExoPlaybackException, IOException {
// getting stuck if tracks in the current period have uneven durations and are still being
// read by another renderer. See: https://github.com/google/ExoPlayer/issues/1874.
renderersEnded = renderersEnded && renderer.isEnded();
+ if (seekIsPendingWhileScrubbing
+ && renderer.isRendererEnabled()
+ && (renderer.getTrackType() == C.TRACK_TYPE_VIDEO
+ || renderer.getTrackType() == C.TRACK_TYPE_IMAGE)
+ && !renderer.isEnded()) {
+ hasActiveVideoOrImageRenderer = true;
+ }
boolean allowsPlayback = renderer.allowsPlayback(playingPeriodHolder);
maybeTriggerOnRendererReadyChanged(/* rendererIndex= */ i, allowsPlayback);
renderersAllowPlayback = renderersAllowPlayback && allowsPlayback;
@@ -1491,6 +1502,11 @@ private void doSomeWork() throws ExoPlaybackException, IOException {
maybeThrowRendererStreamError(/* rendererIndex= */ i);
}
}
+ if (seekIsPendingWhileScrubbing
+ && !hasActiveVideoOrImageRenderer
+ && !handler.hasMessages(MSG_SEEK_COMPLETED_IN_SCRUBBING_MODE)) {
+ handler.obtainMessage(MSG_SEEK_COMPLETED_IN_SCRUBBING_MODE).sendToTarget();
+ }
} else {
playingPeriodHolder.mediaPeriod.maybeThrowPrepareError();
}
@@ -4040,13 +4056,10 @@ private static PositionUpdateForPlaylistChange resolvePositionForPlaylistChange(
contentPositionForAdResolutionUs,
enforceAdPlaybackOnTimelineRefresh,
/* transitionsFromPlaceholderPeriod= */ isUsingPlaceholderPeriod);
- boolean earliestAdGroupIsUnchangedOrLater =
+ boolean earliestCuePointIsUnchangedOrLater =
periodIdWithAds.nextAdGroupIndex == C.INDEX_UNSET
|| (oldPeriodId.nextAdGroupIndex != C.INDEX_UNSET
&& periodIdWithAds.nextAdGroupIndex >= oldPeriodId.nextAdGroupIndex);
- boolean isOldAdGroupWithinNewPeriod =
- isOldAdGroupWithinNewPeriod(
- timeline.getPeriodByUid(newPeriodUid, period), oldPeriodId.nextAdGroupIndex);
// Drop update if we keep playing the same content (MediaPeriod.periodUid are identical) and
// the only change is that MediaPeriodId.nextAdGroupIndex increased. This postpones a potential
// discontinuity until we reach the former next ad group position.
@@ -4055,18 +4068,18 @@ private static PositionUpdateForPlaylistChange resolvePositionForPlaylistChange(
sameOldAndNewPeriodUid
&& !oldPeriodId.isAd()
&& !periodIdWithAds.isAd()
- && isOldAdGroupWithinNewPeriod
- && earliestAdGroupIsUnchangedOrLater;
+ && earliestCuePointIsUnchangedOrLater;
// Drop update if the change is from/to server-side inserted ads at the same content position to
// avoid any unintentional renderer reset.
boolean isInStreamAdChange =
isIgnorableServerSideAdInsertionPeriodChange(
+ timeline,
isUsingPlaceholderPeriod,
oldPeriodId,
oldContentPositionUs,
periodIdWithAds,
- timeline.getPeriodByUid(newPeriodUid, period),
- newContentPositionUs);
+ newContentPositionUs,
+ period);
MediaPeriodId newPeriodId =
onlyNextAdGroupIndexIncreased || isInStreamAdChange ? oldPeriodId : periodIdWithAds;
@@ -4140,27 +4153,29 @@ private static PositionUpdateForPlaylistChange resolvePositionForPlaylistChange(
}
private static boolean isIgnorableServerSideAdInsertionPeriodChange(
+ Timeline timeline,
boolean isUsingPlaceholderPeriod,
MediaPeriodId oldPeriodId,
long oldContentPositionUs,
MediaPeriodId newPeriodId,
- Timeline.Period newPeriod,
- long newContentPositionUs) {
+ long newContentPositionUs,
+ Timeline.Period period) {
if (isUsingPlaceholderPeriod
|| oldContentPositionUs != newContentPositionUs
|| !oldPeriodId.periodUid.equals(newPeriodId.periodUid)) {
// The period position changed.
return false;
}
- if (oldPeriodId.isAd() && newPeriod.isServerSideInsertedAdGroup(oldPeriodId.adGroupIndex)) {
+ timeline.getPeriodByUid(newPeriodId.periodUid, period);
+ if (oldPeriodId.isAd() && period.isServerSideInsertedAdGroup(oldPeriodId.adGroupIndex)) {
// Whether the old period was a server side ad that doesn't need skipping to the content.
- return newPeriod.getAdState(oldPeriodId.adGroupIndex, oldPeriodId.adIndexInAdGroup)
+ return period.getAdState(oldPeriodId.adGroupIndex, oldPeriodId.adIndexInAdGroup)
!= AdPlaybackState.AD_STATE_ERROR
- && newPeriod.getAdState(oldPeriodId.adGroupIndex, oldPeriodId.adIndexInAdGroup)
+ && period.getAdState(oldPeriodId.adGroupIndex, oldPeriodId.adIndexInAdGroup)
!= AdPlaybackState.AD_STATE_SKIPPED;
}
// If the new period is a server side inserted ad, we can just continue playing.
- return newPeriodId.isAd() && newPeriod.isServerSideInsertedAdGroup(newPeriodId.adGroupIndex);
+ return newPeriodId.isAd() && period.isServerSideInsertedAdGroup(newPeriodId.adGroupIndex);
}
private static boolean isUsingPlaceholderPeriod(
@@ -4170,22 +4185,6 @@ private static boolean isUsingPlaceholderPeriod(
return timeline.isEmpty() || timeline.getPeriodByUid(periodId.periodUid, period).isPlaceholder;
}
- private static boolean isOldAdGroupWithinNewPeriod(
- Timeline.Period newPeriod, int oldNextAdGroupIndex) {
- if (oldNextAdGroupIndex == C.INDEX_UNSET) {
- return true;
- }
- if (oldNextAdGroupIndex >= newPeriod.adPlaybackState.adGroupCount) {
- return false;
- }
- AdGroup newAdGroupAtOldIndex = newPeriod.adPlaybackState.getAdGroup(oldNextAdGroupIndex);
- if (newAdGroupAtOldIndex.timeUs == C.TIME_END_OF_SOURCE) {
- return true;
- }
- return newAdGroupAtOldIndex.timeUs <= newPeriod.durationUs
- || newPeriod.durationUs == C.TIME_UNSET;
- }
-
/**
* Updates the {@link #isRebuffering} state and the timestamp of the last rebuffering event.
*
diff --git a/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/RendererHolder.java b/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/RendererHolder.java
index bf693ca6e52..181a80bf682 100644
--- a/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/RendererHolder.java
+++ b/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/RendererHolder.java
@@ -76,12 +76,15 @@ public boolean hasSecondary() {
public void startPrewarming() {
checkState(!isPrewarming());
- prewarmingState =
- isRendererEnabled(primaryRenderer)
- ? RENDERER_PREWARMING_STATE_TRANSITIONING_TO_SECONDARY
- : secondaryRenderer != null && isRendererEnabled(secondaryRenderer)
- ? RENDERER_PREWARMING_STATE_TRANSITIONING_TO_PRIMARY
- : RENDERER_PREWARMING_STATE_PREWARMING_PRIMARY;
+ if (isRendererEnabled(primaryRenderer)) {
+ prewarmingState = RENDERER_PREWARMING_STATE_TRANSITIONING_TO_SECONDARY;
+ } else if (secondaryRenderer != null && isRendererEnabled(secondaryRenderer)) {
+ prewarmingState = RENDERER_PREWARMING_STATE_TRANSITIONING_TO_PRIMARY;
+ } else if (prewarmingState == RENDERER_PREWARMING_STATE_NOT_PREWARMING_USING_PRIMARY) {
+ prewarmingState = RENDERER_PREWARMING_STATE_PREWARMING_PRIMARY;
+ } else {
+ prewarmingState = RENDERER_PREWARMING_STATE_PREWARMING_SECONDARY;
+ }
}
public boolean isPrewarming() {
@@ -94,7 +97,8 @@ private boolean isPrimaryRendererPrewarming() {
}
private boolean isSecondaryRendererPrewarming() {
- return prewarmingState == RENDERER_PREWARMING_STATE_TRANSITIONING_TO_SECONDARY;
+ return prewarmingState == RENDERER_PREWARMING_STATE_PREWARMING_SECONDARY
+ || prewarmingState == RENDERER_PREWARMING_STATE_TRANSITIONING_TO_SECONDARY;
}
public int getEnabledRendererCount() {
@@ -165,6 +169,7 @@ public void setCurrentStreamFinal(long streamEndPositionUs) {
boolean isPrimaryRenderer =
secondaryRenderer == null
|| prewarmingState == RENDERER_PREWARMING_STATE_TRANSITIONING_TO_SECONDARY
+ || prewarmingState == RENDERER_PREWARMING_STATE_PREWARMING_SECONDARY
|| prewarmingState == RENDERER_PREWARMING_STATE_NOT_PREWARMING_USING_PRIMARY;
Renderer renderer = isPrimaryRenderer ? primaryRenderer : checkNotNull(secondaryRenderer);
setCurrentStreamFinalInternal(renderer, streamEndPositionUs);
@@ -190,6 +195,7 @@ public void maybeSetOldStreamToFinal(
boolean isPrimaryOldRenderer =
secondaryRenderer == null
|| prewarmingState == RENDERER_PREWARMING_STATE_TRANSITIONING_TO_SECONDARY
+ || prewarmingState == RENDERER_PREWARMING_STATE_PREWARMING_SECONDARY
|| (prewarmingState == RENDERER_PREWARMING_STATE_NOT_PREWARMING_USING_PRIMARY
&& isRendererEnabled(primaryRenderer));
Renderer oldRenderer = isPrimaryOldRenderer ? primaryRenderer : checkNotNull(secondaryRenderer);
@@ -226,7 +232,8 @@ public void setAllNonPrewarmingRendererStreamsFinal(long streamEndPositionUs) {
}
if (secondaryRenderer != null
&& isRendererEnabled(secondaryRenderer)
- && prewarmingState != RENDERER_PREWARMING_STATE_TRANSITIONING_TO_SECONDARY) {
+ && prewarmingState != RENDERER_PREWARMING_STATE_TRANSITIONING_TO_SECONDARY
+ && prewarmingState != RENDERER_PREWARMING_STATE_PREWARMING_SECONDARY) {
setCurrentStreamFinalInternal(secondaryRenderer, streamEndPositionUs);
}
}
@@ -594,8 +601,9 @@ public void disable(DefaultMediaClock mediaClock) throws ExoPlaybackException {
disableRenderer(primaryRenderer, mediaClock);
if (secondaryRenderer != null) {
boolean shouldTransferResources =
- isRendererEnabled(secondaryRenderer)
- && prewarmingState != RENDERER_PREWARMING_STATE_TRANSITIONING_TO_SECONDARY;
+ prewarmingState == RENDERER_PREWARMING_STATE_TRANSITIONING_TO_PRIMARY
+ || prewarmingState == RENDERER_PREWARMING_STATE_NOT_PREWARMING_USING_SECONDARY
+ || prewarmingState == RENDERER_PREWARMING_STATE_PREWARMING_SECONDARY;
disableRenderer(secondaryRenderer, mediaClock);
maybeResetRenderer(/* resetPrimary= */ false);
if (shouldTransferResources) {
@@ -618,6 +626,8 @@ public void maybeHandlePrewarmingTransition() throws ExoPlaybackException {
: RENDERER_PREWARMING_STATE_NOT_PREWARMING_USING_SECONDARY;
} else if (prewarmingState == RENDERER_PREWARMING_STATE_PREWARMING_PRIMARY) {
prewarmingState = RENDERER_PREWARMING_STATE_NOT_PREWARMING_USING_PRIMARY;
+ } else if (prewarmingState == RENDERER_PREWARMING_STATE_PREWARMING_SECONDARY) {
+ prewarmingState = RENDERER_PREWARMING_STATE_NOT_PREWARMING_USING_SECONDARY;
}
}
@@ -637,7 +647,8 @@ public void disablePrewarming(DefaultMediaClock mediaClock) {
prewarmingState == RENDERER_PREWARMING_STATE_TRANSITIONING_TO_PRIMARY
|| prewarmingState == RENDERER_PREWARMING_STATE_PREWARMING_PRIMARY;
boolean isSecondaryActiveRenderer =
- prewarmingState == RENDERER_PREWARMING_STATE_TRANSITIONING_TO_PRIMARY;
+ prewarmingState == RENDERER_PREWARMING_STATE_TRANSITIONING_TO_PRIMARY
+ || prewarmingState == RENDERER_PREWARMING_STATE_PREWARMING_SECONDARY;
try {
disableRenderer(
isPrewarmingPrimary ? primaryRenderer : checkNotNull(secondaryRenderer), mediaClock);
@@ -847,6 +858,7 @@ public void setVideoOutput(@Nullable Object videoOutput) throws ExoPlaybackExcep
return;
}
if (prewarmingState == RENDERER_PREWARMING_STATE_TRANSITIONING_TO_PRIMARY
+ || prewarmingState == RENDERER_PREWARMING_STATE_PREWARMING_SECONDARY
|| prewarmingState == RENDERER_PREWARMING_STATE_NOT_PREWARMING_USING_SECONDARY) {
checkNotNull(secondaryRenderer).handleMessage(Renderer.MSG_SET_VIDEO_OUTPUT, videoOutput);
} else {
@@ -949,6 +961,7 @@ private Renderer getRendererReadingFromPeriod(@Nullable MediaPeriodHolder period
RENDERER_PREWARMING_STATE_NOT_PREWARMING_USING_PRIMARY,
RENDERER_PREWARMING_STATE_NOT_PREWARMING_USING_SECONDARY,
RENDERER_PREWARMING_STATE_PREWARMING_PRIMARY,
+ RENDERER_PREWARMING_STATE_PREWARMING_SECONDARY,
RENDERER_PREWARMING_STATE_TRANSITIONING_TO_SECONDARY,
RENDERER_PREWARMING_STATE_TRANSITIONING_TO_PRIMARY
})
@@ -972,17 +985,23 @@ private Renderer getRendererReadingFromPeriod(@Nullable MediaPeriodHolder period
*/
/* package */ static final int RENDERER_PREWARMING_STATE_PREWARMING_PRIMARY = 2;
+ /**
+ * ExoPlayer is currently pre-warming the secondary renderer that is not being used for the
+ * current media item for a subsequent media item.
+ */
+ /* package */ static final int RENDERER_PREWARMING_STATE_PREWARMING_SECONDARY = 3;
+
/**
* Both a primary and secondary renderer are enabled and ExoPlayer is transitioning to a media
* item using the secondary renderer.
*/
- /* package */ static final int RENDERER_PREWARMING_STATE_TRANSITIONING_TO_SECONDARY = 3;
+ /* package */ static final int RENDERER_PREWARMING_STATE_TRANSITIONING_TO_SECONDARY = 4;
/**
* Both a primary and secondary renderer are enabled and ExoPlayer is transitioning to a media
* item using the primary renderer.
*/
- /* package */ static final int RENDERER_PREWARMING_STATE_TRANSITIONING_TO_PRIMARY = 4;
+ /* package */ static final int RENDERER_PREWARMING_STATE_TRANSITIONING_TO_PRIMARY = 5;
/** Results for calls to {@link #replaceStreamsOrDisableRendererForTransition}. */
@Documented
diff --git a/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/audio/AudioOutputProvider.java b/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/audio/AudioOutputProvider.java
index 85f4620973e..9f4aff290e2 100644
--- a/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/audio/AudioOutputProvider.java
+++ b/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/audio/AudioOutputProvider.java
@@ -688,6 +688,16 @@ public InitializationException(Throwable cause) {
@UnstableApi
default void setClock(Clock clock) {}
+ /**
+ * Returns whether there are any pending asynchronous releases of audio outputs.
+ *
+ *
Implementations that release resources synchronously should return false.
+ */
+ @UnstableApi
+ default boolean hasPendingReleases() {
+ return false;
+ }
+
/** Releases resources held by the provider. */
void release();
}
diff --git a/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/audio/AudioTrackAudioOutput.java b/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/audio/AudioTrackAudioOutput.java
index 3a6d02a3c85..dd7662f827b 100644
--- a/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/audio/AudioTrackAudioOutput.java
+++ b/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/audio/AudioTrackAudioOutput.java
@@ -397,6 +397,13 @@ public boolean isStalled() {
return audioTrackPositionTracker.isStalled(getWrittenFrames());
}
+ /** Returns whether there are any pending asynchronous releases. */
+ /* package */ static boolean hasPendingReleases() {
+ synchronized (releaseExecutorLock) {
+ return pendingReleaseCount > 0;
+ }
+ }
+
private long getWrittenFrames() {
return isOutputPcm ? Util.ceilDivide(writtenPcmBytes, pcmFrameSize) : writtenEncodedFrames;
}
diff --git a/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/audio/AudioTrackAudioOutputProvider.java b/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/audio/AudioTrackAudioOutputProvider.java
index 77d9af183a5..c20065daaad 100644
--- a/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/audio/AudioTrackAudioOutputProvider.java
+++ b/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/audio/AudioTrackAudioOutputProvider.java
@@ -424,6 +424,12 @@ public void setClock(Clock clock) {
this.clock = clock;
}
+ @UnstableApi
+ @Override
+ public boolean hasPendingReleases() {
+ return AudioTrackAudioOutput.hasPendingReleases();
+ }
+
@Override
public void release() {
if (listeners != null) {
diff --git a/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/audio/DefaultAudioSink.java b/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/audio/DefaultAudioSink.java
index 7cf699b3648..e6007c4afd5 100644
--- a/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/audio/DefaultAudioSink.java
+++ b/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/audio/DefaultAudioSink.java
@@ -77,7 +77,6 @@
import java.nio.ByteOrder;
import java.util.ArrayDeque;
import java.util.Objects;
-import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.BiConsumer;
import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
import org.checkerframework.checker.nullness.qual.RequiresNonNull;
@@ -562,8 +561,6 @@ public DefaultAudioSink build() {
private static final String TAG = "DefaultAudioSink";
- private static final AtomicInteger pendingReleaseCount = new AtomicInteger();
-
@Nullable private final Context context;
private final androidx.media3.common.audio.AudioProcessorChain audioProcessorChain;
private final boolean enableFloatOutput;
@@ -601,6 +598,8 @@ public DefaultAudioSink build() {
private long writtenPcmBytes;
private long writtenEncodedFrames;
private int framesPerEncodedSample;
+ private int inputBufferOriginalRemaining;
+ private long currentBufferFramesWritten;
private boolean startMediaTimeUsNeedsSync;
private boolean startMediaTimeUsNeedsInit;
private long startMediaTimeUs;
@@ -1071,6 +1070,8 @@ public boolean handleBuffer(
inputBuffer = buffer;
inputBufferAccessUnitCount = encodedAccessUnitCount;
+ inputBufferOriginalRemaining = buffer.remaining();
+ currentBufferFramesWritten = 0;
}
processBuffers(presentationTimeUs);
@@ -1316,17 +1317,37 @@ private void drainOutputBuffer(long avSyncPresentationTimeUs) throws WriteExcept
}
}
+ int bytesWritten = bytesRemaining - outputBuffer.remaining();
if (configuration.isPcm()) {
- writtenPcmBytes += bytesRemaining - outputBuffer.remaining();
+ writtenPcmBytes += bytesWritten;
}
if (fullyHandled) {
if (!configuration.isPcm()) {
// When playing non-PCM, the inputBuffer is never processed, thus the last inputBuffer
// must be the current input buffer.
checkState(outputBuffer == inputBuffer);
- writtenEncodedFrames += (long) framesPerEncodedSample * inputBufferAccessUnitCount;
+ // Add only the remaining unreconciled frames to ensure exact sample accuracy.
+ writtenEncodedFrames +=
+ ((long) framesPerEncodedSample * inputBufferAccessUnitCount)
+ - currentBufferFramesWritten;
+ currentBufferFramesWritten = 0;
}
outputBuffer = null;
+ } else {
+ if (!configuration.isPcm() && bytesWritten > 0) {
+ checkState(inputBufferOriginalRemaining > 0);
+ int totalBytesWrittenForCurrentBuffer =
+ inputBufferOriginalRemaining - outputBuffer.remaining();
+ long totalFramesWrittenForCurrentBuffer =
+ Util.scaleLargeValue(
+ totalBytesWrittenForCurrentBuffer,
+ (long) framesPerEncodedSample * inputBufferAccessUnitCount,
+ inputBufferOriginalRemaining,
+ RoundingMode.CEILING);
+ long newFramesWritten = totalFramesWrittenForCurrentBuffer - currentBufferFramesWritten;
+ writtenEncodedFrames += newFramesWritten;
+ currentBufferFramesWritten = totalFramesWrittenForCurrentBuffer;
+ }
}
}
@@ -1575,7 +1596,6 @@ public void flush() {
// We need to release the audio output on every flush because of known AudioTrack flush issues
// on some devices. See b/7941810 or b/19193985.
// TODO: b/143500232 - Experiment with not releasing AudioOutput on flush.
- pendingReleaseCount.incrementAndGet();
audioOutput.release();
audioOutput = null;
}
@@ -1638,6 +1658,8 @@ private void resetSinkStateForFlush() {
writtenEncodedFrames = 0;
isWaitingForOffloadEndOfStreamHandled = false;
framesPerEncodedSample = 0;
+ inputBufferOriginalRemaining = 0;
+ currentBufferFramesWritten = 0;
mediaPositionParameters =
new MediaPositionParameters(
playbackParameters, /* mediaTimeUs= */ 0, /* audioOutputPositionUs= */ 0);
@@ -1938,8 +1960,8 @@ private boolean hasAudioOutputPendingData(long writtenFrames) {
return writtenFrames > currentPositionFrames;
}
- private static boolean hasPendingAudioOutputReleases() {
- return pendingReleaseCount.get() > 0;
+ private boolean hasPendingAudioOutputReleases() {
+ return audioOutputProvider.hasPendingReleases();
}
@RequiresApi(34)
@@ -2025,7 +2047,6 @@ public void onUnderrun() {
public void onReleased() {
// Don't check for stale events. It's expected that this event arrives after the class field
// has been updated to null or a new listener.
- pendingReleaseCount.getAndDecrement();
if (listener != null) {
listener.onAudioTrackReleased(
new AudioTrackConfig(
@@ -2131,7 +2152,7 @@ private boolean isPcm() {
}
}
- private static final class PendingExceptionHolder {
+ private final class PendingExceptionHolder {
/**
* The duration for which failed audio output operations may be retried before throwing an
diff --git a/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/audio/ForwardingAudioOutputProvider.java b/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/audio/ForwardingAudioOutputProvider.java
index 0b36623799a..85bf8384da3 100644
--- a/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/audio/ForwardingAudioOutputProvider.java
+++ b/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/audio/ForwardingAudioOutputProvider.java
@@ -63,6 +63,12 @@ public void setClock(Clock clock) {
audioOutputProvider.setClock(clock);
}
+ @UnstableApi
+ @Override
+ public boolean hasPendingReleases() {
+ return audioOutputProvider.hasPendingReleases();
+ }
+
@Override
public void release() {
audioOutputProvider.release();
diff --git a/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/audio/MediaCodecAudioRenderer.java b/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/audio/MediaCodecAudioRenderer.java
index ec4d209aaad..eb7a86bff83 100644
--- a/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/audio/MediaCodecAudioRenderer.java
+++ b/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/audio/MediaCodecAudioRenderer.java
@@ -48,6 +48,7 @@
import androidx.media3.common.util.CodecSpecificDataUtil;
import androidx.media3.common.util.Log;
import androidx.media3.common.util.MediaFormatUtil;
+import androidx.media3.common.util.ThrowingRunnable;
import androidx.media3.common.util.UnstableApi;
import androidx.media3.common.util.Util;
import androidx.media3.decoder.DecoderInputBuffer;
@@ -77,6 +78,7 @@
import java.nio.ByteOrder;
import java.util.List;
import java.util.Objects;
+import java.util.concurrent.atomic.AtomicBoolean;
/**
* Decodes and renders audio using {@link MediaCodec} and an {@link AudioSink}.
@@ -132,6 +134,7 @@ public class MediaCodecAudioRenderer extends MediaCodecRenderer implements Media
private final EventDispatcher eventDispatcher;
private final AudioSink audioSink;
@Nullable private final LoudnessCodecController loudnessCodecController;
+ private final AtomicBoolean processOutputBufferResultHolder;
private int codecMaxInputSize;
private boolean codecNeedsDiscardChannelsWorkaround;
@@ -330,6 +333,7 @@ public MediaCodecAudioRenderer(
this.loudnessCodecController = loudnessCodecController;
rendererPriority = C.PRIORITY_PLAYBACK;
eventDispatcher = new EventDispatcher(eventHandler, eventListener);
+ processOutputBufferResultHolder = new AtomicBoolean();
nextBufferToWritePresentationTimeUs = C.TIME_UNSET;
firstNotReadyTimeMs = C.TIME_UNSET;
hasBeenReady = false;
@@ -927,9 +931,16 @@ protected boolean processOutputBuffer(
return true;
}
- boolean fullyConsumed;
try {
- fullyConsumed = audioSink.handleBuffer(buffer, bufferPresentationTimeUs, sampleCount);
+ ThrowingRunnable handleBufferOperation =
+ () ->
+ processOutputBufferResultHolder.set(
+ audioSink.handleBuffer(buffer, bufferPresentationTimeUs, sampleCount));
+ if (codec != null) {
+ codec.useBuffer(handleBufferOperation);
+ } else {
+ handleBufferOperation.run();
+ }
} catch (InitializationException e) {
throw createRendererException(
e,
@@ -948,8 +959,14 @@ && getConfiguration().offloadModePreferred != AudioSink.OFFLOAD_MODE_DISABLED
&& getConfiguration().offloadModePreferred != AudioSink.OFFLOAD_MODE_DISABLED
? PlaybackException.ERROR_CODE_AUDIO_TRACK_OFFLOAD_WRITE_FAILED
: PlaybackException.ERROR_CODE_AUDIO_TRACK_WRITE_FAILED);
+ } catch (RuntimeException e) {
+ throw e;
+ } catch (Exception e) {
+ throw new IllegalStateException(e);
}
+ boolean fullyConsumed = processOutputBufferResultHolder.get();
+
if (fullyConsumed) {
if (codec != null) {
if (DEBUG_LOG_ENABLED) {
diff --git a/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/audio/PcmAudioUtil.java b/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/audio/PcmAudioUtil.java
index 7e43f29f8ca..6fda8676ff9 100644
--- a/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/audio/PcmAudioUtil.java
+++ b/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/audio/PcmAudioUtil.java
@@ -71,7 +71,7 @@ public static ByteBuffer rampUpVolume(
public static int readAs32BitIntPcm(ByteBuffer buffer, @C.Encoding int pcmEncoding) {
switch (pcmEncoding) {
case C.ENCODING_PCM_8BIT:
- return (buffer.get() & 0xFF) << 24;
+ return ((buffer.get() & 0xFF) - 128) << 24;
case C.ENCODING_PCM_16BIT:
return ((buffer.get() & 0xFF) << 16) | ((buffer.get() & 0xFF) << 24);
case C.ENCODING_PCM_16BIT_BIG_ENDIAN:
@@ -161,7 +161,7 @@ public static void write32BitIntPcm(
buffer.put((byte) (pcm32bit >> 24));
return;
case C.ENCODING_PCM_8BIT:
- buffer.put((byte) (pcm32bit >> 24));
+ buffer.put((byte) ((pcm32bit >> 24) + 128));
return;
case C.ENCODING_PCM_32BIT_BIG_ENDIAN:
buffer.put((byte) (pcm32bit >> 24));
diff --git a/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/audio/SilenceSkippingAudioProcessor.java b/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/audio/SilenceSkippingAudioProcessor.java
index 3a9dc605fe4..ac68d89c655 100644
--- a/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/audio/SilenceSkippingAudioProcessor.java
+++ b/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/audio/SilenceSkippingAudioProcessor.java
@@ -309,7 +309,9 @@ public void onFlush(StreamMetadata streamMetadata) {
bytesPerFrame = inputAudioFormat.channelCount * 2;
// Divide by 2 to allow the buffer to be split into two bytesPerFrame aligned parts.
int maybeSilenceBufferSize =
- alignToBytePerFrameBoundary(durationUsToFrames(minimumSilenceDurationUs) / 2) * 2;
+ alignToBytePerFrameBoundary(
+ durationUsToFrames(minimumSilenceDurationUs) * bytesPerFrame / 2)
+ * 2;
if (maybeSilenceBuffer.length != maybeSilenceBufferSize) {
maybeSilenceBuffer = new byte[maybeSilenceBufferSize];
contiguousOutputBuffer = new byte[maybeSilenceBufferSize];
@@ -647,6 +649,7 @@ private void modifyVolume(byte[] sampleBuffer, int size, @VolumeChangeType int v
return;
}
+ int lastFrameIdx = (size / bytesPerFrame) - 1;
for (int idx = 0; idx < size; idx += 2) {
byte mostSignificantByte = sampleBuffer[idx + 1];
byte leastSignificantByte = sampleBuffer[idx];
@@ -655,10 +658,10 @@ private void modifyVolume(byte[] sampleBuffer, int size, @VolumeChangeType int v
int volumeModificationPercentage;
if (volumeChangeType == FADE_OUT) {
volumeModificationPercentage =
- calculateFadeOutPercentage(/* value= */ idx, /* max= */ size - 1);
+ calculateFadeOutPercentage(/* value= */ idx / bytesPerFrame, /* max= */ lastFrameIdx);
} else if (volumeChangeType == FADE_IN) {
volumeModificationPercentage =
- calculateFadeInPercentage(/* value= */ idx, /* max= */ size - 1);
+ calculateFadeInPercentage(/* value= */ idx / bytesPerFrame, /* max= */ lastFrameIdx);
} else {
volumeModificationPercentage = minVolumeToKeepPercentageWhenMuting;
}
@@ -669,12 +672,18 @@ private void modifyVolume(byte[] sampleBuffer, int size, @VolumeChangeType int v
}
private int calculateFadeOutPercentage(int value, int max) {
+ if (max == 0) {
+ return minVolumeToKeepPercentageWhenMuting;
+ }
return ((minVolumeToKeepPercentageWhenMuting - 100) * ((AVOID_TRUNCATION_FACTOR * value) / max))
/ AVOID_TRUNCATION_FACTOR
+ 100;
}
private int calculateFadeInPercentage(int value, int max) {
+ if (max == 0) {
+ return 100;
+ }
return (minVolumeToKeepPercentageWhenMuting
+ ((100 - minVolumeToKeepPercentageWhenMuting) * (AVOID_TRUNCATION_FACTOR * value) / max)
/ AVOID_TRUNCATION_FACTOR);
diff --git a/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/drm/DefaultDrmSession.java b/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/drm/DefaultDrmSession.java
index ed2c33b5d86..92fc5322104 100644
--- a/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/drm/DefaultDrmSession.java
+++ b/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/drm/DefaultDrmSession.java
@@ -691,9 +691,12 @@ public void handleMessage(Message msg) {
synchronized (keyRequestInfoLock) {
if (currentKeyRequestInfo != null && keyResponse.loadEventInfo != null) {
currentKeyRequestInfo.addLoadInfo(
- keyResponse.loadEventInfo.copyWithTaskIdAndDurationMs(
- requestTask.taskId,
- SystemClock.elapsedRealtime() - requestTask.startTimeMs));
+ keyResponse
+ .loadEventInfo
+ .buildUpon()
+ .setLoadTaskId(requestTask.taskId)
+ .setLoadDurationMs(SystemClock.elapsedRealtime() - requestTask.startTimeMs)
+ .build());
}
}
break;
@@ -730,14 +733,13 @@ private boolean maybeRetryRequest(Message originalMsg, MediaDrmCallbackException
return false;
}
LoadEventInfo loadEventInfo =
- new LoadEventInfo(
- requestTask.taskId,
- exception.dataSpec,
- exception.uriAfterRedirects,
- exception.responseHeaders,
- SystemClock.elapsedRealtime(),
- /* loadDurationMs= */ SystemClock.elapsedRealtime() - requestTask.startTimeMs,
- exception.bytesLoaded);
+ new LoadEventInfo.Builder(
+ requestTask.taskId, exception.dataSpec, SystemClock.elapsedRealtime())
+ .setUri(exception.uriAfterRedirects)
+ .setResponseHeaders(exception.responseHeaders)
+ .setLoadDurationMs(SystemClock.elapsedRealtime() - requestTask.startTimeMs)
+ .setBytesLoaded(exception.bytesLoaded)
+ .build();
MediaLoadData mediaLoadData = new MediaLoadData(C.DATA_TYPE_DRM);
IOException loadErrorCause =
exception.getCause() instanceof IOException
diff --git a/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/drm/DrmUtil.java b/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/drm/DrmUtil.java
index 3b3430b013f..df2b696762d 100644
--- a/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/drm/DrmUtil.java
+++ b/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/drm/DrmUtil.java
@@ -187,14 +187,14 @@ public static MediaDrmCallback.Response executePost(
try {
byte[] response = ByteStreams.toByteArray(inputStream);
LoadEventInfo loadEventInfo =
- new LoadEventInfo(
- -1, // This will be replaced with the actual taskId from the request.
- originalDataSpec,
- statsDataSource.getLastOpenedUri(),
- statsDataSource.getLastResponseHeaders(),
- SystemClock.elapsedRealtime(),
- /* loadDurationMs= */ 0,
- response.length);
+ new LoadEventInfo.Builder(
+ -1, // This will be replaced with the actual taskId from the request.
+ originalDataSpec,
+ SystemClock.elapsedRealtime())
+ .setUri(statsDataSource.getLastOpenedUri())
+ .setResponseHeaders(statsDataSource.getLastResponseHeaders())
+ .setBytesLoaded(response.length)
+ .build();
return new MediaDrmCallback.Response.Builder(response)
.setLoadEventInfo(loadEventInfo)
.build();
diff --git a/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/mediacodec/AsynchronousMediaCodecAdapter.java b/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/mediacodec/AsynchronousMediaCodecAdapter.java
index 7ce18a39582..87ff730bb72 100644
--- a/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/mediacodec/AsynchronousMediaCodecAdapter.java
+++ b/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/mediacodec/AsynchronousMediaCodecAdapter.java
@@ -34,6 +34,7 @@
import androidx.annotation.VisibleForTesting;
import androidx.media3.common.C;
import androidx.media3.common.util.ExperimentalApi;
+import androidx.media3.common.util.ThrowingRunnable;
import androidx.media3.common.util.TraceUtil;
import androidx.media3.decoder.CryptoInfo;
import com.google.common.base.Supplier;
@@ -266,11 +267,11 @@ public ByteBuffer getInputBuffer(int index) {
}
@Override
- public void useInputBuffer(Runnable runnable) {
- asynchronousMediaCodecCallback.useInputBuffer(
+ public void useBuffer(ThrowingRunnable runnable) throws E {
+ asynchronousMediaCodecCallback.useBuffer(
() -> {
bufferEnqueuer.maybeThrowException();
- asynchronousMediaCodecCallback.useInputBuffer(runnable);
+ runnable.run();
});
}
diff --git a/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/mediacodec/AsynchronousMediaCodecCallback.java b/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/mediacodec/AsynchronousMediaCodecCallback.java
index 8055b2ac1d6..ae201d3e3cb 100644
--- a/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/mediacodec/AsynchronousMediaCodecCallback.java
+++ b/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/mediacodec/AsynchronousMediaCodecCallback.java
@@ -26,6 +26,7 @@
import androidx.annotation.GuardedBy;
import androidx.annotation.Nullable;
import androidx.media3.common.util.CircularIntArray;
+import androidx.media3.common.util.ThrowingRunnable;
import androidx.media3.common.util.Util;
import java.util.ArrayDeque;
import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
@@ -128,14 +129,17 @@ public void shutdown() {
}
/**
- * Uses an acquired input buffer (e.g. to write data to it).
+ * Uses an acquired input or output buffer (e.g. to populate, drain, or write data to it).
*
- *
This can be used for error handling, e.g. to guarantee no operation can invalidate the
- * buffer while it's being used.
+ *
This runs the provided {@link ThrowingRunnable} under the internal codec/callback monitor,
+ * ensuring that concurrent asynchronous framework callbacks (e.g., error reporting or pipeline
+ * invalidation) cannot pre-empt or interleave with active {@code ByteBuffer} memory traversals.
*
- * @param runnable The {@link Runnable} using an acquired input buffer.
+ * @param runnable The {@link ThrowingRunnable} utilizing an acquired buffer.
+ * @param The type of exception thrown by {@code runnable}.
+ * @throws E If {@code runnable} throws an exception.
*/
- public void useInputBuffer(Runnable runnable) {
+ public void useBuffer(ThrowingRunnable runnable) throws E {
synchronized (lock) {
// Run the Runnable under the buffer lock to ensure no new error callback can be handled
// while the buffer is used (under the assumption that error callbacks invalidate the buffer).
diff --git a/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/mediacodec/ForwardingMediaCodecAdapter.java b/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/mediacodec/ForwardingMediaCodecAdapter.java
index 483d9a4c1a4..919e79546ee 100644
--- a/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/mediacodec/ForwardingMediaCodecAdapter.java
+++ b/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/mediacodec/ForwardingMediaCodecAdapter.java
@@ -23,6 +23,7 @@
import android.view.Surface;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
+import androidx.media3.common.util.ThrowingRunnable;
import androidx.media3.common.util.UnstableApi;
import androidx.media3.decoder.CryptoInfo;
import java.nio.ByteBuffer;
@@ -65,8 +66,8 @@ public ByteBuffer getInputBuffer(int index) {
}
@Override
- public void useInputBuffer(Runnable runnable) {
- delegate.useInputBuffer(runnable);
+ public void useBuffer(ThrowingRunnable runnable) throws E {
+ delegate.useBuffer(runnable);
}
@Nullable
diff --git a/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/mediacodec/MediaCodecAdapter.java b/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/mediacodec/MediaCodecAdapter.java
index cda3b77483f..7dbf562a983 100644
--- a/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/mediacodec/MediaCodecAdapter.java
+++ b/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/mediacodec/MediaCodecAdapter.java
@@ -27,6 +27,7 @@
import androidx.annotation.RequiresApi;
import androidx.media3.common.C;
import androidx.media3.common.Format;
+import androidx.media3.common.util.ThrowingRunnable;
import androidx.media3.common.util.UnstableApi;
import androidx.media3.decoder.CryptoInfo;
import java.io.IOException;
@@ -208,14 +209,16 @@ default void onOutputBufferAvailable() {}
ByteBuffer getInputBuffer(int index);
/**
- * Uses an acquired input buffer (e.g. to write data to it).
+ * Uses an acquired input or output buffer (e.g. to read from or write data to it).
*
*
This can be used for error handling, e.g. to guarantee no operation can invalidate the
- * buffer while it's being used.
+ * buffer while it's being used by the application or downstream components.
*
- * @param runnable The {@link Runnable} using an acquired input buffer.
+ * @param runnable The {@link ThrowingRunnable} utilizing an acquired buffer.
+ * @param The type of exception thrown by {@code runnable}.
+ * @throws E If {@code runnable} throws an exception.
*/
- default void useInputBuffer(Runnable runnable) {
+ default void useBuffer(ThrowingRunnable runnable) throws E {
runnable.run();
}
diff --git a/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/mediacodec/MediaCodecInfo.java b/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/mediacodec/MediaCodecInfo.java
index ccdc4a965d7..b30898f9c33 100644
--- a/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/mediacodec/MediaCodecInfo.java
+++ b/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/mediacodec/MediaCodecInfo.java
@@ -54,10 +54,15 @@
import androidx.media3.common.util.CodecSpecificDataUtil;
import androidx.media3.common.util.CodecSpecificDataUtil.MediaCodecProfileAndLevel;
import androidx.media3.common.util.Log;
+import androidx.media3.common.util.NullableType;
import androidx.media3.common.util.UnstableApi;
import androidx.media3.common.util.Util;
import androidx.media3.exoplayer.DecoderReuseEvaluation;
import androidx.media3.exoplayer.DecoderReuseEvaluation.DecoderDiscardReasons;
+import com.google.common.base.Strings;
+import com.google.common.base.Supplier;
+import com.google.common.base.Suppliers;
+import com.google.common.primitives.Ints;
import com.google.errorprone.annotations.InlineMe;
import java.util.Objects;
@@ -74,6 +79,10 @@ public final class MediaCodecInfo {
*/
public static final int MAX_SUPPORTED_INSTANCES_UNKNOWN = -1;
+ private static final Supplier<@NullableType Integer> VENDOR_API_LEVEL =
+ Suppliers.<@NullableType Integer>memoize(
+ () -> Ints.tryParse(Strings.nullToEmpty(Util.getSystemProperty("ro.vendor.api_level"))));
+
/**
* The name of the decoder.
*
@@ -1072,13 +1081,12 @@ private static boolean needsProfileExcludedWorkaround(String mimeType, int profi
/** Returns whether the device is known to have issues with the detached surface mode. */
private static boolean needsDetachedSurfaceUnsupportedWorkaround() {
- if (!MediaLibraryInfo.enableWorkarounds()) {
- return false;
- }
- if (SDK_INT > 37) {
- // After API 37, we assume that detached surface mode is supported and we don't apply the
- // workaround. Devices should not declare support for FEATURE_DetachedSurface unless they
- // actually support it.
+ // We deliberately don't check MediaLibraryInfo.enableWorkarounds() here, because this
+ // workaround needs to remain active even during certification testing (b/535214648).
+ Integer vendorApiLevel = VENDOR_API_LEVEL.get();
+ if (vendorApiLevel == null || vendorApiLevel > 202604) {
+ // Devices with vendor API level higher than SDK_INT 37 must only declare support for
+ // FEATURE_DetachedSurface if they actually support it.
return false;
}
return Build.MANUFACTURER.equals("Xiaomi")
diff --git a/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/mediacodec/MediaCodecRenderer.java b/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/mediacodec/MediaCodecRenderer.java
index cd731fd2d0f..b5c2f8ecb3b 100644
--- a/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/mediacodec/MediaCodecRenderer.java
+++ b/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/mediacodec/MediaCodecRenderer.java
@@ -391,6 +391,7 @@ private static String buildCustomDiagnosticInfo(int errorCode) {
private int outputIndex;
@Nullable private ByteBuffer outputBuffer;
private boolean isDecodeOnlyOutputBuffer;
+ private boolean isOutputBufferStale;
private boolean bypassEnabled;
private boolean bypassSampleBufferPending;
private boolean bypassDrainAndReinitialize;
@@ -415,6 +416,7 @@ private static String buildCustomDiagnosticInfo(int errorCode) {
private boolean experimentalEnableProcessedStreamChangedAtStart;
private boolean hasSkippedFlushAndWaitingForQueueInputBuffer;
private long skippedFlushOffsetUs;
+ private long largestStaleModifiedPresentationTimeUs;
private CodecParameters activeCodecParameters;
private CodecParameters lastDispatchedCodecParameters;
private ImmutableSet subscribedCodecParameterKeys;
@@ -479,6 +481,7 @@ public MediaCodecRenderer(
decoderCounters = new DecoderCounters();
hasSkippedFlushAndWaitingForQueueInputBuffer = false;
skippedFlushOffsetUs = 0;
+ largestStaleModifiedPresentationTimeUs = C.TIME_UNSET;
this.subscribedCodecParameterKeys = ImmutableSet.of();
this.activeCodecParameters = CodecParameters.EMPTY;
this.lastDispatchedCodecParameters = CodecParameters.EMPTY;
@@ -1127,6 +1130,7 @@ protected void resetCodecStateForFlush() {
codecReceivedBuffers = false;
codecNeedsAdaptationWorkaroundBuffer = false;
shouldSkipAdaptationWorkaroundOutputBuffer = false;
+ isOutputBufferStale = false;
isDecodeOnlyOutputBuffer = false;
codecDrainState = DRAIN_STATE_NONE;
codecDrainAction = DRAIN_ACTION_NONE;
@@ -1137,6 +1141,7 @@ protected void resetCodecStateForFlush() {
codecReconfigured ? RECONFIGURATION_STATE_WRITE_PENDING : RECONFIGURATION_STATE_NONE;
hasSkippedFlushAndWaitingForQueueInputBuffer = false;
skippedFlushOffsetUs = 0;
+ largestStaleModifiedPresentationTimeUs = C.TIME_UNSET;
}
/**
@@ -1557,7 +1562,7 @@ private boolean feedInputBuffer() throws ExoPlaybackException {
FormatHolder formatHolder = getFormatHolder();
try {
- codec.useInputBuffer(
+ codec.useBuffer(
() -> readDataResultHolder.set(readSource(formatHolder, buffer, /* readFlags= */ 0)));
} catch (InsufficientCapacityException e) {
onCodecError(e);
@@ -1659,6 +1664,18 @@ private boolean feedInputBuffer() throws ExoPlaybackException {
lastStreamInfo.queuedBufferAfterReset = true;
waitingForFirstSampleInFormat = false;
}
+
+ if (hasSkippedFlushAndWaitingForQueueInputBuffer) {
+ largestStaleModifiedPresentationTimeUs =
+ largestQueuedPresentationTimeUs + skippedFlushOffsetUs;
+ if (presentationTimeUs <= largestQueuedPresentationTimeUs) {
+ skippedFlushOffsetUs += largestQueuedPresentationTimeUs - presentationTimeUs + 1;
+ }
+ largestQueuedPresentationTimeUs = presentationTimeUs;
+ largestQueuedPresentationTimeWithinDurationUs = presentationTimeUs;
+ hasSkippedFlushAndWaitingForQueueInputBuffer = false;
+ }
+
largestQueuedPresentationTimeUs = max(largestQueuedPresentationTimeUs, presentationTimeUs);
long streamEndPositionUs = getStreamEndPositionUs();
if (streamEndPositionUs == C.TIME_UNSET
@@ -1675,15 +1692,6 @@ private boolean feedInputBuffer() throws ExoPlaybackException {
handleInputBufferSupplementalData(buffer);
}
- if (hasSkippedFlushAndWaitingForQueueInputBuffer) {
- if (presentationTimeUs <= largestQueuedPresentationTimeUs) {
- skippedFlushOffsetUs += largestQueuedPresentationTimeUs - presentationTimeUs + 1;
- }
- largestQueuedPresentationTimeUs = presentationTimeUs;
- largestQueuedPresentationTimeWithinDurationUs = presentationTimeUs;
- hasSkippedFlushAndWaitingForQueueInputBuffer = false;
- }
-
onQueueInputBuffer(buffer);
int flags = getCodecBufferFlags(buffer);
presentationTimeUs += skippedFlushOffsetUs;
@@ -2264,6 +2272,9 @@ private boolean drainOutputBuffer(long positionUs, long elapsedRealtimeUs)
}
// We've dequeued a buffer.
+ isOutputBufferStale =
+ largestStaleModifiedPresentationTimeUs != C.TIME_UNSET
+ && outputBufferInfo.presentationTimeUs <= largestStaleModifiedPresentationTimeUs;
outputBufferInfo.presentationTimeUs -= skippedFlushOffsetUs;
if (shouldSkipAdaptationWorkaroundOutputBuffer) {
shouldSkipAdaptationWorkaroundOutputBuffer = false;
@@ -2301,7 +2312,8 @@ private boolean drainOutputBuffer(long positionUs, long elapsedRealtimeUs)
&& outputBufferInfo.presentationTimeUs - getOutputStreamOffsetUs()
>= outputStreamInfo.durationUs;
isDecodeOnlyOutputBuffer =
- hasSkippedFlushAndWaitingForQueueInputBuffer
+ isOutputBufferStale
+ || hasSkippedFlushAndWaitingForQueueInputBuffer
|| outputBufferInfo.presentationTimeUs < getLastResetPositionUs()
|| isStrictDurationExceeded;
boolean isLastOutputBuffer =
diff --git a/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/offline/DownloadHelper.java b/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/offline/DownloadHelper.java
index ce63faf3fea..0c9932c24bf 100644
--- a/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/offline/DownloadHelper.java
+++ b/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/offline/DownloadHelper.java
@@ -563,7 +563,7 @@ public DownloadHelper(
this.rendererCapabilities = rendererCapabilities;
this.debugLoggingEnabled = debugLoggingEnabled;
this.scratchSet = new SparseIntArray();
- trackSelector.init(/* listener= */ () -> {}, BandwidthMeter.NO_OP);
+ trackSelector.init(/* listener= */ trackSelector::onParametersActivated, BandwidthMeter.NO_OP);
callbackHandler = Util.createHandlerForCurrentOrMainLooper();
window = new Timeline.Window();
}
diff --git a/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/offline/SegmentDownloader.java b/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/offline/SegmentDownloader.java
index fedfdd80dd8..5a18faa4cd1 100644
--- a/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/offline/SegmentDownloader.java
+++ b/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/offline/SegmentDownloader.java
@@ -394,6 +394,29 @@ public final void remove() {
*/
protected final M getManifest(DataSource dataSource, DataSpec dataSpec, boolean removing)
throws InterruptedException, IOException {
+ return getManifest(dataSource, manifestParser, dataSpec, removing);
+ }
+
+ /**
+ * Loads and parses a manifest with a different parser than the base parser that parsed the
+ * initial manifest.
+ *
+ *
This is useful for cases where the manifest needs to be loaded and parsed as a different
+ * type than the base parser. For example, HLS manifests can be parsed as media playlists that
+ * inherit variable declarations from the multivariant playlist.
+ *
+ * @param dataSource The source to use when loading the manifest.
+ * @param manifestParser The parser to use when parsing the manifest.
+ * @param dataSpec The manifest {@link DataSpec}.
+ * @param removing Whether the manifest is being loaded as part of the download being removed.
+ * @return The loaded manifest.
+ * @throws InterruptedException If the thread on which the method is called is interrupted.
+ * @throws IOException If an error occurs during execution.
+ */
+ protected final M getManifest(
+ DataSource dataSource, Parser manifestParser, DataSpec dataSpec, boolean removing)
+ throws InterruptedException, IOException {
+ checkNotNull(manifestParser);
return execute(
() ->
new RunnableFutureTask() {
diff --git a/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/source/DefaultMediaSourceFactory.java b/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/source/DefaultMediaSourceFactory.java
index 1ee5c795ef2..1701896c277 100644
--- a/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/source/DefaultMediaSourceFactory.java
+++ b/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/source/DefaultMediaSourceFactory.java
@@ -496,23 +496,6 @@ public DefaultMediaSourceFactory setLoadOnlySelectedTracks(boolean loadOnlySelec
return this;
}
- /**
- * Sets whether to enable experimental HAGC (ST 2094-50) metadata playback support. When enabled,
- * the player will automatically merge HAGC metadata tracks with the associated video track and
- * deliver the metadata out-of-band to the decoder on API 37+.
- *
- *
The default value is {@code true}.
- *
- * @param enableHagcPlayback Whether experimental HAGC metadata playback is enabled.
- * @return This factory, for convenience.
- */
- @CanIgnoreReturnValue
- @UnstableApi
- public DefaultMediaSourceFactory setExperimentalEnableHagcPlayback(boolean enableHagcPlayback) {
- delegateFactoryLoader.setExperimentalEnableHagcPlayback(enableHagcPlayback);
- return this;
- }
-
/**
* Sets whether an experimental setting to delegate end position clipping to a wrapped {@link
* MediaPeriod} is enabled.
@@ -723,7 +706,6 @@ private static final class DelegateFactoryLoader {
private SubtitleParser.Factory subtitleParserFactory;
private @C.VideoCodecFlags int codecsToParseWithinGopSampleDependencies;
private boolean loadOnlySelectedTracks;
- private boolean experimentalEnableHagcPlayback;
@Nullable private CmcdConfiguration.Factory cmcdConfigurationFactory;
@Nullable private DrmSessionManagerProvider drmSessionManagerProvider;
@Nullable private LoadErrorHandlingPolicy loadErrorHandlingPolicy;
@@ -736,7 +718,6 @@ public DelegateFactoryLoader(
mediaSourceFactorySuppliers = new HashMap<>();
mediaSourceFactories = new HashMap<>();
parseSubtitlesDuringExtraction = true;
- experimentalEnableHagcPlayback = true;
codecsToParseWithinGopSampleDependencies = C.VIDEO_CODEC_FLAG_H264 | C.VIDEO_CODEC_FLAG_H265;
}
@@ -840,10 +821,6 @@ private void setLoadOnlySelectedTracks(boolean loadOnlySelectedTracks) {
this.loadOnlySelectedTracks = loadOnlySelectedTracks;
}
- private void setExperimentalEnableHagcPlayback(boolean experimentalEnableHagcPlayback) {
- this.experimentalEnableHagcPlayback = experimentalEnableHagcPlayback;
- }
-
private void setHeifExtractorFlags(@HeifExtractor.Flags int flags) {
if (this.extractorsFactory instanceof DefaultExtractorsFactory) {
((DefaultExtractorsFactory) this.extractorsFactory).setHeifExtractorFlags(flags);
@@ -917,8 +894,7 @@ private Supplier loadSupplier(@C.ContentType int contentTyp
mediaSourceFactorySupplier =
() ->
new ProgressiveMediaSource.Factory(dataSourceFactory, extractorsFactory)
- .setLoadOnlySelectedTracks(loadOnlySelectedTracks)
- .setExperimentalEnableHagcPlayback(experimentalEnableHagcPlayback);
+ .setLoadOnlySelectedTracks(loadOnlySelectedTracks);
break;
default:
throw new IllegalArgumentException("Unrecognized contentType: " + contentType);
diff --git a/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/source/LoadEventInfo.java b/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/source/LoadEventInfo.java
index 0cd0f7e12a7..dde135c86b5 100644
--- a/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/source/LoadEventInfo.java
+++ b/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/source/LoadEventInfo.java
@@ -17,9 +17,11 @@
import android.net.Uri;
import android.os.SystemClock;
+import androidx.annotation.Nullable;
import androidx.media3.common.util.UnstableApi;
import androidx.media3.datasource.DataSpec;
-import java.util.Collections;
+import com.google.common.collect.ImmutableMap;
+import com.google.errorprone.annotations.CanIgnoreReturnValue;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicLong;
@@ -28,6 +30,155 @@
@UnstableApi
public final class LoadEventInfo {
+ /** Builder for {@link LoadEventInfo}. */
+ public static final class Builder {
+ private long loadTaskId;
+ private DataSpec dataSpec;
+ private long elapsedRealtimeMs;
+ private Uri uri;
+ @Nullable private String steeredPathwayId;
+ private Map> responseHeaders;
+ private long loadDurationMs;
+ private long bytesLoaded;
+
+ /**
+ * Creates a builder.
+ *
+ * @param loadTaskId See {@link LoadEventInfo#loadTaskId}.
+ * @param dataSpec See {@link LoadEventInfo#dataSpec}.
+ * @param elapsedRealtimeMs See {@link LoadEventInfo#elapsedRealtimeMs}.
+ */
+ public Builder(long loadTaskId, DataSpec dataSpec, long elapsedRealtimeMs) {
+ this.loadTaskId = loadTaskId;
+ this.dataSpec = dataSpec;
+ this.uri = dataSpec.uri;
+ this.elapsedRealtimeMs = elapsedRealtimeMs;
+ this.responseHeaders = ImmutableMap.of();
+ }
+
+ /**
+ * Creates a builder with the values from the given {@link LoadEventInfo} instance.
+ *
+ * @param loadEventInfo The {@link LoadEventInfo} to build upon.
+ */
+ private Builder(LoadEventInfo loadEventInfo) {
+ this.loadTaskId = loadEventInfo.loadTaskId;
+ this.dataSpec = loadEventInfo.dataSpec;
+ this.uri = loadEventInfo.uri;
+ this.responseHeaders = loadEventInfo.responseHeaders;
+ this.elapsedRealtimeMs = loadEventInfo.elapsedRealtimeMs;
+ this.loadDurationMs = loadEventInfo.loadDurationMs;
+ this.bytesLoaded = loadEventInfo.bytesLoaded;
+ }
+
+ /**
+ * Sets the {@link LoadEventInfo#loadTaskId}.
+ *
+ * @param loadTaskId See {@link LoadEventInfo#loadTaskId}.
+ * @return This builder.
+ */
+ @CanIgnoreReturnValue
+ public Builder setLoadTaskId(long loadTaskId) {
+ this.loadTaskId = loadTaskId;
+ return this;
+ }
+
+ /**
+ * Sets the {@link LoadEventInfo#dataSpec}.
+ *
+ *
This also sets the {@link LoadEventInfo#uri} to the {@link DataSpec#uri} of the given
+ * {@link DataSpec}. If you need to set the {@link LoadEventInfo#uri} to a different value, call
+ * {@link #setUri(Uri)} to override it.
+ *
+ * @param dataSpec See {@link LoadEventInfo#dataSpec}.
+ * @return This builder.
+ */
+ @CanIgnoreReturnValue
+ public Builder setDataSpec(DataSpec dataSpec) {
+ this.dataSpec = dataSpec;
+ this.uri = dataSpec.uri;
+ return this;
+ }
+
+ /**
+ * Sets the {@link LoadEventInfo#elapsedRealtimeMs}.
+ *
+ * @param elapsedRealtimeMs See {@link LoadEventInfo#elapsedRealtimeMs}.
+ * @return This builder.
+ */
+ @CanIgnoreReturnValue
+ public Builder setElapsedRealtimeMs(long elapsedRealtimeMs) {
+ this.elapsedRealtimeMs = elapsedRealtimeMs;
+ return this;
+ }
+
+ /**
+ * Sets the {@link LoadEventInfo#uri}.
+ *
+ * @param uri See {@link LoadEventInfo#uri}. The default is {@code dataSpec.uri}, where {@link
+ * DataSpec} is the one passed to the builder constructor or {@link #setDataSpec(DataSpec)}.
+ * @return This builder.
+ */
+ @CanIgnoreReturnValue
+ public Builder setUri(Uri uri) {
+ this.uri = uri;
+ return this;
+ }
+
+ /**
+ * Sets the {@link LoadEventInfo#steeredPathwayId}, the default is {@code null}.
+ *
+ * @param steeredPathwayId See {@link LoadEventInfo#steeredPathwayId}.
+ * @return This builder.
+ */
+ @CanIgnoreReturnValue
+ public Builder setSteeredPathwayId(@Nullable String steeredPathwayId) {
+ this.steeredPathwayId = steeredPathwayId;
+ return this;
+ }
+
+ /**
+ * Sets the {@link LoadEventInfo#responseHeaders}.
+ *
+ * @param responseHeaders See {@link LoadEventInfo#responseHeaders}.
+ * @return This builder.
+ */
+ @CanIgnoreReturnValue
+ public Builder setResponseHeaders(Map> responseHeaders) {
+ this.responseHeaders = responseHeaders;
+ return this;
+ }
+
+ /**
+ * Sets the {@link LoadEventInfo#loadDurationMs}.
+ *
+ * @param loadDurationMs See {@link LoadEventInfo#loadDurationMs}. The default is 0.
+ * @return This builder.
+ */
+ @CanIgnoreReturnValue
+ public Builder setLoadDurationMs(long loadDurationMs) {
+ this.loadDurationMs = loadDurationMs;
+ return this;
+ }
+
+ /**
+ * Sets the {@link LoadEventInfo#bytesLoaded}.
+ *
+ * @param bytesLoaded See {@link LoadEventInfo#bytesLoaded}. The default is 0.
+ * @return This builder.
+ */
+ @CanIgnoreReturnValue
+ public Builder setBytesLoaded(long bytesLoaded) {
+ this.bytesLoaded = bytesLoaded;
+ return this;
+ }
+
+ /** Builds the {@link LoadEventInfo}. */
+ public LoadEventInfo build() {
+ return new LoadEventInfo(this);
+ }
+ }
+
/** Used for the generation of unique ids. */
private static final AtomicLong idSource = new AtomicLong();
@@ -49,6 +200,12 @@ public static long getNewId() {
*/
public final Uri uri;
+ /**
+ * The ID of the steered pathway from which data is being loaded, or {@code null} if not
+ * applicable.
+ */
+ @Nullable public final String steeredPathwayId;
+
/** The response headers associated with the load, or an empty map if unavailable. */
public final Map> responseHeaders;
@@ -62,32 +219,24 @@ public static long getNewId() {
public final long bytesLoaded;
/**
- * Equivalent to {@link #LoadEventInfo(long, DataSpec, Uri, Map, long, long, long)
- * LoadEventInfo(loadTaskId, dataSpec, dataSpec.uri, Collections.emptyMap(), elapsedRealtimeMs, 0,
- * 0)}.
+ * @deprecated Use {@link Builder} instead.
*/
+ @Deprecated
public LoadEventInfo(long loadTaskId, DataSpec dataSpec, long elapsedRealtimeMs) {
this(
loadTaskId,
dataSpec,
dataSpec.uri,
- Collections.emptyMap(),
+ ImmutableMap.of(),
elapsedRealtimeMs,
/* loadDurationMs= */ 0,
/* bytesLoaded= */ 0);
}
/**
- * Creates load event info.
- *
- * @param loadTaskId See {@link #loadTaskId}.
- * @param dataSpec See {@link #dataSpec}.
- * @param uri See {@link #uri}.
- * @param responseHeaders See {@link #responseHeaders}.
- * @param elapsedRealtimeMs See {@link #elapsedRealtimeMs}.
- * @param loadDurationMs See {@link #loadDurationMs}.
- * @param bytesLoaded See {@link #bytesLoaded}.
+ * @deprecated Use {@link Builder} instead.
*/
+ @Deprecated
public LoadEventInfo(
long loadTaskId,
DataSpec dataSpec,
@@ -99,14 +248,39 @@ public LoadEventInfo(
this.loadTaskId = loadTaskId;
this.dataSpec = dataSpec;
this.uri = uri;
+ this.steeredPathwayId = null;
this.responseHeaders = responseHeaders;
this.elapsedRealtimeMs = elapsedRealtimeMs;
this.loadDurationMs = loadDurationMs;
this.bytesLoaded = bytesLoaded;
}
+ /**
+ * Creates a {@link LoadEventInfo} from a {@link Builder}.
+ *
+ * @param builder The builder.
+ */
+ private LoadEventInfo(Builder builder) {
+ this.loadTaskId = builder.loadTaskId;
+ this.dataSpec = builder.dataSpec;
+ this.uri = builder.uri;
+ this.steeredPathwayId = builder.steeredPathwayId;
+ this.responseHeaders = builder.responseHeaders;
+ this.elapsedRealtimeMs = builder.elapsedRealtimeMs;
+ this.loadDurationMs = builder.loadDurationMs;
+ this.bytesLoaded = builder.bytesLoaded;
+ }
+
+ /** Initializes a new {@link Builder} with the values from this instance. */
+ public Builder buildUpon() {
+ return new Builder(this);
+ }
+
+ /**
+ * @deprecated Use {@link #buildUpon()} instead.
+ */
+ @Deprecated
public LoadEventInfo copyWithTaskIdAndDurationMs(long loadTaskId, long loadDurationMs) {
- return new LoadEventInfo(
- loadTaskId, dataSpec, uri, responseHeaders, elapsedRealtimeMs, loadDurationMs, bytesLoaded);
+ return buildUpon().setLoadTaskId(loadTaskId).setLoadDurationMs(loadDurationMs).build();
}
}
diff --git a/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/source/ProgressiveMediaPeriod.java b/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/source/ProgressiveMediaPeriod.java
index 29ef6f6edb6..a30c98dbee0 100644
--- a/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/source/ProgressiveMediaPeriod.java
+++ b/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/source/ProgressiveMediaPeriod.java
@@ -32,6 +32,7 @@
import androidx.media3.common.MimeTypes;
import androidx.media3.common.ParserException;
import androidx.media3.common.TrackGroup;
+import androidx.media3.common.util.CodecSpecificDataUtil;
import androidx.media3.common.util.ConditionVariable;
import androidx.media3.common.util.Log;
import androidx.media3.common.util.NullableType;
@@ -80,7 +81,7 @@
import java.util.List;
import java.util.Map;
import java.util.Objects;
-import java.util.concurrent.atomic.AtomicReference;
+import java.util.concurrent.atomic.AtomicBoolean;
import org.checkerframework.checker.nullness.qual.EnsuresNonNull;
import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
@@ -130,7 +131,6 @@ interface Listener {
@Nullable private final String customCacheKey;
private final long continueLoadingCheckIntervalBytes;
private final boolean loadOnlySelectedTracks;
- private final boolean experimentalEnableHagcPlayback;
private final int singleTrackId;
@Nullable private final Format singleTrackFormat;
private final long singleSampleDurationUs;
@@ -217,7 +217,6 @@ public ProgressiveMediaPeriod(
@Nullable String customCacheKey,
int continueLoadingCheckIntervalBytes,
boolean loadOnlySelectedTracks,
- boolean experimentalEnableHagcPlayback,
int singleTrackId,
@Nullable Format singleTrackFormat,
long singleSampleDurationUs,
@@ -233,7 +232,6 @@ public ProgressiveMediaPeriod(
this.customCacheKey = customCacheKey;
this.continueLoadingCheckIntervalBytes = continueLoadingCheckIntervalBytes;
this.loadOnlySelectedTracks = loadOnlySelectedTracks;
- this.experimentalEnableHagcPlayback = experimentalEnableHagcPlayback;
this.singleTrackId = singleTrackId;
this.singleTrackFormat = singleTrackFormat;
this.endPositionUs = C.TIME_END_OF_SOURCE;
@@ -393,8 +391,7 @@ public long selectTracks(
hasPreroll |= selection.getSelectedFormat().hasPrerollSamples;
SampleStream stream =
new SampleStreamImpl(track, selection.getSelectedFormat().hasPrerollSamples);
- if (experimentalEnableHagcPlayback
- && Build.VERSION.SDK_INT >= 37
+ if (Build.VERSION.SDK_INT >= 37
&& !isT35TrackExplicitlySelected
&& MimeTypes.isVideo(selection.getSelectedFormat().sampleMimeType)) {
// TODO: b/388762778 - The MP4 container has extra information (e.g. cdsc box) specifying
@@ -402,36 +399,19 @@ public long selectTracks(
// the correct mergingSampleStreams when multiple video/HAGC tracks exist.
for (int j = 0; j < tracks.length; j++) {
Format format = tracks.get(j).getFormat(0);
- if (Objects.equals(format.sampleMimeType, MimeTypes.APPLICATION_ITUT_T35)) {
- boolean isHagc = false;
- if (!format.initializationData.isEmpty()) {
- byte[] initData = format.initializationData.get(0);
- if (initData.length >= 5
- && initData[0] == (byte) 0xB5
- && initData[1] == (byte) 0x00
- && initData[2] == (byte) 0x90
- && initData[3] == (byte) 0x00
- && initData[4] == (byte) 0x01) {
- isHagc = true;
- }
- }
- if (isHagc) {
- checkState(!trackEnabledStates[j]);
- enabledTrackCount++;
- trackEnabledStates[j] = true;
- if (loadOnlySelectedTracks) {
- controlledTrackOutputs[j].updateSelectionState(true);
- }
- // HAGC it35 metadata samples are standalone and do not depend on
- // previous samples, hence hasPreroll is not relevant and always false.
- stream =
- new MergingMetadataSampleStream(
- stream,
- new SampleStreamImpl(j, /* hasPreroll= */ false),
- selection.getSelectedFormat());
- mergingSampleStreams.add((MergingMetadataSampleStream) stream);
- break;
- }
+ if (CodecSpecificDataUtil.isHagcTrack(format)) {
+ checkState(!trackEnabledStates[j]);
+ enabledTrackCount++;
+ trackEnabledStates[j] = true;
+ // HAGC it35 metadata samples are standalone and do not depend on
+ // previous samples, hence hasPreroll is not relevant and always false.
+ stream =
+ new MergingMetadataSampleStream(
+ stream,
+ new SampleStreamImpl(j, /* hasPreroll= */ false),
+ selection.getSelectedFormat());
+ mergingSampleStreams.add((MergingMetadataSampleStream) stream);
+ break;
}
}
}
@@ -772,19 +752,17 @@ private boolean suppressRead() {
public void onLoadStarted(
ExtractingLoadable loadable, long elapsedRealtimeMs, long loadDurationMs, int retryCount) {
StatsDataSource dataSource = loadable.dataSource;
- LoadEventInfo loadEventInfo =
- retryCount == 0
- ? new LoadEventInfo(loadable.loadTaskId, loadable.dataSpec, elapsedRealtimeMs)
- : new LoadEventInfo(
- loadable.loadTaskId,
- loadable.dataSpec,
- dataSource.getLastOpenedUri(),
- dataSource.getLastResponseHeaders(),
- elapsedRealtimeMs,
- loadDurationMs,
- dataSource.getBytesRead());
+ LoadEventInfo.Builder loadEventInfo =
+ new LoadEventInfo.Builder(loadable.loadTaskId, loadable.dataSpec, elapsedRealtimeMs);
+ if (retryCount != 0) {
+ loadEventInfo
+ .setUri(dataSource.getLastOpenedUri())
+ .setResponseHeaders(dataSource.getLastResponseHeaders())
+ .setLoadDurationMs(loadDurationMs)
+ .setBytesLoaded(dataSource.getBytesRead());
+ }
mediaSourceEventDispatcher.loadStarted(
- loadEventInfo,
+ loadEventInfo.build(),
C.DATA_TYPE_MEDIA,
C.TRACK_TYPE_UNKNOWN,
/* trackFormat= */ null,
@@ -809,14 +787,12 @@ public void onLoadCompleted(
}
StatsDataSource dataSource = loadable.dataSource;
LoadEventInfo loadEventInfo =
- new LoadEventInfo(
- loadable.loadTaskId,
- loadable.dataSpec,
- dataSource.getLastOpenedUri(),
- dataSource.getLastResponseHeaders(),
- elapsedRealtimeMs,
- loadDurationMs,
- dataSource.getBytesRead());
+ new LoadEventInfo.Builder(loadable.loadTaskId, loadable.dataSpec, elapsedRealtimeMs)
+ .setUri(dataSource.getLastOpenedUri())
+ .setResponseHeaders(dataSource.getLastResponseHeaders())
+ .setLoadDurationMs(loadDurationMs)
+ .setBytesLoaded(dataSource.getBytesRead())
+ .build();
loadErrorHandlingPolicy.onLoadTaskConcluded(loadable.loadTaskId);
mediaSourceEventDispatcher.loadCompleted(
loadEventInfo,
@@ -836,14 +812,12 @@ public void onLoadCanceled(
ExtractingLoadable loadable, long elapsedRealtimeMs, long loadDurationMs, boolean released) {
StatsDataSource dataSource = loadable.dataSource;
LoadEventInfo loadEventInfo =
- new LoadEventInfo(
- loadable.loadTaskId,
- loadable.dataSpec,
- dataSource.getLastOpenedUri(),
- dataSource.getLastResponseHeaders(),
- elapsedRealtimeMs,
- loadDurationMs,
- dataSource.getBytesRead());
+ new LoadEventInfo.Builder(loadable.loadTaskId, loadable.dataSpec, elapsedRealtimeMs)
+ .setUri(dataSource.getLastOpenedUri())
+ .setResponseHeaders(dataSource.getLastResponseHeaders())
+ .setLoadDurationMs(loadDurationMs)
+ .setBytesLoaded(dataSource.getBytesRead())
+ .build();
loadErrorHandlingPolicy.onLoadTaskConcluded(loadable.loadTaskId);
mediaSourceEventDispatcher.loadCanceled(
loadEventInfo,
@@ -873,14 +847,12 @@ public LoadErrorAction onLoadError(
int errorCount) {
StatsDataSource dataSource = loadable.dataSource;
LoadEventInfo loadEventInfo =
- new LoadEventInfo(
- loadable.loadTaskId,
- loadable.dataSpec,
- dataSource.getLastOpenedUri(),
- dataSource.getLastResponseHeaders(),
- elapsedRealtimeMs,
- loadDurationMs,
- dataSource.getBytesRead());
+ new LoadEventInfo.Builder(loadable.loadTaskId, loadable.dataSpec, elapsedRealtimeMs)
+ .setUri(dataSource.getLastOpenedUri())
+ .setResponseHeaders(dataSource.getLastResponseHeaders())
+ .setLoadDurationMs(loadDurationMs)
+ .setBytesLoaded(dataSource.getBytesRead())
+ .build();
MediaLoadData mediaLoadData =
new MediaLoadData(
C.DATA_TYPE_MEDIA,
@@ -1171,13 +1143,28 @@ private boolean seekInsideBufferUs(
continue;
}
- if (sampleQueue.getReadIndex() == 0 && isSameAsLastSeekPosition) {
+ if (isSingleSample) {
+ // Single sample media (like images) only have one sample, so seeking always succeeds
+ // by resetting to the first sample.
+ sampleQueue.seekTo(sampleQueue.getFirstIndex());
+ continue;
+ }
+ long firstTimestampUs = sampleQueue.getFirstTimestampUs();
+ if (firstTimestampUs != Long.MIN_VALUE
+ && positionUs <= firstTimestampUs
+ && sampleQueue.getFirstIndex() == 0
+ && isSameAsLastSeekPosition) {
+ // If the seek target position is at or before the timestamp of the very first queued
+ // sample,
+ // and all samples loaded from the beginning of the stream are still present, reset to the
+ // first sample by index. This avoids falling through to a timestamp-based seek, which would
+ // reject the seek if the target position is strictly smaller than the first sample's
+ // timestamp (e.g., when seeking to time 0 on an audio track with encoder delay).
+ sampleQueue.seekTo(sampleQueue.getFirstIndex());
continue;
}
boolean seekInsideQueue =
- isSingleSample
- ? sampleQueue.seekTo(sampleQueue.getFirstIndex())
- : sampleQueue.seekTo(positionUs, /* allowTimeBeyondBuffer= */ loadingFinished);
+ sampleQueue.seekTo(positionUs, /* allowTimeBeyondBuffer= */ loadingFinished);
// If we have AV tracks then an in-buffer seek is successful if the seek into every AV queue
// is successful. We ignore whether seeks within non-AV queues are successful in this case, as
// they may be sparse or poorly interleaved. If we only have non-AV tracks then a seek is
@@ -1487,26 +1474,13 @@ private static int getTrackTypePriority(@C.TrackType int trackType) {
private static class ControlledTrackOutput extends ForwardingTrackOutput {
private final SampleQueue sampleQueue;
private final DiscardingTrackOutput discardingTrackOutput;
- private final AtomicReference outputMode;
-
- /** The mode of operation for the controlled track output. */
- static enum OutputMode {
- /** Pass samples through to the downstream track output. */
- PASS_THROUGH,
- /**
- * Pass samples through to the downstream track output, but discard after the next sample
- * metadata is received.
- */
- DISCARD_AFTER_NEXT_SAMPLE_METADATA,
- /** Discards all samples. */
- DISCARDING,
- };
+ private final AtomicBoolean isDiscarding;
ControlledTrackOutput(SampleQueue sampleQueue) {
super(sampleQueue);
this.sampleQueue = sampleQueue;
this.discardingTrackOutput = new DiscardingTrackOutput();
- this.outputMode = new AtomicReference<>(OutputMode.PASS_THROUGH);
+ this.isDiscarding = new AtomicBoolean(false);
}
@Override
@@ -1540,10 +1514,6 @@ public void sampleMetadata(
int offset,
@Nullable CryptoData cryptoData) {
getCurrentOutput().sampleMetadata(timeUs, flags, size, offset, cryptoData);
- if (outputMode.get() == OutputMode.DISCARD_AFTER_NEXT_SAMPLE_METADATA) {
- sampleQueue.reset();
- outputMode.set(OutputMode.DISCARDING);
- }
}
/**
@@ -1552,10 +1522,7 @@ public void sampleMetadata(
* @param selected Whether the track is selected.
*/
void updateSelectionState(boolean selected) {
- // Since there could still be some samples within the internal SampleDataQueue, we will be
- // confident about releasing them all after the next sample metadata is received.
- outputMode.set(
- selected ? OutputMode.PASS_THROUGH : OutputMode.DISCARD_AFTER_NEXT_SAMPLE_METADATA);
+ isDiscarding.set(!selected);
// In case the existing samples are taking too much memory, preventing further load, release
// them optimistically.
if (!selected) {
@@ -1565,11 +1532,11 @@ void updateSelectionState(boolean selected) {
/** Returns whether the track is selected. */
boolean isSelected() {
- return outputMode.get() == OutputMode.PASS_THROUGH;
+ return !isDiscarding.get();
}
private TrackOutput getCurrentOutput() {
- return outputMode.get() == OutputMode.DISCARDING ? discardingTrackOutput : sampleQueue;
+ return isDiscarding.get() ? discardingTrackOutput : sampleQueue;
}
}
}
diff --git a/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/source/ProgressiveMediaSource.java b/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/source/ProgressiveMediaSource.java
index 87aaee8e097..3d7c8b1c0f9 100644
--- a/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/source/ProgressiveMediaSource.java
+++ b/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/source/ProgressiveMediaSource.java
@@ -96,7 +96,6 @@ public static final class Factory implements MediaSourceFactory {
private int singleTrackId;
@Nullable private Format singleTrackFormat;
private boolean loadOnlySelectedTracks;
- private boolean experimentalEnableHagcPlayback;
/**
* Creates a new factory for {@link ProgressiveMediaSource}s.
@@ -187,19 +186,6 @@ public Factory(
this.drmSessionManagerProvider = drmSessionManagerProvider;
this.loadErrorHandlingPolicy = loadErrorHandlingPolicy;
this.continueLoadingCheckIntervalBytes = continueLoadingCheckIntervalBytes;
- this.experimentalEnableHagcPlayback = true;
- }
-
- /**
- * Sets whether to enable experimental HAGC (ST 2094-50) metadata playback support.
- *
- * @param enableHagcPlayback Whether experimental HAGC metadata playback is enabled.
- * @return This factory, for convenience.
- */
- @CanIgnoreReturnValue
- public Factory setExperimentalEnableHagcPlayback(boolean enableHagcPlayback) {
- this.experimentalEnableHagcPlayback = enableHagcPlayback;
- return this;
}
@CanIgnoreReturnValue
@@ -317,7 +303,6 @@ public ProgressiveMediaSource createMediaSource(MediaItem mediaItem) {
loadErrorHandlingPolicy,
continueLoadingCheckIntervalBytes,
loadOnlySelectedTracks,
- experimentalEnableHagcPlayback,
singleTrackId,
singleTrackFormat,
downloadExecutorSupplier);
@@ -341,7 +326,6 @@ public ProgressiveMediaSource createMediaSource(MediaItem mediaItem) {
private final LoadErrorHandlingPolicy loadableLoadErrorHandlingPolicy;
private final int continueLoadingCheckIntervalBytes;
private final boolean loadOnlySelectedTracks;
- private final boolean experimentalEnableHagcPlayback;
/**
* The ID passed to {@link Factory#enableLazyLoadingWithSingleTrack(int, Format)}. Only valid if
@@ -377,7 +361,6 @@ private ProgressiveMediaSource(
LoadErrorHandlingPolicy loadableLoadErrorHandlingPolicy,
int continueLoadingCheckIntervalBytes,
boolean loadOnlySelectedTracks,
- boolean experimentalEnableHagcPlayback,
int singleTrackId,
@Nullable Format singleTrackFormat,
@Nullable Supplier downloadExecutorSupplier) {
@@ -388,7 +371,6 @@ private ProgressiveMediaSource(
this.loadableLoadErrorHandlingPolicy = loadableLoadErrorHandlingPolicy;
this.continueLoadingCheckIntervalBytes = continueLoadingCheckIntervalBytes;
this.loadOnlySelectedTracks = loadOnlySelectedTracks;
- this.experimentalEnableHagcPlayback = experimentalEnableHagcPlayback;
this.singleTrackFormat = singleTrackFormat;
this.singleTrackId = singleTrackId;
this.timelineIsPlaceholder = true;
@@ -450,7 +432,6 @@ public MediaPeriod createPeriod(MediaPeriodId id, Allocator allocator, long star
localConfiguration.customCacheKey,
continueLoadingCheckIntervalBytes,
loadOnlySelectedTracks,
- experimentalEnableHagcPlayback,
singleTrackId,
singleTrackFormat,
Util.msToUs(localConfiguration.imageDurationMs),
diff --git a/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/source/SingleSampleMediaPeriod.java b/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/source/SingleSampleMediaPeriod.java
index 11a9575f173..d29645ca74a 100644
--- a/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/source/SingleSampleMediaPeriod.java
+++ b/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/source/SingleSampleMediaPeriod.java
@@ -207,19 +207,17 @@ public long getAdjustedSeekPositionUs(long positionUs, SeekParameters seekParame
public void onLoadStarted(
SourceLoadable loadable, long elapsedRealtimeMs, long loadDurationMs, int retryCount) {
StatsDataSource dataSource = loadable.dataSource;
- LoadEventInfo loadEventInfo =
- retryCount == 0
- ? new LoadEventInfo(loadable.loadTaskId, loadable.dataSpec, elapsedRealtimeMs)
- : new LoadEventInfo(
- loadable.loadTaskId,
- loadable.dataSpec,
- dataSource.getLastOpenedUri(),
- dataSource.getLastResponseHeaders(),
- elapsedRealtimeMs,
- loadDurationMs,
- dataSource.getBytesRead());
+ LoadEventInfo.Builder loadEventInfo =
+ new LoadEventInfo.Builder(loadable.loadTaskId, loadable.dataSpec, elapsedRealtimeMs);
+ if (retryCount != 0) {
+ loadEventInfo
+ .setUri(dataSource.getLastOpenedUri())
+ .setResponseHeaders(dataSource.getLastResponseHeaders())
+ .setLoadDurationMs(loadDurationMs)
+ .setBytesLoaded(dataSource.getBytesRead());
+ }
eventDispatcher.loadStarted(
- loadEventInfo,
+ loadEventInfo.build(),
C.DATA_TYPE_MEDIA,
C.TRACK_TYPE_UNKNOWN,
format,
@@ -238,14 +236,12 @@ public void onLoadCompleted(
loadingFinished = true;
StatsDataSource dataSource = loadable.dataSource;
LoadEventInfo loadEventInfo =
- new LoadEventInfo(
- loadable.loadTaskId,
- loadable.dataSpec,
- dataSource.getLastOpenedUri(),
- dataSource.getLastResponseHeaders(),
- elapsedRealtimeMs,
- loadDurationMs,
- sampleSize);
+ new LoadEventInfo.Builder(loadable.loadTaskId, loadable.dataSpec, elapsedRealtimeMs)
+ .setUri(dataSource.getLastOpenedUri())
+ .setResponseHeaders(dataSource.getLastResponseHeaders())
+ .setLoadDurationMs(loadDurationMs)
+ .setBytesLoaded(sampleSize)
+ .build();
loadErrorHandlingPolicy.onLoadTaskConcluded(loadable.loadTaskId);
eventDispatcher.loadCompleted(
loadEventInfo,
@@ -263,14 +259,12 @@ public void onLoadCanceled(
SourceLoadable loadable, long elapsedRealtimeMs, long loadDurationMs, boolean released) {
StatsDataSource dataSource = loadable.dataSource;
LoadEventInfo loadEventInfo =
- new LoadEventInfo(
- loadable.loadTaskId,
- loadable.dataSpec,
- dataSource.getLastOpenedUri(),
- dataSource.getLastResponseHeaders(),
- elapsedRealtimeMs,
- loadDurationMs,
- dataSource.getBytesRead());
+ new LoadEventInfo.Builder(loadable.loadTaskId, loadable.dataSpec, elapsedRealtimeMs)
+ .setUri(dataSource.getLastOpenedUri())
+ .setResponseHeaders(dataSource.getLastResponseHeaders())
+ .setLoadDurationMs(loadDurationMs)
+ .setBytesLoaded(dataSource.getBytesRead())
+ .build();
loadErrorHandlingPolicy.onLoadTaskConcluded(loadable.loadTaskId);
eventDispatcher.loadCanceled(
loadEventInfo,
@@ -292,14 +286,12 @@ public LoadErrorAction onLoadError(
int errorCount) {
StatsDataSource dataSource = loadable.dataSource;
LoadEventInfo loadEventInfo =
- new LoadEventInfo(
- loadable.loadTaskId,
- loadable.dataSpec,
- dataSource.getLastOpenedUri(),
- dataSource.getLastResponseHeaders(),
- elapsedRealtimeMs,
- loadDurationMs,
- dataSource.getBytesRead());
+ new LoadEventInfo.Builder(loadable.loadTaskId, loadable.dataSpec, elapsedRealtimeMs)
+ .setUri(dataSource.getLastOpenedUri())
+ .setResponseHeaders(dataSource.getLastResponseHeaders())
+ .setLoadDurationMs(loadDurationMs)
+ .setBytesLoaded(dataSource.getBytesRead())
+ .build();
MediaLoadData mediaLoadData =
new MediaLoadData(
C.DATA_TYPE_MEDIA,
diff --git a/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/source/ads/AdTimeline.java b/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/source/ads/AdTimeline.java
deleted file mode 100644
index 5437ac0b909..00000000000
--- a/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/source/ads/AdTimeline.java
+++ /dev/null
@@ -1,121 +0,0 @@
-/*
- * Copyright (C) 2026 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 androidx.media3.exoplayer.source.ads;
-
-import static com.google.common.base.Preconditions.checkState;
-
-import androidx.annotation.VisibleForTesting;
-import androidx.media3.common.AdPlaybackState;
-import androidx.media3.common.C;
-import androidx.media3.common.Timeline;
-import androidx.media3.common.util.UnstableApi;
-import androidx.media3.exoplayer.source.ForwardingTimeline;
-
-/**
- * A custom {@link Timeline} for sources that have {@link AdPlaybackState} split among multiple
- * periods.
- *
- *
For each period a modified {@link AdPlaybackState} is created:
- *
- *
- *
Ad group time is offset relative to period start time
- *
Post-roll ad group is kept only for last period
- *
Ad group count and indices are kept unchanged
- *
- */
-@VisibleForTesting(otherwise = VisibleForTesting.PACKAGE_PRIVATE)
-@UnstableApi
-public final class AdTimeline extends ForwardingTimeline {
-
- private final AdPlaybackState[] adPlaybackStates;
-
- /**
- * Creates a new timeline alongside which ads will be played.
- *
- * @param contentTimeline The timeline of the content alongside which ads will be played.
- * @param adPlaybackState The state of the media's ads. The ad group times in this state must be
- * relative to the start of the window (i.e. timeUs = 0 corresponds to the start of the
- * window).
- */
- public AdTimeline(Timeline contentTimeline, AdPlaybackState adPlaybackState) {
- super(contentTimeline);
- int periodCount = contentTimeline.getPeriodCount();
- checkState(contentTimeline.getWindowCount() == 1);
- this.adPlaybackStates = new AdPlaybackState[periodCount];
-
- Timeline.Period period = new Timeline.Period();
- for (int periodIndex = 0; periodIndex < periodCount; periodIndex++) {
- timeline.getPeriod(periodIndex, period);
- adPlaybackStates[periodIndex] =
- forPeriod(
- adPlaybackState,
- period.positionInWindowUs,
- period.durationUs,
- /* isLastPeriod= */ periodIndex == periodCount - 1);
- }
- }
-
- @Override
- public Period getPeriod(int periodIndex, Period period, boolean setIds) {
- timeline.getPeriod(periodIndex, period, setIds);
- long durationUs =
- period.durationUs == C.TIME_UNSET
- ? adPlaybackStates[periodIndex].contentDurationUs
- : period.durationUs;
- period.set(
- period.id,
- period.uid,
- period.windowIndex,
- durationUs,
- period.getPositionInWindowUs(),
- adPlaybackStates[periodIndex],
- period.isPlaceholder);
- return period;
- }
-
- private AdPlaybackState forPeriod(
- AdPlaybackState adPlaybackState,
- long periodStartOffsetUs,
- long periodDurationUs,
- boolean isLastPeriod) {
- if (periodStartOffsetUs == 0 && isLastPeriod) {
- long contentDurationUs =
- periodDurationUs == C.TIME_UNSET ? adPlaybackState.contentDurationUs : periodDurationUs;
- return adPlaybackState.withContentDurationUs(contentDurationUs);
- }
- long contentDurationUs = periodDurationUs;
- if (contentDurationUs == C.TIME_UNSET) {
- contentDurationUs =
- isLastPeriod && adPlaybackState.contentDurationUs != C.TIME_UNSET
- ? adPlaybackState.contentDurationUs - periodStartOffsetUs
- : C.TIME_UNSET;
- }
- adPlaybackState = adPlaybackState.withContentDurationUs(contentDurationUs);
-
- for (int adGroupIndex = 0; adGroupIndex < adPlaybackState.adGroupCount; adGroupIndex++) {
- long adGroupTimeUs = adPlaybackState.getAdGroup(adGroupIndex).timeUs;
- if (adGroupTimeUs == C.TIME_END_OF_SOURCE) {
- if (!isLastPeriod) {
- adPlaybackState = adPlaybackState.withSkippedAdGroup(adGroupIndex);
- }
- } else {
- adPlaybackState =
- adPlaybackState.withAdGroupTimeUs(adGroupIndex, adGroupTimeUs - periodStartOffsetUs);
- }
- }
- return adPlaybackState;
- }
-}
diff --git a/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/source/ads/AdsLoader.java b/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/source/ads/AdsLoader.java
index 5d98c8223e4..52f5d318918 100644
--- a/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/source/ads/AdsLoader.java
+++ b/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/source/ads/AdsLoader.java
@@ -76,9 +76,6 @@ interface EventListener {
* Called when the ad playback state has been updated. The number of {@link
* AdPlaybackState#adGroupCount ad groups} may not change after the first call.
*
- *
The ad group times in the {@link AdPlaybackState} must be relative to the start of the
- * timeline window (i.e. timeUs = 0 corresponds to the start of the window).
- *
* @param adPlaybackState The new ad playback state.
*/
default void onAdPlaybackState(AdPlaybackState adPlaybackState) {}
diff --git a/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/source/ads/AdsMediaSource.java b/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/source/ads/AdsMediaSource.java
index fb67b28a619..87f3744a2a8 100644
--- a/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/source/ads/AdsMediaSource.java
+++ b/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/source/ads/AdsMediaSource.java
@@ -59,13 +59,12 @@
import java.util.Map;
import java.util.Objects;
import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
+import org.checkerframework.checker.nullness.qual.RequiresNonNull;
/**
* A {@link MediaSource} that inserts ads linearly into a provided content media source.
*
- *
The wrapped content media source can contain multiple {@linkplain Timeline.Period periods}.
- * Note that multi-period ad insertion is not supported by all {@link AdsLoader} implementations
- * (for example, the IMA extension's {@code ImaAdsLoader} only supports single-period Timelines).
+ *
The wrapped content media source must contain a single {@link Timeline.Period}.
*/
@UnstableApi
public final class AdsMediaSource extends CompositeMediaSource {
@@ -154,12 +153,11 @@ public RuntimeException getRuntimeExceptionForUnexpected() {
private final boolean useLazyContentSourcePreparation;
private final boolean useAdMediaSourceClipping;
private final List activeMediaSourceHolders;
- private final Map activeContentClippingMediaPeriods;
+ private final Map activeContentClippingMediaPeriods;
// Accessed on the player thread.
@Nullable private ComponentListener componentListener;
@Nullable private Timeline contentTimeline;
- @Nullable private Timeline activeTimeline;
@Nullable private AdPlaybackState adPlaybackState;
private @NullableType AdMediaSourceHolder[][] adMediaSourceHolders;
@Nullable private Handler playerHandler;
@@ -332,16 +330,14 @@ public MediaPeriod createPeriod(MediaPeriodId id, Allocator allocator, long star
if (id.nextAdGroupIndex == C.INDEX_UNSET) {
return maskingMediaPeriod;
}
- long endPositionUs =
- getContentClippingEndPositionUs(
- checkNotNull(activeTimeline), id.periodUid, id.nextAdGroupIndex);
+ long nextAdGroupTimeUs = adPlaybackState.getAdGroup(id.nextAdGroupIndex).timeUs;
ClippingMediaPeriod clippingMediaPeriod =
new ClippingMediaPeriod(
maskingMediaPeriod,
/* enableInitialDiscontinuity= */ true,
/* startUs= */ 0,
- /* endUs= */ endPositionUs);
- activeContentClippingMediaPeriods.put(clippingMediaPeriod, id);
+ /* endUs= */ nextAdGroupTimeUs);
+ activeContentClippingMediaPeriods.put(clippingMediaPeriod, id.nextAdGroupIndex);
return clippingMediaPeriod;
}
}
@@ -379,7 +375,6 @@ protected void releaseSourceInternal() {
this.playerHandler = null;
componentListener.stop();
contentTimeline = null;
- activeTimeline = null;
adPlaybackState = null;
adMediaSourceHolders = new AdMediaSourceHolder[0][];
mainHandler.post(() -> adsLoader.stop(/* adsMediaSource= */ this, componentListener));
@@ -395,6 +390,7 @@ protected void onChildSourceInfoRefreshed(
.handleSourceInfoRefresh(newTimeline);
maybeUpdateSourceInfo();
} else {
+ checkArgument(newTimeline.getPeriodCount() == 1);
contentTimeline = newTimeline;
mainHandler.post(
() -> {
@@ -449,6 +445,14 @@ private void onAdPlaybackState(AdPlaybackState adPlaybackState) {
}
}
}
+ for (Map.Entry activeClippingPeriod :
+ activeContentClippingMediaPeriods.entrySet()) {
+ int nextAdGroupIndex = activeClippingPeriod.getValue();
+ long nextAdGroupTimeUs = adPlaybackState.getAdGroup(nextAdGroupIndex).timeUs;
+ activeClippingPeriod
+ .getKey()
+ .updateClipping(/* startUs= */ 0, /* endUs= */ nextAdGroupTimeUs);
+ }
}
this.adPlaybackState = adPlaybackState;
maybeUpdateAdMediaSources();
@@ -532,30 +536,19 @@ private void maybeUpdateAdMediaSources() {
private void maybeUpdateSourceInfo() {
@Nullable Timeline contentTimeline = this.contentTimeline;
- @Nullable AdPlaybackState adPlaybackState = this.adPlaybackState;
if (adPlaybackState != null && contentTimeline != null) {
if (adPlaybackState.adGroupCount == 0) {
- activeTimeline = contentTimeline;
refreshSourceInfo(contentTimeline);
} else {
- long[][] durations = getAdDurationsUs(adPlaybackState);
- adPlaybackState = adPlaybackState.withAdDurationsUs(durations);
- this.adPlaybackState = adPlaybackState;
- Timeline activeTimeline = new AdTimeline(contentTimeline, adPlaybackState);
- this.activeTimeline = activeTimeline;
- for (Map.Entry activeClippingPeriod :
- activeContentClippingMediaPeriods.entrySet()) {
- MediaPeriodId id = activeClippingPeriod.getValue();
- long endPositionUs =
- getContentClippingEndPositionUs(activeTimeline, id.periodUid, id.nextAdGroupIndex);
- activeClippingPeriod.getKey().updateClipping(/* startUs= */ 0, endPositionUs);
- }
- refreshSourceInfo(activeTimeline);
+ adPlaybackState = adPlaybackState.withAdDurationsUs(getAdDurationsUs());
+ refreshSourceInfo(new SinglePeriodAdTimeline(contentTimeline, adPlaybackState));
}
}
}
- private long[][] getAdDurationsUs(AdPlaybackState adPlaybackState) {
+ @RequiresNonNull("adPlaybackState")
+ private long[][] getAdDurationsUs() {
+ AdPlaybackState adPlaybackState = checkNotNull(this.adPlaybackState);
boolean hasPostRollPlaceholder = adPlaybackState.endsWithLivePostrollPlaceHolder();
int adGroupCount = adMediaSourceHolders.length + (hasPostRollPlaceholder ? 1 : 0);
long[][] adDurationsUs = new long[adGroupCount][];
@@ -635,10 +628,11 @@ public void onAdLoadError(final AdLoadException error, DataSpec dataSpec) {
}
createEventDispatcher(/* mediaPeriodId= */ null)
.loadError(
- new LoadEventInfo(
- LoadEventInfo.getNewId(),
- dataSpec,
- /* elapsedRealtimeMs= */ SystemClock.elapsedRealtime()),
+ new LoadEventInfo.Builder(
+ LoadEventInfo.getNewId(),
+ dataSpec,
+ /* elapsedRealtimeMs= */ SystemClock.elapsedRealtime())
+ .build(),
C.DATA_TYPE_AD,
error,
/* wasCanceled= */ true);
@@ -667,10 +661,11 @@ public void onPrepareComplete(MediaPeriodId mediaPeriodId) {
public void onPrepareError(MediaPeriodId mediaPeriodId, IOException exception) {
createEventDispatcher(mediaPeriodId)
.loadError(
- new LoadEventInfo(
- LoadEventInfo.getNewId(),
- new DataSpec(checkNotNull(adMediaItem.localConfiguration).uri),
- /* elapsedRealtimeMs= */ SystemClock.elapsedRealtime()),
+ new LoadEventInfo.Builder(
+ LoadEventInfo.getNewId(),
+ new DataSpec(checkNotNull(adMediaItem.localConfiguration).uri),
+ /* elapsedRealtimeMs= */ SystemClock.elapsedRealtime())
+ .build(),
C.DATA_TYPE_AD,
AdLoadException.createForAd(exception),
/* wasCanceled= */ true);
@@ -804,14 +799,4 @@ private MaskingMediaPeriod getActiveMaskingMediaPeriod(int activeMediaPeriodInde
: mediaPeriod);
}
}
-
- private long getContentClippingEndPositionUs(
- Timeline activeTimeline, Object periodUid, int nextAdGroupIndex) {
- int periodIndex = activeTimeline.getIndexOfPeriod(periodUid);
- if (periodIndex == C.INDEX_UNSET) {
- return C.TIME_END_OF_SOURCE;
- }
- activeTimeline.getPeriod(periodIndex, period);
- return period.adPlaybackState.getAdGroup(nextAdGroupIndex).timeUs;
- }
}
diff --git a/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/source/ads/SinglePeriodAdTimeline.java b/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/source/ads/SinglePeriodAdTimeline.java
new file mode 100644
index 00000000000..c0d6b9b4ce2
--- /dev/null
+++ b/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/source/ads/SinglePeriodAdTimeline.java
@@ -0,0 +1,63 @@
+/*
+ * 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 androidx.media3.exoplayer.source.ads;
+
+import static com.google.common.base.Preconditions.checkState;
+
+import androidx.annotation.VisibleForTesting;
+import androidx.media3.common.AdPlaybackState;
+import androidx.media3.common.C;
+import androidx.media3.common.Timeline;
+import androidx.media3.common.util.UnstableApi;
+import androidx.media3.exoplayer.source.ForwardingTimeline;
+
+/** A {@link Timeline} for sources that have ads. */
+@VisibleForTesting(otherwise = VisibleForTesting.PACKAGE_PRIVATE)
+@UnstableApi
+public final class SinglePeriodAdTimeline extends ForwardingTimeline {
+
+ private final AdPlaybackState adPlaybackState;
+
+ /**
+ * Creates a new timeline with a single period containing ads.
+ *
+ * @param contentTimeline The timeline of the content alongside which ads will be played. It must
+ * have one window and one period.
+ * @param adPlaybackState The state of the period's ads.
+ */
+ public SinglePeriodAdTimeline(Timeline contentTimeline, AdPlaybackState adPlaybackState) {
+ super(contentTimeline);
+ checkState(contentTimeline.getPeriodCount() == 1);
+ checkState(contentTimeline.getWindowCount() == 1);
+ this.adPlaybackState = adPlaybackState;
+ }
+
+ @Override
+ public Period getPeriod(int periodIndex, Period period, boolean setIds) {
+ timeline.getPeriod(periodIndex, period, setIds);
+ long durationUs =
+ period.durationUs == C.TIME_UNSET ? adPlaybackState.contentDurationUs : period.durationUs;
+ period.set(
+ period.id,
+ period.uid,
+ period.windowIndex,
+ durationUs,
+ period.getPositionInWindowUs(),
+ adPlaybackState,
+ period.isPlaceholder);
+ return period;
+ }
+}
diff --git a/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/source/chunk/BaseMediaChunk.java b/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/source/chunk/BaseMediaChunk.java
index c7dc60b7910..387d14dd63e 100644
--- a/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/source/chunk/BaseMediaChunk.java
+++ b/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/source/chunk/BaseMediaChunk.java
@@ -77,7 +77,8 @@ public BaseMediaChunk(
trackSelectionData,
startTimeUs,
endTimeUs,
- chunkIndex);
+ chunkIndex,
+ /* steeredPathwayId= */ null);
this.clippedStartTimeUs = clippedStartTimeUs;
this.clippedEndTimeUs = clippedEndTimeUs;
}
diff --git a/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/source/chunk/Chunk.java b/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/source/chunk/Chunk.java
index 9e1de1ed649..df20030dd7c 100644
--- a/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/source/chunk/Chunk.java
+++ b/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/source/chunk/Chunk.java
@@ -75,6 +75,12 @@ public abstract class Chunk implements Loadable {
*/
public final long endTimeUs;
+ /**
+ * The ID of the steered pathway from which data is being loaded, or {@code null} if not
+ * applicable.
+ */
+ @Nullable public final String steeredPathwayId;
+
protected final StatsDataSource dataSource;
/**
@@ -86,6 +92,7 @@ public abstract class Chunk implements Loadable {
* @param trackSelectionData See {@link #trackSelectionData}.
* @param startTimeUs See {@link #startTimeUs}.
* @param endTimeUs See {@link #endTimeUs}.
+ * @param steeredPathwayId See {@link #steeredPathwayId}.
*/
public Chunk(
DataSource dataSource,
@@ -95,7 +102,8 @@ public Chunk(
@C.SelectionReason int trackSelectionReason,
@Nullable Object trackSelectionData,
long startTimeUs,
- long endTimeUs) {
+ long endTimeUs,
+ @Nullable String steeredPathwayId) {
this.dataSource = new StatsDataSource(dataSource);
this.dataSpec = checkNotNull(dataSpec);
this.type = type;
@@ -104,6 +112,7 @@ public Chunk(
this.trackSelectionData = trackSelectionData;
this.startTimeUs = startTimeUs;
this.endTimeUs = endTimeUs;
+ this.steeredPathwayId = steeredPathwayId;
loadTaskId = LoadEventInfo.getNewId();
}
diff --git a/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/source/chunk/ChunkSampleStream.java b/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/source/chunk/ChunkSampleStream.java
index dff4778efd9..5ae5e7f70ad 100644
--- a/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/source/chunk/ChunkSampleStream.java
+++ b/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/source/chunk/ChunkSampleStream.java
@@ -484,19 +484,17 @@ public int skipData(long positionUs) {
@Override
public void onLoadStarted(
Chunk loadable, long elapsedRealtimeMs, long loadDurationMs, int retryCount) {
- LoadEventInfo loadEventInfo =
- retryCount == 0
- ? new LoadEventInfo(loadable.loadTaskId, loadable.dataSpec, elapsedRealtimeMs)
- : new LoadEventInfo(
- loadable.loadTaskId,
- loadable.dataSpec,
- loadable.getUri(),
- loadable.getResponseHeaders(),
- elapsedRealtimeMs,
- loadDurationMs,
- loadable.bytesLoaded());
+ LoadEventInfo.Builder loadEventInfo =
+ new LoadEventInfo.Builder(loadable.loadTaskId, loadable.dataSpec, elapsedRealtimeMs);
+ if (retryCount != 0) {
+ loadEventInfo
+ .setUri(loadable.getUri())
+ .setResponseHeaders(loadable.getResponseHeaders())
+ .setLoadDurationMs(loadDurationMs)
+ .setBytesLoaded(loadable.bytesLoaded());
+ }
mediaSourceEventDispatcher.loadStarted(
- loadEventInfo,
+ loadEventInfo.build(),
loadable.type,
primaryTrackType,
loadable.trackFormat,
@@ -512,14 +510,12 @@ public void onLoadCompleted(Chunk loadable, long elapsedRealtimeMs, long loadDur
loadingChunk = null;
chunkSource.onChunkLoadCompleted(loadable);
LoadEventInfo loadEventInfo =
- new LoadEventInfo(
- loadable.loadTaskId,
- loadable.dataSpec,
- loadable.getUri(),
- loadable.getResponseHeaders(),
- elapsedRealtimeMs,
- loadDurationMs,
- loadable.bytesLoaded());
+ new LoadEventInfo.Builder(loadable.loadTaskId, loadable.dataSpec, elapsedRealtimeMs)
+ .setUri(loadable.getUri())
+ .setResponseHeaders(loadable.getResponseHeaders())
+ .setLoadDurationMs(loadDurationMs)
+ .setBytesLoaded(loadable.bytesLoaded())
+ .build();
loadErrorHandlingPolicy.onLoadTaskConcluded(loadable.loadTaskId);
mediaSourceEventDispatcher.loadCompleted(
loadEventInfo,
@@ -539,14 +535,12 @@ public void onLoadCanceled(
loadingChunk = null;
canceledMediaChunk = null;
LoadEventInfo loadEventInfo =
- new LoadEventInfo(
- loadable.loadTaskId,
- loadable.dataSpec,
- loadable.getUri(),
- loadable.getResponseHeaders(),
- elapsedRealtimeMs,
- loadDurationMs,
- loadable.bytesLoaded());
+ new LoadEventInfo.Builder(loadable.loadTaskId, loadable.dataSpec, elapsedRealtimeMs)
+ .setUri(loadable.getUri())
+ .setResponseHeaders(loadable.getResponseHeaders())
+ .setLoadDurationMs(loadDurationMs)
+ .setBytesLoaded(loadable.bytesLoaded())
+ .build();
loadErrorHandlingPolicy.onLoadTaskConcluded(loadable.loadTaskId);
mediaSourceEventDispatcher.loadCanceled(
loadEventInfo,
@@ -584,14 +578,12 @@ public LoadErrorAction onLoadError(
boolean cancelable =
bytesLoaded == 0 || !isMediaChunk || !haveReadFromMediaChunk(lastChunkIndex);
LoadEventInfo loadEventInfo =
- new LoadEventInfo(
- loadable.loadTaskId,
- loadable.dataSpec,
- loadable.getUri(),
- loadable.getResponseHeaders(),
- elapsedRealtimeMs,
- loadDurationMs,
- bytesLoaded);
+ new LoadEventInfo.Builder(loadable.loadTaskId, loadable.dataSpec, elapsedRealtimeMs)
+ .setUri(loadable.getUri())
+ .setResponseHeaders(loadable.getResponseHeaders())
+ .setLoadDurationMs(loadDurationMs)
+ .setBytesLoaded(bytesLoaded)
+ .build();
MediaLoadData mediaLoadData =
new MediaLoadData(
loadable.type,
diff --git a/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/source/chunk/DataChunk.java b/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/source/chunk/DataChunk.java
index 5e8707ad2c9..38d96e9ced8 100644
--- a/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/source/chunk/DataChunk.java
+++ b/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/source/chunk/DataChunk.java
@@ -65,7 +65,8 @@ public DataChunk(
trackSelectionReason,
trackSelectionData,
C.TIME_UNSET,
- C.TIME_UNSET);
+ C.TIME_UNSET,
+ /* steeredPathwayId= */ null);
this.data = data == null ? Util.EMPTY_BYTE_ARRAY : data;
}
diff --git a/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/source/chunk/InitializationChunk.java b/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/source/chunk/InitializationChunk.java
index 4ad794a88f2..25751dbcdf5 100644
--- a/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/source/chunk/InitializationChunk.java
+++ b/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/source/chunk/InitializationChunk.java
@@ -66,7 +66,8 @@ public InitializationChunk(
trackSelectionReason,
trackSelectionData,
C.TIME_UNSET,
- C.TIME_UNSET);
+ C.TIME_UNSET,
+ /* steeredPathwayId= */ null);
this.chunkExtractor = chunkExtractor;
}
diff --git a/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/source/chunk/MediaChunk.java b/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/source/chunk/MediaChunk.java
index 8817bece221..35ec7024514 100644
--- a/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/source/chunk/MediaChunk.java
+++ b/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/source/chunk/MediaChunk.java
@@ -40,6 +40,7 @@ public abstract class MediaChunk extends Chunk {
* @param startTimeUs The start time of the media contained by the chunk, in microseconds.
* @param endTimeUs The end time of the media contained by the chunk, in microseconds.
* @param chunkIndex The index of the chunk, or {@link C#INDEX_UNSET} if it is not known.
+ * @param steeredPathwayId See {@link #steeredPathwayId}.
*/
public MediaChunk(
DataSource dataSource,
@@ -49,7 +50,8 @@ public MediaChunk(
@Nullable Object trackSelectionData,
long startTimeUs,
long endTimeUs,
- long chunkIndex) {
+ long chunkIndex,
+ @Nullable String steeredPathwayId) {
super(
dataSource,
dataSpec,
@@ -58,7 +60,8 @@ public MediaChunk(
trackSelectionReason,
trackSelectionData,
startTimeUs,
- endTimeUs);
+ endTimeUs,
+ steeredPathwayId);
checkNotNull(trackFormat);
this.chunkIndex = chunkIndex;
}
diff --git a/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/source/preload/DefaultPreloadManager.java b/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/source/preload/DefaultPreloadManager.java
index c363aebcae5..91634a58db6 100644
--- a/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/source/preload/DefaultPreloadManager.java
+++ b/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/source/preload/DefaultPreloadManager.java
@@ -648,7 +648,7 @@ private DefaultPreloadManager(Builder builder) {
preloadLooperProvider = builder.preloadLooperProvider;
trackSelector = builder.trackSelectorFactory.createTrackSelector(builder.context);
BandwidthMeter bandwidthMeter = builder.bandwidthMeterSupplier.get();
- trackSelector.init(() -> {}, bandwidthMeter);
+ trackSelector.init(trackSelector::onParametersActivated, bandwidthMeter);
Looper preloadLooper = preloadLooperProvider.obtainLooper();
preloadMediaSourceFactory =
new PreloadMediaSource.Factory(
diff --git a/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/trackselection/DefaultTrackSelector.java b/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/trackselection/DefaultTrackSelector.java
index b3824c00da1..6b63549119b 100644
--- a/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/trackselection/DefaultTrackSelector.java
+++ b/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/trackselection/DefaultTrackSelector.java
@@ -38,7 +38,6 @@
import android.util.SparseArray;
import android.util.SparseBooleanArray;
import android.view.accessibility.CaptioningManager;
-import androidx.annotation.GuardedBy;
import androidx.annotation.IntDef;
import androidx.annotation.Nullable;
import androidx.media3.common.AudioAttributes;
@@ -67,6 +66,7 @@
import androidx.media3.exoplayer.mediacodec.MediaCodecUtil;
import androidx.media3.exoplayer.source.MediaSource.MediaPeriodId;
import androidx.media3.exoplayer.source.TrackGroupArray;
+import androidx.media3.exoplayer.upstream.BandwidthMeter;
import androidx.media3.exoplayer.util.SpatializerWrapper;
import com.google.common.base.Predicate;
import com.google.common.collect.ComparisonChain;
@@ -2455,16 +2455,13 @@ public static SelectionOverride fromBundle(Bundle bundle) {
? (second == Format.NO_VALUE ? 0 : -1)
: (second == Format.NO_VALUE ? 1 : (first - second)));
- private final Object lock;
@Nullable public final Context context;
private final ExoTrackSelection.Factory trackSelectionFactory;
- @GuardedBy("lock")
- private Parameters parameters;
+ private Parameters applicationParameters;
+ private Parameters playerParameters;
- @GuardedBy("lock")
- @Nullable
- private Thread playbackThread;
+ @Nullable private volatile Thread playbackThread;
@Nullable private SpatializerWrapper spatializer;
private AudioAttributes audioAttributes;
@@ -2531,28 +2528,46 @@ private DefaultTrackSelector(
TrackSelectionParameters parameters,
ExoTrackSelection.Factory trackSelectionFactory,
@Nullable Context context) {
- this.lock = new Object();
this.context = context != null ? context.getApplicationContext() : null;
this.trackSelectionFactory = trackSelectionFactory;
+ Parameters initialParameters;
if (parameters instanceof Parameters) {
- this.parameters = (Parameters) parameters;
+ initialParameters = (Parameters) parameters;
} else {
- this.parameters = Parameters.DEFAULT.buildUpon().set(parameters).build();
+ initialParameters = Parameters.DEFAULT.buildUpon().set(parameters).build();
}
+ this.applicationParameters = initialParameters;
+ this.playerParameters = initialParameters;
this.audioAttributes = AudioAttributes.DEFAULT;
- if (this.parameters.constrainAudioChannelCountToDeviceCapabilities && context == null) {
+ if (initialParameters.constrainAudioChannelCountToDeviceCapabilities && context == null) {
Log.w(TAG, AUDIO_CHANNEL_COUNT_CONSTRAINTS_WARN_MESSAGE);
}
}
+ @Override
+ public void init(InvalidationListener listener, BandwidthMeter bandwidthMeter) {
+ super.init(listener, bandwidthMeter);
+ this.playerParameters = getParameters();
+ }
+
+ @Override
+ public void onParametersActivated(@Nullable TrackSelectionParameters parameters) {
+ if (parameters == null) {
+ return;
+ }
+ if (parameters instanceof Parameters) {
+ this.playerParameters = (Parameters) parameters;
+ } else {
+ this.playerParameters = new Parameters.Builder(this.playerParameters).set(parameters).build();
+ }
+ }
+
@Override
public void release() {
- synchronized (lock) {
- if (playbackThread != null) {
- checkState(
- playbackThread == Thread.currentThread(),
- "DefaultTrackSelector is accessed on the wrong thread.");
- }
+ if (playbackThread != null) {
+ checkState(
+ Thread.currentThread().equals(playbackThread),
+ "DefaultTrackSelector is accessed on the wrong thread.");
}
if (SDK_INT >= 32 && spatializer != null) {
spatializer.release();
@@ -2563,9 +2578,7 @@ public void release() {
@Override
public Parameters getParameters() {
- synchronized (lock) {
- return parameters;
- }
+ return applicationParameters;
}
@Override
@@ -2622,17 +2635,14 @@ public Parameters.Builder buildUponParameters() {
*/
private void setParametersInternal(Parameters parameters) {
checkNotNull(parameters);
- boolean parametersChanged;
- synchronized (lock) {
- parametersChanged = !this.parameters.equals(parameters);
- this.parameters = parameters;
- }
+ boolean parametersChanged = !this.applicationParameters.equals(parameters);
+ this.applicationParameters = parameters;
if (parametersChanged) {
if (parameters.constrainAudioChannelCountToDeviceCapabilities && context == null) {
Log.w(TAG, AUDIO_CHANNEL_COUNT_CONSTRAINTS_WARN_MESSAGE);
}
- invalidate();
+ invalidate(parameters);
}
}
@@ -2660,15 +2670,11 @@ public void onRendererCapabilitiesChanged(Renderer renderer) {
MediaPeriodId mediaPeriodId,
Timeline timeline)
throws ExoPlaybackException {
- Parameters parameters;
- synchronized (lock) {
- playbackThread = Thread.currentThread();
- parameters = this.parameters;
- }
+ playbackThread = Thread.currentThread();
if (deviceIsTV == null && context != null) {
deviceIsTV = Util.isTv(context);
}
- if (parameters.constrainAudioChannelCountToDeviceCapabilities
+ if (playerParameters.constrainAudioChannelCountToDeviceCapabilities
&& SDK_INT >= 32
&& spatializer == null) {
spatializer =
@@ -2681,9 +2687,9 @@ public void onRendererCapabilitiesChanged(Renderer renderer) {
new ExoTrackSelection.Definition[rendererCount];
// Apply overrides once so that selectAllTracks is aware of the overridden tracks.
- applyTrackSelectionOverrides(mappedTrackInfo, parameters, definitions);
- applyLegacyRendererOverrides(mappedTrackInfo, parameters, definitions);
- applyRendererDisableOverrides(mappedTrackInfo, parameters, definitions);
+ applyTrackSelectionOverrides(mappedTrackInfo, playerParameters, definitions);
+ applyLegacyRendererOverrides(mappedTrackInfo, playerParameters, definitions);
+ applyRendererDisableOverrides(mappedTrackInfo, playerParameters, definitions);
// Select remaining tracks based on parameters.
selectAllTracks(
@@ -2691,13 +2697,13 @@ public void onRendererCapabilitiesChanged(Renderer renderer) {
mappedTrackInfo,
rendererFormatSupports,
rendererMixedMimeTypeAdaptationSupport,
- parameters);
+ playerParameters);
// Re-apply overrides to enforce them in case selectAllTracks changed them or added additional
// tracks that shouldn't be enabled according to the overrides.
- applyTrackSelectionOverrides(mappedTrackInfo, parameters, definitions);
- applyLegacyRendererOverrides(mappedTrackInfo, parameters, definitions);
- applyRendererDisableOverrides(mappedTrackInfo, parameters, definitions);
+ applyTrackSelectionOverrides(mappedTrackInfo, playerParameters, definitions);
+ applyLegacyRendererOverrides(mappedTrackInfo, playerParameters, definitions);
+ applyRendererDisableOverrides(mappedTrackInfo, playerParameters, definitions);
@NullableType
ExoTrackSelection[] rendererTrackSelections =
@@ -2711,7 +2717,8 @@ public void onRendererCapabilitiesChanged(Renderer renderer) {
for (int i = 0; i < rendererCount; i++) {
@C.TrackType int rendererType = mappedTrackInfo.getRendererType(i);
boolean forceRendererDisabled =
- parameters.getRendererDisabled(i) || parameters.disabledTrackTypes.contains(rendererType);
+ playerParameters.getRendererDisabled(i)
+ || playerParameters.disabledTrackTypes.contains(rendererType);
boolean rendererEnabled =
!forceRendererDisabled
&& (mappedTrackInfo.getRendererType(i) == C.TRACK_TYPE_NONE
@@ -2720,15 +2727,15 @@ public void onRendererCapabilitiesChanged(Renderer renderer) {
}
// Configure audio and video renderers to use tunneling if appropriate.
- if (parameters.tunnelingEnabled) {
+ if (playerParameters.tunnelingEnabled) {
maybeConfigureRenderersForTunneling(
mappedTrackInfo, rendererFormatSupports, rendererConfigurations, rendererTrackSelections);
}
// Configure audio renderer to use offload if appropriate.
- if (parameters.audioOffloadPreferences.audioOffloadMode != AUDIO_OFFLOAD_MODE_DISABLED) {
+ if (playerParameters.audioOffloadPreferences.audioOffloadMode != AUDIO_OFFLOAD_MODE_DISABLED) {
maybeConfigureRendererForOffload(
- parameters,
+ playerParameters,
mappedTrackInfo,
rendererFormatSupports,
rendererConfigurations,
@@ -3258,25 +3265,18 @@ private > Pair sel
}
private void maybeInvalidateForAudioChannelCountConstraints() {
- boolean shouldInvalidate;
- synchronized (lock) {
- shouldInvalidate =
- parameters.constrainAudioChannelCountToDeviceCapabilities
- && SDK_INT >= 32
- && spatializer != null
- && spatializer.isSpatializationSupported();
- }
+ boolean shouldInvalidate =
+ playerParameters.constrainAudioChannelCountToDeviceCapabilities
+ && SDK_INT >= 32
+ && spatializer != null
+ && spatializer.isSpatializationSupported();
if (shouldInvalidate) {
- invalidate();
+ invalidate(/* parameters= */ null);
}
}
private void maybeInvalidateForRendererCapabilitiesChange(Renderer renderer) {
- boolean shouldInvalidate;
- synchronized (lock) {
- shouldInvalidate = parameters.allowInvalidateSelectionsOnRendererCapabilitiesChange;
- }
- if (shouldInvalidate) {
+ if (playerParameters.allowInvalidateSelectionsOnRendererCapabilitiesChange) {
invalidateForRendererCapabilitiesChange(renderer);
}
}
diff --git a/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/trackselection/TrackSelector.java b/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/trackselection/TrackSelector.java
index 671fa256dab..db42a091c61 100644
--- a/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/trackselection/TrackSelector.java
+++ b/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/trackselection/TrackSelector.java
@@ -61,20 +61,28 @@
* the media during the 30 second gap. The player indicates to the track selector when a
* selection it has previously made becomes active by calling {@link
* #onSelectionActivated(Object)}.
- *