VideoHlsService.cs 6.4 KB

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