StreamInfo.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. using MediaBrowser.Model.Drawing;
  2. using MediaBrowser.Model.Dto;
  3. using MediaBrowser.Model.Entities;
  4. using MediaBrowser.Model.Extensions;
  5. using MediaBrowser.Model.MediaInfo;
  6. using MediaBrowser.Model.Session;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Globalization;
  10. namespace MediaBrowser.Model.Dlna
  11. {
  12. /// <summary>
  13. /// Class StreamInfo.
  14. /// </summary>
  15. public class StreamInfo
  16. {
  17. public string ItemId { get; set; }
  18. public PlayMethod PlayMethod { get; set; }
  19. public DlnaProfileType MediaType { get; set; }
  20. public string Container { get; set; }
  21. public string Protocol { get; set; }
  22. public long StartPositionTicks { get; set; }
  23. public string VideoCodec { get; set; }
  24. public string VideoProfile { get; set; }
  25. public string AudioCodec { get; set; }
  26. public int? AudioStreamIndex { get; set; }
  27. public int? SubtitleStreamIndex { get; set; }
  28. public int? MaxAudioChannels { get; set; }
  29. public int? AudioBitrate { get; set; }
  30. public int? VideoBitrate { get; set; }
  31. public int? VideoLevel { get; set; }
  32. public int? MaxWidth { get; set; }
  33. public int? MaxHeight { get; set; }
  34. public int? MaxVideoBitDepth { get; set; }
  35. public int? MaxRefFrames { get; set; }
  36. public float? MaxFramerate { get; set; }
  37. public string DeviceProfileId { get; set; }
  38. public string DeviceId { get; set; }
  39. public long? RunTimeTicks { get; set; }
  40. public TranscodeSeekInfo TranscodeSeekInfo { get; set; }
  41. public bool EstimateContentLength { get; set; }
  42. public MediaSourceInfo MediaSource { get; set; }
  43. public SubtitleDeliveryMethod SubtitleDeliveryMethod { get; set; }
  44. public string SubtitleFormat { get; set; }
  45. public string MediaSourceId
  46. {
  47. get
  48. {
  49. return MediaSource == null ? null : MediaSource.Id;
  50. }
  51. }
  52. public bool IsDirectStream
  53. {
  54. get {
  55. return PlayMethod == PlayMethod.DirectStream ||
  56. PlayMethod == PlayMethod.DirectPlay;
  57. }
  58. }
  59. public string ToUrl(string baseUrl)
  60. {
  61. return ToDlnaUrl(baseUrl);
  62. }
  63. public string ToDlnaUrl(string baseUrl)
  64. {
  65. if (PlayMethod == PlayMethod.DirectPlay)
  66. {
  67. return MediaSource.Path;
  68. }
  69. if (string.IsNullOrEmpty(baseUrl))
  70. {
  71. throw new ArgumentNullException(baseUrl);
  72. }
  73. string dlnaCommand = BuildDlnaParam(this);
  74. string extension = string.IsNullOrEmpty(Container) ? string.Empty : "." + Container;
  75. baseUrl = baseUrl.TrimEnd('/');
  76. if (MediaType == DlnaProfileType.Audio)
  77. {
  78. return string.Format("{0}/audio/{1}/stream{2}?{3}", baseUrl, ItemId, extension, dlnaCommand);
  79. }
  80. if (StringHelper.EqualsIgnoreCase(Protocol, "hls"))
  81. {
  82. return string.Format("{0}/videos/{1}/master.m3u8?{2}", baseUrl, ItemId, dlnaCommand);
  83. }
  84. return string.Format("{0}/videos/{1}/stream{2}?{3}", baseUrl, ItemId, extension, dlnaCommand);
  85. }
  86. private static string BuildDlnaParam(StreamInfo item)
  87. {
  88. List<string> list = new List<string>
  89. {
  90. item.DeviceProfileId ?? string.Empty,
  91. item.DeviceId ?? string.Empty,
  92. item.MediaSourceId ?? string.Empty,
  93. (item.IsDirectStream).ToString().ToLower(),
  94. item.VideoCodec ?? string.Empty,
  95. item.AudioCodec ?? string.Empty,
  96. item.AudioStreamIndex.HasValue ? StringHelper.ToStringCultureInvariant(item.AudioStreamIndex.Value) : string.Empty,
  97. item.SubtitleStreamIndex.HasValue && item.SubtitleDeliveryMethod != SubtitleDeliveryMethod.External ? StringHelper.ToStringCultureInvariant(item.SubtitleStreamIndex.Value) : string.Empty,
  98. item.VideoBitrate.HasValue ? StringHelper.ToStringCultureInvariant(item.VideoBitrate.Value) : string.Empty,
  99. item.AudioBitrate.HasValue ? StringHelper.ToStringCultureInvariant(item.AudioBitrate.Value) : string.Empty,
  100. item.MaxAudioChannels.HasValue ? StringHelper.ToStringCultureInvariant(item.MaxAudioChannels.Value) : string.Empty,
  101. item.MaxFramerate.HasValue ? StringHelper.ToStringCultureInvariant(item.MaxFramerate.Value) : string.Empty,
  102. item.MaxWidth.HasValue ? StringHelper.ToStringCultureInvariant(item.MaxWidth.Value) : string.Empty,
  103. item.MaxHeight.HasValue ? StringHelper.ToStringCultureInvariant(item.MaxHeight.Value) : string.Empty,
  104. StringHelper.ToStringCultureInvariant(item.StartPositionTicks),
  105. item.VideoLevel.HasValue ? StringHelper.ToStringCultureInvariant(item.VideoLevel.Value) : string.Empty
  106. };
  107. list.Add(item.IsDirectStream ? string.Empty : DateTime.UtcNow.Ticks.ToString(CultureInfo.InvariantCulture));
  108. list.Add(item.MaxRefFrames.HasValue ? StringHelper.ToStringCultureInvariant(item.MaxRefFrames.Value) : string.Empty);
  109. list.Add(item.MaxVideoBitDepth.HasValue ? StringHelper.ToStringCultureInvariant(item.MaxVideoBitDepth.Value) : string.Empty);
  110. list.Add(item.VideoProfile ?? string.Empty);
  111. return string.Format("Params={0}", string.Join(";", list.ToArray()));
  112. }
  113. public List<SubtitleStreamInfo> GetExternalSubtitles(string baseUrl)
  114. {
  115. if (string.IsNullOrEmpty(baseUrl))
  116. {
  117. throw new ArgumentNullException(baseUrl);
  118. }
  119. List<SubtitleStreamInfo> list = new List<SubtitleStreamInfo>();
  120. if (SubtitleDeliveryMethod != SubtitleDeliveryMethod.External)
  121. {
  122. return list;
  123. }
  124. if (!SubtitleStreamIndex.HasValue)
  125. {
  126. return list;
  127. }
  128. // HLS will preserve timestamps so we can just grab the full subtitle stream
  129. long startPositionTicks = StringHelper.EqualsIgnoreCase(Protocol, "hls")
  130. ? 0
  131. : StartPositionTicks;
  132. string url = string.Format("{0}/Videos/{1}/{2}/Subtitles/{3}/{4}/Stream.{5}",
  133. baseUrl,
  134. ItemId,
  135. MediaSourceId,
  136. StringHelper.ToStringCultureInvariant(SubtitleStreamIndex.Value),
  137. StringHelper.ToStringCultureInvariant(startPositionTicks),
  138. SubtitleFormat);
  139. foreach (MediaStream stream in MediaSource.MediaStreams)
  140. {
  141. if (stream.Type == MediaStreamType.Subtitle && stream.Index == SubtitleStreamIndex.Value)
  142. {
  143. list.Add(new SubtitleStreamInfo
  144. {
  145. Url = url,
  146. IsForced = stream.IsForced,
  147. Language = stream.Language,
  148. Name = stream.Language ?? "Unknown",
  149. Format = SubtitleFormat
  150. });
  151. }
  152. }
  153. return list;
  154. }
  155. /// <summary>
  156. /// Returns the audio stream that will be used
  157. /// </summary>
  158. public MediaStream TargetAudioStream
  159. {
  160. get
  161. {
  162. if (MediaSource != null)
  163. {
  164. if (AudioStreamIndex.HasValue)
  165. {
  166. foreach (MediaStream i in MediaSource.MediaStreams)
  167. {
  168. if (i.Index == AudioStreamIndex.Value && i.Type == MediaStreamType.Audio)
  169. return i;
  170. }
  171. return null;
  172. }
  173. return MediaSource.DefaultAudioStream;
  174. }
  175. return null;
  176. }
  177. }
  178. /// <summary>
  179. /// Returns the video stream that will be used
  180. /// </summary>
  181. public MediaStream TargetVideoStream
  182. {
  183. get
  184. {
  185. if (MediaSource != null)
  186. {
  187. return MediaSource.VideoStream;
  188. }
  189. return null;
  190. }
  191. }
  192. /// <summary>
  193. /// Predicts the audio sample rate that will be in the output stream
  194. /// </summary>
  195. public int? TargetAudioSampleRate
  196. {
  197. get
  198. {
  199. MediaStream stream = TargetAudioStream;
  200. return stream == null ? null : stream.SampleRate;
  201. }
  202. }
  203. /// <summary>
  204. /// Predicts the audio sample rate that will be in the output stream
  205. /// </summary>
  206. public int? TargetVideoBitDepth
  207. {
  208. get
  209. {
  210. MediaStream stream = TargetVideoStream;
  211. return stream == null || !IsDirectStream ? null : stream.BitDepth;
  212. }
  213. }
  214. /// <summary>
  215. /// Gets the target reference frames.
  216. /// </summary>
  217. /// <value>The target reference frames.</value>
  218. public int? TargetRefFrames
  219. {
  220. get
  221. {
  222. MediaStream stream = TargetVideoStream;
  223. return stream == null || !IsDirectStream ? null : stream.RefFrames;
  224. }
  225. }
  226. /// <summary>
  227. /// Predicts the audio sample rate that will be in the output stream
  228. /// </summary>
  229. public float? TargetFramerate
  230. {
  231. get
  232. {
  233. MediaStream stream = TargetVideoStream;
  234. return MaxFramerate.HasValue && !IsDirectStream
  235. ? MaxFramerate
  236. : stream == null ? null : stream.AverageFrameRate ?? stream.RealFrameRate;
  237. }
  238. }
  239. /// <summary>
  240. /// Predicts the audio sample rate that will be in the output stream
  241. /// </summary>
  242. public double? TargetVideoLevel
  243. {
  244. get
  245. {
  246. MediaStream stream = TargetVideoStream;
  247. return VideoLevel.HasValue && !IsDirectStream
  248. ? VideoLevel
  249. : stream == null ? null : stream.Level;
  250. }
  251. }
  252. /// <summary>
  253. /// Predicts the audio sample rate that will be in the output stream
  254. /// </summary>
  255. public int? TargetPacketLength
  256. {
  257. get
  258. {
  259. MediaStream stream = TargetVideoStream;
  260. return !IsDirectStream
  261. ? null
  262. : stream == null ? null : stream.PacketLength;
  263. }
  264. }
  265. /// <summary>
  266. /// Predicts the audio sample rate that will be in the output stream
  267. /// </summary>
  268. public string TargetVideoProfile
  269. {
  270. get
  271. {
  272. MediaStream stream = TargetVideoStream;
  273. return !string.IsNullOrEmpty(VideoProfile) && !IsDirectStream
  274. ? VideoProfile
  275. : stream == null ? null : stream.Profile;
  276. }
  277. }
  278. /// <summary>
  279. /// Predicts the audio bitrate that will be in the output stream
  280. /// </summary>
  281. public int? TargetAudioBitrate
  282. {
  283. get
  284. {
  285. MediaStream stream = TargetAudioStream;
  286. return AudioBitrate.HasValue && !IsDirectStream
  287. ? AudioBitrate
  288. : stream == null ? null : stream.BitRate;
  289. }
  290. }
  291. /// <summary>
  292. /// Predicts the audio channels that will be in the output stream
  293. /// </summary>
  294. public int? TargetAudioChannels
  295. {
  296. get
  297. {
  298. MediaStream stream = TargetAudioStream;
  299. int? streamChannels = stream == null ? null : stream.Channels;
  300. return MaxAudioChannels.HasValue && !IsDirectStream
  301. ? (streamChannels.HasValue ? Math.Min(MaxAudioChannels.Value, streamChannels.Value) : MaxAudioChannels.Value)
  302. : streamChannels;
  303. }
  304. }
  305. /// <summary>
  306. /// Predicts the audio codec that will be in the output stream
  307. /// </summary>
  308. public string TargetAudioCodec
  309. {
  310. get
  311. {
  312. MediaStream stream = TargetAudioStream;
  313. return IsDirectStream
  314. ? (stream == null ? null : stream.Codec)
  315. : AudioCodec;
  316. }
  317. }
  318. /// <summary>
  319. /// Predicts the audio channels that will be in the output stream
  320. /// </summary>
  321. public long? TargetSize
  322. {
  323. get
  324. {
  325. if (IsDirectStream)
  326. {
  327. return MediaSource.Size;
  328. }
  329. if (RunTimeTicks.HasValue)
  330. {
  331. int? totalBitrate = TargetTotalBitrate;
  332. double totalSeconds = RunTimeTicks.Value;
  333. // Convert to ms
  334. totalSeconds /= 10000;
  335. // Convert to seconds
  336. totalSeconds /= 1000;
  337. return totalBitrate.HasValue ?
  338. Convert.ToInt64(totalBitrate.Value * totalSeconds) :
  339. (long?)null;
  340. }
  341. return null;
  342. }
  343. }
  344. public int? TargetVideoBitrate
  345. {
  346. get
  347. {
  348. MediaStream stream = TargetVideoStream;
  349. return VideoBitrate.HasValue && !IsDirectStream
  350. ? VideoBitrate
  351. : stream == null ? null : stream.BitRate;
  352. }
  353. }
  354. public TransportStreamTimestamp TargetTimestamp
  355. {
  356. get
  357. {
  358. TransportStreamTimestamp defaultValue = StringHelper.EqualsIgnoreCase(Container, "m2ts")
  359. ? TransportStreamTimestamp.Valid
  360. : TransportStreamTimestamp.None;
  361. return !IsDirectStream
  362. ? defaultValue
  363. : MediaSource == null ? defaultValue : MediaSource.Timestamp ?? TransportStreamTimestamp.None;
  364. }
  365. }
  366. public int? TargetTotalBitrate
  367. {
  368. get
  369. {
  370. return (TargetAudioBitrate ?? 0) + (TargetVideoBitrate ?? 0);
  371. }
  372. }
  373. public bool? IsTargetAnamorphic
  374. {
  375. get
  376. {
  377. if (IsDirectStream)
  378. {
  379. return TargetVideoStream == null ? null : TargetVideoStream.IsAnamorphic;
  380. }
  381. return false;
  382. }
  383. }
  384. public int? TargetWidth
  385. {
  386. get
  387. {
  388. MediaStream videoStream = TargetVideoStream;
  389. if (videoStream != null && videoStream.Width.HasValue && videoStream.Height.HasValue)
  390. {
  391. ImageSize size = new ImageSize
  392. {
  393. Width = videoStream.Width.Value,
  394. Height = videoStream.Height.Value
  395. };
  396. double? maxWidth = MaxWidth.HasValue ? (double)MaxWidth.Value : (double?)null;
  397. double? maxHeight = MaxHeight.HasValue ? (double)MaxHeight.Value : (double?)null;
  398. ImageSize newSize = DrawingUtils.Resize(size,
  399. null,
  400. null,
  401. maxWidth,
  402. maxHeight);
  403. return Convert.ToInt32(newSize.Width);
  404. }
  405. return MaxWidth;
  406. }
  407. }
  408. public int? TargetHeight
  409. {
  410. get
  411. {
  412. MediaStream videoStream = TargetVideoStream;
  413. if (videoStream != null && videoStream.Width.HasValue && videoStream.Height.HasValue)
  414. {
  415. ImageSize size = new ImageSize
  416. {
  417. Width = videoStream.Width.Value,
  418. Height = videoStream.Height.Value
  419. };
  420. double? maxWidth = MaxWidth.HasValue ? (double)MaxWidth.Value : (double?)null;
  421. double? maxHeight = MaxHeight.HasValue ? (double)MaxHeight.Value : (double?)null;
  422. ImageSize newSize = DrawingUtils.Resize(size,
  423. null,
  424. null,
  425. maxWidth,
  426. maxHeight);
  427. return Convert.ToInt32(newSize.Height);
  428. }
  429. return MaxHeight;
  430. }
  431. }
  432. }
  433. }