StreamInfo.cs 21 KB

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