StreamInfo.cs 31 KB

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