aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Api/Playback
diff options
context:
space:
mode:
Diffstat (limited to 'MediaBrowser.Api/Playback')
-rw-r--r--MediaBrowser.Api/Playback/BaseStreamingService.cs57
-rw-r--r--MediaBrowser.Api/Playback/Hls/BaseHlsService.cs7
-rw-r--r--MediaBrowser.Api/Playback/Hls/DynamicHlsService.cs444
-rw-r--r--MediaBrowser.Api/Playback/Hls/HlsSegmentService.cs35
-rw-r--r--MediaBrowser.Api/Playback/Hls/VideoHlsService.cs21
-rw-r--r--MediaBrowser.Api/Playback/Progressive/VideoService.cs2
-rw-r--r--MediaBrowser.Api/Playback/StreamState.cs7
-rw-r--r--MediaBrowser.Api/Playback/TranscodingThrottler.cs2
8 files changed, 416 insertions, 159 deletions
diff --git a/MediaBrowser.Api/Playback/BaseStreamingService.cs b/MediaBrowser.Api/Playback/BaseStreamingService.cs
index 5e06ab1d0..31679aad3 100644
--- a/MediaBrowser.Api/Playback/BaseStreamingService.cs
+++ b/MediaBrowser.Api/Playback/BaseStreamingService.cs
@@ -157,11 +157,11 @@ namespace MediaBrowser.Api.Playback
/// <value>The fast seek command line parameter.</value>
protected string GetFastSeekCommandLineParameter(StreamRequest request)
{
- var time = request.StartTimeTicks;
+ var time = request.StartTimeTicks ?? 0;
- if (time.HasValue && time.Value > 0)
+ if (time > 0)
{
- return string.Format("-ss {0}", MediaEncoder.GetTimeParameter(time.Value));
+ return string.Format("-ss {0}", MediaEncoder.GetTimeParameter(time));
}
return string.Empty;
@@ -690,7 +690,7 @@ namespace MediaBrowser.Api.Playback
// TODO: Perhaps also use original_size=1920x800 ??
return string.Format("subtitles=filename='{0}'{1},setpts=PTS -{2}/TB",
- subtitlePath.Replace('\\', '/').Replace(":/", "\\:/"),
+ subtitlePath.Replace("'", "\\'").Replace('\\', '/').Replace(":/", "\\:/"),
charsetParam,
seconds.ToString(UsCulture));
}
@@ -698,7 +698,7 @@ namespace MediaBrowser.Api.Playback
var mediaPath = state.MediaPath ?? string.Empty;
return string.Format("subtitles='{0}:si={1}',setpts=PTS -{2}/TB",
- mediaPath.Replace('\\', '/').Replace(":/", "\\:/"),
+ mediaPath.Replace("'", "\\'").Replace('\\', '/').Replace(":/", "\\:/"),
state.InternalSubtitleStreamOffset.ToString(UsCulture),
seconds.ToString(UsCulture));
}
@@ -769,26 +769,31 @@ namespace MediaBrowser.Api.Playback
/// <returns>System.Nullable{System.Int32}.</returns>
private int? GetNumAudioChannelsParam(StreamRequest request, MediaStream audioStream, string outputAudioCodec)
{
- if (audioStream != null)
- {
- var codec = outputAudioCodec ?? string.Empty;
+ var inputChannels = audioStream == null
+ ? null
+ : audioStream.Channels;
- if (audioStream.Channels > 2 && codec.IndexOf("wma", StringComparison.OrdinalIgnoreCase) != -1)
- {
- // wmav2 currently only supports two channel output
- return 2;
- }
+ var codec = outputAudioCodec ?? string.Empty;
+
+ if (codec.IndexOf("wma", StringComparison.OrdinalIgnoreCase) != -1)
+ {
+ // wmav2 currently only supports two channel output
+ return Math.Min(2, inputChannels ?? 2);
}
if (request.MaxAudioChannels.HasValue)
{
- if (audioStream != null && audioStream.Channels.HasValue)
+ if (inputChannels.HasValue)
{
- return Math.Min(request.MaxAudioChannels.Value, audioStream.Channels.Value);
+ return Math.Min(request.MaxAudioChannels.Value, inputChannels.Value);
}
+ var channelLimit = codec.IndexOf("mp3", StringComparison.OrdinalIgnoreCase) != -1
+ ? 2
+ : 5;
+
// If we don't have any media info then limit it to 5 to prevent encoding errors due to asking for too many channels
- return Math.Min(request.MaxAudioChannels.Value, 5);
+ return Math.Min(request.MaxAudioChannels.Value, channelLimit);
}
return request.AudioChannels;
@@ -1055,7 +1060,7 @@ namespace MediaBrowser.Api.Playback
private void StartThrottler(StreamState state, TranscodingJob transcodingJob)
{
- if (state.InputProtocol == MediaProtocol.File &&
+ if (EnableThrottling(state) && state.InputProtocol == MediaProtocol.File &&
state.RunTimeTicks.HasValue &&
state.VideoType == VideoType.VideoFile &&
!string.Equals(state.OutputVideoCodec, "copy", StringComparison.OrdinalIgnoreCase))
@@ -1068,6 +1073,11 @@ namespace MediaBrowser.Api.Playback
}
}
+ protected virtual bool EnableThrottling(StreamState state)
+ {
+ return true;
+ }
+
private async void StartStreamingLog(TranscodingJob transcodingJob, StreamState state, Stream source, Stream target)
{
try
@@ -1690,6 +1700,11 @@ namespace MediaBrowser.Api.Playback
private void TryStreamCopy(StreamState state, VideoStreamRequest videoRequest)
{
+ if (!EnableStreamCopy)
+ {
+ return;
+ }
+
if (state.VideoStream != null && CanStreamCopyVideo(videoRequest, state.VideoStream))
{
state.OutputVideoCodec = "copy";
@@ -1701,6 +1716,14 @@ namespace MediaBrowser.Api.Playback
}
}
+ protected virtual bool EnableStreamCopy
+ {
+ get
+ {
+ return true;
+ }
+ }
+
private void AttachMediaSourceInfo(StreamState state,
MediaSourceInfo mediaSource,
VideoStreamRequest videoRequest,
diff --git a/MediaBrowser.Api/Playback/Hls/BaseHlsService.cs b/MediaBrowser.Api/Playback/Hls/BaseHlsService.cs
index b10c02e17..b2ffeca3d 100644
--- a/MediaBrowser.Api/Playback/Hls/BaseHlsService.cs
+++ b/MediaBrowser.Api/Playback/Hls/BaseHlsService.cs
@@ -7,13 +7,13 @@ using MediaBrowser.Controller.MediaEncoding;
using MediaBrowser.Model.Extensions;
using MediaBrowser.Model.IO;
using MediaBrowser.Model.Net;
+using MediaBrowser.Model.Serialization;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
-using MediaBrowser.Model.Serialization;
namespace MediaBrowser.Api.Playback.Hls
{
@@ -100,6 +100,7 @@ namespace MediaBrowser.Api.Playback.Hls
try
{
job = await StartFfMpeg(state, playlist, cancellationTokenSource).ConfigureAwait(false);
+ job.IsLiveOutput = isLive;
}
catch
{
@@ -133,7 +134,7 @@ namespace MediaBrowser.Api.Playback.Hls
var appendBaselineStream = false;
var baselineStreamBitrate = 64000;
- var hlsVideoRequest = state.VideoRequest as GetHlsVideoStream;
+ var hlsVideoRequest = state.VideoRequest as GetHlsVideoStreamLegacy;
if (hlsVideoRequest != null)
{
appendBaselineStream = hlsVideoRequest.AppendBaselineStream;
@@ -244,7 +245,7 @@ namespace MediaBrowser.Api.Playback.Hls
protected override string GetCommandLineArguments(string outputPath, StreamState state, bool isEncoding)
{
- var hlsVideoRequest = state.VideoRequest as GetHlsVideoStream;
+ var hlsVideoRequest = state.VideoRequest as GetHlsVideoStreamLegacy;
var itsOffsetMs = hlsVideoRequest == null
? 0
diff --git a/MediaBrowser.Api/Playback/Hls/DynamicHlsService.cs b/MediaBrowser.Api/Playback/Hls/DynamicHlsService.cs
index 1f6bc242d..fdddc0c37 100644
--- a/MediaBrowser.Api/Playback/Hls/DynamicHlsService.cs
+++ b/MediaBrowser.Api/Playback/Hls/DynamicHlsService.cs
@@ -13,6 +13,7 @@ using MediaBrowser.Model.IO;
using MediaBrowser.Model.Serialization;
using ServiceStack;
using System;
+using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
@@ -29,27 +30,60 @@ namespace MediaBrowser.Api.Playback.Hls
/// </summary>
[Route("/Videos/{Id}/master.m3u8", "GET", Summary = "Gets a video stream using HTTP live streaming.")]
[Route("/Videos/{Id}/master.m3u8", "HEAD", Summary = "Gets a video stream using HTTP live streaming.")]
- public class GetMasterHlsVideoStream : VideoStreamRequest
+ public class GetMasterHlsVideoPlaylist : VideoStreamRequest, IMasterHlsRequest
{
public bool EnableAdaptiveBitrateStreaming { get; set; }
- public GetMasterHlsVideoStream()
+ public GetMasterHlsVideoPlaylist()
{
EnableAdaptiveBitrateStreaming = true;
}
}
+ [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.")]
- public class GetMainHlsVideoStream : VideoStreamRequest
+ public class GetVariantHlsVideoPlaylist : VideoStreamRequest
+ {
+ }
+
+ [Route("/Audio/{Id}/main.m3u8", "GET", Summary = "Gets an audio stream using HTTP live streaming.")]
+ public class GetVariantHlsAudioPlaylist : StreamRequest
{
}
- /// <summary>
- /// Class GetHlsVideoSegment
- /// </summary>
[Route("/Videos/{Id}/hlsdynamic/{PlaylistId}/{SegmentId}.ts", "GET")]
[Api(Description = "Gets an Http live streaming segment file. Internal use only.")]
- public class GetDynamicHlsVideoSegment : VideoStreamRequest
+ 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; }
+ }
+
+ [Route("/Audio/{Id}/hlsdynamic/{PlaylistId}/{SegmentId}.aac", "GET")]
+ [Route("/Audio/{Id}/hlsdynamic/{PlaylistId}/{SegmentId}.ts", "GET")]
+ [Api(Description = "Gets an Http live streaming segment file. Internal use only.")]
+ public class GetHlsAudioSegment : StreamRequest
{
public string PlaylistId { get; set; }
@@ -62,34 +96,55 @@ namespace MediaBrowser.Api.Playback.Hls
public class DynamicHlsService : BaseHlsService
{
- 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, INetworkManager networkManager) : base(serverConfig, userManager, libraryManager, isoManager, mediaEncoder, fileSystem, dlnaManager, subtitleEncoder, deviceManager, mediaSourceManager, zipClient, jsonSerializer)
+ 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, INetworkManager networkManager)
+ : base(serverConfig, userManager, libraryManager, isoManager, mediaEncoder, fileSystem, dlnaManager, subtitleEncoder, deviceManager, mediaSourceManager, zipClient, jsonSerializer)
{
NetworkManager = networkManager;
}
protected INetworkManager NetworkManager { get; private set; }
- public Task<object> Get(GetMasterHlsVideoStream request)
+ public Task<object> Get(GetMasterHlsVideoPlaylist request)
+ {
+ return GetMasterPlaylistInternal(request, "GET");
+ }
+
+ public Task<object> Head(GetMasterHlsVideoPlaylist request)
{
- return GetAsync(request, "GET");
+ return GetMasterPlaylistInternal(request, "HEAD");
}
- public Task<object> Head(GetMasterHlsVideoStream request)
+ public Task<object> Get(GetMasterHlsAudioPlaylist request)
{
- return GetAsync(request, "HEAD");
+ return GetMasterPlaylistInternal(request, "GET");
}
- public Task<object> Get(GetMainHlsVideoStream request)
+ public Task<object> Head(GetMasterHlsAudioPlaylist request)
{
- return GetPlaylistAsync(request, "main");
+ 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(GetDynamicHlsVideoSegment request)
+ public Task<object> Get(GetHlsAudioSegment request)
{
return GetDynamicSegment(request, request.SegmentId);
}
- private async Task<object> GetDynamicSegment(VideoStreamRequest request, string segmentId)
+ private async Task<object> GetDynamicSegment(StreamRequest request, string segmentId)
{
if ((request.StartTimeTicks ?? 0) > 0)
{
@@ -105,7 +160,7 @@ namespace MediaBrowser.Api.Playback.Hls
var playlistPath = Path.ChangeExtension(state.OutputFilePath, ".m3u8");
- var segmentPath = GetSegmentPath(playlistPath, requestedIndex);
+ var segmentPath = GetSegmentPath(state, playlistPath, requestedIndex);
var segmentLength = state.SegmentLength;
var segmentExtension = GetSegmentFileExtension(state);
@@ -130,7 +185,7 @@ namespace MediaBrowser.Api.Playback.Hls
{
var startTranscoding = false;
- var currentTranscodingIndex = GetCurrentTranscodingIndex(playlistPath, segmentExtension);
+ var currentTranscodingIndex = GetCurrentTranscodingIndex(playlistPath, request.PlaySessionId, segmentExtension);
var segmentGapRequiringTranscodingChange = 24 / state.SegmentLength;
if (currentTranscodingIndex == null)
@@ -155,12 +210,14 @@ namespace MediaBrowser.Api.Playback.Hls
{
ApiEntryPoint.Instance.KillTranscodingJobs(request.DeviceId, request.PlaySessionId, p => false);
+ await ReadSegmentLengths(playlistPath).ConfigureAwait(false);
+
if (currentTranscodingIndex.HasValue)
{
DeleteLastFile(playlistPath, segmentExtension, 0);
}
- request.StartTimeTicks = GetSeekPositionTicks(state, requestedIndex);
+ request.StartTimeTicks = GetSeekPositionTicks(state, playlistPath, requestedIndex);
job = await StartFfMpeg(state, playlistPath, cancellationTokenSource).ConfigureAwait(false);
}
@@ -187,28 +244,92 @@ namespace MediaBrowser.Api.Playback.Hls
ApiEntryPoint.Instance.TranscodingStartLock.Release();
}
- Logger.Info("waiting for {0}", segmentPath);
- while (!File.Exists(segmentPath))
- {
- await Task.Delay(50, cancellationToken).ConfigureAwait(false);
- }
+ //Logger.Info("waiting for {0}", segmentPath);
+ //while (!File.Exists(segmentPath))
+ //{
+ // await Task.Delay(50, cancellationToken).ConfigureAwait(false);
+ //}
Logger.Info("returning {0}", segmentPath);
job = job ?? ApiEntryPoint.Instance.OnTranscodeBeginRequest(playlistPath, TranscodingJobType);
return await GetSegmentResult(playlistPath, segmentPath, requestedIndex, segmentLength, job, cancellationToken).ConfigureAwait(false);
}
- private long GetSeekPositionTicks(StreamState state, int requestedIndex)
+ private static readonly ConcurrentDictionary<string, double> SegmentLengths = new ConcurrentDictionary<string, double>(StringComparer.OrdinalIgnoreCase);
+ private async Task ReadSegmentLengths(string playlist)
{
- var startSeconds = requestedIndex * state.SegmentLength;
- var position = TimeSpan.FromSeconds(startSeconds).Ticks;
+ try
+ {
+ using (var fileStream = GetPlaylistFileStream(playlist))
+ {
+ using (var reader = new StreamReader(fileStream))
+ {
+ double duration = -1;
+
+ while (!reader.EndOfStream)
+ {
+ var text = await reader.ReadLineAsync().ConfigureAwait(false);
+
+ if (text.StartsWith("#EXTINF", StringComparison.OrdinalIgnoreCase))
+ {
+ var parts = text.Split(new[] { ':' }, 2);
+ if (parts.Length == 2)
+ {
+ var time = parts[1].Trim(new[] { ',' }).Trim();
+ double timeValue;
+ if (double.TryParse(time, NumberStyles.Any, CultureInfo.InvariantCulture, out timeValue))
+ {
+ duration = timeValue;
+ continue;
+ }
+ }
+ }
+ else if (duration != -1)
+ {
+ SegmentLengths.AddOrUpdate(text, duration, (k, v) => duration);
+ Logger.Debug("Added segment length of {0} for {1}", duration, text);
+ }
+
+ duration = -1;
+ }
+ }
+ }
+ }
+ catch (FileNotFoundException)
+ {
+
+ }
+ }
+
+ private long GetSeekPositionTicks(StreamState state, string playlist, int requestedIndex)
+ {
+ double startSeconds = 0;
+
+ for (var i = 0; i < requestedIndex; i++)
+ {
+ var segmentPath = GetSegmentPath(state, playlist, i);
+ double length;
+ if (SegmentLengths.TryGetValue(Path.GetFileName(segmentPath), out length))
+ {
+ Logger.Debug("Found segment length of {0} for index {1}", length, i);
+ startSeconds += length;
+ }
+ else
+ {
+ startSeconds += state.SegmentLength;
+ }
+ }
+
+ var position = TimeSpan.FromSeconds(startSeconds).Ticks;
return position;
}
- public int? GetCurrentTranscodingIndex(string playlist, string segmentExtension)
+ public int? GetCurrentTranscodingIndex(string playlist, string playSessionId, string segmentExtension)
{
- var job = ApiEntryPoint.Instance.GetTranscodingJob(playlist, TranscodingJobType);
+ var job = string.IsNullOrWhiteSpace(playSessionId) ?
+ ApiEntryPoint.Instance.GetTranscodingJob(playlist, TranscodingJobType) :
+ ApiEntryPoint.Instance.GetTranscodingJobByPlaySessionId(playSessionId);
if (job == null || job.HasExited)
{
@@ -292,7 +413,7 @@ namespace MediaBrowser.Api.Playback.Hls
{
var segmentId = "0";
- var segmentRequest = request as GetDynamicHlsVideoSegment;
+ var segmentRequest = request as GetHlsVideoSegment;
if (segmentRequest != null)
{
segmentId = segmentRequest.SegmentId;
@@ -301,13 +422,13 @@ namespace MediaBrowser.Api.Playback.Hls
return int.Parse(segmentId, NumberStyles.Integer, UsCulture);
}
- private string GetSegmentPath(string playlist, int index)
+ private string GetSegmentPath(StreamState state, string playlist, int index)
{
var folder = Path.GetDirectoryName(playlist);
var filename = Path.GetFileNameWithoutExtension(playlist);
- return Path.Combine(folder, filename + index.ToString(UsCulture) + ".ts");
+ return Path.Combine(folder, filename + index.ToString(UsCulture) + GetSegmentFileExtension(state));
}
private async Task<object> GetSegmentResult(string playlistPath,
@@ -325,21 +446,26 @@ namespace MediaBrowser.Api.Playback.Hls
var segmentFilename = Path.GetFileName(segmentPath);
- using (var fileStream = GetPlaylistFileStream(playlistPath))
+ while (!cancellationToken.IsCancellationRequested)
{
- using (var reader = new StreamReader(fileStream))
+ using (var fileStream = GetPlaylistFileStream(playlistPath))
{
- while (!reader.EndOfStream)
+ using (var reader = new StreamReader(fileStream))
{
- var text = await reader.ReadLineAsync().ConfigureAwait(false);
-
- // If it appears in the playlist, it's done
- if (text.IndexOf(segmentFilename, StringComparison.OrdinalIgnoreCase) != -1)
+ while (!reader.EndOfStream)
{
- return GetSegmentResult(segmentPath, segmentIndex, segmentLength, transcodingJob);
+ var text = await reader.ReadLineAsync().ConfigureAwait(false);
+
+ // If it appears in the playlist, it's done
+ if (text.IndexOf(segmentFilename, StringComparison.OrdinalIgnoreCase) != -1)
+ {
+ return GetSegmentResult(segmentPath, segmentIndex, segmentLength, transcodingJob);
+ }
}
}
}
+
+ await Task.Delay(100, cancellationToken).ConfigureAwait(false);
}
// if a different file is encoding, it's done
@@ -349,34 +475,35 @@ namespace MediaBrowser.Api.Playback.Hls
//return GetSegmentResult(segmentPath, segmentIndex);
//}
- // Wait for the file to stop being written to, then stream it
- var length = new FileInfo(segmentPath).Length;
- var eofCount = 0;
-
- while (eofCount < 10)
- {
- var info = new FileInfo(segmentPath);
-
- if (!info.Exists)
- {
- break;
- }
-
- var newLength = info.Length;
+ //// Wait for the file to stop being written to, then stream it
+ //var length = new FileInfo(segmentPath).Length;
+ //var eofCount = 0;
- if (newLength == length)
- {
- eofCount++;
- }
- else
- {
- eofCount = 0;
- }
-
- length = newLength;
- await Task.Delay(100, cancellationToken).ConfigureAwait(false);
- }
+ //while (eofCount < 10)
+ //{
+ // var info = new FileInfo(segmentPath);
+
+ // if (!info.Exists)
+ // {
+ // break;
+ // }
+
+ // var newLength = info.Length;
+
+ // if (newLength == length)
+ // {
+ // eofCount++;
+ // }
+ // else
+ // {
+ // eofCount = 0;
+ // }
+
+ // length = newLength;
+ // await Task.Delay(100, cancellationToken).ConfigureAwait(false);
+ //}
+ cancellationToken.ThrowIfCancellationRequested();
return GetSegmentResult(segmentPath, segmentIndex, segmentLength, transcodingJob);
}
@@ -400,7 +527,7 @@ namespace MediaBrowser.Api.Playback.Hls
});
}
- private async Task<object> GetAsync(GetMasterHlsVideoStream request, string method)
+ private async Task<object> GetMasterPlaylistInternal(StreamRequest request, string method)
{
var state = await GetState(request, CancellationToken.None).ConfigureAwait(false);
@@ -437,14 +564,16 @@ namespace MediaBrowser.Api.Playback.Hls
var playlistUrl = isLiveStream ? "live.m3u8" : "main.m3u8";
playlistUrl += queryString;
- var request = (GetMasterHlsVideoStream)state.Request;
+ var request = state.Request;
var subtitleStreams = state.MediaSource
.MediaStreams
.Where(i => i.IsTextSubtitleStream)
.ToList();
- var subtitleGroup = subtitleStreams.Count > 0 && request.SubtitleMethod == SubtitleDeliveryMethod.Hls ?
+ var subtitleGroup = subtitleStreams.Count > 0 &&
+ (request is GetMasterHlsVideoPlaylist) &&
+ ((GetMasterHlsVideoPlaylist)request).SubtitleMethod == SubtitleDeliveryMethod.Hls ?
"subs" :
null;
@@ -452,7 +581,7 @@ namespace MediaBrowser.Api.Playback.Hls
if (EnableAdaptiveBitrateStreaming(state, isLiveStream))
{
- var requestedVideoBitrate = state.VideoRequest.VideoBitRate.Value;
+ var requestedVideoBitrate = state.VideoRequest == null ? 0 : state.VideoRequest.VideoBitRate ?? 0;
// By default, vary by just 200k
var variation = GetBitrateVariation(totalBitrate);
@@ -522,7 +651,7 @@ namespace MediaBrowser.Api.Playback.Hls
return false;
}
- var request = state.Request as GetMasterHlsVideoStream;
+ var request = state.Request as IMasterHlsRequest;
if (request != null && !request.EnableAdaptiveBitrateStreaming)
{
return false;
@@ -544,6 +673,11 @@ namespace MediaBrowser.Api.Playback.Hls
return false;
}
+ if (!state.IsOutputVideo)
+ {
+ return false;
+ }
+
// Having problems in android
return false;
//return state.VideoRequest.VideoBitRate.HasValue;
@@ -599,7 +733,7 @@ namespace MediaBrowser.Api.Playback.Hls
return variation;
}
- private async Task<object> GetPlaylistAsync(VideoStreamRequest request, string name)
+ private async Task<object> GetVariantPlaylistInternal(StreamRequest request, bool isOutputVideo, string name)
{
var state = await GetState(request, CancellationToken.None).ConfigureAwait(false);
@@ -607,7 +741,7 @@ namespace MediaBrowser.Api.Playback.Hls
builder.AppendLine("#EXTM3U");
builder.AppendLine("#EXT-X-VERSION:3");
- builder.AppendLine("#EXT-X-TARGETDURATION:" + state.SegmentLength.ToString(UsCulture));
+ builder.AppendLine("#EXT-X-TARGETDURATION:" + (state.SegmentLength).ToString(UsCulture));
builder.AppendLine("#EXT-X-MEDIA-SEQUENCE:0");
var queryStringIndex = Request.RawUrl.IndexOf('?');
@@ -623,10 +757,11 @@ namespace MediaBrowser.Api.Playback.Hls
builder.AppendLine("#EXTINF:" + length.ToString(UsCulture) + ",");
- builder.AppendLine(string.Format("hlsdynamic/{0}/{1}.ts{2}",
+ builder.AppendLine(string.Format("hlsdynamic/{0}/{1}{2}{3}",
name,
index.ToString(UsCulture),
+ GetSegmentFileExtension(isOutputVideo),
queryString));
seconds -= state.SegmentLength;
@@ -642,6 +777,28 @@ namespace MediaBrowser.Api.Playback.Hls
protected override string GetAudioArguments(StreamState state)
{
+ if (!state.IsOutputVideo)
+ {
+ var audioTranscodeParams = new List<string>();
+ 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());
+ }
+
var codec = state.OutputAudioCodec;
if (string.Equals(codec, "copy", StringComparison.OrdinalIgnoreCase))
@@ -672,6 +829,11 @@ namespace MediaBrowser.Api.Playback.Hls
protected override string GetVideoArguments(StreamState state)
{
+ if (!state.IsOutputVideo)
+ {
+ return string.Empty;
+ }
+
var codec = state.OutputVideoCodec;
var args = "-codec:v:0 " + codec;
@@ -684,30 +846,36 @@ namespace MediaBrowser.Api.Playback.Hls
// See if we can save come cpu cycles by avoiding encoding
if (codec.Equals("copy", StringComparison.OrdinalIgnoreCase))
{
- return state.VideoStream != null && IsH264(state.VideoStream) ?
- args + " -bsf:v h264_mp4toannexb" :
- args;
+ args += state.VideoStream != null && IsH264(state.VideoStream)
+ ? args + " -bsf:v h264_mp4toannexb"
+ : args;
}
+ else
+ {
+ var keyFrameArg = string.Format(" -force_key_frames expr:gte(t,n_forced*{0})",
+ state.SegmentLength.ToString(UsCulture));
- var keyFrameArg = string.Format(" -force_key_frames expr:gte(t,n_forced*{0})",
- state.SegmentLength.ToString(UsCulture));
+ var hasGraphicalSubs = state.SubtitleStream != null && !state.SubtitleStream.IsTextSubtitleStream;
- var hasGraphicalSubs = state.SubtitleStream != null && !state.SubtitleStream.IsTextSubtitleStream;
+ args += " " + GetVideoQualityParam(state, H264Encoder, true) + keyFrameArg;
- args += " " + GetVideoQualityParam(state, H264Encoder, true) + keyFrameArg;
+ //args += " -mixed-refs 0 -refs 3 -x264opts b_pyramid=0:weightb=0:weightp=0";
- // Add resolution params, if specified
- if (!hasGraphicalSubs)
- {
- args += GetOutputSizeParam(state, codec, false);
- }
+ // Add resolution params, if specified
+ if (!hasGraphicalSubs)
+ {
+ args += GetOutputSizeParam(state, codec, false);
+ }
- // This is for internal graphical subs
- if (hasGraphicalSubs)
- {
- args += GetGraphicalSubtitleParam(state, codec);
+ // This is for internal graphical subs
+ if (hasGraphicalSubs)
+ {
+ args += GetGraphicalSubtitleParam(state, codec);
+ }
}
+ args += " -flags +loop-global_header -sc_threshold 0";
+
return args;
}
@@ -715,43 +883,96 @@ namespace MediaBrowser.Api.Playback.Hls
{
var threads = GetNumberOfThreads(state, false);
- var inputModifier = GetInputModifier(state);
+ var inputModifier = GetInputModifier(state, false);
// If isEncoding is true we're actually starting ffmpeg
var startNumberParam = isEncoding ? GetStartNumber(state).ToString(UsCulture) : "0";
- if (state.EnableGenericHlsSegmenter)
+ var toTimeParam = string.Empty;
+ if (EnableSplitTranscoding(state))
{
- var outputTsArg = Path.Combine(Path.GetDirectoryName(outputPath), Path.GetFileNameWithoutExtension(outputPath)) + "%d.ts";
+ var startTime = state.Request.StartTimeTicks ?? 0;
+ var durationSeconds = ApiEntryPoint.Instance.GetEncodingOptions().ThrottleThresholdInSeconds;
+
+ var endTime = startTime + TimeSpan.FromSeconds(durationSeconds).Ticks;
+ endTime = Math.Min(endTime, state.RunTimeTicks.Value);
+
+ if (endTime < state.RunTimeTicks.Value)
+ {
+ //toTimeParam = " -to " + MediaEncoder.GetTimeParameter(endTime);
+ toTimeParam = " -t " + MediaEncoder.GetTimeParameter(TimeSpan.FromSeconds(durationSeconds).Ticks);
+ }
+ }
- return string.Format("{0} {1} -map_metadata -1 -threads {2} {3} {4} -flags -global_header -sc_threshold 0 {5} -f segment -segment_time {6} -segment_start_number {7} -segment_list \"{8}\" -y \"{9}\"",
- inputModifier,
- GetInputArgument(state),
- threads,
- GetMapArgs(state),
- GetVideoArguments(state),
- GetAudioArguments(state),
- state.SegmentLength.ToString(UsCulture),
- startNumberParam,
- outputPath,
- outputTsArg
- ).Trim();
+ var timestampOffsetParam = string.Empty;
+ if (state.IsOutputVideo)
+ {
+ timestampOffsetParam = " -output_ts_offset " + MediaEncoder.GetTimeParameter(state.Request.StartTimeTicks ?? 0).ToString(CultureInfo.InvariantCulture);
}
+
+ var mapArgs = state.IsOutputVideo ? GetMapArgs(state) : string.Empty;
+
+ //var outputTsArg = Path.Combine(Path.GetDirectoryName(outputPath), Path.GetFileNameWithoutExtension(outputPath)) + "%d" + GetSegmentFileExtension(state);
- return string.Format("{0} {1} -map_metadata -1 -threads {2} {3} {4} -flags -global_header -copyts -sc_threshold 0 {5} -hls_time {6} -start_number {7} -hls_list_size {8} -y \"{9}\"",
+ //return string.Format("{0} {11} {1}{10} -map_metadata -1 -threads {2} {3} {4} {5} -f segment -segment_time {6} -segment_format mpegts -segment_list_type m3u8 -segment_start_number {7} -segment_list \"{8}\" -y \"{9}\"",
+ // inputModifier,
+ // GetInputArgument(state),
+ // threads,
+ // mapArgs,
+ // GetVideoArguments(state),
+ // GetAudioArguments(state),
+ // state.SegmentLength.ToString(UsCulture),
+ // startNumberParam,
+ // outputPath,
+ // outputTsArg,
+ // slowSeekParam,
+ // toTimeParam
+ // ).Trim();
+
+ return string.Format("{0}{11} {1} -map_metadata -1 -threads {2} {3} {4}{5} {6} -hls_time {7} -start_number {8} -hls_list_size {9} -y \"{10}\"",
inputModifier,
GetInputArgument(state),
threads,
- GetMapArgs(state),
+ mapArgs,
GetVideoArguments(state),
+ timestampOffsetParam,
GetAudioArguments(state),
state.SegmentLength.ToString(UsCulture),
startNumberParam,
state.HlsListSize.ToString(UsCulture),
- outputPath
+ outputPath,
+ toTimeParam
).Trim();
}
+ protected override bool EnableThrottling(StreamState state)
+ {
+ return !EnableSplitTranscoding(state);
+ }
+
+ private bool EnableSplitTranscoding(StreamState state)
+ {
+ if (string.Equals(Request.QueryString["EnableSplitTranscoding"], "false", StringComparison.OrdinalIgnoreCase))
+ {
+ return false;
+ }
+
+ if (string.Equals(state.OutputVideoCodec, "copy", StringComparison.OrdinalIgnoreCase))
+ {
+ return false;
+ }
+
+ return state.RunTimeTicks.HasValue && state.IsOutputVideo;
+ }
+
+ protected override bool EnableStreamCopy
+ {
+ get
+ {
+ return false;
+ }
+ }
+
/// <summary>
/// Gets the segment file extension.
/// </summary>
@@ -759,7 +980,12 @@ namespace MediaBrowser.Api.Playback.Hls
/// <returns>System.String.</returns>
protected override string GetSegmentFileExtension(StreamState state)
{
- return ".ts";
+ return GetSegmentFileExtension(state.IsOutputVideo);
+ }
+
+ protected string GetSegmentFileExtension(bool isOutputVideo)
+ {
+ return isOutputVideo ? ".ts" : ".ts";
}
}
}
diff --git a/MediaBrowser.Api/Playback/Hls/HlsSegmentService.cs b/MediaBrowser.Api/Playback/Hls/HlsSegmentService.cs
index 5d8c67abe..b44d7f660 100644
--- a/MediaBrowser.Api/Playback/Hls/HlsSegmentService.cs
+++ b/MediaBrowser.Api/Playback/Hls/HlsSegmentService.cs
@@ -14,8 +14,10 @@ namespace MediaBrowser.Api.Playback.Hls
[Route("/Audio/{Id}/hls/{SegmentId}/stream.mp3", "GET")]
[Route("/Audio/{Id}/hls/{SegmentId}/stream.aac", "GET")]
[Api(Description = "Gets an Http live streaming segment file. Internal use only.")]
- public class GetHlsAudioSegment
+ public class GetHlsAudioSegmentLegacy
{
+ // TODO: Deprecate with new iOS app
+
/// <summary>
/// Gets or sets the id.
/// </summary>
@@ -30,11 +32,30 @@ namespace MediaBrowser.Api.Playback.Hls
}
/// <summary>
+ /// Class GetHlsVideoStream
+ /// </summary>
+ [Route("/Videos/{Id}/stream.m3u8", "GET")]
+ [Api(Description = "Gets a video stream using HTTP live streaming.")]
+ public class GetHlsVideoStreamLegacy : VideoStreamRequest
+ {
+ // TODO: Deprecate with new iOS app
+
+ [ApiMember(Name = "BaselineStreamAudioBitRate", Description = "Optional. Specify the audio bitrate for the baseline stream.", IsRequired = false, DataType = "int", ParameterType = "query", Verb = "GET")]
+ public int? BaselineStreamAudioBitRate { get; set; }
+
+ [ApiMember(Name = "AppendBaselineStream", Description = "Optional. Whether or not to include a baseline audio-only stream in the master playlist.", IsRequired = false, DataType = "bool", ParameterType = "query", Verb = "GET")]
+ public bool AppendBaselineStream { get; set; }
+
+ [ApiMember(Name = "TimeStampOffsetMs", Description = "Optional. Alter the timestamps in the playlist by a given amount, in ms. Default is 1000.", IsRequired = false, DataType = "int", ParameterType = "query", Verb = "GET")]
+ public int TimeStampOffsetMs { 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
+ public class GetHlsPlaylistLegacy
{
// TODO: Deprecate with new iOS app
@@ -63,8 +84,10 @@ namespace MediaBrowser.Api.Playback.Hls
/// </summary>
[Route("/Videos/{Id}/hls/{PlaylistId}/{SegmentId}.ts", "GET")]
[Api(Description = "Gets an Http live streaming segment file. Internal use only.")]
- public class GetHlsVideoSegment : VideoStreamRequest
+ public class GetHlsVideoSegmentLegacy : VideoStreamRequest
{
+ // TODO: Deprecate with new iOS app
+
public string PlaylistId { get; set; }
/// <summary>
@@ -85,7 +108,7 @@ namespace MediaBrowser.Api.Playback.Hls
_config = config;
}
- public object Get(GetHlsPlaylist request)
+ public object Get(GetHlsPlaylistLegacy request)
{
var file = request.PlaylistId + Path.GetExtension(Request.PathInfo);
file = Path.Combine(_appPaths.TranscodingTempPath, file);
@@ -103,7 +126,7 @@ namespace MediaBrowser.Api.Playback.Hls
/// </summary>
/// <param name="request">The request.</param>
/// <returns>System.Object.</returns>
- public object Get(GetHlsVideoSegment request)
+ public object Get(GetHlsVideoSegmentLegacy request)
{
var file = request.SegmentId + Path.GetExtension(Request.PathInfo);
file = Path.Combine(_config.ApplicationPaths.TranscodingTempPath, file);
@@ -121,7 +144,7 @@ namespace MediaBrowser.Api.Playback.Hls
/// </summary>
/// <param name="request">The request.</param>
/// <returns>System.Object.</returns>
- public object Get(GetHlsAudioSegment request)
+ public object Get(GetHlsAudioSegmentLegacy request)
{
// TODO: Deprecate with new iOS app
var file = request.SegmentId + Path.GetExtension(Request.PathInfo);
diff --git a/MediaBrowser.Api/Playback/Hls/VideoHlsService.cs b/MediaBrowser.Api/Playback/Hls/VideoHlsService.cs
index 626df59f2..f21be190f 100644
--- a/MediaBrowser.Api/Playback/Hls/VideoHlsService.cs
+++ b/MediaBrowser.Api/Playback/Hls/VideoHlsService.cs
@@ -11,25 +11,6 @@ using System;
namespace MediaBrowser.Api.Playback.Hls
{
- /// <summary>
- /// Class GetHlsVideoStream
- /// </summary>
- [Route("/Videos/{Id}/stream.m3u8", "GET")]
- [Api(Description = "Gets a video stream using HTTP live streaming.")]
- public class GetHlsVideoStream : VideoStreamRequest
- {
- // TODO: Deprecate with new iOS app
-
- [ApiMember(Name = "BaselineStreamAudioBitRate", Description = "Optional. Specify the audio bitrate for the baseline stream.", IsRequired = false, DataType = "int", ParameterType = "query", Verb = "GET")]
- public int? BaselineStreamAudioBitRate { get; set; }
-
- [ApiMember(Name = "AppendBaselineStream", Description = "Optional. Whether or not to include a baseline audio-only stream in the master playlist.", IsRequired = false, DataType = "bool", ParameterType = "query", Verb = "GET")]
- public bool AppendBaselineStream { get; set; }
-
- [ApiMember(Name = "TimeStampOffsetMs", Description = "Optional. Alter the timestamps in the playlist by a given amount, in ms. Default is 1000.", IsRequired = false, DataType = "int", ParameterType = "query", Verb = "GET")]
- public int TimeStampOffsetMs { get; set; }
- }
-
[Route("/Videos/{Id}/live.m3u8", "GET")]
[Api(Description = "Gets a video stream using HTTP live streaming.")]
public class GetLiveHlsStream : VideoStreamRequest
@@ -50,7 +31,7 @@ namespace MediaBrowser.Api.Playback.Hls
/// </summary>
/// <param name="request">The request.</param>
/// <returns>System.Object.</returns>
- public object Get(GetHlsVideoStream request)
+ public object Get(GetHlsVideoStreamLegacy request)
{
return ProcessRequest(request, false);
}
diff --git a/MediaBrowser.Api/Playback/Progressive/VideoService.cs b/MediaBrowser.Api/Playback/Progressive/VideoService.cs
index 27482c50c..283f9671f 100644
--- a/MediaBrowser.Api/Playback/Progressive/VideoService.cs
+++ b/MediaBrowser.Api/Playback/Progressive/VideoService.cs
@@ -15,7 +15,7 @@ using System.IO;
namespace MediaBrowser.Api.Playback.Progressive
{
/// <summary>
- /// Class GetAudioStream
+ /// Class GetVideoStream
/// </summary>
[Route("/Videos/{Id}/stream.ts", "GET")]
[Route("/Videos/{Id}/stream.webm", "GET")]
diff --git a/MediaBrowser.Api/Playback/StreamState.cs b/MediaBrowser.Api/Playback/StreamState.cs
index 2d1e896db..02b7720a4 100644
--- a/MediaBrowser.Api/Playback/StreamState.cs
+++ b/MediaBrowser.Api/Playback/StreamState.cs
@@ -41,7 +41,7 @@ namespace MediaBrowser.Api.Playback
public string InputContainer { get; set; }
public MediaSourceInfo MediaSource { get; set; }
-
+
public MediaStream AudioStream { get; set; }
public MediaStream VideoStream { get; set; }
public MediaStream SubtitleStream { get; set; }
@@ -57,6 +57,10 @@ namespace MediaBrowser.Api.Playback
public MediaProtocol InputProtocol { get; set; }
+ public bool IsOutputVideo
+ {
+ get { return Request is VideoStreamRequest; }
+ }
public bool IsInputVideo { get; set; }
public bool IsInputArchive { get; set; }
@@ -66,7 +70,6 @@ namespace MediaBrowser.Api.Playback
public List<string> PlayableStreamFileNames { get; set; }
public int SegmentLength = 3;
- public bool EnableGenericHlsSegmenter = false;
public int HlsListSize
{
get
diff --git a/MediaBrowser.Api/Playback/TranscodingThrottler.cs b/MediaBrowser.Api/Playback/TranscodingThrottler.cs
index ece455009..fec3dda86 100644
--- a/MediaBrowser.Api/Playback/TranscodingThrottler.cs
+++ b/MediaBrowser.Api/Playback/TranscodingThrottler.cs
@@ -42,7 +42,7 @@ namespace MediaBrowser.Api.Playback
var options = GetOptions();
- if (options.EnableThrottling && IsThrottleAllowed(_job, options.ThrottleThresholdSeconds))
+ if (options.EnableThrottling && IsThrottleAllowed(_job, options.ThrottleThresholdInSeconds))
{
PauseTranscoding();
}