StreamInfo.cs 23 KB

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