diff --git a/app/src/main/java/com/github/libretube/helpers/ProxyHelper.kt b/app/src/main/java/com/github/libretube/helpers/ProxyHelper.kt index b48b01de4..b6e15e581 100644 --- a/app/src/main/java/com/github/libretube/helpers/ProxyHelper.kt +++ b/app/src/main/java/com/github/libretube/helpers/ProxyHelper.kt @@ -1,5 +1,6 @@ package com.github.libretube.helpers +import android.util.Log import com.github.libretube.api.CronetHelper import com.github.libretube.api.RetrofitInstance import com.github.libretube.constants.PreferenceKeys @@ -36,15 +37,15 @@ object ProxyHelper { * Detect whether the proxy should be used or not for a given stream URL based on user preferences */ suspend fun unwrapStreamUrl(url: String): String { - return if (shouldDisableProxy(url)) unwrapUrl(url) else url + return if (useYouTubeSourceWithoutProxy(url)) unwrapUrl(url) else url } - suspend fun shouldDisableProxy(url: String) = when { + suspend fun useYouTubeSourceWithoutProxy(url: String) = when { !PreferenceHelper.getBoolean(PreferenceKeys.DISABLE_VIDEO_IMAGE_PROXY, false) -> false - PreferenceHelper.getBoolean(PreferenceKeys.FALLBACK_PIPED_PROXY, true) -> isUrlUsable( - unwrapUrl(url) - ) - + PreferenceHelper.getBoolean(PreferenceKeys.FALLBACK_PIPED_PROXY, true) -> { + // check whether the URL has content available, and disable proxy if that's the case + isUrlUsable(unwrapUrl(url)) + } else -> true } @@ -61,8 +62,12 @@ object ProxyHelper { */ fun unwrapUrl(url: String, unwrap: Boolean = true) = url.toHttpUrlOrNull() ?.takeIf { unwrap }?.let { + val host = it.queryParameter("host") + // if there's no host parameter specified, there's no way to unwrap the URL + // and the proxied one must be used. That's the case if using LBRY. + if (host.isNullOrEmpty()) return@let url it.newBuilder() - .host(it.queryParameter("host").orEmpty()) + .host(host) .removeAllQueryParameters("host") .build() .toString() diff --git a/app/src/main/java/com/github/libretube/services/OnlinePlayerService.kt b/app/src/main/java/com/github/libretube/services/OnlinePlayerService.kt index 97d306014..5ede7d7c1 100644 --- a/app/src/main/java/com/github/libretube/services/OnlinePlayerService.kt +++ b/app/src/main/java/com/github/libretube/services/OnlinePlayerService.kt @@ -42,7 +42,6 @@ import com.github.libretube.util.PlayingQueue import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch -import kotlinx.coroutines.runBlocking import kotlinx.coroutines.withContext import kotlinx.serialization.encodeToString @@ -299,7 +298,7 @@ class OnlinePlayerService : LifecycleService() { val streams = streams ?: return val (uri, mimeType) = if (streams.audioStreams.isNotEmpty()) { - val disableProxy = ProxyHelper.shouldDisableProxy(streams.videoStreams.first().url!!) + val disableProxy = ProxyHelper.useYouTubeSourceWithoutProxy(streams.videoStreams.first().url!!) PlayerHelper.createDashSource( streams, this, diff --git a/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt b/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt index bd730069e..661f82fb2 100644 --- a/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt +++ b/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt @@ -1290,8 +1290,12 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions { if (streams.livestream && streams.dash != null) ProxyHelper.unwrapStreamUrl( streams.dash!! ).toUri() else { + // skip LBRY urls when checking whether the stream source is usable + val urlToTest = streams.videoStreams.firstOrNull { + !it.quality.orEmpty().contains("LBRY") + }?.url.orEmpty() val shouldDisableProxy = - ProxyHelper.shouldDisableProxy(streams.videoStreams.first().url!!) + ProxyHelper.useYouTubeSourceWithoutProxy(urlToTest) PlayerHelper.createDashSource( streams, requireContext(),