StreamInfo.cs 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397
  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. // Default to using the API URL
  1077. info.Url = string.Format(
  1078. CultureInfo.InvariantCulture,
  1079. "{0}/Videos/{1}/{2}/Subtitles/{3}/{4}/Stream.{5}",
  1080. baseUrl,
  1081. ItemId,
  1082. MediaSourceId,
  1083. stream.Index.ToString(CultureInfo.InvariantCulture),
  1084. startPositionTicks.ToString(CultureInfo.InvariantCulture),
  1085. subtitleProfile.Format);
  1086. info.IsExternalUrl = false; // Default to API URL
  1087. // Check conditions for potentially using the direct path
  1088. if (stream.IsExternal // Must be external
  1089. && MediaSource?.Protocol != MediaProtocol.File // Main media must not be a local file
  1090. && string.Equals(stream.Codec, subtitleProfile.Format, StringComparison.OrdinalIgnoreCase) // Format must match (no conversion needed)
  1091. && !string.IsNullOrEmpty(stream.Path) // Path must exist
  1092. && Uri.TryCreate(stream.Path, UriKind.Absolute, out Uri? uriResult) // Path must be an absolute URI
  1093. && (uriResult.Scheme == Uri.UriSchemeHttp || uriResult.Scheme == Uri.UriSchemeHttps)) // Scheme must be HTTP or HTTPS
  1094. {
  1095. // All conditions met, override with the direct path
  1096. info.Url = stream.Path;
  1097. info.IsExternalUrl = true;
  1098. }
  1099. // Append ApiKey only if we are using the API URL
  1100. if (!info.IsExternalUrl && !string.IsNullOrEmpty(accessToken))
  1101. {
  1102. // Use "?ApiKey=" as seen in HEAD and other parts of the code
  1103. info.Url += "?ApiKey=" + accessToken;
  1104. }
  1105. }
  1106. return info;
  1107. }
  1108. /// <summary>
  1109. /// Gets the target video bit depth.
  1110. /// </summary>
  1111. /// <param name="codec">The codec.</param>
  1112. /// <returns>The target video bit depth.</returns>
  1113. public int? GetTargetVideoBitDepth(string? codec)
  1114. {
  1115. var value = GetOption(codec, "videobitdepth");
  1116. if (int.TryParse(value, CultureInfo.InvariantCulture, out var result))
  1117. {
  1118. return result;
  1119. }
  1120. return null;
  1121. }
  1122. /// <summary>
  1123. /// Gets the target audio bit depth.
  1124. /// </summary>
  1125. /// <param name="codec">The codec.</param>
  1126. /// <returns>The target audio bit depth.</returns>
  1127. public int? GetTargetAudioBitDepth(string? codec)
  1128. {
  1129. var value = GetOption(codec, "audiobitdepth");
  1130. if (int.TryParse(value, CultureInfo.InvariantCulture, out var result))
  1131. {
  1132. return result;
  1133. }
  1134. return null;
  1135. }
  1136. /// <summary>
  1137. /// Gets the target video level.
  1138. /// </summary>
  1139. /// <param name="codec">The codec.</param>
  1140. /// <returns>The target video level.</returns>
  1141. public double? GetTargetVideoLevel(string? codec)
  1142. {
  1143. var value = GetOption(codec, "level");
  1144. if (double.TryParse(value, CultureInfo.InvariantCulture, out var result))
  1145. {
  1146. return result;
  1147. }
  1148. return null;
  1149. }
  1150. /// <summary>
  1151. /// Gets the target reference frames.
  1152. /// </summary>
  1153. /// <param name="codec">The codec.</param>
  1154. /// <returns>The target reference frames.</returns>
  1155. public int? GetTargetRefFrames(string? codec)
  1156. {
  1157. var value = GetOption(codec, "maxrefframes");
  1158. if (int.TryParse(value, CultureInfo.InvariantCulture, out var result))
  1159. {
  1160. return result;
  1161. }
  1162. return null;
  1163. }
  1164. /// <summary>
  1165. /// Gets the target audio channels.
  1166. /// </summary>
  1167. /// <param name="codec">The codec.</param>
  1168. /// <returns>The target audio channels.</returns>
  1169. public int? GetTargetAudioChannels(string? codec)
  1170. {
  1171. var defaultValue = GlobalMaxAudioChannels ?? TranscodingMaxAudioChannels;
  1172. var value = GetOption(codec, "audiochannels");
  1173. if (string.IsNullOrEmpty(value))
  1174. {
  1175. return defaultValue;
  1176. }
  1177. if (int.TryParse(value, NumberStyles.Integer, CultureInfo.InvariantCulture, out var result))
  1178. {
  1179. return Math.Min(result, defaultValue ?? result);
  1180. }
  1181. return defaultValue;
  1182. }
  1183. /// <summary>
  1184. /// Gets the media stream count.
  1185. /// </summary>
  1186. /// <param name="type">The type.</param>
  1187. /// <param name="limit">The limit.</param>
  1188. /// <returns>The media stream count.</returns>
  1189. private int? GetMediaStreamCount(MediaStreamType type, int limit)
  1190. {
  1191. var count = MediaSource?.GetStreamCount(type);
  1192. if (count.HasValue)
  1193. {
  1194. count = Math.Min(count.Value, limit);
  1195. }
  1196. return count;
  1197. }
  1198. }