HlsSegmentService.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. ApiEntryPoint.Instance.KillTranscodingJobs(request.DeviceId, true);
  66. }
  67. /// <summary>
  68. /// Gets the specified request.
  69. /// </summary>
  70. /// <param name="request">The request.</param>
  71. /// <returns>System.Object.</returns>
  72. public object Get(GetHlsAudioSegment request)
  73. {
  74. var file = request.SegmentId + Path.GetExtension(Request.PathInfo);
  75. file = Path.Combine(_appPaths.TranscodingTempPath, file);
  76. return ResultFactory.GetStaticFileResult(Request, file, FileShare.ReadWrite);
  77. }
  78. /// <summary>
  79. /// Called when [begin request].
  80. /// </summary>
  81. /// <param name="playlistId">The playlist id.</param>
  82. protected void OnBeginRequest(string playlistId)
  83. {
  84. var normalizedPlaylistId = playlistId.Replace("-low", string.Empty);
  85. foreach (var playlist in Directory.EnumerateFiles(_appPaths.TranscodingTempPath, "*.m3u8")
  86. .Where(i => i.IndexOf(normalizedPlaylistId, StringComparison.OrdinalIgnoreCase) != -1)
  87. .ToList())
  88. {
  89. if (!string.IsNullOrEmpty(playlist))
  90. {
  91. ExtendPlaylistTimer(playlist);
  92. }
  93. }
  94. }
  95. private async void ExtendPlaylistTimer(string playlist)
  96. {
  97. ApiEntryPoint.Instance.OnTranscodeBeginRequest(playlist, TranscodingJobType.Hls);
  98. await Task.Delay(20000).ConfigureAwait(false);
  99. ApiEntryPoint.Instance.OnTranscodeEndRequest(playlist, TranscodingJobType.Hls);
  100. }
  101. }
  102. }