HlsSegmentService.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. public string PlaylistId { get; set; }
  63. /// <summary>
  64. /// Gets or sets the segment id.
  65. /// </summary>
  66. /// <value>The segment id.</value>
  67. public string SegmentId { get; set; }
  68. }
  69. public class HlsSegmentService : BaseApiService
  70. {
  71. private readonly IServerApplicationPaths _appPaths;
  72. private readonly IServerConfigurationManager _config;
  73. public HlsSegmentService(IServerApplicationPaths appPaths, IServerConfigurationManager config)
  74. {
  75. _appPaths = appPaths;
  76. _config = config;
  77. }
  78. public Task<object> Get(GetHlsPlaylistLegacy request)
  79. {
  80. var file = request.PlaylistId + Path.GetExtension(Request.PathInfo);
  81. file = Path.Combine(_appPaths.TranscodingTempPath, file);
  82. return GetFileResult(file, file);
  83. }
  84. public void Delete(StopEncodingProcess request)
  85. {
  86. ApiEntryPoint.Instance.KillTranscodingJobs(request.DeviceId, request.PlaySessionId, path => true);
  87. }
  88. /// <summary>
  89. /// Gets the specified request.
  90. /// </summary>
  91. /// <param name="request">The request.</param>
  92. /// <returns>System.Object.</returns>
  93. public Task<object> Get(GetHlsVideoSegmentLegacy request)
  94. {
  95. var file = request.SegmentId + Path.GetExtension(Request.PathInfo);
  96. file = Path.Combine(_config.ApplicationPaths.TranscodingTempPath, file);
  97. var normalizedPlaylistId = request.PlaylistId;
  98. var playlistPath = Directory.EnumerateFiles(_config.ApplicationPaths.TranscodingTempPath, "*")
  99. .FirstOrDefault(i => string.Equals(Path.GetExtension(i), ".m3u8", StringComparison.OrdinalIgnoreCase) && i.IndexOf(normalizedPlaylistId, StringComparison.OrdinalIgnoreCase) != -1);
  100. return GetFileResult(file, playlistPath);
  101. }
  102. /// <summary>
  103. /// Gets the specified request.
  104. /// </summary>
  105. /// <param name="request">The request.</param>
  106. /// <returns>System.Object.</returns>
  107. public object Get(GetHlsAudioSegmentLegacy request)
  108. {
  109. // TODO: Deprecate with new iOS app
  110. var file = request.SegmentId + Path.GetExtension(Request.PathInfo);
  111. file = Path.Combine(_appPaths.TranscodingTempPath, file);
  112. return ResultFactory.GetStaticFileResult(Request, file, FileShare.ReadWrite).Result;
  113. }
  114. private Task<object> GetFileResult(string path, string playlistPath)
  115. {
  116. var transcodingJob = ApiEntryPoint.Instance.OnTranscodeBeginRequest(playlistPath, TranscodingJobType.Hls);
  117. return ResultFactory.GetStaticFileResult(Request, new StaticFileResultOptions
  118. {
  119. Path = path,
  120. FileShare = FileShare.ReadWrite,
  121. OnComplete = () =>
  122. {
  123. if (transcodingJob != null)
  124. {
  125. ApiEntryPoint.Instance.OnTranscodeEndRequest(transcodingJob);
  126. }
  127. }
  128. });
  129. }
  130. }
  131. }