jellyfin/MediaBrowser.Controller/LiveTv/LiveStream.cs

88 lines
2.5 KiB
C#
Raw Normal View History

2016-09-29 14:55:49 +02:00
using System;
2016-10-10 20:18:28 +02:00
using System.Collections.Generic;
2017-05-22 06:54:02 +02:00
using System.IO;
2016-09-29 14:55:49 +02:00
using System.Threading;
2016-09-25 20:39:13 +02:00
using System.Threading.Tasks;
using MediaBrowser.Model.Dto;
2017-05-22 06:54:02 +02:00
using MediaBrowser.Model.IO;
using MediaBrowser.Model.System;
2016-09-25 20:39:13 +02:00
namespace MediaBrowser.Controller.LiveTv
{
public class LiveStream
{
public MediaSourceInfo OriginalMediaSource { get; set; }
2016-09-29 14:55:49 +02:00
public MediaSourceInfo OpenedMediaSource { get; set; }
2017-05-22 06:54:02 +02:00
public int ConsumerCount
{
2016-10-10 20:18:28 +02:00
get { return SharedStreamIds.Count; }
}
2016-09-29 14:55:49 +02:00
public ITunerHost TunerHost { get; set; }
public string OriginalStreamId { get; set; }
2016-09-30 08:50:06 +02:00
public bool EnableStreamSharing { get; set; }
2016-10-07 17:08:13 +02:00
public string UniqueId = Guid.NewGuid().ToString("N");
2016-09-25 20:39:13 +02:00
2017-05-22 06:54:02 +02:00
public List<string> SharedStreamIds = new List<string>();
protected readonly IEnvironmentInfo Environment;
protected readonly IFileSystem FileSystem;
const int StreamCopyToBufferSize = 81920;
2016-10-10 20:18:28 +02:00
2017-05-22 06:54:02 +02:00
public LiveStream(MediaSourceInfo mediaSource, IEnvironmentInfo environment, IFileSystem fileSystem)
2016-09-25 20:39:13 +02:00
{
OriginalMediaSource = mediaSource;
2017-05-22 06:54:02 +02:00
Environment = environment;
FileSystem = fileSystem;
2016-09-29 14:55:49 +02:00
OpenedMediaSource = mediaSource;
2016-09-30 08:50:06 +02:00
EnableStreamSharing = true;
2016-09-25 20:39:13 +02:00
}
2016-10-10 20:18:28 +02:00
public Task Open(CancellationToken cancellationToken)
2016-09-29 14:55:49 +02:00
{
2016-10-10 20:18:28 +02:00
return OpenInternal(cancellationToken);
2016-09-29 14:55:49 +02:00
}
protected virtual Task OpenInternal(CancellationToken cancellationToken)
2016-09-25 20:39:13 +02:00
{
return Task.FromResult(true);
}
public virtual Task Close()
{
return Task.FromResult(true);
}
2017-05-22 06:54:02 +02:00
2017-09-10 05:47:33 +02:00
protected Stream GetInputStream(string path, bool allowAsyncFileRead)
2017-05-22 06:54:02 +02:00
{
2017-06-01 06:51:43 +02:00
var fileOpenOptions = FileOpenOptions.SequentialScan;
2017-05-22 06:54:02 +02:00
if (allowAsyncFileRead)
{
fileOpenOptions |= FileOpenOptions.Asynchronous;
}
return FileSystem.GetFileStream(path, FileOpenMode.Open, FileAccessMode.Read, FileShareMode.ReadWrite, fileOpenOptions);
}
protected async Task DeleteTempFile(string path, int retryCount = 0)
{
try
{
FileSystem.DeleteFile(path);
return;
}
catch
{
}
if (retryCount > 20)
{
return;
}
await Task.Delay(500).ConfigureAwait(false);
await DeleteTempFile(path, retryCount + 1).ConfigureAwait(false);
}
2016-09-25 20:39:13 +02:00
}
}