StreamInfo.cs 18 KB

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