MediaSourceInfo.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. using MediaBrowser.Model.Entities;
  2. using MediaBrowser.Model.Extensions;
  3. using MediaBrowser.Model.MediaInfo;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using MediaBrowser.Model.Serialization;
  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 MediaSourceType Type { get; set; }
  16. public string Container { get; set; }
  17. public long? Size { get; set; }
  18. public string Name { get; set; }
  19. /// <summary>
  20. /// Differentiate internet url vs local network
  21. /// </summary>
  22. public bool IsRemote { get; set; }
  23. public string ETag { get; set; }
  24. public long? RunTimeTicks { get; set; }
  25. public bool ReadAtNativeFramerate { get; set; }
  26. public bool IgnoreDts { get; set; }
  27. public bool IgnoreIndex { get; set; }
  28. public bool GenPtsInput { get; set; }
  29. public bool SupportsTranscoding { get; set; }
  30. public bool SupportsDirectStream { get; set; }
  31. public bool SupportsDirectPlay { get; set; }
  32. public bool IsInfiniteStream { get; set; }
  33. public bool RequiresOpening { get; set; }
  34. public string OpenToken { get; set; }
  35. public bool RequiresClosing { get; set; }
  36. public bool SupportsProbing { get; set; }
  37. public string LiveStreamId { get; set; }
  38. public int? BufferMs { get; set; }
  39. public bool RequiresLooping { get; set; }
  40. public VideoType? VideoType { get; set; }
  41. public IsoType? IsoType { get; set; }
  42. public Video3DFormat? Video3DFormat { get; set; }
  43. public List<MediaStream> MediaStreams { get; set; }
  44. public List<string> PlayableStreamFileNames { get; set; }
  45. public List<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 = new List<string>();
  56. MediaStreams = new List<MediaStream>();
  57. RequiredHttpHeaders = new Dictionary<string, string>();
  58. PlayableStreamFileNames = new List<string>();
  59. SupportsTranscoding = true;
  60. SupportsDirectStream = true;
  61. SupportsDirectPlay = true;
  62. SupportsProbing = true;
  63. }
  64. public void InferTotalBitrate(bool force = false)
  65. {
  66. if (MediaStreams == null)
  67. {
  68. return;
  69. }
  70. if (!force && Bitrate.HasValue)
  71. {
  72. return;
  73. }
  74. var internalStreams = MediaStreams
  75. .Where(i => !i.IsExternal)
  76. .ToList();
  77. if (internalStreams.Count == 0)
  78. {
  79. return;
  80. }
  81. var bitrate = internalStreams
  82. .Select(m => m.BitRate ?? 0)
  83. .Sum();
  84. if (bitrate > 0)
  85. {
  86. Bitrate = bitrate;
  87. }
  88. }
  89. [IgnoreDataMember]
  90. public List<TranscodeReason> TranscodeReasons { get; set; }
  91. public int? DefaultAudioStreamIndex { get; set; }
  92. public int? DefaultSubtitleStreamIndex { get; set; }
  93. [IgnoreDataMember]
  94. public MediaStream DefaultAudioStream
  95. {
  96. get { return GetDefaultAudioStream(DefaultAudioStreamIndex); }
  97. }
  98. public MediaStream GetDefaultAudioStream(int? defaultIndex)
  99. {
  100. if (defaultIndex.HasValue)
  101. {
  102. var val = defaultIndex.Value;
  103. foreach (MediaStream i in MediaStreams)
  104. {
  105. if (i.Type == MediaStreamType.Audio && i.Index == val)
  106. {
  107. return i;
  108. }
  109. }
  110. }
  111. foreach (MediaStream i in MediaStreams)
  112. {
  113. if (i.Type == MediaStreamType.Audio && i.IsDefault)
  114. {
  115. return i;
  116. }
  117. }
  118. foreach (MediaStream i in MediaStreams)
  119. {
  120. if (i.Type == MediaStreamType.Audio)
  121. {
  122. return i;
  123. }
  124. }
  125. return null;
  126. }
  127. [IgnoreDataMember]
  128. public MediaStream VideoStream
  129. {
  130. get
  131. {
  132. foreach (MediaStream i in MediaStreams)
  133. {
  134. if (i.Type == MediaStreamType.Video && StringHelper.IndexOfIgnoreCase(i.Codec ?? string.Empty, "jpeg") == -1)
  135. {
  136. return i;
  137. }
  138. }
  139. return null;
  140. }
  141. }
  142. public MediaStream GetMediaStream(MediaStreamType type, int index)
  143. {
  144. foreach (MediaStream 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 (MediaStream 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. // Look for the first audio track marked as default
  174. foreach (MediaStream currentStream in MediaStreams)
  175. {
  176. if (currentStream.Type == MediaStreamType.Audio && currentStream.IsDefault)
  177. {
  178. return currentStream.Index != stream.Index;
  179. }
  180. }
  181. // Look for the first audio track
  182. foreach (MediaStream currentStream in MediaStreams)
  183. {
  184. if (currentStream.Type == MediaStreamType.Audio)
  185. {
  186. return currentStream.Index != stream.Index;
  187. }
  188. }
  189. return null;
  190. }
  191. }
  192. }