Compare commits

...

2 Commits

Author SHA1 Message Date
4f9ebe2bce enable query and provide stream assumed stream info if missing.
All checks were successful
🏗️ Build Plugin / build (push) Successful in 2m38s
🧪 Test Plugin / test (push) Successful in 1m14s
2025-12-06 18:08:34 +01:00
14b6c33542 Proxy images to fix broken meta-data 2025-12-06 17:34:17 +01:00
4 changed files with 231 additions and 67 deletions

View File

@ -489,18 +489,21 @@ public class SRFPlayChannel : IChannel, IHasCacheKey
var overview = chapter.Description ?? chapter.Lead; var overview = chapter.Description ?? chapter.Lead;
// Get image URL - prefer chapter image, fall back to show image if available // Get image URL - prefer chapter image, fall back to show image if available
var imageUrl = chapter.ImageUrl; var originalImageUrl = chapter.ImageUrl;
if (string.IsNullOrEmpty(imageUrl) && mediaComposition.Show != null) if (string.IsNullOrEmpty(originalImageUrl) && mediaComposition.Show != null)
{ {
imageUrl = mediaComposition.Show.ImageUrl; originalImageUrl = mediaComposition.Show.ImageUrl;
_logger.LogDebug("URN {Urn}: Using show image as fallback", urn); _logger.LogDebug("URN {Urn}: Using show image as fallback", urn);
} }
if (string.IsNullOrEmpty(imageUrl)) if (string.IsNullOrEmpty(originalImageUrl))
{ {
_logger.LogWarning("URN {Urn}: No image URL available for '{Title}'", urn, chapter.Title); _logger.LogWarning("URN {Urn}: No image URL available for '{Title}'", urn, chapter.Title);
} }
// Proxy image URL to fix Content-Type headers from SRF CDN
var imageUrl = CreateProxiedImageUrl(originalImageUrl, serverUrl);
// Use ValidFrom for premiere date if this is a scheduled livestream, otherwise use Date // Use ValidFrom for premiere date if this is a scheduled livestream, otherwise use Date
var premiereDate = chapter.Type == "SCHEDULED_LIVESTREAM" ? chapter.ValidFrom?.ToUniversalTime() : chapter.Date?.ToUniversalTime(); var premiereDate = chapter.Type == "SCHEDULED_LIVESTREAM" ? chapter.ValidFrom?.ToUniversalTime() : chapter.Date?.ToUniversalTime();
@ -617,6 +620,24 @@ public class SRFPlayChannel : IChannel, IHasCacheKey
} }
#pragma warning restore CA5351 #pragma warning restore CA5351
/// <summary>
/// Creates a proxy URL for an image to fix Content-Type headers from SRF CDN.
/// </summary>
/// <param name="originalUrl">The original SRF image URL.</param>
/// <param name="serverUrl">The server base URL.</param>
/// <returns>The proxied image URL, or null if original is null.</returns>
private static string? CreateProxiedImageUrl(string? originalUrl, string serverUrl)
{
if (string.IsNullOrEmpty(originalUrl))
{
return null;
}
// Encode the original URL as base64 for safe transport
var encodedUrl = Convert.ToBase64String(Encoding.UTF8.GetBytes(originalUrl));
return $"{serverUrl}/Plugins/SRFPlay/Proxy/Image?url={encodedUrl}";
}
/// <inheritdoc /> /// <inheritdoc />
public bool IsEnabledFor(string userId) public bool IsEnabledFor(string userId)
{ {

View File

@ -1,8 +1,11 @@
using System; using System;
using System.Globalization; using System.Globalization;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Jellyfin.Plugin.SRFPlay.Services.Interfaces; using Jellyfin.Plugin.SRFPlay.Services.Interfaces;
using MediaBrowser.Common.Net;
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
@ -19,16 +22,22 @@ public class StreamProxyController : ControllerBase
{ {
private readonly ILogger<StreamProxyController> _logger; private readonly ILogger<StreamProxyController> _logger;
private readonly IStreamProxyService _proxyService; private readonly IStreamProxyService _proxyService;
private readonly IHttpClientFactory _httpClientFactory;
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="StreamProxyController"/> class. /// Initializes a new instance of the <see cref="StreamProxyController"/> class.
/// </summary> /// </summary>
/// <param name="logger">The logger.</param> /// <param name="logger">The logger.</param>
/// <param name="proxyService">The proxy service.</param> /// <param name="proxyService">The proxy service.</param>
public StreamProxyController(ILogger<StreamProxyController> logger, IStreamProxyService proxyService) /// <param name="httpClientFactory">The HTTP client factory.</param>
public StreamProxyController(
ILogger<StreamProxyController> logger,
IStreamProxyService proxyService,
IHttpClientFactory httpClientFactory)
{ {
_logger = logger; _logger = logger;
_proxyService = proxyService; _proxyService = proxyService;
_httpClientFactory = httpClientFactory;
} }
/// <summary> /// <summary>
@ -316,4 +325,117 @@ public class StreamProxyController : ControllerBase
return "application/octet-stream"; return "application/octet-stream";
} }
/// <summary>
/// Proxies image requests from SRF CDN, fixing Content-Type headers.
/// </summary>
/// <param name="url">The original image URL (base64 encoded).</param>
/// <param name="cancellationToken">Cancellation token.</param>
/// <returns>The image data with correct Content-Type.</returns>
[HttpGet("Image")]
[AllowAnonymous]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public async Task<IActionResult> GetImage(
[FromQuery] string url,
CancellationToken cancellationToken)
{
if (string.IsNullOrEmpty(url))
{
return BadRequest("URL parameter is required");
}
string decodedUrl;
try
{
// Decode base64 URL
var bytes = Convert.FromBase64String(url);
decodedUrl = System.Text.Encoding.UTF8.GetString(bytes);
}
catch (FormatException)
{
_logger.LogWarning("Invalid base64 URL parameter: {Url}", url);
return BadRequest("Invalid URL encoding");
}
_logger.LogDebug("Proxying image from: {Url}", decodedUrl);
try
{
var httpClient = _httpClientFactory.CreateClient(NamedClient.Default);
// Create request with proper headers
var request = new HttpRequestMessage(HttpMethod.Get, new Uri(decodedUrl));
request.Headers.UserAgent.ParseAdd(
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36");
request.Headers.Accept.ParseAdd("image/jpeg, image/png, image/webp, image/*;q=0.8");
var response = await httpClient.SendAsync(
request,
HttpCompletionOption.ResponseHeadersRead,
cancellationToken).ConfigureAwait(false);
if (!response.IsSuccessStatusCode)
{
_logger.LogWarning(
"Image fetch failed with status {StatusCode} for {Url}",
response.StatusCode,
decodedUrl);
return NotFound();
}
var imageData = await response.Content.ReadAsByteArrayAsync(cancellationToken)
.ConfigureAwait(false);
// Determine correct content type
var contentType = GetImageContentType(decodedUrl);
_logger.LogDebug(
"Returning proxied image ({Length} bytes, {ContentType})",
imageData.Length,
contentType);
return File(imageData, contentType);
}
catch (HttpRequestException ex)
{
_logger.LogError(ex, "Error fetching image from {Url}", decodedUrl);
return StatusCode(StatusCodes.Status502BadGateway);
}
}
/// <summary>
/// Gets the content type for an image based on URL or file extension.
/// </summary>
/// <param name="url">The image URL.</param>
/// <returns>The MIME content type.</returns>
private static string GetImageContentType(string url)
{
if (string.IsNullOrEmpty(url))
{
return "image/jpeg";
}
var uri = new Uri(url);
var path = uri.AbsolutePath.ToLowerInvariant();
if (path.EndsWith(".png", StringComparison.Ordinal))
{
return "image/png";
}
if (path.EndsWith(".gif", StringComparison.Ordinal))
{
return "image/gif";
}
if (path.EndsWith(".webp", StringComparison.Ordinal))
{
return "image/webp";
}
// Default to JPEG for SRF images (most common)
return "image/jpeg";
}
} }

View File

@ -156,26 +156,46 @@ public class SRFImageProvider : IRemoteImageProvider, IHasOrder
/// <inheritdoc /> /// <inheritdoc />
public async Task<HttpResponseMessage> GetImageResponse(string url, CancellationToken cancellationToken) public async Task<HttpResponseMessage> GetImageResponse(string url, CancellationToken cancellationToken)
{ {
_logger.LogDebug("Fetching image from URL: {Url}", url);
var httpClient = _httpClientFactory.CreateClient(NamedClient.Default); var httpClient = _httpClientFactory.CreateClient(NamedClient.Default);
// Create request with proper headers - SRF CDN requires User-Agent // Create request with proper headers - SRF CDN requires User-Agent
var request = new HttpRequestMessage(HttpMethod.Get, new Uri(url)); var request = new HttpRequestMessage(HttpMethod.Get, new Uri(url));
request.Headers.UserAgent.ParseAdd("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"); request.Headers.UserAgent.ParseAdd("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36");
request.Headers.Accept.ParseAdd("image/*"); request.Headers.Accept.ParseAdd("image/jpeg, image/png, image/webp, image/*;q=0.8");
var response = await httpClient.SendAsync(request, cancellationToken).ConfigureAwait(false); var response = await httpClient.SendAsync(request, cancellationToken).ConfigureAwait(false);
// Fix Content-Type if it's binary/octet-stream - SRF CDN returns wrong content type var originalContentType = response.Content.Headers.ContentType?.MediaType;
_logger.LogDebug(
"Image response status: {StatusCode}, Content-Type: {ContentType}, Content-Length: {Length}",
response.StatusCode,
originalContentType ?? "null",
response.Content.Headers.ContentLength);
// Fix Content-Type if it's not a proper image type - SRF CDN often returns wrong content type
// Jellyfin needs correct Content-Type to process images // Jellyfin needs correct Content-Type to process images
if (response.IsSuccessStatusCode && if (response.IsSuccessStatusCode)
response.Content.Headers.ContentType?.MediaType == "binary/octet-stream")
{ {
// Determine correct content type from URL extension var needsContentTypeFix = string.IsNullOrEmpty(originalContentType) ||
var contentType = GetContentTypeFromUrl(url); originalContentType == "binary/octet-stream" ||
if (!string.IsNullOrEmpty(contentType)) originalContentType == "application/octet-stream" ||
!originalContentType.StartsWith("image/", StringComparison.OrdinalIgnoreCase);
if (needsContentTypeFix)
{ {
_logger.LogDebug("Fixing Content-Type from binary/octet-stream to {ContentType} for {Url}", contentType, url); // Determine correct content type from URL extension or default to JPEG
response.Content.Headers.ContentType = new MediaTypeHeaderValue(contentType); var contentType = GetContentTypeFromUrl(url);
if (!string.IsNullOrEmpty(contentType))
{
_logger.LogInformation(
"Fixing Content-Type from '{OriginalType}' to '{NewType}' for {Url}",
originalContentType ?? "null",
contentType,
url);
response.Content.Headers.ContentType = new MediaTypeHeaderValue(contentType);
}
} }
} }

View File

@ -1,7 +1,5 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Jellyfin.Plugin.SRFPlay.Configuration; using Jellyfin.Plugin.SRFPlay.Configuration;
@ -22,12 +20,10 @@ namespace Jellyfin.Plugin.SRFPlay.Providers;
public class SRFMediaProvider : IMediaSourceProvider public class SRFMediaProvider : IMediaSourceProvider
{ {
private readonly ILogger<SRFMediaProvider> _logger; private readonly ILogger<SRFMediaProvider> _logger;
private readonly ILoggerFactory _loggerFactory;
private readonly IMediaCompositionFetcher _compositionFetcher; private readonly IMediaCompositionFetcher _compositionFetcher;
private readonly IStreamUrlResolver _streamResolver; private readonly IStreamUrlResolver _streamResolver;
private readonly IStreamProxyService _proxyService; private readonly IStreamProxyService _proxyService;
private readonly IServerApplicationHost _appHost; private readonly IServerApplicationHost _appHost;
private readonly Dictionary<string, string> _openTokenToItemId = new();
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="SRFMediaProvider"/> class. /// Initializes a new instance of the <see cref="SRFMediaProvider"/> class.
@ -44,7 +40,6 @@ public class SRFMediaProvider : IMediaSourceProvider
IStreamProxyService proxyService, IStreamProxyService proxyService,
IServerApplicationHost appHost) IServerApplicationHost appHost)
{ {
_loggerFactory = loggerFactory;
_logger = loggerFactory.CreateLogger<SRFMediaProvider>(); _logger = loggerFactory.CreateLogger<SRFMediaProvider>();
_compositionFetcher = compositionFetcher; _compositionFetcher = compositionFetcher;
_streamResolver = streamResolver; _streamResolver = streamResolver;
@ -172,14 +167,8 @@ public class SRFMediaProvider : IMediaSourceProvider
? config.PublicServerUrl.TrimEnd('/') // Use configured public URL (important for Android/remote clients) ? config.PublicServerUrl.TrimEnd('/') // Use configured public URL (important for Android/remote clients)
: _appHost.GetSmartApiUrl(string.Empty); // Fall back to Jellyfin's smart URL resolution : _appHost.GetSmartApiUrl(string.Empty); // Fall back to Jellyfin's smart URL resolution
// Generate an open token for this media source (used to track transcoding sessions) // Create proxy URL - item ID is all we need since proxy handles auth
var openToken = Guid.NewGuid().ToString("N"); var proxyUrl = $"{serverUrl}/Plugins/SRFPlay/Proxy/{itemIdStr}/master.m3u8";
_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( _logger.LogInformation(
"Using proxy URL for item {ItemId}: {ProxyUrl} (PublicServerUrl configured: {IsPublicConfigured})", "Using proxy URL for item {ItemId}: {ProxyUrl} (PublicServerUrl configured: {IsPublicConfigured})",
@ -197,36 +186,17 @@ public class SRFMediaProvider : IMediaSourceProvider
Container = "hls", Container = "hls",
SupportsDirectStream = true, SupportsDirectStream = true,
SupportsDirectPlay = true, // ✅ Enabled! Proxy handles auth SupportsDirectPlay = true, // ✅ Enabled! Proxy handles auth
SupportsTranscoding = true, SupportsTranscoding = false, // Prefer DirectPlay - no transcoding needed for HLS
IsRemote = false, // False because it's a local proxy endpoint IsRemote = false, // False because it's a local proxy endpoint
Type = MediaSourceType.Default, Type = MediaSourceType.Default,
RunTimeTicks = chapter.Duration > 0 ? TimeSpan.FromMilliseconds(chapter.Duration).Ticks : null, RunTimeTicks = chapter.Duration > 0 ? TimeSpan.FromMilliseconds(chapter.Duration).Ticks : null,
VideoType = VideoType.VideoFile, VideoType = VideoType.VideoFile,
IsInfiniteStream = isLiveStream, // True for live streams! IsInfiniteStream = isLiveStream, // True for live streams!
RequiresOpening = true, // Enable to handle transcoding sessions RequiresOpening = false, // Proxy handles auth - no need for OpenMediaSource
RequiresClosing = false, RequiresClosing = false,
SupportsProbing = false, // Disable probing for proxy URLs SupportsProbing = true, // Enable probing so Jellyfin can verify stream compatibility
ReadAtNativeFramerate = isLiveStream, // Read at native framerate for live streams ReadAtNativeFramerate = isLiveStream, // Read at native framerate for live streams
OpenToken = openToken, // Token to identify this media source MediaStreams = CreateMediaStreams(qualityPref)
MediaStreams = new List<MediaBrowser.Model.Entities.MediaStream>
{
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); sources.Add(mediaSource);
@ -271,26 +241,57 @@ public class SRFMediaProvider : IMediaSourceProvider
} }
/// <inheritdoc /> /// <inheritdoc />
public async Task<ILiveStream> OpenMediaSource(string openToken, List<ILiveStream> currentLiveStreams, CancellationToken cancellationToken) public Task<ILiveStream> OpenMediaSource(string openToken, List<ILiveStream> currentLiveStreams, CancellationToken cancellationToken)
{ {
_logger.LogInformation("OpenMediaSource called with openToken: {OpenToken}", openToken); // Not used - RequiresOpening is false, proxy handles authentication directly
_logger.LogWarning("OpenMediaSource unexpectedly called with openToken: {OpenToken}", openToken);
throw new NotSupportedException("OpenMediaSource not supported - streams use direct proxy access");
}
// Look up the original item ID from the open token /// <summary>
if (!_openTokenToItemId.TryGetValue(openToken, out var originalItemId)) /// Creates MediaStream metadata based on quality preference.
/// These are approximate values - HLS adaptive streaming will use actual stream qualities.
/// </summary>
private static List<MediaBrowser.Model.Entities.MediaStream> CreateMediaStreams(QualityPreference quality)
{
// Set resolution/bitrate based on quality preference
// These are typical values for SRF streams - actual HLS will adapt
var (width, height, videoBitrate) = quality switch
{ {
_logger.LogError("Open token {OpenToken} not found in registry", openToken); QualityPreference.SD => (1280, 720, 2500000),
throw new InvalidOperationException($"Open token {openToken} not found"); QualityPreference.HD => (1920, 1080, 5000000),
} _ => (1280, 720, 3000000)
};
_logger.LogInformation("Open token {OpenToken} maps to original item ID: {ItemId}", openToken, originalItemId); return new List<MediaBrowser.Model.Entities.MediaStream>
{
// Create a live stream wrapper new MediaBrowser.Model.Entities.MediaStream
var liveStream = new SRFLiveStream( {
_logger, Type = MediaStreamType.Video,
_proxyService, Codec = "h264",
originalItemId, Profile = "high",
openToken); Level = 40,
Width = width,
return await Task.FromResult<ILiveStream>(liveStream).ConfigureAwait(false); Height = height,
BitRate = videoBitrate,
BitDepth = 8,
IsInterlaced = false,
IsDefault = true,
Index = 0,
IsAVC = true,
PixelFormat = "yuv420p"
},
new MediaBrowser.Model.Entities.MediaStream
{
Type = MediaStreamType.Audio,
Codec = "aac",
Profile = "LC",
Channels = 2,
SampleRate = 48000,
BitRate = 128000,
IsDefault = true,
Index = 1
}
};
} }
} }