using MediaBrowser.Api.Images;
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;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using MediaBrowser.Model.Entities;
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;
}
///
/// Adds the dlna headers.
///
/// The state.
/// The response headers.
/// true if XXXX, false otherwise
private void AddDlnaHeaders(StreamState state, IDictionary responseHeaders)
{
var timeSeek = RequestContext.GetHeader("TimeSeekRange.dlna.org");
if (!string.IsNullOrEmpty(timeSeek))
{
ResultFactory.ThrowError(406, "Time seek not supported during encoding.", responseHeaders);
return;
}
var transferMode = RequestContext.GetHeader("transferMode.dlna.org");
responseHeaders["transferMode.dlna.org"] = string.IsNullOrEmpty(transferMode) ? "Streaming" : transferMode;
var contentFeatures = string.Empty;
var extension = GetOutputFileExtension(state);
if (string.Equals(extension, ".mp3", StringComparison.OrdinalIgnoreCase))
{
contentFeatures = "DLNA.ORG_PN=MP3;DLNA.ORG_OP=01;DLNA.ORG_CI=0;DLNA.ORG_FLAGS=01500000000000000000000000000000";
}
else if (string.Equals(extension, ".aac", StringComparison.OrdinalIgnoreCase))
{
contentFeatures = "DLNA.ORG_PN=AAC_ISO;DLNA.ORG_OP=01;DLNA.ORG_CI=0;DLNA.ORG_FLAGS=01500000000000000000000000000000";
}
else if (string.Equals(extension, ".wma", StringComparison.OrdinalIgnoreCase))
{
contentFeatures = "DLNA.ORG_PN=WMABASE;DLNA.ORG_OP=01;DLNA.ORG_CI=0;DLNA.ORG_FLAGS=01500000000000000000000000000000";
}
else if (string.Equals(extension, ".avi", StringComparison.OrdinalIgnoreCase))
{
contentFeatures = "DLNA.ORG_PN=AVI;DLNA.ORG_OP=01;DLNA.ORG_CI=0;DLNA.ORG_FLAGS=01500000000000000000000000000000";
}
else if (string.Equals(extension, ".mp4", StringComparison.OrdinalIgnoreCase))
{
contentFeatures = "DLNA.ORG_PN=MPEG4_P2_SP_AAC;DLNA.ORG_OP=01;DLNA.ORG_CI=0;DLNA.ORG_FLAGS=01500000000000000000000000000000";
}
else if (string.Equals(extension, ".mpeg", StringComparison.OrdinalIgnoreCase))
{
contentFeatures = "DLNA.ORG_PN=MPEG_PS_PAL;DLNA.ORG_OP=01;DLNA.ORG_CI=0;DLNA.ORG_FLAGS=01500000000000000000000000000000";
}
else if (string.Equals(extension, ".wmv", StringComparison.OrdinalIgnoreCase))
{
contentFeatures = "DLNA.ORG_PN=WMVHIGH_BASE;DLNA.ORG_OP=01;DLNA.ORG_CI=0;DLNA.ORG_FLAGS=01500000000000000000000000000000";
}
else if (string.Equals(extension, ".asf", StringComparison.OrdinalIgnoreCase))
{
contentFeatures = "DLNA.ORG_OP=01;DLNA.ORG_CI=0;DLNA.ORG_FLAGS=01500000000000000000000000000000";
}
else if (string.Equals(extension, ".mkv", StringComparison.OrdinalIgnoreCase))
{
contentFeatures = "DLNA.ORG_OP=01;DLNA.ORG_CI=0";
}
if (!string.IsNullOrEmpty(contentFeatures))
{
responseHeaders["ContentFeatures.DLNA.ORG"] = contentFeatures;
}
}
///
/// 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.
/// if set to true [is head request].
/// Task.
protected object ProcessRequest(StreamRequest request, bool isHeadRequest)
{
var state = GetState(request);
if (request.AlbumArt)
{
return GetAlbumArtResponse(state);
}
var responseHeaders = new Dictionary();
AddDlnaHeaders(state, responseHeaders);
if (request.Static)
{
return ResultFactory.GetStaticFileResult(RequestContext, state.Item.Path, responseHeaders, isHeadRequest);
}
var outputPath = GetOutputFilePath(state);
if (File.Exists(outputPath) && !ApiEntryPoint.Instance.HasActiveTranscodingJob(outputPath, TranscodingJobType.Progressive))
{
return ResultFactory.GetStaticFileResult(RequestContext, outputPath, responseHeaders, isHeadRequest);
}
return GetStreamResult(state, responseHeaders, isHeadRequest).Result;
}
///
/// Gets the album art response.
///
/// The state.
/// System.Object.
private object GetAlbumArtResponse(StreamState state)
{
var request = new GetItemImage
{
MaxWidth = 800,
MaxHeight = 800,
Type = ImageType.Primary,
Id = state.Item.Id.ToString()
};
// Try and find some image to return
if (!state.Item.HasImage(ImageType.Primary))
{
if (state.Item.HasImage(ImageType.Backdrop))
{
request.Type = ImageType.Backdrop;
}
else if (state.Item.HasImage(ImageType.Thumb))
{
request.Type = ImageType.Thumb;
}
else if (state.Item.HasImage(ImageType.Logo))
{
request.Type = ImageType.Logo;
}
}
return new ImageService(UserManager, LibraryManager)
{
Logger = Logger,
RequestContext = RequestContext,
ResultFactory = ResultFactory
}.Get(request);
}
///
/// Gets the stream result.
///
/// The state.
/// The response headers.
/// if set to true [is head request].
/// Task{System.Object}.
private async Task