/// <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> /// <returns>Task{System.Object}.</returns> private async Task <object> GetStreamResult(StreamState state, IDictionary <string, string> responseHeaders, bool isHeadRequest) { // Use the command line args with a dummy playlist path var outputPath = state.OutputFilePath; responseHeaders["Accept-Ranges"] = "none"; var contentType = state.GetMimeType(outputPath); var contentLength = state.EstimateContentLength ? GetEstimatedContentLength(state) : null; if (contentLength.HasValue) { responseHeaders["Content-Length"] = contentLength.Value.ToString(UsCulture); } // Headers only if (isHeadRequest) { var streamResult = ResultFactory.GetResult(new byte[] { }, contentType, responseHeaders); if (!contentLength.HasValue) { var hasOptions = streamResult as IHasOptions; if (hasOptions != null) { if (hasOptions.Options.ContainsKey("Content-Length")) { hasOptions.Options.Remove("Content-Length"); } } } return(streamResult); } if (!File.Exists(outputPath)) { await StartFfMpeg(state, outputPath, new CancellationTokenSource()).ConfigureAwait(false); } else { ApiEntryPoint.Instance.OnTranscodeBeginRequest(outputPath, TranscodingJobType.Progressive); state.Dispose(); } var job = ApiEntryPoint.Instance.GetTranscodingJob(outputPath, TranscodingJobType.Progressive); var result = new ProgressiveStreamWriter(outputPath, Logger, FileSystem, job); result.Options["Content-Type"] = contentType; // Add the response headers to the result object foreach (var item in responseHeaders) { result.Options[item.Key] = item.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> /// <returns>Task{System.Object}.</returns> private async Task <object> GetStreamResult(StreamState state, IDictionary <string, string> responseHeaders, bool isHeadRequest) { // Use the command line args with a dummy playlist path var outputPath = GetOutputFilePath(state); var contentType = MimeTypes.GetMimeType(outputPath); // Headers only if (isHeadRequest) { responseHeaders["Accept-Ranges"] = "none"; return(ResultFactory.GetResult(null, contentType, responseHeaders)); } if (!File.Exists(outputPath)) { await StartFfMpeg(state, outputPath).ConfigureAwait(false); } else { ApiEntryPoint.Instance.OnTranscodeBeginRequest(outputPath, TranscodingJobType.Progressive); } var result = new ProgressiveStreamWriter(outputPath, Logger, FileSystem); result.Options["Accept-Ranges"] = "none"; result.Options["Content-Type"] = contentType; // Add the response headers to the result object foreach (var item in responseHeaders) { result.Options[item.Key] = item.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(StreamState state, IDictionary<string, string> responseHeaders, bool isHeadRequest, CancellationTokenSource cancellationTokenSource) { // Use the command line args with a dummy playlist path var outputPath = state.OutputFilePath; responseHeaders["Accept-Ranges"] = "none"; var contentType = state.GetMimeType(outputPath); // 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; if (contentLength.HasValue) { responseHeaders["Content-Length"] = contentLength.Value.ToString(UsCulture); } // Headers only if (isHeadRequest) { var streamResult = ResultFactory.GetResult(new byte[] { }, contentType, responseHeaders); var hasOptions = streamResult as IHasOptions; if (hasOptions != null) { if (contentLength.HasValue) { hasOptions.Options["Content-Length"] = contentLength.Value.ToString(CultureInfo.InvariantCulture); } else { if (hasOptions.Options.ContainsKey("Content-Length")) { hasOptions.Options.Remove("Content-Length"); } } } return streamResult; } await ApiEntryPoint.Instance.TranscodingStartLock.WaitAsync(cancellationTokenSource.Token).ConfigureAwait(false); try { TranscodingJob job; if (!File.Exists(outputPath)) { job = await StartFfMpeg(state, outputPath, cancellationTokenSource).ConfigureAwait(false); } else { job = ApiEntryPoint.Instance.OnTranscodeBeginRequest(outputPath, TranscodingJobType.Progressive); state.Dispose(); } var result = new ProgressiveStreamWriter(outputPath, Logger, FileSystem, job); result.Options["Content-Type"] = contentType; // Add the response headers to the result object foreach (var item in responseHeaders) { result.Options[item.Key] = item.Value; } return result; } finally { ApiEntryPoint.Instance.TranscodingStartLock.Release(); } }
/// <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(StreamState state, IDictionary <string, string> responseHeaders, bool isHeadRequest, CancellationTokenSource cancellationTokenSource) { // Use the command line args with a dummy playlist path var outputPath = state.OutputFilePath; responseHeaders["Accept-Ranges"] = "none"; var contentType = state.GetMimeType(outputPath); // 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; if (contentLength.HasValue) { responseHeaders["Content-Length"] = contentLength.Value.ToString(UsCulture); } // Headers only if (isHeadRequest) { var streamResult = ResultFactory.GetResult(new byte[] { }, contentType, responseHeaders); var hasOptions = streamResult as IHasOptions; if (hasOptions != null) { if (contentLength.HasValue) { hasOptions.Options["Content-Length"] = contentLength.Value.ToString(CultureInfo.InvariantCulture); } else { if (hasOptions.Options.ContainsKey("Content-Length")) { hasOptions.Options.Remove("Content-Length"); } } } return(streamResult); } await ApiEntryPoint.Instance.TranscodingStartLock.WaitAsync(cancellationTokenSource.Token).ConfigureAwait(false); try { TranscodingJob job; if (!FileSystem.FileExists(outputPath)) { job = await StartFfMpeg(state, outputPath, cancellationTokenSource).ConfigureAwait(false); } else { job = ApiEntryPoint.Instance.OnTranscodeBeginRequest(outputPath, TranscodingJobType.Progressive); state.Dispose(); } var result = new ProgressiveStreamWriter(outputPath, Logger, FileSystem, job); result.Options["Content-Type"] = contentType; // Add the response headers to the result object foreach (var item in responseHeaders) { result.Options[item.Key] = item.Value; } return(result); } finally { ApiEntryPoint.Instance.TranscodingStartLock.Release(); } }
/// <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> /// <returns>Task{System.Object}.</returns> private async Task<object> GetStreamResult(StreamState state, IDictionary<string, string> responseHeaders, bool isHeadRequest) { // Use the command line args with a dummy playlist path var outputPath = GetOutputFilePath(state); responseHeaders["Accept-Ranges"] = "none"; var contentType = MimeTypes.GetMimeType(outputPath); // Headers only if (isHeadRequest) { var streamResult = ResultFactory.GetResult(new byte[] { }, contentType, responseHeaders); var hasOptions = streamResult as IHasOptions; if (hasOptions != null) { if (hasOptions.Options.ContainsKey("Content-Length")) { hasOptions.Options.Remove("Content-Length"); } } return streamResult; } if (!File.Exists(outputPath)) { await StartFfMpeg(state, outputPath).ConfigureAwait(false); } else { ApiEntryPoint.Instance.OnTranscodeBeginRequest(outputPath, TranscodingJobType.Progressive); } var result = new ProgressiveStreamWriter(outputPath, Logger, FileSystem); result.Options["Content-Type"] = contentType; // Add the response headers to the result object foreach (var item in responseHeaders) { result.Options[item.Key] = item.Value; } return result; }