HlsSegmentService.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. using MediaBrowser.Controller;
  2. using ServiceStack.ServiceHost;
  3. using System;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Threading.Tasks;
  7. namespace MediaBrowser.Api.Playback.Hls
  8. {
  9. /// <summary>
  10. /// Class GetHlsAudioSegment
  11. /// </summary>
  12. [Route("/Audio/{Id}/hls/{SegmentId}/stream.mp3", "GET")]
  13. [Route("/Audio/{Id}/hls/{SegmentId}/stream.aac", "GET")]
  14. [Api(Description = "Gets an Http live streaming segment file. Internal use only.")]
  15. public class GetHlsAudioSegment
  16. {
  17. /// <summary>
  18. /// Gets or sets the id.
  19. /// </summary>
  20. /// <value>The id.</value>
  21. public string Id { get; set; }
  22. /// <summary>
  23. /// Gets or sets the segment id.
  24. /// </summary>
  25. /// <value>The segment id.</value>
  26. public string SegmentId { get; set; }
  27. }
  28. /// <summary>
  29. /// Class GetHlsVideoSegment
  30. /// </summary>
  31. [Route("/Videos/{Id}/hls/{PlaylistId}/{SegmentId}.ts", "GET")]
  32. [Api(Description = "Gets an Http live streaming segment file. Internal use only.")]
  33. public class GetHlsVideoSegment
  34. {
  35. /// <summary>
  36. /// Gets or sets the id.
  37. /// </summary>
  38. /// <value>The id.</value>
  39. public string Id { get; set; }
  40. public string PlaylistId { get; set; }
  41. /// <summary>
  42. /// Gets or sets the segment id.
  43. /// </summary>
  44. /// <value>The segment id.</value>
  45. public string SegmentId { get; set; }
  46. }
  47. /// <summary>
  48. /// Class GetHlsVideoSegment
  49. /// </summary>
  50. [Route("/Videos/{Id}/hls/{PlaylistId}/stream.m3u8", "GET")]
  51. [Api(Description = "Gets an Http live streaming segment file. Internal use only.")]
  52. public class GetHlsPlaylist
  53. {
  54. /// <summary>
  55. /// Gets or sets the id.
  56. /// </summary>
  57. /// <value>The id.</value>
  58. public string Id { get; set; }
  59. public string PlaylistId { get; set; }
  60. }
  61. public class HlsSegmentService : BaseApiService
  62. {
  63. private readonly IServerApplicationPaths _appPaths;
  64. public HlsSegmentService(IServerApplicationPaths appPaths)
  65. {
  66. _appPaths = appPaths;
  67. }
  68. public object Get(GetHlsPlaylist request)
  69. {
  70. OnBeginRequest(request.PlaylistId);
  71. var file = request.PlaylistId + Path.GetExtension(RequestContext.PathInfo);
  72. file = Path.Combine(_appPaths.EncodedMediaCachePath, file);
  73. return ResultFactory.GetStaticFileResult(RequestContext, file, FileShare.ReadWrite);
  74. }
  75. /// <summary>
  76. /// Gets the specified request.
  77. /// </summary>
  78. /// <param name="request">The request.</param>
  79. /// <returns>System.Object.</returns>
  80. public object Get(GetHlsVideoSegment request)
  81. {
  82. var file = request.SegmentId + Path.GetExtension(RequestContext.PathInfo);
  83. file = Path.Combine(_appPaths.EncodedMediaCachePath, file);
  84. OnBeginRequest(request.PlaylistId);
  85. return ResultFactory.GetStaticFileResult(RequestContext, file);
  86. }
  87. /// <summary>
  88. /// Gets the specified request.
  89. /// </summary>
  90. /// <param name="request">The request.</param>
  91. /// <returns>System.Object.</returns>
  92. public object Get(GetHlsAudioSegment request)
  93. {
  94. var file = request.SegmentId + Path.GetExtension(RequestContext.PathInfo);
  95. file = Path.Combine(_appPaths.EncodedMediaCachePath, file);
  96. return ResultFactory.GetStaticFileResult(RequestContext, file, FileShare.ReadWrite);
  97. }
  98. /// <summary>
  99. /// Called when [begin request].
  100. /// </summary>
  101. /// <param name="playlistId">The playlist id.</param>
  102. protected void OnBeginRequest(string playlistId)
  103. {
  104. var normalizedPlaylistId = playlistId.Replace("-low", string.Empty);
  105. foreach (var playlist in Directory.EnumerateFiles(_appPaths.EncodedMediaCachePath, "*.m3u8")
  106. .Where(i => i.IndexOf(normalizedPlaylistId, StringComparison.OrdinalIgnoreCase) != -1)
  107. .ToList())
  108. {
  109. ExtendPlaylistTimer(playlist);
  110. }
  111. }
  112. private void ExtendPlaylistTimer(string playlist)
  113. {
  114. ApiEntryPoint.Instance.OnTranscodeBeginRequest(playlist, TranscodingJobType.Hls);
  115. Task.Run(async () =>
  116. {
  117. await Task.Delay(20000).ConfigureAwait(false);
  118. ApiEntryPoint.Instance.OnTranscodeEndRequest(playlist, TranscodingJobType.Hls);
  119. });
  120. }
  121. }
  122. }