HlsSegmentService.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. using MediaBrowser.Controller;
  2. using MediaBrowser.Controller.Configuration;
  3. using MediaBrowser.Controller.Net;
  4. using System;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Threading.Tasks;
  8. using MediaBrowser.Common.IO;
  9. using MediaBrowser.Controller.MediaEncoding;
  10. using MediaBrowser.Model.IO;
  11. using MediaBrowser.Model.Services;
  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 IServerApplicationPaths _appPaths;
  77. private readonly IServerConfigurationManager _config;
  78. private readonly IFileSystem _fileSystem;
  79. public HlsSegmentService(IServerApplicationPaths appPaths, IServerConfigurationManager config, IFileSystem fileSystem)
  80. {
  81. _appPaths = appPaths;
  82. _config = config;
  83. _fileSystem = fileSystem;
  84. }
  85. public Task<object> Get(GetHlsPlaylistLegacy request)
  86. {
  87. var file = request.PlaylistId + Path.GetExtension(Request.PathInfo);
  88. file = Path.Combine(_appPaths.TranscodingTempPath, file);
  89. return GetFileResult(file, file);
  90. }
  91. public void Delete(StopEncodingProcess request)
  92. {
  93. ApiEntryPoint.Instance.KillTranscodingJobs(request.DeviceId, request.PlaySessionId, path => true);
  94. }
  95. /// <summary>
  96. /// Gets the specified request.
  97. /// </summary>
  98. /// <param name="request">The request.</param>
  99. /// <returns>System.Object.</returns>
  100. public Task<object> Get(GetHlsVideoSegmentLegacy request)
  101. {
  102. var file = request.SegmentId + Path.GetExtension(Request.PathInfo);
  103. var transcodeFolderPath = _config.ApplicationPaths.TranscodingTempPath;
  104. file = Path.Combine(transcodeFolderPath, file);
  105. var normalizedPlaylistId = request.PlaylistId;
  106. var playlistPath = _fileSystem.GetFilePaths(transcodeFolderPath)
  107. .FirstOrDefault(i => string.Equals(Path.GetExtension(i), ".m3u8", StringComparison.OrdinalIgnoreCase) && i.IndexOf(normalizedPlaylistId, StringComparison.OrdinalIgnoreCase) != -1);
  108. return GetFileResult(file, playlistPath);
  109. }
  110. /// <summary>
  111. /// Gets the specified request.
  112. /// </summary>
  113. /// <param name="request">The request.</param>
  114. /// <returns>System.Object.</returns>
  115. public Task<object> Get(GetHlsAudioSegmentLegacy request)
  116. {
  117. // TODO: Deprecate with new iOS app
  118. var file = request.SegmentId + Path.GetExtension(Request.PathInfo);
  119. file = Path.Combine(_appPaths.TranscodingTempPath, file);
  120. return ResultFactory.GetStaticFileResult(Request, file, FileShareMode.ReadWrite);
  121. }
  122. private Task<object> GetFileResult(string path, string playlistPath)
  123. {
  124. var transcodingJob = ApiEntryPoint.Instance.OnTranscodeBeginRequest(playlistPath, TranscodingJobType.Hls);
  125. return ResultFactory.GetStaticFileResult(Request, new StaticFileResultOptions
  126. {
  127. Path = path,
  128. FileShare = FileShareMode.ReadWrite,
  129. OnComplete = () =>
  130. {
  131. if (transcodingJob != null)
  132. {
  133. ApiEntryPoint.Instance.OnTranscodeEndRequest(transcodingJob);
  134. }
  135. }
  136. });
  137. }
  138. }
  139. }