2023-01-31 21:13:39 +05:30
|
|
|
package com.github.libretube.helpers
|
2022-07-17 01:01:15 +05:30
|
|
|
|
2023-01-09 21:38:05 +05:30
|
|
|
import android.app.Activity
|
|
|
|
import android.app.PendingIntent
|
2022-07-17 01:01:15 +05:30
|
|
|
import android.content.Context
|
2023-01-09 21:38:05 +05:30
|
|
|
import android.content.Intent
|
2022-10-07 22:48:04 +05:30
|
|
|
import android.content.pm.ActivityInfo
|
2023-06-01 21:58:44 +05:30
|
|
|
import android.net.Uri
|
|
|
|
import android.util.Base64
|
2022-07-17 01:01:15 +05:30
|
|
|
import android.view.accessibility.CaptioningManager
|
2023-02-12 17:47:44 +05:30
|
|
|
import android.widget.Toast
|
2023-01-09 21:38:05 +05:30
|
|
|
import androidx.annotation.StringRes
|
2023-04-09 17:36:28 +05:30
|
|
|
import androidx.core.app.PendingIntentCompat
|
2023-02-03 17:09:08 +05:30
|
|
|
import androidx.core.app.RemoteActionCompat
|
2023-03-31 06:09:38 +05:30
|
|
|
import androidx.core.content.getSystemService
|
2023-02-03 17:09:08 +05:30
|
|
|
import androidx.core.graphics.drawable.IconCompat
|
2023-06-01 21:58:44 +05:30
|
|
|
import androidx.core.net.toUri
|
2023-05-15 20:51:49 +05:30
|
|
|
import androidx.media3.common.AudioAttributes
|
|
|
|
import androidx.media3.common.C
|
|
|
|
import androidx.media3.common.PlaybackParameters
|
2023-10-14 21:29:22 +05:30
|
|
|
import androidx.media3.common.Player
|
2023-07-16 21:01:17 +05:30
|
|
|
import androidx.media3.common.Tracks
|
2023-09-26 16:52:12 +05:30
|
|
|
import androidx.media3.datasource.DefaultDataSource
|
|
|
|
import androidx.media3.datasource.cronet.CronetDataSource
|
2023-05-15 20:51:49 +05:30
|
|
|
import androidx.media3.exoplayer.DefaultLoadControl
|
|
|
|
import androidx.media3.exoplayer.ExoPlayer
|
|
|
|
import androidx.media3.exoplayer.LoadControl
|
2023-09-26 16:52:12 +05:30
|
|
|
import androidx.media3.exoplayer.source.DefaultMediaSourceFactory
|
2023-07-19 13:39:05 +05:30
|
|
|
import androidx.media3.exoplayer.trackselection.DefaultTrackSelector
|
2023-05-15 20:51:49 +05:30
|
|
|
import androidx.media3.ui.CaptionStyleCompat
|
2023-08-22 22:18:27 +05:30
|
|
|
import com.github.libretube.LibreTubeApp
|
2023-01-09 21:38:05 +05:30
|
|
|
import com.github.libretube.R
|
2023-09-26 16:52:12 +05:30
|
|
|
import com.github.libretube.api.CronetHelper
|
2023-06-19 16:26:53 +05:30
|
|
|
import com.github.libretube.api.obj.ChapterSegment
|
2023-02-12 17:47:44 +05:30
|
|
|
import com.github.libretube.api.obj.Segment
|
2023-06-01 21:58:44 +05:30
|
|
|
import com.github.libretube.api.obj.Streams
|
2022-09-20 23:30:51 +05:30
|
|
|
import com.github.libretube.constants.PreferenceKeys
|
2023-07-11 15:45:44 +05:30
|
|
|
import com.github.libretube.db.DatabaseHolder
|
2023-01-09 21:38:05 +05:30
|
|
|
import com.github.libretube.enums.PlayerEvent
|
2023-06-19 16:10:47 +05:30
|
|
|
import com.github.libretube.enums.SbSkipOptions
|
2023-07-19 13:39:05 +05:30
|
|
|
import com.github.libretube.extensions.updateParameters
|
2023-09-13 19:23:15 +05:30
|
|
|
import com.github.libretube.obj.VideoStats
|
|
|
|
import com.github.libretube.util.TextUtils
|
2023-07-16 21:01:17 +05:30
|
|
|
import java.util.Locale
|
2023-09-26 16:52:12 +05:30
|
|
|
import java.util.concurrent.Executors
|
2023-02-12 17:57:35 +05:30
|
|
|
import kotlin.math.absoluteValue
|
2022-10-07 23:09:41 +05:30
|
|
|
import kotlin.math.roundToInt
|
2023-10-11 21:43:32 +05:30
|
|
|
import kotlinx.coroutines.runBlocking
|
2022-07-17 01:01:15 +05:30
|
|
|
|
|
|
|
object PlayerHelper {
|
2023-01-09 21:38:05 +05:30
|
|
|
private const val ACTION_MEDIA_CONTROL = "media_control"
|
|
|
|
const val CONTROL_TYPE = "control_type"
|
2023-06-24 23:08:39 +05:30
|
|
|
const val SPONSOR_HIGHLIGHT_CATEGORY = "poi_highlight"
|
2023-08-16 21:25:32 +05:30
|
|
|
const val ROLE_FLAG_AUTO_GEN_SUBTITLE = C.ROLE_FLAG_SUPPLEMENTARY
|
2023-09-21 13:43:20 +05:30
|
|
|
const val MINIMUM_BUFFER_DURATION = 1000 * 10 // exo default is 50s
|
2023-01-09 21:38:05 +05:30
|
|
|
|
2023-10-14 21:29:22 +05:30
|
|
|
val repeatModes = listOf(
|
|
|
|
Player.REPEAT_MODE_OFF to R.string.repeat_mode_none,
|
|
|
|
Player.REPEAT_MODE_ONE to R.string.repeat_mode_current,
|
|
|
|
Player.REPEAT_MODE_ALL to R.string.repeat_mode_all
|
|
|
|
)
|
|
|
|
|
2023-09-18 12:43:33 +05:30
|
|
|
/**
|
|
|
|
* A list of all categories that are not disabled by default
|
|
|
|
* Also update `sponsorblock_settings.xml` when modifying this!
|
|
|
|
*/
|
|
|
|
private val sbDefaultValues = mapOf(
|
|
|
|
"sponsor" to SbSkipOptions.AUTOMATIC,
|
|
|
|
"selfpromo" to SbSkipOptions.AUTOMATIC
|
|
|
|
)
|
|
|
|
|
2023-01-08 20:36:29 +05:30
|
|
|
/**
|
2023-06-01 21:58:44 +05:30
|
|
|
* Create a base64 encoded DASH stream manifest
|
2023-01-08 20:36:29 +05:30
|
|
|
*/
|
2023-10-11 21:43:32 +05:30
|
|
|
fun createDashSource(streams: Streams, context: Context, disableProxy: Boolean): Uri {
|
2023-06-19 23:41:12 +05:30
|
|
|
val manifest = DashHelper.createManifest(
|
|
|
|
streams,
|
|
|
|
DisplayHelper.supportsHdr(context),
|
|
|
|
disableProxy
|
|
|
|
)
|
2023-06-01 21:58:44 +05:30
|
|
|
|
|
|
|
// encode to base64
|
|
|
|
val encoded = Base64.encodeToString(manifest.toByteArray(), Base64.DEFAULT)
|
|
|
|
return "data:application/dash+xml;charset=utf-8;base64,$encoded".toUri()
|
2022-07-17 01:01:15 +05:30
|
|
|
}
|
|
|
|
|
2023-01-08 20:36:29 +05:30
|
|
|
/**
|
2023-06-01 21:58:44 +05:30
|
|
|
* Get the system's default captions style
|
2023-01-08 20:36:29 +05:30
|
|
|
*/
|
2023-05-15 20:51:49 +05:30
|
|
|
@androidx.annotation.OptIn(androidx.media3.common.util.UnstableApi::class)
|
2022-07-17 01:01:15 +05:30
|
|
|
fun getCaptionStyle(context: Context): CaptionStyleCompat {
|
2023-03-31 06:09:38 +05:30
|
|
|
val captioningManager = context.getSystemService<CaptioningManager>()!!
|
2022-07-17 01:01:15 +05:30
|
|
|
return if (!captioningManager.isEnabled) {
|
|
|
|
// system captions are disabled, using android default captions style
|
|
|
|
CaptionStyleCompat.DEFAULT
|
|
|
|
} else {
|
|
|
|
// system captions are enabled
|
|
|
|
CaptionStyleCompat.createFromCaptionStyle(captioningManager.userStyle)
|
|
|
|
}
|
|
|
|
}
|
2022-08-02 14:47:15 +05:30
|
|
|
|
2023-03-24 20:32:56 +05:30
|
|
|
fun getOrientation(videoWidth: Int, videoHeight: Int): Int {
|
2022-10-07 22:48:04 +05:30
|
|
|
val fullscreenOrientationPref = PreferenceHelper.getString(
|
|
|
|
PreferenceKeys.FULLSCREEN_ORIENTATION,
|
2023-06-24 23:27:00 +05:30
|
|
|
"ratio"
|
2022-10-07 22:48:04 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
return when (fullscreenOrientationPref) {
|
|
|
|
"ratio" -> {
|
|
|
|
// probably a youtube shorts video
|
2023-03-24 20:32:56 +05:30
|
|
|
if (videoHeight > videoWidth) {
|
2022-10-07 22:48:04 +05:30
|
|
|
ActivityInfo.SCREEN_ORIENTATION_USER_PORTRAIT
|
|
|
|
} // a video with normal aspect ratio
|
|
|
|
else {
|
|
|
|
ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE
|
|
|
|
}
|
|
|
|
}
|
2023-06-24 22:27:33 +05:30
|
|
|
|
2022-10-07 22:48:04 +05:30
|
|
|
"auto" -> ActivityInfo.SCREEN_ORIENTATION_SENSOR
|
|
|
|
"landscape" -> ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE
|
|
|
|
"portrait" -> ActivityInfo.SCREEN_ORIENTATION_USER_PORTRAIT
|
|
|
|
else -> ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE
|
|
|
|
}
|
|
|
|
}
|
2022-10-07 23:00:59 +05:30
|
|
|
|
2023-08-06 16:09:52 +05:30
|
|
|
val autoFullscreenEnabled: Boolean
|
2022-10-07 23:00:59 +05:30
|
|
|
get() = PreferenceHelper.getBoolean(
|
|
|
|
PreferenceKeys.AUTO_FULLSCREEN,
|
2023-06-24 23:27:00 +05:30
|
|
|
false
|
2022-10-07 23:00:59 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
val relatedStreamsEnabled: Boolean
|
|
|
|
get() = PreferenceHelper.getBoolean(
|
|
|
|
PreferenceKeys.RELATED_STREAMS,
|
2023-06-24 23:27:00 +05:30
|
|
|
true
|
2022-10-07 23:00:59 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
val pausePlayerOnScreenOffEnabled: Boolean
|
|
|
|
get() = PreferenceHelper.getBoolean(
|
|
|
|
PreferenceKeys.PAUSE_ON_SCREEN_OFF,
|
2023-06-24 23:27:00 +05:30
|
|
|
false
|
2022-10-07 23:00:59 +05:30
|
|
|
)
|
|
|
|
|
2023-02-12 17:25:31 +05:30
|
|
|
private val watchPositionsPref: String
|
|
|
|
get() = PreferenceHelper.getString(
|
|
|
|
PreferenceKeys.WATCH_POSITIONS,
|
2023-06-24 23:27:00 +05:30
|
|
|
"always"
|
2022-10-07 23:00:59 +05:30
|
|
|
)
|
|
|
|
|
2023-02-12 17:25:31 +05:30
|
|
|
val watchPositionsVideo: Boolean
|
|
|
|
get() = watchPositionsPref in listOf("always", "videos")
|
|
|
|
|
|
|
|
val watchPositionsAudio: Boolean
|
|
|
|
get() = watchPositionsPref == "always"
|
|
|
|
|
2022-10-07 23:00:59 +05:30
|
|
|
val watchHistoryEnabled: Boolean
|
|
|
|
get() = PreferenceHelper.getBoolean(
|
|
|
|
PreferenceKeys.WATCH_HISTORY_TOGGLE,
|
2023-06-24 23:27:00 +05:30
|
|
|
true
|
2022-10-07 23:00:59 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
val useSystemCaptionStyle: Boolean
|
|
|
|
get() = PreferenceHelper.getBoolean(
|
|
|
|
PreferenceKeys.SYSTEM_CAPTION_STYLE,
|
2023-06-24 23:27:00 +05:30
|
|
|
true
|
2022-10-07 23:00:59 +05:30
|
|
|
)
|
|
|
|
|
2023-01-19 22:18:35 +05:30
|
|
|
private val bufferingGoal: Int
|
2022-10-07 23:00:59 +05:30
|
|
|
get() = PreferenceHelper.getString(
|
|
|
|
PreferenceKeys.BUFFERING_GOAL,
|
2023-06-24 23:27:00 +05:30
|
|
|
"50"
|
2022-10-07 23:00:59 +05:30
|
|
|
).toInt() * 1000
|
|
|
|
|
|
|
|
val sponsorBlockEnabled: Boolean
|
|
|
|
get() = PreferenceHelper.getBoolean(
|
|
|
|
"sb_enabled_key",
|
2023-06-24 23:27:00 +05:30
|
|
|
true
|
2022-10-07 23:00:59 +05:30
|
|
|
)
|
|
|
|
|
2023-02-12 17:47:44 +05:30
|
|
|
private val sponsorBlockNotifications: Boolean
|
2022-10-07 23:00:59 +05:30
|
|
|
get() = PreferenceHelper.getBoolean(
|
|
|
|
"sb_notifications_key",
|
2023-06-24 23:27:00 +05:30
|
|
|
true
|
2022-10-07 23:00:59 +05:30
|
|
|
)
|
|
|
|
|
2023-06-24 23:08:39 +05:30
|
|
|
private val sponsorBlockHighlights: Boolean
|
|
|
|
get() = PreferenceHelper.getBoolean(
|
|
|
|
PreferenceKeys.SB_HIGHLIGHTS,
|
|
|
|
true
|
|
|
|
)
|
|
|
|
|
2022-12-22 15:47:34 +05:30
|
|
|
val defaultSubtitleCode: String?
|
2022-10-07 23:00:59 +05:30
|
|
|
get() {
|
|
|
|
val code = PreferenceHelper.getString(
|
|
|
|
PreferenceKeys.DEFAULT_SUBTITLE,
|
2023-06-24 23:27:00 +05:30
|
|
|
""
|
2022-10-07 23:00:59 +05:30
|
|
|
)
|
|
|
|
|
2022-12-22 15:47:34 +05:30
|
|
|
if (code == "") return null
|
|
|
|
|
2022-10-07 23:00:59 +05:30
|
|
|
if (code.contains("-")) {
|
|
|
|
return code.split("-")[0]
|
|
|
|
}
|
|
|
|
return code
|
|
|
|
}
|
|
|
|
|
|
|
|
val skipButtonsEnabled: Boolean
|
|
|
|
get() = PreferenceHelper.getBoolean(
|
|
|
|
PreferenceKeys.SKIP_BUTTONS,
|
2023-06-24 23:27:00 +05:30
|
|
|
false
|
2022-10-07 23:00:59 +05:30
|
|
|
)
|
|
|
|
|
2022-10-19 23:23:18 +05:30
|
|
|
val pipEnabled: Boolean
|
|
|
|
get() = PreferenceHelper.getBoolean(
|
|
|
|
PreferenceKeys.PICTURE_IN_PICTURE,
|
2023-06-24 23:27:00 +05:30
|
|
|
true
|
2022-10-19 23:23:18 +05:30
|
|
|
)
|
2022-10-07 23:00:59 +05:30
|
|
|
|
2023-06-25 13:50:06 +05:30
|
|
|
var autoPlayEnabled: Boolean
|
2022-10-07 23:09:41 +05:30
|
|
|
get() = PreferenceHelper.getBoolean(
|
2023-06-25 13:50:06 +05:30
|
|
|
PreferenceKeys.AUTOPLAY,
|
2023-06-24 23:27:00 +05:30
|
|
|
true
|
2022-10-07 23:09:41 +05:30
|
|
|
)
|
2023-06-25 13:50:06 +05:30
|
|
|
set(value) {
|
|
|
|
PreferenceHelper.putBoolean(PreferenceKeys.AUTOPLAY, value)
|
|
|
|
}
|
2022-10-07 23:09:41 +05:30
|
|
|
|
2023-02-22 16:57:03 +05:30
|
|
|
val autoPlayCountdown: Boolean
|
|
|
|
get() = PreferenceHelper.getBoolean(
|
|
|
|
PreferenceKeys.AUTOPLAY_COUNTDOWN,
|
2023-06-24 23:27:00 +05:30
|
|
|
false
|
2023-02-22 16:57:03 +05:30
|
|
|
)
|
|
|
|
|
2022-10-07 23:09:41 +05:30
|
|
|
val seekIncrement: Long
|
|
|
|
get() = PreferenceHelper.getString(
|
|
|
|
PreferenceKeys.SEEK_INCREMENT,
|
2023-06-24 23:27:00 +05:30
|
|
|
"10.0"
|
2022-10-07 23:09:41 +05:30
|
|
|
).toFloat()
|
|
|
|
.roundToInt()
|
|
|
|
.toLong() * 1000
|
|
|
|
|
2023-09-18 12:43:33 +05:30
|
|
|
private val playbackSpeed: Float
|
2022-10-07 23:09:41 +05:30
|
|
|
get() = PreferenceHelper.getString(
|
|
|
|
PreferenceKeys.PLAYBACK_SPEED,
|
2023-06-24 23:27:00 +05:30
|
|
|
"1"
|
2023-02-17 00:53:46 +05:30
|
|
|
).replace("F", "").toFloat()
|
|
|
|
|
|
|
|
private val backgroundSpeed: Float
|
|
|
|
get() = when (PreferenceHelper.getBoolean(PreferenceKeys.CUSTOM_PLAYBACK_SPEED, false)) {
|
2023-06-24 22:27:33 +05:30
|
|
|
true -> PreferenceHelper.getString(PreferenceKeys.BACKGROUND_PLAYBACK_SPEED, "1")
|
|
|
|
.toFloat()
|
|
|
|
|
2023-02-17 00:53:46 +05:30
|
|
|
else -> playbackSpeed
|
|
|
|
}
|
2022-10-07 23:09:41 +05:30
|
|
|
|
|
|
|
val resizeModePref: String
|
|
|
|
get() = PreferenceHelper.getString(
|
|
|
|
PreferenceKeys.PLAYER_RESIZE_MODE,
|
2023-06-24 23:27:00 +05:30
|
|
|
"fit"
|
2022-10-07 23:09:41 +05:30
|
|
|
)
|
|
|
|
|
2022-11-22 21:29:24 +05:30
|
|
|
val autoInsertRelatedVideos: Boolean
|
|
|
|
get() = PreferenceHelper.getBoolean(
|
|
|
|
PreferenceKeys.QUEUE_AUTO_INSERT_RELATED,
|
2023-06-24 23:27:00 +05:30
|
|
|
true
|
2022-11-22 21:29:24 +05:30
|
|
|
)
|
|
|
|
|
2022-11-25 16:48:37 +05:30
|
|
|
val swipeGestureEnabled: Boolean
|
|
|
|
get() = PreferenceHelper.getBoolean(
|
2022-11-25 20:23:41 +05:30
|
|
|
PreferenceKeys.PLAYER_SWIPE_CONTROLS,
|
2023-06-24 23:27:00 +05:30
|
|
|
true
|
2022-11-25 16:48:37 +05:30
|
|
|
)
|
|
|
|
|
2023-04-10 18:23:28 +05:30
|
|
|
val fullscreenGesturesEnabled: Boolean
|
|
|
|
get() = PreferenceHelper.getBoolean(
|
|
|
|
PreferenceKeys.FULLSCREEN_GESTURES,
|
2023-06-24 23:27:00 +05:30
|
|
|
false
|
2023-04-10 18:23:28 +05:30
|
|
|
)
|
|
|
|
|
2022-12-01 23:03:30 +05:30
|
|
|
val pinchGestureEnabled: Boolean
|
|
|
|
get() = PreferenceHelper.getBoolean(
|
|
|
|
PreferenceKeys.PLAYER_PINCH_CONTROL,
|
2023-06-24 23:27:00 +05:30
|
|
|
true
|
2022-12-01 23:03:30 +05:30
|
|
|
)
|
|
|
|
|
2022-11-27 22:20:09 +05:30
|
|
|
val captionsTextSize: Float
|
|
|
|
get() = PreferenceHelper.getString(
|
|
|
|
PreferenceKeys.CAPTIONS_SIZE,
|
2023-06-24 23:27:00 +05:30
|
|
|
"18"
|
2022-11-27 22:20:09 +05:30
|
|
|
).toFloat()
|
|
|
|
|
2022-11-27 23:11:34 +05:30
|
|
|
val doubleTapToSeek: Boolean
|
|
|
|
get() = PreferenceHelper.getBoolean(
|
|
|
|
PreferenceKeys.DOUBLE_TAP_TO_SEEK,
|
2023-06-24 23:27:00 +05:30
|
|
|
true
|
2022-11-27 23:11:34 +05:30
|
|
|
)
|
|
|
|
|
2022-12-27 23:18:09 +05:30
|
|
|
val pauseOnQuit: Boolean
|
|
|
|
get() = PreferenceHelper.getBoolean(
|
|
|
|
PreferenceKeys.PAUSE_ON_QUIT,
|
2023-06-24 23:27:00 +05:30
|
|
|
false
|
2022-12-27 23:18:09 +05:30
|
|
|
)
|
|
|
|
|
2023-01-19 22:18:35 +05:30
|
|
|
private val alternativePiPControls: Boolean
|
2023-01-09 22:08:17 +05:30
|
|
|
get() = PreferenceHelper.getBoolean(
|
|
|
|
PreferenceKeys.ALTERNATIVE_PIP_CONTROLS,
|
2023-06-24 23:27:00 +05:30
|
|
|
false
|
2023-01-09 22:08:17 +05:30
|
|
|
)
|
|
|
|
|
2023-01-21 16:37:28 +05:30
|
|
|
private val skipSilence: Boolean
|
|
|
|
get() = PreferenceHelper.getBoolean(
|
|
|
|
PreferenceKeys.SKIP_SILENCE,
|
2023-06-24 23:27:00 +05:30
|
|
|
false
|
2023-01-21 16:37:28 +05:30
|
|
|
)
|
|
|
|
|
2023-02-08 14:11:59 +05:30
|
|
|
val enabledVideoCodecs: String
|
|
|
|
get() = PreferenceHelper.getString(
|
|
|
|
PreferenceKeys.ENABLED_VIDEO_CODECS,
|
2023-06-24 23:27:00 +05:30
|
|
|
"all"
|
2023-02-08 14:11:59 +05:30
|
|
|
)
|
|
|
|
|
2023-08-04 22:08:45 +05:30
|
|
|
val playAutomatically: Boolean
|
|
|
|
get() = PreferenceHelper
|
|
|
|
.getBoolean(
|
|
|
|
PreferenceKeys.PLAY_AUTOMATICALLY,
|
|
|
|
true
|
|
|
|
)
|
|
|
|
|
2023-10-05 17:49:19 +05:30
|
|
|
fun shouldPlayNextVideo(isPlaylist: Boolean = false): Boolean {
|
2023-10-11 21:43:32 +05:30
|
|
|
return autoPlayEnabled || (
|
|
|
|
isPlaylist && PreferenceHelper.getBoolean(
|
|
|
|
PreferenceKeys.AUTOPLAY_PLAYLISTS,
|
|
|
|
false
|
|
|
|
)
|
|
|
|
)
|
2023-10-05 17:49:19 +05:30
|
|
|
}
|
|
|
|
|
2023-09-26 17:03:28 +05:30
|
|
|
private val handleAudioFocus
|
|
|
|
get() = !PreferenceHelper.getBoolean(
|
|
|
|
PreferenceKeys.ALLOW_PLAYBACK_DURING_CALL,
|
|
|
|
false
|
|
|
|
)
|
|
|
|
|
2023-09-25 14:14:36 +05:30
|
|
|
fun getDefaultResolution(context: Context, isFullscreen: Boolean): Int? {
|
|
|
|
var prefKey = if (NetworkHelper.isNetworkMetered(context)) {
|
2023-01-21 17:44:06 +05:30
|
|
|
PreferenceKeys.DEFAULT_RESOLUTION_MOBILE
|
2022-10-07 23:00:59 +05:30
|
|
|
} else {
|
2023-01-21 17:44:06 +05:30
|
|
|
PreferenceKeys.DEFAULT_RESOLUTION
|
2022-10-07 23:00:59 +05:30
|
|
|
}
|
2023-09-25 14:14:36 +05:30
|
|
|
if (!isFullscreen) prefKey += "_no_fullscreen"
|
|
|
|
|
2023-01-21 17:44:06 +05:30
|
|
|
return PreferenceHelper.getString(prefKey, "")
|
2023-09-25 14:14:36 +05:30
|
|
|
.replace("p", "")
|
|
|
|
.toIntOrNull()
|
2022-10-07 23:00:59 +05:30
|
|
|
}
|
2023-01-09 21:38:05 +05:30
|
|
|
|
2023-07-19 13:39:05 +05:30
|
|
|
/**
|
|
|
|
* Apply the preferred audio quality: auto or worst
|
|
|
|
*/
|
|
|
|
@androidx.annotation.OptIn(androidx.media3.common.util.UnstableApi::class)
|
|
|
|
fun applyPreferredAudioQuality(context: Context, trackSelector: DefaultTrackSelector) {
|
|
|
|
val prefKey = if (NetworkHelper.isNetworkMetered(context)) {
|
|
|
|
PreferenceKeys.PLAYER_AUDIO_QUALITY_MOBILE
|
|
|
|
} else {
|
|
|
|
PreferenceKeys.PLAYER_AUDIO_QUALITY
|
|
|
|
}
|
|
|
|
when (PreferenceHelper.getString(prefKey, "auto")) {
|
|
|
|
"worst" -> trackSelector.updateParameters {
|
|
|
|
setMaxAudioBitrate(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-20 07:30:59 +05:30
|
|
|
fun getIntentAction(context: Context): String {
|
2023-01-09 21:38:05 +05:30
|
|
|
return context.packageName + "." + ACTION_MEDIA_CONTROL
|
|
|
|
}
|
|
|
|
|
2023-08-20 07:30:59 +05:30
|
|
|
private fun getPendingIntent(activity: Activity, event: PlayerEvent): PendingIntent {
|
|
|
|
val intent = Intent(getIntentAction(activity)).putExtra(CONTROL_TYPE, event)
|
2023-09-07 19:28:52 +05:30
|
|
|
return PendingIntentCompat.getBroadcast(activity, event.ordinal, intent, 0, false)!!
|
2023-01-09 21:38:05 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
private fun getRemoteAction(
|
|
|
|
activity: Activity,
|
|
|
|
id: Int,
|
|
|
|
@StringRes title: Int,
|
2023-06-24 23:27:00 +05:30
|
|
|
event: PlayerEvent
|
2023-02-03 17:09:08 +05:30
|
|
|
): RemoteActionCompat {
|
2023-01-09 21:38:05 +05:30
|
|
|
val text = activity.getString(title)
|
2023-02-03 17:09:08 +05:30
|
|
|
return RemoteActionCompat(
|
|
|
|
IconCompat.createWithResource(activity, id),
|
2023-01-09 21:38:05 +05:30
|
|
|
text,
|
|
|
|
text,
|
2023-08-20 07:30:59 +05:30
|
|
|
getPendingIntent(activity, event)
|
2023-01-09 21:38:05 +05:30
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-01-09 22:08:17 +05:30
|
|
|
/**
|
|
|
|
* Create controls to use in the PiP window
|
|
|
|
*/
|
2023-02-03 17:09:08 +05:30
|
|
|
fun getPiPModeActions(activity: Activity, isPlaying: Boolean): List<RemoteActionCompat> {
|
2023-01-15 17:56:16 +05:30
|
|
|
val audioModeAction = getRemoteAction(
|
|
|
|
activity,
|
|
|
|
R.drawable.ic_headphones,
|
|
|
|
R.string.background_mode,
|
2023-06-24 23:27:00 +05:30
|
|
|
PlayerEvent.Background
|
2023-01-09 21:38:05 +05:30
|
|
|
)
|
|
|
|
|
2023-01-15 17:56:16 +05:30
|
|
|
val rewindAction = getRemoteAction(
|
|
|
|
activity,
|
|
|
|
R.drawable.ic_rewind,
|
|
|
|
R.string.rewind,
|
2023-06-24 23:27:00 +05:30
|
|
|
PlayerEvent.Rewind
|
2023-01-09 21:38:05 +05:30
|
|
|
)
|
|
|
|
|
2023-01-15 17:56:16 +05:30
|
|
|
val playPauseAction = getRemoteAction(
|
|
|
|
activity,
|
|
|
|
if (isPlaying) R.drawable.ic_pause else R.drawable.ic_play,
|
|
|
|
R.string.pause,
|
2023-06-24 23:27:00 +05:30
|
|
|
if (isPlaying) PlayerEvent.Pause else PlayerEvent.Play
|
2023-01-15 17:56:16 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
val skipNextAction = getRemoteAction(
|
|
|
|
activity,
|
|
|
|
R.drawable.ic_next,
|
|
|
|
R.string.play_next,
|
2023-06-24 23:27:00 +05:30
|
|
|
PlayerEvent.Next
|
2023-01-09 21:38:05 +05:30
|
|
|
)
|
2023-01-15 17:56:16 +05:30
|
|
|
|
|
|
|
val forwardAction = getRemoteAction(
|
|
|
|
activity,
|
|
|
|
R.drawable.ic_forward,
|
|
|
|
R.string.forward,
|
2023-06-24 23:27:00 +05:30
|
|
|
PlayerEvent.Forward
|
2023-01-15 17:56:16 +05:30
|
|
|
)
|
2023-02-03 17:09:08 +05:30
|
|
|
return if (alternativePiPControls) {
|
|
|
|
listOf(audioModeAction, playPauseAction, skipNextAction)
|
2023-01-15 17:56:16 +05:30
|
|
|
} else {
|
2023-02-03 17:09:08 +05:30
|
|
|
listOf(rewindAction, playPauseAction, forwardAction)
|
2023-01-15 17:56:16 +05:30
|
|
|
}
|
2023-01-09 21:38:05 +05:30
|
|
|
}
|
2023-01-19 22:18:35 +05:30
|
|
|
|
2023-09-26 16:52:12 +05:30
|
|
|
/**
|
|
|
|
* Create a basic player, that is used for all types of playback situations inside the app
|
|
|
|
*/
|
|
|
|
@androidx.annotation.OptIn(androidx.media3.common.util.UnstableApi::class)
|
2023-10-06 21:07:20 +05:30
|
|
|
fun createPlayer(
|
|
|
|
context: Context,
|
|
|
|
trackSelector: DefaultTrackSelector,
|
|
|
|
isBackgroundMode: Boolean
|
|
|
|
): ExoPlayer {
|
2023-09-26 16:52:12 +05:30
|
|
|
val cronetDataSourceFactory = CronetDataSource.Factory(
|
|
|
|
CronetHelper.cronetEngine,
|
|
|
|
Executors.newCachedThreadPool()
|
|
|
|
)
|
|
|
|
val dataSourceFactory = DefaultDataSource.Factory(context, cronetDataSourceFactory)
|
2023-09-26 17:03:28 +05:30
|
|
|
val audioAttributes = AudioAttributes.Builder()
|
|
|
|
.setUsage(C.USAGE_MEDIA)
|
|
|
|
.setContentType(C.AUDIO_CONTENT_TYPE_MOVIE)
|
|
|
|
.build()
|
2023-09-26 16:52:12 +05:30
|
|
|
|
|
|
|
return ExoPlayer.Builder(context)
|
|
|
|
.setUsePlatformDiagnostics(false)
|
|
|
|
.setMediaSourceFactory(DefaultMediaSourceFactory(dataSourceFactory))
|
|
|
|
.setTrackSelector(trackSelector)
|
|
|
|
.setHandleAudioBecomingNoisy(true)
|
|
|
|
.setLoadControl(getLoadControl())
|
2023-09-26 17:03:28 +05:30
|
|
|
.setAudioAttributes(audioAttributes, handleAudioFocus)
|
2023-09-26 16:52:12 +05:30
|
|
|
.setUsePlatformDiagnostics(false)
|
|
|
|
.build()
|
|
|
|
.apply {
|
|
|
|
loadPlaybackParams(isBackgroundMode)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-19 22:18:35 +05:30
|
|
|
/**
|
|
|
|
* Get the load controls for the player (buffering, etc)
|
|
|
|
*/
|
2023-05-15 20:51:49 +05:30
|
|
|
@androidx.annotation.OptIn(androidx.media3.common.util.UnstableApi::class)
|
2023-01-19 22:18:35 +05:30
|
|
|
fun getLoadControl(): LoadControl {
|
|
|
|
return DefaultLoadControl.Builder()
|
|
|
|
// cache the last three minutes
|
|
|
|
.setBackBuffer(1000 * 60 * 3, true)
|
|
|
|
.setBufferDurationsMs(
|
2023-09-21 13:41:05 +05:30
|
|
|
MINIMUM_BUFFER_DURATION,
|
2023-01-19 22:18:35 +05:30
|
|
|
bufferingGoal,
|
|
|
|
DefaultLoadControl.DEFAULT_BUFFER_FOR_PLAYBACK_MS,
|
2023-06-24 23:27:00 +05:30
|
|
|
DefaultLoadControl.DEFAULT_BUFFER_FOR_PLAYBACK_AFTER_REBUFFER_MS
|
2023-01-19 22:18:35 +05:30
|
|
|
)
|
|
|
|
.build()
|
|
|
|
}
|
2023-01-21 16:37:28 +05:30
|
|
|
|
|
|
|
/**
|
|
|
|
* Load playback parameters such as speed and skip silence
|
|
|
|
*/
|
2023-05-15 20:51:49 +05:30
|
|
|
@androidx.annotation.OptIn(androidx.media3.common.util.UnstableApi::class)
|
2023-02-17 00:53:46 +05:30
|
|
|
fun ExoPlayer.loadPlaybackParams(isBackgroundMode: Boolean = false): ExoPlayer {
|
2023-01-21 16:37:28 +05:30
|
|
|
skipSilenceEnabled = skipSilence
|
2023-02-17 00:53:46 +05:30
|
|
|
val speed = if (isBackgroundMode) backgroundSpeed else playbackSpeed
|
|
|
|
playbackParameters = PlaybackParameters(speed, 1.0f)
|
2023-01-21 16:37:28 +05:30
|
|
|
return this
|
|
|
|
}
|
2023-02-12 17:47:44 +05:30
|
|
|
|
2023-06-24 23:08:39 +05:30
|
|
|
/**
|
|
|
|
* get the categories for sponsorBlock
|
|
|
|
*/
|
|
|
|
fun getSponsorBlockCategories(): MutableMap<String, SbSkipOptions> {
|
|
|
|
val categories: MutableMap<String, SbSkipOptions> = mutableMapOf()
|
|
|
|
|
2023-08-22 22:18:27 +05:30
|
|
|
for (category in LibreTubeApp.instance.resources.getStringArray(
|
|
|
|
R.array.sponsorBlockSegments
|
|
|
|
)) {
|
2023-09-18 12:43:33 +05:30
|
|
|
val defaultCategoryValue = sbDefaultValues.getOrDefault(category, SbSkipOptions.OFF)
|
|
|
|
val skipOption = PreferenceHelper
|
|
|
|
.getString("${category}_category", defaultCategoryValue.name)
|
|
|
|
.let { SbSkipOptions.valueOf(it.uppercase()) }
|
|
|
|
|
|
|
|
if (skipOption != SbSkipOptions.OFF) {
|
|
|
|
categories[category] = skipOption
|
2023-06-24 23:08:39 +05:30
|
|
|
}
|
|
|
|
}
|
2023-09-18 12:43:33 +05:30
|
|
|
|
2023-06-24 23:08:39 +05:30
|
|
|
// Add the highlights category to display in the chapters
|
|
|
|
if (sponsorBlockHighlights) categories[SPONSOR_HIGHLIGHT_CATEGORY] = SbSkipOptions.OFF
|
|
|
|
return categories
|
|
|
|
}
|
|
|
|
|
2023-02-12 17:47:44 +05:30
|
|
|
/**
|
|
|
|
* Check for SponsorBlock segments matching the current player position
|
|
|
|
* @param context A main dispatcher context
|
|
|
|
* @param segments List of the SponsorBlock segments
|
2023-06-24 22:27:33 +05:30
|
|
|
* @return If segment found and should skip manually, the end position of the segment in ms, otherwise null
|
2023-02-12 17:47:44 +05:30
|
|
|
*/
|
2023-02-12 17:57:35 +05:30
|
|
|
fun ExoPlayer.checkForSegments(
|
|
|
|
context: Context,
|
|
|
|
segments: List<Segment>,
|
2023-06-24 23:27:00 +05:30
|
|
|
sponsorBlockConfig: MutableMap<String, SbSkipOptions>
|
2023-08-22 19:35:21 +05:30
|
|
|
): Segment? {
|
2023-06-24 23:08:39 +05:30
|
|
|
for (segment in segments.filter { it.category != SPONSOR_HIGHLIGHT_CATEGORY }) {
|
2023-07-04 06:47:50 +05:30
|
|
|
val (start, end) = segment.segmentStartAndEnd
|
|
|
|
val (segmentStart, segmentEnd) = (start * 1000f).toLong() to (end * 1000f).toLong()
|
2023-02-12 17:57:35 +05:30
|
|
|
|
|
|
|
// avoid seeking to the same segment multiple times, e.g. when the SB segment is at the end of the video
|
|
|
|
if ((duration - currentPosition).absoluteValue < 500) continue
|
|
|
|
|
2023-02-12 17:47:44 +05:30
|
|
|
if (currentPosition in segmentStart until segmentEnd) {
|
2023-08-22 19:35:21 +05:30
|
|
|
if (sponsorBlockConfig[segment.category] == SbSkipOptions.AUTOMATIC ||
|
|
|
|
(
|
2023-10-11 21:43:32 +05:30
|
|
|
sponsorBlockConfig[segment.category] == SbSkipOptions.AUTOMATIC_ONCE &&
|
|
|
|
!segment.skipped
|
|
|
|
)
|
2023-08-22 19:35:21 +05:30
|
|
|
) {
|
2023-02-12 17:47:44 +05:30
|
|
|
if (sponsorBlockNotifications) {
|
|
|
|
runCatching {
|
|
|
|
Toast.makeText(context, R.string.segment_skipped, Toast.LENGTH_SHORT)
|
|
|
|
.show()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
seekTo(segmentEnd)
|
2023-08-22 19:35:21 +05:30
|
|
|
segment.skipped = true
|
|
|
|
} else if (sponsorBlockConfig[segment.category] == SbSkipOptions.MANUAL ||
|
|
|
|
(
|
2023-10-11 21:43:32 +05:30
|
|
|
sponsorBlockConfig[segment.category] == SbSkipOptions.AUTOMATIC_ONCE &&
|
|
|
|
segment.skipped
|
|
|
|
)
|
2023-08-22 19:35:21 +05:30
|
|
|
) {
|
|
|
|
return segment
|
2023-02-12 17:47:44 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null
|
|
|
|
}
|
2023-06-19 16:26:53 +05:30
|
|
|
|
2023-07-04 06:47:50 +05:30
|
|
|
fun ExoPlayer.isInSegment(segments: List<Segment>): Boolean {
|
|
|
|
return segments.any {
|
|
|
|
val (start, end) = it.segmentStartAndEnd
|
|
|
|
val (segmentStart, segmentEnd) = (start * 1000f).toLong() to (end * 1000f).toLong()
|
|
|
|
currentPosition in segmentStart..segmentEnd
|
2023-06-23 17:34:58 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-24 22:27:33 +05:30
|
|
|
/**
|
|
|
|
* Get the name of the currently played chapter
|
|
|
|
*/
|
2023-09-13 19:44:21 +05:30
|
|
|
fun getCurrentChapterIndex(currentPositionMs: Long, chapters: List<ChapterSegment>): Int? {
|
|
|
|
val currentPositionSeconds = currentPositionMs / 1000
|
2023-08-24 20:42:27 +05:30
|
|
|
return chapters
|
|
|
|
.sortedBy { it.start }
|
2023-09-13 19:44:21 +05:30
|
|
|
.indexOfLast { currentPositionSeconds >= it.start }
|
2023-08-24 20:42:27 +05:30
|
|
|
.takeIf { it >= 0 }
|
2023-10-06 21:07:20 +05:30
|
|
|
?.takeIf { index ->
|
|
|
|
val chapter = chapters[index]
|
|
|
|
// remove the video highlight if it's already longer ago than [ChapterSegment.HIGHLIGHT_LENGTH],
|
|
|
|
// otherwise the SponsorBlock highlight would be shown from its starting point to the end
|
|
|
|
val isWithinMaxHighlightDuration = (currentPositionSeconds - chapter.start) < ChapterSegment.HIGHLIGHT_LENGTH
|
|
|
|
chapter.highlightDrawable == null || isWithinMaxHighlightDuration
|
|
|
|
}
|
2023-06-24 22:27:33 +05:30
|
|
|
}
|
|
|
|
|
2023-07-11 15:45:44 +05:30
|
|
|
fun getPosition(videoId: String, duration: Long?): Long? {
|
|
|
|
if (duration == null) return null
|
|
|
|
|
|
|
|
runCatching {
|
|
|
|
val watchPosition = runBlocking {
|
|
|
|
DatabaseHolder.Database.watchPositionDao().findById(videoId)
|
|
|
|
}
|
|
|
|
if (watchPosition != null && watchPosition.position < duration * 1000 * 0.9) {
|
|
|
|
return watchPosition.position
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null
|
|
|
|
}
|
2023-07-16 21:01:17 +05:30
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the track type string resource corresponding to ExoPlayer role flags used for audio
|
|
|
|
* track types.
|
|
|
|
*
|
|
|
|
* If the role flags doesn't have any role flags used for audio track types, the string
|
|
|
|
* resource `unknown_audio_track_type` is returned.
|
|
|
|
*
|
|
|
|
* @param context a context to get the string resources used to build the audio track type
|
|
|
|
* @param roleFlags the ExoPlayer role flags from which the audio track type will be returned
|
|
|
|
* @return the track type string resource corresponding to an ExoPlayer role flag or the
|
|
|
|
* `unknown_audio_track_type` one if no role flags corresponding to the ones used for audio
|
|
|
|
* track types is set
|
|
|
|
*/
|
|
|
|
private fun getDisplayAudioTrackTypeFromFormat(
|
|
|
|
context: Context,
|
|
|
|
@C.RoleFlags roleFlags: Int
|
|
|
|
): String {
|
|
|
|
// These role flags should not be set together, so the first role only take into account
|
|
|
|
// flag which matches
|
|
|
|
return when {
|
|
|
|
// If the flag ROLE_FLAG_DESCRIBES_VIDEO is set, return the descriptive_audio_track
|
|
|
|
// string resource
|
|
|
|
roleFlags and C.ROLE_FLAG_DESCRIBES_VIDEO == C.ROLE_FLAG_DESCRIBES_VIDEO ->
|
|
|
|
context.getString(R.string.descriptive_audio_track)
|
|
|
|
|
|
|
|
// If the flag ROLE_FLAG_DESCRIBES_VIDEO is set, return the dubbed_audio_track
|
|
|
|
// string resource
|
|
|
|
roleFlags and C.ROLE_FLAG_DUB == C.ROLE_FLAG_DUB ->
|
|
|
|
context.getString(R.string.dubbed_audio_track)
|
|
|
|
|
|
|
|
// If the flag ROLE_FLAG_DESCRIBES_VIDEO is set, return the original_or_main_audio_track
|
|
|
|
// string resource
|
|
|
|
roleFlags and C.ROLE_FLAG_MAIN == C.ROLE_FLAG_MAIN ->
|
|
|
|
context.getString(R.string.original_or_main_audio_track)
|
|
|
|
|
|
|
|
// Return the unknown_audio_track_type string resource for any other value
|
|
|
|
else -> context.getString(R.string.unknown_audio_track_type)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get an audio track name from an audio format, using its language tag and its role flags.
|
|
|
|
*
|
|
|
|
* If the given language is `null`, the string resource `unknown_audio_language` is used
|
|
|
|
* instead and when the given role flags have no track type value used by the app, the string
|
|
|
|
* resource `unknown_audio_track_type` is used instead.
|
|
|
|
*
|
|
|
|
* @param context a context to get the string resources used to build the
|
|
|
|
* audio track name
|
|
|
|
* @param audioLanguageAndRoleFlags a pair of an audio format language tag and role flags from
|
|
|
|
* which the audio track name will be built
|
|
|
|
* @return an audio track name of an audio format language and role flags, localized according
|
|
|
|
* to the language preferences of the user
|
|
|
|
*/
|
|
|
|
fun getAudioTrackNameFromFormat(
|
|
|
|
context: Context,
|
|
|
|
audioLanguageAndRoleFlags: Pair<String?, @C.RoleFlags Int>
|
|
|
|
): String {
|
|
|
|
val audioLanguage = audioLanguageAndRoleFlags.first
|
|
|
|
return context.getString(R.string.audio_track_format)
|
|
|
|
.format(
|
2023-07-20 18:32:13 +05:30
|
|
|
if (audioLanguage == null) {
|
|
|
|
context.getString(R.string.unknown_audio_language)
|
|
|
|
} else {
|
|
|
|
Locale.forLanguageTag(audioLanguage)
|
|
|
|
.getDisplayLanguage(
|
|
|
|
LocaleHelper.getAppLocale()
|
|
|
|
)
|
|
|
|
.ifEmpty { context.getString(R.string.unknown_audio_language) }
|
|
|
|
},
|
2023-07-16 21:01:17 +05:30
|
|
|
getDisplayAudioTrackTypeFromFormat(context, audioLanguageAndRoleFlags.second)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get audio languages with their role flags of supported formats from ExoPlayer track groups
|
|
|
|
* and only the selected ones if requested.
|
|
|
|
*
|
|
|
|
* Duplicate audio languages with their role flags are removed.
|
|
|
|
*
|
2023-07-19 13:39:05 +05:30
|
|
|
* @param groups the list of [Tracks.Group]s of the current tracks played by the player
|
2023-07-16 21:01:17 +05:30
|
|
|
* @param keepOnlySelectedTracks whether to get only the selected audio languages with their
|
|
|
|
* role flags among the supported ones
|
|
|
|
* @return a list of distinct audio languages with their role flags from the supported formats
|
|
|
|
* of the given track groups and only the selected ones if requested
|
|
|
|
*/
|
|
|
|
fun getAudioLanguagesAndRoleFlagsFromTrackGroups(
|
|
|
|
groups: List<Tracks.Group>,
|
|
|
|
keepOnlySelectedTracks: Boolean
|
|
|
|
): List<Pair<String?, @C.RoleFlags Int>> {
|
|
|
|
// Filter unsupported tracks and keep only selected tracks if requested
|
|
|
|
// Use a lambda expression to avoid checking on each audio format if we keep only selected
|
|
|
|
// tracks or not
|
2023-07-20 18:32:13 +05:30
|
|
|
val trackFilter = if (keepOnlySelectedTracks) {
|
2023-07-16 21:01:17 +05:30
|
|
|
{ group: Tracks.Group, trackIndex: Int ->
|
|
|
|
group.isTrackSupported(trackIndex) && group.isTrackSelected(
|
|
|
|
trackIndex
|
|
|
|
)
|
2023-07-20 18:32:13 +05:30
|
|
|
}
|
|
|
|
} else {
|
|
|
|
{ group: Tracks.Group, trackIndex: Int -> group.isTrackSupported(trackIndex) }
|
|
|
|
}
|
2023-07-16 21:01:17 +05:30
|
|
|
|
|
|
|
return groups.filter {
|
|
|
|
it.type == C.TRACK_TYPE_AUDIO
|
|
|
|
}.flatMap { group ->
|
|
|
|
(0 until group.length).filter {
|
|
|
|
trackFilter(group, it)
|
|
|
|
}.map { group.getTrackFormat(it) }
|
|
|
|
}.map { format ->
|
|
|
|
format.language to format.roleFlags
|
|
|
|
}.distinct()
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check whether the given flag is set in the given bitfield.
|
|
|
|
*
|
|
|
|
* @param bitField a bitfield
|
|
|
|
* @param flag a flag to check its presence in the given bitfield
|
|
|
|
* @return whether the given flag is set in the given bitfield
|
|
|
|
*/
|
2023-07-19 13:39:05 +05:30
|
|
|
private fun isFlagSet(bitField: Int, flag: Int) = bitField and flag == flag
|
2023-07-16 21:01:17 +05:30
|
|
|
|
|
|
|
/**
|
|
|
|
* Check whether the given ExoPlayer role flags contain at least one flag used for audio
|
|
|
|
* track types.
|
|
|
|
*
|
|
|
|
* ExoPlayer role flags currently used for audio track types are [C.ROLE_FLAG_DESCRIBES_VIDEO],
|
|
|
|
* [C.ROLE_FLAG_DUB], [C.ROLE_FLAG_MAIN] and [C.ROLE_FLAG_ALTERNATE].
|
|
|
|
*
|
|
|
|
* @param roleFlags the ExoPlayer role flags to check, an int representing a bitfield
|
|
|
|
* @return whether the provided ExoPlayer flags contain a flag used for audio track types
|
|
|
|
*/
|
|
|
|
fun haveAudioTrackRoleFlagSet(@C.RoleFlags roleFlags: Int): Boolean {
|
2023-07-20 18:32:13 +05:30
|
|
|
return isFlagSet(roleFlags, C.ROLE_FLAG_DESCRIBES_VIDEO) ||
|
2023-10-11 21:43:32 +05:30
|
|
|
isFlagSet(roleFlags, C.ROLE_FLAG_DUB) ||
|
|
|
|
isFlagSet(roleFlags, C.ROLE_FLAG_MAIN) ||
|
|
|
|
isFlagSet(roleFlags, C.ROLE_FLAG_ALTERNATE)
|
2023-07-16 21:01:17 +05:30
|
|
|
}
|
2023-09-13 19:23:15 +05:30
|
|
|
|
|
|
|
@androidx.annotation.OptIn(androidx.media3.common.util.UnstableApi::class)
|
|
|
|
fun getVideoStats(player: ExoPlayer, videoId: String): VideoStats {
|
|
|
|
val videoInfo = "${player.videoFormat?.codecs.orEmpty()} ${
|
|
|
|
TextUtils.formatBitrate(
|
|
|
|
player.videoFormat?.bitrate
|
|
|
|
)
|
|
|
|
}"
|
|
|
|
val audioInfo = "${player.audioFormat?.codecs.orEmpty()} ${
|
|
|
|
TextUtils.formatBitrate(player.audioFormat?.bitrate)
|
|
|
|
}"
|
2023-09-25 14:14:36 +05:30
|
|
|
val videoQuality =
|
|
|
|
"${player.videoFormat?.width}x${player.videoFormat?.height} ${player.videoFormat?.frameRate?.toInt()}fps"
|
2023-09-13 19:23:15 +05:30
|
|
|
return VideoStats(videoId, videoInfo, videoQuality, audioInfo)
|
|
|
|
}
|
2022-07-17 02:19:32 +05:30
|
|
|
}
|