HlsHelpers.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. using System;
  2. using System.Globalization;
  3. using System.IO;
  4. using System.Runtime.InteropServices;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. using Jellyfin.Api.Models.StreamingDtos;
  8. using MediaBrowser.Model.IO;
  9. using Microsoft.Extensions.Logging;
  10. namespace Jellyfin.Api.Helpers
  11. {
  12. /// <summary>
  13. /// The hls helpers.
  14. /// </summary>
  15. public static class HlsHelpers
  16. {
  17. /// <summary>
  18. /// Waits for a minimum number of segments to be available.
  19. /// </summary>
  20. /// <param name="playlist">The playlist string.</param>
  21. /// <param name="segmentCount">The segment count.</param>
  22. /// <param name="logger">Instance of the <see cref="ILogger"/> interface.</param>
  23. /// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
  24. /// <returns>A <see cref="Task"/> indicating the waiting process.</returns>
  25. public static async Task WaitForMinimumSegmentCount(string playlist, int? segmentCount, ILogger logger, CancellationToken cancellationToken)
  26. {
  27. logger.LogDebug("Waiting for {0} segments in {1}", segmentCount, playlist);
  28. while (!cancellationToken.IsCancellationRequested)
  29. {
  30. try
  31. {
  32. // Need to use FileShare.ReadWrite because we're reading the file at the same time it's being written
  33. var fileStream = new FileStream(
  34. playlist,
  35. FileMode.Open,
  36. FileAccess.Read,
  37. FileShare.ReadWrite,
  38. IODefaults.FileStreamBufferSize,
  39. FileOptions.SequentialScan);
  40. await using (fileStream.ConfigureAwait(false))
  41. {
  42. using var reader = new StreamReader(fileStream);
  43. var count = 0;
  44. while (!reader.EndOfStream)
  45. {
  46. var line = await reader.ReadLineAsync().ConfigureAwait(false);
  47. if (line == null)
  48. {
  49. // Nothing currently in buffer.
  50. break;
  51. }
  52. if (line.IndexOf("#EXTINF:", StringComparison.OrdinalIgnoreCase) != -1)
  53. {
  54. count++;
  55. if (count >= segmentCount)
  56. {
  57. logger.LogDebug("Finished waiting for {0} segments in {1}", segmentCount, playlist);
  58. return;
  59. }
  60. }
  61. }
  62. }
  63. await Task.Delay(100, cancellationToken).ConfigureAwait(false);
  64. }
  65. catch (IOException)
  66. {
  67. // May get an error if the file is locked
  68. }
  69. await Task.Delay(50, cancellationToken).ConfigureAwait(false);
  70. }
  71. }
  72. /// <summary>
  73. /// Gets the extension of segment container.
  74. /// </summary>
  75. /// <param name="segmentContainer">The name of the segment container.</param>
  76. /// <returns>The string text of extension.</returns>
  77. public static string GetSegmentFileExtension(string? segmentContainer)
  78. {
  79. if (!string.IsNullOrWhiteSpace(segmentContainer))
  80. {
  81. return "." + segmentContainer;
  82. }
  83. return ".ts";
  84. }
  85. /// <summary>
  86. /// Gets the #EXT-X-MAP string.
  87. /// </summary>
  88. /// <param name="outputPath">The output path of the file.</param>
  89. /// <param name="state">The <see cref="StreamState"/>.</param>
  90. /// <param name="isOsDepends">Get a normal string or depends on OS.</param>
  91. /// <returns>The string text of #EXT-X-MAP.</returns>
  92. public static string GetFmp4InitFileName(string outputPath, StreamState state, bool isOsDepends)
  93. {
  94. var outputFileNameWithoutExtension = Path.GetFileNameWithoutExtension(outputPath);
  95. var outputPrefix = Path.Combine(Path.GetDirectoryName(outputPath), outputFileNameWithoutExtension);
  96. var outputExtension = GetSegmentFileExtension(state.Request.SegmentContainer);
  97. // on Linux/Unix
  98. // #EXT-X-MAP:URI="prefix-1.mp4"
  99. var fmp4InitFileName = outputFileNameWithoutExtension + "-1" + outputExtension;
  100. if (!isOsDepends)
  101. {
  102. return fmp4InitFileName;
  103. }
  104. var isWindows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
  105. if (isWindows)
  106. {
  107. // on Windows
  108. // #EXT-X-MAP:URI="X:\transcodes\prefix-1.mp4"
  109. fmp4InitFileName = outputPrefix + "-1" + outputExtension;
  110. }
  111. return fmp4InitFileName;
  112. }
  113. /// <summary>
  114. /// Gets the hls playlist text.
  115. /// </summary>
  116. /// <param name="path">The path to the playlist file.</param>
  117. /// <param name="state">The <see cref="StreamState"/>.</param>
  118. /// <returns>The playlist text as a string.</returns>
  119. public static string GetLivePlaylistText(string path, StreamState state)
  120. {
  121. using var stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
  122. using var reader = new StreamReader(stream);
  123. var text = reader.ReadToEnd();
  124. var segmentFormat = GetSegmentFileExtension(state.Request.SegmentContainer).TrimStart('.');
  125. if (string.Equals(segmentFormat, "mp4", StringComparison.OrdinalIgnoreCase))
  126. {
  127. var fmp4InitFileName = GetFmp4InitFileName(path, state, true);
  128. var baseUrlParam = string.Format(
  129. CultureInfo.InvariantCulture,
  130. "hls/{0}/",
  131. Path.GetFileNameWithoutExtension(path));
  132. var newFmp4InitFileName = baseUrlParam + GetFmp4InitFileName(path, state, false);
  133. // Replace fMP4 init file URI.
  134. text = text.Replace(fmp4InitFileName, newFmp4InitFileName, StringComparison.InvariantCulture);
  135. }
  136. return text;
  137. }
  138. }
  139. }