aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Controller/Entities/MediaSourceWidthComparator.cs
blob: 0224577a4c1f2803208fb703b38bc0ce59fa20c2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
using System;
using System.Collections.Generic;
using System.Runtime.Intrinsics.X86;
using MediaBrowser.Model.Dto;

namespace MediaBrowser.Controller.Entities;

/// <summary>
/// Compare MediaSource of the same file by Video width <see cref="IComparer{T}" />.
/// </summary>
public class MediaSourceWidthComparator : IComparer<MediaSourceInfo>
{
    /// <inheritdoc />
    public int Compare(MediaSourceInfo? x, MediaSourceInfo? y)
    {
        if (x is null && y is null)
        {
            return 0;
        }

        if (x is null)
        {
            return -1;
        }

        if (y is null)
        {
            return 1;
        }

        if (string.Equals(x.Path, y.Path, StringComparison.OrdinalIgnoreCase))
        {
            if (x.VideoStream is null && y.VideoStream is null)
            {
                return 0;
            }

            if (x.VideoStream is null)
            {
                return -1;
            }

            if (y.VideoStream is null)
            {
                return 1;
            }

            var xWidth = x.VideoStream.Width ?? 0;
            var yWidth = y.VideoStream.Width ?? 0;

            return xWidth - yWidth;
        }

        return 0;
    }
}