2018-12-14 09:40:55 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
2019-02-02 22:55:47 +00:00
|
|
|
using System.Diagnostics;
|
2018-12-14 09:40:55 +00:00
|
|
|
using System.Globalization;
|
|
|
|
using System.IO;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Text;
|
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
2019-11-08 11:49:00 +00:00
|
|
|
using MediaBrowser.Common.Configuration;
|
2019-01-13 19:24:58 +00:00
|
|
|
using MediaBrowser.Common.Extensions;
|
|
|
|
using MediaBrowser.Controller.Configuration;
|
|
|
|
using MediaBrowser.Controller.Devices;
|
|
|
|
using MediaBrowser.Controller.Dlna;
|
|
|
|
using MediaBrowser.Controller.Library;
|
|
|
|
using MediaBrowser.Controller.MediaEncoding;
|
2018-12-14 09:40:55 +00:00
|
|
|
using MediaBrowser.Controller.Net;
|
|
|
|
using MediaBrowser.Model.Configuration;
|
2019-01-13 19:24:58 +00:00
|
|
|
using MediaBrowser.Model.Dlna;
|
|
|
|
using MediaBrowser.Model.Dto;
|
|
|
|
using MediaBrowser.Model.Entities;
|
|
|
|
using MediaBrowser.Model.IO;
|
|
|
|
using MediaBrowser.Model.MediaInfo;
|
|
|
|
using MediaBrowser.Model.Serialization;
|
2018-12-14 23:48:06 +00:00
|
|
|
using Microsoft.Extensions.Logging;
|
2018-12-14 09:40:55 +00:00
|
|
|
|
|
|
|
namespace MediaBrowser.Api.Playback
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// Class BaseStreamingService
|
|
|
|
/// </summary>
|
|
|
|
public abstract class BaseStreamingService : BaseApiService
|
|
|
|
{
|
2019-03-27 15:26:33 +00:00
|
|
|
protected virtual bool EnableOutputInSubFolder => false;
|
|
|
|
|
2018-12-14 09:40:55 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Gets or sets the user manager.
|
|
|
|
/// </summary>
|
|
|
|
/// <value>The user manager.</value>
|
|
|
|
protected IUserManager UserManager { get; private set; }
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets or sets the library manager.
|
|
|
|
/// </summary>
|
|
|
|
/// <value>The library manager.</value>
|
|
|
|
protected ILibraryManager LibraryManager { get; private set; }
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets or sets the iso manager.
|
|
|
|
/// </summary>
|
|
|
|
/// <value>The iso manager.</value>
|
|
|
|
protected IIsoManager IsoManager { get; private set; }
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets or sets the media encoder.
|
|
|
|
/// </summary>
|
|
|
|
/// <value>The media encoder.</value>
|
|
|
|
protected IMediaEncoder MediaEncoder { get; private set; }
|
|
|
|
|
|
|
|
protected IFileSystem FileSystem { get; private set; }
|
|
|
|
|
|
|
|
protected IDlnaManager DlnaManager { get; private set; }
|
2019-03-26 21:56:05 +00:00
|
|
|
|
2018-12-14 09:40:55 +00:00
|
|
|
protected IDeviceManager DeviceManager { get; private set; }
|
2019-03-26 21:56:05 +00:00
|
|
|
|
2018-12-14 09:40:55 +00:00
|
|
|
protected IMediaSourceManager MediaSourceManager { get; private set; }
|
2019-03-26 21:56:05 +00:00
|
|
|
|
2018-12-14 09:40:55 +00:00
|
|
|
protected IJsonSerializer JsonSerializer { get; private set; }
|
|
|
|
|
|
|
|
protected IAuthorizationContext AuthorizationContext { get; private set; }
|
|
|
|
|
|
|
|
protected EncodingHelper EncodingHelper { get; set; }
|
|
|
|
|
2019-03-26 21:56:05 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Gets the type of the transcoding job.
|
|
|
|
/// </summary>
|
|
|
|
/// <value>The type of the transcoding job.</value>
|
|
|
|
protected abstract TranscodingJobType TranscodingJobType { get; }
|
|
|
|
|
2018-12-14 09:40:55 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Initializes a new instance of the <see cref="BaseStreamingService" /> class.
|
|
|
|
/// </summary>
|
2019-02-02 22:55:47 +00:00
|
|
|
protected BaseStreamingService(
|
2019-11-17 22:05:39 +00:00
|
|
|
ILogger logger,
|
|
|
|
IServerConfigurationManager serverConfigurationManager,
|
|
|
|
IHttpResultFactory httpResultFactory,
|
2019-02-02 22:55:47 +00:00
|
|
|
IUserManager userManager,
|
|
|
|
ILibraryManager libraryManager,
|
|
|
|
IIsoManager isoManager,
|
|
|
|
IMediaEncoder mediaEncoder,
|
|
|
|
IFileSystem fileSystem,
|
|
|
|
IDlnaManager dlnaManager,
|
|
|
|
IDeviceManager deviceManager,
|
|
|
|
IMediaSourceManager mediaSourceManager,
|
|
|
|
IJsonSerializer jsonSerializer,
|
2019-10-26 20:53:53 +00:00
|
|
|
IAuthorizationContext authorizationContext,
|
|
|
|
EncodingHelper encodingHelper)
|
2019-11-17 22:05:39 +00:00
|
|
|
: base(logger, serverConfigurationManager, httpResultFactory)
|
2018-12-14 09:40:55 +00:00
|
|
|
{
|
|
|
|
UserManager = userManager;
|
|
|
|
LibraryManager = libraryManager;
|
|
|
|
IsoManager = isoManager;
|
|
|
|
MediaEncoder = mediaEncoder;
|
2019-02-02 22:55:47 +00:00
|
|
|
FileSystem = fileSystem;
|
|
|
|
DlnaManager = dlnaManager;
|
|
|
|
DeviceManager = deviceManager;
|
|
|
|
MediaSourceManager = mediaSourceManager;
|
|
|
|
JsonSerializer = jsonSerializer;
|
|
|
|
AuthorizationContext = authorizationContext;
|
|
|
|
|
2019-10-26 20:53:53 +00:00
|
|
|
EncodingHelper = encodingHelper;
|
2018-12-14 09:40:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets the command line arguments.
|
|
|
|
/// </summary>
|
|
|
|
protected abstract string GetCommandLineArguments(string outputPath, EncodingOptions encodingOptions, StreamState state, bool isEncoding);
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets the output file extension.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="state">The state.</param>
|
|
|
|
/// <returns>System.String.</returns>
|
|
|
|
protected virtual string GetOutputFileExtension(StreamState state)
|
|
|
|
{
|
|
|
|
return Path.GetExtension(state.RequestedUrl);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets the output file path.
|
|
|
|
/// </summary>
|
|
|
|
private string GetOutputFilePath(StreamState state, EncodingOptions encodingOptions, string outputFileExtension)
|
|
|
|
{
|
2019-08-24 09:17:17 +00:00
|
|
|
var data = $"{state.MediaPath}-{state.UserAgent}-{state.Request.DeviceId}-{state.Request.PlaySessionId}";
|
2018-12-14 09:40:55 +00:00
|
|
|
|
2019-02-28 22:22:57 +00:00
|
|
|
var filename = data.GetMD5().ToString("N", CultureInfo.InvariantCulture);
|
2019-03-27 15:26:33 +00:00
|
|
|
var ext = outputFileExtension.ToLowerInvariant();
|
2019-11-08 11:49:00 +00:00
|
|
|
var folder = ServerConfigurationManager.GetTranscodePath();
|
2018-12-14 09:40:55 +00:00
|
|
|
|
|
|
|
if (EnableOutputInSubFolder)
|
|
|
|
{
|
2019-03-27 15:26:33 +00:00
|
|
|
return Path.Combine(folder, filename, filename + ext);
|
2018-12-14 09:40:55 +00:00
|
|
|
}
|
|
|
|
|
2019-03-27 15:26:33 +00:00
|
|
|
return Path.Combine(folder, filename + ext);
|
2018-12-14 09:40:55 +00:00
|
|
|
}
|
|
|
|
|
2019-06-01 22:03:48 +00:00
|
|
|
protected virtual string GetDefaultEncoderPreset()
|
2018-12-14 09:40:55 +00:00
|
|
|
{
|
|
|
|
return "superfast";
|
|
|
|
}
|
|
|
|
|
|
|
|
private async Task AcquireResources(StreamState state, CancellationTokenSource cancellationTokenSource)
|
|
|
|
{
|
|
|
|
if (state.VideoType == VideoType.Iso && state.IsoType.HasValue && IsoManager.CanMount(state.MediaPath))
|
|
|
|
{
|
|
|
|
state.IsoMount = await IsoManager.Mount(state.MediaPath, cancellationTokenSource.Token).ConfigureAwait(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (state.MediaSource.RequiresOpening && string.IsNullOrWhiteSpace(state.Request.LiveStreamId))
|
|
|
|
{
|
|
|
|
var liveStreamResponse = await MediaSourceManager.OpenLiveStream(new LiveStreamRequest
|
|
|
|
{
|
|
|
|
OpenToken = state.MediaSource.OpenToken
|
|
|
|
}, cancellationTokenSource.Token).ConfigureAwait(false);
|
|
|
|
|
|
|
|
EncodingHelper.AttachMediaSourceInfo(state, liveStreamResponse.MediaSource, state.RequestedUrl);
|
|
|
|
|
|
|
|
if (state.VideoRequest != null)
|
|
|
|
{
|
|
|
|
EncodingHelper.TryStreamCopy(state);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (state.MediaSource.BufferMs.HasValue)
|
|
|
|
{
|
|
|
|
await Task.Delay(state.MediaSource.BufferMs.Value, cancellationTokenSource.Token).ConfigureAwait(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Starts the FFMPEG.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="state">The state.</param>
|
|
|
|
/// <param name="outputPath">The output path.</param>
|
|
|
|
/// <param name="cancellationTokenSource">The cancellation token source.</param>
|
|
|
|
/// <param name="workingDirectory">The working directory.</param>
|
|
|
|
/// <returns>Task.</returns>
|
2019-02-02 22:55:47 +00:00
|
|
|
protected async Task<TranscodingJob> StartFfMpeg(
|
|
|
|
StreamState state,
|
2018-12-14 09:40:55 +00:00
|
|
|
string outputPath,
|
|
|
|
CancellationTokenSource cancellationTokenSource,
|
|
|
|
string workingDirectory = null)
|
|
|
|
{
|
2019-01-26 21:08:04 +00:00
|
|
|
Directory.CreateDirectory(Path.GetDirectoryName(outputPath));
|
2018-12-14 09:40:55 +00:00
|
|
|
|
|
|
|
await AcquireResources(state, cancellationTokenSource).ConfigureAwait(false);
|
|
|
|
|
|
|
|
if (state.VideoRequest != null && !string.Equals(state.OutputVideoCodec, "copy", StringComparison.OrdinalIgnoreCase))
|
|
|
|
{
|
|
|
|
var auth = AuthorizationContext.GetAuthorizationInfo(Request);
|
2019-03-26 21:56:05 +00:00
|
|
|
if (auth.User != null && !auth.User.Policy.EnableVideoPlaybackTranscoding)
|
2018-12-14 09:40:55 +00:00
|
|
|
{
|
2019-03-26 21:56:05 +00:00
|
|
|
ApiEntryPoint.Instance.OnTranscodeFailedToStart(outputPath, TranscodingJobType, state);
|
2018-12-14 09:40:55 +00:00
|
|
|
|
2019-03-26 21:56:05 +00:00
|
|
|
throw new ArgumentException("User does not have access to video transcoding");
|
2018-12-14 09:40:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-08 11:49:00 +00:00
|
|
|
var encodingOptions = ServerConfigurationManager.GetEncodingOptions();
|
2018-12-14 09:40:55 +00:00
|
|
|
|
2019-02-02 22:55:47 +00:00
|
|
|
var process = new Process()
|
2018-12-14 09:40:55 +00:00
|
|
|
{
|
2019-02-02 22:55:47 +00:00
|
|
|
StartInfo = new ProcessStartInfo()
|
|
|
|
{
|
|
|
|
WindowStyle = ProcessWindowStyle.Hidden,
|
|
|
|
CreateNoWindow = true,
|
|
|
|
UseShellExecute = false,
|
2018-12-14 09:40:55 +00:00
|
|
|
|
2019-02-02 22:55:47 +00:00
|
|
|
// Must consume both stdout and stderr or deadlocks may occur
|
|
|
|
//RedirectStandardOutput = true,
|
|
|
|
RedirectStandardError = true,
|
|
|
|
RedirectStandardInput = true,
|
2018-12-14 09:40:55 +00:00
|
|
|
|
2019-02-02 22:55:47 +00:00
|
|
|
FileName = MediaEncoder.EncoderPath,
|
2019-03-26 21:56:05 +00:00
|
|
|
Arguments = GetCommandLineArguments(outputPath, encodingOptions, state, true),
|
2019-02-02 22:55:47 +00:00
|
|
|
WorkingDirectory = string.IsNullOrWhiteSpace(workingDirectory) ? null : workingDirectory,
|
2018-12-14 09:40:55 +00:00
|
|
|
|
2019-02-02 22:55:47 +00:00
|
|
|
ErrorDialog = false
|
|
|
|
},
|
|
|
|
EnableRaisingEvents = true
|
|
|
|
};
|
2018-12-14 09:40:55 +00:00
|
|
|
|
|
|
|
var transcodingJob = ApiEntryPoint.Instance.OnTranscodeBeginning(outputPath,
|
|
|
|
state.Request.PlaySessionId,
|
|
|
|
state.MediaSource.LiveStreamId,
|
2019-07-02 19:47:36 +00:00
|
|
|
Guid.NewGuid().ToString("N", CultureInfo.InvariantCulture),
|
2018-12-14 09:40:55 +00:00
|
|
|
TranscodingJobType,
|
|
|
|
process,
|
|
|
|
state.Request.DeviceId,
|
|
|
|
state,
|
|
|
|
cancellationTokenSource);
|
|
|
|
|
|
|
|
var commandLineLogMessage = process.StartInfo.FileName + " " + process.StartInfo.Arguments;
|
2018-12-14 23:48:06 +00:00
|
|
|
Logger.LogInformation(commandLineLogMessage);
|
2018-12-14 09:40:55 +00:00
|
|
|
|
|
|
|
var logFilePrefix = "ffmpeg-transcode";
|
2019-03-26 21:56:05 +00:00
|
|
|
if (state.VideoRequest != null
|
|
|
|
&& string.Equals(state.OutputVideoCodec, "copy", StringComparison.OrdinalIgnoreCase))
|
2018-12-14 09:40:55 +00:00
|
|
|
{
|
2019-03-26 21:56:05 +00:00
|
|
|
if (string.Equals(state.OutputAudioCodec, "copy", StringComparison.OrdinalIgnoreCase))
|
2019-02-02 22:55:47 +00:00
|
|
|
{
|
|
|
|
logFilePrefix = "ffmpeg-directstream";
|
|
|
|
}
|
2019-03-26 21:56:05 +00:00
|
|
|
else
|
2019-02-02 22:55:47 +00:00
|
|
|
{
|
|
|
|
logFilePrefix = "ffmpeg-remux";
|
|
|
|
}
|
2018-12-14 09:40:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var logFilePath = Path.Combine(ServerConfigurationManager.ApplicationPaths.LogDirectoryPath, logFilePrefix + "-" + Guid.NewGuid() + ".txt");
|
|
|
|
|
|
|
|
// FFMpeg writes debug/error info to stderr. This is useful when debugging so let's put it in the log directory.
|
2019-03-26 21:56:05 +00:00
|
|
|
Stream logStream = FileSystem.GetFileStream(logFilePath, FileOpenMode.Create, FileAccessMode.Write, FileShareMode.Read, true);
|
2018-12-14 09:40:55 +00:00
|
|
|
|
|
|
|
var commandLineLogMessageBytes = Encoding.UTF8.GetBytes(Request.AbsoluteUri + Environment.NewLine + Environment.NewLine + JsonSerializer.SerializeToString(state.MediaSource) + Environment.NewLine + Environment.NewLine + commandLineLogMessage + Environment.NewLine + Environment.NewLine);
|
2019-03-26 21:56:05 +00:00
|
|
|
await logStream.WriteAsync(commandLineLogMessageBytes, 0, commandLineLogMessageBytes.Length, cancellationTokenSource.Token).ConfigureAwait(false);
|
2018-12-14 09:40:55 +00:00
|
|
|
|
|
|
|
process.Exited += (sender, args) => OnFfMpegProcessExited(process, transcodingJob, state);
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
process.Start();
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
2018-12-20 12:11:26 +00:00
|
|
|
Logger.LogError(ex, "Error starting ffmpeg");
|
2018-12-14 09:40:55 +00:00
|
|
|
|
|
|
|
ApiEntryPoint.Instance.OnTranscodeFailedToStart(outputPath, TranscodingJobType, state);
|
|
|
|
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
|
2019-10-03 15:50:39 +00:00
|
|
|
Logger.LogDebug("Launched ffmpeg process");
|
2018-12-14 09:40:55 +00:00
|
|
|
state.TranscodingJob = transcodingJob;
|
|
|
|
|
|
|
|
// Important - don't await the log task or we won't be able to kill ffmpeg when the user stops playback
|
2019-03-26 21:56:05 +00:00
|
|
|
_ = new JobLogger(Logger).StartStreamingLog(state, process.StandardError.BaseStream, logStream);
|
2018-12-14 09:40:55 +00:00
|
|
|
|
|
|
|
// Wait for the file to exist before proceeeding
|
2019-10-16 13:13:59 +00:00
|
|
|
var ffmpegTargetFile = state.WaitForPath ?? outputPath;
|
|
|
|
Logger.LogDebug("Waiting for the creation of {0}", ffmpegTargetFile);
|
|
|
|
while (!File.Exists(ffmpegTargetFile) && !transcodingJob.HasExited)
|
2018-12-14 09:40:55 +00:00
|
|
|
{
|
|
|
|
await Task.Delay(100, cancellationTokenSource.Token).ConfigureAwait(false);
|
|
|
|
}
|
2019-10-21 08:58:04 +00:00
|
|
|
|
2019-10-16 13:13:59 +00:00
|
|
|
Logger.LogDebug("File {0} created or transcoding has finished", ffmpegTargetFile);
|
2018-12-14 09:40:55 +00:00
|
|
|
|
|
|
|
if (state.IsInputVideo && transcodingJob.Type == TranscodingJobType.Progressive && !transcodingJob.HasExited)
|
|
|
|
{
|
|
|
|
await Task.Delay(1000, cancellationTokenSource.Token).ConfigureAwait(false);
|
|
|
|
|
|
|
|
if (state.ReadInputAtNativeFramerate && !transcodingJob.HasExited)
|
|
|
|
{
|
|
|
|
await Task.Delay(1500, cancellationTokenSource.Token).ConfigureAwait(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!transcodingJob.HasExited)
|
|
|
|
{
|
|
|
|
StartThrottler(state, transcodingJob);
|
|
|
|
}
|
2019-10-03 15:50:39 +00:00
|
|
|
Logger.LogDebug("StartFfMpeg() finished successfully");
|
2018-12-14 09:40:55 +00:00
|
|
|
|
|
|
|
return transcodingJob;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void StartThrottler(StreamState state, TranscodingJob transcodingJob)
|
|
|
|
{
|
|
|
|
if (EnableThrottling(state))
|
|
|
|
{
|
2019-02-05 08:49:46 +00:00
|
|
|
transcodingJob.TranscodingThrottler = state.TranscodingThrottler = new TranscodingThrottler(transcodingJob, Logger, ServerConfigurationManager, FileSystem);
|
2018-12-14 09:40:55 +00:00
|
|
|
state.TranscodingThrottler.Start();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private bool EnableThrottling(StreamState state)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
//// do not use throttling with hardware encoders
|
|
|
|
//return state.InputProtocol == MediaProtocol.File &&
|
|
|
|
// state.RunTimeTicks.HasValue &&
|
|
|
|
// state.RunTimeTicks.Value >= TimeSpan.FromMinutes(5).Ticks &&
|
|
|
|
// state.IsInputVideo &&
|
|
|
|
// state.VideoType == VideoType.VideoFile &&
|
|
|
|
// !string.Equals(state.OutputVideoCodec, "copy", StringComparison.OrdinalIgnoreCase) &&
|
|
|
|
// string.Equals(GetVideoEncoder(state), "libx264", StringComparison.OrdinalIgnoreCase);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Processes the exited.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="process">The process.</param>
|
|
|
|
/// <param name="job">The job.</param>
|
|
|
|
/// <param name="state">The state.</param>
|
2019-02-02 22:55:47 +00:00
|
|
|
private void OnFfMpegProcessExited(Process process, TranscodingJob job, StreamState state)
|
2018-12-14 09:40:55 +00:00
|
|
|
{
|
|
|
|
if (job != null)
|
|
|
|
{
|
|
|
|
job.HasExited = true;
|
|
|
|
}
|
|
|
|
|
2018-12-14 23:48:06 +00:00
|
|
|
Logger.LogDebug("Disposing stream resources");
|
2018-12-14 09:40:55 +00:00
|
|
|
state.Dispose();
|
|
|
|
|
2019-03-26 21:56:05 +00:00
|
|
|
if (process.ExitCode == 0)
|
2018-12-14 09:40:55 +00:00
|
|
|
{
|
2019-03-26 21:56:05 +00:00
|
|
|
Logger.LogInformation("FFMpeg exited with code 0");
|
2018-12-14 09:40:55 +00:00
|
|
|
}
|
2019-03-26 21:56:05 +00:00
|
|
|
else
|
2018-12-14 09:40:55 +00:00
|
|
|
{
|
2019-03-26 21:56:05 +00:00
|
|
|
Logger.LogError("FFMpeg exited with code {0}", process.ExitCode);
|
2018-12-14 09:40:55 +00:00
|
|
|
}
|
|
|
|
|
2019-03-26 21:56:05 +00:00
|
|
|
process.Dispose();
|
2018-12-14 09:40:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Parses the parameters.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="request">The request.</param>
|
|
|
|
private void ParseParams(StreamRequest request)
|
|
|
|
{
|
|
|
|
var vals = request.Params.Split(';');
|
|
|
|
|
|
|
|
var videoRequest = request as VideoStreamRequest;
|
|
|
|
|
|
|
|
for (var i = 0; i < vals.Length; i++)
|
|
|
|
{
|
|
|
|
var val = vals[i];
|
|
|
|
|
|
|
|
if (string.IsNullOrWhiteSpace(val))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i == 0)
|
|
|
|
{
|
|
|
|
request.DeviceProfileId = val;
|
|
|
|
}
|
|
|
|
else if (i == 1)
|
|
|
|
{
|
|
|
|
request.DeviceId = val;
|
|
|
|
}
|
|
|
|
else if (i == 2)
|
|
|
|
{
|
|
|
|
request.MediaSourceId = val;
|
|
|
|
}
|
|
|
|
else if (i == 3)
|
|
|
|
{
|
|
|
|
request.Static = string.Equals("true", val, StringComparison.OrdinalIgnoreCase);
|
|
|
|
}
|
|
|
|
else if (i == 4)
|
|
|
|
{
|
|
|
|
if (videoRequest != null)
|
|
|
|
{
|
|
|
|
videoRequest.VideoCodec = val;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (i == 5)
|
|
|
|
{
|
|
|
|
request.AudioCodec = val;
|
|
|
|
}
|
|
|
|
else if (i == 6)
|
|
|
|
{
|
|
|
|
if (videoRequest != null)
|
|
|
|
{
|
2019-04-17 13:23:03 +00:00
|
|
|
videoRequest.AudioStreamIndex = int.Parse(val, CultureInfo.InvariantCulture);
|
2018-12-14 09:40:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (i == 7)
|
|
|
|
{
|
|
|
|
if (videoRequest != null)
|
|
|
|
{
|
2019-04-17 13:23:03 +00:00
|
|
|
videoRequest.SubtitleStreamIndex = int.Parse(val, CultureInfo.InvariantCulture);
|
2018-12-14 09:40:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (i == 8)
|
|
|
|
{
|
|
|
|
if (videoRequest != null)
|
|
|
|
{
|
2019-04-17 13:23:03 +00:00
|
|
|
videoRequest.VideoBitRate = int.Parse(val, CultureInfo.InvariantCulture);
|
2018-12-14 09:40:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (i == 9)
|
|
|
|
{
|
2019-04-17 13:23:03 +00:00
|
|
|
request.AudioBitRate = int.Parse(val, CultureInfo.InvariantCulture);
|
2018-12-14 09:40:55 +00:00
|
|
|
}
|
|
|
|
else if (i == 10)
|
|
|
|
{
|
2019-04-17 13:23:03 +00:00
|
|
|
request.MaxAudioChannels = int.Parse(val, CultureInfo.InvariantCulture);
|
2018-12-14 09:40:55 +00:00
|
|
|
}
|
|
|
|
else if (i == 11)
|
|
|
|
{
|
|
|
|
if (videoRequest != null)
|
|
|
|
{
|
2019-04-17 13:23:03 +00:00
|
|
|
videoRequest.MaxFramerate = float.Parse(val, CultureInfo.InvariantCulture);
|
2018-12-14 09:40:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (i == 12)
|
|
|
|
{
|
|
|
|
if (videoRequest != null)
|
|
|
|
{
|
2019-04-17 13:23:03 +00:00
|
|
|
videoRequest.MaxWidth = int.Parse(val, CultureInfo.InvariantCulture);
|
2018-12-14 09:40:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (i == 13)
|
|
|
|
{
|
|
|
|
if (videoRequest != null)
|
|
|
|
{
|
2019-04-17 13:23:03 +00:00
|
|
|
videoRequest.MaxHeight = int.Parse(val, CultureInfo.InvariantCulture);
|
2018-12-14 09:40:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (i == 14)
|
|
|
|
{
|
2019-04-17 13:23:03 +00:00
|
|
|
request.StartTimeTicks = long.Parse(val, CultureInfo.InvariantCulture);
|
2018-12-14 09:40:55 +00:00
|
|
|
}
|
|
|
|
else if (i == 15)
|
|
|
|
{
|
|
|
|
if (videoRequest != null)
|
|
|
|
{
|
|
|
|
videoRequest.Level = val;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (i == 16)
|
|
|
|
{
|
|
|
|
if (videoRequest != null)
|
|
|
|
{
|
2019-04-17 13:23:03 +00:00
|
|
|
videoRequest.MaxRefFrames = int.Parse(val, CultureInfo.InvariantCulture);
|
2018-12-14 09:40:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (i == 17)
|
|
|
|
{
|
|
|
|
if (videoRequest != null)
|
|
|
|
{
|
2019-04-17 13:23:03 +00:00
|
|
|
videoRequest.MaxVideoBitDepth = int.Parse(val, CultureInfo.InvariantCulture);
|
2018-12-14 09:40:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (i == 18)
|
|
|
|
{
|
|
|
|
if (videoRequest != null)
|
|
|
|
{
|
|
|
|
videoRequest.Profile = val;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (i == 19)
|
|
|
|
{
|
|
|
|
// cabac no longer used
|
|
|
|
}
|
|
|
|
else if (i == 20)
|
|
|
|
{
|
|
|
|
request.PlaySessionId = val;
|
|
|
|
}
|
|
|
|
else if (i == 21)
|
|
|
|
{
|
|
|
|
// api_key
|
|
|
|
}
|
|
|
|
else if (i == 22)
|
|
|
|
{
|
|
|
|
request.LiveStreamId = val;
|
|
|
|
}
|
|
|
|
else if (i == 23)
|
|
|
|
{
|
|
|
|
// Duplicating ItemId because of MediaMonkey
|
|
|
|
}
|
|
|
|
else if (i == 24)
|
|
|
|
{
|
|
|
|
if (videoRequest != null)
|
|
|
|
{
|
|
|
|
videoRequest.CopyTimestamps = string.Equals("true", val, StringComparison.OrdinalIgnoreCase);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (i == 25)
|
|
|
|
{
|
|
|
|
if (!string.IsNullOrWhiteSpace(val) && videoRequest != null)
|
|
|
|
{
|
2019-01-13 20:46:33 +00:00
|
|
|
if (Enum.TryParse(val, out SubtitleDeliveryMethod method))
|
2018-12-14 09:40:55 +00:00
|
|
|
{
|
|
|
|
videoRequest.SubtitleMethod = method;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (i == 26)
|
|
|
|
{
|
2019-04-17 13:23:03 +00:00
|
|
|
request.TranscodingMaxAudioChannels = int.Parse(val, CultureInfo.InvariantCulture);
|
2018-12-14 09:40:55 +00:00
|
|
|
}
|
|
|
|
else if (i == 27)
|
|
|
|
{
|
|
|
|
if (videoRequest != null)
|
|
|
|
{
|
|
|
|
videoRequest.EnableSubtitlesInManifest = string.Equals("true", val, StringComparison.OrdinalIgnoreCase);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (i == 28)
|
|
|
|
{
|
|
|
|
request.Tag = val;
|
|
|
|
}
|
|
|
|
else if (i == 29)
|
|
|
|
{
|
|
|
|
if (videoRequest != null)
|
|
|
|
{
|
|
|
|
videoRequest.RequireAvc = string.Equals("true", val, StringComparison.OrdinalIgnoreCase);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (i == 30)
|
|
|
|
{
|
|
|
|
request.SubtitleCodec = val;
|
|
|
|
}
|
|
|
|
else if (i == 31)
|
|
|
|
{
|
|
|
|
if (videoRequest != null)
|
|
|
|
{
|
|
|
|
videoRequest.RequireNonAnamorphic = string.Equals("true", val, StringComparison.OrdinalIgnoreCase);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (i == 32)
|
|
|
|
{
|
|
|
|
if (videoRequest != null)
|
|
|
|
{
|
|
|
|
videoRequest.DeInterlace = string.Equals("true", val, StringComparison.OrdinalIgnoreCase);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (i == 33)
|
|
|
|
{
|
|
|
|
request.TranscodeReasons = val;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-29 07:18:06 +00:00
|
|
|
/// <summary>
|
2019-11-17 22:05:39 +00:00
|
|
|
/// Parses query parameters as StreamOptions.
|
2019-10-25 10:47:20 +00:00
|
|
|
/// </summary>
|
2018-12-29 07:18:06 +00:00
|
|
|
/// <param name="request">The stream request.</param>
|
|
|
|
private void ParseStreamOptions(StreamRequest request)
|
|
|
|
{
|
2019-01-12 20:41:08 +00:00
|
|
|
foreach (var param in Request.QueryString)
|
|
|
|
{
|
2019-02-27 13:23:39 +00:00
|
|
|
if (char.IsLower(param.Key[0]))
|
2019-01-12 20:41:08 +00:00
|
|
|
{
|
2018-12-29 07:18:06 +00:00
|
|
|
// This was probably not parsed initially and should be a StreamOptions
|
|
|
|
// TODO: This should be incorporated either in the lower framework for parsing requests
|
|
|
|
// or the generated URL should correctly serialize it
|
2019-02-27 13:23:39 +00:00
|
|
|
request.StreamOptions[param.Key] = param.Value;
|
2018-12-29 07:18:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-14 09:40:55 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Parses the dlna headers.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="request">The request.</param>
|
|
|
|
private void ParseDlnaHeaders(StreamRequest request)
|
|
|
|
{
|
|
|
|
if (!request.StartTimeTicks.HasValue)
|
|
|
|
{
|
|
|
|
var timeSeek = GetHeader("TimeSeekRange.dlna.org");
|
|
|
|
|
|
|
|
request.StartTimeTicks = ParseTimeSeekHeader(timeSeek);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Parses the time seek header.
|
|
|
|
/// </summary>
|
|
|
|
private long? ParseTimeSeekHeader(string value)
|
|
|
|
{
|
|
|
|
if (string.IsNullOrWhiteSpace(value))
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2019-03-27 15:31:26 +00:00
|
|
|
const string Npt = "npt=";
|
|
|
|
if (!value.StartsWith(Npt, StringComparison.OrdinalIgnoreCase))
|
2018-12-14 09:40:55 +00:00
|
|
|
{
|
|
|
|
throw new ArgumentException("Invalid timeseek header");
|
|
|
|
}
|
2019-03-26 21:56:05 +00:00
|
|
|
int index = value.IndexOf('-');
|
|
|
|
if (index == -1)
|
|
|
|
{
|
2019-03-27 15:31:26 +00:00
|
|
|
value = value.Substring(Npt.Length);
|
2019-03-26 21:56:05 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-08-28 18:05:17 +00:00
|
|
|
value = value.Substring(Npt.Length, index - Npt.Length);
|
2019-03-26 21:56:05 +00:00
|
|
|
}
|
2018-12-14 09:40:55 +00:00
|
|
|
|
|
|
|
if (value.IndexOf(':') == -1)
|
|
|
|
{
|
|
|
|
// Parses npt times in the format of '417.33'
|
2019-04-17 13:23:03 +00:00
|
|
|
if (double.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out var seconds))
|
2018-12-14 09:40:55 +00:00
|
|
|
{
|
|
|
|
return TimeSpan.FromSeconds(seconds).Ticks;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new ArgumentException("Invalid timeseek header");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parses npt times in the format of '10:19:25.7'
|
|
|
|
var tokens = value.Split(new[] { ':' }, 3);
|
|
|
|
double secondsSum = 0;
|
|
|
|
var timeFactor = 3600;
|
|
|
|
|
|
|
|
foreach (var time in tokens)
|
|
|
|
{
|
2019-04-17 13:23:03 +00:00
|
|
|
if (double.TryParse(time, NumberStyles.Any, CultureInfo.InvariantCulture, out var digit))
|
2018-12-14 09:40:55 +00:00
|
|
|
{
|
|
|
|
secondsSum += digit * timeFactor;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw new ArgumentException("Invalid timeseek header");
|
|
|
|
}
|
|
|
|
timeFactor /= 60;
|
|
|
|
}
|
|
|
|
return TimeSpan.FromSeconds(secondsSum).Ticks;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets the state.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="request">The request.</param>
|
|
|
|
/// <param name="cancellationToken">The cancellation token.</param>
|
|
|
|
/// <returns>StreamState.</returns>
|
|
|
|
protected async Task<StreamState> GetState(StreamRequest request, CancellationToken cancellationToken)
|
|
|
|
{
|
|
|
|
ParseDlnaHeaders(request);
|
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(request.Params))
|
|
|
|
{
|
|
|
|
ParseParams(request);
|
|
|
|
}
|
|
|
|
|
2018-12-29 07:18:06 +00:00
|
|
|
ParseStreamOptions(request);
|
|
|
|
|
2018-12-14 09:40:55 +00:00
|
|
|
var url = Request.PathInfo;
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(request.AudioCodec))
|
|
|
|
{
|
|
|
|
request.AudioCodec = EncodingHelper.InferAudioCodec(url);
|
|
|
|
}
|
|
|
|
|
2019-07-27 17:55:18 +00:00
|
|
|
var enableDlnaHeaders = !string.IsNullOrWhiteSpace(request.Params) ||
|
|
|
|
string.Equals(GetHeader("GetContentFeatures.DLNA.ORG"), "1", StringComparison.OrdinalIgnoreCase);
|
2018-12-14 09:40:55 +00:00
|
|
|
|
2019-03-26 22:04:53 +00:00
|
|
|
var state = new StreamState(MediaSourceManager, TranscodingJobType)
|
2018-12-14 09:40:55 +00:00
|
|
|
{
|
|
|
|
Request = request,
|
|
|
|
RequestedUrl = url,
|
|
|
|
UserAgent = Request.UserAgent,
|
|
|
|
EnableDlnaHeaders = enableDlnaHeaders
|
|
|
|
};
|
|
|
|
|
|
|
|
var auth = AuthorizationContext.GetAuthorizationInfo(Request);
|
2019-01-03 06:58:14 +00:00
|
|
|
if (!auth.UserId.Equals(Guid.Empty))
|
2018-12-14 09:40:55 +00:00
|
|
|
{
|
|
|
|
state.User = UserManager.GetUserById(auth.UserId);
|
|
|
|
}
|
|
|
|
|
|
|
|
//if ((Request.UserAgent ?? string.Empty).IndexOf("iphone", StringComparison.OrdinalIgnoreCase) != -1 ||
|
|
|
|
// (Request.UserAgent ?? string.Empty).IndexOf("ipad", StringComparison.OrdinalIgnoreCase) != -1 ||
|
|
|
|
// (Request.UserAgent ?? string.Empty).IndexOf("ipod", StringComparison.OrdinalIgnoreCase) != -1)
|
|
|
|
//{
|
|
|
|
// state.SegmentLength = 6;
|
|
|
|
//}
|
|
|
|
|
2019-03-26 21:56:05 +00:00
|
|
|
if (state.VideoRequest != null && !string.IsNullOrWhiteSpace(state.VideoRequest.VideoCodec))
|
2018-12-14 09:40:55 +00:00
|
|
|
{
|
2019-03-26 21:56:05 +00:00
|
|
|
state.SupportedVideoCodecs = state.VideoRequest.VideoCodec.Split(',').Where(i => !string.IsNullOrWhiteSpace(i)).ToArray();
|
|
|
|
state.VideoRequest.VideoCodec = state.SupportedVideoCodecs.FirstOrDefault();
|
2018-12-14 09:40:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(request.AudioCodec))
|
|
|
|
{
|
|
|
|
state.SupportedAudioCodecs = request.AudioCodec.Split(',').Where(i => !string.IsNullOrWhiteSpace(i)).ToArray();
|
|
|
|
state.Request.AudioCodec = state.SupportedAudioCodecs.FirstOrDefault(i => MediaEncoder.CanEncodeToAudioCodec(i))
|
|
|
|
?? state.SupportedAudioCodecs.FirstOrDefault();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(request.SubtitleCodec))
|
|
|
|
{
|
|
|
|
state.SupportedSubtitleCodecs = request.SubtitleCodec.Split(',').Where(i => !string.IsNullOrWhiteSpace(i)).ToArray();
|
|
|
|
state.Request.SubtitleCodec = state.SupportedSubtitleCodecs.FirstOrDefault(i => MediaEncoder.CanEncodeToSubtitleCodec(i))
|
|
|
|
?? state.SupportedSubtitleCodecs.FirstOrDefault();
|
|
|
|
}
|
|
|
|
|
|
|
|
var item = LibraryManager.GetItemById(request.Id);
|
|
|
|
|
|
|
|
state.IsInputVideo = string.Equals(item.MediaType, MediaType.Video, StringComparison.OrdinalIgnoreCase);
|
|
|
|
|
|
|
|
//var primaryImage = item.GetImageInfo(ImageType.Primary, 0) ??
|
|
|
|
// item.Parents.Select(i => i.GetImageInfo(ImageType.Primary, 0)).FirstOrDefault(i => i != null);
|
|
|
|
//if (primaryImage != null)
|
|
|
|
//{
|
|
|
|
// state.AlbumCoverPath = primaryImage.Path;
|
|
|
|
//}
|
|
|
|
|
|
|
|
MediaSourceInfo mediaSource = null;
|
|
|
|
if (string.IsNullOrWhiteSpace(request.LiveStreamId))
|
|
|
|
{
|
2019-01-13 20:37:13 +00:00
|
|
|
var currentJob = !string.IsNullOrWhiteSpace(request.PlaySessionId) ?
|
2018-12-14 09:40:55 +00:00
|
|
|
ApiEntryPoint.Instance.GetTranscodingJob(request.PlaySessionId)
|
|
|
|
: null;
|
|
|
|
|
|
|
|
if (currentJob != null)
|
|
|
|
{
|
|
|
|
mediaSource = currentJob.MediaSource;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mediaSource == null)
|
|
|
|
{
|
2020-01-10 20:16:46 +00:00
|
|
|
var mediaSources = await MediaSourceManager.GetPlaybackMediaSources(LibraryManager.GetItemById(request.Id), null, false, false, cancellationToken).ConfigureAwait(false);
|
2018-12-14 09:40:55 +00:00
|
|
|
|
|
|
|
mediaSource = string.IsNullOrEmpty(request.MediaSourceId)
|
2019-03-26 21:56:05 +00:00
|
|
|
? mediaSources[0]
|
2019-03-26 22:04:53 +00:00
|
|
|
: mediaSources.Find(i => string.Equals(i.Id, request.MediaSourceId));
|
2018-12-14 09:40:55 +00:00
|
|
|
|
2019-12-10 16:25:21 +00:00
|
|
|
if (mediaSource == null && Guid.Parse(request.MediaSourceId) == request.Id)
|
2018-12-14 09:40:55 +00:00
|
|
|
{
|
2019-03-26 22:04:53 +00:00
|
|
|
mediaSource = mediaSources[0];
|
2018-12-14 09:40:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
var liveStreamInfo = await MediaSourceManager.GetLiveStreamWithDirectStreamProvider(request.LiveStreamId, cancellationToken).ConfigureAwait(false);
|
|
|
|
mediaSource = liveStreamInfo.Item1;
|
|
|
|
state.DirectStreamProvider = liveStreamInfo.Item2;
|
|
|
|
}
|
|
|
|
|
|
|
|
var videoRequest = request as VideoStreamRequest;
|
|
|
|
|
|
|
|
EncodingHelper.AttachMediaSourceInfo(state, mediaSource, url);
|
|
|
|
|
|
|
|
var container = Path.GetExtension(state.RequestedUrl);
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(container))
|
|
|
|
{
|
|
|
|
container = request.Container;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(container))
|
|
|
|
{
|
|
|
|
container = request.Static ?
|
|
|
|
StreamBuilder.NormalizeMediaSourceFormatIntoSingleContainer(state.InputContainer, state.MediaPath, null, DlnaProfileType.Audio) :
|
|
|
|
GetOutputFileExtension(state);
|
|
|
|
}
|
|
|
|
|
|
|
|
state.OutputContainer = (container ?? string.Empty).TrimStart('.');
|
|
|
|
|
|
|
|
state.OutputAudioBitrate = EncodingHelper.GetAudioBitrateParam(state.Request, state.AudioStream);
|
|
|
|
|
|
|
|
state.OutputAudioCodec = state.Request.AudioCodec;
|
|
|
|
|
|
|
|
state.OutputAudioChannels = EncodingHelper.GetNumAudioChannelsParam(state, state.AudioStream, state.OutputAudioCodec);
|
|
|
|
|
|
|
|
if (videoRequest != null)
|
|
|
|
{
|
|
|
|
state.OutputVideoCodec = state.VideoRequest.VideoCodec;
|
|
|
|
state.OutputVideoBitrate = EncodingHelper.GetVideoBitrateParamValue(state.VideoRequest, state.VideoStream, state.OutputVideoCodec);
|
|
|
|
|
|
|
|
if (videoRequest != null)
|
|
|
|
{
|
|
|
|
EncodingHelper.TryStreamCopy(state);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (state.OutputVideoBitrate.HasValue && !string.Equals(state.OutputVideoCodec, "copy", StringComparison.OrdinalIgnoreCase))
|
|
|
|
{
|
|
|
|
var resolution = ResolutionNormalizer.Normalize(
|
2019-03-26 21:56:05 +00:00
|
|
|
state.VideoStream?.BitRate,
|
|
|
|
state.VideoStream?.Width,
|
|
|
|
state.VideoStream?.Height,
|
2018-12-14 09:40:55 +00:00
|
|
|
state.OutputVideoBitrate.Value,
|
2019-03-26 21:56:05 +00:00
|
|
|
state.VideoStream?.Codec,
|
2018-12-14 09:40:55 +00:00
|
|
|
state.OutputVideoCodec,
|
|
|
|
videoRequest.MaxWidth,
|
|
|
|
videoRequest.MaxHeight);
|
|
|
|
|
|
|
|
videoRequest.MaxWidth = resolution.MaxWidth;
|
|
|
|
videoRequest.MaxHeight = resolution.MaxHeight;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-26 21:56:05 +00:00
|
|
|
ApplyDeviceProfileSettings(state);
|
|
|
|
|
2018-12-14 09:40:55 +00:00
|
|
|
var ext = string.IsNullOrWhiteSpace(state.OutputContainer)
|
|
|
|
? GetOutputFileExtension(state)
|
2019-03-26 21:56:05 +00:00
|
|
|
: ('.' + state.OutputContainer);
|
2018-12-14 09:40:55 +00:00
|
|
|
|
2019-11-08 11:49:00 +00:00
|
|
|
var encodingOptions = ServerConfigurationManager.GetEncodingOptions();
|
2018-12-14 09:40:55 +00:00
|
|
|
|
|
|
|
state.OutputFilePath = GetOutputFilePath(state, encodingOptions, ext);
|
|
|
|
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void ApplyDeviceProfileSettings(StreamState state)
|
|
|
|
{
|
2019-02-27 13:23:39 +00:00
|
|
|
var headers = Request.Headers;
|
2018-12-14 09:40:55 +00:00
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(state.Request.DeviceProfileId))
|
|
|
|
{
|
|
|
|
state.DeviceProfile = DlnaManager.GetProfile(state.Request.DeviceProfileId);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!string.IsNullOrWhiteSpace(state.Request.DeviceId))
|
|
|
|
{
|
|
|
|
var caps = DeviceManager.GetCapabilities(state.Request.DeviceId);
|
|
|
|
|
|
|
|
if (caps != null)
|
|
|
|
{
|
|
|
|
state.DeviceProfile = caps.DeviceProfile;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
state.DeviceProfile = DlnaManager.GetProfile(headers);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var profile = state.DeviceProfile;
|
|
|
|
|
|
|
|
if (profile == null)
|
|
|
|
{
|
2019-01-07 23:27:46 +00:00
|
|
|
// Don't use settings from the default profile.
|
2018-12-14 09:40:55 +00:00
|
|
|
// Only use a specific profile if it was requested.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var audioCodec = state.ActualOutputAudioCodec;
|
|
|
|
var videoCodec = state.ActualOutputVideoCodec;
|
|
|
|
|
|
|
|
var mediaProfile = state.VideoRequest == null ?
|
|
|
|
profile.GetAudioMediaProfile(state.OutputContainer, audioCodec, state.OutputAudioChannels, state.OutputAudioBitrate, state.OutputAudioSampleRate, state.OutputAudioBitDepth) :
|
|
|
|
profile.GetVideoMediaProfile(state.OutputContainer,
|
|
|
|
audioCodec,
|
|
|
|
videoCodec,
|
|
|
|
state.OutputWidth,
|
|
|
|
state.OutputHeight,
|
|
|
|
state.TargetVideoBitDepth,
|
|
|
|
state.OutputVideoBitrate,
|
|
|
|
state.TargetVideoProfile,
|
|
|
|
state.TargetVideoLevel,
|
|
|
|
state.TargetFramerate,
|
|
|
|
state.TargetPacketLength,
|
|
|
|
state.TargetTimestamp,
|
|
|
|
state.IsTargetAnamorphic,
|
|
|
|
state.IsTargetInterlaced,
|
|
|
|
state.TargetRefFrames,
|
|
|
|
state.TargetVideoStreamCount,
|
|
|
|
state.TargetAudioStreamCount,
|
|
|
|
state.TargetVideoCodecTag,
|
|
|
|
state.IsTargetAVC);
|
|
|
|
|
|
|
|
if (mediaProfile != null)
|
|
|
|
{
|
|
|
|
state.MimeType = mediaProfile.MimeType;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!state.Request.Static)
|
|
|
|
{
|
|
|
|
var transcodingProfile = state.VideoRequest == null ?
|
|
|
|
profile.GetAudioTranscodingProfile(state.OutputContainer, audioCodec) :
|
|
|
|
profile.GetVideoTranscodingProfile(state.OutputContainer, audioCodec, videoCodec);
|
|
|
|
|
|
|
|
if (transcodingProfile != null)
|
|
|
|
{
|
|
|
|
state.EstimateContentLength = transcodingProfile.EstimateContentLength;
|
|
|
|
//state.EnableMpegtsM2TsMode = transcodingProfile.EnableMpegtsM2TsMode;
|
|
|
|
state.TranscodeSeekInfo = transcodingProfile.TranscodeSeekInfo;
|
|
|
|
|
|
|
|
if (state.VideoRequest != null)
|
|
|
|
{
|
|
|
|
state.VideoRequest.CopyTimestamps = transcodingProfile.CopyTimestamps;
|
|
|
|
state.VideoRequest.EnableSubtitlesInManifest = transcodingProfile.EnableSubtitlesInManifest;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Adds the dlna headers.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="state">The state.</param>
|
|
|
|
/// <param name="responseHeaders">The response headers.</param>
|
|
|
|
/// <param name="isStaticallyStreamed">if set to <c>true</c> [is statically streamed].</param>
|
|
|
|
/// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
|
|
|
|
protected void AddDlnaHeaders(StreamState state, IDictionary<string, string> responseHeaders, bool isStaticallyStreamed)
|
|
|
|
{
|
|
|
|
if (!state.EnableDlnaHeaders)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var profile = state.DeviceProfile;
|
|
|
|
|
|
|
|
var transferMode = GetHeader("transferMode.dlna.org");
|
|
|
|
responseHeaders["transferMode.dlna.org"] = string.IsNullOrEmpty(transferMode) ? "Streaming" : transferMode;
|
|
|
|
responseHeaders["realTimeInfo.dlna.org"] = "DLNA.ORG_TLAG=*";
|
|
|
|
|
2019-03-26 21:56:05 +00:00
|
|
|
if (state.RunTimeTicks.HasValue)
|
2018-12-14 09:40:55 +00:00
|
|
|
{
|
2019-03-26 21:56:05 +00:00
|
|
|
if (string.Equals(GetHeader("getMediaInfo.sec"), "1", StringComparison.OrdinalIgnoreCase))
|
2018-12-14 09:40:55 +00:00
|
|
|
{
|
|
|
|
var ms = TimeSpan.FromTicks(state.RunTimeTicks.Value).TotalMilliseconds;
|
2019-09-08 19:07:29 +00:00
|
|
|
responseHeaders["MediaInfo.sec"] = string.Format(
|
|
|
|
CultureInfo.InvariantCulture,
|
|
|
|
"SEC_Duration={0};",
|
|
|
|
Convert.ToInt32(ms));
|
2018-12-14 09:40:55 +00:00
|
|
|
}
|
|
|
|
|
2019-03-26 21:56:05 +00:00
|
|
|
if (!isStaticallyStreamed && profile != null)
|
|
|
|
{
|
|
|
|
AddTimeSeekResponseHeaders(state, responseHeaders);
|
|
|
|
}
|
2018-12-14 09:40:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (profile == null)
|
|
|
|
{
|
|
|
|
profile = DlnaManager.GetDefaultProfile();
|
|
|
|
}
|
|
|
|
|
|
|
|
var audioCodec = state.ActualOutputAudioCodec;
|
|
|
|
|
|
|
|
if (state.VideoRequest == null)
|
|
|
|
{
|
2019-09-08 19:07:29 +00:00
|
|
|
responseHeaders["contentFeatures.dlna.org"] = new ContentFeatureBuilder(profile).BuildAudioHeader(
|
2018-12-14 09:40:55 +00:00
|
|
|
state.OutputContainer,
|
|
|
|
audioCodec,
|
|
|
|
state.OutputAudioBitrate,
|
|
|
|
state.OutputAudioSampleRate,
|
|
|
|
state.OutputAudioChannels,
|
|
|
|
state.OutputAudioBitDepth,
|
|
|
|
isStaticallyStreamed,
|
|
|
|
state.RunTimeTicks,
|
2019-09-08 19:07:29 +00:00
|
|
|
state.TranscodeSeekInfo);
|
2018-12-14 09:40:55 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
var videoCodec = state.ActualOutputVideoCodec;
|
|
|
|
|
2019-09-08 19:07:29 +00:00
|
|
|
responseHeaders["contentFeatures.dlna.org"] = new ContentFeatureBuilder(profile).BuildVideoHeader(
|
2018-12-14 09:40:55 +00:00
|
|
|
state.OutputContainer,
|
|
|
|
videoCodec,
|
|
|
|
audioCodec,
|
|
|
|
state.OutputWidth,
|
|
|
|
state.OutputHeight,
|
|
|
|
state.TargetVideoBitDepth,
|
|
|
|
state.OutputVideoBitrate,
|
|
|
|
state.TargetTimestamp,
|
|
|
|
isStaticallyStreamed,
|
|
|
|
state.RunTimeTicks,
|
|
|
|
state.TargetVideoProfile,
|
|
|
|
state.TargetVideoLevel,
|
|
|
|
state.TargetFramerate,
|
|
|
|
state.TargetPacketLength,
|
|
|
|
state.TranscodeSeekInfo,
|
|
|
|
state.IsTargetAnamorphic,
|
|
|
|
state.IsTargetInterlaced,
|
|
|
|
state.TargetRefFrames,
|
|
|
|
state.TargetVideoStreamCount,
|
|
|
|
state.TargetAudioStreamCount,
|
|
|
|
state.TargetVideoCodecTag,
|
2019-09-08 19:07:29 +00:00
|
|
|
state.IsTargetAVC).FirstOrDefault() ?? string.Empty;
|
2018-12-14 09:40:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void AddTimeSeekResponseHeaders(StreamState state, IDictionary<string, string> responseHeaders)
|
|
|
|
{
|
2019-04-17 13:23:03 +00:00
|
|
|
var runtimeSeconds = TimeSpan.FromTicks(state.RunTimeTicks.Value).TotalSeconds.ToString(CultureInfo.InvariantCulture);
|
|
|
|
var startSeconds = TimeSpan.FromTicks(state.Request.StartTimeTicks ?? 0).TotalSeconds.ToString(CultureInfo.InvariantCulture);
|
2018-12-14 09:40:55 +00:00
|
|
|
|
2019-09-08 19:07:29 +00:00
|
|
|
responseHeaders["TimeSeekRange.dlna.org"] = string.Format(
|
|
|
|
CultureInfo.InvariantCulture,
|
|
|
|
"npt={0}-{1}/{1}",
|
|
|
|
startSeconds,
|
|
|
|
runtimeSeconds);
|
|
|
|
responseHeaders["X-AvailableSeekRange"] = string.Format(
|
|
|
|
CultureInfo.InvariantCulture,
|
|
|
|
"1 npt={0}-{1}",
|
|
|
|
startSeconds,
|
|
|
|
runtimeSeconds);
|
2018-12-14 09:40:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|