StreamInfo.cs 27 KB

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