ResolutionNormalizer.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. using System;
  2. using System.Collections.Generic;
  3. namespace MediaBrowser.Model.Dlna
  4. {
  5. public class ResolutionNormalizer
  6. {
  7. private static readonly List<ResolutionConfiguration> Configurations =
  8. new List<ResolutionConfiguration>
  9. {
  10. new ResolutionConfiguration(426, 320000),
  11. new ResolutionConfiguration(640, 400000),
  12. new ResolutionConfiguration(720, 950000),
  13. new ResolutionConfiguration(1280, 2500000)
  14. };
  15. public static ResolutionOptions Normalize(int maxBitrate,
  16. string codec,
  17. int? maxWidth,
  18. int? maxHeight)
  19. {
  20. foreach (var config in Configurations)
  21. {
  22. if (maxBitrate <= config.MaxBitrate)
  23. {
  24. var originvalValue = maxWidth;
  25. maxWidth = Math.Min(config.MaxWidth, maxWidth ?? config.MaxWidth);
  26. if (!originvalValue.HasValue || originvalValue.Value != maxWidth.Value)
  27. {
  28. maxHeight = null;
  29. }
  30. break;
  31. }
  32. }
  33. return new ResolutionOptions
  34. {
  35. MaxWidth = maxWidth,
  36. MaxHeight = maxHeight
  37. };
  38. }
  39. }
  40. public class ResolutionConfiguration
  41. {
  42. public int MaxWidth { get; set; }
  43. public int MaxBitrate { get; set; }
  44. public ResolutionConfiguration(int maxWidth, int maxBitrate)
  45. {
  46. MaxWidth = maxWidth;
  47. MaxBitrate = maxBitrate;
  48. }
  49. }
  50. public class ResolutionOptions
  51. {
  52. public int? MaxWidth { get; set; }
  53. public int? MaxHeight { get; set; }
  54. }
  55. }