ConditionProcessor.cs 11 KB

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