StreamInfo.cs 9.2 KB

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