StreamInfoSorter.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. using System;
  2. using MediaBrowser.Model.MediaInfo;
  3. using MediaBrowser.Model.Session;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. namespace MediaBrowser.Model.Dlna
  7. {
  8. public class StreamInfoSorter
  9. {
  10. public static List<StreamInfo> SortMediaSources(List<StreamInfo> streams, long? maxBitrate)
  11. {
  12. return streams.OrderBy(i =>
  13. {
  14. // Nothing beats direct playing a file
  15. if (i.PlayMethod == PlayMethod.DirectPlay && i.MediaSource.Protocol == MediaProtocol.File)
  16. {
  17. return 0;
  18. }
  19. return 1;
  20. }).ThenBy(i =>
  21. {
  22. switch (i.PlayMethod)
  23. {
  24. // Let's assume direct streaming a file is just as desirable as direct playing a remote url
  25. case PlayMethod.DirectStream:
  26. case PlayMethod.DirectPlay:
  27. return 0;
  28. default:
  29. return 1;
  30. }
  31. }).ThenBy(i =>
  32. {
  33. switch (i.MediaSource.Protocol)
  34. {
  35. case MediaProtocol.File:
  36. return 0;
  37. default:
  38. return 1;
  39. }
  40. }).ThenBy(i =>
  41. {
  42. if (maxBitrate.HasValue)
  43. {
  44. if (i.MediaSource.Bitrate.HasValue)
  45. {
  46. return Math.Abs(i.MediaSource.Bitrate.Value - maxBitrate.Value);
  47. }
  48. }
  49. return 0;
  50. }).ToList();
  51. }
  52. }
  53. }