HlsSegmentService.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 GetHlsVideoStream
  32. /// </summary>
  33. [Route("/Videos/{Id}/stream.m3u8", "GET")]
  34. [Api(Description = "Gets a video stream using HTTP live streaming.")]
  35. public class GetHlsVideoStreamLegacy : VideoStreamRequest
  36. {
  37. // TODO: Deprecate with new iOS app
  38. [ApiMember(Name = "BaselineStreamAudioBitRate", Description = "Optional. Specify the audio bitrate for the baseline stream.", IsRequired = false, DataType = "int", ParameterType = "query", Verb = "GET")]
  39. public int? BaselineStreamAudioBitRate { get; set; }
  40. [ApiMember(Name = "AppendBaselineStream", Description = "Optional. Whether or not to include a baseline audio-only stream in the master playlist.", IsRequired = false, DataType = "bool", ParameterType = "query", Verb = "GET")]
  41. public bool AppendBaselineStream { get; set; }
  42. [ApiMember(Name = "TimeStampOffsetMs", Description = "Optional. Alter the timestamps in the playlist by a given amount, in ms. Default is 1000.", IsRequired = false, DataType = "int", ParameterType = "query", Verb = "GET")]
  43. public int TimeStampOffsetMs { get; set; }
  44. }
  45. /// <summary>
  46. /// Class GetHlsVideoSegment
  47. /// </summary>
  48. [Route("/Videos/{Id}/hls/{PlaylistId}/stream.m3u8", "GET")]
  49. [Api(Description = "Gets an Http live streaming segment file. Internal use only.")]
  50. public class GetHlsPlaylistLegacy
  51. {
  52. // TODO: Deprecate with new iOS app
  53. /// <summary>
  54. /// Gets or sets the id.
  55. /// </summary>
  56. /// <value>The id.</value>
  57. public string Id { get; set; }
  58. public string PlaylistId { get; set; }
  59. }
  60. [Route("/Videos/ActiveEncodings", "DELETE")]
  61. [Api(Description = "Stops an encoding process")]
  62. public class StopEncodingProcess
  63. {
  64. [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")]
  65. public string DeviceId { get; set; }
  66. [ApiMember(Name = "PlaySessionId", Description = "The play session id", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "DELETE")]
  67. public string PlaySessionId { get; set; }
  68. }
  69. /// <summary>
  70. /// Class GetHlsVideoSegment
  71. /// </summary>
  72. [Route("/Videos/{Id}/hls/{PlaylistId}/{SegmentId}.ts", "GET")]
  73. [Api(Description = "Gets an Http live streaming segment file. Internal use only.")]
  74. public class GetHlsVideoSegmentLegacy : VideoStreamRequest
  75. {
  76. // TODO: Deprecate with new iOS app
  77. public string PlaylistId { get; set; }
  78. /// <summary>
  79. /// Gets or sets the segment id.
  80. /// </summary>
  81. /// <value>The segment id.</value>
  82. public string SegmentId { get; set; }
  83. }
  84. public class HlsSegmentService : BaseApiService
  85. {
  86. private readonly IServerApplicationPaths _appPaths;
  87. private readonly IServerConfigurationManager _config;
  88. public HlsSegmentService(IServerApplicationPaths appPaths, IServerConfigurationManager config)
  89. {
  90. _appPaths = appPaths;
  91. _config = config;
  92. }
  93. public object Get(GetHlsPlaylistLegacy request)
  94. {
  95. var file = request.PlaylistId + Path.GetExtension(Request.PathInfo);
  96. file = Path.Combine(_appPaths.TranscodingTempPath, file);
  97. return GetFileResult(file, file);
  98. }
  99. public void Delete(StopEncodingProcess request)
  100. {
  101. ApiEntryPoint.Instance.KillTranscodingJobs(request.DeviceId, request.PlaySessionId, path => true);
  102. }
  103. /// <summary>
  104. /// Gets the specified request.
  105. /// </summary>
  106. /// <param name="request">The request.</param>
  107. /// <returns>System.Object.</returns>
  108. public object Get(GetHlsVideoSegmentLegacy request)
  109. {
  110. var file = request.SegmentId + Path.GetExtension(Request.PathInfo);
  111. file = Path.Combine(_config.ApplicationPaths.TranscodingTempPath, file);
  112. var normalizedPlaylistId = request.PlaylistId.Replace("-low", string.Empty);
  113. var playlistPath = Directory.EnumerateFiles(_config.ApplicationPaths.TranscodingTempPath, "*")
  114. .FirstOrDefault(i => string.Equals(Path.GetExtension(i), ".m3u8", StringComparison.OrdinalIgnoreCase) && i.IndexOf(normalizedPlaylistId, StringComparison.OrdinalIgnoreCase) != -1);
  115. return GetFileResult(file, playlistPath);
  116. }
  117. /// <summary>
  118. /// Gets the specified request.
  119. /// </summary>
  120. /// <param name="request">The request.</param>
  121. /// <returns>System.Object.</returns>
  122. public object Get(GetHlsAudioSegmentLegacy request)
  123. {
  124. // TODO: Deprecate with new iOS app
  125. var file = request.SegmentId + Path.GetExtension(Request.PathInfo);
  126. file = Path.Combine(_appPaths.TranscodingTempPath, file);
  127. return ResultFactory.GetStaticFileResult(Request, file, FileShare.ReadWrite);
  128. }
  129. private object GetFileResult(string path, string playlistPath)
  130. {
  131. var transcodingJob = ApiEntryPoint.Instance.OnTranscodeBeginRequest(playlistPath, TranscodingJobType.Hls);
  132. return ResultFactory.GetStaticFileResult(Request, new StaticFileResultOptions
  133. {
  134. Path = path,
  135. FileShare = FileShare.ReadWrite,
  136. OnComplete = () =>
  137. {
  138. if (transcodingJob != null)
  139. {
  140. ApiEntryPoint.Instance.OnTranscodeEndRequest(transcodingJob);
  141. }
  142. }
  143. });
  144. }
  145. }
  146. }