From 4b88a53d0810c6dc34654b94b4d1b18f26fa66fc Mon Sep 17 00:00:00 2001 From: Yuto Terada Date: Wed, 25 Dec 2024 17:23:57 +0900 Subject: [PATCH] Implement Equals method for IImageSource and IVideoSource Fixes #1210 --- src/Beutl.Engine/Media/Source/BitmapSource.cs | 7 +++++++ src/Beutl.Engine/Media/Source/SoundSource.cs | 21 +++++++++++++------ src/Beutl.Engine/Media/Source/VideoSource.cs | 8 ++++++- 3 files changed, 29 insertions(+), 7 deletions(-) diff --git a/src/Beutl.Engine/Media/Source/BitmapSource.cs b/src/Beutl.Engine/Media/Source/BitmapSource.cs index 0354bc484..c5b456984 100644 --- a/src/Beutl.Engine/Media/Source/BitmapSource.cs +++ b/src/Beutl.Engine/Media/Source/BitmapSource.cs @@ -76,4 +76,11 @@ protected override void OnDispose(bool disposing) { _bitmap.Dispose(); } + + public override bool Equals(object? obj) + { + return obj is BitmapSource source + && !IsDisposed && !source.IsDisposed + && ReferenceEquals(_bitmap.Value, source._bitmap.Value); + } } diff --git a/src/Beutl.Engine/Media/Source/SoundSource.cs b/src/Beutl.Engine/Media/Source/SoundSource.cs index be3961179..234ef41aa 100644 --- a/src/Beutl.Engine/Media/Source/SoundSource.cs +++ b/src/Beutl.Engine/Media/Source/SoundSource.cs @@ -7,6 +7,8 @@ namespace Beutl.Media.Source; public class SoundSource(Ref mediaReader, string fileName) : ISoundSource { + private readonly Ref _mediaReader = mediaReader; + ~SoundSource() { Dispose(); @@ -46,7 +48,7 @@ public void Dispose() { if (!IsDisposed) { - mediaReader.Dispose(); + _mediaReader.Dispose(); GC.SuppressFinalize(this); IsDisposed = true; } @@ -56,7 +58,7 @@ public SoundSource Clone() { ObjectDisposedException.ThrowIf(IsDisposed, this); - return new SoundSource(mediaReader.Clone(), Name); + return new SoundSource(_mediaReader.Clone(), Name); } public bool Read(int start, int length, [NotNullWhen(true)] out IPcm? sound) @@ -67,7 +69,7 @@ public bool Read(int start, int length, [NotNullWhen(true)] out IPcm? sound) return false; } - return mediaReader.Value.ReadAudio(start, length, out sound); + return _mediaReader.Value.ReadAudio(start, length, out sound); } public bool Read(TimeSpan start, TimeSpan length, [NotNullWhen(true)] out IPcm? sound) @@ -78,7 +80,7 @@ public bool Read(TimeSpan start, TimeSpan length, [NotNullWhen(true)] out IPcm? return false; } - return mediaReader.Value.ReadAudio(ToSamples(start), ToSamples(length), out sound); + return _mediaReader.Value.ReadAudio(ToSamples(start), ToSamples(length), out sound); } public bool Read(TimeSpan start, int length, [NotNullWhen(true)] out IPcm? sound) @@ -89,7 +91,7 @@ public bool Read(TimeSpan start, int length, [NotNullWhen(true)] out IPcm? sound return false; } - return mediaReader.Value.ReadAudio(ToSamples(start), length, out sound); + return _mediaReader.Value.ReadAudio(ToSamples(start), length, out sound); } public bool Read(int start, TimeSpan length, [NotNullWhen(true)] out IPcm? sound) @@ -100,11 +102,18 @@ public bool Read(int start, TimeSpan length, [NotNullWhen(true)] out IPcm? sound return false; } - return mediaReader.Value.ReadAudio(start, ToSamples(length), out sound); + return _mediaReader.Value.ReadAudio(start, ToSamples(length), out sound); } ISoundSource ISoundSource.Clone() => Clone(); + public override bool Equals(object? obj) + { + return obj is SoundSource source + && !IsDisposed && !source.IsDisposed + && ReferenceEquals(_mediaReader.Value, source._mediaReader.Value); + } + private int ToSamples(TimeSpan timeSpan) { return (int)(timeSpan.TotalSeconds * SampleRate); diff --git a/src/Beutl.Engine/Media/Source/VideoSource.cs b/src/Beutl.Engine/Media/Source/VideoSource.cs index 135dd3246..ac43f7b4e 100644 --- a/src/Beutl.Engine/Media/Source/VideoSource.cs +++ b/src/Beutl.Engine/Media/Source/VideoSource.cs @@ -1,5 +1,4 @@ using System.Diagnostics.CodeAnalysis; - using Beutl.Media.Decoding; namespace Beutl.Media.Source; @@ -94,6 +93,13 @@ public bool Read(int frame, [NotNullWhen(true)] out IBitmap? bitmap) return _mediaReader.Value.ReadVideo(frame, out bitmap); } + public override bool Equals(object? obj) + { + return obj is VideoSource source + && !IsDisposed && !source.IsDisposed + && ReferenceEquals(_mediaReader.Value, source._mediaReader.Value); + } + IVideoSource IVideoSource.Clone() => Clone(); IMediaSource IMediaSource.Clone() => Clone();