HlsSegmentService.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. [Route("/Audio/{Id}/hls/{SegmentId}/stream.mp3", "GET")]
  18. [Route("/Audio/{Id}/hls/{SegmentId}/stream.aac", "GET")]
  19. public class GetHlsAudioSegmentLegacy
  20. {
  21. // TODO: Deprecate with new iOS app
  22. /// <summary>
  23. /// Gets or sets the id.
  24. /// </summary>
  25. /// <value>The id.</value>
  26. public string Id { get; set; }
  27. /// <summary>
  28. /// Gets or sets the segment id.
  29. /// </summary>
  30. /// <value>The segment id.</value>
  31. public string SegmentId { get; set; }
  32. }
  33. /// <summary>
  34. /// Class GetHlsVideoSegment
  35. /// </summary>
  36. [Route("/Videos/{Id}/hls/{PlaylistId}/stream.m3u8", "GET")]
  37. public class GetHlsPlaylistLegacy
  38. {
  39. // TODO: Deprecate with new iOS app
  40. /// <summary>
  41. /// Gets or sets the id.
  42. /// </summary>
  43. /// <value>The id.</value>
  44. public string Id { get; set; }
  45. public string PlaylistId { get; set; }
  46. }
  47. [Route("/Videos/ActiveEncodings", "DELETE")]
  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}.{SegmentContainer}", "GET")]
  59. public class GetHlsVideoSegmentLegacy : VideoStreamRequest
  60. {
  61. public string PlaylistId { get; set; }
  62. /// <summary>
  63. /// Gets or sets the segment id.
  64. /// </summary>
  65. /// <value>The segment id.</value>
  66. public string SegmentId { get; set; }
  67. }
  68. public class HlsSegmentService : BaseApiService
  69. {
  70. private readonly IServerApplicationPaths _appPaths;
  71. private readonly IServerConfigurationManager _config;
  72. private readonly IFileSystem _fileSystem;
  73. public HlsSegmentService(IServerApplicationPaths appPaths, IServerConfigurationManager config, IFileSystem fileSystem)
  74. {
  75. _appPaths = appPaths;
  76. _config = config;
  77. _fileSystem = fileSystem;
  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. var transcodeFolderPath = _config.ApplicationPaths.TranscodingTempPath;
  98. file = Path.Combine(transcodeFolderPath, file);
  99. var normalizedPlaylistId = request.PlaylistId;
  100. var playlistPath = _fileSystem.GetFilePaths(transcodeFolderPath)
  101. .FirstOrDefault(i => string.Equals(Path.GetExtension(i), ".m3u8", StringComparison.OrdinalIgnoreCase) && i.IndexOf(normalizedPlaylistId, StringComparison.OrdinalIgnoreCase) != -1);
  102. return GetFileResult(file, playlistPath);
  103. }
  104. /// <summary>
  105. /// Gets the specified request.
  106. /// </summary>
  107. /// <param name="request">The request.</param>
  108. /// <returns>System.Object.</returns>
  109. public Task<object> Get(GetHlsAudioSegmentLegacy request)
  110. {
  111. // TODO: Deprecate with new iOS app
  112. var file = request.SegmentId + Path.GetExtension(Request.PathInfo);
  113. file = Path.Combine(_appPaths.TranscodingTempPath, file);
  114. return ResultFactory.GetStaticFileResult(Request, file, FileShareMode.ReadWrite);
  115. }
  116. private Task<object> GetFileResult(string path, string playlistPath)
  117. {
  118. var transcodingJob = ApiEntryPoint.Instance.OnTranscodeBeginRequest(playlistPath, TranscodingJobType.Hls);
  119. return ResultFactory.GetStaticFileResult(Request, new StaticFileResultOptions
  120. {
  121. Path = path,
  122. FileShare = FileShareMode.ReadWrite,
  123. OnComplete = () =>
  124. {
  125. if (transcodingJob != null)
  126. {
  127. ApiEntryPoint.Instance.OnTranscodeEndRequest(transcodingJob);
  128. }
  129. }
  130. });
  131. }
  132. }
  133. }