MediaSourceWidthComparator.cs 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Runtime.Intrinsics.X86;
  4. using MediaBrowser.Model.Dto;
  5. namespace MediaBrowser.Controller.Entities;
  6. /// <summary>
  7. /// Compare MediaSource of the same file by Video width <see cref="IComparer{T}" />.
  8. /// </summary>
  9. public class MediaSourceWidthComparator : IComparer<MediaSourceInfo>
  10. {
  11. /// <inheritdoc />
  12. public int Compare(MediaSourceInfo? x, MediaSourceInfo? y)
  13. {
  14. if (x is null && y is null)
  15. {
  16. return 0;
  17. }
  18. if (x is null)
  19. {
  20. return -1;
  21. }
  22. if (y is null)
  23. {
  24. return 1;
  25. }
  26. if (string.Equals(x.Path, y.Path, StringComparison.OrdinalIgnoreCase))
  27. {
  28. if (x.VideoStream is null && y.VideoStream is null)
  29. {
  30. return 0;
  31. }
  32. if (x.VideoStream is null)
  33. {
  34. return -1;
  35. }
  36. if (y.VideoStream is null)
  37. {
  38. return 1;
  39. }
  40. var xWidth = x.VideoStream.Width ?? 0;
  41. var yWidth = y.VideoStream.Width ?? 0;
  42. return xWidth - yWidth;
  43. }
  44. return 0;
  45. }
  46. }