From b60efd6acb93621be1e23c4515f01dbac57ff5df Mon Sep 17 00:00:00 2001 From: Michael SCHAL Date: Sat, 20 Dec 2025 16:26:11 +0100 Subject: [PATCH] feat: implement native video player for IPTV live TV, VOD, and series with media_kit, including related UI and settings. --- .../iptv/screens/lite_player_screen.dart | 29 ++++----- .../iptv/screens/native_player_screen.dart | 65 ++++++++++--------- .../iptv/widgets/mobile_live_tv_tab.dart | 44 +++++++++---- .../iptv/widgets/mobile_settings_tab.dart | 12 ++++ .../providers/mobile_settings_providers.dart | 11 ++++ 5 files changed, 101 insertions(+), 60 deletions(-) diff --git a/lib/mobile/features/iptv/screens/lite_player_screen.dart b/lib/mobile/features/iptv/screens/lite_player_screen.dart index 2fbc2f4..3b96872 100644 --- a/lib/mobile/features/iptv/screens/lite_player_screen.dart +++ b/lib/mobile/features/iptv/screens/lite_player_screen.dart @@ -307,19 +307,27 @@ class _LitePlayerScreenState extends ConsumerState if (key == LogicalKeyboardKey.arrowDown || key == LogicalKeyboardKey.channelDown) { if (widget.streamType == StreamType.live) { - _playPrevious(); - _onUserInteraction(); + // Only switch with arrows if controls are hidden + if (key == LogicalKeyboardKey.channelDown || !_showControls) { + _playPrevious(); + _onUserInteraction(); + return true; + } } - return true; + return false; // Let focus system handle it if controls are visible } if (key == LogicalKeyboardKey.arrowUp || key == LogicalKeyboardKey.channelUp) { if (widget.streamType == StreamType.live) { - _playNext(); - _onUserInteraction(); + // Only switch with arrows if controls are hidden + if (key == LogicalKeyboardKey.channelUp || !_showControls) { + _playNext(); + _onUserInteraction(); + return true; + } } - return true; + return false; // Let focus system handle it if controls are visible } if (widget.streamType != StreamType.live) { @@ -524,15 +532,6 @@ class _LitePlayerScreenState extends ConsumerState style: const TextStyle( color: Colors.white, fontWeight: FontWeight.bold)), const Spacer(), - // Aspect Ratio Toggle - TVFocusable( - focusNode: FocusNode(), - onPressed: _cycleAspectRatio, - child: const Padding( - padding: EdgeInsets.symmetric(horizontal: 12), - child: Icon(Icons.aspect_ratio, color: Colors.white70), - ), - ), const SizedBox(width: 16), Text( _currentTime, diff --git a/lib/mobile/features/iptv/screens/native_player_screen.dart b/lib/mobile/features/iptv/screens/native_player_screen.dart index 62d1765..2034622 100644 --- a/lib/mobile/features/iptv/screens/native_player_screen.dart +++ b/lib/mobile/features/iptv/screens/native_player_screen.dart @@ -132,8 +132,16 @@ class _NativePlayerScreenState extends ConsumerState // Enable software decoding fallback if hardware fails (handled by mpv usually, but ensuring 'auto' helps) final decoderMode = ref.read(mobileSettingsProvider).decoderMode; + final deinterlace = ref.read(mobileSettingsProvider).deinterlace; (_player.platform as dynamic)?.setProperty('hwdec', decoderMode); - debugPrint('MediaKitPlayer: Decoder Mode set to $decoderMode'); + (_player.platform as dynamic) + ?.setProperty('deinterlace', deinterlace ? 'yes' : 'no'); + + // Performance & Scaling Fixes for Android + (_player.platform as dynamic)?.setProperty('opengl-pbo', 'yes'); + (_player.platform as dynamic)?.setProperty('video-unscaled', 'no'); + debugPrint( + 'MediaKitPlayer: Decoder Mode set to $decoderMode, Deinterlace: $deinterlace'); // ============ PERFORMANCE 3.0 (ANTI MICRO-COUPURE PROFILE) ============ // Tuned to eliminate micro-stuttering during live TV playback @@ -200,16 +208,16 @@ class _NativePlayerScreenState extends ConsumerState ); // Prevent stalls (readahead already set above) + (_player.platform as dynamic)?.setProperty('hr-seek', 'yes'); + (_player.platform as dynamic)?.setProperty('hr-seek-framedrop', 'yes'); (_player.platform as dynamic) - ?.setProperty('hr-seek', 'yes'); // High-res seeking + ?.setProperty('vd-lavc-dr', 'yes'); // Direct rendering (_player.platform as dynamic) - ?.setProperty('hr-seek-framedrop', 'yes'); // Drop frames to catch up - - // Frame dropping policy - prefer dropping frames over stuttering + ?.setProperty('vd-lavc-fast', 'yes'); // Fast decoding + (_player.platform as dynamic) + ?.setProperty('vd-lavc-threads', '4'); // Multithreading (_player.platform as dynamic)?.setProperty( - 'framedrop', - 'decoder+vo', - ); // Allow frame drops at decoder and video output + 'vd-lavc-skiploopfilter', 'all'); // Skip loop filter for performance // ============ AUDIO CODEC SUPPORT FOR VOD ============ // V3: Explicit LAVC Downmix & Audiotrack @@ -831,21 +839,31 @@ class _NativePlayerScreenState extends ConsumerState } // Up Arrow - Previous channel (Live only) - if (key == LogicalKeyboardKey.arrowUp) { + if (key == LogicalKeyboardKey.arrowUp || + key == LogicalKeyboardKey.channelUp) { if (widget.streamType == StreamType.live && widget.channels != null) { - _playPrevious(); + if (key == LogicalKeyboardKey.channelUp || !_showControls) { + _playPrevious(); + return true; + } } - return true; + return false; // Let focus handle navigation if controls shown } // Down Arrow - Next channel (Live) or Cycle Audio (VOD) - if (key == LogicalKeyboardKey.arrowDown) { + if (key == LogicalKeyboardKey.arrowDown || + key == LogicalKeyboardKey.channelDown) { if (widget.streamType == StreamType.live && widget.channels != null) { - _playNext(); - } else if (widget.streamType != StreamType.live) { + if (key == LogicalKeyboardKey.channelDown || !_showControls) { + _playNext(); + return true; + } + } else if (widget.streamType != StreamType.live && + key == LogicalKeyboardKey.arrowDown) { _cycleAudioTrack(); + return true; } - return true; + return false; // Let focus handle navigation if controls shown } // Cycle Aspect Ratio - secret key 'A' or digit 1 @@ -1288,23 +1306,6 @@ class _NativePlayerScreenState extends ConsumerState ), ), ), - - const SizedBox(width: 32), - - // Aspect Ratio Toggle - TVFocusable( - focusNode: - FocusNode(), // Temporary node or add to state if needed - onPressed: _cycleAspectRatio, - onFocus: _resetControlsTimer, - borderRadius: BorderRadius.circular(50), - child: IconButton( - icon: const Icon(Icons.aspect_ratio, - color: Colors.white, size: 36), - onPressed: _cycleAspectRatio, - tooltip: 'Format d\'image', - ), - ), ], ), ), diff --git a/lib/mobile/features/iptv/widgets/mobile_live_tv_tab.dart b/lib/mobile/features/iptv/widgets/mobile_live_tv_tab.dart index 8358c4c..f4e2085 100644 --- a/lib/mobile/features/iptv/widgets/mobile_live_tv_tab.dart +++ b/lib/mobile/features/iptv/widgets/mobile_live_tv_tab.dart @@ -431,20 +431,38 @@ class _MobileLiveTVTabState extends ConsumerState void _playChannel(BuildContext context, Channel channel, List channels, PlaylistConfig playlist, int index) async { - // Unified Player Strategy: Use LitePlayer (ExoPlayer) for Live TV (Stability) - await Navigator.push( - context, - MaterialPageRoute( - builder: (context) => LitePlayerScreen( - streamId: channel.streamId, - title: channel.name, - playlist: playlist, - streamType: StreamType.live, - channels: channels, // Pass full channel list for zapping - initialIndex: index, + final settings = ref.read(mobileSettingsProvider); + final useNative = settings.playerEngine == 'ultra'; + + if (useNative) { + await Navigator.push( + context, + MaterialPageRoute( + builder: (context) => NativePlayerScreen( + streamId: channel.streamId, + title: channel.name, + playlist: playlist, + streamType: StreamType.live, + channels: channels, // Pass full channel list for zapping + initialIndex: index, + ), ), - ), - ); + ); + } else { + await Navigator.push( + context, + MaterialPageRoute( + builder: (context) => LitePlayerScreen( + streamId: channel.streamId, + title: channel.name, + playlist: playlist, + streamType: StreamType.live, + channels: channels, // Pass full channel list for zapping + initialIndex: index, + ), + ), + ); + } // When returning from player, set flag to prevent PopScope from going back to categories if (mounted) { diff --git a/lib/mobile/features/iptv/widgets/mobile_settings_tab.dart b/lib/mobile/features/iptv/widgets/mobile_settings_tab.dart index 9b9bdf6..c78f8e5 100644 --- a/lib/mobile/features/iptv/widgets/mobile_settings_tab.dart +++ b/lib/mobile/features/iptv/widgets/mobile_settings_tab.dart @@ -145,6 +145,18 @@ class _MobileSettingsTabState extends ConsumerState { }, ), + _buildSettingItem( + icon: Icons.line_weight, + title: 'Désentrelacement', + subtitle: settings.deinterlace + ? 'Activé (Recommandé pour TV en direct)' + : 'Désactivé', + value: settings.deinterlace ? 'Oui' : 'Non', + onTap: () => ref + .read(mobileSettingsProvider.notifier) + .toggleDeinterlace(!settings.deinterlace), + ), + _buildSettingItem( icon: Icons.speed, title: 'Buffer (Cache)', diff --git a/lib/mobile/providers/mobile_settings_providers.dart b/lib/mobile/providers/mobile_settings_providers.dart index 6dd8ca6..870392c 100644 --- a/lib/mobile/providers/mobile_settings_providers.dart +++ b/lib/mobile/providers/mobile_settings_providers.dart @@ -13,6 +13,7 @@ class MobileSettings { final String decoderMode; // 'auto', 'mediacodec' (HW), 'no' (SW) final String playerEngine; // 'ultra' (MPV), 'lite' (ExoPlayer) final String aspectRatioMode; // 'contain', 'cover', 'fill' + final bool deinterlace; final int bufferDuration; // seconds, 0 = auto const MobileSettings({ @@ -24,6 +25,7 @@ class MobileSettings { this.decoderMode = 'auto', this.playerEngine = 'ultra', this.aspectRatioMode = 'contain', + this.deinterlace = false, this.bufferDuration = 0, }); @@ -36,6 +38,7 @@ class MobileSettings { String? decoderMode, String? playerEngine, String? aspectRatioMode, + bool? deinterlace, int? bufferDuration, }) { return MobileSettings( @@ -47,6 +50,7 @@ class MobileSettings { decoderMode: decoderMode ?? this.decoderMode, playerEngine: playerEngine ?? this.playerEngine, aspectRatioMode: aspectRatioMode ?? this.aspectRatioMode, + deinterlace: deinterlace ?? this.deinterlace, bufferDuration: bufferDuration ?? this.bufferDuration, ); } @@ -108,6 +112,7 @@ class MobileSettingsNotifier extends StateNotifier { _box?.get('playerEngine', defaultValue: 'ultra') as String?; final aspectRatioMode = _box?.get('aspectRatioMode', defaultValue: 'contain') as String?; + final deinterlace = _box?.get('deinterlace', defaultValue: false) as bool?; final buffer = _box?.get('bufferDuration', defaultValue: 0) as int?; state = MobileSettings( @@ -119,6 +124,7 @@ class MobileSettingsNotifier extends StateNotifier { decoderMode: decoderMode ?? 'auto', playerEngine: playerEngine ?? 'ultra', aspectRatioMode: aspectRatioMode ?? 'contain', + deinterlace: deinterlace ?? false, bufferDuration: buffer ?? 0, ); } @@ -163,6 +169,11 @@ class MobileSettingsNotifier extends StateNotifier { _box?.put('aspectRatioMode', mode); } + void toggleDeinterlace(bool value) { + state = state.copyWith(deinterlace: value); + _box?.put('deinterlace', value); + } + void setBufferDuration(int seconds) { state = state.copyWith(bufferDuration: seconds); _box?.put('bufferDuration', seconds);