Repository navigation
fix(player): apply playback speed when audio is passthrough or tunneled #1119
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
4513137
fix(player): apply playback speed when audio is bitstreamed or tunneled
Quick104 31d9382
fix(player): retry the speed remount, keep route settles, and check d…
Quick104 dfc5ae4
fix(tv): keep an in-flight subtitle mount across the speed remount
Quick104 1233fe5
fix(player): hold the speed remount off during seek recovery
Quick104 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
...red/src/androidMain/kotlin/org/siloserver/silo/common/player/DecodedAudioRemountEffect.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| package org.siloserver.silo.common.player | ||
|
|
||
| import androidx.annotation.OptIn | ||
| import androidx.compose.runtime.Composable | ||
| import androidx.compose.runtime.LaunchedEffect | ||
| import androidx.compose.runtime.collectAsState | ||
| import androidx.compose.runtime.getValue | ||
| import androidx.compose.runtime.rememberUpdatedState | ||
| import androidx.media3.common.util.UnstableApi | ||
| import kotlinx.coroutines.delay | ||
| import kotlinx.coroutines.flow.StateFlow | ||
| import org.siloserver.silo.common.player.backend.VideoPlaybackBackend | ||
|
|
||
| private const val REMOUNT_RETRY_MS = 1_000L | ||
|
|
||
| /** | ||
| * Mounts the stream again while it keeps bitstreaming audio that the chosen | ||
| * speed needs decoded (see [PlaybackSpeedAudioPolicy.needsRemount]). | ||
| * | ||
| * [presetsApplied]: presets asking for decoded audio are in force, so the sink | ||
| * already refuses the bitstream and only the mounted stream still carries it. | ||
| * [remount] returns whether it started a remount. The player refuses one while | ||
| * a load, a recovery, or another mount is under way, so this tries again until | ||
| * the stream decodes or the speed is back at 1x. Re-checks whenever a new | ||
| * AudioTrack starts. | ||
| */ | ||
| @OptIn(UnstableApi::class) | ||
| @Composable | ||
| fun DecodedAudioRemountEffect( | ||
| backend: VideoPlaybackBackend?, | ||
| presetsApplied: Boolean, | ||
| audioOutput: StateFlow<AudioOutput?>, | ||
| remount: () -> Boolean, | ||
| ) { | ||
| val output by audioOutput.collectAsState() | ||
| val currentRemount by rememberUpdatedState(remount) | ||
| LaunchedEffect(backend, presetsApplied, output) { | ||
| if (backend == null || !presetsApplied) return@LaunchedEffect | ||
| while (backend.needsRemountForDecodedAudio()) { | ||
| if (currentRemount()) return@LaunchedEffect | ||
| delay(REMOUNT_RETRY_MS) | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
...ared/src/androidMain/kotlin/org/siloserver/silo/common/player/PlaybackSpeedAudioPolicy.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| package org.siloserver.silo.common.player | ||
|
|
||
| import androidx.annotation.OptIn | ||
| import androidx.media3.common.Format | ||
| import androidx.media3.common.MimeTypes | ||
| import androidx.media3.common.util.UnstableApi | ||
| import kotlin.math.abs | ||
|
|
||
| /** | ||
| * What the audio path needs for a playback speed other than 1x to take effect. | ||
| * | ||
| * Media3 changes speed only on decoded, untunneled PCM. Its DefaultAudioSink | ||
| * holds a bitstream (HDMI passthrough, offload) and tunneled audio at 1x, and | ||
| * because the audio renderer is the playback clock the video falls back to 1x | ||
| * with it. A TV that sends Dolby to a receiver, or one that tunnels, therefore | ||
| * ignored every speed change (silo-android#1089). | ||
| * | ||
| * So while the viewer picks another speed, the sink refuses a bitstream it can | ||
| * decode locally and the TV presets turn tunneling off; at 1x both return. | ||
| * Audio no local decoder can handle stays a bitstream at 1x, as before. | ||
| */ | ||
| object PlaybackSpeedAudioPolicy { | ||
| /** Whether playing at [speed] needs decoded, untunneled audio. */ | ||
| fun needsDecodedAudio(speed: Double): Boolean = | ||
| speed.isFinite() && abs(speed - 1.0) > SPEED_TOLERANCE | ||
|
|
||
| /** Whether the sink should refuse [format] as a bitstream so the renderer decodes it. */ | ||
| fun shouldDecode( | ||
| format: Format, | ||
| decodedAudioNeeded: Boolean, | ||
| canDecodeLocally: (Format) -> Boolean, | ||
| ): Boolean = decodedAudioNeeded && isBitstream(format) && canDecodeLocally(format) | ||
|
|
||
| /** | ||
| * Whether the mounted stream must mount again before decoded audio takes | ||
| * over. Turning tunneling off makes Media3 reset the audio renderer, which | ||
| * then asks the sink again; an untunneled bitstream gets no such reset, so | ||
| * its renderer keeps passing [inputFormat] through until the next mount. | ||
| */ | ||
| fun needsRemount( | ||
| output: AudioOutput?, | ||
| inputFormat: Format?, | ||
| canDecodeLocally: (Format) -> Boolean, | ||
| ): Boolean { | ||
| if (output == null || output.path == AudioOutputPath.Pcm || output.tunneled) return false | ||
| val format = inputFormat ?: return false | ||
| return isBitstream(format) && canDecodeLocally(format) | ||
| } | ||
|
|
||
| @OptIn(UnstableApi::class) | ||
| private fun isBitstream(format: Format): Boolean { | ||
| val mime = format.sampleMimeType ?: return false | ||
| return MimeTypes.isAudio(mime) && mime != MimeTypes.AUDIO_RAW | ||
| } | ||
|
|
||
| private const val SPEED_TOLERANCE = 0.001 | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
...d-shared/src/androidMain/kotlin/org/siloserver/silo/common/player/TrackSelectionSettle.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| package org.siloserver.silo.common.player | ||
|
|
||
| import android.os.SystemClock | ||
|
|
||
| /** | ||
| * Paces the player screens' track-selection presets effect. | ||
| * | ||
| * A reselection seeks the current media period, and while an audio route | ||
| * change rebuilds the sink there is none, so a change to the route or another | ||
| * selection input waits [settleMs] for the reports to stop. A playback speed | ||
| * change moves no route and applies at once, but it still waits out a settle | ||
| * that another change started: the effect restarts on every key change, and | ||
| * the restart must not cut that wait short. | ||
| */ | ||
| class TrackSelectionSettle( | ||
| private val settleMs: Long, | ||
| private val nowMs: () -> Long = SystemClock::elapsedRealtime, | ||
| ) { | ||
| private var settledAtMs = 0L | ||
| private var lastRouteInputs: Any? = null | ||
|
|
||
| /** | ||
| * How long to wait before applying presets. [routeInputs] holds every key | ||
| * of the effect except the speed requirement. Until presets have been | ||
| * [applied] once, nothing waits, because startup track selection must not | ||
| * be deferred. | ||
| */ | ||
| fun waitMs(routeInputs: Any, applied: Boolean): Long { | ||
| val now = nowMs() | ||
| if (applied && routeInputs != lastRouteInputs) settledAtMs = now + settleMs | ||
| lastRouteInputs = routeInputs | ||
| return (settledAtMs - now).coerceAtLeast(0L) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.