From f41a971cd8852f31f1b0b851362c0cc2adc5b694 Mon Sep 17 00:00:00 2001 From: Kelvin Date: Thu, 29 Aug 2024 15:43:29 +0200 Subject: [PATCH] Fix download states --- .../platforms/js/models/sources/JSSource.kt | 2 +- .../platformplayer/downloads/VideoDownload.kt | 42 ++++++++++++++++--- .../platformplayer/helpers/VideoHelper.kt | 4 +- .../services/DownloadService.kt | 6 +-- 4 files changed, 41 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/com/futo/platformplayer/api/media/platforms/js/models/sources/JSSource.kt b/app/src/main/java/com/futo/platformplayer/api/media/platforms/js/models/sources/JSSource.kt index a658f5cb..45ace168 100644 --- a/app/src/main/java/com/futo/platformplayer/api/media/platforms/js/models/sources/JSSource.kt +++ b/app/src/main/java/com/futo/platformplayer/api/media/platforms/js/models/sources/JSSource.kt @@ -50,7 +50,7 @@ abstract class JSSource { _requestExecutor = obj.getOrDefault(_config, "requestExecutor", "JSSource.requestExecutor", null)?.let { JSRequest(plugin, it, null, null, true); } - hasRequestExecutor = _requestModifier != null || obj.has("getRequestExecutor"); + hasRequestExecutor = _requestExecutor != null || obj.has("getRequestExecutor"); } fun getRequestModifier(): IRequestModifier? { diff --git a/app/src/main/java/com/futo/platformplayer/downloads/VideoDownload.kt b/app/src/main/java/com/futo/platformplayer/downloads/VideoDownload.kt index 574a2875..a1a11d91 100644 --- a/app/src/main/java/com/futo/platformplayer/downloads/VideoDownload.kt +++ b/app/src/main/java/com/futo/platformplayer/downloads/VideoDownload.kt @@ -93,6 +93,18 @@ class VideoDownload { @Transient val audioSourceToUse: IAudioSource? get () = if(requiresLiveAudioSource) audioSourceLive as IAudioSource? else audioSource as IAudioSource?; + var requireVideoSource: Boolean = false; + var requireAudioSource: Boolean = false; + + @Contextual + @Transient + val isVideoDownloadReady: Boolean get() = !requireVideoSource || + ((requiresLiveVideoSource && isLiveVideoSourceValid) || (!requiresLiveVideoSource && videoSource != null)); + @Contextual + @Transient + val isAudioDownloadReady: Boolean get() = !requireAudioSource || + ((requiresLiveAudioSource && isLiveAudioSourceValid) || (!requiresLiveAudioSource && audioSource != null)); + var subtitleSource: SubtitleRawSource?; @kotlinx.serialization.Serializable(with = OffsetDateTimeNullableSerializer::class) @@ -157,6 +169,8 @@ class VideoDownload { this.hasVideoRequestExecutor = video is JSSource && video.hasRequestExecutor; this.requiresLiveVideoSource = false; this.targetVideoName = videoSource?.name; + this.requireVideoSource = targetPixelCount != null + this.requireAudioSource = targetBitrate != null; //TODO: May not be a valid check.. can only be determined after live fetch? } constructor(video: IPlatformVideoDetails, videoSource: IVideoSource?, audioSource: IAudioSource?, subtitleSource: SubtitleRawSource?) { this.video = SerializedPlatformVideo.fromVideo(video); @@ -175,6 +189,8 @@ class VideoDownload { this.targetAudioName = audioSource?.name; this.targetPixelCount = if(videoSource != null) (videoSource.width * videoSource.height).toLong() else null; this.targetBitrate = if(audioSource != null) audioSource.bitrate.toLong() else null; + this.requireVideoSource = videoSource != null; + this.requireAudioSource = audioSource != null; } fun withGroup(groupType: String, groupID: String): VideoDownload { @@ -320,8 +336,10 @@ class VideoDownload { throw DownloadException("Audio source is not supported for downloading (yet)", false); } - if(((!requiresLiveVideoSource && videoSource == null) || (requiresLiveVideoSource && !isLiveVideoSourceValid)) || ((!requiresLiveAudioSource && audioSource == null) || (requiresLiveAudioSource && !isLiveAudioSourceValid))) - throw DownloadException("No valid sources found for video/audio"); + if(!isVideoDownloadReady) + throw DownloadException("No valid sources found for video"); + if(!isAudioDownloadReady) + throw DownloadException("No valid sources found for audio"); } } @@ -367,6 +385,7 @@ class VideoDownload { sourcesToDownload.add(async { Logger.i(TAG, "Started downloading video"); + var lastEmit = 0L; val progressCallback = { length: Long, totalRead: Long, speed: Long -> synchronized(progressLock) { lastVideoLength = length; @@ -379,9 +398,14 @@ class VideoDownload { val total = lastVideoRead + lastAudioRead; if(totalLength > 0) { val percentage = (total / totalLength.toDouble()); - onProgress?.invoke(percentage); progress = percentage; - onProgressChanged.emit(percentage); + + val now = System.currentTimeMillis(); + if(now - lastEmit > 200) { + lastEmit = System.currentTimeMillis(); + onProgress?.invoke(percentage); + onProgressChanged.emit(percentage); + } } } } @@ -401,6 +425,7 @@ class VideoDownload { sourcesToDownload.add(async { Logger.i(TAG, "Started downloading audio"); + var lastEmit = 0L; val progressCallback = { length: Long, totalRead: Long, speed: Long -> synchronized(progressLock) { lastAudioLength = length; @@ -413,9 +438,14 @@ class VideoDownload { val total = lastVideoRead + lastAudioRead; if(totalLength > 0) { val percentage = (total / totalLength.toDouble()); - onProgress?.invoke(percentage); progress = percentage; - onProgressChanged.emit(percentage); + + val now = System.currentTimeMillis(); + if(now - lastEmit > 200) { + lastEmit = System.currentTimeMillis(); + onProgress?.invoke(percentage); + onProgressChanged.emit(percentage); + } } } } diff --git a/app/src/main/java/com/futo/platformplayer/helpers/VideoHelper.kt b/app/src/main/java/com/futo/platformplayer/helpers/VideoHelper.kt index 860c2daa..23fa2b73 100644 --- a/app/src/main/java/com/futo/platformplayer/helpers/VideoHelper.kt +++ b/app/src/main/java/com/futo/platformplayer/helpers/VideoHelper.kt @@ -193,7 +193,7 @@ class VideoHelper { fun estimateSourceSize(source: IVideoSource?): Int { if(source == null) return 0; - if(source is IVideoUrlSource) { + if(source is IVideoSource) { if(source.bitrate ?: 0 <= 0 || source.duration.toInt() == 0) return 0; return (source.duration / 8).toInt() * source.bitrate!!; @@ -202,7 +202,7 @@ class VideoHelper { } fun estimateSourceSize(source: IAudioSource?): Int { if(source == null) return 0; - if(source is IAudioUrlSource) { + if(source is IAudioSource) { if(source.bitrate <= 0 || source.duration?.toInt() ?: 0 == 0) return 0; return (source.duration!! / 8).toInt() * source.bitrate; diff --git a/app/src/main/java/com/futo/platformplayer/services/DownloadService.kt b/app/src/main/java/com/futo/platformplayer/services/DownloadService.kt index 6955cbe9..b9c56420 100644 --- a/app/src/main/java/com/futo/platformplayer/services/DownloadService.kt +++ b/app/src/main/java/com/futo/platformplayer/services/DownloadService.kt @@ -195,9 +195,7 @@ class DownloadService : Service() { download.targetBitrate = download.audioSource!!.bitrate.toLong(); download.audioSource = null; } - if(download.videoDetails == null || - ((download.videoSource == null && download.audioSource == null) && - (download.requiresLiveVideoSource && !download.isLiveVideoSourceValid) && (download.requiresLiveAudioSource && !download.isLiveAudioSourceValid))) + if(download.videoDetails == null || (!download.isVideoDownloadReady || !download.isAudioDownloadReady)) download.changeState(VideoDownload.State.PREPARING); notifyDownload(download); @@ -214,7 +212,7 @@ class DownloadService : Service() { download.progress = progress; val currentTime = System.currentTimeMillis(); - if (currentTime - lastNotifyTime > 500) { + if (currentTime - lastNotifyTime > 800) { notifyDownload(download); lastNotifyTime = currentTime; }