ConditionProcessor.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. using System;
  2. using System.Globalization;
  3. using MediaBrowser.Model.Entities;
  4. using MediaBrowser.Model.MediaInfo;
  5. namespace MediaBrowser.Model.Dlna
  6. {
  7. public class ConditionProcessor
  8. {
  9. public bool IsVideoConditionSatisfied(ProfileCondition condition,
  10. int? audioBitrate,
  11. int? audioChannels,
  12. int? width,
  13. int? height,
  14. int? bitDepth,
  15. int? videoBitrate,
  16. string videoProfile,
  17. double? videoLevel,
  18. double? videoFramerate,
  19. int? packetLength,
  20. TransportStreamTimestamp? timestamp)
  21. {
  22. switch (condition.Property)
  23. {
  24. case ProfileConditionValue.AudioProfile:
  25. // TODO: Implement
  26. return true;
  27. case ProfileConditionValue.Has64BitOffsets:
  28. // TODO: Implement
  29. return true;
  30. case ProfileConditionValue.VideoFramerate:
  31. return IsConditionSatisfied(condition, videoFramerate);
  32. case ProfileConditionValue.VideoLevel:
  33. return IsConditionSatisfied(condition, videoLevel);
  34. case ProfileConditionValue.VideoProfile:
  35. return IsConditionSatisfied(condition, videoProfile);
  36. case ProfileConditionValue.PacketLength:
  37. return IsConditionSatisfied(condition, packetLength);
  38. case ProfileConditionValue.AudioBitrate:
  39. return IsConditionSatisfied(condition, audioBitrate);
  40. case ProfileConditionValue.AudioChannels:
  41. return IsConditionSatisfied(condition, audioChannels);
  42. case ProfileConditionValue.VideoBitDepth:
  43. return IsConditionSatisfied(condition, bitDepth);
  44. case ProfileConditionValue.VideoBitrate:
  45. return IsConditionSatisfied(condition, videoBitrate);
  46. case ProfileConditionValue.Height:
  47. return IsConditionSatisfied(condition, height);
  48. case ProfileConditionValue.Width:
  49. return IsConditionSatisfied(condition, width);
  50. case ProfileConditionValue.VideoTimestamp:
  51. return IsConditionSatisfied(condition, timestamp);
  52. default:
  53. throw new ArgumentException("Unexpected condition on video file: " + condition.Property);
  54. }
  55. }
  56. public bool IsImageConditionSatisfied(ProfileCondition condition, int? width, int? height)
  57. {
  58. switch (condition.Property)
  59. {
  60. case ProfileConditionValue.Height:
  61. return IsConditionSatisfied(condition, height);
  62. case ProfileConditionValue.Width:
  63. return IsConditionSatisfied(condition, width);
  64. default:
  65. throw new ArgumentException("Unexpected condition on image file: " + condition.Property);
  66. }
  67. }
  68. public bool IsAudioConditionSatisfied(ProfileCondition condition, int? audioChannels, int? audioBitrate)
  69. {
  70. switch (condition.Property)
  71. {
  72. case ProfileConditionValue.AudioBitrate:
  73. return IsConditionSatisfied(condition, audioBitrate);
  74. case ProfileConditionValue.AudioChannels:
  75. return IsConditionSatisfied(condition, audioChannels);
  76. default:
  77. throw new ArgumentException("Unexpected condition on audio file: " + condition.Property);
  78. }
  79. }
  80. public bool IsVideoAudioConditionSatisfied(ProfileCondition condition,
  81. int? audioChannels,
  82. int? audioBitrate)
  83. {
  84. switch (condition.Property)
  85. {
  86. case ProfileConditionValue.AudioBitrate:
  87. return IsConditionSatisfied(condition, audioBitrate);
  88. case ProfileConditionValue.AudioChannels:
  89. return IsConditionSatisfied(condition, audioChannels);
  90. default:
  91. throw new ArgumentException("Unexpected condition on audio file: " + condition.Property);
  92. }
  93. }
  94. private static readonly CultureInfo UsCulture = new CultureInfo("en-US");
  95. private bool IsConditionSatisfied(ProfileCondition condition, int? currentValue)
  96. {
  97. if (!currentValue.HasValue)
  98. {
  99. // If the value is unknown, it satisfies if not marked as required
  100. return !condition.IsRequired;
  101. }
  102. int expected;
  103. if (int.TryParse(condition.Value, NumberStyles.Any, UsCulture, out expected))
  104. {
  105. switch (condition.Condition)
  106. {
  107. case ProfileConditionType.Equals:
  108. return currentValue.Value.Equals(expected);
  109. case ProfileConditionType.GreaterThanEqual:
  110. return currentValue.Value >= expected;
  111. case ProfileConditionType.LessThanEqual:
  112. return currentValue.Value <= expected;
  113. case ProfileConditionType.NotEquals:
  114. return !currentValue.Value.Equals(expected);
  115. default:
  116. throw new InvalidOperationException("Unexpected ProfileConditionType");
  117. }
  118. }
  119. return false;
  120. }
  121. private bool IsConditionSatisfied(ProfileCondition condition, string currentValue)
  122. {
  123. if (string.IsNullOrEmpty(currentValue))
  124. {
  125. // If the value is unknown, it satisfies if not marked as required
  126. return !condition.IsRequired;
  127. }
  128. var expected = condition.Value;
  129. switch (condition.Condition)
  130. {
  131. case ProfileConditionType.Equals:
  132. return string.Equals(currentValue, expected, StringComparison.OrdinalIgnoreCase);
  133. case ProfileConditionType.NotEquals:
  134. return !string.Equals(currentValue, expected, StringComparison.OrdinalIgnoreCase);
  135. default:
  136. throw new InvalidOperationException("Unexpected ProfileConditionType");
  137. }
  138. }
  139. private bool IsConditionSatisfied(ProfileCondition condition, double? currentValue)
  140. {
  141. if (!currentValue.HasValue)
  142. {
  143. // If the value is unknown, it satisfies if not marked as required
  144. return !condition.IsRequired;
  145. }
  146. double expected;
  147. if (double.TryParse(condition.Value, NumberStyles.Any, UsCulture, out expected))
  148. {
  149. switch (condition.Condition)
  150. {
  151. case ProfileConditionType.Equals:
  152. return currentValue.Value.Equals(expected);
  153. case ProfileConditionType.GreaterThanEqual:
  154. return currentValue.Value >= expected;
  155. case ProfileConditionType.LessThanEqual:
  156. return currentValue.Value <= expected;
  157. case ProfileConditionType.NotEquals:
  158. return !currentValue.Value.Equals(expected);
  159. default:
  160. throw new InvalidOperationException("Unexpected ProfileConditionType");
  161. }
  162. }
  163. return false;
  164. }
  165. private bool IsConditionSatisfied(ProfileCondition condition, TransportStreamTimestamp? timestamp)
  166. {
  167. if (!timestamp.HasValue)
  168. {
  169. // If the value is unknown, it satisfies if not marked as required
  170. return !condition.IsRequired;
  171. }
  172. var expected = (TransportStreamTimestamp)Enum.Parse(typeof(TransportStreamTimestamp), condition.Value, true);
  173. switch (condition.Condition)
  174. {
  175. case ProfileConditionType.Equals:
  176. return timestamp == expected;
  177. case ProfileConditionType.NotEquals:
  178. return timestamp != expected;
  179. default:
  180. throw new InvalidOperationException("Unexpected ProfileConditionType");
  181. }
  182. }
  183. }
  184. }