MediaSourceInfo.cs 6.3 KB

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