DirectRecorder.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #pragma warning disable CS1591
  2. using System;
  3. using System.IO;
  4. using System.Net.Http;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. using MediaBrowser.Common.Net;
  8. using MediaBrowser.Controller.Library;
  9. using MediaBrowser.Model.Dto;
  10. using MediaBrowser.Model.IO;
  11. using Microsoft.Extensions.Logging;
  12. namespace Emby.Server.Implementations.LiveTv.EmbyTV
  13. {
  14. public class DirectRecorder : IRecorder
  15. {
  16. private readonly ILogger _logger;
  17. private readonly IHttpClient _httpClient;
  18. private readonly IStreamHelper _streamHelper;
  19. public DirectRecorder(ILogger logger, IHttpClient httpClient, IStreamHelper streamHelper)
  20. {
  21. _logger = logger;
  22. _httpClient = httpClient;
  23. _streamHelper = streamHelper;
  24. }
  25. public string GetOutputPath(MediaSourceInfo mediaSource, string targetFile)
  26. {
  27. return targetFile;
  28. }
  29. public Task Record(IDirectStreamProvider directStreamProvider, MediaSourceInfo mediaSource, string targetFile, TimeSpan duration, Action onStarted, CancellationToken cancellationToken)
  30. {
  31. if (directStreamProvider != null)
  32. {
  33. return RecordFromDirectStreamProvider(directStreamProvider, targetFile, duration, onStarted, cancellationToken);
  34. }
  35. return RecordFromMediaSource(mediaSource, targetFile, duration, onStarted, cancellationToken);
  36. }
  37. private async Task RecordFromDirectStreamProvider(IDirectStreamProvider directStreamProvider, string targetFile, TimeSpan duration, Action onStarted, CancellationToken cancellationToken)
  38. {
  39. Directory.CreateDirectory(Path.GetDirectoryName(targetFile));
  40. using (var output = new FileStream(targetFile, FileMode.Create, FileAccess.Write, FileShare.Read))
  41. {
  42. onStarted();
  43. _logger.LogInformation("Copying recording stream to file {0}", targetFile);
  44. // The media source is infinite so we need to handle stopping ourselves
  45. var durationToken = new CancellationTokenSource(duration);
  46. cancellationToken = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, durationToken.Token).Token;
  47. await directStreamProvider.CopyToAsync(output, cancellationToken).ConfigureAwait(false);
  48. }
  49. _logger.LogInformation("Recording completed to file {0}", targetFile);
  50. }
  51. private async Task RecordFromMediaSource(MediaSourceInfo mediaSource, string targetFile, TimeSpan duration, Action onStarted, CancellationToken cancellationToken)
  52. {
  53. var httpRequestOptions = new HttpRequestOptions
  54. {
  55. Url = mediaSource.Path,
  56. BufferContent = false,
  57. // Some remote urls will expect a user agent to be supplied
  58. UserAgent = "Emby/3.0",
  59. // Shouldn't matter but may cause issues
  60. DecompressionMethod = CompressionMethod.None
  61. };
  62. using (var response = await _httpClient.SendAsync(httpRequestOptions, HttpMethod.Get).ConfigureAwait(false))
  63. {
  64. _logger.LogInformation("Opened recording stream from tuner provider");
  65. Directory.CreateDirectory(Path.GetDirectoryName(targetFile));
  66. using (var output = new FileStream(targetFile, FileMode.Create, FileAccess.Write, FileShare.Read))
  67. {
  68. onStarted();
  69. _logger.LogInformation("Copying recording stream to file {0}", targetFile);
  70. // The media source if infinite so we need to handle stopping ourselves
  71. var durationToken = new CancellationTokenSource(duration);
  72. cancellationToken = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, durationToken.Token).Token;
  73. await _streamHelper.CopyUntilCancelled(response.Content, output, 81920, cancellationToken).ConfigureAwait(false);
  74. }
  75. }
  76. _logger.LogInformation("Recording completed to file {0}", targetFile);
  77. }
  78. }
  79. }