Files
video-v1/vav1/Vav1Player/MainWindow.xaml.cs
2025-09-18 01:00:04 +09:00

164 lines
5.3 KiB
C#

using System.IO;
using System.Windows;
using Microsoft.Win32;
using Vav1Player.Decoder;
using Vav1Player.Rendering;
using Vav1Player.Video;
namespace Vav1Player;
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private VideoPlayer? _videoPlayer;
private string? _currentVideoFile;
private System.Windows.Threading.DispatcherTimer? _statsTimer;
public MainWindow()
{
InitializeComponent();
InitializeRenderer();
}
private void InitializeRenderer()
{
try
{
string diagnosticReport = DllDiagnostic.RunComprehensiveDiagnostic();
System.Diagnostics.Debug.WriteLine(diagnosticReport);
if (!DllChecker.CheckDav1dDll())
{
string errorMessage = "dav1d.dll validation failed.\n\n" + diagnosticReport;
System.Windows.MessageBox.Show(errorMessage, "DLL Validation Error", MessageBoxButton.OK, MessageBoxImage.Error);
return;
}
var renderer = new WpfVideoRenderer(VideoDisplay);
var decoder = new Dav1dDecoder();
if (!decoder.Initialize())
{
string errorMessage = "Failed to initialize AV1 decoder.\n\n" +
"Possible causes:\n" +
"1. dav1d.dll is missing or cannot be loaded\n" +
"2. Required Visual C++ Redistributables are not installed\n" +
"3. Architecture mismatch (x86 vs x64)\n\n" +
"Please ensure dav1d.dll is in the same directory as the executable.";
System.Windows.MessageBox.Show(errorMessage, "Decoder Initialization Error", MessageBoxButton.OK, MessageBoxImage.Error);
return;
}
_videoPlayer = new VideoPlayer(decoder, renderer);
_statsTimer = new System.Windows.Threading.DispatcherTimer
{
Interval = TimeSpan.FromMilliseconds(100) // Update UI more frequently
};
_statsTimer.Tick += StatsTimer_Tick;
_statsTimer.Start();
System.Diagnostics.Debug.WriteLine("[MainWindow] VideoPlayer initialized successfully");
}
catch (Exception ex)
{
System.Windows.MessageBox.Show($"Failed to initialize renderer: {ex.Message}", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
private async void OpenButton_Click(object sender, RoutedEventArgs e)
{
var openFileDialog = new Microsoft.Win32.OpenFileDialog
{
Filter = "AV1 Video Files (*.webm;*.mkv;*.mp4)|*.webm;*.mkv;*.mp4|All files (*.*)|*.*",
Title = "Select AV1 Video File"
};
if (openFileDialog.ShowDialog() == true)
{
_currentVideoFile = openFileDialog.FileName;
if (_videoPlayer != null)
{
await _videoPlayer.LoadVideoAsync(_currentVideoFile);
}
}
}
private async void PlayButton_Click(object sender, RoutedEventArgs e)
{
if (_videoPlayer == null) return;
var currentState = _videoPlayer.CurrentState;
if (currentState == PlayerState.Stopped)
{
if (string.IsNullOrEmpty(_currentVideoFile))
{
OpenButton_Click(sender, e); // Prompt user to select a file
return;
}
if (_videoPlayer.CurrentFilePath != _currentVideoFile)
{
var loadSuccess = await _videoPlayer.LoadVideoAsync(_currentVideoFile);
if (!loadSuccess)
{
System.Windows.MessageBox.Show("Failed to load video file.", "Video Load Error", MessageBoxButton.OK, MessageBoxImage.Error);
return;
}
}
await _videoPlayer.PlayAsync();
}
else if (currentState == PlayerState.Paused)
{
_videoPlayer.Resume();
}
}
private void PauseButton_Click(object sender, RoutedEventArgs e)
{
if (_videoPlayer?.CurrentState == PlayerState.Playing)
{
_videoPlayer.Pause();
}
}
private void StopButton_Click(object sender, RoutedEventArgs e)
{
if (_videoPlayer?.CurrentState == PlayerState.Playing || _videoPlayer?.CurrentState == PlayerState.Paused)
{
_videoPlayer.Stop();
}
}
private void StatsTimer_Tick(object? sender, EventArgs e)
{
if (_videoPlayer != null)
{
var stats = _videoPlayer.GetStats();
this.Title = $"VAV1 Player - {stats}";
// Update button states based on player state
var currentState = _videoPlayer.CurrentState;
PlayButton.IsEnabled = currentState == PlayerState.Stopped || currentState == PlayerState.Paused;
PauseButton.IsEnabled = currentState == PlayerState.Playing;
StopButton.IsEnabled = currentState == PlayerState.Playing || currentState == PlayerState.Paused;
}
else
{
PlayButton.IsEnabled = false;
PauseButton.IsEnabled = false;
StopButton.IsEnabled = false;
}
}
protected override void OnClosed(EventArgs e)
{
_statsTimer?.Stop();
_videoPlayer?.Dispose();
base.OnClosed(e);
}
}