ConditionProcessor.cs 8.5 KB

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