HlsHelpers.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. using System;
  2. using System.Globalization;
  3. using System.IO;
  4. using System.Threading;
  5. using System.Threading.Tasks;
  6. using MediaBrowser.Model.IO;
  7. using Microsoft.Extensions.Logging;
  8. namespace Jellyfin.Api.Helpers
  9. {
  10. /// <summary>
  11. /// The hls helpers.
  12. /// </summary>
  13. public static class HlsHelpers
  14. {
  15. /// <summary>
  16. /// Waits for a minimum number of segments to be available.
  17. /// </summary>
  18. /// <param name="playlist">The playlist string.</param>
  19. /// <param name="segmentCount">The segment count.</param>
  20. /// <param name="logger">Instance of the <see cref="ILogger"/> interface.</param>
  21. /// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
  22. /// <returns>A <see cref="Task"/> indicating the waiting process.</returns>
  23. public static async Task WaitForMinimumSegmentCount(string playlist, int? segmentCount, ILogger logger, CancellationToken cancellationToken)
  24. {
  25. logger.LogDebug("Waiting for {0} segments in {1}", segmentCount, playlist);
  26. while (!cancellationToken.IsCancellationRequested)
  27. {
  28. try
  29. {
  30. // Need to use FileShare.ReadWrite because we're reading the file at the same time it's being written
  31. var fileStream = new FileStream(
  32. playlist,
  33. FileMode.Open,
  34. FileAccess.Read,
  35. FileShare.ReadWrite,
  36. IODefaults.FileStreamBufferSize,
  37. FileOptions.SequentialScan);
  38. await using (fileStream.ConfigureAwait(false))
  39. {
  40. using var reader = new StreamReader(fileStream);
  41. var count = 0;
  42. while (!reader.EndOfStream)
  43. {
  44. var line = await reader.ReadLineAsync().ConfigureAwait(false);
  45. if (line == null)
  46. {
  47. // Nothing currently in buffer.
  48. break;
  49. }
  50. if (line.IndexOf("#EXTINF:", StringComparison.OrdinalIgnoreCase) != -1)
  51. {
  52. count++;
  53. if (count >= segmentCount)
  54. {
  55. logger.LogDebug("Finished waiting for {0} segments in {1}", segmentCount, playlist);
  56. return;
  57. }
  58. }
  59. }
  60. }
  61. await Task.Delay(100, cancellationToken).ConfigureAwait(false);
  62. }
  63. catch (IOException)
  64. {
  65. // May get an error if the file is locked
  66. }
  67. await Task.Delay(50, cancellationToken).ConfigureAwait(false);
  68. }
  69. }
  70. /// <summary>
  71. /// Gets the hls playlist text.
  72. /// </summary>
  73. /// <param name="path">The path to the playlist file.</param>
  74. /// <param name="segmentLength">The segment length.</param>
  75. /// <returns>The playlist text as a string.</returns>
  76. public static string GetLivePlaylistText(string path, int segmentLength)
  77. {
  78. using var stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
  79. using var reader = new StreamReader(stream);
  80. var text = reader.ReadToEnd();
  81. text = text.Replace("#EXTM3U", "#EXTM3U\n#EXT-X-PLAYLIST-TYPE:EVENT", StringComparison.InvariantCulture);
  82. var newDuration = "#EXT-X-TARGETDURATION:" + segmentLength.ToString(CultureInfo.InvariantCulture);
  83. text = text.Replace("#EXT-X-TARGETDURATION:" + (segmentLength - 1).ToString(CultureInfo.InvariantCulture), newDuration, StringComparison.OrdinalIgnoreCase);
  84. // text = text.Replace("#EXT-X-TARGETDURATION:" + (segmentLength + 1).ToString(CultureInfo.InvariantCulture), newDuration, StringComparison.OrdinalIgnoreCase);
  85. return text;
  86. }
  87. }
  88. }