CodecProfile.cs 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Xml.Serialization;
  5. using MediaBrowser.Model.Extensions;
  6. namespace MediaBrowser.Model.Dlna;
  7. /// <summary>
  8. /// Defines the <see cref="CodecProfile"/>.
  9. /// </summary>
  10. public class CodecProfile
  11. {
  12. /// <summary>
  13. /// Initializes a new instance of the <see cref="CodecProfile"/> class.
  14. /// </summary>
  15. public CodecProfile()
  16. {
  17. Conditions = [];
  18. ApplyConditions = [];
  19. }
  20. /// <summary>
  21. /// Gets or sets the <see cref="CodecType"/> which this container must meet.
  22. /// </summary>
  23. [XmlAttribute("type")]
  24. public CodecType Type { get; set; }
  25. /// <summary>
  26. /// Gets or sets the list of <see cref="ProfileCondition"/> which this profile must meet.
  27. /// </summary>
  28. public ProfileCondition[] Conditions { get; set; }
  29. /// <summary>
  30. /// Gets or sets the list of <see cref="ProfileCondition"/> to apply if this profile is met.
  31. /// </summary>
  32. public ProfileCondition[] ApplyConditions { get; set; }
  33. /// <summary>
  34. /// Gets or sets the codec(s) that this profile applies to.
  35. /// </summary>
  36. [XmlAttribute("codec")]
  37. public string? Codec { get; set; }
  38. /// <summary>
  39. /// Gets or sets the container(s) which this profile will be applied to.
  40. /// </summary>
  41. [XmlAttribute("container")]
  42. public string? Container { get; set; }
  43. /// <summary>
  44. /// Gets or sets the sub-container(s) which this profile will be applied to.
  45. /// </summary>
  46. [XmlAttribute("subcontainer")]
  47. public string? SubContainer { get; set; }
  48. /// <summary>
  49. /// Checks to see whether the codecs and containers contain the given parameters.
  50. /// </summary>
  51. /// <param name="codecs">The codecs to match.</param>
  52. /// <param name="container">The container to match.</param>
  53. /// <param name="useSubContainer">Consider sub-containers.</param>
  54. /// <returns>True if both conditions are met.</returns>
  55. public bool ContainsAnyCodec(IReadOnlyList<string> codecs, string? container, bool useSubContainer = false)
  56. {
  57. var containerToCheck = useSubContainer && string.Equals(Container, "hls", StringComparison.OrdinalIgnoreCase) ? SubContainer : Container;
  58. return ContainerHelper.ContainsContainer(containerToCheck, container) && codecs.Any(c => ContainerHelper.ContainsContainer(Codec, false, c));
  59. }
  60. /// <summary>
  61. /// Checks to see whether the codecs and containers contain the given parameters.
  62. /// </summary>
  63. /// <param name="codec">The codec to match.</param>
  64. /// <param name="container">The container to match.</param>
  65. /// <param name="useSubContainer">Consider sub-containers.</param>
  66. /// <returns>True if both conditions are met.</returns>
  67. public bool ContainsAnyCodec(string? codec, string? container, bool useSubContainer = false)
  68. {
  69. return ContainsAnyCodec(codec.AsSpan(), container, useSubContainer);
  70. }
  71. /// <summary>
  72. /// Checks to see whether the codecs and containers contain the given parameters.
  73. /// </summary>
  74. /// <param name="codec">The codec to match.</param>
  75. /// <param name="container">The container to match.</param>
  76. /// <param name="useSubContainer">Consider sub-containers.</param>
  77. /// <returns>True if both conditions are met.</returns>
  78. public bool ContainsAnyCodec(ReadOnlySpan<char> codec, string? container, bool useSubContainer = false)
  79. {
  80. var containerToCheck = useSubContainer && string.Equals(Container, "hls", StringComparison.OrdinalIgnoreCase) ? SubContainer : Container;
  81. return ContainerHelper.ContainsContainer(containerToCheck, container) && ContainerHelper.ContainsContainer(Codec, false, codec);
  82. }
  83. }