MediaStream.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  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(Title))
  163. {
  164. var result = new StringBuilder(Title);
  165. foreach (var tag in attributes)
  166. {
  167. // Keep Tags that are not already in Title.
  168. if (Title.IndexOf(tag, StringComparison.OrdinalIgnoreCase) == -1)
  169. {
  170. result.Append(" - ").Append(tag);
  171. }
  172. }
  173. return result.ToString();
  174. }
  175. return string.Join(" ", attributes);
  176. }
  177. case MediaStreamType.Subtitle:
  178. {
  179. var attributes = new List<string>();
  180. if (!string.IsNullOrEmpty(Language))
  181. {
  182. // Get full language string i.e. eng -> English. Will not work for some languages which use ISO 639-2/B instead of /T codes.
  183. string fullLanguage = CultureInfo
  184. .GetCultures(CultureTypes.NeutralCultures)
  185. .FirstOrDefault(r => r.ThreeLetterISOLanguageName.Equals(Language, StringComparison.OrdinalIgnoreCase))
  186. ?.DisplayName;
  187. attributes.Add(StringHelper.FirstToUpper(fullLanguage ?? Language));
  188. }
  189. else
  190. {
  191. attributes.Add(string.IsNullOrEmpty(localizedUndefined) ? "Und" : localizedUndefined);
  192. }
  193. if (IsDefault)
  194. {
  195. attributes.Add(string.IsNullOrEmpty(localizedDefault) ? "Default" : localizedDefault);
  196. }
  197. if (IsForced)
  198. {
  199. attributes.Add(string.IsNullOrEmpty(localizedForced) ? "Forced" : localizedForced);
  200. }
  201. if (!string.IsNullOrEmpty(Title))
  202. {
  203. var result = new StringBuilder(Title);
  204. foreach (var tag in attributes)
  205. {
  206. // Keep Tags that are not already in Title.
  207. if (Title.IndexOf(tag, StringComparison.OrdinalIgnoreCase) == -1)
  208. {
  209. result.Append(" - ").Append(tag);
  210. }
  211. }
  212. return result.ToString();
  213. }
  214. return string.Join(" - ", attributes);
  215. }
  216. default:
  217. return null;
  218. }
  219. }
  220. }
  221. private string GetResolutionText()
  222. {
  223. var i = this;
  224. if (i.Width.HasValue && i.Height.HasValue)
  225. {
  226. var width = i.Width.Value;
  227. var height = i.Height.Value;
  228. if (width >= 3800 || height >= 2000)
  229. {
  230. return "4K";
  231. }
  232. if (width >= 2500)
  233. {
  234. if (i.IsInterlaced)
  235. {
  236. return "1440i";
  237. }
  238. return "1440p";
  239. }
  240. if (width >= 1900 || height >= 1000)
  241. {
  242. if (i.IsInterlaced)
  243. {
  244. return "1080i";
  245. }
  246. return "1080p";
  247. }
  248. if (width >= 1260 || height >= 700)
  249. {
  250. if (i.IsInterlaced)
  251. {
  252. return "720i";
  253. }
  254. return "720p";
  255. }
  256. if (width >= 700 || height >= 440)
  257. {
  258. if (i.IsInterlaced)
  259. {
  260. return "480i";
  261. }
  262. return "480p";
  263. }
  264. return "SD";
  265. }
  266. return null;
  267. }
  268. public string NalLengthSize { get; set; }
  269. /// <summary>
  270. /// Gets or sets a value indicating whether this instance is interlaced.
  271. /// </summary>
  272. /// <value><c>true</c> if this instance is interlaced; otherwise, <c>false</c>.</value>
  273. public bool IsInterlaced { get; set; }
  274. public bool? IsAVC { get; set; }
  275. /// <summary>
  276. /// Gets or sets the channel layout.
  277. /// </summary>
  278. /// <value>The channel layout.</value>
  279. public string ChannelLayout { get; set; }
  280. /// <summary>
  281. /// Gets or sets the bit rate.
  282. /// </summary>
  283. /// <value>The bit rate.</value>
  284. public int? BitRate { get; set; }
  285. /// <summary>
  286. /// Gets or sets the bit depth.
  287. /// </summary>
  288. /// <value>The bit depth.</value>
  289. public int? BitDepth { get; set; }
  290. /// <summary>
  291. /// Gets or sets the reference frames.
  292. /// </summary>
  293. /// <value>The reference frames.</value>
  294. public int? RefFrames { get; set; }
  295. /// <summary>
  296. /// Gets or sets the length of the packet.
  297. /// </summary>
  298. /// <value>The length of the packet.</value>
  299. public int? PacketLength { get; set; }
  300. /// <summary>
  301. /// Gets or sets the channels.
  302. /// </summary>
  303. /// <value>The channels.</value>
  304. public int? Channels { get; set; }
  305. /// <summary>
  306. /// Gets or sets the sample rate.
  307. /// </summary>
  308. /// <value>The sample rate.</value>
  309. public int? SampleRate { get; set; }
  310. /// <summary>
  311. /// Gets or sets a value indicating whether this instance is default.
  312. /// </summary>
  313. /// <value><c>true</c> if this instance is default; otherwise, <c>false</c>.</value>
  314. public bool IsDefault { get; set; }
  315. /// <summary>
  316. /// Gets or sets a value indicating whether this instance is forced.
  317. /// </summary>
  318. /// <value><c>true</c> if this instance is forced; otherwise, <c>false</c>.</value>
  319. public bool IsForced { get; set; }
  320. /// <summary>
  321. /// Gets or sets the height.
  322. /// </summary>
  323. /// <value>The height.</value>
  324. public int? Height { get; set; }
  325. /// <summary>
  326. /// Gets or sets the width.
  327. /// </summary>
  328. /// <value>The width.</value>
  329. public int? Width { get; set; }
  330. /// <summary>
  331. /// Gets or sets the average frame rate.
  332. /// </summary>
  333. /// <value>The average frame rate.</value>
  334. public float? AverageFrameRate { get; set; }
  335. /// <summary>
  336. /// Gets or sets the real frame rate.
  337. /// </summary>
  338. /// <value>The real frame rate.</value>
  339. public float? RealFrameRate { get; set; }
  340. /// <summary>
  341. /// Gets or sets the profile.
  342. /// </summary>
  343. /// <value>The profile.</value>
  344. public string Profile { get; set; }
  345. /// <summary>
  346. /// Gets or sets the type.
  347. /// </summary>
  348. /// <value>The type.</value>
  349. public MediaStreamType Type { get; set; }
  350. /// <summary>
  351. /// Gets or sets the aspect ratio.
  352. /// </summary>
  353. /// <value>The aspect ratio.</value>
  354. public string AspectRatio { get; set; }
  355. /// <summary>
  356. /// Gets or sets the index.
  357. /// </summary>
  358. /// <value>The index.</value>
  359. public int Index { get; set; }
  360. /// <summary>
  361. /// Gets or sets the score.
  362. /// </summary>
  363. /// <value>The score.</value>
  364. public int? Score { get; set; }
  365. /// <summary>
  366. /// Gets or sets a value indicating whether this instance is external.
  367. /// </summary>
  368. /// <value><c>true</c> if this instance is external; otherwise, <c>false</c>.</value>
  369. public bool IsExternal { get; set; }
  370. /// <summary>
  371. /// Gets or sets the method.
  372. /// </summary>
  373. /// <value>The method.</value>
  374. public SubtitleDeliveryMethod? DeliveryMethod { get; set; }
  375. /// <summary>
  376. /// Gets or sets the delivery URL.
  377. /// </summary>
  378. /// <value>The delivery URL.</value>
  379. public string DeliveryUrl { get; set; }
  380. /// <summary>
  381. /// Gets or sets a value indicating whether this instance is external URL.
  382. /// </summary>
  383. /// <value><c>null</c> if [is external URL] contains no value, <c>true</c> if [is external URL]; otherwise, <c>false</c>.</value>
  384. public bool? IsExternalUrl { get; set; }
  385. public bool IsTextSubtitleStream
  386. {
  387. get
  388. {
  389. if (Type != MediaStreamType.Subtitle)
  390. {
  391. return false;
  392. }
  393. if (string.IsNullOrEmpty(Codec) && !IsExternal)
  394. {
  395. return false;
  396. }
  397. return IsTextFormat(Codec);
  398. }
  399. }
  400. public static bool IsTextFormat(string format)
  401. {
  402. string codec = format ?? string.Empty;
  403. // sub = external .sub file
  404. return codec.IndexOf("pgs", StringComparison.OrdinalIgnoreCase) == -1 &&
  405. codec.IndexOf("dvd", StringComparison.OrdinalIgnoreCase) == -1 &&
  406. codec.IndexOf("dvbsub", StringComparison.OrdinalIgnoreCase) == -1 &&
  407. !string.Equals(codec, "sub", StringComparison.OrdinalIgnoreCase) &&
  408. !string.Equals(codec, "dvb_subtitle", StringComparison.OrdinalIgnoreCase);
  409. }
  410. public bool SupportsSubtitleConversionTo(string toCodec)
  411. {
  412. if (!IsTextSubtitleStream)
  413. {
  414. return false;
  415. }
  416. var fromCodec = Codec;
  417. // Can't convert from this
  418. if (string.Equals(fromCodec, "ass", StringComparison.OrdinalIgnoreCase))
  419. {
  420. return false;
  421. }
  422. if (string.Equals(fromCodec, "ssa", StringComparison.OrdinalIgnoreCase))
  423. {
  424. return false;
  425. }
  426. // Can't convert to this
  427. if (string.Equals(toCodec, "ass", StringComparison.OrdinalIgnoreCase))
  428. {
  429. return false;
  430. }
  431. if (string.Equals(toCodec, "ssa", StringComparison.OrdinalIgnoreCase))
  432. {
  433. return false;
  434. }
  435. return true;
  436. }
  437. /// <summary>
  438. /// Gets or sets a value indicating whether [supports external stream].
  439. /// </summary>
  440. /// <value><c>true</c> if [supports external stream]; otherwise, <c>false</c>.</value>
  441. public bool SupportsExternalStream { get; set; }
  442. /// <summary>
  443. /// Gets or sets the filename.
  444. /// </summary>
  445. /// <value>The filename.</value>
  446. public string Path { get; set; }
  447. /// <summary>
  448. /// Gets or sets the pixel format.
  449. /// </summary>
  450. /// <value>The pixel format.</value>
  451. public string PixelFormat { get; set; }
  452. /// <summary>
  453. /// Gets or sets the level.
  454. /// </summary>
  455. /// <value>The level.</value>
  456. public double? Level { get; set; }
  457. /// <summary>
  458. /// Gets a value indicating whether this instance is anamorphic.
  459. /// </summary>
  460. /// <value><c>true</c> if this instance is anamorphic; otherwise, <c>false</c>.</value>
  461. public bool? IsAnamorphic { get; set; }
  462. }
  463. }