Add option to disable Piped proxy

This commit is contained in:
Bnyro 2023-03-11 16:11:43 +01:00
parent 3326305309
commit dba5dd048e
10 changed files with 53 additions and 19 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 kotlinx.datetime.LocalDate
import kotlinx.serialization.Serializable
@ -54,7 +55,7 @@ data class Streams(
videoId = videoId,
fileName = stream?.getQualityString(fileName).orEmpty(),
path = "",
url = stream?.url,
url = stream?.url?.let { ProxyHelper.unwrapIfEnabled(it) },
format = videoFormat,
quality = videoQuality
)
@ -71,7 +72,7 @@ data class Streams(
videoId = videoId,
fileName = stream?.getQualityString(fileName).orEmpty(),
path = "",
url = stream?.url,
url = stream?.url?.let { ProxyHelper.unwrapIfEnabled(it) },
format = audioFormat,
quality = audioQuality
)
@ -85,7 +86,9 @@ data class Streams(
videoId = videoId,
fileName = "${fileName}_$subtitleCode.srt",
path = "",
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

@ -51,7 +51,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: String) {

View File

@ -27,4 +27,17 @@ 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()
}
}

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

@ -116,7 +116,10 @@ class CommentsAdapter(
}
}
root.setOnLongClickListener {
ClipboardHelper.save(root.context, comment.commentText.orEmpty().parseAsHtml().toString())
ClipboardHelper.save(
root.context,
comment.commentText.orEmpty().parseAsHtml().toString()
)
Toast.makeText(root.context, R.string.copied, Toast.LENGTH_SHORT).show()
true
}

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)
@ -970,14 +971,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() })
@ -1285,7 +1284,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)
@ -1300,7 +1299,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,8 @@
<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!</string>
<!-- Notification channel strings -->
<string name="download_channel_name">Download Service</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="lbry_hls" />
</PreferenceCategory>
</PreferenceScreen>