2014-05-04 14:19:46 +00:00
using MediaBrowser.Model.MediaInfo ;
2014-04-24 05:08:10 +00:00
using System.Collections.Generic ;
2014-04-23 02:47:46 +00:00
namespace MediaBrowser.Model.Dlna
{
public class ContentFeatureBuilder
{
private readonly DeviceProfile _profile ;
public ContentFeatureBuilder ( DeviceProfile profile )
{
_profile = profile ;
}
2014-04-24 05:08:10 +00:00
public string BuildImageHeader ( string container ,
int? width ,
int? height )
{
2014-05-08 20:44:17 +00:00
string orgOp = ";DLNA.ORG_OP=" + DlnaMaps . GetImageOrgOpValue ( ) ;
2014-04-24 05:08:10 +00:00
// 0 = native, 1 = transcoded
const string orgCi = ";DLNA.ORG_CI=0" ;
2014-05-08 20:44:17 +00:00
DlnaFlags flagValue = DlnaFlags . StreamingTransferMode |
2014-04-24 05:08:10 +00:00
DlnaFlags . BackgroundTransferMode |
DlnaFlags . DlnaV15 ;
2014-05-08 20:44:17 +00:00
string dlnaflags = string . Format ( ";DLNA.ORG_FLAGS={0}" ,
2014-04-24 05:08:10 +00:00
FlagsToString ( flagValue ) ) ;
2014-05-08 20:44:17 +00:00
ResponseProfile mediaProfile = _profile . GetImageMediaProfile ( container ,
2014-04-24 05:08:10 +00:00
width ,
height ) ;
2014-05-08 20:44:17 +00:00
string orgPn = mediaProfile = = null ? null : mediaProfile . OrgPn ;
2014-04-24 05:08:10 +00:00
if ( string . IsNullOrEmpty ( orgPn ) )
{
orgPn = GetImageOrgPnValue ( container , width , height ) ;
}
2014-05-08 20:44:17 +00:00
string contentFeatures = string . IsNullOrEmpty ( orgPn ) ? string . Empty : "DLNA.ORG_PN=" + orgPn ;
2014-04-24 05:08:10 +00:00
return ( contentFeatures + orgOp + orgCi + dlnaflags ) . Trim ( ';' ) ;
}
public string BuildAudioHeader ( string container ,
2014-04-23 02:47:46 +00:00
string audioCodec ,
2014-04-24 05:08:10 +00:00
int? audioBitrate ,
int? audioSampleRate ,
int? audioChannels ,
bool isDirectStream ,
long? runtimeTicks ,
2014-04-23 02:47:46 +00:00
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
2014-05-08 20:44:17 +00:00
string orgOp = ";DLNA.ORG_OP=" + DlnaMaps . GetOrgOpValue ( runtimeTicks . HasValue , isDirectStream , transcodeSeekInfo ) ;
2014-04-23 02:47:46 +00:00
// 0 = native, 1 = transcoded
2014-05-08 20:44:17 +00:00
string orgCi = isDirectStream ? ";DLNA.ORG_CI=0" : ";DLNA.ORG_CI=1" ;
2014-04-23 02:47:46 +00:00
2014-05-08 20:44:17 +00:00
DlnaFlags flagValue = DlnaFlags . StreamingTransferMode |
2014-04-23 02:47:46 +00:00
DlnaFlags . BackgroundTransferMode |
DlnaFlags . DlnaV15 ;
if ( isDirectStream )
{
//flagValue = flagValue | DlnaFlags.DLNA_ORG_FLAG_BYTE_BASED_SEEK;
}
else if ( runtimeTicks . HasValue )
{
//flagValue = flagValue | DlnaFlags.DLNA_ORG_FLAG_TIME_BASED_SEEK;
}
2014-05-08 20:44:17 +00:00
string dlnaflags = string . Format ( ";DLNA.ORG_FLAGS={0}" ,
2014-04-23 15:35:31 +00:00
FlagsToString ( flagValue ) ) ;
2014-04-23 02:47:46 +00:00
2014-05-08 20:44:17 +00:00
ResponseProfile mediaProfile = _profile . GetAudioMediaProfile ( container ,
2014-04-24 05:08:10 +00:00
audioCodec ,
audioChannels ,
audioBitrate ) ;
2014-04-23 02:47:46 +00:00
2014-05-08 20:44:17 +00:00
string orgPn = mediaProfile = = null ? null : mediaProfile . OrgPn ;
2014-04-23 02:47:46 +00:00
if ( string . IsNullOrEmpty ( orgPn ) )
{
orgPn = GetAudioOrgPnValue ( container , audioBitrate , audioSampleRate , audioChannels ) ;
}
2014-05-08 20:44:17 +00:00
string contentFeatures = string . IsNullOrEmpty ( orgPn ) ? string . Empty : "DLNA.ORG_PN=" + orgPn ;
2014-04-23 02:47:46 +00:00
return ( contentFeatures + orgOp + orgCi + dlnaflags ) . Trim ( ';' ) ;
}
2014-04-23 15:35:31 +00:00
private static string FlagsToString ( DlnaFlags flags )
{
//return Enum.Format(typeof(DlnaFlags), flags, "x");
return string . Format ( "{0:X8}{1:D24}" , ( ulong ) flags , 0 ) ;
}
2014-04-24 05:08:10 +00:00
public string BuildVideoHeader ( string container ,
string videoCodec ,
string audioCodec ,
int? width ,
int? height ,
int? bitDepth ,
int? videoBitrate ,
int? audioChannels ,
int? audioBitrate ,
TransportStreamTimestamp timestamp ,
bool isDirectStream ,
long? runtimeTicks ,
string videoProfile ,
double? videoLevel ,
double? videoFramerate ,
int? packetLength ,
2014-04-23 02:47:46 +00:00
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
2014-05-08 20:44:17 +00:00
string orgOp = ";DLNA.ORG_OP=" + DlnaMaps . GetOrgOpValue ( runtimeTicks . HasValue , isDirectStream , transcodeSeekInfo ) ;
2014-04-23 02:47:46 +00:00
// 0 = native, 1 = transcoded
2014-05-08 20:44:17 +00:00
string orgCi = isDirectStream ? ";DLNA.ORG_CI=0" : ";DLNA.ORG_CI=1" ;
2014-04-23 02:47:46 +00:00
2014-05-08 20:44:17 +00:00
DlnaFlags flagValue = DlnaFlags . StreamingTransferMode |
2014-04-23 02:47:46 +00:00
DlnaFlags . BackgroundTransferMode |
DlnaFlags . DlnaV15 ;
if ( isDirectStream )
{
//flagValue = flagValue | DlnaFlags.DLNA_ORG_FLAG_BYTE_BASED_SEEK;
}
else if ( runtimeTicks . HasValue )
{
//flagValue = flagValue | DlnaFlags.DLNA_ORG_FLAG_TIME_BASED_SEEK;
}
2014-05-08 20:44:17 +00:00
string dlnaflags = string . Format ( ";DLNA.ORG_FLAGS={0}" ,
2014-05-03 20:43:09 +00:00
FlagsToString ( flagValue ) ) ;
2014-04-23 02:47:46 +00:00
2014-05-08 20:44:17 +00:00
ResponseProfile mediaProfile = _profile . GetVideoMediaProfile ( container ,
2014-04-24 05:08:10 +00:00
audioCodec ,
videoCodec ,
audioBitrate ,
audioChannels ,
width ,
height ,
bitDepth ,
videoBitrate ,
videoProfile ,
videoLevel ,
videoFramerate ,
packetLength ,
timestamp ) ;
2014-05-08 20:44:17 +00:00
string orgPn = mediaProfile = = null ? null : mediaProfile . OrgPn ;
2014-04-24 05:08:10 +00:00
2014-04-23 02:47:46 +00:00
if ( string . IsNullOrEmpty ( orgPn ) )
{
2014-05-09 04:38:12 +00:00
foreach ( string s in GetVideoOrgPnValue ( container , videoCodec , audioCodec , width , height , timestamp ) )
{
orgPn = s ;
break ;
}
}
2014-04-24 05:08:10 +00:00
2014-05-09 04:38:12 +00:00
if ( string . IsNullOrEmpty ( orgPn ) )
{
2014-04-24 05:08:10 +00:00
// TODO: Support multiple values and return multiple headers?
2014-05-09 04:38:12 +00:00
foreach ( string s in ( orgPn ? ? string . Empty ) . Split ( ',' ) )
{
orgPn = s ;
break ;
}
2014-04-23 02:47:46 +00:00
}
2014-05-08 20:44:17 +00:00
string contentFeatures = string . IsNullOrEmpty ( orgPn ) ? string . Empty : "DLNA.ORG_PN=" + orgPn ;
2014-04-23 02:47:46 +00:00
return ( contentFeatures + orgOp + orgCi + dlnaflags ) . Trim ( ';' ) ;
}
2014-04-24 05:08:10 +00:00
private string GetImageOrgPnValue ( string container , int? width , int? height )
{
2014-05-08 20:44:17 +00:00
MediaFormatProfile ? format = new MediaFormatProfileResolver ( )
2014-04-24 05:08:10 +00:00
. ResolveImageFormat ( container ,
width ,
height ) ;
return format . HasValue ? format . Value . ToString ( ) : null ;
}
2014-04-23 02:47:46 +00:00
private string GetAudioOrgPnValue ( string container , int? audioBitrate , int? audioSampleRate , int? audioChannels )
{
2014-05-08 20:44:17 +00:00
MediaFormatProfile ? format = new MediaFormatProfileResolver ( )
2014-04-23 02:47:46 +00:00
. ResolveAudioFormat ( container ,
audioBitrate ,
audioSampleRate ,
audioChannels ) ;
return format . HasValue ? format . Value . ToString ( ) : null ;
}
2014-05-09 04:38:12 +00:00
private List < string > GetVideoOrgPnValue ( string container , string videoCodec , string audioCodec , int? width , int? height , TransportStreamTimestamp timestamp )
2014-04-23 02:47:46 +00:00
{
2014-05-09 04:38:12 +00:00
List < string > list = new List < string > ( ) ;
foreach ( MediaFormatProfile i in new MediaFormatProfileResolver ( ) . ResolveVideoFormat ( container , videoCodec , audioCodec , width , height , timestamp ) )
list . Add ( i . ToString ( ) ) ;
return list ;
2014-04-23 02:47:46 +00:00
}
}
}