StreamInfo.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720
  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. {
  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. list.Add(new NameValuePair("StartTimeTicks", StringHelper.ToStringCultureInvariant(item.StartPositionTicks)));
  156. list.Add(new NameValuePair("Level", item.VideoLevel.HasValue ? StringHelper.ToStringCultureInvariant(item.VideoLevel.Value) : string.Empty));
  157. list.Add(new NameValuePair("ClientTime", item.IsDirectStream ? string.Empty : DateTime.UtcNow.Ticks.ToString(CultureInfo.InvariantCulture)));
  158. list.Add(new NameValuePair("MaxRefFrames", item.MaxRefFrames.HasValue ? StringHelper.ToStringCultureInvariant(item.MaxRefFrames.Value) : string.Empty));
  159. list.Add(new NameValuePair("MaxVideoBitDepth", item.MaxVideoBitDepth.HasValue ? StringHelper.ToStringCultureInvariant(item.MaxVideoBitDepth.Value) : string.Empty));
  160. list.Add(new NameValuePair("Profile", item.VideoProfile ?? string.Empty));
  161. list.Add(new NameValuePair("Cabac", item.Cabac.HasValue ? item.Cabac.Value.ToString() : string.Empty));
  162. string playSessionId = item.PlaybackInfo == null ? null : item.PlaybackInfo.PlaySessionId;
  163. list.Add(new NameValuePair("PlaySessionId", playSessionId ?? string.Empty));
  164. list.Add(new NameValuePair("api_key", accessToken ?? string.Empty));
  165. string liveStreamId = item.MediaSource == null ? null : item.MediaSource.LiveStreamId;
  166. list.Add(new NameValuePair("LiveStreamId", liveStreamId ?? string.Empty));
  167. return list;
  168. }
  169. public List<SubtitleStreamInfo> GetExternalSubtitles(bool includeSelectedTrackOnly, bool enableAllProfiles, string baseUrl, string accessToken)
  170. {
  171. List<SubtitleStreamInfo> list = GetSubtitleProfiles(includeSelectedTrackOnly, enableAllProfiles, baseUrl, accessToken);
  172. List<SubtitleStreamInfo> newList = new List<SubtitleStreamInfo>();
  173. // First add the selected track
  174. foreach (SubtitleStreamInfo stream in list)
  175. {
  176. if (stream.DeliveryMethod == SubtitleDeliveryMethod.External)
  177. {
  178. newList.Add(stream);
  179. }
  180. }
  181. return newList;
  182. }
  183. public List<SubtitleStreamInfo> GetSubtitleProfiles(bool includeSelectedTrackOnly, string baseUrl, string accessToken)
  184. {
  185. return GetSubtitleProfiles(includeSelectedTrackOnly, false, baseUrl, accessToken);
  186. }
  187. public List<SubtitleStreamInfo> GetSubtitleProfiles(bool includeSelectedTrackOnly, bool enableAllProfiles, string baseUrl, string accessToken)
  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. AddSubtitleProfiles(list, stream, enableAllProfiles, baseUrl, accessToken, startPositionTicks);
  202. }
  203. }
  204. }
  205. if (!includeSelectedTrackOnly)
  206. {
  207. foreach (MediaStream stream in MediaSource.MediaStreams)
  208. {
  209. if (stream.Type == MediaStreamType.Subtitle && (!SubtitleStreamIndex.HasValue || stream.Index != SubtitleStreamIndex.Value))
  210. {
  211. AddSubtitleProfiles(list, stream, enableAllProfiles, baseUrl, accessToken, startPositionTicks);
  212. }
  213. }
  214. }
  215. return list;
  216. }
  217. private void AddSubtitleProfiles(List<SubtitleStreamInfo> list, MediaStream stream, bool enableAllProfiles, string baseUrl, string accessToken, long startPositionTicks)
  218. {
  219. if (enableAllProfiles)
  220. {
  221. foreach (SubtitleProfile profile in DeviceProfile.SubtitleProfiles)
  222. {
  223. SubtitleStreamInfo info = GetSubtitleStreamInfo(stream, baseUrl, accessToken, startPositionTicks, new[] { profile });
  224. list.Add(info);
  225. }
  226. }
  227. else
  228. {
  229. SubtitleStreamInfo info = GetSubtitleStreamInfo(stream, baseUrl, accessToken, startPositionTicks, DeviceProfile.SubtitleProfiles);
  230. list.Add(info);
  231. }
  232. }
  233. private SubtitleStreamInfo GetSubtitleStreamInfo(MediaStream stream, string baseUrl, string accessToken, long startPositionTicks, SubtitleProfile[] subtitleProfiles)
  234. {
  235. SubtitleProfile subtitleProfile = StreamBuilder.GetSubtitleProfile(stream, subtitleProfiles, Context);
  236. SubtitleStreamInfo info = new SubtitleStreamInfo
  237. {
  238. IsForced = stream.IsForced,
  239. Language = stream.Language,
  240. Name = stream.Language ?? "Unknown",
  241. Format = subtitleProfile.Format,
  242. Index = stream.Index,
  243. DeliveryMethod = subtitleProfile.Method
  244. };
  245. if (info.DeliveryMethod == SubtitleDeliveryMethod.External)
  246. {
  247. if (MediaSource.Protocol == MediaProtocol.File || !StringHelper.EqualsIgnoreCase(stream.Codec, subtitleProfile.Format))
  248. {
  249. info.Url = string.Format("{0}/Videos/{1}/{2}/Subtitles/{3}/{4}/Stream.{5}",
  250. baseUrl,
  251. ItemId,
  252. MediaSourceId,
  253. StringHelper.ToStringCultureInvariant(stream.Index),
  254. StringHelper.ToStringCultureInvariant(startPositionTicks),
  255. subtitleProfile.Format);
  256. }
  257. else
  258. {
  259. info.Url = stream.Path;
  260. }
  261. }
  262. return info;
  263. }
  264. /// <summary>
  265. /// Returns the audio stream that will be used
  266. /// </summary>
  267. public MediaStream TargetAudioStream
  268. {
  269. get
  270. {
  271. if (MediaSource != null)
  272. {
  273. return MediaSource.GetDefaultAudioStream(AudioStreamIndex);
  274. }
  275. return null;
  276. }
  277. }
  278. /// <summary>
  279. /// Returns the video stream that will be used
  280. /// </summary>
  281. public MediaStream TargetVideoStream
  282. {
  283. get
  284. {
  285. if (MediaSource != null)
  286. {
  287. return MediaSource.VideoStream;
  288. }
  289. return null;
  290. }
  291. }
  292. /// <summary>
  293. /// Predicts the audio sample rate that will be in the output stream
  294. /// </summary>
  295. public int? TargetAudioSampleRate
  296. {
  297. get
  298. {
  299. MediaStream stream = TargetAudioStream;
  300. return stream == null ? null : stream.SampleRate;
  301. }
  302. }
  303. /// <summary>
  304. /// Predicts the audio sample rate that will be in the output stream
  305. /// </summary>
  306. public int? TargetVideoBitDepth
  307. {
  308. get
  309. {
  310. MediaStream stream = TargetVideoStream;
  311. return stream == null || !IsDirectStream ? null : stream.BitDepth;
  312. }
  313. }
  314. /// <summary>
  315. /// Gets the target reference frames.
  316. /// </summary>
  317. /// <value>The target reference frames.</value>
  318. public int? TargetRefFrames
  319. {
  320. get
  321. {
  322. MediaStream stream = TargetVideoStream;
  323. return stream == null || !IsDirectStream ? null : stream.RefFrames;
  324. }
  325. }
  326. /// <summary>
  327. /// Predicts the audio sample rate that will be in the output stream
  328. /// </summary>
  329. public float? TargetFramerate
  330. {
  331. get
  332. {
  333. MediaStream stream = TargetVideoStream;
  334. return MaxFramerate.HasValue && !IsDirectStream
  335. ? MaxFramerate
  336. : stream == null ? null : stream.AverageFrameRate ?? stream.RealFrameRate;
  337. }
  338. }
  339. /// <summary>
  340. /// Predicts the audio sample rate that will be in the output stream
  341. /// </summary>
  342. public double? TargetVideoLevel
  343. {
  344. get
  345. {
  346. MediaStream stream = TargetVideoStream;
  347. return VideoLevel.HasValue && !IsDirectStream
  348. ? VideoLevel
  349. : stream == null ? null : stream.Level;
  350. }
  351. }
  352. /// <summary>
  353. /// Predicts the audio sample rate that will be in the output stream
  354. /// </summary>
  355. public int? TargetPacketLength
  356. {
  357. get
  358. {
  359. MediaStream stream = TargetVideoStream;
  360. return !IsDirectStream
  361. ? null
  362. : stream == null ? null : stream.PacketLength;
  363. }
  364. }
  365. /// <summary>
  366. /// Predicts the audio sample rate that will be in the output stream
  367. /// </summary>
  368. public string TargetVideoProfile
  369. {
  370. get
  371. {
  372. MediaStream stream = TargetVideoStream;
  373. return !string.IsNullOrEmpty(VideoProfile) && !IsDirectStream
  374. ? VideoProfile
  375. : stream == null ? null : stream.Profile;
  376. }
  377. }
  378. /// <summary>
  379. /// Predicts the audio bitrate that will be in the output stream
  380. /// </summary>
  381. public int? TargetAudioBitrate
  382. {
  383. get
  384. {
  385. MediaStream stream = TargetAudioStream;
  386. return AudioBitrate.HasValue && !IsDirectStream
  387. ? AudioBitrate
  388. : stream == null ? null : stream.BitRate;
  389. }
  390. }
  391. /// <summary>
  392. /// Predicts the audio channels that will be in the output stream
  393. /// </summary>
  394. public int? TargetAudioChannels
  395. {
  396. get
  397. {
  398. MediaStream stream = TargetAudioStream;
  399. int? streamChannels = stream == null ? null : stream.Channels;
  400. if (MaxAudioChannels.HasValue && !IsDirectStream)
  401. {
  402. if (streamChannels.HasValue)
  403. {
  404. return Math.Min(MaxAudioChannels.Value, streamChannels.Value);
  405. }
  406. return MaxAudioChannels.Value;
  407. }
  408. return streamChannels;
  409. }
  410. }
  411. /// <summary>
  412. /// Predicts the audio codec that will be in the output stream
  413. /// </summary>
  414. public string TargetAudioCodec
  415. {
  416. get
  417. {
  418. MediaStream stream = TargetAudioStream;
  419. return IsDirectStream
  420. ? (stream == null ? null : stream.Codec)
  421. : AudioCodec;
  422. }
  423. }
  424. /// <summary>
  425. /// Predicts the audio channels that will be in the output stream
  426. /// </summary>
  427. public long? TargetSize
  428. {
  429. get
  430. {
  431. if (IsDirectStream)
  432. {
  433. return MediaSource.Size;
  434. }
  435. if (RunTimeTicks.HasValue)
  436. {
  437. int? totalBitrate = TargetTotalBitrate;
  438. double totalSeconds = RunTimeTicks.Value;
  439. // Convert to ms
  440. totalSeconds /= 10000;
  441. // Convert to seconds
  442. totalSeconds /= 1000;
  443. return totalBitrate.HasValue ?
  444. Convert.ToInt64(totalBitrate.Value * totalSeconds) :
  445. (long?)null;
  446. }
  447. return null;
  448. }
  449. }
  450. public int? TargetVideoBitrate
  451. {
  452. get
  453. {
  454. MediaStream stream = TargetVideoStream;
  455. return VideoBitrate.HasValue && !IsDirectStream
  456. ? VideoBitrate
  457. : stream == null ? null : stream.BitRate;
  458. }
  459. }
  460. public TransportStreamTimestamp TargetTimestamp
  461. {
  462. get
  463. {
  464. TransportStreamTimestamp defaultValue = StringHelper.EqualsIgnoreCase(Container, "m2ts")
  465. ? TransportStreamTimestamp.Valid
  466. : TransportStreamTimestamp.None;
  467. return !IsDirectStream
  468. ? defaultValue
  469. : MediaSource == null ? defaultValue : MediaSource.Timestamp ?? TransportStreamTimestamp.None;
  470. }
  471. }
  472. public int? TargetTotalBitrate
  473. {
  474. get
  475. {
  476. return (TargetAudioBitrate ?? 0) + (TargetVideoBitrate ?? 0);
  477. }
  478. }
  479. public bool? IsTargetAnamorphic
  480. {
  481. get
  482. {
  483. if (IsDirectStream)
  484. {
  485. return TargetVideoStream == null ? null : TargetVideoStream.IsAnamorphic;
  486. }
  487. return false;
  488. }
  489. }
  490. public bool? IsTargetCabac
  491. {
  492. get
  493. {
  494. if (IsDirectStream)
  495. {
  496. return TargetVideoStream == null ? null : TargetVideoStream.IsCabac;
  497. }
  498. return true;
  499. }
  500. }
  501. public int? TargetWidth
  502. {
  503. get
  504. {
  505. MediaStream videoStream = TargetVideoStream;
  506. if (videoStream != null && videoStream.Width.HasValue && videoStream.Height.HasValue)
  507. {
  508. ImageSize size = new ImageSize
  509. {
  510. Width = videoStream.Width.Value,
  511. Height = videoStream.Height.Value
  512. };
  513. double? maxWidth = MaxWidth.HasValue ? (double)MaxWidth.Value : (double?)null;
  514. double? maxHeight = MaxHeight.HasValue ? (double)MaxHeight.Value : (double?)null;
  515. ImageSize newSize = DrawingUtils.Resize(size,
  516. null,
  517. null,
  518. maxWidth,
  519. maxHeight);
  520. return Convert.ToInt32(newSize.Width);
  521. }
  522. return MaxWidth;
  523. }
  524. }
  525. public int? TargetHeight
  526. {
  527. get
  528. {
  529. MediaStream videoStream = TargetVideoStream;
  530. if (videoStream != null && videoStream.Width.HasValue && videoStream.Height.HasValue)
  531. {
  532. ImageSize size = new ImageSize
  533. {
  534. Width = videoStream.Width.Value,
  535. Height = videoStream.Height.Value
  536. };
  537. double? maxWidth = MaxWidth.HasValue ? (double)MaxWidth.Value : (double?)null;
  538. double? maxHeight = MaxHeight.HasValue ? (double)MaxHeight.Value : (double?)null;
  539. ImageSize newSize = DrawingUtils.Resize(size,
  540. null,
  541. null,
  542. maxWidth,
  543. maxHeight);
  544. return Convert.ToInt32(newSize.Height);
  545. }
  546. return MaxHeight;
  547. }
  548. }
  549. public int? TargetVideoStreamCount
  550. {
  551. get
  552. {
  553. if (IsDirectStream)
  554. {
  555. return GetMediaStreamCount(MediaStreamType.Video, int.MaxValue);
  556. }
  557. return GetMediaStreamCount(MediaStreamType.Video, 1);
  558. }
  559. }
  560. public int? TargetAudioStreamCount
  561. {
  562. get
  563. {
  564. if (IsDirectStream)
  565. {
  566. return GetMediaStreamCount(MediaStreamType.Audio, int.MaxValue);
  567. }
  568. return GetMediaStreamCount(MediaStreamType.Audio, 1);
  569. }
  570. }
  571. private int? GetMediaStreamCount(MediaStreamType type, int limit)
  572. {
  573. var count = MediaSource.GetStreamCount(type);
  574. if (count.HasValue)
  575. {
  576. count = Math.Min(count.Value, limit);
  577. }
  578. return count;
  579. }
  580. public List<MediaStream> GetSelectableAudioStreams()
  581. {
  582. return GetSelectableStreams(MediaStreamType.Audio);
  583. }
  584. public List<MediaStream> GetSelectableSubtitleStreams()
  585. {
  586. return GetSelectableStreams(MediaStreamType.Subtitle);
  587. }
  588. public List<MediaStream> GetSelectableStreams(MediaStreamType type)
  589. {
  590. List<MediaStream> list = new List<MediaStream>();
  591. foreach (MediaStream stream in MediaSource.MediaStreams)
  592. {
  593. if (type == stream.Type)
  594. {
  595. list.Add(stream);
  596. }
  597. }
  598. return list;
  599. }
  600. }
  601. }