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