MediaSourceInfo.cs 6.3 KB

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