Merge pull request #3241 from Bnyro/master

Support for LBRY HLS
This commit is contained in:
Bnyro 2023-03-05 12:37:59 +01:00 committed by GitHub
commit 76ec558f0a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 55 additions and 27 deletions

View File

@ -93,6 +93,7 @@ object PreferenceKeys {
const val SKIP_SILENCE = "skip_silence"
const val ENABLED_VIDEO_CODECS = "video_codecs"
const val AUTOPLAY_COUNTDOWN = "autoplay_countdown"
const val LBRY_HLS = "lbry_hls"
/**
* Background mode

View File

@ -40,14 +40,17 @@ object DashHelper {
val adapSetInfos = ArrayList<AdapSetInfo>()
val enabledVideoCodecs = PlayerHelper.enabledVideoCodecs
// filter the codecs according to the user's preferences
for (stream in streams.videoStreams.filter {
if (enabledVideoCodecs != "all") {
it.codec?.lowercase()?.startsWith(enabledVideoCodecs) ?: true
} else {
true
}
}) {
for (stream in streams.videoStreams
// used to avoid including LBRY HLS inside the streams in the manifest
.filter { !it.format.orEmpty().contains("HLS") }
// filter the codecs according to the user's preferences
.filter {
if (enabledVideoCodecs != "all") {
it.codec?.lowercase()?.startsWith(enabledVideoCodecs) ?: true
} else {
true
}
}) {
// ignore dual format streams
if (!stream.videoOnly!!) {
continue

View File

@ -81,7 +81,7 @@ class DownloadDialog(
val videoStreams = streams.videoStreams.filter {
!it.url.isNullOrEmpty()
}.sortedByDescending {
}.filter { !it.format.orEmpty().contains("HLS") }.sortedByDescending {
it.quality.getWhileDigit()
}

View File

@ -1272,26 +1272,40 @@ class PlayerFragment : Fragment(R.layout.fragment_player), OnlinePlayerOptions {
val defaultResolution = PlayerHelper.getDefaultResolution(requireContext()).replace("p", "")
if (defaultResolution.isNotEmpty()) setPlayerResolution(defaultResolution.toInt())
if (!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 {
streams.livestream || streams.videoStreams.isEmpty()
} ?: let {
val manifest = DashHelper.createManifest(streams)
// encode to base64
val encoded = Base64.encodeToString(manifest.toByteArray(), Base64.DEFAULT)
"data:application/dash+xml;charset=utf-8;base64,$encoded".toUri()
when {
// LBRY HLS
PreferenceHelper.getBoolean(PreferenceKeys.LBRY_HLS, false) && streams.videoStreams.any {
it.quality.orEmpty().contains("LBRY HLS")
} -> {
val lbryHlsUrl = streams.videoStreams.first {
it.quality!!.contains("LBRY HLS")
}.url!!
setMediaSource(lbryHlsUrl.toUri(), MimeTypes.APPLICATION_M3U8)
}
// 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 {
streams.livestream || streams.videoStreams.isEmpty()
} ?: let {
val manifest = DashHelper.createManifest(streams)
this.setMediaSource(uri, MimeTypes.APPLICATION_MPD)
} else if (streams.hls != null) {
setMediaSource(streams.hls!!.toUri(), MimeTypes.APPLICATION_M3U8)
} else {
Toast.makeText(context, R.string.unknown_error, Toast.LENGTH_SHORT).show()
// encode to base64
val encoded = Base64.encodeToString(manifest.toByteArray(), Base64.DEFAULT)
"data:application/dash+xml;charset=utf-8;base64,$encoded".toUri()
}
this.setMediaSource(uri, MimeTypes.APPLICATION_MPD)
}
// HLS
streams.hls != null -> {
setMediaSource(streams.hls!!.toUri(), MimeTypes.APPLICATION_M3U8)
}
// NO STREAM FOUND
else -> {
Toast.makeText(context, R.string.unknown_error, Toast.LENGTH_SHORT).show()
}
}
}

View File

@ -449,6 +449,9 @@
<string name="autoplay_countdown">Autoplay countdown</string>
<string name="autoplay_countdown_summary">Show a 5s countdown before auto-playing the next video.</string>
<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>
<!-- 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

@ -76,6 +76,13 @@
android:title="@string/hls_instead_of_dash"
app:key="use_hls" />
<SwitchPreferenceCompat
android:defaultValue="false"
android:icon="@drawable/ic_library"
android:summary="@string/lbry_hls_summary"
android:title="@string/lbry_hls"
app:key="lbry_hls" />
</PreferenceCategory>
<PreferenceCategory app:title="@string/advanced">