HlsSegmentService.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using MediaBrowser.Common.Configuration;
  6. using MediaBrowser.Controller.Configuration;
  7. using MediaBrowser.Controller.MediaEncoding;
  8. using MediaBrowser.Controller.Net;
  9. using MediaBrowser.Model.IO;
  10. using MediaBrowser.Model.Services;
  11. using Microsoft.Extensions.Logging;
  12. namespace MediaBrowser.Api.Playback.Hls
  13. {
  14. /// <summary>
  15. /// Class GetHlsAudioSegment
  16. /// </summary>
  17. // Can't require authentication just yet due to seeing some requests come from Chrome without full query string
  18. //[Authenticated]
  19. [Route("/Audio/{Id}/hls/{SegmentId}/stream.mp3", "GET")]
  20. [Route("/Audio/{Id}/hls/{SegmentId}/stream.aac", "GET")]
  21. public class GetHlsAudioSegmentLegacy
  22. {
  23. // TODO: Deprecate with new iOS app
  24. /// <summary>
  25. /// Gets or sets the id.
  26. /// </summary>
  27. /// <value>The id.</value>
  28. public string Id { get; set; }
  29. /// <summary>
  30. /// Gets or sets the segment id.
  31. /// </summary>
  32. /// <value>The segment id.</value>
  33. public string SegmentId { get; set; }
  34. }
  35. /// <summary>
  36. /// Class GetHlsVideoSegment
  37. /// </summary>
  38. [Route("/Videos/{Id}/hls/{PlaylistId}/stream.m3u8", "GET")]
  39. [Authenticated]
  40. public class GetHlsPlaylistLegacy
  41. {
  42. // TODO: Deprecate with new iOS app
  43. /// <summary>
  44. /// Gets or sets the id.
  45. /// </summary>
  46. /// <value>The id.</value>
  47. public string Id { get; set; }
  48. public string PlaylistId { get; set; }
  49. }
  50. [Route("/Videos/ActiveEncodings", "DELETE")]
  51. [Authenticated]
  52. public class StopEncodingProcess
  53. {
  54. [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")]
  55. public string DeviceId { get; set; }
  56. [ApiMember(Name = "PlaySessionId", Description = "The play session id", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "DELETE")]
  57. public string PlaySessionId { get; set; }
  58. }
  59. /// <summary>
  60. /// Class GetHlsVideoSegment
  61. /// </summary>
  62. // Can't require authentication just yet due to seeing some requests come from Chrome without full query string
  63. //[Authenticated]
  64. [Route("/Videos/{Id}/hls/{PlaylistId}/{SegmentId}.{SegmentContainer}", "GET")]
  65. public class GetHlsVideoSegmentLegacy : VideoStreamRequest
  66. {
  67. public string PlaylistId { get; set; }
  68. /// <summary>
  69. /// Gets or sets the segment id.
  70. /// </summary>
  71. /// <value>The segment id.</value>
  72. public string SegmentId { get; set; }
  73. }
  74. public class HlsSegmentService : BaseApiService
  75. {
  76. private readonly IFileSystem _fileSystem;
  77. public HlsSegmentService(
  78. ILogger<HlsSegmentService> logger,
  79. IServerConfigurationManager serverConfigurationManager,
  80. IHttpResultFactory httpResultFactory,
  81. IFileSystem fileSystem)
  82. : base(logger, serverConfigurationManager, httpResultFactory)
  83. {
  84. _fileSystem = fileSystem;
  85. }
  86. public Task<object> Get(GetHlsPlaylistLegacy request)
  87. {
  88. var file = request.PlaylistId + Path.GetExtension(Request.PathInfo);
  89. file = Path.Combine(ServerConfigurationManager.GetTranscodePath(), file);
  90. return GetFileResult(file, file);
  91. }
  92. public Task Delete(StopEncodingProcess request)
  93. {
  94. return ApiEntryPoint.Instance.KillTranscodingJobs(request.DeviceId, request.PlaySessionId, path => true);
  95. }
  96. /// <summary>
  97. /// Gets the specified request.
  98. /// </summary>
  99. /// <param name="request">The request.</param>
  100. /// <returns>System.Object.</returns>
  101. public Task<object> Get(GetHlsVideoSegmentLegacy request)
  102. {
  103. var file = request.SegmentId + Path.GetExtension(Request.PathInfo);
  104. var transcodeFolderPath = ServerConfigurationManager.GetTranscodePath();
  105. file = Path.Combine(transcodeFolderPath, file);
  106. var normalizedPlaylistId = request.PlaylistId;
  107. var playlistPath = _fileSystem.GetFilePaths(transcodeFolderPath)
  108. .FirstOrDefault(i => string.Equals(Path.GetExtension(i), ".m3u8", StringComparison.OrdinalIgnoreCase) && i.IndexOf(normalizedPlaylistId, StringComparison.OrdinalIgnoreCase) != -1);
  109. return GetFileResult(file, playlistPath);
  110. }
  111. /// <summary>
  112. /// Gets the specified request.
  113. /// </summary>
  114. /// <param name="request">The request.</param>
  115. /// <returns>System.Object.</returns>
  116. public Task<object> Get(GetHlsAudioSegmentLegacy request)
  117. {
  118. // TODO: Deprecate with new iOS app
  119. var file = request.SegmentId + Path.GetExtension(Request.PathInfo);
  120. file = Path.Combine(ServerConfigurationManager.GetTranscodePath(), file);
  121. return ResultFactory.GetStaticFileResult(Request, file, FileShare.ReadWrite);
  122. }
  123. private Task<object> GetFileResult(string path, string playlistPath)
  124. {
  125. var transcodingJob = ApiEntryPoint.Instance.OnTranscodeBeginRequest(playlistPath, TranscodingJobType.Hls);
  126. return ResultFactory.GetStaticFileResult(Request, new StaticFileResultOptions
  127. {
  128. Path = path,
  129. FileShare = FileShare.ReadWrite,
  130. OnComplete = () =>
  131. {
  132. if (transcodingJob != null)
  133. {
  134. ApiEntryPoint.Instance.OnTranscodeEndRequest(transcodingJob);
  135. }
  136. }
  137. });
  138. }
  139. }
  140. }