2016-03-27 14:11:27 -07:00
|
|
|
|
using MediaBrowser.Common.Net;
|
2013-12-15 12:39:21 -07:00
|
|
|
|
using MediaBrowser.Controller.Configuration;
|
2015-01-19 22:19:13 -07:00
|
|
|
|
using MediaBrowser.Controller.Devices;
|
2014-03-24 22:25:03 -07:00
|
|
|
|
using MediaBrowser.Controller.Dlna;
|
2013-09-18 11:49:06 -07:00
|
|
|
|
using MediaBrowser.Controller.Drawing;
|
2013-02-27 13:25:45 -07:00
|
|
|
|
using MediaBrowser.Controller.Library;
|
2014-02-20 09:37:41 -07:00
|
|
|
|
using MediaBrowser.Controller.MediaEncoding;
|
2014-08-30 07:26:29 -07:00
|
|
|
|
using MediaBrowser.Controller.Net;
|
2013-09-01 06:13:11 -07:00
|
|
|
|
using MediaBrowser.Model.IO;
|
2014-06-16 18:56:23 -07:00
|
|
|
|
using MediaBrowser.Model.MediaInfo;
|
2015-05-04 10:44:25 -07:00
|
|
|
|
using MediaBrowser.Model.Serialization;
|
2014-02-20 09:37:41 -07:00
|
|
|
|
using ServiceStack.Web;
|
2014-02-13 09:38:43 -07:00
|
|
|
|
using System;
|
2013-03-23 19:45:00 -07:00
|
|
|
|
using System.Collections.Generic;
|
2015-06-01 18:26:51 -07:00
|
|
|
|
using System.Globalization;
|
2016-07-15 10:13:55 -07:00
|
|
|
|
using System.IO;
|
2013-12-21 11:37:34 -07:00
|
|
|
|
using System.Threading;
|
2013-02-26 21:19:05 -07:00
|
|
|
|
using System.Threading.Tasks;
|
2015-10-03 21:23:11 -07:00
|
|
|
|
using CommonIO;
|
2013-02-26 21:19:05 -07:00
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.Api.Playback.Progressive
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Class BaseProgressiveStreamingService
|
|
|
|
|
/// </summary>
|
|
|
|
|
public abstract class BaseProgressiveStreamingService : BaseStreamingService
|
|
|
|
|
{
|
2013-09-18 11:49:06 -07:00
|
|
|
|
protected readonly IImageProcessor ImageProcessor;
|
2014-06-11 12:31:33 -07:00
|
|
|
|
protected readonly IHttpClient HttpClient;
|
2013-09-04 10:02:19 -07:00
|
|
|
|
|
2015-05-04 10:44:25 -07:00
|
|
|
|
protected BaseProgressiveStreamingService(IServerConfigurationManager serverConfig, IUserManager userManager, ILibraryManager libraryManager, IIsoManager isoManager, IMediaEncoder mediaEncoder, IFileSystem fileSystem, IDlnaManager dlnaManager, ISubtitleEncoder subtitleEncoder, IDeviceManager deviceManager, IMediaSourceManager mediaSourceManager, IZipClient zipClient, IJsonSerializer jsonSerializer, IImageProcessor imageProcessor, IHttpClient httpClient) : base(serverConfig, userManager, libraryManager, isoManager, mediaEncoder, fileSystem, dlnaManager, subtitleEncoder, deviceManager, mediaSourceManager, zipClient, jsonSerializer)
|
2013-02-26 21:19:05 -07:00
|
|
|
|
{
|
2014-02-20 09:37:41 -07:00
|
|
|
|
ImageProcessor = imageProcessor;
|
2014-06-11 12:31:33 -07:00
|
|
|
|
HttpClient = httpClient;
|
2013-02-26 21:19:05 -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;
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-05 17:39:02 -07:00
|
|
|
|
var isVideoRequest = state.VideoRequest != null;
|
2013-03-08 19:34:54 -07:00
|
|
|
|
|
2013-02-26 21:19:05 -07:00
|
|
|
|
// Try to infer based on the desired video codec
|
2014-06-05 17:39:02 -07:00
|
|
|
|
if (isVideoRequest)
|
2013-02-26 21:19:05 -07:00
|
|
|
|
{
|
2014-06-05 17:39:02 -07:00
|
|
|
|
var videoCodec = state.VideoRequest.VideoCodec;
|
2014-08-30 07:26:29 -07:00
|
|
|
|
|
|
|
|
|
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";
|
|
|
|
|
}
|
2013-02-26 21:19:05 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Try to infer based on the desired audio codec
|
2014-06-05 17:39:02 -07:00
|
|
|
|
if (!isVideoRequest)
|
2013-02-26 21:19:05 -07:00
|
|
|
|
{
|
2014-06-05 17:39:02 -07:00
|
|
|
|
var audioCodec = state.Request.AudioCodec;
|
|
|
|
|
|
|
|
|
|
if (string.Equals("aac", audioCodec, StringComparison.OrdinalIgnoreCase))
|
2013-02-26 21:19:05 -07:00
|
|
|
|
{
|
2014-06-05 17:39:02 -07:00
|
|
|
|
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";
|
2013-02-26 21:19:05 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the type of the transcoding job.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value>The type of the transcoding job.</value>
|
|
|
|
|
protected override TranscodingJobType TranscodingJobType
|
|
|
|
|
{
|
|
|
|
|
get { return TranscodingJobType.Progressive; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Processes the request.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="request">The request.</param>
|
2013-03-12 20:57:54 -07:00
|
|
|
|
/// <param name="isHeadRequest">if set to <c>true</c> [is head request].</param>
|
2013-02-26 21:19:05 -07:00
|
|
|
|
/// <returns>Task.</returns>
|
2016-06-18 23:18:29 -07:00
|
|
|
|
protected async Task<object> ProcessRequest(StreamRequest request, bool isHeadRequest)
|
2013-02-26 21:19:05 -07:00
|
|
|
|
{
|
2014-08-16 22:38:13 -07:00
|
|
|
|
var cancellationTokenSource = new CancellationTokenSource();
|
|
|
|
|
|
2016-06-18 23:18:29 -07:00
|
|
|
|
var state = await GetState(request, cancellationTokenSource.Token).ConfigureAwait(false);
|
2013-02-26 21:19:05 -07:00
|
|
|
|
|
2013-03-23 19:45:00 -07:00
|
|
|
|
var responseHeaders = new Dictionary<string, string>();
|
|
|
|
|
|
2014-04-05 08:02:50 -07:00
|
|
|
|
// Static remote stream
|
2014-06-16 18:56:23 -07:00
|
|
|
|
if (request.Static && state.InputProtocol == MediaProtocol.Http)
|
2013-05-17 11:05:49 -07:00
|
|
|
|
{
|
2014-02-03 10:44:13 -07:00
|
|
|
|
AddDlnaHeaders(state, responseHeaders, true);
|
2014-02-13 09:38:43 -07:00
|
|
|
|
|
2014-07-08 19:53:04 -07:00
|
|
|
|
using (state)
|
2014-04-05 08:02:50 -07:00
|
|
|
|
{
|
2016-06-18 23:18:29 -07:00
|
|
|
|
return await GetStaticRemoteStreamResult(state, responseHeaders, isHeadRequest, cancellationTokenSource)
|
|
|
|
|
.ConfigureAwait(false);
|
2014-04-05 08:02:50 -07:00
|
|
|
|
}
|
2013-05-17 11:05:49 -07:00
|
|
|
|
}
|
|
|
|
|
|
2014-06-16 18:56:23 -07:00
|
|
|
|
if (request.Static && state.InputProtocol != MediaProtocol.File)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentException(string.Format("Input protocol {0} cannot be streamed statically.", state.InputProtocol));
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-05 17:39:02 -07:00
|
|
|
|
var outputPath = state.OutputFilePath;
|
2016-06-18 23:18:29 -07:00
|
|
|
|
var outputPathExists = FileSystem.FileExists(outputPath);
|
2013-04-03 05:03:37 -07:00
|
|
|
|
|
2016-08-06 07:08:38 -07:00
|
|
|
|
var transcodingJob = ApiEntryPoint.Instance.GetTranscodingJob(outputPath, TranscodingJobType.Progressive);
|
|
|
|
|
var isTranscodeCached = outputPathExists && transcodingJob != null;
|
2013-04-03 05:03:37 -07:00
|
|
|
|
|
2014-09-02 19:30:24 -07:00
|
|
|
|
AddDlnaHeaders(state, responseHeaders, request.Static || isTranscodeCached);
|
2013-03-13 18:46:27 -07:00
|
|
|
|
|
2014-04-05 08:02:50 -07:00
|
|
|
|
// Static stream
|
2013-02-26 21:19:05 -07:00
|
|
|
|
if (request.Static)
|
|
|
|
|
{
|
2014-03-12 12:56:12 -07:00
|
|
|
|
var contentType = state.GetMimeType(state.MediaPath);
|
|
|
|
|
|
2014-07-08 19:53:04 -07:00
|
|
|
|
using (state)
|
2014-04-05 08:02:50 -07:00
|
|
|
|
{
|
2016-06-18 23:18:29 -07:00
|
|
|
|
return await ResultFactory.GetStaticFileResult(Request, new StaticFileResultOptions
|
2014-08-30 07:26:29 -07:00
|
|
|
|
{
|
|
|
|
|
ResponseHeaders = responseHeaders,
|
|
|
|
|
ContentType = contentType,
|
|
|
|
|
IsHeadRequest = isHeadRequest,
|
2015-02-26 11:24:38 -07:00
|
|
|
|
Path = state.MediaPath
|
2016-08-06 07:08:38 -07:00
|
|
|
|
|
2016-06-18 23:18:29 -07:00
|
|
|
|
}).ConfigureAwait(false);
|
2014-04-05 08:02:50 -07:00
|
|
|
|
}
|
2013-02-26 21:19:05 -07:00
|
|
|
|
}
|
|
|
|
|
|
2016-08-08 22:10:17 -07:00
|
|
|
|
//// 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 = FileShare.ReadWrite,
|
|
|
|
|
// OnComplete = () =>
|
|
|
|
|
// {
|
|
|
|
|
// if (transcodingJob != null)
|
|
|
|
|
// {
|
|
|
|
|
// ApiEntryPoint.Instance.OnTranscodeEndRequest(transcodingJob);
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// }).ConfigureAwait(false);
|
|
|
|
|
// }
|
|
|
|
|
// finally
|
|
|
|
|
// {
|
|
|
|
|
// state.Dispose();
|
|
|
|
|
// }
|
|
|
|
|
//}
|
2014-04-05 08:02:50 -07:00
|
|
|
|
|
|
|
|
|
// Need to start ffmpeg
|
|
|
|
|
try
|
|
|
|
|
{
|
2016-07-27 23:29:14 -07:00
|
|
|
|
return await GetStreamResult(state, responseHeaders, isHeadRequest, cancellationTokenSource).ConfigureAwait(false);
|
2013-02-26 21:19:05 -07:00
|
|
|
|
}
|
2014-04-05 08:02:50 -07:00
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
state.Dispose();
|
2013-02-26 21:19:05 -07:00
|
|
|
|
|
2014-04-05 08:02:50 -07:00
|
|
|
|
throw;
|
|
|
|
|
}
|
2013-02-26 21:19:05 -07:00
|
|
|
|
}
|
|
|
|
|
|
2013-05-17 11:05:49 -07:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the static remote stream result.
|
|
|
|
|
/// </summary>
|
2014-06-16 18:56:23 -07:00
|
|
|
|
/// <param name="state">The state.</param>
|
2013-05-17 11:05:49 -07:00
|
|
|
|
/// <param name="responseHeaders">The response headers.</param>
|
|
|
|
|
/// <param name="isHeadRequest">if set to <c>true</c> [is head request].</param>
|
2014-08-16 22:38:13 -07:00
|
|
|
|
/// <param name="cancellationTokenSource">The cancellation token source.</param>
|
2013-05-17 11:05:49 -07:00
|
|
|
|
/// <returns>Task{System.Object}.</returns>
|
2014-08-16 22:38:13 -07:00
|
|
|
|
private async Task<object> GetStaticRemoteStreamResult(StreamState state, Dictionary<string, string> responseHeaders, bool isHeadRequest, CancellationTokenSource cancellationTokenSource)
|
2013-05-17 11:05:49 -07:00
|
|
|
|
{
|
2014-06-16 18:56:23 -07:00
|
|
|
|
string useragent = null;
|
|
|
|
|
state.RemoteHttpHeaders.TryGetValue("User-Agent", out useragent);
|
|
|
|
|
|
2014-10-11 18:46:02 -07:00
|
|
|
|
var trySupportSeek = false;
|
|
|
|
|
|
2014-02-13 21:00:13 -07:00
|
|
|
|
var options = new HttpRequestOptions
|
2013-05-17 11:05:49 -07:00
|
|
|
|
{
|
2014-06-16 18:56:23 -07:00
|
|
|
|
Url = state.MediaPath,
|
|
|
|
|
UserAgent = useragent,
|
2014-08-16 22:38:13 -07:00
|
|
|
|
BufferContent = false,
|
|
|
|
|
CancellationToken = cancellationTokenSource.Token
|
2014-02-13 21:00:13 -07:00
|
|
|
|
};
|
2013-05-17 11:05:49 -07:00
|
|
|
|
|
2014-10-11 18:46:02 -07:00
|
|
|
|
if (trySupportSeek)
|
2014-10-11 13:38:13 -07:00
|
|
|
|
{
|
2014-10-11 18:46:02 -07:00
|
|
|
|
if (!string.IsNullOrWhiteSpace(Request.QueryString["Range"]))
|
|
|
|
|
{
|
|
|
|
|
options.RequestHeaders["Range"] = Request.QueryString["Range"];
|
|
|
|
|
}
|
2014-10-11 13:38:13 -07:00
|
|
|
|
}
|
|
|
|
|
var response = await HttpClient.GetResponse(options).ConfigureAwait(false);
|
2014-02-18 22:21:03 -07:00
|
|
|
|
|
2014-10-11 18:46:02 -07:00
|
|
|
|
if (trySupportSeek)
|
2014-02-18 22:21:03 -07:00
|
|
|
|
{
|
2016-06-18 23:18:29 -07:00
|
|
|
|
foreach (var name in new[] { "Content-Range", "Accept-Ranges" })
|
2014-10-11 13:38:13 -07:00
|
|
|
|
{
|
2014-10-11 18:46:02 -07:00
|
|
|
|
var val = response.Headers[name];
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(val))
|
|
|
|
|
{
|
|
|
|
|
responseHeaders[name] = val;
|
|
|
|
|
}
|
2014-10-11 13:38:13 -07:00
|
|
|
|
}
|
2014-02-18 22:21:03 -07:00
|
|
|
|
}
|
2014-10-11 18:46:02 -07:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
responseHeaders["Accept-Ranges"] = "none";
|
|
|
|
|
}
|
2016-06-18 23:18:29 -07:00
|
|
|
|
|
2014-10-11 18:46:02 -07:00
|
|
|
|
if (response.ContentLength.HasValue)
|
|
|
|
|
{
|
|
|
|
|
responseHeaders["Content-Length"] = response.ContentLength.Value.ToString(UsCulture);
|
|
|
|
|
}
|
2016-06-18 23:18:29 -07:00
|
|
|
|
|
2014-02-13 09:38:43 -07:00
|
|
|
|
if (isHeadRequest)
|
|
|
|
|
{
|
2014-10-11 18:46:02 -07:00
|
|
|
|
using (response)
|
2013-05-17 11:05:49 -07:00
|
|
|
|
{
|
2014-02-13 21:00:13 -07:00
|
|
|
|
return ResultFactory.GetResult(new byte[] { }, response.ContentType, responseHeaders);
|
2013-05-17 11:05:49 -07:00
|
|
|
|
}
|
2014-02-13 09:38:43 -07:00
|
|
|
|
}
|
2013-05-17 11:05:49 -07:00
|
|
|
|
|
2014-02-13 09:38:43 -07:00
|
|
|
|
var result = new StaticRemoteStreamWriter(response);
|
2013-05-17 11:05:49 -07:00
|
|
|
|
|
2014-02-13 09:38:43 -07:00
|
|
|
|
result.Options["Content-Type"] = response.ContentType;
|
2013-05-17 11:05:49 -07:00
|
|
|
|
|
2014-02-13 09:38:43 -07:00
|
|
|
|
// Add the response headers to the result object
|
|
|
|
|
foreach (var header in responseHeaders)
|
|
|
|
|
{
|
|
|
|
|
result.Options[header.Key] = header.Value;
|
2013-05-17 11:05:49 -07:00
|
|
|
|
}
|
2014-02-13 09:38:43 -07:00
|
|
|
|
|
|
|
|
|
return result;
|
2013-05-17 11:05:49 -07:00
|
|
|
|
}
|
|
|
|
|
|
2013-02-26 21:19:05 -07:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the stream result.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="state">The state.</param>
|
2013-03-23 19:45:00 -07:00
|
|
|
|
/// <param name="responseHeaders">The response headers.</param>
|
2013-03-12 20:57:54 -07:00
|
|
|
|
/// <param name="isHeadRequest">if set to <c>true</c> [is head request].</param>
|
2014-08-16 22:38:13 -07:00
|
|
|
|
/// <param name="cancellationTokenSource">The cancellation token source.</param>
|
2013-02-26 21:19:05 -07:00
|
|
|
|
/// <returns>Task{System.Object}.</returns>
|
2014-08-16 22:38:13 -07:00
|
|
|
|
private async Task<object> GetStreamResult(StreamState state, IDictionary<string, string> responseHeaders, bool isHeadRequest, CancellationTokenSource cancellationTokenSource)
|
2013-02-26 21:19:05 -07:00
|
|
|
|
{
|
|
|
|
|
// Use the command line args with a dummy playlist path
|
2014-06-05 17:39:02 -07:00
|
|
|
|
var outputPath = state.OutputFilePath;
|
2013-02-26 21:19:05 -07:00
|
|
|
|
|
2014-02-18 22:21:03 -07:00
|
|
|
|
responseHeaders["Accept-Ranges"] = "none";
|
|
|
|
|
|
2014-03-12 12:56:12 -07:00
|
|
|
|
var contentType = state.GetMimeType(outputPath);
|
2013-02-26 21:44:41 -07:00
|
|
|
|
|
2015-05-08 20:48:43 -07:00
|
|
|
|
// TODO: The isHeadRequest is only here because ServiceStack will add Content-Length=0 to the response
|
|
|
|
|
// What we really want to do is hunt that down and remove that
|
|
|
|
|
var contentLength = state.EstimateContentLength || isHeadRequest ? GetEstimatedContentLength(state) : null;
|
2014-03-26 21:02:45 -07:00
|
|
|
|
|
|
|
|
|
if (contentLength.HasValue)
|
|
|
|
|
{
|
|
|
|
|
responseHeaders["Content-Length"] = contentLength.Value.ToString(UsCulture);
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-12 20:57:54 -07:00
|
|
|
|
// Headers only
|
|
|
|
|
if (isHeadRequest)
|
|
|
|
|
{
|
2014-02-18 22:25:00 -07:00
|
|
|
|
var streamResult = ResultFactory.GetResult(new byte[] { }, contentType, responseHeaders);
|
2014-03-26 21:02:45 -07:00
|
|
|
|
|
2015-05-08 20:48:43 -07:00
|
|
|
|
var hasOptions = streamResult as IHasOptions;
|
|
|
|
|
if (hasOptions != null)
|
2014-02-18 22:25:00 -07:00
|
|
|
|
{
|
2015-05-08 20:48:43 -07:00
|
|
|
|
if (contentLength.HasValue)
|
|
|
|
|
{
|
|
|
|
|
hasOptions.Options["Content-Length"] = contentLength.Value.ToString(CultureInfo.InvariantCulture);
|
|
|
|
|
}
|
|
|
|
|
else
|
2014-02-18 22:25:00 -07:00
|
|
|
|
{
|
2014-03-26 21:02:45 -07:00
|
|
|
|
if (hasOptions.Options.ContainsKey("Content-Length"))
|
|
|
|
|
{
|
|
|
|
|
hasOptions.Options.Remove("Content-Length");
|
|
|
|
|
}
|
2014-02-18 22:25:00 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
2014-03-26 21:02:45 -07:00
|
|
|
|
|
2014-02-18 22:25:00 -07:00
|
|
|
|
return streamResult;
|
2013-03-12 20:57:54 -07:00
|
|
|
|
}
|
|
|
|
|
|
2014-08-16 22:38:13 -07:00
|
|
|
|
await ApiEntryPoint.Instance.TranscodingStartLock.WaitAsync(cancellationTokenSource.Token).ConfigureAwait(false);
|
|
|
|
|
try
|
2013-02-26 21:19:05 -07:00
|
|
|
|
{
|
2014-09-02 19:30:24 -07:00
|
|
|
|
TranscodingJob job;
|
|
|
|
|
|
2016-06-18 23:18:29 -07:00
|
|
|
|
if (!FileSystem.FileExists(outputPath))
|
2014-08-16 22:38:13 -07:00
|
|
|
|
{
|
2014-09-02 19:30:24 -07:00
|
|
|
|
job = await StartFfMpeg(state, outputPath, cancellationTokenSource).ConfigureAwait(false);
|
2014-08-16 22:38:13 -07:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2014-09-02 19:30:24 -07:00
|
|
|
|
job = ApiEntryPoint.Instance.OnTranscodeBeginRequest(outputPath, TranscodingJobType.Progressive);
|
2014-08-16 22:38:13 -07:00
|
|
|
|
state.Dispose();
|
|
|
|
|
}
|
2013-02-26 21:19:05 -07:00
|
|
|
|
|
2016-07-15 10:13:55 -07:00
|
|
|
|
var outputHeaders = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
|
2013-03-23 19:45:00 -07:00
|
|
|
|
|
2016-07-15 10:13:55 -07:00
|
|
|
|
outputHeaders["Content-Type"] = contentType;
|
2013-03-23 19:45:00 -07:00
|
|
|
|
|
2014-08-16 22:38:13 -07:00
|
|
|
|
// Add the response headers to the result object
|
|
|
|
|
foreach (var item in responseHeaders)
|
|
|
|
|
{
|
2016-07-15 10:13:55 -07:00
|
|
|
|
outputHeaders[item.Key] = item.Value;
|
2014-08-16 22:38:13 -07:00
|
|
|
|
}
|
|
|
|
|
|
2016-08-06 07:08:38 -07:00
|
|
|
|
Func<Stream, Task> streamWriter = stream => new ProgressiveFileCopier(FileSystem, job, Logger).StreamFile(outputPath, stream, CancellationToken.None);
|
2016-07-15 10:13:55 -07:00
|
|
|
|
|
|
|
|
|
return ResultFactory.GetAsyncStreamWriter(streamWriter, outputHeaders);
|
2014-08-16 22:38:13 -07:00
|
|
|
|
}
|
|
|
|
|
finally
|
2013-03-23 19:45:00 -07:00
|
|
|
|
{
|
2014-08-16 22:38:13 -07:00
|
|
|
|
ApiEntryPoint.Instance.TranscodingStartLock.Release();
|
2013-03-23 19:45:00 -07:00
|
|
|
|
}
|
2013-02-26 21:19:05 -07:00
|
|
|
|
}
|
2014-03-26 21:02:45 -07:00
|
|
|
|
|
2014-03-27 12:30:21 -07:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the length of the estimated content.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="state">The state.</param>
|
|
|
|
|
/// <returns>System.Nullable{System.Int64}.</returns>
|
2014-03-26 21:02:45 -07:00
|
|
|
|
private long? GetEstimatedContentLength(StreamState state)
|
|
|
|
|
{
|
2014-04-22 17:50:47 -07:00
|
|
|
|
var totalBitrate = state.TotalOutputBitrate ?? 0;
|
2014-03-26 21:02:45 -07:00
|
|
|
|
|
|
|
|
|
if (totalBitrate > 0 && state.RunTimeTicks.HasValue)
|
|
|
|
|
{
|
|
|
|
|
return Convert.ToInt64(totalBitrate * TimeSpan.FromTicks(state.RunTimeTicks.Value).TotalSeconds);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2013-02-26 21:19:05 -07:00
|
|
|
|
}
|
|
|
|
|
}
|