VideoHlsService.cs 6.1 KB

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