IHasMediaSources.cs 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 identifier.
  13. /// </summary>
  14. /// <value>The identifier.</value>
  15. Guid Id { get; }
  16. /// <summary>
  17. /// Gets the media sources.
  18. /// </summary>
  19. /// <param name="enablePathSubstitution">if set to <c>true</c> [enable path substitution].</param>
  20. /// <returns>Task{IEnumerable{MediaSourceInfo}}.</returns>
  21. IEnumerable<MediaSourceInfo> GetMediaSources(bool enablePathSubstitution);
  22. }
  23. public static class HasMediaSourceExtensions
  24. {
  25. public static IEnumerable<MediaSourceInfo> GetMediaSources(this IHasMediaSources item, bool enablePathSubstitution, User user)
  26. {
  27. if (item == null)
  28. {
  29. throw new ArgumentNullException("item");
  30. }
  31. if (!(item is Video))
  32. {
  33. return item.GetMediaSources(enablePathSubstitution);
  34. }
  35. if (user == null)
  36. {
  37. throw new ArgumentNullException("user");
  38. }
  39. var sources = item.GetMediaSources(enablePathSubstitution).ToList();
  40. var preferredAudio = string.IsNullOrEmpty(user.Configuration.AudioLanguagePreference)
  41. ? new string[] { }
  42. : new[] { user.Configuration.AudioLanguagePreference };
  43. var preferredSubs = string.IsNullOrEmpty(user.Configuration.SubtitleLanguagePreference)
  44. ? new List<string> { }
  45. : new List<string> { user.Configuration.SubtitleLanguagePreference };
  46. foreach (var source in sources)
  47. {
  48. source.DefaultAudioStreamIndex = MediaStreamSelector.GetDefaultAudioStreamIndex(
  49. source.MediaStreams, preferredAudio, user.Configuration.PlayDefaultAudioTrack);
  50. var defaultAudioIndex = source.DefaultAudioStreamIndex;
  51. var audioLangage = defaultAudioIndex == null
  52. ? null
  53. : source.MediaStreams.Where(i => i.Type == MediaStreamType.Audio && i.Index == defaultAudioIndex).Select(i => i.Language).FirstOrDefault();
  54. source.DefaultSubtitleStreamIndex = MediaStreamSelector.GetDefaultSubtitleStreamIndex(source.MediaStreams,
  55. preferredSubs,
  56. user.Configuration.SubtitleMode,
  57. audioLangage);
  58. }
  59. return sources;
  60. }
  61. }
  62. }