2
0

StreamInfo.cs 31 KB

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