VideoHlsService.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. if (string.Equals(codec, "aac", StringComparison.OrdinalIgnoreCase))
  89. {
  90. args += " -strict experimental";
  91. }
  92. var channels = GetNumAudioChannelsParam(state.Request, state.AudioStream);
  93. if (channels.HasValue)
  94. {
  95. args += " -ac " + channels.Value;
  96. }
  97. if (state.Request.AudioSampleRate.HasValue)
  98. {
  99. args += " -ar " + state.Request.AudioSampleRate.Value;
  100. }
  101. if (state.Request.AudioBitRate.HasValue)
  102. {
  103. args += " -ab " + state.Request.AudioBitRate.Value;
  104. }
  105. var volParam = string.Empty;
  106. // Boost volume to 200% when downsampling from 6ch to 2ch
  107. if (channels.HasValue && channels.Value <= 2 && state.AudioStream.Channels.HasValue && state.AudioStream.Channels.Value > 5)
  108. {
  109. volParam = ",volume=2.000000";
  110. }
  111. args += string.Format(" -af \"aresample=async=1000{0}\"", volParam);
  112. return args;
  113. }
  114. return args;
  115. }
  116. /// <summary>
  117. /// Gets the video arguments.
  118. /// </summary>
  119. /// <param name="state">The state.</param>
  120. /// <param name="performSubtitleConversion">if set to <c>true</c> [perform subtitle conversion].</param>
  121. /// <returns>System.String.</returns>
  122. protected override string GetVideoArguments(StreamState state, bool performSubtitleConversion)
  123. {
  124. var codec = GetVideoCodec(state.VideoRequest);
  125. // See if we can save come cpu cycles by avoiding encoding
  126. if (codec.Equals("copy", StringComparison.OrdinalIgnoreCase))
  127. {
  128. return IsH264(state.VideoStream) ? "-codec:v:0 copy -bsf h264_mp4toannexb" : "-codec:v:0 copy";
  129. }
  130. const string keyFrameArg = " -force_key_frames expr:if(isnan(prev_forced_t),gte(t,0),gte(t,prev_forced_t+5))";
  131. var args = "-codec:v:0 " + codec + " -preset superfast" + keyFrameArg;
  132. if (state.VideoRequest.VideoBitRate.HasValue)
  133. {
  134. args += string.Format(" -b:v {0}", state.VideoRequest.VideoBitRate.Value);
  135. }
  136. // Add resolution params, if specified
  137. if (state.VideoRequest.Width.HasValue || state.VideoRequest.Height.HasValue || state.VideoRequest.MaxHeight.HasValue || state.VideoRequest.MaxWidth.HasValue)
  138. {
  139. args += GetOutputSizeParam(state, codec, performSubtitleConversion);
  140. }
  141. // Get the output framerate based on the FrameRate param
  142. var framerate = state.VideoRequest.Framerate ?? 0;
  143. // We have to supply a framerate for hls, so if it's null, account for that here
  144. if (framerate.Equals(0))
  145. {
  146. framerate = state.VideoStream.AverageFrameRate ?? 0;
  147. }
  148. if (framerate.Equals(0))
  149. {
  150. framerate = state.VideoStream.RealFrameRate ?? 0;
  151. }
  152. if (framerate.Equals(0))
  153. {
  154. framerate = 23.976;
  155. }
  156. framerate = Math.Round(framerate);
  157. args += string.Format(" -r {0}", framerate);
  158. args += " -vsync vfr";
  159. if (!string.IsNullOrEmpty(state.VideoRequest.Profile))
  160. {
  161. args += " -profile:v " + state.VideoRequest.Profile;
  162. }
  163. if (!string.IsNullOrEmpty(state.VideoRequest.Level))
  164. {
  165. args += " -level " + state.VideoRequest.Level;
  166. }
  167. if (state.SubtitleStream != null)
  168. {
  169. // This is for internal graphical subs
  170. if (!state.SubtitleStream.IsExternal && (state.SubtitleStream.Codec.IndexOf("pgs", StringComparison.OrdinalIgnoreCase) != -1 || state.SubtitleStream.Codec.IndexOf("dvd", StringComparison.OrdinalIgnoreCase) != -1))
  171. {
  172. args += GetInternalGraphicalSubtitleParam(state, codec);
  173. }
  174. }
  175. return args;
  176. }
  177. /// <summary>
  178. /// Gets the segment file extension.
  179. /// </summary>
  180. /// <param name="state">The state.</param>
  181. /// <returns>System.String.</returns>
  182. protected override string GetSegmentFileExtension(StreamState state)
  183. {
  184. return ".ts";
  185. }
  186. }
  187. }