LiveStream.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Threading;
  5. using System.Threading.Tasks;
  6. using MediaBrowser.Controller;
  7. using MediaBrowser.Controller.IO;
  8. using MediaBrowser.Controller.LiveTv;
  9. using MediaBrowser.Model.Dto;
  10. using MediaBrowser.Model.IO;
  11. using MediaBrowser.Model.Logging;
  12. using MediaBrowser.Model.System;
  13. namespace Emby.Server.Implementations.LiveTv.TunerHosts
  14. {
  15. public class LiveStream : ILiveStream
  16. {
  17. public MediaSourceInfo OriginalMediaSource { get; set; }
  18. public MediaSourceInfo OpenedMediaSource { get; set; }
  19. public int ConsumerCount
  20. {
  21. get { return SharedStreamIds.Count; }
  22. }
  23. public ITunerHost TunerHost { get; set; }
  24. public string OriginalStreamId { get; set; }
  25. public bool EnableStreamSharing { get; set; }
  26. public string UniqueId { get; private set; }
  27. public List<string> SharedStreamIds { get; private set; }
  28. protected readonly IEnvironmentInfo Environment;
  29. protected readonly IFileSystem FileSystem;
  30. protected readonly string TempFilePath;
  31. protected readonly ILogger Logger;
  32. public LiveStream(MediaSourceInfo mediaSource, IEnvironmentInfo environment, IFileSystem fileSystem, ILogger logger, IServerApplicationPaths appPaths)
  33. {
  34. OriginalMediaSource = mediaSource;
  35. Environment = environment;
  36. FileSystem = fileSystem;
  37. OpenedMediaSource = mediaSource;
  38. Logger = logger;
  39. EnableStreamSharing = true;
  40. SharedStreamIds = new List<string>();
  41. UniqueId = Guid.NewGuid().ToString("N");
  42. TempFilePath = Path.Combine(appPaths.TranscodingTempPath, UniqueId + ".ts");
  43. }
  44. public Task Open(CancellationToken cancellationToken)
  45. {
  46. return OpenInternal(cancellationToken);
  47. }
  48. protected virtual Task OpenInternal(CancellationToken cancellationToken)
  49. {
  50. return Task.FromResult(true);
  51. }
  52. public virtual Task Close()
  53. {
  54. return Task.FromResult(true);
  55. }
  56. protected Stream GetInputStream(string path, bool allowAsyncFileRead)
  57. {
  58. var fileOpenOptions = FileOpenOptions.SequentialScan;
  59. if (allowAsyncFileRead)
  60. {
  61. fileOpenOptions |= FileOpenOptions.Asynchronous;
  62. }
  63. return FileSystem.GetFileStream(path, FileOpenMode.Open, FileAccessMode.Read, FileShareMode.ReadWrite, fileOpenOptions);
  64. }
  65. protected async Task DeleteTempFile(string path, int retryCount = 0)
  66. {
  67. try
  68. {
  69. FileSystem.DeleteFile(path);
  70. return;
  71. }
  72. catch
  73. {
  74. }
  75. if (retryCount > 20)
  76. {
  77. return;
  78. }
  79. await Task.Delay(500).ConfigureAwait(false);
  80. await DeleteTempFile(path, retryCount + 1).ConfigureAwait(false);
  81. }
  82. public async Task CopyToAsync(Stream stream, CancellationToken cancellationToken)
  83. {
  84. var allowAsync = false;//Environment.OperatingSystem != MediaBrowser.Model.System.OperatingSystem.Windows;
  85. // use non-async filestream along with read due to https://github.com/dotnet/corefx/issues/6039
  86. using (var inputStream = (FileStream)GetInputStream(TempFilePath, allowAsync))
  87. {
  88. TrySeek(inputStream, -20000);
  89. await CopyTo(inputStream, stream, 81920, null, cancellationToken).ConfigureAwait(false);
  90. }
  91. }
  92. private static async Task CopyTo(Stream source, Stream destination, int bufferSize, Action onStarted, CancellationToken cancellationToken)
  93. {
  94. byte[] buffer = new byte[bufferSize];
  95. while (true)
  96. {
  97. cancellationToken.ThrowIfCancellationRequested();
  98. var read = source.Read(buffer, 0, buffer.Length);
  99. if (read > 0)
  100. {
  101. //await destination.WriteAsync(buffer, 0, read).ConfigureAwait(false);
  102. destination.Write(buffer, 0, read);
  103. if (onStarted != null)
  104. {
  105. onStarted();
  106. onStarted = null;
  107. }
  108. }
  109. else
  110. {
  111. await Task.Delay(10).ConfigureAwait(false);
  112. }
  113. }
  114. }
  115. private void TrySeek(FileStream stream, long offset)
  116. {
  117. try
  118. {
  119. stream.Seek(offset, SeekOrigin.End);
  120. }
  121. catch (ArgumentException)
  122. {
  123. }
  124. catch (Exception ex)
  125. {
  126. Logger.ErrorException("Error seeking stream", ex);
  127. }
  128. }
  129. }
  130. }