StreamInfo.cs 18 KB

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