MediaSourceInfo.cs 6.4 KB

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