VideoHlsService.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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}.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(Request.PathInfo);
  30. file = Path.Combine(ApplicationPaths.EncodedMediaCachePath, file);
  31. return ToStaticFileResult(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. if (!state.Request.AudioCodec.HasValue)
  50. {
  51. return "-codec:a:0 copy";
  52. }
  53. var codec = GetAudioCodec(state.Request);
  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. return args;
  71. }
  72. return args;
  73. }
  74. /// <summary>
  75. /// Gets the video arguments.
  76. /// </summary>
  77. /// <param name="state">The state.</param>
  78. /// <returns>System.String.</returns>
  79. protected override string GetVideoArguments(StreamState state)
  80. {
  81. var codec = GetVideoCodec(state.VideoRequest);
  82. // Right now all we support is either h264 or copy
  83. if (!codec.Equals("copy", StringComparison.OrdinalIgnoreCase) && !codec.Equals("libx264", StringComparison.OrdinalIgnoreCase))
  84. {
  85. codec = "libx264";
  86. }
  87. // See if we can save come cpu cycles by avoiding encoding
  88. if (codec.Equals("copy", StringComparison.OrdinalIgnoreCase))
  89. {
  90. return IsH264(state.VideoStream) ? "-codec:v:0 copy -bsf h264_mp4toannexb" : "-codec:v:0 copy";
  91. }
  92. var args = "-codec:v:0 " + codec + " -preset superfast";
  93. if (state.VideoRequest.VideoBitRate.HasValue)
  94. {
  95. args += string.Format(" -b:v {0}", state.VideoRequest.VideoBitRate.Value);
  96. }
  97. // Add resolution params, if specified
  98. if (state.VideoRequest.Width.HasValue || state.VideoRequest.Height.HasValue || state.VideoRequest.MaxHeight.HasValue || state.VideoRequest.MaxWidth.HasValue)
  99. {
  100. args += GetOutputSizeParam(state, codec);
  101. }
  102. // Get the output framerate based on the FrameRate param
  103. double framerate = state.VideoRequest.Framerate ?? 0;
  104. // We have to supply a framerate for hls, so if it's null, account for that here
  105. if (framerate.Equals(0))
  106. {
  107. framerate = state.VideoStream.AverageFrameRate ?? 0;
  108. }
  109. if (framerate.Equals(0))
  110. {
  111. framerate = state.VideoStream.RealFrameRate ?? 0;
  112. }
  113. if (framerate.Equals(0))
  114. {
  115. framerate = 23.976;
  116. }
  117. args += string.Format(" -r {0}", framerate);
  118. // Needed to ensure segments stay under 10 seconds
  119. args += string.Format(" -g {0}", framerate);
  120. return args;
  121. }
  122. /// <summary>
  123. /// Gets the segment file extension.
  124. /// </summary>
  125. /// <param name="state">The state.</param>
  126. /// <returns>System.String.</returns>
  127. protected override string GetSegmentFileExtension(StreamState state)
  128. {
  129. return ".ts";
  130. }
  131. }
  132. }