Finaly working version of livestreams
This commit is contained in:
@@ -23,6 +23,7 @@ public class SRFMediaProvider : IMediaSourceProvider
|
||||
private readonly ILoggerFactory _loggerFactory;
|
||||
private readonly MetadataCache _metadataCache;
|
||||
private readonly StreamUrlResolver _streamResolver;
|
||||
private readonly StreamProxyService _proxyService;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="SRFMediaProvider"/> class.
|
||||
@@ -30,15 +31,18 @@ public class SRFMediaProvider : IMediaSourceProvider
|
||||
/// <param name="loggerFactory">The logger factory.</param>
|
||||
/// <param name="metadataCache">The metadata cache.</param>
|
||||
/// <param name="streamResolver">The stream URL resolver.</param>
|
||||
/// <param name="proxyService">The stream proxy service.</param>
|
||||
public SRFMediaProvider(
|
||||
ILoggerFactory loggerFactory,
|
||||
MetadataCache metadataCache,
|
||||
StreamUrlResolver streamResolver)
|
||||
StreamUrlResolver streamResolver,
|
||||
StreamProxyService proxyService)
|
||||
{
|
||||
_loggerFactory = loggerFactory;
|
||||
_logger = loggerFactory.CreateLogger<SRFMediaProvider>();
|
||||
_metadataCache = metadataCache;
|
||||
_streamResolver = streamResolver;
|
||||
_proxyService = proxyService;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -52,24 +56,35 @@ public class SRFMediaProvider : IMediaSourceProvider
|
||||
/// <param name="item">The item.</param>
|
||||
/// <param name="cancellationToken">The cancellation token.</param>
|
||||
/// <returns>List of media sources.</returns>
|
||||
public Task<IEnumerable<MediaSourceInfo>> GetMediaSources(BaseItem item, CancellationToken cancellationToken)
|
||||
public async Task<IEnumerable<MediaSourceInfo>> GetMediaSources(BaseItem item, CancellationToken cancellationToken)
|
||||
{
|
||||
var sources = new List<MediaSourceInfo>();
|
||||
|
||||
try
|
||||
{
|
||||
// Log detailed information about the request
|
||||
var stackTrace = new System.Diagnostics.StackTrace(true);
|
||||
var callingMethod = stackTrace.GetFrame(1)?.GetMethod();
|
||||
_logger.LogInformation(
|
||||
"GetMediaSources called - Item: {ItemName}, Type: {ItemType}, Id: {ItemId}, CalledBy: {CallingMethod}",
|
||||
item.Name,
|
||||
item.GetType().Name,
|
||||
item.Id,
|
||||
callingMethod?.DeclaringType?.Name + "." + callingMethod?.Name);
|
||||
|
||||
// Check if this is an SRF item
|
||||
if (!item.ProviderIds.TryGetValue("SRF", out var urn) || string.IsNullOrEmpty(urn))
|
||||
{
|
||||
return Task.FromResult<IEnumerable<MediaSourceInfo>>(sources);
|
||||
_logger.LogDebug("Item {ItemName} is not an SRF item, returning empty sources", item.Name);
|
||||
return sources;
|
||||
}
|
||||
|
||||
_logger.LogDebug("Getting media sources for URN: {Urn}", urn);
|
||||
_logger.LogInformation("Getting media sources for URN: {Urn}, Item: {ItemName}", urn, item.Name);
|
||||
|
||||
var config = Plugin.Instance?.Configuration;
|
||||
if (config == null)
|
||||
{
|
||||
return Task.FromResult<IEnumerable<MediaSourceInfo>>(sources);
|
||||
return sources;
|
||||
}
|
||||
|
||||
// For scheduled livestreams, use shorter cache TTL (5 minutes) so they refresh when they go live
|
||||
@@ -85,7 +100,7 @@ public class SRFMediaProvider : IMediaSourceProvider
|
||||
if (mediaComposition == null)
|
||||
{
|
||||
using var apiClient = new SRFApiClient(_loggerFactory);
|
||||
mediaComposition = apiClient.GetMediaCompositionByUrnAsync(urn, cancellationToken).GetAwaiter().GetResult();
|
||||
mediaComposition = await apiClient.GetMediaCompositionByUrnAsync(urn, cancellationToken).ConfigureAwait(false);
|
||||
|
||||
if (mediaComposition != null)
|
||||
{
|
||||
@@ -96,7 +111,7 @@ public class SRFMediaProvider : IMediaSourceProvider
|
||||
if (mediaComposition?.ChapterList == null || mediaComposition.ChapterList.Count == 0)
|
||||
{
|
||||
_logger.LogWarning("No chapters found for URN: {Urn}", urn);
|
||||
return Task.FromResult<IEnumerable<MediaSourceInfo>>(sources);
|
||||
return sources;
|
||||
}
|
||||
|
||||
// Get the first chapter (main video)
|
||||
@@ -106,14 +121,14 @@ public class SRFMediaProvider : IMediaSourceProvider
|
||||
if (_streamResolver.IsContentExpired(chapter))
|
||||
{
|
||||
_logger.LogWarning("Content expired for URN: {Urn}, ValidTo: {ValidTo}", urn, chapter.ValidTo);
|
||||
return Task.FromResult<IEnumerable<MediaSourceInfo>>(sources);
|
||||
return sources;
|
||||
}
|
||||
|
||||
// Check if content has playable streams
|
||||
if (!_streamResolver.HasPlayableContent(chapter))
|
||||
{
|
||||
_logger.LogWarning("No playable content found for URN: {Urn}", urn);
|
||||
return Task.FromResult<IEnumerable<MediaSourceInfo>>(sources);
|
||||
return sources;
|
||||
}
|
||||
|
||||
// Get stream URL based on quality preference
|
||||
@@ -125,7 +140,7 @@ public class SRFMediaProvider : IMediaSourceProvider
|
||||
_logger.LogDebug("URN {Urn}: Scheduled livestream has no stream URL, fetching fresh data", urn);
|
||||
|
||||
using var freshApiClient = new SRFApiClient(_loggerFactory);
|
||||
var freshMediaComposition = freshApiClient.GetMediaCompositionByUrnAsync(urn, cancellationToken).GetAwaiter().GetResult();
|
||||
var freshMediaComposition = await freshApiClient.GetMediaCompositionByUrnAsync(urn, cancellationToken).ConfigureAwait(false);
|
||||
|
||||
if (freshMediaComposition?.ChapterList != null && freshMediaComposition.ChapterList.Count > 0)
|
||||
{
|
||||
@@ -145,58 +160,91 @@ public class SRFMediaProvider : IMediaSourceProvider
|
||||
if (string.IsNullOrEmpty(streamUrl))
|
||||
{
|
||||
_logger.LogWarning("Could not resolve stream URL for URN: {Urn}", urn);
|
||||
return Task.FromResult<IEnumerable<MediaSourceInfo>>(sources);
|
||||
return sources;
|
||||
}
|
||||
|
||||
// Authenticate the stream URL (required for all SRF streams, especially livestreams)
|
||||
if (!string.IsNullOrEmpty(streamUrl))
|
||||
{
|
||||
streamUrl = _streamResolver.GetAuthenticatedStreamUrlAsync(streamUrl, cancellationToken).GetAwaiter().GetResult();
|
||||
streamUrl = await _streamResolver.GetAuthenticatedStreamUrlAsync(streamUrl, cancellationToken).ConfigureAwait(false);
|
||||
_logger.LogDebug("Authenticated stream URL for URN: {Urn}", urn);
|
||||
}
|
||||
|
||||
// Create media source
|
||||
// Register stream with proxy service
|
||||
var itemIdStr = item.Id.ToString("N"); // Use hex format without dashes
|
||||
_proxyService.RegisterStream(itemIdStr, streamUrl);
|
||||
|
||||
// Create proxy URL as absolute HTTP URL (required for ffmpeg)
|
||||
// Use localhost as Jellyfin should be able to access its own endpoints
|
||||
var proxyUrl = $"http://localhost:8096/Plugins/SRFPlay/Proxy/{itemIdStr}/master.m3u8";
|
||||
|
||||
_logger.LogInformation("Using proxy URL for item {ItemId}: {ProxyUrl}", itemIdStr, proxyUrl);
|
||||
|
||||
// Create media source using proxy URL - enables DirectPlay!
|
||||
var mediaSource = new MediaSourceInfo
|
||||
{
|
||||
Id = item.Id.ToString(), // Use item GUID, not URN string (required for transcoding)
|
||||
Name = chapter.Title,
|
||||
Path = streamUrl,
|
||||
Path = proxyUrl, // Proxy URL instead of direct Akamai URL
|
||||
Protocol = MediaProtocol.Http,
|
||||
Container = "m3u8",
|
||||
Container = "hls",
|
||||
SupportsDirectStream = true,
|
||||
SupportsDirectPlay = false, // Disabled: auth tokens don't carry over to HLS segments in browser
|
||||
SupportsDirectPlay = true, // ✅ Enabled! Proxy handles auth
|
||||
SupportsTranscoding = true,
|
||||
IsRemote = true,
|
||||
IsRemote = false, // False because it's a local proxy endpoint
|
||||
Type = MediaSourceType.Default,
|
||||
RunTimeTicks = chapter.Duration > 0 ? TimeSpan.FromMilliseconds(chapter.Duration).Ticks : null,
|
||||
VideoType = VideoType.VideoFile,
|
||||
IsInfiniteStream = false,
|
||||
RequiresOpening = false,
|
||||
RequiresClosing = false,
|
||||
SupportsProbing = true
|
||||
};
|
||||
|
||||
// Add video stream info
|
||||
mediaSource.MediaStreams = new List<MediaStream>
|
||||
{
|
||||
new MediaStream
|
||||
SupportsProbing = false, // Disable probing for proxy URLs
|
||||
ReadAtNativeFramerate = false,
|
||||
MediaStreams = new List<MediaBrowser.Model.Entities.MediaStream>
|
||||
{
|
||||
Type = MediaStreamType.Video,
|
||||
Codec = "h264",
|
||||
IsInterlaced = false,
|
||||
IsDefault = true
|
||||
new MediaBrowser.Model.Entities.MediaStream
|
||||
{
|
||||
Type = MediaStreamType.Video,
|
||||
Codec = "h264",
|
||||
Profile = "high",
|
||||
IsInterlaced = false,
|
||||
IsDefault = true,
|
||||
Index = 0
|
||||
},
|
||||
new MediaBrowser.Model.Entities.MediaStream
|
||||
{
|
||||
Type = MediaStreamType.Audio,
|
||||
Codec = "aac",
|
||||
IsDefault = true,
|
||||
Index = 1
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
sources.Add(mediaSource);
|
||||
_logger.LogInformation("Resolved stream URL for {Title}: {Url}", chapter.Title, streamUrl);
|
||||
_logger.LogInformation(
|
||||
"MediaSource created - Id={Id}, DirectStream={DirectStream}, DirectPlay={DirectPlay}, Probing={Probing}, Container={Container}, Protocol={Protocol}, IsRemote={IsRemote}",
|
||||
mediaSource.Id,
|
||||
mediaSource.SupportsDirectStream,
|
||||
mediaSource.SupportsDirectPlay,
|
||||
mediaSource.SupportsProbing,
|
||||
mediaSource.Container,
|
||||
mediaSource.Protocol,
|
||||
mediaSource.IsRemote);
|
||||
_logger.LogInformation(
|
||||
"MediaSource capabilities - SupportsTranscoding={Transcoding}, RequiresOpening={RequiresOpening}, RequiresClosing={RequiresClosing}, Type={Type}",
|
||||
mediaSource.SupportsTranscoding,
|
||||
mediaSource.RequiresOpening,
|
||||
mediaSource.RequiresClosing,
|
||||
mediaSource.Type);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error getting media sources for item: {Name}", item.Name);
|
||||
}
|
||||
|
||||
return Task.FromResult<IEnumerable<MediaSourceInfo>>(sources);
|
||||
return sources;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -207,6 +255,7 @@ public class SRFMediaProvider : IMediaSourceProvider
|
||||
/// <returns>The direct stream provider.</returns>
|
||||
public Task<IDirectStreamProvider?> GetDirectStreamProviderByUniqueId(string uniqueId, CancellationToken cancellationToken)
|
||||
{
|
||||
_logger.LogInformation("GetDirectStreamProviderByUniqueId called with uniqueId: {UniqueId}", uniqueId);
|
||||
// Not needed for HTTP streams
|
||||
return Task.FromResult<IDirectStreamProvider?>(null);
|
||||
}
|
||||
@@ -214,6 +263,7 @@ public class SRFMediaProvider : IMediaSourceProvider
|
||||
/// <inheritdoc />
|
||||
public Task<ILiveStream> OpenMediaSource(string openToken, List<ILiveStream> currentLiveStreams, CancellationToken cancellationToken)
|
||||
{
|
||||
_logger.LogWarning("OpenMediaSource called with openToken: {OpenToken} - This should not be called for HTTP streams!", openToken);
|
||||
// Not needed for static HTTP streams
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user