Skip to content

armgit / ente

Public Imported from GitHub
0

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

Update video streaming bitrate to 2000kbps max and add upload check

Prateek Sunale166d328ebcd
diff --git a/desktop/src/main/services/ffmpeg-worker.ts b/desktop/src/main/services/ffmpeg-worker.ts
index 82ee9bce69..f47f4dfe5e 100644
--- a/desktop/src/main/services/ffmpeg-worker.ts
+++ b/desktop/src/main/services/ffmpeg-worker.ts
@@ -497,11 +497,14 @@ const ffmpegGenerateHLSPlaylistAndSegments = async (
//
// - `-c:v libx264` converts the video stream to the H.264 codec.
//
- // - We don't supply a bitrate, instead it uses the default CRF
- // ("23") as recommended in the ffmpeg trac.
+ // - We use CRF 23 (default) for quality, but cap the bitrate at
+ // 2000 kbps using `-maxrate` to ensure smaller bitrates are
+ // preserved while preventing CRF from exceeding 2000 kbps.
+ //
+ // - `-bufsize` is set to 2x maxrate for smooth rate control.
//
// - We don't supply a preset, it'll use the default ("medium").
- ["-c:v", "libx264"]
+ ["-c:v", "libx264", "-maxrate", "2000k", "-bufsize", "4000k"]
: // Keep the video stream unchanged
["-c:v", "copy"],
// Audio codec AAC
diff --git a/mobile/apps/photos/lib/services/video_preview_service.dart b/mobile/apps/photos/lib/services/video_preview_service.dart
index 7563119b3f..89fa4a6220 100644
--- a/mobile/apps/photos/lib/services/video_preview_service.dart
+++ b/mobile/apps/photos/lib/services/video_preview_service.dart
@@ -36,6 +36,7 @@ import "package:photos/services/machine_learning/compute_controller.dart";
import "package:photos/ui/notification/toast.dart";
import "package:photos/utils/exif_util.dart";
import "package:photos/utils/file_key.dart";
+import "package:photos/utils/file_uploader.dart";
import "package:photos/utils/file_util.dart";
import "package:photos/utils/gzip.dart";
import "package:photos/utils/network_util.dart";
@@ -280,6 +281,16 @@ class VideoPreviewService {
// not used currently
bool forceUpload = false,
}) async {
+ // Check if file upload is happening before processing video for streaming
+ if (FileUploader.instance.isUploading) {
+ _logger.info(
+ "Pausing video streaming because file upload is in progress",
+ );
+ computeController.releaseCompute(stream: true);
+ clearQueue();
+ return;
+ }
+
final bool isManual =
await uploadLocksDB.isInStreamQueue(enteFile.uploadedFileID!);
final canStream = _isPermissionGranted();
@@ -454,8 +465,9 @@ class VideoPreviewService {
final command =
// scaling, fps, tonemapping
'$filters'
- // video encoding
- '${reencodeVideo ? '-c:v libx264 -crf 23 -preset medium ' : '-c:v copy '}'
+ // video encoding with maxrate cap at 2000kbps to preserve smaller bitrates
+ // while preventing CRF from exceeding the max bitrate
+ '${reencodeVideo ? '-c:v libx264 -crf 23 -preset medium -maxrate 2000k -bufsize 4000k ' : '-c:v copy '}'
// audio encoding
'-c:a aac -b:a 128k '
// hls options
diff --git a/mobile/apps/photos/lib/utils/file_uploader.dart b/mobile/apps/photos/lib/utils/file_uploader.dart
index 5d91af9b82..c4946b83f1 100644
--- a/mobile/apps/photos/lib/utils/file_uploader.dart
+++ b/mobile/apps/photos/lib/utils/file_uploader.dart
@@ -77,6 +77,9 @@ class FileUploader {
LinkedHashMap<String, BackupItem> get allBackups => _allBackups;
+ /// Returns true if any file uploads are currently in progress
+ bool get isUploading => _uploadCounter > 0;
+
// Maintains the count of files in the current upload session.
// Upload session is the period between the first entry into the _queue and last entry out of the _queue
int _totalCountInUploadSession = 0;