π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Move Photos casting to the owned transport
Neeraj Guptabb4859c9a179
diff --git a/mobile/apps/photos/lib/ui/cast/auto.dart b/mobile/apps/photos/lib/ui/cast/auto.dartdeleted file mode 100644index 4d47ab70a2..0000000000--- a/mobile/apps/photos/lib/ui/cast/auto.dart+++ /dev/null@@ -1,122 +0,0 @@-import "dart:io";--import "package:ente_cast/ente_cast.dart";-import "package:flutter/material.dart";-import "package:photos/generated/l10n.dart";-import "package:photos/service_locator.dart";-import "package:photos/theme/ente_theme.dart";-import "package:photos/ui/common/loading_widget.dart";-import "package:photos/utils/dialog_util.dart";--class AutoCastDialog extends StatefulWidget {- // async method that takes string as input- // and returns void- final void Function(String) onConnect;- const AutoCastDialog(this.onConnect, {super.key});-- @override- State<AutoCastDialog> createState() => _AutoCastDialogState();-}--class _AutoCastDialogState extends State<AutoCastDialog> {- final Set<Object> _isDeviceTapInProgress = {};-- @override- Widget build(BuildContext context) {- final l10n = AppLocalizations.of(context);- final textStyle = getEnteTextTheme(context);- final AlertDialog alert = AlertDialog(- title: Text(l10n.connectToDevice, style: textStyle.largeBold),- content: Column(- crossAxisAlignment: CrossAxisAlignment.start,- mainAxisSize: MainAxisSize.min,- children: [- Text(l10n.autoCastDialogBody, style: textStyle.bodyMuted),- if (Platform.isIOS)- Text(l10n.autoCastiOSPermission, style: textStyle.bodyMuted),- const SizedBox(height: 16),- FutureBuilder(- future: castService.searchDevices(),- builder: (_, snapshot) {- if (snapshot.hasError) {- return Center(- child: Text('Error: ${snapshot.error.toString()}'),- );- } else if (!snapshot.hasData) {- return const EnteLoadingWidget();- }-- if (snapshot.data!.isEmpty) {- return Center(child: Text(l10n.noDeviceFound));- }-- return Column(- crossAxisAlignment: CrossAxisAlignment.start,- children: snapshot.data!.map((result) {- final device = result.$2;- final name = result.$1;- return GestureDetector(- onTap: () async {- if (_isDeviceTapInProgress.contains(device)) {- return;- }- setState(() {- _isDeviceTapInProgress.add(device);- });- try {- await _connectToYourApp(context, device);- } catch (e) {- if (mounted) {- setState(() {- _isDeviceTapInProgress.remove(device);- });- if (!context.mounted) return;- showGenericErrorDialog(- context: context,- error: e,- ).ignore();- }- }- },- child: Padding(- padding: const EdgeInsets.symmetric(vertical: 8.0),- child: Row(- children: [- Expanded(child: Text(name)),- if (_isDeviceTapInProgress.contains(device))- const EnteLoadingWidget(),- ],- ),- ),- );- }).toList(),- );- },- ),- ],- ),- );- return alert;- }-- Future<void> _connectToYourApp(- BuildContext context,- Object castDevice,- ) async {- await castService.connectDevice(- context,- castDevice,- onMessage: (message) {- if (message.containsKey(CastMessageType.pairCode)) {- final code = message[CastMessageType.pairCode]!['code'];- widget.onConnect(code);- }- if (mounted) {- setState(() {- _isDeviceTapInProgress.remove(castDevice);- });- }- },- );- }-}diff --git a/mobile/apps/photos/lib/ui/cast/cast.dart b/mobile/apps/photos/lib/ui/cast/cast.dartindex c018002b94..433fddd952 100644--- a/mobile/apps/photos/lib/ui/cast/cast.dart+++ b/mobile/apps/photos/lib/ui/cast/cast.dart@@ -1,8 +1,6 @@import "dart:async";-import "dart:io";import "package:ente_components/ente_components.dart";-import "package:flutter/foundation.dart";import "package:flutter/widgets.dart";import "package:hugeicons/hugeicons.dart";import "package:logging/logging.dart";@@ -21,7 +19,7 @@ Future<void> showCastSheet(BuildContext context, Collection collection) async {final l10n = AppLocalizations.of(context);final textStyle = getEnteTextTheme(context);final gw = CastGateway(NetworkClient.instance.enteDio);- final showAutoPair = Platform.isAndroid || kDebugMode;+ final showAutoPair = castService.isSupported;if (!flagService.enableMultiCast) {if (castService.getActiveSessions().isNotEmpty) {await showChoiceDialog(@@ -38,8 +36,6 @@ Future<void> showCastSheet(BuildContext context, Collection collection) async {return;}unawaited(gw.revokeAllTokens());- } else {- await castService.closeActiveCasts();}final logger = Logger("showCastSheet");List<CastInfo> sessions = [];diff --git a/mobile/apps/photos/lib/ui/cast/pair_with_auto.dart b/mobile/apps/photos/lib/ui/cast/pair_with_auto.dartindex de446ac73c..3bff3b4edd 100644--- a/mobile/apps/photos/lib/ui/cast/pair_with_auto.dart+++ b/mobile/apps/photos/lib/ui/cast/pair_with_auto.dart@@ -1,6 +1,5 @@import "dart:io";-import "package:ente_cast/ente_cast.dart";import "package:ente_components/ente_components.dart";import "package:flutter/material.dart";import "package:hugeicons/hugeicons.dart";@@ -20,11 +19,7 @@ class _DeviceNotFoundException implements Exception {const _DeviceNotFoundException();}-Future<void> _pairWithAuto(- BuildContext context,- Collection collection,- String code,-) async {+Future<void> _pairWithAuto(Collection collection, String code) async {final gw = CastGateway(NetworkClient.instance.enteDio);final publicKey = await gw.getPublicKey(code);if (publicKey == null) {@@ -62,6 +57,13 @@ class _PairWithAutoSheet extends StatefulWidget {class _PairWithAutoSheetState extends State<_PairWithAutoSheet> {final _devicesInProgress = <Object>{};final _logger = Logger("PairWithAutoSheet");+ late final Future<List<(String, Object)>> _devices;++ @override+ void initState() {+ super.initState();+ _devices = castService.searchDevices();+ }@overrideWidget build(BuildContext context) {@@ -80,56 +82,50 @@ class _PairWithAutoSheetState extends State<_PairWithAutoSheet> {content: Text(body, style: textStyles.smallMuted),actions: [FutureBuilder<List<(String, Object)>>(- future: castService.searchDevices(),+ future: _devices,builder: (_, snapshot) {if (snapshot.hasError) {- return Center(child: Text("Error: ${snapshot.error}"));+ _logger.warning(+ "Failed to discover Cast devices",+ snapshot.error,+ );+ return _statusMessage(l10n.somethingWentWrongPleaseTryAgain);}if (!snapshot.hasData) {return const EnteLoadingWidget(padding: 16);}if (snapshot.data!.isEmpty) {- final colors = context.componentColors;- return Container(- width: double.infinity,- padding: const EdgeInsets.symmetric(- horizontal: Spacing.xl,- vertical: 14,- ),- decoration: BoxDecoration(- color: colors.fillLight,- borderRadius: Radii.buttonBorder,- ),- child: Row(- mainAxisAlignment: MainAxisAlignment.center,- children: [- Image.asset("assets/warning-yellow.png"),- const SizedBox(width: Spacing.sm),- Flexible(- child: Text(- l10n.noDeviceFound,- maxLines: 2,- overflow: TextOverflow.ellipsis,- textAlign: TextAlign.center,- style: TextStyles.bodyBold.copyWith(- color: colors.caution,- ),- ),- ),- ],- ),- );+ return _statusMessage(l10n.noDeviceFound);}return Column(crossAxisAlignment: CrossAxisAlignment.start,children: snapshot.data!.map((result) {final device = result.$2;final name = result.$1;+ final isCasting = castService.isCastingToDevice(device);+ final isInProgress = _devicesInProgress.contains(device);return MenuComponent(title: name,- isDisabled: _devicesInProgress.contains(device),+ subtitle: isCasting ? l10n.stopCastingTitle : null,+ isDisabled: isInProgress,leading: const HugeIcon(icon: HugeIcons.strokeRoundedTvSmart),- onTap: () async => _connectToDevice(context, device),+ trailing: isInProgress+ ? const EnteLoadingWidget(size: 16)+ : isCasting+ ? IconButtonComponent(+ icon: const HugeIcon(+ icon: HugeIcons.strokeRoundedCancel01,+ size: IconSizes.small,+ strokeWidth: 1.6,+ ),+ tooltip: l10n.stopCastingTitle,+ shouldSurfaceExecutionStates: false,+ onTap: () async => _confirmStopCasting(device),+ )+ : null,+ onTap: isCasting+ ? null+ : () async => _connectToDevice(device),);}).toList(),);@@ -139,43 +135,65 @@ class _PairWithAutoSheetState extends State<_PairWithAutoSheet> {);}- Future<void> _connectToDevice(BuildContext context, Object device) async {+ Widget _statusMessage(String message) {+ final colors = context.componentColors;+ return Container(+ width: double.infinity,+ padding: const EdgeInsets.symmetric(horizontal: Spacing.xl, vertical: 14),+ decoration: BoxDecoration(+ color: colors.fillLight,+ borderRadius: Radii.buttonBorder,+ ),+ child: Row(+ mainAxisAlignment: MainAxisAlignment.center,+ children: [+ Image.asset("assets/warning-yellow.png"),+ const SizedBox(width: Spacing.sm),+ Flexible(+ child: Text(+ message,+ maxLines: 2,+ overflow: TextOverflow.ellipsis,+ textAlign: TextAlign.center,+ style: TextStyles.bodyBold.copyWith(color: colors.caution),+ ),+ ),+ ],+ ),+ );+ }++ Future<void> _connectToDevice(Object device) async {if (_devicesInProgress.contains(device)) {return;}setState(() => _devicesInProgress.add(device));try {- await castService.connectDevice(- context,- device,- onMessage: (message) async {- final code = message[CastMessageType.pairCode]?["code"];- if (code is! String) {- return;- }- await _handlePairCode(context, device, code);- },- );- } catch (e, s) {- if (!context.mounted) return;- await _handleError(context, device, e, s);- }- }-- Future<void> _handlePairCode(- BuildContext context,- Object device,- String code,- ) async {- try {- await _pairWithAuto(context, widget.collection, code);+ final code = await castService.connectDevice(device);+ if (!mounted) {+ await castService.stopCastingToDevice(device);+ return;+ }+ try {+ await _pairWithAuto(widget.collection, code);+ } catch (error, stackTrace) {+ try {+ await castService.stopCastingToDevice(device);+ } catch (stopError, stopStackTrace) {+ _logger.warning(+ "Failed to stop Cast after pairing failed",+ stopError,+ stopStackTrace,+ );+ }+ Error.throwWithStackTrace(error, stackTrace);+ }if (mounted) {- if (!context.mounted) return;await Navigator.maybePop(context);}} catch (e, s) {- if (!context.mounted) return;- await _handleError(context, device, e, s);+ if (!mounted) return;+ await _handleError(e, s);} finally {if (mounted) {setState(() => _devicesInProgress.remove(device));@@ -183,17 +201,44 @@ class _PairWithAutoSheetState extends State<_PairWithAutoSheet> {}}- Future<void> _handleError(- BuildContext context,- Object device,- Object error,- StackTrace stackTrace,- ) async {+ Future<void> _confirmStopCasting(Object device) async {+ final l10n = AppLocalizations.of(context);+ await showBottomSheetComponent<void>(+ context: context,+ builder: (sheetContext) => BottomSheetComponent(+ title: l10n.stopCastingTitle,+ message: l10n.stopCastingBody,+ illustration: Image.asset("assets/warning-grey.png"),+ actions: [+ ButtonComponent(+ label: l10n.stopCastingTitle,+ variant: ButtonComponentVariant.critical,+ onTap: () async {+ Navigator.of(sheetContext).pop();+ if (!mounted) return;+ setState(() => _devicesInProgress.add(device));+ try {+ await castService.stopCastingToDevice(device);+ } catch (e, s) {+ _logger.severe("Failed to stop casting", e, s);+ if (!mounted) return;+ await showGenericErrorDialog(context: context, error: e);+ return;+ } finally {+ if (mounted) {+ setState(() => _devicesInProgress.remove(device));+ }+ }+ },+ ),+ ],+ ),+ );+ }++ Future<void> _handleError(Object error, StackTrace stackTrace) async {final l10n = AppLocalizations.of(context);_logger.severe("Failed to pair automatically", error, stackTrace);- if (mounted) {- setState(() => _devicesInProgress.remove(device));- }if (error is CastIPMismatchException) {await showErrorDialog(context,diff --git a/mobile/apps/photos/plugins/ente_cast/lib/ente_cast.dart b/mobile/apps/photos/plugins/ente_cast/lib/ente_cast.dartindex f421a92970..66a7132d8d 100644--- a/mobile/apps/photos/plugins/ente_cast/lib/ente_cast.dart+++ b/mobile/apps/photos/plugins/ente_cast/lib/ente_cast.dart@@ -1,2 +1 @@-export 'src/model.dart';export 'src/service.dart';diff --git a/mobile/apps/photos/plugins/ente_cast/lib/src/model.dart b/mobile/apps/photos/plugins/ente_cast/lib/src/model.dartdeleted file mode 100644index bcc61ec2e1..0000000000--- a/mobile/apps/photos/plugins/ente_cast/lib/src/model.dart+++ /dev/null@@ -1,2 +0,0 @@-// create enum for type of message for cast-enum CastMessageType { pairCode, alreadyCasting }diff --git a/mobile/apps/photos/plugins/ente_cast/lib/src/service.dart b/mobile/apps/photos/plugins/ente_cast/lib/src/service.dartindex f69a77177c..dabe466703 100644--- a/mobile/apps/photos/plugins/ente_cast/lib/src/service.dart+++ b/mobile/apps/photos/plugins/ente_cast/lib/src/service.dart@@ -2,14 +2,21 @@ import "dart:async";import "dart:developer" as dev;import "dart:io";-import "package:cast/cast.dart";-import "package:ente_cast/src/model.dart";+import "package:ente_cast/src/cast/cast_pairing.dart";+import "package:ente_cast/src/cast/chromecast_channel.dart";+import "package:ente_cast/src/cast/chromecast_client.dart";+import "package:ente_cast/src/cast/chromecast_connection.dart";+import "package:ente_cast/src/cast/chromecast_device.dart";+import "package:ente_cast/src/cast/chromecast_discovery.dart";import "package:flutter/foundation.dart";-import "package:flutter/material.dart";class CastService {- final String _appId = "F5BCEC64";- final String _pairRequestNamespace = "urn:x-cast:pair-request";+ static const _appID = "F5BCEC64";+ static const _pairingTimeout = Duration(seconds: 20);++ final _client = ChromecastClient();+ final _sessions = <String, _CastSession>{};+ final _connectionAttempts = <String, Future<String>>{};bool get isSupported => !kIsWeb && (Platform.isAndroid || Platform.isIOS);@@ -18,85 +25,76 @@ class CastService {return [];}- final devices = await CastDiscoveryService().search(+ final devices = await const ChromecastDiscovery().search(timeout: const Duration(seconds: 7),);return devices.map((device) => (device.name, device as Object)).toList();}- Future<void> connectDevice(- BuildContext context,- Object device, {- int? collectionID,- void Function(Map<CastMessageType, Map<String, dynamic>>)? onMessage,- }) async {+ Future<String> connectDevice(Object device, {int? collectionID}) {if (!isSupported) {- throw UnsupportedError("Cast is only supported on Android and iOS");+ return Future.error(+ UnsupportedError("Cast is only supported on Android and iOS"),+ );+ }+ if (device is! ChromecastDevice) {+ return Future.error(ArgumentError.value(device, "device"));}- final castDevice = device as CastDevice;- final session = await CastSessionManager().startSession(castDevice);+ final serviceName = device.serviceName;+ if (_connectionAttempts.containsKey(serviceName)) {+ return Future.error(StateError("Already connecting to ${device.name}"));+ }+ if (_sessions.containsKey(serviceName)) {+ return Future.error(StateError("Already connected to ${device.name}"));+ }- session.messageStream.listen((message) {- if (message["type"] == "RECEIVER_STATUS") {- dev.log(- "got RECEIVER_STATUS, Send request to pair",- name: "CastService",- );- session.sendMessage(_pairRequestNamespace, {- "collectionID": collectionID,- });- } else if (onMessage != null && message.containsKey("code")) {- onMessage({CastMessageType.pairCode: message});- } else if (kDebugMode) {- print("receive message: $message");+ late final Future<String> attempt;+ attempt = _connect(device, collectionID: collectionID).whenComplete(() {+ if (identical(_connectionAttempts[serviceName], attempt)) {+ _connectionAttempts.remove(serviceName);}});+ _connectionAttempts[serviceName] = attempt;+ return attempt;+ }- session.stateStream.listen((state) {- if (state == CastSessionState.connected) {- debugPrint("Send request to pair");- session.sendMessage(_pairRequestNamespace, {});- } else if (state == CastSessionState.closed) {- dev.log("Session closed", name: "CastService");- }- });+ bool isCastingToDevice(Object device) {+ if (!isSupported || device is! ChromecastDevice) {+ return false;+ }+ return _sessions[device.serviceName]?.receiverSessionID != null;+ }- debugPrint("Send request to launch");- session.sendMessage(CastSession.kNamespaceReceiver, {- "type": "LAUNCH",- "appId": _appId,- });+ Future<void> stopCastingToDevice(Object device) async {+ if (!isSupported || device is! ChromecastDevice) {+ return;+ }++ final session = _sessions[device.serviceName];+ if (session == null) {+ return;+ }++ try {+ final receiverSessionID = session.receiverSessionID;+ if (receiverSessionID != null) {+ session.connection.sendToPlatformReceiver(+ ChromecastConnection.receiverNamespace,+ {"type": "STOP", "sessionId": receiverSessionID},+ );+ await session.connection.flush();+ }+ } finally {+ await _disconnectSession(session);+ }}Future<void> closeActiveCasts() async {if (!isSupported) {return;}-- final sessions = CastSessionManager().sessions;- for (final session in sessions) {- debugPrint("send close message for ${session.sessionId}");- unawaited(- Future(() {- try {- session.sendMessage(CastSession.kNamespaceConnection, {- "type": "CLOSE",- });- } catch (error) {- debugPrint("sendMessage failed: $error");- }- }).timeout(- const Duration(seconds: 5),- onTimeout: () {- debugPrint("sendMessage timed out after 5 seconds");- },- ),- );- debugPrint("close session ${session.sessionId}");- unawaited(session.close());- }- CastSessionManager().sessions.clear();+ await Future.wait(_sessions.values.toList().map(_disconnectSession));}Map<String, String> getActiveSessions() {@@ -104,13 +102,112 @@ class CastService {return {};}- final sessions = CastSessionManager().sessions;- final result = <String, String>{};- for (final session in sessions) {- if (session.state == CastSessionState.connected) {- result[session.sessionId] = session.state.toString();+ return {+ for (final session in _sessions.values)+ if (session.receiverSessionID != null)+ session.connection.sourceID: session.connection.state.toString(),+ };+ }++ Future<String> _connect(+ ChromecastDevice device, {+ required int? collectionID,+ }) async {+ final connection = await _client.connect(device);+ final session = _CastSession(device, connection);+ _sessions[device.serviceName] = session;++ session.messageSubscription = connection.messages.listen(+ (message) => _handleMessage(session, message),+ onError: (Object error, StackTrace stackTrace) {+ dev.log(+ "Cast connection failed",+ name: "CastService",+ error: error,+ stackTrace: stackTrace,+ );+ unawaited(_disconnectSession(session));+ },+ cancelOnError: false,+ );+ session.stateSubscription = connection.states.listen((state) {+ if (state == ChromecastConnectionState.closed) {+ dev.log("Session closed", name: "CastService");+ unawaited(_disconnectSession(session));}+ });++ try {+ return await waitForPairCode(+ messages: connection.messages,+ states: connection.states,+ initialState: connection.state,+ start: () => connection.launch(_appID),+ sendPairRequest: () => connection.send(castPairRequestNamespace, {+ "collectionID": ?collectionID,+ }),+ timeout: _pairingTimeout,+ );+ } catch (_) {+ await _disconnectSession(session);+ rethrow;}- return result;+ }++ void _handleMessage(_CastSession session, ChromecastMessage message) {+ if (message.namespace != ChromecastConnection.receiverNamespace ||+ message.payload["type"] != "RECEIVER_STATUS") {+ return;+ }++ final status = message.payload["status"];+ final applications = status is Map ? status["applications"] : null;+ String? receiverSessionID;+ if (applications is List) {+ for (final application in applications) {+ if (application is Map &&+ application["appId"] == _appID &&+ application["sessionId"] is String) {+ receiverSessionID = application["sessionId"] as String;+ break;+ }+ }+ }++ if (receiverSessionID != null) {+ session.receiverSessionID = receiverSessionID;+ } else if (session.receiverSessionID != null) {+ unawaited(_disconnectSession(session));+ }+ }++ Future<void> _disconnectSession(_CastSession session) {+ return session.closeFuture ??= _closeSession(session);+ }++ Future<void> _closeSession(_CastSession session) async {+ if (identical(_sessions[session.device.serviceName], session)) {+ _sessions.remove(session.device.serviceName);+ }+ await session.cancelSubscriptions();+ await _client.disconnect(session.connection);+ }+}++final class _CastSession {+ final ChromecastDevice device;+ final ChromecastConnection connection;+ StreamSubscription<ChromecastMessage>? messageSubscription;+ StreamSubscription<ChromecastConnectionState>? stateSubscription;+ String? receiverSessionID;+ Future<void>? closeFuture;++ _CastSession(this.device, this.connection);++ Future<void> cancelSubscriptions() async {+ await messageSubscription?.cancel();+ await stateSubscription?.cancel();+ messageSubscription = null;+ stateSubscription = null;}}diff --git a/mobile/apps/photos/plugins/ente_cast/pubspec.yaml b/mobile/apps/photos/plugins/ente_cast/pubspec.yamlindex 4310a44c70..062227f6d8 100644--- a/mobile/apps/photos/plugins/ente_cast/pubspec.yaml+++ b/mobile/apps/photos/plugins/ente_cast/pubspec.yaml@@ -6,12 +6,6 @@ environment:flutter: ">=3.38.0"dependencies:- cast:- git:- url: https://github.com/guyluz11/flutter_cast.git- ref: af6378574352884beab6cddec462c7fdfc9a8c35- collection: 1.19.1- dio: 5.9.2flutter:sdk: fluttermulticast_dns: 0.3.3+1diff --git a/mobile/pubspec.lock b/mobile/pubspec.lockindex 15700d7f21..7b2fe511e1 100644--- a/mobile/pubspec.lock+++ b/mobile/pubspec.lock@@ -314,15 +314,6 @@ packages:url: "https://pub.dev"source: hostedversion: "0.3.5+4"- cast:- dependency: transitive- description:- path: "."- ref: af6378574352884beab6cddec462c7fdfc9a8c35- resolved-ref: af6378574352884beab6cddec462c7fdfc9a8c35- url: "https://github.com/guyluz11/flutter_cast.git"- source: git- version: "2.0.9"characters:dependency: transitivedescription: