MediaSourceInfo.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. #nullable disable
  2. #pragma warning disable CS1591
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Text.Json.Serialization;
  6. using Jellyfin.Data.Enums;
  7. using MediaBrowser.Model.Dlna;
  8. using MediaBrowser.Model.Entities;
  9. using MediaBrowser.Model.MediaInfo;
  10. using MediaBrowser.Model.Session;
  11. namespace MediaBrowser.Model.Dto
  12. {
  13. public class MediaSourceInfo
  14. {
  15. public MediaSourceInfo()
  16. {
  17. Formats = Array.Empty<string>();
  18. MediaStreams = Array.Empty<MediaStream>();
  19. MediaAttachments = Array.Empty<MediaAttachment>();
  20. RequiredHttpHeaders = new Dictionary<string, string>();
  21. SupportsTranscoding = true;
  22. SupportsDirectStream = true;
  23. SupportsDirectPlay = true;
  24. SupportsProbing = true;
  25. }
  26. public MediaProtocol Protocol { get; set; }
  27. public string Id { get; set; }
  28. public string Path { get; set; }
  29. public string EncoderPath { get; set; }
  30. public MediaProtocol? EncoderProtocol { get; set; }
  31. public MediaSourceType Type { get; set; }
  32. public string Container { get; set; }
  33. public long? Size { get; set; }
  34. public string Name { get; set; }
  35. /// <summary>
  36. /// Gets or sets a value indicating whether the media is remote.
  37. /// Differentiate internet url vs local network.
  38. /// </summary>
  39. public bool IsRemote { get; set; }
  40. public string ETag { get; set; }
  41. public long? RunTimeTicks { get; set; }
  42. public bool ReadAtNativeFramerate { get; set; }
  43. public bool IgnoreDts { get; set; }
  44. public bool IgnoreIndex { get; set; }
  45. public bool GenPtsInput { get; set; }
  46. public bool SupportsTranscoding { get; set; }
  47. public bool SupportsDirectStream { get; set; }
  48. public bool SupportsDirectPlay { get; set; }
  49. public bool IsInfiniteStream { get; set; }
  50. public bool RequiresOpening { get; set; }
  51. public string OpenToken { get; set; }
  52. public bool RequiresClosing { get; set; }
  53. public string LiveStreamId { get; set; }
  54. public int? BufferMs { get; set; }
  55. public bool RequiresLooping { get; set; }
  56. public bool SupportsProbing { get; set; }
  57. public VideoType? VideoType { get; set; }
  58. public IsoType? IsoType { get; set; }
  59. public Video3DFormat? Video3DFormat { get; set; }
  60. public IReadOnlyList<MediaStream> MediaStreams { get; set; }
  61. public IReadOnlyList<MediaAttachment> MediaAttachments { get; set; }
  62. public string[] Formats { get; set; }
  63. public int? Bitrate { get; set; }
  64. public TransportStreamTimestamp? Timestamp { get; set; }
  65. public Dictionary<string, string> RequiredHttpHeaders { get; set; }
  66. public string TranscodingUrl { get; set; }
  67. public MediaStreamProtocol TranscodingSubProtocol { get; set; }
  68. public string TranscodingContainer { get; set; }
  69. public int? AnalyzeDurationMs { get; set; }
  70. [JsonIgnore]
  71. public TranscodeReason TranscodeReasons { get; set; }
  72. public int? DefaultAudioStreamIndex { get; set; }
  73. public int? DefaultSubtitleStreamIndex { get; set; }
  74. [JsonIgnore]
  75. public MediaStream VideoStream
  76. {
  77. get
  78. {
  79. foreach (var i in MediaStreams)
  80. {
  81. if (i.Type == MediaStreamType.Video)
  82. {
  83. return i;
  84. }
  85. }
  86. return null;
  87. }
  88. }
  89. public void InferTotalBitrate(bool force = false)
  90. {
  91. if (MediaStreams is null)
  92. {
  93. return;
  94. }
  95. if (!force && Bitrate.HasValue)
  96. {
  97. return;
  98. }
  99. var bitrate = 0;
  100. foreach (var stream in MediaStreams)
  101. {
  102. if (!stream.IsExternal)
  103. {
  104. bitrate += stream.BitRate ?? 0;
  105. }
  106. }
  107. if (bitrate > 0)
  108. {
  109. Bitrate = bitrate;
  110. }
  111. }
  112. public MediaStream GetDefaultAudioStream(int? defaultIndex)
  113. {
  114. if (defaultIndex.HasValue && defaultIndex != -1)
  115. {
  116. var val = defaultIndex.Value;
  117. foreach (var i in MediaStreams)
  118. {
  119. if (i.Type == MediaStreamType.Audio && i.Index == val)
  120. {
  121. return i;
  122. }
  123. }
  124. }
  125. foreach (var i in MediaStreams)
  126. {
  127. if (i.Type == MediaStreamType.Audio && i.IsDefault)
  128. {
  129. return i;
  130. }
  131. }
  132. foreach (var i in MediaStreams)
  133. {
  134. if (i.Type == MediaStreamType.Audio)
  135. {
  136. return i;
  137. }
  138. }
  139. return null;
  140. }
  141. public MediaStream GetMediaStream(MediaStreamType type, int index)
  142. {
  143. foreach (var i in MediaStreams)
  144. {
  145. if (i.Type == type && i.Index == index)
  146. {
  147. return i;
  148. }
  149. }
  150. return null;
  151. }
  152. public int? GetStreamCount(MediaStreamType type)
  153. {
  154. int numMatches = 0;
  155. int numStreams = 0;
  156. foreach (var i in MediaStreams)
  157. {
  158. numStreams++;
  159. if (i.Type == type)
  160. {
  161. numMatches++;
  162. }
  163. }
  164. if (numStreams == 0)
  165. {
  166. return null;
  167. }
  168. return numMatches;
  169. }
  170. public bool? IsSecondaryAudio(MediaStream stream)
  171. {
  172. if (stream.IsExternal)
  173. {
  174. return false;
  175. }
  176. // Look for the first audio track
  177. foreach (var currentStream in MediaStreams)
  178. {
  179. if (currentStream.Type == MediaStreamType.Audio && !currentStream.IsExternal)
  180. {
  181. return currentStream.Index != stream.Index;
  182. }
  183. }
  184. return null;
  185. }
  186. }
  187. }