working livestreams!
🏗️ Build Plugin / build (push) Successful in 3m27s
🧪 Test Plugin / test (push) Successful in 1m38s
🚀 Release Plugin / build-and-release (push) Successful in 3m29s

This commit is contained in:
2025-11-22 14:14:43 +01:00
parent b8ac466c90
commit 89a911b9c4
5 changed files with 268 additions and 54 deletions
@@ -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
}
}
/// <summary>
/// Gets the correct scheme for proxy URLs (https if public URL is configured with https).
/// </summary>
/// <returns>The scheme to use (http or https).</returns>
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;
}
/// <summary>
/// Resolves the actual item ID from the request.
/// </summary>
@@ -178,16 +219,24 @@ public class StreamProxyController : ControllerBase
/// <returns>The resolved item ID.</returns>
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
/// <returns>The rewritten manifest.</returns>
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}");
}
}