From 27d72eb821faaeba6c71b7e3cbab902cb6a68604 Mon Sep 17 00:00:00 2001 From: Prateek Sunal Date: Tue, 2 Sep 2025 02:44:11 +0530 Subject: [PATCH] fix: make continuation and releasing compute better --- .../lib/services/video_preview_service.dart | 40 +++++++++++++------ 1 file changed, 27 insertions(+), 13 deletions(-) diff --git a/mobile/apps/photos/lib/services/video_preview_service.dart b/mobile/apps/photos/lib/services/video_preview_service.dart index a9d68664ae..652890a935 100644 --- a/mobile/apps/photos/lib/services/video_preview_service.dart +++ b/mobile/apps/photos/lib/services/video_preview_service.dart @@ -274,9 +274,12 @@ class VideoPreviewService { Future chunkAndUploadVideo( BuildContext? ctx, - EnteFile enteFile, [ + EnteFile enteFile, { + /// Indicates this function is an continuation of a chunking thread + bool continuation = false, + // not used currently bool forceUpload = false, - ]) async { + }) async { final bool isManual = await uploadLocksDB.isInStreamQueue(enteFile.uploadedFileID!); final canStream = _isPermissionGranted(); @@ -284,6 +287,7 @@ class VideoPreviewService { _logger.info( "Pause preview due to disabledSteaming($isVideoStreamingEnabled) or computeController permission) - isManual: $isManual", ); + computeController.releaseCompute(stream: true); if (isVideoStreamingEnabled) _logger.info("No permission to run compute"); clearQueue(); return; @@ -330,7 +334,7 @@ class VideoPreviewService { } // check if there is already a preview in processing - if (uploadingFileId >= 0) { + if (!continuation && uploadingFileId >= 0) { if (uploadingFileId == enteFile.uploadedFileID) return; _items[enteFile.uploadedFileID!] = PreviewItem( @@ -581,10 +585,6 @@ class VideoPreviewService { _removeFile(enteFile); _removeFromLocks(enteFile).ignore(); } - // reset uploading status if this was getting processed - if (uploadingFileId == enteFile.uploadedFileID!) { - uploadingFileId = -1; - } if (fileQueue.isNotEmpty) { // process next file _logger.info( @@ -593,12 +593,18 @@ class VideoPreviewService { final entry = fileQueue.entries.first; final file = entry.value; fileQueue.remove(entry.key); - await chunkAndUploadVideo(ctx, file); + await chunkAndUploadVideo( + ctx, + file, + continuation: true, + ); } else { _logger.info( "[chunk] Nothing to process releasing compute, $error", ); computeController.releaseCompute(stream: true); + + uploadingFileId = -1; } } } @@ -1009,8 +1015,9 @@ class VideoPreviewService { } // generate stream for all files after cutoff date - Future _putFilesForPreviewCreation() async { - if (!isVideoStreamingEnabled || !await canUseHighBandwidth()) return; + // returns false if it fails to launch chuncking function + Future _putFilesForPreviewCreation() async { + if (!isVideoStreamingEnabled || !await canUseHighBandwidth()) return false; Map failureFiles = {}; Map manualQueueFiles = {}; @@ -1146,7 +1153,7 @@ class VideoPreviewService { final totalFiles = fileQueue.length; if (totalFiles == 0) { _logger.info("[init] No preview to cache"); - return; + return false; } _logger.info( @@ -1158,6 +1165,7 @@ class VideoPreviewService { final file = entry.value; fileQueue.remove(entry.key); chunkAndUploadVideo(null, file).ignore(); + return true; } bool _allowStream() { @@ -1174,9 +1182,11 @@ class VideoPreviewService { ); } + /// To check if it's enabled, device is healthy and running streaming bool _isPermissionGranted() { return isVideoStreamingEnabled && - computeController.computeState == ComputeRunState.generatingStream; + computeController.computeState == ComputeRunState.generatingStream && + computeController.isDeviceHealthy; } void queueFiles({ @@ -1191,7 +1201,11 @@ class VideoPreviewService { if (!isStreamAllowed) return; await _ensurePreviewIdsInitialized(); - await _putFilesForPreviewCreation(); + final result = await _putFilesForPreviewCreation(); + // Cannot proceed to stream generation, would have to release compute ASAP + if (!result) { + computeController.releaseCompute(stream: true); + } }); } }