HlsSegmentService.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. using MediaBrowser.Controller;
  2. using ServiceStack;
  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. [Route("/Videos/ActiveEncodings", "DELETE")]
  62. [Api(Description = "Stops an encoding process")]
  63. public class StopEncodingProcess
  64. {
  65. [ApiMember(Name = "DeviceId", Description = "The device id of the client requesting. Used to stop encoding processes when needed.", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "DELETE")]
  66. public string DeviceId { get; set; }
  67. }
  68. public class HlsSegmentService : BaseApiService
  69. {
  70. private readonly IServerApplicationPaths _appPaths;
  71. public HlsSegmentService(IServerApplicationPaths appPaths)
  72. {
  73. _appPaths = appPaths;
  74. }
  75. public object Get(GetHlsPlaylist request)
  76. {
  77. OnBeginRequest(request.PlaylistId);
  78. var file = request.PlaylistId + Path.GetExtension(Request.PathInfo);
  79. file = Path.Combine(_appPaths.EncodedMediaCachePath, file);
  80. return ResultFactory.GetStaticFileResult(Request, file, FileShare.ReadWrite);
  81. }
  82. public void Delete(StopEncodingProcess request)
  83. {
  84. ApiEntryPoint.Instance.KillTranscodingJobs(request.DeviceId, true);
  85. }
  86. /// <summary>
  87. /// Gets the specified request.
  88. /// </summary>
  89. /// <param name="request">The request.</param>
  90. /// <returns>System.Object.</returns>
  91. public object Get(GetHlsVideoSegment request)
  92. {
  93. var file = request.SegmentId + Path.GetExtension(Request.PathInfo);
  94. file = Path.Combine(_appPaths.EncodedMediaCachePath, file);
  95. OnBeginRequest(request.PlaylistId);
  96. return ResultFactory.GetStaticFileResult(Request, file);
  97. }
  98. /// <summary>
  99. /// Gets the specified request.
  100. /// </summary>
  101. /// <param name="request">The request.</param>
  102. /// <returns>System.Object.</returns>
  103. public object Get(GetHlsAudioSegment request)
  104. {
  105. var file = request.SegmentId + Path.GetExtension(Request.PathInfo);
  106. file = Path.Combine(_appPaths.EncodedMediaCachePath, file);
  107. return ResultFactory.GetStaticFileResult(Request, file, FileShare.ReadWrite);
  108. }
  109. /// <summary>
  110. /// Called when [begin request].
  111. /// </summary>
  112. /// <param name="playlistId">The playlist id.</param>
  113. protected void OnBeginRequest(string playlistId)
  114. {
  115. var normalizedPlaylistId = playlistId.Replace("-low", string.Empty);
  116. foreach (var playlist in Directory.EnumerateFiles(_appPaths.EncodedMediaCachePath, "*.m3u8")
  117. .Where(i => i.IndexOf(normalizedPlaylistId, StringComparison.OrdinalIgnoreCase) != -1)
  118. .ToList())
  119. {
  120. ExtendPlaylistTimer(playlist);
  121. }
  122. }
  123. private async void ExtendPlaylistTimer(string playlist)
  124. {
  125. ApiEntryPoint.Instance.OnTranscodeBeginRequest(playlist, TranscodingJobType.Hls);
  126. await Task.Delay(20000).ConfigureAwait(false);
  127. ApiEntryPoint.Instance.OnTranscodeEndRequest(playlist, TranscodingJobType.Hls);
  128. }
  129. }
  130. }