HlsHelpers.cs 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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.IndexOf("#EXTINF:", StringComparison.OrdinalIgnoreCase) != -1)
  46. {
  47. count++;
  48. if (count >= segmentCount)
  49. {
  50. logger.LogDebug("Finished waiting for {0} segments in {1}", segmentCount, playlist);
  51. return;
  52. }
  53. }
  54. }
  55. }
  56. await Task.Delay(100, cancellationToken).ConfigureAwait(false);
  57. }
  58. catch (IOException)
  59. {
  60. // May get an error if the file is locked
  61. }
  62. await Task.Delay(50, cancellationToken).ConfigureAwait(false);
  63. }
  64. }
  65. /// <summary>
  66. /// Gets the hls playlist text.
  67. /// </summary>
  68. /// <param name="path">The path to the playlist file.</param>
  69. /// <param name="segmentLength">The segment length.</param>
  70. /// <returns>The playlist text as a string.</returns>
  71. public static string GetLivePlaylistText(string path, int segmentLength)
  72. {
  73. using var stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
  74. using var reader = new StreamReader(stream);
  75. var text = reader.ReadToEnd();
  76. text = text.Replace("#EXTM3U", "#EXTM3U\n#EXT-X-PLAYLIST-TYPE:EVENT", StringComparison.InvariantCulture);
  77. var newDuration = "#EXT-X-TARGETDURATION:" + segmentLength.ToString(CultureInfo.InvariantCulture);
  78. text = text.Replace("#EXT-X-TARGETDURATION:" + (segmentLength - 1).ToString(CultureInfo.InvariantCulture), newDuration, StringComparison.OrdinalIgnoreCase);
  79. // text = text.Replace("#EXT-X-TARGETDURATION:" + (segmentLength + 1).ToString(CultureInfo.InvariantCulture), newDuration, StringComparison.OrdinalIgnoreCase);
  80. return text;
  81. }
  82. }
  83. }