HlsSegmentHandler.cs 3.1 KB

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