StreamInfo.cs 28 KB

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