Skip to content

Commit

Permalink
feat: prefer vp9.2 if user's device doesn't support av1 hardware deco…
Browse files Browse the repository at this point in the history
…ding
  • Loading branch information
JunkFood02 committed Dec 22, 2023
1 parent 1cc50f9 commit 72c2113
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 10 deletions.
3 changes: 3 additions & 0 deletions .idea/deploymentTargetSelector.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

50 changes: 40 additions & 10 deletions app/src/main/java/com/junkfood/seal/util/DownloadUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.junkfood.seal.util

import android.database.sqlite.SQLiteDatabase
import android.database.sqlite.SQLiteDatabase.OPEN_READONLY
import android.media.MediaCodecList
import android.os.Build
import android.util.Log
import android.webkit.CookieManager
Expand Down Expand Up @@ -32,6 +33,7 @@ import com.junkfood.seal.util.PreferenceUtil.COOKIE_HEADER
import com.junkfood.seal.util.PreferenceUtil.getBoolean
import com.junkfood.seal.util.PreferenceUtil.getInt
import com.junkfood.seal.util.PreferenceUtil.getString
import com.junkfood.seal.util.PreferenceUtil.updateBoolean
import com.yausername.youtubedl_android.YoutubeDL
import com.yausername.youtubedl_android.YoutubeDLRequest
import com.yausername.youtubedl_android.YoutubeDLResponse
Expand All @@ -40,17 +42,18 @@ import kotlinx.coroutines.withContext
import kotlinx.serialization.json.Json
import java.util.Locale

object CookieScheme {
const val NAME = "name"
const val VALUE = "value"
const val SECURE = "is_secure"
const val EXPIRY = "expires_utc"
const val HOST = "host_key"
const val PATH = "path"
}

object DownloadUtil {

object CookieScheme {
const val NAME = "name"
const val VALUE = "value"
const val SECURE = "is_secure"
const val EXPIRY = "expires_utc"
const val HOST = "host_key"
const val PATH = "path"
}

private val jsonFormat = Json {
ignoreUnknownKeys = true
}
Expand Down Expand Up @@ -209,7 +212,8 @@ object DownloadUtil {
val outputTemplate: String = OUTPUT_TEMPLATE.getString(),
val useDownloadArchive: Boolean = DOWNLOAD_ARCHIVE.getBoolean(),
val embedMetadata: Boolean = EMBED_METADATA.getBoolean(),
val restrictFilenames: Boolean = RESTRICT_FILENAMES.getBoolean()
val restrictFilenames: Boolean = RESTRICT_FILENAMES.getBoolean(),
val supportAv1HardwareDecoding: Boolean = checkIfAv1HardwareAccelerated()
)

private fun YoutubeDLRequest.enableCookies(userAgentString: String): YoutubeDLRequest =
Expand Down Expand Up @@ -336,7 +340,12 @@ object DownloadUtil {
private fun DownloadPreferences.toVideoFormatSorter(): String = this.run {
val format = when (videoFormat) {
FORMAT_COMPATIBILITY -> "proto,vcodec:h264,ext"
FORMAT_QUALITY -> "vcodec:av01"
FORMAT_QUALITY -> if (supportAv1HardwareDecoding) {
"vcodec:av01"
} else {
"vcodec:vp9.2"
}

else -> ""
}
val res = when (videoResolution) {
Expand Down Expand Up @@ -708,4 +717,25 @@ object DownloadUtil {
onProcessEnded()
}
}

private fun checkIfAv1HardwareAccelerated(): Boolean {
if (PreferenceUtil.containsKey(AV1_HARDWARE_ACCELERATED)) {
return AV1_HARDWARE_ACCELERATED.getBoolean()
} else {
val res = if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
false
} else {
MediaCodecList(MediaCodecList.REGULAR_CODECS).codecInfos.any { info ->
info.supportedTypes.any {
it.equals(
"video/av01",
ignoreCase = true
)
} && info.isHardwareAccelerated
}
}
AV1_HARDWARE_ACCELERATED.updateBoolean(res)
return res
}
}
}
1 change: 1 addition & 0 deletions app/src/main/java/com/junkfood/seal/util/PreferenceUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ const val CUSTOM_OUTPUT_TEMPLATE = "custom_output_template"
const val DOWNLOAD_ARCHIVE = "download_archive"
const val EMBED_METADATA = "embed_metadata"
const val RESTRICT_FILENAMES = "restrict_filenames"
const val AV1_HARDWARE_ACCELERATED = "av1_hardware_accelerated"

const val DEFAULT = 0
const val NOT_SPECIFIED = 0
Expand Down

0 comments on commit 72c2113

Please sign in to comment.