jellyfin-server/MediaBrowser.Api/Playback/Hls/DynamicHlsService.cs

966 lines
36 KiB
C#
Raw Normal View History

2015-11-27 04:33:20 +00:00
using MediaBrowser.Common.Net;
2014-01-23 18:05:41 +00:00
using MediaBrowser.Controller.Configuration;
2015-03-07 22:43:53 +00:00
using MediaBrowser.Controller.Devices;
2014-03-25 05:25:03 +00:00
using MediaBrowser.Controller.Dlna;
2014-01-23 18:05:41 +00:00
using MediaBrowser.Controller.Library;
2014-02-20 16:37:41 +00:00
using MediaBrowser.Controller.MediaEncoding;
2014-09-03 02:30:24 +00:00
using MediaBrowser.Controller.Net;
using MediaBrowser.Model.Dlna;
using MediaBrowser.Model.Entities;
2015-03-07 22:43:53 +00:00
using MediaBrowser.Model.Extensions;
2014-01-23 18:05:41 +00:00
using MediaBrowser.Model.IO;
2015-05-04 17:44:25 +00:00
using MediaBrowser.Model.Serialization;
2014-01-23 18:05:41 +00:00
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
2014-06-26 17:04:11 +00:00
using System.Linq;
2014-01-23 18:05:41 +00:00
using System.Text;
using System.Threading;
using System.Threading.Tasks;
2016-10-25 19:02:04 +00:00
using MediaBrowser.Model.Services;
2014-12-26 17:45:06 +00:00
using MimeTypes = MediaBrowser.Model.Net.MimeTypes;
2014-01-23 18:05:41 +00:00
namespace MediaBrowser.Api.Playback.Hls
{
2014-08-17 05:38:13 +00:00
/// <summary>
/// Options is needed for chromecast. Threw Head in there since it's related
/// </summary>
[Route("/Videos/{Id}/master.m3u8", "GET", Summary = "Gets a video stream using HTTP live streaming.")]
2014-08-17 05:38:13 +00:00
[Route("/Videos/{Id}/master.m3u8", "HEAD", Summary = "Gets a video stream using HTTP live streaming.")]
2015-05-24 18:33:28 +00:00
public class GetMasterHlsVideoPlaylist : VideoStreamRequest, IMasterHlsRequest
2014-01-23 18:05:41 +00:00
{
2014-08-01 02:58:37 +00:00
public bool EnableAdaptiveBitrateStreaming { get; set; }
2015-05-24 18:33:28 +00:00
public GetMasterHlsVideoPlaylist()
2014-08-01 02:58:37 +00:00
{
EnableAdaptiveBitrateStreaming = true;
}
2014-01-23 18:05:41 +00:00
}
2015-05-24 18:33:28 +00:00
[Route("/Audio/{Id}/master.m3u8", "GET", Summary = "Gets an audio stream using HTTP live streaming.")]
[Route("/Audio/{Id}/master.m3u8", "HEAD", Summary = "Gets an audio stream using HTTP live streaming.")]
public class GetMasterHlsAudioPlaylist : StreamRequest, IMasterHlsRequest
{
public bool EnableAdaptiveBitrateStreaming { get; set; }
public GetMasterHlsAudioPlaylist()
{
EnableAdaptiveBitrateStreaming = true;
}
}
public interface IMasterHlsRequest
{
bool EnableAdaptiveBitrateStreaming { get; set; }
}
[Route("/Videos/{Id}/main.m3u8", "GET", Summary = "Gets a video stream using HTTP live streaming.")]
2015-05-24 18:33:28 +00:00
public class GetVariantHlsVideoPlaylist : VideoStreamRequest
{
}
[Route("/Audio/{Id}/main.m3u8", "GET", Summary = "Gets an audio stream using HTTP live streaming.")]
public class GetVariantHlsAudioPlaylist : StreamRequest
2014-01-23 18:05:41 +00:00
{
}
2017-03-15 19:57:18 +00:00
[Route("/Videos/{Id}/hls1/{PlaylistId}/{SegmentId}.{SegmentContainer}", "GET")]
2015-05-24 18:33:28 +00:00
public class GetHlsVideoSegment : VideoStreamRequest
{
public string PlaylistId { get; set; }
/// <summary>
/// Gets or sets the segment id.
/// </summary>
/// <value>The segment id.</value>
public string SegmentId { get; set; }
}
2017-03-15 19:57:18 +00:00
[Route("/Audio/{Id}/hls1/{PlaylistId}/{SegmentId}.{SegmentContainer}", "GET")]
2015-05-24 18:33:28 +00:00
public class GetHlsAudioSegment : StreamRequest
2014-01-23 18:05:41 +00:00
{
public string PlaylistId { get; set; }
/// <summary>
/// Gets or sets the segment id.
/// </summary>
/// <value>The segment id.</value>
public string SegmentId { get; set; }
}
2017-04-25 18:23:38 +00:00
[Authenticated]
2014-01-23 18:05:41 +00:00
public class DynamicHlsService : BaseHlsService
{
2016-11-10 14:41:24 +00:00
public DynamicHlsService(IServerConfigurationManager serverConfig, IUserManager userManager, ILibraryManager libraryManager, IIsoManager isoManager, IMediaEncoder mediaEncoder, IFileSystem fileSystem, IDlnaManager dlnaManager, ISubtitleEncoder subtitleEncoder, IDeviceManager deviceManager, IMediaSourceManager mediaSourceManager, IZipClient zipClient, IJsonSerializer jsonSerializer, IAuthorizationContext authorizationContext, INetworkManager networkManager) : base(serverConfig, userManager, libraryManager, isoManager, mediaEncoder, fileSystem, dlnaManager, subtitleEncoder, deviceManager, mediaSourceManager, zipClient, jsonSerializer, authorizationContext)
2014-01-23 18:05:41 +00:00
{
2014-09-10 00:28:59 +00:00
NetworkManager = networkManager;
2014-01-23 18:05:41 +00:00
}
2015-01-20 05:19:13 +00:00
protected INetworkManager NetworkManager { get; private set; }
2014-08-17 05:38:13 +00:00
2015-05-24 18:33:28 +00:00
public Task<object> Get(GetMasterHlsVideoPlaylist request)
2015-01-20 05:19:13 +00:00
{
2015-05-24 18:33:28 +00:00
return GetMasterPlaylistInternal(request, "GET");
2014-08-17 05:38:13 +00:00
}
2015-05-24 18:33:28 +00:00
public Task<object> Head(GetMasterHlsVideoPlaylist request)
2014-08-17 05:38:13 +00:00
{
2015-05-24 18:33:28 +00:00
return GetMasterPlaylistInternal(request, "HEAD");
2014-01-23 18:05:41 +00:00
}
2015-05-24 18:33:28 +00:00
public Task<object> Get(GetMasterHlsAudioPlaylist request)
2014-07-04 05:59:30 +00:00
{
2015-05-24 18:33:28 +00:00
return GetMasterPlaylistInternal(request, "GET");
2014-07-04 05:59:30 +00:00
}
2015-05-24 18:33:28 +00:00
public Task<object> Head(GetMasterHlsAudioPlaylist request)
{
return GetMasterPlaylistInternal(request, "HEAD");
}
public Task<object> Get(GetVariantHlsVideoPlaylist request)
{
return GetVariantPlaylistInternal(request, true, "main");
}
public Task<object> Get(GetVariantHlsAudioPlaylist request)
{
return GetVariantPlaylistInternal(request, false, "main");
}
public Task<object> Get(GetHlsVideoSegment request)
{
return GetDynamicSegment(request, request.SegmentId);
}
public Task<object> Get(GetHlsAudioSegment request)
2014-01-23 18:05:41 +00:00
{
2015-01-20 05:19:13 +00:00
return GetDynamicSegment(request, request.SegmentId);
2014-01-23 18:05:41 +00:00
}
2015-05-24 18:33:28 +00:00
private async Task<object> GetDynamicSegment(StreamRequest request, string segmentId)
2014-01-23 18:05:41 +00:00
{
2014-06-26 17:04:11 +00:00
if ((request.StartTimeTicks ?? 0) > 0)
{
throw new ArgumentException("StartTimeTicks is not allowed.");
}
2014-06-20 04:50:30 +00:00
var cancellationTokenSource = new CancellationTokenSource();
var cancellationToken = cancellationTokenSource.Token;
2015-03-18 16:40:16 +00:00
var requestedIndex = int.Parse(segmentId, NumberStyles.Integer, UsCulture);
2014-01-23 18:05:41 +00:00
2014-06-20 04:50:30 +00:00
var state = await GetState(request, cancellationToken).ConfigureAwait(false);
2014-01-23 18:05:41 +00:00
var playlistPath = Path.ChangeExtension(state.OutputFilePath, ".m3u8");
2014-01-23 18:05:41 +00:00
2015-05-24 18:33:28 +00:00
var segmentPath = GetSegmentPath(state, playlistPath, requestedIndex);
2014-09-03 02:30:24 +00:00
2017-03-15 19:57:18 +00:00
var segmentExtension = GetSegmentFileExtension(state.Request);
2014-10-16 03:26:39 +00:00
2014-09-03 02:30:24 +00:00
TranscodingJob job = null;
2014-01-23 18:05:41 +00:00
2015-09-20 17:56:26 +00:00
if (FileSystem.FileExists(segmentPath))
2014-01-23 18:05:41 +00:00
{
2015-03-25 18:29:21 +00:00
job = ApiEntryPoint.Instance.OnTranscodeBeginRequest(playlistPath, TranscodingJobType);
2017-04-04 05:20:50 +00:00
return await GetSegmentResult(state, playlistPath, segmentPath, segmentExtension, requestedIndex, job, cancellationToken).ConfigureAwait(false);
2014-01-23 18:05:41 +00:00
}
2016-09-25 18:39:13 +00:00
var transcodingLock = ApiEntryPoint.Instance.GetTranscodingLock(playlistPath);
await transcodingLock.WaitAsync(cancellationTokenSource.Token).ConfigureAwait(false);
2015-11-21 02:04:50 +00:00
var released = false;
2017-04-04 05:20:50 +00:00
var startTranscoding = false;
2014-06-20 04:50:30 +00:00
try
{
2015-09-20 17:56:26 +00:00
if (FileSystem.FileExists(segmentPath))
2014-06-20 04:50:30 +00:00
{
2015-03-25 18:29:21 +00:00
job = ApiEntryPoint.Instance.OnTranscodeBeginRequest(playlistPath, TranscodingJobType);
2016-09-25 18:39:13 +00:00
transcodingLock.Release();
2015-11-21 02:04:50 +00:00
released = true;
2017-04-04 05:20:50 +00:00
return await GetSegmentResult(state, playlistPath, segmentPath, segmentExtension, requestedIndex, job, cancellationToken).ConfigureAwait(false);
2014-06-20 04:50:30 +00:00
}
else
{
2015-05-27 17:50:40 +00:00
var currentTranscodingIndex = GetCurrentTranscodingIndex(playlistPath, segmentExtension);
2015-04-13 19:14:37 +00:00
var segmentGapRequiringTranscodingChange = 24 / state.SegmentLength;
if (currentTranscodingIndex == null)
{
Logger.Debug("Starting transcoding because currentTranscodingIndex=null");
startTranscoding = true;
}
else if (requestedIndex < currentTranscodingIndex.Value)
{
Logger.Debug("Starting transcoding because requestedIndex={0} and currentTranscodingIndex={1}", requestedIndex, currentTranscodingIndex);
startTranscoding = true;
}
2016-03-27 21:11:27 +00:00
else if (requestedIndex - currentTranscodingIndex.Value > segmentGapRequiringTranscodingChange)
2015-04-13 19:14:37 +00:00
{
2016-03-27 21:11:27 +00:00
Logger.Debug("Starting transcoding because segmentGap is {0} and max allowed gap is {1}. requestedIndex={2}", requestedIndex - currentTranscodingIndex.Value, segmentGapRequiringTranscodingChange, requestedIndex);
2015-04-13 19:14:37 +00:00
startTranscoding = true;
}
if (startTranscoding)
2014-06-20 04:50:30 +00:00
{
// If the playlist doesn't already exist, startup ffmpeg
try
{
2015-03-29 18:31:28 +00:00
ApiEntryPoint.Instance.KillTranscodingJobs(request.DeviceId, request.PlaySessionId, p => false);
2014-07-02 18:34:08 +00:00
2014-06-26 17:04:11 +00:00
if (currentTranscodingIndex.HasValue)
{
2014-10-16 03:26:39 +00:00
DeleteLastFile(playlistPath, segmentExtension, 0);
2014-06-26 17:04:11 +00:00
}
2015-08-27 15:58:07 +00:00
request.StartTimeTicks = GetStartPositionTicks(state, requestedIndex);
2014-06-20 04:50:30 +00:00
2014-09-03 02:30:24 +00:00
job = await StartFfMpeg(state, playlistPath, cancellationTokenSource).ConfigureAwait(false);
2014-06-20 04:50:30 +00:00
}
catch
{
state.Dispose();
throw;
}
2015-04-13 19:14:37 +00:00
//await WaitForMinimumSegmentCount(playlistPath, 1, cancellationTokenSource.Token).ConfigureAwait(false);
2014-06-20 04:50:30 +00:00
}
2015-04-10 22:16:41 +00:00
else
{
job = ApiEntryPoint.Instance.OnTranscodeBeginRequest(playlistPath, TranscodingJobType);
if (job.TranscodingThrottler != null)
{
job.TranscodingThrottler.UnpauseTranscoding();
}
}
2014-06-20 04:50:30 +00:00
}
}
finally
2014-01-23 18:05:41 +00:00
{
2015-11-21 02:04:50 +00:00
if (!released)
{
2016-09-25 18:39:13 +00:00
transcodingLock.Release();
2015-11-21 02:04:50 +00:00
}
2014-06-20 04:50:30 +00:00
}
2014-01-23 18:05:41 +00:00
2015-05-24 18:33:28 +00:00
//Logger.Info("waiting for {0}", segmentPath);
//while (!File.Exists(segmentPath))
//{
// await Task.Delay(50, cancellationToken).ConfigureAwait(false);
//}
2014-01-23 18:05:41 +00:00
2014-06-20 04:50:30 +00:00
Logger.Info("returning {0}", segmentPath);
2015-03-25 18:29:21 +00:00
job = job ?? ApiEntryPoint.Instance.OnTranscodeBeginRequest(playlistPath, TranscodingJobType);
2017-04-04 05:20:50 +00:00
return await GetSegmentResult(state, playlistPath, segmentPath, segmentExtension, requestedIndex, job, cancellationToken).ConfigureAwait(false);
2015-03-18 16:40:16 +00:00
}
2016-07-27 17:11:25 +00:00
private const int BufferSize = 81920;
2015-03-18 16:40:16 +00:00
2015-08-27 15:58:07 +00:00
private long GetStartPositionTicks(StreamState state, int requestedIndex)
2015-05-20 16:28:55 +00:00
{
double startSeconds = 0;
2015-08-27 15:58:07 +00:00
var lengths = GetSegmentLengths(state);
2015-05-20 16:28:55 +00:00
2017-01-03 05:59:29 +00:00
if (requestedIndex >= lengths.Length)
{
var msg = string.Format("Invalid segment index requested: {0} - Segment count: {1}", requestedIndex, lengths.Length);
throw new ArgumentException(msg);
}
2015-05-20 16:28:55 +00:00
for (var i = 0; i < requestedIndex; i++)
{
2017-01-03 05:59:29 +00:00
startSeconds += lengths[i];
2015-08-27 15:58:07 +00:00
}
var position = TimeSpan.FromSeconds(startSeconds).Ticks;
return position;
}
2015-05-20 16:28:55 +00:00
2015-08-27 15:58:07 +00:00
private long GetEndPositionTicks(StreamState state, int requestedIndex)
{
double startSeconds = 0;
var lengths = GetSegmentLengths(state);
2017-01-03 05:59:29 +00:00
if (requestedIndex >= lengths.Length)
{
var msg = string.Format("Invalid segment index requested: {0} - Segment count: {1}", requestedIndex, lengths.Length);
throw new ArgumentException(msg);
}
2015-08-27 15:58:07 +00:00
for (var i = 0; i <= requestedIndex; i++)
{
2017-01-03 05:59:29 +00:00
startSeconds += lengths[i];
2015-05-20 16:28:55 +00:00
}
var position = TimeSpan.FromSeconds(startSeconds).Ticks;
2015-03-18 16:40:16 +00:00
return position;
2014-06-26 17:04:11 +00:00
}
2015-08-27 15:58:07 +00:00
private double[] GetSegmentLengths(StreamState state)
{
var result = new List<double>();
var ticks = state.RunTimeTicks ?? 0;
var segmentLengthTicks = TimeSpan.FromSeconds(state.SegmentLength).Ticks;
while (ticks > 0)
{
var length = ticks >= segmentLengthTicks ? segmentLengthTicks : ticks;
result.Add(TimeSpan.FromTicks(length).TotalSeconds);
ticks -= length;
}
return result.ToArray();
}
2015-05-27 17:50:40 +00:00
public int? GetCurrentTranscodingIndex(string playlist, string segmentExtension)
2014-06-26 17:04:11 +00:00
{
2015-05-27 17:50:40 +00:00
var job = ApiEntryPoint.Instance.GetTranscodingJob(playlist, TranscodingJobType);
2015-03-18 16:40:16 +00:00
if (job == null || job.HasExited)
{
return null;
}
2014-10-16 03:26:39 +00:00
var file = GetLastTranscodingFile(playlist, segmentExtension, FileSystem);
2014-06-26 17:04:11 +00:00
if (file == null)
{
return null;
}
var playlistFilename = Path.GetFileNameWithoutExtension(playlist);
var indexString = Path.GetFileNameWithoutExtension(file.Name).Substring(playlistFilename.Length);
return int.Parse(indexString, NumberStyles.Integer, UsCulture);
}
2014-11-24 12:17:48 +00:00
private void DeleteLastFile(string playlistPath, string segmentExtension, int retryCount)
{
var file = GetLastTranscodingFile(playlistPath, segmentExtension, FileSystem);
if (file != null)
{
2017-03-29 06:26:48 +00:00
DeleteFile(file.FullName, retryCount);
2014-11-24 12:17:48 +00:00
}
}
2017-03-29 06:26:48 +00:00
private void DeleteFile(string path, int retryCount)
2014-06-26 17:04:11 +00:00
{
if (retryCount >= 5)
{
return;
}
2015-03-16 20:03:48 +00:00
2017-03-29 06:26:48 +00:00
Logger.Debug("Deleting partial HLS file {0}", path);
2016-12-31 03:23:29 +00:00
2014-11-24 12:17:48 +00:00
try
2014-06-26 17:04:11 +00:00
{
2017-03-29 06:26:48 +00:00
FileSystem.DeleteFile(path);
2014-11-24 12:17:48 +00:00
}
catch (IOException ex)
{
2017-03-29 06:26:48 +00:00
Logger.ErrorException("Error deleting partial stream file(s) {0}", ex, path);
2014-06-26 17:04:11 +00:00
2016-11-01 03:07:45 +00:00
var task = Task.Delay(100);
Task.WaitAll(task);
2017-03-29 06:26:48 +00:00
DeleteFile(path, retryCount + 1);
2014-11-24 12:17:48 +00:00
}
catch (Exception ex)
{
2017-03-29 06:26:48 +00:00
Logger.ErrorException("Error deleting partial stream file(s) {0}", ex, path);
2014-06-26 17:04:11 +00:00
}
}
2015-10-04 03:38:46 +00:00
private static FileSystemMetadata GetLastTranscodingFile(string playlist, string segmentExtension, IFileSystem fileSystem)
2014-06-26 17:04:11 +00:00
{
2017-05-04 18:14:45 +00:00
var folder = fileSystem.GetDirectoryName(playlist);
2014-06-26 17:04:11 +00:00
2014-11-24 12:17:48 +00:00
var filePrefix = Path.GetFileNameWithoutExtension(playlist) ?? string.Empty;
2014-06-26 17:04:11 +00:00
try
{
2017-03-29 06:26:48 +00:00
return fileSystem.GetFiles(folder, new[] { segmentExtension }, true, false)
.Where(i => Path.GetFileNameWithoutExtension(i.Name).StartsWith(filePrefix, StringComparison.OrdinalIgnoreCase))
2014-06-26 17:04:11 +00:00
.OrderByDescending(fileSystem.GetLastWriteTimeUtc)
.FirstOrDefault();
}
2016-11-01 03:07:45 +00:00
catch (IOException)
2014-06-26 17:04:11 +00:00
{
return null;
}
2014-06-20 04:50:30 +00:00
}
protected override int GetStartNumber(StreamState state)
{
2014-07-04 05:59:30 +00:00
return GetStartNumber(state.VideoRequest);
}
private int GetStartNumber(VideoStreamRequest request)
{
var segmentId = "0";
2014-06-20 04:50:30 +00:00
2015-05-24 18:33:28 +00:00
var segmentRequest = request as GetHlsVideoSegment;
2014-07-04 05:59:30 +00:00
if (segmentRequest != null)
{
segmentId = segmentRequest.SegmentId;
}
return int.Parse(segmentId, NumberStyles.Integer, UsCulture);
2014-01-23 18:05:41 +00:00
}
2015-05-24 18:33:28 +00:00
private string GetSegmentPath(StreamState state, string playlist, int index)
2014-01-23 18:05:41 +00:00
{
2017-05-04 18:14:45 +00:00
var folder = FileSystem.GetDirectoryName(playlist);
2014-01-23 18:05:41 +00:00
var filename = Path.GetFileNameWithoutExtension(playlist);
2017-03-15 19:57:18 +00:00
return Path.Combine(folder, filename + index.ToString(UsCulture) + GetSegmentFileExtension(state.Request));
2014-01-23 18:05:41 +00:00
}
2017-04-04 05:20:50 +00:00
private async Task<object> GetSegmentResult(StreamState state,
string playlistPath,
2014-09-03 02:30:24 +00:00
string segmentPath,
2017-04-04 05:20:50 +00:00
string segmentExtension,
2014-09-03 02:30:24 +00:00
int segmentIndex,
TranscodingJob transcodingJob,
CancellationToken cancellationToken)
2014-06-26 17:04:11 +00:00
{
2017-04-04 05:20:50 +00:00
var segmentFileExists = FileSystem.FileExists(segmentPath);
2014-06-26 17:04:11 +00:00
// If all transcoding has completed, just return immediately
2017-04-04 05:20:50 +00:00
if (transcodingJob != null && transcodingJob.HasExited && segmentFileExists)
2014-06-26 17:04:11 +00:00
{
2016-11-10 21:06:00 +00:00
return await GetSegmentResult(state, segmentPath, segmentIndex, transcodingJob).ConfigureAwait(false);
2014-06-26 17:04:11 +00:00
}
2017-04-04 05:20:50 +00:00
if (segmentFileExists)
{
var currentTranscodingIndex = GetCurrentTranscodingIndex(playlistPath, segmentExtension);
// If requested segment is less than transcoding position, we can't transcode backwards, so assume it's ready
2017-04-09 19:49:18 +00:00
if (segmentIndex < currentTranscodingIndex)
2017-04-04 05:20:50 +00:00
{
return await GetSegmentResult(state, segmentPath, segmentIndex, transcodingJob).ConfigureAwait(false);
}
}
2014-06-26 17:04:11 +00:00
var segmentFilename = Path.GetFileName(segmentPath);
2015-05-22 15:59:17 +00:00
while (!cancellationToken.IsCancellationRequested)
2014-06-26 17:04:11 +00:00
{
2015-06-07 21:21:30 +00:00
try
2014-07-27 22:01:29 +00:00
{
2015-06-07 21:21:30 +00:00
using (var fileStream = GetPlaylistFileStream(playlistPath))
2014-07-27 22:01:29 +00:00
{
2015-07-31 20:38:08 +00:00
using (var reader = new StreamReader(fileStream, Encoding.UTF8, true, BufferSize))
2015-03-17 21:02:54 +00:00
{
2015-07-31 20:38:08 +00:00
var text = await reader.ReadToEndAsync().ConfigureAwait(false);
2015-06-07 21:21:30 +00:00
2015-07-31 20:38:08 +00:00
// If it appears in the playlist, it's done
if (text.IndexOf(segmentFilename, StringComparison.OrdinalIgnoreCase) != -1)
{
2017-04-04 05:20:50 +00:00
if (!segmentFileExists)
{
segmentFileExists = FileSystem.FileExists(segmentPath);
}
if (segmentFileExists)
2015-06-07 21:21:30 +00:00
{
2016-11-10 21:06:00 +00:00
return await GetSegmentResult(state, segmentPath, segmentIndex, transcodingJob).ConfigureAwait(false);
2015-06-07 21:21:30 +00:00
}
2015-07-31 20:38:08 +00:00
//break;
2015-05-22 15:59:17 +00:00
}
2015-03-17 21:02:54 +00:00
}
2014-07-27 22:01:29 +00:00
}
}
2015-06-07 21:21:30 +00:00
catch (IOException)
{
// May get an error if the file is locked
}
2015-05-22 15:59:17 +00:00
await Task.Delay(100, cancellationToken).ConfigureAwait(false);
2014-06-26 17:04:11 +00:00
}
2015-05-22 15:59:17 +00:00
cancellationToken.ThrowIfCancellationRequested();
2016-11-10 21:06:00 +00:00
return await GetSegmentResult(state, segmentPath, segmentIndex, transcodingJob).ConfigureAwait(false);
2014-09-03 02:30:24 +00:00
}
2016-11-10 21:06:00 +00:00
private Task<object> GetSegmentResult(StreamState state, string segmentPath, int index, TranscodingJob transcodingJob)
2014-09-03 02:30:24 +00:00
{
2015-08-27 15:58:07 +00:00
var segmentEndingPositionTicks = GetEndPositionTicks(state, index);
2014-09-03 02:30:24 +00:00
return ResultFactory.GetStaticFileResult(Request, new StaticFileResultOptions
{
Path = segmentPath,
2016-10-25 19:02:04 +00:00
FileShare = FileShareMode.ReadWrite,
2014-09-03 02:30:24 +00:00
OnComplete = () =>
{
if (transcodingJob != null)
{
transcodingJob.DownloadPositionTicks = Math.Max(transcodingJob.DownloadPositionTicks ?? segmentEndingPositionTicks, segmentEndingPositionTicks);
2015-03-25 18:29:21 +00:00
ApiEntryPoint.Instance.OnTranscodeEndRequest(transcodingJob);
2014-09-03 02:30:24 +00:00
}
}
2016-11-10 21:06:00 +00:00
});
2014-06-26 17:04:11 +00:00
}
2015-05-24 18:33:28 +00:00
private async Task<object> GetMasterPlaylistInternal(StreamRequest request, string method)
2014-01-23 18:05:41 +00:00
{
var state = await GetState(request, CancellationToken.None).ConfigureAwait(false);
2014-07-31 02:09:23 +00:00
if (string.IsNullOrEmpty(request.MediaSourceId))
{
throw new ArgumentException("MediaSourceId is required");
}
2014-08-17 05:38:13 +00:00
var playlistText = string.Empty;
if (string.Equals(method, "GET", StringComparison.OrdinalIgnoreCase))
{
var audioBitrate = state.OutputAudioBitrate ?? 0;
var videoBitrate = state.OutputVideoBitrate ?? 0;
2014-01-23 18:05:41 +00:00
2014-08-17 05:38:13 +00:00
playlistText = GetMasterPlaylistFileText(state, videoBitrate + audioBitrate);
}
2014-01-23 18:05:41 +00:00
2014-12-26 17:45:06 +00:00
return ResultFactory.GetResult(playlistText, MimeTypes.GetMimeType("playlist.m3u8"), new Dictionary<string, string>());
2014-01-23 18:05:41 +00:00
}
private string GetMasterPlaylistFileText(StreamState state, int totalBitrate)
2014-01-23 18:05:41 +00:00
{
var builder = new StringBuilder();
builder.AppendLine("#EXTM3U");
2017-01-07 08:09:24 +00:00
var isLiveStream = state.IsSegmentedLiveStream;
2015-07-03 11:51:45 +00:00
2014-01-23 18:05:41 +00:00
var queryStringIndex = Request.RawUrl.IndexOf('?');
var queryString = queryStringIndex == -1 ? string.Empty : Request.RawUrl.Substring(queryStringIndex);
2017-05-18 21:05:47 +00:00
// from universal audio service
if (queryString.IndexOf("SegmentContainer", StringComparison.OrdinalIgnoreCase) == -1 && !string.IsNullOrWhiteSpace(state.Request.SegmentContainer))
{
queryString += "&SegmentContainer=" + state.Request.SegmentContainer;
}
2014-01-23 18:05:41 +00:00
// Main stream
2014-10-06 23:58:46 +00:00
var playlistUrl = isLiveStream ? "live.m3u8" : "main.m3u8";
2015-12-14 21:53:47 +00:00
playlistUrl += queryString;
2014-07-01 21:13:32 +00:00
2015-05-24 18:33:28 +00:00
var request = state.Request;
2015-03-28 20:22:27 +00:00
var subtitleStreams = state.MediaSource
.MediaStreams
.Where(i => i.IsTextSubtitleStream)
.ToList();
2015-05-24 18:33:28 +00:00
var subtitleGroup = subtitleStreams.Count > 0 &&
2016-03-27 21:11:27 +00:00
request is GetMasterHlsVideoPlaylist &&
2017-04-04 05:20:50 +00:00
(state.SubtitleDeliveryMethod == SubtitleDeliveryMethod.Hls || state.VideoRequest.EnableSubtitlesInManifest) ?
2014-08-17 05:38:13 +00:00
"subs" :
null;
2016-07-13 19:16:51 +00:00
// If we're burning in subtitles then don't add additional subs to the manifest
2017-04-04 05:20:50 +00:00
if (state.SubtitleStream != null && state.SubtitleDeliveryMethod == SubtitleDeliveryMethod.Encode)
2016-07-13 19:16:51 +00:00
{
subtitleGroup = null;
}
if (!string.IsNullOrWhiteSpace(subtitleGroup))
{
AddSubtitles(state, subtitleStreams, builder);
}
2016-03-06 21:31:56 +00:00
AppendPlaylist(builder, state, playlistUrl, totalBitrate, subtitleGroup);
2014-10-06 23:58:46 +00:00
if (EnableAdaptiveBitrateStreaming(state, isLiveStream))
{
2015-05-24 18:33:28 +00:00
var requestedVideoBitrate = state.VideoRequest == null ? 0 : state.VideoRequest.VideoBitRate ?? 0;
2014-08-21 15:55:35 +00:00
// By default, vary by just 200k
var variation = GetBitrateVariation(totalBitrate);
2014-08-21 15:55:35 +00:00
var newBitrate = totalBitrate - variation;
2016-03-27 21:11:27 +00:00
var variantUrl = ReplaceBitrate(playlistUrl, requestedVideoBitrate, requestedVideoBitrate - variation);
2016-03-06 21:31:56 +00:00
AppendPlaylist(builder, state, variantUrl, newBitrate, subtitleGroup);
2014-08-21 15:55:35 +00:00
variation *= 2;
newBitrate = totalBitrate - variation;
2016-03-27 21:11:27 +00:00
variantUrl = ReplaceBitrate(playlistUrl, requestedVideoBitrate, requestedVideoBitrate - variation);
2016-03-06 21:31:56 +00:00
AppendPlaylist(builder, state, variantUrl, newBitrate, subtitleGroup);
}
2014-01-23 18:05:41 +00:00
return builder.ToString();
}
2014-08-21 15:55:35 +00:00
private string ReplaceBitrate(string url, int oldValue, int newValue)
{
return url.Replace(
"videobitrate=" + oldValue.ToString(UsCulture),
"videobitrate=" + newValue.ToString(UsCulture),
StringComparison.OrdinalIgnoreCase);
}
private void AddSubtitles(StreamState state, IEnumerable<MediaStream> subtitles, StringBuilder builder)
{
2017-04-04 05:20:50 +00:00
var selectedIndex = state.SubtitleStream == null || state.SubtitleDeliveryMethod != SubtitleDeliveryMethod.Hls ? (int?)null : state.SubtitleStream.Index;
foreach (var stream in subtitles)
{
const string format = "#EXT-X-MEDIA:TYPE=SUBTITLES,GROUP-ID=\"subs\",NAME=\"{0}\",DEFAULT={1},FORCED={2},AUTOSELECT=YES,URI=\"{3}\",LANGUAGE=\"{4}\"";
2016-07-13 17:46:04 +00:00
var name = stream.DisplayTitle;
var isDefault = selectedIndex.HasValue && selectedIndex.Value == stream.Index;
var isForced = stream.IsForced;
2016-03-07 18:50:58 +00:00
var url = string.Format("{0}/Subtitles/{1}/subtitles.m3u8?SegmentLength={2}&api_key={3}",
state.Request.MediaSourceId,
stream.Index.ToString(UsCulture),
2016-03-07 18:50:58 +00:00
30.ToString(UsCulture),
AuthorizationContext.GetAuthorizationInfo(Request).Token);
var line = string.Format(format,
name,
isDefault ? "YES" : "NO",
isForced ? "YES" : "NO",
url,
stream.Language ?? "Unknown");
builder.AppendLine(line);
}
}
2014-10-06 23:58:46 +00:00
private bool EnableAdaptiveBitrateStreaming(StreamState state, bool isLiveStream)
2014-07-15 19:16:16 +00:00
{
2014-09-10 00:28:59 +00:00
// Within the local network this will likely do more harm than good.
if (Request.IsLocal || NetworkManager.IsInLocalNetwork(Request.RemoteIp))
{
return false;
}
2015-05-24 18:33:28 +00:00
var request = state.Request as IMasterHlsRequest;
2014-08-01 02:58:37 +00:00
if (request != null && !request.EnableAdaptiveBitrateStreaming)
{
return false;
}
2014-10-06 23:58:46 +00:00
if (isLiveStream || string.IsNullOrWhiteSpace(state.MediaPath))
2014-07-15 19:16:16 +00:00
{
// Opening live streams is so slow it's not even worth it
return false;
}
2014-12-14 20:01:26 +00:00
if (string.Equals(state.OutputVideoCodec, "copy", StringComparison.OrdinalIgnoreCase))
{
return false;
}
if (string.Equals(state.OutputAudioCodec, "copy", StringComparison.OrdinalIgnoreCase))
{
return false;
}
2015-05-24 18:33:28 +00:00
if (!state.IsOutputVideo)
{
return false;
}
2015-04-18 06:15:31 +00:00
// Having problems in android
return false;
//return state.VideoRequest.VideoBitRate.HasValue;
2014-07-15 19:16:16 +00:00
}
2016-03-06 21:31:56 +00:00
private void AppendPlaylist(StringBuilder builder, StreamState state, string url, int bitrate, string subtitleGroup)
{
2016-03-06 21:31:56 +00:00
var header = "#EXT-X-STREAM-INF:BANDWIDTH=" + bitrate.ToString(UsCulture) + ",AVERAGE-BANDWIDTH=" + bitrate.ToString(UsCulture);
// tvos wants resolution, codecs, framerate
//if (state.TargetFramerate.HasValue)
//{
// header += string.Format(",FRAME-RATE=\"{0}\"", state.TargetFramerate.Value.ToString(CultureInfo.InvariantCulture));
//}
if (!string.IsNullOrWhiteSpace(subtitleGroup))
{
header += string.Format(",SUBTITLES=\"{0}\"", subtitleGroup);
}
builder.AppendLine(header);
builder.AppendLine(url);
}
private int GetBitrateVariation(int bitrate)
{
2014-08-21 15:55:35 +00:00
// By default, vary by just 50k
var variation = 50000;
if (bitrate >= 10000000)
{
variation = 2000000;
}
else if (bitrate >= 5000000)
{
variation = 1500000;
}
else if (bitrate >= 3000000)
{
variation = 1000000;
}
else if (bitrate >= 2000000)
{
variation = 500000;
}
else if (bitrate >= 1000000)
{
variation = 300000;
}
2014-07-15 19:16:16 +00:00
else if (bitrate >= 600000)
{
variation = 200000;
}
2014-08-21 15:55:35 +00:00
else if (bitrate >= 400000)
{
variation = 100000;
}
return variation;
}
2015-05-24 18:33:28 +00:00
private async Task<object> GetVariantPlaylistInternal(StreamRequest request, bool isOutputVideo, string name)
2014-01-23 18:05:41 +00:00
{
var state = await GetState(request, CancellationToken.None).ConfigureAwait(false);
2015-08-27 15:58:07 +00:00
var segmentLengths = GetSegmentLengths(state);
2014-01-23 18:05:41 +00:00
var builder = new StringBuilder();
builder.AppendLine("#EXTM3U");
2016-03-06 21:31:56 +00:00
builder.AppendLine("#EXT-X-PLAYLIST-TYPE:VOD");
2014-01-23 18:05:41 +00:00
builder.AppendLine("#EXT-X-VERSION:3");
2016-03-27 21:11:27 +00:00
builder.AppendLine("#EXT-X-TARGETDURATION:" + Math.Ceiling(segmentLengths.Length > 0 ? segmentLengths.Max() : state.SegmentLength).ToString(UsCulture));
2014-01-23 18:05:41 +00:00
builder.AppendLine("#EXT-X-MEDIA-SEQUENCE:0");
var queryStringIndex = Request.RawUrl.IndexOf('?');
var queryString = queryStringIndex == -1 ? string.Empty : Request.RawUrl.Substring(queryStringIndex);
2015-12-17 02:17:40 +00:00
//if ((Request.UserAgent ?? string.Empty).IndexOf("roku", StringComparison.OrdinalIgnoreCase) != -1)
//{
// queryString = string.Empty;
//}
2015-12-14 21:53:47 +00:00
2014-01-23 18:05:41 +00:00
var index = 0;
2015-08-27 15:58:07 +00:00
foreach (var length in segmentLengths)
2014-01-23 18:05:41 +00:00
{
2016-12-31 03:23:29 +00:00
builder.AppendLine("#EXTINF:" + length.ToString("0.0000", UsCulture) + ", nodesc");
2014-01-23 18:05:41 +00:00
2015-11-30 18:01:28 +00:00
builder.AppendLine(string.Format("hls1/{0}/{1}{2}{3}",
2014-01-23 18:05:41 +00:00
name,
index.ToString(UsCulture),
2017-03-15 19:57:18 +00:00
GetSegmentFileExtension(request),
2014-01-23 18:05:41 +00:00
queryString));
index++;
}
builder.AppendLine("#EXT-X-ENDLIST");
var playlistText = builder.ToString();
2014-12-26 17:45:06 +00:00
return ResultFactory.GetResult(playlistText, MimeTypes.GetMimeType("playlist.m3u8"), new Dictionary<string, string>());
2014-01-23 18:05:41 +00:00
}
protected override string GetAudioArguments(StreamState state)
{
var codec = EncodingHelper.GetAudioEncoder(state);
2015-06-08 01:23:56 +00:00
2015-05-24 18:33:28 +00:00
if (!state.IsOutputVideo)
{
2015-06-08 01:23:56 +00:00
if (string.Equals(codec, "copy", StringComparison.OrdinalIgnoreCase))
{
return "-acodec copy";
}
2015-05-24 18:33:28 +00:00
var audioTranscodeParams = new List<string>();
2015-06-08 01:23:56 +00:00
audioTranscodeParams.Add("-acodec " + codec);
2015-07-03 11:51:45 +00:00
2015-05-24 18:33:28 +00:00
if (state.OutputAudioBitrate.HasValue)
{
audioTranscodeParams.Add("-ab " + state.OutputAudioBitrate.Value.ToString(UsCulture));
}
if (state.OutputAudioChannels.HasValue)
{
audioTranscodeParams.Add("-ac " + state.OutputAudioChannels.Value.ToString(UsCulture));
}
if (state.OutputAudioSampleRate.HasValue)
{
audioTranscodeParams.Add("-ar " + state.OutputAudioSampleRate.Value.ToString(UsCulture));
}
audioTranscodeParams.Add("-vn");
return string.Join(" ", audioTranscodeParams.ToArray());
}
2014-10-20 03:04:45 +00:00
if (string.Equals(codec, "copy", StringComparison.OrdinalIgnoreCase))
2014-01-23 18:05:41 +00:00
{
2017-04-22 19:32:24 +00:00
return "-codec:a:0 copy -copypriorss:a:0 0";
2014-01-23 18:05:41 +00:00
}
var args = "-codec:a:0 " + codec;
2014-06-23 16:05:19 +00:00
var channels = state.OutputAudioChannels;
2014-01-23 18:05:41 +00:00
2014-06-23 16:05:19 +00:00
if (channels.HasValue)
{
args += " -ac " + channels.Value;
}
2014-01-23 18:05:41 +00:00
2014-06-23 16:05:19 +00:00
var bitrate = state.OutputAudioBitrate;
2014-01-23 18:05:41 +00:00
2014-06-23 16:05:19 +00:00
if (bitrate.HasValue)
{
args += " -ab " + bitrate.Value.ToString(UsCulture);
2014-01-23 18:05:41 +00:00
}
2017-04-15 19:45:33 +00:00
if (state.OutputAudioSampleRate.HasValue)
{
args += " -ar " + state.OutputAudioSampleRate.Value.ToString(UsCulture);
}
args += " " + EncodingHelper.GetAudioFilterParam(state, ApiEntryPoint.Instance.GetEncodingOptions(), true);
2014-06-23 16:05:19 +00:00
2014-01-23 18:05:41 +00:00
return args;
}
2014-06-10 17:36:06 +00:00
protected override string GetVideoArguments(StreamState state)
2014-01-23 18:05:41 +00:00
{
2015-05-24 18:33:28 +00:00
if (!state.IsOutputVideo)
{
return string.Empty;
}
var codec = EncodingHelper.GetVideoEncoder(state, ApiEntryPoint.Instance.GetEncodingOptions());
2014-01-23 18:05:41 +00:00
2014-12-26 17:45:06 +00:00
var args = "-codec:v:0 " + codec;
if (state.EnableMpegtsM2TsMode)
{
args += " -mpegts_m2ts_mode 1";
}
2014-01-23 18:05:41 +00:00
// See if we can save come cpu cycles by avoiding encoding
2015-05-27 17:50:40 +00:00
if (string.Equals(codec, "copy", StringComparison.OrdinalIgnoreCase))
2014-01-23 18:05:41 +00:00
{
if (state.VideoStream != null && EncodingHelper.IsH264(state.VideoStream) && !string.Equals(state.VideoStream.NalLengthSize, "0", StringComparison.OrdinalIgnoreCase))
2015-05-27 17:50:40 +00:00
{
args += " -bsf:v h264_mp4toannexb";
}
2016-12-31 03:23:29 +00:00
//args += " -flags -global_header";
2014-01-23 18:05:41 +00:00
}
2015-05-24 18:33:28 +00:00
else
{
2015-08-09 16:35:27 +00:00
var keyFrameArg = string.Format(" -force_key_frames \"expr:gte(t,n_forced*{0})\"",
2015-05-24 18:33:28 +00:00
state.SegmentLength.ToString(UsCulture));
2014-01-23 18:05:41 +00:00
2017-04-04 05:20:50 +00:00
var hasGraphicalSubs = state.SubtitleStream != null && !state.SubtitleStream.IsTextSubtitleStream && state.SubtitleDeliveryMethod == SubtitleDeliveryMethod.Encode;
2014-01-23 18:05:41 +00:00
var encodingOptions = ApiEntryPoint.Instance.GetEncodingOptions();
2017-03-15 19:57:18 +00:00
args += " " + EncodingHelper.GetVideoQualityParam(state, codec, encodingOptions, GetDefaultH264Preset()) + keyFrameArg;
2014-01-23 18:05:41 +00:00
2015-05-24 18:33:28 +00:00
//args += " -mixed-refs 0 -refs 3 -x264opts b_pyramid=0:weightb=0:weightp=0";
2014-01-23 18:05:41 +00:00
2015-05-24 18:33:28 +00:00
// Add resolution params, if specified
if (!hasGraphicalSubs)
{
2017-05-11 14:54:41 +00:00
args += EncodingHelper.GetOutputSizeParam(state, codec, true);
2015-05-24 18:33:28 +00:00
}
2015-05-21 20:53:14 +00:00
2015-05-24 18:33:28 +00:00
// This is for internal graphical subs
if (hasGraphicalSubs)
{
args += EncodingHelper.GetGraphicalSubtitleParam(state, codec);
2015-05-24 18:33:28 +00:00
}
2015-05-27 17:50:40 +00:00
2016-12-31 03:23:29 +00:00
//args += " -flags -global_header";
2014-01-23 18:05:41 +00:00
}
2017-05-11 14:54:41 +00:00
if (args.IndexOf("-copyts", StringComparison.OrdinalIgnoreCase) == -1)
2016-04-20 18:51:47 +00:00
{
args += " -copyts";
}
2017-04-09 21:38:59 +00:00
if (!string.IsNullOrEmpty(state.OutputVideoSync))
{
args += " -vsync " + state.OutputVideoSync;
}
2017-05-11 14:54:41 +00:00
args += EncodingHelper.GetOutputFFlags(state);
2014-01-23 18:05:41 +00:00
2017-05-11 14:54:41 +00:00
return args;
2015-09-02 01:50:02 +00:00
}
2015-03-28 20:22:27 +00:00
protected override string GetCommandLineArguments(string outputPath, StreamState state, bool isEncoding)
2014-06-28 02:43:10 +00:00
{
var encodingOptions = ApiEntryPoint.Instance.GetEncodingOptions();
var threads = EncodingHelper.GetNumberOfThreads(state, encodingOptions, false);
2014-06-28 02:43:10 +00:00
var inputModifier = EncodingHelper.GetInputModifier(state, encodingOptions);
2014-06-28 02:43:10 +00:00
// If isEncoding is true we're actually starting ffmpeg
2016-12-31 03:23:29 +00:00
var startNumber = GetStartNumber(state);
var startNumberParam = isEncoding ? startNumber.ToString(UsCulture) : "0";
2015-05-27 17:50:40 +00:00
var mapArgs = state.IsOutputVideo ? EncodingHelper.GetMapArgs(state) : string.Empty;
2016-10-17 16:18:12 +00:00
2017-05-18 21:05:47 +00:00
var outputTsArg = Path.Combine(FileSystem.GetDirectoryName(outputPath), Path.GetFileNameWithoutExtension(outputPath)) + "%d" + GetSegmentFileExtension(state.Request);
var timeDeltaParam = String.Empty;
if (isEncoding && startNumber > 0)
2016-04-21 04:04:57 +00:00
{
2017-05-18 21:05:47 +00:00
var startTime = state.SegmentLength * startNumber;
timeDeltaParam = string.Format("-segment_time_delta -{0}", startTime);
}
2016-04-21 04:04:57 +00:00
2017-05-18 21:05:47 +00:00
var segmentFormat = GetSegmentFileExtension(state.Request).TrimStart('.');
if (string.Equals(segmentFormat, "ts", StringComparison.OrdinalIgnoreCase))
{
segmentFormat = "mpegts";
}
2016-12-31 03:23:29 +00:00
2017-05-18 21:05:47 +00:00
var videoCodec = EncodingHelper.GetVideoEncoder(state, ApiEntryPoint.Instance.GetEncodingOptions());
var breakOnNonKeyFrames = state.EnableBreakOnNonKeyFrames(videoCodec);
2016-12-31 03:23:29 +00:00
2017-05-18 21:05:47 +00:00
var breakOnNonKeyFramesArg = breakOnNonKeyFrames ? " -break_non_keyframes 1" : "";
2017-03-15 19:57:18 +00:00
2017-05-18 21:05:47 +00:00
return string.Format("{0} {1} -map_metadata -1 -map_chapters -1 -threads {2} {3} {4} {5} -f segment -max_delay 5000000 -avoid_negative_ts disabled -start_at_zero -segment_time {6} {10} -individual_header_trailer 0{12} -segment_format {11} -segment_list_type m3u8 -segment_start_number {7} -segment_list \"{8}\" -y \"{9}\"",
inputModifier,
EncodingHelper.GetInputArgument(state, encodingOptions),
threads,
mapArgs,
GetVideoArguments(state),
GetAudioArguments(state),
state.SegmentLength.ToString(UsCulture),
startNumberParam,
outputPath,
outputTsArg,
timeDeltaParam,
segmentFormat,
breakOnNonKeyFramesArg
).Trim();
2014-06-28 02:43:10 +00:00
}
2014-01-23 18:05:41 +00:00
}
2015-09-20 17:56:26 +00:00
}