2019-01-13 13:01:16 -07:00
|
|
|
using System;
|
2019-01-13 12:24:58 -07:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Globalization;
|
2019-01-26 14:59:53 -07:00
|
|
|
using System.IO;
|
2019-01-13 12:24:58 -07:00
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using MediaBrowser.Common.Net;
|
2018-12-14 02:40:55 -07:00
|
|
|
using MediaBrowser.Controller.Configuration;
|
|
|
|
using MediaBrowser.Controller.Devices;
|
|
|
|
using MediaBrowser.Controller.Dlna;
|
|
|
|
using MediaBrowser.Controller.Drawing;
|
|
|
|
using MediaBrowser.Controller.Library;
|
|
|
|
using MediaBrowser.Controller.MediaEncoding;
|
|
|
|
using MediaBrowser.Controller.Net;
|
|
|
|
using MediaBrowser.Model.IO;
|
|
|
|
using MediaBrowser.Model.MediaInfo;
|
|
|
|
using MediaBrowser.Model.Serialization;
|
|
|
|
using MediaBrowser.Model.Services;
|
|
|
|
using MediaBrowser.Model.System;
|
2019-03-05 11:20:28 -07:00
|
|
|
using Microsoft.Net.Http.Headers;
|
2018-12-14 02:40:55 -07:00
|
|
|
|
|
|
|
namespace MediaBrowser.Api.Playback.Progressive
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// Class BaseProgressiveStreamingService
|
|
|
|
/// </summary>
|
|
|
|
public abstract class BaseProgressiveStreamingService : BaseStreamingService
|
|
|
|
{
|
|
|
|
protected readonly IEnvironmentInfo EnvironmentInfo;
|
2019-02-11 15:23:51 -07:00
|
|
|
protected IHttpClient HttpClient { get; private set; }
|
2018-12-14 02:40:55 -07:00
|
|
|
|
2019-02-02 15:55:47 -07:00
|
|
|
public BaseProgressiveStreamingService(
|
2019-02-11 06:24:00 -07:00
|
|
|
IHttpClient httpClient,
|
2019-02-02 15:55:47 -07:00
|
|
|
IServerConfigurationManager serverConfig,
|
|
|
|
IUserManager userManager,
|
|
|
|
ILibraryManager libraryManager,
|
|
|
|
IIsoManager isoManager,
|
|
|
|
IMediaEncoder mediaEncoder,
|
|
|
|
IFileSystem fileSystem,
|
|
|
|
IDlnaManager dlnaManager,
|
|
|
|
ISubtitleEncoder subtitleEncoder,
|
|
|
|
IDeviceManager deviceManager,
|
|
|
|
IMediaSourceManager mediaSourceManager,
|
|
|
|
IJsonSerializer jsonSerializer,
|
|
|
|
IAuthorizationContext authorizationContext,
|
|
|
|
IEnvironmentInfo environmentInfo)
|
2019-02-11 15:23:51 -07:00
|
|
|
: base(serverConfig,
|
2019-02-02 15:55:47 -07:00
|
|
|
userManager,
|
|
|
|
libraryManager,
|
|
|
|
isoManager,
|
|
|
|
mediaEncoder,
|
|
|
|
fileSystem,
|
|
|
|
dlnaManager,
|
|
|
|
subtitleEncoder,
|
|
|
|
deviceManager,
|
|
|
|
mediaSourceManager,
|
|
|
|
jsonSerializer,
|
|
|
|
authorizationContext)
|
2018-12-14 02:40:55 -07:00
|
|
|
{
|
|
|
|
EnvironmentInfo = environmentInfo;
|
2019-02-11 15:23:51 -07:00
|
|
|
HttpClient = httpClient;
|
2018-12-14 02:40:55 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets the output file extension.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="state">The state.</param>
|
|
|
|
/// <returns>System.String.</returns>
|
|
|
|
protected override string GetOutputFileExtension(StreamState state)
|
|
|
|
{
|
|
|
|
var ext = base.GetOutputFileExtension(state);
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(ext))
|
|
|
|
{
|
|
|
|
return ext;
|
|
|
|
}
|
|
|
|
|
|
|
|
var isVideoRequest = state.VideoRequest != null;
|
|
|
|
|
|
|
|
// Try to infer based on the desired video codec
|
|
|
|
if (isVideoRequest)
|
|
|
|
{
|
|
|
|
var videoCodec = state.VideoRequest.VideoCodec;
|
|
|
|
|
|
|
|
if (string.Equals(videoCodec, "h264", StringComparison.OrdinalIgnoreCase))
|
|
|
|
{
|
|
|
|
return ".ts";
|
|
|
|
}
|
|
|
|
if (string.Equals(videoCodec, "theora", StringComparison.OrdinalIgnoreCase))
|
|
|
|
{
|
|
|
|
return ".ogv";
|
|
|
|
}
|
|
|
|
if (string.Equals(videoCodec, "vpx", StringComparison.OrdinalIgnoreCase))
|
|
|
|
{
|
|
|
|
return ".webm";
|
|
|
|
}
|
|
|
|
if (string.Equals(videoCodec, "wmv", StringComparison.OrdinalIgnoreCase))
|
|
|
|
{
|
|
|
|
return ".asf";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to infer based on the desired audio codec
|
|
|
|
if (!isVideoRequest)
|
|
|
|
{
|
|
|
|
var audioCodec = state.Request.AudioCodec;
|
|
|
|
|
|
|
|
if (string.Equals("aac", audioCodec, StringComparison.OrdinalIgnoreCase))
|
|
|
|
{
|
|
|
|
return ".aac";
|
|
|
|
}
|
|
|
|
if (string.Equals("mp3", audioCodec, StringComparison.OrdinalIgnoreCase))
|
|
|
|
{
|
|
|
|
return ".mp3";
|
|
|
|
}
|
|
|
|
if (string.Equals("vorbis", audioCodec, StringComparison.OrdinalIgnoreCase))
|
|
|
|
{
|
|
|
|
return ".ogg";
|
|
|
|
}
|
|
|
|
if (string.Equals("wma", audioCodec, StringComparison.OrdinalIgnoreCase))
|
|
|
|
{
|
|
|
|
return ".wma";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets the type of the transcoding job.
|
|
|
|
/// </summary>
|
|
|
|
/// <value>The type of the transcoding job.</value>
|
2019-01-13 13:31:14 -07:00
|
|
|
protected override TranscodingJobType TranscodingJobType => TranscodingJobType.Progressive;
|
2018-12-14 02:40:55 -07:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Processes the request.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="request">The request.</param>
|
|
|
|
/// <param name="isHeadRequest">if set to <c>true</c> [is head request].</param>
|
|
|
|
/// <returns>Task.</returns>
|
|
|
|
protected async Task<object> ProcessRequest(StreamRequest request, bool isHeadRequest)
|
|
|
|
{
|
|
|
|
var cancellationTokenSource = new CancellationTokenSource();
|
|
|
|
|
|
|
|
var state = await GetState(request, cancellationTokenSource.Token).ConfigureAwait(false);
|
|
|
|
|
|
|
|
var responseHeaders = new Dictionary<string, string>();
|
|
|
|
|
|
|
|
if (request.Static && state.DirectStreamProvider != null)
|
|
|
|
{
|
|
|
|
AddDlnaHeaders(state, responseHeaders, true);
|
|
|
|
|
|
|
|
using (state)
|
|
|
|
{
|
|
|
|
var outputHeaders = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
|
|
|
|
|
|
|
|
// TODO: Don't hardcode this
|
2019-03-05 11:20:28 -07:00
|
|
|
outputHeaders[HeaderNames.ContentType] = Model.Net.MimeTypes.GetMimeType("file.ts");
|
2018-12-14 02:40:55 -07:00
|
|
|
|
|
|
|
return new ProgressiveFileCopier(state.DirectStreamProvider, outputHeaders, null, Logger, EnvironmentInfo, CancellationToken.None)
|
|
|
|
{
|
|
|
|
AllowEndOfFile = false
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Static remote stream
|
|
|
|
if (request.Static && state.InputProtocol == MediaProtocol.Http)
|
|
|
|
{
|
|
|
|
AddDlnaHeaders(state, responseHeaders, true);
|
|
|
|
|
|
|
|
using (state)
|
|
|
|
{
|
|
|
|
return await GetStaticRemoteStreamResult(state, responseHeaders, isHeadRequest, cancellationTokenSource).ConfigureAwait(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (request.Static && state.InputProtocol != MediaProtocol.File)
|
|
|
|
{
|
|
|
|
throw new ArgumentException(string.Format("Input protocol {0} cannot be streamed statically.", state.InputProtocol));
|
|
|
|
}
|
|
|
|
|
|
|
|
var outputPath = state.OutputFilePath;
|
2019-01-26 14:59:53 -07:00
|
|
|
var outputPathExists = File.Exists(outputPath);
|
2018-12-14 02:40:55 -07:00
|
|
|
|
|
|
|
var transcodingJob = ApiEntryPoint.Instance.GetTranscodingJob(outputPath, TranscodingJobType.Progressive);
|
|
|
|
var isTranscodeCached = outputPathExists && transcodingJob != null;
|
|
|
|
|
|
|
|
AddDlnaHeaders(state, responseHeaders, request.Static || isTranscodeCached);
|
|
|
|
|
|
|
|
// Static stream
|
|
|
|
if (request.Static)
|
|
|
|
{
|
|
|
|
var contentType = state.GetMimeType("." + state.OutputContainer, false) ?? state.GetMimeType(state.MediaPath);
|
|
|
|
|
|
|
|
using (state)
|
|
|
|
{
|
|
|
|
if (state.MediaSource.IsInfiniteStream)
|
|
|
|
{
|
2019-03-05 11:20:28 -07:00
|
|
|
var outputHeaders = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
|
|
|
|
{
|
|
|
|
[HeaderNames.ContentType] = contentType
|
|
|
|
};
|
2018-12-14 02:40:55 -07:00
|
|
|
|
|
|
|
|
|
|
|
return new ProgressiveFileCopier(FileSystem, state.MediaPath, outputHeaders, null, Logger, EnvironmentInfo, CancellationToken.None)
|
|
|
|
{
|
|
|
|
AllowEndOfFile = false
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
TimeSpan? cacheDuration = null;
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(request.Tag))
|
|
|
|
{
|
|
|
|
cacheDuration = TimeSpan.FromDays(365);
|
|
|
|
}
|
|
|
|
|
|
|
|
return await ResultFactory.GetStaticFileResult(Request, new StaticFileResultOptions
|
|
|
|
{
|
|
|
|
ResponseHeaders = responseHeaders,
|
|
|
|
ContentType = contentType,
|
|
|
|
IsHeadRequest = isHeadRequest,
|
|
|
|
Path = state.MediaPath,
|
|
|
|
CacheDuration = cacheDuration
|
|
|
|
|
|
|
|
}).ConfigureAwait(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//// Not static but transcode cache file exists
|
|
|
|
//if (isTranscodeCached && state.VideoRequest == null)
|
|
|
|
//{
|
|
|
|
// var contentType = state.GetMimeType(outputPath);
|
|
|
|
|
|
|
|
// try
|
|
|
|
// {
|
|
|
|
// if (transcodingJob != null)
|
|
|
|
// {
|
|
|
|
// ApiEntryPoint.Instance.OnTranscodeBeginRequest(transcodingJob);
|
|
|
|
// }
|
|
|
|
|
|
|
|
// return await ResultFactory.GetStaticFileResult(Request, new StaticFileResultOptions
|
|
|
|
// {
|
|
|
|
// ResponseHeaders = responseHeaders,
|
|
|
|
// ContentType = contentType,
|
|
|
|
// IsHeadRequest = isHeadRequest,
|
|
|
|
// Path = outputPath,
|
|
|
|
// FileShare = FileShareMode.ReadWrite,
|
|
|
|
// OnComplete = () =>
|
|
|
|
// {
|
|
|
|
// if (transcodingJob != null)
|
|
|
|
// {
|
|
|
|
// ApiEntryPoint.Instance.OnTranscodeEndRequest(transcodingJob);
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
|
|
|
// }).ConfigureAwait(false);
|
|
|
|
// }
|
|
|
|
// finally
|
|
|
|
// {
|
|
|
|
// state.Dispose();
|
|
|
|
// }
|
|
|
|
//}
|
|
|
|
|
|
|
|
// Need to start ffmpeg
|
|
|
|
try
|
|
|
|
{
|
|
|
|
return await GetStreamResult(request, state, responseHeaders, isHeadRequest, cancellationTokenSource).ConfigureAwait(false);
|
|
|
|
}
|
|
|
|
catch
|
|
|
|
{
|
|
|
|
state.Dispose();
|
|
|
|
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets the static remote stream result.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="state">The state.</param>
|
|
|
|
/// <param name="responseHeaders">The response headers.</param>
|
|
|
|
/// <param name="isHeadRequest">if set to <c>true</c> [is head request].</param>
|
|
|
|
/// <param name="cancellationTokenSource">The cancellation token source.</param>
|
|
|
|
/// <returns>Task{System.Object}.</returns>
|
|
|
|
private async Task<object> GetStaticRemoteStreamResult(StreamState state, Dictionary<string, string> responseHeaders, bool isHeadRequest, CancellationTokenSource cancellationTokenSource)
|
|
|
|
{
|
|
|
|
string useragent = null;
|
|
|
|
state.RemoteHttpHeaders.TryGetValue("User-Agent", out useragent);
|
|
|
|
|
|
|
|
var trySupportSeek = false;
|
|
|
|
|
|
|
|
var options = new HttpRequestOptions
|
|
|
|
{
|
|
|
|
Url = state.MediaPath,
|
|
|
|
UserAgent = useragent,
|
|
|
|
BufferContent = false,
|
|
|
|
CancellationToken = cancellationTokenSource.Token
|
|
|
|
};
|
|
|
|
|
|
|
|
if (trySupportSeek)
|
|
|
|
{
|
2019-03-05 11:20:28 -07:00
|
|
|
if (!string.IsNullOrWhiteSpace(Request.QueryString[HeaderNames.Range]))
|
2018-12-14 02:40:55 -07:00
|
|
|
{
|
2019-03-05 11:20:28 -07:00
|
|
|
options.RequestHeaders[HeaderNames.Range] = Request.QueryString[HeaderNames.Range];
|
2018-12-14 02:40:55 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
var response = await HttpClient.GetResponse(options).ConfigureAwait(false);
|
|
|
|
|
|
|
|
if (trySupportSeek)
|
|
|
|
{
|
2019-03-05 11:20:28 -07:00
|
|
|
foreach (var name in new[] { HeaderNames.ContentRange, HeaderNames.AcceptRanges })
|
2018-12-14 02:40:55 -07:00
|
|
|
{
|
|
|
|
var val = response.Headers[name];
|
|
|
|
if (!string.IsNullOrWhiteSpace(val))
|
|
|
|
{
|
|
|
|
responseHeaders[name] = val;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-03-05 11:20:28 -07:00
|
|
|
responseHeaders[HeaderNames.AcceptRanges] = "none";
|
2018-12-14 02:40:55 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (isHeadRequest)
|
|
|
|
{
|
|
|
|
using (response)
|
|
|
|
{
|
|
|
|
return ResultFactory.GetResult(null, new byte[] { }, response.ContentType, responseHeaders);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var result = new StaticRemoteStreamWriter(response);
|
|
|
|
|
2019-03-05 11:20:28 -07:00
|
|
|
result.Headers[HeaderNames.ContentType] = response.ContentType;
|
2018-12-14 02:40:55 -07:00
|
|
|
|
|
|
|
// Add the response headers to the result object
|
|
|
|
foreach (var header in responseHeaders)
|
|
|
|
{
|
|
|
|
result.Headers[header.Key] = header.Value;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets the stream result.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="state">The state.</param>
|
|
|
|
/// <param name="responseHeaders">The response headers.</param>
|
|
|
|
/// <param name="isHeadRequest">if set to <c>true</c> [is head request].</param>
|
|
|
|
/// <param name="cancellationTokenSource">The cancellation token source.</param>
|
|
|
|
/// <returns>Task{System.Object}.</returns>
|
|
|
|
private async Task<object> GetStreamResult(StreamRequest request, StreamState state, IDictionary<string, string> responseHeaders, bool isHeadRequest, CancellationTokenSource cancellationTokenSource)
|
|
|
|
{
|
|
|
|
// Use the command line args with a dummy playlist path
|
|
|
|
var outputPath = state.OutputFilePath;
|
|
|
|
|
2019-03-05 11:20:28 -07:00
|
|
|
responseHeaders[HeaderNames.AcceptRanges] = "none";
|
2018-12-14 02:40:55 -07:00
|
|
|
|
|
|
|
var contentType = state.GetMimeType(outputPath);
|
|
|
|
|
|
|
|
// Headers only
|
|
|
|
if (isHeadRequest)
|
|
|
|
{
|
2019-03-05 11:20:28 -07:00
|
|
|
return ResultFactory.GetResult(null, new byte[] { }, contentType, responseHeaders);
|
2018-12-14 02:40:55 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
var transcodingLock = ApiEntryPoint.Instance.GetTranscodingLock(outputPath);
|
|
|
|
await transcodingLock.WaitAsync(cancellationTokenSource.Token).ConfigureAwait(false);
|
|
|
|
try
|
|
|
|
{
|
|
|
|
TranscodingJob job;
|
|
|
|
|
2019-01-26 14:59:53 -07:00
|
|
|
if (!File.Exists(outputPath))
|
2018-12-14 02:40:55 -07:00
|
|
|
{
|
|
|
|
job = await StartFfMpeg(state, outputPath, cancellationTokenSource).ConfigureAwait(false);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
job = ApiEntryPoint.Instance.OnTranscodeBeginRequest(outputPath, TranscodingJobType.Progressive);
|
|
|
|
state.Dispose();
|
|
|
|
}
|
|
|
|
|
2019-03-05 11:20:28 -07:00
|
|
|
var outputHeaders = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
|
|
|
|
{
|
|
|
|
[HeaderNames.ContentType] = contentType
|
|
|
|
};
|
2018-12-14 02:40:55 -07:00
|
|
|
|
|
|
|
|
|
|
|
// Add the response headers to the result object
|
|
|
|
foreach (var item in responseHeaders)
|
|
|
|
{
|
|
|
|
outputHeaders[item.Key] = item.Value;
|
|
|
|
}
|
|
|
|
|
|
|
|
return new ProgressiveFileCopier(FileSystem, outputPath, outputHeaders, job, Logger, EnvironmentInfo, CancellationToken.None);
|
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
|
|
|
transcodingLock.Release();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|