Skip to content

Commit

Permalink
Implement Equals method for IImageSource and IVideoSource
Browse files Browse the repository at this point in the history
Fixes #1210
  • Loading branch information
yuto-trd committed Dec 25, 2024
1 parent 7a84e54 commit 4b88a53
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 7 deletions.
7 changes: 7 additions & 0 deletions src/Beutl.Engine/Media/Source/BitmapSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
21 changes: 15 additions & 6 deletions src/Beutl.Engine/Media/Source/SoundSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ namespace Beutl.Media.Source;

public class SoundSource(Ref<MediaReader> mediaReader, string fileName) : ISoundSource

Check warning on line 8 in src/Beutl.Engine/Media/Source/SoundSource.cs

View workflow job for this annotation

GitHub Actions / build

'SoundSource' overrides Object.Equals(object o) but does not override Object.GetHashCode()

Check warning on line 8 in src/Beutl.Engine/Media/Source/SoundSource.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

'SoundSource' overrides Object.Equals(object o) but does not override Object.GetHashCode()
{
private readonly Ref<MediaReader> _mediaReader = mediaReader;

~SoundSource()
{
Dispose();
Expand Down Expand Up @@ -46,7 +48,7 @@ public void Dispose()
{
if (!IsDisposed)
{
mediaReader.Dispose();
_mediaReader.Dispose();
GC.SuppressFinalize(this);
IsDisposed = true;
}
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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);
Expand Down
8 changes: 7 additions & 1 deletion src/Beutl.Engine/Media/Source/VideoSource.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Diagnostics.CodeAnalysis;

using Beutl.Media.Decoding;

namespace Beutl.Media.Source;
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 4b88a53

Please sign in to comment.