LiveStream.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Threading;
  5. using System.Threading.Tasks;
  6. using MediaBrowser.Model.Dto;
  7. using MediaBrowser.Model.IO;
  8. using MediaBrowser.Model.System;
  9. namespace MediaBrowser.Controller.LiveTv
  10. {
  11. public class LiveStream
  12. {
  13. public MediaSourceInfo OriginalMediaSource { get; set; }
  14. public MediaSourceInfo OpenedMediaSource { get; set; }
  15. public int ConsumerCount
  16. {
  17. get { return SharedStreamIds.Count; }
  18. }
  19. public ITunerHost TunerHost { get; set; }
  20. public string OriginalStreamId { get; set; }
  21. public bool EnableStreamSharing { get; set; }
  22. public string UniqueId = Guid.NewGuid().ToString("N");
  23. public List<string> SharedStreamIds = new List<string>();
  24. protected readonly IEnvironmentInfo Environment;
  25. protected readonly IFileSystem FileSystem;
  26. const int StreamCopyToBufferSize = 81920;
  27. public LiveStream(MediaSourceInfo mediaSource, IEnvironmentInfo environment, IFileSystem fileSystem)
  28. {
  29. OriginalMediaSource = mediaSource;
  30. Environment = environment;
  31. FileSystem = fileSystem;
  32. OpenedMediaSource = mediaSource;
  33. EnableStreamSharing = true;
  34. }
  35. public Task Open(CancellationToken cancellationToken)
  36. {
  37. return OpenInternal(cancellationToken);
  38. }
  39. protected virtual Task OpenInternal(CancellationToken cancellationToken)
  40. {
  41. return Task.FromResult(true);
  42. }
  43. public virtual Task Close()
  44. {
  45. return Task.FromResult(true);
  46. }
  47. private Stream GetInputStream(string path, long startPosition, bool allowAsyncFileRead)
  48. {
  49. var fileOpenOptions = startPosition > 0
  50. ? FileOpenOptions.RandomAccess
  51. : FileOpenOptions.SequentialScan;
  52. if (allowAsyncFileRead)
  53. {
  54. fileOpenOptions |= FileOpenOptions.Asynchronous;
  55. }
  56. return FileSystem.GetFileStream(path, FileOpenMode.Open, FileAccessMode.Read, FileShareMode.ReadWrite, fileOpenOptions);
  57. }
  58. protected async Task DeleteTempFile(string path, int retryCount = 0)
  59. {
  60. try
  61. {
  62. FileSystem.DeleteFile(path);
  63. return;
  64. }
  65. catch
  66. {
  67. }
  68. if (retryCount > 20)
  69. {
  70. return;
  71. }
  72. await Task.Delay(500).ConfigureAwait(false);
  73. await DeleteTempFile(path, retryCount + 1).ConfigureAwait(false);
  74. }
  75. protected async Task CopyFileTo(string path, bool allowEndOfFile, Stream outputStream, CancellationToken cancellationToken)
  76. {
  77. var eofCount = 0;
  78. long startPosition = -25000;
  79. if (startPosition < 0)
  80. {
  81. var length = FileSystem.GetFileInfo(path).Length;
  82. startPosition = Math.Max(length - startPosition, 0);
  83. }
  84. // use non-async filestream along with read due to https://github.com/dotnet/corefx/issues/6039
  85. var allowAsyncFileRead = Environment.OperatingSystem != OperatingSystem.Windows;
  86. using (var inputStream = GetInputStream(path, startPosition, allowAsyncFileRead))
  87. {
  88. if (startPosition > 0)
  89. {
  90. inputStream.Position = startPosition;
  91. }
  92. while (eofCount < 20 || !allowEndOfFile)
  93. {
  94. int bytesRead;
  95. if (allowAsyncFileRead)
  96. {
  97. bytesRead = await CopyToInternalAsync(inputStream, outputStream, cancellationToken).ConfigureAwait(false);
  98. }
  99. else
  100. {
  101. bytesRead = await CopyToInternalAsyncWithSyncRead(inputStream, outputStream, cancellationToken).ConfigureAwait(false);
  102. }
  103. //var position = fs.Position;
  104. //_logger.Debug("Streamed {0} bytes to position {1} from file {2}", bytesRead, position, path);
  105. if (bytesRead == 0)
  106. {
  107. eofCount++;
  108. await Task.Delay(100, cancellationToken).ConfigureAwait(false);
  109. }
  110. else
  111. {
  112. eofCount = 0;
  113. }
  114. }
  115. }
  116. }
  117. private async Task<int> CopyToInternalAsyncWithSyncRead(Stream source, Stream destination, CancellationToken cancellationToken)
  118. {
  119. var array = new byte[StreamCopyToBufferSize];
  120. int bytesRead;
  121. int totalBytesRead = 0;
  122. while ((bytesRead = source.Read(array, 0, array.Length)) != 0)
  123. {
  124. var bytesToWrite = bytesRead;
  125. if (bytesToWrite > 0)
  126. {
  127. await destination.WriteAsync(array, 0, Convert.ToInt32(bytesToWrite), cancellationToken).ConfigureAwait(false);
  128. totalBytesRead += bytesRead;
  129. }
  130. }
  131. return totalBytesRead;
  132. }
  133. private async Task<int> CopyToInternalAsync(Stream source, Stream destination, CancellationToken cancellationToken)
  134. {
  135. var array = new byte[StreamCopyToBufferSize];
  136. int bytesRead;
  137. int totalBytesRead = 0;
  138. while ((bytesRead = await source.ReadAsync(array, 0, array.Length, cancellationToken).ConfigureAwait(false)) != 0)
  139. {
  140. var bytesToWrite = bytesRead;
  141. if (bytesToWrite > 0)
  142. {
  143. await destination.WriteAsync(array, 0, Convert.ToInt32(bytesToWrite), cancellationToken).ConfigureAwait(false);
  144. totalBytesRead += bytesRead;
  145. }
  146. }
  147. return totalBytesRead;
  148. }
  149. }
  150. }