Skip to content

Commit

Permalink
Move opening responisbility to LiveTvService
Browse files Browse the repository at this point in the history
  • Loading branch information
Kevinjil committed Jan 8, 2025
1 parent 336d7af commit f1f020d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
5 changes: 3 additions & 2 deletions Jellyfin.Xtream/LiveTvService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ public Task ResetTuner(string id, CancellationToken cancellationToken)
}

/// <inheritdoc />
public Task<ILiveStream> GetChannelStreamWithDirectStreamProvider(string channelId, string streamId, List<ILiveStream> currentLiveStreams, CancellationToken cancellationToken)
public async Task<ILiveStream> GetChannelStreamWithDirectStreamProvider(string channelId, string streamId, List<ILiveStream> currentLiveStreams, CancellationToken cancellationToken)
{
Guid guid = Guid.Parse(channelId);
StreamService.FromGuid(guid, out int prefix, out int channel, out int _, out int _);
Expand All @@ -233,9 +233,10 @@ public Task<ILiveStream> GetChannelStreamWithDirectStreamProvider(string channel
if (stream == null)
{
stream = new Restream(appHost, httpClientFactory, logger, mediaSourceInfo);
await stream.Open(cancellationToken).ConfigureAwait(false);
}

stream.ConsumerCount++;
return Task.FromResult(stream);
return stream;
}
}
10 changes: 5 additions & 5 deletions Jellyfin.Xtream/Service/Restream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ namespace Jellyfin.Xtream.Service;
/// <summary>
/// A live stream implementation that can be restreamed.
/// </summary>
public class Restream : ILiveStream, IDisposable
public class Restream : ILiveStream, IDirectStreamProvider, IDisposable
{
/// <summary>
/// The global constant for the restream tuner host.
Expand Down Expand Up @@ -113,7 +113,7 @@ public async Task Open(CancellationToken openCancellationToken)
{
if (inputStream != null)
{
// Channel is already opened.
logger.LogWarning("Restream for channel {ChannelId} is already open.", MediaSource.Id);
return;
}

Expand All @@ -122,7 +122,7 @@ public async Task Open(CancellationToken openCancellationToken)

// Response stream is disposed manually.
HttpResponseMessage response = await httpClientFactory.CreateClient(NamedClient.Default)
.GetAsync(uri, HttpCompletionOption.ResponseHeadersRead, CancellationToken.None)
.GetAsync(uri, HttpCompletionOption.ResponseHeadersRead, openCancellationToken)
.ConfigureAwait(true);
logger.LogDebug("Stream for channel {ChannelId} using url {Url}", channelId, uri);

Expand All @@ -131,7 +131,7 @@ public async Task Open(CancellationToken openCancellationToken)
{
logger.LogDebug("Stream for channel {ChannelId} redirected to url {Url}", channelId, response.Headers.Location);
response = await httpClientFactory.CreateClient(NamedClient.Default)
.GetAsync(response.Headers.Location, HttpCompletionOption.ResponseHeadersRead, CancellationToken.None)
.GetAsync(response.Headers.Location, HttpCompletionOption.ResponseHeadersRead, openCancellationToken)
.ConfigureAwait(true);
}

Expand Down Expand Up @@ -166,7 +166,7 @@ public Stream GetStream()
{
if (inputStream == null)
{
logger.LogInformation("Restream for channel {ChannelId} was not opened.", mediaSource.Id);
logger.LogWarning("Restream for channel {ChannelId} was not opened.", mediaSource.Id);
_ = Open(CancellationToken.None);
}

Expand Down

0 comments on commit f1f020d

Please sign in to comment.