2020-04-05 18:10:56 +02:00
#nullable disable
2020-02-04 01:49:27 +01:00
#pragma warning disable CS1591
2019-01-13 21:02:23 +01:00
using System ;
2018-12-28 00:27:57 +01:00
using System.Collections.Generic ;
2019-09-08 21:07:29 +02:00
using System.Globalization ;
2019-01-13 20:26:15 +01:00
using MediaBrowser.Model.MediaInfo ;
2018-12-28 00:27:57 +01:00
namespace MediaBrowser.Model.Dlna
{
public class ContentFeatureBuilder
{
2021-04-02 20:06:38 +02:00
public static string BuildImageHeader (
DeviceProfile profile ,
2019-03-26 22:56:05 +01:00
string container ,
2018-12-28 00:27:57 +01: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 21:37:13 +01:00
var flagValue = DlnaFlags . BackgroundTransferMode |
2018-12-28 00:27:57 +01:00
DlnaFlags . InteractiveTransferMode |
DlnaFlags . DlnaV15 ;
2018-12-27 22:43:48 +01:00
2020-04-05 18:10:56 +02:00
string dlnaflags = string . Format (
CultureInfo . InvariantCulture ,
";DLNA.ORG_FLAGS={0}" ,
DlnaMaps . FlagsToString ( flagValue ) ) ;
2018-12-28 00:27:57 +01:00
if ( string . IsNullOrEmpty ( orgPn ) )
{
2021-04-02 20:06:38 +02:00
ResponseProfile mediaProfile = profile . GetImageMediaProfile (
container ,
width ,
height ) ;
2019-03-26 22:56:05 +01:00
orgPn = mediaProfile ? . OrgPn ;
2021-04-02 20:06:38 +02:00
if ( string . IsNullOrEmpty ( orgPn ) )
{
orgPn = GetImageOrgPnValue ( container , width , height ) ;
}
2018-12-28 00:27:57 +01:00
}
if ( string . IsNullOrEmpty ( orgPn ) )
{
2021-04-02 20:06:38 +02:00
return orgOp . TrimStart ( ';' ) + orgCi + dlnaflags ;
2018-12-28 00:27:57 +01:00
}
2021-04-02 20:06:38 +02:00
return "DLNA.ORG_PN=" + orgPn + orgOp + orgCi + dlnaflags ;
2018-12-28 00:27:57 +01:00
}
2021-04-02 20:06:38 +02:00
public static string BuildAudioHeader (
DeviceProfile profile ,
2019-03-26 22:56:05 +01:00
string container ,
2018-12-28 00:27:57 +01: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 21:37:13 +01:00
var flagValue = DlnaFlags . StreamingTransferMode |
2018-12-28 00:27:57 +01:00
DlnaFlags . BackgroundTransferMode |
DlnaFlags . InteractiveTransferMode |
DlnaFlags . DlnaV15 ;
2020-06-14 11:11:11 +02:00
// if (isDirectStream)
2021-02-20 23:13:04 +01:00
// {
// flagValue = flagValue | DlnaFlags.ByteBasedSeek;
// }
// else if (runtimeTicks.HasValue)
// {
// flagValue = flagValue | DlnaFlags.TimeBasedSeek;
// }
2018-12-28 00:27:57 +01:00
2019-09-08 21:07:29 +02:00
string dlnaflags = string . Format (
CultureInfo . InvariantCulture ,
";DLNA.ORG_FLAGS={0}" ,
DlnaMaps . FlagsToString ( flagValue ) ) ;
2018-12-28 00:27:57 +01:00
2021-04-02 20:06:38 +02:00
ResponseProfile mediaProfile = profile . GetAudioMediaProfile (
2019-09-08 21:07:29 +02:00
container ,
2018-12-28 00:27:57 +01:00
audioCodec ,
audioChannels ,
audioBitrate ,
audioSampleRate ,
audioBitDepth ) ;
2019-09-08 21:07:29 +02:00
string orgPn = mediaProfile ? . OrgPn ;
2018-12-28 00:27:57 +01:00
if ( string . IsNullOrEmpty ( orgPn ) )
{
orgPn = GetAudioOrgPnValue ( container , audioBitrate , audioSampleRate , audioChannels ) ;
}
2021-04-02 20:06:38 +02:00
if ( string . IsNullOrEmpty ( orgPn ) )
{
return orgOp . TrimStart ( ';' ) + orgCi + dlnaflags ;
}
2018-12-28 00:27:57 +01:00
2021-04-02 20:06:38 +02:00
return "DLNA.ORG_PN=" + orgPn + orgOp + orgCi + dlnaflags ;
2018-12-28 00:27:57 +01:00
}
2022-01-22 15:40:05 +01:00
public static IEnumerable < string > BuildVideoHeader (
2021-04-02 20:06:38 +02:00
DeviceProfile profile ,
2019-03-26 22:56:05 +01:00
string container ,
2018-12-28 00:27:57 +01:00
string videoCodec ,
string audioCodec ,
int? width ,
int? height ,
int? bitDepth ,
int? videoBitrate ,
TransportStreamTimestamp timestamp ,
bool isDirectStream ,
long? runtimeTicks ,
string videoProfile ,
2022-06-16 15:32:54 +02:00
string videoRangeType ,
2018-12-28 00:27:57 +01:00
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 21:37:13 +01:00
var flagValue = DlnaFlags . StreamingTransferMode |
2018-12-28 00:27:57 +01:00
DlnaFlags . BackgroundTransferMode |
DlnaFlags . InteractiveTransferMode |
DlnaFlags . DlnaV15 ;
2021-11-25 18:42:43 +01:00
if ( isDirectStream )
{
flagValue | = DlnaFlags . ByteBasedSeek ;
}
// Time based seek is curently disabled when streaming. On LG CX3 adding DlnaFlags.TimeBasedSeek and orgPn causes the DLNA playback to fail (format not supported). Further investigations are needed before enabling the remaining code paths.
2021-02-20 23:13:04 +01:00
// else if (runtimeTicks.HasValue)
// {
// flagValue = flagValue | DlnaFlags.TimeBasedSeek;
// }
string dlnaflags = string . Format (
CultureInfo . InvariantCulture ,
";DLNA.ORG_FLAGS={0}" ,
DlnaMaps . FlagsToString ( flagValue ) ) ;
2018-12-28 00:27:57 +01:00
2021-04-02 20:06:38 +02:00
ResponseProfile mediaProfile = profile . GetVideoMediaProfile (
2020-10-12 20:05:11 +02:00
container ,
2018-12-28 00:27:57 +01:00
audioCodec ,
videoCodec ,
width ,
height ,
bitDepth ,
videoBitrate ,
videoProfile ,
2022-06-16 15:32:54 +02:00
videoRangeType ,
2018-12-28 00:27:57 +01:00
videoLevel ,
videoFramerate ,
packetLength ,
timestamp ,
isAnamorphic ,
isInterlaced ,
refFrames ,
numVideoStreams ,
numAudioStreams ,
videoCodecTag ,
isAvc ) ;
2019-01-13 21:37:13 +01:00
var orgPnValues = new List < string > ( ) ;
2018-12-28 00:27:57 +01:00
if ( mediaProfile ! = null & & ! string . IsNullOrEmpty ( mediaProfile . OrgPn ) )
{
2020-11-14 16:28:49 +01:00
orgPnValues . AddRange ( mediaProfile . OrgPn . Split ( ',' , StringSplitOptions . RemoveEmptyEntries ) ) ;
2018-12-28 00:27:57 +01:00
}
else
{
2021-04-02 20:06:38 +02:00
foreach ( var s in GetVideoOrgPnValue ( container , videoCodec , audioCodec , width , height , timestamp ) )
2018-12-28 00:27:57 +01:00
{
2021-04-02 20:06:38 +02:00
orgPnValues . Add ( s . ToString ( ) ) ;
2018-12-28 00:27:57 +01:00
break ;
}
}
2019-01-13 21:37:13 +01:00
var contentFeatureList = new List < string > ( ) ;
2018-12-28 00:27:57 +01:00
foreach ( string orgPn in orgPnValues )
{
2021-04-02 20:06:38 +02:00
if ( string . IsNullOrEmpty ( orgPn ) )
{
contentFeatureList . Add ( orgOp . TrimStart ( ';' ) + orgCi + dlnaflags ) ;
}
2021-11-25 18:42:43 +01:00
else if ( isDirectStream )
{
// orgOp should be added all the time once the time based seek is resolved for transcoded streams
contentFeatureList . Add ( "DLNA.ORG_PN=" + orgPn + orgOp + orgCi + dlnaflags ) ;
}
2021-04-02 20:06:38 +02:00
else
{
contentFeatureList . Add ( "DLNA.ORG_PN=" + orgPn + orgCi + dlnaflags ) ;
}
2018-12-28 00:27:57 +01:00
}
if ( orgPnValues . Count = = 0 )
{
2021-04-02 20:06:38 +02:00
contentFeatureList . Add ( orgOp . TrimStart ( ';' ) + orgCi + dlnaflags ) ;
2018-12-28 00:27:57 +01:00
}
return contentFeatureList ;
}
2019-03-26 22:56:05 +01:00
private static string GetImageOrgPnValue ( string container , int? width , int? height )
2018-12-28 00:27:57 +01:00
{
2021-04-02 20:06:38 +02:00
MediaFormatProfile ? format = MediaFormatProfileResolver . ResolveImageFormat ( container , width , height ) ;
2018-12-28 00:27:57 +01:00
return format . HasValue ? format . Value . ToString ( ) : null ;
}
2019-03-26 22:56:05 +01:00
private static string GetAudioOrgPnValue ( string container , int? audioBitrate , int? audioSampleRate , int? audioChannels )
2018-12-28 00:27:57 +01:00
{
2021-04-02 20:06:38 +02:00
MediaFormatProfile ? format = MediaFormatProfileResolver . ResolveAudioFormat (
2020-10-12 20:05:11 +02:00
container ,
2018-12-28 00:27:57 +01:00
audioBitrate ,
audioSampleRate ,
audioChannels ) ;
return format . HasValue ? format . Value . ToString ( ) : null ;
}
2021-04-02 20:06:38 +02:00
private static MediaFormatProfile [ ] GetVideoOrgPnValue ( string container , string videoCodec , string audioCodec , int? width , int? height , TransportStreamTimestamp timestamp )
2018-12-28 00:27:57 +01:00
{
2021-04-02 20:06:38 +02:00
return MediaFormatProfileResolver . ResolveVideoFormat ( container , videoCodec , audioCodec , width , height , timestamp ) ;
2018-12-28 00:27:57 +01:00
}
}
}