MediaStream.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  1. #nullable disable
  2. #pragma warning disable CS1591
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Globalization;
  6. using System.Linq;
  7. using System.Text;
  8. using MediaBrowser.Model.Dlna;
  9. using MediaBrowser.Model.Extensions;
  10. using MediaBrowser.Model.MediaInfo;
  11. namespace MediaBrowser.Model.Entities
  12. {
  13. /// <summary>
  14. /// Class MediaStream.
  15. /// </summary>
  16. public class MediaStream
  17. {
  18. /// <summary>
  19. /// Gets or sets the codec.
  20. /// </summary>
  21. /// <value>The codec.</value>
  22. public string Codec { get; set; }
  23. /// <summary>
  24. /// Gets or sets the codec tag.
  25. /// </summary>
  26. /// <value>The codec tag.</value>
  27. public string CodecTag { get; set; }
  28. /// <summary>
  29. /// Gets or sets the language.
  30. /// </summary>
  31. /// <value>The language.</value>
  32. public string Language { get; set; }
  33. /// <summary>
  34. /// Gets or sets the color range.
  35. /// </summary>
  36. /// <value>The color range.</value>
  37. public string ColorRange { get; set; }
  38. /// <summary>
  39. /// Gets or sets the color space.
  40. /// </summary>
  41. /// <value>The color space.</value>
  42. public string ColorSpace { get; set; }
  43. /// <summary>
  44. /// Gets or sets the color transfer.
  45. /// </summary>
  46. /// <value>The color transfer.</value>
  47. public string ColorTransfer { get; set; }
  48. /// <summary>
  49. /// Gets or sets the color primaries.
  50. /// </summary>
  51. /// <value>The color primaries.</value>
  52. public string ColorPrimaries { get; set; }
  53. /// <summary>
  54. /// Gets or sets the comment.
  55. /// </summary>
  56. /// <value>The comment.</value>
  57. public string Comment { get; set; }
  58. /// <summary>
  59. /// Gets or sets the time base.
  60. /// </summary>
  61. /// <value>The time base.</value>
  62. public string TimeBase { get; set; }
  63. /// <summary>
  64. /// Gets or sets the codec time base.
  65. /// </summary>
  66. /// <value>The codec time base.</value>
  67. public string CodecTimeBase { get; set; }
  68. /// <summary>
  69. /// Gets or sets the title.
  70. /// </summary>
  71. /// <value>The title.</value>
  72. public string Title { get; set; }
  73. /// <summary>
  74. /// Gets or sets the video range.
  75. /// </summary>
  76. /// <value>The video range.</value>
  77. public string VideoRange
  78. {
  79. get
  80. {
  81. if (Type != MediaStreamType.Video)
  82. {
  83. return null;
  84. }
  85. var colorTransfer = ColorTransfer;
  86. if (string.Equals(colorTransfer, "smpte2084", StringComparison.OrdinalIgnoreCase)
  87. || string.Equals(colorTransfer, "arib-std-b67", StringComparison.OrdinalIgnoreCase))
  88. {
  89. return "HDR";
  90. }
  91. return "SDR";
  92. }
  93. }
  94. public string localizedUndefined { get; set; }
  95. public string localizedDefault { get; set; }
  96. public string localizedForced { get; set; }
  97. public string DisplayTitle
  98. {
  99. get
  100. {
  101. switch (Type)
  102. {
  103. case MediaStreamType.Audio:
  104. {
  105. var attributes = new List<string>();
  106. if (!string.IsNullOrEmpty(Language))
  107. {
  108. // Get full language string i.e. eng -> English. Will not work for some languages which use ISO 639-2/B instead of /T codes.
  109. string fullLanguage = CultureInfo
  110. .GetCultures(CultureTypes.NeutralCultures)
  111. .FirstOrDefault(r => r.ThreeLetterISOLanguageName.Equals(Language, StringComparison.OrdinalIgnoreCase))
  112. ?.DisplayName;
  113. attributes.Add(StringHelper.FirstToUpper(fullLanguage ?? Language));
  114. }
  115. if (!string.IsNullOrEmpty(Codec) && !string.Equals(Codec, "dca", StringComparison.OrdinalIgnoreCase))
  116. {
  117. attributes.Add(AudioCodec.GetFriendlyName(Codec));
  118. }
  119. else if (!string.IsNullOrEmpty(Profile) && !string.Equals(Profile, "lc", StringComparison.OrdinalIgnoreCase))
  120. {
  121. attributes.Add(Profile);
  122. }
  123. if (!string.IsNullOrEmpty(ChannelLayout))
  124. {
  125. attributes.Add(StringHelper.FirstToUpper(ChannelLayout));
  126. }
  127. else if (Channels.HasValue)
  128. {
  129. attributes.Add(Channels.Value.ToString(CultureInfo.InvariantCulture) + " ch");
  130. }
  131. if (IsDefault)
  132. {
  133. attributes.Add(string.IsNullOrEmpty(localizedDefault) ? "Default" : localizedDefault);
  134. }
  135. if (!string.IsNullOrEmpty(Title))
  136. {
  137. var result = new StringBuilder(Title);
  138. foreach (var tag in attributes)
  139. {
  140. // Keep Tags that are not already in Title.
  141. if (Title.IndexOf(tag, StringComparison.OrdinalIgnoreCase) == -1)
  142. {
  143. result.Append(" - ").Append(tag);
  144. }
  145. }
  146. return result.ToString();
  147. }
  148. return string.Join(" - ", attributes);
  149. }
  150. case MediaStreamType.Video:
  151. {
  152. var attributes = new List<string>();
  153. var resolutionText = GetResolutionText();
  154. if (!string.IsNullOrEmpty(resolutionText))
  155. {
  156. attributes.Add(resolutionText);
  157. }
  158. if (!string.IsNullOrEmpty(Codec))
  159. {
  160. attributes.Add(Codec.ToUpperInvariant());
  161. }
  162. if (!string.IsNullOrEmpty(VideoRange))
  163. {
  164. attributes.Add(VideoRange.ToUpperInvariant());
  165. }
  166. if (!string.IsNullOrEmpty(Title))
  167. {
  168. var result = new StringBuilder(Title);
  169. foreach (var tag in attributes)
  170. {
  171. // Keep Tags that are not already in Title.
  172. if (Title.IndexOf(tag, StringComparison.OrdinalIgnoreCase) == -1)
  173. {
  174. result.Append(" - ").Append(tag);
  175. }
  176. }
  177. return result.ToString();
  178. }
  179. return string.Join(" ", attributes);
  180. }
  181. case MediaStreamType.Subtitle:
  182. {
  183. var attributes = new List<string>();
  184. if (!string.IsNullOrEmpty(Language))
  185. {
  186. // Get full language string i.e. eng -> English. Will not work for some languages which use ISO 639-2/B instead of /T codes.
  187. string fullLanguage = CultureInfo
  188. .GetCultures(CultureTypes.NeutralCultures)
  189. .FirstOrDefault(r => r.ThreeLetterISOLanguageName.Equals(Language, StringComparison.OrdinalIgnoreCase))
  190. ?.DisplayName;
  191. attributes.Add(StringHelper.FirstToUpper(fullLanguage ?? Language));
  192. }
  193. else
  194. {
  195. attributes.Add(string.IsNullOrEmpty(localizedUndefined) ? "Und" : localizedUndefined);
  196. }
  197. if (IsDefault)
  198. {
  199. attributes.Add(string.IsNullOrEmpty(localizedDefault) ? "Default" : localizedDefault);
  200. }
  201. if (IsForced)
  202. {
  203. attributes.Add(string.IsNullOrEmpty(localizedForced) ? "Forced" : localizedForced);
  204. }
  205. if (!string.IsNullOrEmpty(Title))
  206. {
  207. var result = new StringBuilder(Title);
  208. foreach (var tag in attributes)
  209. {
  210. // Keep Tags that are not already in Title.
  211. if (Title.IndexOf(tag, StringComparison.OrdinalIgnoreCase) == -1)
  212. {
  213. result.Append(" - ").Append(tag);
  214. }
  215. }
  216. return result.ToString();
  217. }
  218. return string.Join(" - ", attributes);
  219. }
  220. default:
  221. return null;
  222. }
  223. }
  224. }
  225. private string GetResolutionText()
  226. {
  227. var i = this;
  228. if (i.Width.HasValue && i.Height.HasValue)
  229. {
  230. var width = i.Width.Value;
  231. var height = i.Height.Value;
  232. if (width >= 3800 || height >= 2000)
  233. {
  234. return "4K";
  235. }
  236. if (width >= 2500)
  237. {
  238. if (i.IsInterlaced)
  239. {
  240. return "1440i";
  241. }
  242. return "1440p";
  243. }
  244. if (width >= 1900 || height >= 1000)
  245. {
  246. if (i.IsInterlaced)
  247. {
  248. return "1080i";
  249. }
  250. return "1080p";
  251. }
  252. if (width >= 1260 || height >= 700)
  253. {
  254. if (i.IsInterlaced)
  255. {
  256. return "720i";
  257. }
  258. return "720p";
  259. }
  260. if (width >= 700 || height >= 440)
  261. {
  262. if (i.IsInterlaced)
  263. {
  264. return "480i";
  265. }
  266. return "480p";
  267. }
  268. return "SD";
  269. }
  270. return null;
  271. }
  272. public string NalLengthSize { get; set; }
  273. /// <summary>
  274. /// Gets or sets a value indicating whether this instance is interlaced.
  275. /// </summary>
  276. /// <value><c>true</c> if this instance is interlaced; otherwise, <c>false</c>.</value>
  277. public bool IsInterlaced { get; set; }
  278. public bool? IsAVC { get; set; }
  279. /// <summary>
  280. /// Gets or sets the channel layout.
  281. /// </summary>
  282. /// <value>The channel layout.</value>
  283. public string ChannelLayout { get; set; }
  284. /// <summary>
  285. /// Gets or sets the bit rate.
  286. /// </summary>
  287. /// <value>The bit rate.</value>
  288. public int? BitRate { get; set; }
  289. /// <summary>
  290. /// Gets or sets the bit depth.
  291. /// </summary>
  292. /// <value>The bit depth.</value>
  293. public int? BitDepth { get; set; }
  294. /// <summary>
  295. /// Gets or sets the reference frames.
  296. /// </summary>
  297. /// <value>The reference frames.</value>
  298. public int? RefFrames { get; set; }
  299. /// <summary>
  300. /// Gets or sets the length of the packet.
  301. /// </summary>
  302. /// <value>The length of the packet.</value>
  303. public int? PacketLength { get; set; }
  304. /// <summary>
  305. /// Gets or sets the channels.
  306. /// </summary>
  307. /// <value>The channels.</value>
  308. public int? Channels { get; set; }
  309. /// <summary>
  310. /// Gets or sets the sample rate.
  311. /// </summary>
  312. /// <value>The sample rate.</value>
  313. public int? SampleRate { get; set; }
  314. /// <summary>
  315. /// Gets or sets a value indicating whether this instance is default.
  316. /// </summary>
  317. /// <value><c>true</c> if this instance is default; otherwise, <c>false</c>.</value>
  318. public bool IsDefault { get; set; }
  319. /// <summary>
  320. /// Gets or sets a value indicating whether this instance is forced.
  321. /// </summary>
  322. /// <value><c>true</c> if this instance is forced; otherwise, <c>false</c>.</value>
  323. public bool IsForced { get; set; }
  324. /// <summary>
  325. /// Gets or sets the height.
  326. /// </summary>
  327. /// <value>The height.</value>
  328. public int? Height { get; set; }
  329. /// <summary>
  330. /// Gets or sets the width.
  331. /// </summary>
  332. /// <value>The width.</value>
  333. public int? Width { get; set; }
  334. /// <summary>
  335. /// Gets or sets the average frame rate.
  336. /// </summary>
  337. /// <value>The average frame rate.</value>
  338. public float? AverageFrameRate { get; set; }
  339. /// <summary>
  340. /// Gets or sets the real frame rate.
  341. /// </summary>
  342. /// <value>The real frame rate.</value>
  343. public float? RealFrameRate { get; set; }
  344. /// <summary>
  345. /// Gets or sets the profile.
  346. /// </summary>
  347. /// <value>The profile.</value>
  348. public string Profile { get; set; }
  349. /// <summary>
  350. /// Gets or sets the type.
  351. /// </summary>
  352. /// <value>The type.</value>
  353. public MediaStreamType Type { get; set; }
  354. /// <summary>
  355. /// Gets or sets the aspect ratio.
  356. /// </summary>
  357. /// <value>The aspect ratio.</value>
  358. public string AspectRatio { get; set; }
  359. /// <summary>
  360. /// Gets or sets the index.
  361. /// </summary>
  362. /// <value>The index.</value>
  363. public int Index { get; set; }
  364. /// <summary>
  365. /// Gets or sets the score.
  366. /// </summary>
  367. /// <value>The score.</value>
  368. public int? Score { get; set; }
  369. /// <summary>
  370. /// Gets or sets a value indicating whether this instance is external.
  371. /// </summary>
  372. /// <value><c>true</c> if this instance is external; otherwise, <c>false</c>.</value>
  373. public bool IsExternal { get; set; }
  374. /// <summary>
  375. /// Gets or sets the method.
  376. /// </summary>
  377. /// <value>The method.</value>
  378. public SubtitleDeliveryMethod? DeliveryMethod { get; set; }
  379. /// <summary>
  380. /// Gets or sets the delivery URL.
  381. /// </summary>
  382. /// <value>The delivery URL.</value>
  383. public string DeliveryUrl { get; set; }
  384. /// <summary>
  385. /// Gets or sets a value indicating whether this instance is external URL.
  386. /// </summary>
  387. /// <value><c>null</c> if [is external URL] contains no value, <c>true</c> if [is external URL]; otherwise, <c>false</c>.</value>
  388. public bool? IsExternalUrl { get; set; }
  389. public bool IsTextSubtitleStream
  390. {
  391. get
  392. {
  393. if (Type != MediaStreamType.Subtitle)
  394. {
  395. return false;
  396. }
  397. if (string.IsNullOrEmpty(Codec) && !IsExternal)
  398. {
  399. return false;
  400. }
  401. return IsTextFormat(Codec);
  402. }
  403. }
  404. public static bool IsTextFormat(string format)
  405. {
  406. string codec = format ?? string.Empty;
  407. // sub = external .sub file
  408. return codec.IndexOf("pgs", StringComparison.OrdinalIgnoreCase) == -1 &&
  409. codec.IndexOf("dvd", StringComparison.OrdinalIgnoreCase) == -1 &&
  410. codec.IndexOf("dvbsub", StringComparison.OrdinalIgnoreCase) == -1 &&
  411. !string.Equals(codec, "sub", StringComparison.OrdinalIgnoreCase) &&
  412. !string.Equals(codec, "dvb_subtitle", StringComparison.OrdinalIgnoreCase);
  413. }
  414. public bool SupportsSubtitleConversionTo(string toCodec)
  415. {
  416. if (!IsTextSubtitleStream)
  417. {
  418. return false;
  419. }
  420. var fromCodec = Codec;
  421. // Can't convert from this
  422. if (string.Equals(fromCodec, "ass", StringComparison.OrdinalIgnoreCase))
  423. {
  424. return false;
  425. }
  426. if (string.Equals(fromCodec, "ssa", StringComparison.OrdinalIgnoreCase))
  427. {
  428. return false;
  429. }
  430. // Can't convert to this
  431. if (string.Equals(toCodec, "ass", StringComparison.OrdinalIgnoreCase))
  432. {
  433. return false;
  434. }
  435. if (string.Equals(toCodec, "ssa", StringComparison.OrdinalIgnoreCase))
  436. {
  437. return false;
  438. }
  439. return true;
  440. }
  441. /// <summary>
  442. /// Gets or sets a value indicating whether [supports external stream].
  443. /// </summary>
  444. /// <value><c>true</c> if [supports external stream]; otherwise, <c>false</c>.</value>
  445. public bool SupportsExternalStream { get; set; }
  446. /// <summary>
  447. /// Gets or sets the filename.
  448. /// </summary>
  449. /// <value>The filename.</value>
  450. public string Path { get; set; }
  451. /// <summary>
  452. /// Gets or sets the pixel format.
  453. /// </summary>
  454. /// <value>The pixel format.</value>
  455. public string PixelFormat { get; set; }
  456. /// <summary>
  457. /// Gets or sets the level.
  458. /// </summary>
  459. /// <value>The level.</value>
  460. public double? Level { get; set; }
  461. /// <summary>
  462. /// Gets a value indicating whether this instance is anamorphic.
  463. /// </summary>
  464. /// <value><c>true</c> if this instance is anamorphic; otherwise, <c>false</c>.</value>
  465. public bool? IsAnamorphic { get; set; }
  466. }
  467. }