StreamInfo.cs 20 KB

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