From 9ff15c78ec02abeccec12e6f5d95058f110332b8 Mon Sep 17 00:00:00 2001 From: Xavier Laffargue Date: Fri, 5 Jul 2024 20:23:03 -0400 Subject: [PATCH] Reformat --- README.md | 30 +- src/M3U8Parser/Attributes/Audio.cs | 7 +- src/M3U8Parser/Attributes/Autoselect.cs | 7 +- src/M3U8Parser/Attributes/AverageBandwidth.cs | 9 +- src/M3U8Parser/Attributes/Bandwidth.cs | 7 +- .../Attributes/BaseAttribute/BoolAttribute.cs | 57 +-- .../BaseAttribute/CustomAttribute.cs | 114 +++--- .../BaseAttribute/DecimalAttribute.cs | 52 +-- .../Attributes/BaseAttribute/HdcpLevelType.cs | 96 ++--- .../Attributes/BaseAttribute/MediaType.cs | 92 ++--- .../Attributes/BaseAttribute/MethodType.cs | 93 ++--- .../Attributes/BaseAttribute/PlaylistType.cs | 100 ++--- .../BaseAttribute/ResolutionType.cs | 27 +- .../BaseAttribute/StringAttribute.cs | 51 +-- .../BaseAttribute/VideoRangeType.cs | 96 ++--- src/M3U8Parser/Attributes/Characteristics.cs | 7 +- src/M3U8Parser/Attributes/ClosedCaptions.cs | 7 +- src/M3U8Parser/Attributes/Codecs.cs | 7 +- src/M3U8Parser/Attributes/Default.cs | 7 +- src/M3U8Parser/Attributes/FrameRate.cs | 7 +- src/M3U8Parser/Attributes/GroupId.cs | 7 +- src/M3U8Parser/Attributes/HdcpLevel.cs | 7 +- src/M3U8Parser/Attributes/InstreamId.cs | 7 +- src/M3U8Parser/Attributes/Language.cs | 7 +- src/M3U8Parser/Attributes/Method.cs | 7 +- src/M3U8Parser/Attributes/Name.cs | 7 +- src/M3U8Parser/Attributes/Resolution.cs | 7 +- src/M3U8Parser/Attributes/Subtitles.cs | 7 +- src/M3U8Parser/Attributes/Type.cs | 7 +- src/M3U8Parser/Attributes/Uri.cs | 7 +- src/M3U8Parser/Attributes/Video.cs | 7 +- src/M3U8Parser/Attributes/VideoRange.cs | 7 +- src/M3U8Parser/CodeAnalysis.ruleset | 13 + src/M3U8Parser/ExtXType/BaseExtX.cs | 30 +- src/M3U8Parser/ExtXType/HlsVersion.cs | 24 +- src/M3U8Parser/ExtXType/IframeStreamInf.cs | 150 +++---- src/M3U8Parser/ExtXType/Key.cs | 38 +- src/M3U8Parser/ExtXType/Map.cs | 13 +- src/M3U8Parser/ExtXType/Media.cs | 118 +++--- src/M3U8Parser/ExtXType/MediaSegment.cs | 102 ++--- src/M3U8Parser/ExtXType/MediaSequence.cs | 22 +- src/M3U8Parser/ExtXType/PlaylistTypeExt.cs | 110 +++-- src/M3U8Parser/ExtXType/Segment.cs | 53 ++- src/M3U8Parser/ExtXType/StreamInf.cs | 168 ++++---- src/M3U8Parser/ExtXType/TargetDuration.cs | 24 +- .../Extensions/StringBuilderExtension.cs | 17 +- src/M3U8Parser/Extensions/StringExtension.cs | 14 +- src/M3U8Parser/GlobalSuppression.cs | 2 + src/M3U8Parser/Interfaces/IAttribute.cs | 3 +- src/M3U8Parser/Interfaces/IExtXType.cs | 8 +- src/M3U8Parser/M3U8Parser.csproj | 12 +- src/M3U8Parser/MasterPlaylist.cs | 211 +++++----- src/M3U8Parser/MediaPlaylist.cs | 241 ++++++----- src/M3U8Parser/stylecop.json | 24 ++ tests/M3U8Parser.Tests/CodeAnalysis.ruleset | 11 + .../CreateMediaPlaylistTests.cs | 22 +- tests/M3U8Parser.Tests/LoadFromFileTest.cs | 38 +- .../LoadMasterPlaylistTests.cs | 385 +++++++++--------- .../LoadMediaPlaylistByteRangeTests.cs | 277 +++++++------ .../LoadMediaPlaylistTests.cs | 311 +++++++------- .../M3U8Parser.Tests/M3U8Parser.Tests.csproj | 17 +- 61 files changed, 1758 insertions(+), 1657 deletions(-) create mode 100644 src/M3U8Parser/CodeAnalysis.ruleset create mode 100644 src/M3U8Parser/GlobalSuppression.cs create mode 100644 src/M3U8Parser/stylecop.json create mode 100644 tests/M3U8Parser.Tests/CodeAnalysis.ruleset diff --git a/README.md b/README.md index 08193f3..cb29b69 100644 --- a/README.md +++ b/README.md @@ -1,19 +1,22 @@ # M3U8 HLS Parser C# ## Installation + Package NuGet : https://www.nuget.org/packages/M3U8Parser/ ```dotnet add package M3U8Parser``` ## Documentation -M3U8Parser makes it easy to read, edit and create a m3u8 file. +M3U8Parser makes it easy to read, edit and create a m3u8 file. + * M3U8Parser does not try to validate your file, it's your responsability to follow RFC. * Require .netstandard2.0 / .net6 Usage: Read a file and edit it + ```csharp // Load a file var masterPlaylist = MasterPlaylist.LoadFromFile("master.m3u8"); @@ -29,6 +32,7 @@ Read a file and edit it ``` Or your can create a master file + ```csharp var masterPlaylist = new MasterPlaylist(hlsVersion: 4); @@ -67,6 +71,7 @@ Or your can create a master file ``` This code should produce the following master playlist: + ``` #EXTM3U #EXT-X-VERSION:4 @@ -80,23 +85,26 @@ v0.m3u8 v1.m3u8 ``` -Limitation: -* Alpha version : Only master is supported (playlist be coming) +Limitation: +* Alpha version : Only master is supported (playlist be coming) ## Supported tags + The following tags should be fully supported: ### Basics -| TAGS | -| ------------- | -| EXTM3U | + +| TAGS | +|---------------| +| EXTM3U | | EXT-X-VERSION | ### Master Playlist Tags -| TAGS | ATTRIBUTE | -| ------------- |---------------------------------------------------------------------------------------------------------------------| -| EXT-X-MEDIA | GROUP-ID, AUTOSELECT, DEFAULT, LANGUAGE, NAME, TYPE, URI, CHARACTERISTICS, INSTREAM-ID | -| EXT-X-STREAM-INF | BANDWIDTH, CODECS, RESOLUTION, URI, SUBTITLES, VIDEO-RANGE, HDCP-LEVEL, AVERAGE-BANDWIDTH | -| EXT-X-I-FRAME-STREAM-INF | BANDWIDTH, CODECS, RESOLUTION, AUDIO, VIDEO, CLOSED-CAPTIONS | + +| TAGS | ATTRIBUTE | +|--------------------------|--------------------------------------------------------------------------------------------| +| EXT-X-MEDIA | GROUP-ID, AUTOSELECT, DEFAULT, LANGUAGE, NAME, TYPE, URI, CHARACTERISTICS, INSTREAM-ID | +| EXT-X-STREAM-INF | BANDWIDTH, CODECS, RESOLUTION, URI, SUBTITLES, VIDEO-RANGE, HDCP-LEVEL, AVERAGE-BANDWIDTH | +| EXT-X-I-FRAME-STREAM-INF | BANDWIDTH, CODECS, RESOLUTION, AUDIO, VIDEO, CLOSED-CAPTIONS | diff --git a/src/M3U8Parser/Attributes/Audio.cs b/src/M3U8Parser/Attributes/Audio.cs index c4225fb..3017033 100644 --- a/src/M3U8Parser/Attributes/Audio.cs +++ b/src/M3U8Parser/Attributes/Audio.cs @@ -1,10 +1,11 @@ -using M3U8Parser.Attributes.BaseAttribute; - namespace M3U8Parser.Attributes { + using M3U8Parser.Attributes.BaseAttribute; + public class Audio : StringAttribute { - public Audio() : base("AUDIO") + public Audio() + : base("AUDIO") { } } diff --git a/src/M3U8Parser/Attributes/Autoselect.cs b/src/M3U8Parser/Attributes/Autoselect.cs index 530f0fe..2858f73 100644 --- a/src/M3U8Parser/Attributes/Autoselect.cs +++ b/src/M3U8Parser/Attributes/Autoselect.cs @@ -1,10 +1,11 @@ -using M3U8Parser.Attributes.BaseAttribute; - namespace M3U8Parser.Attributes { + using M3U8Parser.Attributes.BaseAttribute; + public class Autoselect : BoolAttribute { - public Autoselect() : base("AUTOSELECT") + public Autoselect() + : base("AUTOSELECT") { } } diff --git a/src/M3U8Parser/Attributes/AverageBandwidth.cs b/src/M3U8Parser/Attributes/AverageBandwidth.cs index 5ae6717..0a2c69f 100644 --- a/src/M3U8Parser/Attributes/AverageBandwidth.cs +++ b/src/M3U8Parser/Attributes/AverageBandwidth.cs @@ -1,10 +1,11 @@ -using M3U8Parser.Attributes.BaseAttribute; - namespace M3U8Parser.Attributes { - public class AverageBandwidth: CustomAttribute + using M3U8Parser.Attributes.BaseAttribute; + + public class AverageBandwidth : CustomAttribute { - public AverageBandwidth() : base("AVERAGE-BANDWIDTH") + public AverageBandwidth() + : base("AVERAGE-BANDWIDTH") { } } diff --git a/src/M3U8Parser/Attributes/Bandwidth.cs b/src/M3U8Parser/Attributes/Bandwidth.cs index 4248c55..2670e6b 100644 --- a/src/M3U8Parser/Attributes/Bandwidth.cs +++ b/src/M3U8Parser/Attributes/Bandwidth.cs @@ -1,10 +1,11 @@ -using M3U8Parser.Attributes.BaseAttribute; - namespace M3U8Parser.Attributes { + using M3U8Parser.Attributes.BaseAttribute; + public class Bandwidth : CustomAttribute { - public Bandwidth() : base("BANDWIDTH") + public Bandwidth() + : base("BANDWIDTH") { } } diff --git a/src/M3U8Parser/Attributes/BaseAttribute/BoolAttribute.cs b/src/M3U8Parser/Attributes/BaseAttribute/BoolAttribute.cs index d7e4b6f..d9c4b7c 100644 --- a/src/M3U8Parser/Attributes/BaseAttribute/BoolAttribute.cs +++ b/src/M3U8Parser/Attributes/BaseAttribute/BoolAttribute.cs @@ -1,41 +1,42 @@ -using System.Text.RegularExpressions; - namespace M3U8Parser.Attributes.BaseAttribute { - public class BoolAttribute : CustomAttribute - { + using System.Text.RegularExpressions; + + public class BoolAttribute : CustomAttribute + { private const string YesValue = "YES"; private const string NoValue = "NO"; - public BoolAttribute(string attributeName) : base(attributeName) - { - } + public BoolAttribute(string attributeName) + : base(attributeName) + { + } - public override string ToString() - { - return $"{AttributeName}={BoolToString(Value)}"; - } + public override string ToString() + { + return $"{AttributeName}={BoolToString(Value)}"; + } - public override void Read(string content) + public override void Read(string content) { var pattern = $"(?={AttributeName})(.*?)(?=,|$)"; - var match = Regex.Match(content.Trim(), pattern, RegexOptions.Multiline & RegexOptions.IgnoreCase); - + var match = Regex.Match(content.Trim(), pattern, RegexOptions.Multiline & RegexOptions.IgnoreCase); + if (match.Success) - { - var valueFounded = match.Groups[0].Value.Split('=')[1]; - Value = StringToBool(valueFounded); - } - } + { + var valueFounded = match.Groups[0].Value.Split('=')[1]; + Value = StringToBool(valueFounded); + } + } - private string BoolToString(bool value) - { - return value ? YesValue : NoValue; - } + private string BoolToString(bool value) + { + return value ? YesValue : NoValue; + } - private bool StringToBool(string value) - { - return value == YesValue; - } - } + private bool StringToBool(string value) + { + return value == YesValue; + } + } } \ No newline at end of file diff --git a/src/M3U8Parser/Attributes/BaseAttribute/CustomAttribute.cs b/src/M3U8Parser/Attributes/BaseAttribute/CustomAttribute.cs index d1ce9f1..2dcc63e 100644 --- a/src/M3U8Parser/Attributes/BaseAttribute/CustomAttribute.cs +++ b/src/M3U8Parser/Attributes/BaseAttribute/CustomAttribute.cs @@ -1,60 +1,60 @@ -using System; -using System.Text.RegularExpressions; -using M3U8Parser.Interfaces; - namespace M3U8Parser.Attributes.BaseAttribute { - public class CustomAttribute : IAttribute - { - public CustomAttribute(string attributeName) - { - AttributeName = attributeName; - } - - public T Value { get; set; } - - protected string AttributeName { get; } - - public override string ToString() - { - if (Value != null) - { - return $"{AttributeName}={Value}"; - } - - return string.Empty; - } - - public virtual void Read(string content) - { - var match = Regex.Match(content.Trim(), $"[,|:](?={AttributeName})(.*?)(?=,|$)", - RegexOptions.Multiline & RegexOptions.IgnoreCase); - - var type = typeof(T); - - if (match.Success) - { - var valueFounded = match.Groups[0].Value.Split('=')[1]; - - if (type.IsGenericType && type.GetGenericTypeDefinition().Equals(typeof(Nullable<>))) - { - type = Nullable.GetUnderlyingType(type); - } - - if (typeof(ICustomAttribute).IsAssignableFrom(type)) - { - var instanceAttribute = (ICustomAttribute)Activator.CreateInstance(type, false); - Value = (T)instanceAttribute.ParseFromString(valueFounded); - } - else - { - Value = (T)Convert.ChangeType(valueFounded, type); - } - } - else - { - Value = default(T); - } - } - } + using System; + using System.Text.RegularExpressions; + using M3U8Parser.Interfaces; + + public class CustomAttribute : IAttribute + { + public CustomAttribute(string attributeName) + { + AttributeName = attributeName; + } + + public T Value { get; set; } + + protected string AttributeName { get; } + + public override string ToString() + { + if (Value != null) + { + return $"{AttributeName}={Value}"; + } + + return string.Empty; + } + + public virtual void Read(string content) + { + var match = Regex.Match(content.Trim(), $"[,|:](?={AttributeName})(.*?)(?=,|$)", + RegexOptions.Multiline & RegexOptions.IgnoreCase); + + var type = typeof(T); + + if (match.Success) + { + var valueFounded = match.Groups[0].Value.Split('=')[1]; + + if (type.IsGenericType && type.GetGenericTypeDefinition().Equals(typeof(Nullable<>))) + { + type = Nullable.GetUnderlyingType(type); + } + + if (typeof(ICustomAttribute).IsAssignableFrom(type)) + { + var instanceAttribute = (ICustomAttribute)Activator.CreateInstance(type, false); + Value = (T)instanceAttribute.ParseFromString(valueFounded); + } + else + { + Value = (T)Convert.ChangeType(valueFounded, type); + } + } + else + { + Value = default; + } + } + } } \ No newline at end of file diff --git a/src/M3U8Parser/Attributes/BaseAttribute/DecimalAttribute.cs b/src/M3U8Parser/Attributes/BaseAttribute/DecimalAttribute.cs index eeaa9cf..d028688 100644 --- a/src/M3U8Parser/Attributes/BaseAttribute/DecimalAttribute.cs +++ b/src/M3U8Parser/Attributes/BaseAttribute/DecimalAttribute.cs @@ -1,34 +1,34 @@ -using System.Globalization; -using System.Text.RegularExpressions; - namespace M3U8Parser.Attributes.BaseAttribute { - public class DecimalAttribute : CustomAttribute - { - public DecimalAttribute(string attributeName) : base(attributeName) - { - } + using System.Globalization; + using System.Text.RegularExpressions; + + public class DecimalAttribute : CustomAttribute + { + public DecimalAttribute(string attributeName) : base(attributeName) + { + } - public override string ToString() - { - if (Value != null) - { - return $"{AttributeName}={Value.Value.ToString(CultureInfo.InvariantCulture)}"; - } + public override string ToString() + { + if (Value != null) + { + return $"{AttributeName}={Value.Value.ToString(CultureInfo.InvariantCulture)}"; + } + + return string.Empty; + } - return string.Empty; - } - - public override void Read(string content) + public override void Read(string content) { var pattern = $"(?={AttributeName})(.*?)(?=,|$)"; - var match = Regex.Match(content.Trim(), pattern, RegexOptions.Multiline & RegexOptions.IgnoreCase); - + var match = Regex.Match(content.Trim(), pattern, RegexOptions.Multiline & RegexOptions.IgnoreCase); + if (match.Success) - { - var valueFounded = match.Groups[0].Value.Split('=')[1]; - Value = decimal.Parse(valueFounded, CultureInfo.InvariantCulture); - } - } - } + { + var valueFounded = match.Groups[0].Value.Split('=')[1]; + Value = decimal.Parse(valueFounded, CultureInfo.InvariantCulture); + } + } + } } \ No newline at end of file diff --git a/src/M3U8Parser/Attributes/BaseAttribute/HdcpLevelType.cs b/src/M3U8Parser/Attributes/BaseAttribute/HdcpLevelType.cs index 28981c1..f233ec7 100644 --- a/src/M3U8Parser/Attributes/BaseAttribute/HdcpLevelType.cs +++ b/src/M3U8Parser/Attributes/BaseAttribute/HdcpLevelType.cs @@ -1,58 +1,58 @@ -using System; -using M3U8Parser.Interfaces; - namespace M3U8Parser.Attributes.BaseAttribute { - public class HdcpLevelType : ICustomAttribute, IEquatable - { - public HdcpLevelType() - { - } + using System; + using M3U8Parser.Interfaces; + + public class HdcpLevelType : ICustomAttribute, IEquatable + { + public HdcpLevelType() + { + } + + private HdcpLevelType(string value) + { + this.value = value; + } + + public static HdcpLevelType NONE => new ("NONE"); + + public static HdcpLevelType TYPE_0 => new ("TYPE-0"); + + public static HdcpLevelType TYPE_1 => new ("TYPE-1"); + + private string value { get; } + + public object ParseFromString(string value) + { + switch (value) + { + case "NONE": + return NONE; - private HdcpLevelType(string value) - { - _value = value; - } + case "TYPE-0": + return TYPE_0; - private string _value { get; } + case "TYPE-1": + return TYPE_1; - public static HdcpLevelType NONE => new ("NONE"); + default: + return null; + } + } - public static HdcpLevelType TYPE_0 => new ("TYPE-0"); + public bool Equals(HdcpLevelType other) + { + if (other!.ToString() == ToString()) + { + return true; + } - public static HdcpLevelType TYPE_1 => new ("TYPE-1"); + return false; + } public override string ToString() - { - return _value; - } - - public object ParseFromString(string value) - { - switch (value) - { - case "NONE": - return NONE; - - case "TYPE-0": - return TYPE_0; - - case "TYPE-1": - return TYPE_1; - - default: - return null; - } - } - - public bool Equals(HdcpLevelType other) - { - if (other!.ToString() == ToString()) - { - return true; - } - - return false; - } - } + { + return value; + } + } } \ No newline at end of file diff --git a/src/M3U8Parser/Attributes/BaseAttribute/MediaType.cs b/src/M3U8Parser/Attributes/BaseAttribute/MediaType.cs index 3650cec..0769fce 100644 --- a/src/M3U8Parser/Attributes/BaseAttribute/MediaType.cs +++ b/src/M3U8Parser/Attributes/BaseAttribute/MediaType.cs @@ -1,63 +1,63 @@ -using System; -using M3U8Parser.Interfaces; - namespace M3U8Parser.Attributes.BaseAttribute { - public class MediaType : ICustomAttribute, IEquatable - { - public MediaType() - { - } + using System; + using M3U8Parser.Interfaces; + + public class MediaType : ICustomAttribute, IEquatable + { + public MediaType() + { + } - private MediaType(string value) - { - _value = value; - } + private MediaType(string value) + { + _value = value; + } - private string _value { get; } + private string _value { get; } - public static MediaType Audio => new ("AUDIO"); + public static MediaType Audio => new ("AUDIO"); - public static MediaType Video => new ("VIDEO"); + public static MediaType Video => new ("VIDEO"); - public static MediaType Subtitles => new ("SUBTITLES"); + public static MediaType Subtitles => new ("SUBTITLES"); - public static MediaType CloseCaptions => new ("CLOSED-CAPTIONS"); + public static MediaType CloseCaptions => new ("CLOSED-CAPTIONS"); - public override string ToString() - { - return _value; - } + public object ParseFromString(string value) + { + switch (value) + { + case "AUDIO": + return Audio; - public object ParseFromString(string value) - { - switch (value) - { - case "AUDIO": - return Audio; + case "VIDEO": + return Video; - case "VIDEO": - return Video; + case "SUBTITLES": + return Subtitles; - case "SUBTITLES": - return Subtitles; + case "CLOSED-CAPTIONS": + return CloseCaptions; - case "CLOSED-CAPTIONS": - return CloseCaptions; + default: + return null; + } + } - default: - return null; - } - } + public bool Equals(MediaType other) + { + if (other.ToString() == ToString()) + { + return true; + } - public bool Equals(MediaType other) - { - if (other.ToString() == ToString()) - { - return true; - } + return false; + } - return false; - } - } + public override string ToString() + { + return _value; + } + } } \ No newline at end of file diff --git a/src/M3U8Parser/Attributes/BaseAttribute/MethodType.cs b/src/M3U8Parser/Attributes/BaseAttribute/MethodType.cs index 40c89cc..9023f26 100644 --- a/src/M3U8Parser/Attributes/BaseAttribute/MethodType.cs +++ b/src/M3U8Parser/Attributes/BaseAttribute/MethodType.cs @@ -1,58 +1,55 @@ -using System; -using M3U8Parser.Interfaces; - namespace M3U8Parser.Attributes.BaseAttribute { - public class MethodType : ICustomAttribute, IEquatable - { - public MethodType() - { - } + using System; + using M3U8Parser.Interfaces; + + public class MethodType : ICustomAttribute, IEquatable + { + public MethodType() + { + } + + private MethodType(string value) + { + _value = value; + } + + private string _value { get; } + + public static MethodType None => new ("NONE"); + + public static MethodType AES_128 => new ("AES-128"); + + public static MethodType SAMPLE_AES => new ("SAMPLE-AES"); + + public object ParseFromString(string value) + { + switch (value) + { + case "NONE": + return None; - private MethodType(string value) - { - _value = value; - } + case "AES-128": + return AES_128; - private string _value { get; } + case "SAMPLE-AES": + return SAMPLE_AES; - public static MethodType None => new ("NONE"); + default: + return null; + } + } - public static MethodType AES_128 => new("AES-128"); + public bool Equals(MethodType other) + { + if (other.ToString() == ToString()) return true; - public static MethodType SAMPLE_AES => new("SAMPLE-AES"); + return false; + } public override string ToString() - { - return _value; - } - - public object ParseFromString(string value) - { - switch (value) - { - case "NONE": - return None; - - case "AES-128": - return AES_128; - - case "SAMPLE-AES": - return SAMPLE_AES; - - default: - return null; - } - } - - public bool Equals(MethodType other) - { - if (other.ToString() == ToString()) - { - return true; - } - - return false; - } - } + { + return _value; + } + } } \ No newline at end of file diff --git a/src/M3U8Parser/Attributes/BaseAttribute/PlaylistType.cs b/src/M3U8Parser/Attributes/BaseAttribute/PlaylistType.cs index b1a4b0a..b5e456b 100644 --- a/src/M3U8Parser/Attributes/BaseAttribute/PlaylistType.cs +++ b/src/M3U8Parser/Attributes/BaseAttribute/PlaylistType.cs @@ -1,53 +1,53 @@ -using System; -using M3U8Parser.Interfaces; - namespace M3U8Parser.Attributes.BaseAttribute { - public class PlaylistType : ICustomAttribute, IEquatable - { - public PlaylistType() - { - } - - private PlaylistType(string value) - { - _value = value; - } - - private string _value { get; } - - public static PlaylistType Event => new ("EVENT"); - - public static PlaylistType Vod => new ("VOD"); - - public override string ToString() - { - return _value; - } - - public object ParseFromString(string value) - { - switch (value) - { - case "EVENT": - return Event; - - case "VOD": - return Vod; - - default: - return null; - } - } - - public bool Equals(PlaylistType other) - { - if (other.ToString() == ToString()) - { - return true; - } - - return false; - } - } + using System; + using M3U8Parser.Interfaces; + + public class PlaylistType : ICustomAttribute, IEquatable + { + public PlaylistType() + { + } + + private PlaylistType(string value) + { + _value = value; + } + + private string _value { get; } + + public static PlaylistType Event => new ("EVENT"); + + public static PlaylistType Vod => new ("VOD"); + + public object ParseFromString(string value) + { + switch (value) + { + case "EVENT": + return Event; + + case "VOD": + return Vod; + + default: + return null; + } + } + + public bool Equals(PlaylistType other) + { + if (other.ToString() == ToString()) + { + return true; + } + + return false; + } + + public override string ToString() + { + return _value; + } + } } \ No newline at end of file diff --git a/src/M3U8Parser/Attributes/BaseAttribute/ResolutionType.cs b/src/M3U8Parser/Attributes/BaseAttribute/ResolutionType.cs index 742a22f..feb87b2 100644 --- a/src/M3U8Parser/Attributes/BaseAttribute/ResolutionType.cs +++ b/src/M3U8Parser/Attributes/BaseAttribute/ResolutionType.cs @@ -1,16 +1,14 @@ -// Copyright (c) PlaceholderCompany. All rights reserved. - -using System; -using M3U8Parser.Interfaces; - -namespace M3U8Parser.Attributes.BaseAttribute +namespace M3U8Parser.Attributes.BaseAttribute { + using System; + using M3U8Parser.Interfaces; + public class ResolutionType : ICustomAttribute, IEquatable { public long Width { get; set; } public long Height { get; set; } - + public object ParseFromString(string value) { var widthAndHeight = value.Split('x'); @@ -20,19 +18,16 @@ public object ParseFromString(string value) return this; } - public override string ToString() - { - return $"{Width}x{Height}"; - } - public bool Equals(ResolutionType other) { - if (other!.Height == Height && other!.Width == Width) - { - return true; - } + if (other!.Height == Height && other!.Width == Width) return true; return false; } + + public override string ToString() + { + return $"{Width}x{Height}"; + } } } \ No newline at end of file diff --git a/src/M3U8Parser/Attributes/BaseAttribute/StringAttribute.cs b/src/M3U8Parser/Attributes/BaseAttribute/StringAttribute.cs index df4dff5..a155f97 100644 --- a/src/M3U8Parser/Attributes/BaseAttribute/StringAttribute.cs +++ b/src/M3U8Parser/Attributes/BaseAttribute/StringAttribute.cs @@ -1,33 +1,36 @@ -using System; -using System.Text.RegularExpressions; - namespace M3U8Parser.Attributes.BaseAttribute { - public class StringAttribute : CustomAttribute - { - public StringAttribute(string attributeName) : base(attributeName) - { - } - - public override string ToString() - { - if (Value == null) return string.Empty; + using System; + using System.Text.RegularExpressions; - Value = Regex.Replace(Value, "(? + { + public StringAttribute(string attributeName) + : base(attributeName) + { + } - } + public override string ToString() + { + if (Value == null) + { + return string.Empty; + } + + Value = Regex.Replace(Value, "(? - { - public VideoRangeType() - { - } + using System; + using M3U8Parser.Interfaces; + + public class VideoRangeType : ICustomAttribute, IEquatable + { + public VideoRangeType() + { + } + + private VideoRangeType(string value) + { + _value = value; + } + + private string _value { get; } + + public static VideoRangeType PQ => new ("PQ"); + + public static VideoRangeType HLG => new ("HLG"); + + public static VideoRangeType SDR => new ("SDR"); + + public object ParseFromString(string value) + { + switch (value) + { + case "PQ": + return PQ; - private VideoRangeType(string value) - { - _value = value; - } + case "HLG": + return HLG; - private string _value { get; } + case "SDR": + return SDR; - public static VideoRangeType PQ => new ("PQ"); + default: + return null; + } + } - public static VideoRangeType HLG => new ("HLG"); + public bool Equals(VideoRangeType other) + { + if (other!.ToString() == ToString()) + { + return true; + } - public static VideoRangeType SDR => new ("SDR"); + return false; + } public override string ToString() - { - return _value; - } - - public object ParseFromString(string value) - { - switch (value) - { - case "PQ": - return PQ; - - case "HLG": - return HLG; - - case "SDR": - return SDR; - - default: - return null; - } - } - - public bool Equals(VideoRangeType other) - { - if (other!.ToString() == ToString()) - { - return true; - } - - return false; - } - } + { + return _value; + } + } } \ No newline at end of file diff --git a/src/M3U8Parser/Attributes/Characteristics.cs b/src/M3U8Parser/Attributes/Characteristics.cs index 3774ff9..243e348 100644 --- a/src/M3U8Parser/Attributes/Characteristics.cs +++ b/src/M3U8Parser/Attributes/Characteristics.cs @@ -1,10 +1,11 @@ -using M3U8Parser.Attributes.BaseAttribute; - namespace M3U8Parser.Attributes { + using M3U8Parser.Attributes.BaseAttribute; + public class Characteristics : StringAttribute { - public Characteristics() : base("CHARACTERISTICS") + public Characteristics() + : base("CHARACTERISTICS") { } } diff --git a/src/M3U8Parser/Attributes/ClosedCaptions.cs b/src/M3U8Parser/Attributes/ClosedCaptions.cs index 9b4d6af..962ed57 100644 --- a/src/M3U8Parser/Attributes/ClosedCaptions.cs +++ b/src/M3U8Parser/Attributes/ClosedCaptions.cs @@ -1,10 +1,11 @@ -using M3U8Parser.Attributes.BaseAttribute; - namespace M3U8Parser.Attributes { + using M3U8Parser.Attributes.BaseAttribute; + public class ClosedCaptions : StringAttribute { - public ClosedCaptions() : base("CLOSED-CAPTIONS") + public ClosedCaptions() + : base("CLOSED-CAPTIONS") { } } diff --git a/src/M3U8Parser/Attributes/Codecs.cs b/src/M3U8Parser/Attributes/Codecs.cs index de9aeb2..e8f3bd0 100644 --- a/src/M3U8Parser/Attributes/Codecs.cs +++ b/src/M3U8Parser/Attributes/Codecs.cs @@ -1,10 +1,11 @@ -using M3U8Parser.Attributes.BaseAttribute; - namespace M3U8Parser.Attributes { + using M3U8Parser.Attributes.BaseAttribute; + public class Codecs : StringAttribute { - public Codecs() : base("CODECS") + public Codecs() + : base("CODECS") { } } diff --git a/src/M3U8Parser/Attributes/Default.cs b/src/M3U8Parser/Attributes/Default.cs index 67f489c..8ca50de 100644 --- a/src/M3U8Parser/Attributes/Default.cs +++ b/src/M3U8Parser/Attributes/Default.cs @@ -1,10 +1,11 @@ -using M3U8Parser.Attributes.BaseAttribute; - namespace M3U8Parser.Attributes { + using M3U8Parser.Attributes.BaseAttribute; + public class Default : BoolAttribute { - public Default() : base("DEFAULT") + public Default() + : base("DEFAULT") { } } diff --git a/src/M3U8Parser/Attributes/FrameRate.cs b/src/M3U8Parser/Attributes/FrameRate.cs index 1f2c04f..4d2d96b 100644 --- a/src/M3U8Parser/Attributes/FrameRate.cs +++ b/src/M3U8Parser/Attributes/FrameRate.cs @@ -1,10 +1,11 @@ -using M3U8Parser.Attributes.BaseAttribute; - namespace M3U8Parser.Attributes { + using M3U8Parser.Attributes.BaseAttribute; + public class FrameRate : DecimalAttribute { - public FrameRate() : base("FRAME-RATE") + public FrameRate() + : base("FRAME-RATE") { } } diff --git a/src/M3U8Parser/Attributes/GroupId.cs b/src/M3U8Parser/Attributes/GroupId.cs index 1b073b7..61995a8 100644 --- a/src/M3U8Parser/Attributes/GroupId.cs +++ b/src/M3U8Parser/Attributes/GroupId.cs @@ -1,10 +1,11 @@ -using M3U8Parser.Attributes.BaseAttribute; - namespace M3U8Parser.Attributes { + using M3U8Parser.Attributes.BaseAttribute; + public class GroupId : StringAttribute { - public GroupId() : base("GROUP-ID") + public GroupId() + : base("GROUP-ID") { } } diff --git a/src/M3U8Parser/Attributes/HdcpLevel.cs b/src/M3U8Parser/Attributes/HdcpLevel.cs index 118c385..ac5e664 100644 --- a/src/M3U8Parser/Attributes/HdcpLevel.cs +++ b/src/M3U8Parser/Attributes/HdcpLevel.cs @@ -1,10 +1,11 @@ -using M3U8Parser.Attributes.BaseAttribute; - namespace M3U8Parser.Attributes { + using M3U8Parser.Attributes.BaseAttribute; + public class HdcpLevel : CustomAttribute { - public HdcpLevel() : base("HDCP-LEVEL") + public HdcpLevel() + : base("HDCP-LEVEL") { } } diff --git a/src/M3U8Parser/Attributes/InstreamId.cs b/src/M3U8Parser/Attributes/InstreamId.cs index 817b291..d9dd49c 100644 --- a/src/M3U8Parser/Attributes/InstreamId.cs +++ b/src/M3U8Parser/Attributes/InstreamId.cs @@ -1,10 +1,11 @@ -using M3U8Parser.Attributes.BaseAttribute; - namespace M3U8Parser.Attributes { + using M3U8Parser.Attributes.BaseAttribute; + public class InstreamId : StringAttribute { - public InstreamId() : base("INSTREAM-ID") + public InstreamId() + : base("INSTREAM-ID") { } } diff --git a/src/M3U8Parser/Attributes/Language.cs b/src/M3U8Parser/Attributes/Language.cs index e59fca5..9d154ac 100644 --- a/src/M3U8Parser/Attributes/Language.cs +++ b/src/M3U8Parser/Attributes/Language.cs @@ -1,10 +1,11 @@ -using M3U8Parser.Attributes.BaseAttribute; - namespace M3U8Parser.Attributes { + using M3U8Parser.Attributes.BaseAttribute; + public class Language : StringAttribute { - public Language() : base("LANGUAGE") + public Language() + : base("LANGUAGE") { } } diff --git a/src/M3U8Parser/Attributes/Method.cs b/src/M3U8Parser/Attributes/Method.cs index 00f8b40..e027878 100644 --- a/src/M3U8Parser/Attributes/Method.cs +++ b/src/M3U8Parser/Attributes/Method.cs @@ -1,10 +1,11 @@ -using M3U8Parser.Attributes.BaseAttribute; - namespace M3U8Parser.Attributes { + using M3U8Parser.Attributes.BaseAttribute; + public class Method : CustomAttribute { - public Method() : base("METHOD") + public Method() + : base("METHOD") { } } diff --git a/src/M3U8Parser/Attributes/Name.cs b/src/M3U8Parser/Attributes/Name.cs index 04801b0..2679527 100644 --- a/src/M3U8Parser/Attributes/Name.cs +++ b/src/M3U8Parser/Attributes/Name.cs @@ -1,10 +1,11 @@ -using M3U8Parser.Attributes.BaseAttribute; - namespace M3U8Parser.Attributes { + using M3U8Parser.Attributes.BaseAttribute; + public class Name : StringAttribute { - public Name() : base("NAME") + public Name() + : base("NAME") { } } diff --git a/src/M3U8Parser/Attributes/Resolution.cs b/src/M3U8Parser/Attributes/Resolution.cs index 245e51d..351acda 100644 --- a/src/M3U8Parser/Attributes/Resolution.cs +++ b/src/M3U8Parser/Attributes/Resolution.cs @@ -1,10 +1,11 @@ -using M3U8Parser.Attributes.BaseAttribute; - namespace M3U8Parser.Attributes { + using M3U8Parser.Attributes.BaseAttribute; + public class Resolution : CustomAttribute { - public Resolution() : base("RESOLUTION") + public Resolution() + : base("RESOLUTION") { } } diff --git a/src/M3U8Parser/Attributes/Subtitles.cs b/src/M3U8Parser/Attributes/Subtitles.cs index b70a98d..1589b68 100644 --- a/src/M3U8Parser/Attributes/Subtitles.cs +++ b/src/M3U8Parser/Attributes/Subtitles.cs @@ -1,10 +1,11 @@ -using M3U8Parser.Attributes.BaseAttribute; - namespace M3U8Parser.Attributes { + using M3U8Parser.Attributes.BaseAttribute; + public class Subtitles : StringAttribute { - public Subtitles() : base("SUBTITLES") + public Subtitles() + : base("SUBTITLES") { } } diff --git a/src/M3U8Parser/Attributes/Type.cs b/src/M3U8Parser/Attributes/Type.cs index fcda219..5f82a06 100644 --- a/src/M3U8Parser/Attributes/Type.cs +++ b/src/M3U8Parser/Attributes/Type.cs @@ -1,10 +1,11 @@ -using M3U8Parser.Attributes.BaseAttribute; - namespace M3U8Parser.Attributes { + using M3U8Parser.Attributes.BaseAttribute; + public class Type : CustomAttribute { - public Type() : base("TYPE") + public Type() + : base("TYPE") { } } diff --git a/src/M3U8Parser/Attributes/Uri.cs b/src/M3U8Parser/Attributes/Uri.cs index a7775d9..93f12fe 100644 --- a/src/M3U8Parser/Attributes/Uri.cs +++ b/src/M3U8Parser/Attributes/Uri.cs @@ -1,10 +1,11 @@ -using M3U8Parser.Attributes.BaseAttribute; - namespace M3U8Parser.Attributes { + using M3U8Parser.Attributes.BaseAttribute; + public class Uri : StringAttribute { - public Uri() : base("URI") + public Uri() + : base("URI") { } } diff --git a/src/M3U8Parser/Attributes/Video.cs b/src/M3U8Parser/Attributes/Video.cs index 1e64b4f..1162413 100644 --- a/src/M3U8Parser/Attributes/Video.cs +++ b/src/M3U8Parser/Attributes/Video.cs @@ -1,10 +1,11 @@ -using M3U8Parser.Attributes.BaseAttribute; - namespace M3U8Parser.Attributes { + using M3U8Parser.Attributes.BaseAttribute; + public class Video : StringAttribute { - public Video() : base("VIDEO") + public Video() + : base("VIDEO") { } } diff --git a/src/M3U8Parser/Attributes/VideoRange.cs b/src/M3U8Parser/Attributes/VideoRange.cs index 47c3cc3..dacf46f 100644 --- a/src/M3U8Parser/Attributes/VideoRange.cs +++ b/src/M3U8Parser/Attributes/VideoRange.cs @@ -1,10 +1,11 @@ -using M3U8Parser.Attributes.BaseAttribute; - namespace M3U8Parser.Attributes { + using M3U8Parser.Attributes.BaseAttribute; + public class VideoRange : CustomAttribute { - public VideoRange() : base("VIDEO-RANGE") + public VideoRange() + : base("VIDEO-RANGE") { } } diff --git a/src/M3U8Parser/CodeAnalysis.ruleset b/src/M3U8Parser/CodeAnalysis.ruleset new file mode 100644 index 0000000..8c565d1 --- /dev/null +++ b/src/M3U8Parser/CodeAnalysis.ruleset @@ -0,0 +1,13 @@ + + + + + + + + + + + + + diff --git a/src/M3U8Parser/ExtXType/BaseExtX.cs b/src/M3U8Parser/ExtXType/BaseExtX.cs index 23e425a..bd6adec 100644 --- a/src/M3U8Parser/ExtXType/BaseExtX.cs +++ b/src/M3U8Parser/ExtXType/BaseExtX.cs @@ -1,21 +1,23 @@ -using M3U8Parser.Extensions; -using M3U8Parser.Interfaces; -using System.Reflection; -using System.Text; - -namespace M3U8Parser.ExtXType +namespace M3U8Parser.ExtXType { + using System.Reflection; + using System.Text; + using M3U8Parser.Extensions; + using M3U8Parser.Interfaces; + public abstract class BaseExtX : IExtXType { - protected virtual string ExtPrefix => ""; - - public BaseExtX() { } + public BaseExtX() + { + } public BaseExtX(string str) { ReadAllAttributes(str); } + protected virtual string ExtPrefix => ""; + public override string ToString() { var strBuilder = new StringBuilder(); @@ -29,13 +31,13 @@ public override string ToString() protected void ReadAllAttributes(string str) { - foreach (var field in this.GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Instance)) + foreach (var field in GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Instance)) { var isAnAttribute = typeof(IAttribute).IsAssignableFrom(field.FieldType); if (isAnAttribute) { - MethodInfo m = field.FieldType.GetMethod("Read"); + var m = field.FieldType.GetMethod("Read"); m.Invoke(field.GetValue(this), new object[] { str }); } } @@ -43,17 +45,17 @@ protected void ReadAllAttributes(string str) protected void WriteAllAttributes(StringBuilder strBuilder) { - foreach (var field in this.GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Instance)) + foreach (var field in GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Instance)) { var isAnAttribute = typeof(IAttribute).IsAssignableFrom(field.FieldType); if (isAnAttribute) { - MethodInfo m = field.FieldType.GetMethod("ToString"); + var m = field.FieldType.GetMethod("ToString"); var str = m.Invoke(field.GetValue(this), new object[] { }); strBuilder.AppendWithSeparator(str.ToString(), ","); } } } } -} +} \ No newline at end of file diff --git a/src/M3U8Parser/ExtXType/HlsVersion.cs b/src/M3U8Parser/ExtXType/HlsVersion.cs index 0e1b009..508bf33 100644 --- a/src/M3U8Parser/ExtXType/HlsVersion.cs +++ b/src/M3U8Parser/ExtXType/HlsVersion.cs @@ -1,16 +1,12 @@ -using System; -using System.Text.RegularExpressions; -using M3U8Parser.Interfaces; - -namespace M3U8Parser.ExtXType +namespace M3U8Parser.ExtXType { + using System; + using System.Text.RegularExpressions; + using M3U8Parser.Interfaces; + public class HlsVersion : BaseExtX { - public static string Prefix = "#EXT-X-VERSION"; - - protected override string ExtPrefix => HlsVersion.Prefix; - - public int Value { get; set; } + public const string Prefix = "#EXT-X-VERSION"; public HlsVersion() { @@ -21,6 +17,10 @@ public HlsVersion(string str) Read(str); } + public int Value { get; set; } + + protected override string ExtPrefix => Prefix; + public void Read(string content) { var match = Regex.Match(content.Trim(), $"(?<={Prefix}:)(.*?)(?=$)", @@ -33,9 +33,7 @@ public void Read(string content) var valueFounded = match.Groups[0].Value; if (type.IsGenericType && type.GetGenericTypeDefinition().Equals(typeof(Nullable<>))) - { type = Nullable.GetUnderlyingType(type); - } if (typeof(ICustomAttribute).IsAssignableFrom(type)) { @@ -49,7 +47,7 @@ public void Read(string content) } else { - Value = default(int); + Value = default; } } } diff --git a/src/M3U8Parser/ExtXType/IframeStreamInf.cs b/src/M3U8Parser/ExtXType/IframeStreamInf.cs index b8818d1..faca8ac 100644 --- a/src/M3U8Parser/ExtXType/IframeStreamInf.cs +++ b/src/M3U8Parser/ExtXType/IframeStreamInf.cs @@ -1,88 +1,98 @@ -using M3U8Parser.Attributes.BaseAttribute; - namespace M3U8Parser.ExtXType { using M3U8Parser.Attributes; + using M3U8Parser.Attributes.BaseAttribute; public class IframeStreamInf : BaseExtX { - private readonly Bandwidth _bandwidth = new (); - private readonly AverageBandwidth _averageBandwidth = new (); - private readonly Codecs _codecs = new (); - private readonly FrameRate _frameRate = new (); - private readonly VideoRange _videoRange = new (); - private readonly HdcpLevel _hdcpLevel = new (); - private readonly Audio _audio = new (); - private readonly Video _video = new (); - private readonly Subtitles _subtitles = new (); - private readonly ClosedCaptions _closedCaptions = new (); - private readonly Resolution _resolutionAttribute = new (); - private readonly Uri _uri = new (); + public const string Prefix = "#EXT-X-I-FRAME-STREAM-INF"; + private readonly Audio _audio = new (); + private readonly AverageBandwidth _averageBandwidth = new (); + private readonly Bandwidth _bandwidth = new (); + private readonly ClosedCaptions _closedCaptions = new (); + private readonly Codecs _codecs = new (); + private readonly FrameRate _frameRate = new (); + private readonly HdcpLevel _hdcpLevel = new (); + private readonly Resolution _resolutionAttribute = new (); + private readonly Subtitles _subtitles = new (); + private readonly Uri _uri = new (); + private readonly Video _video = new (); + private readonly VideoRange _videoRange = new (); + + public IframeStreamInf() + { + } + + public IframeStreamInf(string str) : base(str) + { + } + + public long Bandwidth + { + get => _bandwidth.Value; + set => _bandwidth.Value = value; + } + + public long? AverageBandwidth + { + get => _averageBandwidth.Value; + set => _averageBandwidth.Value = value; + } - public IframeStreamInf() - { - } + public string Codecs + { + get => _codecs.Value; + set => _codecs.Value = value; + } - public IframeStreamInf(string str) : base(str) { } + public decimal? FrameRate + { + get => _frameRate.Value; + set => _frameRate.Value = value; + } - public long Bandwidth { - get => _bandwidth.Value; - set => _bandwidth.Value = value; - } - - public long? AverageBandwidth { - get => _averageBandwidth.Value; - set => _averageBandwidth.Value = value; - } + public VideoRangeType VideoRange + { + get => _videoRange.Value; + set => _videoRange.Value = value; + } - public string Codecs { - get => _codecs.Value; - set => _codecs.Value = value; - } - - public decimal? FrameRate { - get => _frameRate.Value; - set => _frameRate.Value = value; - } - - public VideoRangeType VideoRange { - get => _videoRange.Value; - set => _videoRange.Value = value; - } - - public HdcpLevelType HdcpLevel { - get => _hdcpLevel.Value; - set => _hdcpLevel.Value = value; - } - public string Audio { - get => _audio.Value; - set => _audio.Value = value; - } + public HdcpLevelType HdcpLevel + { + get => _hdcpLevel.Value; + set => _hdcpLevel.Value = value; + } - public string Video { - get => _video.Value; - set => _video.Value = value; - } + public string Audio + { + get => _audio.Value; + set => _audio.Value = value; + } - - public string Subtitles { - get => _subtitles.Value; - set => _subtitles.Value = value; - } - - public string ClosedCaptions { - get => _closedCaptions.Value; - set => _closedCaptions.Value = value; - } + public string Video + { + get => _video.Value; + set => _video.Value = value; + } - public ResolutionType Resolution { - get => _resolutionAttribute.Value; - set => _resolutionAttribute.Value = value; - } + public string Subtitles + { + get => _subtitles.Value; + set => _subtitles.Value = value; + } + public string ClosedCaptions + { + get => _closedCaptions.Value; + set => _closedCaptions.Value = value; + } - public static string Prefix = "#EXT-X-I-FRAME-STREAM-INF"; + public ResolutionType Resolution + { + get => _resolutionAttribute.Value; + set => _resolutionAttribute.Value = value; + } protected override string ExtPrefix => Prefix; - } + } } \ No newline at end of file diff --git a/src/M3U8Parser/ExtXType/Key.cs b/src/M3U8Parser/ExtXType/Key.cs index 9b74f5f..6e9466e 100644 --- a/src/M3U8Parser/ExtXType/Key.cs +++ b/src/M3U8Parser/ExtXType/Key.cs @@ -1,33 +1,33 @@ -using M3U8Parser.Attributes.BaseAttribute; - namespace M3U8Parser.ExtXType { using M3U8Parser.Attributes; + using M3U8Parser.Attributes.BaseAttribute; public class Key : BaseExtX { - private readonly Method _method = new (); - private readonly Uri _uri = new (); - - public Key() - { - } + public const string Prefix = "#EXT-X-KEY"; + private readonly Method _method = new (); + private readonly Uri _uri = new (); - public Key(string str) : base(str) - { + public Key() + { } - public string Uri { - get => _uri.Value; - set => _uri.Value = value; - } + public Key(string str) : base(str) + { + } - public MethodType Method { - get => _method.Value; - set => _method.Value = value; - } + public string Uri + { + get => _uri.Value; + set => _uri.Value = value; + } - public static string Prefix = "#EXT-X-KEY"; + public MethodType Method + { + get => _method.Value; + set => _method.Value = value; + } protected override string ExtPrefix => Prefix; } diff --git a/src/M3U8Parser/ExtXType/Map.cs b/src/M3U8Parser/ExtXType/Map.cs index 21fe7bc..0aa40e8 100644 --- a/src/M3U8Parser/ExtXType/Map.cs +++ b/src/M3U8Parser/ExtXType/Map.cs @@ -1,16 +1,17 @@ -using M3U8Parser.Attributes; - namespace M3U8Parser.ExtXType { + using M3U8Parser.Attributes; + public class Map : BaseExtX { - private readonly Uri _uri = new(); + private readonly Uri _uri = new (); public Map() { } - - public Map(string str): base (str) + + public Map(string str) + : base (str) { } @@ -19,7 +20,7 @@ public string Uri { set => _uri.Value = value; } - public static string Prefix = "#EXT-X-MAP"; + public const string Prefix = "#EXT-X-MAP"; protected override string ExtPrefix => Map.Prefix; } diff --git a/src/M3U8Parser/ExtXType/Media.cs b/src/M3U8Parser/ExtXType/Media.cs index 16ed722..120d17f 100644 --- a/src/M3U8Parser/ExtXType/Media.cs +++ b/src/M3U8Parser/ExtXType/Media.cs @@ -1,76 +1,84 @@ -using M3U8Parser.Attributes.BaseAttribute; - namespace M3U8Parser.ExtXType { using M3U8Parser.Attributes; + using M3U8Parser.Attributes.BaseAttribute; public class Media : BaseExtX { - private readonly Autoselect _autoSelect = new (); - private readonly Characteristics _characteristics = new (); - private readonly Default _default = new (); - private readonly GroupId _groupId = new (); - private readonly InstreamId _instreamId = new (); - private readonly Language _language = new (); - private readonly Type _mediaType = new (); - private readonly Name _name = new (); - private readonly Uri _uri = new (); - - public Media() - { - } + public const string Prefix = "#EXT-X-MEDIA"; + private readonly Autoselect _autoSelect = new (); + private readonly Characteristics _characteristics = new (); + private readonly Default _default = new (); + private readonly GroupId _groupId = new (); + private readonly InstreamId _instreamId = new (); + private readonly Language _language = new (); + private readonly Type _mediaType = new (); + private readonly Name _name = new (); + private readonly Uri _uri = new (); - public Media(string str) : base(str) - { + public Media() + { } - public string Uri { - get => _uri.Value; - set => _uri.Value = value; - } + public Media(string str) + : base(str) + { + } - public string Language { - get => _language.Value; - set => _language.Value = value; - } + public string Uri + { + get => _uri.Value; + set => _uri.Value = value; + } - public string Name { - get => _name.Value; - set => _name.Value = value; - } + public string Language + { + get => _language.Value; + set => _language.Value = value; + } - public bool AutoSelect { - get => _autoSelect.Value; - set => _autoSelect.Value = value; - } + public string Name + { + get => _name.Value; + set => _name.Value = value; + } - public bool Default { - get => _default.Value; - set => _default.Value = value; - } + public bool AutoSelect + { + get => _autoSelect.Value; + set => _autoSelect.Value = value; + } - public string GroupId { - get => _groupId.Value; - set => _groupId.Value = value; - } + public bool Default + { + get => _default.Value; + set => _default.Value = value; + } - public string InstreamId { - get => _instreamId.Value; - set => _instreamId.Value = value; - } + public string GroupId + { + get => _groupId.Value; + set => _groupId.Value = value; + } - public MediaType Type { - get => _mediaType.Value; - set => _mediaType.Value = value; - } + public string InstreamId + { + get => _instreamId.Value; + set => _instreamId.Value = value; + } - public string Characteristics { - get => _characteristics.Value; - set => _characteristics.Value = value; - } + public MediaType Type + { + get => _mediaType.Value; + set => _mediaType.Value = value; + } - public static string Prefix = "#EXT-X-MEDIA"; + public string Characteristics + { + get => _characteristics.Value; + set => _characteristics.Value = value; + } - protected override string ExtPrefix => Media.Prefix; + protected override string ExtPrefix => Prefix; } } \ No newline at end of file diff --git a/src/M3U8Parser/ExtXType/MediaSegment.cs b/src/M3U8Parser/ExtXType/MediaSegment.cs index 7d41dba..e970f2a 100644 --- a/src/M3U8Parser/ExtXType/MediaSegment.cs +++ b/src/M3U8Parser/ExtXType/MediaSegment.cs @@ -1,57 +1,57 @@ -using System.Collections.Generic; -using System.IO; -using System.Text; -using M3U8Parser.Attributes.BaseAttribute; -using M3U8Parser.Interfaces; - -namespace M3U8Parser.ExtXType +namespace M3U8Parser.ExtXType { - public class MediaSegment : IExtXType - { - public List Segments { get; set; } = new List(); - - public Key Key { get; set; } - - - public MediaSegment() - { - } - - public MediaSegment(string str) - { - using var reader = new StringReader(str); - var line = ""; - while ((line = reader.ReadLine()) != null) - { - if (line.StartsWith(Key.Prefix)) - { - Key = new Key(line); - } - else if (line.StartsWith(Segment.Prefix)) - { - var segment = new Segment(line); - Segments.Add(segment); - } - } - } - - public override string ToString() - { - var strBuilder = new StringBuilder(); - - if(Key != null && Key.Method != MethodType.None) - { + using System.Collections.Generic; + using System.IO; + using System.Text; + using M3U8Parser.Attributes.BaseAttribute; + using M3U8Parser.Interfaces; + + public class MediaSegment : IExtXType + { + public MediaSegment() + { + } + + public MediaSegment(string str) + { + using var reader = new StringReader(str); + string line; + while ((line = reader.ReadLine()) != null) + { + if (line.StartsWith(Key.Prefix)) + { + Key = new Key(line); + } + else if (line.StartsWith(Segment.Prefix)) + { + var segment = new Segment(line); + Segments.Add(segment); + } + } + } + + public List Segments { get; set; } = new (); + + public Key Key { get; set; } + + public override string ToString() + { + var strBuilder = new StringBuilder(); + + if (Key != null && Key.Method != MethodType.None) + { strBuilder.AppendLine(Key.ToString()); } - if(Segments != null && Segments.Count > 0) - { - foreach (var segment in Segments) - { - strBuilder.Append(segment.ToString()); + + if (Segments != null && Segments.Count > 0) + { + foreach (var segment in Segments) + { + strBuilder.Append(segment); } - } + } - return strBuilder.ToString(); - } - } + return strBuilder.ToString(); + } + } } \ No newline at end of file diff --git a/src/M3U8Parser/ExtXType/MediaSequence.cs b/src/M3U8Parser/ExtXType/MediaSequence.cs index 31e176e..45cd52e 100644 --- a/src/M3U8Parser/ExtXType/MediaSequence.cs +++ b/src/M3U8Parser/ExtXType/MediaSequence.cs @@ -1,16 +1,12 @@ -using System; -using System.Text.RegularExpressions; -using M3U8Parser.Interfaces; - -namespace M3U8Parser.ExtXType +namespace M3U8Parser.ExtXType { + using System; + using System.Text.RegularExpressions; + using M3U8Parser.Interfaces; + public class MediaSequence : BaseExtX { - public static string Prefix = "#EXT-X-MEDIA-SEQUENCE"; - - protected override string ExtPrefix => HlsVersion.Prefix; - - public int Value { get; set; } + public const string Prefix = "#EXT-X-MEDIA-SEQUENCE"; public MediaSequence() { @@ -21,6 +17,10 @@ public MediaSequence(string str) Read(str); } + protected override string ExtPrefix => HlsVersion.Prefix; + + public int Value { get; set; } + public void Read(string content) { var match = Regex.Match(content.Trim(), $"(?<={Prefix}:)(.*?)(?=$)", @@ -49,7 +49,7 @@ public void Read(string content) } else { - Value = default(int); + Value = default; } } } diff --git a/src/M3U8Parser/ExtXType/PlaylistTypeExt.cs b/src/M3U8Parser/ExtXType/PlaylistTypeExt.cs index 2876bdb..39bdea6 100644 --- a/src/M3U8Parser/ExtXType/PlaylistTypeExt.cs +++ b/src/M3U8Parser/ExtXType/PlaylistTypeExt.cs @@ -1,58 +1,56 @@ -using M3U8Parser.Attributes.BaseAttribute; -using M3U8Parser.Interfaces; - -namespace M3U8Parser.ExtXType +namespace M3U8Parser.ExtXType { - using System; - using System.Text.RegularExpressions; - - public class PlaylistTypeExt : BaseExtX - { - public static string Prefix = "#EXT-X-PLAYLIST-TYPE"; - - protected override string ExtPrefix => PlaylistTypeExt.Prefix; - - public PlaylistType Value { get; set; } - - public PlaylistTypeExt() - { - } - - public PlaylistTypeExt(string str) - { - Read(str); - } - - public void Read(string content) - { - var match = Regex.Match(content.Trim(), $"(?<={Prefix}:)(.*?)(?=$)", - RegexOptions.Multiline & RegexOptions.IgnoreCase); - - var type = typeof(PlaylistType); - - if (match.Success) - { - var valueFounded = match.Groups[0].Value; - - if (type.IsGenericType && type.GetGenericTypeDefinition().Equals(typeof(Nullable<>))) - { - type = Nullable.GetUnderlyingType(type); - } - - if (typeof(ICustomAttribute).IsAssignableFrom(type)) - { - var instanceAttribute = (ICustomAttribute)Activator.CreateInstance(type, false); - Value = (PlaylistType)instanceAttribute.ParseFromString(valueFounded); - } - else - { - Value = (PlaylistType)Convert.ChangeType(valueFounded, type); - } - } - else - { - Value = default(PlaylistType); - } - } - } + using System; + using System.Text.RegularExpressions; + using M3U8Parser.Attributes.BaseAttribute; + using M3U8Parser.Interfaces; + + public class PlaylistTypeExt : BaseExtX + { + public const string Prefix = "#EXT-X-PLAYLIST-TYPE"; + + public PlaylistTypeExt() + { + } + + public PlaylistTypeExt(string str) + { + Read(str); + } + + protected override string ExtPrefix => Prefix; + + public PlaylistType Value { get; set; } + + public void Read(string content) + { + var match = Regex.Match(content.Trim(), $"(?<={Prefix}:)(.*?)(?=$)", RegexOptions.Multiline & RegexOptions.IgnoreCase); + + var type = typeof(PlaylistType); + + if (match.Success) + { + var valueFounded = match.Groups[0].Value; + + if (type.IsGenericType && type.GetGenericTypeDefinition().Equals(typeof(Nullable<>))) + { + type = Nullable.GetUnderlyingType(type); + } + + if (typeof(ICustomAttribute).IsAssignableFrom(type)) + { + var instanceAttribute = (ICustomAttribute)Activator.CreateInstance(type, false); + Value = (PlaylistType)instanceAttribute.ParseFromString(valueFounded); + } + else + { + Value = (PlaylistType)Convert.ChangeType(valueFounded, type); + } + } + else + { + Value = default; + } + } + } } \ No newline at end of file diff --git a/src/M3U8Parser/ExtXType/Segment.cs b/src/M3U8Parser/ExtXType/Segment.cs index 039b4d1..7d01d0b 100644 --- a/src/M3U8Parser/ExtXType/Segment.cs +++ b/src/M3U8Parser/ExtXType/Segment.cs @@ -1,23 +1,13 @@ -using System.Globalization; -using System.IO; -using System.Text; -using System.Text.RegularExpressions; -using M3U8Parser.Interfaces; - -namespace M3U8Parser.ExtXType +namespace M3U8Parser.ExtXType { + using System.Globalization; + using System.IO; + using System.Text; + using System.Text.RegularExpressions; + using M3U8Parser.Interfaces; + public class Segment : IExtXType { - public double Duration { get; set; } - - public string Title { get; set; } - - public string Uri { get; set; } - - public long? ByteRangeLentgh { get; set; } - - public long? ByteRangeStartSubRange { get; set; } - public Segment() { } @@ -30,8 +20,7 @@ public Segment(string str) { if (line.StartsWith("#EXTINF")) { - var match = Regex.Match(line.Trim(), $"(?<=#EXTINF:)(.*?)(?=$)", - RegexOptions.Multiline & RegexOptions.IgnoreCase); + var match = Regex.Match(line.Trim(), "(?<=#EXTINF:)(.*?)(?=$)", RegexOptions.Multiline & RegexOptions.IgnoreCase); if (match.Success) { @@ -41,7 +30,7 @@ public Segment(string str) } else if (line.StartsWith("#EXT-X-BYTERANGE")) { - var match = Regex.Match(line.Trim(), $"(?<=#EXT-X-BYTERANGE:)(.*?)(?=$)", + var match = Regex.Match(line.Trim(), "(?<=#EXT-X-BYTERANGE:)(.*?)(?=$)", RegexOptions.Multiline & RegexOptions.IgnoreCase); if (match.Success) @@ -49,10 +38,7 @@ public Segment(string str) var byterange = match.Groups[0].Value.Split('@'); ByteRangeLentgh = long.Parse(byterange[0]); - if (byterange.Length > 1) - { - ByteRangeStartSubRange = long.Parse(byterange[1]); - } + if (byterange.Length > 1) ByteRangeStartSubRange = long.Parse(byterange[1]); } } else if (!line.StartsWith("#EXT")) @@ -62,7 +48,17 @@ public Segment(string str) } } - public static string Prefix => "#EXTINF"; + public double Duration { get; set; } + + public string Title { get; set; } + + public string Uri { get; set; } + + public long? ByteRangeLentgh { get; set; } + + public long? ByteRangeStartSubRange { get; set; } + + public const string Prefix = "#EXTINF"; public override string ToString() { @@ -73,10 +69,7 @@ public override string ToString() { strBuilder.Append($"#EXT-X-BYTERANGE:{ByteRangeLentgh}"); - if (ByteRangeStartSubRange != null) - { - strBuilder.Append($"@{ByteRangeStartSubRange}"); - } + if (ByteRangeStartSubRange != null) strBuilder.Append($"@{ByteRangeStartSubRange}"); strBuilder.AppendLine(); } @@ -86,4 +79,4 @@ public override string ToString() return strBuilder.ToString(); } } -} +} \ No newline at end of file diff --git a/src/M3U8Parser/ExtXType/StreamInf.cs b/src/M3U8Parser/ExtXType/StreamInf.cs index b49452f..820d38a 100644 --- a/src/M3U8Parser/ExtXType/StreamInf.cs +++ b/src/M3U8Parser/ExtXType/StreamInf.cs @@ -1,119 +1,129 @@ -using M3U8Parser.Attributes.BaseAttribute; -using M3U8Parser.Interfaces; - namespace M3U8Parser.ExtXType { - using M3U8Parser.Attributes; - using M3U8Parser.Extensions; using System.IO; using System.Text; + using M3U8Parser.Attributes; + using M3U8Parser.Attributes.BaseAttribute; + using M3U8Parser.Extensions; + using M3U8Parser.Interfaces; public class StreamInf : IExtXType - { - private readonly Bandwidth _bandwidth = new (); + { + private readonly Audio _audio = new (); private readonly AverageBandwidth _averageBandwidth = new (); - private readonly Codecs _codecs = new (); + private readonly Bandwidth _bandwidth = new (); + private readonly ClosedCaptions _closedCaptions = new (); + private readonly Codecs _codecs = new (); private readonly FrameRate _frameRate = new (); - private readonly VideoRange _videoRange = new (); private readonly HdcpLevel _hdcpLevel = new (); - private readonly Audio _audio = new (); - private readonly Video _video = new (); + private readonly Resolution _resolutionAttribute = new (); private readonly Subtitles _subtitles = new (); - private readonly ClosedCaptions _closedCaptions = new (); - private readonly Resolution _resolutionAttribute = new (); + private readonly Video _video = new (); + private readonly VideoRange _videoRange = new (); - public StreamInf() - { - } + public StreamInf() + { + } - public StreamInf(string str) - { - string lineWithAttribute; - string lineWithUri; + public StreamInf(string str) + { + string lineWithAttribute; + string lineWithUri; - using var reader = new StringReader(str); - lineWithAttribute = reader.ReadLine(); - lineWithUri = reader.ReadLine(); + using var reader = new StringReader(str); + lineWithAttribute = reader.ReadLine(); + lineWithUri = reader.ReadLine(); - _bandwidth.Read(lineWithAttribute); + _bandwidth.Read(lineWithAttribute); _averageBandwidth.Read(lineWithAttribute); - _codecs.Read(lineWithAttribute); + _codecs.Read(lineWithAttribute); _frameRate.Read(lineWithAttribute); _videoRange.Read(lineWithAttribute); _hdcpLevel.Read(lineWithAttribute); - _audio.Read(lineWithAttribute); + _audio.Read(lineWithAttribute); _video.Read(lineWithAttribute); _subtitles.Read(lineWithAttribute); _closedCaptions.Read(lineWithAttribute); - _resolutionAttribute.Read(lineWithAttribute); - Uri = lineWithUri; - } - - public string Uri { get; set; } - - public long Bandwidth { - get => _bandwidth.Value; - set => _bandwidth.Value = value; - } - - public long? AverageBandwidth { + _resolutionAttribute.Read(lineWithAttribute); + Uri = lineWithUri; + } + + public string Uri { get; set; } + + public long Bandwidth + { + get => _bandwidth.Value; + set => _bandwidth.Value = value; + } + + public long? AverageBandwidth + { get => _averageBandwidth.Value; set => _averageBandwidth.Value = value; } - public string Codecs { - get => _codecs.Value; - set => _codecs.Value = value; - } - - public decimal? FrameRate { + public string Codecs + { + get => _codecs.Value; + set => _codecs.Value = value; + } + + public decimal? FrameRate + { get => _frameRate.Value; set => _frameRate.Value = value; } - - public VideoRangeType VideoRange { + + public VideoRangeType VideoRange + { get => _videoRange.Value; set => _videoRange.Value = value; } - - public HdcpLevelType HdcpLevel { + + public HdcpLevelType HdcpLevel + { get => _hdcpLevel.Value; set => _hdcpLevel.Value = value; } - public string Audio { - get => _audio.Value; - set => _audio.Value = value; - } - public string Video { + public string Audio + { + get => _audio.Value; + set => _audio.Value = value; + } + + public string Video + { get => _video.Value; set => _video.Value = value; } - - public string Subtitles { + public string Subtitles + { get => _subtitles.Value; set => _subtitles.Value = value; } - - public string ClosedCaptions { - get => _closedCaptions.Value; - set => _closedCaptions.Value = value; - } - - public ResolutionType Resolution { - get => _resolutionAttribute.Value; - set => _resolutionAttribute.Value = value; - } - - public static string Prefix => "#EXT-X-STREAM-INF"; - - public override string ToString() - { - var strBuilder = new StringBuilder(); - strBuilder.Append(Prefix); - strBuilder.Append(":"); - strBuilder.AppendWithSeparator(_bandwidth.ToString(), ","); + + public string ClosedCaptions + { + get => _closedCaptions.Value; + set => _closedCaptions.Value = value; + } + + public ResolutionType Resolution + { + get => _resolutionAttribute.Value; + set => _resolutionAttribute.Value = value; + } + + public const string Prefix = "#EXT-X-STREAM-INF"; + + public override string ToString() + { + var strBuilder = new StringBuilder(); + strBuilder.Append(Prefix); + strBuilder.Append(":"); + strBuilder.AppendWithSeparator(_bandwidth.ToString(), ","); strBuilder.AppendWithSeparator(_averageBandwidth.ToString(), ","); strBuilder.AppendWithSeparator(_videoRange.ToString(), ","); strBuilder.AppendWithSeparator(_codecs.ToString(), ","); @@ -122,10 +132,10 @@ public override string ToString() strBuilder.AppendWithSeparator(_closedCaptions.ToString(), ","); strBuilder.AppendWithSeparator(_hdcpLevel.ToString(), ","); strBuilder.AppendWithSeparator(_video.ToString(), ","); - strBuilder.AppendWithSeparator(_audio.ToString(), ","); - strBuilder.AppendWithSeparator(_subtitles.ToString(), ","); + strBuilder.AppendWithSeparator(_audio.ToString(), ","); + strBuilder.AppendWithSeparator(_subtitles.ToString(), ","); - return strBuilder.ToString().RemoveLastCharacter() + "\r\n" + Uri; - } - } + return strBuilder.ToString().RemoveLastCharacter() + "\r\n" + Uri; + } + } } \ No newline at end of file diff --git a/src/M3U8Parser/ExtXType/TargetDuration.cs b/src/M3U8Parser/ExtXType/TargetDuration.cs index 4c5175a..7d5d614 100644 --- a/src/M3U8Parser/ExtXType/TargetDuration.cs +++ b/src/M3U8Parser/ExtXType/TargetDuration.cs @@ -1,16 +1,12 @@ -using System; -using System.Text.RegularExpressions; -using M3U8Parser.Interfaces; - -namespace M3U8Parser.ExtXType +namespace M3U8Parser.ExtXType { + using System; + using System.Text.RegularExpressions; + using M3U8Parser.Interfaces; + public class TargetDuration : BaseExtX { - public static string Prefix = "#EXT-X-TARGETDURATION"; - - protected override string ExtPrefix => TargetDuration.Prefix; - - public int Value { get; set; } + public const string Prefix = "#EXT-X-TARGETDURATION"; public TargetDuration() { @@ -21,6 +17,10 @@ public TargetDuration(string str) Read(str); } + protected override string ExtPrefix => Prefix; + + public int Value { get; set; } + public void Read(string content) { var match = Regex.Match(content.Trim(), $"(?<={Prefix}:)(.*?)(?=$)", @@ -33,9 +33,7 @@ public void Read(string content) var valueFounded = match.Groups[0].Value; if (type.IsGenericType && type.GetGenericTypeDefinition().Equals(typeof(Nullable<>))) - { type = Nullable.GetUnderlyingType(type); - } if (typeof(ICustomAttribute).IsAssignableFrom(type)) { @@ -49,7 +47,7 @@ public void Read(string content) } else { - Value = default(int); + Value = default; } } } diff --git a/src/M3U8Parser/Extensions/StringBuilderExtension.cs b/src/M3U8Parser/Extensions/StringBuilderExtension.cs index 5820607..52f4d66 100644 --- a/src/M3U8Parser/Extensions/StringBuilderExtension.cs +++ b/src/M3U8Parser/Extensions/StringBuilderExtension.cs @@ -3,14 +3,13 @@ namespace M3U8Parser.Extensions using System.Text; public static class StringBuilderExtension - { - public static void AppendWithSeparator(this StringBuilder strBuilder, string text, - string separator) - { - if (!string.IsNullOrEmpty(text)) - { - strBuilder.Append(text + separator); - } + { + public static void AppendWithSeparator(this StringBuilder strBuilder, string text, string separator) + { + if (!string.IsNullOrEmpty(text)) + { + strBuilder.Append(text + separator); + } } - } + } } \ No newline at end of file diff --git a/src/M3U8Parser/Extensions/StringExtension.cs b/src/M3U8Parser/Extensions/StringExtension.cs index 1de3b59..205b256 100644 --- a/src/M3U8Parser/Extensions/StringExtension.cs +++ b/src/M3U8Parser/Extensions/StringExtension.cs @@ -1,10 +1,10 @@ namespace M3U8Parser.Extensions { - public static class StringExtension - { - public static string RemoveLastCharacter(this string str) - { - return str.Remove(str.Length - 1, 1); - } - } + public static class StringExtension + { + public static string RemoveLastCharacter(this string str) + { + return str.Remove(str.Length - 1, 1); + } + } } \ No newline at end of file diff --git a/src/M3U8Parser/GlobalSuppression.cs b/src/M3U8Parser/GlobalSuppression.cs new file mode 100644 index 0000000..2e73087 --- /dev/null +++ b/src/M3U8Parser/GlobalSuppression.cs @@ -0,0 +1,2 @@ +[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1633:File must have header", Justification = "")] +[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1652:Enable XML documentation output", Justification = "")] \ No newline at end of file diff --git a/src/M3U8Parser/Interfaces/IAttribute.cs b/src/M3U8Parser/Interfaces/IAttribute.cs index 9a29c13..01cbc93 100644 --- a/src/M3U8Parser/Interfaces/IAttribute.cs +++ b/src/M3U8Parser/Interfaces/IAttribute.cs @@ -3,6 +3,7 @@ public interface IAttribute { public void Read(string content); + public string ToString(); } -} +} \ No newline at end of file diff --git a/src/M3U8Parser/Interfaces/IExtXType.cs b/src/M3U8Parser/Interfaces/IExtXType.cs index c9d394a..e99da7a 100644 --- a/src/M3U8Parser/Interfaces/IExtXType.cs +++ b/src/M3U8Parser/Interfaces/IExtXType.cs @@ -1,7 +1,7 @@ namespace M3U8Parser.Interfaces { - public interface IExtXType - { - public string ToString(); - } + public interface IExtXType + { + public string ToString(); + } } \ No newline at end of file diff --git a/src/M3U8Parser/M3U8Parser.csproj b/src/M3U8Parser/M3U8Parser.csproj index 8ca3c2c..fae3782 100644 --- a/src/M3U8Parser/M3U8Parser.csproj +++ b/src/M3U8Parser/M3U8Parser.csproj @@ -16,11 +16,19 @@ readme.md https://github.com/xavierlaffargue/m3u8parser-csharp.git git + CodeAnalysis.ruleset - - + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/src/M3U8Parser/MasterPlaylist.cs b/src/M3U8Parser/MasterPlaylist.cs index d3e3e5d..10581c8 100644 --- a/src/M3U8Parser/MasterPlaylist.cs +++ b/src/M3U8Parser/MasterPlaylist.cs @@ -1,119 +1,112 @@ -using System.Collections.Generic; -using System.IO; -using System.Text; -using System.Text.RegularExpressions; - -namespace M3U8Parser +namespace M3U8Parser { + using System.Collections.Generic; + using System.IO; + using System.Text; + using System.Text.RegularExpressions; using M3U8Parser.ExtXType; public class MasterPlaylist - { - private const int DefaultHlsVersion = 4; - public int HlsVersion { get; set; } - - public MasterPlaylist(int hlsVersion = DefaultHlsVersion) - { - HlsVersion = hlsVersion; - } - - public List Medias { get; set; } = new (); - - public List IFrameStreams { get; set; } = new (); - - public List Streams { get; set; } = new (); - - public static MasterPlaylist LoadFromFile(string path) - { - if (!File.Exists(path)) - { - throw new FileNotFoundException($"File not found : {path}"); - } - - return LoadFromText(File.ReadAllText(path)); - } - - public static MasterPlaylist LoadFromText(string text) - { - List medias = new (); - List iFrameStreams = new (); - List streams = new (); - var hlsVersion = DefaultHlsVersion; - - var matchHlsVersion = Regex.Match(text, "(?<=#EXT-X-VERSION:)(.*?)(?<=$)", RegexOptions.Multiline); - if (matchHlsVersion.Success) - { - var info = matchHlsVersion.Groups[0].Value; - hlsVersion = int.Parse(info); - } - - - var l = Regex.Split(text, "(?=#EXT)"); - - foreach (var line in l) - { - if (line.StartsWith(Media.Prefix)) - { + { + private const int DefaultHlsVersion = 4; + + public MasterPlaylist(int hlsVersion = DefaultHlsVersion) + { + HlsVersion = hlsVersion; + } + + public int HlsVersion { get; set; } + + public List Medias { get; set; } = new (); + + public List IFrameStreams { get; set; } = new (); + + public List Streams { get; set; } = new (); + + public static MasterPlaylist LoadFromFile(string path) + { + if (!File.Exists(path)) throw new FileNotFoundException($"File not found : {path}"); + + return LoadFromText(File.ReadAllText(path)); + } + + public static MasterPlaylist LoadFromText(string text) + { + List medias = new (); + List iFrameStreams = new (); + List streams = new (); + var hlsVersion = DefaultHlsVersion; + + var matchHlsVersion = Regex.Match(text, "(?<=#EXT-X-VERSION:)(.*?)(?<=$)", RegexOptions.Multiline); + if (matchHlsVersion.Success) + { + var info = matchHlsVersion.Groups[0].Value; + hlsVersion = int.Parse(info); + } + + var l = Regex.Split(text, "(?=#EXT)"); + + foreach (var line in l) + { + if (line.StartsWith(Media.Prefix)) + { medias.Add(new Media(line)); - } + } else if (line.StartsWith(IframeStreamInf.Prefix)) - { + { iFrameStreams.Add(new IframeStreamInf(line)); - } + } else if (line.StartsWith(StreamInf.Prefix)) - { + { streams.Add(new StreamInf(line)); - } - } - - return new MasterPlaylist(hlsVersion) - { - Medias = medias, - Streams = streams, - IFrameStreams = iFrameStreams - }; - } - - public override string ToString() - { - var strBuilder = new StringBuilder(); - - strBuilder.AppendLine("#EXTM3U"); - strBuilder.AppendLine($"#EXT-X-VERSION:{HlsVersion}"); - - strBuilder.AppendLine(); - - if (Medias.Count > 0) - { - foreach (var media in Medias) - { - strBuilder.AppendLine(media.ToString()); - } - - strBuilder.AppendLine(); - } - - if (IFrameStreams.Count > 0) - { - foreach (var iframeStream in IFrameStreams) - { - strBuilder.AppendLine(iframeStream.ToString()); - } - - strBuilder.AppendLine(); - } - - if (Streams.Count > 0) - { - foreach (var stream in Streams) - { - strBuilder.AppendLine(stream.ToString()); - } - - strBuilder.AppendLine(); - } - - return strBuilder.ToString().TrimEnd(); - } - } + } + } + + return new MasterPlaylist(hlsVersion) + { + Medias = medias, + Streams = streams, + IFrameStreams = iFrameStreams + }; + } + + public override string ToString() + { + var strBuilder = new StringBuilder(); + + strBuilder.AppendLine("#EXTM3U"); + strBuilder.AppendLine($"#EXT-X-VERSION:{HlsVersion}"); + + strBuilder.AppendLine(); + + if (Medias.Count > 0) + { + foreach (var media in Medias) + { + strBuilder.AppendLine(media.ToString()); + } + + strBuilder.AppendLine(); + } + + if (IFrameStreams.Count > 0) + { + foreach (var iframeStream in IFrameStreams) strBuilder.AppendLine(iframeStream.ToString()); + + strBuilder.AppendLine(); + } + + if (Streams.Count > 0) + { + foreach (var stream in Streams) + { + strBuilder.AppendLine(stream.ToString()); + } + + strBuilder.AppendLine(); + } + + return strBuilder.ToString().TrimEnd(); + } + } } \ No newline at end of file diff --git a/src/M3U8Parser/MediaPlaylist.cs b/src/M3U8Parser/MediaPlaylist.cs index 8338cf5..175fcd6 100644 --- a/src/M3U8Parser/MediaPlaylist.cs +++ b/src/M3U8Parser/MediaPlaylist.cs @@ -1,20 +1,19 @@ -using System.Collections.Generic; -using System.IO; -using System.Text; -using System.Text.RegularExpressions; -using M3U8Parser.Attributes.BaseAttribute; - -namespace M3U8Parser +namespace M3U8Parser { - using M3U8Parser.ExtXType; + using System.Collections.Generic; + using System.IO; + using System.Text; + using System.Text.RegularExpressions; + using M3U8Parser.Attributes.BaseAttribute; + using M3U8Parser.ExtXType; + + public class MediaPlaylist + { + public PlaylistType PlaylistType { get; set; } = new (); - public class MediaPlaylist - { - public PlaylistType PlaylistType { get; set; } = new (); + public Map Map { get; set; } = new (); - public Map Map { get; set; } = new(); - - public int HlsVersion { get; set; } + public int HlsVersion { get; set; } public bool? IFrameOnly { get; set; } @@ -23,157 +22,153 @@ public class MediaPlaylist public int? MediaSequence { get; set; } public List MediaSegments { get; set; } = new (); - - public bool HasEndList { get; set; } - - public MediaPlaylist() - { - } - - public static MediaPlaylist LoadFromFile(string path) - { - if (!File.Exists(path)) - { - throw new FileNotFoundException($"File not found : {path}"); - } - - return LoadFromText(File.ReadAllText(path)); - } - - public static MediaPlaylist LoadFromText(string text) - { - PlaylistType playlistType = null; - Map map = null; - List mediaSegments = new (); - var hlsVersion = 4; - var hasEndList = false; - int? targetDuration = null; + + public bool HasEndList { get; set; } + + public static MediaPlaylist LoadFromFile(string path) + { + if (!File.Exists(path)) + { + throw new FileNotFoundException($"File not found : {path}"); + } + + return LoadFromText(File.ReadAllText(path)); + } + + public static MediaPlaylist LoadFromText(string text) + { + PlaylistType playlistType = null; + Map map = null; + List mediaSegments = new (); + var hlsVersion = 4; + var hasEndList = false; + int? targetDuration = null; int? mediaSequence = null; - bool? iFrameOnly = null; + bool? iFrameOnly = null; - Regex regex = new Regex("(?=#EXT-X)(.*?)(?<=$)", RegexOptions.Multiline); - var matches = regex.Matches(text); + var regex = new Regex("(?=#EXT-X)(.*?)(?<=$)", RegexOptions.Multiline); + var matches = regex.Matches(text); foreach (Match match in matches) - { - var line = match.Value; - if (line.StartsWith(PlaylistTypeExt.Prefix)) - { - playlistType = new PlaylistTypeExt(line).Value; - } - else if (line.StartsWith(ExtXType.HlsVersion.Prefix)) - { - hlsVersion = new HlsVersion(line).Value; - } - else if (line.StartsWith(ExtXType.Map.Prefix)) - { - map = new Map(line); - } - else if (line.StartsWith("#EXT-X-ENDLIST")) - { - hasEndList = true; - } - else if(line.StartsWith("#EXT-X-I-FRAMES-ONLY")) - { + { + var line = match.Value; + if (line.StartsWith(PlaylistTypeExt.Prefix)) + { + playlistType = new PlaylistTypeExt(line).Value; + } + else if (line.StartsWith(ExtXType.HlsVersion.Prefix)) + { + hlsVersion = new HlsVersion(line).Value; + } + else if (line.StartsWith(Map.Prefix)) + { + map = new Map(line); + } + else if (line.StartsWith("#EXT-X-ENDLIST")) + { + hasEndList = true; + } + else if (line.StartsWith("#EXT-X-I-FRAMES-ONLY")) + { iFrameOnly = true; } - else if(line.StartsWith(ExtXType.TargetDuration.Prefix)) - { + else if (line.StartsWith(ExtXType.TargetDuration.Prefix)) + { targetDuration = new TargetDuration(line).Value; } - else if (line.StartsWith(M3U8Parser.ExtXType.MediaSequence.Prefix)) + else if (line.StartsWith(ExtXType.MediaSequence.Prefix)) { mediaSequence = new MediaSequence(line).Value; } } - - var l = Regex.Split(text, $"(?=#EXT-X-KEY|#EXTINF)"); - var segments = new List(); + + var l = Regex.Split(text, "(?=#EXT-X-KEY|#EXTINF)"); + var segments = new List(); MediaSegment mediaSegment = null; - foreach (var line in l) - { + foreach (var line in l) + { if (line.StartsWith(Key.Prefix)) { - if(mediaSegment != null) + if (mediaSegment != null) { - mediaSegment.Segments = segments; - mediaSegments.Add(mediaSegment); - } + mediaSegment.Segments = segments; + mediaSegments.Add(mediaSegment); + } mediaSegment = new MediaSegment(line); } if (line.StartsWith(Segment.Prefix)) - { - if (mediaSegment == null) - { - mediaSegment= new MediaSegment(); - } + { + if (mediaSegment == null) + { + mediaSegment = new MediaSegment(); + } - segments.Add(new Segment(line)); - } - } + segments.Add(new Segment(line)); + } + } mediaSegment.Segments = segments; mediaSegments.Add(mediaSegment); - return new MediaPlaylist() - { - PlaylistType = playlistType, - HlsVersion = hlsVersion, - MediaSegments = mediaSegments, - HasEndList = hasEndList, + return new MediaPlaylist + { + PlaylistType = playlistType, + HlsVersion = hlsVersion, + MediaSegments = mediaSegments, + HasEndList = hasEndList, TargetDuration = targetDuration, - MediaSequence = mediaSequence, + MediaSequence = mediaSequence, IFrameOnly = iFrameOnly, Map = map }; - } + } - public override string ToString() - { - var strBuilder = new StringBuilder(); + public override string ToString() + { + var strBuilder = new StringBuilder(); - strBuilder.AppendLine("#EXTM3U"); - strBuilder.AppendLine($"#EXT-X-VERSION:{HlsVersion}"); + strBuilder.AppendLine("#EXTM3U"); + strBuilder.AppendLine($"#EXT-X-VERSION:{HlsVersion}"); - if (TargetDuration != null) - { - strBuilder.AppendLine($"#EXT-X-TARGETDURATION:{TargetDuration}"); - } + if (TargetDuration != null) + { + strBuilder.AppendLine($"#EXT-X-TARGETDURATION:{TargetDuration}"); + } - if (MediaSequence != null) - { - strBuilder.AppendLine($"#EXT-X-MEDIA-SEQUENCE:{MediaSequence}"); - } + if (MediaSequence != null) + { + strBuilder.AppendLine($"#EXT-X-MEDIA-SEQUENCE:{MediaSequence}"); + } - if (PlaylistType != null) - { - strBuilder.AppendLine($"#EXT-X-PLAYLIST-TYPE:{PlaylistType}"); - } + if (PlaylistType != null) + { + strBuilder.AppendLine($"#EXT-X-PLAYLIST-TYPE:{PlaylistType}"); + } if (IFrameOnly.HasValue && IFrameOnly.Value) { - strBuilder.AppendLine($"#EXT-X-I-FRAMES-ONLY"); + strBuilder.AppendLine("#EXT-X-I-FRAMES-ONLY"); } - + if (Map != null) { - strBuilder.AppendLine(Map.ToString()); + strBuilder.AppendLine(Map.ToString()); } foreach (var segment in MediaSegments) - { - strBuilder.AppendLine(); - strBuilder.Append(segment); - } - - if (HasEndList) - { - strBuilder.AppendLine($"#EXT-X-ENDLIST"); - } - - return strBuilder.ToString().TrimEnd(); - } - } + { + strBuilder.AppendLine(); + strBuilder.Append(segment); + } + + if (HasEndList) + { + strBuilder.AppendLine("#EXT-X-ENDLIST"); + } + + return strBuilder.ToString().TrimEnd(); + } + } } \ No newline at end of file diff --git a/src/M3U8Parser/stylecop.json b/src/M3U8Parser/stylecop.json new file mode 100644 index 0000000..37e1ace --- /dev/null +++ b/src/M3U8Parser/stylecop.json @@ -0,0 +1,24 @@ +{ + "$schema": "https://raw.githubusercontent.com/DotNetAnalyzers/StyleCopAnalyzers/master/StyleCop.Analyzers/StyleCop.Analyzers/Settings/stylecop.schema.json", + "settings": { + "indentation": { + "indentationSize": 4, + "tabSize": 4 + }, + "orderingRules": { + "systemUsingDirectivesFirst": false, + "blankLinesBetweenUsingGroups": "require" + }, + "maintainabilityRules": { + "topLevelTypes": [ + "class", + "interface", + "enum", + "struct" + ] + }, + "layoutRules": { + "newlineAtEndOfFile": "omit" + } + } +} \ No newline at end of file diff --git a/tests/M3U8Parser.Tests/CodeAnalysis.ruleset b/tests/M3U8Parser.Tests/CodeAnalysis.ruleset new file mode 100644 index 0000000..8416264 --- /dev/null +++ b/tests/M3U8Parser.Tests/CodeAnalysis.ruleset @@ -0,0 +1,11 @@ + + + + + + + + + + + diff --git a/tests/M3U8Parser.Tests/CreateMediaPlaylistTests.cs b/tests/M3U8Parser.Tests/CreateMediaPlaylistTests.cs index f2e2517..82e9758 100644 --- a/tests/M3U8Parser.Tests/CreateMediaPlaylistTests.cs +++ b/tests/M3U8Parser.Tests/CreateMediaPlaylistTests.cs @@ -1,15 +1,15 @@ +using System; using NUnit.Framework; -namespace M3U8Parser.Tests +namespace M3U8Parser.Tests; + +public class CreateMediaPlaylistTests { - public class CreateMediaPlaylistTests - { - [Test] - public void DefaultHlsValueShouldContainOnlyVersion4() - { - MasterPlaylist mp = new MasterPlaylist(); - Assert.AreEqual(4, mp.HlsVersion); - Assert.AreEqual($"#EXTM3U{System.Environment.NewLine}#EXT-X-VERSION:4", mp.ToString()); - } - } + [Test] + public void DefaultHlsValueShouldContainOnlyVersion4() + { + var mp = new MasterPlaylist(); + Assert.AreEqual(4, mp.HlsVersion); + Assert.AreEqual($"#EXTM3U{Environment.NewLine}#EXT-X-VERSION:4", mp.ToString()); + } } \ No newline at end of file diff --git a/tests/M3U8Parser.Tests/LoadFromFileTest.cs b/tests/M3U8Parser.Tests/LoadFromFileTest.cs index 7962a97..e344d22 100644 --- a/tests/M3U8Parser.Tests/LoadFromFileTest.cs +++ b/tests/M3U8Parser.Tests/LoadFromFileTest.cs @@ -1,25 +1,23 @@ +using System.IO; using NUnit.Framework; -namespace M3U8Parser.Tests +namespace M3U8Parser.Tests; + +public class LoadFromFileTest { - using System.IO; + [Test] + public void LoadFromFileMediaPlaylist() + { + var result = MediaPlaylist.LoadFromFile(@"Sample" + Path.DirectorySeparatorChar + "mediaplaylist_vod_1.m3u8"); + + Assert.NotNull(result); + } + + [Test] + public void LoadFromFileMasterPlaylist() + { + var result = MasterPlaylist.LoadFromFile(@"Sample" + Path.DirectorySeparatorChar + "manifest_1.m3u8"); - public class LoadFromFileTest - { - [Test] - public void LoadFromFileMediaPlaylist() - { - var result = MediaPlaylist.LoadFromFile(@"Sample" + Path.DirectorySeparatorChar + "mediaplaylist_vod_1.m3u8"); - - Assert.NotNull(result); - } - - [Test] - public void LoadFromFileMasterPlaylist() - { - var result = MasterPlaylist.LoadFromFile(@"Sample" + Path.DirectorySeparatorChar + "manifest_1.m3u8"); - - Assert.NotNull(result); - } - } + Assert.NotNull(result); + } } \ No newline at end of file diff --git a/tests/M3U8Parser.Tests/LoadMasterPlaylistTests.cs b/tests/M3U8Parser.Tests/LoadMasterPlaylistTests.cs index 3bfd06e..f4cc4de 100644 --- a/tests/M3U8Parser.Tests/LoadMasterPlaylistTests.cs +++ b/tests/M3U8Parser.Tests/LoadMasterPlaylistTests.cs @@ -1,193 +1,208 @@ +using System; +using System.IO; using M3U8Parser.Attributes.BaseAttribute; +using M3U8Parser.ExtXType; using NUnit.Framework; -namespace M3U8Parser.Tests +namespace M3U8Parser.Tests; + +public class LoadMasterPlaylistTests { - using M3U8Parser.ExtXType; - using System; - using System.IO; - - public class LoadMasterPlaylistTests - { - private MasterPlaylist _masterPlaylist = new (); - - [SetUp] - public void Setup() - { - _masterPlaylist = MasterPlaylist.LoadFromFile(@"Sample" + Path.DirectorySeparatorChar + "manifest_1.m3u8"); - } - - [Test] - public void ShouldBeHave5Medias() - { - Assert.AreEqual(5, _masterPlaylist.Medias.Count); - } - - [Test] - public void ShouldBeHave5Streams() - { - Assert.AreEqual(7, _masterPlaylist.Streams.Count); - } - - [Test] - public void ShouldBeHave5IFrameStreams() - { - Assert.AreEqual(6, _masterPlaylist.IFrameStreams.Count); - } - - [Test] - public void ShouldBeHlsVersion7() - { - Assert.AreEqual(7, _masterPlaylist.HlsVersion); - } - - [Test] - public void CheckFirstMedia() - { - Assert.AreEqual(MediaType.Audio, _masterPlaylist.Medias[0].Type); - Assert.AreEqual("audio", _masterPlaylist.Medias[0].GroupId); - Assert.AreEqual("English", _masterPlaylist.Medias[0].Name); - Assert.AreEqual("eng", _masterPlaylist.Medias[0].Language); - Assert.AreEqual(true, _masterPlaylist.Medias[0].AutoSelect); - Assert.AreEqual(true, _masterPlaylist.Medias[0].Default); - Assert.AreEqual("QualityLevels(192000)/Manifest(audio_eng_aacl,format=m3u8-aapl,filter=desktop)", _masterPlaylist.Medias[0].Uri); - } - - [Test] - public void AfterEditMediaTypeShouldBeChanged() - { - _masterPlaylist.Medias[0].Type = MediaType.Video; - Assert.AreEqual(MediaType.Video, _masterPlaylist.Medias[0].Type); - } - - [Test] - public void AfterEditAutoSelectShouldBeChanged() - { - _masterPlaylist.Medias[0].AutoSelect = false; - Assert.AreEqual(false, _masterPlaylist.Medias[0].AutoSelect); - } - - [Test] - public void AfterEditLanguageShouldBeChanged() - { - _masterPlaylist.Medias[0].Language = "fre"; - Assert.AreEqual("fre", _masterPlaylist.Medias[0].Language); - } - - [Test] - public void DefaultVersionShouldBe4() - { - var masterPlaylist = new MasterPlaylist(); - Assert.AreEqual(4, masterPlaylist.HlsVersion); - } - - [Test] - public void ParseClosedCaptionsMediaShouldBeOk() - { - var media = new Media("#EXT-X-MEDIA:TYPE=CLOSED-CAPTIONS,GROUP-ID=\"cc\",NAME=\"CEA608_CC\",AUTOSELECT=NO,DEFAULT=NO,INSTREAM-ID=\"CC1\""); - Assert.AreEqual(MediaType.CloseCaptions, media.Type); - Assert.AreEqual("cc", media.GroupId); - Assert.AreEqual("CEA608_CC", media.Name); - Assert.AreEqual(false, media.AutoSelect); - Assert.AreEqual(false, media.Default); - Assert.AreEqual("CC1", media.InstreamId); - Assert.AreEqual(null, media.Language); - } - - [Test] - public void ParseVideoMediaShouldBeOk() - { - var media = new Media("#EXT-X-MEDIA:TYPE=AUDIO,GROUP-ID=\"audio\",NAME=\"English\",LANGUAGE=\"eng\",AUTOSELECT=YES,DEFAULT=YES,URI=\"uri/Manifest\""); - Assert.AreEqual(MediaType.Audio, media.Type); - Assert.AreEqual("audio", media.GroupId); - Assert.AreEqual("English", media.Name); - Assert.AreEqual("eng", media.Language); - Assert.AreEqual(true, media.AutoSelect); - Assert.AreEqual(true, media.Default); - Assert.AreEqual("uri/Manifest", media.Uri); - Assert.AreEqual(null, media.InstreamId); - } - - [Test] - public void ParseStreamInfShouldBeOk() - { - var media = new StreamInf("#EXT-X-STREAM-INF:AVERAGE-BANDWIDTH=2778321,BANDWIDTH=3971374,VIDEO-RANGE=PQ,CODECS=\"hvc1.2.4.L123.B0\",RESOLUTION=1280x720,FRAME-RATE=23.976,CLOSED-CAPTIONS=NONE,HDCP-LEVEL=TYPE-1\r\nsdr_720/prog_index.m3u8"); - Assert.AreEqual(2778321, media.AverageBandwidth); - Assert.AreEqual(3971374, media.Bandwidth); - Assert.AreEqual(VideoRangeType.PQ, media.VideoRange); - Assert.AreEqual("hvc1.2.4.L123.B0", media.Codecs); - Assert.AreEqual(23.976, media.FrameRate); - Assert.AreEqual(HdcpLevelType.TYPE_1, media.HdcpLevel); - Assert.AreEqual(1280, media.Resolution.Width); - Assert.AreEqual(720, media.Resolution.Height); - Assert.AreEqual("sdr_720/prog_index.m3u8", media.Uri); - } - - [Test] - public void EditResolutionShouldBeOk() + private MasterPlaylist _masterPlaylist = new(); + + [SetUp] + public void Setup() + { + _masterPlaylist = MasterPlaylist.LoadFromFile(@"Sample" + Path.DirectorySeparatorChar + "manifest_1.m3u8"); + } + + [Test] + public void ShouldBeHave5Medias() + { + Assert.AreEqual(5, _masterPlaylist.Medias.Count); + } + + [Test] + public void ShouldBeHave5Streams() + { + Assert.AreEqual(7, _masterPlaylist.Streams.Count); + } + + [Test] + public void ShouldBeHave5IFrameStreams() + { + Assert.AreEqual(6, _masterPlaylist.IFrameStreams.Count); + } + + [Test] + public void ShouldBeHlsVersion7() + { + Assert.AreEqual(7, _masterPlaylist.HlsVersion); + } + + [Test] + public void CheckFirstMedia() + { + Assert.AreEqual(MediaType.Audio, _masterPlaylist.Medias[0].Type); + Assert.AreEqual("audio", _masterPlaylist.Medias[0].GroupId); + Assert.AreEqual("English", _masterPlaylist.Medias[0].Name); + Assert.AreEqual("eng", _masterPlaylist.Medias[0].Language); + Assert.AreEqual(true, _masterPlaylist.Medias[0].AutoSelect); + Assert.AreEqual(true, _masterPlaylist.Medias[0].Default); + Assert.AreEqual("QualityLevels(192000)/Manifest(audio_eng_aacl,format=m3u8-aapl,filter=desktop)", + _masterPlaylist.Medias[0].Uri); + } + + [Test] + public void AfterEditMediaTypeShouldBeChanged() + { + _masterPlaylist.Medias[0].Type = MediaType.Video; + Assert.AreEqual(MediaType.Video, _masterPlaylist.Medias[0].Type); + } + + [Test] + public void AfterEditAutoSelectShouldBeChanged() + { + _masterPlaylist.Medias[0].AutoSelect = false; + Assert.AreEqual(false, _masterPlaylist.Medias[0].AutoSelect); + } + + [Test] + public void AfterEditLanguageShouldBeChanged() + { + _masterPlaylist.Medias[0].Language = "fre"; + Assert.AreEqual("fre", _masterPlaylist.Medias[0].Language); + } + + [Test] + public void DefaultVersionShouldBe4() + { + var masterPlaylist = new MasterPlaylist(); + Assert.AreEqual(4, masterPlaylist.HlsVersion); + } + + [Test] + public void ParseClosedCaptionsMediaShouldBeOk() + { + var media = new Media( + "#EXT-X-MEDIA:TYPE=CLOSED-CAPTIONS,GROUP-ID=\"cc\",NAME=\"CEA608_CC\",AUTOSELECT=NO,DEFAULT=NO,INSTREAM-ID=\"CC1\""); + Assert.AreEqual(MediaType.CloseCaptions, media.Type); + Assert.AreEqual("cc", media.GroupId); + Assert.AreEqual("CEA608_CC", media.Name); + Assert.AreEqual(false, media.AutoSelect); + Assert.AreEqual(false, media.Default); + Assert.AreEqual("CC1", media.InstreamId); + Assert.AreEqual(null, media.Language); + } + + [Test] + public void ParseVideoMediaShouldBeOk() + { + var media = new Media( + "#EXT-X-MEDIA:TYPE=AUDIO,GROUP-ID=\"audio\",NAME=\"English\",LANGUAGE=\"eng\",AUTOSELECT=YES,DEFAULT=YES,URI=\"uri/Manifest\""); + Assert.AreEqual(MediaType.Audio, media.Type); + Assert.AreEqual("audio", media.GroupId); + Assert.AreEqual("English", media.Name); + Assert.AreEqual("eng", media.Language); + Assert.AreEqual(true, media.AutoSelect); + Assert.AreEqual(true, media.Default); + Assert.AreEqual("uri/Manifest", media.Uri); + Assert.AreEqual(null, media.InstreamId); + } + + [Test] + public void ParseStreamInfShouldBeOk() + { + var media = new StreamInf( + "#EXT-X-STREAM-INF:AVERAGE-BANDWIDTH=2778321,BANDWIDTH=3971374,VIDEO-RANGE=PQ,CODECS=\"hvc1.2.4.L123.B0\",RESOLUTION=1280x720,FRAME-RATE=23.976,CLOSED-CAPTIONS=NONE,HDCP-LEVEL=TYPE-1\r\nsdr_720/prog_index.m3u8"); + Assert.AreEqual(2778321, media.AverageBandwidth); + Assert.AreEqual(3971374, media.Bandwidth); + Assert.AreEqual(VideoRangeType.PQ, media.VideoRange); + Assert.AreEqual("hvc1.2.4.L123.B0", media.Codecs); + Assert.AreEqual(23.976, media.FrameRate); + Assert.AreEqual(HdcpLevelType.TYPE_1, media.HdcpLevel); + Assert.AreEqual(1280, media.Resolution.Width); + Assert.AreEqual(720, media.Resolution.Height); + Assert.AreEqual("sdr_720/prog_index.m3u8", media.Uri); + } + + [Test] + public void EditResolutionShouldBeOk() + { + var media = new StreamInf("#EXT-X-STREAM-INF:RESOLUTION=1280x720") { - var media = new StreamInf("#EXT-X-STREAM-INF:RESOLUTION=1280x720") + Resolution = { - Resolution = - { - Width = 1920, - Height = 1080 - } - }; - - Assert.AreEqual(1920, media.Resolution.Width); - Assert.AreEqual(1080, media.Resolution.Height); - } - - [Test] - public void ParseAndToStringMediaShouldBeEqual() - { - var media = new Media("#EXT-X-MEDIA:TYPE=AUDIO,GROUP-ID=\"audio\",NAME=\"English\",LANGUAGE=\"eng\",AUTOSELECT=YES,DEFAULT=YES,URI=\"uri/Manifest\""); - Assert.AreEqual("#EXT-X-MEDIA:AUTOSELECT=YES,DEFAULT=YES,GROUP-ID=\"audio\",LANGUAGE=\"eng\",TYPE=AUDIO,NAME=\"English\",URI=\"uri/Manifest\"", media.ToString()); - } - - [Test] - public void ParseAndToStringMediaShouldBeEqualWithDoubleQuote() - { - var media = new Media("#EXT-X-MEDIA:TYPE=AUDIO,GROUP-ID=\"audio\",NAME=\"English \\\"Original\\\"\",LANGUAGE=\"eng\",AUTOSELECT=YES,DEFAULT=YES,URI=\"uri/Manifest\""); - Assert.AreEqual("#EXT-X-MEDIA:AUTOSELECT=YES,DEFAULT=YES,GROUP-ID=\"audio\",LANGUAGE=\"eng\",TYPE=AUDIO,NAME=\"English \\\"Original\\\"\",URI=\"uri/Manifest\"", media.ToString()); - } - - [Test] - public void ParseAndToStringStreamShouldBeEqual() - { - var media = new StreamInf("#EXT-X-STREAM-INF:BANDWIDTH=3971374,AVERAGE-BANDWIDTH=2778321,VIDEO-RANGE=SDR,CODECS=\"hvc1.2.4.L123.B0\",RESOLUTION=1280x720,FRAME-RATE=23.976,HDCP-LEVEL=NONE,SUBTITLES=\"subtitle\"\r\nsdr_720/prog_index.m3u8"); - Assert.AreEqual("#EXT-X-STREAM-INF:BANDWIDTH=3971374,AVERAGE-BANDWIDTH=2778321,VIDEO-RANGE=SDR,CODECS=\"hvc1.2.4.L123.B0\",RESOLUTION=1280x720,FRAME-RATE=23.976,HDCP-LEVEL=NONE,SUBTITLES=\"subtitle\"\r\nsdr_720/prog_index.m3u8", media.ToString()); - } - - [Test] - public void OptionnalAttributesShouldBeNullable() - { - var media = new StreamInf("#EXT-X-STREAM-INF:BANDWIDTH=2778321,CODECS=\"hvc1.2.4.L123.B0\"\r\nsdr_720/prog_index.m3u8"); - Assert.AreEqual(media.AverageBandwidth, null); - Assert.AreEqual(media.FrameRate, null); - Assert.AreEqual(media.VideoRange, null); - Assert.AreEqual(media.Resolution, null); - Assert.AreEqual(media.HdcpLevel, null); - } - - [Test] - public void ParseAndToStringIFrameStreamShouldBeEqual() - { - var media = new IframeStreamInf("#EXT-X-I-FRAME-STREAM-INF:BANDWIDTH=621335,CODECS=\"avc1.42c00d\",RESOLUTION=416x234,URI=\"QualityLevels(399992)/Manifest(video,format=m3u8-aapl,filter=desktop,type=keyframes)\""); - Assert.AreEqual("#EXT-X-I-FRAME-STREAM-INF:BANDWIDTH=621335,CODECS=\"avc1.42c00d\",RESOLUTION=416x234,URI=\"QualityLevels(399992)/Manifest(video,format=m3u8-aapl,filter=desktop,type=keyframes)\"", media.ToString()); - } - - [Test] - public void MasterPlaylistShouldBeContainExtM3U() - { - Assert.That(_masterPlaylist.ToString(), Does.Contain("#EXTM3U" + Environment.NewLine)); - } - - [Test] - public void MasterPlaylistShouldBeContainVersionExtM3U() - { - Assert.That(_masterPlaylist.ToString(), Does.Contain("#EXT-X-VERSION:7" + Environment.NewLine)); - } + Width = 1920, + Height = 1080 + } + }; + + Assert.AreEqual(1920, media.Resolution.Width); + Assert.AreEqual(1080, media.Resolution.Height); + } + + [Test] + public void ParseAndToStringMediaShouldBeEqual() + { + var media = new Media( + "#EXT-X-MEDIA:TYPE=AUDIO,GROUP-ID=\"audio\",NAME=\"English\",LANGUAGE=\"eng\",AUTOSELECT=YES,DEFAULT=YES,URI=\"uri/Manifest\""); + Assert.AreEqual( + "#EXT-X-MEDIA:AUTOSELECT=YES,DEFAULT=YES,GROUP-ID=\"audio\",LANGUAGE=\"eng\",TYPE=AUDIO,NAME=\"English\",URI=\"uri/Manifest\"", + media.ToString()); + } + + [Test] + public void ParseAndToStringMediaShouldBeEqualWithDoubleQuote() + { + var media = new Media( + "#EXT-X-MEDIA:TYPE=AUDIO,GROUP-ID=\"audio\",NAME=\"English \\\"Original\\\"\",LANGUAGE=\"eng\",AUTOSELECT=YES,DEFAULT=YES,URI=\"uri/Manifest\""); + Assert.AreEqual( + "#EXT-X-MEDIA:AUTOSELECT=YES,DEFAULT=YES,GROUP-ID=\"audio\",LANGUAGE=\"eng\",TYPE=AUDIO,NAME=\"English \\\"Original\\\"\",URI=\"uri/Manifest\"", + media.ToString()); + } + + [Test] + public void ParseAndToStringStreamShouldBeEqual() + { + var media = new StreamInf( + "#EXT-X-STREAM-INF:BANDWIDTH=3971374,AVERAGE-BANDWIDTH=2778321,VIDEO-RANGE=SDR,CODECS=\"hvc1.2.4.L123.B0\",RESOLUTION=1280x720,FRAME-RATE=23.976,HDCP-LEVEL=NONE,SUBTITLES=\"subtitle\"\r\nsdr_720/prog_index.m3u8"); + Assert.AreEqual( + "#EXT-X-STREAM-INF:BANDWIDTH=3971374,AVERAGE-BANDWIDTH=2778321,VIDEO-RANGE=SDR,CODECS=\"hvc1.2.4.L123.B0\",RESOLUTION=1280x720,FRAME-RATE=23.976,HDCP-LEVEL=NONE,SUBTITLES=\"subtitle\"\r\nsdr_720/prog_index.m3u8", + media.ToString()); + } + + [Test] + public void OptionnalAttributesShouldBeNullable() + { + var media = new StreamInf( + "#EXT-X-STREAM-INF:BANDWIDTH=2778321,CODECS=\"hvc1.2.4.L123.B0\"\r\nsdr_720/prog_index.m3u8"); + Assert.AreEqual(media.AverageBandwidth, null); + Assert.AreEqual(media.FrameRate, null); + Assert.AreEqual(media.VideoRange, null); + Assert.AreEqual(media.Resolution, null); + Assert.AreEqual(media.HdcpLevel, null); + } + + [Test] + public void ParseAndToStringIFrameStreamShouldBeEqual() + { + var media = new IframeStreamInf( + "#EXT-X-I-FRAME-STREAM-INF:BANDWIDTH=621335,CODECS=\"avc1.42c00d\",RESOLUTION=416x234,URI=\"QualityLevels(399992)/Manifest(video,format=m3u8-aapl,filter=desktop,type=keyframes)\""); + Assert.AreEqual( + "#EXT-X-I-FRAME-STREAM-INF:BANDWIDTH=621335,CODECS=\"avc1.42c00d\",RESOLUTION=416x234,URI=\"QualityLevels(399992)/Manifest(video,format=m3u8-aapl,filter=desktop,type=keyframes)\"", + media.ToString()); + } + + [Test] + public void MasterPlaylistShouldBeContainExtM3U() + { + Assert.That(_masterPlaylist.ToString(), Does.Contain("#EXTM3U" + Environment.NewLine)); + } + + [Test] + public void MasterPlaylistShouldBeContainVersionExtM3U() + { + Assert.That(_masterPlaylist.ToString(), Does.Contain("#EXT-X-VERSION:7" + Environment.NewLine)); } } \ No newline at end of file diff --git a/tests/M3U8Parser.Tests/LoadMediaPlaylistByteRangeTests.cs b/tests/M3U8Parser.Tests/LoadMediaPlaylistByteRangeTests.cs index 0516641..1a4ad3d 100644 --- a/tests/M3U8Parser.Tests/LoadMediaPlaylistByteRangeTests.cs +++ b/tests/M3U8Parser.Tests/LoadMediaPlaylistByteRangeTests.cs @@ -1,150 +1,149 @@ +using System; using System.Collections.Generic; +using System.IO; using M3U8Parser.Attributes.BaseAttribute; +using M3U8Parser.ExtXType; using NUnit.Framework; -namespace M3U8Parser.Tests +namespace M3U8Parser.Tests; + +public class LoadMediaPlaylistByteRangeTests { - using M3U8Parser.ExtXType; - using System; - using System.IO; + private MediaPlaylist _mediaPlaylist = new(); + + [SetUp] + public void Setup() + { + _mediaPlaylist = + MediaPlaylist.LoadFromFile(@"Sample" + Path.DirectorySeparatorChar + "mediaplaylist_vod_2.m3u8"); + } + + [Test] + public void PlaylistTypeShouldBePVod() + { + Assert.AreEqual(PlaylistType.Vod, _mediaPlaylist.PlaylistType); + } + + [Test] + public void TargetDurationShouldBe10() + { + Assert.AreEqual(11, _mediaPlaylist.TargetDuration); + } + + [Test] + public void MediaSequenceShouldBe0() + { + Assert.AreEqual(1, _mediaPlaylist.MediaSequence); + } + + [Test] + public void MediaSequenceShouldBeTrue() + { + Assert.AreEqual(null, _mediaPlaylist.IFrameOnly); + } + + [Test] + public void HlsVersionShouldBe4() + { + Assert.AreEqual(4, _mediaPlaylist.HlsVersion); + } + + [Test] + public void MediaSegmentKeyShouldBeNull() + { + Assert.AreEqual(null, _mediaPlaylist.MediaSegments[0].Key); + } + + [Test] + public void MediaSegmentCountShouldBe4() + { + Assert.AreEqual(296, _mediaPlaylist.MediaSegments[0].Segments.Count); + } - public class LoadMediaPlaylistByteRangeTests - { - private MediaPlaylist _mediaPlaylist = new (); - - [SetUp] - public void Setup() - { - _mediaPlaylist = MediaPlaylist.LoadFromFile(@"Sample" + Path.DirectorySeparatorChar + "mediaplaylist_vod_2.m3u8"); - } - - [Test] - public void PlaylistTypeShouldBePVod() - { - Assert.AreEqual(PlaylistType.Vod, _mediaPlaylist.PlaylistType); - } - - [Test] - public void TargetDurationShouldBe10() - { - Assert.AreEqual(11, _mediaPlaylist.TargetDuration); - } - - [Test] - public void MediaSequenceShouldBe0() - { - Assert.AreEqual(1, _mediaPlaylist.MediaSequence); - } - - [Test] - public void MediaSequenceShouldBeTrue() - { - Assert.AreEqual(null, _mediaPlaylist.IFrameOnly); - } - - [Test] - public void HlsVersionShouldBe4() - { - Assert.AreEqual(4, _mediaPlaylist.HlsVersion); - } + [Test] + public void FirstMediaSegmentDurationShouldBe10() + { + Assert.AreEqual(10.005, _mediaPlaylist.MediaSegments[0].Segments[0].Duration); + } - [Test] - public void MediaSegmentKeyShouldBeNull() - { - Assert.AreEqual(null, _mediaPlaylist.MediaSegments[0].Key); - } - - [Test] - public void MediaSegmentCountShouldBe4() - { - Assert.AreEqual(296, _mediaPlaylist.MediaSegments[0].Segments.Count); - } + [Test] + public void UrlMediaSegmentDurationShouldBefileSequenceTs() + { + Assert.AreEqual("mediaFileSample.aac", _mediaPlaylist.MediaSegments[0].Segments[0].Uri); + } - [Test] - public void FirstMediaSegmentDurationShouldBe10() - { - Assert.AreEqual(10.005, _mediaPlaylist.MediaSegments[0].Segments[0].Duration); - } - - [Test] - public void UrlMediaSegmentDurationShouldBefileSequenceTs() - { - Assert.AreEqual("mediaFileSample.aac", _mediaPlaylist.MediaSegments[0].Segments[0].Uri); - } - - [Test] - public void WriteToString() - { - var mediaPlaylist = MediaPlaylist.LoadFromFile(@"Sample" + Path.DirectorySeparatorChar + "mediaplaylist_vod_3.m3u8"); - Console.WriteLine(mediaPlaylist.ToString()); - } - - [Test] - public void CreateMediaPlaylist() - { - var mediaPlaylist = new MediaPlaylist() - { - HasEndList = true, - HlsVersion = 7, - IFrameOnly = true, - PlaylistType = PlaylistType.Vod, - TargetDuration = 3600, - MediaSequence = null, - MediaSegments = - { - new MediaSegment() - { - Key = new Key() - { - Method = MethodType.AES_128, Uri = "http://test.local" - }, - Segments = new List() - { - new Segment() - { - ByteRangeLentgh = 100, - ByteRangeStartSubRange = 0, - Duration = 100, - Uri = "http://test/1.mp4" - }, - new Segment() - { - ByteRangeLentgh = 100, - ByteRangeStartSubRange = 100, - Duration = 100, - Uri = "http://test/2.mp4" - } - } - }, - new MediaSegment() - { - Key = new Key() - { - Method = MethodType.AES_128, Uri = "http://test2.local" - }, - Segments = new List() - { - new Segment() - { - ByteRangeLentgh = 100, - ByteRangeStartSubRange = 300, - Duration = 100, - Uri = "http://test/3.mp4" - }, - new Segment() - { - ByteRangeLentgh = 100, - ByteRangeStartSubRange = 400, - Duration = 100, - Uri = "http://test/4.mp4" - } - } - } - } - }; + [Test] + public void WriteToString() + { + var mediaPlaylist = + MediaPlaylist.LoadFromFile(@"Sample" + Path.DirectorySeparatorChar + "mediaplaylist_vod_3.m3u8"); + Console.WriteLine(mediaPlaylist.ToString()); + } - Console.WriteLine(mediaPlaylist.ToString()); - } + [Test] + public void CreateMediaPlaylist() + { + var mediaPlaylist = new MediaPlaylist + { + HasEndList = true, + HlsVersion = 7, + IFrameOnly = true, + PlaylistType = PlaylistType.Vod, + TargetDuration = 3600, + MediaSequence = null, + MediaSegments = + { + new MediaSegment + { + Key = new Key + { + Method = MethodType.AES_128, Uri = "http://test.local" + }, + Segments = new List + { + new() + { + ByteRangeLentgh = 100, + ByteRangeStartSubRange = 0, + Duration = 100, + Uri = "http://test/1.mp4" + }, + new() + { + ByteRangeLentgh = 100, + ByteRangeStartSubRange = 100, + Duration = 100, + Uri = "http://test/2.mp4" + } + } + }, + new MediaSegment + { + Key = new Key + { + Method = MethodType.AES_128, Uri = "http://test2.local" + }, + Segments = new List + { + new() + { + ByteRangeLentgh = 100, + ByteRangeStartSubRange = 300, + Duration = 100, + Uri = "http://test/3.mp4" + }, + new() + { + ByteRangeLentgh = 100, + ByteRangeStartSubRange = 400, + Duration = 100, + Uri = "http://test/4.mp4" + } + } + } + } + }; - } + Console.WriteLine(mediaPlaylist.ToString()); + } } \ No newline at end of file diff --git a/tests/M3U8Parser.Tests/LoadMediaPlaylistTests.cs b/tests/M3U8Parser.Tests/LoadMediaPlaylistTests.cs index a6964dc..8e52335 100644 --- a/tests/M3U8Parser.Tests/LoadMediaPlaylistTests.cs +++ b/tests/M3U8Parser.Tests/LoadMediaPlaylistTests.cs @@ -1,162 +1,161 @@ +using System; using System.Collections.Generic; +using System.IO; using M3U8Parser.Attributes.BaseAttribute; +using M3U8Parser.ExtXType; using NUnit.Framework; -namespace M3U8Parser.Tests +namespace M3U8Parser.Tests; + +public class LoadMediaPlaylistTests { - using M3U8Parser.ExtXType; - using System; - using System.IO; - - public class LoadMediaPlaylistTests - { - private MediaPlaylist _mediaPlaylist = new (); - - [SetUp] - public void Setup() - { - _mediaPlaylist = MediaPlaylist.LoadFromFile(@"Sample" + Path.DirectorySeparatorChar + "mediaplaylist_vod_1.m3u8"); - } - - [Test] - public void PlaylistTypeShouldBePVod() - { - Assert.AreEqual(PlaylistType.Vod, _mediaPlaylist.PlaylistType); - } - - [Test] - public void TargetDurationShouldBe10() - { - Assert.AreEqual(10, _mediaPlaylist.TargetDuration); - } - - [Test] - public void MediaSequenceShouldBe0() - { - Assert.AreEqual(0, _mediaPlaylist.MediaSequence); - } - - [Test] - public void MediaSequenceShouldBeTrue() - { - Assert.AreEqual(true, _mediaPlaylist.IFrameOnly); - } - - [Test] - public void HlsVersionShouldBe4() - { - Assert.AreEqual(4, _mediaPlaylist.HlsVersion); - } - - [Test] - public void MapShouldExist() - { - Assert.NotNull(_mediaPlaylist.Map); - } - - [Test] - public void MapUriShouldBeCorrect() - { - Assert.AreEqual("main.mp4", _mediaPlaylist.Map.Uri); - } - - [Test] - public void MediaSegmentKeyShouldBeNull() - { - Assert.AreEqual(null, _mediaPlaylist.MediaSegments[0].Key); - } - - [Test] - public void MediaSegmentCountShouldBe4() - { - Assert.AreEqual(4, _mediaPlaylist.MediaSegments[0].Segments.Count); - } - - [Test] - public void FirstMediaSegmentDurationShouldBe10() - { - Assert.AreEqual(10, _mediaPlaylist.MediaSegments[0].Segments[0].Duration); - } - - [Test] - public void UrlMediaSegmentDurationShouldBefileSequenceTs() - { - Assert.AreEqual("http://example.com/movie1/fileSequenceA.ts", _mediaPlaylist.MediaSegments[0].Segments[0].Uri); - } - - [Test] - public void WriteToString() - { - var mediaPlaylist = MediaPlaylist.LoadFromFile(@"Sample" + Path.DirectorySeparatorChar + "mediaplaylist_vod_3.m3u8"); - Console.WriteLine(mediaPlaylist.ToString()); - } - - [Test] - public void CreateMediaPlaylist() - { - var mediaPlaylist = new MediaPlaylist() - { - HasEndList = true, - HlsVersion = 7, - IFrameOnly = true, - PlaylistType = PlaylistType.Vod, - TargetDuration = 3600, - MediaSequence = null, - MediaSegments = - { - new MediaSegment() - { - Key = new Key() - { - Method = MethodType.AES_128, Uri = "http://test.local" - }, - Segments = new List() - { - new Segment() - { - ByteRangeLentgh = 100, - ByteRangeStartSubRange = 0, - Duration = 100, - Uri = "http://test/1.mp4" - }, - new Segment() - { - ByteRangeLentgh = 100, - ByteRangeStartSubRange = 100, - Duration = 100, - Uri = "http://test/2.mp4" - } - } - }, - new MediaSegment() - { - Key = new Key() - { - Method = MethodType.AES_128, Uri = "http://test2.local" - }, - Segments = new List() - { - new Segment() - { - ByteRangeLentgh = 100, - ByteRangeStartSubRange = 300, - Duration = 100, - Uri = "http://test/3.mp4" - }, - new Segment() - { - ByteRangeLentgh = 100, - ByteRangeStartSubRange = 400, - Duration = 100, - Uri = "http://test/4.mp4" - } - } - } - } - }; - - Console.WriteLine(mediaPlaylist.ToString()); - } - - } + private MediaPlaylist _mediaPlaylist = new(); + + [SetUp] + public void Setup() + { + _mediaPlaylist = + MediaPlaylist.LoadFromFile(@"Sample" + Path.DirectorySeparatorChar + "mediaplaylist_vod_1.m3u8"); + } + + [Test] + public void PlaylistTypeShouldBePVod() + { + Assert.AreEqual(PlaylistType.Vod, _mediaPlaylist.PlaylistType); + } + + [Test] + public void TargetDurationShouldBe10() + { + Assert.AreEqual(10, _mediaPlaylist.TargetDuration); + } + + [Test] + public void MediaSequenceShouldBe0() + { + Assert.AreEqual(0, _mediaPlaylist.MediaSequence); + } + + [Test] + public void MediaSequenceShouldBeTrue() + { + Assert.AreEqual(true, _mediaPlaylist.IFrameOnly); + } + + [Test] + public void HlsVersionShouldBe4() + { + Assert.AreEqual(4, _mediaPlaylist.HlsVersion); + } + + [Test] + public void MapShouldExist() + { + Assert.NotNull(_mediaPlaylist.Map); + } + + [Test] + public void MapUriShouldBeCorrect() + { + Assert.AreEqual("main.mp4", _mediaPlaylist.Map.Uri); + } + + [Test] + public void MediaSegmentKeyShouldBeNull() + { + Assert.AreEqual(null, _mediaPlaylist.MediaSegments[0].Key); + } + + [Test] + public void MediaSegmentCountShouldBe4() + { + Assert.AreEqual(4, _mediaPlaylist.MediaSegments[0].Segments.Count); + } + + [Test] + public void FirstMediaSegmentDurationShouldBe10() + { + Assert.AreEqual(10, _mediaPlaylist.MediaSegments[0].Segments[0].Duration); + } + + [Test] + public void UrlMediaSegmentDurationShouldBefileSequenceTs() + { + Assert.AreEqual("http://example.com/movie1/fileSequenceA.ts", _mediaPlaylist.MediaSegments[0].Segments[0].Uri); + } + + [Test] + public void WriteToString() + { + var mediaPlaylist = + MediaPlaylist.LoadFromFile(@"Sample" + Path.DirectorySeparatorChar + "mediaplaylist_vod_3.m3u8"); + Console.WriteLine(mediaPlaylist.ToString()); + } + + [Test] + public void CreateMediaPlaylist() + { + var mediaPlaylist = new MediaPlaylist + { + HasEndList = true, + HlsVersion = 7, + IFrameOnly = true, + PlaylistType = PlaylistType.Vod, + TargetDuration = 3600, + MediaSequence = null, + MediaSegments = + { + new MediaSegment + { + Key = new Key + { + Method = MethodType.AES_128, Uri = "http://test.local" + }, + Segments = new List + { + new() + { + ByteRangeLentgh = 100, + ByteRangeStartSubRange = 0, + Duration = 100, + Uri = "http://test/1.mp4" + }, + new() + { + ByteRangeLentgh = 100, + ByteRangeStartSubRange = 100, + Duration = 100, + Uri = "http://test/2.mp4" + } + } + }, + new MediaSegment + { + Key = new Key + { + Method = MethodType.AES_128, Uri = "http://test2.local" + }, + Segments = new List + { + new() + { + ByteRangeLentgh = 100, + ByteRangeStartSubRange = 300, + Duration = 100, + Uri = "http://test/3.mp4" + }, + new() + { + ByteRangeLentgh = 100, + ByteRangeStartSubRange = 400, + Duration = 100, + Uri = "http://test/4.mp4" + } + } + } + } + }; + + Console.WriteLine(mediaPlaylist.ToString()); + } } \ No newline at end of file diff --git a/tests/M3U8Parser.Tests/M3U8Parser.Tests.csproj b/tests/M3U8Parser.Tests/M3U8Parser.Tests.csproj index 2c6dcee..6752f8c 100644 --- a/tests/M3U8Parser.Tests/M3U8Parser.Tests.csproj +++ b/tests/M3U8Parser.Tests/M3U8Parser.Tests.csproj @@ -5,17 +5,18 @@ enable false + CodeAnalysis.ruleset - - - - + + + + - + @@ -23,13 +24,13 @@ Always - Always + Always - Always + Always - Always + Always