StreamInfo.cs 26 KB

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