StreamInfo.cs 27 KB

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