DirectRecorder.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. using System;
  2. using System.IO;
  3. using System.Threading;
  4. using System.Threading.Tasks;
  5. using MediaBrowser.Model.IO;
  6. using MediaBrowser.Common.IO;
  7. using MediaBrowser.Common.Net;
  8. using MediaBrowser.Controller.IO;
  9. using MediaBrowser.Model.Dto;
  10. using MediaBrowser.Model.Logging;
  11. namespace Emby.Server.Implementations.LiveTv.EmbyTV
  12. {
  13. public class DirectRecorder : IRecorder
  14. {
  15. private readonly ILogger _logger;
  16. private readonly IHttpClient _httpClient;
  17. private readonly IFileSystem _fileSystem;
  18. public DirectRecorder(ILogger logger, IHttpClient httpClient, IFileSystem fileSystem)
  19. {
  20. _logger = logger;
  21. _httpClient = httpClient;
  22. _fileSystem = fileSystem;
  23. }
  24. public string GetOutputPath(MediaSourceInfo mediaSource, string targetFile)
  25. {
  26. return targetFile;
  27. }
  28. public async Task Record(MediaSourceInfo mediaSource, string targetFile, TimeSpan duration, Action onStarted, CancellationToken cancellationToken)
  29. {
  30. var httpRequestOptions = new HttpRequestOptions
  31. {
  32. Url = mediaSource.Path,
  33. BufferContent = false,
  34. // Some remote urls will expect a user agent to be supplied
  35. UserAgent = "Emby/3.0",
  36. // Shouldn't matter but may cause issues
  37. EnableHttpCompression = false
  38. };
  39. using (var response = await _httpClient.SendAsync(httpRequestOptions, "GET").ConfigureAwait(false))
  40. {
  41. _logger.Info("Opened recording stream from tuner provider");
  42. using (var output = _fileSystem.GetFileStream(targetFile, FileOpenMode.Create, FileAccessMode.Write, FileShareMode.Read))
  43. {
  44. onStarted();
  45. _logger.Info("Copying recording stream to file {0}", targetFile);
  46. // The media source if infinite so we need to handle stopping ourselves
  47. var durationToken = new CancellationTokenSource(duration);
  48. cancellationToken = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, durationToken.Token).Token;
  49. await CopyUntilCancelled(response.Content, output, cancellationToken).ConfigureAwait(false);
  50. }
  51. }
  52. _logger.Info("Recording completed to file {0}", targetFile);
  53. }
  54. private const int BufferSize = 81920;
  55. public static Task CopyUntilCancelled(Stream source, Stream target, CancellationToken cancellationToken)
  56. {
  57. return CopyUntilCancelled(source, target, null, cancellationToken);
  58. }
  59. public static async Task CopyUntilCancelled(Stream source, Stream target, Action onStarted, CancellationToken cancellationToken)
  60. {
  61. while (!cancellationToken.IsCancellationRequested)
  62. {
  63. var bytesRead = await CopyToAsyncInternal(source, target, BufferSize, onStarted, cancellationToken).ConfigureAwait(false);
  64. onStarted = null;
  65. //var position = fs.Position;
  66. //_logger.Debug("Streamed {0} bytes to position {1} from file {2}", bytesRead, position, path);
  67. if (bytesRead == 0)
  68. {
  69. await Task.Delay(100).ConfigureAwait(false);
  70. }
  71. }
  72. }
  73. private static async Task<int> CopyToAsyncInternal(Stream source, Stream destination, Int32 bufferSize, Action onStarted, CancellationToken cancellationToken)
  74. {
  75. byte[] buffer = new byte[bufferSize];
  76. int bytesRead;
  77. int totalBytesRead = 0;
  78. while ((bytesRead = await source.ReadAsync(buffer, 0, buffer.Length, cancellationToken).ConfigureAwait(false)) != 0)
  79. {
  80. await destination.WriteAsync(buffer, 0, bytesRead, cancellationToken).ConfigureAwait(false);
  81. totalBytesRead += bytesRead;
  82. if (onStarted != null)
  83. {
  84. onStarted();
  85. }
  86. onStarted = null;
  87. }
  88. return totalBytesRead;
  89. }
  90. }
  91. }