ContentFeatureBuilder.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. using MediaBrowser.Model.MediaInfo;
  2. using System;
  3. using System.Collections.Generic;
  4. namespace MediaBrowser.Model.Dlna
  5. {
  6. public class ContentFeatureBuilder
  7. {
  8. private readonly DeviceProfile _profile;
  9. public ContentFeatureBuilder(DeviceProfile profile)
  10. {
  11. _profile = profile;
  12. }
  13. public string BuildImageHeader(string container,
  14. int? width,
  15. int? height,
  16. bool isDirectStream,
  17. string orgPn = null)
  18. {
  19. string orgOp = ";DLNA.ORG_OP=" + DlnaMaps.GetImageOrgOpValue();
  20. // 0 = native, 1 = transcoded
  21. var orgCi = isDirectStream ? ";DLNA.ORG_CI=0" : ";DLNA.ORG_CI=1";
  22. DlnaFlags flagValue = DlnaFlags.BackgroundTransferMode |
  23. DlnaFlags.InteractiveTransferMode |
  24. DlnaFlags.DlnaV15;
  25. string dlnaflags = string.Format(";DLNA.ORG_FLAGS={0}",
  26. DlnaMaps.FlagsToString(flagValue));
  27. ResponseProfile mediaProfile = _profile.GetImageMediaProfile(container,
  28. width,
  29. height);
  30. if (string.IsNullOrEmpty(orgPn))
  31. {
  32. orgPn = mediaProfile == null ? null : mediaProfile.OrgPn;
  33. }
  34. if (string.IsNullOrEmpty(orgPn))
  35. {
  36. orgPn = GetImageOrgPnValue(container, width, height);
  37. }
  38. string contentFeatures = string.IsNullOrEmpty(orgPn) ? string.Empty : "DLNA.ORG_PN=" + orgPn;
  39. return (contentFeatures + orgOp + orgCi + dlnaflags).Trim(';');
  40. }
  41. public string BuildAudioHeader(string container,
  42. string audioCodec,
  43. int? audioBitrate,
  44. int? audioSampleRate,
  45. int? audioChannels,
  46. bool isDirectStream,
  47. long? runtimeTicks,
  48. TranscodeSeekInfo transcodeSeekInfo)
  49. {
  50. // 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
  51. string orgOp = ";DLNA.ORG_OP=" + DlnaMaps.GetOrgOpValue(runtimeTicks.HasValue, isDirectStream, transcodeSeekInfo);
  52. // 0 = native, 1 = transcoded
  53. string orgCi = isDirectStream ? ";DLNA.ORG_CI=0" : ";DLNA.ORG_CI=1";
  54. DlnaFlags flagValue = DlnaFlags.StreamingTransferMode |
  55. DlnaFlags.BackgroundTransferMode |
  56. DlnaFlags.InteractiveTransferMode |
  57. DlnaFlags.DlnaV15;
  58. //if (isDirectStream)
  59. //{
  60. // flagValue = flagValue | DlnaFlags.ByteBasedSeek;
  61. //}
  62. //else if (runtimeTicks.HasValue)
  63. //{
  64. // flagValue = flagValue | DlnaFlags.TimeBasedSeek;
  65. //}
  66. string dlnaflags = string.Format(";DLNA.ORG_FLAGS={0}",
  67. DlnaMaps.FlagsToString(flagValue));
  68. ResponseProfile mediaProfile = _profile.GetAudioMediaProfile(container,
  69. audioCodec,
  70. audioChannels,
  71. audioBitrate);
  72. string orgPn = mediaProfile == null ? null : mediaProfile.OrgPn;
  73. if (string.IsNullOrEmpty(orgPn))
  74. {
  75. orgPn = GetAudioOrgPnValue(container, audioBitrate, audioSampleRate, audioChannels);
  76. }
  77. string contentFeatures = string.IsNullOrEmpty(orgPn) ? string.Empty : "DLNA.ORG_PN=" + orgPn;
  78. return (contentFeatures + orgOp + orgCi + dlnaflags).Trim(';');
  79. }
  80. public List<string> BuildVideoHeader(string container,
  81. string videoCodec,
  82. string audioCodec,
  83. int? width,
  84. int? height,
  85. int? bitDepth,
  86. int? videoBitrate,
  87. TransportStreamTimestamp timestamp,
  88. bool isDirectStream,
  89. long? runtimeTicks,
  90. string videoProfile,
  91. double? videoLevel,
  92. float? videoFramerate,
  93. int? packetLength,
  94. TranscodeSeekInfo transcodeSeekInfo,
  95. bool? isAnamorphic,
  96. int? refFrames,
  97. int? numVideoStreams,
  98. int? numAudioStreams,
  99. string videoCodecTag,
  100. bool? isAvc)
  101. {
  102. // 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
  103. string orgOp = ";DLNA.ORG_OP=" + DlnaMaps.GetOrgOpValue(runtimeTicks.HasValue, isDirectStream, transcodeSeekInfo);
  104. // 0 = native, 1 = transcoded
  105. string orgCi = isDirectStream ? ";DLNA.ORG_CI=0" : ";DLNA.ORG_CI=1";
  106. DlnaFlags flagValue = DlnaFlags.StreamingTransferMode |
  107. DlnaFlags.BackgroundTransferMode |
  108. DlnaFlags.InteractiveTransferMode |
  109. DlnaFlags.DlnaV15;
  110. //if (isDirectStream)
  111. //{
  112. // flagValue = flagValue | DlnaFlags.ByteBasedSeek;
  113. //}
  114. //else if (runtimeTicks.HasValue)
  115. //{
  116. // flagValue = flagValue | DlnaFlags.TimeBasedSeek;
  117. //}
  118. string dlnaflags = string.Format(";DLNA.ORG_FLAGS={0}",
  119. DlnaMaps.FlagsToString(flagValue));
  120. ResponseProfile mediaProfile = _profile.GetVideoMediaProfile(container,
  121. audioCodec,
  122. videoCodec,
  123. width,
  124. height,
  125. bitDepth,
  126. videoBitrate,
  127. videoProfile,
  128. videoLevel,
  129. videoFramerate,
  130. packetLength,
  131. timestamp,
  132. isAnamorphic,
  133. refFrames,
  134. numVideoStreams,
  135. numAudioStreams,
  136. videoCodecTag,
  137. isAvc);
  138. List<string> orgPnValues = new List<string>();
  139. if (mediaProfile != null && !string.IsNullOrEmpty(mediaProfile.OrgPn))
  140. {
  141. orgPnValues.AddRange(mediaProfile.OrgPn.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries));
  142. }
  143. else
  144. {
  145. foreach (string s in GetVideoOrgPnValue(container, videoCodec, audioCodec, width, height, timestamp))
  146. {
  147. orgPnValues.Add(s);
  148. break;
  149. }
  150. }
  151. List<string> contentFeatureList = new List<string>();
  152. foreach (string orgPn in orgPnValues)
  153. {
  154. string contentFeatures = string.IsNullOrEmpty(orgPn) ? string.Empty : "DLNA.ORG_PN=" + orgPn;
  155. var value = (contentFeatures + orgOp + orgCi + dlnaflags).Trim(';');
  156. contentFeatureList.Add(value);
  157. }
  158. if (orgPnValues.Count == 0)
  159. {
  160. string contentFeatures = string.Empty;
  161. var value = (contentFeatures + orgOp + orgCi + dlnaflags).Trim(';');
  162. contentFeatureList.Add(value);
  163. }
  164. return contentFeatureList;
  165. }
  166. private string GetImageOrgPnValue(string container, int? width, int? height)
  167. {
  168. MediaFormatProfile? format = new MediaFormatProfileResolver()
  169. .ResolveImageFormat(container,
  170. width,
  171. height);
  172. return format.HasValue ? format.Value.ToString() : null;
  173. }
  174. private string GetAudioOrgPnValue(string container, int? audioBitrate, int? audioSampleRate, int? audioChannels)
  175. {
  176. MediaFormatProfile? format = new MediaFormatProfileResolver()
  177. .ResolveAudioFormat(container,
  178. audioBitrate,
  179. audioSampleRate,
  180. audioChannels);
  181. return format.HasValue ? format.Value.ToString() : null;
  182. }
  183. private List<string> GetVideoOrgPnValue(string container, string videoCodec, string audioCodec, int? width, int? height, TransportStreamTimestamp timestamp)
  184. {
  185. List<string> list = new List<string>();
  186. foreach (MediaFormatProfile i in new MediaFormatProfileResolver().ResolveVideoFormat(container, videoCodec, audioCodec, width, height, timestamp))
  187. list.Add(i.ToString());
  188. return list;
  189. }
  190. }
  191. }