using MediaBrowser.Common.IO;
using MediaBrowser.Common.Net;
using MediaBrowser.Controller;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.Audio;
using MediaBrowser.Controller.Library;
using MediaBrowser.Model.Dto;
using System.IO;
using System.Threading.Tasks;
namespace MediaBrowser.Api.Playback.Progressive
{
///
/// Class BaseProgressiveStreamingService
///
public abstract class BaseProgressiveStreamingService : BaseStreamingService
{
protected BaseProgressiveStreamingService(IServerApplicationPaths appPaths, IUserManager userManager, ILibraryManager libraryManager, IIsoManager isoManager) :
base(appPaths, userManager, libraryManager, isoManager)
{
}
///
/// Gets the output file extension.
///
/// The state.
/// System.String.
protected override string GetOutputFileExtension(StreamState state)
{
var ext = base.GetOutputFileExtension(state);
if (!string.IsNullOrEmpty(ext))
{
return ext;
}
var videoRequest = state.Request as VideoStreamRequest;
// Try to infer based on the desired video codec
if (videoRequest != null && videoRequest.VideoCodec.HasValue)
{
var video = state.Item as Video;
if (video != null)
{
switch (videoRequest.VideoCodec.Value)
{
case VideoCodecs.H264:
return ".ts";
case VideoCodecs.Theora:
return ".ogv";
case VideoCodecs.Vpx:
return ".webm";
case VideoCodecs.Wmv:
return ".asf";
}
}
}
// Try to infer based on the desired audio codec
if (state.Request.AudioCodec.HasValue)
{
var audio = state.Item as Audio;
if (audio != null)
{
switch (state.Request.AudioCodec.Value)
{
case AudioCodecs.Aac:
return ".aac";
case AudioCodecs.Mp3:
return ".mp3";
case AudioCodecs.Vorbis:
return ".ogg";
case AudioCodecs.Wma:
return ".wma";
}
}
}
return null;
}
///
/// Gets the type of the transcoding job.
///
/// The type of the transcoding job.
protected override TranscodingJobType TranscodingJobType
{
get { return TranscodingJobType.Progressive; }
}
///
/// Processes the request.
///
/// The request.
/// Task.
protected object ProcessRequest(StreamRequest request)
{
var state = GetState(request);
if (request.Static)
{
return ToStaticFileResult(state.Item.Path);
}
var outputPath = GetOutputFilePath(state);
if (File.Exists(outputPath) && !ApiEntryPoint.Instance.HasActiveTranscodingJob(outputPath, TranscodingJobType.Progressive))
{
return ToStaticFileResult(outputPath);
}
return GetStreamResult(state).Result;
}
///
/// Gets the stream result.
///
/// The state.
/// Task{System.Object}.
private async Task GetStreamResult(StreamState state)
{
// Use the command line args with a dummy playlist path
var outputPath = GetOutputFilePath(state);
Response.ContentType = MimeTypes.GetMimeType(outputPath);
if (!File.Exists(outputPath))
{
await StartFFMpeg(state, outputPath).ConfigureAwait(false);
}
else
{
ApiEntryPoint.Instance.OnTranscodeBeginRequest(outputPath, TranscodingJobType.Progressive);
}
return new ProgressiveStreamWriter
{
Path = outputPath,
State = state
};
}
///
/// Deletes the partial stream files.
///
/// The output file path.
protected override void DeletePartialStreamFiles(string outputFilePath)
{
File.Delete(outputFilePath);
}
}
}