StreamInfo.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770
  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 bool CopyTimestamps { get; set; }
  28. public string AudioCodec { get; set; }
  29. public int? AudioStreamIndex { get; set; }
  30. public int? SubtitleStreamIndex { get; set; }
  31. public int? MaxAudioChannels { get; set; }
  32. public int? AudioBitrate { get; set; }
  33. public int? VideoBitrate { get; set; }
  34. public int? VideoLevel { get; set; }
  35. public int? MaxWidth { get; set; }
  36. public int? MaxHeight { get; set; }
  37. public int? MaxVideoBitDepth { get; set; }
  38. public int? MaxRefFrames { get; set; }
  39. public float? MaxFramerate { get; set; }
  40. public DeviceProfile DeviceProfile { get; set; }
  41. public string DeviceProfileId { get; set; }
  42. public string DeviceId { get; set; }
  43. public long? RunTimeTicks { get; set; }
  44. public TranscodeSeekInfo TranscodeSeekInfo { get; set; }
  45. public bool EstimateContentLength { get; set; }
  46. public MediaSourceInfo MediaSource { get; set; }
  47. public SubtitleDeliveryMethod SubtitleDeliveryMethod { get; set; }
  48. public string SubtitleFormat { get; set; }
  49. public string PlaySessionId { get; set; }
  50. public List<MediaSourceInfo> AllMediaSources { get; set; }
  51. public string MediaSourceId
  52. {
  53. get
  54. {
  55. return MediaSource == null ? null : MediaSource.Id;
  56. }
  57. }
  58. public bool IsDirectStream
  59. {
  60. get
  61. {
  62. return PlayMethod == PlayMethod.DirectStream ||
  63. PlayMethod == PlayMethod.DirectPlay;
  64. }
  65. }
  66. public string ToUrl(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. List<string> list = new List<string>();
  77. foreach (NameValuePair pair in BuildParams(this, accessToken, false))
  78. {
  79. if (string.IsNullOrEmpty(pair.Value))
  80. {
  81. continue;
  82. }
  83. // Try to keep the url clean by omitting defaults
  84. if (StringHelper.EqualsIgnoreCase(pair.Name, "StartTimeTicks") &&
  85. StringHelper.EqualsIgnoreCase(pair.Value, "0"))
  86. {
  87. continue;
  88. }
  89. if (StringHelper.EqualsIgnoreCase(pair.Name, "SubtitleStreamIndex") &&
  90. StringHelper.EqualsIgnoreCase(pair.Value, "-1"))
  91. {
  92. continue;
  93. }
  94. if (StringHelper.EqualsIgnoreCase(pair.Name, "Static") &&
  95. StringHelper.EqualsIgnoreCase(pair.Value, "false"))
  96. {
  97. continue;
  98. }
  99. list.Add(string.Format("{0}={1}", pair.Name, pair.Value));
  100. }
  101. string queryString = string.Join("&", list.ToArray());
  102. return GetUrl(baseUrl, queryString);
  103. }
  104. public string ToDlnaUrl(string baseUrl, string accessToken)
  105. {
  106. if (PlayMethod == PlayMethod.DirectPlay)
  107. {
  108. return MediaSource.Path;
  109. }
  110. string dlnaCommand = BuildDlnaParam(this, accessToken);
  111. return GetUrl(baseUrl, dlnaCommand);
  112. }
  113. private string GetUrl(string baseUrl, string queryString)
  114. {
  115. if (string.IsNullOrEmpty(baseUrl))
  116. {
  117. throw new ArgumentNullException(baseUrl);
  118. }
  119. string extension = string.IsNullOrEmpty(Container) ? string.Empty : "." + Container;
  120. baseUrl = baseUrl.TrimEnd('/');
  121. if (MediaType == DlnaProfileType.Audio)
  122. {
  123. if (StringHelper.EqualsIgnoreCase(SubProtocol, "hls"))
  124. {
  125. return string.Format("{0}/audio/{1}/master.m3u8?{2}", baseUrl, ItemId, queryString);
  126. }
  127. return string.Format("{0}/audio/{1}/stream{2}?{3}", baseUrl, ItemId, extension, queryString);
  128. }
  129. if (StringHelper.EqualsIgnoreCase(SubProtocol, "hls"))
  130. {
  131. return string.Format("{0}/videos/{1}/master.m3u8?{2}", baseUrl, ItemId, queryString);
  132. }
  133. return string.Format("{0}/videos/{1}/stream{2}?{3}", baseUrl, ItemId, extension, queryString);
  134. }
  135. private static string BuildDlnaParam(StreamInfo item, string accessToken)
  136. {
  137. List<string> list = new List<string>();
  138. foreach (NameValuePair pair in BuildParams(item, accessToken, true))
  139. {
  140. list.Add(pair.Value);
  141. }
  142. return string.Format("Params={0}", string.Join(";", list.ToArray()));
  143. }
  144. private static List<NameValuePair> BuildParams(StreamInfo item, string accessToken, bool isDlna)
  145. {
  146. List<NameValuePair> list = new List<NameValuePair>();
  147. list.Add(new NameValuePair("DeviceProfileId", item.DeviceProfileId ?? string.Empty));
  148. list.Add(new NameValuePair("DeviceId", item.DeviceId ?? string.Empty));
  149. list.Add(new NameValuePair("MediaSourceId", item.MediaSourceId ?? string.Empty));
  150. list.Add(new NameValuePair("Static", (item.IsDirectStream).ToString().ToLower()));
  151. list.Add(new NameValuePair("VideoCodec", item.VideoCodec ?? string.Empty));
  152. list.Add(new NameValuePair("AudioCodec", item.AudioCodec ?? string.Empty));
  153. list.Add(new NameValuePair("AudioStreamIndex", item.AudioStreamIndex.HasValue ? StringHelper.ToStringCultureInvariant(item.AudioStreamIndex.Value) : string.Empty));
  154. list.Add(new NameValuePair("SubtitleStreamIndex", item.SubtitleStreamIndex.HasValue && item.SubtitleDeliveryMethod != SubtitleDeliveryMethod.External ? StringHelper.ToStringCultureInvariant(item.SubtitleStreamIndex.Value) : string.Empty));
  155. list.Add(new NameValuePair("VideoBitrate", item.VideoBitrate.HasValue ? StringHelper.ToStringCultureInvariant(item.VideoBitrate.Value) : string.Empty));
  156. list.Add(new NameValuePair("AudioBitrate", item.AudioBitrate.HasValue ? StringHelper.ToStringCultureInvariant(item.AudioBitrate.Value) : string.Empty));
  157. list.Add(new NameValuePair("MaxAudioChannels", item.MaxAudioChannels.HasValue ? StringHelper.ToStringCultureInvariant(item.MaxAudioChannels.Value) : string.Empty));
  158. list.Add(new NameValuePair("MaxFramerate", item.MaxFramerate.HasValue ? StringHelper.ToStringCultureInvariant(item.MaxFramerate.Value) : string.Empty));
  159. list.Add(new NameValuePair("MaxWidth", item.MaxWidth.HasValue ? StringHelper.ToStringCultureInvariant(item.MaxWidth.Value) : string.Empty));
  160. list.Add(new NameValuePair("MaxHeight", item.MaxHeight.HasValue ? StringHelper.ToStringCultureInvariant(item.MaxHeight.Value) : string.Empty));
  161. if (StringHelper.EqualsIgnoreCase(item.SubProtocol, "hls"))
  162. {
  163. list.Add(new NameValuePair("StartTimeTicks", string.Empty));
  164. }
  165. else
  166. {
  167. list.Add(new NameValuePair("StartTimeTicks", StringHelper.ToStringCultureInvariant(item.StartPositionTicks)));
  168. }
  169. list.Add(new NameValuePair("Level", item.VideoLevel.HasValue ? StringHelper.ToStringCultureInvariant(item.VideoLevel.Value) : string.Empty));
  170. list.Add(new NameValuePair("MaxRefFrames", item.MaxRefFrames.HasValue ? StringHelper.ToStringCultureInvariant(item.MaxRefFrames.Value) : string.Empty));
  171. list.Add(new NameValuePair("MaxVideoBitDepth", item.MaxVideoBitDepth.HasValue ? StringHelper.ToStringCultureInvariant(item.MaxVideoBitDepth.Value) : string.Empty));
  172. list.Add(new NameValuePair("Profile", item.VideoProfile ?? string.Empty));
  173. list.Add(new NameValuePair("Cabac", item.Cabac.HasValue ? item.Cabac.Value.ToString() : string.Empty));
  174. list.Add(new NameValuePair("PlaySessionId", item.PlaySessionId ?? string.Empty));
  175. list.Add(new NameValuePair("api_key", accessToken ?? string.Empty));
  176. string liveStreamId = item.MediaSource == null ? null : item.MediaSource.LiveStreamId;
  177. list.Add(new NameValuePair("LiveStreamId", liveStreamId ?? string.Empty));
  178. if (isDlna)
  179. {
  180. list.Add(new NameValuePair("ItemId", item.ItemId));
  181. }
  182. list.Add(new NameValuePair("CopyTimestamps", (item.CopyTimestamps).ToString().ToLower()));
  183. list.Add(new NameValuePair("SubtitleMethod", item.SubtitleStreamIndex.HasValue && item.SubtitleDeliveryMethod != SubtitleDeliveryMethod.External ? item.SubtitleDeliveryMethod.ToString() : string.Empty));
  184. return list;
  185. }
  186. public List<SubtitleStreamInfo> GetExternalSubtitles(bool includeSelectedTrackOnly, string baseUrl, string accessToken)
  187. {
  188. return GetExternalSubtitles(includeSelectedTrackOnly, false, baseUrl, accessToken);
  189. }
  190. public List<SubtitleStreamInfo> GetExternalSubtitles(bool includeSelectedTrackOnly, bool enableAllProfiles, string baseUrl, string accessToken)
  191. {
  192. List<SubtitleStreamInfo> list = GetSubtitleProfiles(includeSelectedTrackOnly, enableAllProfiles, baseUrl, accessToken);
  193. List<SubtitleStreamInfo> newList = new List<SubtitleStreamInfo>();
  194. // First add the selected track
  195. foreach (SubtitleStreamInfo stream in list)
  196. {
  197. if (stream.DeliveryMethod == SubtitleDeliveryMethod.External)
  198. {
  199. newList.Add(stream);
  200. }
  201. }
  202. return newList;
  203. }
  204. public List<SubtitleStreamInfo> GetSubtitleProfiles(bool includeSelectedTrackOnly, string baseUrl, string accessToken)
  205. {
  206. return GetSubtitleProfiles(includeSelectedTrackOnly, false, baseUrl, accessToken);
  207. }
  208. public List<SubtitleStreamInfo> GetSubtitleProfiles(bool includeSelectedTrackOnly, bool enableAllProfiles, string baseUrl, string accessToken)
  209. {
  210. List<SubtitleStreamInfo> list = new List<SubtitleStreamInfo>();
  211. // HLS will preserve timestamps so we can just grab the full subtitle stream
  212. long startPositionTicks = StringHelper.EqualsIgnoreCase(SubProtocol, "hls")
  213. ? 0
  214. : (PlayMethod == PlayMethod.Transcode && !CopyTimestamps ? StartPositionTicks : 0);
  215. // First add the selected track
  216. if (SubtitleStreamIndex.HasValue)
  217. {
  218. foreach (MediaStream stream in MediaSource.MediaStreams)
  219. {
  220. if (stream.Type == MediaStreamType.Subtitle && stream.Index == SubtitleStreamIndex.Value)
  221. {
  222. AddSubtitleProfiles(list, stream, enableAllProfiles, baseUrl, accessToken, startPositionTicks);
  223. }
  224. }
  225. }
  226. if (!includeSelectedTrackOnly)
  227. {
  228. foreach (MediaStream stream in MediaSource.MediaStreams)
  229. {
  230. if (stream.Type == MediaStreamType.Subtitle && (!SubtitleStreamIndex.HasValue || stream.Index != SubtitleStreamIndex.Value))
  231. {
  232. AddSubtitleProfiles(list, stream, enableAllProfiles, baseUrl, accessToken, startPositionTicks);
  233. }
  234. }
  235. }
  236. return list;
  237. }
  238. private void AddSubtitleProfiles(List<SubtitleStreamInfo> list, MediaStream stream, bool enableAllProfiles, string baseUrl, string accessToken, long startPositionTicks)
  239. {
  240. if (enableAllProfiles)
  241. {
  242. foreach (SubtitleProfile profile in DeviceProfile.SubtitleProfiles)
  243. {
  244. SubtitleStreamInfo info = GetSubtitleStreamInfo(stream, baseUrl, accessToken, startPositionTicks, new[] { profile });
  245. list.Add(info);
  246. }
  247. }
  248. else
  249. {
  250. SubtitleStreamInfo info = GetSubtitleStreamInfo(stream, baseUrl, accessToken, startPositionTicks, DeviceProfile.SubtitleProfiles);
  251. list.Add(info);
  252. }
  253. }
  254. private SubtitleStreamInfo GetSubtitleStreamInfo(MediaStream stream, string baseUrl, string accessToken, long startPositionTicks, SubtitleProfile[] subtitleProfiles)
  255. {
  256. SubtitleProfile subtitleProfile = StreamBuilder.GetSubtitleProfile(stream, subtitleProfiles, Context, PlayMethod);
  257. SubtitleStreamInfo info = new SubtitleStreamInfo
  258. {
  259. IsForced = stream.IsForced,
  260. Language = stream.Language,
  261. Name = stream.Language ?? "Unknown",
  262. Format = subtitleProfile.Format,
  263. Index = stream.Index,
  264. DeliveryMethod = subtitleProfile.Method
  265. };
  266. if (info.DeliveryMethod == SubtitleDeliveryMethod.External)
  267. {
  268. if (MediaSource.Protocol == MediaProtocol.File || !StringHelper.EqualsIgnoreCase(stream.Codec, subtitleProfile.Format))
  269. {
  270. info.Url = string.Format("{0}/Videos/{1}/{2}/Subtitles/{3}/{4}/Stream.{5}",
  271. baseUrl,
  272. ItemId,
  273. MediaSourceId,
  274. StringHelper.ToStringCultureInvariant(stream.Index),
  275. StringHelper.ToStringCultureInvariant(startPositionTicks),
  276. subtitleProfile.Format);
  277. if (!string.IsNullOrEmpty(accessToken))
  278. {
  279. info.Url += "?api_key=" + accessToken;
  280. }
  281. info.IsExternalUrl = false;
  282. }
  283. else
  284. {
  285. info.Url = stream.Path;
  286. info.IsExternalUrl = true;
  287. }
  288. }
  289. return info;
  290. }
  291. /// <summary>
  292. /// Returns the audio stream that will be used
  293. /// </summary>
  294. public MediaStream TargetAudioStream
  295. {
  296. get
  297. {
  298. if (MediaSource != null)
  299. {
  300. return MediaSource.GetDefaultAudioStream(AudioStreamIndex);
  301. }
  302. return null;
  303. }
  304. }
  305. /// <summary>
  306. /// Returns the video stream that will be used
  307. /// </summary>
  308. public MediaStream TargetVideoStream
  309. {
  310. get
  311. {
  312. if (MediaSource != null)
  313. {
  314. return MediaSource.VideoStream;
  315. }
  316. return null;
  317. }
  318. }
  319. /// <summary>
  320. /// Predicts the audio sample rate that will be in the output stream
  321. /// </summary>
  322. public int? TargetAudioSampleRate
  323. {
  324. get
  325. {
  326. MediaStream stream = TargetAudioStream;
  327. return stream == null ? null : stream.SampleRate;
  328. }
  329. }
  330. /// <summary>
  331. /// Predicts the audio sample rate that will be in the output stream
  332. /// </summary>
  333. public int? TargetVideoBitDepth
  334. {
  335. get
  336. {
  337. MediaStream stream = TargetVideoStream;
  338. return stream == null || !IsDirectStream ? null : stream.BitDepth;
  339. }
  340. }
  341. /// <summary>
  342. /// Gets the target reference frames.
  343. /// </summary>
  344. /// <value>The target reference frames.</value>
  345. public int? TargetRefFrames
  346. {
  347. get
  348. {
  349. MediaStream stream = TargetVideoStream;
  350. return stream == null || !IsDirectStream ? null : stream.RefFrames;
  351. }
  352. }
  353. /// <summary>
  354. /// Predicts the audio sample rate that will be in the output stream
  355. /// </summary>
  356. public float? TargetFramerate
  357. {
  358. get
  359. {
  360. MediaStream stream = TargetVideoStream;
  361. return MaxFramerate.HasValue && !IsDirectStream
  362. ? MaxFramerate
  363. : stream == null ? null : stream.AverageFrameRate ?? stream.RealFrameRate;
  364. }
  365. }
  366. /// <summary>
  367. /// Predicts the audio sample rate that will be in the output stream
  368. /// </summary>
  369. public double? TargetVideoLevel
  370. {
  371. get
  372. {
  373. MediaStream stream = TargetVideoStream;
  374. return VideoLevel.HasValue && !IsDirectStream
  375. ? VideoLevel
  376. : stream == null ? null : stream.Level;
  377. }
  378. }
  379. /// <summary>
  380. /// Predicts the audio sample rate that will be in the output stream
  381. /// </summary>
  382. public int? TargetPacketLength
  383. {
  384. get
  385. {
  386. MediaStream stream = TargetVideoStream;
  387. return !IsDirectStream
  388. ? null
  389. : stream == null ? null : stream.PacketLength;
  390. }
  391. }
  392. /// <summary>
  393. /// Predicts the audio sample rate that will be in the output stream
  394. /// </summary>
  395. public string TargetVideoProfile
  396. {
  397. get
  398. {
  399. MediaStream stream = TargetVideoStream;
  400. return !string.IsNullOrEmpty(VideoProfile) && !IsDirectStream
  401. ? VideoProfile
  402. : stream == null ? null : stream.Profile;
  403. }
  404. }
  405. /// <summary>
  406. /// Gets the target video codec tag.
  407. /// </summary>
  408. /// <value>The target video codec tag.</value>
  409. public string TargetVideoCodecTag
  410. {
  411. get
  412. {
  413. MediaStream stream = TargetVideoStream;
  414. return !IsDirectStream
  415. ? null
  416. : stream == null ? null : stream.CodecTag;
  417. }
  418. }
  419. /// <summary>
  420. /// Predicts the audio bitrate that will be in the output stream
  421. /// </summary>
  422. public int? TargetAudioBitrate
  423. {
  424. get
  425. {
  426. MediaStream stream = TargetAudioStream;
  427. return AudioBitrate.HasValue && !IsDirectStream
  428. ? AudioBitrate
  429. : stream == null ? null : stream.BitRate;
  430. }
  431. }
  432. /// <summary>
  433. /// Predicts the audio channels that will be in the output stream
  434. /// </summary>
  435. public int? TargetAudioChannels
  436. {
  437. get
  438. {
  439. MediaStream stream = TargetAudioStream;
  440. int? streamChannels = stream == null ? null : stream.Channels;
  441. if (MaxAudioChannels.HasValue && !IsDirectStream)
  442. {
  443. if (streamChannels.HasValue)
  444. {
  445. return Math.Min(MaxAudioChannels.Value, streamChannels.Value);
  446. }
  447. return MaxAudioChannels.Value;
  448. }
  449. return streamChannels;
  450. }
  451. }
  452. /// <summary>
  453. /// Predicts the audio codec that will be in the output stream
  454. /// </summary>
  455. public string TargetAudioCodec
  456. {
  457. get
  458. {
  459. MediaStream stream = TargetAudioStream;
  460. return IsDirectStream
  461. ? (stream == null ? null : stream.Codec)
  462. : AudioCodec;
  463. }
  464. }
  465. /// <summary>
  466. /// Predicts the audio channels that will be in the output stream
  467. /// </summary>
  468. public long? TargetSize
  469. {
  470. get
  471. {
  472. if (IsDirectStream)
  473. {
  474. return MediaSource.Size;
  475. }
  476. if (RunTimeTicks.HasValue)
  477. {
  478. int? totalBitrate = TargetTotalBitrate;
  479. double totalSeconds = RunTimeTicks.Value;
  480. // Convert to ms
  481. totalSeconds /= 10000;
  482. // Convert to seconds
  483. totalSeconds /= 1000;
  484. return totalBitrate.HasValue ?
  485. Convert.ToInt64(totalBitrate.Value * totalSeconds) :
  486. (long?)null;
  487. }
  488. return null;
  489. }
  490. }
  491. public int? TargetVideoBitrate
  492. {
  493. get
  494. {
  495. MediaStream stream = TargetVideoStream;
  496. return VideoBitrate.HasValue && !IsDirectStream
  497. ? VideoBitrate
  498. : stream == null ? null : stream.BitRate;
  499. }
  500. }
  501. public TransportStreamTimestamp TargetTimestamp
  502. {
  503. get
  504. {
  505. TransportStreamTimestamp defaultValue = StringHelper.EqualsIgnoreCase(Container, "m2ts")
  506. ? TransportStreamTimestamp.Valid
  507. : TransportStreamTimestamp.None;
  508. return !IsDirectStream
  509. ? defaultValue
  510. : MediaSource == null ? defaultValue : MediaSource.Timestamp ?? TransportStreamTimestamp.None;
  511. }
  512. }
  513. public int? TargetTotalBitrate
  514. {
  515. get
  516. {
  517. return (TargetAudioBitrate ?? 0) + (TargetVideoBitrate ?? 0);
  518. }
  519. }
  520. public bool? IsTargetAnamorphic
  521. {
  522. get
  523. {
  524. if (IsDirectStream)
  525. {
  526. return TargetVideoStream == null ? null : TargetVideoStream.IsAnamorphic;
  527. }
  528. return false;
  529. }
  530. }
  531. public bool? IsTargetCabac
  532. {
  533. get
  534. {
  535. if (IsDirectStream)
  536. {
  537. return TargetVideoStream == null ? null : TargetVideoStream.IsCabac;
  538. }
  539. return true;
  540. }
  541. }
  542. public int? TargetWidth
  543. {
  544. get
  545. {
  546. MediaStream videoStream = TargetVideoStream;
  547. if (videoStream != null && videoStream.Width.HasValue && videoStream.Height.HasValue)
  548. {
  549. ImageSize size = new ImageSize
  550. {
  551. Width = videoStream.Width.Value,
  552. Height = videoStream.Height.Value
  553. };
  554. double? maxWidth = MaxWidth.HasValue ? (double)MaxWidth.Value : (double?)null;
  555. double? maxHeight = MaxHeight.HasValue ? (double)MaxHeight.Value : (double?)null;
  556. ImageSize newSize = DrawingUtils.Resize(size,
  557. null,
  558. null,
  559. maxWidth,
  560. maxHeight);
  561. return Convert.ToInt32(newSize.Width);
  562. }
  563. return MaxWidth;
  564. }
  565. }
  566. public int? TargetHeight
  567. {
  568. get
  569. {
  570. MediaStream videoStream = TargetVideoStream;
  571. if (videoStream != null && videoStream.Width.HasValue && videoStream.Height.HasValue)
  572. {
  573. ImageSize size = new ImageSize
  574. {
  575. Width = videoStream.Width.Value,
  576. Height = videoStream.Height.Value
  577. };
  578. double? maxWidth = MaxWidth.HasValue ? (double)MaxWidth.Value : (double?)null;
  579. double? maxHeight = MaxHeight.HasValue ? (double)MaxHeight.Value : (double?)null;
  580. ImageSize newSize = DrawingUtils.Resize(size,
  581. null,
  582. null,
  583. maxWidth,
  584. maxHeight);
  585. return Convert.ToInt32(newSize.Height);
  586. }
  587. return MaxHeight;
  588. }
  589. }
  590. public int? TargetVideoStreamCount
  591. {
  592. get
  593. {
  594. if (IsDirectStream)
  595. {
  596. return GetMediaStreamCount(MediaStreamType.Video, int.MaxValue);
  597. }
  598. return GetMediaStreamCount(MediaStreamType.Video, 1);
  599. }
  600. }
  601. public int? TargetAudioStreamCount
  602. {
  603. get
  604. {
  605. if (IsDirectStream)
  606. {
  607. return GetMediaStreamCount(MediaStreamType.Audio, int.MaxValue);
  608. }
  609. return GetMediaStreamCount(MediaStreamType.Audio, 1);
  610. }
  611. }
  612. private int? GetMediaStreamCount(MediaStreamType type, int limit)
  613. {
  614. var count = MediaSource.GetStreamCount(type);
  615. if (count.HasValue)
  616. {
  617. count = Math.Min(count.Value, limit);
  618. }
  619. return count;
  620. }
  621. public List<MediaStream> GetSelectableAudioStreams()
  622. {
  623. return GetSelectableStreams(MediaStreamType.Audio);
  624. }
  625. public List<MediaStream> GetSelectableSubtitleStreams()
  626. {
  627. return GetSelectableStreams(MediaStreamType.Subtitle);
  628. }
  629. public List<MediaStream> GetSelectableStreams(MediaStreamType type)
  630. {
  631. List<MediaStream> list = new List<MediaStream>();
  632. foreach (MediaStream stream in MediaSource.MediaStreams)
  633. {
  634. if (type == stream.Type)
  635. {
  636. list.Add(stream);
  637. }
  638. }
  639. return list;
  640. }
  641. }
  642. }