HlsSegmentService.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 GetHlsAudioSegment
  17. {
  18. /// <summary>
  19. /// Gets or sets the id.
  20. /// </summary>
  21. /// <value>The id.</value>
  22. public string Id { get; set; }
  23. /// <summary>
  24. /// Gets or sets the segment id.
  25. /// </summary>
  26. /// <value>The segment id.</value>
  27. public string SegmentId { get; set; }
  28. }
  29. /// <summary>
  30. /// Class GetHlsVideoSegment
  31. /// </summary>
  32. [Route("/Videos/{Id}/hls/{PlaylistId}/stream.m3u8", "GET")]
  33. [Api(Description = "Gets an Http live streaming segment file. Internal use only.")]
  34. public class GetHlsPlaylist
  35. {
  36. // TODO: Deprecate with new iOS app
  37. /// <summary>
  38. /// Gets or sets the id.
  39. /// </summary>
  40. /// <value>The id.</value>
  41. public string Id { get; set; }
  42. public string PlaylistId { get; set; }
  43. }
  44. [Route("/Videos/ActiveEncodings", "DELETE")]
  45. [Api(Description = "Stops an encoding process")]
  46. public class StopEncodingProcess
  47. {
  48. [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")]
  49. public string DeviceId { get; set; }
  50. [ApiMember(Name = "PlaySessionId", Description = "The play session id", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "DELETE")]
  51. public string PlaySessionId { get; set; }
  52. }
  53. /// <summary>
  54. /// Class GetHlsVideoSegment
  55. /// </summary>
  56. [Route("/Videos/{Id}/hls/{PlaylistId}/{SegmentId}.ts", "GET")]
  57. [Api(Description = "Gets an Http live streaming segment file. Internal use only.")]
  58. public class GetHlsVideoSegment : 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 object Get(GetHlsPlaylist 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 object Get(GetHlsVideoSegment request)
  92. {
  93. var file = request.SegmentId + Path.GetExtension(Request.PathInfo);
  94. file = Path.Combine(_config.ApplicationPaths.TranscodingTempPath, file);
  95. var normalizedPlaylistId = request.PlaylistId.Replace("-low", string.Empty);
  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(GetHlsAudioSegment 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, FileShare.ReadWrite);
  111. }
  112. private 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 = FileShare.ReadWrite,
  119. OnComplete = () =>
  120. {
  121. if (transcodingJob != null)
  122. {
  123. ApiEntryPoint.Instance.OnTranscodeEndRequest(transcodingJob);
  124. }
  125. }
  126. });
  127. }
  128. }
  129. }