π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Stream original videos for internal users
Neeraj Gupta9fe1286a5e41
diff --git a/mobile/apps/photos/lib/module/download/progressive_video_stream.dart b/mobile/apps/photos/lib/module/download/progressive_video_stream.dartnew file mode 100644index 0000000000..f0f1e8de9a--- /dev/null+++ b/mobile/apps/photos/lib/module/download/progressive_video_stream.dart@@ -0,0 +1,425 @@+import 'dart:async';+import 'dart:io';+import 'dart:isolate';+import 'dart:math';+import 'dart:typed_data';++import 'package:dio/dio.dart';+import 'package:ente_crypto/ente_crypto.dart';+import 'package:logging/logging.dart';+import 'package:path/path.dart' as p;+import 'package:photos/core/cache/video_cache_manager.dart';+import 'package:photos/core/configuration.dart';+import 'package:photos/core/network/network.dart';+import 'package:photos/models/file/file.dart';+import 'package:photos/models/file/file_type.dart';+import 'package:photos/module/download/file_url.dart';+import 'package:photos/services/collections_service.dart';+import 'package:photos/utils/file_key.dart';++/// One playback attempt. The caller owns disposal, including when open returns+/// null. A null source means the existing full-download path should be used.+class ProgressiveVideoStream {+ static final _logger = Logger('ProgressiveVideo');++ ProgressiveVideoStream(this.file, {required this.onProgress}) {+ _source.future.ignore();+ _failure.future.ignore();+ _commands.future.ignore();+ }++ final EnteFile file;+ final void Function(int, int) onProgress;+ final _source = Completer<String?>();+ final _failure = Completer<void>();+ final _commands = Completer<SendPort>();+ final _exited = Completer<void>();+ final _events = ReceivePort();+ final _urlRequest = CancelToken();+ Isolate? _worker;+ Directory? _directory;+ Future<void>? _opening, _closing, _caching;+ String? url;++ /// Remains pending after download completion so player errors can still cause+ /// fallback. Disposal completes it normally; download/player errors reject it.+ Future<void> get failure => _failure.future;++ Future<String?> open() async {+ _opening = _start();+ await _opening;+ return _source.future;+ }++ Future<void> _start() async {+ final extension = p.extension(file.title ?? '').toLowerCase();+ final size = file.fileSize;+ final token = Configuration.instance.getToken();+ if (file.fileType != FileType.video ||+ !{'.mp4', '.mov', '.m4v'}.contains(extension) ||+ size == null ||+ size <= 0 ||+ token == null ||+ file.uploadedFileID == null ||+ file.collectionID == null ||+ file.fileDecryptionHeader == null ||+ CollectionsService.instance.isSharedPublicLink(file.collectionID!)) {+ _source.complete(null);+ return;+ }+ if (await VideoCacheManager.instance.getFileFromCache(file.downloadUrl) !=+ null) {+ if (!_source.isCompleted) {+ _source.complete(null);+ }+ return;+ }+ if (_closing != null) {+ return;+ }+ final signedUrl = await FileUrl.tryGetV3Url(+ NetworkClient.instance.enteDio,+ file.uploadedFileID!,+ FileUrlType.download,+ headers: {'X-Auth-Token': token},+ cancelToken: _urlRequest,+ );+ if (_closing != null) {+ return;+ }+ _directory = await Directory(+ Configuration.instance.getTempDirectory(),+ ).createTemp('video-');+ if (_closing != null) {+ return;+ }+ final output = '${_directory!.path}/original$extension';+ _events.listen((dynamic event) {+ switch (event) {+ case final SendPort port:+ _commands.complete(port);+ case (source: final String source):+ if (_closing == null && !_source.isCompleted) {+ url = source;+ _source.complete(source);+ }+ case (received: final int count):+ if (_closing == null) {+ onProgress(count, size);+ }+ case (complete: true):+ if (_closing == null) {+ _caching = _cache(output, extension.substring(1));+ }+ case (unsupported: true):+ if (!_source.isCompleted) {+ _source.complete(null);+ }+ case (error: final String kind):+ _fail(StateError('Progressive video failed ($kind)'));+ case null:+ _exited.complete();+ if (!_commands.isCompleted) {+ _commands.completeError(StateError('Video worker did not start'));+ }+ if (_closing == null) {+ _fail(StateError('Video worker exited'));+ }+ }+ });+ _worker = await Isolate.spawn(_runVideoServer, (+ events: _events.sendPort,+ source: signedUrl ?? file.downloadUrl,+ token: signedUrl == null ? token : null,+ output: output,+ size: size,+ header: CryptoUtil.base642bin(file.fileDecryptionHeader!),+ key: getFileKey(file),+ ), onExit: _events.sendPort);+ await _commands.future;+ }++ void playbackFailed() => _fail(StateError('Progressive player failed'));++ void _fail(Object error) {+ if (!_source.isCompleted) {+ _source.completeError(error);+ }+ if (!_failure.isCompleted) {+ _failure.completeError(error);+ }+ }++ Future<void> _cache(String output, String extension) async {+ try {+ await VideoCacheManager.instance.putFileStream(+ file.downloadUrl,+ File(output).openRead(),+ fileExtension: extension,+ maxAge: const Duration(days: 365),+ );+ } catch (error) {+ _logger.warning('Cache failed (${error.runtimeType})');+ }+ }++ Future<void> dispose() => _closing ??= _dispose();++ Future<void> _dispose() async {+ _urlRequest.cancel();+ if (!_source.isCompleted) {+ _source.complete(null);+ }+ if (!_failure.isCompleted) {+ _failure.complete();+ }+ await _opening?.catchError((Object _) {});+ if (_worker != null) {+ if (_commands.isCompleted && !_exited.isCompleted) {+ (await _commands.future).send(null);+ }+ try {+ await _exited.future.timeout(const Duration(seconds: 2));+ } on TimeoutException {+ _worker!.kill(priority: Isolate.immediate);+ await _exited.future;+ }+ }+ await _caching;+ _events.close();+ try {+ await _directory?.delete(recursive: true);+ } on FileSystemException catch (error) {+ _logger.warning('Cleanup failed (${error.runtimeType})');+ }+ }+}++typedef _Download = ({+ SendPort events,+ String source,+ String? token,+ String output,+ int size,+ Uint8List header,+ Uint8List key,+});++Future<void> _runVideoServer(_Download download) =>+ _VideoServer(download).run();++class _VideoServer {+ _VideoServer(this.download);+ final _Download download;+ final _client = HttpClient()..connectionTimeout = const Duration(seconds: 30);+ final _commands = ReceivePort();+ final _stopped = Completer<void>();+ Completer<void> _changed = Completer<void>();+ HttpServer? _server;+ int _written = 0;+ bool _closed = false;++ // Every encrypted Ente record adds 17 authentication bytes.+ int get _length =>+ download.size -+ ((download.size + decryptionChunkSize - 1) ~/ decryptionChunkSize) *+ (decryptionChunkSize - encryptionChunkSize);++ void _wakeReaders() {+ _changed.complete();+ _changed = Completer<void>();+ }++ Future<void> run() async {+ download.events.send(_commands.sendPort);+ _commands.listen((_) {+ _closed = true;+ _client.close(force: true);+ _wakeReaders();+ if (!_stopped.isCompleted) {+ _stopped.complete();+ }+ });+ RandomAccessFile? output;+ try {+ CryptoUtil.init();+ output = File(download.output).openSync(mode: FileMode.write);+ _server = await HttpServer.bind(InternetAddress.loopbackIPv4, 0);+ final random = Random.secure();+ final path =+ '/${List.generate(24, (_) => random.nextInt(256).toRadixString(16).padLeft(2, '0')).join()}${p.extension(download.output)}';+ _server!.listen((request) => unawaited(_serve(request, path)));+ final request = await _client.getUrl(Uri.parse(download.source));+ // Legacy authenticated endpoints may redirect. Let the existing downloader+ // handle that path rather than forwarding X-Auth-Token to another host.+ request.followRedirects = download.token == null;+ if (download.token != null) {+ request.headers.set('X-Auth-Token', download.token!);+ }+ final response = await request.close().timeout(+ const Duration(seconds: 30),+ );+ if (response.statusCode != HttpStatus.ok ||+ (response.contentLength >= 0 &&+ response.contentLength != download.size)) {+ throw const HttpException('Unexpected encrypted video response');+ }+ var received = 0;+ final input = response.timeout(const Duration(seconds: 30)).map((bytes) {+ received += bytes.length;+ if (received > download.size) {+ throw const FormatException('Video too long');+ }+ return bytes;+ });+ await for (final bytes in decryptFileStream(+ input,+ header: download.header,+ key: download.key,+ )) {+ if (_closed) {+ return;+ }+ if (_written == 0 && !_hasFrontMovie(bytes)) {+ download.events.send((unsupported: true));+ return;+ }+ output.writeFromSync(bytes);+ final first = _written == 0;+ _written += bytes.length;+ _wakeReaders();+ if (first) {+ download.events.send((+ source: 'http://127.0.0.1:${_server!.port}$path',+ ));+ }+ download.events.send((received: received));+ }+ if (_closed) {+ return;+ }+ if (received != download.size || _written != _length) {+ throw const FormatException('Incomplete video');+ }+ output.closeSync();+ output = null;+ download.events.send((complete: true));+ await _stopped.future;+ } catch (error) {+ if (!_closed) {+ download.events.send((error: error.runtimeType.toString()));+ }+ } finally {+ _closed = true;+ _wakeReaders();+ _client.close(force: true);+ await _server?.close(force: true);+ output?.closeSync();+ _commands.close();+ }+ }++ Future<void> _serve(HttpRequest request, String path) async {+ final response = request.response;+ try {+ if (request.uri.path != path || request.headers.host != '127.0.0.1') {+ response.statusCode = HttpStatus.forbidden;+ return;+ }+ if (request.method != 'GET' && request.method != 'HEAD') {+ response.statusCode = HttpStatus.methodNotAllowed;+ return;+ }+ final header = request.method == 'GET'+ ? request.headers.value('Range')+ : null;+ // Ignore unsupported range syntax (including multiple ranges) with a 200.+ final range = header == null+ ? null+ : RegExp(r'^bytes=(\d*)-(\d*)$').firstMatch(header);+ var start = 0;+ var end = _length;+ if (range != null) {+ final left = int.tryParse(range[1]!);+ final right = int.tryParse(range[2]!);+ if (left == null) {+ start = max(0, end - (right ?? 0));+ } else {+ start = left;+ if (right != null) {+ end = min(end, right + 1);+ }+ }+ if (start >= end) {+ response.statusCode = HttpStatus.requestedRangeNotSatisfiable;+ response.headers.set('Content-Range', 'bytes */$_length');+ return;+ }+ response.statusCode = HttpStatus.partialContent;+ response.headers.set(+ 'Content-Range',+ 'bytes $start-${end - 1}/$_length',+ );+ }+ response.headers+ ..set('Accept-Ranges', 'bytes')+ ..set(+ 'Content-Type',+ download.output.endsWith('.mov') ? 'video/quicktime' : 'video/mp4',+ )+ ..set('Cache-Control', 'no-store')+ ..set('X-Content-Type-Options', 'nosniff');+ response.contentLength = end - start;+ if (request.method == 'HEAD') {+ return;+ }+ while (start < end && !_closed) {+ if (start >= _written) {+ await _changed.future;+ } else {+ final available = min(_written, end);+ await response.addStream(+ File(download.output).openRead(start, available),+ );+ start = available;+ }+ }+ } catch (_) {+ // The player closes range requests when seeking or switching sources.+ } finally {+ try {+ await response.close();+ } catch (_) {}+ }+ }+}++// Keep all metadata in the first record: the native plugin reads it synchronously.+// A moov box after mdat, or one extending beyond this record, uses full download.+bool _hasFrontMovie(Uint8List bytes) {+ final data = ByteData.sublistView(bytes);+ var offset = 0;+ while (offset + 8 <= bytes.length) {+ var size = data.getUint32(offset);+ final type = data.getUint32(offset + 4);+ var headerSize = 8;+ if (size == 1) {+ if (offset + 16 > bytes.length) {+ return false;+ }+ size = data.getUint64(offset + 8);+ headerSize = 16;+ }+ if (size < headerSize || size > bytes.length - offset) {+ return false;+ }+ if (type == 0x6d6f6f76) {+ return true; // moov+ }+ if (type == 0x6d646174) {+ return false; // mdat+ }+ offset += size;+ }+ return false;+}diff --git a/mobile/apps/photos/lib/ui/viewer/file/video_widget_media_kit.dart b/mobile/apps/photos/lib/ui/viewer/file/video_widget_media_kit.dartindex e2f807ccd3..a3d12819bf 100644--- a/mobile/apps/photos/lib/ui/viewer/file/video_widget_media_kit.dart+++ b/mobile/apps/photos/lib/ui/viewer/file/video_widget_media_kit.dart@@ -17,6 +17,7 @@ import "package:photos/models/file/extensions/file_props.dart";import "package:photos/models/file/file.dart";import 'package:photos/module/download/download_error.dart';import "package:photos/module/download/file.dart";+import "package:photos/module/download/progressive_video_stream.dart";import "package:photos/module/download/task.dart";import "package:photos/service_locator.dart";import "package:photos/services/files_service.dart";@@ -76,6 +77,11 @@ class _VideoWidgetMediaKitState extends State<VideoWidgetMediaKit>bool _isGuestView = false;StreamSubscription<StreamSwitchedEvent>? _streamSwitchedSubscription;StreamSubscription<DownloadTask>? _downloadTaskSubscription;+ ProgressiveVideoStream? _progressive;+ Future<void>? _progressiveCleanup;+ int _videoLoad = 0;+ String? _loadedSource;+ StreamSubscription<String>? _playerErrors;final _transformationController = TransformationController();bool _isZooming = false;@@ -86,6 +92,12 @@ class _VideoWidgetMediaKitState extends State<VideoWidgetMediaKit>);super.initState();WidgetsBinding.instance.addObserver(this);+ _playerErrors = player.stream.error.listen((_) {+ final stream = _progressive;+ if (stream != null && stream.url != null && stream.url == _loadedSource) {+ stream.playbackFailed();+ }+ });if (widget.selectedPreview) {loadPreview();@@ -160,13 +172,29 @@ class _VideoWidgetMediaKitState extends State<VideoWidgetMediaKit>}}- void loadPreview() {+ void loadPreview() async {+ final generation = ++_videoLoad;+ final cleanup = _disposeProgressive();+ if (cleanup != null) {+ await cleanup;+ if (!mounted || generation != _videoLoad) {+ return;+ }+ }final preview = widget.preview;if (preview == null) return;_setVideoController(preview.path);}- void loadOriginal() {+ void loadOriginal() async {+ final generation = ++_videoLoad;+ final cleanup = _disposeProgressive();+ if (cleanup != null) {+ await cleanup;+ if (!mounted || generation != _videoLoad) {+ return;+ }+ }if (widget.file.isRemoteOnlyFile) {_loadNetworkVideo();_setFileSizeIfNull();@@ -178,16 +206,22 @@ class _VideoWidgetMediaKitState extends State<VideoWidgetMediaKit>_loadNetworkVideo();}} else {- widget.file.getAsset.then((asset) async {+ await widget.file.getAsset.then((asset) async {// Android trash assets may report that they do not exist.- if (asset == null ||- !(await asset.exists || widget.file.isDeviceTrash)) {+ final exists = await asset?.exists ?? false;+ if (!mounted || generation != _videoLoad) {+ return;+ }+ if (asset == null || !(exists || widget.file.isDeviceTrash)) {if (widget.file.uploadedFileID != null) {_loadNetworkVideo();}} else {// ignore: unawaited_futuresasset.getMediaUrl().then((url) {+ if (!mounted || generation != _videoLoad) {+ return;+ }_setVideoController(url ??'https://user-images.githubusercontent.com/28951144/229373695-22f88f13-d18f-4288-9bf1-c3e078d83722.mp4',@@ -209,6 +243,10 @@ class _VideoWidgetMediaKitState extends State<VideoWidgetMediaKit>@overridevoid dispose() {+ _videoLoad++;+ _progressive?.dispose().ignore();+ _progressive = null;+ _playerErrors?.cancel();_streamSwitchedSubscription?.cancel();_guestViewEventSubscription.cancel();pauseVideoSubscription.cancel();@@ -253,7 +291,7 @@ class _VideoWidgetMediaKitState extends State<VideoWidgetMediaKit>}},child: Center(- child: controller != null+ child: controller != null && _loadedSource != null? common.VideoWidget(widget.file,controller!,@@ -276,12 +314,72 @@ class _VideoWidgetMediaKitState extends State<VideoWidgetMediaKit>);}+ Future<void>? _disposeProgressive() {+ final stream = _progressive;+ _progressive = null;+ if (stream == null) {+ return _progressiveCleanup;+ }+ return _progressiveCleanup = player+ .stop()+ .catchError((Object _) {})+ .whenComplete(stream.dispose);+ }+void _loadNetworkVideo() {+ if (flagService.progressiveOriginalVideoPlayback) {+ unawaited(_loadProgressively());+ } else {+ _loadNetworkVideoFully();+ }+ }++ Future<void> _loadProgressively() async {+ final generation = _videoLoad;+ bool isCurrent() => mounted && generation == _videoLoad;+ final stream = ProgressiveVideoStream(+ widget.file,+ onProgress: (count, total) {+ if (isCurrent()) {+ _progressNotifier.value = count / total;+ }+ },+ );+ _progressive = stream;+ try {+ final url = await stream.open();+ if (!isCurrent()) {+ return;+ }+ if (url != null) {+ _setVideoController(url);+ await stream.failure;+ return;+ }+ } catch (error) {+ if (isCurrent()) {+ _logger.info('Progressive playback unavailable (${error.runtimeType})');+ }+ }+ if (!isCurrent()) {+ return;+ }+ setState(() => _loadedSource = null);+ await _disposeProgressive();+ if (!isCurrent()) {+ return;+ }+ _loadNetworkVideoFully(generation: generation);+ }++ void _loadNetworkVideoFully({int? generation}) {+ bool isCurrent() =>+ mounted && (generation == null || generation == _videoLoad);getFileFromServer(widget.file,throwOnDecryptionFailure: true,progressCallback: (count, total) {- if (!mounted) {+ if (!isCurrent()) {return;}_progressNotifier.value = count / (widget.file.fileSize ?? total);@@ -293,12 +391,14 @@ class _VideoWidgetMediaKitState extends State<VideoWidgetMediaKit>},).then((file) {- if (file != null) {+ if (file != null && isCurrent()) {_setVideoController(file.path);}}).onError((error, stackTrace) {- if (!mounted) return;+ if (!mounted || !isCurrent()) {+ return;+ }if (error is DownloadDecryptionError) {showDownloadDecryptionFailedDialog(context: context);} else {@@ -336,7 +436,16 @@ class _VideoWidgetMediaKitState extends State<VideoWidgetMediaKit>controller = VideoController(player);}_applyVolume();- player.open(Media(url), play: _isAppInFG && widget.isActive);+ _loadedSource = url;+ final stream = _progressive?.url == url ? _progressive : null;+ player.open(Media(url), play: _isAppInFG && widget.isActive).catchError(+ (Object error, StackTrace stack) {+ if (stream == null) {+ Error.throwWithStackTrace(error, stack);+ }+ stream.playbackFailed();+ },+ );});int duration = controller!.player.state.duration.inSeconds;if (duration == 0) {diff --git a/mobile/apps/photos/lib/ui/viewer/file/video_widget_native.dart b/mobile/apps/photos/lib/ui/viewer/file/video_widget_native.dartindex c683ffed32..5cb6a08aeb 100644--- a/mobile/apps/photos/lib/ui/viewer/file/video_widget_native.dart+++ b/mobile/apps/photos/lib/ui/viewer/file/video_widget_native.dart@@ -20,6 +20,7 @@ import "package:photos/models/file/file.dart";import "package:photos/models/preview/playlist_data.dart";import 'package:photos/module/download/download_error.dart';import "package:photos/module/download/file.dart";+import "package:photos/module/download/progressive_video_stream.dart";import "package:photos/module/download/task.dart";import "package:photos/module/metadata/video.dart";import "package:photos/service_locator.dart";@@ -96,6 +97,13 @@ class _VideoWidgetNativeState extends State<VideoWidgetNative>StreamSubscription<PlaybackEvent>? _subscription;StreamSubscription<StreamSwitchedEvent>? _streamSwitchedSubscription;StreamSubscription<DownloadTask>? downloadTaskSubscription;+ ProgressiveVideoStream? _progressive;+ Future<void>? _progressiveCleanup;+ int _videoLoad = 0;+ int _nativePlayerVersion = 0;+ String? _loadedProgressiveUrl;+ bool get _isProgressiveSource =>+ _filePath?.startsWith('http://127.0.0.1:') ?? false;final _transformationController = TransformationController();bool _isZooming = false;OverlayEntry? _longPressSpeedIndicatorEntry;@@ -166,7 +174,6 @@ class _VideoWidgetNativeState extends State<VideoWidgetNative>event.type != PlayerType.nativeVideoPlayer) {return;}- _filePath = null;if (event.selectedPreview) {loadPreview(update: true);} else {@@ -209,8 +216,11 @@ class _VideoWidgetNativeState extends State<VideoWidgetNative>}final videoSource = VideoSource(path: _filePath!,- type: VideoSourceType.file,+ type: _isProgressiveSource+ ? VideoSourceType.network+ : VideoSourceType.file,);+ _loadedProgressiveUrl = _isProgressiveSource ? _filePath : null;await _controller?.loadVideo(videoSource);await _applyVolume();await _controller?.setPlaybackSpeed(widget.playbackSpeed.value);@@ -220,6 +230,14 @@ class _VideoWidgetNativeState extends State<VideoWidgetNative>}void loadPreview({bool update = false}) async {+ final generation = ++_videoLoad;+ final cleanup = _disposeProgressive();+ if (cleanup != null) {+ await cleanup;+ if (!mounted || generation != _videoLoad) {+ return;+ }+ }final previewPath = widget.playlistData?.preview.path;if (previewPath == null) {loadOriginal(update: true);@@ -231,6 +249,15 @@ class _VideoWidgetNativeState extends State<VideoWidgetNative>}void loadOriginal({bool update = false}) async {+ final generation = ++_videoLoad;+ final cleanup = _disposeProgressive();+ if (cleanup != null) {+ await cleanup;+ if (!mounted || generation != _videoLoad) {+ return;+ }+ }+ setState(() => _filePath = null);if (widget.file.isRemoteOnlyFile) {_loadNetworkVideo(update);_setFileSizeIfNull();@@ -244,14 +271,20 @@ class _VideoWidgetNativeState extends State<VideoWidgetNative>} else {await widget.file.getAsset.then((asset) async {// Android trash assets may report that they do not exist.- if (asset == null ||- !(await asset.exists || widget.file.isDeviceTrash)) {+ final exists = await asset?.exists ?? false;+ if (!mounted || generation != _videoLoad) {+ return;+ }+ if (asset == null || !(exists || widget.file.isDeviceTrash)) {if (widget.file.uploadedFileID != null) {_loadNetworkVideo(update);}} else {// ignore: unawaited_futuresgetFile(widget.file, isOrigin: true).then((file) {+ if (!mounted || generation != _videoLoad) {+ return;+ }if (file == null) {_loadNetworkVideo(update);return;@@ -264,7 +297,7 @@ class _VideoWidgetNativeState extends State<VideoWidgetNative>}});}- if (update) {+ if (update && mounted && generation == _videoLoad) {await setVideoSource();}}@@ -280,6 +313,9 @@ class _VideoWidgetNativeState extends State<VideoWidgetNative>@overridevoid dispose() {+ _videoLoad++;+ _progressive?.dispose().ignore();+ _progressive = null;_longPressSpeedIndicatorEntry?.remove();widget.playbackSpeed.removeListener(_onPlaybackSpeedChanged);_subscription?.cancel();@@ -399,6 +435,7 @@ class _VideoWidgetNativeState extends State<VideoWidgetNative>child: AspectRatio(aspectRatio: aspectRatio ?? 1,child: NativeVideoPlayerView(+ key: ValueKey(_nativePlayerVersion),onViewReady: _initializeController,),),@@ -641,6 +678,12 @@ class _VideoWidgetNativeState extends State<VideoWidgetNative>}void _onError(String errorMessage) {+ if (_loadedProgressiveUrl != null) {+ if (_progressive?.url == _loadedProgressiveUrl) {+ _progressive?.playbackFailed();+ }+ return;+ }_logger.severe("Error in native video player controller for file gen id: ${widget.file.generatedID}",);@@ -650,6 +693,10 @@ class _VideoWidgetNativeState extends State<VideoWidgetNative>Future<void> _onPlaybackReady() async {if (_isPlaybackReady.value) return;+ if (_loadedProgressiveUrl != null &&+ _progressive?.url != _loadedProgressiveUrl) {+ return;+ }await _applyVolume();await _controller?.setPlaybackSpeed(widget.playbackSpeed.value);await _syncPlayback();@@ -689,12 +736,90 @@ class _VideoWidgetNativeState extends State<VideoWidgetNative>}}+ Future<void>? _disposeProgressive() {+ final stream = _progressive;+ _progressive = null;+ if (stream == null) {+ return _progressiveCleanup;+ }+ return _progressiveCleanup = (_controller?.stop() ?? Future<void>.value())+ .catchError((Object _) {})+ .whenComplete(stream.dispose);+ }+void _loadNetworkVideo(bool update) {+ // The native view needs metadata before its asynchronous player is ready.+ if (flagService.progressiveOriginalVideoPlayback &&+ widget.file.hasDimensions &&+ (widget.file.duration ?? 0) > 0) {+ unawaited(_loadProgressively(update));+ } else {+ _loadNetworkVideoFully(update);+ }+ }++ Future<void> _loadProgressively(bool update) async {+ final generation = _videoLoad;+ bool isCurrent() => mounted && generation == _videoLoad;+ final stream = ProgressiveVideoStream(+ widget.file,+ onProgress: (count, total) {+ if (isCurrent()) {+ _progressNotifier.value = count / total;+ }+ },+ );+ _progressive = stream;+ try {+ final url = await stream.open();+ if (!isCurrent()) {+ return;+ }+ if (url != null) {+ _setFilePathForNativePlayer(url, update);+ await stream.failure;+ return;+ }+ } catch (error) {+ if (isCurrent()) {+ _logger.info('Progressive playback unavailable (${error.runtimeType})');+ }+ }+ if (!isCurrent()) {+ return;+ }+ _isPlaybackReady.value = false;+ await _disposeProgressive();+ if (!isCurrent()) {+ return;+ }+ if (Platform.isIOS && stream.url != null) {+ // iOS reports readiness on AVPlayer, not each replacement item.+ _debouncer.cancelDebounceTimer();+ unawaited(_subscription?.cancel());+ _subscription = null;+ _controller?.dispose();+ _controller = null;+ _isSeeking.removeListener(_seekListener);+ setState(() {+ _filePath = null;+ _nativePlayerVersion++;+ });+ }+ _loadNetworkVideoFully(+ update || stream.url != null,+ generation: generation,+ );+ }++ void _loadNetworkVideoFully(bool update, {int? generation}) {+ bool isCurrent() =>+ mounted && (generation == null || generation == _videoLoad);getFileFromServer(widget.file,throwOnDecryptionFailure: true,progressCallback: (count, total) {- if (!mounted) {+ if (!isCurrent()) {return;}_progressNotifier.value = count / (widget.file.fileSize ?? total);@@ -706,12 +831,14 @@ class _VideoWidgetNativeState extends State<VideoWidgetNative>},).then((file) {- if (file != null) {+ if (file != null && isCurrent()) {_setFilePathForNativePlayer(file.path, update);}}).onError((error, stackTrace) {- if (!mounted) return;+ if (!mounted || !isCurrent()) {+ return;+ }if (error is DownloadDecryptionError) {showDownloadDecryptionFailedDialog(context: context);} else {@@ -813,6 +940,11 @@ class _VideoWidgetNativeState extends State<VideoWidgetNative>}Future<void> _setAspectRatioFromVideoProps() async {+ if (_isProgressiveSource) {+ aspectRatio = widget.file.width / widget.file.height;+ duration = secondsToDuration(widget.file.duration!);+ return;+ }if (aspectRatio != null && duration != null) return;if (widget.playlistData != null && widget.selectedPreview) {diff --git a/mobile/apps/photos/plugins/ente_crypto/lib/ente_crypto.dart b/mobile/apps/photos/plugins/ente_crypto/lib/ente_crypto.dartindex 02b6d0bb58..76f645bf2d 100644--- a/mobile/apps/photos/plugins/ente_crypto/lib/ente_crypto.dart+++ b/mobile/apps/photos/plugins/ente_crypto/lib/ente_crypto.dart@@ -1,4 +1,5 @@export 'src/crypto.dart';+export 'src/decrypt_file_stream.dart';export 'src/models/derived_key_result.dart';export 'src/models/encryption_result.dart';export 'src/models/errors.dart';diff --git a/mobile/apps/photos/plugins/ente_crypto/lib/src/decrypt_file_stream.dart b/mobile/apps/photos/plugins/ente_crypto/lib/src/decrypt_file_stream.dartnew file mode 100644index 0000000000..fb3192e14f--- /dev/null+++ b/mobile/apps/photos/plugins/ente_crypto/lib/src/decrypt_file_stream.dart@@ -0,0 +1,64 @@+import 'dart:ffi';+import 'dart:math';+import 'dart:typed_data';++import 'package:ente_crypto/src/crypto.dart';+import 'package:ffi/ffi.dart';+import 'package:flutter_sodium/flutter_sodium.dart';++/// Yields authenticated Ente file records. Run on a worker isolate: each pull+/// synchronously decrypts up to 4 MiB. Cancelling the stream releases its state.+Stream<Uint8List> decryptFileStream(+ Stream<List<int>> input, {+ required Uint8List header,+ required Uint8List key,+}) async* {+ final state = Sodium.cryptoSecretstreamXchacha20poly1305InitPull(header, key);+ final buffer = Uint8List(decryptionChunkSize);+ var buffered = 0;+ var finalRecord = false;++ Uint8List pull() {+ if (finalRecord) {+ throw const FormatException('Data after final encrypted record');+ }+ if (buffered < Sodium.cryptoSecretstreamXchacha20poly1305Abytes) {+ throw const FormatException('Truncated encrypted record');+ }+ final result = Sodium.cryptoSecretstreamXchacha20poly1305Pull(+ state,+ Uint8List.sublistView(buffer, 0, buffered),+ null,+ );+ finalRecord =+ result.tag == Sodium.cryptoSecretstreamXchacha20poly1305TagFinal;+ buffered = 0;+ return result.m;+ }++ try {+ await for (final bytes in input) {+ var offset = 0;+ while (offset < bytes.length) {+ final count = min(buffer.length - buffered, bytes.length - offset);+ buffer.setRange(buffered, buffered + count, bytes, offset);+ buffered += count;+ offset += count;+ if (buffered == buffer.length) {+ yield pull();+ }+ }+ }+ if (buffered > 0) {+ yield pull();+ }+ if (!finalRecord) {+ throw const FormatException('Missing final encrypted record');+ }+ } finally {+ state+ .asTypedList(Sodium.cryptoSecretstreamXchacha20poly1305Statebytes)+ .fillRange(0, Sodium.cryptoSecretstreamXchacha20poly1305Statebytes, 0);+ calloc.free(state);+ }+}diff --git a/mobile/apps/photos/plugins/ente_crypto/pubspec.yaml b/mobile/apps/photos/plugins/ente_crypto/pubspec.yamlindex bf27c961bb..12ab97748e 100644--- a/mobile/apps/photos/plugins/ente_crypto/pubspec.yaml+++ b/mobile/apps/photos/plugins/ente_crypto/pubspec.yaml@@ -12,6 +12,7 @@ dependencies:ref: 82e365fed8a1a76f6eea0220de98389eed7b0445convert: 3.1.2crypto: 3.0.7+ ffi: 2.2.0flutter:sdk: flutterflutter_sodium:diff --git a/mobile/packages/feature_flag/lib/src/flag_service.dart b/mobile/packages/feature_flag/lib/src/flag_service.dartindex a77d2de476..93155bce5a 100644--- a/mobile/packages/feature_flag/lib/src/flag_service.dart+++ b/mobile/packages/feature_flag/lib/src/flag_service.dart@@ -101,6 +101,8 @@ class FlagService {bool get videoEditorSpeedEnabled => internalUser;+ bool get progressiveOriginalVideoPlayback => internalUser;+bool get facesTimeline => true;bool get ritualsFlag => true;