MediaSourceInfo.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. public bool HasSegments { get; set; }
  75. [JsonIgnore]
  76. public MediaStream VideoStream
  77. {
  78. get
  79. {
  80. foreach (var i in MediaStreams)
  81. {
  82. if (i.Type == MediaStreamType.Video)
  83. {
  84. return i;
  85. }
  86. }
  87. return null;
  88. }
  89. }
  90. public void InferTotalBitrate(bool force = false)
  91. {
  92. if (MediaStreams is null)
  93. {
  94. return;
  95. }
  96. if (!force && Bitrate.HasValue)
  97. {
  98. return;
  99. }
  100. var bitrate = 0;
  101. foreach (var stream in MediaStreams)
  102. {
  103. if (!stream.IsExternal)
  104. {
  105. bitrate += stream.BitRate ?? 0;
  106. }
  107. }
  108. if (bitrate > 0)
  109. {
  110. Bitrate = bitrate;
  111. }
  112. }
  113. public MediaStream GetDefaultAudioStream(int? defaultIndex)
  114. {
  115. if (defaultIndex.HasValue && defaultIndex != -1)
  116. {
  117. var val = defaultIndex.Value;
  118. foreach (var i in MediaStreams)
  119. {
  120. if (i.Type == MediaStreamType.Audio && i.Index == val)
  121. {
  122. return i;
  123. }
  124. }
  125. }
  126. foreach (var i in MediaStreams)
  127. {
  128. if (i.Type == MediaStreamType.Audio && i.IsDefault)
  129. {
  130. return i;
  131. }
  132. }
  133. foreach (var i in MediaStreams)
  134. {
  135. if (i.Type == MediaStreamType.Audio)
  136. {
  137. return i;
  138. }
  139. }
  140. return null;
  141. }
  142. public MediaStream GetMediaStream(MediaStreamType type, int index)
  143. {
  144. foreach (var i in MediaStreams)
  145. {
  146. if (i.Type == type && i.Index == index)
  147. {
  148. return i;
  149. }
  150. }
  151. return null;
  152. }
  153. public int? GetStreamCount(MediaStreamType type)
  154. {
  155. int numMatches = 0;
  156. int numStreams = 0;
  157. foreach (var i in MediaStreams)
  158. {
  159. numStreams++;
  160. if (i.Type == type)
  161. {
  162. numMatches++;
  163. }
  164. }
  165. if (numStreams == 0)
  166. {
  167. return null;
  168. }
  169. return numMatches;
  170. }
  171. public bool? IsSecondaryAudio(MediaStream stream)
  172. {
  173. if (stream.IsExternal)
  174. {
  175. return false;
  176. }
  177. // Look for the first audio track
  178. foreach (var currentStream in MediaStreams)
  179. {
  180. if (currentStream.Type == MediaStreamType.Audio && !currentStream.IsExternal)
  181. {
  182. return currentStream.Index != stream.Index;
  183. }
  184. }
  185. return null;
  186. }
  187. }
  188. }