HlsSegmentService.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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}/stream.m3u8", "GET")]
  32. [Api(Description = "Gets an Http live streaming segment file. Internal use only.")]
  33. public class GetHlsPlaylist
  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. }
  42. [Route("/Videos/ActiveEncodings", "DELETE")]
  43. [Api(Description = "Stops an encoding process")]
  44. public class StopEncodingProcess
  45. {
  46. [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")]
  47. public string DeviceId { get; set; }
  48. }
  49. public class HlsSegmentService : BaseApiService
  50. {
  51. private readonly IServerApplicationPaths _appPaths;
  52. public HlsSegmentService(IServerApplicationPaths appPaths)
  53. {
  54. _appPaths = appPaths;
  55. }
  56. public object Get(GetHlsPlaylist request)
  57. {
  58. OnBeginRequest(request.PlaylistId);
  59. var file = request.PlaylistId + Path.GetExtension(Request.PathInfo);
  60. file = Path.Combine(_appPaths.TranscodingTempPath, file);
  61. return ResultFactory.GetStaticFileResult(Request, file, FileShare.ReadWrite);
  62. }
  63. public void Delete(StopEncodingProcess request)
  64. {
  65. var task = ApiEntryPoint.Instance.KillTranscodingJobs(request.DeviceId, path => true, true);
  66. Task.WaitAll(task);
  67. }
  68. /// <summary>
  69. /// Gets the specified request.
  70. /// </summary>
  71. /// <param name="request">The request.</param>
  72. /// <returns>System.Object.</returns>
  73. public object Get(GetHlsAudioSegment request)
  74. {
  75. var file = request.SegmentId + Path.GetExtension(Request.PathInfo);
  76. file = Path.Combine(_appPaths.TranscodingTempPath, file);
  77. return ResultFactory.GetStaticFileResult(Request, file, FileShare.ReadWrite);
  78. }
  79. /// <summary>
  80. /// Called when [begin request].
  81. /// </summary>
  82. /// <param name="playlistId">The playlist id.</param>
  83. protected void OnBeginRequest(string playlistId)
  84. {
  85. var normalizedPlaylistId = playlistId.Replace("-low", string.Empty);
  86. foreach (var playlist in Directory.EnumerateFiles(_appPaths.TranscodingTempPath, "*.m3u8")
  87. .Where(i => i.IndexOf(normalizedPlaylistId, StringComparison.OrdinalIgnoreCase) != -1)
  88. .ToList())
  89. {
  90. if (!string.IsNullOrEmpty(playlist))
  91. {
  92. ExtendPlaylistTimer(playlist);
  93. }
  94. }
  95. }
  96. private async void ExtendPlaylistTimer(string playlist)
  97. {
  98. ApiEntryPoint.Instance.OnTranscodeBeginRequest(playlist, TranscodingJobType.Hls);
  99. await Task.Delay(20000).ConfigureAwait(false);
  100. ApiEntryPoint.Instance.OnTranscodeEndRequest(playlist, TranscodingJobType.Hls);
  101. }
  102. }
  103. }