HlsSegmentService.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. using MediaBrowser.Controller;
  2. using MediaBrowser.Controller.Configuration;
  3. using MediaBrowser.Controller.Net;
  4. using ServiceStack;
  5. using System;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Threading.Tasks;
  9. namespace MediaBrowser.Api.Playback.Hls
  10. {
  11. /// <summary>
  12. /// Class GetHlsAudioSegment
  13. /// </summary>
  14. [Route("/Audio/{Id}/hls/{SegmentId}/stream.mp3", "GET")]
  15. [Route("/Audio/{Id}/hls/{SegmentId}/stream.aac", "GET")]
  16. [Api(Description = "Gets an Http live streaming segment file. Internal use only.")]
  17. public class GetHlsAudioSegmentLegacy
  18. {
  19. // TODO: Deprecate with new iOS app
  20. /// <summary>
  21. /// Gets or sets the id.
  22. /// </summary>
  23. /// <value>The id.</value>
  24. public string Id { get; set; }
  25. /// <summary>
  26. /// Gets or sets the segment id.
  27. /// </summary>
  28. /// <value>The segment id.</value>
  29. public string SegmentId { get; set; }
  30. }
  31. /// <summary>
  32. /// Class GetHlsVideoSegment
  33. /// </summary>
  34. [Route("/Videos/{Id}/hls/{PlaylistId}/stream.m3u8", "GET")]
  35. [Api(Description = "Gets an Http live streaming segment file. Internal use only.")]
  36. public class GetHlsPlaylistLegacy
  37. {
  38. // TODO: Deprecate with new iOS app
  39. /// <summary>
  40. /// Gets or sets the id.
  41. /// </summary>
  42. /// <value>The id.</value>
  43. public string Id { get; set; }
  44. public string PlaylistId { get; set; }
  45. }
  46. [Route("/Videos/ActiveEncodings", "DELETE")]
  47. [Api(Description = "Stops an encoding process")]
  48. public class StopEncodingProcess
  49. {
  50. [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")]
  51. public string DeviceId { get; set; }
  52. [ApiMember(Name = "PlaySessionId", Description = "The play session id", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "DELETE")]
  53. public string PlaySessionId { get; set; }
  54. }
  55. /// <summary>
  56. /// Class GetHlsVideoSegment
  57. /// </summary>
  58. [Route("/Videos/{Id}/hls/{PlaylistId}/{SegmentId}.ts", "GET")]
  59. [Api(Description = "Gets an Http live streaming segment file. Internal use only.")]
  60. public class GetHlsVideoSegmentLegacy : VideoStreamRequest
  61. {
  62. // TODO: Deprecate with new iOS app
  63. public string PlaylistId { get; set; }
  64. /// <summary>
  65. /// Gets or sets the segment id.
  66. /// </summary>
  67. /// <value>The segment id.</value>
  68. public string SegmentId { get; set; }
  69. }
  70. public class HlsSegmentService : BaseApiService
  71. {
  72. private readonly IServerApplicationPaths _appPaths;
  73. private readonly IServerConfigurationManager _config;
  74. public HlsSegmentService(IServerApplicationPaths appPaths, IServerConfigurationManager config)
  75. {
  76. _appPaths = appPaths;
  77. _config = config;
  78. }
  79. public Task<object> Get(GetHlsPlaylistLegacy request)
  80. {
  81. var file = request.PlaylistId + Path.GetExtension(Request.PathInfo);
  82. file = Path.Combine(_appPaths.TranscodingTempPath, file);
  83. return GetFileResult(file, file);
  84. }
  85. public void Delete(StopEncodingProcess request)
  86. {
  87. ApiEntryPoint.Instance.KillTranscodingJobs(request.DeviceId, request.PlaySessionId, path => true);
  88. }
  89. /// <summary>
  90. /// Gets the specified request.
  91. /// </summary>
  92. /// <param name="request">The request.</param>
  93. /// <returns>System.Object.</returns>
  94. public Task<object> Get(GetHlsVideoSegmentLegacy request)
  95. {
  96. var file = request.SegmentId + Path.GetExtension(Request.PathInfo);
  97. file = Path.Combine(_config.ApplicationPaths.TranscodingTempPath, file);
  98. var normalizedPlaylistId = request.PlaylistId.Replace("-low", string.Empty);
  99. var playlistPath = Directory.EnumerateFiles(_config.ApplicationPaths.TranscodingTempPath, "*")
  100. .FirstOrDefault(i => string.Equals(Path.GetExtension(i), ".m3u8", StringComparison.OrdinalIgnoreCase) && i.IndexOf(normalizedPlaylistId, StringComparison.OrdinalIgnoreCase) != -1);
  101. return GetFileResult(file, playlistPath);
  102. }
  103. /// <summary>
  104. /// Gets the specified request.
  105. /// </summary>
  106. /// <param name="request">The request.</param>
  107. /// <returns>System.Object.</returns>
  108. public object Get(GetHlsAudioSegmentLegacy request)
  109. {
  110. // TODO: Deprecate with new iOS app
  111. var file = request.SegmentId + Path.GetExtension(Request.PathInfo);
  112. file = Path.Combine(_appPaths.TranscodingTempPath, file);
  113. return ResultFactory.GetStaticFileResult(Request, file, FileShare.ReadWrite).Result;
  114. }
  115. private Task<object> GetFileResult(string path, string playlistPath)
  116. {
  117. var transcodingJob = ApiEntryPoint.Instance.OnTranscodeBeginRequest(playlistPath, TranscodingJobType.Hls);
  118. return ResultFactory.GetStaticFileResult(Request, new StaticFileResultOptions
  119. {
  120. Path = path,
  121. FileShare = FileShare.ReadWrite,
  122. OnComplete = () =>
  123. {
  124. if (transcodingJob != null)
  125. {
  126. ApiEntryPoint.Instance.OnTranscodeEndRequest(transcodingJob);
  127. }
  128. }
  129. });
  130. }
  131. }
  132. }