refactor to unify data fetching and define abstract API for re-use
This commit is contained in:
@@ -4,8 +4,8 @@ using System.IO;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Jellyfin.Plugin.SRFPlay.Api;
|
||||
using Jellyfin.Plugin.SRFPlay.Services;
|
||||
using Jellyfin.Plugin.SRFPlay.Configuration;
|
||||
using Jellyfin.Plugin.SRFPlay.Services.Interfaces;
|
||||
using MediaBrowser.Controller;
|
||||
using MediaBrowser.Controller.Entities;
|
||||
using MediaBrowser.Controller.Library;
|
||||
@@ -23,9 +23,9 @@ public class SRFMediaProvider : IMediaSourceProvider
|
||||
{
|
||||
private readonly ILogger<SRFMediaProvider> _logger;
|
||||
private readonly ILoggerFactory _loggerFactory;
|
||||
private readonly MetadataCache _metadataCache;
|
||||
private readonly StreamUrlResolver _streamResolver;
|
||||
private readonly StreamProxyService _proxyService;
|
||||
private readonly IMediaCompositionFetcher _compositionFetcher;
|
||||
private readonly IStreamUrlResolver _streamResolver;
|
||||
private readonly IStreamProxyService _proxyService;
|
||||
private readonly IServerApplicationHost _appHost;
|
||||
private readonly Dictionary<string, string> _openTokenToItemId = new();
|
||||
|
||||
@@ -33,20 +33,20 @@ public class SRFMediaProvider : IMediaSourceProvider
|
||||
/// Initializes a new instance of the <see cref="SRFMediaProvider"/> class.
|
||||
/// </summary>
|
||||
/// <param name="loggerFactory">The logger factory.</param>
|
||||
/// <param name="metadataCache">The metadata cache.</param>
|
||||
/// <param name="compositionFetcher">The media composition fetcher.</param>
|
||||
/// <param name="streamResolver">The stream URL resolver.</param>
|
||||
/// <param name="proxyService">The stream proxy service.</param>
|
||||
/// <param name="appHost">The server application host.</param>
|
||||
public SRFMediaProvider(
|
||||
ILoggerFactory loggerFactory,
|
||||
MetadataCache metadataCache,
|
||||
StreamUrlResolver streamResolver,
|
||||
StreamProxyService proxyService,
|
||||
IMediaCompositionFetcher compositionFetcher,
|
||||
IStreamUrlResolver streamResolver,
|
||||
IStreamProxyService proxyService,
|
||||
IServerApplicationHost appHost)
|
||||
{
|
||||
_loggerFactory = loggerFactory;
|
||||
_logger = loggerFactory.CreateLogger<SRFMediaProvider>();
|
||||
_metadataCache = metadataCache;
|
||||
_compositionFetcher = compositionFetcher;
|
||||
_streamResolver = streamResolver;
|
||||
_proxyService = proxyService;
|
||||
_appHost = appHost;
|
||||
@@ -88,32 +88,10 @@ public class SRFMediaProvider : IMediaSourceProvider
|
||||
|
||||
_logger.LogInformation("Getting media sources for URN: {Urn}, Item: {ItemName}", urn, item.Name);
|
||||
|
||||
var config = Plugin.Instance?.Configuration;
|
||||
if (config == null)
|
||||
{
|
||||
return sources;
|
||||
}
|
||||
|
||||
// For scheduled livestreams, use shorter cache TTL (5 minutes) so they refresh when they go live
|
||||
// For regular content, use configured cache duration
|
||||
var cacheDuration = urn.Contains("scheduled_livestream", StringComparison.OrdinalIgnoreCase)
|
||||
? 5
|
||||
: config.CacheDurationMinutes;
|
||||
var cacheDuration = urn.Contains("scheduled_livestream", StringComparison.OrdinalIgnoreCase) ? 5 : (int?)null;
|
||||
|
||||
// Try cache first
|
||||
var mediaComposition = _metadataCache.GetMediaComposition(urn, cacheDuration);
|
||||
|
||||
// If not in cache, fetch from API
|
||||
if (mediaComposition == null)
|
||||
{
|
||||
using var apiClient = new SRFApiClient(_loggerFactory);
|
||||
mediaComposition = await apiClient.GetMediaCompositionByUrnAsync(urn, cancellationToken).ConfigureAwait(false);
|
||||
|
||||
if (mediaComposition != null)
|
||||
{
|
||||
_metadataCache.SetMediaComposition(urn, mediaComposition);
|
||||
}
|
||||
}
|
||||
var mediaComposition = await _compositionFetcher.GetMediaCompositionAsync(urn, cancellationToken, cacheDuration).ConfigureAwait(false);
|
||||
|
||||
if (mediaComposition?.ChapterList == null || mediaComposition.ChapterList.Count == 0)
|
||||
{
|
||||
@@ -139,25 +117,25 @@ public class SRFMediaProvider : IMediaSourceProvider
|
||||
}
|
||||
|
||||
// Get stream URL based on quality preference
|
||||
var streamUrl = _streamResolver.GetStreamUrl(chapter, config.QualityPreference);
|
||||
var config = Plugin.Instance?.Configuration;
|
||||
var qualityPref = config?.QualityPreference ?? QualityPreference.HD;
|
||||
var streamUrl = _streamResolver.GetStreamUrl(chapter, qualityPref);
|
||||
|
||||
// For scheduled livestreams, always fetch fresh data to ensure stream URL is current
|
||||
if (chapter.Type == "SCHEDULED_LIVESTREAM" && string.IsNullOrEmpty(streamUrl))
|
||||
{
|
||||
_logger.LogDebug("URN {Urn}: Scheduled livestream has no stream URL, fetching fresh data", urn);
|
||||
|
||||
using var freshApiClient = new SRFApiClient(_loggerFactory);
|
||||
var freshMediaComposition = await freshApiClient.GetMediaCompositionByUrnAsync(urn, cancellationToken).ConfigureAwait(false);
|
||||
// Force fresh fetch with short cache duration
|
||||
var freshMediaComposition = await _compositionFetcher.GetMediaCompositionAsync(urn, cancellationToken, 1).ConfigureAwait(false);
|
||||
|
||||
if (freshMediaComposition?.ChapterList != null && freshMediaComposition.ChapterList.Count > 0)
|
||||
{
|
||||
var freshChapter = freshMediaComposition.ChapterList[0];
|
||||
streamUrl = _streamResolver.GetStreamUrl(freshChapter, config.QualityPreference);
|
||||
streamUrl = _streamResolver.GetStreamUrl(freshChapter, qualityPref);
|
||||
|
||||
if (!string.IsNullOrEmpty(streamUrl))
|
||||
{
|
||||
// Update cache with fresh data
|
||||
_metadataCache.SetMediaComposition(urn, freshMediaComposition);
|
||||
chapter = freshChapter;
|
||||
_logger.LogInformation("URN {Urn}: Got fresh stream URL for scheduled livestream", urn);
|
||||
}
|
||||
@@ -190,7 +168,7 @@ public class SRFMediaProvider : IMediaSourceProvider
|
||||
_proxyService.RegisterStream(itemIdStr, streamUrl, urn, isLiveStream);
|
||||
|
||||
// Get the server URL for proxy - prefer configured public URL for remote clients
|
||||
var serverUrl = !string.IsNullOrWhiteSpace(config.PublicServerUrl)
|
||||
var serverUrl = config != null && !string.IsNullOrWhiteSpace(config.PublicServerUrl)
|
||||
? config.PublicServerUrl.TrimEnd('/') // Use configured public URL (important for Android/remote clients)
|
||||
: _appHost.GetSmartApiUrl(string.Empty); // Fall back to Jellyfin's smart URL resolution
|
||||
|
||||
@@ -207,7 +185,7 @@ public class SRFMediaProvider : IMediaSourceProvider
|
||||
"Using proxy URL for item {ItemId}: {ProxyUrl} (PublicServerUrl configured: {IsPublicConfigured})",
|
||||
itemIdStr,
|
||||
proxyUrl,
|
||||
!string.IsNullOrWhiteSpace(config.PublicServerUrl));
|
||||
config != null && !string.IsNullOrWhiteSpace(config.PublicServerUrl));
|
||||
|
||||
// Create media source using proxy URL - enables DirectPlay!
|
||||
var mediaSource = new MediaSourceInfo
|
||||
@@ -311,8 +289,7 @@ public class SRFMediaProvider : IMediaSourceProvider
|
||||
_logger,
|
||||
_proxyService,
|
||||
originalItemId,
|
||||
openToken,
|
||||
_loggerFactory);
|
||||
openToken);
|
||||
|
||||
return await Task.FromResult<ILiveStream>(liveStream).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user