added dual layer hls playlist
This commit is contained in:
parent
528100ab31
commit
3c98f62389
|
@ -65,7 +65,7 @@ namespace MediaBrowser.Api.Playback.Hls
|
||||||
/// <returns>System.Object.</returns>
|
/// <returns>System.Object.</returns>
|
||||||
public object Get(GetHlsAudioSegment request)
|
public object Get(GetHlsAudioSegment request)
|
||||||
{
|
{
|
||||||
var file = SegmentFilePrefix + request.SegmentId + Path.GetExtension(RequestContext.PathInfo);
|
var file = request.SegmentId + Path.GetExtension(RequestContext.PathInfo);
|
||||||
|
|
||||||
file = Path.Combine(ApplicationPaths.EncodedMediaCachePath, file);
|
file = Path.Combine(ApplicationPaths.EncodedMediaCachePath, file);
|
||||||
|
|
||||||
|
|
|
@ -3,12 +3,15 @@ using MediaBrowser.Common.IO;
|
||||||
using MediaBrowser.Common.MediaInfo;
|
using MediaBrowser.Common.MediaInfo;
|
||||||
using MediaBrowser.Common.Net;
|
using MediaBrowser.Common.Net;
|
||||||
using MediaBrowser.Controller;
|
using MediaBrowser.Controller;
|
||||||
|
using MediaBrowser.Controller.Entities;
|
||||||
using MediaBrowser.Controller.Library;
|
using MediaBrowser.Controller.Library;
|
||||||
|
using MediaBrowser.Model.IO;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using MediaBrowser.Model.IO;
|
|
||||||
|
|
||||||
namespace MediaBrowser.Api.Playback.Hls
|
namespace MediaBrowser.Api.Playback.Hls
|
||||||
{
|
{
|
||||||
|
@ -17,18 +20,13 @@ namespace MediaBrowser.Api.Playback.Hls
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public abstract class BaseHlsService : BaseStreamingService
|
public abstract class BaseHlsService : BaseStreamingService
|
||||||
{
|
{
|
||||||
/// <summary>
|
|
||||||
/// The segment file prefix
|
|
||||||
/// </summary>
|
|
||||||
public const string SegmentFilePrefix = "hls-";
|
|
||||||
|
|
||||||
protected override string GetOutputFilePath(StreamState state)
|
protected override string GetOutputFilePath(StreamState state)
|
||||||
{
|
{
|
||||||
var folder = ApplicationPaths.EncodedMediaCachePath;
|
var folder = ApplicationPaths.EncodedMediaCachePath;
|
||||||
|
|
||||||
var outputFileExtension = GetOutputFileExtension(state);
|
var outputFileExtension = GetOutputFileExtension(state);
|
||||||
|
|
||||||
return Path.Combine(folder, SegmentFilePrefix + GetCommandLineArguments("dummy\\dummy", state, false).GetMD5() + (outputFileExtension ?? string.Empty).ToLower());
|
return Path.Combine(folder, GetCommandLineArguments("dummy\\dummy", state, false).GetMD5() + (outputFileExtension ?? string.Empty).ToLower());
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -107,8 +105,12 @@ namespace MediaBrowser.Api.Playback.Hls
|
||||||
ApiEntryPoint.Instance.OnTranscodeBeginRequest(playlist, TranscodingJobType.Hls);
|
ApiEntryPoint.Instance.OnTranscodeBeginRequest(playlist, TranscodingJobType.Hls);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get the current playlist text and convert to bytes
|
if (isPlaylistNewlyCreated)
|
||||||
var playlistText = await GetPlaylistFileText(playlist, isPlaylistNewlyCreated).ConfigureAwait(false);
|
{
|
||||||
|
await WaitForMinimumSegmentCount(playlist, 3).ConfigureAwait(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
var playlistText = GetMasterPlaylistFileText(playlist, state.VideoRequest.VideoBitRate.Value);
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
@ -120,18 +122,31 @@ namespace MediaBrowser.Api.Playback.Hls
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
private string GetMasterPlaylistFileText(string firstPlaylist, int bitrate)
|
||||||
/// Gets the current playlist text
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="playlist">The path to the playlist</param>
|
|
||||||
/// <param name="waitForMinimumSegments">Whether or not we should wait until it contains three segments</param>
|
|
||||||
/// <returns>Task{System.String}.</returns>
|
|
||||||
private async Task<string> GetPlaylistFileText(string playlist, bool waitForMinimumSegments)
|
|
||||||
{
|
{
|
||||||
string fileText;
|
var builder = new StringBuilder();
|
||||||
|
|
||||||
|
builder.AppendLine("#EXTM3U");
|
||||||
|
|
||||||
|
// Main stream
|
||||||
|
builder.AppendLine("#EXT-X-STREAM-INF:PROGRAM-ID=1, BANDWIDTH=" + bitrate.ToString(UsCulture));
|
||||||
|
var playlistUrl = "hls/" + Path.GetFileName(firstPlaylist).Replace(".m3u8", "/stream.m3u8");
|
||||||
|
builder.AppendLine(playlistUrl);
|
||||||
|
|
||||||
|
// Low bitrate stream
|
||||||
|
//builder.AppendLine("#EXT-X-STREAM-INF:PROGRAM-ID=1, BANDWIDTH=64000");
|
||||||
|
//playlistUrl = "hls/" + Path.GetFileName(firstPlaylist).Replace(".m3u8", "-low/stream.m3u8");
|
||||||
|
//builder.AppendLine(playlistUrl);
|
||||||
|
|
||||||
|
return builder.ToString();
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task WaitForMinimumSegmentCount(string playlist, int segmentCount)
|
||||||
|
{
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
|
string fileText;
|
||||||
|
|
||||||
// Need to use FileShare.ReadWrite because we're reading the file at the same time it's being written
|
// Need to use FileShare.ReadWrite because we're reading the file at the same time it's being written
|
||||||
using (var fileStream = new FileStream(playlist, FileMode.Open, FileAccess.Read, FileShare.ReadWrite, StreamDefaults.DefaultFileStreamBufferSize, FileOptions.Asynchronous))
|
using (var fileStream = new FileStream(playlist, FileMode.Open, FileAccess.Read, FileShare.ReadWrite, StreamDefaults.DefaultFileStreamBufferSize, FileOptions.Asynchronous))
|
||||||
{
|
{
|
||||||
|
@ -141,17 +156,13 @@ namespace MediaBrowser.Api.Playback.Hls
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!waitForMinimumSegments || CountStringOccurrences(fileText, "#EXTINF:") >= 3)
|
if (CountStringOccurrences(fileText, "#EXTINF:") >= segmentCount)
|
||||||
{
|
{
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
await Task.Delay(25).ConfigureAwait(false);
|
await Task.Delay(25).ConfigureAwait(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
fileText = fileText.Replace(SegmentFilePrefix, "hls/").Replace(".ts", "/stream.ts").Replace(".aac", "/stream.aac").Replace(".mp3", "/stream.mp3");
|
|
||||||
|
|
||||||
return fileText;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -173,6 +184,27 @@ namespace MediaBrowser.Api.Playback.Hls
|
||||||
return count;
|
return count;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected void ExtendHlsTimer(string itemId, string playlistId)
|
||||||
|
{
|
||||||
|
foreach (var playlist in Directory.EnumerateFiles(ApplicationPaths.EncodedMediaCachePath, "*.m3u8")
|
||||||
|
.Where(i => i.IndexOf(playlistId, StringComparison.OrdinalIgnoreCase) != -1)
|
||||||
|
.ToList())
|
||||||
|
{
|
||||||
|
ApiEntryPoint.Instance.OnTranscodeBeginRequest(playlist, TranscodingJobType.Hls);
|
||||||
|
|
||||||
|
// Avoid implicitly captured closure
|
||||||
|
var playlist1 = playlist;
|
||||||
|
|
||||||
|
Task.Run(async () =>
|
||||||
|
{
|
||||||
|
// This is an arbitrary time period corresponding to when the request completes.
|
||||||
|
await Task.Delay(30000).ConfigureAwait(false);
|
||||||
|
|
||||||
|
ApiEntryPoint.Instance.OnTranscodeEndRequest(playlist1, TranscodingJobType.Hls);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the command line arguments.
|
/// Gets the command line arguments.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -184,10 +216,7 @@ namespace MediaBrowser.Api.Playback.Hls
|
||||||
{
|
{
|
||||||
var probeSize = GetProbeSizeArgument(state.Item);
|
var probeSize = GetProbeSizeArgument(state.Item);
|
||||||
|
|
||||||
var audioOnlyPlaylistParams = string.Format(" -threads 0 -vn -codec:a:0 aac -strict experimental -ac 2 -ab 64000 -hls_time 10 -start_number 0 -hls_list_size 1440 \"{0}\"",
|
var args = string.Format("{0} {1} {2} -i {3}{4} -threads 0 {5} {6} {7} -hls_time 10 -start_number 0 -hls_list_size 1440 \"{8}\"",
|
||||||
"");
|
|
||||||
|
|
||||||
return string.Format("{0} {1} {2} -i {3}{4} -threads 0 {5} {6} {7} -hls_time 10 -start_number 0 -hls_list_size 1440 \"{8}\" {9}",
|
|
||||||
probeSize,
|
probeSize,
|
||||||
GetUserAgentParam(state.Item),
|
GetUserAgentParam(state.Item),
|
||||||
GetFastSeekCommandLineParameter(state.Request),
|
GetFastSeekCommandLineParameter(state.Request),
|
||||||
|
@ -196,9 +225,20 @@ namespace MediaBrowser.Api.Playback.Hls
|
||||||
GetMapArgs(state),
|
GetMapArgs(state),
|
||||||
GetVideoArguments(state, performSubtitleConversions),
|
GetVideoArguments(state, performSubtitleConversions),
|
||||||
GetAudioArguments(state),
|
GetAudioArguments(state),
|
||||||
outputPath,
|
outputPath
|
||||||
audioOnlyPlaylistParams
|
|
||||||
).Trim();
|
).Trim();
|
||||||
|
|
||||||
|
if (state.Item is Video)
|
||||||
|
{
|
||||||
|
var lowBitratePath = Path.Combine(Path.GetDirectoryName(outputPath), Path.GetFileNameWithoutExtension(outputPath) + "-low.m3u8");
|
||||||
|
|
||||||
|
var lowBitrateParams = string.Format(" -threads 0 -vn -codec:a:0 aac -strict experimental -ac 2 -ab 64000 -hls_time 10 -start_number 0 -hls_list_size 1440 \"{0}\"",
|
||||||
|
lowBitratePath);
|
||||||
|
|
||||||
|
args += " " + lowBitrateParams;
|
||||||
|
}
|
||||||
|
|
||||||
|
return args;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,8 +5,6 @@ using MediaBrowser.Model.IO;
|
||||||
using ServiceStack.ServiceHost;
|
using ServiceStack.ServiceHost;
|
||||||
using System;
|
using System;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
namespace MediaBrowser.Api.Playback.Hls
|
namespace MediaBrowser.Api.Playback.Hls
|
||||||
{
|
{
|
||||||
|
@ -23,7 +21,7 @@ namespace MediaBrowser.Api.Playback.Hls
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Class GetHlsVideoSegment
|
/// Class GetHlsVideoSegment
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[Route("/Videos/{Id}/hls/{SegmentId}/stream.ts", "GET")]
|
[Route("/Videos/{Id}/hls/{PlaylistId}/{SegmentId}.ts", "GET")]
|
||||||
[Api(Description = "Gets an Http live streaming segment file. Internal use only.")]
|
[Api(Description = "Gets an Http live streaming segment file. Internal use only.")]
|
||||||
public class GetHlsVideoSegment
|
public class GetHlsVideoSegment
|
||||||
{
|
{
|
||||||
|
@ -33,6 +31,8 @@ namespace MediaBrowser.Api.Playback.Hls
|
||||||
/// <value>The id.</value>
|
/// <value>The id.</value>
|
||||||
public string Id { get; set; }
|
public string Id { get; set; }
|
||||||
|
|
||||||
|
public string PlaylistId { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets or sets the segment id.
|
/// Gets or sets the segment id.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -40,6 +40,22 @@ namespace MediaBrowser.Api.Playback.Hls
|
||||||
public string SegmentId { get; set; }
|
public string SegmentId { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Class GetHlsVideoSegment
|
||||||
|
/// </summary>
|
||||||
|
[Route("/Videos/{Id}/hls/{PlaylistId}/stream.m3u8", "GET")]
|
||||||
|
[Api(Description = "Gets an Http live streaming segment file. Internal use only.")]
|
||||||
|
public class GetHlsPlaylist
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Gets or sets the id.
|
||||||
|
/// </summary>
|
||||||
|
/// <value>The id.</value>
|
||||||
|
public string Id { get; set; }
|
||||||
|
|
||||||
|
public string PlaylistId { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Class VideoHlsService
|
/// Class VideoHlsService
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -65,23 +81,20 @@ namespace MediaBrowser.Api.Playback.Hls
|
||||||
/// <returns>System.Object.</returns>
|
/// <returns>System.Object.</returns>
|
||||||
public object Get(GetHlsVideoSegment request)
|
public object Get(GetHlsVideoSegment request)
|
||||||
{
|
{
|
||||||
foreach (var playlist in Directory.EnumerateFiles(ApplicationPaths.EncodedMediaCachePath, "*.m3u8").ToList())
|
ExtendHlsTimer(request.Id, request.PlaylistId);
|
||||||
{
|
|
||||||
ApiEntryPoint.Instance.OnTranscodeBeginRequest(playlist, TranscodingJobType.Hls);
|
|
||||||
|
|
||||||
// Avoid implicitly captured closure
|
var file = request.SegmentId + Path.GetExtension(RequestContext.PathInfo);
|
||||||
var playlist1 = playlist;
|
|
||||||
|
|
||||||
Task.Run(async () =>
|
file = Path.Combine(ApplicationPaths.EncodedMediaCachePath, file);
|
||||||
{
|
|
||||||
// This is an arbitrary time period corresponding to when the request completes.
|
|
||||||
await Task.Delay(30000).ConfigureAwait(false);
|
|
||||||
|
|
||||||
ApiEntryPoint.Instance.OnTranscodeEndRequest(playlist1, TranscodingJobType.Hls);
|
return ResultFactory.GetStaticFileResult(RequestContext, file);
|
||||||
});
|
}
|
||||||
}
|
|
||||||
|
|
||||||
var file = SegmentFilePrefix + request.SegmentId + Path.GetExtension(RequestContext.PathInfo);
|
public object Get(GetHlsPlaylist request)
|
||||||
|
{
|
||||||
|
ExtendHlsTimer(request.Id, request.PlaylistId);
|
||||||
|
|
||||||
|
var file = request.PlaylistId + Path.GetExtension(RequestContext.PathInfo);
|
||||||
|
|
||||||
file = Path.Combine(ApplicationPaths.EncodedMediaCachePath, file);
|
file = Path.Combine(ApplicationPaths.EncodedMediaCachePath, file);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user