MediaStream.cs 18 KB

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