ConditionProcessor.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. using MediaBrowser.Model.Extensions;
  2. using MediaBrowser.Model.MediaInfo;
  3. using System;
  4. namespace MediaBrowser.Model.Dlna
  5. {
  6. public class ConditionProcessor
  7. {
  8. public bool IsVideoConditionSatisfied(ProfileCondition condition,
  9. int? audioBitrate,
  10. int? audioChannels,
  11. int? width,
  12. int? height,
  13. int? bitDepth,
  14. int? videoBitrate,
  15. string videoProfile,
  16. double? videoLevel,
  17. float? videoFramerate,
  18. int? packetLength,
  19. TransportStreamTimestamp? timestamp,
  20. bool? isAnamorphic)
  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.IsAnamorphic:
  31. return IsConditionSatisfied(condition, isAnamorphic);
  32. case ProfileConditionValue.VideoFramerate:
  33. return IsConditionSatisfied(condition, videoFramerate);
  34. case ProfileConditionValue.VideoLevel:
  35. return IsConditionSatisfied(condition, videoLevel);
  36. case ProfileConditionValue.VideoProfile:
  37. return IsConditionSatisfied(condition, videoProfile);
  38. case ProfileConditionValue.PacketLength:
  39. return IsConditionSatisfied(condition, packetLength);
  40. case ProfileConditionValue.AudioBitrate:
  41. return IsConditionSatisfied(condition, audioBitrate);
  42. case ProfileConditionValue.AudioChannels:
  43. return IsConditionSatisfied(condition, audioChannels);
  44. case ProfileConditionValue.VideoBitDepth:
  45. return IsConditionSatisfied(condition, bitDepth);
  46. case ProfileConditionValue.VideoBitrate:
  47. return IsConditionSatisfied(condition, videoBitrate);
  48. case ProfileConditionValue.Height:
  49. return IsConditionSatisfied(condition, height);
  50. case ProfileConditionValue.Width:
  51. return IsConditionSatisfied(condition, width);
  52. case ProfileConditionValue.VideoTimestamp:
  53. return IsConditionSatisfied(condition, timestamp);
  54. default:
  55. throw new ArgumentException("Unexpected condition on video file: " + condition.Property);
  56. }
  57. }
  58. public bool IsImageConditionSatisfied(ProfileCondition condition, int? width, int? height)
  59. {
  60. switch (condition.Property)
  61. {
  62. case ProfileConditionValue.Height:
  63. return IsConditionSatisfied(condition, height);
  64. case ProfileConditionValue.Width:
  65. return IsConditionSatisfied(condition, width);
  66. default:
  67. throw new ArgumentException("Unexpected condition on image file: " + condition.Property);
  68. }
  69. }
  70. public bool IsAudioConditionSatisfied(ProfileCondition condition, int? audioChannels, int? audioBitrate)
  71. {
  72. switch (condition.Property)
  73. {
  74. case ProfileConditionValue.AudioBitrate:
  75. return IsConditionSatisfied(condition, audioBitrate);
  76. case ProfileConditionValue.AudioChannels:
  77. return IsConditionSatisfied(condition, audioChannels);
  78. default:
  79. throw new ArgumentException("Unexpected condition on audio file: " + condition.Property);
  80. }
  81. }
  82. public bool IsVideoAudioConditionSatisfied(ProfileCondition condition,
  83. int? audioChannels,
  84. int? audioBitrate,
  85. string audioProfile)
  86. {
  87. switch (condition.Property)
  88. {
  89. case ProfileConditionValue.AudioProfile:
  90. return IsConditionSatisfied(condition, audioProfile);
  91. case ProfileConditionValue.AudioBitrate:
  92. return IsConditionSatisfied(condition, audioBitrate);
  93. case ProfileConditionValue.AudioChannels:
  94. return IsConditionSatisfied(condition, audioChannels);
  95. default:
  96. throw new ArgumentException("Unexpected condition on audio file: " + condition.Property);
  97. }
  98. }
  99. private bool IsConditionSatisfied(ProfileCondition condition, int? currentValue)
  100. {
  101. if (!currentValue.HasValue)
  102. {
  103. // If the value is unknown, it satisfies if not marked as required
  104. return !condition.IsRequired;
  105. }
  106. int expected;
  107. if (IntHelper.TryParseCultureInvariant(condition.Value, out expected))
  108. {
  109. switch (condition.Condition)
  110. {
  111. case ProfileConditionType.Equals:
  112. return currentValue.Value.Equals(expected);
  113. case ProfileConditionType.GreaterThanEqual:
  114. return currentValue.Value >= expected;
  115. case ProfileConditionType.LessThanEqual:
  116. return currentValue.Value <= expected;
  117. case ProfileConditionType.NotEquals:
  118. return !currentValue.Value.Equals(expected);
  119. default:
  120. throw new InvalidOperationException("Unexpected ProfileConditionType");
  121. }
  122. }
  123. return false;
  124. }
  125. private bool IsConditionSatisfied(ProfileCondition condition, string currentValue)
  126. {
  127. if (string.IsNullOrEmpty(currentValue))
  128. {
  129. // If the value is unknown, it satisfies if not marked as required
  130. return !condition.IsRequired;
  131. }
  132. string expected = condition.Value;
  133. switch (condition.Condition)
  134. {
  135. case ProfileConditionType.Equals:
  136. return StringHelper.EqualsIgnoreCase(currentValue, expected);
  137. case ProfileConditionType.NotEquals:
  138. return !StringHelper.EqualsIgnoreCase(currentValue, expected);
  139. default:
  140. throw new InvalidOperationException("Unexpected ProfileConditionType");
  141. }
  142. }
  143. private bool IsConditionSatisfied(ProfileCondition condition, bool? currentValue)
  144. {
  145. if (!currentValue.HasValue)
  146. {
  147. // If the value is unknown, it satisfies if not marked as required
  148. return !condition.IsRequired;
  149. }
  150. bool expected;
  151. if (BoolHelper.TryParseCultureInvariant(condition.Value, out expected))
  152. {
  153. switch (condition.Condition)
  154. {
  155. case ProfileConditionType.Equals:
  156. return currentValue.Value == expected;
  157. case ProfileConditionType.NotEquals:
  158. return currentValue.Value != expected;
  159. default:
  160. throw new InvalidOperationException("Unexpected ProfileConditionType");
  161. }
  162. }
  163. return false;
  164. }
  165. private bool IsConditionSatisfied(ProfileCondition condition, float? currentValue)
  166. {
  167. if (!currentValue.HasValue)
  168. {
  169. // If the value is unknown, it satisfies if not marked as required
  170. return !condition.IsRequired;
  171. }
  172. float expected;
  173. if (FloatHelper.TryParseCultureInvariant(condition.Value, out expected))
  174. {
  175. switch (condition.Condition)
  176. {
  177. case ProfileConditionType.Equals:
  178. return currentValue.Value.Equals(expected);
  179. case ProfileConditionType.GreaterThanEqual:
  180. return currentValue.Value >= expected;
  181. case ProfileConditionType.LessThanEqual:
  182. return currentValue.Value <= expected;
  183. case ProfileConditionType.NotEquals:
  184. return !currentValue.Value.Equals(expected);
  185. default:
  186. throw new InvalidOperationException("Unexpected ProfileConditionType");
  187. }
  188. }
  189. return false;
  190. }
  191. private bool IsConditionSatisfied(ProfileCondition condition, double? currentValue)
  192. {
  193. if (!currentValue.HasValue)
  194. {
  195. // If the value is unknown, it satisfies if not marked as required
  196. return !condition.IsRequired;
  197. }
  198. double expected;
  199. if (DoubleHelper.TryParseCultureInvariant(condition.Value, out expected))
  200. {
  201. switch (condition.Condition)
  202. {
  203. case ProfileConditionType.Equals:
  204. return currentValue.Value.Equals(expected);
  205. case ProfileConditionType.GreaterThanEqual:
  206. return currentValue.Value >= expected;
  207. case ProfileConditionType.LessThanEqual:
  208. return currentValue.Value <= expected;
  209. case ProfileConditionType.NotEquals:
  210. return !currentValue.Value.Equals(expected);
  211. default:
  212. throw new InvalidOperationException("Unexpected ProfileConditionType");
  213. }
  214. }
  215. return false;
  216. }
  217. private bool IsConditionSatisfied(ProfileCondition condition, TransportStreamTimestamp? timestamp)
  218. {
  219. if (!timestamp.HasValue)
  220. {
  221. // If the value is unknown, it satisfies if not marked as required
  222. return !condition.IsRequired;
  223. }
  224. TransportStreamTimestamp expected = (TransportStreamTimestamp)Enum.Parse(typeof(TransportStreamTimestamp), condition.Value, true);
  225. switch (condition.Condition)
  226. {
  227. case ProfileConditionType.Equals:
  228. return timestamp == expected;
  229. case ProfileConditionType.NotEquals:
  230. return timestamp != expected;
  231. default:
  232. throw new InvalidOperationException("Unexpected ProfileConditionType");
  233. }
  234. }
  235. }
  236. }