aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Controller/Entities/IHasMediaSources.cs
blob: 98d268298945247bf4be02d45444db7826c0fdf8 (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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
using MediaBrowser.Controller.MediaEncoding;
using MediaBrowser.Model.Dto;
using MediaBrowser.Model.Entities;
using System;
using System.Collections.Generic;
using System.Linq;

namespace MediaBrowser.Controller.Entities
{
    public interface IHasMediaSources
    {
        /// <summary>
        /// Gets the identifier.
        /// </summary>
        /// <value>The identifier.</value>
        Guid Id { get; }

        /// <summary>
        /// Gets the media sources.
        /// </summary>
        /// <param name="enablePathSubstitution">if set to <c>true</c> [enable path substitution].</param>
        /// <returns>Task{IEnumerable{MediaSourceInfo}}.</returns>
        IEnumerable<MediaSourceInfo> GetMediaSources(bool enablePathSubstitution);
    }

    public static class HasMediaSourceExtensions
    {
        public static IEnumerable<MediaSourceInfo> GetMediaSources(this IHasMediaSources item, bool enablePathSubstitution, User user)
        {
            if (item == null)
            {
                throw new ArgumentNullException("item");
            }

            if (!(item is Video))
            {
                return item.GetMediaSources(enablePathSubstitution);
            }

            if (user == null)
            {
                throw new ArgumentNullException("user");
            }

            var sources = item.GetMediaSources(enablePathSubstitution).ToList();

            var preferredAudio = string.IsNullOrEmpty(user.Configuration.AudioLanguagePreference)
            ? new string[] { }
            : new[] { user.Configuration.AudioLanguagePreference };

            var preferredSubs = string.IsNullOrEmpty(user.Configuration.SubtitleLanguagePreference)
                ? new List<string> { }
                : new List<string> { user.Configuration.SubtitleLanguagePreference };

            foreach (var source in sources)
            {
                source.DefaultAudioStreamIndex = MediaStreamSelector.GetDefaultAudioStreamIndex(
                    source.MediaStreams, preferredAudio, user.Configuration.PlayDefaultAudioTrack);

                var defaultAudioIndex = source.DefaultAudioStreamIndex;
                var audioLangage = defaultAudioIndex == null
                    ? null
                    : source.MediaStreams.Where(i => i.Type == MediaStreamType.Audio && i.Index == defaultAudioIndex).Select(i => i.Language).FirstOrDefault();

                source.DefaultSubtitleStreamIndex = MediaStreamSelector.GetDefaultSubtitleStreamIndex(source.MediaStreams,
                    preferredSubs,
                    user.Configuration.SubtitleMode,
                    audioLangage);
            }

            return sources;
        }
    }
}