StreamInfo.cs 28 KB

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