2013-06-18 10:36:25 +00:00
|
|
|
|
using MediaBrowser.Common.Extensions;
|
|
|
|
|
using MediaBrowser.Common.IO;
|
2013-04-07 20:55:05 +00:00
|
|
|
|
using MediaBrowser.Common.MediaInfo;
|
2013-02-21 01:33:05 +00:00
|
|
|
|
using MediaBrowser.Common.Net;
|
2013-02-27 23:33:40 +00:00
|
|
|
|
using MediaBrowser.Controller;
|
2013-08-28 22:05:00 +00:00
|
|
|
|
using MediaBrowser.Controller.Entities;
|
2013-02-27 23:33:40 +00:00
|
|
|
|
using MediaBrowser.Controller.Library;
|
2013-08-28 22:05:00 +00:00
|
|
|
|
using MediaBrowser.Model.IO;
|
2013-02-21 01:33:05 +00:00
|
|
|
|
using System;
|
2013-04-07 20:55:05 +00:00
|
|
|
|
using System.Collections.Generic;
|
2013-02-21 01:33:05 +00:00
|
|
|
|
using System.IO;
|
2013-08-28 22:05:00 +00:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
2013-02-21 01:33:05 +00:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
2013-02-27 23:33:40 +00:00
|
|
|
|
namespace MediaBrowser.Api.Playback.Hls
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
2013-04-29 16:01:23 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Class BaseHlsService
|
|
|
|
|
/// </summary>
|
2013-02-27 23:33:40 +00:00
|
|
|
|
public abstract class BaseHlsService : BaseStreamingService
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
2013-06-18 10:36:25 +00:00
|
|
|
|
protected override string GetOutputFilePath(StreamState state)
|
|
|
|
|
{
|
|
|
|
|
var folder = ApplicationPaths.EncodedMediaCachePath;
|
|
|
|
|
|
|
|
|
|
var outputFileExtension = GetOutputFileExtension(state);
|
|
|
|
|
|
2013-08-28 22:05:00 +00:00
|
|
|
|
return Path.Combine(folder, GetCommandLineArguments("dummy\\dummy", state, false).GetMD5() + (outputFileExtension ?? string.Empty).ToLower());
|
2013-06-18 10:36:25 +00:00
|
|
|
|
}
|
2013-02-28 19:32:41 +00:00
|
|
|
|
|
2013-04-29 16:01:23 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Initializes a new instance of the <see cref="BaseStreamingService" /> class.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="appPaths">The app paths.</param>
|
|
|
|
|
/// <param name="userManager">The user manager.</param>
|
|
|
|
|
/// <param name="libraryManager">The library manager.</param>
|
|
|
|
|
/// <param name="isoManager">The iso manager.</param>
|
|
|
|
|
/// <param name="mediaEncoder">The media encoder.</param>
|
2013-06-18 10:36:25 +00:00
|
|
|
|
protected BaseHlsService(IServerApplicationPaths appPaths, IUserManager userManager, ILibraryManager libraryManager, IIsoManager isoManager, IMediaEncoder mediaEncoder)
|
2013-04-07 20:55:05 +00:00
|
|
|
|
: base(appPaths, userManager, libraryManager, isoManager, mediaEncoder)
|
2013-02-27 23:33:40 +00:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-21 01:33:05 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the audio arguments.
|
|
|
|
|
/// </summary>
|
2013-04-29 16:01:23 +00:00
|
|
|
|
/// <param name="state">The state.</param>
|
2013-02-21 01:33:05 +00:00
|
|
|
|
/// <returns>System.String.</returns>
|
2013-02-27 23:33:40 +00:00
|
|
|
|
protected abstract string GetAudioArguments(StreamState state);
|
2013-02-21 01:33:05 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the video arguments.
|
|
|
|
|
/// </summary>
|
2013-04-29 16:01:23 +00:00
|
|
|
|
/// <param name="state">The state.</param>
|
|
|
|
|
/// <param name="performSubtitleConversion">if set to <c>true</c> [perform subtitle conversion].</param>
|
2013-02-21 01:33:05 +00:00
|
|
|
|
/// <returns>System.String.</returns>
|
2013-04-29 16:01:23 +00:00
|
|
|
|
protected abstract string GetVideoArguments(StreamState state, bool performSubtitleConversion);
|
2013-02-21 01:33:05 +00:00
|
|
|
|
|
2013-02-27 23:33:40 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the segment file extension.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="state">The state.</param>
|
|
|
|
|
/// <returns>System.String.</returns>
|
|
|
|
|
protected abstract string GetSegmentFileExtension(StreamState state);
|
2013-04-29 16:01:23 +00:00
|
|
|
|
|
2013-02-21 01:33:05 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the type of the transcoding job.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value>The type of the transcoding job.</value>
|
|
|
|
|
protected override TranscodingJobType TranscodingJobType
|
|
|
|
|
{
|
|
|
|
|
get { return TranscodingJobType.Hls; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2013-02-27 23:33:40 +00:00
|
|
|
|
/// Processes the request.
|
2013-02-21 01:33:05 +00:00
|
|
|
|
/// </summary>
|
2013-04-29 16:01:23 +00:00
|
|
|
|
/// <param name="request">The request.</param>
|
2013-02-27 23:33:40 +00:00
|
|
|
|
/// <returns>System.Object.</returns>
|
2013-03-09 06:05:52 +00:00
|
|
|
|
protected object ProcessRequest(StreamRequest request)
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
2013-03-09 06:05:52 +00:00
|
|
|
|
var state = GetState(request);
|
2013-06-18 10:36:25 +00:00
|
|
|
|
|
2013-02-27 23:33:40 +00:00
|
|
|
|
return ProcessRequestAsync(state).Result;
|
2013-02-21 01:33:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2013-02-27 23:33:40 +00:00
|
|
|
|
/// Processes the request async.
|
2013-02-21 01:33:05 +00:00
|
|
|
|
/// </summary>
|
2013-02-27 23:33:40 +00:00
|
|
|
|
/// <param name="state">The state.</param>
|
|
|
|
|
/// <returns>Task{System.Object}.</returns>
|
|
|
|
|
public async Task<object> ProcessRequestAsync(StreamState state)
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
2013-08-30 01:25:37 +00:00
|
|
|
|
if (!state.VideoRequest.VideoBitRate.HasValue)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentException("A video bitrate is required");
|
|
|
|
|
}
|
|
|
|
|
if (!state.Request.AudioBitRate.HasValue)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentException("An audio bitrate is required");
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-27 23:33:40 +00:00
|
|
|
|
var playlist = GetOutputFilePath(state);
|
2013-02-21 01:33:05 +00:00
|
|
|
|
var isPlaylistNewlyCreated = false;
|
|
|
|
|
|
|
|
|
|
// If the playlist doesn't already exist, startup ffmpeg
|
|
|
|
|
if (!File.Exists(playlist))
|
|
|
|
|
{
|
|
|
|
|
isPlaylistNewlyCreated = true;
|
2013-04-10 15:32:09 +00:00
|
|
|
|
await StartFfMpeg(state, playlist).ConfigureAwait(false);
|
2013-02-21 01:33:05 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2013-03-08 19:14:09 +00:00
|
|
|
|
ApiEntryPoint.Instance.OnTranscodeBeginRequest(playlist, TranscodingJobType.Hls);
|
2013-02-21 01:33:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-08-28 22:05:00 +00:00
|
|
|
|
if (isPlaylistNewlyCreated)
|
|
|
|
|
{
|
|
|
|
|
await WaitForMinimumSegmentCount(playlist, 3).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-30 02:13:58 +00:00
|
|
|
|
var audioBitrate = GetAudioBitrateParam(state) ?? 0;
|
|
|
|
|
var videoBitrate = GetVideoBitrateParam(state) ?? 0;
|
|
|
|
|
|
|
|
|
|
var playlistText = GetMasterPlaylistFileText(playlist, videoBitrate + audioBitrate);
|
2013-02-21 01:33:05 +00:00
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2013-03-24 02:45:00 +00:00
|
|
|
|
return ResultFactory.GetResult(playlistText, MimeTypes.GetMimeType("playlist.m3u8"), new Dictionary<string, string>());
|
2013-02-21 01:33:05 +00:00
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
2013-03-08 19:14:09 +00:00
|
|
|
|
ApiEntryPoint.Instance.OnTranscodeEndRequest(playlist, TranscodingJobType.Hls);
|
2013-02-21 01:33:05 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-28 22:05:00 +00:00
|
|
|
|
private string GetMasterPlaylistFileText(string firstPlaylist, int bitrate)
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
2013-08-28 22:05:00 +00:00
|
|
|
|
var builder = new StringBuilder();
|
|
|
|
|
|
|
|
|
|
builder.AppendLine("#EXTM3U");
|
|
|
|
|
|
|
|
|
|
// Main stream
|
2013-09-01 13:13:11 +00:00
|
|
|
|
builder.AppendLine("#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=" + bitrate.ToString(UsCulture));
|
2013-08-28 22:05:00 +00:00
|
|
|
|
var playlistUrl = "hls/" + Path.GetFileName(firstPlaylist).Replace(".m3u8", "/stream.m3u8");
|
|
|
|
|
builder.AppendLine(playlistUrl);
|
2013-02-21 01:33:05 +00:00
|
|
|
|
|
2013-08-28 22:05:00 +00:00
|
|
|
|
// Low bitrate stream
|
2013-09-02 23:16:46 +00:00
|
|
|
|
//builder.AppendLine("#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=64000");
|
2013-08-31 01:13:15 +00:00
|
|
|
|
//playlistUrl = "hls/" + Path.GetFileName(firstPlaylist).Replace(".m3u8", "-low/stream.m3u8");
|
|
|
|
|
//builder.AppendLine(playlistUrl);
|
2013-08-28 22:05:00 +00:00
|
|
|
|
|
|
|
|
|
return builder.ToString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task WaitForMinimumSegmentCount(string playlist, int segmentCount)
|
|
|
|
|
{
|
2013-02-21 01:33:05 +00:00
|
|
|
|
while (true)
|
|
|
|
|
{
|
2013-08-28 22:05:00 +00:00
|
|
|
|
string fileText;
|
|
|
|
|
|
2013-02-21 01:33:05 +00:00
|
|
|
|
// 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 reader = new StreamReader(fileStream))
|
|
|
|
|
{
|
|
|
|
|
fileText = await reader.ReadToEndAsync().ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-28 22:05:00 +00:00
|
|
|
|
if (CountStringOccurrences(fileText, "#EXTINF:") >= segmentCount)
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await Task.Delay(25).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Count occurrences of strings.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="text">The text.</param>
|
|
|
|
|
/// <param name="pattern">The pattern.</param>
|
|
|
|
|
/// <returns>System.Int32.</returns>
|
|
|
|
|
private static int CountStringOccurrences(string text, string pattern)
|
|
|
|
|
{
|
|
|
|
|
// Loop through all instances of the string 'text'.
|
|
|
|
|
var count = 0;
|
|
|
|
|
var i = 0;
|
|
|
|
|
while ((i = text.IndexOf(pattern, i, StringComparison.OrdinalIgnoreCase)) != -1)
|
|
|
|
|
{
|
|
|
|
|
i += pattern.Length;
|
|
|
|
|
count++;
|
|
|
|
|
}
|
|
|
|
|
return count;
|
|
|
|
|
}
|
2013-04-29 16:01:23 +00:00
|
|
|
|
|
2013-08-28 22:05:00 +00:00
|
|
|
|
protected void ExtendHlsTimer(string itemId, string playlistId)
|
|
|
|
|
{
|
2013-08-28 22:11:02 +00:00
|
|
|
|
var normalizedPlaylistId = playlistId.Replace("-low", string.Empty);
|
|
|
|
|
|
2013-08-28 22:05:00 +00:00
|
|
|
|
foreach (var playlist in Directory.EnumerateFiles(ApplicationPaths.EncodedMediaCachePath, "*.m3u8")
|
2013-08-28 22:11:02 +00:00
|
|
|
|
.Where(i => i.IndexOf(normalizedPlaylistId, StringComparison.OrdinalIgnoreCase) != -1)
|
2013-08-28 22:05:00 +00:00
|
|
|
|
.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);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-21 01:33:05 +00:00
|
|
|
|
/// <summary>
|
2013-02-27 23:33:40 +00:00
|
|
|
|
/// Gets the command line arguments.
|
2013-02-21 01:33:05 +00:00
|
|
|
|
/// </summary>
|
2013-02-27 23:33:40 +00:00
|
|
|
|
/// <param name="outputPath">The output path.</param>
|
|
|
|
|
/// <param name="state">The state.</param>
|
2013-04-29 16:01:23 +00:00
|
|
|
|
/// <param name="performSubtitleConversions">if set to <c>true</c> [perform subtitle conversions].</param>
|
2013-02-21 01:33:05 +00:00
|
|
|
|
/// <returns>System.String.</returns>
|
2013-04-29 16:01:23 +00:00
|
|
|
|
protected override string GetCommandLineArguments(string outputPath, StreamState state, bool performSubtitleConversions)
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
2013-04-07 20:55:05 +00:00
|
|
|
|
var probeSize = GetProbeSizeArgument(state.Item);
|
2013-03-25 14:03:37 +00:00
|
|
|
|
|
2013-09-02 20:19:11 +00:00
|
|
|
|
var args = string.Format("{0} {1} {2} -i {3}{4} -threads 0 {5} {6} -sc_threshold 0 {7} -hls_time 10 -start_number 0 -hls_list_size 1440 \"{8}\"",
|
2013-02-21 01:33:05 +00:00
|
|
|
|
probeSize,
|
2013-05-01 20:07:20 +00:00
|
|
|
|
GetUserAgentParam(state.Item),
|
2013-02-27 23:33:40 +00:00
|
|
|
|
GetFastSeekCommandLineParameter(state.Request),
|
|
|
|
|
GetInputArgument(state.Item, state.IsoMount),
|
|
|
|
|
GetSlowSeekCommandLineParameter(state.Request),
|
|
|
|
|
GetMapArgs(state),
|
2013-04-29 16:01:23 +00:00
|
|
|
|
GetVideoArguments(state, performSubtitleConversions),
|
2013-02-27 23:33:40 +00:00
|
|
|
|
GetAudioArguments(state),
|
2013-08-28 22:05:00 +00:00
|
|
|
|
outputPath
|
2013-02-21 01:33:05 +00:00
|
|
|
|
).Trim();
|
2013-08-28 22:05:00 +00:00
|
|
|
|
|
|
|
|
|
if (state.Item is Video)
|
|
|
|
|
{
|
|
|
|
|
var lowBitratePath = Path.Combine(Path.GetDirectoryName(outputPath), Path.GetFileNameWithoutExtension(outputPath) + "-low.m3u8");
|
|
|
|
|
|
2013-09-02 20:19:11 +00:00
|
|
|
|
var lowBitrateParams = string.Format(" -threads 0 -vn -codec:a:{1} libmp3lame -ac 2 -ab 32000 -hls_time 10 -start_number 0 -hls_list_size 1440 \"{0}\"",
|
2013-08-30 01:25:37 +00:00
|
|
|
|
lowBitratePath,
|
|
|
|
|
state.AudioStream.Index);
|
2013-08-28 22:05:00 +00:00
|
|
|
|
|
|
|
|
|
args += " " + lowBitrateParams;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return args;
|
2013-02-21 01:33:05 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|