Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Live TV DI #10951

Merged
merged 3 commits into from
Feb 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions Emby.Server.Implementations/ApplicationHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -695,8 +695,6 @@ private void FindParts()
GetExports<IMetadataSaver>(),
GetExports<IExternalId>());

Resolve<ILiveTvManager>().AddParts(GetExports<ILiveTvService>(), GetExports<IListingsProvider>());

Resolve<IMediaSourceManager>().AddParts(GetExports<IMediaSourceProvider>());
}

Expand Down
7 changes: 0 additions & 7 deletions MediaBrowser.Controller/LiveTv/ILiveTvManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,6 @@ public interface ILiveTvManager
/// <returns>Task.</returns>
Task CancelSeriesTimer(string id);

/// <summary>
/// Adds the parts.
/// </summary>
/// <param name="services">The services.</param>
/// <param name="listingProviders">The listing providers.</param>
void AddParts(IEnumerable<ILiveTvService> services, IEnumerable<IListingsProvider> listingProviders);

/// <summary>
/// Gets the timer.
/// </summary>
Expand Down
29 changes: 19 additions & 10 deletions src/Jellyfin.LiveTv/EmbyTV/EmbyTV.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ public sealed class EmbyTV : ILiveTvService, ISupportsDirectStreamProvider, ISup
private readonly ItemDataProvider<SeriesTimerInfo> _seriesTimerProvider;
private readonly TimerManager _timerProvider;

private readonly LiveTvManager _liveTvManager;
private readonly ITunerHostManager _tunerHostManager;
private readonly IFileSystem _fileSystem;

Expand All @@ -61,6 +60,8 @@ public sealed class EmbyTV : ILiveTvService, ISupportsDirectStreamProvider, ISup
private readonly IMediaEncoder _mediaEncoder;
private readonly IMediaSourceManager _mediaSourceManager;
private readonly IStreamHelper _streamHelper;
private readonly LiveTvDtoService _tvDtoService;
private readonly IListingsProvider[] _listingsProviders;

private readonly ConcurrentDictionary<string, ActiveRecordingInfo> _activeRecordings =
new ConcurrentDictionary<string, ActiveRecordingInfo>(StringComparer.OrdinalIgnoreCase);
Expand All @@ -78,13 +79,14 @@ public sealed class EmbyTV : ILiveTvService, ISupportsDirectStreamProvider, ISup
ILogger<EmbyTV> logger,
IHttpClientFactory httpClientFactory,
IServerConfigurationManager config,
ILiveTvManager liveTvManager,
ITunerHostManager tunerHostManager,
IFileSystem fileSystem,
ILibraryManager libraryManager,
ILibraryMonitor libraryMonitor,
IProviderManager providerManager,
IMediaEncoder mediaEncoder)
IMediaEncoder mediaEncoder,
LiveTvDtoService tvDtoService,
IEnumerable<IListingsProvider> listingsProviders)
{
Current = this;

Expand All @@ -96,10 +98,11 @@ public sealed class EmbyTV : ILiveTvService, ISupportsDirectStreamProvider, ISup
_libraryMonitor = libraryMonitor;
_providerManager = providerManager;
_mediaEncoder = mediaEncoder;
_liveTvManager = (LiveTvManager)liveTvManager;
_tvDtoService = tvDtoService;
_tunerHostManager = tunerHostManager;
_mediaSourceManager = mediaSourceManager;
_streamHelper = streamHelper;
_listingsProviders = listingsProviders.ToArray();

_seriesTimerProvider = new SeriesTimerManager(_logger, Path.Combine(DataPath, "seriestimers.json"));
_timerProvider = new TimerManager(_logger, Path.Combine(DataPath, "timers.json"));
Expand Down Expand Up @@ -937,7 +940,7 @@ public async Task<IEnumerable<ProgramInfo>> GetProgramsAsync(string channelId, D
return _config.GetLiveTvConfiguration().ListingProviders
.Select(i =>
{
var provider = _liveTvManager.ListingProviders.FirstOrDefault(l => string.Equals(l.Type, i.Type, StringComparison.OrdinalIgnoreCase));
var provider = _listingsProviders.FirstOrDefault(l => string.Equals(l.Type, i.Type, StringComparison.OrdinalIgnoreCase));

return provider is null ? null : new Tuple<IListingsProvider, ListingsProviderInfo>(provider, i);
})
Expand Down Expand Up @@ -1181,6 +1184,12 @@ private string GetRecordingPath(TimerInfo timer, RemoteSearchResult metadata, ou
return Path.Combine(recordPath, recordingFileName);
}

private BaseItem GetLiveTvChannel(TimerInfo timer)
{
var internalChannelId = _tvDtoService.GetInternalChannelId(Name, timer.ChannelId);
return _libraryManager.GetItemById(internalChannelId);
}

private async Task RecordStream(TimerInfo timer, DateTime recordingEndDate, ActiveRecordingInfo activeRecordingInfo)
{
ArgumentNullException.ThrowIfNull(timer);
Expand All @@ -1206,7 +1215,7 @@ private async Task RecordStream(TimerInfo timer, DateTime recordingEndDate, Acti
var remoteMetadata = await FetchInternetMetadata(timer, CancellationToken.None).ConfigureAwait(false);
var recordPath = GetRecordingPath(timer, remoteMetadata, out string seriesPath);

var channelItem = _liveTvManager.GetLiveTvChannel(timer, this);
var channelItem = GetLiveTvChannel(timer);

string liveStreamId = null;
RecordingStatus recordingStatus;
Expand Down Expand Up @@ -2089,7 +2098,7 @@ private LiveTvProgram GetProgramInfoFromCache(string programId)
{
var query = new InternalItemsQuery
{
ItemIds = new[] { _liveTvManager.GetInternalProgramId(programId) },
ItemIds = [_tvDtoService.GetInternalProgramId(programId)],
Limit = 1,
DtoOptions = new DtoOptions()
};
Expand Down Expand Up @@ -2119,7 +2128,7 @@ private LiveTvProgram GetProgramInfoFromCache(string channelId, DateTime startDa

if (!string.IsNullOrWhiteSpace(channelId))
{
query.ChannelIds = new[] { _liveTvManager.GetInternalChannelId(Name, channelId) };
query.ChannelIds = [_tvDtoService.GetInternalChannelId(Name, channelId)];
}

return _libraryManager.GetItemList(query).Cast<LiveTvProgram>().FirstOrDefault();
Expand Down Expand Up @@ -2155,7 +2164,7 @@ private bool ShouldCancelTimerForSeriesTimer(SeriesTimerInfo seriesTimer, TimerI
private void HandleDuplicateShowIds(List<TimerInfo> timers)
{
// sort showings by HD channels first, then by startDate, record earliest showing possible
foreach (var timer in timers.OrderByDescending(t => _liveTvManager.GetLiveTvChannel(t, this).IsHD).ThenBy(t => t.StartDate).Skip(1))
foreach (var timer in timers.OrderByDescending(t => GetLiveTvChannel(t).IsHD).ThenBy(t => t.StartDate).Skip(1))
{
timer.Status = RecordingStatus.Cancelled;
_timerProvider.Update(timer);
Expand Down Expand Up @@ -2305,7 +2314,7 @@ private IEnumerable<TimerInfo> GetTimersForSeries(SeriesTimerInfo seriesTimer)

if (!seriesTimer.RecordAnyChannel)
{
query.ChannelIds = new[] { _liveTvManager.GetInternalChannelId(Name, seriesTimer.ChannelId) };
query.ChannelIds = [_tvDtoService.GetInternalChannelId(Name, seriesTimer.ChannelId)];
}

var tempChannelCache = new Dictionary<Guid, LiveTvChannel>();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Jellyfin.LiveTv.Channels;
using Jellyfin.LiveTv.Guide;
using Jellyfin.LiveTv.Listings;
using Jellyfin.LiveTv.TunerHosts;
using Jellyfin.LiveTv.TunerHosts.HdHomerun;
using MediaBrowser.Controller.Channels;
Expand Down Expand Up @@ -27,7 +28,10 @@ public static void AddLiveTvServices(this IServiceCollection services)
services.AddSingleton<ITunerHostManager, TunerHostManager>();
services.AddSingleton<IGuideManager, GuideManager>();

services.AddSingleton<ILiveTvService, EmbyTV.EmbyTV>();
services.AddSingleton<ITunerHost, HdHomerunHost>();
services.AddSingleton<ITunerHost, M3UTunerHost>();
services.AddSingleton<IListingsProvider, SchedulesDirect>();
services.AddSingleton<IListingsProvider, XmlTvListingsProvider>();
}
}
48 changes: 11 additions & 37 deletions src/Jellyfin.LiveTv/LiveTvManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,8 @@ public class LiveTvManager : ILiveTvManager
private readonly ILocalizationManager _localization;
private readonly IChannelManager _channelManager;
private readonly LiveTvDtoService _tvDtoService;

private ILiveTvService[] _services = Array.Empty<ILiveTvService>();
private IListingsProvider[] _listingProviders = Array.Empty<IListingsProvider>();
private readonly ILiveTvService[] _services;
private readonly IListingsProvider[] _listingProviders;

public LiveTvManager(
IServerConfigurationManager config,
Expand All @@ -61,7 +60,9 @@ public class LiveTvManager : ILiveTvManager
ITaskManager taskManager,
ILocalizationManager localization,
IChannelManager channelManager,
LiveTvDtoService liveTvDtoService)
LiveTvDtoService liveTvDtoService,
IEnumerable<ILiveTvService> services,
IEnumerable<IListingsProvider> listingProviders)
{
_config = config;
_logger = logger;
Expand All @@ -73,6 +74,12 @@ public class LiveTvManager : ILiveTvManager
_userDataManager = userDataManager;
_channelManager = channelManager;
_tvDtoService = liveTvDtoService;
_services = services.ToArray();
_listingProviders = listingProviders.ToArray();

var defaultService = _services.OfType<EmbyTV.EmbyTV>().First();
defaultService.TimerCreated += OnEmbyTvTimerCreated;
defaultService.TimerCancelled += OnEmbyTvTimerCancelled;
}

public event EventHandler<GenericEventArgs<TimerEventInfo>> SeriesTimerCancelled;
Expand All @@ -96,23 +103,6 @@ public string GetEmbyTvActiveRecordingPath(string id)
return EmbyTV.EmbyTV.Current.GetActiveRecordingPath(id);
}

/// <inheritdoc />
public void AddParts(IEnumerable<ILiveTvService> services, IEnumerable<IListingsProvider> listingProviders)
{
_services = services.ToArray();

_listingProviders = listingProviders.ToArray();

foreach (var service in _services)
{
if (service is EmbyTV.EmbyTV embyTv)
{
embyTv.TimerCreated += OnEmbyTvTimerCreated;
embyTv.TimerCancelled += OnEmbyTvTimerCancelled;
}
}
}

private void OnEmbyTvTimerCancelled(object sender, GenericEventArgs<string> e)
{
var timerId = e.Argument;
Expand Down Expand Up @@ -1165,12 +1155,6 @@ public async Task<QueryResult<SeriesTimerInfoDto>> GetSeriesTimers(SeriesTimerQu
return new QueryResult<SeriesTimerInfoDto>(returnArray);
}

public BaseItem GetLiveTvChannel(TimerInfo timer, ILiveTvService service)
{
var internalChannelId = _tvDtoService.GetInternalChannelId(service.Name, timer.ChannelId);
return _libraryManager.GetItemById(internalChannelId);
}

public void AddChannelInfo(IReadOnlyCollection<(BaseItemDto ItemDto, LiveTvChannel Channel)> items, DtoOptions options, User user)
{
var now = DateTime.UtcNow;
Expand Down Expand Up @@ -1636,16 +1620,6 @@ public Task<List<ChannelInfo>> GetChannelsFromListingsProviderData(string id, Ca
return provider.GetChannels(info, cancellationToken);
}

public Guid GetInternalChannelId(string serviceName, string externalId)
{
return _tvDtoService.GetInternalChannelId(serviceName, externalId);
}

public Guid GetInternalProgramId(string externalId)
{
return _tvDtoService.GetInternalProgramId(externalId);
}

/// <inheritdoc />
public Task<BaseItem[]> GetRecordingFoldersAsync(User user)
=> GetRecordingFoldersAsync(user, false);
Expand Down
Loading