MediaSourceInfo.cs 6.2 KB

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