Skip to content

Commit

Permalink
Merge pull request #209 from bdlukaa/sync
Browse files Browse the repository at this point in the history
Late Video
  • Loading branch information
bdlukaa authored Feb 7, 2024
2 parents 244539e + 1eb92f4 commit 4b7e083
Show file tree
Hide file tree
Showing 24 changed files with 565 additions and 264 deletions.
8 changes: 4 additions & 4 deletions lib/api/api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,10 @@ class API {
debugPrint(body);
server.online = false;
}
} catch (exception, stacktrace) {
debugPrint('Failed to checkServerCredentials on server $server');
debugPrint(exception.toString());
debugPrint(stacktrace.toString());
} catch (exception, stack) {
debugPrint(
'Failed to checkServerCredentials on server $server : $exception\n$stack',
);

server.online = false;
}
Expand Down
16 changes: 16 additions & 0 deletions lib/l10n/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@
"timedOut": "TIMED OUT",
"loading": "LOADING",
"recorded": "RECORDED",
"late": "LATE",
"removeFromView": "Remove from view",
"addToView": "Add to view",
"addAllToView": "Add all to view",
Expand Down Expand Up @@ -487,6 +488,21 @@
"contain": "Contain",
"fill": "Fill",
"cover": "Cover",
"@@Late stream behavior": {},
"lateStreamBehavior": "Late stream behavior",
"lateStreamBehaviorDescription": "What to do when a stream is late",
"automaticBehavior": "Automatic",
"automaticBehaviorDescription": "The app will try to reposition the stream automatically",
"manualBehavior": "Manual",
"manualBehaviorDescription": "Press {label} to reposition the stream",
"@manualBehaviorDescription": {
"placeholders": {
"label": {
"type": "String"
}
}
},
"neverBehaviorDescription": "The app will not try to reposition the stream",
"@@LOCALIZATION": {},
"dateLanguage": "Date and Language",
"language": "Language",
Expand Down
16 changes: 16 additions & 0 deletions lib/l10n/app_fr.arb
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@
"timedOut": "EXPIRÉ",
"loading": "CHARGEMENT",
"recorded": "ENREGISTREMENT",
"late": "LATE",
"removeFromView": "Retirer de la vue",
"addToView": "Ajouter à la vue",
"addAllToView": "Tout ajouter à la vue",
Expand Down Expand Up @@ -461,6 +462,21 @@
"contain": "Contenir",
"fill": "Remplir",
"cover": "Couvrir",
"@@Late stream behavior": {},
"lateStreamBehavior": "Late stream behavior",
"lateStreamBehaviorDescription": "What to do when a stream is late",
"automaticBehavior": "Automatic",
"automaticBehaviorDescription": "The app will try to reposition the stream automatically",
"manualBehavior": "Manual",
"manualBehaviorDescription": "Press {label} to reposition the stream",
"@manualBehaviorDescription": {
"placeholders": {
"label": {
"type": "String"
}
}
},
"neverBehaviorDescription": "The app will not try to reposition the stream",
"@@LOCALIZATION": {},
"dateLanguage": "Date et Langue",
"language": "Langue",
Expand Down
16 changes: 16 additions & 0 deletions lib/l10n/app_pl.arb
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@
"timedOut": "PRZEKROCZONY CZAS",
"loading": "ŁADOWANIE",
"recorded": "NAGRANE",
"late": "LATE",
"removeFromView": "Usuń z widoku",
"addToView": "Dodaj do widoku",
"addAllToView": "Dodaj wszystkie do widoku",
Expand Down Expand Up @@ -487,6 +488,21 @@
"contain": "Zawartość",
"fill": "Wypełnienie",
"cover": "Pokrycie",
"@@Late stream behavior": {},
"lateStreamBehavior": "Late stream behavior",
"lateStreamBehaviorDescription": "What to do when a stream is late",
"automaticBehavior": "Automatic",
"automaticBehaviorDescription": "The app will try to reposition the stream automatically",
"manualBehavior": "Manual",
"manualBehaviorDescription": "Press {label} to reposition the stream",
"@manualBehaviorDescription": {
"placeholders": {
"label": {
"type": "String"
}
}
},
"neverBehaviorDescription": "The app will not try to reposition the stream",
"@@LOCALIZATION": {},
"dateLanguage": "Date and Language",
"language": "Language",
Expand Down
16 changes: 16 additions & 0 deletions lib/l10n/app_pt.arb
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@
"timedOut": "EXPIRADO",
"loading": "CARREGANDO",
"recorded": "GRAVADO",
"late": "ATRASADO",
"removeFromView": "Remover do layout",
"addToView": "Adicionar ao layout",
"addAllToView": "Adicionar tudo ao layout",
Expand Down Expand Up @@ -487,6 +488,21 @@
"contain": "Limitar",
"fill": "Preencher",
"cover": "Cobrir",
"@@Late stream behavior": {},
"lateStreamBehavior": "Transmissão atrasada",
"lateStreamBehaviorDescription": "O que fazer quando a transmissão está atrasada.",
"automaticBehavior": "Automático",
"automaticBehaviorDescription": "A transmissão será reajustada automaticamente",
"manualBehavior": "Manual",
"manualBehaviorDescription": "Pressione {label} para reposicionar a transmissão",
"@manualBehaviorDescription": {
"placeholders": {
"label": {
"type": "String"
}
}
},
"neverBehaviorDescription": "A transmissão não será reajustada",
"@@LOCALIZATION": {},
"dateLanguage": "Data e Idioma",
"language": "Idioma",
Expand Down
2 changes: 1 addition & 1 deletion lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ Future<void> main(List<String> args) async {
UpdateManager.ensureInitialized(),
]);

/// Firebase messaging isn't available on windows nor linux
/// Firebase messaging isn't available on Windows nor Linux
if (kIsWeb || isMobilePlatform || Platform.isMacOS) {
FirebaseConfiguration.ensureInitialized();
}
Expand Down
29 changes: 16 additions & 13 deletions lib/providers/desktop_view_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import 'package:bluecherry_client/utils/constants.dart';
import 'package:bluecherry_client/utils/storage.dart';
import 'package:bluecherry_client/utils/video_player.dart';
import 'package:flutter/foundation.dart';
import 'package:unity_video_player/unity_video_player.dart';

class DesktopViewProvider extends UnityProvider {
DesktopViewProvider._();
Expand Down Expand Up @@ -59,19 +60,21 @@ class DesktopViewProvider extends UnityProvider {
@override
Future<void> initialize() async {
await initializeStorage(desktopView, kHiveDesktopLayouts);
for (final device in currentLayout.devices) {
final completer = Completer();
UnityPlayers.players[device.uuid] ??= UnityPlayers.forDevice(
device,
() async {
if (Platform.isLinux) {
await Future.delayed(const Duration(milliseconds: 250));
}
completer.complete();
},
);
await completer.future;
}
await Future.wait(
currentLayout.devices.map<Future>((device) {
final completer = Completer<UnityVideoPlayer>();
UnityPlayers.players[device.uuid] ??= UnityPlayers.forDevice(
device,
() async {
if (Platform.isLinux) {
await Future.delayed(const Duration(milliseconds: 250));
}
completer.complete(UnityPlayers.players[device.uuid]);
},
);
return completer.future;
}),
);
}

/// Saves current layout/order of [Device]s to cache using `package:hive`.
Expand Down
18 changes: 7 additions & 11 deletions lib/providers/mobile_view_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class MobileViewProvider extends UnityProvider {
await super.initializeStorage(mobileView, kHiveMobileView);
for (final device in current) {
if (device != null) {
UnityPlayers.players[device.uuid] = UnityPlayers.forDevice(device);
UnityPlayers.players[device.uuid] ??= UnityPlayers.forDevice(device);
}
}
}
Expand All @@ -99,10 +99,9 @@ class MobileViewProvider extends UnityProvider {
// [Device]s present in the new tab.
final items = devices[value]!;
// Find the non-common i.e. new device tiles in this tab & create a new video player for them.
for (final device in items) {
if (device != null && !UnityPlayers.players.keys.contains(device.uuid)) {
UnityPlayers.players[device.uuid] = UnityPlayers.forDevice(device);
}
for (final device
in items.where((device) => device != null).cast<Device>()) {
UnityPlayers.players[device.uuid] ??= UnityPlayers.forDevice(device);
}
// Remove & dispose the video player instances that will not be used in this new tab.
UnityPlayers.players.removeWhere((deviceUUID, player) {
Expand Down Expand Up @@ -143,7 +142,7 @@ class MobileViewProvider extends UnityProvider {
// Only create new video player instance, if no other camera tile in the same tab is showing the same camera device.
if (!devices[tab]!.contains(device)) {
debugPrint('Added $device');
UnityPlayers.players[device.uuid] = UnityPlayers.forDevice(device);
UnityPlayers.players[device.uuid] ??= UnityPlayers.forDevice(device);
}
devices[tab]![index] = device;
notifyListeners();
Expand All @@ -153,10 +152,7 @@ class MobileViewProvider extends UnityProvider {
/// Replaces a [Device] tile from the camera grid, at specified [tab] [index] with passed [device].
Future<void> replace(int tab, int index, Device device) async {
final current = devices[tab]![index];
var count = 0;
for (final element in devices[tab]!) {
if (element == current) count++;
}
final count = devices[tab]!.where((element) => element == current).length;
// Only dispose if it was the only instance available.
// If some other tile exists showing same camera device, then don't dispose the video player controller.
if (count == 1) {
Expand All @@ -165,7 +161,7 @@ class MobileViewProvider extends UnityProvider {
}
if (!devices[tab]!.contains(device)) {
debugPrint('Replaced $device');
UnityPlayers.players[device.uuid] = UnityPlayers.forDevice(device);
UnityPlayers.players[device.uuid] ??= UnityPlayers.forDevice(device);
}
// Save the new [device] at the position.
devices[tab]![index] = device;
Expand Down
24 changes: 11 additions & 13 deletions lib/providers/server_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -159,20 +159,18 @@ class ServersProvider extends UnityProvider {
notifyListeners();
}

await API.instance.checkServerCredentials(server).then((server) {
return API.instance.getDevices(server).then((devices) {
if (devices != null) {
server.devices
..clear()
..addAll(devices);
}
server = await API.instance.checkServerCredentials(server);
final devices = await API.instance.getDevices(server);
if (devices != null) {
server.devices
..clear()
..addAll(devices);
}

if (loadingServer.contains(server.id)) {
loadingServer.remove(server.id);
notifyListeners();
}
});
});
if (loadingServer.contains(server.id)) {
loadingServer.remove(server.id);
notifyListeners();
}
}));
await save();

Expand Down
45 changes: 43 additions & 2 deletions lib/providers/settings_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ class SettingsProvider extends UnityProvider {
static const kDefaultVideoQuality = RenderingQuality.automatic;
static const kDefaultWakelockEnabled = true;
static const kDefaultBetaMatrixedZoomEnabled = false;
static const kDefaultShowDebugInfo = false;
static const kDefaultLateVideoBehavior = LateVideoBehavior.automatic;

late Locale _locale;
late ThemeMode _themeMode;
Expand All @@ -72,6 +74,8 @@ class SettingsProvider extends UnityProvider {
late RenderingQuality _videoQuality;
late bool _wakelockEnabled;
late bool _betaMatrixedZoomEnabled;
late bool _showDebugInfo;
late LateVideoBehavior _lateVideoBehavior;

// Getters.
Locale get locale => _locale;
Expand All @@ -91,6 +95,8 @@ class SettingsProvider extends UnityProvider {
RenderingQuality get videoQuality => _videoQuality;
bool get wakelockEnabled => _wakelockEnabled;
bool get betaMatrixedZoomEnabled => _betaMatrixedZoomEnabled;
bool get showDebugInfo => _showDebugInfo;
LateVideoBehavior get lateVideoBehavior => _lateVideoBehavior;

// Setters.
set locale(Locale value) {
Expand Down Expand Up @@ -189,6 +195,16 @@ class SettingsProvider extends UnityProvider {
save();
}

set showDebugInfo(bool value) {
_showDebugInfo = value;
save();
}

set lateVideoBehavior(LateVideoBehavior value) {
_lateVideoBehavior = value;
save();
}

/// Initializes the [SettingsProvider] instance & fetches state from `async`
/// `package:hive` method-calls. Called before [runApp].
static Future<SettingsProvider> ensureInitialized() async {
Expand Down Expand Up @@ -217,9 +233,11 @@ class SettingsProvider extends UnityProvider {
kHiveVideoQuality: videoQuality.index,
kHiveWakelockEnabled: wakelockEnabled,
kHiveBetaMatrixedZoom: betaMatrixedZoomEnabled,
kHiveShowDebugInfo: showDebugInfo,
kHiveLateVideoBehavior: lateVideoBehavior.index,
});
} catch (e) {
debugPrint(e.toString());
} catch (error, stack) {
debugPrint('Failed to save settings: $error\n$stack');
}

super.save(notifyListeners: notifyListeners);
Expand Down Expand Up @@ -292,6 +310,9 @@ class SettingsProvider extends UnityProvider {
_wakelockEnabled = data[kHiveWakelockEnabled] ?? kDefaultWakelockEnabled;
_betaMatrixedZoomEnabled =
data[kHiveBetaMatrixedZoom] ?? kDefaultBetaMatrixedZoomEnabled;
_showDebugInfo = data[kHiveShowDebugInfo] ?? kDefaultShowDebugInfo;
_lateVideoBehavior = LateVideoBehavior.values[
data[kHiveLateVideoBehavior] ?? kDefaultLateVideoBehavior.index];

notifyListeners();
}
Expand Down Expand Up @@ -370,3 +391,23 @@ enum StreamingType {
hls,
mjpeg;
}

/// How to handle late video streams.
extension LateVideoBehaviorExtension on LateVideoBehavior {
IconData get icon {
return switch (this) {
LateVideoBehavior.automatic => Icons.auto_awesome,
LateVideoBehavior.manual => Icons.badge,
LateVideoBehavior.never => Icons.close,
};
}

String locale(BuildContext context) {
final loc = AppLocalizations.of(context);
return switch (this) {
LateVideoBehavior.automatic => loc.automaticBehavior,
LateVideoBehavior.manual => loc.manualBehavior,
LateVideoBehavior.never => loc.never,
};
}
}
2 changes: 2 additions & 0 deletions lib/utils/constants.dart
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ const kHiveStreamingProtocol = 'streaming_protocol';
const kHiveVideoQuality = 'video_quality';
const kHiveWakelockEnabled = 'wakelock_enabled';
const kHiveBetaMatrixedZoom = 'matrized_zoom';
const kHiveShowDebugInfo = 'show_debug_info';
const kHiveLateVideoBehavior = 'late_video_behavior';

/// Used as frame buffer size in [DeviceTile], and calculating aspect ratio. Only relevant on desktop.
const kDeviceTileWidth = 640.0;
Expand Down
4 changes: 2 additions & 2 deletions lib/utils/video_player.dart
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class UnityPlayers with ChangeNotifier {

Future<void> setSource() async {
if (device.url != null) {
debugPrint(device.url);
debugPrint('Initializing ${device.url}');
await controller.setDataSource(device.url!);
} else {
final (String source, Future<String> fallback) = switch (
Expand All @@ -92,7 +92,7 @@ class UnityPlayers with ChangeNotifier {
),
StreamingType.mjpeg => (device.mjpegURL, Future.value(device.hlsURL)),
};
debugPrint(source);
debugPrint('Initializing $source');
controller.fallbackUrl = fallback;
await controller.setDataSource(source);

Expand Down
Loading

0 comments on commit 4b7e083

Please sign in to comment.