π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Use FFmpeg progress polling and cancel frame extraction
Neeraj Gupta07e00eba1952
diff --git a/apple/packages/FFmpegRuntime/Package.swift b/apple/packages/FFmpegRuntime/Package.swiftindex d4c5545a46..74f88f73ff 100644--- a/apple/packages/FFmpegRuntime/Package.swift+++ b/apple/packages/FFmpegRuntime/Package.swift@@ -12,8 +12,8 @@ let package = Package(.binaryTarget(name: "FFmpegRuntime",url:- "https://github.com/ente/ffmpeg-packaging/releases/download/9.0.2/ios-FFmpegRuntime.xcframework.zip",- checksum: "2c3923ac301d3deea5df47ab86d5479c5a95cb09b90eae797910498afd554844"+ "https://github.com/ente/ffmpeg-packaging/releases/download/9.0.2-1/ios-FFmpegRuntime.xcframework.zip",+ checksum: "168df3194e3918e70dd233560958cc5849e20e92dadf8587f0cf3adfa4207943")])diff --git a/mobile/apps/photos/lib/services/video_preview_service.dart b/mobile/apps/photos/lib/services/video_preview_service.dartindex fc6e8bfdd0..e1b20cd1c8 100644--- a/mobile/apps/photos/lib/services/video_preview_service.dart+++ b/mobile/apps/photos/lib/services/video_preview_service.dart@@ -627,27 +627,17 @@ class VideoPreviewService {objectId = result.$1;objectSize = result.$2;- final playlistFrameResult = await ffmpegService- .start([- '-allowed_extensions',- 'ALL',- '-i',- '$prefix/output.m3u8',- '-frames:v',- '1',- '-c',- 'copy',- '$prefix/frame.ts',- ])- .completed- .onError((error, stackTrace) {- _logger.warning(- "FFmpeg frame extraction failed",- error,- stackTrace,- );- return ffmpeg.Result(-1, error.toString());- });+ final playlistFrameResult = await _runFfmpeg([+ '-allowed_extensions',+ 'ALL',+ '-i',+ '$prefix/output.m3u8',+ '-frames:v',+ '1',+ '-c',+ 'copy',+ '$prefix/frame.ts',+ ]);if (cancelToken.isCancelled) return;int? width, height;try {diff --git a/mobile/apps/photos/plugins/ffmpeg/android/build.gradle b/mobile/apps/photos/plugins/ffmpeg/android/build.gradleindex 4a24f727ae..d94641e10c 100644--- a/mobile/apps/photos/plugins/ffmpeg/android/build.gradle+++ b/mobile/apps/photos/plugins/ffmpeg/android/build.gradle@@ -7,8 +7,8 @@ plugins {group 'io.ente.ffmpeg'version '0.0.1'-def release = '9.0.2'-def checksum = '9cd0d3c53bc81ddfb13076ce9ecb3a55f8cf772e7324d6dd7a02f19c2df043d6'+def release = '9.0.2-1'+def checksum = 'b2e8e8ca4f6c6e0cdf0aa719f849e7f9f1ecf05c18387421a3064345d69d4649'def localAar = providers.environmentVariable('FFMPEG_AAR').orNulldef archive = configurations.create('ffmpeg') {canBeConsumed = falsediff --git a/mobile/apps/photos/plugins/ffmpeg/lib/src/bindings.dart b/mobile/apps/photos/plugins/ffmpeg/lib/src/bindings.dartindex b547e974e3..0424fa97b3 100644--- a/mobile/apps/photos/plugins/ffmpeg/lib/src/bindings.dart+++ b/mobile/apps/photos/plugins/ffmpeg/lib/src/bindings.dart@@ -5,20 +5,12 @@ import 'package:ffi/ffi.dart';final class SessionHandle extends Opaque {}-typedef ProgressCallback = Void Function(Pointer<Void>, Int64, Int32);-final class Bindings {Bindings(DynamicLibrary library): sessionNew = library.lookupFunction<- Pointer<SessionHandle> Function(- Pointer<NativeFunction<ProgressCallback>>,- Pointer<Void>,- ),- Pointer<SessionHandle> Function(- Pointer<NativeFunction<ProgressCallback>>,- Pointer<Void>,- )+ Pointer<SessionHandle> Function(),+ Pointer<SessionHandle> Function()>('ffmpeg_session_new'),sessionFree = library.lookupFunction<@@ -30,6 +22,11 @@ final class Bindings {Pointer<Utf8> Function(Pointer<SessionHandle>),Pointer<Utf8> Function(Pointer<SessionHandle>)>('ffmpeg_session_output'),+ sessionProgress = library+ .lookupFunction<+ Int64 Function(Pointer<SessionHandle>),+ int Function(Pointer<SessionHandle>)+ >('ffmpeg_session_progress'),execute = library.lookupFunction<Int32 Function(@@ -57,13 +54,10 @@ final class Bindings {static final instance = Bindings(_openLibrary());- final Pointer<SessionHandle> Function(- Pointer<NativeFunction<ProgressCallback>>,- Pointer<Void>,- )- sessionNew;+ final Pointer<SessionHandle> Function() sessionNew;final void Function(Pointer<SessionHandle>) sessionFree;final Pointer<Utf8> Function(Pointer<SessionHandle>) sessionOutput;+ final int Function(Pointer<SessionHandle>) sessionProgress;final int Function(Pointer<SessionHandle>, int, Pointer<Pointer<Utf8>>)execute;final void Function(Pointer<SessionHandle>) cancel;diff --git a/mobile/apps/photos/plugins/ffmpeg/lib/src/session.dart b/mobile/apps/photos/plugins/ffmpeg/lib/src/session.dartindex 964a99cf26..fb27bec17d 100644--- a/mobile/apps/photos/plugins/ffmpeg/lib/src/session.dart+++ b/mobile/apps/photos/plugins/ffmpeg/lib/src/session.dart@@ -1,3 +1,4 @@+import 'dart:async';import 'dart:convert';import 'dart:ffi';import 'dart:isolate';@@ -23,24 +24,14 @@ abstract interface class Session {}final class _NativeSession implements Session {- _NativeSession(void Function(Duration) onProgress) {- final bindings = Bindings.instance;- void progress(Pointer<Void> opaque, int time, int isFinal) {- if (!_finished && time >= 0) {- onProgress(Duration(microseconds: time));- }- }-- _callback = NativeCallable<ProgressCallback>.listener(progress);- _session = bindings.sessionNew(_callback.nativeFunction, nullptr);+ _NativeSession(this._onProgress) : _session = Bindings.instance.sessionNew() {if (_session == nullptr) {- _callback.close();throw StateError('Could not allocate an FFmpeg session.');}}- late final NativeCallable<ProgressCallback> _callback;- late final Pointer<SessionHandle> _session;+ final void Function(Duration) _onProgress;+ final Pointer<SessionHandle> _session;bool _started = false;bool _finished = false;@@ -48,16 +39,28 @@ final class _NativeSession implements Session {Future<Result> execute(List<String> arguments) async {if (_started) throw StateError('An FFmpeg session runs once.');_started = true;+ Timer? timer;try {_checkStrings(arguments);- return await _executeInWorker(_session.address, arguments);+ timer = Timer.periodic(+ const Duration(milliseconds: 500),+ (_) => _reportProgress(),+ );+ final result = await _executeInWorker(_session.address, arguments);+ _reportProgress();+ return result;} finally {+ timer?.cancel();_finished = true;- _callback.close();Bindings.instance.sessionFree(_session);}}+ void _reportProgress() {+ final time = Bindings.instance.sessionProgress(_session);+ if (time >= 0) _onProgress(Duration(microseconds: time));+ }+@overridevoid cancel() {if (!_finished) Bindings.instance.cancel(_session);