StreamInfo.cs 21 KB

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