IHasMediaSources.cs 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. using MediaBrowser.Controller.MediaEncoding;
  2. using MediaBrowser.Model.Dto;
  3. using MediaBrowser.Model.Entities;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. namespace MediaBrowser.Controller.Entities
  8. {
  9. public interface IHasMediaSources
  10. {
  11. /// <summary>
  12. /// Gets the media sources.
  13. /// </summary>
  14. /// <param name="enablePathSubstitution">if set to <c>true</c> [enable path substitution].</param>
  15. /// <returns>Task{IEnumerable{MediaSourceInfo}}.</returns>
  16. IEnumerable<MediaSourceInfo> GetMediaSources(bool enablePathSubstitution);
  17. }
  18. public static class HasMediaSourceExtensions
  19. {
  20. public static IEnumerable<MediaSourceInfo> GetMediaSources(this IHasMediaSources item, bool enablePathSubstitution, User user)
  21. {
  22. if (item == null)
  23. {
  24. throw new ArgumentNullException("item");
  25. }
  26. if (!(item is Video))
  27. {
  28. return item.GetMediaSources(enablePathSubstitution);
  29. }
  30. if (user == null)
  31. {
  32. throw new ArgumentNullException("user");
  33. }
  34. var sources = item.GetMediaSources(enablePathSubstitution).ToList();
  35. var preferredAudio = string.IsNullOrEmpty(user.Configuration.AudioLanguagePreference)
  36. ? new string[] { }
  37. : new[] { user.Configuration.AudioLanguagePreference };
  38. var preferredSubs = string.IsNullOrEmpty(user.Configuration.SubtitleLanguagePreference)
  39. ? new string[] { }
  40. : new[] { user.Configuration.SubtitleLanguagePreference };
  41. foreach (var source in sources)
  42. {
  43. source.DefaultAudioStreamIndex = MediaStreamSelector.GetDefaultAudioStreamIndex(
  44. source.MediaStreams, preferredAudio, user.Configuration.PlayDefaultAudioTrack);
  45. var defaultAudioIndex = source.DefaultAudioStreamIndex;
  46. var audioLangage = defaultAudioIndex == null
  47. ? null
  48. : source.MediaStreams.Where(i => i.Type == MediaStreamType.Audio && i.Index == defaultAudioIndex).Select(i => i.Language).FirstOrDefault();
  49. source.DefaultSubtitleStreamIndex = MediaStreamSelector.GetDefaultSubtitleStreamIndex(source.MediaStreams,
  50. preferredSubs,
  51. user.Configuration.SubtitleMode,
  52. audioLangage);
  53. }
  54. return sources;
  55. }
  56. }
  57. }