VideoHlsService.cs 5.3 KB

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