using MediaBrowser.Common.IO;
using MediaBrowser.Common.Net;
using MediaBrowser.Controller;
using MediaBrowser.Controller.Library;
using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MediaBrowser.Api.Playback.Hls
{
public abstract class BaseHlsService : BaseStreamingService
{
///
/// The segment file prefix
///
public const string SegmentFilePrefix = "segment-";
protected BaseHlsService(IServerApplicationPaths appPaths, IUserManager userManager)
: base(appPaths, userManager)
{
}
///
/// Gets the audio arguments.
///
/// System.String.
protected abstract string GetAudioArguments(StreamState state);
///
/// Gets the video arguments.
///
/// System.String.
protected abstract string GetVideoArguments(StreamState state);
///
/// Gets the segment file extension.
///
/// The state.
/// System.String.
protected abstract string GetSegmentFileExtension(StreamState state);
///
/// Gets the type of the transcoding job.
///
/// The type of the transcoding job.
protected override TranscodingJobType TranscodingJobType
{
get { return TranscodingJobType.Hls; }
}
///
/// Processes the request.
///
/// The state.
/// System.Object.
protected object ProcessRequest(StreamState state)
{
return ProcessRequestAsync(state).Result;
}
///
/// Processes the request async.
///
/// The state.
/// Task{System.Object}.
public async Task ProcessRequestAsync(StreamState state)
{
var playlist = GetOutputFilePath(state);
var isPlaylistNewlyCreated = false;
// If the playlist doesn't already exist, startup ffmpeg
if (!File.Exists(playlist))
{
isPlaylistNewlyCreated = true;
await StartFFMpeg(state, playlist).ConfigureAwait(false);
}
else
{
Plugin.Instance.OnTranscodeBeginRequest(playlist, TranscodingJobType.Hls);
}
// Get the current playlist text and convert to bytes
var playlistText = await GetPlaylistFileText(playlist, isPlaylistNewlyCreated).ConfigureAwait(false);
var content = Encoding.UTF8.GetBytes(playlistText);
var stream = new MemoryStream(content);
try
{
Response.ContentType = MimeTypes.GetMimeType("playlist.m3u8");
return new StreamWriter(stream);
}
finally
{
Plugin.Instance.OnTranscodeEndRequest(playlist, TranscodingJobType.Hls);
}
}
///
/// Gets the current playlist text
///
/// The path to the playlist
/// Whether or not we should wait until it contains three segments
/// Task{System.String}.
private async Task 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);
// Even though we specify target duration of 9, ffmpeg seems unable to keep all segments under that amount
fileText = fileText.Replace("#EXT-X-TARGETDURATION:9", "#EXT-X-TARGETDURATION:10");
// 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";
// Add event type at the top
fileText = fileText.Replace("#EXT-X-ALLOWCACHE", "#EXT-X-PLAYLIST-TYPE:" + playlistType + Environment.NewLine + "#EXT-X-ALLOWCACHE");
return fileText;
}
///
/// Count occurrences of strings.
///
/// The text.
/// The pattern.
/// System.Int32.
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;
}
///
/// Gets the command line arguments.
///
/// The output path.
/// The state.
/// System.String.
protected override string GetCommandLineArguments(string outputPath, StreamState state)
{
var segmentOutputPath = Path.GetDirectoryName(outputPath);
var segmentOutputName = SegmentFilePrefix + Path.GetFileNameWithoutExtension(outputPath);
segmentOutputPath = Path.Combine(segmentOutputPath, segmentOutputName + "%03d." + GetSegmentFileExtension(state).TrimStart('.'));
var kernel = (Kernel)Kernel;
var probeSize = kernel.FFMpegManager.GetProbeSizeArgument(state.Item);
return string.Format("{0} {1} -i {2}{3} -threads 0 {4} {5} {6} -f ssegment -segment_list_flags +live -segment_time 9 -segment_list \"{7}\" \"{8}\"",
probeSize,
GetFastSeekCommandLineParameter(state.Request),
GetInputArgument(state.Item, state.IsoMount),
GetSlowSeekCommandLineParameter(state.Request),
GetMapArgs(state),
GetVideoArguments(state),
GetAudioArguments(state),
outputPath,
segmentOutputPath
).Trim();
}
///
/// Deletes the partial stream files.
///
/// The output file path.
protected override void DeletePartialStreamFiles(string outputFilePath)
{
var directory = Path.GetDirectoryName(outputFilePath);
var name = Path.GetFileNameWithoutExtension(outputFilePath);
var filesToDelete = Directory.EnumerateFiles(directory, "*", SearchOption.TopDirectoryOnly)
.Where(f => f.IndexOf(name, StringComparison.OrdinalIgnoreCase) != -1)
.ToList();
foreach (var file in filesToDelete)
{
try
{
Logger.Info("Deleting HLS file {0}", file);
File.Delete(file);
}
catch (IOException ex)
{
Logger.ErrorException("Error deleting HLS file {0}", ex, file);
}
}
}
}
}