Compare commits
2 Commits
a0e7663323
...
4f9ebe2bce
| Author | SHA1 | Date | |
|---|---|---|---|
| 4f9ebe2bce | |||
| 14b6c33542 |
@ -489,18 +489,21 @@ public class SRFPlayChannel : IChannel, IHasCacheKey
|
||||
var overview = chapter.Description ?? chapter.Lead;
|
||||
|
||||
// Get image URL - prefer chapter image, fall back to show image if available
|
||||
var imageUrl = chapter.ImageUrl;
|
||||
if (string.IsNullOrEmpty(imageUrl) && mediaComposition.Show != null)
|
||||
var originalImageUrl = chapter.ImageUrl;
|
||||
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);
|
||||
}
|
||||
|
||||
if (string.IsNullOrEmpty(imageUrl))
|
||||
if (string.IsNullOrEmpty(originalImageUrl))
|
||||
{
|
||||
_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
|
||||
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
|
||||
|
||||
/// <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 />
|
||||
public bool IsEnabledFor(string userId)
|
||||
{
|
||||
|
||||
@ -1,8 +1,11 @@
|
||||
using System;
|
||||
using System.Globalization;
|
||||
using System.Net.Http;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Jellyfin.Plugin.SRFPlay.Services.Interfaces;
|
||||
using MediaBrowser.Common.Net;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
@ -19,16 +22,22 @@ public class StreamProxyController : ControllerBase
|
||||
{
|
||||
private readonly ILogger<StreamProxyController> _logger;
|
||||
private readonly IStreamProxyService _proxyService;
|
||||
private readonly IHttpClientFactory _httpClientFactory;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="StreamProxyController"/> class.
|
||||
/// </summary>
|
||||
/// <param name="logger">The logger.</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;
|
||||
_proxyService = proxyService;
|
||||
_httpClientFactory = httpClientFactory;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -316,4 +325,117 @@ public class StreamProxyController : ControllerBase
|
||||
|
||||
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";
|
||||
}
|
||||
}
|
||||
|
||||
@ -156,28 +156,48 @@ public class SRFImageProvider : IRemoteImageProvider, IHasOrder
|
||||
/// <inheritdoc />
|
||||
public async Task<HttpResponseMessage> GetImageResponse(string url, CancellationToken cancellationToken)
|
||||
{
|
||||
_logger.LogDebug("Fetching image from URL: {Url}", url);
|
||||
|
||||
var httpClient = _httpClientFactory.CreateClient(NamedClient.Default);
|
||||
|
||||
// Create request with proper headers - SRF CDN requires User-Agent
|
||||
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.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);
|
||||
|
||||
// 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
|
||||
if (response.IsSuccessStatusCode &&
|
||||
response.Content.Headers.ContentType?.MediaType == "binary/octet-stream")
|
||||
if (response.IsSuccessStatusCode)
|
||||
{
|
||||
// Determine correct content type from URL extension
|
||||
var needsContentTypeFix = string.IsNullOrEmpty(originalContentType) ||
|
||||
originalContentType == "binary/octet-stream" ||
|
||||
originalContentType == "application/octet-stream" ||
|
||||
!originalContentType.StartsWith("image/", StringComparison.OrdinalIgnoreCase);
|
||||
|
||||
if (needsContentTypeFix)
|
||||
{
|
||||
// Determine correct content type from URL extension or default to JPEG
|
||||
var contentType = GetContentTypeFromUrl(url);
|
||||
if (!string.IsNullOrEmpty(contentType))
|
||||
{
|
||||
_logger.LogDebug("Fixing Content-Type from binary/octet-stream to {ContentType} for {Url}", contentType, url);
|
||||
_logger.LogInformation(
|
||||
"Fixing Content-Type from '{OriginalType}' to '{NewType}' for {Url}",
|
||||
originalContentType ?? "null",
|
||||
contentType,
|
||||
url);
|
||||
response.Content.Headers.ContentType = new MediaTypeHeaderValue(contentType);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
@ -1,7 +1,5 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Jellyfin.Plugin.SRFPlay.Configuration;
|
||||
@ -22,12 +20,10 @@ namespace Jellyfin.Plugin.SRFPlay.Providers;
|
||||
public class SRFMediaProvider : IMediaSourceProvider
|
||||
{
|
||||
private readonly ILogger<SRFMediaProvider> _logger;
|
||||
private readonly ILoggerFactory _loggerFactory;
|
||||
private readonly IMediaCompositionFetcher _compositionFetcher;
|
||||
private readonly IStreamUrlResolver _streamResolver;
|
||||
private readonly IStreamProxyService _proxyService;
|
||||
private readonly IServerApplicationHost _appHost;
|
||||
private readonly Dictionary<string, string> _openTokenToItemId = new();
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="SRFMediaProvider"/> class.
|
||||
@ -44,7 +40,6 @@ public class SRFMediaProvider : IMediaSourceProvider
|
||||
IStreamProxyService proxyService,
|
||||
IServerApplicationHost appHost)
|
||||
{
|
||||
_loggerFactory = loggerFactory;
|
||||
_logger = loggerFactory.CreateLogger<SRFMediaProvider>();
|
||||
_compositionFetcher = compositionFetcher;
|
||||
_streamResolver = streamResolver;
|
||||
@ -172,14 +167,8 @@ 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
|
||||
|
||||
// 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}";
|
||||
// Create proxy URL - item ID is all we need since proxy handles auth
|
||||
var proxyUrl = $"{serverUrl}/Plugins/SRFPlay/Proxy/{itemIdStr}/master.m3u8";
|
||||
|
||||
_logger.LogInformation(
|
||||
"Using proxy URL for item {ItemId}: {ProxyUrl} (PublicServerUrl configured: {IsPublicConfigured})",
|
||||
@ -197,36 +186,17 @@ public class SRFMediaProvider : IMediaSourceProvider
|
||||
Container = "hls",
|
||||
SupportsDirectStream = true,
|
||||
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
|
||||
Type = MediaSourceType.Default,
|
||||
RunTimeTicks = chapter.Duration > 0 ? TimeSpan.FromMilliseconds(chapter.Duration).Ticks : null,
|
||||
VideoType = VideoType.VideoFile,
|
||||
IsInfiniteStream = isLiveStream, // True for live streams!
|
||||
RequiresOpening = true, // Enable to handle transcoding sessions
|
||||
RequiresOpening = false, // Proxy handles auth - no need for OpenMediaSource
|
||||
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
|
||||
OpenToken = openToken, // Token to identify this media source
|
||||
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
|
||||
}
|
||||
}
|
||||
MediaStreams = CreateMediaStreams(qualityPref)
|
||||
};
|
||||
|
||||
sources.Add(mediaSource);
|
||||
@ -271,26 +241,57 @@ public class SRFMediaProvider : IMediaSourceProvider
|
||||
}
|
||||
|
||||
/// <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);
|
||||
|
||||
// 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");
|
||||
// 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");
|
||||
}
|
||||
|
||||
_logger.LogInformation("Open token {OpenToken} maps to original item ID: {ItemId}", openToken, originalItemId);
|
||||
/// <summary>
|
||||
/// 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
|
||||
{
|
||||
QualityPreference.SD => (1280, 720, 2500000),
|
||||
QualityPreference.HD => (1920, 1080, 5000000),
|
||||
_ => (1280, 720, 3000000)
|
||||
};
|
||||
|
||||
// Create a live stream wrapper
|
||||
var liveStream = new SRFLiveStream(
|
||||
_logger,
|
||||
_proxyService,
|
||||
originalItemId,
|
||||
openToken);
|
||||
|
||||
return await Task.FromResult<ILiveStream>(liveStream).ConfigureAwait(false);
|
||||
return new List<MediaBrowser.Model.Entities.MediaStream>
|
||||
{
|
||||
new MediaBrowser.Model.Entities.MediaStream
|
||||
{
|
||||
Type = MediaStreamType.Video,
|
||||
Codec = "h264",
|
||||
Profile = "high",
|
||||
Level = 40,
|
||||
Width = width,
|
||||
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
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user