StreamInfo.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. using MediaBrowser.Model.Drawing;
  2. using MediaBrowser.Model.Dto;
  3. using MediaBrowser.Model.Entities;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Globalization;
  7. using System.Linq;
  8. namespace MediaBrowser.Model.Dlna
  9. {
  10. /// <summary>
  11. /// Class StreamInfo.
  12. /// </summary>
  13. public class StreamInfo
  14. {
  15. public string ItemId { get; set; }
  16. public bool IsDirectStream { get; set; }
  17. public DlnaProfileType MediaType { get; set; }
  18. public string Container { get; set; }
  19. public string Protocol { get; set; }
  20. public long StartPositionTicks { get; set; }
  21. public string VideoCodec { get; set; }
  22. public string AudioCodec { get; set; }
  23. public int? AudioStreamIndex { get; set; }
  24. public int? SubtitleStreamIndex { get; set; }
  25. public int? MaxAudioChannels { get; set; }
  26. public int? AudioBitrate { get; set; }
  27. public int? VideoBitrate { get; set; }
  28. public int? VideoLevel { get; set; }
  29. public int? MaxWidth { get; set; }
  30. public int? MaxHeight { get; set; }
  31. public int? MaxFramerate { get; set; }
  32. public string DeviceProfileId { get; set; }
  33. public string DeviceId { get; set; }
  34. public long? RunTimeTicks { get; set; }
  35. public TranscodeSeekInfo TranscodeSeekInfo { get; set; }
  36. public bool EstimateContentLength { get; set; }
  37. public MediaSourceInfo MediaSource { get; set; }
  38. public TransportStreamTimestamp TargetTimestamp { get; set; }
  39. public string MediaSourceId
  40. {
  41. get
  42. {
  43. return MediaSource == null ? null : MediaSource.Id;
  44. }
  45. }
  46. public string ToUrl(string baseUrl)
  47. {
  48. return ToDlnaUrl(baseUrl);
  49. }
  50. public string ToDlnaUrl(string baseUrl)
  51. {
  52. if (string.IsNullOrEmpty(baseUrl))
  53. {
  54. throw new ArgumentNullException(baseUrl);
  55. }
  56. var dlnaCommand = BuildDlnaParam(this);
  57. var extension = string.IsNullOrEmpty(Container) ? string.Empty : "." + Container;
  58. baseUrl = baseUrl.TrimEnd('/');
  59. if (MediaType == DlnaProfileType.Audio)
  60. {
  61. return string.Format("{0}/audio/{1}/stream{2}?{3}", baseUrl, ItemId, extension, dlnaCommand);
  62. }
  63. if (string.Equals(Protocol, "hls", StringComparison.OrdinalIgnoreCase))
  64. {
  65. return string.Format("{0}/videos/{1}/stream.m3u8?{2}", baseUrl, ItemId, dlnaCommand);
  66. }
  67. return string.Format("{0}/videos/{1}/stream{2}?{3}", baseUrl, ItemId, extension, dlnaCommand);
  68. }
  69. private static string BuildDlnaParam(StreamInfo item)
  70. {
  71. var usCulture = new CultureInfo("en-US");
  72. var list = new List<string>
  73. {
  74. item.DeviceProfileId ?? string.Empty,
  75. item.DeviceId ?? string.Empty,
  76. item.MediaSourceId ?? string.Empty,
  77. (item.IsDirectStream).ToString().ToLower(),
  78. item.VideoCodec ?? string.Empty,
  79. item.AudioCodec ?? string.Empty,
  80. item.AudioStreamIndex.HasValue ? item.AudioStreamIndex.Value.ToString(usCulture) : string.Empty,
  81. item.SubtitleStreamIndex.HasValue ? item.SubtitleStreamIndex.Value.ToString(usCulture) : string.Empty,
  82. item.VideoBitrate.HasValue ? item.VideoBitrate.Value.ToString(usCulture) : string.Empty,
  83. item.AudioBitrate.HasValue ? item.AudioBitrate.Value.ToString(usCulture) : string.Empty,
  84. item.MaxAudioChannels.HasValue ? item.MaxAudioChannels.Value.ToString(usCulture) : string.Empty,
  85. item.MaxFramerate.HasValue ? item.MaxFramerate.Value.ToString(usCulture) : string.Empty,
  86. item.MaxWidth.HasValue ? item.MaxWidth.Value.ToString(usCulture) : string.Empty,
  87. item.MaxHeight.HasValue ? item.MaxHeight.Value.ToString(usCulture) : string.Empty,
  88. item.StartPositionTicks.ToString(usCulture),
  89. item.VideoLevel.HasValue ? item.VideoLevel.Value.ToString(usCulture) : string.Empty
  90. };
  91. return string.Format("Params={0}", string.Join(";", list.ToArray()));
  92. }
  93. /// <summary>
  94. /// Returns the audio stream that will be used
  95. /// </summary>
  96. public MediaStream TargetAudioStream
  97. {
  98. get
  99. {
  100. if (MediaSource != null)
  101. {
  102. var audioStreams = MediaSource.MediaStreams.Where(i => i.Type == MediaStreamType.Audio);
  103. if (AudioStreamIndex.HasValue)
  104. {
  105. return audioStreams.FirstOrDefault(i => i.Index == AudioStreamIndex.Value);
  106. }
  107. return audioStreams.FirstOrDefault();
  108. }
  109. return null;
  110. }
  111. }
  112. /// <summary>
  113. /// Returns the video stream that will be used
  114. /// </summary>
  115. public MediaStream TargetVideoStream
  116. {
  117. get
  118. {
  119. if (MediaSource != null)
  120. {
  121. return MediaSource.MediaStreams
  122. .FirstOrDefault(i => i.Type == MediaStreamType.Video && (i.Codec ?? string.Empty).IndexOf("jpeg", StringComparison.OrdinalIgnoreCase) == -1);
  123. }
  124. return null;
  125. }
  126. }
  127. /// <summary>
  128. /// Predicts the audio sample rate that will be in the output stream
  129. /// </summary>
  130. public int? TargetAudioSampleRate
  131. {
  132. get
  133. {
  134. var stream = TargetAudioStream;
  135. return stream == null ? null : stream.SampleRate;
  136. }
  137. }
  138. /// <summary>
  139. /// Predicts the audio bitrate that will be in the output stream
  140. /// </summary>
  141. public int? TargetAudioBitrate
  142. {
  143. get
  144. {
  145. var stream = TargetAudioStream;
  146. return AudioBitrate.HasValue && !IsDirectStream
  147. ? AudioBitrate
  148. : stream == null ? null : stream.BitRate;
  149. }
  150. }
  151. /// <summary>
  152. /// Predicts the audio channels that will be in the output stream
  153. /// </summary>
  154. public int? TargetAudioChannels
  155. {
  156. get
  157. {
  158. var stream = TargetAudioStream;
  159. return MaxAudioChannels.HasValue && !IsDirectStream
  160. ? (stream.Channels.HasValue ? Math.Min(MaxAudioChannels.Value, stream.Channels.Value) : MaxAudioChannels.Value)
  161. : stream == null ? null : stream.Channels;
  162. }
  163. }
  164. /// <summary>
  165. /// Predicts the audio codec that will be in the output stream
  166. /// </summary>
  167. public string TargetAudioCodec
  168. {
  169. get
  170. {
  171. var stream = TargetAudioStream;
  172. return IsDirectStream
  173. ? (stream == null ? null : stream.Codec)
  174. : AudioCodec;
  175. }
  176. }
  177. /// <summary>
  178. /// Predicts the audio channels that will be in the output stream
  179. /// </summary>
  180. public long? TargetSize
  181. {
  182. get
  183. {
  184. if (IsDirectStream)
  185. {
  186. return MediaSource.Bitrate;
  187. }
  188. if (RunTimeTicks.HasValue)
  189. {
  190. var totalBitrate = 0;
  191. if (AudioBitrate.HasValue)
  192. {
  193. totalBitrate += AudioBitrate.Value;
  194. }
  195. if (VideoBitrate.HasValue)
  196. {
  197. totalBitrate += VideoBitrate.Value;
  198. }
  199. return Convert.ToInt64(totalBitrate * TimeSpan.FromTicks(RunTimeTicks.Value).TotalSeconds);
  200. }
  201. var stream = TargetAudioStream;
  202. return MaxAudioChannels.HasValue && !IsDirectStream
  203. ? (stream.Channels.HasValue ? Math.Min(MaxAudioChannels.Value, stream.Channels.Value) : MaxAudioChannels.Value)
  204. : stream == null ? null : stream.Channels;
  205. }
  206. }
  207. public int? TotalOutputBitrate
  208. {
  209. get
  210. {
  211. return (TargetAudioBitrate ?? 0) + (VideoBitrate ?? 0);
  212. }
  213. }
  214. public int? TargetWidth
  215. {
  216. get
  217. {
  218. var videoStream = TargetVideoStream;
  219. if (videoStream != null && videoStream.Width.HasValue && videoStream.Height.HasValue)
  220. {
  221. var size = new ImageSize
  222. {
  223. Width = videoStream.Width.Value,
  224. Height = videoStream.Height.Value
  225. };
  226. var newSize = DrawingUtils.Resize(size,
  227. null,
  228. null,
  229. MaxWidth,
  230. MaxHeight);
  231. return Convert.ToInt32(newSize.Width);
  232. }
  233. return MaxWidth;
  234. }
  235. }
  236. public int? TargetHeight
  237. {
  238. get
  239. {
  240. var videoStream = TargetVideoStream;
  241. if (videoStream != null && videoStream.Width.HasValue && videoStream.Height.HasValue)
  242. {
  243. var size = new ImageSize
  244. {
  245. Width = videoStream.Width.Value,
  246. Height = videoStream.Height.Value
  247. };
  248. var newSize = DrawingUtils.Resize(size,
  249. null,
  250. null,
  251. MaxWidth,
  252. MaxHeight);
  253. return Convert.ToInt32(newSize.Height);
  254. }
  255. return MaxHeight;
  256. }
  257. }
  258. }
  259. /// <summary>
  260. /// Class AudioOptions.
  261. /// </summary>
  262. public class AudioOptions
  263. {
  264. public string ItemId { get; set; }
  265. public List<MediaSourceInfo> MediaSources { get; set; }
  266. public DeviceProfile Profile { get; set; }
  267. /// <summary>
  268. /// Optional. Only needed if a specific AudioStreamIndex or SubtitleStreamIndex are requested.
  269. /// </summary>
  270. public string MediaSourceId { get; set; }
  271. public string DeviceId { get; set; }
  272. /// <summary>
  273. /// Allows an override of supported number of audio channels
  274. /// Example: DeviceProfile supports five channel, but user only has stereo speakers
  275. /// </summary>
  276. public int? MaxAudioChannels { get; set; }
  277. /// <summary>
  278. /// The application's configured quality setting
  279. /// </summary>
  280. public int? MaxBitrate { get; set; }
  281. }
  282. /// <summary>
  283. /// Class VideoOptions.
  284. /// </summary>
  285. public class VideoOptions : AudioOptions
  286. {
  287. public int? AudioStreamIndex { get; set; }
  288. public int? SubtitleStreamIndex { get; set; }
  289. public int? MaxAudioTranscodingBitrate { get; set; }
  290. public VideoOptions()
  291. {
  292. MaxAudioTranscodingBitrate = 128000;
  293. }
  294. }
  295. }