StreamInfo.cs 29 KB

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