StreamInfo.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. using MediaBrowser.Model.Dto;
  2. using MediaBrowser.Model.Entities;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Globalization;
  6. using System.Linq;
  7. namespace MediaBrowser.Model.Dlna
  8. {
  9. /// <summary>
  10. /// Class StreamInfo.
  11. /// </summary>
  12. public class StreamInfo
  13. {
  14. public string ItemId { get; set; }
  15. public bool IsDirectStream { get; set; }
  16. public DlnaProfileType MediaType { get; set; }
  17. public string Container { get; set; }
  18. public string Protocol { get; set; }
  19. public long StartPositionTicks { get; set; }
  20. public string VideoCodec { get; set; }
  21. public string AudioCodec { get; set; }
  22. public int? AudioStreamIndex { get; set; }
  23. public int? SubtitleStreamIndex { get; set; }
  24. public int? MaxAudioChannels { get; set; }
  25. public int? AudioBitrate { get; set; }
  26. public int? VideoBitrate { get; set; }
  27. public int? VideoLevel { get; set; }
  28. public int? MaxWidth { get; set; }
  29. public int? MaxHeight { get; set; }
  30. public int? MaxFramerate { get; set; }
  31. public string DeviceProfileId { get; set; }
  32. public string DeviceId { get; set; }
  33. public long? RunTimeTicks { get; set; }
  34. public TranscodeSeekInfo TranscodeSeekInfo { get; set; }
  35. public bool EstimateContentLength { get; set; }
  36. public MediaSourceInfo MediaSource { get; set; }
  37. public string MediaSourceId
  38. {
  39. get
  40. {
  41. return MediaSource == null ? null : MediaSource.Id;
  42. }
  43. }
  44. public string ToUrl(string baseUrl)
  45. {
  46. return ToDlnaUrl(baseUrl);
  47. }
  48. public string ToDlnaUrl(string baseUrl)
  49. {
  50. if (string.IsNullOrEmpty(baseUrl))
  51. {
  52. throw new ArgumentNullException(baseUrl);
  53. }
  54. var dlnaCommand = BuildDlnaParam(this);
  55. var extension = string.IsNullOrEmpty(Container) ? string.Empty : "." + Container;
  56. baseUrl = baseUrl.TrimEnd('/');
  57. if (MediaType == DlnaProfileType.Audio)
  58. {
  59. return string.Format("{0}/audio/{1}/stream{2}?{3}", baseUrl, ItemId, extension, dlnaCommand);
  60. }
  61. if (string.Equals(Protocol, "hls", StringComparison.OrdinalIgnoreCase))
  62. {
  63. return string.Format("{0}/videos/{1}/stream.m3u8?{2}", baseUrl, ItemId, dlnaCommand);
  64. }
  65. return string.Format("{0}/videos/{1}/stream{2}?{3}", baseUrl, ItemId, extension, dlnaCommand);
  66. }
  67. private static string BuildDlnaParam(StreamInfo item)
  68. {
  69. var usCulture = new CultureInfo("en-US");
  70. var list = new List<string>
  71. {
  72. item.DeviceProfileId ?? string.Empty,
  73. item.DeviceId ?? string.Empty,
  74. item.MediaSourceId ?? string.Empty,
  75. (item.IsDirectStream).ToString().ToLower(),
  76. item.VideoCodec ?? string.Empty,
  77. item.AudioCodec ?? string.Empty,
  78. item.AudioStreamIndex.HasValue ? item.AudioStreamIndex.Value.ToString(usCulture) : string.Empty,
  79. item.SubtitleStreamIndex.HasValue ? item.SubtitleStreamIndex.Value.ToString(usCulture) : string.Empty,
  80. item.VideoBitrate.HasValue ? item.VideoBitrate.Value.ToString(usCulture) : string.Empty,
  81. item.AudioBitrate.HasValue ? item.AudioBitrate.Value.ToString(usCulture) : string.Empty,
  82. item.MaxAudioChannels.HasValue ? item.MaxAudioChannels.Value.ToString(usCulture) : string.Empty,
  83. item.MaxFramerate.HasValue ? item.MaxFramerate.Value.ToString(usCulture) : string.Empty,
  84. item.MaxWidth.HasValue ? item.MaxWidth.Value.ToString(usCulture) : string.Empty,
  85. item.MaxHeight.HasValue ? item.MaxHeight.Value.ToString(usCulture) : string.Empty,
  86. item.StartPositionTicks.ToString(usCulture),
  87. item.VideoLevel.HasValue ? item.VideoLevel.Value.ToString(usCulture) : string.Empty
  88. };
  89. return string.Format("Params={0}", string.Join(";", list.ToArray()));
  90. }
  91. /// <summary>
  92. /// Returns the audio stream that will be used
  93. /// </summary>
  94. public MediaStream TargetAudioStream
  95. {
  96. get
  97. {
  98. if (MediaSource != null)
  99. {
  100. var audioStreams = MediaSource.MediaStreams.Where(i => i.Type == MediaStreamType.Audio);
  101. if (AudioStreamIndex.HasValue)
  102. {
  103. return audioStreams.FirstOrDefault(i => i.Index == AudioStreamIndex.Value);
  104. }
  105. return audioStreams.FirstOrDefault();
  106. }
  107. return null;
  108. }
  109. }
  110. /// <summary>
  111. /// Returns the video stream that will be used
  112. /// </summary>
  113. public MediaStream TargetVideoStream
  114. {
  115. get
  116. {
  117. if (MediaSource != null)
  118. {
  119. return MediaSource.MediaStreams
  120. .FirstOrDefault(i => i.Type == MediaStreamType.Video && (i.Codec ?? string.Empty).IndexOf("jpeg", StringComparison.OrdinalIgnoreCase) == -1);
  121. }
  122. return null;
  123. }
  124. }
  125. /// <summary>
  126. /// Predicts the audio sample rate that will be in the output stream
  127. /// </summary>
  128. public int? TargetAudioSampleRate
  129. {
  130. get
  131. {
  132. var stream = TargetAudioStream;
  133. return stream == null ? null : stream.SampleRate;
  134. }
  135. }
  136. /// <summary>
  137. /// Predicts the audio bitrate that will be in the output stream
  138. /// </summary>
  139. public int? TargetAudioBitrate
  140. {
  141. get
  142. {
  143. var stream = TargetAudioStream;
  144. return AudioBitrate.HasValue && !IsDirectStream
  145. ? AudioBitrate
  146. : stream == null ? null : stream.BitRate;
  147. }
  148. }
  149. /// <summary>
  150. /// Predicts the audio channels that will be in the output stream
  151. /// </summary>
  152. public int? TargetAudioChannels
  153. {
  154. get
  155. {
  156. var stream = TargetAudioStream;
  157. return MaxAudioChannels.HasValue && !IsDirectStream
  158. ? (stream.Channels.HasValue ? Math.Min(MaxAudioChannels.Value, stream.Channels.Value) : MaxAudioChannels.Value)
  159. : stream == null ? null : stream.Channels;
  160. }
  161. }
  162. /// <summary>
  163. /// Predicts the audio codec that will be in the output stream
  164. /// </summary>
  165. public string TargetAudioCodec
  166. {
  167. get
  168. {
  169. var stream = TargetAudioStream;
  170. return IsDirectStream
  171. ? (stream == null ? null : stream.Codec)
  172. : AudioCodec;
  173. }
  174. }
  175. /// <summary>
  176. /// Predicts the audio channels that will be in the output stream
  177. /// </summary>
  178. public long? TargetSize
  179. {
  180. get
  181. {
  182. if (IsDirectStream)
  183. {
  184. return MediaSource.Bitrate;
  185. }
  186. if (RunTimeTicks.HasValue)
  187. {
  188. var totalBitrate = 0;
  189. if (AudioBitrate.HasValue)
  190. {
  191. totalBitrate += AudioBitrate.Value;
  192. }
  193. if (VideoBitrate.HasValue)
  194. {
  195. totalBitrate += VideoBitrate.Value;
  196. }
  197. return Convert.ToInt64(totalBitrate * TimeSpan.FromTicks(RunTimeTicks.Value).TotalSeconds);
  198. }
  199. var stream = TargetAudioStream;
  200. return MaxAudioChannels.HasValue && !IsDirectStream
  201. ? (stream.Channels.HasValue ? Math.Min(MaxAudioChannels.Value, stream.Channels.Value) : MaxAudioChannels.Value)
  202. : stream == null ? null : stream.Channels;
  203. }
  204. }
  205. }
  206. /// <summary>
  207. /// Class AudioOptions.
  208. /// </summary>
  209. public class AudioOptions
  210. {
  211. public string ItemId { get; set; }
  212. public List<MediaSourceInfo> MediaSources { get; set; }
  213. public DeviceProfile Profile { get; set; }
  214. /// <summary>
  215. /// Optional. Only needed if a specific AudioStreamIndex or SubtitleStreamIndex are requested.
  216. /// </summary>
  217. public string MediaSourceId { get; set; }
  218. public string DeviceId { get; set; }
  219. /// <summary>
  220. /// Allows an override of supported number of audio channels
  221. /// Example: DeviceProfile supports five channel, but user only has stereo speakers
  222. /// </summary>
  223. public int? MaxAudioChannels { get; set; }
  224. /// <summary>
  225. /// The application's configured quality setting
  226. /// </summary>
  227. public int? MaxBitrate { get; set; }
  228. }
  229. /// <summary>
  230. /// Class VideoOptions.
  231. /// </summary>
  232. public class VideoOptions : AudioOptions
  233. {
  234. public int? AudioStreamIndex { get; set; }
  235. public int? SubtitleStreamIndex { get; set; }
  236. public int? MaxAudioTranscodingBitrate { get; set; }
  237. public VideoOptions()
  238. {
  239. MaxAudioTranscodingBitrate = 128000;
  240. }
  241. }
  242. }