From 85da15685f7a761af3a34f1c591bf129aa5deb5f Mon Sep 17 00:00:00 2001
From: Andreas B <6439218+YouKnowBlom@users.noreply.github.com>
Date: Sun, 15 Mar 2020 15:06:38 +0100
Subject: [PATCH 1/5] Refactor DynamicHlsService.AppendPlaylist to use
StringBuilder
---
MediaBrowser.Api/Playback/Hls/DynamicHlsService.cs | 11 ++++++++---
1 file changed, 8 insertions(+), 3 deletions(-)
diff --git a/MediaBrowser.Api/Playback/Hls/DynamicHlsService.cs b/MediaBrowser.Api/Playback/Hls/DynamicHlsService.cs
index 262f517869..8787eb2a3f 100644
--- a/MediaBrowser.Api/Playback/Hls/DynamicHlsService.cs
+++ b/MediaBrowser.Api/Playback/Hls/DynamicHlsService.cs
@@ -724,7 +724,10 @@ namespace MediaBrowser.Api.Playback.Hls
private void AppendPlaylist(StringBuilder builder, StreamState state, string url, int bitrate, string subtitleGroup)
{
- var header = "#EXT-X-STREAM-INF:BANDWIDTH=" + bitrate.ToString(CultureInfo.InvariantCulture) + ",AVERAGE-BANDWIDTH=" + bitrate.ToString(CultureInfo.InvariantCulture);
+ builder.Append("#EXT-X-STREAM-INF:BANDWIDTH=")
+ .Append(bitrate.ToString(CultureInfo.InvariantCulture))
+ .Append(",AVERAGE-BANDWIDTH=")
+ .Append(bitrate.ToString(CultureInfo.InvariantCulture));
// tvos wants resolution, codecs, framerate
//if (state.TargetFramerate.HasValue)
@@ -734,10 +737,12 @@ namespace MediaBrowser.Api.Playback.Hls
if (!string.IsNullOrWhiteSpace(subtitleGroup))
{
- header += string.Format(",SUBTITLES=\"{0}\"", subtitleGroup);
+ builder.Append(",SUBTITLES=\"")
+ .Append(subtitleGroup)
+ .Append('"');
}
- builder.AppendLine(header);
+ builder.Append(Environment.NewLine);
builder.AppendLine(url);
}
From f2858878d166df214aee20f2dc0710b766285c91 Mon Sep 17 00:00:00 2001
From: Andreas B <6439218+YouKnowBlom@users.noreply.github.com>
Date: Wed, 11 Mar 2020 18:14:36 +0100
Subject: [PATCH 2/5] Add CODECS field to HLS master playlist
---
.../Playback/Hls/DynamicHlsService.cs | 110 +++++++++++++++
.../Playback/Hls/HlsCodecStringFactory.cs | 126 ++++++++++++++++++
2 files changed, 236 insertions(+)
create mode 100644 MediaBrowser.Api/Playback/Hls/HlsCodecStringFactory.cs
diff --git a/MediaBrowser.Api/Playback/Hls/DynamicHlsService.cs b/MediaBrowser.Api/Playback/Hls/DynamicHlsService.cs
index 8787eb2a3f..e6c9213912 100644
--- a/MediaBrowser.Api/Playback/Hls/DynamicHlsService.cs
+++ b/MediaBrowser.Api/Playback/Hls/DynamicHlsService.cs
@@ -722,6 +722,114 @@ namespace MediaBrowser.Api.Playback.Hls
//return state.VideoRequest.VideoBitRate.HasValue;
}
+ ///
+ /// Gets a formatted string of the output audio codec, for use in the CODECS field.
+ ///
+ ///
+ ///
+ /// StreamState of the current stream.
+ /// Formatted audio codec string.
+ private string GetPlaylistAudioCodecs(StreamState state)
+ {
+
+ if (string.Equals(state.ActualOutputAudioCodec, "aac", StringComparison.OrdinalIgnoreCase))
+ {
+ string profile = state.GetRequestedProfiles("aac").FirstOrDefault();
+
+ return HlsCodecStringFactory.GetAACString(profile);
+ }
+ else if (string.Equals(state.ActualOutputAudioCodec, "mp3", StringComparison.OrdinalIgnoreCase))
+ {
+ return HlsCodecStringFactory.GetMP3String();
+ }
+ else if (string.Equals(state.ActualOutputAudioCodec, "ac3", StringComparison.OrdinalIgnoreCase))
+ {
+ return HlsCodecStringFactory.GetAC3String();
+ }
+ else if (string.Equals(state.ActualOutputAudioCodec, "eac3", StringComparison.OrdinalIgnoreCase))
+ {
+ return HlsCodecStringFactory.GetEAC3String();
+ }
+
+ return string.Empty;
+ }
+
+ ///
+ /// Gets a formatted string of the output video codec, for use in the CODECS field.
+ ///
+ ///
+ ///
+ /// StreamState of the current stream.
+ /// Formatted video codec string.
+ private string GetPlaylistVideoCodecs(StreamState state)
+ {
+ int level = Convert.ToInt32(state.GetRequestedLevel(state.ActualOutputVideoCodec));
+
+ if (string.Equals(state.ActualOutputVideoCodec, "h264", StringComparison.OrdinalIgnoreCase))
+ {
+ string profile = state.GetRequestedProfiles("h264").FirstOrDefault();
+
+ return HlsCodecStringFactory.GetH264String(profile, level);
+ }
+ else if (string.Equals(state.ActualOutputVideoCodec, "h265", StringComparison.OrdinalIgnoreCase)
+ || string.Equals(state.ActualOutputVideoCodec, "hevc", StringComparison.OrdinalIgnoreCase))
+ {
+ string profile = state.GetRequestedProfiles("h265").FirstOrDefault();
+
+ return HlsCodecStringFactory.GetH265String(profile, level);
+ }
+
+ return string.Empty;
+ }
+
+ ///
+ /// Appends a CODECS field containing formatted strings of
+ /// the active streams output video and audio codecs.
+ ///
+ ///
+ ///
+ ///
+ /// StringBuilder to append the field to.
+ /// StreamState of the current stream.
+ private void AppendPlaylistCodecsField(StringBuilder builder, StreamState state)
+ {
+ // Video
+ string videoCodecs = string.Empty;
+ if (!string.IsNullOrEmpty(state.ActualOutputVideoCodec))
+ {
+ videoCodecs = GetPlaylistVideoCodecs(state);
+ }
+
+ // Audio
+ string audioCodecs = string.Empty;
+ if (!string.IsNullOrEmpty(state.ActualOutputAudioCodec))
+ {
+ audioCodecs = GetPlaylistAudioCodecs(state);
+ }
+
+ if (!string.IsNullOrEmpty(videoCodecs) || !string.IsNullOrEmpty(audioCodecs))
+ {
+ builder.Append(",CODECS=\"");
+
+ if (!string.IsNullOrEmpty(videoCodecs) && !string.IsNullOrEmpty(audioCodecs))
+ {
+ builder.Append(videoCodecs)
+ .Append(',')
+ .Append(audioCodecs);
+ }
+ else if (!string.IsNullOrEmpty(videoCodecs))
+ {
+ builder.Append(videoCodecs);
+ }
+ else if (!string.IsNullOrEmpty(audioCodecs))
+ {
+ builder.Append(audioCodecs);
+ }
+
+ builder.Append('"');
+ }
+ }
+
private void AppendPlaylist(StringBuilder builder, StreamState state, string url, int bitrate, string subtitleGroup)
{
builder.Append("#EXT-X-STREAM-INF:BANDWIDTH=")
@@ -735,6 +843,8 @@ namespace MediaBrowser.Api.Playback.Hls
// header += string.Format(",FRAME-RATE=\"{0}\"", state.TargetFramerate.Value.ToString(CultureInfo.InvariantCulture));
//}
+ AppendPlaylistCodecsField(builder, state);
+
if (!string.IsNullOrWhiteSpace(subtitleGroup))
{
builder.Append(",SUBTITLES=\"")
diff --git a/MediaBrowser.Api/Playback/Hls/HlsCodecStringFactory.cs b/MediaBrowser.Api/Playback/Hls/HlsCodecStringFactory.cs
new file mode 100644
index 0000000000..3bbb77a65e
--- /dev/null
+++ b/MediaBrowser.Api/Playback/Hls/HlsCodecStringFactory.cs
@@ -0,0 +1,126 @@
+using System;
+using System.Text;
+
+
+namespace MediaBrowser.Api.Playback
+{
+ ///
+ /// Get various codec strings for use in HLS playlists.
+ ///
+ static class HlsCodecStringFactory
+ {
+
+ ///
+ /// Gets a MP3 codec string.
+ ///
+ /// MP3 codec string.
+ public static string GetMP3String()
+ {
+ return "mp4a.40.34";
+ }
+
+ ///
+ /// Gets an AAC codec string.
+ ///
+ /// AAC profile.
+ /// AAC codec string.
+ public static string GetAACString(string profile)
+ {
+ StringBuilder result = new StringBuilder("mp4a", 9);
+
+ if (string.Equals(profile, "HE", StringComparison.OrdinalIgnoreCase))
+ {
+ result.Append(".40.5");
+ }
+ else
+ {
+ // Default to LC if profile is invalid
+ result.Append(".40.2");
+ }
+
+ return result.ToString();
+ }
+
+ ///
+ /// Gets a H.264 codec string.
+ ///
+ /// H.264 profile.
+ /// H.264 level.
+ /// H.264 string.
+ public static string GetH264String(string profile, int level)
+ {
+ StringBuilder result = new StringBuilder("avc1", 11);
+
+ if (string.Equals(profile, "high", StringComparison.OrdinalIgnoreCase))
+ {
+ result.Append(".6400");
+ }
+ else if (string.Equals(profile, "main", StringComparison.OrdinalIgnoreCase))
+ {
+ result.Append(".4D40");
+ }
+ else if (string.Equals(profile, "baseline", StringComparison.OrdinalIgnoreCase))
+ {
+ result.Append(".42E0");
+ }
+ else
+ {
+ // Default to constrained baseline if profile is invalid
+ result.Append(".4240");
+ }
+
+ string levelHex = level.ToString("X2");
+ result.Append(levelHex);
+
+ return result.ToString();
+ }
+
+ ///
+ /// Gets a H.265 codec string.
+ ///
+ /// H.265 profile.
+ /// H.265 level.
+ /// H.265 string.
+ public static string GetH265String(string profile, int level)
+ {
+ // The h265 syntax is a bit of a mystery at the time this comment was written.
+ // This is what I've found through various sources:
+ // FORMAT: [codecTag].[profile].[constraint?].L[level * 30].[UNKNOWN]
+ StringBuilder result = new StringBuilder("hev1", 16);
+
+ if (string.Equals(profile, "main10", StringComparison.OrdinalIgnoreCase))
+ {
+ result.Append(".2.6");
+ }
+ else
+ {
+ // Default to main if profile is invalid
+ result.Append(".1.6");
+ }
+
+ result.Append(".L")
+ .Append(level * 3)
+ .Append(".B0");
+
+ return result.ToString();
+ }
+
+ ///
+ /// Gets an AC-3 codec string.
+ ///
+ /// AC-3 codec string.
+ public static string GetAC3String()
+ {
+ return "mp4a.a5";
+ }
+
+ ///
+ /// Gets an E-AC-3 codec string.
+ ///
+ /// E-AC-3 codec string.
+ public static string GetEAC3String()
+ {
+ return "mp4a.a6";
+ }
+ }
+}
From 8a990d1d95aa22840bae5c3494cb5371bcf2b4d8 Mon Sep 17 00:00:00 2001
From: Andreas B <6439218+YouKnowBlom@users.noreply.github.com>
Date: Wed, 11 Mar 2020 18:16:57 +0100
Subject: [PATCH 3/5] Add FRAME-RATE field to HLS master playlist
---
.../Playback/Hls/DynamicHlsService.cs | 28 +++++++++++++++++++
1 file changed, 28 insertions(+)
diff --git a/MediaBrowser.Api/Playback/Hls/DynamicHlsService.cs b/MediaBrowser.Api/Playback/Hls/DynamicHlsService.cs
index e6c9213912..d56b5cbff4 100644
--- a/MediaBrowser.Api/Playback/Hls/DynamicHlsService.cs
+++ b/MediaBrowser.Api/Playback/Hls/DynamicHlsService.cs
@@ -830,6 +830,32 @@ namespace MediaBrowser.Api.Playback.Hls
}
}
+ ///
+ /// Appends a FRAME-RATE field containing the framerate of the output stream.
+ ///
+ ///
+ /// StringBuilder to append the field to.
+ /// StreamState of the current stream.
+ private void AppendPlaylistFramerateField(StringBuilder builder, StreamState state)
+ {
+ double? framerate = null;
+ if (state.TargetFramerate.HasValue)
+ {
+ framerate = Math.Round(state.TargetFramerate.GetValueOrDefault(), 3);
+ }
+ else if (state.VideoStream.RealFrameRate.HasValue)
+ {
+ framerate = Math.Round(state.VideoStream.RealFrameRate.GetValueOrDefault(), 3);
+ }
+
+ if (framerate.HasValue)
+ {
+ builder.Append(",FRAME-RATE=\"")
+ .Append(framerate.Value)
+ .Append('"');
+ }
+ }
+
private void AppendPlaylist(StringBuilder builder, StreamState state, string url, int bitrate, string subtitleGroup)
{
builder.Append("#EXT-X-STREAM-INF:BANDWIDTH=")
@@ -845,6 +871,8 @@ namespace MediaBrowser.Api.Playback.Hls
AppendPlaylistCodecsField(builder, state);
+ AppendPlaylistFramerateField(builder, state);
+
if (!string.IsNullOrWhiteSpace(subtitleGroup))
{
builder.Append(",SUBTITLES=\"")
From 0a2d24aff3d2e78c97b4b2294a418e2cd4a16be1 Mon Sep 17 00:00:00 2001
From: Andreas B <6439218+YouKnowBlom@users.noreply.github.com>
Date: Sun, 15 Mar 2020 18:13:19 +0100
Subject: [PATCH 4/5] Add RESOLUTION field to HLS master playlist
---
.../Playback/Hls/DynamicHlsService.cs | 26 ++++++++++++++-----
1 file changed, 20 insertions(+), 6 deletions(-)
diff --git a/MediaBrowser.Api/Playback/Hls/DynamicHlsService.cs b/MediaBrowser.Api/Playback/Hls/DynamicHlsService.cs
index d56b5cbff4..ce25676ff5 100644
--- a/MediaBrowser.Api/Playback/Hls/DynamicHlsService.cs
+++ b/MediaBrowser.Api/Playback/Hls/DynamicHlsService.cs
@@ -856,6 +856,24 @@ namespace MediaBrowser.Api.Playback.Hls
}
}
+ ///
+ /// Appends a RESOLUTION field containing the resolution of the output stream.
+ ///
+ ///
+ /// StringBuilder to append the field to.
+ /// StreamState of the current stream.
+ private void AppendPlaylistResolutionField(StringBuilder builder, StreamState state)
+ {
+ if (state.OutputWidth.HasValue && state.OutputHeight.HasValue)
+ {
+ builder.Append(",RESOLUTION=\"")
+ .Append(state.OutputWidth.GetValueOrDefault())
+ .Append('x')
+ .Append(state.OutputHeight.GetValueOrDefault())
+ .Append('"');
+ }
+ }
+
private void AppendPlaylist(StringBuilder builder, StreamState state, string url, int bitrate, string subtitleGroup)
{
builder.Append("#EXT-X-STREAM-INF:BANDWIDTH=")
@@ -863,14 +881,10 @@ namespace MediaBrowser.Api.Playback.Hls
.Append(",AVERAGE-BANDWIDTH=")
.Append(bitrate.ToString(CultureInfo.InvariantCulture));
- // tvos wants resolution, codecs, framerate
- //if (state.TargetFramerate.HasValue)
- //{
- // header += string.Format(",FRAME-RATE=\"{0}\"", state.TargetFramerate.Value.ToString(CultureInfo.InvariantCulture));
- //}
-
AppendPlaylistCodecsField(builder, state);
+ AppendPlaylistResolutionField(builder, state);
+
AppendPlaylistFramerateField(builder, state);
if (!string.IsNullOrWhiteSpace(subtitleGroup))
From 153ea9f027d038ae86f644348eac7b43778d751d Mon Sep 17 00:00:00 2001
From: Andreas B <6439218+YouKnowBlom@users.noreply.github.com>
Date: Sat, 25 Apr 2020 15:22:09 +0200
Subject: [PATCH 5/5] Fix error in HLS codecs field when level is null
---
.../Playback/Hls/DynamicHlsService.cs | 78 ++++++++++++-------
1 file changed, 51 insertions(+), 27 deletions(-)
diff --git a/MediaBrowser.Api/Playback/Hls/DynamicHlsService.cs b/MediaBrowser.Api/Playback/Hls/DynamicHlsService.cs
index ce25676ff5..9071ef18fd 100644
--- a/MediaBrowser.Api/Playback/Hls/DynamicHlsService.cs
+++ b/MediaBrowser.Api/Playback/Hls/DynamicHlsService.cs
@@ -722,11 +722,37 @@ namespace MediaBrowser.Api.Playback.Hls
//return state.VideoRequest.VideoBitRate.HasValue;
}
+ ///
+ /// Get the H.26X level of the output video stream.
+ ///
+ /// StreamState of the current stream.
+ /// H.26X level of the output video stream.
+ private int? GetOutputVideoCodecLevel(StreamState state)
+ {
+ string levelString;
+ if (string.Equals(state.OutputVideoCodec, "copy", StringComparison.OrdinalIgnoreCase)
+ && state.VideoStream.Level.HasValue)
+ {
+ levelString = state.VideoStream?.Level.ToString();
+ }
+ else
+ {
+ levelString = state.GetRequestedLevel(state.ActualOutputVideoCodec);
+ }
+
+ if (int.TryParse(levelString, NumberStyles.Integer, CultureInfo.InvariantCulture, out var parsedLevel))
+ {
+ return parsedLevel;
+ }
+
+ return null;
+ }
+
///
/// Gets a formatted string of the output audio codec, for use in the CODECS field.
///
///
- ///
+ ///
/// StreamState of the current stream.
/// Formatted audio codec string.
private string GetPlaylistAudioCodecs(StreamState state)
@@ -761,18 +787,24 @@ namespace MediaBrowser.Api.Playback.Hls
///
/// StreamState of the current stream.
/// Formatted video codec string.
- private string GetPlaylistVideoCodecs(StreamState state)
+ private string GetPlaylistVideoCodecs(StreamState state, string codec, int level)
{
- int level = Convert.ToInt32(state.GetRequestedLevel(state.ActualOutputVideoCodec));
+ if (level == 0)
+ {
+ // This is 0 when there's no requested H.26X level in the device profile
+ // and the source is not encoded in H.26X
+ Logger.LogError("Got invalid H.26X level when building CODECS field for HLS master playlist");
+ return string.Empty;
+ }
- if (string.Equals(state.ActualOutputVideoCodec, "h264", StringComparison.OrdinalIgnoreCase))
+ if (string.Equals(codec, "h264", StringComparison.OrdinalIgnoreCase))
{
string profile = state.GetRequestedProfiles("h264").FirstOrDefault();
return HlsCodecStringFactory.GetH264String(profile, level);
}
- else if (string.Equals(state.ActualOutputVideoCodec, "h265", StringComparison.OrdinalIgnoreCase)
- || string.Equals(state.ActualOutputVideoCodec, "hevc", StringComparison.OrdinalIgnoreCase))
+ else if (string.Equals(codec, "h265", StringComparison.OrdinalIgnoreCase)
+ || string.Equals(codec, "hevc", StringComparison.OrdinalIgnoreCase))
{
string profile = state.GetRequestedProfiles("h265").FirstOrDefault();
@@ -787,7 +819,7 @@ namespace MediaBrowser.Api.Playback.Hls
/// the active streams output video and audio codecs.
///
///
- ///
+ ///
///
/// StringBuilder to append the field to.
/// StreamState of the current stream.
@@ -795,9 +827,10 @@ namespace MediaBrowser.Api.Playback.Hls
{
// Video
string videoCodecs = string.Empty;
- if (!string.IsNullOrEmpty(state.ActualOutputVideoCodec))
+ int? videoCodecLevel = GetOutputVideoCodecLevel(state);
+ if (!string.IsNullOrEmpty(state.ActualOutputVideoCodec) && videoCodecLevel.HasValue)
{
- videoCodecs = GetPlaylistVideoCodecs(state);
+ videoCodecs = GetPlaylistVideoCodecs(state, state.ActualOutputVideoCodec, videoCodecLevel.Value);
}
// Audio
@@ -807,26 +840,17 @@ namespace MediaBrowser.Api.Playback.Hls
audioCodecs = GetPlaylistAudioCodecs(state);
}
- if (!string.IsNullOrEmpty(videoCodecs) || !string.IsNullOrEmpty(audioCodecs))
- {
- builder.Append(",CODECS=\"");
+ StringBuilder codecs = new StringBuilder();
- if (!string.IsNullOrEmpty(videoCodecs) && !string.IsNullOrEmpty(audioCodecs))
- {
- builder.Append(videoCodecs)
- .Append(',')
- .Append(audioCodecs);
- }
- else if (!string.IsNullOrEmpty(videoCodecs))
- {
- builder.Append(videoCodecs);
- }
- else if (!string.IsNullOrEmpty(audioCodecs))
- {
- builder.Append(audioCodecs);
- }
+ codecs.Append(videoCodecs)
+ .Append(',')
+ .Append(audioCodecs);
- builder.Append('"');
+ if (codecs.Length > 1)
+ {
+ builder.Append(",CODECS=\"")
+ .Append(codecs)
+ .Append('"');
}
}