StreamInfo.cs 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390
  1. #pragma warning disable CA1819 // Properties should not return arrays
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Globalization;
  6. using System.Linq;
  7. using System.Text;
  8. using Jellyfin.Data.Enums;
  9. using Jellyfin.Extensions;
  10. using MediaBrowser.Model.Drawing;
  11. using MediaBrowser.Model.Dto;
  12. using MediaBrowser.Model.Entities;
  13. using MediaBrowser.Model.MediaInfo;
  14. using MediaBrowser.Model.Session;
  15. namespace MediaBrowser.Model.Dlna;
  16. /// <summary>
  17. /// Class holding information on a stream.
  18. /// </summary>
  19. public class StreamInfo
  20. {
  21. /// <summary>
  22. /// Initializes a new instance of the <see cref="StreamInfo"/> class.
  23. /// </summary>
  24. public StreamInfo()
  25. {
  26. AudioCodecs = [];
  27. VideoCodecs = [];
  28. SubtitleCodecs = [];
  29. StreamOptions = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
  30. }
  31. /// <summary>
  32. /// Gets or sets the item id.
  33. /// </summary>
  34. /// <value>The item id.</value>
  35. public Guid ItemId { get; set; }
  36. /// <summary>
  37. /// Gets or sets the play method.
  38. /// </summary>
  39. /// <value>The play method.</value>
  40. public PlayMethod PlayMethod { get; set; }
  41. /// <summary>
  42. /// Gets or sets the encoding context.
  43. /// </summary>
  44. /// <value>The encoding context.</value>
  45. public EncodingContext Context { get; set; }
  46. /// <summary>
  47. /// Gets or sets the media type.
  48. /// </summary>
  49. /// <value>The media type.</value>
  50. public DlnaProfileType MediaType { get; set; }
  51. /// <summary>
  52. /// Gets or sets the container.
  53. /// </summary>
  54. /// <value>The container.</value>
  55. public string? Container { get; set; }
  56. /// <summary>
  57. /// Gets or sets the sub protocol.
  58. /// </summary>
  59. /// <value>The sub protocol.</value>
  60. public MediaStreamProtocol SubProtocol { get; set; }
  61. /// <summary>
  62. /// Gets or sets the start position ticks.
  63. /// </summary>
  64. /// <value>The start position ticks.</value>
  65. public long StartPositionTicks { get; set; }
  66. /// <summary>
  67. /// Gets or sets the segment length.
  68. /// </summary>
  69. /// <value>The segment length.</value>
  70. public int? SegmentLength { get; set; }
  71. /// <summary>
  72. /// Gets or sets the minimum segments count.
  73. /// </summary>
  74. /// <value>The minimum segments count.</value>
  75. public int? MinSegments { get; set; }
  76. /// <summary>
  77. /// Gets or sets a value indicating whether the stream can be broken on non-keyframes.
  78. /// </summary>
  79. public bool BreakOnNonKeyFrames { get; set; }
  80. /// <summary>
  81. /// Gets or sets a value indicating whether the stream requires AVC.
  82. /// </summary>
  83. public bool RequireAvc { get; set; }
  84. /// <summary>
  85. /// Gets or sets a value indicating whether the stream requires AVC.
  86. /// </summary>
  87. public bool RequireNonAnamorphic { get; set; }
  88. /// <summary>
  89. /// Gets or sets a value indicating whether timestamps should be copied.
  90. /// </summary>
  91. public bool CopyTimestamps { get; set; }
  92. /// <summary>
  93. /// Gets or sets a value indicating whether timestamps should be copied.
  94. /// </summary>
  95. public bool EnableMpegtsM2TsMode { get; set; }
  96. /// <summary>
  97. /// Gets or sets a value indicating whether the subtitle manifest is enabled.
  98. /// </summary>
  99. public bool EnableSubtitlesInManifest { get; set; }
  100. /// <summary>
  101. /// Gets or sets the audio codecs.
  102. /// </summary>
  103. /// <value>The audio codecs.</value>
  104. public IReadOnlyList<string> AudioCodecs { get; set; }
  105. /// <summary>
  106. /// Gets or sets the video codecs.
  107. /// </summary>
  108. /// <value>The video codecs.</value>
  109. public IReadOnlyList<string> VideoCodecs { get; set; }
  110. /// <summary>
  111. /// Gets or sets the audio stream index.
  112. /// </summary>
  113. /// <value>The audio stream index.</value>
  114. public int? AudioStreamIndex { get; set; }
  115. /// <summary>
  116. /// Gets or sets the video stream index.
  117. /// </summary>
  118. /// <value>The subtitle stream index.</value>
  119. public int? SubtitleStreamIndex { get; set; }
  120. /// <summary>
  121. /// Gets or sets the maximum transcoding audio channels.
  122. /// </summary>
  123. /// <value>The maximum transcoding audio channels.</value>
  124. public int? TranscodingMaxAudioChannels { get; set; }
  125. /// <summary>
  126. /// Gets or sets the global maximum audio channels.
  127. /// </summary>
  128. /// <value>The global maximum audio channels.</value>
  129. public int? GlobalMaxAudioChannels { get; set; }
  130. /// <summary>
  131. /// Gets or sets the audio bitrate.
  132. /// </summary>
  133. /// <value>The audio bitrate.</value>
  134. public int? AudioBitrate { get; set; }
  135. /// <summary>
  136. /// Gets or sets the audio sample rate.
  137. /// </summary>
  138. /// <value>The audio sample rate.</value>
  139. public int? AudioSampleRate { get; set; }
  140. /// <summary>
  141. /// Gets or sets the video bitrate.
  142. /// </summary>
  143. /// <value>The video bitrate.</value>
  144. public int? VideoBitrate { get; set; }
  145. /// <summary>
  146. /// Gets or sets the maximum output width.
  147. /// </summary>
  148. /// <value>The output width.</value>
  149. public int? MaxWidth { get; set; }
  150. /// <summary>
  151. /// Gets or sets the maximum output height.
  152. /// </summary>
  153. /// <value>The maximum output height.</value>
  154. public int? MaxHeight { get; set; }
  155. /// <summary>
  156. /// Gets or sets the maximum framerate.
  157. /// </summary>
  158. /// <value>The maximum framerate.</value>
  159. public float? MaxFramerate { get; set; }
  160. /// <summary>
  161. /// Gets or sets the device profile.
  162. /// </summary>
  163. /// <value>The device profile.</value>
  164. public required DeviceProfile DeviceProfile { get; set; }
  165. /// <summary>
  166. /// Gets or sets the device profile id.
  167. /// </summary>
  168. /// <value>The device profile id.</value>
  169. public string? DeviceProfileId { get; set; }
  170. /// <summary>
  171. /// Gets or sets the device id.
  172. /// </summary>
  173. /// <value>The device id.</value>
  174. public string? DeviceId { get; set; }
  175. /// <summary>
  176. /// Gets or sets the runtime ticks.
  177. /// </summary>
  178. /// <value>The runtime ticks.</value>
  179. public long? RunTimeTicks { get; set; }
  180. /// <summary>
  181. /// Gets or sets the transcode seek info.
  182. /// </summary>
  183. /// <value>The transcode seek info.</value>
  184. public TranscodeSeekInfo TranscodeSeekInfo { get; set; }
  185. /// <summary>
  186. /// Gets or sets a value indicating whether content length should be estimated.
  187. /// </summary>
  188. public bool EstimateContentLength { get; set; }
  189. /// <summary>
  190. /// Gets or sets the media source info.
  191. /// </summary>
  192. /// <value>The media source info.</value>
  193. public MediaSourceInfo? MediaSource { get; set; }
  194. /// <summary>
  195. /// Gets or sets the subtitle codecs.
  196. /// </summary>
  197. /// <value>The subtitle codecs.</value>
  198. public IReadOnlyList<string> SubtitleCodecs { get; set; }
  199. /// <summary>
  200. /// Gets or sets the subtitle delivery method.
  201. /// </summary>
  202. /// <value>The subtitle delivery method.</value>
  203. public SubtitleDeliveryMethod SubtitleDeliveryMethod { get; set; }
  204. /// <summary>
  205. /// Gets or sets the subtitle format.
  206. /// </summary>
  207. /// <value>The subtitle format.</value>
  208. public string? SubtitleFormat { get; set; }
  209. /// <summary>
  210. /// Gets or sets the play session id.
  211. /// </summary>
  212. /// <value>The play session id.</value>
  213. public string? PlaySessionId { get; set; }
  214. /// <summary>
  215. /// Gets or sets the transcode reasons.
  216. /// </summary>
  217. /// <value>The transcode reasons.</value>
  218. public TranscodeReason TranscodeReasons { get; set; }
  219. /// <summary>
  220. /// Gets the stream options.
  221. /// </summary>
  222. /// <value>The stream options.</value>
  223. public Dictionary<string, string> StreamOptions { get; private set; }
  224. /// <summary>
  225. /// Gets the media source id.
  226. /// </summary>
  227. /// <value>The media source id.</value>
  228. public string? MediaSourceId => MediaSource?.Id;
  229. /// <summary>
  230. /// Gets or sets a value indicating whether audio VBR encoding is enabled.
  231. /// </summary>
  232. public bool EnableAudioVbrEncoding { get; set; }
  233. /// <summary>
  234. /// Gets or sets a value indicating whether always burn in subtitles when transcoding.
  235. /// </summary>
  236. public bool AlwaysBurnInSubtitleWhenTranscoding { get; set; }
  237. /// <summary>
  238. /// Gets a value indicating whether the stream is direct.
  239. /// </summary>
  240. public bool IsDirectStream => MediaSource?.VideoType is not (VideoType.Dvd or VideoType.BluRay)
  241. && PlayMethod is PlayMethod.DirectStream or PlayMethod.DirectPlay;
  242. /// <summary>
  243. /// Gets the audio stream that will be used in the output stream.
  244. /// </summary>
  245. /// <value>The audio stream.</value>
  246. public MediaStream? TargetAudioStream => MediaSource?.GetDefaultAudioStream(AudioStreamIndex);
  247. /// <summary>
  248. /// Gets the video stream that will be used in the output stream.
  249. /// </summary>
  250. /// <value>The video stream.</value>
  251. public MediaStream? TargetVideoStream => MediaSource?.VideoStream;
  252. /// <summary>
  253. /// Gets the audio sample rate that will be in the output stream.
  254. /// </summary>
  255. /// <value>The target audio sample rate.</value>
  256. public int? TargetAudioSampleRate
  257. {
  258. get
  259. {
  260. var stream = TargetAudioStream;
  261. return AudioSampleRate.HasValue && !IsDirectStream
  262. ? AudioSampleRate
  263. : stream?.SampleRate;
  264. }
  265. }
  266. /// <summary>
  267. /// Gets the audio bit depth that will be in the output stream.
  268. /// </summary>
  269. /// <value>The target bit depth.</value>
  270. public int? TargetAudioBitDepth
  271. {
  272. get
  273. {
  274. if (IsDirectStream)
  275. {
  276. return TargetAudioStream?.BitDepth;
  277. }
  278. var targetAudioCodecs = TargetAudioCodec;
  279. var audioCodec = targetAudioCodecs.Count == 0 ? null : targetAudioCodecs[0];
  280. if (!string.IsNullOrEmpty(audioCodec))
  281. {
  282. return GetTargetAudioBitDepth(audioCodec);
  283. }
  284. return TargetAudioStream?.BitDepth;
  285. }
  286. }
  287. /// <summary>
  288. /// Gets the video bit depth that will be in the output stream.
  289. /// </summary>
  290. /// <value>The target video bit depth.</value>
  291. public int? TargetVideoBitDepth
  292. {
  293. get
  294. {
  295. if (IsDirectStream)
  296. {
  297. return TargetVideoStream?.BitDepth;
  298. }
  299. var targetVideoCodecs = TargetVideoCodec;
  300. var videoCodec = targetVideoCodecs.Count == 0 ? null : targetVideoCodecs[0];
  301. if (!string.IsNullOrEmpty(videoCodec))
  302. {
  303. return GetTargetVideoBitDepth(videoCodec);
  304. }
  305. return TargetVideoStream?.BitDepth;
  306. }
  307. }
  308. /// <summary>
  309. /// Gets the target reference frames that will be in the output stream.
  310. /// </summary>
  311. /// <value>The target reference frames.</value>
  312. public int? TargetRefFrames
  313. {
  314. get
  315. {
  316. if (IsDirectStream)
  317. {
  318. return TargetVideoStream?.RefFrames;
  319. }
  320. var targetVideoCodecs = TargetVideoCodec;
  321. var videoCodec = targetVideoCodecs.Count == 0 ? null : targetVideoCodecs[0];
  322. if (!string.IsNullOrEmpty(videoCodec))
  323. {
  324. return GetTargetRefFrames(videoCodec);
  325. }
  326. return TargetVideoStream?.RefFrames;
  327. }
  328. }
  329. /// <summary>
  330. /// Gets the target framerate that will be in the output stream.
  331. /// </summary>
  332. /// <value>The target framerate.</value>
  333. public float? TargetFramerate
  334. {
  335. get
  336. {
  337. var stream = TargetVideoStream;
  338. return MaxFramerate.HasValue && !IsDirectStream
  339. ? MaxFramerate
  340. : stream?.ReferenceFrameRate;
  341. }
  342. }
  343. /// <summary>
  344. /// Gets the target video level that will be in the output stream.
  345. /// </summary>
  346. /// <value>The target video level.</value>
  347. public double? TargetVideoLevel
  348. {
  349. get
  350. {
  351. if (IsDirectStream)
  352. {
  353. return TargetVideoStream?.Level;
  354. }
  355. var targetVideoCodecs = TargetVideoCodec;
  356. var videoCodec = targetVideoCodecs.Count == 0 ? null : targetVideoCodecs[0];
  357. if (!string.IsNullOrEmpty(videoCodec))
  358. {
  359. return GetTargetVideoLevel(videoCodec);
  360. }
  361. return TargetVideoStream?.Level;
  362. }
  363. }
  364. /// <summary>
  365. /// Gets the target packet length that will be in the output stream.
  366. /// </summary>
  367. /// <value>The target packet length.</value>
  368. public int? TargetPacketLength
  369. {
  370. get
  371. {
  372. var stream = TargetVideoStream;
  373. return !IsDirectStream
  374. ? null
  375. : stream?.PacketLength;
  376. }
  377. }
  378. /// <summary>
  379. /// Gets the target video profile that will be in the output stream.
  380. /// </summary>
  381. /// <value>The target video profile.</value>
  382. public string? TargetVideoProfile
  383. {
  384. get
  385. {
  386. if (IsDirectStream)
  387. {
  388. return TargetVideoStream?.Profile;
  389. }
  390. var targetVideoCodecs = TargetVideoCodec;
  391. var videoCodec = targetVideoCodecs.Count == 0 ? null : targetVideoCodecs[0];
  392. if (!string.IsNullOrEmpty(videoCodec))
  393. {
  394. return GetOption(videoCodec, "profile");
  395. }
  396. return TargetVideoStream?.Profile;
  397. }
  398. }
  399. /// <summary>
  400. /// Gets the target video range type that will be in the output stream.
  401. /// </summary>
  402. /// <value>The video range type.</value>
  403. public VideoRangeType TargetVideoRangeType
  404. {
  405. get
  406. {
  407. if (IsDirectStream)
  408. {
  409. return TargetVideoStream?.VideoRangeType ?? VideoRangeType.Unknown;
  410. }
  411. var targetVideoCodecs = TargetVideoCodec;
  412. var videoCodec = targetVideoCodecs.Count == 0 ? null : targetVideoCodecs[0];
  413. if (!string.IsNullOrEmpty(videoCodec)
  414. && Enum.TryParse(GetOption(videoCodec, "rangetype"), true, out VideoRangeType videoRangeType))
  415. {
  416. return videoRangeType;
  417. }
  418. return TargetVideoStream?.VideoRangeType ?? VideoRangeType.Unknown;
  419. }
  420. }
  421. /// <summary>
  422. /// Gets the target video codec tag.
  423. /// </summary>
  424. /// <value>The video codec tag.</value>
  425. public string? TargetVideoCodecTag
  426. {
  427. get
  428. {
  429. var stream = TargetVideoStream;
  430. return !IsDirectStream
  431. ? null
  432. : stream?.CodecTag;
  433. }
  434. }
  435. /// <summary>
  436. /// Gets the audio bitrate that will be in the output stream.
  437. /// </summary>
  438. /// <value>The audio bitrate.</value>
  439. public int? TargetAudioBitrate
  440. {
  441. get
  442. {
  443. var stream = TargetAudioStream;
  444. return AudioBitrate.HasValue && !IsDirectStream
  445. ? AudioBitrate
  446. : stream?.BitRate;
  447. }
  448. }
  449. /// <summary>
  450. /// Gets the amount of audio channels that will be in the output stream.
  451. /// </summary>
  452. /// <value>The target audio channels.</value>
  453. public int? TargetAudioChannels
  454. {
  455. get
  456. {
  457. if (IsDirectStream)
  458. {
  459. return TargetAudioStream?.Channels;
  460. }
  461. var targetAudioCodecs = TargetAudioCodec;
  462. var codec = targetAudioCodecs.Count == 0 ? null : targetAudioCodecs[0];
  463. if (!string.IsNullOrEmpty(codec))
  464. {
  465. return GetTargetRefFrames(codec);
  466. }
  467. return TargetAudioStream?.Channels;
  468. }
  469. }
  470. /// <summary>
  471. /// Gets the audio codec that will be in the output stream.
  472. /// </summary>
  473. /// <value>The audio codec.</value>
  474. public IReadOnlyList<string> TargetAudioCodec
  475. {
  476. get
  477. {
  478. var stream = TargetAudioStream;
  479. string? inputCodec = stream?.Codec;
  480. if (IsDirectStream)
  481. {
  482. return string.IsNullOrEmpty(inputCodec) ? [] : [inputCodec];
  483. }
  484. foreach (string codec in AudioCodecs)
  485. {
  486. if (string.Equals(codec, inputCodec, StringComparison.OrdinalIgnoreCase))
  487. {
  488. return string.IsNullOrEmpty(codec) ? [] : [codec];
  489. }
  490. }
  491. return AudioCodecs;
  492. }
  493. }
  494. /// <summary>
  495. /// Gets the video codec that will be in the output stream.
  496. /// </summary>
  497. /// <value>The target video codec.</value>
  498. public IReadOnlyList<string> TargetVideoCodec
  499. {
  500. get
  501. {
  502. var stream = TargetVideoStream;
  503. string? inputCodec = stream?.Codec;
  504. if (IsDirectStream)
  505. {
  506. return string.IsNullOrEmpty(inputCodec) ? [] : [inputCodec];
  507. }
  508. foreach (string codec in VideoCodecs)
  509. {
  510. if (string.Equals(codec, inputCodec, StringComparison.OrdinalIgnoreCase))
  511. {
  512. return string.IsNullOrEmpty(codec) ? [] : [codec];
  513. }
  514. }
  515. return VideoCodecs;
  516. }
  517. }
  518. /// <summary>
  519. /// Gets the target size of the output stream.
  520. /// </summary>
  521. /// <value>The target size.</value>
  522. public long? TargetSize
  523. {
  524. get
  525. {
  526. if (IsDirectStream)
  527. {
  528. return MediaSource?.Size;
  529. }
  530. if (RunTimeTicks.HasValue)
  531. {
  532. int? totalBitrate = TargetTotalBitrate;
  533. double totalSeconds = RunTimeTicks.Value;
  534. // Convert to ms
  535. totalSeconds /= 10000;
  536. // Convert to seconds
  537. totalSeconds /= 1000;
  538. return totalBitrate.HasValue ?
  539. Convert.ToInt64(totalBitrate.Value * totalSeconds) :
  540. null;
  541. }
  542. return null;
  543. }
  544. }
  545. /// <summary>
  546. /// Gets the target video bitrate of the output stream.
  547. /// </summary>
  548. /// <value>The video bitrate.</value>
  549. public int? TargetVideoBitrate
  550. {
  551. get
  552. {
  553. var stream = TargetVideoStream;
  554. return VideoBitrate.HasValue && !IsDirectStream
  555. ? VideoBitrate
  556. : stream?.BitRate;
  557. }
  558. }
  559. /// <summary>
  560. /// Gets the target timestamp of the output stream.
  561. /// </summary>
  562. /// <value>The target timestamp.</value>
  563. public TransportStreamTimestamp TargetTimestamp
  564. {
  565. get
  566. {
  567. var defaultValue = string.Equals(Container, "m2ts", StringComparison.OrdinalIgnoreCase)
  568. ? TransportStreamTimestamp.Valid
  569. : TransportStreamTimestamp.None;
  570. return !IsDirectStream
  571. ? defaultValue
  572. : MediaSource is null ? defaultValue : MediaSource.Timestamp ?? TransportStreamTimestamp.None;
  573. }
  574. }
  575. /// <summary>
  576. /// Gets the target total bitrate of the output stream.
  577. /// </summary>
  578. /// <value>The target total bitrate.</value>
  579. public int? TargetTotalBitrate => (TargetAudioBitrate ?? 0) + (TargetVideoBitrate ?? 0);
  580. /// <summary>
  581. /// Gets a value indicating whether the output stream is anamorphic.
  582. /// </summary>
  583. public bool? IsTargetAnamorphic
  584. {
  585. get
  586. {
  587. if (IsDirectStream)
  588. {
  589. return TargetVideoStream?.IsAnamorphic;
  590. }
  591. return false;
  592. }
  593. }
  594. /// <summary>
  595. /// Gets a value indicating whether the output stream is interlaced.
  596. /// </summary>
  597. public bool? IsTargetInterlaced
  598. {
  599. get
  600. {
  601. if (IsDirectStream)
  602. {
  603. return TargetVideoStream?.IsInterlaced;
  604. }
  605. var targetVideoCodecs = TargetVideoCodec;
  606. var videoCodec = targetVideoCodecs.Count == 0 ? null : targetVideoCodecs[0];
  607. if (!string.IsNullOrEmpty(videoCodec))
  608. {
  609. if (string.Equals(GetOption(videoCodec, "deinterlace"), "true", StringComparison.OrdinalIgnoreCase))
  610. {
  611. return false;
  612. }
  613. }
  614. return TargetVideoStream?.IsInterlaced;
  615. }
  616. }
  617. /// <summary>
  618. /// Gets a value indicating whether the output stream is AVC.
  619. /// </summary>
  620. public bool? IsTargetAVC
  621. {
  622. get
  623. {
  624. if (IsDirectStream)
  625. {
  626. return TargetVideoStream?.IsAVC;
  627. }
  628. return true;
  629. }
  630. }
  631. /// <summary>
  632. /// Gets the target width of the output stream.
  633. /// </summary>
  634. /// <value>The target width.</value>
  635. public int? TargetWidth
  636. {
  637. get
  638. {
  639. var videoStream = TargetVideoStream;
  640. if (videoStream is not null && videoStream.Width.HasValue && videoStream.Height.HasValue)
  641. {
  642. ImageDimensions size = new ImageDimensions(videoStream.Width.Value, videoStream.Height.Value);
  643. size = DrawingUtils.Resize(size, 0, 0, MaxWidth ?? 0, MaxHeight ?? 0);
  644. return size.Width;
  645. }
  646. return MaxWidth;
  647. }
  648. }
  649. /// <summary>
  650. /// Gets the target height of the output stream.
  651. /// </summary>
  652. /// <value>The target height.</value>
  653. public int? TargetHeight
  654. {
  655. get
  656. {
  657. var videoStream = TargetVideoStream;
  658. if (videoStream is not null && videoStream.Width.HasValue && videoStream.Height.HasValue)
  659. {
  660. ImageDimensions size = new ImageDimensions(videoStream.Width.Value, videoStream.Height.Value);
  661. size = DrawingUtils.Resize(size, 0, 0, MaxWidth ?? 0, MaxHeight ?? 0);
  662. return size.Height;
  663. }
  664. return MaxHeight;
  665. }
  666. }
  667. /// <summary>
  668. /// Gets the target video stream count of the output stream.
  669. /// </summary>
  670. /// <value>The target video stream count.</value>
  671. public int? TargetVideoStreamCount
  672. {
  673. get
  674. {
  675. if (IsDirectStream)
  676. {
  677. return GetMediaStreamCount(MediaStreamType.Video, int.MaxValue);
  678. }
  679. return GetMediaStreamCount(MediaStreamType.Video, 1);
  680. }
  681. }
  682. /// <summary>
  683. /// Gets the target audio stream count of the output stream.
  684. /// </summary>
  685. /// <value>The target audio stream count.</value>
  686. public int? TargetAudioStreamCount
  687. {
  688. get
  689. {
  690. if (IsDirectStream)
  691. {
  692. return GetMediaStreamCount(MediaStreamType.Audio, int.MaxValue);
  693. }
  694. return GetMediaStreamCount(MediaStreamType.Audio, 1);
  695. }
  696. }
  697. /// <summary>
  698. /// Sets a stream option.
  699. /// </summary>
  700. /// <param name="qualifier">The qualifier.</param>
  701. /// <param name="name">The name.</param>
  702. /// <param name="value">The value.</param>
  703. public void SetOption(string? qualifier, string name, string value)
  704. {
  705. if (string.IsNullOrEmpty(qualifier))
  706. {
  707. SetOption(name, value);
  708. }
  709. else
  710. {
  711. SetOption(qualifier + "-" + name, value);
  712. }
  713. }
  714. /// <summary>
  715. /// Sets a stream option.
  716. /// </summary>
  717. /// <param name="name">The name.</param>
  718. /// <param name="value">The value.</param>
  719. public void SetOption(string name, string value)
  720. {
  721. StreamOptions[name] = value;
  722. }
  723. /// <summary>
  724. /// Gets a stream option.
  725. /// </summary>
  726. /// <param name="qualifier">The qualifier.</param>
  727. /// <param name="name">The name.</param>
  728. /// <returns>The value.</returns>
  729. public string? GetOption(string? qualifier, string name)
  730. {
  731. var value = GetOption(qualifier + "-" + name);
  732. if (string.IsNullOrEmpty(value))
  733. {
  734. value = GetOption(name);
  735. }
  736. return value;
  737. }
  738. /// <summary>
  739. /// Gets a stream option.
  740. /// </summary>
  741. /// <param name="name">The name.</param>
  742. /// <returns>The value.</returns>
  743. public string? GetOption(string name)
  744. {
  745. if (StreamOptions.TryGetValue(name, out var value))
  746. {
  747. return value;
  748. }
  749. return null;
  750. }
  751. /// <summary>
  752. /// Returns this output stream URL for this class.
  753. /// </summary>
  754. /// <param name="baseUrl">The base Url.</param>
  755. /// <param name="accessToken">The access Token.</param>
  756. /// <param name="query">Optional extra query.</param>
  757. /// <returns>A querystring representation of this object.</returns>
  758. public string ToUrl(string? baseUrl, string? accessToken, string? query)
  759. {
  760. var sb = new StringBuilder();
  761. if (!string.IsNullOrEmpty(baseUrl))
  762. {
  763. sb.Append(baseUrl.TrimEnd('/'));
  764. }
  765. if (MediaType == DlnaProfileType.Audio)
  766. {
  767. sb.Append("/audio/");
  768. }
  769. else
  770. {
  771. sb.Append("/videos/");
  772. }
  773. sb.Append(ItemId);
  774. if (SubProtocol == MediaStreamProtocol.hls)
  775. {
  776. sb.Append("/master.m3u8?");
  777. }
  778. else
  779. {
  780. sb.Append("/stream");
  781. if (!string.IsNullOrEmpty(Container))
  782. {
  783. sb.Append('.');
  784. sb.Append(Container);
  785. }
  786. sb.Append('?');
  787. }
  788. if (!string.IsNullOrEmpty(DeviceProfileId))
  789. {
  790. sb.Append("&DeviceProfileId=");
  791. sb.Append(DeviceProfileId);
  792. }
  793. if (!string.IsNullOrEmpty(DeviceId))
  794. {
  795. sb.Append("&DeviceId=");
  796. sb.Append(DeviceId);
  797. }
  798. if (!string.IsNullOrEmpty(MediaSourceId))
  799. {
  800. sb.Append("&MediaSourceId=");
  801. sb.Append(MediaSourceId);
  802. }
  803. // default true so don't store.
  804. if (IsDirectStream)
  805. {
  806. sb.Append("&Static=true");
  807. }
  808. if (VideoCodecs.Count != 0)
  809. {
  810. sb.Append("&VideoCodec=");
  811. sb.AppendJoin(',', VideoCodecs);
  812. }
  813. if (AudioCodecs.Count != 0)
  814. {
  815. sb.Append("&AudioCodec=");
  816. sb.AppendJoin(',', AudioCodecs);
  817. }
  818. if (AudioStreamIndex.HasValue)
  819. {
  820. sb.Append("&AudioStreamIndex=");
  821. sb.Append(AudioStreamIndex.Value.ToString(CultureInfo.InvariantCulture));
  822. }
  823. if (SubtitleStreamIndex.HasValue && (AlwaysBurnInSubtitleWhenTranscoding || SubtitleDeliveryMethod != SubtitleDeliveryMethod.External) && SubtitleStreamIndex != -1)
  824. {
  825. sb.Append("&SubtitleStreamIndex=");
  826. sb.Append(SubtitleStreamIndex.Value.ToString(CultureInfo.InvariantCulture));
  827. }
  828. if (VideoBitrate.HasValue)
  829. {
  830. sb.Append("&VideoBitrate=");
  831. sb.Append(VideoBitrate.Value.ToString(CultureInfo.InvariantCulture));
  832. }
  833. if (AudioBitrate.HasValue)
  834. {
  835. sb.Append("&AudioBitrate=");
  836. sb.Append(AudioBitrate.Value.ToString(CultureInfo.InvariantCulture));
  837. }
  838. if (AudioSampleRate.HasValue)
  839. {
  840. sb.Append("&AudioSampleRate=");
  841. sb.Append(AudioSampleRate.Value.ToString(CultureInfo.InvariantCulture));
  842. }
  843. if (MaxFramerate.HasValue)
  844. {
  845. sb.Append("&MaxFramerate=");
  846. sb.Append(MaxFramerate.Value.ToString(CultureInfo.InvariantCulture));
  847. }
  848. if (MaxWidth.HasValue)
  849. {
  850. sb.Append("&MaxWidth=");
  851. sb.Append(MaxWidth.Value.ToString(CultureInfo.InvariantCulture));
  852. }
  853. if (MaxHeight.HasValue)
  854. {
  855. sb.Append("&MaxHeight=");
  856. sb.Append(MaxHeight.Value.ToString(CultureInfo.InvariantCulture));
  857. }
  858. if (SubProtocol == MediaStreamProtocol.hls)
  859. {
  860. if (!string.IsNullOrEmpty(Container))
  861. {
  862. sb.Append("&SegmentContainer=");
  863. sb.Append(Container);
  864. }
  865. if (SegmentLength.HasValue)
  866. {
  867. sb.Append("&SegmentLength=");
  868. sb.Append(SegmentLength.Value.ToString(CultureInfo.InvariantCulture));
  869. }
  870. if (MinSegments.HasValue)
  871. {
  872. sb.Append("&MinSegments=");
  873. sb.Append(MinSegments.Value.ToString(CultureInfo.InvariantCulture));
  874. }
  875. sb.Append("&BreakOnNonKeyFrames=");
  876. sb.Append(BreakOnNonKeyFrames.ToString(CultureInfo.InvariantCulture));
  877. }
  878. else
  879. {
  880. if (StartPositionTicks != 0)
  881. {
  882. sb.Append("&StartTimeTicks=");
  883. sb.Append(StartPositionTicks.ToString(CultureInfo.InvariantCulture));
  884. }
  885. }
  886. if (!string.IsNullOrEmpty(PlaySessionId))
  887. {
  888. sb.Append("&PlaySessionId=");
  889. sb.Append(PlaySessionId);
  890. }
  891. if (!string.IsNullOrEmpty(accessToken))
  892. {
  893. sb.Append("&ApiKey=");
  894. sb.Append(accessToken);
  895. }
  896. var liveStreamId = MediaSource?.LiveStreamId;
  897. if (!string.IsNullOrEmpty(liveStreamId))
  898. {
  899. sb.Append("&LiveStreamId=");
  900. sb.Append(liveStreamId);
  901. }
  902. if (!IsDirectStream)
  903. {
  904. if (RequireNonAnamorphic)
  905. {
  906. sb.Append("&RequireNonAnamorphic=");
  907. sb.Append(RequireNonAnamorphic.ToString(CultureInfo.InvariantCulture));
  908. }
  909. if (TranscodingMaxAudioChannels.HasValue)
  910. {
  911. sb.Append("&TranscodingMaxAudioChannels=");
  912. sb.Append(TranscodingMaxAudioChannels.Value.ToString(CultureInfo.InvariantCulture));
  913. }
  914. if (EnableSubtitlesInManifest)
  915. {
  916. sb.Append("&EnableSubtitlesInManifest=");
  917. sb.Append(EnableSubtitlesInManifest.ToString(CultureInfo.InvariantCulture));
  918. }
  919. if (EnableMpegtsM2TsMode)
  920. {
  921. sb.Append("&EnableMpegtsM2TsMode=");
  922. sb.Append(EnableMpegtsM2TsMode.ToString(CultureInfo.InvariantCulture));
  923. }
  924. if (EstimateContentLength)
  925. {
  926. sb.Append("&EstimateContentLength=");
  927. sb.Append(EstimateContentLength.ToString(CultureInfo.InvariantCulture));
  928. }
  929. if (TranscodeSeekInfo != TranscodeSeekInfo.Auto)
  930. {
  931. sb.Append("&TranscodeSeekInfo=");
  932. sb.Append(TranscodeSeekInfo.ToString());
  933. }
  934. if (CopyTimestamps)
  935. {
  936. sb.Append("&CopyTimestamps=");
  937. sb.Append(CopyTimestamps.ToString(CultureInfo.InvariantCulture));
  938. }
  939. sb.Append("&RequireAvc=");
  940. sb.Append(RequireAvc.ToString(CultureInfo.InvariantCulture).ToLowerInvariant());
  941. sb.Append("&EnableAudioVbrEncoding=");
  942. sb.Append(EnableAudioVbrEncoding.ToString(CultureInfo.InvariantCulture).ToLowerInvariant());
  943. }
  944. var etag = MediaSource?.ETag;
  945. if (!string.IsNullOrEmpty(etag))
  946. {
  947. sb.Append("&Tag=");
  948. sb.Append(etag);
  949. }
  950. if (SubtitleStreamIndex.HasValue && SubtitleDeliveryMethod != SubtitleDeliveryMethod.External)
  951. {
  952. sb.Append("&SubtitleMethod=");
  953. sb.Append(SubtitleDeliveryMethod);
  954. }
  955. if (SubtitleStreamIndex.HasValue && SubtitleDeliveryMethod == SubtitleDeliveryMethod.Embed && SubtitleCodecs.Count != 0)
  956. {
  957. sb.Append("&SubtitleCodec=");
  958. sb.AppendJoin(',', SubtitleCodecs);
  959. }
  960. foreach (var pair in StreamOptions)
  961. {
  962. // Strip spaces to avoid having to encode h264 profile names
  963. sb.Append('&');
  964. sb.Append(pair.Key);
  965. sb.Append('=');
  966. sb.Append(pair.Value.Replace(" ", string.Empty, StringComparison.Ordinal));
  967. }
  968. var transcodeReasonsValues = TranscodeReasons.GetUniqueFlags().ToArray();
  969. if (!IsDirectStream && transcodeReasonsValues.Length > 0)
  970. {
  971. sb.Append("&TranscodeReasons=");
  972. sb.AppendJoin(',', transcodeReasonsValues);
  973. }
  974. if (!string.IsNullOrEmpty(query))
  975. {
  976. sb.Append(query);
  977. }
  978. return sb.ToString();
  979. }
  980. /// <summary>
  981. /// Gets the subtitle profiles.
  982. /// </summary>
  983. /// <param name="transcoderSupport">The transcoder support.</param>
  984. /// <param name="includeSelectedTrackOnly">If only the selected track should be included.</param>
  985. /// <param name="baseUrl">The base URL.</param>
  986. /// <param name="accessToken">The access token.</param>
  987. /// <returns>The <see cref="SubtitleStreamInfo"/> of the profiles.</returns>
  988. public IEnumerable<SubtitleStreamInfo> GetSubtitleProfiles(ITranscoderSupport transcoderSupport, bool includeSelectedTrackOnly, string baseUrl, string? accessToken)
  989. {
  990. return GetSubtitleProfiles(transcoderSupport, includeSelectedTrackOnly, false, baseUrl, accessToken);
  991. }
  992. /// <summary>
  993. /// Gets the subtitle profiles.
  994. /// </summary>
  995. /// <param name="transcoderSupport">The transcoder support.</param>
  996. /// <param name="includeSelectedTrackOnly">If only the selected track should be included.</param>
  997. /// <param name="enableAllProfiles">If all profiles are enabled.</param>
  998. /// <param name="baseUrl">The base URL.</param>
  999. /// <param name="accessToken">The access token.</param>
  1000. /// <returns>The <see cref="SubtitleStreamInfo"/> of the profiles.</returns>
  1001. public IEnumerable<SubtitleStreamInfo> GetSubtitleProfiles(ITranscoderSupport transcoderSupport, bool includeSelectedTrackOnly, bool enableAllProfiles, string baseUrl, string? accessToken)
  1002. {
  1003. if (MediaSource is null)
  1004. {
  1005. return [];
  1006. }
  1007. List<SubtitleStreamInfo> list = [];
  1008. // HLS will preserve timestamps so we can just grab the full subtitle stream
  1009. long startPositionTicks = SubProtocol == MediaStreamProtocol.hls
  1010. ? 0
  1011. : (PlayMethod == PlayMethod.Transcode && !CopyTimestamps ? StartPositionTicks : 0);
  1012. // First add the selected track
  1013. if (SubtitleStreamIndex.HasValue)
  1014. {
  1015. foreach (var stream in MediaSource.MediaStreams)
  1016. {
  1017. if (stream.Type == MediaStreamType.Subtitle && stream.Index == SubtitleStreamIndex.Value)
  1018. {
  1019. AddSubtitleProfiles(list, stream, transcoderSupport, enableAllProfiles, baseUrl, accessToken, startPositionTicks);
  1020. }
  1021. }
  1022. }
  1023. if (!includeSelectedTrackOnly)
  1024. {
  1025. foreach (var stream in MediaSource.MediaStreams)
  1026. {
  1027. if (stream.Type == MediaStreamType.Subtitle && (!SubtitleStreamIndex.HasValue || stream.Index != SubtitleStreamIndex.Value))
  1028. {
  1029. AddSubtitleProfiles(list, stream, transcoderSupport, enableAllProfiles, baseUrl, accessToken, startPositionTicks);
  1030. }
  1031. }
  1032. }
  1033. return list;
  1034. }
  1035. private void AddSubtitleProfiles(List<SubtitleStreamInfo> list, MediaStream stream, ITranscoderSupport transcoderSupport, bool enableAllProfiles, string baseUrl, string? accessToken, long startPositionTicks)
  1036. {
  1037. if (enableAllProfiles)
  1038. {
  1039. foreach (var profile in DeviceProfile.SubtitleProfiles)
  1040. {
  1041. var info = GetSubtitleStreamInfo(stream, baseUrl, accessToken, startPositionTicks, new[] { profile }, transcoderSupport);
  1042. if (info is not null)
  1043. {
  1044. list.Add(info);
  1045. }
  1046. }
  1047. }
  1048. else
  1049. {
  1050. var info = GetSubtitleStreamInfo(stream, baseUrl, accessToken, startPositionTicks, DeviceProfile.SubtitleProfiles, transcoderSupport);
  1051. if (info is not null)
  1052. {
  1053. list.Add(info);
  1054. }
  1055. }
  1056. }
  1057. private SubtitleStreamInfo? GetSubtitleStreamInfo(MediaStream stream, string baseUrl, string? accessToken, long startPositionTicks, SubtitleProfile[] subtitleProfiles, ITranscoderSupport transcoderSupport)
  1058. {
  1059. if (MediaSource is null)
  1060. {
  1061. return null;
  1062. }
  1063. var subtitleProfile = StreamBuilder.GetSubtitleProfile(MediaSource, stream, subtitleProfiles, PlayMethod, transcoderSupport, Container, SubProtocol);
  1064. var info = new SubtitleStreamInfo
  1065. {
  1066. IsForced = stream.IsForced,
  1067. Language = stream.Language,
  1068. Name = stream.Language ?? "Unknown",
  1069. Format = subtitleProfile.Format,
  1070. Index = stream.Index,
  1071. DeliveryMethod = subtitleProfile.Method,
  1072. DisplayTitle = stream.DisplayTitle
  1073. };
  1074. if (info.DeliveryMethod == SubtitleDeliveryMethod.External)
  1075. {
  1076. if (MediaSource.Protocol == MediaProtocol.File || !string.Equals(stream.Codec, subtitleProfile.Format, StringComparison.OrdinalIgnoreCase) || !stream.IsExternal)
  1077. {
  1078. info.Url = string.Format(
  1079. CultureInfo.InvariantCulture,
  1080. "{0}/Videos/{1}/{2}/Subtitles/{3}/{4}/Stream.{5}",
  1081. baseUrl,
  1082. ItemId,
  1083. MediaSourceId,
  1084. stream.Index.ToString(CultureInfo.InvariantCulture),
  1085. startPositionTicks.ToString(CultureInfo.InvariantCulture),
  1086. subtitleProfile.Format);
  1087. if (!string.IsNullOrEmpty(accessToken))
  1088. {
  1089. info.Url += "?ApiKey=" + accessToken;
  1090. }
  1091. info.IsExternalUrl = false;
  1092. }
  1093. else
  1094. {
  1095. info.Url = stream.Path;
  1096. info.IsExternalUrl = true;
  1097. }
  1098. }
  1099. return info;
  1100. }
  1101. /// <summary>
  1102. /// Gets the target video bit depth.
  1103. /// </summary>
  1104. /// <param name="codec">The codec.</param>
  1105. /// <returns>The target video bit depth.</returns>
  1106. public int? GetTargetVideoBitDepth(string? codec)
  1107. {
  1108. var value = GetOption(codec, "videobitdepth");
  1109. if (int.TryParse(value, CultureInfo.InvariantCulture, out var result))
  1110. {
  1111. return result;
  1112. }
  1113. return null;
  1114. }
  1115. /// <summary>
  1116. /// Gets the target audio bit depth.
  1117. /// </summary>
  1118. /// <param name="codec">The codec.</param>
  1119. /// <returns>The target audio bit depth.</returns>
  1120. public int? GetTargetAudioBitDepth(string? codec)
  1121. {
  1122. var value = GetOption(codec, "audiobitdepth");
  1123. if (int.TryParse(value, CultureInfo.InvariantCulture, out var result))
  1124. {
  1125. return result;
  1126. }
  1127. return null;
  1128. }
  1129. /// <summary>
  1130. /// Gets the target video level.
  1131. /// </summary>
  1132. /// <param name="codec">The codec.</param>
  1133. /// <returns>The target video level.</returns>
  1134. public double? GetTargetVideoLevel(string? codec)
  1135. {
  1136. var value = GetOption(codec, "level");
  1137. if (double.TryParse(value, CultureInfo.InvariantCulture, out var result))
  1138. {
  1139. return result;
  1140. }
  1141. return null;
  1142. }
  1143. /// <summary>
  1144. /// Gets the target reference frames.
  1145. /// </summary>
  1146. /// <param name="codec">The codec.</param>
  1147. /// <returns>The target reference frames.</returns>
  1148. public int? GetTargetRefFrames(string? codec)
  1149. {
  1150. var value = GetOption(codec, "maxrefframes");
  1151. if (int.TryParse(value, CultureInfo.InvariantCulture, out var result))
  1152. {
  1153. return result;
  1154. }
  1155. return null;
  1156. }
  1157. /// <summary>
  1158. /// Gets the target audio channels.
  1159. /// </summary>
  1160. /// <param name="codec">The codec.</param>
  1161. /// <returns>The target audio channels.</returns>
  1162. public int? GetTargetAudioChannels(string? codec)
  1163. {
  1164. var defaultValue = GlobalMaxAudioChannels ?? TranscodingMaxAudioChannels;
  1165. var value = GetOption(codec, "audiochannels");
  1166. if (string.IsNullOrEmpty(value))
  1167. {
  1168. return defaultValue;
  1169. }
  1170. if (int.TryParse(value, NumberStyles.Integer, CultureInfo.InvariantCulture, out var result))
  1171. {
  1172. return Math.Min(result, defaultValue ?? result);
  1173. }
  1174. return defaultValue;
  1175. }
  1176. /// <summary>
  1177. /// Gets the media stream count.
  1178. /// </summary>
  1179. /// <param name="type">The type.</param>
  1180. /// <param name="limit">The limit.</param>
  1181. /// <returns>The media stream count.</returns>
  1182. private int? GetMediaStreamCount(MediaStreamType type, int limit)
  1183. {
  1184. var count = MediaSource?.GetStreamCount(type);
  1185. if (count.HasValue)
  1186. {
  1187. count = Math.Min(count.Value, limit);
  1188. }
  1189. return count;
  1190. }
  1191. }