StreamInfo.cs 31 KB

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