StreamInfo.cs 28 KB

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