2020-04-05 16:10:56 +00:00
#nullable disable
2020-02-04 00:49:27 +00:00
#pragma warning disable CS1591
2019-01-13 20:02:23 +00:00
using System ;
2018-12-27 23:27:57 +00:00
using System.Collections.Generic ;
2019-09-08 19:07:29 +00:00
using System.Globalization ;
2019-01-13 19:26:15 +00:00
using MediaBrowser.Model.MediaInfo ;
2018-12-27 23:27:57 +00:00
namespace MediaBrowser.Model.Dlna
{
public class ContentFeatureBuilder
{
private readonly DeviceProfile _profile ;
public ContentFeatureBuilder ( DeviceProfile profile )
{
_profile = profile ;
}
2019-03-26 21:56:05 +00:00
public string BuildImageHeader (
string container ,
2018-12-27 23:27:57 +00:00
int? width ,
int? height ,
bool isDirectStream ,
string orgPn = null )
{
string orgOp = ";DLNA.ORG_OP=" + DlnaMaps . GetImageOrgOpValue ( ) ;
// 0 = native, 1 = transcoded
var orgCi = isDirectStream ? ";DLNA.ORG_CI=0" : ";DLNA.ORG_CI=1" ;
2019-01-13 20:37:13 +00:00
var flagValue = DlnaFlags . BackgroundTransferMode |
2018-12-27 23:27:57 +00:00
DlnaFlags . InteractiveTransferMode |
DlnaFlags . DlnaV15 ;
2018-12-27 21:43:48 +00:00
2020-04-05 16:10:56 +00:00
string dlnaflags = string . Format (
CultureInfo . InvariantCulture ,
";DLNA.ORG_FLAGS={0}" ,
DlnaMaps . FlagsToString ( flagValue ) ) ;
2018-12-27 23:27:57 +00:00
2020-10-12 18:05:11 +00:00
ResponseProfile mediaProfile = _profile . GetImageMediaProfile (
container ,
2018-12-27 23:27:57 +00:00
width ,
height ) ;
if ( string . IsNullOrEmpty ( orgPn ) )
{
2019-03-26 21:56:05 +00:00
orgPn = mediaProfile ? . OrgPn ;
2018-12-27 23:27:57 +00:00
}
if ( string . IsNullOrEmpty ( orgPn ) )
{
orgPn = GetImageOrgPnValue ( container , width , height ) ;
}
string contentFeatures = string . IsNullOrEmpty ( orgPn ) ? string . Empty : "DLNA.ORG_PN=" + orgPn ;
return ( contentFeatures + orgOp + orgCi + dlnaflags ) . Trim ( ';' ) ;
}
2019-03-26 21:56:05 +00:00
public string BuildAudioHeader (
string container ,
2018-12-27 23:27:57 +00:00
string audioCodec ,
int? audioBitrate ,
int? audioSampleRate ,
int? audioChannels ,
int? audioBitDepth ,
bool isDirectStream ,
long? runtimeTicks ,
TranscodeSeekInfo transcodeSeekInfo )
{
// first bit means Time based seek supported, second byte range seek supported (not sure about the order now), so 01 = only byte seek, 10 = time based, 11 = both, 00 = none
string orgOp = ";DLNA.ORG_OP=" + DlnaMaps . GetOrgOpValue ( runtimeTicks > 0 , isDirectStream , transcodeSeekInfo ) ;
// 0 = native, 1 = transcoded
string orgCi = isDirectStream ? ";DLNA.ORG_CI=0" : ";DLNA.ORG_CI=1" ;
2019-01-13 20:37:13 +00:00
var flagValue = DlnaFlags . StreamingTransferMode |
2018-12-27 23:27:57 +00:00
DlnaFlags . BackgroundTransferMode |
DlnaFlags . InteractiveTransferMode |
DlnaFlags . DlnaV15 ;
2020-06-14 09:11:11 +00:00
// if (isDirectStream)
2018-12-27 23:27:57 +00:00
//{
// flagValue = flagValue | DlnaFlags.ByteBasedSeek;
//}
2020-06-14 09:11:11 +00:00
// else if (runtimeTicks.HasValue)
2018-12-27 23:27:57 +00:00
//{
// flagValue = flagValue | DlnaFlags.TimeBasedSeek;
//}
2019-09-08 19:07:29 +00:00
string dlnaflags = string . Format (
CultureInfo . InvariantCulture ,
";DLNA.ORG_FLAGS={0}" ,
DlnaMaps . FlagsToString ( flagValue ) ) ;
2018-12-27 23:27:57 +00:00
2019-09-08 19:07:29 +00:00
ResponseProfile mediaProfile = _profile . GetAudioMediaProfile (
container ,
2018-12-27 23:27:57 +00:00
audioCodec ,
audioChannels ,
audioBitrate ,
audioSampleRate ,
audioBitDepth ) ;
2019-09-08 19:07:29 +00:00
string orgPn = mediaProfile ? . OrgPn ;
2018-12-27 23:27:57 +00:00
if ( string . IsNullOrEmpty ( orgPn ) )
{
orgPn = GetAudioOrgPnValue ( container , audioBitrate , audioSampleRate , audioChannels ) ;
}
string contentFeatures = string . IsNullOrEmpty ( orgPn ) ? string . Empty : "DLNA.ORG_PN=" + orgPn ;
return ( contentFeatures + orgOp + orgCi + dlnaflags ) . Trim ( ';' ) ;
}
2019-03-26 21:56:05 +00:00
public List < string > BuildVideoHeader (
string container ,
2018-12-27 23:27:57 +00:00
string videoCodec ,
string audioCodec ,
int? width ,
int? height ,
int? bitDepth ,
int? videoBitrate ,
TransportStreamTimestamp timestamp ,
bool isDirectStream ,
long? runtimeTicks ,
string videoProfile ,
double? videoLevel ,
float? videoFramerate ,
int? packetLength ,
TranscodeSeekInfo transcodeSeekInfo ,
bool? isAnamorphic ,
bool? isInterlaced ,
int? refFrames ,
int? numVideoStreams ,
int? numAudioStreams ,
string videoCodecTag ,
bool? isAvc )
{
// first bit means Time based seek supported, second byte range seek supported (not sure about the order now), so 01 = only byte seek, 10 = time based, 11 = both, 00 = none
string orgOp = ";DLNA.ORG_OP=" + DlnaMaps . GetOrgOpValue ( runtimeTicks > 0 , isDirectStream , transcodeSeekInfo ) ;
// 0 = native, 1 = transcoded
string orgCi = isDirectStream ? ";DLNA.ORG_CI=0" : ";DLNA.ORG_CI=1" ;
2019-01-13 20:37:13 +00:00
var flagValue = DlnaFlags . StreamingTransferMode |
2018-12-27 23:27:57 +00:00
DlnaFlags . BackgroundTransferMode |
DlnaFlags . InteractiveTransferMode |
DlnaFlags . DlnaV15 ;
2020-06-14 09:11:11 +00:00
// if (isDirectStream)
2018-12-27 23:27:57 +00:00
//{
// flagValue = flagValue | DlnaFlags.ByteBasedSeek;
//}
2020-06-14 09:11:11 +00:00
// else if (runtimeTicks.HasValue)
2018-12-27 23:27:57 +00:00
//{
// flagValue = flagValue | DlnaFlags.TimeBasedSeek;
//}
2020-08-07 17:26:28 +00:00
string dlnaflags = string . Format ( CultureInfo . InvariantCulture , ";DLNA.ORG_FLAGS={0}" ,
2018-12-27 23:27:57 +00:00
DlnaMaps . FlagsToString ( flagValue ) ) ;
2020-10-12 18:05:11 +00:00
ResponseProfile mediaProfile = _profile . GetVideoMediaProfile (
container ,
2018-12-27 23:27:57 +00:00
audioCodec ,
videoCodec ,
width ,
height ,
bitDepth ,
videoBitrate ,
videoProfile ,
videoLevel ,
videoFramerate ,
packetLength ,
timestamp ,
isAnamorphic ,
isInterlaced ,
refFrames ,
numVideoStreams ,
numAudioStreams ,
videoCodecTag ,
isAvc ) ;
2019-01-13 20:37:13 +00:00
var orgPnValues = new List < string > ( ) ;
2018-12-27 23:27:57 +00:00
if ( mediaProfile ! = null & & ! string . IsNullOrEmpty ( mediaProfile . OrgPn ) )
{
2020-11-14 15:28:49 +00:00
orgPnValues . AddRange ( mediaProfile . OrgPn . Split ( ',' , StringSplitOptions . RemoveEmptyEntries ) ) ;
2018-12-27 23:27:57 +00:00
}
else
{
foreach ( string s in GetVideoOrgPnValue ( container , videoCodec , audioCodec , width , height , timestamp ) )
{
orgPnValues . Add ( s ) ;
break ;
}
}
2019-01-13 20:37:13 +00:00
var contentFeatureList = new List < string > ( ) ;
2018-12-27 23:27:57 +00:00
foreach ( string orgPn in orgPnValues )
{
string contentFeatures = string . IsNullOrEmpty ( orgPn ) ? string . Empty : "DLNA.ORG_PN=" + orgPn ;
var value = ( contentFeatures + orgOp + orgCi + dlnaflags ) . Trim ( ';' ) ;
contentFeatureList . Add ( value ) ;
}
if ( orgPnValues . Count = = 0 )
{
string contentFeatures = string . Empty ;
var value = ( contentFeatures + orgOp + orgCi + dlnaflags ) . Trim ( ';' ) ;
contentFeatureList . Add ( value ) ;
}
return contentFeatureList ;
}
2019-03-26 21:56:05 +00:00
private static string GetImageOrgPnValue ( string container , int? width , int? height )
2018-12-27 23:27:57 +00:00
{
MediaFormatProfile ? format = new MediaFormatProfileResolver ( )
2020-10-12 18:05:11 +00:00
. ResolveImageFormat (
container ,
2018-12-27 23:27:57 +00:00
width ,
height ) ;
return format . HasValue ? format . Value . ToString ( ) : null ;
}
2019-03-26 21:56:05 +00:00
private static string GetAudioOrgPnValue ( string container , int? audioBitrate , int? audioSampleRate , int? audioChannels )
2018-12-27 23:27:57 +00:00
{
MediaFormatProfile ? format = new MediaFormatProfileResolver ( )
2020-10-12 18:05:11 +00:00
. ResolveAudioFormat (
container ,
2018-12-27 23:27:57 +00:00
audioBitrate ,
audioSampleRate ,
audioChannels ) ;
return format . HasValue ? format . Value . ToString ( ) : null ;
}
2019-03-26 21:56:05 +00:00
private static string [ ] GetVideoOrgPnValue ( string container , string videoCodec , string audioCodec , int? width , int? height , TransportStreamTimestamp timestamp )
2018-12-27 23:27:57 +00:00
{
return new MediaFormatProfileResolver ( ) . ResolveVideoFormat ( container , videoCodec , audioCodec , width , height , timestamp ) ;
}
}
}