ContentFeatureBuilder.cs 8.6 KB

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