update hls stream copy rules

This commit is contained in:
Luke Pulverenti 2016-04-04 01:07:10 -04:00
parent 25125377e5
commit 64795ef35b
10 changed files with 49 additions and 851 deletions

View File

@ -80,8 +80,6 @@
<Compile Include="FilterService.cs" />
<Compile Include="IHasDtoOptions.cs" />
<Compile Include="Library\ChapterService.cs" />
<Compile Include="Playback\Dash\ManifestBuilder.cs" />
<Compile Include="Playback\Dash\MpegDashService.cs" />
<Compile Include="Playback\MediaInfoService.cs" />
<Compile Include="Playback\TranscodingThrottler.cs" />
<Compile Include="PlaylistService.cs" />

View File

@ -1026,7 +1026,7 @@ namespace MediaBrowser.Api.Playback
StartStreamingLog(transcodingJob, state, process.StandardError.BaseStream, state.LogFileStream);
// Wait for the file to exist before proceeeding
while (!FileSystem.FileExists(state.WaitForPath ?? outputPath) && !transcodingJob.HasExited)
while (!FileSystem.FileExists(state.WaitForPath ?? outputPath) && !transcodingJob.HasExited)
{
await Task.Delay(100, cancellationTokenSource.Token).ConfigureAwait(false);
}
@ -1651,9 +1651,9 @@ namespace MediaBrowser.Api.Playback
if (state.OutputVideoBitrate.HasValue)
{
var resolution = ResolutionNormalizer.Normalize(
state.VideoStream == null ? (int?)null : state.VideoStream.BitRate,
state.OutputVideoBitrate.Value,
state.VideoStream == null ? null : state.VideoStream.Codec,
state.VideoStream == null ? (int?)null : state.VideoStream.BitRate,
state.OutputVideoBitrate.Value,
state.VideoStream == null ? null : state.VideoStream.Codec,
state.OutputVideoCodec,
videoRequest.MaxWidth,
videoRequest.MaxHeight);
@ -1682,7 +1682,7 @@ namespace MediaBrowser.Api.Playback
state.OutputVideoCodec = "copy";
}
if (state.AudioStream != null && CanStreamCopyAudio(videoRequest, state.AudioStream, state.SupportedAudioCodecs))
if (state.AudioStream != null && CanStreamCopyAudio(state, state.SupportedAudioCodecs))
{
state.OutputAudioCodec = "copy";
}
@ -1784,7 +1784,7 @@ namespace MediaBrowser.Api.Playback
{
return false;
}
// Can't stream copy if we're burning in subtitles
if (request.SubtitleStreamIndex.HasValue)
{
@ -1913,8 +1913,11 @@ namespace MediaBrowser.Api.Playback
return Array.FindIndex(list.ToArray(), t => string.Equals(t, profile, StringComparison.OrdinalIgnoreCase));
}
protected virtual bool CanStreamCopyAudio(VideoStreamRequest request, MediaStream audioStream, List<string> supportedAudioCodecs)
protected virtual bool CanStreamCopyAudio(StreamState state, List<string> supportedAudioCodecs)
{
var request = state.VideoRequest;
var audioStream = state.AudioStream;
// Source and target codecs must match
if (string.IsNullOrEmpty(audioStream.Codec) || !supportedAudioCodecs.Contains(audioStream.Codec, StringComparer.OrdinalIgnoreCase))
{
@ -2213,7 +2216,7 @@ namespace MediaBrowser.Api.Playback
inputModifier += " -noaccurate_seek";
}
}
return inputModifier;
}

View File

@ -1,224 +0,0 @@
using System;
using System.Globalization;
using System.Security;
using System.Text;
namespace MediaBrowser.Api.Playback.Dash
{
public class ManifestBuilder
{
protected readonly CultureInfo UsCulture = new CultureInfo("en-US");
public string GetManifestText(StreamState state, string playlistUrl)
{
var builder = new StringBuilder();
var time = TimeSpan.FromTicks(state.RunTimeTicks.Value);
var duration = "PT" + time.Hours.ToString("00", UsCulture) + "H" + time.Minutes.ToString("00", UsCulture) + "M" + time.Seconds.ToString("00", UsCulture) + ".00S";
builder.Append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
builder.AppendFormat(
"<MPD xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"urn:mpeg:dash:schema:mpd:2011\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" xsi:schemaLocation=\"urn:mpeg:DASH:schema:MPD:2011 http://standards.iso.org/ittf/PubliclyAvailableStandards/MPEG-DASH_schema_files/DASH-MPD.xsd\" profiles=\"urn:mpeg:dash:profile:isoff-live:2011\" type=\"static\" mediaPresentationDuration=\"{0}\" minBufferTime=\"PT5.0S\">",
duration);
builder.Append("<ProgramInformation>");
builder.Append("</ProgramInformation>");
builder.Append("<Period start=\"PT0S\">");
builder.Append(GetVideoAdaptationSet(state, playlistUrl));
builder.Append(GetAudioAdaptationSet(state, playlistUrl));
builder.Append("</Period>");
builder.Append("</MPD>");
return builder.ToString();
}
private string GetVideoAdaptationSet(StreamState state, string playlistUrl)
{
var builder = new StringBuilder();
builder.Append("<AdaptationSet id=\"video\" segmentAlignment=\"true\" bitstreamSwitching=\"true\">");
builder.Append(GetVideoRepresentationOpenElement(state));
AppendSegmentList(state, builder, "0", playlistUrl);
builder.Append("</Representation>");
builder.Append("</AdaptationSet>");
return builder.ToString();
}
private string GetAudioAdaptationSet(StreamState state, string playlistUrl)
{
var builder = new StringBuilder();
builder.Append("<AdaptationSet id=\"audio\" segmentAlignment=\"true\" bitstreamSwitching=\"true\">");
builder.Append(GetAudioRepresentationOpenElement(state));
builder.Append("<AudioChannelConfiguration schemeIdUri=\"urn:mpeg:dash:23003:3:audio_channel_configuration:2011\" value=\"6\" />");
AppendSegmentList(state, builder, "1", playlistUrl);
builder.Append("</Representation>");
builder.Append("</AdaptationSet>");
return builder.ToString();
}
private string GetVideoRepresentationOpenElement(StreamState state)
{
var codecs = GetVideoCodecDescriptor(state);
var mime = "video/mp4";
var xml = "<Representation id=\"0\" mimeType=\"" + mime + "\" codecs=\"" + codecs + "\"";
if (state.OutputWidth.HasValue)
{
xml += " width=\"" + state.OutputWidth.Value.ToString(UsCulture) + "\"";
}
if (state.OutputHeight.HasValue)
{
xml += " height=\"" + state.OutputHeight.Value.ToString(UsCulture) + "\"";
}
if (state.OutputVideoBitrate.HasValue)
{
xml += " bandwidth=\"" + state.OutputVideoBitrate.Value.ToString(UsCulture) + "\"";
}
xml += ">";
return xml;
}
private string GetAudioRepresentationOpenElement(StreamState state)
{
var codecs = GetAudioCodecDescriptor(state);
var mime = "audio/mp4";
var xml = "<Representation id=\"1\" mimeType=\"" + mime + "\" codecs=\"" + codecs + "\"";
if (state.OutputAudioSampleRate.HasValue)
{
xml += " audioSamplingRate=\"" + state.OutputAudioSampleRate.Value.ToString(UsCulture) + "\"";
}
if (state.OutputAudioBitrate.HasValue)
{
xml += " bandwidth=\"" + state.OutputAudioBitrate.Value.ToString(UsCulture) + "\"";
}
xml += ">";
return xml;
}
private string GetVideoCodecDescriptor(StreamState state)
{
// https://developer.apple.com/library/ios/documentation/networkinginternet/conceptual/streamingmediaguide/FrequentlyAskedQuestions/FrequentlyAskedQuestions.html
// http://www.chipwreck.de/blog/2010/02/25/html-5-video-tag-and-attributes/
var level = state.TargetVideoLevel ?? 0;
var profile = state.TargetVideoProfile ?? string.Empty;
if (profile.IndexOf("high", StringComparison.OrdinalIgnoreCase) != -1)
{
if (level >= 4.1)
{
return "avc1.640028";
}
if (level >= 4)
{
return "avc1.640028";
}
return "avc1.64001f";
}
if (profile.IndexOf("main", StringComparison.OrdinalIgnoreCase) != -1)
{
if (level >= 4)
{
return "avc1.4d0028";
}
if (level >= 3.1)
{
return "avc1.4d001f";
}
return "avc1.4d001e";
}
if (level >= 3.1)
{
return "avc1.42001f";
}
return "avc1.42E01E";
}
private string GetAudioCodecDescriptor(StreamState state)
{
// https://developer.apple.com/library/ios/documentation/networkinginternet/conceptual/streamingmediaguide/FrequentlyAskedQuestions/FrequentlyAskedQuestions.html
if (string.Equals(state.OutputAudioCodec, "mp3", StringComparison.OrdinalIgnoreCase))
{
return "mp4a.40.34";
}
// AAC 5ch
if (state.OutputAudioChannels.HasValue && state.OutputAudioChannels.Value >= 5)
{
return "mp4a.40.5";
}
// AAC 2ch
return "mp4a.40.2";
}
private void AppendSegmentList(StreamState state, StringBuilder builder, string type, string playlistUrl)
{
var extension = ".m4s";
var seconds = TimeSpan.FromTicks(state.RunTimeTicks ?? 0).TotalSeconds;
var queryStringIndex = playlistUrl.IndexOf('?');
var queryString = queryStringIndex == -1 ? string.Empty : playlistUrl.Substring(queryStringIndex);
var index = 0;
var duration = 1000000 * state.SegmentLength;
builder.AppendFormat("<SegmentList timescale=\"1000000\" duration=\"{0}\" startNumber=\"1\">", duration.ToString(CultureInfo.InvariantCulture));
while (seconds > 0)
{
var filename = index == 0
? "init"
: (index - 1).ToString(UsCulture);
var segmentUrl = string.Format("dash/{3}/{0}{1}{2}",
filename,
extension,
SecurityElement.Escape(queryString),
type);
if (index == 0)
{
builder.AppendFormat("<Initialization sourceURL=\"{0}\"/>", segmentUrl);
}
else
{
builder.AppendFormat("<SegmentURL media=\"{0}\"/>", segmentUrl);
}
seconds -= state.SegmentLength;
index++;
}
builder.Append("</SegmentList>");
}
}
}

View File

@ -1,547 +0,0 @@
using MediaBrowser.Api.Playback.Hls;
using MediaBrowser.Common.Net;
using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Devices;
using MediaBrowser.Controller.Dlna;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.MediaEncoding;
using MediaBrowser.Controller.Net;
using MediaBrowser.Model.IO;
using MediaBrowser.Model.Serialization;
using ServiceStack;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using CommonIO;
using MimeTypes = MediaBrowser.Model.Net.MimeTypes;
namespace MediaBrowser.Api.Playback.Dash
{
/// <summary>
/// Options is needed for chromecast. Threw Head in there since it's related
/// </summary>
[Route("/Videos/{Id}/master.mpd", "GET", Summary = "Gets a video stream using Mpeg dash.")]
[Route("/Videos/{Id}/master.mpd", "HEAD", Summary = "Gets a video stream using Mpeg dash.")]
public class GetMasterManifest : VideoStreamRequest
{
public bool EnableAdaptiveBitrateStreaming { get; set; }
public GetMasterManifest()
{
EnableAdaptiveBitrateStreaming = true;
}
}
[Route("/Videos/{Id}/dash/{RepresentationId}/{SegmentId}.m4s", "GET")]
public class GetDashSegment : VideoStreamRequest
{
/// <summary>
/// Gets or sets the segment id.
/// </summary>
/// <value>The segment id.</value>
public string SegmentId { get; set; }
/// <summary>
/// Gets or sets the representation identifier.
/// </summary>
/// <value>The representation identifier.</value>
public string RepresentationId { get; set; }
}
public class MpegDashService : BaseHlsService
{
public MpegDashService(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 object Get(GetMasterManifest request)
{
var result = GetAsync(request, "GET").Result;
return result;
}
public object Head(GetMasterManifest request)
{
var result = GetAsync(request, "HEAD").Result;
return result;
}
protected override bool EnableOutputInSubFolder
{
get
{
return true;
}
}
private async Task<object> GetAsync(GetMasterManifest request, string method)
{
if (string.IsNullOrEmpty(request.MediaSourceId))
{
throw new ArgumentException("MediaSourceId is required");
}
var state = await GetState(request, CancellationToken.None).ConfigureAwait(false);
var playlistText = string.Empty;
if (string.Equals(method, "GET", StringComparison.OrdinalIgnoreCase))
{
playlistText = new ManifestBuilder().GetManifestText(state, Request.RawUrl);
}
return ResultFactory.GetResult(playlistText, MimeTypes.GetMimeType("playlist.mpd"), new Dictionary<string, string>());
}
public object Get(GetDashSegment request)
{
return GetDynamicSegment(request, request.SegmentId, request.RepresentationId).Result;
}
private async Task<object> GetDynamicSegment(VideoStreamRequest request, string segmentId, string representationId)
{
if ((request.StartTimeTicks ?? 0) > 0)
{
throw new ArgumentException("StartTimeTicks is not allowed.");
}
var cancellationTokenSource = new CancellationTokenSource();
var cancellationToken = cancellationTokenSource.Token;
var requestedIndex = string.Equals(segmentId, "init", StringComparison.OrdinalIgnoreCase) ?
-1 :
int.Parse(segmentId, NumberStyles.Integer, UsCulture);
var state = await GetState(request, cancellationToken).ConfigureAwait(false);
var playlistPath = Path.ChangeExtension(state.OutputFilePath, ".mpd");
var segmentExtension = GetSegmentFileExtension(state);
var segmentPath = FindSegment(playlistPath, representationId, segmentExtension, requestedIndex);
var segmentLength = state.SegmentLength;
TranscodingJob job = null;
if (!string.IsNullOrWhiteSpace(segmentPath))
{
job = ApiEntryPoint.Instance.GetTranscodingJob(playlistPath, TranscodingJobType);
return await GetSegmentResult(playlistPath, segmentPath, requestedIndex, segmentLength, job, cancellationToken).ConfigureAwait(false);
}
await ApiEntryPoint.Instance.TranscodingStartLock.WaitAsync(cancellationTokenSource.Token).ConfigureAwait(false);
try
{
segmentPath = FindSegment(playlistPath, representationId, segmentExtension, requestedIndex);
if (!string.IsNullOrWhiteSpace(segmentPath))
{
job = ApiEntryPoint.Instance.GetTranscodingJob(playlistPath, TranscodingJobType);
return await GetSegmentResult(playlistPath, segmentPath, requestedIndex, segmentLength, job, cancellationToken).ConfigureAwait(false);
}
else
{
if (string.Equals(representationId, "0", StringComparison.OrdinalIgnoreCase))
{
job = ApiEntryPoint.Instance.GetTranscodingJob(playlistPath, TranscodingJobType);
var currentTranscodingIndex = GetCurrentTranscodingIndex(playlistPath, segmentExtension);
var segmentGapRequiringTranscodingChange = 24 / state.SegmentLength;
Logger.Debug("Current transcoding index is {0}. requestedIndex={1}. segmentGapRequiringTranscodingChange={2}", currentTranscodingIndex ?? -2, requestedIndex, segmentGapRequiringTranscodingChange);
if (currentTranscodingIndex == null || requestedIndex < currentTranscodingIndex.Value || requestedIndex - currentTranscodingIndex.Value > segmentGapRequiringTranscodingChange)
{
// If the playlist doesn't already exist, startup ffmpeg
try
{
ApiEntryPoint.Instance.KillTranscodingJobs(request.DeviceId, request.PlaySessionId, p => false);
if (currentTranscodingIndex.HasValue)
{
DeleteLastTranscodedFiles(playlistPath, 0);
}
var positionTicks = GetPositionTicks(state, requestedIndex);
request.StartTimeTicks = positionTicks;
var startNumber = GetStartNumber(state);
var workingDirectory = Path.Combine(Path.GetDirectoryName(playlistPath), (startNumber == -1 ? 0 : startNumber).ToString(CultureInfo.InvariantCulture));
state.WaitForPath = Path.Combine(workingDirectory, Path.GetFileName(playlistPath));
FileSystem.CreateDirectory(workingDirectory);
job = await StartFfMpeg(state, playlistPath, cancellationTokenSource, workingDirectory).ConfigureAwait(false);
await WaitForMinimumDashSegmentCount(Path.Combine(workingDirectory, Path.GetFileName(playlistPath)), 1, cancellationTokenSource.Token).ConfigureAwait(false);
}
catch
{
state.Dispose();
throw;
}
}
}
}
}
finally
{
ApiEntryPoint.Instance.TranscodingStartLock.Release();
}
while (string.IsNullOrWhiteSpace(segmentPath))
{
segmentPath = FindSegment(playlistPath, representationId, segmentExtension, requestedIndex);
await Task.Delay(50, cancellationToken).ConfigureAwait(false);
}
Logger.Info("returning {0}", segmentPath);
return await GetSegmentResult(playlistPath, segmentPath, requestedIndex, segmentLength, job ?? ApiEntryPoint.Instance.GetTranscodingJob(playlistPath, TranscodingJobType), cancellationToken).ConfigureAwait(false);
}
private long GetPositionTicks(StreamState state, int requestedIndex)
{
if (requestedIndex <= 0)
{
return 0;
}
var startSeconds = requestedIndex * state.SegmentLength;
return TimeSpan.FromSeconds(startSeconds).Ticks;
}
protected Task WaitForMinimumDashSegmentCount(string playlist, int segmentCount, CancellationToken cancellationToken)
{
return WaitForSegment(playlist, "stream0-" + segmentCount.ToString("00000", CultureInfo.InvariantCulture) + ".m4s", cancellationToken);
}
private async Task<object> GetSegmentResult(string playlistPath,
string segmentPath,
int segmentIndex,
int segmentLength,
TranscodingJob transcodingJob,
CancellationToken cancellationToken)
{
// If all transcoding has completed, just return immediately
if (transcodingJob != null && transcodingJob.HasExited)
{
return GetSegmentResult(segmentPath, segmentIndex, segmentLength, transcodingJob);
}
// 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;
if (newLength == length)
{
eofCount++;
}
else
{
eofCount = 0;
}
length = newLength;
await Task.Delay(100, cancellationToken).ConfigureAwait(false);
}
return GetSegmentResult(segmentPath, segmentIndex, segmentLength, transcodingJob);
}
private object GetSegmentResult(string segmentPath, int index, int segmentLength, TranscodingJob transcodingJob)
{
var segmentEndingSeconds = (1 + index) * segmentLength;
var segmentEndingPositionTicks = TimeSpan.FromSeconds(segmentEndingSeconds).Ticks;
return ResultFactory.GetStaticFileResult(Request, new StaticFileResultOptions
{
Path = segmentPath,
FileShare = FileShare.ReadWrite,
OnComplete = () =>
{
if (transcodingJob != null)
{
transcodingJob.DownloadPositionTicks = Math.Max(transcodingJob.DownloadPositionTicks ?? segmentEndingPositionTicks, segmentEndingPositionTicks);
}
}
});
}
public int? GetCurrentTranscodingIndex(string playlist, string segmentExtension)
{
var job = ApiEntryPoint.Instance.GetTranscodingJob(playlist, TranscodingJobType);
if (job == null || job.HasExited)
{
return null;
}
var file = GetLastTranscodingFiles(playlist, segmentExtension, FileSystem, 1).FirstOrDefault();
if (file == null)
{
return null;
}
return GetIndex(file.FullName);
}
public int GetIndex(string segmentPath)
{
var indexString = Path.GetFileNameWithoutExtension(segmentPath).Split('-').LastOrDefault();
if (string.Equals(indexString, "init", StringComparison.OrdinalIgnoreCase))
{
return -1;
}
var startNumber = int.Parse(Path.GetFileNameWithoutExtension(Path.GetDirectoryName(segmentPath)), NumberStyles.Integer, UsCulture);
return startNumber + int.Parse(indexString, NumberStyles.Integer, UsCulture) - 1;
}
private void DeleteLastTranscodedFiles(string playlistPath, int retryCount)
{
if (retryCount >= 5)
{
return;
}
}
private static List<FileSystemMetadata> GetLastTranscodingFiles(string playlist, string segmentExtension, IFileSystem fileSystem, int count)
{
var folder = Path.GetDirectoryName(playlist);
try
{
return fileSystem.GetFiles(folder)
.Where(i => string.Equals(i.Extension, segmentExtension, StringComparison.OrdinalIgnoreCase))
.OrderByDescending(fileSystem.GetLastWriteTimeUtc)
.Take(count)
.ToList();
}
catch (DirectoryNotFoundException)
{
return new List<FileSystemMetadata>();
}
}
private string FindSegment(string playlist, string representationId, string segmentExtension, int requestedIndex)
{
var folder = Path.GetDirectoryName(playlist);
if (requestedIndex == -1)
{
var path = Path.Combine(folder, "0", "stream" + representationId + "-" + "init" + segmentExtension);
return FileSystem.FileExists(path) ? path : null;
}
try
{
foreach (var subfolder in FileSystem.GetDirectoryPaths(folder).ToList())
{
var subfolderName = Path.GetFileNameWithoutExtension(subfolder);
int startNumber;
if (int.TryParse(subfolderName, NumberStyles.Any, UsCulture, out startNumber))
{
var segmentIndex = requestedIndex - startNumber + 1;
var path = Path.Combine(folder, subfolderName, "stream" + representationId + "-" + segmentIndex.ToString("00000", CultureInfo.InvariantCulture) + segmentExtension);
if (FileSystem.FileExists(path))
{
return path;
}
}
}
}
catch (DirectoryNotFoundException)
{
}
return null;
}
protected override string GetAudioArguments(StreamState state)
{
var codec = GetAudioEncoder(state);
if (string.Equals(codec, "copy", StringComparison.OrdinalIgnoreCase))
{
return "-codec:a:0 copy";
}
var args = "-codec:a:0 " + codec;
var channels = state.OutputAudioChannels;
if (channels.HasValue)
{
args += " -ac " + channels.Value;
}
var bitrate = state.OutputAudioBitrate;
if (bitrate.HasValue)
{
args += " -ab " + bitrate.Value.ToString(UsCulture);
}
args += " " + GetAudioFilterParam(state, true);
return args;
}
protected override string GetVideoArguments(StreamState state)
{
var codec = GetVideoEncoder(state);
var args = "-codec:v:0 " + codec;
if (state.EnableMpegtsM2TsMode)
{
args += " -mpegts_m2ts_mode 1";
}
// 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;
}
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;
args += " " + GetVideoQualityParam(state, GetH264Encoder(state)) + keyFrameArg;
// Add resolution params, if specified
if (!hasGraphicalSubs)
{
args += GetOutputSizeParam(state, codec, false);
}
// This is for internal graphical subs
if (hasGraphicalSubs)
{
args += GetGraphicalSubtitleParam(state, codec);
}
return args;
}
protected override string GetCommandLineArguments(string outputPath, StreamState state, bool isEncoding)
{
// test url http://192.168.1.2:8096/videos/233e8905d559a8f230db9bffd2ac9d6d/master.mpd?mediasourceid=233e8905d559a8f230db9bffd2ac9d6d&videocodec=h264&audiocodec=aac&maxwidth=1280&videobitrate=500000&audiobitrate=128000&profile=baseline&level=3
// Good info on i-frames http://blog.streamroot.io/encode-multi-bitrate-videos-mpeg-dash-mse-based-media-players/
var threads = GetNumberOfThreads(state, false);
var inputModifier = GetInputModifier(state);
var initSegmentName = "stream$RepresentationID$-init.m4s";
var segmentName = "stream$RepresentationID$-$Number%05d$.m4s";
var args = string.Format("{0} {1} -map_metadata -1 -threads {2} {3} {4} -copyts {5} -f dash -init_seg_name \"{6}\" -media_seg_name \"{7}\" -use_template 0 -use_timeline 1 -min_seg_duration {8} -y \"{9}\"",
inputModifier,
GetInputArgument(state),
threads,
GetMapArgs(state),
GetVideoArguments(state),
GetAudioArguments(state),
initSegmentName,
segmentName,
(state.SegmentLength * 1000000).ToString(CultureInfo.InvariantCulture),
state.WaitForPath
).Trim();
return args;
}
protected override int GetStartNumber(StreamState state)
{
return GetStartNumber(state.VideoRequest);
}
private int GetStartNumber(VideoStreamRequest request)
{
var segmentId = "0";
var segmentRequest = request as GetDashSegment;
if (segmentRequest != null)
{
segmentId = segmentRequest.SegmentId;
}
if (string.Equals(segmentId, "init", StringComparison.OrdinalIgnoreCase))
{
return -1;
}
return int.Parse(segmentId, NumberStyles.Integer, UsCulture);
}
/// <summary>
/// Gets the segment file extension.
/// </summary>
/// <param name="state">The state.</param>
/// <returns>System.String.</returns>
protected override string GetSegmentFileExtension(StreamState state)
{
return ".m4s";
}
protected override TranscodingJobType TranscodingJobType
{
get
{
return TranscodingJobType.Dash;
}
}
private async Task WaitForSegment(string playlist, string segment, CancellationToken cancellationToken)
{
var segmentFilename = Path.GetFileName(segment);
Logger.Debug("Waiting for {0} in {1}", segmentFilename, playlist);
while (true)
{
// Need to use FileShare.ReadWrite because we're reading the file at the same time it's being written
using (var fileStream = GetPlaylistFileStream(playlist))
{
using (var reader = new StreamReader(fileStream))
{
while (!reader.EndOfStream)
{
var line = await reader.ReadLineAsync().ConfigureAwait(false);
if (line.IndexOf(segmentFilename, StringComparison.OrdinalIgnoreCase) != -1)
{
Logger.Debug("Finished waiting for {0} in {1}", segmentFilename, playlist);
return;
}
}
await Task.Delay(100, cancellationToken).ConfigureAwait(false);
}
}
}
}
}
}

View File

@ -83,11 +83,6 @@ namespace MediaBrowser.Api.Playback.Hls
var state = await GetState(request, cancellationTokenSource.Token).ConfigureAwait(false);
if (isLive)
{
state.Request.StartTimeTicks = null;
}
TranscodingJob job = null;
var playlist = state.OutputFilePath;
@ -137,13 +132,6 @@ namespace MediaBrowser.Api.Playback.Hls
var appendBaselineStream = false;
var baselineStreamBitrate = 64000;
var hlsVideoRequest = state.VideoRequest as GetHlsVideoStreamLegacy;
if (hlsVideoRequest != null)
{
appendBaselineStream = hlsVideoRequest.AppendBaselineStream;
baselineStreamBitrate = hlsVideoRequest.BaselineStreamAudioBitRate ?? baselineStreamBitrate;
}
var playlistText = GetMasterPlaylistFileText(playlist, videoBitrate + audioBitrate, appendBaselineStream, baselineStreamBitrate);
job = job ?? ApiEntryPoint.Instance.OnTranscodeBeginRequest(playlist, TranscodingJobType);
@ -248,11 +236,7 @@ namespace MediaBrowser.Api.Playback.Hls
protected override string GetCommandLineArguments(string outputPath, StreamState state, bool isEncoding)
{
var hlsVideoRequest = state.VideoRequest as GetHlsVideoStreamLegacy;
var itsOffsetMs = hlsVideoRequest == null
? 0
: hlsVideoRequest.TimeStampOffsetMs;
var itsOffsetMs = 0;
var itsOffset = itsOffsetMs == 0 ? string.Empty : string.Format("-itsoffset {0} ", TimeSpan.FromMilliseconds(itsOffsetMs).TotalSeconds.ToString(UsCulture));
@ -286,26 +270,6 @@ namespace MediaBrowser.Api.Playback.Hls
outputPath
).Trim();
if (hlsVideoRequest != null)
{
if (hlsVideoRequest.AppendBaselineStream)
{
var lowBitratePath = Path.Combine(Path.GetDirectoryName(outputPath), Path.GetFileNameWithoutExtension(outputPath) + "-low.m3u8");
var bitrate = hlsVideoRequest.BaselineStreamAudioBitRate ?? 64000;
var lowBitrateParams = string.Format(" -threads {0} -vn -codec:a:0 libmp3lame -ac 2 -ab {1} -hls_time {2} -start_number {3} -hls_list_size {4} -y \"{5}\"",
threads,
bitrate / 2,
state.SegmentLength.ToString(UsCulture),
startNumberParam,
state.HlsListSize.ToString(UsCulture),
lowBitratePath);
args += " " + lowBitrateParams;
}
}
return args;
}
@ -314,9 +278,28 @@ namespace MediaBrowser.Api.Playback.Hls
return 0;
}
protected override bool CanStreamCopyAudio(VideoStreamRequest request, MediaStream audioStream, List<string> supportedAudioCodecs)
protected bool IsLiveStream(StreamState state)
{
return false;
var isLiveStream = (state.RunTimeTicks ?? 0) == 0;
if (state.VideoRequest.ForceLiveStream)
{
return true;
}
return isLiveStream;
}
protected override bool CanStreamCopyAudio(StreamState state, List<string> supportedAudioCodecs)
{
var isLiveStream = IsLiveStream(state);
if (!isLiveStream)
{
return false;
}
return base.CanStreamCopyAudio(state, supportedAudioCodecs);
}
}
}

View File

@ -504,6 +504,11 @@ namespace MediaBrowser.Api.Playback.Hls
{
var isLiveStream = (state.RunTimeTicks ?? 0) == 0;
if (state.VideoRequest.ForceLiveStream)
{
return true;
}
return isLiveStream;
}

View File

@ -31,25 +31,6 @@ namespace MediaBrowser.Api.Playback.Hls
public string SegmentId { get; set; }
}
/// <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>

View File

@ -27,16 +27,6 @@ namespace MediaBrowser.Api.Playback.Hls
{
}
/// <summary>
/// Gets the specified request.
/// </summary>
/// <param name="request">The request.</param>
/// <returns>System.Object.</returns>
public object Get(GetHlsVideoStreamLegacy request)
{
return ProcessRequest(request, false);
}
public object Get(GetLiveHlsStream request)
{
return ProcessRequest(request, true);

View File

@ -189,7 +189,9 @@ namespace MediaBrowser.Api.Playback
[ApiMember(Name = "CopyTimestamps", Description = "Whether or not to copy timestamps when transcoding with an offset. Defaults to false.", IsRequired = false, DataType = "bool", ParameterType = "query", Verb = "GET")]
public bool CopyTimestamps { get; set; }
public bool ForceLiveStream { get; set; }
public VideoStreamRequest()
{
EnableAutoStreamCopy = true;

View File

@ -513,7 +513,14 @@ namespace MediaBrowser.Model.Dlna
{
if (targetAudioChannels.Value >= 5 && (maxTotalBitrate ?? 0) >= 2000000)
{
defaultBitrate = 320000;
if (StringHelper.EqualsIgnoreCase(targetAudioCodec, "ac3"))
{
defaultBitrate = 384000;
}
else
{
defaultBitrate = 320000;
}
}
}