HlsSegmentService.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. using MediaBrowser.Controller;
  2. using MediaBrowser.Controller.Configuration;
  3. using MediaBrowser.Controller.Net;
  4. using ServiceStack;
  5. using System;
  6. using System.IO;
  7. using System.Linq;
  8. namespace MediaBrowser.Api.Playback.Hls
  9. {
  10. /// <summary>
  11. /// Class GetHlsAudioSegment
  12. /// </summary>
  13. [Route("/Audio/{Id}/hls/{SegmentId}/stream.mp3", "GET")]
  14. [Route("/Audio/{Id}/hls/{SegmentId}/stream.aac", "GET")]
  15. [Api(Description = "Gets an Http live streaming segment file. Internal use only.")]
  16. public class GetHlsAudioSegmentLegacy
  17. {
  18. // TODO: Deprecate with new iOS app
  19. /// <summary>
  20. /// Gets or sets the id.
  21. /// </summary>
  22. /// <value>The id.</value>
  23. public string Id { get; set; }
  24. /// <summary>
  25. /// Gets or sets the segment id.
  26. /// </summary>
  27. /// <value>The segment id.</value>
  28. public string SegmentId { get; set; }
  29. }
  30. /// <summary>
  31. /// Class GetHlsVideoSegment
  32. /// </summary>
  33. [Route("/Videos/{Id}/hls/{PlaylistId}/stream.m3u8", "GET")]
  34. [Api(Description = "Gets an Http live streaming segment file. Internal use only.")]
  35. public class GetHlsPlaylistLegacy
  36. {
  37. // TODO: Deprecate with new iOS app
  38. /// <summary>
  39. /// Gets or sets the id.
  40. /// </summary>
  41. /// <value>The id.</value>
  42. public string Id { get; set; }
  43. public string PlaylistId { get; set; }
  44. }
  45. [Route("/Videos/ActiveEncodings", "DELETE")]
  46. [Api(Description = "Stops an encoding process")]
  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. [Api(Description = "Gets an Http live streaming segment file. Internal use only.")]
  59. public class GetHlsVideoSegmentLegacy : VideoStreamRequest
  60. {
  61. // TODO: Deprecate with new iOS app
  62. public string PlaylistId { get; set; }
  63. /// <summary>
  64. /// Gets or sets the segment id.
  65. /// </summary>
  66. /// <value>The segment id.</value>
  67. public string SegmentId { get; set; }
  68. }
  69. public class HlsSegmentService : BaseApiService
  70. {
  71. private readonly IServerApplicationPaths _appPaths;
  72. private readonly IServerConfigurationManager _config;
  73. public HlsSegmentService(IServerApplicationPaths appPaths, IServerConfigurationManager config)
  74. {
  75. _appPaths = appPaths;
  76. _config = config;
  77. }
  78. public object Get(GetHlsPlaylistLegacy request)
  79. {
  80. var file = request.PlaylistId + Path.GetExtension(Request.PathInfo);
  81. file = Path.Combine(_appPaths.TranscodingTempPath, file);
  82. return GetFileResult(file, file);
  83. }
  84. public void Delete(StopEncodingProcess request)
  85. {
  86. ApiEntryPoint.Instance.KillTranscodingJobs(request.DeviceId, request.PlaySessionId, path => true);
  87. }
  88. /// <summary>
  89. /// Gets the specified request.
  90. /// </summary>
  91. /// <param name="request">The request.</param>
  92. /// <returns>System.Object.</returns>
  93. public object Get(GetHlsVideoSegmentLegacy request)
  94. {
  95. var file = request.SegmentId + Path.GetExtension(Request.PathInfo);
  96. file = Path.Combine(_config.ApplicationPaths.TranscodingTempPath, file);
  97. var normalizedPlaylistId = request.PlaylistId.Replace("-low", string.Empty);
  98. var playlistPath = Directory.EnumerateFiles(_config.ApplicationPaths.TranscodingTempPath, "*")
  99. .FirstOrDefault(i => string.Equals(Path.GetExtension(i), ".m3u8", StringComparison.OrdinalIgnoreCase) && i.IndexOf(normalizedPlaylistId, StringComparison.OrdinalIgnoreCase) != -1);
  100. return GetFileResult(file, playlistPath);
  101. }
  102. /// <summary>
  103. /// Gets the specified request.
  104. /// </summary>
  105. /// <param name="request">The request.</param>
  106. /// <returns>System.Object.</returns>
  107. public object Get(GetHlsAudioSegmentLegacy request)
  108. {
  109. // TODO: Deprecate with new iOS app
  110. var file = request.SegmentId + Path.GetExtension(Request.PathInfo);
  111. file = Path.Combine(_appPaths.TranscodingTempPath, file);
  112. return ResultFactory.GetStaticFileResult(Request, file, FileShare.ReadWrite);
  113. }
  114. private object GetFileResult(string path, string playlistPath)
  115. {
  116. var transcodingJob = ApiEntryPoint.Instance.OnTranscodeBeginRequest(playlistPath, TranscodingJobType.Hls);
  117. return ResultFactory.GetStaticFileResult(Request, new StaticFileResultOptions
  118. {
  119. Path = path,
  120. FileShare = FileShare.ReadWrite,
  121. OnComplete = () =>
  122. {
  123. if (transcodingJob != null)
  124. {
  125. ApiEntryPoint.Instance.OnTranscodeEndRequest(transcodingJob);
  126. }
  127. }
  128. });
  129. }
  130. }
  131. }