VideoHlsService.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. using System.IO;
  2. using MediaBrowser.Common.IO;
  3. using MediaBrowser.Common.MediaInfo;
  4. using MediaBrowser.Controller;
  5. using MediaBrowser.Controller.Library;
  6. using System;
  7. using ServiceStack.ServiceHost;
  8. namespace MediaBrowser.Api.Playback.Hls
  9. {
  10. /// <summary>
  11. /// Class GetHlsVideoStream
  12. /// </summary>
  13. [Route("/Videos/{Id}/stream.m3u8", "GET")]
  14. [Api(Description = "Gets a video stream using HTTP live streaming.")]
  15. public class GetHlsVideoStream : VideoStreamRequest
  16. {
  17. }
  18. /// <summary>
  19. /// Class GetHlsVideoSegment
  20. /// </summary>
  21. [Route("/Videos/{Id}/segments/{SegmentId}/stream.ts", "GET")]
  22. [Api(Description = "Gets an Http live streaming segment file. Internal use only.")]
  23. public class GetHlsVideoSegment
  24. {
  25. /// <summary>
  26. /// Gets or sets the id.
  27. /// </summary>
  28. /// <value>The id.</value>
  29. public string Id { get; set; }
  30. /// <summary>
  31. /// Gets or sets the segment id.
  32. /// </summary>
  33. /// <value>The segment id.</value>
  34. public string SegmentId { get; set; }
  35. }
  36. /// <summary>
  37. /// Class VideoHlsService
  38. /// </summary>
  39. public class VideoHlsService : BaseHlsService
  40. {
  41. /// <summary>
  42. /// Initializes a new instance of the <see cref="BaseStreamingService" /> class.
  43. /// </summary>
  44. /// <param name="appPaths">The app paths.</param>
  45. /// <param name="userManager">The user manager.</param>
  46. /// <param name="libraryManager">The library manager.</param>
  47. /// <param name="isoManager">The iso manager.</param>
  48. /// <param name="mediaEncoder">The media encoder.</param>
  49. public VideoHlsService(IServerApplicationPaths appPaths, IUserManager userManager, ILibraryManager libraryManager, IIsoManager isoManager, IMediaEncoder mediaEncoder)
  50. : base(appPaths, userManager, libraryManager, isoManager, mediaEncoder)
  51. {
  52. }
  53. /// <summary>
  54. /// Gets the specified request.
  55. /// </summary>
  56. /// <param name="request">The request.</param>
  57. /// <returns>System.Object.</returns>
  58. public object Get(GetHlsVideoSegment request)
  59. {
  60. var file = SegmentFilePrefix + request.SegmentId + Path.GetExtension(RequestContext.PathInfo);
  61. file = Path.Combine(ApplicationPaths.EncodedMediaCachePath, file);
  62. return ResultFactory.GetStaticFileResult(RequestContext, file);
  63. }
  64. /// <summary>
  65. /// Gets the specified request.
  66. /// </summary>
  67. /// <param name="request">The request.</param>
  68. /// <returns>System.Object.</returns>
  69. public object Get(GetHlsVideoStream request)
  70. {
  71. return ProcessRequest(request);
  72. }
  73. /// <summary>
  74. /// Gets the audio arguments.
  75. /// </summary>
  76. /// <param name="state">The state.</param>
  77. /// <returns>System.String.</returns>
  78. protected override string GetAudioArguments(StreamState state)
  79. {
  80. var codec = GetAudioCodec(state.Request);
  81. if (codec.Equals("copy", StringComparison.OrdinalIgnoreCase))
  82. {
  83. return "-codec:a:0 copy";
  84. }
  85. var args = "-codec:a:0 " + codec;
  86. if (state.AudioStream != null)
  87. {
  88. var channels = GetNumAudioChannelsParam(state.Request, state.AudioStream);
  89. if (channels.HasValue)
  90. {
  91. args += " -ac " + channels.Value;
  92. }
  93. if (state.Request.AudioSampleRate.HasValue)
  94. {
  95. args += " -ar " + state.Request.AudioSampleRate.Value;
  96. }
  97. if (state.Request.AudioBitRate.HasValue)
  98. {
  99. args += " -ab " + state.Request.AudioBitRate.Value;
  100. }
  101. var volParam = string.Empty;
  102. // Boost volume to 200% when downsampling from 6ch to 2ch
  103. if (channels.HasValue && channels.Value <= 2 && state.AudioStream.Channels.HasValue && state.AudioStream.Channels.Value > 5)
  104. {
  105. volParam = ",volume=2.000000";
  106. }
  107. args += string.Format(" -af \"aresample=async=1000{0}\"", volParam);
  108. return args;
  109. }
  110. return args;
  111. }
  112. /// <summary>
  113. /// Gets the video arguments.
  114. /// </summary>
  115. /// <param name="state">The state.</param>
  116. /// <param name="performSubtitleConversion">if set to <c>true</c> [perform subtitle conversion].</param>
  117. /// <returns>System.String.</returns>
  118. protected override string GetVideoArguments(StreamState state, bool performSubtitleConversion)
  119. {
  120. var codec = GetVideoCodec(state.VideoRequest);
  121. // See if we can save come cpu cycles by avoiding encoding
  122. if (codec.Equals("copy", StringComparison.OrdinalIgnoreCase))
  123. {
  124. return IsH264(state.VideoStream) ? "-codec:v:0 copy -bsf h264_mp4toannexb" : "-codec:v:0 copy";
  125. }
  126. const string keyFrameArg = " -force_key_frames expr:if(isnan(prev_forced_t),gte(t,0),gte(t,prev_forced_t+5))";
  127. var args = "-codec:v:0 " + codec + " -preset superfast" + keyFrameArg;
  128. if (state.VideoRequest.VideoBitRate.HasValue)
  129. {
  130. args += string.Format(" -b:v {0}", state.VideoRequest.VideoBitRate.Value);
  131. }
  132. // Add resolution params, if specified
  133. if (state.VideoRequest.Width.HasValue || state.VideoRequest.Height.HasValue || state.VideoRequest.MaxHeight.HasValue || state.VideoRequest.MaxWidth.HasValue)
  134. {
  135. args += GetOutputSizeParam(state, codec, performSubtitleConversion);
  136. }
  137. // Get the output framerate based on the FrameRate param
  138. var framerate = state.VideoRequest.Framerate ?? 0;
  139. // We have to supply a framerate for hls, so if it's null, account for that here
  140. if (framerate.Equals(0))
  141. {
  142. framerate = state.VideoStream.AverageFrameRate ?? 0;
  143. }
  144. if (framerate.Equals(0))
  145. {
  146. framerate = state.VideoStream.RealFrameRate ?? 0;
  147. }
  148. if (framerate.Equals(0))
  149. {
  150. framerate = 23.976;
  151. }
  152. framerate = Math.Round(framerate);
  153. args += string.Format(" -r {0}", framerate);
  154. args += " -vsync vfr";
  155. if (!string.IsNullOrEmpty(state.VideoRequest.Profile))
  156. {
  157. args += " -profile:v " + state.VideoRequest.Profile;
  158. }
  159. if (!string.IsNullOrEmpty(state.VideoRequest.Level))
  160. {
  161. args += " -level " + state.VideoRequest.Level;
  162. }
  163. if (state.SubtitleStream != null)
  164. {
  165. // This is for internal graphical subs
  166. if (!state.SubtitleStream.IsExternal && (state.SubtitleStream.Codec.IndexOf("pgs", StringComparison.OrdinalIgnoreCase) != -1 || state.SubtitleStream.Codec.IndexOf("dvd", StringComparison.OrdinalIgnoreCase) != -1))
  167. {
  168. args += GetInternalGraphicalSubtitleParam(state, codec);
  169. }
  170. }
  171. return args;
  172. }
  173. /// <summary>
  174. /// Gets the segment file extension.
  175. /// </summary>
  176. /// <param name="state">The state.</param>
  177. /// <returns>System.String.</returns>
  178. protected override string GetSegmentFileExtension(StreamState state)
  179. {
  180. return ".ts";
  181. }
  182. }
  183. }