HlsSegmentService.cs 5.4 KB

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