StreamInfo.cs 30 KB

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