Fix download states

This commit is contained in:
Kelvin 2024-08-29 15:43:29 +02:00
commit f41a971cd8
4 changed files with 41 additions and 13 deletions

View file

@ -50,7 +50,7 @@ abstract class JSSource {
_requestExecutor = obj.getOrDefault<V8ValueObject>(_config, "requestExecutor", "JSSource.requestExecutor", null)?.let { _requestExecutor = obj.getOrDefault<V8ValueObject>(_config, "requestExecutor", "JSSource.requestExecutor", null)?.let {
JSRequest(plugin, it, null, null, true); JSRequest(plugin, it, null, null, true);
} }
hasRequestExecutor = _requestModifier != null || obj.has("getRequestExecutor"); hasRequestExecutor = _requestExecutor != null || obj.has("getRequestExecutor");
} }
fun getRequestModifier(): IRequestModifier? { fun getRequestModifier(): IRequestModifier? {

View file

@ -93,6 +93,18 @@ class VideoDownload {
@Transient @Transient
val audioSourceToUse: IAudioSource? get () = if(requiresLiveAudioSource) audioSourceLive as IAudioSource? else audioSource as IAudioSource?; 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?; var subtitleSource: SubtitleRawSource?;
@kotlinx.serialization.Serializable(with = OffsetDateTimeNullableSerializer::class) @kotlinx.serialization.Serializable(with = OffsetDateTimeNullableSerializer::class)
@ -157,6 +169,8 @@ class VideoDownload {
this.hasVideoRequestExecutor = video is JSSource && video.hasRequestExecutor; this.hasVideoRequestExecutor = video is JSSource && video.hasRequestExecutor;
this.requiresLiveVideoSource = false; this.requiresLiveVideoSource = false;
this.targetVideoName = videoSource?.name; 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?) { constructor(video: IPlatformVideoDetails, videoSource: IVideoSource?, audioSource: IAudioSource?, subtitleSource: SubtitleRawSource?) {
this.video = SerializedPlatformVideo.fromVideo(video); this.video = SerializedPlatformVideo.fromVideo(video);
@ -175,6 +189,8 @@ class VideoDownload {
this.targetAudioName = audioSource?.name; this.targetAudioName = audioSource?.name;
this.targetPixelCount = if(videoSource != null) (videoSource.width * videoSource.height).toLong() else null; this.targetPixelCount = if(videoSource != null) (videoSource.width * videoSource.height).toLong() else null;
this.targetBitrate = if(audioSource != null) audioSource.bitrate.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 { fun withGroup(groupType: String, groupID: String): VideoDownload {
@ -320,8 +336,10 @@ class VideoDownload {
throw DownloadException("Audio source is not supported for downloading (yet)", false); throw DownloadException("Audio source is not supported for downloading (yet)", false);
} }
if(((!requiresLiveVideoSource && videoSource == null) || (requiresLiveVideoSource && !isLiveVideoSourceValid)) || ((!requiresLiveAudioSource && audioSource == null) || (requiresLiveAudioSource && !isLiveAudioSourceValid))) if(!isVideoDownloadReady)
throw DownloadException("No valid sources found for video/audio"); 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 { sourcesToDownload.add(async {
Logger.i(TAG, "Started downloading video"); Logger.i(TAG, "Started downloading video");
var lastEmit = 0L;
val progressCallback = { length: Long, totalRead: Long, speed: Long -> val progressCallback = { length: Long, totalRead: Long, speed: Long ->
synchronized(progressLock) { synchronized(progressLock) {
lastVideoLength = length; lastVideoLength = length;
@ -379,12 +398,17 @@ class VideoDownload {
val total = lastVideoRead + lastAudioRead; val total = lastVideoRead + lastAudioRead;
if(totalLength > 0) { if(totalLength > 0) {
val percentage = (total / totalLength.toDouble()); val percentage = (total / totalLength.toDouble());
onProgress?.invoke(percentage);
progress = percentage; progress = percentage;
val now = System.currentTimeMillis();
if(now - lastEmit > 200) {
lastEmit = System.currentTimeMillis();
onProgress?.invoke(percentage);
onProgressChanged.emit(percentage); onProgressChanged.emit(percentage);
} }
} }
} }
}
if(actualVideoSource is IVideoUrlSource) if(actualVideoSource is IVideoUrlSource)
videoFileSize = when (videoSource!!.container) { videoFileSize = when (videoSource!!.container) {
@ -401,6 +425,7 @@ class VideoDownload {
sourcesToDownload.add(async { sourcesToDownload.add(async {
Logger.i(TAG, "Started downloading audio"); Logger.i(TAG, "Started downloading audio");
var lastEmit = 0L;
val progressCallback = { length: Long, totalRead: Long, speed: Long -> val progressCallback = { length: Long, totalRead: Long, speed: Long ->
synchronized(progressLock) { synchronized(progressLock) {
lastAudioLength = length; lastAudioLength = length;
@ -413,12 +438,17 @@ class VideoDownload {
val total = lastVideoRead + lastAudioRead; val total = lastVideoRead + lastAudioRead;
if(totalLength > 0) { if(totalLength > 0) {
val percentage = (total / totalLength.toDouble()); val percentage = (total / totalLength.toDouble());
onProgress?.invoke(percentage);
progress = percentage; progress = percentage;
val now = System.currentTimeMillis();
if(now - lastEmit > 200) {
lastEmit = System.currentTimeMillis();
onProgress?.invoke(percentage);
onProgressChanged.emit(percentage); onProgressChanged.emit(percentage);
} }
} }
} }
}
if(actualAudioSource is IAudioUrlSource) if(actualAudioSource is IAudioUrlSource)
audioFileSize = when (audioSource!!.container) { audioFileSize = when (audioSource!!.container) {

View file

@ -193,7 +193,7 @@ class VideoHelper {
fun estimateSourceSize(source: IVideoSource?): Int { fun estimateSourceSize(source: IVideoSource?): Int {
if(source == null) return 0; if(source == null) return 0;
if(source is IVideoUrlSource) { if(source is IVideoSource) {
if(source.bitrate ?: 0 <= 0 || source.duration.toInt() == 0) if(source.bitrate ?: 0 <= 0 || source.duration.toInt() == 0)
return 0; return 0;
return (source.duration / 8).toInt() * source.bitrate!!; return (source.duration / 8).toInt() * source.bitrate!!;
@ -202,7 +202,7 @@ class VideoHelper {
} }
fun estimateSourceSize(source: IAudioSource?): Int { fun estimateSourceSize(source: IAudioSource?): Int {
if(source == null) return 0; if(source == null) return 0;
if(source is IAudioUrlSource) { if(source is IAudioSource) {
if(source.bitrate <= 0 || source.duration?.toInt() ?: 0 == 0) if(source.bitrate <= 0 || source.duration?.toInt() ?: 0 == 0)
return 0; return 0;
return (source.duration!! / 8).toInt() * source.bitrate; return (source.duration!! / 8).toInt() * source.bitrate;

View file

@ -195,9 +195,7 @@ class DownloadService : Service() {
download.targetBitrate = download.audioSource!!.bitrate.toLong(); download.targetBitrate = download.audioSource!!.bitrate.toLong();
download.audioSource = null; download.audioSource = null;
} }
if(download.videoDetails == null || if(download.videoDetails == null || (!download.isVideoDownloadReady || !download.isAudioDownloadReady))
((download.videoSource == null && download.audioSource == null) &&
(download.requiresLiveVideoSource && !download.isLiveVideoSourceValid) && (download.requiresLiveAudioSource && !download.isLiveAudioSourceValid)))
download.changeState(VideoDownload.State.PREPARING); download.changeState(VideoDownload.State.PREPARING);
notifyDownload(download); notifyDownload(download);
@ -214,7 +212,7 @@ class DownloadService : Service() {
download.progress = progress; download.progress = progress;
val currentTime = System.currentTimeMillis(); val currentTime = System.currentTimeMillis();
if (currentTime - lastNotifyTime > 500) { if (currentTime - lastNotifyTime > 800) {
notifyDownload(download); notifyDownload(download);
lastNotifyTime = currentTime; lastNotifyTime = currentTime;
} }