StreamInfo.cs 25 KB

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