StreamInfo.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  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. });
  171. }
  172. /// <summary>
  173. /// Returns the audio stream that will be used
  174. /// </summary>
  175. public MediaStream TargetAudioStream
  176. {
  177. get
  178. {
  179. if (MediaSource != null)
  180. {
  181. if (AudioStreamIndex.HasValue)
  182. {
  183. foreach (MediaStream i in MediaSource.MediaStreams)
  184. {
  185. if (i.Index == AudioStreamIndex.Value && i.Type == MediaStreamType.Audio)
  186. return i;
  187. }
  188. return null;
  189. }
  190. return MediaSource.DefaultAudioStream;
  191. }
  192. return null;
  193. }
  194. }
  195. /// <summary>
  196. /// Returns the video stream that will be used
  197. /// </summary>
  198. public MediaStream TargetVideoStream
  199. {
  200. get
  201. {
  202. if (MediaSource != null)
  203. {
  204. return MediaSource.VideoStream;
  205. }
  206. return null;
  207. }
  208. }
  209. /// <summary>
  210. /// Predicts the audio sample rate that will be in the output stream
  211. /// </summary>
  212. public int? TargetAudioSampleRate
  213. {
  214. get
  215. {
  216. MediaStream stream = TargetAudioStream;
  217. return stream == null ? null : stream.SampleRate;
  218. }
  219. }
  220. /// <summary>
  221. /// Predicts the audio sample rate that will be in the output stream
  222. /// </summary>
  223. public int? TargetVideoBitDepth
  224. {
  225. get
  226. {
  227. MediaStream stream = TargetVideoStream;
  228. return stream == null || !IsDirectStream ? null : stream.BitDepth;
  229. }
  230. }
  231. /// <summary>
  232. /// Gets the target reference frames.
  233. /// </summary>
  234. /// <value>The target reference frames.</value>
  235. public int? TargetRefFrames
  236. {
  237. get
  238. {
  239. MediaStream stream = TargetVideoStream;
  240. return stream == null || !IsDirectStream ? null : stream.RefFrames;
  241. }
  242. }
  243. /// <summary>
  244. /// Predicts the audio sample rate that will be in the output stream
  245. /// </summary>
  246. public float? TargetFramerate
  247. {
  248. get
  249. {
  250. MediaStream stream = TargetVideoStream;
  251. return MaxFramerate.HasValue && !IsDirectStream
  252. ? MaxFramerate
  253. : stream == null ? null : stream.AverageFrameRate ?? stream.RealFrameRate;
  254. }
  255. }
  256. /// <summary>
  257. /// Predicts the audio sample rate that will be in the output stream
  258. /// </summary>
  259. public double? TargetVideoLevel
  260. {
  261. get
  262. {
  263. MediaStream stream = TargetVideoStream;
  264. return VideoLevel.HasValue && !IsDirectStream
  265. ? VideoLevel
  266. : stream == null ? null : stream.Level;
  267. }
  268. }
  269. /// <summary>
  270. /// Predicts the audio sample rate that will be in the output stream
  271. /// </summary>
  272. public int? TargetPacketLength
  273. {
  274. get
  275. {
  276. MediaStream stream = TargetVideoStream;
  277. return !IsDirectStream
  278. ? null
  279. : stream == null ? null : stream.PacketLength;
  280. }
  281. }
  282. /// <summary>
  283. /// Predicts the audio sample rate that will be in the output stream
  284. /// </summary>
  285. public string TargetVideoProfile
  286. {
  287. get
  288. {
  289. MediaStream stream = TargetVideoStream;
  290. return !string.IsNullOrEmpty(VideoProfile) && !IsDirectStream
  291. ? VideoProfile
  292. : stream == null ? null : stream.Profile;
  293. }
  294. }
  295. /// <summary>
  296. /// Predicts the audio bitrate that will be in the output stream
  297. /// </summary>
  298. public int? TargetAudioBitrate
  299. {
  300. get
  301. {
  302. MediaStream stream = TargetAudioStream;
  303. return AudioBitrate.HasValue && !IsDirectStream
  304. ? AudioBitrate
  305. : stream == null ? null : stream.BitRate;
  306. }
  307. }
  308. /// <summary>
  309. /// Predicts the audio channels that will be in the output stream
  310. /// </summary>
  311. public int? TargetAudioChannels
  312. {
  313. get
  314. {
  315. MediaStream stream = TargetAudioStream;
  316. int? streamChannels = stream == null ? null : stream.Channels;
  317. if (MaxAudioChannels.HasValue && !IsDirectStream)
  318. {
  319. if (streamChannels.HasValue)
  320. {
  321. return Math.Min(MaxAudioChannels.Value, streamChannels.Value);
  322. }
  323. return MaxAudioChannels.Value;
  324. }
  325. return streamChannels;
  326. }
  327. }
  328. /// <summary>
  329. /// Predicts the audio codec that will be in the output stream
  330. /// </summary>
  331. public string TargetAudioCodec
  332. {
  333. get
  334. {
  335. MediaStream stream = TargetAudioStream;
  336. return IsDirectStream
  337. ? (stream == null ? null : stream.Codec)
  338. : AudioCodec;
  339. }
  340. }
  341. /// <summary>
  342. /// Predicts the audio channels that will be in the output stream
  343. /// </summary>
  344. public long? TargetSize
  345. {
  346. get
  347. {
  348. if (IsDirectStream)
  349. {
  350. return MediaSource.Size;
  351. }
  352. if (RunTimeTicks.HasValue)
  353. {
  354. int? totalBitrate = TargetTotalBitrate;
  355. double totalSeconds = RunTimeTicks.Value;
  356. // Convert to ms
  357. totalSeconds /= 10000;
  358. // Convert to seconds
  359. totalSeconds /= 1000;
  360. return totalBitrate.HasValue ?
  361. Convert.ToInt64(totalBitrate.Value * totalSeconds) :
  362. (long?)null;
  363. }
  364. return null;
  365. }
  366. }
  367. public int? TargetVideoBitrate
  368. {
  369. get
  370. {
  371. MediaStream stream = TargetVideoStream;
  372. return VideoBitrate.HasValue && !IsDirectStream
  373. ? VideoBitrate
  374. : stream == null ? null : stream.BitRate;
  375. }
  376. }
  377. public TransportStreamTimestamp TargetTimestamp
  378. {
  379. get
  380. {
  381. TransportStreamTimestamp defaultValue = StringHelper.EqualsIgnoreCase(Container, "m2ts")
  382. ? TransportStreamTimestamp.Valid
  383. : TransportStreamTimestamp.None;
  384. return !IsDirectStream
  385. ? defaultValue
  386. : MediaSource == null ? defaultValue : MediaSource.Timestamp ?? TransportStreamTimestamp.None;
  387. }
  388. }
  389. public int? TargetTotalBitrate
  390. {
  391. get
  392. {
  393. return (TargetAudioBitrate ?? 0) + (TargetVideoBitrate ?? 0);
  394. }
  395. }
  396. public bool? IsTargetAnamorphic
  397. {
  398. get
  399. {
  400. if (IsDirectStream)
  401. {
  402. return TargetVideoStream == null ? null : TargetVideoStream.IsAnamorphic;
  403. }
  404. return false;
  405. }
  406. }
  407. public bool? IsTargetCabac
  408. {
  409. get
  410. {
  411. if (IsDirectStream)
  412. {
  413. return TargetVideoStream == null ? null : TargetVideoStream.IsCabac;
  414. }
  415. return true;
  416. }
  417. }
  418. public int? TargetWidth
  419. {
  420. get
  421. {
  422. MediaStream videoStream = TargetVideoStream;
  423. if (videoStream != null && videoStream.Width.HasValue && videoStream.Height.HasValue)
  424. {
  425. ImageSize size = new ImageSize
  426. {
  427. Width = videoStream.Width.Value,
  428. Height = videoStream.Height.Value
  429. };
  430. double? maxWidth = MaxWidth.HasValue ? (double)MaxWidth.Value : (double?)null;
  431. double? maxHeight = MaxHeight.HasValue ? (double)MaxHeight.Value : (double?)null;
  432. ImageSize newSize = DrawingUtils.Resize(size,
  433. null,
  434. null,
  435. maxWidth,
  436. maxHeight);
  437. return Convert.ToInt32(newSize.Width);
  438. }
  439. return MaxWidth;
  440. }
  441. }
  442. public int? TargetHeight
  443. {
  444. get
  445. {
  446. MediaStream videoStream = TargetVideoStream;
  447. if (videoStream != null && videoStream.Width.HasValue && videoStream.Height.HasValue)
  448. {
  449. ImageSize size = new ImageSize
  450. {
  451. Width = videoStream.Width.Value,
  452. Height = videoStream.Height.Value
  453. };
  454. double? maxWidth = MaxWidth.HasValue ? (double)MaxWidth.Value : (double?)null;
  455. double? maxHeight = MaxHeight.HasValue ? (double)MaxHeight.Value : (double?)null;
  456. ImageSize newSize = DrawingUtils.Resize(size,
  457. null,
  458. null,
  459. maxWidth,
  460. maxHeight);
  461. return Convert.ToInt32(newSize.Height);
  462. }
  463. return MaxHeight;
  464. }
  465. }
  466. }
  467. }