Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

feat: Add stop and stopSafely methods for streaming cancellation

Prateek Sunal5b63929b386c
diff --git a/mobile/apps/photos/lib/services/video_preview_service.dart b/mobile/apps/photos/lib/services/video_preview_service.dart
index 0473369b6e..1d1c2f357d 100644
--- a/mobile/apps/photos/lib/services/video_preview_service.dart
+++ b/mobile/apps/photos/lib/services/video_preview_service.dart
@@ -7,6 +7,8 @@ import "package:collection/collection.dart";
import "package:dio/dio.dart";
import "package:encrypt/encrypt.dart" as enc;
import "package:ffmpeg_kit_flutter/ffmpeg_kit.dart";
+import "package:ffmpeg_kit_flutter/ffmpeg_kit_config.dart";
+import "package:ffmpeg_kit_flutter/ffmpeg_session.dart";
import "package:flutter/foundation.dart";
import "package:flutter/widgets.dart";
import "package:flutter_cache_manager/flutter_cache_manager.dart";
@@ -98,10 +100,7 @@ class VideoPreviewService {
CancelToken? _streamingCancelToken;
bool _stopRequested = false;
int? _currentFfmpegSessionId;
-
- /// Override this to customize when to cancel FFmpeg (e.g., after 75% progress).
- /// Returns true if FFmpeg should be cancelled.
- bool shouldCancelFfmpeg() => _stopRequested;
+ int? _currentVideoDurationMs;
final Configuration config;
final ServiceLocator serviceLocator;
@@ -147,28 +146,59 @@ class VideoPreviewService {
_stopRequested = false;
_streamingCancelToken = null;
_currentFfmpegSessionId = null;
+ _currentVideoDurationMs = null;
uploadingFileId = -1;
}
- Future<void> _cancelCurrentFfmpeg() async {
+ /// Get current FFmpeg progress (0.0 to 1.0), returns null if unavailable.
+ Future<double?> _getFfmpegProgress() async {
final sessionId = _currentFfmpegSessionId;
- if (sessionId != null) {
- await FFmpegKit.cancel(sessionId);
- _currentFfmpegSessionId = null;
+ final totalDuration = _currentVideoDurationMs;
+ if (sessionId == null || totalDuration == null || totalDuration <= 0) {
+ return null;
}
- }
- Future<void> stopForLogout() async {
- if (!flagService.stopStreamOnLogOut) return;
+ final session = await FFmpegKitConfig.getSession(sessionId);
+ if (session == null || session is! FFmpegSession) return null;
+
+ final stats = await session.getStatistics();
+ if (stats.isEmpty) return null;
+
+ final processedMs = stats.last.getTime();
+ return (processedMs / totalDuration).clamp(0.0, 1.0);
+ }
+ /// Stop streaming immediately, cancels FFmpeg regardless of progress.
+ Future<void> stop() async {
_stopRequested = true;
_streamingCancelToken?.cancel();
- await _cancelCurrentFfmpeg();
+ final sessionId = _currentFfmpegSessionId;
+ if (sessionId != null) {
+ await FFmpegKit.cancel(sessionId);
+ _currentFfmpegSessionId = null;
+ }
uploadingFileId = -1;
clearQueue();
computeController.releaseCompute(stream: true);
}
+ /// Stop streaming only if < 75% processed (for thermal throttling).
+ /// Returns true if stopped, false if skipped due to progress >= 75%.
+ Future<bool> stopSafely({double threshold = 0.75}) async {
+ final progress = await _getFfmpegProgress();
+ // If we can't determine progress or progress < threshold, stop
+ if (progress == null || progress < threshold) {
+ await stop();
+ return true;
+ }
+ return false;
+ }
+
+ Future<void> stopForLogout() async {
+ if (!flagService.stopStreamOnLogOut) return;
+ await stop();
+ }
+
void _fireVideoPreviewStateChange(int fileId, PreviewItemStatus status) {
Bus.instance.fire(VideoPreviewStateChangedEvent(fileId, status));
}
@@ -542,6 +572,7 @@ class VideoPreviewService {
_logger.info(command);
+ _currentVideoDurationMs = props?.duration?.inMilliseconds;
final playlistGenResult = await ffmpegService
.runFfmpegCancellable(
'-i "${file.path}" $command$prefix/output.m3u8',
@@ -553,7 +584,7 @@ class VideoPreviewService {
return {};
});
- if (shouldCancelFfmpeg()) {
+ if (_stopRequested) {
Directory(prefix).delete(recursive: true).ignore();
return;
}