using System.Diagnostics; using System.IO; using System.Reflection; using System.Security.Cryptography; using System.Text.Json; using System.Windows; using System.Windows.Media; using System.Windows.Threading; using XFEToolBox.Tools.VideoStudio; namespace VideoStudio.Validation; internal static partial class Program { private static async Task NativeAudioPlaybackTests(string? userFile) { string mp3 = Path.Combine(Root, "原始 音频 & pause-retry.mp3"); await Ffmpeg(["-f", "lavfi", "-i", "sine=frequency=440:duration=24", "-ar", "44100", "-ac", "2", "-c:a", "libmp3lame", "-b:a", "192k", mp3]); using var library = new MediaLibrary(); var generated = await library.ImportAsync(ffmpeg, ffprobe, mp3, default); await library.WaitForPreviewsAsync(); var results = new List(); foreach (string source in userFile is null ? new[] { mp3 } : new[] { mp3, Path.GetFullPath(userFile) }) { byte[] hash; using (var input = File.OpenRead(source)) hash = SHA256.HashData(input); var asset = source == mp3 ? generated : await library.ImportAsync(ffmpeg, ffprobe, source, default); await library.WaitForPreviewsAsync(); await Ffmpeg(["-xerror", "-i", source, "-map", "0:a:0", "-f", "null", "-"]); Check(asset.Kind == MediaKind.Audio && asset.Probe.AudioCodec == "mp3", "actual MP3 imports as audio and fully decodes: " + asset.Name); var track = new TimelineTrack { Kind = MediaKind.Audio, Index = 2 }; var clip = new TimelineClip(asset.Name, 1, 20) { AssetId = asset.Id, Kind = MediaKind.Audio, TrackId = track.Id }; var sequence = new SequenceSnapshot([asset], [track], [clip], new()); using var playback = new SourcePlaybackController { IsMuted = true }; var surface = new SourcePreviewSurface { Playback = playback }; var window = TestWindow(surface); window.Show(); var messages = new List(); playback.Message += messages.Add; var watch = Stopwatch.StartNew(); try { playback.SetSequence(sequence, 0); await AudioWait(() => playback.ReadySourceCount == 1, "MP3 opens while transport is paused: " + asset.Name); double openMs = watch.Elapsed.TotalMilliseconds; Check(!playback.IsPlaying && Near(playback.Position, 0) && Near(playback.SourcePosition(playback.ActiveLayers[0]) ?? -1, 1, .1), "silent MP3 initialization retains paused source in-point"); await Task.Delay(250); Check(Near(playback.SourcePosition(playback.ActiveLayers[0]) ?? -1, 1, .1), "initialization does not keep audio running while paused"); playback.Play(); await AudioWait(() => playback.Position > .5 && playback.SourcePosition(playback.ActiveLayers[0]) > 1.3, "MP3 native decoder and timeline both advance"); playback.Pause(); double paused = playback.Position; await Task.Delay(200); Check(Near(playback.Position, paused), "MP3 pause freezes sequence clock"); playback.Seek(9); Check(Near(playback.SourcePosition(playback.ActiveLayers[0]) ?? -1, 10, .1), "MP3 paused seek uses exact original-file offset"); int opened = playback.OpenCount; playback.Play(); await AudioWait(() => playback.Position > 9.4 && playback.SourcePosition(playback.ActiveLayers[0]) > 10.2, "MP3 playback resumes after seek"); Check(playback.OpenCount == opened && playback.OpenSources.Single().LocalPath == source, "MP3 pause/seek/resume reuses original decoder without proxy or transcoding"); playback.Pause(); playback.SetSequence(sequence, 2); playback.Pause(); await Task.Delay(150); Check(!playback.IsPlaying && Near(playback.Position, 2), "pausing an MP3 immediately never resumes transport implicitly"); Check(!messages.Any(m => m.Contains("无法直接播放")), "MP3 play, pause and seek have no native-open errors"); results.Add(new { source, openMilliseconds = openMs, playback.OpenCount, sourceUnchanged = true }); } finally { surface.Playback = null; window.Close(); } using var after = File.OpenRead(source); Check(hash.SequenceEqual(SHA256.HashData(after)), "MP3 source remains byte-identical after playback tests"); } await AudioInitializationLifecycle(generated); await RetryAudioAfterFailure(generated); await AudioEditorIntegration(userFile ?? mp3); File.WriteAllText(Path.Combine(Root, "audio-playback-results.json"), JsonSerializer.Serialize(results, Json)); } private static async Task AudioWait(Func condition, string name) { var watch = Stopwatch.StartNew(); while (!condition() && watch.Elapsed < TimeSpan.FromSeconds(6)) await Task.Delay(40); Check(condition(), name); } private static object AudioSession(SourcePlaybackController playback) { var dictionary = (System.Collections.IDictionary)typeof(SourcePlaybackController).GetField("sessions", BindingFlags.Instance | BindingFlags.NonPublic)!.GetValue(playback)!; return dictionary.Values.Cast().Single(); } private static async Task AudioInitializationLifecycle(MediaAsset asset) { var track = new TimelineTrack { Kind = MediaKind.Audio, Index = 1 }; var secondTrack = new TimelineTrack { Kind = MediaKind.Audio, Index = 2 }; var first = new TimelineClip("first", 0, 20) { AssetId = asset.Id, Kind = MediaKind.Audio, TrackId = track.Id }; var second = new TimelineClip("independent offset", 3, 23) { AssetId = asset.Id, Kind = MediaKind.Audio, TrackId = secondTrack.Id }; var sequence = new SequenceSnapshot([asset], [track, secondTrack], [first], new()); using var playback = new SourcePlaybackController { IsMuted = true }; var surface = new SourcePreviewSurface { Playback = playback }; var window = TestWindow(surface); window.Show(); try { playback.SetSequence(sequence, 2); var pending = AudioSession(playback); var player = (MediaPlayer)pending.GetType().GetField("Player")!.GetValue(pending)!; Check(player.IsMuted && player.Volume == 0 && playback.ReadySourceCount == 0, "native initialization is explicitly muted before MediaOpened"); playback.Play(); playback.Pause(); await AudioWait(() => playback.ReadySourceCount == 1, "opening finishes after immediate Play/Pause"); Check(!playback.IsPlaying && Near(playback.Position, 2), "late open completion never restarts a paused transport"); playback.SetSequence(sequence with { Clips = [first, second] }, 2); playback.Play(); await AudioWait(() => playback.ReadySourceCount == 2 && playback.Position > 2.5, "two audio lanes open and play concurrently with different source offsets"); Check(playback.ActiveLayers.All(l => Near(playback.SourcePosition(l) ?? -1, l.SourceTime(playback.Position), .3)), "concurrent MP3 source positions follow independent track offsets"); playback.Pause(); using var timeout = new SourcePlaybackController(TimeSpan.FromTicks(1)) { IsMuted = true }; string error = ""; timeout.Message += text => error = text; timeout.SetSequence(sequence, 1); Check(error.Contains("打开超时") && timeout.ReadySourceCount == 0 && !timeout.IsPlaying, "opening timeout is enforced while paused and releases the decoder"); object expired = AudioSession(timeout); var staleOpened = (EventHandler)expired.GetType().GetField("Opened")!.GetValue(expired)!; staleOpened(null, EventArgs.Empty); Check(timeout.ReadySourceCount == 0 && timeout.PlayerFor(timeout.ActiveLayers[0]) is null, "late MediaOpened after timeout is ignored"); timeout.Play(); await Task.Delay(100); Check(timeout.OpenCount == 2 && !timeout.IsPlaying && Near(timeout.Position, 1), "synchronous retry failure does not restart clock or advance past the failed audio"); var timeoutTimer = (DispatcherTimer)typeof(SourcePlaybackController).GetField("timer", BindingFlags.Instance | BindingFlags.NonPublic)!.GetValue(timeout)!; Check(!timeoutTimer.IsEnabled, "failed paused sources leave no polling timer running"); using var disposed = new SourcePlaybackController { IsMuted = true }; disposed.SetSequence(sequence, 1); object closing = AudioSession(disposed); var afterClose = (EventHandler)closing.GetType().GetField("Opened")!.GetValue(closing)!; disposed.Dispose(); afterClose(null, EventArgs.Empty); await Task.Delay(100); Check(disposed.DecoderCount == 0 && !disposed.IsPlaying, "closing during native initialization releases sources and ignores queued callbacks"); } finally { surface.Playback = null; window.Close(); } } private static async Task RetryAudioAfterFailure(MediaAsset generated) { string restored = Path.Combine(Root, "offline-then-restored.mp3"); var asset = new MediaAsset("restored", restored, MediaKind.Audio, generated.Probe); var track = new TimelineTrack { Kind = MediaKind.Audio, Index = 1 }; var clip = new TimelineClip(asset.Name, 0, 20) { AssetId = asset.Id, Kind = MediaKind.Audio, TrackId = track.Id }; var sequence = new SequenceSnapshot([asset], [track], [clip], new()); using var playback = new SourcePlaybackController { IsMuted = true }; var surface = new SourcePreviewSurface { Playback = playback }; var window = TestWindow(surface); window.Show(); string message = ""; playback.Message += m => message = m; try { playback.SetSequence(sequence, 2); playback.Play(); object old = AudioSession(playback); var lateOpened = (EventHandler)old.GetType().GetField("Opened")!.GetValue(old)!; await AudioWait(() => message.Contains("无法直接播放"), "missing MP3 reports native open failure"); Check(!playback.IsPlaying && playback.ReadySourceCount == 0 && Near(playback.Position, 2), "failed audio stops timeline instead of silently advancing"); Check(message.Contains("重试") && !message.Contains("请检查系统解码器"), "failure offers retry without falsely diagnosing missing codecs"); lateOpened.Invoke(null, EventArgs.Empty); Check(playback.ReadySourceCount == 0 && playback.PlayerFor(playback.ActiveLayers[0]) is null, "late MediaOpened cannot resurrect a failed decoder"); int attempts = playback.OpenCount; await Task.Delay(200); Check(playback.OpenCount == attempts && !playback.IsPlaying, "failure does not create an unbounded automatic retry loop"); File.Copy(generated.FilePath, restored); playback.Play(); await AudioWait(() => playback.ReadySourceCount == 1 && playback.Position > 2.4, "Play retries a restored MP3 without reimport or application restart"); Check(playback.OpenCount == attempts + 1 && playback.DecoderCount == 1, "retry replaces only the failed source session"); lateOpened.Invoke(null, EventArgs.Empty); Check(playback.ReadySourceCount == 1 && playback.OpenCount == attempts + 1, "obsolete callbacks cannot change the replacement decoder"); } finally { surface.Playback = null; window.Close(); } } private static async Task AudioEditorIntegration(string source) { var page = new MainPage(); var vm = (MainPageViewModel)page.DataContext; vm.FfmpegPath = ffmpeg; vm.FfprobePath = ffprobe; page.Playback.IsMuted = true; var window = TestWindow(page); window.Show(); await Dispatcher.Yield(DispatcherPriority.ApplicationIdle); try { await vm.ImportAssetsAsync([source]); await vm.WaitForAssetPreviewsAsync(); Check(vm.Assets.Count == 1 && vm.Clips.Count == 0, "real audio stays in Project Media until inserted"); vm.SelectedTrack = vm.Tracks.Single(t => t.Kind == MediaKind.Audio && t.Index == 2); vm.InsertAsset(vm.Assets[0], 0); vm.SeekSequence(0); await AudioWait(() => page.Playback.ReadySourceCount == 1, "actual editor prepares the MP3 on A2"); vm.FfmpegPath = Path.Combine(Root, "ffmpeg-deliberately-unavailable.exe"); vm.FfprobePath = Path.Combine(Root, "ffprobe-deliberately-unavailable.exe"); vm.TogglePlaybackCommand.Execute(null); await AudioWait(() => vm.IsPlaying && vm.SequencePosition > .4 && page.Playback.SourcePosition(page.Playback.ActiveLayers[0]) > .2, "actual editor transport plays MP3 with FFmpeg and FFprobe unavailable"); vm.TogglePlaybackCommand.Execute(null); vm.SeekSequence(12); vm.TogglePlaybackCommand.Execute(null); await AudioWait(() => vm.SequencePosition > 12.3, "actual editor resumes the MP3 after scrubbing"); vm.TogglePlaybackCommand.Execute(null); Check(!vm.IsPlaying && !vm.Status.Contains("无法直接播放"), "actual editor pauses and keeps a successful playback status"); Save(page, "mp3-original-playback.png"); } finally { vm.HasUnsavedChanges = false; window.Close(); } } }