HlsSegmentService.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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.Model.IO;
  10. using MediaBrowser.Model.Services;
  11. namespace MediaBrowser.Api.Playback.Hls
  12. {
  13. /// <summary>
  14. /// Class GetHlsAudioSegment
  15. /// </summary>
  16. [Route("/Audio/{Id}/hls/{SegmentId}/stream.mp3", "GET")]
  17. [Route("/Audio/{Id}/hls/{SegmentId}/stream.aac", "GET")]
  18. public class GetHlsAudioSegmentLegacy
  19. {
  20. // TODO: Deprecate with new iOS app
  21. /// <summary>
  22. /// Gets or sets the id.
  23. /// </summary>
  24. /// <value>The id.</value>
  25. public string Id { get; set; }
  26. /// <summary>
  27. /// Gets or sets the segment id.
  28. /// </summary>
  29. /// <value>The segment id.</value>
  30. public string SegmentId { get; set; }
  31. }
  32. /// <summary>
  33. /// Class GetHlsVideoSegment
  34. /// </summary>
  35. [Route("/Videos/{Id}/hls/{PlaylistId}/stream.m3u8", "GET")]
  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. public class StopEncodingProcess
  48. {
  49. [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")]
  50. public string DeviceId { get; set; }
  51. [ApiMember(Name = "PlaySessionId", Description = "The play session id", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "DELETE")]
  52. public string PlaySessionId { get; set; }
  53. }
  54. /// <summary>
  55. /// Class GetHlsVideoSegment
  56. /// </summary>
  57. [Route("/Videos/{Id}/hls/{PlaylistId}/{SegmentId}.ts", "GET")]
  58. public class GetHlsVideoSegmentLegacy : VideoStreamRequest
  59. {
  60. public string PlaylistId { get; set; }
  61. /// <summary>
  62. /// Gets or sets the segment id.
  63. /// </summary>
  64. /// <value>The segment id.</value>
  65. public string SegmentId { get; set; }
  66. }
  67. public class HlsSegmentService : BaseApiService
  68. {
  69. private readonly IServerApplicationPaths _appPaths;
  70. private readonly IServerConfigurationManager _config;
  71. public HlsSegmentService(IServerApplicationPaths appPaths, IServerConfigurationManager config)
  72. {
  73. _appPaths = appPaths;
  74. _config = config;
  75. }
  76. public Task<object> Get(GetHlsPlaylistLegacy request)
  77. {
  78. var file = request.PlaylistId + Path.GetExtension(Request.PathInfo);
  79. file = Path.Combine(_appPaths.TranscodingTempPath, file);
  80. return GetFileResult(file, file);
  81. }
  82. public void Delete(StopEncodingProcess request)
  83. {
  84. ApiEntryPoint.Instance.KillTranscodingJobs(request.DeviceId, request.PlaySessionId, path => true);
  85. }
  86. /// <summary>
  87. /// Gets the specified request.
  88. /// </summary>
  89. /// <param name="request">The request.</param>
  90. /// <returns>System.Object.</returns>
  91. public Task<object> Get(GetHlsVideoSegmentLegacy request)
  92. {
  93. var file = request.SegmentId + Path.GetExtension(Request.PathInfo);
  94. file = Path.Combine(_config.ApplicationPaths.TranscodingTempPath, file);
  95. var normalizedPlaylistId = request.PlaylistId;
  96. var playlistPath = Directory.EnumerateFiles(_config.ApplicationPaths.TranscodingTempPath, "*")
  97. .FirstOrDefault(i => string.Equals(Path.GetExtension(i), ".m3u8", StringComparison.OrdinalIgnoreCase) && i.IndexOf(normalizedPlaylistId, StringComparison.OrdinalIgnoreCase) != -1);
  98. return GetFileResult(file, playlistPath);
  99. }
  100. /// <summary>
  101. /// Gets the specified request.
  102. /// </summary>
  103. /// <param name="request">The request.</param>
  104. /// <returns>System.Object.</returns>
  105. public object Get(GetHlsAudioSegmentLegacy request)
  106. {
  107. // TODO: Deprecate with new iOS app
  108. var file = request.SegmentId + Path.GetExtension(Request.PathInfo);
  109. file = Path.Combine(_appPaths.TranscodingTempPath, file);
  110. return ResultFactory.GetStaticFileResult(Request, file, FileShareMode.ReadWrite).Result;
  111. }
  112. private Task<object> GetFileResult(string path, string playlistPath)
  113. {
  114. var transcodingJob = ApiEntryPoint.Instance.OnTranscodeBeginRequest(playlistPath, TranscodingJobType.Hls);
  115. return ResultFactory.GetStaticFileResult(Request, new StaticFileResultOptions
  116. {
  117. Path = path,
  118. FileShare = FileShareMode.ReadWrite,
  119. OnComplete = () =>
  120. {
  121. if (transcodingJob != null)
  122. {
  123. ApiEntryPoint.Instance.OnTranscodeEndRequest(transcodingJob);
  124. }
  125. }
  126. });
  127. }
  128. }
  129. }