| | | 1 | | using System; |
| | | 2 | | using System.Globalization; |
| | | 3 | | using Jellyfin.Data.Enums; |
| | | 4 | | using Jellyfin.Extensions; |
| | | 5 | | using MediaBrowser.Model.MediaInfo; |
| | | 6 | | |
| | | 7 | | namespace MediaBrowser.Model.Dlna |
| | | 8 | | { |
| | | 9 | | /// <summary> |
| | | 10 | | /// The condition processor. |
| | | 11 | | /// </summary> |
| | | 12 | | public static class ConditionProcessor |
| | | 13 | | { |
| | | 14 | | /// <summary> |
| | | 15 | | /// Checks if a video condition is satisfied. |
| | | 16 | | /// </summary> |
| | | 17 | | /// <param name="condition">The <see cref="ProfileCondition"/>.</param> |
| | | 18 | | /// <param name="width">The width.</param> |
| | | 19 | | /// <param name="height">The height.</param> |
| | | 20 | | /// <param name="videoBitDepth">The bit depth.</param> |
| | | 21 | | /// <param name="videoBitrate">The bitrate.</param> |
| | | 22 | | /// <param name="videoProfile">The video profile.</param> |
| | | 23 | | /// <param name="videoRangeType">The <see cref="VideoRangeType"/>.</param> |
| | | 24 | | /// <param name="videoLevel">The video level.</param> |
| | | 25 | | /// <param name="videoFramerate">The framerate.</param> |
| | | 26 | | /// <param name="packetLength">The packet length.</param> |
| | | 27 | | /// <param name="timestamp">The <see cref="TransportStreamTimestamp"/>.</param> |
| | | 28 | | /// <param name="isAnamorphic">A value indicating whether the video is anamorphic.</param> |
| | | 29 | | /// <param name="isInterlaced">A value indicating whether the video is interlaced.</param> |
| | | 30 | | /// <param name="refFrames">The reference frames.</param> |
| | | 31 | | /// <param name="numStreams">The number of streams.</param> |
| | | 32 | | /// <param name="numVideoStreams">The number of video streams.</param> |
| | | 33 | | /// <param name="numAudioStreams">The number of audio streams.</param> |
| | | 34 | | /// <param name="videoCodecTag">The video codec tag.</param> |
| | | 35 | | /// <param name="isAvc">A value indicating whether the video is AVC.</param> |
| | | 36 | | /// <returns><b>True</b> if the condition is satisfied.</returns> |
| | | 37 | | public static bool IsVideoConditionSatisfied( |
| | | 38 | | ProfileCondition condition, |
| | | 39 | | int? width, |
| | | 40 | | int? height, |
| | | 41 | | int? videoBitDepth, |
| | | 42 | | int? videoBitrate, |
| | | 43 | | string? videoProfile, |
| | | 44 | | VideoRangeType? videoRangeType, |
| | | 45 | | double? videoLevel, |
| | | 46 | | float? videoFramerate, |
| | | 47 | | int? packetLength, |
| | | 48 | | TransportStreamTimestamp? timestamp, |
| | | 49 | | bool? isAnamorphic, |
| | | 50 | | bool? isInterlaced, |
| | | 51 | | int? refFrames, |
| | | 52 | | int numStreams, |
| | | 53 | | int? numVideoStreams, |
| | | 54 | | int? numAudioStreams, |
| | | 55 | | string? videoCodecTag, |
| | | 56 | | bool? isAvc) |
| | | 57 | | { |
| | 1345 | 58 | | switch (condition.Property) |
| | | 59 | | { |
| | | 60 | | case ProfileConditionValue.IsInterlaced: |
| | 168 | 61 | | return IsConditionSatisfied(condition, isInterlaced); |
| | | 62 | | case ProfileConditionValue.IsAnamorphic: |
| | 238 | 63 | | return IsConditionSatisfied(condition, isAnamorphic); |
| | | 64 | | case ProfileConditionValue.IsAvc: |
| | 0 | 65 | | return IsConditionSatisfied(condition, isAvc); |
| | | 66 | | case ProfileConditionValue.VideoFramerate: |
| | 19 | 67 | | return IsConditionSatisfied(condition, videoFramerate); |
| | | 68 | | case ProfileConditionValue.VideoLevel: |
| | 251 | 69 | | return IsConditionSatisfied(condition, videoLevel); |
| | | 70 | | case ProfileConditionValue.VideoProfile: |
| | 259 | 71 | | return IsConditionSatisfied(condition, videoProfile); |
| | | 72 | | case ProfileConditionValue.VideoRangeType: |
| | 240 | 73 | | return IsConditionSatisfied(condition, videoRangeType); |
| | | 74 | | case ProfileConditionValue.VideoCodecTag: |
| | 19 | 75 | | return IsConditionSatisfied(condition, videoCodecTag); |
| | | 76 | | case ProfileConditionValue.PacketLength: |
| | 0 | 77 | | return IsConditionSatisfied(condition, packetLength); |
| | | 78 | | case ProfileConditionValue.VideoBitDepth: |
| | 0 | 79 | | return IsConditionSatisfied(condition, videoBitDepth); |
| | | 80 | | case ProfileConditionValue.VideoBitrate: |
| | 68 | 81 | | return IsConditionSatisfied(condition, videoBitrate); |
| | | 82 | | case ProfileConditionValue.Height: |
| | 0 | 83 | | return IsConditionSatisfied(condition, height); |
| | | 84 | | case ProfileConditionValue.Width: |
| | 18 | 85 | | return IsConditionSatisfied(condition, width); |
| | | 86 | | case ProfileConditionValue.RefFrames: |
| | 7 | 87 | | return IsConditionSatisfied(condition, refFrames); |
| | | 88 | | case ProfileConditionValue.NumStreams: |
| | 58 | 89 | | return IsConditionSatisfied(condition, numStreams); |
| | | 90 | | case ProfileConditionValue.NumAudioStreams: |
| | 0 | 91 | | return IsConditionSatisfied(condition, numAudioStreams); |
| | | 92 | | case ProfileConditionValue.NumVideoStreams: |
| | 0 | 93 | | return IsConditionSatisfied(condition, numVideoStreams); |
| | | 94 | | case ProfileConditionValue.VideoTimestamp: |
| | 0 | 95 | | return IsConditionSatisfied(condition, timestamp); |
| | | 96 | | default: |
| | 0 | 97 | | return true; |
| | | 98 | | } |
| | | 99 | | } |
| | | 100 | | |
| | | 101 | | /// <summary> |
| | | 102 | | /// Checks if a image condition is satisfied. |
| | | 103 | | /// </summary> |
| | | 104 | | /// <param name="condition">The <see cref="ProfileCondition"/>.</param> |
| | | 105 | | /// <param name="width">The width.</param> |
| | | 106 | | /// <param name="height">The height.</param> |
| | | 107 | | /// <returns><b>True</b> if the condition is satisfied.</returns> |
| | | 108 | | public static bool IsImageConditionSatisfied(ProfileCondition condition, int? width, int? height) |
| | | 109 | | { |
| | 0 | 110 | | switch (condition.Property) |
| | | 111 | | { |
| | | 112 | | case ProfileConditionValue.Height: |
| | 0 | 113 | | return IsConditionSatisfied(condition, height); |
| | | 114 | | case ProfileConditionValue.Width: |
| | 0 | 115 | | return IsConditionSatisfied(condition, width); |
| | | 116 | | default: |
| | 0 | 117 | | throw new ArgumentException("Unexpected condition on image file: " + condition.Property); |
| | | 118 | | } |
| | | 119 | | } |
| | | 120 | | |
| | | 121 | | /// <summary> |
| | | 122 | | /// Checks if an audio condition is satisfied. |
| | | 123 | | /// </summary> |
| | | 124 | | /// <param name="condition">The <see cref="ProfileCondition"/>.</param> |
| | | 125 | | /// <param name="audioChannels">The channel count.</param> |
| | | 126 | | /// <param name="audioBitrate">The bitrate.</param> |
| | | 127 | | /// <param name="audioSampleRate">The sample rate.</param> |
| | | 128 | | /// <param name="audioBitDepth">The bit depth.</param> |
| | | 129 | | /// <returns><b>True</b> if the condition is satisfied.</returns> |
| | | 130 | | public static bool IsAudioConditionSatisfied(ProfileCondition condition, int? audioChannels, int? audioBitrate, |
| | | 131 | | { |
| | 0 | 132 | | switch (condition.Property) |
| | | 133 | | { |
| | | 134 | | case ProfileConditionValue.AudioBitrate: |
| | 0 | 135 | | return IsConditionSatisfied(condition, audioBitrate); |
| | | 136 | | case ProfileConditionValue.AudioChannels: |
| | 0 | 137 | | return IsConditionSatisfied(condition, audioChannels); |
| | | 138 | | case ProfileConditionValue.AudioSampleRate: |
| | 0 | 139 | | return IsConditionSatisfied(condition, audioSampleRate); |
| | | 140 | | case ProfileConditionValue.AudioBitDepth: |
| | 0 | 141 | | return IsConditionSatisfied(condition, audioBitDepth); |
| | | 142 | | default: |
| | 0 | 143 | | throw new ArgumentException("Unexpected condition on audio file: " + condition.Property); |
| | | 144 | | } |
| | | 145 | | } |
| | | 146 | | |
| | | 147 | | /// <summary> |
| | | 148 | | /// Checks if an audio condition is satisfied for a video. |
| | | 149 | | /// </summary> |
| | | 150 | | /// <param name="condition">The <see cref="ProfileCondition"/>.</param> |
| | | 151 | | /// <param name="audioChannels">The channel count.</param> |
| | | 152 | | /// <param name="audioBitrate">The bitrate.</param> |
| | | 153 | | /// <param name="audioSampleRate">The sample rate.</param> |
| | | 154 | | /// <param name="audioBitDepth">The bit depth.</param> |
| | | 155 | | /// <param name="audioProfile">The profile.</param> |
| | | 156 | | /// <param name="isSecondaryTrack">A value indicating whether the audio is a secondary track.</param> |
| | | 157 | | /// <returns><b>True</b> if the condition is satisfied.</returns> |
| | | 158 | | public static bool IsVideoAudioConditionSatisfied( |
| | | 159 | | ProfileCondition condition, |
| | | 160 | | int? audioChannels, |
| | | 161 | | int? audioBitrate, |
| | | 162 | | int? audioSampleRate, |
| | | 163 | | int? audioBitDepth, |
| | | 164 | | string? audioProfile, |
| | | 165 | | bool? isSecondaryTrack) |
| | | 166 | | { |
| | 982 | 167 | | switch (condition.Property) |
| | | 168 | | { |
| | | 169 | | case ProfileConditionValue.AudioProfile: |
| | 0 | 170 | | return IsConditionSatisfied(condition, audioProfile); |
| | | 171 | | case ProfileConditionValue.AudioBitrate: |
| | 0 | 172 | | return IsConditionSatisfied(condition, audioBitrate); |
| | | 173 | | case ProfileConditionValue.AudioChannels: |
| | 194 | 174 | | return IsConditionSatisfied(condition, audioChannels); |
| | | 175 | | case ProfileConditionValue.IsSecondaryAudio: |
| | 788 | 176 | | return IsConditionSatisfied(condition, isSecondaryTrack); |
| | | 177 | | case ProfileConditionValue.AudioSampleRate: |
| | 0 | 178 | | return IsConditionSatisfied(condition, audioSampleRate); |
| | | 179 | | case ProfileConditionValue.AudioBitDepth: |
| | 0 | 180 | | return IsConditionSatisfied(condition, audioBitDepth); |
| | | 181 | | default: |
| | 0 | 182 | | throw new ArgumentException("Unexpected condition on audio file: " + condition.Property); |
| | | 183 | | } |
| | | 184 | | } |
| | | 185 | | |
| | | 186 | | private static bool IsConditionSatisfied(ProfileCondition condition, int? currentValue) |
| | | 187 | | { |
| | 345 | 188 | | if (!currentValue.HasValue) |
| | | 189 | | { |
| | | 190 | | // If the value is unknown, it satisfies if not marked as required |
| | 0 | 191 | | return !condition.IsRequired; |
| | | 192 | | } |
| | | 193 | | |
| | 345 | 194 | | var conditionType = condition.Condition; |
| | 345 | 195 | | if (condition.Condition == ProfileConditionType.EqualsAny) |
| | | 196 | | { |
| | 0 | 197 | | foreach (var singleConditionString in condition.Value.AsSpan().Split('|')) |
| | | 198 | | { |
| | 0 | 199 | | if (int.TryParse(singleConditionString, NumberStyles.Integer, CultureInfo.InvariantCulture, out int |
| | 0 | 200 | | && conditionValue.Equals(currentValue)) |
| | | 201 | | { |
| | 0 | 202 | | return true; |
| | | 203 | | } |
| | | 204 | | } |
| | | 205 | | |
| | 0 | 206 | | return false; |
| | | 207 | | } |
| | | 208 | | |
| | 345 | 209 | | if (int.TryParse(condition.Value, NumberStyles.Integer, CultureInfo.InvariantCulture, out var expected)) |
| | | 210 | | { |
| | | 211 | | switch (conditionType) |
| | | 212 | | { |
| | | 213 | | case ProfileConditionType.Equals: |
| | 0 | 214 | | return currentValue.Value.Equals(expected); |
| | | 215 | | case ProfileConditionType.GreaterThanEqual: |
| | 18 | 216 | | return currentValue.Value >= expected; |
| | | 217 | | case ProfileConditionType.LessThanEqual: |
| | 327 | 218 | | return currentValue.Value <= expected; |
| | | 219 | | case ProfileConditionType.NotEquals: |
| | 0 | 220 | | return !currentValue.Value.Equals(expected); |
| | | 221 | | default: |
| | 0 | 222 | | throw new InvalidOperationException("Unexpected ProfileConditionType: " + condition.Condition); |
| | | 223 | | } |
| | | 224 | | } |
| | | 225 | | |
| | 0 | 226 | | return false; |
| | | 227 | | } |
| | | 228 | | |
| | | 229 | | private static bool IsConditionSatisfied(ProfileCondition condition, string? currentValue) |
| | | 230 | | { |
| | 278 | 231 | | if (string.IsNullOrEmpty(currentValue)) |
| | | 232 | | { |
| | | 233 | | // If the value is unknown, it satisfies if not marked as required |
| | 0 | 234 | | return !condition.IsRequired; |
| | | 235 | | } |
| | | 236 | | |
| | 278 | 237 | | string expected = condition.Value; |
| | | 238 | | |
| | 278 | 239 | | switch (condition.Condition) |
| | | 240 | | { |
| | | 241 | | case ProfileConditionType.EqualsAny: |
| | 278 | 242 | | return expected.Split('|').Contains(currentValue, StringComparison.OrdinalIgnoreCase); |
| | | 243 | | case ProfileConditionType.Equals: |
| | 0 | 244 | | return string.Equals(currentValue, expected, StringComparison.OrdinalIgnoreCase); |
| | | 245 | | case ProfileConditionType.NotEquals: |
| | 0 | 246 | | return !string.Equals(currentValue, expected, StringComparison.OrdinalIgnoreCase); |
| | | 247 | | default: |
| | 0 | 248 | | throw new InvalidOperationException("Unexpected ProfileConditionType: " + condition.Condition); |
| | | 249 | | } |
| | | 250 | | } |
| | | 251 | | |
| | | 252 | | private static bool IsConditionSatisfied(ProfileCondition condition, bool? currentValue) |
| | | 253 | | { |
| | 1194 | 254 | | if (!currentValue.HasValue) |
| | | 255 | | { |
| | | 256 | | // If the value is unknown, it satisfies if not marked as required |
| | 185 | 257 | | return !condition.IsRequired; |
| | | 258 | | } |
| | | 259 | | |
| | 1009 | 260 | | if (bool.TryParse(condition.Value, out var expected)) |
| | | 261 | | { |
| | 1009 | 262 | | switch (condition.Condition) |
| | | 263 | | { |
| | | 264 | | case ProfileConditionType.Equals: |
| | 788 | 265 | | return currentValue.Value == expected; |
| | | 266 | | case ProfileConditionType.NotEquals: |
| | 221 | 267 | | return currentValue.Value != expected; |
| | | 268 | | default: |
| | 0 | 269 | | throw new InvalidOperationException("Unexpected ProfileConditionType: " + condition.Condition); |
| | | 270 | | } |
| | | 271 | | } |
| | | 272 | | |
| | 0 | 273 | | return false; |
| | | 274 | | } |
| | | 275 | | |
| | | 276 | | private static bool IsConditionSatisfied(ProfileCondition condition, double? currentValue) |
| | | 277 | | { |
| | 270 | 278 | | if (!currentValue.HasValue) |
| | | 279 | | { |
| | | 280 | | // If the value is unknown, it satisfies if not marked as required |
| | 0 | 281 | | return !condition.IsRequired; |
| | | 282 | | } |
| | | 283 | | |
| | 270 | 284 | | var conditionType = condition.Condition; |
| | 270 | 285 | | if (condition.Condition == ProfileConditionType.EqualsAny) |
| | | 286 | | { |
| | 0 | 287 | | foreach (var singleConditionString in condition.Value.AsSpan().Split('|')) |
| | | 288 | | { |
| | 0 | 289 | | if (double.TryParse(singleConditionString, NumberStyles.Float | NumberStyles.AllowThousands, Culture |
| | 0 | 290 | | && conditionValue.Equals(currentValue)) |
| | | 291 | | { |
| | 0 | 292 | | return true; |
| | | 293 | | } |
| | | 294 | | } |
| | | 295 | | |
| | 0 | 296 | | return false; |
| | | 297 | | } |
| | | 298 | | |
| | 270 | 299 | | if (double.TryParse(condition.Value, NumberStyles.Float | NumberStyles.AllowThousands, CultureInfo.Invariant |
| | | 300 | | { |
| | | 301 | | switch (conditionType) |
| | | 302 | | { |
| | | 303 | | case ProfileConditionType.Equals: |
| | 0 | 304 | | return currentValue.Value.Equals(expected); |
| | | 305 | | case ProfileConditionType.GreaterThanEqual: |
| | 0 | 306 | | return currentValue.Value >= expected; |
| | | 307 | | case ProfileConditionType.LessThanEqual: |
| | 270 | 308 | | return currentValue.Value <= expected; |
| | | 309 | | case ProfileConditionType.NotEquals: |
| | 0 | 310 | | return !currentValue.Value.Equals(expected); |
| | | 311 | | default: |
| | 0 | 312 | | throw new InvalidOperationException("Unexpected ProfileConditionType: " + condition.Condition); |
| | | 313 | | } |
| | | 314 | | } |
| | | 315 | | |
| | 0 | 316 | | return false; |
| | | 317 | | } |
| | | 318 | | |
| | | 319 | | private static bool IsConditionSatisfied(ProfileCondition condition, TransportStreamTimestamp? timestamp) |
| | | 320 | | { |
| | 0 | 321 | | if (!timestamp.HasValue) |
| | | 322 | | { |
| | | 323 | | // If the value is unknown, it satisfies if not marked as required |
| | 0 | 324 | | return !condition.IsRequired; |
| | | 325 | | } |
| | | 326 | | |
| | 0 | 327 | | var expected = (TransportStreamTimestamp)Enum.Parse(typeof(TransportStreamTimestamp), condition.Value, true) |
| | | 328 | | |
| | 0 | 329 | | switch (condition.Condition) |
| | | 330 | | { |
| | | 331 | | case ProfileConditionType.Equals: |
| | 0 | 332 | | return timestamp == expected; |
| | | 333 | | case ProfileConditionType.NotEquals: |
| | 0 | 334 | | return timestamp != expected; |
| | | 335 | | default: |
| | 0 | 336 | | throw new InvalidOperationException("Unexpected ProfileConditionType: " + condition.Condition); |
| | | 337 | | } |
| | | 338 | | } |
| | | 339 | | |
| | | 340 | | private static bool IsConditionSatisfied(ProfileCondition condition, VideoRangeType? currentValue) |
| | | 341 | | { |
| | 240 | 342 | | if (!currentValue.HasValue || currentValue.Equals(VideoRangeType.Unknown)) |
| | | 343 | | { |
| | | 344 | | // If the value is unknown, it satisfies if not marked as required |
| | 0 | 345 | | return !condition.IsRequired; |
| | | 346 | | } |
| | | 347 | | |
| | | 348 | | // Special case: HDR10 also satisfies if the video is HDR10Plus |
| | 240 | 349 | | if (currentValue.Value == VideoRangeType.HDR10Plus) |
| | | 350 | | { |
| | 0 | 351 | | if (IsConditionSatisfied(condition, VideoRangeType.HDR10)) |
| | | 352 | | { |
| | 0 | 353 | | return true; |
| | | 354 | | } |
| | | 355 | | } |
| | | 356 | | |
| | 240 | 357 | | var conditionType = condition.Condition; |
| | 240 | 358 | | if (conditionType == ProfileConditionType.EqualsAny) |
| | | 359 | | { |
| | 1108 | 360 | | foreach (var singleConditionString in condition.Value.AsSpan().Split('|')) |
| | | 361 | | { |
| | 420 | 362 | | if (Enum.TryParse(singleConditionString, true, out VideoRangeType conditionValue) |
| | 420 | 363 | | && conditionValue.Equals(currentValue)) |
| | | 364 | | { |
| | 212 | 365 | | return true; |
| | | 366 | | } |
| | | 367 | | } |
| | | 368 | | |
| | 28 | 369 | | return false; |
| | | 370 | | } |
| | | 371 | | |
| | 0 | 372 | | if (Enum.TryParse(condition.Value, true, out VideoRangeType expected)) |
| | | 373 | | { |
| | 0 | 374 | | return conditionType switch |
| | 0 | 375 | | { |
| | 0 | 376 | | ProfileConditionType.Equals => currentValue.Value == expected, |
| | 0 | 377 | | ProfileConditionType.NotEquals => currentValue.Value != expected, |
| | 0 | 378 | | _ => throw new InvalidOperationException("Unexpected ProfileConditionType: " + condition.Condition) |
| | 0 | 379 | | }; |
| | | 380 | | } |
| | | 381 | | |
| | 0 | 382 | | return false; |
| | | 383 | | } |
| | | 384 | | } |
| | | 385 | | } |