diff --git a/Jellyfin.Plugin.SRFPlay/Api/SRFApiClient.cs b/Jellyfin.Plugin.SRFPlay/Api/SRFApiClient.cs index c638542..23358bc 100644 --- a/Jellyfin.Plugin.SRFPlay/Api/SRFApiClient.cs +++ b/Jellyfin.Plugin.SRFPlay/Api/SRFApiClient.cs @@ -140,6 +140,11 @@ public class SRFApiClient : IDisposable var fullUrl = $"{BaseUrl}{url}"; _logger.LogInformation("Fetching media composition for URN: {Urn} from {Url}", urn, fullUrl); + // HttpClient consistently fails with 404, use curl directly + // This is likely due to routing/network configuration on the Jellyfin server + return await FetchWithCurlAsync(fullUrl, cancellationToken).ConfigureAwait(false); + + /* HttpClient fallback disabled - always returns 404 var response = await _httpClient.GetAsync(url, cancellationToken).ConfigureAwait(false); // Log response headers to diagnose geo-blocking @@ -178,17 +183,7 @@ public class SRFApiClient : IDisposable } return result; - } - catch (HttpRequestException ex) - { - _logger.LogError( - ex, - "HTTP error fetching media composition for URN: {Urn} - StatusCode: {StatusCode}, trying curl fallback", - urn, - ex.StatusCode); - - var fullUrl = $"{BaseUrl}/mediaComposition/byUrn/{urn}.json"; - return await FetchWithCurlAsync(fullUrl, cancellationToken).ConfigureAwait(false); + */ } catch (Exception ex) { diff --git a/Jellyfin.Plugin.SRFPlay/Controllers/StreamProxyController.cs b/Jellyfin.Plugin.SRFPlay/Controllers/StreamProxyController.cs index 2e06f2d..d0ddb2a 100644 --- a/Jellyfin.Plugin.SRFPlay/Controllers/StreamProxyController.cs +++ b/Jellyfin.Plugin.SRFPlay/Controllers/StreamProxyController.cs @@ -45,27 +45,42 @@ public class StreamProxyController : ControllerBase [FromRoute] string itemId, CancellationToken cancellationToken) { - _logger.LogInformation("Proxy request for master manifest - ItemId: {ItemId}", itemId); + _logger.LogInformation("Proxy request for master manifest - Path ItemId: {PathItemId}, Query params: {QueryString}", itemId, Request.QueryString); // Try to resolve the actual item ID (path ID might be a session ID during transcoding) var actualItemId = ResolveItemId(itemId); try { - // Build the base proxy URL for this item (use original itemId from path to maintain URL structure) - // Always include the actualItemId as a query parameter to ensure proper resolution during transcoding - var baseProxyUrl = $"{Request.Scheme}://{Request.Host}/Plugins/SRFPlay/Proxy/{itemId}?itemId={actualItemId}"; + // Get the correct scheme (https if configured, otherwise use request scheme) + var scheme = GetProxyScheme(); - if (actualItemId != itemId) + // Build the base proxy URL for this item + // Preserve query parameters (token or itemId) from the original request + string baseProxyUrl; + if (Request.Query.TryGetValue("token", out var token) && !string.IsNullOrEmpty(token)) { - _logger.LogDebug("Path itemId {PathId} differs from resolved itemId {ResolvedId}, adding query parameter", itemId, actualItemId); + baseProxyUrl = $"{scheme}://{Request.Host}/Plugins/SRFPlay/Proxy/{itemId}?token={token}"; + _logger.LogDebug("Using token-based proxy URL with token: {Token}", token.ToString()); + } + else if (actualItemId != itemId) + { + // Legacy: If path ID differs from resolved ID, add itemId query parameter + baseProxyUrl = $"{scheme}://{Request.Host}/Plugins/SRFPlay/Proxy/{itemId}?itemId={actualItemId}"; + _logger.LogInformation("Path itemId {PathId} differs from resolved itemId {ResolvedId}, adding query parameter", itemId, actualItemId); + } + else + { + // Simple case: no query parameters needed + baseProxyUrl = $"{scheme}://{Request.Host}/Plugins/SRFPlay/Proxy/{itemId}"; + _logger.LogDebug("Path itemId matches resolved itemId: {ItemId}", itemId); } var manifestContent = await _proxyService.GetRewrittenManifestAsync(actualItemId, baseProxyUrl, cancellationToken).ConfigureAwait(false); if (manifestContent == null) { - _logger.LogWarning("Manifest not found for item {ItemId}", itemId); + _logger.LogWarning("Manifest not found for path itemId {PathItemId}, resolved itemId {ResolvedItemId} - stream may not be registered", itemId, actualItemId); return NotFound(); } @@ -114,7 +129,8 @@ public class StreamProxyController : ControllerBase // Convert to string and rewrite URLs var manifestContent = System.Text.Encoding.UTF8.GetString(manifestData); - var baseProxyUrl = $"{Request.Scheme}://{Request.Host}/Plugins/SRFPlay/Proxy/{itemId}"; + var scheme = GetProxyScheme(); + var baseProxyUrl = $"{scheme}://{Request.Host}/Plugins/SRFPlay/Proxy/{itemId}"; var rewrittenContent = RewriteSegmentUrls(manifestContent, baseProxyUrl); _logger.LogDebug("Returning variant manifest for item {ItemId} ({Length} bytes)", itemId, rewrittenContent.Length); @@ -171,6 +187,31 @@ public class StreamProxyController : ControllerBase } } + /// + /// Gets the correct scheme for proxy URLs (https if public URL is configured with https). + /// + /// The scheme to use (http or https). + private string GetProxyScheme() + { + // Check if PublicServerUrl is configured and uses HTTPS + var config = Plugin.Instance?.Configuration; + if (config != null && !string.IsNullOrWhiteSpace(config.PublicServerUrl)) + { + if (config.PublicServerUrl.StartsWith("https://", StringComparison.OrdinalIgnoreCase)) + { + return "https"; + } + } + + // Fall back to request scheme, but prefer https if forwarded headers indicate it + if (Request.Headers.TryGetValue("X-Forwarded-Proto", out var forwardedProto)) + { + return forwardedProto.ToString().ToLowerInvariant(); + } + + return Request.Scheme; + } + /// /// Resolves the actual item ID from the request. /// @@ -178,16 +219,24 @@ public class StreamProxyController : ControllerBase /// The resolved item ID. private string ResolveItemId(string pathItemId) { - // Check if there's an itemId query parameter (fallback for transcoding sessions) + // Check for token parameter first (preferred method) + if (Request.Query.TryGetValue("token", out var token) && !string.IsNullOrEmpty(token)) + { + // Try to resolve the original item ID from the token via the proxy service + // We'll need to add a method to StreamProxyService to look up by token + _logger.LogInformation("Found token parameter: {Token}, will use path ID {PathItemId} for lookup", token.ToString(), pathItemId); + return pathItemId; // Use path ID for now; token prevents Jellyfin from rewriting the URL + } + + // Check if there's an itemId query parameter (legacy fallback) if (Request.Query.TryGetValue("itemId", out var queryItemId) && !string.IsNullOrEmpty(queryItemId)) { - _logger.LogDebug("Using itemId from query parameter: {QueryItemId} (path had: {PathItemId})", queryItemId.ToString(), pathItemId); + _logger.LogInformation("Using itemId from query parameter: {QueryItemId} (path had: {PathItemId})", queryItemId.ToString(), pathItemId); return queryItemId.ToString(); } - // If path ID and query ID don't match, it's likely a transcoding session - // Try to use the proxy service fallback to find the correct stream - _logger.LogDebug("No itemId query parameter found, using path ID as-is: {PathItemId}", pathItemId); + // No query parameters - use path ID as-is (TRANSCODING SESSION ID CASE) + _logger.LogWarning("⚠️ No query parameters found, using path ID as-is: {PathItemId} (likely transcoding session ID)", pathItemId); return pathItemId; } @@ -199,10 +248,20 @@ public class StreamProxyController : ControllerBase /// The rewritten manifest. private string RewriteSegmentUrls(string manifestContent, string baseProxyUrl) { - // Extract the itemId query parameter from the current request to propagate it - var itemIdParam = Request.Query.TryGetValue("itemId", out var itemId) && !string.IsNullOrEmpty(itemId) - ? $"?itemId={itemId}" - : string.Empty; + // Extract query parameters from the current request to propagate them + string queryParams; + if (Request.Query.TryGetValue("token", out var token) && !string.IsNullOrEmpty(token)) + { + queryParams = $"?token={token}"; + } + else if (Request.Query.TryGetValue("itemId", out var itemId) && !string.IsNullOrEmpty(itemId)) + { + queryParams = $"?itemId={itemId}"; + } + else + { + queryParams = string.Empty; + } var lines = manifestContent.Split('\n'); var result = new System.Text.StringBuilder(); @@ -220,12 +279,12 @@ public class StreamProxyController : ControllerBase var uri = new Uri(line.Trim()); var segments = uri.AbsolutePath.Split('/'); var fileName = segments[^1]; - result.AppendLine(CultureInfo.InvariantCulture, $"{baseProxyUrl}/{fileName}{itemIdParam}"); + result.AppendLine(CultureInfo.InvariantCulture, $"{baseProxyUrl}/{fileName}{queryParams}"); } else { // Relative URL - rewrite to proxy - result.AppendLine(CultureInfo.InvariantCulture, $"{baseProxyUrl}/{line.Trim()}{itemIdParam}"); + result.AppendLine(CultureInfo.InvariantCulture, $"{baseProxyUrl}/{line.Trim()}{queryParams}"); } } diff --git a/Jellyfin.Plugin.SRFPlay/Providers/SRFLiveStream.cs b/Jellyfin.Plugin.SRFPlay/Providers/SRFLiveStream.cs new file mode 100644 index 0000000..ffe2ff5 --- /dev/null +++ b/Jellyfin.Plugin.SRFPlay/Providers/SRFLiveStream.cs @@ -0,0 +1,134 @@ +using System; +using System.IO; +using System.Threading; +using System.Threading.Tasks; +using Jellyfin.Plugin.SRFPlay.Services; +using MediaBrowser.Controller.Library; +using MediaBrowser.Model.Dto; +using Microsoft.Extensions.Logging; + +namespace Jellyfin.Plugin.SRFPlay.Providers; + +/// +/// Live stream wrapper for SRF Play streams to handle transcoding sessions. +/// +internal sealed class SRFLiveStream : ILiveStream +{ + private readonly ILogger _logger; + private readonly StreamProxyService _proxyService; + private readonly string _originalItemId; + private MediaSourceInfo? _mediaSource; + + /// + /// Initializes a new instance of the class. + /// + /// The logger. + /// The stream proxy service. + /// The original item ID. + /// The open token. + /// The logger factory. + public SRFLiveStream( + ILogger logger, + StreamProxyService proxyService, + string originalItemId, + string openToken, + ILoggerFactory loggerFactory) + { + _logger = logger; + _proxyService = proxyService; + _originalItemId = originalItemId; + OriginalStreamId = openToken; + UniqueId = openToken; + } + + /// + public int ConsumerCount { get; set; } + + /// + public string OriginalStreamId { get; set; } + + /// + public string UniqueId { get; } + + /// + public string TunerHostId => string.Empty; + + /// + public bool EnableStreamSharing => false; + + /// + public MediaSourceInfo MediaSource + { + get => _mediaSource ?? throw new InvalidOperationException("MediaSource not set"); + set + { + _mediaSource = value; + _logger.LogInformation( + "SRFLiveStream MediaSource set - Id: {MediaSourceId}, Path: {Path}, OriginalItemId: {OriginalItemId}", + value.Id, + value.Path, + _originalItemId); + + // When Jellyfin assigns a live stream ID (for transcoding), register the stream with that ID too + if (value.Id != _originalItemId) + { + _logger.LogInformation( + "Transcoding session detected - LiveStream ID {LiveStreamId} differs from original item ID {OriginalItemId}. Registering stream with both IDs.", + value.Id, + _originalItemId); + + // Get the authenticated URL from the original registration + var authenticatedUrl = _proxyService.GetAuthenticatedUrl(_originalItemId); + if (authenticatedUrl != null) + { + // Register the same stream URL with the transcoding session ID + _proxyService.RegisterStream(value.Id, authenticatedUrl); + _logger.LogInformation("Registered stream for transcoding session ID: {LiveStreamId}", value.Id); + } + else + { + _logger.LogWarning("Could not find authenticated URL for original item {OriginalItemId}", _originalItemId); + } + } + } + } + + /// + public Task Close() + { + _logger.LogInformation("Closing SRF live stream for item {OriginalItemId}", _originalItemId); + return Task.CompletedTask; + } + + /// + public Task Open(CancellationToken cancellationToken) + { + _logger.LogInformation("Opening SRF live stream for item {OriginalItemId}", _originalItemId); + return Task.CompletedTask; + } + + /// + public Stream GetStream() + { + throw new NotSupportedException("Direct stream access not supported for SRF streams"); + } + + /// + public void Dispose() + { + Dispose(true); + GC.SuppressFinalize(this); + } + + /// + /// Releases the unmanaged resources used by the SRFLiveStream and optionally releases the managed resources. + /// + /// True to release both managed and unmanaged resources; false to release only unmanaged resources. + private void Dispose(bool disposing) + { + if (disposing) + { + _logger.LogDebug("Disposing SRF live stream for item {OriginalItemId}", _originalItemId); + } + } +} diff --git a/Jellyfin.Plugin.SRFPlay/Providers/SRFMediaProvider.cs b/Jellyfin.Plugin.SRFPlay/Providers/SRFMediaProvider.cs index dd18ced..6136a70 100644 --- a/Jellyfin.Plugin.SRFPlay/Providers/SRFMediaProvider.cs +++ b/Jellyfin.Plugin.SRFPlay/Providers/SRFMediaProvider.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.IO; using System.Linq; using System.Threading; using System.Threading.Tasks; @@ -26,6 +27,7 @@ public class SRFMediaProvider : IMediaSourceProvider private readonly StreamUrlResolver _streamResolver; private readonly StreamProxyService _proxyService; private readonly IServerApplicationHost _appHost; + private readonly Dictionary _openTokenToItemId = new(); /// /// Initializes a new instance of the class. @@ -184,10 +186,17 @@ public class SRFMediaProvider : IMediaSourceProvider ? config.PublicServerUrl.TrimEnd('/') // Use configured public URL (important for Android/remote clients) : _appHost.GetSmartApiUrl(string.Empty); // Fall back to Jellyfin's smart URL resolution - // Create proxy URL as absolute HTTP URL (required for ffmpeg) - // Use the actual server URL so remote clients can access it - // Include item ID as query parameter to preserve it during transcoding - var proxyUrl = $"{serverUrl}/Plugins/SRFPlay/Proxy/{itemIdStr}/master.m3u8?itemId={itemIdStr}"; + // Detect if this is a live stream + var isLiveStream = chapter.Type == "SCHEDULED_LIVESTREAM" || urn.Contains("livestream", StringComparison.OrdinalIgnoreCase); + + // Generate an open token for this media source (used to track transcoding sessions) + var openToken = Guid.NewGuid().ToString("N"); + _openTokenToItemId[openToken] = itemIdStr; + _logger.LogDebug("Created open token {OpenToken} for item {ItemId}", openToken, itemIdStr); + + // Create proxy URL using token instead of item ID in path + // This prevents Jellyfin from rewriting the URL during transcoding + var proxyUrl = $"{serverUrl}/Plugins/SRFPlay/Proxy/{itemIdStr}/master.m3u8?token={openToken}"; _logger.LogInformation( "Using proxy URL for item {ItemId}: {ProxyUrl} (PublicServerUrl configured: {IsPublicConfigured})", @@ -195,9 +204,6 @@ public class SRFMediaProvider : IMediaSourceProvider proxyUrl, !string.IsNullOrWhiteSpace(config.PublicServerUrl)); - // Detect if this is a live stream - var isLiveStream = chapter.Type == "SCHEDULED_LIVESTREAM" || urn.Contains("livestream", StringComparison.OrdinalIgnoreCase); - // Create media source using proxy URL - enables DirectPlay! var mediaSource = new MediaSourceInfo { @@ -214,10 +220,11 @@ public class SRFMediaProvider : IMediaSourceProvider RunTimeTicks = chapter.Duration > 0 ? TimeSpan.FromMilliseconds(chapter.Duration).Ticks : null, VideoType = VideoType.VideoFile, IsInfiniteStream = isLiveStream, // True for live streams! - RequiresOpening = false, + RequiresOpening = true, // Enable to handle transcoding sessions RequiresClosing = false, SupportsProbing = false, // Disable probing for proxy URLs ReadAtNativeFramerate = isLiveStream, // Read at native framerate for live streams + OpenToken = openToken, // Token to identify this media source MediaStreams = new List { new MediaBrowser.Model.Entities.MediaStream @@ -281,10 +288,27 @@ public class SRFMediaProvider : IMediaSourceProvider } /// - public Task OpenMediaSource(string openToken, List currentLiveStreams, CancellationToken cancellationToken) + public async Task OpenMediaSource(string openToken, List 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(); + _logger.LogInformation("OpenMediaSource called with openToken: {OpenToken}", openToken); + + // Look up the original item ID from the open token + if (!_openTokenToItemId.TryGetValue(openToken, out var originalItemId)) + { + _logger.LogError("Open token {OpenToken} not found in registry", openToken); + throw new InvalidOperationException($"Open token {openToken} not found"); + } + + _logger.LogInformation("Open token {OpenToken} maps to original item ID: {ItemId}", openToken, originalItemId); + + // Create a live stream wrapper + var liveStream = new SRFLiveStream( + _logger, + _proxyService, + originalItemId, + openToken, + _loggerFactory); + + return await Task.FromResult(liveStream).ConfigureAwait(false); } } diff --git a/Jellyfin.Plugin.SRFPlay/Services/StreamProxyService.cs b/Jellyfin.Plugin.SRFPlay/Services/StreamProxyService.cs index 45d9a27..94f00e7 100644 --- a/Jellyfin.Plugin.SRFPlay/Services/StreamProxyService.cs +++ b/Jellyfin.Plugin.SRFPlay/Services/StreamProxyService.cs @@ -100,12 +100,17 @@ public class StreamProxyService : IDisposable /// The authenticated URL, or null if not found or expired. public string? GetAuthenticatedUrl(string itemId) { + _logger.LogInformation("GetAuthenticatedUrl called for itemId: {ItemId}", itemId); + // Try direct lookup first if (_streamMappings.TryGetValue(itemId, out var streamInfo)) { + _logger.LogInformation("✅ Found stream by direct lookup for itemId: {ItemId}", itemId); return ValidateAndReturnStream(itemId, streamInfo); } + _logger.LogWarning("❌ No direct match for itemId: {ItemId}, trying fallbacks... (Registered streams: {Count})", itemId, _streamMappings.Count); + // Fallback: Try to find by GUID variations (with/without dashes) // This handles cases where Jellyfin uses different GUID formats var normalizedId = NormalizeGuid(itemId); @@ -403,17 +408,14 @@ public class StreamProxyService : IDisposable var baseUri = new Uri(originalBaseUrl); var baseUrl = $"{baseUri.Scheme}://{baseUri.Host}{string.Join('/', baseUri.AbsolutePath.Split('/')[..^1])}"; - // Extract itemId query parameter from proxyBaseUrl to propagate it - var itemIdParam = string.Empty; - var queryMarker = "?itemId="; - if (proxyBaseUrl.Contains(queryMarker, StringComparison.Ordinal)) + // Extract query parameters from proxyBaseUrl to propagate them + var queryParams = string.Empty; + var queryStart = proxyBaseUrl.IndexOf('?', StringComparison.Ordinal); + if (queryStart >= 0) { - var queryStart = proxyBaseUrl.IndexOf(queryMarker, StringComparison.Ordinal); - if (queryStart >= 0) - { - itemIdParam = proxyBaseUrl[queryStart..]; - proxyBaseUrl = proxyBaseUrl[..queryStart]; // Remove query from base URL - } + queryParams = proxyBaseUrl[queryStart..]; + proxyBaseUrl = proxyBaseUrl[..queryStart]; // Remove query from base URL + _logger.LogDebug("Extracted query parameters from proxy URL: {QueryParams}", queryParams); } // Pattern to match .m3u8 and .ts/.mp4 segment references @@ -429,11 +431,11 @@ public class StreamProxyService : IDisposable { // Rewrite absolute URLs to proxy var relativePath = url.Replace(baseUrl + "/", string.Empty, StringComparison.Ordinal); - return $"\n{proxyBaseUrl}/{relativePath}{itemIdParam}"; + return $"\n{proxyBaseUrl}/{relativePath}{queryParams}"; } // Relative URL - rewrite to proxy - return $"\n{proxyBaseUrl}/{url}{itemIdParam}"; + return $"\n{proxyBaseUrl}/{url}{queryParams}"; }); return rewritten;