From db8bfa51944606d66237f006697578f032f7d65d Mon Sep 17 00:00:00 2001 From: WizDom13 Date: Sat, 28 Nov 2026 01:29:04 +0200 Subject: [PATCH] Remember channel audio mute beside fullscreen --- app/src/main/assets/changelog.html | 5 + .../player/ChannelPlaybackMuteController.kt | 71 ++++++ .../java/org/schabi/newpipe/player/Player.kt | 15 ++ .../newpipe/player/PlayerAudioController.kt | 46 +++- .../player/PlayerMedia3ListenerController.kt | 3 + .../player/PlayerMetadataController.kt | 1 + .../helper/ChannelPlaybackMutePreferences.kt | 31 +++ .../newpipe/player/ui/ChannelMuteControls.kt | 75 ++++++ .../newpipe/player/ui/MainPlayerUi.java | 1 + .../newpipe/player/ui/PopupPlayerUi.java | 9 +- .../newpipe/player/ui/PopupVideoSizing.kt | 6 +- .../newpipe/player/ui/VideoPlayerUi.java | 4 + app/src/main/res/layout/player.xml | 67 ++++++ app/src/main/res/values/dimens.xml | 1 + .../values/strings_channel_playback_mute.xml | 5 + .../ChannelPlaybackMuteControllerTest.kt | 150 ++++++++++++ .../PlayerChannelMuteIntegrationTest.kt | 225 ++++++++++++++++++ .../player/ui/ChannelMuteControlsTest.kt | 212 +++++++++++++++++ .../newpipe/player/ui/PopupVideoSizingTest.kt | 8 + .../sync/ChannelMuteSyncBoundaryTest.kt | 45 ++++ docs/changelogs.md | 5 + docs/channel-playback-mute.md | 23 ++ 22 files changed, 996 insertions(+), 12 deletions(-) create mode 100644 app/src/main/java/org/schabi/newpipe/player/ChannelPlaybackMuteController.kt create mode 100644 app/src/main/java/org/schabi/newpipe/player/helper/ChannelPlaybackMutePreferences.kt create mode 100644 app/src/main/java/org/schabi/newpipe/player/ui/ChannelMuteControls.kt create mode 100644 app/src/main/res/values/strings_channel_playback_mute.xml create mode 100644 app/src/test/java/org/schabi/newpipe/player/ChannelPlaybackMuteControllerTest.kt create mode 100644 app/src/test/java/org/schabi/newpipe/player/PlayerChannelMuteIntegrationTest.kt create mode 100644 app/src/test/java/org/schabi/newpipe/player/ui/ChannelMuteControlsTest.kt create mode 100644 app/src/test/java/org/schabi/newpipe/sync/ChannelMuteSyncBoundaryTest.kt create mode 100644 docs/channel-playback-mute.md diff --git a/app/src/main/assets/changelog.html b/app/src/main/assets/changelog.html index 64e5c41b3af..641cd659206 100644 --- a/app/src/main/assets/changelog.html +++ b/app/src/main/assets/changelog.html @@ -162,6 +162,11 @@

Live chat controls

  • Use compact Material 3 recording and menu actions with space between controls and the screen edges. Show Retry only after a connection failure, and keep Saved chat and danmaku available from the menu.
  • Keep the newest messages visible above the bottom navigation while the video header is expanded.
  • +

    Channel playback mute

    +
    diff --git a/app/src/main/java/org/schabi/newpipe/player/ChannelPlaybackMuteController.kt b/app/src/main/java/org/schabi/newpipe/player/ChannelPlaybackMuteController.kt new file mode 100644 index 00000000000..7878e142615 --- /dev/null +++ b/app/src/main/java/org/schabi/newpipe/player/ChannelPlaybackMuteController.kt @@ -0,0 +1,71 @@ +/* + * SPDX-FileCopyrightText: 2026 WizeStream contributors + * SPDX-License-Identifier: GPL-3.0-or-later + */ +package org.schabi.newpipe.player + +import android.content.Context +import org.schabi.newpipe.extractor.stream.StreamInfo +import org.schabi.newpipe.player.helper.ChannelPlaybackMutePreferences +import org.schabi.newpipe.player.playqueue.PlayQueueItem + +/** Resolves channel identity before applying sound, while preserving an explicit in-session choice. */ +internal class ChannelPlaybackMuteController( + context: Context, + private val onChanged: () -> Unit +) { + private val preferences = ChannelPlaybackMutePreferences(context) + private var stream: Pair? = null + private var channelKey: String? = null + private var explicitChoice: Boolean? = null + + var isMuted: Boolean = false + private set + + val hasChannelIdentity: Boolean get() = channelKey != null + + fun begin(item: PlayQueueItem?) { + if (item == null) return + select(item.serviceId, item.url, item.uploaderUrl.takeUnless { item.isLocalMedia }) + } + + fun select(serviceId: Int, url: String, uploaderUrl: String?) { + val identity = serviceId to url + if (stream != identity) { + stream = identity + explicitChoice = null + channelKey = preferences.key(serviceId, uploaderUrl) + isMuted = channelKey?.let(preferences::isMuted) ?: false + onChanged() + } else { + resolveChannel(serviceId, uploaderUrl) + } + } + + fun onMetadata(info: StreamInfo) { + // Extraction for the previous video may finish after the next one is selected. + if (stream != (info.serviceId to info.url)) return + resolveChannel(info.serviceId, info.uploaderUrl) + } + + fun toggle() { + isMuted = !isMuted + explicitChoice = isMuted + channelKey?.let { preferences.save(it, isMuted) } + onChanged() + } + + private fun resolveChannel(serviceId: Int, uploaderUrl: String?) { + val key = preferences.key(serviceId, uploaderUrl) ?: return + if (key == channelKey) return + channelKey = key + val explicit = explicitChoice + if (explicit != null) { + // A click made before the uploader was known belongs to this video, not stale metadata. + preferences.save(key, explicit) + } else { + isMuted = preferences.isMuted(key) + } + onChanged() + } +} diff --git a/app/src/main/java/org/schabi/newpipe/player/Player.kt b/app/src/main/java/org/schabi/newpipe/player/Player.kt index aa0fa8c665d..907e47954e0 100644 --- a/app/src/main/java/org/schabi/newpipe/player/Player.kt +++ b/app/src/main/java/org/schabi/newpipe/player/Player.kt @@ -10,6 +10,7 @@ import android.content.Intent import android.content.SharedPreferences import android.graphics.Bitmap import android.view.LayoutInflater +import androidx.media3.common.MediaItem import androidx.media3.common.PlaybackException import androidx.media3.common.PlaybackParameters import androidx.media3.common.Player as Media3Player @@ -347,11 +348,15 @@ class Player( fun setPlayQueueForLifecycle(queue: PlayQueue) { activePlayQueue = queue + // The previous engine has been released, and the new queue has not been prepared yet. + audioController.beginItem(queue.item) queueRecovery.bind(queue) } fun setExoPlayerForLifecycle(exoPlayer: ExoPlayer) { media3Player = exoPlayer + // A new engine starts at full volume; restore the selected channel before preparing media. + applyPlayerVolume() } fun clearExoPlayerForLifecycle() { @@ -485,6 +490,11 @@ class Player( val isMuted: Boolean get() = audioController.isMuted + val hasChannelMuteIdentity: Boolean + get() = audioController.hasChannelMuteIdentity + + fun onChannelPlaybackMetadata(info: StreamInfo) = audioController.onMetadata(info) + val equalizerState: EqualizerState get() = audioController.equalizerState @@ -578,6 +588,10 @@ class Player( override fun onMetadata(metadata: androidx.media3.common.Metadata) = historyController.onRadioMetadata(metadata) + override fun onMediaItemTransition(mediaItem: MediaItem?, reason: Int) { + audioController.onMediaItemTransition(mediaItem) + } + override fun onEvents(player: Media3Player, events: Media3Player.Events) { media3ListenerController.onEvents(player) historyController.onPlaybackEvents() @@ -880,6 +894,7 @@ class Player( fun setCurrentItemForPlaybackSynchronization(item: PlayQueueItem) { activeItem = item + audioController.beginItem(item) } fun clearCurrentPlaybackForBlock() { diff --git a/app/src/main/java/org/schabi/newpipe/player/PlayerAudioController.kt b/app/src/main/java/org/schabi/newpipe/player/PlayerAudioController.kt index 9e0a5b0ca63..896905e594e 100644 --- a/app/src/main/java/org/schabi/newpipe/player/PlayerAudioController.kt +++ b/app/src/main/java/org/schabi/newpipe/player/PlayerAudioController.kt @@ -5,9 +5,16 @@ package org.schabi.newpipe.player +import androidx.media3.common.MediaItem +import org.schabi.newpipe.extractor.stream.StreamInfo import org.schabi.newpipe.player.boost.VolumeBoostController import org.schabi.newpipe.player.equalizer.EqualizerController import org.schabi.newpipe.player.equalizer.EqualizerState +import org.schabi.newpipe.player.mediaitem.LocalMediaItemTag +import org.schabi.newpipe.player.mediaitem.MediaItemTag +import org.schabi.newpipe.player.mediaitem.PlaceholderTag +import org.schabi.newpipe.player.mediasource.LoadedMediaSource +import org.schabi.newpipe.player.playqueue.PlayQueueItem /** Owns mute, real-time audio effects, audio focus, and effective-volume coordination. */ internal class PlayerAudioController(private val player: Player) { @@ -19,8 +26,27 @@ internal class PlayerAudioController(private val player: Player) { player::updateAudioTunneling ) - var isMuted = false - private set + private val channelMute = ChannelPlaybackMuteController(player.context, ::applyMute) + + val isMuted: Boolean get() = channelMute.isMuted + val hasChannelMuteIdentity: Boolean get() = channelMute.hasChannelIdentity + + fun beginItem(item: PlayQueueItem?) = channelMute.begin(item) + + fun onMetadata(info: StreamInfo) = channelMute.onMetadata(info) + + fun onMediaItemTransition(item: MediaItem?) { + val engine = player.getExoPlayer() ?: return + if (item != engine.currentMediaItem) return + MediaItemTag.from(item).ifPresent { tag -> + if (tag is PlaceholderTag) return@ifPresent + // Extractors may canonicalize the URL. Keep clicks made while the original item loaded. + val source = tag.getMaybeExtras(Any::class.java).orElse(null) as? LoadedMediaSource + val queueItem = source?.stream + val uploaderUrl = if (tag is LocalMediaItemTag) null else tag.uploaderUrl + channelMute.select(tag.serviceId, queueItem?.url ?: tag.streamUrl, uploaderUrl) + } + } val equalizerState: EqualizerState get() = equalizer.state @@ -68,16 +94,20 @@ internal class PlayerAudioController(private val player: Player) { } fun toggleMute() { - val audioReactor = player.audioReactor - if (player.exoPlayerIsNull() || audioReactor == null) { + if (player.exoPlayerIsNull() || player.audioReactor == null) { return } - isMuted = !isMuted + // Media3 may have switched items before the batched metadata/UI callback is delivered. + onMediaItemTransition(player.exoPlayer.currentMediaItem) + channelMute.toggle() + } + + private fun applyMute() { player.applyPlayerVolume() if (isMuted) { - audioReactor.abandonAudioFocus() - } else { - audioReactor.requestAudioFocus() + player.audioReactor?.abandonAudioFocus() + } else if (player.playWhenReady) { + player.audioReactor?.requestAudioFocus() } player.UIs().call { ui -> ui.onMuteUnmuteChanged(isMuted) } player.notifyPlaybackUpdateToListeners() diff --git a/app/src/main/java/org/schabi/newpipe/player/PlayerMedia3ListenerController.kt b/app/src/main/java/org/schabi/newpipe/player/PlayerMedia3ListenerController.kt index 05fd72d8793..872094555c1 100644 --- a/app/src/main/java/org/schabi/newpipe/player/PlayerMedia3ListenerController.kt +++ b/app/src/main/java/org/schabi/newpipe/player/PlayerMedia3ListenerController.kt @@ -32,6 +32,9 @@ internal class PlayerMedia3ListenerController( } fun onEvents(media3Player: Media3Player) { + if (player.getExoPlayer() === media3Player) { + audioController.onMediaItemTransition(media3Player.currentMediaItem) + } metadataController.onEvents(media3Player) playbackParametersController.updateLiveCatchup() // A seamless item transition can reuse the decoder without another size or first-frame diff --git a/app/src/main/java/org/schabi/newpipe/player/PlayerMetadataController.kt b/app/src/main/java/org/schabi/newpipe/player/PlayerMetadataController.kt index 2e335bf3bb5..5b0b436f798 100644 --- a/app/src/main/java/org/schabi/newpipe/player/PlayerMetadataController.kt +++ b/app/src/main/java/org/schabi/newpipe/player/PlayerMetadataController.kt @@ -59,6 +59,7 @@ internal class PlayerMetadataController( } tag.maybeStreamInfo.ifPresent { info -> + player.onChannelPlaybackMetadata(info) if (Player.DEBUG) { android.util.Log.d( Player.TAG, diff --git a/app/src/main/java/org/schabi/newpipe/player/helper/ChannelPlaybackMutePreferences.kt b/app/src/main/java/org/schabi/newpipe/player/helper/ChannelPlaybackMutePreferences.kt new file mode 100644 index 00000000000..0bf5ce69357 --- /dev/null +++ b/app/src/main/java/org/schabi/newpipe/player/helper/ChannelPlaybackMutePreferences.kt @@ -0,0 +1,31 @@ +/* + * SPDX-FileCopyrightText: 2026 WizeStream contributors + * SPDX-License-Identifier: GPL-3.0-or-later + */ +package org.schabi.newpipe.player.helper + +import android.content.Context +import androidx.preference.PreferenceManager + +/** Explicit channel mute choices share the existing app-wide service/uploader identity. */ +internal class ChannelPlaybackMutePreferences(context: Context) { + private val preferences = PreferenceManager.getDefaultSharedPreferences(context) + + fun key(serviceId: Int, uploaderUrl: String?): String? = if (serviceId < 0) { + null + } else { + ChannelPlaybackProfileManager.profileKey(serviceId, uploaderUrl)?.plus(".muted") + } + + fun isMuted(key: String): Boolean = try { + preferences.getBoolean(key, false) + } catch (_: ClassCastException) { + false + } + + fun save(key: String, muted: Boolean) { + preferences.edit().apply { + if (muted) putBoolean(key, true) else remove(key) + }.apply() + } +} diff --git a/app/src/main/java/org/schabi/newpipe/player/ui/ChannelMuteControls.kt b/app/src/main/java/org/schabi/newpipe/player/ui/ChannelMuteControls.kt new file mode 100644 index 00000000000..90288518297 --- /dev/null +++ b/app/src/main/java/org/schabi/newpipe/player/ui/ChannelMuteControls.kt @@ -0,0 +1,75 @@ +/* + * SPDX-FileCopyrightText: 2026 WizeStream contributors + * SPDX-License-Identifier: GPL-3.0-or-later + */ +package org.schabi.newpipe.player.ui + +import android.view.View +import android.view.ViewGroup +import android.widget.FrameLayout +import androidx.appcompat.content.res.AppCompatResources +import org.schabi.newpipe.R +import org.schabi.newpipe.databinding.PlayerBinding + +/** Sound and fullscreen remain beside one another without opening the optional tools row. */ +object ChannelMuteControls { + @JvmStatic + fun bind(binding: PlayerBinding, mute: View.OnClickListener?, fullscreen: View.OnClickListener?) { + binding.channelMuteButton.setOnClickListener(mute) + binding.popupMuteButton.setOnClickListener(mute) + binding.popupFullscreenButton.setOnClickListener(fullscreen) + if (mute == null) restorePopupTools(binding) + } + + @JvmStatic + fun show(binding: PlayerBinding, popup: Boolean) { + binding.channelMuteButton.visibility = if (popup) View.GONE else View.VISIBLE + binding.popupPlaybackActions.visibility = if (popup) View.VISIBLE else View.GONE + binding.popupToolsScroller.visibility = if (popup) View.VISIBLE else View.GONE + if (popup) { + binding.fullScreenButton.visibility = View.GONE + if (binding.topControls.parent !== binding.popupToolsScroller) { + binding.popupToolsScroller.tag = PopupLayoutState( + binding.topControls.layoutParams, + binding.playbackSeekBar.nextFocusUpId + ) + (binding.topControls.parent as ViewGroup).removeView(binding.topControls) + binding.popupToolsScroller.addView( + binding.topControls, + FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT) + ) + binding.playbackSeekBar.nextFocusUpId = R.id.popupFullscreenButton + } + } else { + restorePopupTools(binding) + } + } + + @JvmStatic + fun update(binding: PlayerBinding, muted: Boolean, hasChannelIdentity: Boolean) { + val context = binding.root.context + val label = when { + hasChannelIdentity && muted -> R.string.channel_playback_unmute + hasChannelIdentity -> R.string.channel_playback_mute + muted -> R.string.unmute + else -> R.string.mute + } + listOf(binding.channelMuteButton, binding.popupMuteButton).forEach { + it.setImageDrawable(AppCompatResources.getDrawable(context, if (muted) R.drawable.ic_volume_off else R.drawable.ic_volume_up)) + it.contentDescription = context.getString(label) + it.isActivated = muted + } + binding.switchMute.contentDescription = context.getString(if (muted) R.string.unmute else R.string.mute) + binding.switchMute.isActivated = muted + } + + private fun restorePopupTools(binding: PlayerBinding) { + val state = binding.popupToolsScroller.tag as? PopupLayoutState ?: return + binding.popupToolsScroller.removeView(binding.topControls) + binding.playbackWindowRoot.addView(binding.topControls, 0, state.params) + binding.playbackSeekBar.nextFocusUpId = state.seekUp + binding.popupToolsScroller.tag = null + } + + private data class PopupLayoutState(val params: ViewGroup.LayoutParams, val seekUp: Int) +} diff --git a/app/src/main/java/org/schabi/newpipe/player/ui/MainPlayerUi.java b/app/src/main/java/org/schabi/newpipe/player/ui/MainPlayerUi.java index 15515b6fa19..e5ffe8c3467 100644 --- a/app/src/main/java/org/schabi/newpipe/player/ui/MainPlayerUi.java +++ b/app/src/main/java/org/schabi/newpipe/player/ui/MainPlayerUi.java @@ -454,6 +454,7 @@ protected void setupElementsVisibility() { updateListenModeUi(player.getPlaybackPresentationMode()); updateLearningNoteButtonVisibility(player.getCurrentStreamInfo().orElse(null)); binding.switchMute.setVisibility(View.VISIBLE); + ChannelMuteControls.show(binding, false); binding.playerCloseButton.setVisibility(isFullscreen ? View.GONE : View.VISIBLE); // Top controls have a large minHeight which is allows to drag the player // down in fullscreen mode (just larger area to make easy to locate by finger) diff --git a/app/src/main/java/org/schabi/newpipe/player/ui/PopupPlayerUi.java b/app/src/main/java/org/schabi/newpipe/player/ui/PopupPlayerUi.java index 8d7d48457bd..ccfad5970c4 100644 --- a/app/src/main/java/org/schabi/newpipe/player/ui/PopupPlayerUi.java +++ b/app/src/main/java/org/schabi/newpipe/player/ui/PopupPlayerUi.java @@ -183,6 +183,7 @@ protected void setupElementsVisibility() { binding.videoAdjustmentsButton.setVisibility(View.GONE); binding.sleepTimerCountdown.setVisibility(View.GONE); binding.switchMute.setVisibility(View.GONE); + ChannelMuteControls.show(binding, true); binding.playerCloseButton.setVisibility(View.GONE); binding.topControls.bringToFront(); binding.topControls.setClickable(false); @@ -336,7 +337,9 @@ private void applyPopupVideoSize() { R.dimen.popup_minimum_width); final PopupVideoSizing.Size size = PopupVideoSizing.fit( preferredPopupWidth > 0 ? preferredPopupWidth : popupLayoutParams.width, - screenWidth, screenHeight, minWidth, popupVideoAspectRatio); + screenWidth, screenHeight, minWidth, popupVideoAspectRatio, + context.getResources().getDimensionPixelSize( + R.dimen.player_popup_minimum_controls_height)); if (popupLayoutParams.width == size.getWidth() && popupLayoutParams.height == size.getHeight()) { return; @@ -535,7 +538,9 @@ public WindowManager.LayoutParams retrievePopupLayoutParamsFromPrefs() { final PopupVideoSizing.Size size = PopupVideoSizing.fit( preferredPopupWidth, screenWidth, screenHeight, context.getResources().getDimensionPixelSize(R.dimen.popup_minimum_width), - popupVideoAspectRatio); + popupVideoAspectRatio, + context.getResources().getDimensionPixelSize( + R.dimen.player_popup_minimum_controls_height)); final WindowManager.LayoutParams params = new WindowManager.LayoutParams( size.getWidth(), size.getHeight(), diff --git a/app/src/main/java/org/schabi/newpipe/player/ui/PopupVideoSizing.kt b/app/src/main/java/org/schabi/newpipe/player/ui/PopupVideoSizing.kt index 067c36a48ce..b10a91ae92e 100644 --- a/app/src/main/java/org/schabi/newpipe/player/ui/PopupVideoSizing.kt +++ b/app/src/main/java/org/schabi/newpipe/player/ui/PopupVideoSizing.kt @@ -12,7 +12,8 @@ internal object PopupVideoSizing { availableWidth: Int, availableHeight: Int, minimumWidth: Int, - videoAspectRatio: Float + videoAspectRatio: Float, + minimumHeight: Int = 0 ): Size { val maxWidth = availableWidth.coerceAtLeast(1) val maxHeight = availableHeight.coerceAtLeast(1) @@ -20,7 +21,8 @@ internal object PopupVideoSizing { ?.takeIf { it < 1.0f } ?: (16f / 9f) val minWidth = minimumWidth.coerceIn(1, maxWidth) val wantedWidth = requestedWidth.coerceIn(minWidth, maxWidth) - val wantedHeight = (wantedWidth / effectiveRatio).roundToInt().coerceAtLeast(1) + val minHeight = minimumHeight.coerceIn(1, maxHeight) + val wantedHeight = (wantedWidth / effectiveRatio).roundToInt().coerceAtLeast(minHeight) if (wantedHeight <= maxHeight) { return Size(wantedWidth, wantedHeight) } diff --git a/app/src/main/java/org/schabi/newpipe/player/ui/VideoPlayerUi.java b/app/src/main/java/org/schabi/newpipe/player/ui/VideoPlayerUi.java index b683b5842f9..c6de624d4e2 100644 --- a/app/src/main/java/org/schabi/newpipe/player/ui/VideoPlayerUi.java +++ b/app/src/main/java/org/schabi/newpipe/player/ui/VideoPlayerUi.java @@ -288,6 +288,8 @@ protected void initListeners() { .setPackage(App.PACKAGE_NAME)) )); binding.switchMute.setOnClickListener(makeOnClickListener(player::toggleMute)); + ChannelMuteControls.bind(binding, makeOnClickListener(player::toggleMute), + v -> binding.fullScreenButton.performClick()); ViewCompat.setOnApplyWindowInsetsListener(binding.itemsListPanel, (view, windowInsets) -> { final Insets cutout = windowInsets.getInsets(WindowInsetsCompat.Type.displayCutout()); if (!cutout.equals(Insets.NONE)) { @@ -356,6 +358,7 @@ protected void deinitListeners() { binding.openInBrowser.setOnLongClickListener(null); binding.playerCloseButton.setOnClickListener(null); binding.switchMute.setOnClickListener(null); + ChannelMuteControls.bind(binding, null, null); ViewCompat.setOnApplyWindowInsetsListener(binding.itemsListPanel, null); @@ -1116,6 +1119,7 @@ public void onMuteUnmuteChanged(final boolean isMuted) { private void setMuteButton(final boolean isMuted) { binding.switchMute.setImageDrawable(AppCompatResources.getDrawable(context, isMuted ? R.drawable.ic_volume_off : R.drawable.ic_volume_up)); + ChannelMuteControls.update(binding, isMuted, player.getHasChannelMuteIdentity()); } private void setShuffleButton(final boolean shuffled) { diff --git a/app/src/main/res/layout/player.xml b/app/src/main/res/layout/player.xml index eb4e98389f0..124f34bb7c4 100644 --- a/app/src/main/res/layout/player.xml +++ b/app/src/main/res/layout/player.xml @@ -929,6 +929,22 @@ android:visibility="gone" tools:ignore="HardcodedText,RtlHardcoded,RtlSymmetry" /> + + + @@ -1002,6 +1019,56 @@ + + + + + + + + + + + 16sp 12dp 1dp + 96dp 40dp 200dp 4dp diff --git a/app/src/main/res/values/strings_channel_playback_mute.xml b/app/src/main/res/values/strings_channel_playback_mute.xml new file mode 100644 index 00000000000..2cbccede0d4 --- /dev/null +++ b/app/src/main/res/values/strings_channel_playback_mute.xml @@ -0,0 +1,5 @@ + + + Mute this channel + Unmute this channel + diff --git a/app/src/test/java/org/schabi/newpipe/player/ChannelPlaybackMuteControllerTest.kt b/app/src/test/java/org/schabi/newpipe/player/ChannelPlaybackMuteControllerTest.kt new file mode 100644 index 00000000000..22851876a4d --- /dev/null +++ b/app/src/test/java/org/schabi/newpipe/player/ChannelPlaybackMuteControllerTest.kt @@ -0,0 +1,150 @@ +/* + * SPDX-FileCopyrightText: 2026 WizeStream contributors + * SPDX-License-Identifier: GPL-3.0-or-later + */ +package org.schabi.newpipe.player + +import android.app.Application +import androidx.preference.PreferenceManager +import org.junit.Assert.assertFalse +import org.junit.Assert.assertTrue +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.mockito.Mockito.mock +import org.mockito.Mockito.`when` +import org.robolectric.RobolectricTestRunner +import org.robolectric.RuntimeEnvironment +import org.robolectric.annotation.Config +import org.schabi.newpipe.R +import org.schabi.newpipe.extractor.stream.StreamInfo +import org.schabi.newpipe.player.playqueue.PlayQueueItem + +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [35], application = Application::class) +class ChannelPlaybackMuteControllerTest { + private val context get() = RuntimeEnvironment.getApplication() + private val preferences get() = PreferenceManager.getDefaultSharedPreferences(context) + + @Before + fun clearPreferences() { + preferences.edit().clear().commit() + } + + @Test + fun channelChoiceSurvivesRecreationAndDoesNotMuteOtherChannelsOrServices() { + val first = controller() + first.select(0, "video-a", "channel-a") + first.toggle() + assertTrue(first.isMuted) + + val restarted = controller() + restarted.select(0, "another-video", "channel-a") + assertTrue(restarted.isMuted) + restarted.select(0, "video-b", "channel-b") + assertFalse(restarted.isMuted) + restarted.select(1, "video-c", "channel-a") + assertFalse(restarted.isMuted) + restarted.select(0, "video-a", "channel-a") + assertTrue(restarted.isMuted) + restarted.toggle() + assertFalse(restarted.isMuted) + + controller().also { + it.select(0, "third-video", "channel-a") + assertFalse(it.isMuted) + } + } + + @Test + fun explicitChoiceBeforeLateMetadataIsRememberedWithoutBeingOverridden() { + val value = controller() + value.select(0, "video-a", null) + value.toggle() + value.onMetadata(info("video-a", "channel-a")) + assertTrue(value.isMuted) + assertTrue(value.hasChannelIdentity) + repeat(3) { value.onMetadata(info("video-a", "channel-a")) } + assertTrue(value.isMuted) + controller().also { + it.select(0, "video-b", "channel-a") + assertTrue(it.isMuted) + } + } + + @Test + fun lateMetadataUsesTheLatestManualChoiceAndNeverThePreviousVideo() { + controller().also { + it.select(0, "existing", "channel-a") + it.toggle() + } + val value = controller() + value.select(0, "video-a", null) + value.toggle() + value.toggle() + value.onMetadata(info("video-a", "channel-a")) + assertFalse(value.isMuted) + + value.select(0, "video-b", null) + value.onMetadata(info("video-a", "channel-a")) + assertFalse(value.hasChannelIdentity) + assertFalse(value.isMuted) + value.toggle() + value.onMetadata(info("video-a", "channel-a")) + assertTrue(value.isMuted) + assertFalse(value.hasChannelIdentity) + controller().also { + it.select(0, "video-c", "channel-a") + assertFalse(it.isMuted) + } + } + + @Test + fun missingChannelAndLocalMediaChoicesStayWithinTheirCurrentVideo() { + val value = controller() + value.select(0, "without-uploader", " ") + value.toggle() + assertTrue(value.isMuted) + assertFalse(value.hasChannelIdentity) + value.select(0, "unrelated", null) + assertFalse(value.isMuted) + val local = mock(PlayQueueItem::class.java) + `when`(local.serviceId).thenReturn(0) + `when`(local.url).thenReturn("file:///video.mp4") + `when`(local.uploaderUrl).thenReturn("not-an-online-channel") + `when`(local.isLocalMedia).thenReturn(true) + value.begin(local) + value.toggle() + assertFalse(value.hasChannelIdentity) + assertTrue(preferences.all.isEmpty()) + } + + @Test + fun channelChoiceIsExplicitAndIndependentOfAutomaticSpeedAndCaptionProfiles() { + preferences.edit().putBoolean(context.getString(R.string.per_channel_playback_profiles_key), false).commit() + val value = controller() + value.select(0, "video-a", "channel-a") + value.toggle() + controller().also { + it.select(0, "video-b", "channel-a") + assertTrue(it.isMuted) + } + } + + @Test + fun malformedImportedFlagCannotCrashPlayback() { + val value = controller() + value.select(0, "video-a", "channel-a") + value.toggle() + val key = preferences.all.keys.single() + preferences.edit().putString(key, "not a Boolean").commit() + controller().also { + it.select(0, "video-b", "channel-a") + assertFalse(it.isMuted) + } + } + + private fun controller() = ChannelPlaybackMuteController(context) {} + + private fun info(url: String, channel: String) = StreamInfo(0, url, url, url).apply { uploaderUrl = channel } +} diff --git a/app/src/test/java/org/schabi/newpipe/player/PlayerChannelMuteIntegrationTest.kt b/app/src/test/java/org/schabi/newpipe/player/PlayerChannelMuteIntegrationTest.kt new file mode 100644 index 00000000000..e9965c26d4f --- /dev/null +++ b/app/src/test/java/org/schabi/newpipe/player/PlayerChannelMuteIntegrationTest.kt @@ -0,0 +1,225 @@ +/* + * SPDX-FileCopyrightText: 2026 WizeStream contributors + * SPDX-License-Identifier: GPL-3.0-or-later + */ +package org.schabi.newpipe.player + +import android.app.Application +import androidx.media3.common.MediaItem +import androidx.media3.exoplayer.ExoPlayer +import androidx.preference.PreferenceManager +import java.util.Optional +import org.junit.Assert.assertFalse +import org.junit.Assert.assertTrue +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.mockito.Mockito.clearInvocations +import org.mockito.Mockito.doAnswer +import org.mockito.Mockito.inOrder +import org.mockito.Mockito.mock +import org.mockito.Mockito.never +import org.mockito.Mockito.verify +import org.mockito.Mockito.`when` +import org.robolectric.RobolectricTestRunner +import org.robolectric.RuntimeEnvironment +import org.robolectric.annotation.Config +import org.schabi.newpipe.extractor.stream.StreamInfo +import org.schabi.newpipe.player.helper.AudioReactor +import org.schabi.newpipe.player.mediaitem.ExceptionTag +import org.schabi.newpipe.player.mediaitem.MediaItemTag +import org.schabi.newpipe.player.mediaitem.PlaceholderTag +import org.schabi.newpipe.player.mediasource.LoadedMediaSource +import org.schabi.newpipe.player.playqueue.PlayQueueItem +import org.schabi.newpipe.player.ui.PlayerUi +import org.schabi.newpipe.player.ui.PlayerUiList + +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [35], application = Application::class) +class PlayerChannelMuteIntegrationTest { + private val context get() = RuntimeEnvironment.getApplication() + + @Before + fun clearPreferences() { + PreferenceManager.getDefaultSharedPreferences(context).edit().clear().commit() + } + + @Test + fun savedMuteIsAppliedWhenQueueSelectsTheItemAndSurvivesMetadataAndEngineReplacement() { + saveMute("channel-a") + val fixture = Fixture() + val item = queueItem("video-a", "channel-a") + + fixture.audio.beginItem(item) + assertTrue(fixture.audio.isMuted) + assertTrue(fixture.appliedVolumes.last()) + verify(fixture.focus).abandonAudioFocus() + fixture.mediaItem = mediaItem("video-a", "channel-a") + fixture.audio.onMediaItemTransition(fixture.mediaItem) + fixture.audio.onMetadata(info("video-a", "channel-a")) + assertTrue(fixture.audio.isMuted) + fixture.audio.onAudioSessionChanged(12) + assertTrue(fixture.appliedVolumes.last()) + verify(fixture.ui).onMuteUnmuteChanged(true) + fixture.audio.releaseAudioSession() + } + + @Test + fun media3TransitionAppliesTheNextChannelsMuteBeforeBatchedMetadataUpdates() { + saveMute("channel-b") + val fixture = Fixture() + fixture.audio.beginItem(queueItem("video-a", "channel-a")) + fixture.mediaItem = mediaItem("video-b", "channel-b") + val metadata = mock(PlayerMetadataController::class.java) + val listener = PlayerMedia3ListenerController( + fixture.player, + fixture.audio, + metadata, + mock(PlaybackParametersController::class.java), + mock(SponsorBlockPlaybackController::class.java), + mock(PlayerErrorController::class.java), + mock(SleepTimerPlaybackController::class.java) + ) + clearInvocations(fixture.player, metadata) + + listener.onEvents(fixture.engine) + + assertTrue(fixture.audio.isMuted) + inOrder(fixture.player, metadata).apply { + verify(fixture.player).applyPlayerVolume() + verify(metadata).onEvents(fixture.engine) + } + fixture.mediaItem = mediaItem("video-c", "channel-c") + fixture.audio.onMediaItemTransition(fixture.mediaItem) + assertFalse(fixture.audio.isMuted) + assertFalse(fixture.appliedVolumes.last()) + } + + @Test + fun aClickDuringAQueueTransitionWritesTheNewChannelAndDoesNotChangePausedState() { + val fixture = Fixture() + fixture.audio.beginItem(queueItem("video-a", "channel-a")) + fixture.playWhenReady = false + fixture.mediaItem = mediaItem("video-b", "channel-b") + fixture.audio.toggleMute() + assertTrue(fixture.audio.isMuted) + clearInvocations(fixture.focus) + fixture.audio.toggleMute() + assertFalse(fixture.audio.isMuted) + verify(fixture.focus, never()).requestAudioFocus() + verify(fixture.engine, never()).play() + fixture.audio.toggleMute() + + ChannelPlaybackMuteController(context) {}.also { + it.select(0, "another-a", "channel-a") + assertFalse(it.isMuted) + it.select(0, "another-b", "channel-b") + assertTrue(it.isMuted) + } + } + + @Test + fun delayedTransitionFromTheReleasedSourceCannotOverwriteTheNewQueueChoice() { + saveMute("channel-b") + val fixture = Fixture() + val oldSource = mediaItem("video-a", "channel-a") + fixture.mediaItem = oldSource + fixture.audio.beginItem(queueItem("video-a", "channel-a")) + // initPlayback releases the old engine before selecting the next queue item. + fixture.mediaItem = null + fixture.audio.beginItem(queueItem("video-b", "channel-b")) + fixture.audio.onMediaItemTransition(oldSource) + fixture.audio.onMetadata(info("video-a", "channel-a")) + assertTrue(fixture.audio.isMuted) + assertTrue(fixture.appliedVolumes.last()) + } + + @Test + fun clicksDuringPlaceholderLoadingSurviveCanonicalizedStreamMetadata() { + val fixture = Fixture() + val queueItem = queueItem("original-url", "") + fixture.audio.beginItem(queueItem) + fixture.mediaItem = PlaceholderTag.EMPTY.asMediaItem() + fixture.audio.toggleMute() + assertTrue(fixture.audio.isMuted) + val source = mock(LoadedMediaSource::class.java) + `when`(source.stream).thenReturn(queueItem) + fixture.mediaItem = mediaItem("canonical-url", "channel-a", source) + + fixture.audio.onMediaItemTransition(fixture.mediaItem) + fixture.audio.onMetadata(info("canonical-url", "channel-a")) + + assertTrue(fixture.audio.isMuted) + assertTrue(fixture.audio.hasChannelMuteIdentity) + ChannelPlaybackMuteController(context) {}.also { + it.select(0, "another-video", "channel-a") + assertTrue(it.isMuted) + } + } + + @Test + fun failedSourceExtrasCannotCrashOrClearTheSelectedChannelsMute() { + saveMute("channel-a") + val fixture = Fixture() + val item = queueItem("video-a", "channel-a") + fixture.audio.beginItem(item) + // ExceptionTag casts its extras directly, and a failure does not carry LoadedMediaSource. + val failedTag = ExceptionTag.of(item, listOf(IllegalStateException("Unavailable"))).withExtras(Any()) + fixture.mediaItem = MediaItem.fromUri("video-a").buildUpon().setTag(failedTag).build() + + fixture.audio.onMediaItemTransition(fixture.mediaItem) + + assertTrue(fixture.audio.isMuted) + assertTrue(fixture.audio.hasChannelMuteIdentity) + } + + private fun saveMute(channel: String) = ChannelPlaybackMuteController(context) {}.apply { + select(0, "saved-video", channel) + toggle() + } + + private inner class Fixture { + val player = mock(Player::class.java) + val engine = mock(ExoPlayer::class.java) + val focus = mock(AudioReactor::class.java) + val ui = mock(PlayerUi::class.java) + var mediaItem: MediaItem? = null + var playWhenReady = true + val appliedVolumes = mutableListOf() + val audio: PlayerAudioController + + init { + `when`(player.context).thenReturn(context) + `when`(player.prefs).thenReturn(PreferenceManager.getDefaultSharedPreferences(context)) + `when`(player.exoPlayer).thenReturn(engine) + `when`(player.getExoPlayer()).thenReturn(engine) + `when`(player.audioReactor).thenReturn(focus) + `when`(player.playWhenReady).thenAnswer { playWhenReady } + `when`(engine.currentMediaItem).thenAnswer { mediaItem } + `when`(player.UIs()).thenReturn(PlayerUiList(ui)) + audio = PlayerAudioController(player) + doAnswer { + appliedVolumes += audio.isMuted + null + }.`when`(player).applyPlayerVolume() + } + } + + private fun queueItem(url: String, channel: String) = mock(PlayQueueItem::class.java).also { + `when`(it.serviceId).thenReturn(0) + `when`(it.url).thenReturn(url) + `when`(it.uploaderUrl).thenReturn(channel) + } + + private fun info(url: String, channel: String) = StreamInfo(0, url, url, url).apply { uploaderUrl = channel } + + private fun mediaItem(url: String, channel: String, source: LoadedMediaSource? = null): MediaItem { + val tag = mock(MediaItemTag::class.java) + `when`(tag.serviceId).thenReturn(0) + `when`(tag.streamUrl).thenReturn(url) + `when`(tag.uploaderUrl).thenReturn(channel) + `when`(tag.maybeStreamInfo).thenReturn(Optional.of(info(url, channel))) + `when`(tag.getMaybeExtras(Any::class.java)).thenReturn(Optional.ofNullable(source)) + return MediaItem.fromUri(url).buildUpon().setTag(tag).build() + } +} diff --git a/app/src/test/java/org/schabi/newpipe/player/ui/ChannelMuteControlsTest.kt b/app/src/test/java/org/schabi/newpipe/player/ui/ChannelMuteControlsTest.kt new file mode 100644 index 00000000000..115eba792cc --- /dev/null +++ b/app/src/test/java/org/schabi/newpipe/player/ui/ChannelMuteControlsTest.kt @@ -0,0 +1,212 @@ +/* + * SPDX-FileCopyrightText: 2026 WizeStream contributors + * SPDX-License-Identifier: GPL-3.0-or-later + */ +package org.schabi.newpipe.player.ui + +import android.app.Activity +import android.app.Application +import android.graphics.Rect +import android.os.Looper +import android.view.ContextThemeWrapper +import android.view.LayoutInflater +import android.view.MotionEvent +import android.view.View +import android.view.ViewGroup +import android.widget.LinearLayout +import androidx.preference.PreferenceManager +import org.junit.Assert.assertEquals +import org.junit.Assert.assertFalse +import org.junit.Assert.assertSame +import org.junit.Assert.assertTrue +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.Robolectric +import org.robolectric.RobolectricTestRunner +import org.robolectric.RuntimeEnvironment +import org.robolectric.Shadows.shadowOf +import org.robolectric.annotation.Config +import org.schabi.newpipe.R +import org.schabi.newpipe.databinding.PlayerBinding + +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [23, 35], application = Application::class) +class ChannelMuteControlsTest { + private val context get() = ContextThemeWrapper(RuntimeEnvironment.getApplication(), R.style.LightTheme) + + @Before + fun clearPreferences() { + PreferenceManager.getDefaultSharedPreferences(context).edit().clear().commit() + } + + @Test + fun overlayMuteRemainsBesideFullscreenWithOptionalToolsHiddenInBothDirections() { + for (direction in listOf(View.LAYOUT_DIRECTION_LTR, View.LAYOUT_DIRECTION_RTL)) { + for ((width, height) in listOf(320 to 180, 640 to 360)) { + val binding = binding() + binding.root.layoutDirection = direction + binding.playbackControlRoot.visibility = View.VISIBLE + binding.secondaryControls.visibility = View.GONE + PlayerActionVisibility.save(context, setOf("mute")) + binding.switchMute.visibility = View.GONE + binding.screenRotationButton.visibility = View.VISIBLE + ChannelMuteControls.show(binding, false) + measure(binding.root, width, height) + + val mute = binding.channelMuteButton + val fullscreen = binding.screenRotationButton + assertEquals(View.VISIBLE, mute.visibility) + assertEquals(View.VISIBLE, fullscreen.visibility) + assertTrue(mute.width >= dp(48)) + assertTrue(mute.height >= dp(48)) + assertTrue(mute.right <= fullscreen.left) + assertTrue(fullscreen.right <= binding.bottomControls.width) + assertTrue(binding.playbackSeekBar.width > 0) + assertEquals(mute.parent, fullscreen.parent) + } + } + } + + @Test + fun minimumPopupKeepsSoundAndFullscreenReachableWithoutShrinkingTheTimeline() { + for (direction in listOf(View.LAYOUT_DIRECTION_LTR, View.LAYOUT_DIRECTION_RTL)) { + for (width in listOf(150, 180, 320)) { + val height = PopupVideoSizing.fit(width, 1080, 1920, 150, 16f / 9f, 96).height + val binding = binding() + binding.root.layoutDirection = direction + binding.playbackControlRoot.visibility = View.VISIBLE + binding.topControls.orientation = LinearLayout.HORIZONTAL + binding.primaryControls.layoutParams.width = ViewGroup.LayoutParams.WRAP_CONTENT + binding.metadataView.visibility = View.GONE + binding.secondaryControls.visibility = View.VISIBLE + binding.screenRotationButton.visibility = View.GONE + binding.bottomControls.setPadding(dp(6), 0, dp(6), 0) + binding.playbackCurrentTime.text = "1:06:29" + binding.playbackEndTime.text = "1:23:49" + measure(binding.root, width, height) + val timelineWidth = binding.playbackSeekBar.width + + ChannelMuteControls.show(binding, true) + measure(binding.root, width, height) + + val actions = binding.popupPlaybackActions + val mute = binding.popupMuteButton + val fullscreen = binding.popupFullscreenButton + assertEquals(View.VISIBLE, actions.visibility) + assertEquals(View.GONE, binding.channelMuteButton.visibility) + assertTrue(actions.left >= 0) + assertTrue(actions.right <= binding.root.width) + assertTrue(actions.bottom <= binding.root.height) + assertTrue(actions.bottom <= binding.bottomControls.top) + assertTrue(mute.width >= dp(48)) + assertTrue(mute.height >= dp(48)) + assertTrue(fullscreen.width >= dp(48)) + assertTrue(mute.right <= fullscreen.left || fullscreen.right <= mute.left) + assertTrue(binding.popupToolsScroller.width > 0) + assertTrue(binding.topControls.width >= binding.popupToolsScroller.width) + assertEquals(timelineWidth, binding.playbackSeekBar.width) + assertTrue(timelineWidth > 0) + assertEquals(View.VISIBLE, binding.playbackCurrentTime.visibility) + assertEquals(View.VISIBLE, binding.playbackEndTime.visibility) + assertEquals(R.id.popupFullscreenButton, binding.playbackSeekBar.nextFocusUpId) + var muteClicks = 0 + var playbackClicks = 0 + binding.popupMuteButton.setOnClickListener { muteClicks++ } + binding.playPauseButton.setOnClickListener { playbackClicks++ } + tap(binding, binding.popupMuteButton) + assertEquals(1, muteClicks) + assertEquals(0, playbackClicks) + } + } + } + + @Test + fun switchingBackToMainRestoresTheOriginalControlsParentAndFocusRoute() { + val binding = binding() + val originalParams = binding.topControls.layoutParams + val originalSeekUp = binding.playbackSeekBar.nextFocusUpId + repeat(2) { + ChannelMuteControls.show(binding, true) + assertSame(binding.popupToolsScroller, binding.topControls.parent) + ChannelMuteControls.show(binding, false) + assertSame(binding.playbackWindowRoot, binding.topControls.parent) + assertSame(originalParams, binding.topControls.layoutParams) + assertEquals(originalSeekUp, binding.playbackSeekBar.nextFocusUpId) + assertEquals(View.VISIBLE, binding.channelMuteButton.visibility) + assertEquals(View.GONE, binding.popupPlaybackActions.visibility) + assertEquals(View.GONE, binding.popupToolsScroller.visibility) + } + } + + @Test + fun clicksAndAccessibleActionReflectMutedChannelAndDetachCleanly() { + val binding = binding() + var muted = false + var fullscreenClicks = 0 + ChannelMuteControls.bind(binding, { + muted = !muted + ChannelMuteControls.update(binding, muted, true) + }, { fullscreenClicks++ }) + ChannelMuteControls.show(binding, true) + ChannelMuteControls.update(binding, false, true) + assertEquals(context.getString(R.string.channel_playback_mute), binding.channelMuteButton.contentDescription) + binding.popupMuteButton.performClick() + assertTrue(muted) + assertTrue(binding.channelMuteButton.isActivated) + assertTrue(binding.popupMuteButton.isActivated) + assertEquals(context.getString(R.string.channel_playback_unmute), binding.channelMuteButton.contentDescription) + assertEquals(context.getString(R.string.unmute), binding.switchMute.contentDescription) + binding.channelMuteButton.performClick() + binding.popupMuteButton.performClick() + assertTrue(muted) + binding.popupMuteButton.performClick() + assertFalse(muted) + binding.popupFullscreenButton.performClick() + assertEquals(1, fullscreenClicks) + ChannelMuteControls.bind(binding, null, null) + binding.channelMuteButton.performClick() + binding.popupMuteButton.performClick() + binding.popupFullscreenButton.performClick() + assertFalse(muted) + assertEquals(1, fullscreenClicks) + ChannelMuteControls.update(binding, true, false) + assertEquals(context.getString(R.string.unmute), binding.channelMuteButton.contentDescription) + } + + private fun binding() = PlayerBinding.inflate(LayoutInflater.from(context)) + + private fun measure(view: View, width: Int, height: Int) { + view.measure( + View.MeasureSpec.makeMeasureSpec(dp(width), View.MeasureSpec.EXACTLY), + View.MeasureSpec.makeMeasureSpec(dp(height), View.MeasureSpec.EXACTLY) + ) + view.layout(0, 0, view.measuredWidth, view.measuredHeight) + } + + private fun dp(value: Int) = (value * context.resources.displayMetrics.density + 0.5f).toInt() + + private fun tap(binding: PlayerBinding, target: View) { + val width = binding.root.width + val height = binding.root.height + val activity = Robolectric.buildActivity(Activity::class.java).setup() + try { + activity.get().setContentView(binding.root) + binding.root.measure( + View.MeasureSpec.makeMeasureSpec(width, View.MeasureSpec.EXACTLY), + View.MeasureSpec.makeMeasureSpec(height, View.MeasureSpec.EXACTLY) + ) + binding.root.layout(0, 0, width, height) + val bounds = Rect(0, 0, target.width, target.height) + binding.root.offsetDescendantRectToMyCoords(target, bounds) + for (action in listOf(MotionEvent.ACTION_DOWN, MotionEvent.ACTION_UP)) { + val event = MotionEvent.obtain(0, 1, action, bounds.exactCenterX(), bounds.exactCenterY(), 0) + binding.root.dispatchTouchEvent(event) + event.recycle() + } + shadowOf(Looper.getMainLooper()).idle() + } finally { + activity.pause().stop().destroy() + } + } +} diff --git a/app/src/test/java/org/schabi/newpipe/player/ui/PopupVideoSizingTest.kt b/app/src/test/java/org/schabi/newpipe/player/ui/PopupVideoSizingTest.kt index 4128d33393c..72ece7432d5 100644 --- a/app/src/test/java/org/schabi/newpipe/player/ui/PopupVideoSizingTest.kt +++ b/app/src/test/java/org/schabi/newpipe/player/ui/PopupVideoSizingTest.kt @@ -28,4 +28,12 @@ class PopupVideoSizingTest { ) assertEquals(180, PopupVideoSizing.fit(320, 1080, 2000, 180, 16f / 9f).height) } + + @Test + fun smallestPopupKeepsSeparateTouchRowsWithoutChangingItsPreferredWidth() { + assertEquals(PopupVideoSizing.Size(150, 96), PopupVideoSizing.fit(150, 1080, 2000, 150, 16f / 9f, 96)) + assertEquals(PopupVideoSizing.Size(180, 101), PopupVideoSizing.fit(180, 1080, 2000, 150, 16f / 9f, 96)) + assertEquals(PopupVideoSizing.Size(150, 267), PopupVideoSizing.fit(150, 1080, 2000, 150, 9f / 16f, 96)) + assertTrue(PopupVideoSizing.fit(150, 150, 80, 150, 16f / 9f, 96).height <= 80) + } } diff --git a/app/src/test/java/org/schabi/newpipe/sync/ChannelMuteSyncBoundaryTest.kt b/app/src/test/java/org/schabi/newpipe/sync/ChannelMuteSyncBoundaryTest.kt new file mode 100644 index 00000000000..d20ed2992fe --- /dev/null +++ b/app/src/test/java/org/schabi/newpipe/sync/ChannelMuteSyncBoundaryTest.kt @@ -0,0 +1,45 @@ +/* + * SPDX-FileCopyrightText: 2026 WizeStream contributors + * SPDX-License-Identifier: GPL-3.0-or-later + */ +package org.schabi.newpipe.sync + +import android.app.Application +import androidx.preference.PreferenceManager +import org.junit.Assert.assertEquals +import org.junit.Assert.assertTrue +import org.junit.Test +import org.junit.runner.RunWith +import org.mockito.Mockito.mock +import org.mockito.Mockito.`when` +import org.robolectric.RobolectricTestRunner +import org.robolectric.RuntimeEnvironment +import org.robolectric.annotation.Config +import org.schabi.newpipe.player.ChannelPlaybackMuteController + +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [35], application = Application::class) +class ChannelMuteSyncBoundaryTest { + @Test + fun existingChannelProfileSyncLeavesLocalMuteFlagsUntouchedAndDoesNotChangeItsProtocol() { + val context = RuntimeEnvironment.getApplication() + val preferences = PreferenceManager.getDefaultSharedPreferences(context) + preferences.edit().clear().commit() + val repository = mock(StructuredPreferenceRecordRepository::class.java) + `when`(repository.getRecordsByType(StructuredPreferenceCategory.CHANNEL_PROFILES, StructuredPreferenceRecordType.CHANNEL_PROFILE_FIELD)) + .thenReturn(emptyList()) + val adapter = ChannelProfileSyncAdapter(preferences, repository) + val originalHash = adapter.snapshotHash() + ChannelPlaybackMuteController(context) {}.apply { + select(0, "video-a", "channel-a") + toggle() + } + val key = preferences.all.keys.single() + + assertEquals(originalHash, adapter.snapshotHash()) + adapter.materialize() + + assertTrue(preferences.getBoolean(key, false)) + assertEquals(originalHash, adapter.snapshotHash()) + } +} diff --git a/docs/changelogs.md b/docs/changelogs.md index 8fb582af472..288f8e5b9c2 100644 --- a/docs/changelogs.md +++ b/docs/changelogs.md @@ -36,6 +36,11 @@ Release history is listed newest first. The number beside each release is its An - Use compact Material 3 recording and menu actions with space between controls and the screen edges. Show Retry only after a connection failure, and keep Saved chat and danmaku available from the menu. - Keep the newest messages visible above the bottom navigation while the video header is expanded. +### Channel playback mute + +- Add a mute button beside fullscreen in the main and popup video controls, available without opening the extra tools. +- Remember mute and unmute for each service and channel across videos and app restarts. Videos without a channel keep a temporary choice for that video. + ## WizeStream 1.53.0 (`1053000`) ### Retained subscription alerts diff --git a/docs/channel-playback-mute.md b/docs/channel-playback-mute.md new file mode 100644 index 00000000000..dbfbfc42b0a --- /dev/null +++ b/docs/channel-playback-mute.md @@ -0,0 +1,23 @@ +# Channel playback mute + +The speaker button beside fullscreen changes the current video's audio immediately. When the source identifies its channel, the choice applies to that service and channel's other videos and survives app restarts. Pressing it again removes the saved mute. The existing Mute action in the extra player tools uses the same state. + +The main player keeps this control in the seek row beside fullscreen. Popup playback uses a compact pair of over-video mute and fullscreen buttons, preserving room for timestamps and seeking at its minimum width. Very small popups have a minimum height of 96dp to keep the two control rows separate; the video continues to fit within the window. The accessible label identifies whether the next action will mute or unmute the channel. Audio, Listen mode and live playback use the same player state. + +## Identity and lifetime + +- The key uses the existing channel playback profile identity: service ID and the SHA-256 of the uploader URL. It is app-wide, matching playback profiles, rather than tied to a subscription or user profile. +- Explicit mute choices are remembered even when automatic speed, quality and caption profiles are disabled. Subscription feed muting is a separate setting. +- Local files and streams without a channel have a temporary choice for the current item. A different item with no saved choice starts unmuted. +- A click made before channel metadata arrives remains authoritative. Matching metadata attaches that choice to the channel; stale metadata from a previous item cannot change it. Placeholder source tags do not represent a different video. +- Queue selection applies known mute state before media preparation or seeking. Media3 transitions also resolve the active source, including transitions that reuse stream metadata or audio decoders. Engine replacement reapplies the selected volume before playback resumes. + +## Persistence and transfer + +Saved mute values are Boolean `.muted` entries under the existing `channel_playback_profile.v1` preference prefix. Unmute removes the entry. WizeStream's full settings backup includes these entries through its normal typed preference export and restore. + +Device Sync continues to transfer the existing speed, quality and caption profile fields. It neither sends nor removes local mute entries; this keeps the current sync protocol compatible with older devices. Portable subscription exports do not carry playback mute choices. + +## Verification + +Focused behavioral tests cover service/channel separation, recreation, late metadata and explicit clicks, unknown and local items, stale source events, volume/focus callbacks, UI visibility and accessible actions, popup minimum width, directionality and the Device Sync boundary. Device validation should also exercise real audio output while a mixed-channel queue advances, then switch between main, popup, Listen and background playback. JVM tests do not measure physical audio output or replace Android device rendering checks.