2017-02-17 21:11:13 +00:00
using System ;
using System.Collections.Generic ;
2017-05-18 21:05:47 +00:00
using System.Globalization ;
2017-06-24 18:33:19 +00:00
using System.Linq ;
2017-02-17 21:11:13 +00:00
using System.Threading.Tasks ;
using MediaBrowser.Api.Playback.Hls ;
using MediaBrowser.Api.Playback.Progressive ;
using MediaBrowser.Common.Net ;
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.Dlna ;
using MediaBrowser.Model.IO ;
using MediaBrowser.Model.MediaInfo ;
using MediaBrowser.Model.Serialization ;
using MediaBrowser.Model.Services ;
2017-05-22 04:54:02 +00:00
using MediaBrowser.Model.System ;
2017-02-17 21:11:13 +00:00
namespace MediaBrowser.Api.Playback
{
public class BaseUniversalRequest
{
/// <summary>
/// Gets or sets the id.
/// </summary>
/// <value>The id.</value>
[ApiMember(Name = "Id", Description = "Item Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
public string Id { get ; set ; }
[ApiMember(Name = "MediaSourceId", Description = "The media version id, if playing an alternate version", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
public string MediaSourceId { get ; set ; }
[ApiMember(Name = "DeviceId", Description = "The device id of the client requesting. Used to stop encoding processes when needed.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")]
public string DeviceId { get ; set ; }
public string UserId { get ; set ; }
public string AudioCodec { get ; set ; }
public string Container { get ; set ; }
public int? MaxAudioChannels { get ; set ; }
2017-05-31 19:21:32 +00:00
public int? TranscodingAudioChannels { get ; set ; }
2017-02-17 21:11:13 +00:00
public long? MaxStreamingBitrate { get ; set ; }
[ApiMember(Name = "StartTimeTicks", Description = "Optional. Specify a starting offset, in ticks. 1 tick = 10000 ms", IsRequired = false, DataType = "int", ParameterType = "query", Verb = "GET")]
public long? StartTimeTicks { get ; set ; }
2017-05-18 21:05:47 +00:00
public string TranscodingContainer { get ; set ; }
public string TranscodingProtocol { get ; set ; }
public int? MaxAudioSampleRate { get ; set ; }
2017-06-26 15:10:52 +00:00
public int? MaxAudioBitDepth { get ; set ; }
2017-05-31 19:21:32 +00:00
public bool EnableRedirection { get ; set ; }
public bool EnableRemoteMedia { get ; set ; }
public bool BreakOnNonKeyFrames { get ; set ; }
public BaseUniversalRequest ( )
{
EnableRedirection = true ;
}
2017-02-17 21:11:13 +00:00
}
[Route("/Audio/{Id}/universal.{Container}", "GET", Summary = "Gets an audio stream")]
[Route("/Audio/{Id}/universal", "GET", Summary = "Gets an audio stream")]
[Route("/Audio/{Id}/universal.{Container}", "HEAD", Summary = "Gets an audio stream")]
[Route("/Audio/{Id}/universal", "HEAD", Summary = "Gets an audio stream")]
public class GetUniversalAudioStream : BaseUniversalRequest
{
}
2017-05-27 11:42:25 +00:00
[Authenticated]
2017-02-17 21:11:13 +00:00
public class UniversalAudioService : BaseApiService
{
2017-05-22 04:54:02 +00:00
public UniversalAudioService ( IServerConfigurationManager serverConfigurationManager , IUserManager userManager , ILibraryManager libraryManager , IIsoManager isoManager , IMediaEncoder mediaEncoder , IFileSystem fileSystem , IDlnaManager dlnaManager , IDeviceManager deviceManager , ISubtitleEncoder subtitleEncoder , IMediaSourceManager mediaSourceManager , IZipClient zipClient , IJsonSerializer jsonSerializer , IAuthorizationContext authorizationContext , IImageProcessor imageProcessor , INetworkManager networkManager , IEnvironmentInfo environmentInfo )
2017-02-17 21:11:13 +00:00
{
ServerConfigurationManager = serverConfigurationManager ;
UserManager = userManager ;
LibraryManager = libraryManager ;
IsoManager = isoManager ;
MediaEncoder = mediaEncoder ;
FileSystem = fileSystem ;
DlnaManager = dlnaManager ;
DeviceManager = deviceManager ;
SubtitleEncoder = subtitleEncoder ;
MediaSourceManager = mediaSourceManager ;
ZipClient = zipClient ;
JsonSerializer = jsonSerializer ;
AuthorizationContext = authorizationContext ;
ImageProcessor = imageProcessor ;
NetworkManager = networkManager ;
2017-05-22 04:54:02 +00:00
EnvironmentInfo = environmentInfo ;
2017-02-17 21:11:13 +00:00
}
protected IServerConfigurationManager ServerConfigurationManager { get ; private set ; }
protected IUserManager UserManager { get ; private set ; }
protected ILibraryManager LibraryManager { get ; private set ; }
protected IIsoManager IsoManager { get ; private set ; }
protected IMediaEncoder MediaEncoder { get ; private set ; }
protected IFileSystem FileSystem { get ; private set ; }
protected IDlnaManager DlnaManager { get ; private set ; }
protected IDeviceManager DeviceManager { get ; private set ; }
protected ISubtitleEncoder SubtitleEncoder { get ; private set ; }
protected IMediaSourceManager MediaSourceManager { get ; private set ; }
protected IZipClient ZipClient { get ; private set ; }
protected IJsonSerializer JsonSerializer { get ; private set ; }
protected IAuthorizationContext AuthorizationContext { get ; private set ; }
protected IImageProcessor ImageProcessor { get ; private set ; }
protected INetworkManager NetworkManager { get ; private set ; }
2017-05-22 04:54:02 +00:00
protected IEnvironmentInfo EnvironmentInfo { get ; private set ; }
2017-02-17 21:11:13 +00:00
public Task < object > Get ( GetUniversalAudioStream request )
{
return GetUniversalStream ( request , false ) ;
}
public Task < object > Head ( GetUniversalAudioStream request )
{
return GetUniversalStream ( request , true ) ;
}
private DeviceProfile GetDeviceProfile ( GetUniversalAudioStream request )
{
var deviceProfile = new DeviceProfile ( ) ;
var directPlayProfiles = new List < DirectPlayProfile > ( ) ;
2017-08-03 16:26:01 +00:00
var containers = ( request . Container ? ? string . Empty ) . Split ( new [ ] { ',' } , StringSplitOptions . RemoveEmptyEntries ) ;
foreach ( var container in containers )
2017-02-17 21:11:13 +00:00
{
2017-08-03 16:26:01 +00:00
var parts = container . Split ( new [ ] { '|' } , StringSplitOptions . RemoveEmptyEntries ) ;
var audioCodecs = parts . Length = = 1 ? null : string . Join ( "," , parts . Skip ( 1 ) . ToArray ( ) ) ;
directPlayProfiles . Add ( new DirectPlayProfile
{
Type = DlnaProfileType . Audio ,
Container = parts [ 0 ] ,
AudioCodec = audioCodecs
} ) ;
}
2017-02-17 21:11:13 +00:00
deviceProfile . DirectPlayProfiles = directPlayProfiles . ToArray ( ) ;
deviceProfile . TranscodingProfiles = new [ ]
{
new TranscodingProfile
{
Type = DlnaProfileType . Audio ,
Context = EncodingContext . Streaming ,
2017-05-18 21:05:47 +00:00
Container = request . TranscodingContainer ,
AudioCodec = request . AudioCodec ,
2017-05-31 19:21:32 +00:00
Protocol = request . TranscodingProtocol ,
BreakOnNonKeyFrames = request . BreakOnNonKeyFrames ,
MaxAudioChannels = request . TranscodingAudioChannels . HasValue ? request . TranscodingAudioChannels . Value . ToString ( CultureInfo . InvariantCulture ) : null
2017-02-17 21:11:13 +00:00
}
} ;
2017-05-18 21:05:47 +00:00
var codecProfiles = new List < CodecProfile > ( ) ;
var conditions = new List < ProfileCondition > ( ) ;
if ( request . MaxAudioSampleRate . HasValue )
{
// codec profile
conditions . Add ( new ProfileCondition
{
Condition = ProfileConditionType . LessThanEqual ,
IsRequired = false ,
Property = ProfileConditionValue . AudioSampleRate ,
Value = request . MaxAudioSampleRate . Value . ToString ( CultureInfo . InvariantCulture )
} ) ;
}
2017-06-26 15:10:52 +00:00
if ( request . MaxAudioBitDepth . HasValue )
{
// codec profile
conditions . Add ( new ProfileCondition
{
Condition = ProfileConditionType . LessThanEqual ,
IsRequired = false ,
Property = ProfileConditionValue . AudioBitDepth ,
Value = request . MaxAudioBitDepth . Value . ToString ( CultureInfo . InvariantCulture )
} ) ;
}
2017-05-18 21:05:47 +00:00
if ( request . MaxAudioChannels . HasValue )
{
// codec profile
conditions . Add ( new ProfileCondition
{
Condition = ProfileConditionType . LessThanEqual ,
IsRequired = false ,
Property = ProfileConditionValue . AudioChannels ,
Value = request . MaxAudioChannels . Value . ToString ( CultureInfo . InvariantCulture )
} ) ;
}
if ( conditions . Count > 0 )
{
// codec profile
codecProfiles . Add ( new CodecProfile
{
Type = CodecType . Audio ,
Container = request . Container ,
Conditions = conditions . ToArray ( )
} ) ;
}
deviceProfile . CodecProfiles = codecProfiles . ToArray ( ) ;
2017-02-17 21:11:13 +00:00
return deviceProfile ;
}
private async Task < object > GetUniversalStream ( GetUniversalAudioStream request , bool isHeadRequest )
{
var deviceProfile = GetDeviceProfile ( request ) ;
AuthorizationContext . GetAuthorizationInfo ( Request ) . DeviceId = request . DeviceId ;
var mediaInfoService = new MediaInfoService ( MediaSourceManager , DeviceManager , LibraryManager , ServerConfigurationManager , NetworkManager , MediaEncoder , UserManager , JsonSerializer , AuthorizationContext )
{
Request = Request
} ;
var playbackInfoResult = await mediaInfoService . GetPlaybackInfo ( new GetPostedPlaybackInfo
{
Id = request . Id ,
MaxAudioChannels = request . MaxAudioChannels ,
MaxStreamingBitrate = request . MaxStreamingBitrate ,
StartTimeTicks = request . StartTimeTicks ,
UserId = request . UserId ,
DeviceProfile = deviceProfile ,
MediaSourceId = request . MediaSourceId
} ) . ConfigureAwait ( false ) ;
var mediaSource = playbackInfoResult . MediaSources [ 0 ] ;
2017-05-31 19:21:32 +00:00
if ( mediaSource . SupportsDirectPlay & & mediaSource . Protocol = = MediaProtocol . Http )
{
if ( request . EnableRedirection )
{
if ( mediaSource . IsRemote & & request . EnableRemoteMedia )
{
return ResultFactory . GetRedirectResult ( mediaSource . Path ) ;
}
}
}
2017-02-17 21:11:13 +00:00
var isStatic = mediaSource . SupportsDirectStream ;
if ( ! isStatic & & string . Equals ( mediaSource . TranscodingSubProtocol , "hls" , StringComparison . OrdinalIgnoreCase ) )
{
var service = new DynamicHlsService ( ServerConfigurationManager ,
UserManager ,
LibraryManager ,
IsoManager ,
MediaEncoder ,
FileSystem ,
DlnaManager ,
SubtitleEncoder ,
DeviceManager ,
MediaSourceManager ,
ZipClient ,
JsonSerializer ,
AuthorizationContext ,
NetworkManager )
{
Request = Request
} ;
var transcodingProfile = deviceProfile . TranscodingProfiles [ 0 ] ;
var newRequest = new GetMasterHlsAudioPlaylist
{
AudioBitRate = isStatic ? ( int? ) null : Convert . ToInt32 ( Math . Min ( request . MaxStreamingBitrate ? ? 192000 , int . MaxValue ) ) ,
AudioCodec = transcodingProfile . AudioCodec ,
Container = ".m3u8" ,
DeviceId = request . DeviceId ,
Id = request . Id ,
MaxAudioChannels = request . MaxAudioChannels ,
MediaSourceId = mediaSource . Id ,
PlaySessionId = playbackInfoResult . PlaySessionId ,
StartTimeTicks = request . StartTimeTicks ,
2017-05-18 21:05:47 +00:00
Static = isStatic ,
SegmentContainer = request . TranscodingContainer ,
2017-05-31 19:21:32 +00:00
AudioSampleRate = request . MaxAudioSampleRate ,
2017-06-26 15:10:52 +00:00
MaxAudioBitDepth = request . MaxAudioBitDepth ,
2017-06-24 18:33:19 +00:00
BreakOnNonKeyFrames = transcodingProfile . BreakOnNonKeyFrames ,
TranscodeReasons = mediaSource . TranscodeReasons = = null ? null : string . Join ( "," , mediaSource . TranscodeReasons . Select ( i = > i . ToString ( ) ) . ToArray ( ) )
2017-02-17 21:11:13 +00:00
} ;
if ( isHeadRequest )
{
2017-02-19 03:46:09 +00:00
return await service . Head ( newRequest ) . ConfigureAwait ( false ) ;
2017-02-17 21:11:13 +00:00
}
2017-02-19 03:46:09 +00:00
return await service . Get ( newRequest ) . ConfigureAwait ( false ) ;
2017-02-17 21:11:13 +00:00
}
else
{
var service = new AudioService ( ServerConfigurationManager ,
UserManager ,
LibraryManager ,
IsoManager ,
MediaEncoder ,
FileSystem ,
DlnaManager ,
SubtitleEncoder ,
DeviceManager ,
MediaSourceManager ,
ZipClient ,
JsonSerializer ,
AuthorizationContext ,
2017-05-22 04:54:02 +00:00
ImageProcessor ,
EnvironmentInfo )
2017-02-17 21:11:13 +00:00
{
Request = Request
} ;
var newRequest = new GetAudioStream
{
AudioBitRate = isStatic ? ( int? ) null : Convert . ToInt32 ( Math . Min ( request . MaxStreamingBitrate ? ? 192000 , int . MaxValue ) ) ,
2017-05-18 21:05:47 +00:00
AudioCodec = request . AudioCodec ,
2017-02-17 21:11:13 +00:00
Container = isStatic ? null : ( "." + mediaSource . TranscodingContainer ) ,
DeviceId = request . DeviceId ,
Id = request . Id ,
MaxAudioChannels = request . MaxAudioChannels ,
MediaSourceId = mediaSource . Id ,
PlaySessionId = playbackInfoResult . PlaySessionId ,
StartTimeTicks = request . StartTimeTicks ,
2017-05-18 21:05:47 +00:00
Static = isStatic ,
2017-06-24 18:33:19 +00:00
AudioSampleRate = request . MaxAudioSampleRate ,
2017-06-26 15:10:52 +00:00
MaxAudioBitDepth = request . MaxAudioBitDepth ,
2017-06-24 18:33:19 +00:00
TranscodeReasons = mediaSource . TranscodeReasons = = null ? null : string . Join ( "," , mediaSource . TranscodeReasons . Select ( i = > i . ToString ( ) ) . ToArray ( ) )
2017-02-17 21:11:13 +00:00
} ;
if ( isHeadRequest )
{
2017-02-19 03:46:09 +00:00
return await service . Head ( newRequest ) . ConfigureAwait ( false ) ;
2017-02-17 21:11:13 +00:00
}
2017-02-19 03:46:09 +00:00
return await service . Get ( newRequest ) . ConfigureAwait ( false ) ;
2017-02-17 21:11:13 +00:00
}
}
}
}