2013-04-07 20:55:05 +00:00
|
|
|
|
using MediaBrowser.Common.IO;
|
|
|
|
|
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;
|
|
|
|
|
using MediaBrowser.Controller.Library;
|
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;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
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-02-27 23:33:40 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// The segment file prefix
|
|
|
|
|
/// </summary>
|
|
|
|
|
public const string SegmentFilePrefix = "segment-";
|
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-04-07 20:55:05 +00:00
|
|
|
|
protected BaseHlsService(IServerApplicationPaths appPaths, IUserManager userManager, ILibraryManager libraryManager, IIsoManager isoManager, IMediaEncoder mediaEncoder)
|
|
|
|
|
: 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-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-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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get the current playlist text and convert to bytes
|
|
|
|
|
var playlistText = await GetPlaylistFileText(playlist, isPlaylistNewlyCreated).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 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;
|
|
|
|
|
|
|
|
|
|
while (true)
|
|
|
|
|
{
|
|
|
|
|
// 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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!waitForMinimumSegments || CountStringOccurrences(fileText, "#EXTINF:") >= 3)
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await Task.Delay(25).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// The segement paths within the playlist are phsyical, so strip that out to make it relative
|
|
|
|
|
fileText = fileText.Replace(Path.GetDirectoryName(playlist) + Path.DirectorySeparatorChar, string.Empty);
|
|
|
|
|
|
2013-03-11 03:12:21 +00:00
|
|
|
|
fileText = fileText.Replace(SegmentFilePrefix, "segments/").Replace(".ts", "/stream.ts").Replace(".aac", "/stream.aac").Replace(".mp3", "/stream.mp3");
|
2013-03-09 06:05:52 +00:00
|
|
|
|
|
2013-02-21 01:33:05 +00:00
|
|
|
|
// It's considered live while still encoding (EVENT). Once the encoding has finished, it's video on demand (VOD).
|
|
|
|
|
var playlistType = fileText.IndexOf("#EXT-X-ENDLIST", StringComparison.OrdinalIgnoreCase) == -1 ? "EVENT" : "VOD";
|
|
|
|
|
|
2013-03-27 15:08:02 +00:00
|
|
|
|
const string allowCacheAttributeName = "#EXT-X-ALLOW-CACHE";
|
|
|
|
|
|
2013-03-27 14:54:00 +00:00
|
|
|
|
// fix this to make the media stream validator happy
|
|
|
|
|
// https://ffmpeg.org/trac/ffmpeg/ticket/2228
|
2013-03-27 15:08:02 +00:00
|
|
|
|
fileText = fileText.Replace("#EXT-X-ALLOWCACHE", allowCacheAttributeName);
|
2013-03-27 14:54:00 +00:00
|
|
|
|
|
2013-02-21 01:33:05 +00:00
|
|
|
|
// Add event type at the top
|
2013-03-27 15:08:02 +00:00
|
|
|
|
fileText = fileText.Replace(allowCacheAttributeName, "#EXT-X-PLAYLIST-TYPE:" + playlistType + Environment.NewLine + allowCacheAttributeName);
|
2013-03-27 14:54:00 +00:00
|
|
|
|
|
2013-02-21 01:33:05 +00:00
|
|
|
|
return fileText;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <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-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
|
|
|
|
{
|
|
|
|
|
var segmentOutputPath = Path.GetDirectoryName(outputPath);
|
2013-02-27 23:33:40 +00:00
|
|
|
|
var segmentOutputName = SegmentFilePrefix + Path.GetFileNameWithoutExtension(outputPath);
|
|
|
|
|
|
|
|
|
|
segmentOutputPath = Path.Combine(segmentOutputPath, segmentOutputName + "%03d." + GetSegmentFileExtension(state).TrimStart('.'));
|
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-03-28 15:22:05 +00:00
|
|
|
|
return string.Format("{0} {1} -i {2}{3} -threads 0 {4} {5} {6} -f ssegment -segment_list_flags +live -segment_time 10 -segment_list \"{7}\" \"{8}\"",
|
2013-02-21 01:33:05 +00:00
|
|
|
|
probeSize,
|
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-02-21 01:33:05 +00:00
|
|
|
|
outputPath,
|
|
|
|
|
segmentOutputPath
|
|
|
|
|
).Trim();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Deletes the partial stream files.
|
|
|
|
|
/// </summary>
|
2013-02-27 23:33:40 +00:00
|
|
|
|
/// <param name="outputFilePath">The output file path.</param>
|
|
|
|
|
protected override void DeletePartialStreamFiles(string outputFilePath)
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
2013-02-27 23:33:40 +00:00
|
|
|
|
var directory = Path.GetDirectoryName(outputFilePath);
|
|
|
|
|
var name = Path.GetFileNameWithoutExtension(outputFilePath);
|
2013-02-21 01:33:05 +00:00
|
|
|
|
|
2013-04-28 05:29:27 +00:00
|
|
|
|
var filesToDelete = Directory.EnumerateFiles(directory)
|
2013-02-21 01:33:05 +00:00
|
|
|
|
.Where(f => f.IndexOf(name, StringComparison.OrdinalIgnoreCase) != -1)
|
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
foreach (var file in filesToDelete)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2013-02-27 23:33:40 +00:00
|
|
|
|
Logger.Info("Deleting HLS file {0}", file);
|
2013-02-21 01:33:05 +00:00
|
|
|
|
File.Delete(file);
|
|
|
|
|
}
|
|
|
|
|
catch (IOException ex)
|
|
|
|
|
{
|
2013-02-27 23:33:40 +00:00
|
|
|
|
Logger.ErrorException("Error deleting HLS file {0}", ex, file);
|
2013-02-21 01:33:05 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|