Merge pull request #3290 from Bnyro/disable-proxy

Add option to disable Piped proxy
This commit is contained in:
Bnyro 2023-03-21 19:18:20 +01:00 committed by GitHub
commit aaae5d81ac
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 59 additions and 18 deletions

View File

@ -2,6 +2,7 @@ package com.github.libretube.api.obj
import com.github.libretube.db.obj.DownloadItem
import com.github.libretube.enums.FileType
import com.github.libretube.helpers.ProxyHelper
import java.nio.file.Paths
import kotlinx.datetime.LocalDate
import kotlinx.serialization.Serializable
@ -56,7 +57,7 @@ data class Streams(
videoId = videoId,
fileName = stream?.getQualityString(fileName).orEmpty(),
path = Paths.get(""),
url = stream?.url,
url = stream?.url?.let { ProxyHelper.unwrapIfEnabled(it) },
format = videoFormat,
quality = videoQuality
)
@ -73,7 +74,7 @@ data class Streams(
videoId = videoId,
fileName = stream?.getQualityString(fileName).orEmpty(),
path = Paths.get(""),
url = stream?.url,
url = stream?.url?.let { ProxyHelper.unwrapIfEnabled(it) },
format = audioFormat,
quality = audioQuality
)
@ -87,7 +88,9 @@ data class Streams(
videoId = videoId,
fileName = "${fileName}_$subtitleCode.srt",
path = Paths.get(""),
url = subtitles.find { it.code == subtitleCode }?.url
url = subtitles.find {
it.code == subtitleCode
}?.url?.let { ProxyHelper.unwrapIfEnabled(it) }
)
)
}

View File

@ -133,6 +133,7 @@ object PreferenceKeys {
const val CONFIRM_UNSUBSCRIBE = "confirm_unsubscribing"
const val CLEAR_BOOKMARKS = "clear_bookmarks"
const val MAX_CONCURRENT_DOWNLOADS = "max_concurrent_downloads"
const val DISABLE_VIDEO_IMAGE_PROXY = "disable_video_image_proxy"
/**
* History

View File

@ -149,7 +149,7 @@ object DashHelper {
audioChannelConfiguration.setAttribute("value", "2")
val baseUrl = doc.createElement("BaseURL")
baseUrl.appendChild(doc.createTextNode(stream.url!!))
baseUrl.appendChild(doc.createTextNode(ProxyHelper.unwrapIfEnabled(stream.url!!)))
val segmentBase = doc.createElement("SegmentBase")
segmentBase.setAttribute("indexRange", "${stream.indexStart}-${stream.indexEnd}")
@ -175,7 +175,7 @@ object DashHelper {
representation.setAttribute("frameRate", stream.fps.toString())
val baseUrl = doc.createElement("BaseURL")
baseUrl.appendChild(doc.createTextNode(stream.url!!))
baseUrl.appendChild(doc.createTextNode(ProxyHelper.unwrapIfEnabled(stream.url!!)))
val segmentBase = doc.createElement("SegmentBase")
segmentBase.setAttribute("indexRange", "${stream.indexStart}-${stream.indexEnd}")

View File

@ -53,7 +53,9 @@ object ImageHelper {
*/
fun loadImage(url: String?, target: ImageView) {
// only load the image if the data saver mode is disabled
if (!DataSaverMode.isEnabled(target.context)) target.load(url, imageLoader)
if (DataSaverMode.isEnabled(target.context) || url == null) return
val urlToLoad = ProxyHelper.unwrapIfEnabled(url)
target.load(urlToLoad, imageLoader)
}
fun downloadImage(context: Context, url: String, path: Path) {

View File

@ -27,4 +27,25 @@ object ProxyHelper {
?.port(proxyUrl.port)
?.toString()
}
/**
* Load the YT url directly instead of the proxied Piped URL if enabled
*/
fun unwrapIfEnabled(url: String): String {
if (!PreferenceHelper.getBoolean(
PreferenceKeys.DISABLE_VIDEO_IMAGE_PROXY,
false
)
) {
return url
}
return url.toHttpUrlOrNull()?.let {
it.newBuilder()
.host(it.queryParameter("host").orEmpty())
.removeAllQueryParameters("host")
.build()
.toString()
} ?: url
}
}

View File

@ -29,6 +29,7 @@ import com.github.libretube.extensions.toID
import com.github.libretube.helpers.PlayerHelper
import com.github.libretube.helpers.PlayerHelper.checkForSegments
import com.github.libretube.helpers.PlayerHelper.loadPlaybackParams
import com.github.libretube.helpers.ProxyHelper
import com.github.libretube.util.NowPlayingNotification
import com.github.libretube.util.PlayingQueue
import com.google.android.exoplayer2.ExoPlayer
@ -304,7 +305,7 @@ class BackgroundMode : LifecycleService() {
}
val mediaItem = MediaItem.Builder()
.setUri(uri)
.setUri(ProxyHelper.rewriteUrl(uri))
.build()
player?.setMediaItem(mediaItem)
}

View File

@ -67,6 +67,7 @@ import com.github.libretube.helpers.PlayerHelper
import com.github.libretube.helpers.PlayerHelper.checkForSegments
import com.github.libretube.helpers.PlayerHelper.loadPlaybackParams
import com.github.libretube.helpers.PreferenceHelper
import com.github.libretube.helpers.ProxyHelper
import com.github.libretube.obj.ShareData
import com.github.libretube.obj.VideoResolution
import com.github.libretube.services.BackgroundMode
@ -424,7 +425,7 @@ class PlayerFragment : Fragment(R.layout.fragment_player), OnlinePlayerOptions {
streams.hls ?: return@setOnLongClickListener true
// start an intent with video as mimetype using the hls stream
val uri: Uri = Uri.parse(streams.hls)
val uri = Uri.parse(ProxyHelper.unwrapIfEnabled(streams.hls!!))
val intent = Intent(Intent.ACTION_VIEW).apply {
setDataAndType(uri, "video/*")
putExtra(Intent.EXTRA_TITLE, streams.title)
@ -971,14 +972,12 @@ class PlayerFragment : Fragment(R.layout.fragment_player), OnlinePlayerOptions {
}
}
if (streams.hls != null) {
binding.relPlayerPip.setOnClickListener {
if (SDK_INT < Build.VERSION_CODES.O) return@setOnClickListener
try {
activity?.enterPictureInPictureMode(getPipParams())
} catch (e: Exception) {
e.printStackTrace()
}
binding.relPlayerPip.setOnClickListener {
if (SDK_INT < Build.VERSION_CODES.O) return@setOnClickListener
try {
activity?.enterPictureInPictureMode(getPipParams())
} catch (e: Exception) {
e.printStackTrace()
}
}
initializeRelatedVideos(streams.relatedStreams.filter { !it.title.isNullOrBlank() })
@ -1286,7 +1285,7 @@ class PlayerFragment : Fragment(R.layout.fragment_player), OnlinePlayerOptions {
// DASH
!PreferenceHelper.getBoolean(PreferenceKeys.USE_HLS_OVER_DASH, false) && streams.videoStreams.isNotEmpty() -> {
// only use the dash manifest generated by YT if either it's a livestream or no other source is available
val uri = streams.dash?.toUri().takeIf {
val uri = streams.dash?.let { ProxyHelper.unwrapIfEnabled(it) }?.toUri().takeIf {
streams.livestream || streams.videoStreams.isEmpty()
} ?: let {
val manifest = DashHelper.createManifest(streams)
@ -1301,7 +1300,10 @@ class PlayerFragment : Fragment(R.layout.fragment_player), OnlinePlayerOptions {
}
// HLS
streams.hls != null -> {
setMediaSource(streams.hls!!.toUri(), MimeTypes.APPLICATION_M3U8)
setMediaSource(
ProxyHelper.unwrapIfEnabled(streams.hls!!).toUri(),
MimeTypes.APPLICATION_M3U8
)
}
// NO STREAM FOUND
else -> {

View File

@ -453,6 +453,10 @@
<string name="playing_next">Playing next in %1$s</string>
<string name="lbry_hls">LBRY HLS</string>
<string name="lbry_hls_summary">Use LBRY HLS for streaming if available.</string>
<string name="disable_proxy">Disable Piped proxy</string>
<string name="disable_proxy_summary">Load videos and images directly from YouTube\'s servers. Only enable the option if you use a VPN anyways! Note that this might not work with content from YT music.</string>
<!-- Notification channel strings -->
<string name="download_channel_name">Download Service</string>
<string name="download_channel_description">Shows a notification when downloading media.</string>

View File

@ -96,6 +96,13 @@
app:title="@string/codecs"
app:useSimpleSummaryProvider="true" />
<SwitchPreferenceCompat
android:defaultValue="false"
android:icon="@drawable/ic_server"
android:summary="@string/disable_proxy_summary"
android:title="@string/disable_proxy"
app:key="disable_video_image_proxy" />
</PreferenceCategory>
</PreferenceScreen>