HlsSegmentHandler.cs 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. using MediaBrowser.Common.Net.Handlers;
  2. using MediaBrowser.Controller;
  3. using System;
  4. using System.IO;
  5. using System.Net;
  6. using System.Threading.Tasks;
  7. namespace MediaBrowser.Api.Streaming
  8. {
  9. /// <summary>
  10. /// Class HlsSegmentHandler
  11. /// </summary>
  12. public class HlsSegmentHandler : BaseHandler<Kernel>
  13. {
  14. /// <summary>
  15. /// The segment file prefix
  16. /// </summary>
  17. public const string SegmentFilePrefix = "segment-";
  18. /// <summary>
  19. /// Handleses the request.
  20. /// </summary>
  21. /// <param name="request">The request.</param>
  22. /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
  23. public override bool HandlesRequest(HttpListenerRequest request)
  24. {
  25. const string url = "/api/" + SegmentFilePrefix;
  26. return request.Url.LocalPath.IndexOf(url, StringComparison.OrdinalIgnoreCase) != -1;
  27. }
  28. /// <summary>
  29. /// Writes the response to output stream.
  30. /// </summary>
  31. /// <param name="stream">The stream.</param>
  32. /// <param name="responseInfo">The response info.</param>
  33. /// <param name="contentLength">Length of the content.</param>
  34. /// <returns>Task.</returns>
  35. /// <exception cref="System.NotImplementedException"></exception>
  36. /// <exception cref="NotImplementedException"></exception>
  37. protected override Task WriteResponseToOutputStream(Stream stream, ResponseInfo responseInfo, long? contentLength)
  38. {
  39. throw new NotImplementedException();
  40. }
  41. /// <summary>
  42. /// Gets the response info.
  43. /// </summary>
  44. /// <returns>Task{ResponseInfo}.</returns>
  45. /// <exception cref="System.NotImplementedException"></exception>
  46. /// <exception cref="NotImplementedException"></exception>
  47. protected override Task<ResponseInfo> GetResponseInfo()
  48. {
  49. throw new NotImplementedException();
  50. }
  51. /// <summary>
  52. /// Processes the request.
  53. /// </summary>
  54. /// <param name="ctx">The CTX.</param>
  55. /// <returns>Task.</returns>
  56. public override async Task ProcessRequest(HttpListenerContext ctx)
  57. {
  58. var path = Path.GetFileName(ctx.Request.Url.LocalPath);
  59. path = Path.Combine(Kernel.ApplicationPaths.FFMpegStreamCachePath, path);
  60. var playlistFilename = Path.GetFileNameWithoutExtension(path).Substring(SegmentFilePrefix.Length);
  61. playlistFilename = playlistFilename.Substring(0, playlistFilename.Length - 3);
  62. var playlistPath = Path.Combine(Path.GetDirectoryName(path), playlistFilename + ".m3u8");
  63. Plugin.Instance.OnTranscodeBeginRequest(playlistPath, TranscodingJobType.Hls);
  64. try
  65. {
  66. await new StaticFileHandler(Kernel) { Path = path }.ProcessRequest(ctx).ConfigureAwait(false);
  67. }
  68. finally
  69. {
  70. Plugin.Instance.OnTranscodeEndRequest(playlistPath, TranscodingJobType.Hls);
  71. }
  72. }
  73. }
  74. }