-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathQuadPlayerWindow.xaml.cs
More file actions
828 lines (708 loc) · 32.6 KB
/
Copy pathQuadPlayerWindow.xaml.cs
File metadata and controls
828 lines (708 loc) · 32.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Threading;
using LibVLCSharp.Shared;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
namespace FeralCode
{
public partial class QuadPlayerWindow : Window
{
// --- NEW: Toggle this to false to disable logging! ---
private bool _enableLogging;
private LibVLC _libVLC;
private LibVLCSharp.Shared.MediaPlayer[] _players = new LibVLCSharp.Shared.MediaPlayer[4];
private LibVLCSharp.WPF.VideoView[] _views;
private Border[] _borders;
private TextBlock[] _errorTexts;
private int _activeIndex = 0;
private int _totalActive = 0;
public int TotalActive => _totalActive;
public int ActiveIndex => _activeIndex;
private bool _isFullscreen = true;
private DispatcherTimer _audioTimer;
private DispatcherTimer _uiTimer;
private DateTime _lastMouseMove = DateTime.MinValue;
private Window? _glassOverlay;
// --- NEW: Watchdog & Cast Trackers ---
private bool _isWaitingForCast = false;
[DllImport("user32.dll")]
private static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, int dwExtraInfo);
private const byte VK_LWIN = 0x5B;
private const byte VK_K = 0x4B;
private const uint KEYEVENTF_KEYUP = 0x0002;
// --- NEW: Local Logger Helper ---
private void LogDebug(string msg)
{
if (_enableLogging) AppLogger.Log(msg);
}
public QuadPlayerWindow(string baseUrl, List<Channel> channels)
{
// Load settings FIRST so the logger knows if it should be awake!
var initialSettings = SettingsManager.Load();
_enableLogging = initialSettings.EnableDebugLogging;
LogDebug($"QuadPlayerWindow: Initializing with {channels.Count} channels.");
InitializeComponent();
this.Loaded += (s, e) =>
{
this.Activate();
this.Topmost = true;
var settings = SettingsManager.Load();
if (!settings.StartPlayersFullscreen)
{
this.Topmost = false;
if (_isFullscreen) ToggleFullscreen();
}
SetupGlassOverlay();
this.Focus();
// --- ADD THIS: Force layout update AFTER native video players fully load ---
Application.Current.Dispatcher.InvokeAsync(() =>
{
UpdateDynamicLayout();
}, System.Windows.Threading.DispatcherPriority.ContextIdle);
};
_libVLC = MainWindow.SharedLibVLC;
_totalActive = channels.Count;
_views = new[] { View0, View1, View2, View3 };
_borders = new[] { Border0, Border1, Border2, Border3 };
_errorTexts = new[] { ErrorText0, ErrorText1, ErrorText2, ErrorText3 };
_audioTimer = new DispatcherTimer { Interval = TimeSpan.FromMilliseconds(250) };
_audioTimer.Tick += (s, e) =>
{
_audioTimer.Stop();
ApplyAudioFocus();
};
for (int i = 0; i < 4; i++)
{
if (i < channels.Count)
{
int playerIndex = i; // Create local copy for async closure
LogDebug($"QuadPlayerWindow: Setting up player {playerIndex} for CH {channels[playerIndex].Number}");
_players[playerIndex] = new LibVLCSharp.Shared.MediaPlayer(_libVLC);
_views[playerIndex].MediaPlayer = _players[playerIndex];
string audioCodec = "aac";
var settings = SettingsManager.Load();
if (!settings.ForceAacAudio)
{
audioCodec = "copy";
if (double.TryParse(channels[playerIndex].Number, out double chNum) && chNum >= 100 && chNum < 200)
{
audioCodec = "aac";
}
}
string streamUrl = "";
int offsetSeconds = 0;
if (channels[playerIndex].Id != null && channels[playerIndex].Id!.StartsWith("virtual", StringComparison.OrdinalIgnoreCase))
{
var currentAiring = channels[playerIndex].CurrentAirings?.FirstOrDefault(a => a.IsAiringNow);
if (currentAiring != null && !string.IsNullOrWhiteSpace(currentAiring.Source))
{
// Extract the raw file ID from the source path
string fileId = currentAiring.Source.Split('/').Last();
streamUrl = $"{baseUrl.TrimEnd('/')}/dvr/files/{fileId}/hls/master.m3u8";
// Calculate how far into the episode we should jump to simulate "Live TV"
offsetSeconds = (int)(DateTime.Now - currentAiring.StartTime).TotalSeconds;
if (offsetSeconds < 0) offsetSeconds = 0;
LogDebug($"QuadPlayer: Virtual CH {channels[playerIndex].Number} -> File {fileId} @ {offsetSeconds}s");
}
else
{
LogDebug("QuadPlayer: Virtual channel missing guide data. Fallback to default HLS.");
streamUrl = $"{baseUrl.TrimEnd('/')}/devices/ANY/channels/{channels[playerIndex].Number}/hls/master.m3u8?vcodec=copy&acodec={audioCodec}";
}
}
else
{
streamUrl = $"{baseUrl.TrimEnd('/')}/devices/ANY/channels/{channels[playerIndex].Number}/stream.mpg?format=ts&vcodec=copy&acodec={audioCodec}";
}
_players[playerIndex].EncounteredError += (sender, args) =>
{
LogDebug($"VLC CALLBACK: QuadPlayer {playerIndex} EncounteredError.");
// --- SHOW THE ERROR TEXT ---
_ = Application.Current.Dispatcher.InvokeAsync(() =>
{
_errorTexts[playerIndex].Visibility = Visibility.Visible;
});
};
_players[playerIndex].Playing += async (sender, args) =>
{
// --- HIDE THE ERROR TEXT IF IT SUCCESSFULLY PLAYS ---
_ = Application.Current.Dispatcher.InvokeAsync(() =>
{
_errorTexts[playerIndex].Visibility = Visibility.Collapsed;
});
await Task.Delay(1000);
Application.Current.Dispatcher.Invoke(() => ApplyAudioFocus());
await Task.Delay(2500);
Application.Current.Dispatcher.Invoke(() => ApplyAudioFocus());
};
// --- THE FIX: Clean Native Loading & STAGGERED TUNING ---
_ = Task.Run(async () =>
{
try
{
// Stagger each tuner by 1.5 seconds to prevent hardware collisions!
if (playerIndex > 0)
{
await Task.Delay(playerIndex * 1500);
}
// --- NEW: Respect Remux/Transcode Settings ---
bool useTranscode = settings.ForceLocalTranscode ||
(settings.ForcedFfmpegChannels != null && settings.ForcedFfmpegChannels.Contains(channels[playerIndex].Number!));
bool useRemux = settings.ForceLocalRemux ||
(settings.ForcedFfmpegRemuxChannels != null && settings.ForcedFfmpegRemuxChannels.Contains(channels[playerIndex].Number!));
if (useTranscode || useRemux)
{
LogDebug($"QuadPlayer {playerIndex}: Routing through local FFmpeg proxy (Transcode: {useTranscode}).");
streamUrl = await StartFfmpegProxyAsync(playerIndex, streamUrl, offsetSeconds, audioCodec, useTranscode);
}
var media = new Media(_libVLC, new Uri(streamUrl));
// Bump caching to 5 seconds to survive massive OTA signal gaps
media.AddOption(":network-caching=5000");
media.AddOption(":live-caching=5000");
media.AddOption(":avcodec-hw=none");
// --- THE MAGIC BULLET FOR MISSING CLOCKS ---
// Force VLC to draw the video frames even if the timestamps are broken!
media.AddOption(":no-drop-late-frames");
// Prevent corrupted subtitles from natively crashing VLC
media.AddOption(":no-spu");
media.AddOption(":no-sub-autodetect-file");
// Apply the jump-in time for Virtual Channels
if (offsetSeconds > 0)
{
media.AddOption($":start-time={offsetSeconds}");
}
// DO NOT use a 'using' statement here! Let VLC hold the reference.
_players[playerIndex].Play(media);
}
catch (Exception ex)
{
LogDebug($"QuadPlayer {playerIndex} Background Task Error: {ex.Message}");
}
});
}
else
{
_borders[i].Visibility = Visibility.Collapsed;
}
}
DebounceAudioSwitch();
_uiTimer = new DispatcherTimer { Interval = TimeSpan.FromSeconds(3) };
_uiTimer.Tick += UiTimer_Tick;
_uiTimer.Start();
}
private int GetAvailablePort()
{
var listener = new System.Net.Sockets.TcpListener(System.Net.IPAddress.Loopback, 0);
listener.Start();
int port = ((System.Net.IPEndPoint)listener.LocalEndpoint).Port;
listener.Stop();
return port;
}
private async Task<string> StartFfmpegProxyAsync(int playerIndex, string sourceUrl, int offsetSeconds, string targetAudioCodec, bool useTranscode)
{
int dynamicPort = GetAvailablePort();
string ffmpegBindUrl = $"http://127.0.0.1:{dynamicPort}";
string clientUrl = $"http://127.0.0.1:{dynamicPort}/stream.ts";
string appData = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
string localFfmpegPath = System.IO.Path.Combine(appData, "FeralHTPC", "ffmpeg", "ffmpeg.exe");
string targetExecutable = System.IO.File.Exists(localFfmpegPath) ? localFfmpegPath : "ffmpeg";
string audioArgs = targetAudioCodec == "copy" ? "-c:a copy" : "-c:a aac -ac 2";
string videoArgs = useTranscode ? "-vf yadif -c:v libx264 -preset ultrafast -tune zerolatency" : "-c:v copy";
string timeFlags = useTranscode ? "+genpts+igndts+discardcorrupt" : "+genpts+discardcorrupt";
var startInfo = new System.Diagnostics.ProcessStartInfo
{
FileName = targetExecutable,
Arguments = $"-nostdin -hide_banner -loglevel warning -analyzeduration 3000000 -probesize 3000000 -fflags {timeFlags} -i \"{sourceUrl}\" -map 0:V:0? -map 0:a:0? -map 0:s? {videoArgs} {audioArgs} -c:s copy -ignore_unknown -max_muxing_queue_size 4096 -f mpegts -listen 1 {ffmpegBindUrl}",
UseShellExecute = false,
CreateNoWindow = true,
RedirectStandardError = true
};
_ffmpegProcesses[playerIndex] = new System.Diagnostics.Process { StartInfo = startInfo };
_ffmpegProcesses[playerIndex]!.Start();
var sw = System.Diagnostics.Stopwatch.StartNew();
bool isBound = false;
while (sw.ElapsedMilliseconds < 20000)
{
if (_ffmpegProcesses[playerIndex] == null || _ffmpegProcesses[playerIndex]!.HasExited) break;
if (System.Net.NetworkInformation.IPGlobalProperties.GetIPGlobalProperties().GetActiveTcpListeners().Any(l => l.Port == dynamicPort))
{
isBound = true;
break;
}
await Task.Delay(250);
}
return isBound ? clientUrl : sourceUrl; // Fallback to raw stream if FFmpeg fails
}
private void UpdateDynamicLayout()
{
if (_totalActive == 4)
{
PlayerGrid.RowDefinitions.Clear();
PlayerGrid.ColumnDefinitions.Clear();
PlayerGrid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Star) });
PlayerGrid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Star) });
PlayerGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
PlayerGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
SetGridPos(_borders[0], 0, 0, 1, 1, 0, false);
SetGridPos(_borders[1], 0, 1, 1, 1, 0, false);
SetGridPos(_borders[2], 1, 0, 1, 1, 0, false);
SetGridPos(_borders[3], 1, 1, 1, 1, 0, false);
}
else if (_totalActive == 3)
{
PlayerGrid.RowDefinitions.Clear();
PlayerGrid.ColumnDefinitions.Clear();
PlayerGrid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Star) });
PlayerGrid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Star) });
PlayerGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(2, GridUnitType.Star) }); // Large Left
PlayerGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) }); // Small Right
int smallRow = 0;
for (int i = 0; i < 3; i++)
{
if (i == _activeIndex)
{
SetGridPos(_borders[i], 0, 0, 2, 1, 0, false); // Active is large
}
else
{
SetGridPos(_borders[i], smallRow, 1, 1, 1, 0, false); // Inactive are small
smallRow++;
}
}
}
else if (_totalActive == 2)
{
PlayerGrid.RowDefinitions.Clear();
PlayerGrid.ColumnDefinitions.Clear();
PlayerGrid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Star) });
PlayerGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
Border bgBorder = _borders[_activeIndex];
Border pipBorder = _borders[_activeIndex == 0 ? 1 : 0];
// --- THE NATIVE AIRSPACE FIX ---
// Because LibVLC uses native hardware video surfaces, it ignores standard WPF Z-Index.
// We must physically remove and re-add the borders to the Grid to force the OS
// to stack the hardware layers in the correct order (last added = top layer).
if (PlayerGrid.Children.Contains(bgBorder)) PlayerGrid.Children.Remove(bgBorder);
if (PlayerGrid.Children.Contains(pipBorder)) PlayerGrid.Children.Remove(pipBorder);
PlayerGrid.Children.Add(bgBorder); // Renders underneath
PlayerGrid.Children.Add(pipBorder); // Renders on top
SetGridPos(bgBorder, 0, 0, 1, 1, 0, false); // Background Fullscreen
SetGridPos(pipBorder, 0, 0, 1, 1, 99, true); // Foreground PiP
}
}
private void SetGridPos(Border b, int row, int col, int rowSpan, int colSpan, int zIndex, bool isPip)
{
Grid.SetRow(b, row);
Grid.SetColumn(b, col);
Grid.SetRowSpan(b, rowSpan);
Grid.SetColumnSpan(b, colSpan);
Panel.SetZIndex(b, zIndex);
if (isPip)
{
b.HorizontalAlignment = HorizontalAlignment.Right;
b.VerticalAlignment = VerticalAlignment.Bottom;
b.Width = 426; // PiP Width
b.Height = 240; // PiP Height
b.Margin = new Thickness(0, 0, 20, 20);
// --- THE FIX: Force WPF Composition Layering ---
// This tricks the graphics card into strictly obeying the Z-Index!
b.Opacity = 0.99;
}
else
{
b.HorizontalAlignment = HorizontalAlignment.Stretch;
b.VerticalAlignment = VerticalAlignment.Stretch;
b.Width = double.NaN;
b.Height = double.NaN;
b.Margin = new Thickness(0);
// Restore normal hardware rendering for the background
b.Opacity = 1.0;
}
}
private void Overlay_MouseMove(object sender, MouseEventArgs e)
{
if ((DateTime.Now - _lastMouseMove).TotalMilliseconds < 100) return;
_lastMouseMove = DateTime.Now;
if (ControlBar.Visibility != Visibility.Visible)
{
ControlBar.Visibility = Visibility.Visible;
Mouse.OverrideCursor = null; // Restore globally
}
_uiTimer.Stop();
_uiTimer.Start();
}
private void SetupGlassOverlay()
{
LayoutRoot.Children.Remove(OverlayGrid);
_glassOverlay = new Window
{
WindowStyle = WindowStyle.None,
AllowsTransparency = true,
Background = Brushes.Transparent,
ShowInTaskbar = false,
ShowActivated = false,
Owner = this,
Content = OverlayGrid
};
_glassOverlay.PreviewKeyDown += Window_PreviewKeyDown;
// --- NEW: Safety rails for QuadPlayer ---
_glassOverlay.MouseLeave += (s, ev) => Mouse.OverrideCursor = null;
this.Deactivated += (s, ev) => Mouse.OverrideCursor = null;
this.LocationChanged += (s, e) => SyncOverlay();
this.SizeChanged += (s, e) => SyncOverlay();
this.StateChanged += (s, e) => SyncOverlay();
_glassOverlay.Show();
SyncOverlay();
}
private void SyncOverlay()
{
if (_glassOverlay != null)
{
if (this.WindowState == WindowState.Maximized)
{
_glassOverlay.WindowState = WindowState.Maximized;
}
else
{
_glassOverlay.WindowState = WindowState.Normal;
_glassOverlay.Left = this.Left;
_glassOverlay.Top = this.Top;
_glassOverlay.Width = this.ActualWidth;
_glassOverlay.Height = this.ActualHeight;
}
}
}
private void UiTimer_Tick(object? sender, EventArgs e)
{
ControlBar.Visibility = Visibility.Collapsed;
// --- FIX: The native VLC video surface breaks WPF's "IsMouseOver" hit-testing.
// Checking "IsActive" guarantees we only hide the cursor if this window is currently in focus.
if (this.IsActive)
{
System.Windows.Input.Mouse.OverrideCursor = System.Windows.Input.Cursors.None;
}
_uiTimer.Stop();
}
private void Overlay_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (e.ClickCount == 2)
{
ToggleFullscreen();
e.Handled = true;
return;
}
if (sender is FrameworkElement overlay)
{
int clickedIndex = -1;
// If PiP mode, ensure we check the smaller foreground screen first!
if (_totalActive == 2)
{
int pipIndex = _activeIndex == 0 ? 1 : 0;
Point pipPos = e.GetPosition(_borders[pipIndex]);
if (pipPos.X >= 0 && pipPos.X <= _borders[pipIndex].ActualWidth &&
pipPos.Y >= 0 && pipPos.Y <= _borders[pipIndex].ActualHeight)
{
clickedIndex = pipIndex;
}
else
{
clickedIndex = _activeIndex; // Clicked the background
}
}
else
{
for (int i = 0; i < _totalActive; i++)
{
Point bPos = e.GetPosition(_borders[i]);
if (bPos.X >= 0 && bPos.X <= _borders[i].ActualWidth &&
bPos.Y >= 0 && bPos.Y <= _borders[i].ActualHeight)
{
clickedIndex = i;
break;
}
}
}
if (clickedIndex != -1 && clickedIndex < _totalActive && _activeIndex != clickedIndex)
{
_activeIndex = clickedIndex;
DebounceAudioSwitch();
}
Overlay_MouseMove(overlay, e);
}
}
// --- NEW: Auto-Cast Auto-Fullscreen Feature ---
private void CastButton_Click(object sender, RoutedEventArgs e)
{
LogDebug("UI Action: CastButton_Click triggered in QuadPlayer.");
keybd_event(VK_LWIN, 0, 0, 0);
keybd_event(VK_K, 0, 0, 0);
keybd_event(VK_K, 0, KEYEVENTF_KEYUP, 0);
keybd_event(VK_LWIN, 0, KEYEVENTF_KEYUP, 0);
Overlay_MouseMove(null!, null!);
if (!_isWaitingForCast)
{
_isWaitingForCast = true;
Microsoft.Win32.SystemEvents.DisplaySettingsChanged += SystemEvents_DisplaySettingsChanged;
}
}
private void SystemEvents_DisplaySettingsChanged(object? sender, EventArgs e)
{
LogDebug("Event: SystemEvents_DisplaySettingsChanged fired in QuadPlayer (Cast connected)");
Microsoft.Win32.SystemEvents.DisplaySettingsChanged -= SystemEvents_DisplaySettingsChanged;
_isWaitingForCast = false;
_ = Dispatcher.InvokeAsync(async () =>
{
await Task.Delay(2000);
if (!_isFullscreen)
{
ToggleFullscreen();
}
});
}
private void ToggleFullscreen_Click(object sender, RoutedEventArgs e) => ToggleFullscreen();
private void Close_Click(object sender, RoutedEventArgs e) => this.Close();
private void BtnCC_Click(object sender, RoutedEventArgs e) => ToggleClosedCaptions();
private void Mute_Click(object sender, RoutedEventArgs e) => ToggleMute();
public void SetActiveQuadrant(int index)
{
if (index >= 0 && index < _totalActive)
{
_activeIndex = index;
DebounceAudioSwitch();
}
}
private void DebounceAudioSwitch()
{
UpdateDynamicLayout();
for (int i = 0; i < 4; i++)
{
_borders[i].BorderBrush = (i == _activeIndex)
? (Brush)Application.Current.FindResource("StatusConnecting")
: Brushes.Transparent;
}
_audioTimer.Stop();
_audioTimer.Start();
}
private System.Diagnostics.Process?[] _ffmpegProcesses = new System.Diagnostics.Process?[4];
private void ApplyAudioFocus()
{
for (int i = 0; i < _totalActive; i++)
{
if (_players[i] != null)
{
if (i == _activeIndex)
{
// --- THE FIX: Force the volume back up! ---
// VLC remembered the "Volume = 0" state from our previous test.
if (_players[i].Volume == 0)
{
_players[i].Volume = 100;
}
// Cycle mute to force VLC to re-evaluate the audio device
_players[i].Mute = true;
_players[i].Mute = false;
var tracks = _players[i].AudioTrackDescription;
if (tracks != null)
{
foreach (var track in tracks)
{
if (track.Id != -1)
{
_players[i].SetAudioTrack(track.Id);
break;
}
}
}
}
else
{
// Completely destroy the audio track for background players
_players[i].SetAudioTrack(-1);
}
}
}
}
public void ToggleFullscreen()
{
if (!_isFullscreen)
{
this.WindowStyle = WindowStyle.None;
this.ResizeMode = ResizeMode.NoResize;
this.WindowState = WindowState.Maximized;
this.Topmost = true;
_isFullscreen = true;
}
else
{
this.Topmost = false;
this.WindowStyle = WindowStyle.SingleBorderWindow;
this.ResizeMode = ResizeMode.CanResize;
this.WindowState = WindowState.Normal;
_isFullscreen = false;
}
}
public void ToggleMute()
{
if (_players[_activeIndex] != null)
{
_players[_activeIndex].Mute = !_players[_activeIndex].Mute;
}
}
public void VolumeUp()
{
if (_players[_activeIndex] != null)
{
if (_players[_activeIndex].Mute) _players[_activeIndex].Mute = false;
int newVol = _players[_activeIndex].Volume + 10;
_players[_activeIndex].Volume = newVol > 100 ? 100 : newVol;
}
}
public void VolumeDown()
{
if (_players[_activeIndex] != null)
{
int newVol = _players[_activeIndex].Volume - 10;
_players[_activeIndex].Volume = newVol < 0 ? 0 : newVol;
}
}
public void ToggleClosedCaptions()
{
if (_players[_activeIndex] == null) return;
if (_players[_activeIndex].Spu == -1)
{
var spus = _players[_activeIndex].SpuDescription;
if (spus != null)
{
foreach (var track in spus)
{
if (track.Id > -1)
{
_players[_activeIndex].SetSpu(track.Id);
break;
}
}
}
}
else
{
_players[_activeIndex].SetSpu(-1);
}
}
private void Window_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Escape || e.Key == Key.Back || e.Key == Key.BrowserBack)
{
if (_isFullscreen) ToggleFullscreen();
else this.Close();
e.Handled = true;
return;
}
if (e.Key == Key.BrowserHome)
{
if (Application.Current.MainWindow is MainWindow main && main.MainFrame.Content is Page page)
{
page.NavigationService?.Navigate(new StartPage());
}
this.Close();
e.Handled = true;
return;
}
if (e.Key == Key.F || e.Key == Key.F11)
{
ToggleFullscreen();
e.Handled = true;
return;
}
if (e.Key == Key.M)
{
this.WindowState = WindowState.Minimized;
e.Handled = true;
return;
}
if (e.Key == Key.MediaStop)
{
if (_isFullscreen) ToggleFullscreen();
else this.Close();
e.Handled = true;
return;
}
int previousIndex = _activeIndex;
// --- NEW: Dynamic Navigation Logic ---
if (_totalActive == 4)
{
// Original 2x2 logic
if (e.Key == Key.Left) { if (_activeIndex == 1) _activeIndex = 0; else if (_activeIndex == 3) _activeIndex = 2; }
else if (e.Key == Key.Right) { if (_activeIndex == 0) _activeIndex = 1; else if (_activeIndex == 2) _activeIndex = 3; }
else if (e.Key == Key.Up) { if (_activeIndex == 2) _activeIndex = 0; else if (_activeIndex == 3) _activeIndex = 1; }
else if (e.Key == Key.Down) { if (_activeIndex == 0) _activeIndex = 2; else if (_activeIndex == 1) _activeIndex = 3; }
}
else
{
// Simple cycle for 2 or 3 channels (left/up goes back, right/down goes forward)
if (e.Key == Key.Right || e.Key == Key.Down)
{
_activeIndex = (_activeIndex + 1) % _totalActive;
}
else if (e.Key == Key.Left || e.Key == Key.Up)
{
_activeIndex = (_activeIndex - 1 + _totalActive) % _totalActive;
}
}
// -------------------------------------
if (e.Key == Key.VolumeMute) { ToggleMute(); e.Handled = true; return; }
else if (e.Key == Key.VolumeUp) { VolumeUp(); e.Handled = true; return; }
else if (e.Key == Key.VolumeDown) { VolumeDown(); e.Handled = true; return; }
if (previousIndex != _activeIndex)
{
DebounceAudioSwitch();
e.Handled = true;
}
}
protected override void OnClosed(EventArgs e)
{
LogDebug("QuadPlayerWindow: OnClosed triggered. Cleaning up resources.");
Mouse.OverrideCursor = null; // Restore globally
Microsoft.Win32.SystemEvents.DisplaySettingsChanged -= SystemEvents_DisplaySettingsChanged;
_audioTimer?.Stop();
_uiTimer?.Stop();
if (_glassOverlay != null)
{
_glassOverlay.Close();
}
// --- THE FIX: Detach before stopping in background! ---
var playersToDispose = _players.ToArray();
for (int i = 0; i < 4; i++)
{
if (_views[i] != null) _views[i].MediaPlayer = null;
}
Task.Run(() =>
{
// --- NEW: Kill all FFmpeg proxies ---
for (int i = 0; i < 4; i++)
{
try { _ffmpegProcesses[i]?.Kill(); } catch { }
_ffmpegProcesses[i]?.Dispose();
}
for (int i = 0; i < 4; i++)
{
if (playersToDispose[i] != null)
{
if (playersToDispose[i].IsPlaying) playersToDispose[i].Stop();
System.Threading.Thread.Sleep(50); // Let VLC release D3D locks
playersToDispose[i].Dispose();
}
}
LogDebug("QuadPlayerWindow: Background teardown complete.");
});
base.OnClosed(e);
Application.Current.MainWindow?.Show();
Application.Current.MainWindow?.Activate();
}
}
}